code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _UpperCAmelCase ( lowercase_ , unittest.TestCase ): UpperCamelCase = KandinskyVaaPipeline UpperCamelCase = [ '''image_embeds''', '''negative_image_embeds''', ] UpperCamelCase = ['''image_embeds''', '''negative_image_embeds'''] UpperCamelCase = [ '''generator''', '''height''', '''width''', '''latents''', '''guidance_scale''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] UpperCamelCase = False @property def lowerCamelCase ( self :Any ): return 32 @property def lowerCamelCase ( self :int ): return 32 @property def lowerCamelCase ( self :Dict ): return self.time_input_dim @property def lowerCamelCase ( self :Union[str, Any] ): return self.time_input_dim * 4 @property def lowerCamelCase ( self :List[Any] ): return 1_00 @property def lowerCamelCase ( self :List[Any] ): torch.manual_seed(0 ) A = { "in_channels": 4, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } A = UNetaDConditionModel(**__UpperCamelCase ) return model @property def lowerCamelCase ( self :List[str] ): return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def lowerCamelCase ( self :Any ): torch.manual_seed(0 ) A = VQModel(**self.dummy_movq_kwargs ) return model def lowerCamelCase ( self :Dict ): A = self.dummy_unet A = self.dummy_movq A = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=__UpperCamelCase , set_alpha_to_one=__UpperCamelCase , steps_offset=1 , prediction_type="epsilon" , thresholding=__UpperCamelCase , ) A = { "unet": unet, "scheduler": scheduler, "movq": movq, } return components def lowerCamelCase ( self :str , __UpperCamelCase :Tuple , __UpperCamelCase :List[str]=0 ): A = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) A = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( __UpperCamelCase ) if str(__UpperCamelCase ).startswith("mps" ): A = torch.manual_seed(__UpperCamelCase ) else: A = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) A = { "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 64, "width": 64, "guidance_scale": 4.0, "num_inference_steps": 2, "output_type": "np", } return inputs def lowerCamelCase ( self :Dict ): A = "cpu" A = self.get_dummy_components() A = self.pipeline_class(**__UpperCamelCase ) A = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) A = pipe(**self.get_dummy_inputs(__UpperCamelCase ) ) A = output.images A = pipe( **self.get_dummy_inputs(__UpperCamelCase ) , return_dict=__UpperCamelCase , )[0] A = image[0, -3:, -3:, -1] A = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) A = np.array( [0.6_237_976, 1.0, 0.36_441_332, 1.0, 0.70_639_634, 0.29_877_186, 0.85_652_125, 0.5_216_843, 0.54_454_046] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self :str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCamelCase ( self :List[str] ): A = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy" ) A = KandinskyVaaPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-prior" , torch_dtype=torch.floataa ) pipe_prior.to(__UpperCamelCase ) A = KandinskyVaaPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-decoder" , torch_dtype=torch.floataa ) A = pipeline.to(__UpperCamelCase ) pipeline.set_progress_bar_config(disable=__UpperCamelCase ) A = "red cat, 4k photo" A = torch.Generator(device="cuda" ).manual_seed(0 ) A, A = pipe_prior( __UpperCamelCase , generator=__UpperCamelCase , num_inference_steps=5 , negative_prompt="" , ).to_tuple() A = torch.Generator(device="cuda" ).manual_seed(0 ) A = pipeline( image_embeds=__UpperCamelCase , negative_image_embeds=__UpperCamelCase , generator=__UpperCamelCase , num_inference_steps=1_00 , output_type="np" , ) A = output.images[0] assert image.shape == (5_12, 5_12, 3) assert_mean_pixel_difference(__UpperCamelCase , __UpperCamelCase )
292
"""simple docstring""" import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class _UpperCAmelCase : def __init__( self :List[Any] , __UpperCamelCase :Tuple , __UpperCamelCase :List[str]=13 , __UpperCamelCase :Any=30 , __UpperCamelCase :int=2 , __UpperCamelCase :Union[str, Any]=3 , __UpperCamelCase :Union[str, Any]=True , __UpperCamelCase :Optional[int]=True , __UpperCamelCase :List[str]=32 , __UpperCamelCase :List[Any]=5 , __UpperCamelCase :Dict=4 , __UpperCamelCase :List[str]=37 , __UpperCamelCase :str="gelu" , __UpperCamelCase :Union[str, Any]=0.1 , __UpperCamelCase :List[Any]=0.1 , __UpperCamelCase :Tuple=10 , __UpperCamelCase :Tuple=0.02 , __UpperCamelCase :int=None , ): A = parent A = batch_size A = image_size A = patch_size A = num_channels A = is_training A = use_labels A = hidden_size A = num_hidden_layers A = num_attention_heads A = intermediate_size A = hidden_act A = hidden_dropout_prob A = attention_probs_dropout_prob A = type_sequence_label_size A = initializer_range A = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A = (image_size // patch_size) ** 2 A = num_patches + 1 def lowerCamelCase ( self :Any ): A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A = None if self.use_labels: A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A = self.get_config() return config, pixel_values, labels def lowerCamelCase ( self :Union[str, Any] ): return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def lowerCamelCase ( self :Dict , __UpperCamelCase :Dict , __UpperCamelCase :Any , __UpperCamelCase :Any ): A = ViTMSNModel(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() A = model(__UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase ( self :Optional[int] , __UpperCamelCase :List[str] , __UpperCamelCase :Union[str, Any] , __UpperCamelCase :Optional[Any] ): A = self.type_sequence_label_size A = ViTMSNForImageClassification(__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() A = model(__UpperCamelCase , labels=__UpperCamelCase ) print("Pixel and labels shape: {pixel_values.shape}, {labels.shape}" ) print("Labels: {labels}" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A = 1 A = ViTMSNForImageClassification(__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() A = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A = model(__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowerCamelCase ( self :Optional[Any] ): A = self.prepare_config_and_inputs() A, A, A = config_and_inputs A = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class _UpperCAmelCase ( lowercase_ , lowercase_ , unittest.TestCase ): UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () UpperCamelCase = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def lowerCamelCase ( self :Optional[int] ): A = ViTMSNModelTester(self ) A = ConfigTester(self , config_class=__UpperCamelCase , has_text_modality=__UpperCamelCase , hidden_size=37 ) def lowerCamelCase ( self :Any ): self.config_tester.run_common_tests() @unittest.skip(reason="ViTMSN does not use inputs_embeds" ) def lowerCamelCase ( self :Union[str, Any] ): pass def lowerCamelCase ( self :int ): A, A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A = model_class(__UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__UpperCamelCase , nn.Linear ) ) def lowerCamelCase ( self :Tuple ): A, A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A = model_class(__UpperCamelCase ) A = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A = [*signature.parameters.keys()] A = ["pixel_values"] self.assertListEqual(arg_names[:1] , __UpperCamelCase ) def lowerCamelCase ( self :List[str] ): A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCamelCase ) def lowerCamelCase ( self :Dict ): A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase ) @slow def lowerCamelCase ( self :List[Any] ): for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A = ViTMSNModel.from_pretrained(__UpperCamelCase ) self.assertIsNotNone(__UpperCamelCase ) def A__ ( ): A = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class _UpperCAmelCase ( unittest.TestCase ): @cached_property def lowerCamelCase ( self :Union[str, Any] ): return ViTImageProcessor.from_pretrained("facebook/vit-msn-small" ) if is_vision_available() else None @slow def lowerCamelCase ( self :Any ): torch.manual_seed(2 ) A = ViTMSNForImageClassification.from_pretrained("facebook/vit-msn-small" ).to(__UpperCamelCase ) A = self.default_image_processor A = prepare_img() A = image_processor(images=__UpperCamelCase , return_tensors="pt" ).to(__UpperCamelCase ) # forward pass with torch.no_grad(): A = model(**__UpperCamelCase ) # verify the logits A = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , __UpperCamelCase ) A = torch.tensor([-0.0_803, -0.4_454, -0.2_375] ).to(__UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , __UpperCamelCase , atol=1e-4 ) )
292
1
import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class lowerCamelCase__ ( __lowercase): '''simple docstring''' def _lowerCamelCase ( self :Union[str, Any] ) -> List[str]: __UpperCamelCase : List[str] = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(a , "tf_padding" ) ) self.parent.assertTrue(hasattr(a , "depth_multiplier" ) ) class lowerCamelCase__ : '''simple docstring''' def __init__( self :Tuple , a :List[str] , a :int=1_3 , a :Union[str, Any]=3 , a :Dict=3_2 , a :Union[str, Any]=0.25 , a :Dict=8 , a :Tuple=8 , a :Optional[Any]=6 , a :str=3_2 , a :Any=True , a :int=True , a :str=True , a :List[Any]="relu6" , a :Optional[int]=1_2_8_0 , a :int=0.1 , a :Tuple=0.02 , a :Any=True , a :Tuple=True , a :Any=1_0 , a :List[Any]=None , ) -> Union[str, Any]: __UpperCamelCase : int = parent __UpperCamelCase : Any = batch_size __UpperCamelCase : Union[str, Any] = num_channels __UpperCamelCase : Optional[int] = image_size __UpperCamelCase : int = depth_multiplier __UpperCamelCase : Optional[int] = depth_divisible_by __UpperCamelCase : str = min_depth __UpperCamelCase : Optional[int] = expand_ratio __UpperCamelCase : str = tf_padding __UpperCamelCase : Tuple = output_stride __UpperCamelCase : List[str] = first_layer_is_expansion __UpperCamelCase : Dict = finegrained_output __UpperCamelCase : List[str] = hidden_act __UpperCamelCase : List[str] = last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier ) __UpperCamelCase : Optional[Any] = classifier_dropout_prob __UpperCamelCase : Tuple = use_labels __UpperCamelCase : Optional[Any] = is_training __UpperCamelCase : Dict = num_labels __UpperCamelCase : str = initializer_range __UpperCamelCase : List[Any] = scope def _lowerCamelCase ( self :Any ) -> List[Any]: __UpperCamelCase : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __UpperCamelCase : Dict = None __UpperCamelCase : Dict = None if self.use_labels: __UpperCamelCase : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) __UpperCamelCase : Optional[int] = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) __UpperCamelCase : List[Any] = self.get_config() return config, pixel_values, labels, pixel_labels def _lowerCamelCase ( self :str ) -> Tuple: return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , depth_divisible_by=self.depth_divisible_by , min_depth=self.min_depth , expand_ratio=self.expand_ratio , output_stride=self.output_stride , first_layer_is_expansion=self.first_layer_is_expansion , finegrained_output=self.finegrained_output , hidden_act=self.hidden_act , tf_padding=self.tf_padding , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def _lowerCamelCase ( self :Union[str, Any] , a :Optional[Any] , a :Optional[int] , a :List[Any] , a :Union[str, Any] ) -> str: __UpperCamelCase : str = MobileNetVaModel(config=a ) model.to(a ) model.eval() __UpperCamelCase : Tuple = model(a ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) self.parent.assertEqual( result.pooler_output.shape , (self.batch_size, self.last_hidden_size) , ) def _lowerCamelCase ( self :List[str] , a :Optional[int] , a :Union[str, Any] , a :Optional[int] , a :Tuple ) -> Optional[int]: __UpperCamelCase : List[str] = self.num_labels __UpperCamelCase : Any = MobileNetVaForImageClassification(a ) model.to(a ) model.eval() __UpperCamelCase : Tuple = model(a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowerCamelCase ( self :int , a :List[Any] , a :str , a :Optional[Any] , a :Any ) -> Dict: __UpperCamelCase : Optional[int] = self.num_labels __UpperCamelCase : Optional[Any] = MobileNetVaForSemanticSegmentation(a ) model.to(a ) model.eval() __UpperCamelCase : Optional[Any] = model(a ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) __UpperCamelCase : Optional[int] = model(a , labels=a ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def _lowerCamelCase ( self :List[Any] ) -> Dict: __UpperCamelCase : Union[str, Any] = self.prepare_config_and_inputs() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : Any = config_and_inputs __UpperCamelCase : int = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase__ ( __lowercase , __lowercase , unittest.TestCase): '''simple docstring''' _A = ( (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation) if is_torch_available() else () ) _A = ( { 'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification, 'image-segmentation': MobileNetVaForSemanticSegmentation, } if is_torch_available() else {} ) _A = False _A = False _A = False _A = False def _lowerCamelCase ( self :Tuple ) -> List[Any]: __UpperCamelCase : Tuple = MobileNetVaModelTester(self ) __UpperCamelCase : Tuple = MobileNetVaConfigTester(self , config_class=a , has_text_modality=a ) def _lowerCamelCase ( self :Optional[int] ) -> Optional[int]: self.config_tester.run_common_tests() @unittest.skip(reason="MobileNetV2 does not use inputs_embeds" ) def _lowerCamelCase ( self :int ) -> Tuple: pass @unittest.skip(reason="MobileNetV2 does not support input and output embeddings" ) def _lowerCamelCase ( self :Optional[Any] ) -> str: pass @unittest.skip(reason="MobileNetV2 does not output attentions" ) def _lowerCamelCase ( self :Union[str, Any] ) -> List[Any]: pass def _lowerCamelCase ( self :int ) -> List[str]: __UpperCamelCase , __UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCamelCase : int = model_class(a ) __UpperCamelCase : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __UpperCamelCase : List[Any] = [*signature.parameters.keys()] __UpperCamelCase : str = ["pixel_values"] self.assertListEqual(arg_names[:1] , a ) def _lowerCamelCase ( self :Tuple ) -> int: __UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*a ) def _lowerCamelCase ( self :Tuple ) -> int: def check_hidden_states_output(a :Tuple , a :int , a :int ): __UpperCamelCase : Optional[int] = model_class(a ) model.to(a ) model.eval() with torch.no_grad(): __UpperCamelCase : Union[str, Any] = model(**self._prepare_for_class(a , a ) ) __UpperCamelCase : Tuple = outputs.hidden_states __UpperCamelCase : Optional[int] = 1_6 self.assertEqual(len(a ) , a ) __UpperCamelCase , __UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCamelCase : Optional[int] = True check_hidden_states_output(a , a , a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __UpperCamelCase : Tuple = True check_hidden_states_output(a , a , a ) def _lowerCamelCase ( self :Any ) -> List[Any]: __UpperCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*a ) def _lowerCamelCase ( self :List[Any] ) -> Tuple: __UpperCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*a ) @slow def _lowerCamelCase ( self :str ) -> Union[str, Any]: for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase : Any = MobileNetVaModel.from_pretrained(a ) self.assertIsNotNone(a ) def _SCREAMING_SNAKE_CASE ( ) -> int: '''simple docstring''' __UpperCamelCase : List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") return image @require_torch @require_vision class lowerCamelCase__ ( unittest.TestCase): '''simple docstring''' @cached_property def _lowerCamelCase ( self :Union[str, Any] ) -> Dict: return ( MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224" ) if is_vision_available() else None ) @slow def _lowerCamelCase ( self :Union[str, Any] ) -> Union[str, Any]: __UpperCamelCase : Dict = MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224" ).to(a ) __UpperCamelCase : Union[str, Any] = self.default_image_processor __UpperCamelCase : int = prepare_img() __UpperCamelCase : Tuple = image_processor(images=a , return_tensors="pt" ).to(a ) # forward pass with torch.no_grad(): __UpperCamelCase : Union[str, Any] = model(**a ) # verify the logits __UpperCamelCase : Tuple = torch.Size((1, 1_0_0_1) ) self.assertEqual(outputs.logits.shape , a ) __UpperCamelCase : List[Any] = torch.tensor([0.2445, -1.1993, 0.1905] ).to(a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , a , atol=1E-4 ) ) @slow def _lowerCamelCase ( self :Optional[Any] ) -> Optional[Any]: __UpperCamelCase : Any = MobileNetVaForSemanticSegmentation.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513" ) __UpperCamelCase : str = model.to(a ) __UpperCamelCase : List[Any] = MobileNetVaImageProcessor.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513" ) __UpperCamelCase : Dict = prepare_img() __UpperCamelCase : Any = image_processor(images=a , return_tensors="pt" ).to(a ) # forward pass with torch.no_grad(): __UpperCamelCase : Dict = model(**a ) __UpperCamelCase : List[str] = outputs.logits # verify the logits __UpperCamelCase : Dict = torch.Size((1, 2_1, 6_5, 6_5) ) self.assertEqual(logits.shape , a ) __UpperCamelCase : Union[str, Any] = torch.tensor( [ [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]], [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]], [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]], ] , device=a , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , a , atol=1E-4 ) )
151
import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin lowercase : Optional[int] = get_tests_dir('fixtures/spiece.model') @require_sentencepiece @require_tokenizers class lowerCamelCase__ ( __lowercase , unittest.TestCase): '''simple docstring''' _A = DebertaVaTokenizer _A = DebertaVaTokenizerFast _A = True _A = True def _lowerCamelCase ( self :int ) -> int: super().setUp() # We have a SentencePiece fixture for testing __UpperCamelCase : Any = DebertaVaTokenizer(a , unk_token="<unk>" ) tokenizer.save_pretrained(self.tmpdirname ) def _lowerCamelCase ( self :Optional[int] , a :List[str] ) -> List[str]: __UpperCamelCase : Any = "this is a test" __UpperCamelCase : Optional[int] = "this is a test" return input_text, output_text def _lowerCamelCase ( self :str ) -> Any: __UpperCamelCase : Optional[Any] = "<pad>" __UpperCamelCase : Union[str, Any] = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a ) , a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a ) , a ) def _lowerCamelCase ( self :Union[str, Any] ) -> Tuple: __UpperCamelCase : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<pad>" ) self.assertEqual(vocab_keys[1] , "<unk>" ) self.assertEqual(vocab_keys[-1] , "[PAD]" ) self.assertEqual(len(a ) , 3_0_0_0_1 ) def _lowerCamelCase ( self :Union[str, Any] ) -> Optional[Any]: self.assertEqual(self.get_tokenizer().vocab_size , 3_0_0_0_0 ) def _lowerCamelCase ( self :List[Any] ) -> str: # fmt: off __UpperCamelCase : int = " \tHeLLo!how \n Are yoU? " __UpperCamelCase : Optional[int] = ["▁hello", "!", "how", "▁are", "▁you", "?"] # fmt: on __UpperCamelCase : Dict = DebertaVaTokenizer(a , do_lower_case=a ) __UpperCamelCase : int = tokenizer.convert_ids_to_tokens(tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) __UpperCamelCase : List[Any] = DebertaVaTokenizerFast(a , do_lower_case=a ) __UpperCamelCase : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def _lowerCamelCase ( self :Dict ) -> Optional[Any]: pass @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def _lowerCamelCase ( self :str ) -> Any: pass def _lowerCamelCase ( self :Tuple ) -> Dict: # fmt: off __UpperCamelCase : Optional[int] = "I was born in 92000, and this is falsé." __UpperCamelCase : Optional[int] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on __UpperCamelCase : Dict = DebertaVaTokenizer(a , split_by_punct=a ) __UpperCamelCase : List[Any] = tokenizer.convert_ids_to_tokens(tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) __UpperCamelCase : Optional[Any] = DebertaVaTokenizerFast(a , split_by_punct=a ) __UpperCamelCase : List[Any] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) def _lowerCamelCase ( self :List[Any] ) -> str: # fmt: off __UpperCamelCase : Dict = "I was born in 92000, and this is falsé." __UpperCamelCase : Any = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on __UpperCamelCase : Any = DebertaVaTokenizer(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : Optional[int] = tokenizer.convert_ids_to_tokens(tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) __UpperCamelCase : Dict = DebertaVaTokenizerFast(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : List[str] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) def _lowerCamelCase ( self :Dict ) -> Any: # fmt: off __UpperCamelCase : Optional[int] = "I was born in 92000, and this is falsé." __UpperCamelCase : Tuple = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on __UpperCamelCase : Optional[int] = DebertaVaTokenizer(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : str = tokenizer.convert_ids_to_tokens(tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) __UpperCamelCase : List[Any] = DebertaVaTokenizerFast(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) def _lowerCamelCase ( self :List[str] ) -> Tuple: # fmt: off __UpperCamelCase : Dict = "I was born in 92000, and this is falsé." __UpperCamelCase : List[str] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on __UpperCamelCase : List[str] = DebertaVaTokenizer(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : int = tokenizer.convert_ids_to_tokens(tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) __UpperCamelCase : List[str] = DebertaVaTokenizerFast(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : Optional[int] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) def _lowerCamelCase ( self :Union[str, Any] ) -> Any: # fmt: off __UpperCamelCase : Optional[int] = " \tHeLLo!how \n Are yoU? " __UpperCamelCase : str = ["▁", "<unk>", "e", "<unk>", "o", "!", "how", "▁", "<unk>", "re", "▁yo", "<unk>", "?"] # fmt: on __UpperCamelCase : int = DebertaVaTokenizer(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : Union[str, Any] = tokenizer.convert_ids_to_tokens(tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) __UpperCamelCase : Tuple = DebertaVaTokenizerFast(a , do_lower_case=a , split_by_punct=a ) __UpperCamelCase : int = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) def _lowerCamelCase ( self :int ) -> Any: __UpperCamelCase : Tuple = self.get_tokenizer() __UpperCamelCase : List[Any] = self.get_rust_tokenizer() __UpperCamelCase : Dict = "I was born in 92000, and this is falsé." __UpperCamelCase : List[str] = tokenizer.convert_ids_to_tokens(tokenizer.encode(a , add_special_tokens=a ) ) __UpperCamelCase : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(a , add_special_tokens=a ) ) self.assertListEqual(a , a ) __UpperCamelCase : str = tokenizer.encode(a , add_special_tokens=a ) __UpperCamelCase : Union[str, Any] = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) __UpperCamelCase : Optional[int] = self.get_rust_tokenizer() __UpperCamelCase : List[Any] = tokenizer.encode(a ) __UpperCamelCase : Union[str, Any] = rust_tokenizer.encode(a ) self.assertListEqual(a , a ) def _lowerCamelCase ( self :List[Any] ) -> List[str]: __UpperCamelCase : Optional[int] = "This is a test" __UpperCamelCase : List[Any] = [1_3, 1, 4_3_9_8, 2_5, 2_1, 1_2_8_9] __UpperCamelCase : Tuple = ["▁", "T", "his", "▁is", "▁a", "▁test"] __UpperCamelCase : Union[str, Any] = ["▁", "<unk>", "his", "▁is", "▁a", "▁test"] __UpperCamelCase : Union[str, Any] = DebertaVaTokenizer(a , keep_accents=a ) __UpperCamelCase : int = DebertaVaTokenizerFast(a , keep_accents=a ) __UpperCamelCase : Tuple = tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) __UpperCamelCase : List[str] = tokenizer.tokenize(a ) self.assertListEqual(a , a ) __UpperCamelCase : List[Any] = tokenizer.convert_ids_to_tokens(a ) self.assertListEqual(a , a ) __UpperCamelCase : List[Any] = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) __UpperCamelCase : Optional[Any] = rust_tokenizer.tokenize(a ) self.assertListEqual(a , a ) __UpperCamelCase : Optional[Any] = rust_tokenizer.convert_ids_to_tokens(a ) self.assertListEqual(a , a ) # fmt: off __UpperCamelCase : Optional[int] = "I was born in 92000, and this is falsé." __UpperCamelCase : int = [1_3, 1, 2_3, 3_8_6, 1_9, 5_6_1, 3_0_5_0, 1_5, 1_7, 4_8, 2_5, 8_2_5_6, 1_8, 1, 9] __UpperCamelCase : Optional[int] = ["▁", "I", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", ".", ] __UpperCamelCase : Union[str, Any] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on __UpperCamelCase : List[str] = tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) __UpperCamelCase : Dict = tokenizer.tokenize(a ) self.assertListEqual(a , a ) __UpperCamelCase : Optional[int] = tokenizer.convert_ids_to_tokens(a ) self.assertListEqual(a , a ) __UpperCamelCase : Dict = rust_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) __UpperCamelCase : int = rust_tokenizer.tokenize(a ) self.assertListEqual(a , a ) __UpperCamelCase : Optional[int] = rust_tokenizer.convert_ids_to_tokens(a ) self.assertListEqual(a , a ) def _lowerCamelCase ( self :Union[str, Any] ) -> str: __UpperCamelCase : List[Any] = DebertaVaTokenizer(a ) __UpperCamelCase : Optional[int] = tokenizer.encode("sequence builders" ) __UpperCamelCase : Optional[int] = tokenizer.encode("multi-sequence build" ) __UpperCamelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(a ) __UpperCamelCase : Optional[int] = tokenizer.build_inputs_with_special_tokens(a , a ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] , a ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] , a , ) @slow def _lowerCamelCase ( self :Dict ) -> int: # fmt: off __UpperCamelCase : Dict = {"input_ids": [[1, 3_9_8_6_7, 3_6, 1_9_3_9_0, 4_8_6, 2_7, 3_5_0_5_2, 8_1_4_3_6, 1_8, 6_0_6_8_5, 1_2_2_5, 7, 3_5_0_5_2, 8_1_4_3_6, 1_8, 9_3_6_7, 1_6_8_9_9, 1_8, 1_5_9_3_7, 5_3, 5_9_4, 7_7_3, 1_8, 1_6_2_8_7, 3_0_4_6_5, 3_6, 1_5_9_3_7, 6, 4_1_1_3_9, 3_8, 3_6_9_7_9, 6_0_7_6_3, 1_9_1, 6, 3_4_1_3_2, 9_9, 6, 5_0_5_3_8, 3_9_0, 4_3_2_3_0, 6, 3_4_1_3_2, 2_7_7_9, 2_0_8_5_0, 1_4, 6_9_9, 1_0_7_2, 1_1_9_4, 3_6, 3_8_2, 1_0_9_0_1, 5_3, 7, 6_9_9, 1_0_7_2, 2_0_8_4, 3_6, 2_0_4_2_2, 6_3_0, 5_3, 1_9, 1_0_5, 3_0_4_9, 1_8_9_6, 1_0_5_3, 1_6_8_9_9, 1_5_0_6, 1_1, 3_7_9_7_8, 4_2_4_3, 7, 1_2_3_7, 3_1_8_6_9, 2_0_0, 1_6_5_6_6, 6_5_4, 6, 3_5_0_5_2, 8_1_4_3_6, 7, 5_5_6_3_0, 1_3_5_9_3, 4, 2], [1, 2_6, 1_5_0_1_1, 1_3, 6_6_7, 8, 1_0_5_3, 1_8, 2_3_6_1_1, 1_2_3_7, 7_2_3_5_6, 1_2_8_2_0, 3_4, 1_0_4_1_3_4, 1_2_0_9, 3_5, 1_3_3_1_3, 6_6_2_7, 2_1, 2_0_2, 3_4_7, 7, 1_6_4, 2_3_9_9, 1_1, 4_6, 4_4_8_5, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 1_2_3_2, 2_8_6_4, 1_5_7_8_5, 1_4_9_5_1, 1_0_5, 5, 8_5_8_1, 1_2_5_0, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "token_type_ids": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=a , model_name="microsoft/deberta-v2-xlarge" , revision="ad6e42c1532ddf3a15c39246b63f5559d558b670" , )
151
1
"""simple docstring""" def _A ( UpperCamelCase_ : int) -> bool: '''simple docstring''' return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number if __name__ == "__main__": print('Program to check whether a number is a Perfect number or not...') _a = int(input('Enter number: ').strip()) print(F"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
17
"""simple docstring""" import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def _A ( UpperCamelCase_ : Union[str, Any], UpperCamelCase_ : Union[str, Any], UpperCamelCase_ : List[str]) -> Optional[int]: '''simple docstring''' if isinstance(UpperCamelCase_, torch.Tensor): return image elif isinstance(UpperCamelCase_, PIL.Image.Image): __lowercase = [image] if isinstance(image[0], PIL.Image.Image): __lowercase = [np.array(i.resize((w, h), resample=PIL_INTERPOLATION["lanczos"]))[None, :] for i in image] __lowercase = np.concatenate(UpperCamelCase_, axis=0) __lowercase = np.array(UpperCamelCase_).astype(np.floataa) / 255.0 __lowercase = image.transpose(0, 3, 1, 2) __lowercase = 2.0 * image - 1.0 __lowercase = torch.from_numpy(UpperCamelCase_) elif isinstance(image[0], torch.Tensor): __lowercase = torch.cat(UpperCamelCase_, dim=0) return image def _A ( UpperCamelCase_ : Dict, UpperCamelCase_ : str, UpperCamelCase_ : Union[str, Any], UpperCamelCase_ : List[Any]=0.9_995) -> int: '''simple docstring''' if not isinstance(UpperCamelCase_, np.ndarray): __lowercase = True __lowercase = va.device __lowercase = va.cpu().numpy() __lowercase = va.cpu().numpy() __lowercase = np.sum(va * va / (np.linalg.norm(UpperCamelCase_) * np.linalg.norm(UpperCamelCase_))) if np.abs(UpperCamelCase_) > DOT_THRESHOLD: __lowercase = (1 - t) * va + t * va else: __lowercase = np.arccos(UpperCamelCase_) __lowercase = np.sin(UpperCamelCase_) __lowercase = theta_a * t __lowercase = np.sin(UpperCamelCase_) __lowercase = np.sin(theta_a - theta_t) / sin_theta_a __lowercase = sin_theta_t / sin_theta_a __lowercase = sa * va + sa * va if inputs_are_torch: __lowercase = torch.from_numpy(UpperCamelCase_).to(UpperCamelCase_) return va def _A ( UpperCamelCase_ : List[str], UpperCamelCase_ : Union[str, Any]) -> int: '''simple docstring''' __lowercase = F.normalize(UpperCamelCase_, dim=-1) __lowercase = F.normalize(UpperCamelCase_, dim=-1) return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2) def _A ( UpperCamelCase_ : Optional[int], UpperCamelCase_ : str) -> Optional[int]: '''simple docstring''' for param in model.parameters(): __lowercase = value class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : Dict, UpperCAmelCase__ : AutoencoderKL, UpperCAmelCase__ : CLIPTextModel, UpperCAmelCase__ : CLIPModel, UpperCAmelCase__ : CLIPTokenizer, UpperCAmelCase__ : UNetaDConditionModel, UpperCAmelCase__ : Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler], UpperCAmelCase__ : CLIPFeatureExtractor, UpperCAmelCase__ : Union[str, Any]=None, UpperCAmelCase__ : List[str]=None, UpperCAmelCase__ : Any=None, ): super().__init__() self.register_modules( vae=UpperCAmelCase__, text_encoder=UpperCAmelCase__, clip_model=UpperCAmelCase__, tokenizer=UpperCAmelCase__, unet=UpperCAmelCase__, scheduler=UpperCAmelCase__, feature_extractor=UpperCAmelCase__, coca_model=UpperCAmelCase__, coca_tokenizer=UpperCAmelCase__, coca_transform=UpperCAmelCase__, ) __lowercase = ( feature_extractor.size if isinstance(feature_extractor.size, UpperCAmelCase__ ) else feature_extractor.size["shortest_edge"] ) __lowercase = transforms.Normalize(mean=feature_extractor.image_mean, std=feature_extractor.image_std ) set_requires_grad(self.text_encoder, UpperCAmelCase__ ) set_requires_grad(self.clip_model, UpperCAmelCase__ ) def _lowercase ( self : Tuple, UpperCAmelCase__ : Optional[Union[str, int]] = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory __lowercase = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(UpperCAmelCase__ ) def _lowercase ( self : int ): self.enable_attention_slicing(UpperCAmelCase__ ) def _lowercase ( self : str ): set_requires_grad(self.vae, UpperCAmelCase__ ) def _lowercase ( self : Any ): set_requires_grad(self.vae, UpperCAmelCase__ ) def _lowercase ( self : Union[str, Any] ): set_requires_grad(self.unet, UpperCAmelCase__ ) def _lowercase ( self : Any ): set_requires_grad(self.unet, UpperCAmelCase__ ) def _lowercase ( self : List[str], UpperCAmelCase__ : Dict, UpperCAmelCase__ : Any, UpperCAmelCase__ : Optional[Any] ): # get the original timestep using init_timestep __lowercase = min(int(num_inference_steps * strength ), UpperCAmelCase__ ) __lowercase = max(num_inference_steps - init_timestep, 0 ) __lowercase = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def _lowercase ( self : List[str], UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : Optional[Any], UpperCAmelCase__ : Dict, UpperCAmelCase__ : Any, UpperCAmelCase__ : int=None ): if not isinstance(UpperCAmelCase__, torch.Tensor ): raise ValueError(F"""`image` has to be of type `torch.Tensor` but is {type(UpperCAmelCase__ )}""" ) __lowercase = image.to(device=UpperCAmelCase__, dtype=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(UpperCAmelCase__ ) ] __lowercase = torch.cat(UpperCAmelCase__, dim=0 ) else: __lowercase = self.vae.encode(UpperCAmelCase__ ).latent_dist.sample(UpperCAmelCase__ ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowercase = 0.18_215 * init_latents __lowercase = init_latents.repeat_interleave(UpperCAmelCase__, dim=0 ) __lowercase = randn_tensor(init_latents.shape, generator=UpperCAmelCase__, device=UpperCAmelCase__, dtype=UpperCAmelCase__ ) # get latents __lowercase = self.scheduler.add_noise(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) __lowercase = init_latents return latents def _lowercase ( self : Optional[int], UpperCAmelCase__ : Dict ): __lowercase = self.coca_transform(UpperCAmelCase__ ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): __lowercase = self.coca_model.generate(transformed_image.to(device=self.device, dtype=self.coca_model.dtype ) ) __lowercase = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split("<end_of_text>" )[0].replace("<start_of_text>", "" ).rstrip(" .," ) def _lowercase ( self : Tuple, UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : Tuple ): __lowercase = self.feature_extractor.preprocess(UpperCAmelCase__ ) __lowercase = torch.from_numpy(clip_image_input["pixel_values"][0] ).unsqueeze(0 ).to(self.device ).half() __lowercase = self.clip_model.get_image_features(UpperCAmelCase__ ) __lowercase = image_embeddings_clip / image_embeddings_clip.norm(p=2, dim=-1, keepdim=UpperCAmelCase__ ) __lowercase = image_embeddings_clip.repeat_interleave(UpperCAmelCase__, dim=0 ) return image_embeddings_clip @torch.enable_grad() def _lowercase ( self : str, UpperCAmelCase__ : List[Any], UpperCAmelCase__ : Dict, UpperCAmelCase__ : List[str], UpperCAmelCase__ : Dict, UpperCAmelCase__ : List[str], UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : Optional[int], ): __lowercase = latents.detach().requires_grad_() __lowercase = self.scheduler.scale_model_input(UpperCAmelCase__, UpperCAmelCase__ ) # predict the noise residual __lowercase = self.unet(UpperCAmelCase__, UpperCAmelCase__, encoder_hidden_states=UpperCAmelCase__ ).sample if isinstance(self.scheduler, (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): __lowercase = self.scheduler.alphas_cumprod[timestep] __lowercase = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf __lowercase = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 __lowercase = torch.sqrt(UpperCAmelCase__ ) __lowercase = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler, UpperCAmelCase__ ): __lowercase = self.scheduler.sigmas[index] __lowercase = latents - sigma * noise_pred else: raise ValueError(F"""scheduler type {type(self.scheduler )} not supported""" ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowercase = 1 / 0.18_215 * sample __lowercase = self.vae.decode(UpperCAmelCase__ ).sample __lowercase = (image / 2 + 0.5).clamp(0, 1 ) __lowercase = transforms.Resize(self.feature_extractor_size )(UpperCAmelCase__ ) __lowercase = self.normalize(UpperCAmelCase__ ).to(latents.dtype ) __lowercase = self.clip_model.get_image_features(UpperCAmelCase__ ) __lowercase = image_embeddings_clip / image_embeddings_clip.norm(p=2, dim=-1, keepdim=UpperCAmelCase__ ) __lowercase = spherical_dist_loss(UpperCAmelCase__, UpperCAmelCase__ ).mean() * clip_guidance_scale __lowercase = -torch.autograd.grad(UpperCAmelCase__, UpperCAmelCase__ )[0] if isinstance(self.scheduler, UpperCAmelCase__ ): __lowercase = latents.detach() + grads * (sigma**2) __lowercase = noise_pred_original else: __lowercase = noise_pred_original - torch.sqrt(UpperCAmelCase__ ) * grads return noise_pred, latents @torch.no_grad() def __call__( self : str, UpperCAmelCase__ : Union[torch.FloatTensor, PIL.Image.Image], UpperCAmelCase__ : Union[torch.FloatTensor, PIL.Image.Image], UpperCAmelCase__ : Optional[str] = None, UpperCAmelCase__ : Optional[str] = None, UpperCAmelCase__ : Optional[int] = 5_1_2, UpperCAmelCase__ : Optional[int] = 5_1_2, UpperCAmelCase__ : float = 0.6, UpperCAmelCase__ : Optional[int] = 5_0, UpperCAmelCase__ : Optional[float] = 7.5, UpperCAmelCase__ : Optional[int] = 1, UpperCAmelCase__ : float = 0.0, UpperCAmelCase__ : Optional[float] = 1_0_0, UpperCAmelCase__ : Optional[torch.Generator] = None, UpperCAmelCase__ : Optional[str] = "pil", UpperCAmelCase__ : bool = True, UpperCAmelCase__ : float = 0.8, UpperCAmelCase__ : float = 0.1, UpperCAmelCase__ : float = 0.1, ): if isinstance(UpperCAmelCase__, UpperCAmelCase__ ) and len(UpperCAmelCase__ ) != batch_size: raise ValueError(F"""You have passed {batch_size} batch_size, but only {len(UpperCAmelCase__ )} generators.""" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""" ) if isinstance(UpperCAmelCase__, torch.Generator ) and batch_size > 1: __lowercase = [generator] + [None] * (batch_size - 1) __lowercase = [ ("model", self.coca_model is None), ("tokenizer", self.coca_tokenizer is None), ("transform", self.coca_transform is None), ] __lowercase = [x[0] for x in coca_is_none if x[1]] __lowercase = ", ".join(UpperCAmelCase__ ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(UpperCAmelCase__ ): raise ValueError( F"""Content prompt is None and CoCa [{coca_is_none_str}] is None.""" F"""Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.""" ) __lowercase = self.get_image_description(UpperCAmelCase__ ) if style_prompt is None: if len(UpperCAmelCase__ ): raise ValueError( F"""Style prompt is None and CoCa [{coca_is_none_str}] is None.""" F""" Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.""" ) __lowercase = self.get_image_description(UpperCAmelCase__ ) # get prompt text embeddings for content and style __lowercase = self.tokenizer( UpperCAmelCase__, padding="max_length", max_length=self.tokenizer.model_max_length, truncation=UpperCAmelCase__, return_tensors="pt", ) __lowercase = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] __lowercase = self.tokenizer( UpperCAmelCase__, padding="max_length", max_length=self.tokenizer.model_max_length, truncation=UpperCAmelCase__, return_tensors="pt", ) __lowercase = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] __lowercase = slerp(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) # duplicate text embeddings for each generation per prompt __lowercase = text_embeddings.repeat_interleave(UpperCAmelCase__, dim=0 ) # set timesteps __lowercase = "offset" in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) __lowercase = {} if accepts_offset: __lowercase = 1 self.scheduler.set_timesteps(UpperCAmelCase__, **UpperCAmelCase__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) __lowercase ,__lowercase = self.get_timesteps(UpperCAmelCase__, UpperCAmelCase__, self.device ) __lowercase = timesteps[:1].repeat(UpperCAmelCase__ ) # Preprocess image __lowercase = preprocess(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) __lowercase = self.prepare_latents( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, text_embeddings.dtype, self.device, UpperCAmelCase__ ) __lowercase = preprocess(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) __lowercase = self.prepare_latents( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, text_embeddings.dtype, self.device, UpperCAmelCase__ ) __lowercase = slerp(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) if clip_guidance_scale > 0: __lowercase = self.get_clip_image_embeddings(UpperCAmelCase__, UpperCAmelCase__ ) __lowercase = self.get_clip_image_embeddings(UpperCAmelCase__, UpperCAmelCase__ ) __lowercase = slerp( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. __lowercase = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: __lowercase = content_text_input.input_ids.shape[-1] __lowercase = self.tokenizer([""], padding="max_length", max_length=UpperCAmelCase__, return_tensors="pt" ) __lowercase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt __lowercase = uncond_embeddings.repeat_interleave(UpperCAmelCase__, dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes __lowercase = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. __lowercase = (batch_size, self.unet.config.in_channels, height // 8, width // 8) __lowercase = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps __lowercase = torch.randn(UpperCAmelCase__, generator=UpperCAmelCase__, device="cpu", dtype=UpperCAmelCase__ ).to( self.device ) else: __lowercase = torch.randn(UpperCAmelCase__, generator=UpperCAmelCase__, device=self.device, dtype=UpperCAmelCase__ ) else: if latents.shape != latents_shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) __lowercase = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler __lowercase = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __lowercase = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __lowercase = {} if accepts_eta: __lowercase = eta # check if the scheduler accepts generator __lowercase = "generator" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: __lowercase = generator with self.progress_bar(total=UpperCAmelCase__ ): for i, t in enumerate(UpperCAmelCase__ ): # expand the latents if we are doing classifier free guidance __lowercase = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __lowercase = self.scheduler.scale_model_input(UpperCAmelCase__, UpperCAmelCase__ ) # predict the noise residual __lowercase = self.unet(UpperCAmelCase__, UpperCAmelCase__, encoder_hidden_states=UpperCAmelCase__ ).sample # perform classifier free guidance if do_classifier_free_guidance: __lowercase ,__lowercase = noise_pred.chunk(2 ) __lowercase = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: __lowercase = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) __lowercase ,__lowercase = self.cond_fn( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) # compute the previous noisy sample x_t -> x_t-1 __lowercase = self.scheduler.step(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, **UpperCAmelCase__ ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowercase = 1 / 0.18_215 * latents __lowercase = self.vae.decode(UpperCAmelCase__ ).sample __lowercase = (image / 2 + 0.5).clamp(0, 1 ) __lowercase = image.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": __lowercase = self.numpy_to_pil(UpperCAmelCase__ ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=UpperCAmelCase__, nsfw_content_detected=UpperCAmelCase__ )
17
1
from __future__ import annotations def __a ( lowerCAmelCase_ : list[float] ) -> bool: '''simple docstring''' if len(lowerCAmelCase_ ) < 2: raise ValueError("""Monogons and Digons are not polygons in the Euclidean space""" ) if any(i <= 0 for i in nums ): raise ValueError("""All values must be greater than 0""" ) UpperCAmelCase_= nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
277
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) __A = { '''configuration_clip''': [ '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPConfig''', '''CLIPOnnxConfig''', '''CLIPTextConfig''', '''CLIPVisionConfig''', ], '''processing_clip''': ['''CLIPProcessor'''], '''tokenization_clip''': ['''CLIPTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ['''CLIPTokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ['''CLIPFeatureExtractor'''] __A = ['''CLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPModel''', '''CLIPPreTrainedModel''', '''CLIPTextModel''', '''CLIPTextModelWithProjection''', '''CLIPVisionModel''', '''CLIPVisionModelWithProjection''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFCLIPModel''', '''TFCLIPPreTrainedModel''', '''TFCLIPTextModel''', '''TFCLIPVisionModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ '''FlaxCLIPModel''', '''FlaxCLIPPreTrainedModel''', '''FlaxCLIPTextModel''', '''FlaxCLIPTextPreTrainedModel''', '''FlaxCLIPVisionModel''', '''FlaxCLIPVisionPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys __A = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
277
1
import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def SCREAMING_SNAKE_CASE ( lowercase_ ) -> Dict: """simple docstring""" A__ = [ 'encoder.version', 'decoder.version', 'model.encoder.version', 'model.decoder.version', '_float_tensor', 'decoder.output_projection.weight', ] for k in ignore_keys: state_dict.pop(_UpperCAmelCase , _UpperCAmelCase ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> int: """simple docstring""" A__ = emb.weight.shape A__ = nn.Linear(_UpperCAmelCase , _UpperCAmelCase , bias=_UpperCAmelCase ) A__ = emb.weight.data return lin_layer def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_="facebook/mbart-large-en-ro" , lowercase_=False , lowercase_=False ) -> str: """simple docstring""" A__ = torch.load(_UpperCAmelCase , map_location='''cpu''' )['model'] remove_ignore_keys_(_UpperCAmelCase ) A__ = state_dict['encoder.embed_tokens.weight'].shape[0] A__ = MBartConfig.from_pretrained(_UpperCAmelCase , vocab_size=_UpperCAmelCase ) if mbart_aa and finetuned: A__ = 'relu' A__ = state_dict['decoder.embed_tokens.weight'] A__ = MBartForConditionalGeneration(_UpperCAmelCase ) model.model.load_state_dict(_UpperCAmelCase ) if finetuned: A__ = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": _lowerCamelCase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """fairseq_path""", type=str, help="""bart.large, bart.large.cnn or a path to a model.pt on local filesystem.""" ) parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--hf_config""", default="""facebook/mbart-large-cc25""", type=str, help="""Which huggingface architecture to use: mbart-large""", ) parser.add_argument("""--mbart_50""", action="""store_true""", help="""whether the model is mMART-50 checkpoint""") parser.add_argument("""--finetuned""", action="""store_true""", help="""whether the model is a fine-tuned checkpoint""") _lowerCamelCase : Any = parser.parse_args() _lowerCamelCase : Tuple = convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
14
"""simple docstring""" import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase_ : Optional[Any] = logging.get_logger(__name__) def UpperCAmelCase__ ( _UpperCAmelCase , _UpperCAmelCase=False ): """simple docstring""" A_ : Optional[Any] = [] # fmt: off # stem: rename_keys.append(('cls_token', 'vit.embeddings.cls_token') ) rename_keys.append(('pos_embed', 'vit.embeddings.position_embeddings') ) rename_keys.append(('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight') ) rename_keys.append(('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias') ) # backbone rename_keys.append(('patch_embed.backbone.stem.conv.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.bias', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias') ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias""") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"""blocks.{i}.norm1.weight""", f"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((f"""blocks.{i}.norm1.bias""", f"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((f"""blocks.{i}.attn.proj.weight""", f"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((f"""blocks.{i}.attn.proj.bias""", f"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((f"""blocks.{i}.norm2.weight""", f"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((f"""blocks.{i}.norm2.bias""", f"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((f"""blocks.{i}.mlp.fc1.weight""", f"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((f"""blocks.{i}.mlp.fc1.bias""", f"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((f"""blocks.{i}.mlp.fc2.weight""", f"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((f"""blocks.{i}.mlp.fc2.bias""", f"""vit.encoder.layer.{i}.output.dense.bias""") ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" A_ : List[str] = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) # fmt: on return rename_keys def UpperCAmelCase__ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: A_ : List[str] = '' else: A_ : Dict = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A_ : str = state_dict.pop(f"""blocks.{i}.attn.qkv.weight""" ) A_ : List[Any] = state_dict.pop(f"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A_ : List[Any] = in_proj_weight[ : config.hidden_size, : ] A_ : Tuple = in_proj_bias[: config.hidden_size] A_ : Union[str, Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A_ : Dict = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A_ : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] A_ : Tuple = in_proj_bias[-config.hidden_size :] def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" A_ : List[str] = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(_UpperCAmelCase , _UpperCAmelCase ) def UpperCAmelCase__ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): """simple docstring""" A_ : Any = dct.pop(_UpperCAmelCase ) A_ : Optional[int] = val def UpperCAmelCase__ ( ): """simple docstring""" A_ : Optional[int] = 'http://images.cocodataset.org/val2017/000000039769.jpg' A_ : int = Image.open(requests.get(_UpperCAmelCase , stream=_UpperCAmelCase ).raw ) return im @torch.no_grad() def UpperCAmelCase__ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): """simple docstring""" A_ : List[Any] = BitConfig( global_padding='same' , layer_type='bottleneck' , depths=(3, 4, 9) , out_features=['stage3'] , embedding_dynamic_padding=_UpperCAmelCase , ) A_ : Optional[int] = ViTHybridConfig(backbone_config=_UpperCAmelCase , image_size=384 , num_labels=1000 ) A_ : Union[str, Any] = False # load original model from timm A_ : List[Any] = timm.create_model(_UpperCAmelCase , pretrained=_UpperCAmelCase ) timm_model.eval() # load state_dict of original model, remove and rename some keys A_ : Tuple = timm_model.state_dict() if base_model: remove_classification_head_(_UpperCAmelCase ) A_ : Any = create_rename_keys(_UpperCAmelCase , _UpperCAmelCase ) for src, dest in rename_keys: rename_key(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) read_in_q_k_v(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) A_ : Union[str, Any] = 'huggingface/label-files' A_ : Dict = 'imagenet-1k-id2label.json' A_ : List[str] = json.load(open(hf_hub_download(_UpperCAmelCase , _UpperCAmelCase , repo_type='dataset' ) , 'r' ) ) A_ : str = {int(_UpperCAmelCase ): v for k, v in idalabel.items()} A_ : Any = idalabel A_ : Optional[int] = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": A_ : List[Any] = ViTHybridModel(_UpperCAmelCase ).eval() else: A_ : str = ViTHybridForImageClassification(_UpperCAmelCase ).eval() model.load_state_dict(_UpperCAmelCase ) # create image processor A_ : Dict = create_transform(**resolve_data_config({} , model=_UpperCAmelCase ) ) A_ : List[str] = transform.transforms A_ : List[str] = { 'bilinear': PILImageResampling.BILINEAR, 'bicubic': PILImageResampling.BICUBIC, 'nearest': PILImageResampling.NEAREST, } A_ : Tuple = ViTHybridImageProcessor( do_resize=_UpperCAmelCase , size={'shortest_edge': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_UpperCAmelCase , crop_size={'height': timm_transforms[1].size[0], 'width': timm_transforms[1].size[1]} , do_normalize=_UpperCAmelCase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) A_ : Optional[Any] = prepare_img() A_ : Any = transform(_UpperCAmelCase ).unsqueeze(0 ) A_ : Dict = processor(_UpperCAmelCase , return_tensors='pt' ).pixel_values # verify pixel values assert torch.allclose(_UpperCAmelCase , _UpperCAmelCase ) # verify logits with torch.no_grad(): A_ : List[Any] = model(_UpperCAmelCase ) A_ : List[str] = outputs.logits print('Predicted class:' , logits.argmax(-1 ).item() ) if base_model: A_ : Union[str, Any] = timm_model.forward_features(_UpperCAmelCase ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(_UpperCAmelCase , outputs.pooler_output , atol=1E-3 ) else: A_ : Tuple = timm_model(_UpperCAmelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_UpperCAmelCase , outputs.logits , atol=1E-3 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) print(f"""Saving model {vit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_UpperCAmelCase ) print(f"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(_UpperCAmelCase ) if push_to_hub: print(f"""Pushing model and processor to the hub {vit_name}""" ) model.push_to_hub(f"""ybelkada/{vit_name}""" ) processor.push_to_hub(f"""ybelkada/{vit_name}""" ) if __name__ == "__main__": lowerCamelCase_ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--vit_name', default='vit_base_r50_s16_384', type=str, help='Name of the hybrid ViT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to upload the model to the HuggingFace hub.' ) lowerCamelCase_ : List[str] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
286
0
def _UpperCamelCase ( snake_case__ ) -> int: __UpperCAmelCase : List[Any] = [1] __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Optional[Any] = 0, 0, 0 __UpperCAmelCase : Tuple = ugly_nums[ia] * 2 __UpperCAmelCase : Dict = ugly_nums[ia] * 3 __UpperCAmelCase : Optional[int] = ugly_nums[ia] * 5 for _ in range(1, snake_case__ ): __UpperCAmelCase : List[Any] = min(snake_case__, snake_case__, snake_case__ ) ugly_nums.append(snake_case__ ) if next_num == next_a: ia += 1 __UpperCAmelCase : List[Any] = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 __UpperCAmelCase : List[Any] = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 __UpperCAmelCase : Optional[int] = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(F'{ugly_numbers(200) = }')
342
from typing import Optional, Tuple import jax import jax.numpy as jnp from flax import linen as nn from flax.core.frozen_dict import FrozenDict from transformers import CLIPConfig, FlaxPreTrainedModel from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule def _UpperCamelCase ( snake_case__, snake_case__, snake_case__=1e-1_2 ) -> str: __UpperCAmelCase : Any = jnp.divide(emb_a.T, jnp.clip(jnp.linalg.norm(snake_case__, axis=1 ), a_min=snake_case__ ) ).T __UpperCAmelCase : int = jnp.divide(emb_a.T, jnp.clip(jnp.linalg.norm(snake_case__, axis=1 ), a_min=snake_case__ ) ).T return jnp.matmul(snake_case__, norm_emb_a.T ) class _snake_case ( nn.Module ): lowerCamelCase__: CLIPConfig lowerCamelCase__: jnp.dtype = jnp.floataa def _lowerCamelCase ( self: Any ) -> Tuple: __UpperCAmelCase : List[str] = FlaxCLIPVisionModule(self.config.vision_config ) __UpperCAmelCase : Any = nn.Dense(self.config.projection_dim , use_bias=__lowerCamelCase , dtype=self.dtype ) __UpperCAmelCase : int = self.param("concept_embeds" , jax.nn.initializers.ones , (17, self.config.projection_dim) ) __UpperCAmelCase : int = self.param( "special_care_embeds" , jax.nn.initializers.ones , (3, self.config.projection_dim) ) __UpperCAmelCase : Tuple = self.param("concept_embeds_weights" , jax.nn.initializers.ones , (17,) ) __UpperCAmelCase : str = self.param("special_care_embeds_weights" , jax.nn.initializers.ones , (3,) ) def __call__( self: List[Any] , __lowerCamelCase: Dict ) -> Dict: __UpperCAmelCase : Optional[int] = self.vision_model(__lowerCamelCase )[1] __UpperCAmelCase : List[str] = self.visual_projection(__lowerCamelCase ) __UpperCAmelCase : Optional[int] = jax_cosine_distance(__lowerCamelCase , self.special_care_embeds ) __UpperCAmelCase : Optional[Any] = jax_cosine_distance(__lowerCamelCase , self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs __UpperCAmelCase : List[str] = 0.0 __UpperCAmelCase : Tuple = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment __UpperCAmelCase : List[str] = jnp.round(__lowerCamelCase , 3 ) __UpperCAmelCase : Any = jnp.any(special_scores > 0 , axis=1 , keepdims=__lowerCamelCase ) # Use a lower threshold if an image has any special care concept __UpperCAmelCase : List[Any] = is_special_care * 0.01 __UpperCAmelCase : Any = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment __UpperCAmelCase : List[str] = jnp.round(__lowerCamelCase , 3 ) __UpperCAmelCase : Any = jnp.any(concept_scores > 0 , axis=1 ) return has_nsfw_concepts class _snake_case ( _lowercase ): lowerCamelCase__: int = CLIPConfig lowerCamelCase__: Tuple = "clip_input" lowerCamelCase__: str = FlaxStableDiffusionSafetyCheckerModule def __init__( self: Union[str, Any] , __lowerCamelCase: CLIPConfig , __lowerCamelCase: Optional[Tuple] = None , __lowerCamelCase: int = 0 , __lowerCamelCase: jnp.dtype = jnp.floataa , __lowerCamelCase: bool = True , **__lowerCamelCase: Optional[int] , ) -> int: if input_shape is None: __UpperCAmelCase : Dict = (1, 2_24, 2_24, 3) __UpperCAmelCase : Tuple = self.module_class(config=__lowerCamelCase , dtype=__lowerCamelCase , **__lowerCamelCase ) super().__init__(__lowerCamelCase , __lowerCamelCase , input_shape=__lowerCamelCase , seed=__lowerCamelCase , dtype=__lowerCamelCase , _do_init=_do_init ) def _lowerCamelCase ( self: Dict , __lowerCamelCase: jax.random.KeyArray , __lowerCamelCase: Tuple , __lowerCamelCase: FrozenDict = None ) -> FrozenDict: # init input tensor __UpperCAmelCase : Tuple = jax.random.normal(__lowerCamelCase , __lowerCamelCase ) __UpperCAmelCase , __UpperCAmelCase : Dict = jax.random.split(__lowerCamelCase ) __UpperCAmelCase : Optional[int] = {"params": params_rng, "dropout": dropout_rng} __UpperCAmelCase : str = self.module.init(__lowerCamelCase , __lowerCamelCase )["params"] return random_params def __call__( self: Union[str, Any] , __lowerCamelCase: Optional[Any] , __lowerCamelCase: dict = None , ) -> List[Any]: __UpperCAmelCase : int = jnp.transpose(__lowerCamelCase , (0, 2, 3, 1) ) return self.module.apply( {"params": params or self.params} , jnp.array(__lowerCamelCase , dtype=jnp.floataa ) , rngs={} , )
342
1
'''simple docstring''' import argparse import json from collections import OrderedDict from functools import partial from pathlib import Path import timm import torch from huggingface_hub import hf_hub_download from transformers import LevitConfig, LevitForImageClassificationWithTeacher, LevitImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowercase__ = logging.get_logger() def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = True ): print(F"""Converting {name}...""" ) with torch.no_grad(): if hidden_sizes == 1_28: if name[-1] == "S": UpperCAmelCase : Optional[int] = timm.create_model('levit_128s' , pretrained=UpperCAmelCase_ ) else: UpperCAmelCase : Any = timm.create_model('levit_128' , pretrained=UpperCAmelCase_ ) if hidden_sizes == 1_92: UpperCAmelCase : Optional[int] = timm.create_model('levit_192' , pretrained=UpperCAmelCase_ ) if hidden_sizes == 2_56: UpperCAmelCase : Optional[Any] = timm.create_model('levit_256' , pretrained=UpperCAmelCase_ ) if hidden_sizes == 3_84: UpperCAmelCase : str = timm.create_model('levit_384' , pretrained=UpperCAmelCase_ ) from_model.eval() UpperCAmelCase : Tuple = LevitForImageClassificationWithTeacher(UpperCAmelCase_ ).eval() UpperCAmelCase : List[Any] = OrderedDict() UpperCAmelCase : str = from_model.state_dict() UpperCAmelCase : Optional[int] = list(from_model.state_dict().keys() ) UpperCAmelCase : Union[str, Any] = list(our_model.state_dict().keys() ) print(len(UpperCAmelCase_ ) , len(UpperCAmelCase_ ) ) for i in range(len(UpperCAmelCase_ ) ): UpperCAmelCase : Union[str, Any] = weights[og_keys[i]] our_model.load_state_dict(UpperCAmelCase_ ) UpperCAmelCase : Tuple = torch.randn((2, 3, 2_24, 2_24) ) UpperCAmelCase : Tuple = from_model(UpperCAmelCase_ ) UpperCAmelCase : Any = our_model(UpperCAmelCase_ ).logits assert torch.allclose(UpperCAmelCase_ , UpperCAmelCase_ ), "The model logits don't match the original one." UpperCAmelCase : Optional[Any] = name print(UpperCAmelCase_ ) if push_to_hub: our_model.save_pretrained(save_directory / checkpoint_name ) UpperCAmelCase : int = LevitImageProcessor() image_processor.save_pretrained(save_directory / checkpoint_name ) print(F"""Pushed {checkpoint_name}""" ) def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ = None , UpperCAmelCase_ = True ): UpperCAmelCase : Tuple = 'imagenet-1k-id2label.json' UpperCAmelCase : Tuple = 10_00 UpperCAmelCase : Tuple = (1, num_labels) UpperCAmelCase : Optional[Any] = 'huggingface/label-files' UpperCAmelCase : Optional[int] = num_labels UpperCAmelCase : Union[str, Any] = json.load(open(hf_hub_download(UpperCAmelCase_ , UpperCAmelCase_ , repo_type='dataset' ) , 'r' ) ) UpperCAmelCase : Tuple = {int(UpperCAmelCase_ ): v for k, v in idalabel.items()} UpperCAmelCase : Tuple = idalabel UpperCAmelCase : Optional[Any] = {v: k for k, v in idalabel.items()} UpperCAmelCase : str = partial(UpperCAmelCase_ , num_labels=UpperCAmelCase_ , idalabel=UpperCAmelCase_ , labelaid=UpperCAmelCase_ ) UpperCAmelCase : Tuple = { 'levit-128S': 1_28, 'levit-128': 1_28, 'levit-192': 1_92, 'levit-256': 2_56, 'levit-384': 3_84, } UpperCAmelCase : Optional[Any] = { 'levit-128S': ImageNetPreTrainedConfig( hidden_sizes=[1_28, 2_56, 3_84] , num_attention_heads=[4, 6, 8] , depths=[2, 3, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), 'levit-128': ImageNetPreTrainedConfig( hidden_sizes=[1_28, 2_56, 3_84] , num_attention_heads=[4, 8, 12] , depths=[4, 4, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), 'levit-192': ImageNetPreTrainedConfig( hidden_sizes=[1_92, 2_88, 3_84] , num_attention_heads=[3, 5, 6] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), 'levit-256': ImageNetPreTrainedConfig( hidden_sizes=[2_56, 3_84, 5_12] , num_attention_heads=[4, 6, 8] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), 'levit-384': ImageNetPreTrainedConfig( hidden_sizes=[3_84, 5_12, 7_68] , num_attention_heads=[6, 9, 12] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0.1 , ), } if model_name: convert_weight_and_push( names_to_hidden_sizes[model_name] , UpperCAmelCase_ , names_to_config[model_name] , UpperCAmelCase_ , UpperCAmelCase_ ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(names_to_hidden_sizes[model_name] , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) return config, expected_shape if __name__ == "__main__": lowercase__ = 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 Levit* architecture,", ) parser.add_argument( "--pytorch_dump_folder_path", default="levit-dump-folder/", type=Path, required=False, help="Path to the output PyTorch model directory.", ) parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub") parser.add_argument( "--no-push_to_hub", dest="push_to_hub", action="store_false", help="Do not push model and image processor to the hub", ) lowercase__ = parser.parse_args() lowercase__ = 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)
151
'''simple docstring''' def UpperCamelCase( UpperCAmelCase_ ): return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def UpperCamelCase( UpperCAmelCase_ ): UpperCAmelCase : List[Any] = 0 UpperCAmelCase : Dict = len(UpperCAmelCase_ ) # No of vertices in graph UpperCAmelCase : Tuple = [0] * n UpperCAmelCase : List[Any] = [False] * n def dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): UpperCAmelCase : List[str] = True UpperCAmelCase : Dict = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , id_ ) UpperCAmelCase : Optional[Any] = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge UpperCAmelCase : Dict = min(low[at] , low[to] ) UpperCAmelCase : list[tuple[int, int]] = [] for i in range(UpperCAmelCase_ ): if not visited[i]: dfs(UpperCAmelCase_ , -1 , UpperCAmelCase_ , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
151
1
"""simple docstring""" import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def _SCREAMING_SNAKE_CASE ( _lowercase : List[Any] , _lowercase : Optional[int] , _lowercase : str , _lowercase : List[Any] , _lowercase : int ) ->Any: '''simple docstring''' with open(_lowercase ) as metadata_file: a : int = json.load(_lowercase ) a : List[str] = LukeConfig(use_entity_aware_attention=_lowercase , **metadata["model_config"] ) # Load in the weights from the checkpoint_path a : Optional[Any] = torch.load(_lowercase , map_location="cpu" )["module"] # Load the entity vocab file a : Optional[Any] = load_original_entity_vocab(_lowercase ) # add an entry for [MASK2] a : Optional[int] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 a : Any = XLMRobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"] ) # Add special tokens to the token vocabulary for downstream tasks a : Dict = AddedToken("<ent>" , lstrip=_lowercase , rstrip=_lowercase ) a : str = AddedToken("<ent2>" , lstrip=_lowercase , rstrip=_lowercase ) tokenizer.add_special_tokens({"additional_special_tokens": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(F"""Saving tokenizer to {pytorch_dump_folder_path}""" ) tokenizer.save_pretrained(_lowercase ) with open(os.path.join(_lowercase , "tokenizer_config.json" ) , "r" ) as f: a : Tuple = json.load(_lowercase ) a : Any = "MLukeTokenizer" with open(os.path.join(_lowercase , "tokenizer_config.json" ) , "w" ) as f: json.dump(_lowercase , _lowercase ) with open(os.path.join(_lowercase , MLukeTokenizer.vocab_files_names["entity_vocab_file"] ) , "w" ) as f: json.dump(_lowercase , _lowercase ) a : Dict = MLukeTokenizer.from_pretrained(_lowercase ) # Initialize the embeddings of the special tokens a : List[str] = tokenizer.convert_tokens_to_ids(["@"] )[0] a : List[str] = tokenizer.convert_tokens_to_ids(["#"] )[0] a : int = state_dict["embeddings.word_embeddings.weight"] a : Optional[Any] = word_emb[ent_init_index].unsqueeze(0 ) a : List[str] = word_emb[enta_init_index].unsqueeze(0 ) a : Optional[int] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: a : List[Any] = state_dict[bias_name] a : Optional[Any] = decoder_bias[ent_init_index].unsqueeze(0 ) a : List[str] = decoder_bias[enta_init_index].unsqueeze(0 ) a : Any = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: a : Dict = F"""encoder.layer.{layer_index}.attention.self.""" a : Tuple = state_dict[prefix + matrix_name] a : Optional[Any] = state_dict[prefix + matrix_name] a : Union[str, Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks a : Union[str, Any] = state_dict["entity_embeddings.entity_embeddings.weight"] a : Dict = entity_emb[entity_vocab["[MASK]"]].unsqueeze(0 ) a : int = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' a : int = state_dict["entity_predictions.bias"] a : int = entity_prediction_bias[entity_vocab["[MASK]"]].unsqueeze(0 ) a : Dict = torch.cat([entity_prediction_bias, entity_mask_bias] ) a : Any = LukeForMaskedLM(config=_lowercase ).eval() state_dict.pop("entity_predictions.decoder.weight" ) state_dict.pop("lm_head.decoder.weight" ) state_dict.pop("lm_head.decoder.bias" ) a : Union[str, Any] = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("lm_head" ) or key.startswith("entity_predictions" )): a : str = state_dict[key] else: a : Optional[Any] = state_dict[key] a : List[str] = model.load_state_dict(_lowercase , strict=_lowercase ) if set(_lowercase ) != {"luke.embeddings.position_ids"}: raise ValueError(F"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(_lowercase ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs a : Optional[int] = MLukeTokenizer.from_pretrained(_lowercase , task="entity_classification" ) a : Optional[int] = "ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan)." a : List[str] = (0, 9) a : Dict = tokenizer(_lowercase , entity_spans=[span] , return_tensors="pt" ) a : str = model(**_lowercase ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base a : Tuple = torch.Size((1, 33, 768) ) a : List[str] = torch.tensor([[0.0892, 0.0596, -0.2819], [0.0134, 0.1199, 0.0573], [-0.0169, 0.0927, 0.0644]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , _lowercase , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base a : Tuple = torch.Size((1, 1, 768) ) a : Any = torch.tensor([[-0.1482, 0.0609, 0.0322]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" F""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , _lowercase , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction a : Optional[int] = MLukeTokenizer.from_pretrained(_lowercase ) a : int = "Tokyo is the capital of <mask>." a : str = (24, 30) a : List[str] = tokenizer(_lowercase , entity_spans=[span] , return_tensors="pt" ) a : str = model(**_lowercase ) a : Any = encoding["input_ids"][0].tolist() a : List[Any] = input_ids.index(tokenizer.convert_tokens_to_ids("<mask>" ) ) a : str = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(_lowercase ) a : str = outputs.entity_logits[0][0].argmax().item() a : Any = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("en:" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("Saving PyTorch model to {}".format(_lowercase ) ) model.save_pretrained(_lowercase ) def _SCREAMING_SNAKE_CASE ( _lowercase : Union[str, Any] ) ->str: '''simple docstring''' a : Tuple = ["[MASK]", "[PAD]", "[UNK]"] a : Union[str, Any] = [json.loads(_lowercase ) for line in open(_lowercase )] a : Any = {} for entry in data: a : List[Any] = entry["id"] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: a : Optional[Any] = entity_id break a : int = F"""{language}:{entity_name}""" a : Dict = entity_id return new_mapping if __name__ == "__main__": a : str = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) a : Any = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
353
"""simple docstring""" import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm a : Dict = re.compile('''[^A-Za-z_0-9]''') # parameters used in DuplicationIndex a : List[str] = 10 a : Optional[int] = 256 def _SCREAMING_SNAKE_CASE ( _lowercase : List[str] ) ->Optional[MinHash]: '''simple docstring''' if len(_lowercase ) < MIN_NUM_TOKENS: return None a : Any = MinHash(num_perm=_lowercase ) for token in set(_lowercase ): min_hash.update(token.encode() ) return min_hash def _SCREAMING_SNAKE_CASE ( _lowercase : str ) ->Set[str]: '''simple docstring''' return {t for t in NON_ALPHA.split(_lowercase ) if len(t.strip() ) > 0} class __UpperCamelCase : def __init__( self , *, lowerCAmelCase__ = 0.85 , ) -> Any: a : Any = duplication_jaccard_threshold a : Dict = NUM_PERM a : Dict = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a : List[str] = defaultdict(lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> None: a : Any = self._index.query(lowerCAmelCase__ ) if code_key in self._index.keys: print(f"""Duplicate key {code_key}""" ) return self._index.insert(lowerCAmelCase__ , lowerCAmelCase__ ) if len(lowerCAmelCase__ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(lowerCAmelCase__ ) break else: self._duplicate_clusters[close_duplicates[0]].add(lowerCAmelCase__ ) def __a ( self ) -> List[List[Dict]]: a : Any = [] for base, duplicates in self._duplicate_clusters.items(): a : Any = [base] + list(lowerCAmelCase__ ) # reformat the cluster to be a list of dict a : Optional[int] = [{"base_index": el[0], "repo_name": el[1], "path": el[2]} for el in cluster] duplicate_clusters.append(lowerCAmelCase__ ) return duplicate_clusters def __a ( self , lowerCAmelCase__ ) -> None: a : Optional[int] = self.get_duplicate_clusters() with open(lowerCAmelCase__ , "w" ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ ) def _SCREAMING_SNAKE_CASE ( _lowercase : List[str] ) ->Optional[Any]: '''simple docstring''' a, a : Optional[Any] = element a : Any = get_min_hash([t for t in NON_ALPHA.split(data["content"] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _SCREAMING_SNAKE_CASE ( _lowercase : Type[Dataset] ) ->Optional[int]: '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_lowercase , max_queue_size=1_0000 ) , chunksize=100 , ): if data is not None: yield data def _SCREAMING_SNAKE_CASE ( _lowercase : Type[Dataset] , _lowercase : float ) ->Dict: '''simple docstring''' a : Optional[int] = DuplicationIndex(duplication_jaccard_threshold=_lowercase ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(_lowercase ) ) , max_queue_size=100 ) ): di.add(_lowercase , _lowercase ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _SCREAMING_SNAKE_CASE ( _lowercase : str , _lowercase : str ) ->float: '''simple docstring''' a : Any = get_tokens(_lowercase ) a : List[str] = get_tokens(_lowercase ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) a : Dict = None def _SCREAMING_SNAKE_CASE ( _lowercase : Tuple , _lowercase : Dict ) ->Tuple: '''simple docstring''' a : Union[str, Any] = [] for elementa in cluster: a : List[Any] = _shared_dataset[elementa["base_index"]]["content"] for elementa in extremes: a : Optional[int] = _shared_dataset[elementa["base_index"]]["content"] if jaccard_similarity(_lowercase , _lowercase ) >= jaccard_threshold: elementa["copies"] += 1 break else: a : Optional[Any] = 1 extremes.append(_lowercase ) return extremes def _SCREAMING_SNAKE_CASE ( _lowercase : int , _lowercase : Any , _lowercase : Optional[Any] ) ->Any: '''simple docstring''' global _shared_dataset a : Tuple = dataset a : List[Any] = [] a : int = partial(_find_cluster_extremes_shared , jaccard_threshold=_lowercase ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( _lowercase , _lowercase , ) , total=len(_lowercase ) , ): extremes_list.append(_lowercase ) return extremes_list def _SCREAMING_SNAKE_CASE ( _lowercase : Type[Dataset] , _lowercase : float = 0.85 ) ->Tuple[Type[Dataset], List[List[Dict]]]: '''simple docstring''' a : str = make_duplicate_clusters(_lowercase , _lowercase ) a : List[Any] = {x["base_index"] for cluster in duplicate_clusters for x in cluster} a : List[Any] = {} a : str = find_extremes(_lowercase , _lowercase , _lowercase ) for extremes in extremes_clusters: for element in extremes: a : Optional[Any] = element a : Union[str, Any] = duplicate_indices - set(extreme_dict.keys() ) a : Union[str, Any] = dataset.filter(lambda _lowercase , _lowercase : idx not in remove_indices , with_indices=_lowercase ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a : Union[str, Any] = element["base_index"] in extreme_dict if element["is_extreme"]: a : Optional[int] = extreme_dict[element["base_index"]]["copies"] print(F"""Original dataset size: {len(_lowercase )}""" ) print(F"""Number of duplicate clusters: {len(_lowercase )}""" ) print(F"""Files in duplicate cluster: {len(_lowercase )}""" ) print(F"""Unique files in duplicate cluster: {len(_lowercase )}""" ) print(F"""Filtered dataset size: {len(_lowercase )}""" ) return ds_filter, duplicate_clusters
79
0
'''simple docstring''' from maths.prime_check import is_prime def UpperCamelCase( UpperCAmelCase_ ): if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): UpperCAmelCase : Any = F"""Input value of [number={number}] must be an integer""" raise TypeError(UpperCAmelCase_ ) if is_prime(UpperCAmelCase_ ) and is_prime(number + 2 ): return number + 2 else: return -1 if __name__ == "__main__": import doctest doctest.testmod()
151
'''simple docstring''' from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class A_ : '''simple docstring''' UpperCAmelCase_ : Optional[Any] = XGLMConfig UpperCAmelCase_ : str = {} UpperCAmelCase_ : List[str] = """gelu""" def __init__( self : Tuple , lowercase_ : str , lowercase_ : List[str]=14 , lowercase_ : Optional[int]=7 , lowercase_ : Optional[int]=True , lowercase_ : List[str]=True , lowercase_ : Union[str, Any]=True , lowercase_ : Dict=99 , lowercase_ : Optional[int]=32 , lowercase_ : Any=2 , lowercase_ : Union[str, Any]=4 , lowercase_ : Optional[int]=37 , lowercase_ : List[str]="gelu" , lowercase_ : Tuple=0.1 , lowercase_ : List[Any]=0.1 , lowercase_ : List[Any]=512 , lowercase_ : Union[str, Any]=0.02 , ) -> str: UpperCAmelCase : Optional[Any] = parent UpperCAmelCase : Optional[int] = batch_size UpperCAmelCase : List[str] = seq_length UpperCAmelCase : List[str] = is_training UpperCAmelCase : str = use_input_mask UpperCAmelCase : int = use_labels UpperCAmelCase : Union[str, Any] = vocab_size UpperCAmelCase : Optional[int] = d_model UpperCAmelCase : str = num_hidden_layers UpperCAmelCase : Optional[int] = num_attention_heads UpperCAmelCase : List[str] = ffn_dim UpperCAmelCase : Optional[int] = activation_function UpperCAmelCase : Optional[Any] = activation_dropout UpperCAmelCase : Dict = attention_dropout UpperCAmelCase : List[str] = max_position_embeddings UpperCAmelCase : List[Any] = initializer_range UpperCAmelCase : Optional[Any] = None UpperCAmelCase : str = 0 UpperCAmelCase : List[Any] = 2 UpperCAmelCase : Optional[Any] = 1 def UpperCAmelCase_ ( self : int ) -> Union[str, Any]: return XGLMConfig.from_pretrained('facebook/xglm-564M' ) def UpperCAmelCase_ ( self : Dict ) -> int: UpperCAmelCase : Any = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) UpperCAmelCase : Optional[Any] = None if self.use_input_mask: UpperCAmelCase : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Any = self.get_config() UpperCAmelCase : Optional[int] = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Tuple: return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=lowercase_ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=lowercase_ , ) def UpperCAmelCase_ ( self : List[str] ) -> Dict: UpperCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) : Union[str, Any] = config_and_inputs UpperCAmelCase : List[str] = { 'input_ids': input_ids, 'head_mask': head_mask, } return config, inputs_dict @require_tf class A_ ( _snake_case , _snake_case , unittest.TestCase ): '''simple docstring''' UpperCAmelCase_ : int = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () UpperCAmelCase_ : List[str] = (TFXGLMForCausalLM,) if is_tf_available() else () UpperCAmelCase_ : str = ( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) UpperCAmelCase_ : List[str] = False UpperCAmelCase_ : List[str] = False UpperCAmelCase_ : str = False def UpperCAmelCase_ ( self : Union[str, Any] ) -> Any: UpperCAmelCase : Any = TFXGLMModelTester(self ) UpperCAmelCase : int = ConfigTester(self , config_class=lowercase_ , n_embd=37 ) def UpperCAmelCase_ ( self : Any ) -> List[str]: self.config_tester.run_common_tests() @slow def UpperCAmelCase_ ( self : Tuple ) -> Optional[int]: for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : int = TFXGLMModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.' ) def UpperCAmelCase_ ( self : List[Any] ) -> List[Any]: super().test_resize_token_embeddings() @require_tf class A_ ( unittest.TestCase ): '''simple docstring''' @slow def UpperCAmelCase_ ( self : int , lowercase_ : str=True ) -> Any: UpperCAmelCase : str = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) UpperCAmelCase : Any = tf.convert_to_tensor([[2, 268, 9_865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off UpperCAmelCase : Union[str, Any] = [2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581] # fmt: on UpperCAmelCase : int = model.generate(lowercase_ , do_sample=lowercase_ , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , lowercase_ ) @slow def UpperCAmelCase_ ( self : Optional[int] ) -> int: UpperCAmelCase : str = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) UpperCAmelCase : Tuple = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) tf.random.set_seed(0 ) UpperCAmelCase : Dict = tokenizer('Today is a nice day and' , return_tensors='tf' ) UpperCAmelCase : Tuple = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(':/CPU:0' ): UpperCAmelCase : int = model.generate(lowercase_ , do_sample=lowercase_ , seed=[7, 0] ) UpperCAmelCase : Dict = tokenizer.decode(output_ids[0] , skip_special_tokens=lowercase_ ) UpperCAmelCase : Dict = ( 'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due' ) self.assertEqual(lowercase_ , lowercase_ ) @slow def UpperCAmelCase_ ( self : int ) -> str: UpperCAmelCase : List[str] = TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M' ) UpperCAmelCase : Any = XGLMTokenizer.from_pretrained('facebook/xglm-564M' ) UpperCAmelCase : str = 'left' # use different length sentences to test batching UpperCAmelCase : Tuple = [ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When', 'Hello, my dog is a little', ] UpperCAmelCase : Union[str, Any] = tokenizer(lowercase_ , return_tensors='tf' , padding=lowercase_ ) UpperCAmelCase : Any = inputs['input_ids'] UpperCAmelCase : int = model.generate(input_ids=lowercase_ , attention_mask=inputs['attention_mask'] , max_new_tokens=12 ) UpperCAmelCase : Union[str, Any] = tokenizer(sentences[0] , return_tensors='tf' ).input_ids UpperCAmelCase : Dict = model.generate(input_ids=lowercase_ , max_new_tokens=12 ) UpperCAmelCase : Tuple = tokenizer(sentences[1] , return_tensors='tf' ).input_ids UpperCAmelCase : List[Any] = model.generate(input_ids=lowercase_ , max_new_tokens=12 ) UpperCAmelCase : List[str] = tokenizer.batch_decode(lowercase_ , skip_special_tokens=lowercase_ ) UpperCAmelCase : List[str] = tokenizer.decode(output_non_padded[0] , skip_special_tokens=lowercase_ ) UpperCAmelCase : Optional[int] = tokenizer.decode(output_padded[0] , skip_special_tokens=lowercase_ ) UpperCAmelCase : str = [ 'This is an extremelly long sentence that only exists to test the ability of the model to cope with ' 'left-padding, such as in batched generation. The output for the sequence below should be the same ' 'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ' 'a single', 'Hello, my dog is a little bit of a shy one, but he is very friendly', ] self.assertListEqual(lowercase_ , lowercase_ ) self.assertListEqual(lowercase_ , [non_padded_sentence, padded_sentence] )
151
1
"""simple docstring""" import inspect import tempfile from collections import OrderedDict, UserDict from collections.abc import MutableMapping from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from typing import Any, ContextManager, List, Tuple import numpy as np from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy if is_flax_available(): import jax.numpy as jnp class _UpperCAmelCase ( a ): '''simple docstring''' def __get__( self , A , A=None ) -> Dict: # See docs.python.org/3/howto/descriptor.html#properties if obj is None: return self if self.fget is None: raise AttributeError('''unreadable attribute''' ) _UpperCAmelCase : List[Any] = '''__cached_''' + self.fget.__name__ _UpperCAmelCase : Tuple = getattr(A , A , A ) if cached is None: _UpperCAmelCase : Optional[Any] = self.fget(A ) setattr(A , A , A ) return cached def lowerCamelCase_ (UpperCamelCase__ : Dict ): _UpperCAmelCase : Any = val.lower() if val in {"y", "yes", "t", "true", "on", "1"}: return 1 if val in {"n", "no", "f", "false", "off", "0"}: return 0 raise ValueError(F'invalid truth value {val!r}' ) def lowerCamelCase_ (UpperCamelCase__ : str ): if is_torch_fx_proxy(UpperCamelCase__ ): return True if is_torch_available(): import torch if isinstance(UpperCamelCase__ , torch.Tensor ): return True if is_tf_available(): import tensorflow as tf if isinstance(UpperCamelCase__ , tf.Tensor ): return True if is_flax_available(): import jax.numpy as jnp from jax.core import Tracer if isinstance(UpperCamelCase__ , (jnp.ndarray, Tracer) ): return True return isinstance(UpperCamelCase__ , np.ndarray ) def lowerCamelCase_ (UpperCamelCase__ : str ): return isinstance(UpperCamelCase__ , np.ndarray ) def lowerCamelCase_ (UpperCamelCase__ : List[Any] ): return _is_numpy(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : Union[str, Any] ): import torch return isinstance(UpperCamelCase__ , torch.Tensor ) def lowerCamelCase_ (UpperCamelCase__ : Dict ): return False if not is_torch_available() else _is_torch(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : Any ): import torch return isinstance(UpperCamelCase__ , torch.device ) def lowerCamelCase_ (UpperCamelCase__ : List[str] ): return False if not is_torch_available() else _is_torch_device(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : Tuple ): import torch if isinstance(UpperCamelCase__ , UpperCamelCase__ ): if hasattr(UpperCamelCase__ , UpperCamelCase__ ): _UpperCAmelCase : Optional[Any] = getattr(UpperCamelCase__ , UpperCamelCase__ ) else: return False return isinstance(UpperCamelCase__ , torch.dtype ) def lowerCamelCase_ (UpperCamelCase__ : Tuple ): return False if not is_torch_available() else _is_torch_dtype(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : List[Any] ): import tensorflow as tf return isinstance(UpperCamelCase__ , tf.Tensor ) def lowerCamelCase_ (UpperCamelCase__ : List[str] ): return False if not is_tf_available() else _is_tensorflow(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : List[Any] ): import tensorflow as tf # the `is_symbolic_tensor` predicate is only available starting with TF 2.14 if hasattr(UpperCamelCase__ , '''is_symbolic_tensor''' ): return tf.is_symbolic_tensor(UpperCamelCase__ ) return type(UpperCamelCase__ ) == tf.Tensor def lowerCamelCase_ (UpperCamelCase__ : List[str] ): return False if not is_tf_available() else _is_tf_symbolic_tensor(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : Union[str, Any] ): import jax.numpy as jnp # noqa: F811 return isinstance(UpperCamelCase__ , jnp.ndarray ) def lowerCamelCase_ (UpperCamelCase__ : Tuple ): return False if not is_flax_available() else _is_jax(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : Union[str, Any] ): if isinstance(UpperCamelCase__ , (dict, UserDict) ): return {k: to_py_obj(UpperCamelCase__ ) for k, v in obj.items()} elif isinstance(UpperCamelCase__ , (list, tuple) ): return [to_py_obj(UpperCamelCase__ ) for o in obj] elif is_tf_tensor(UpperCamelCase__ ): return obj.numpy().tolist() elif is_torch_tensor(UpperCamelCase__ ): return obj.detach().cpu().tolist() elif is_jax_tensor(UpperCamelCase__ ): return np.asarray(UpperCamelCase__ ).tolist() elif isinstance(UpperCamelCase__ , (np.ndarray, np.number) ): # tolist also works on 0d np arrays return obj.tolist() else: return obj def lowerCamelCase_ (UpperCamelCase__ : int ): if isinstance(UpperCamelCase__ , (dict, UserDict) ): return {k: to_numpy(UpperCamelCase__ ) for k, v in obj.items()} elif isinstance(UpperCamelCase__ , (list, tuple) ): return np.array(UpperCamelCase__ ) elif is_tf_tensor(UpperCamelCase__ ): return obj.numpy() elif is_torch_tensor(UpperCamelCase__ ): return obj.detach().cpu().numpy() elif is_jax_tensor(UpperCamelCase__ ): return np.asarray(UpperCamelCase__ ) else: return obj class _UpperCAmelCase ( a ): '''simple docstring''' def __lowerCAmelCase ( self ) -> Optional[Any]: _UpperCAmelCase : Optional[Any] = fields(self ) # Safety and consistency checks if not len(A ): raise ValueError(f'{self.__class__.__name__} has no fields.' ) if not all(field.default is None for field in class_fields[1:] ): raise ValueError(f'{self.__class__.__name__} should not have more than one required field.' ) _UpperCAmelCase : Union[str, Any] = getattr(self , class_fields[0].name ) _UpperCAmelCase : List[Any] = all(getattr(self , field.name ) is None for field in class_fields[1:] ) if other_fields_are_none and not is_tensor(A ): if isinstance(A , A ): _UpperCAmelCase : List[str] = first_field.items() _UpperCAmelCase : Any = True else: try: _UpperCAmelCase : int = iter(A ) _UpperCAmelCase : Tuple = True except TypeError: _UpperCAmelCase : Optional[Any] = False # if we provided an iterator as first field and the iterator is a (key, value) iterator # set the associated fields if first_field_iterator: for idx, element in enumerate(A ): if ( not isinstance(A , (list, tuple) ) or not len(A ) == 2 or not isinstance(element[0] , A ) ): if idx == 0: # If we do not have an iterator of key/values, set it as attribute _UpperCAmelCase : str = first_field else: # If we have a mixed iterator, raise an error raise ValueError( f'Cannot set key/value for {element}. It needs to be a tuple (key, value).' ) break setattr(self , element[0] , element[1] ) if element[1] is not None: _UpperCAmelCase : Any = element[1] elif first_field is not None: _UpperCAmelCase : Any = first_field else: for field in class_fields: _UpperCAmelCase : Dict = getattr(self , field.name ) if v is not None: _UpperCAmelCase : Optional[Any] = v def __delitem__( self , *A , **A ) -> Dict: raise Exception(f'You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.' ) def __lowerCAmelCase ( self , *A , **A ) -> Optional[Any]: raise Exception(f'You cannot use ``setdefault`` on a {self.__class__.__name__} instance.' ) def __lowerCAmelCase ( self , *A , **A ) -> Union[str, Any]: raise Exception(f'You cannot use ``pop`` on a {self.__class__.__name__} instance.' ) def __lowerCAmelCase ( self , *A , **A ) -> Union[str, Any]: raise Exception(f'You cannot use ``update`` on a {self.__class__.__name__} instance.' ) def __getitem__( self , A ) -> Union[str, Any]: if isinstance(A , A ): _UpperCAmelCase : str = dict(self.items() ) return inner_dict[k] else: return self.to_tuple()[k] def __setattr__( self , A , A ) -> List[Any]: if name in self.keys() and value is not None: # Don't call self.__setitem__ to avoid recursion errors super().__setitem__(A , A ) super().__setattr__(A , A ) def __setitem__( self , A , A ) -> List[Any]: # Will raise a KeyException if needed super().__setitem__(A , A ) # Don't call self.__setattr__ to avoid recursion errors super().__setattr__(A , A ) def __lowerCAmelCase ( self ) -> Tuple[Any]: return tuple(self[k] for k in self.keys() ) class _UpperCAmelCase ( a ,a ): '''simple docstring''' @classmethod def __lowerCAmelCase ( cls , A ) -> Tuple: raise ValueError( f'{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys() )}' ) class _UpperCAmelCase ( a ): '''simple docstring''' a__ ='''longest''' a__ ='''max_length''' a__ ='''do_not_pad''' class _UpperCAmelCase ( a ): '''simple docstring''' a__ ='''pt''' a__ ='''tf''' a__ ='''np''' a__ ='''jax''' class _UpperCAmelCase : '''simple docstring''' def __init__( self , A ) -> List[Any]: _UpperCAmelCase : str = context_managers _UpperCAmelCase : Optional[int] = ExitStack() def __enter__( self ) -> int: for context_manager in self.context_managers: self.stack.enter_context(A ) def __exit__( self , *A , **A ) -> List[str]: self.stack.__exit__(*A , **A ) def lowerCamelCase_ (UpperCamelCase__ : Optional[int] ): _UpperCAmelCase : List[str] = infer_framework(UpperCamelCase__ ) if framework == "tf": _UpperCAmelCase : List[str] = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": _UpperCAmelCase : Any = inspect.signature(model_class.forward ) # PyTorch models else: _UpperCAmelCase : int = inspect.signature(model_class.__call__ ) # Flax models for p in signature.parameters: if p == "return_loss" and signature.parameters[p].default is True: return True return False def lowerCamelCase_ (UpperCamelCase__ : Optional[Any] ): _UpperCAmelCase : Union[str, Any] = model_class.__name__ _UpperCAmelCase : Union[str, Any] = infer_framework(UpperCamelCase__ ) if framework == "tf": _UpperCAmelCase : Tuple = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": _UpperCAmelCase : Optional[Any] = inspect.signature(model_class.forward ) # PyTorch models else: _UpperCAmelCase : List[str] = inspect.signature(model_class.__call__ ) # Flax models if "QuestionAnswering" in model_name: return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")] else: return [p for p in signature.parameters if "label" in p] def lowerCamelCase_ (UpperCamelCase__ : MutableMapping , UpperCamelCase__ : str = "" , UpperCamelCase__ : str = "." ): def _flatten_dict(UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : int="" , UpperCamelCase__ : Optional[Any]="." ): for k, v in d.items(): _UpperCAmelCase : Tuple = str(UpperCamelCase__ ) + delimiter + str(UpperCamelCase__ ) if parent_key else k if v and isinstance(UpperCamelCase__ , UpperCamelCase__ ): yield from flatten_dict(UpperCamelCase__ , UpperCamelCase__ , delimiter=UpperCamelCase__ ).items() else: yield key, v return dict(_flatten_dict(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) ) @contextmanager def lowerCamelCase_ (UpperCamelCase__ : List[str] , UpperCamelCase__ : bool = False ): if use_temp_dir: with tempfile.TemporaryDirectory() as tmp_dir: yield tmp_dir else: yield working_dir def lowerCamelCase_ (UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[str]=None ): if is_numpy_array(UpperCamelCase__ ): return np.transpose(UpperCamelCase__ , axes=UpperCamelCase__ ) elif is_torch_tensor(UpperCamelCase__ ): return array.T if axes is None else array.permute(*UpperCamelCase__ ) elif is_tf_tensor(UpperCamelCase__ ): import tensorflow as tf return tf.transpose(UpperCamelCase__ , perm=UpperCamelCase__ ) elif is_jax_tensor(UpperCamelCase__ ): return jnp.transpose(UpperCamelCase__ , axes=UpperCamelCase__ ) else: raise ValueError(F'Type not supported for transpose: {type(UpperCamelCase__ )}.' ) def lowerCamelCase_ (UpperCamelCase__ : Any , UpperCamelCase__ : List[Any] ): if is_numpy_array(UpperCamelCase__ ): return np.reshape(UpperCamelCase__ , UpperCamelCase__ ) elif is_torch_tensor(UpperCamelCase__ ): return array.reshape(*UpperCamelCase__ ) elif is_tf_tensor(UpperCamelCase__ ): import tensorflow as tf return tf.reshape(UpperCamelCase__ , UpperCamelCase__ ) elif is_jax_tensor(UpperCamelCase__ ): return jnp.reshape(UpperCamelCase__ , UpperCamelCase__ ) else: raise ValueError(F'Type not supported for reshape: {type(UpperCamelCase__ )}.' ) def lowerCamelCase_ (UpperCamelCase__ : List[str] , UpperCamelCase__ : Any=None ): if is_numpy_array(UpperCamelCase__ ): return np.squeeze(UpperCamelCase__ , axis=UpperCamelCase__ ) elif is_torch_tensor(UpperCamelCase__ ): return array.squeeze() if axis is None else array.squeeze(dim=UpperCamelCase__ ) elif is_tf_tensor(UpperCamelCase__ ): import tensorflow as tf return tf.squeeze(UpperCamelCase__ , axis=UpperCamelCase__ ) elif is_jax_tensor(UpperCamelCase__ ): return jnp.squeeze(UpperCamelCase__ , axis=UpperCamelCase__ ) else: raise ValueError(F'Type not supported for squeeze: {type(UpperCamelCase__ )}.' ) def lowerCamelCase_ (UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] ): if is_numpy_array(UpperCamelCase__ ): return np.expand_dims(UpperCamelCase__ , UpperCamelCase__ ) elif is_torch_tensor(UpperCamelCase__ ): return array.unsqueeze(dim=UpperCamelCase__ ) elif is_tf_tensor(UpperCamelCase__ ): import tensorflow as tf return tf.expand_dims(UpperCamelCase__ , axis=UpperCamelCase__ ) elif is_jax_tensor(UpperCamelCase__ ): return jnp.expand_dims(UpperCamelCase__ , axis=UpperCamelCase__ ) else: raise ValueError(F'Type not supported for expand_dims: {type(UpperCamelCase__ )}.' ) def lowerCamelCase_ (UpperCamelCase__ : Optional[Any] ): if is_numpy_array(UpperCamelCase__ ): return np.size(UpperCamelCase__ ) elif is_torch_tensor(UpperCamelCase__ ): return array.numel() elif is_tf_tensor(UpperCamelCase__ ): import tensorflow as tf return tf.size(UpperCamelCase__ ) elif is_jax_tensor(UpperCamelCase__ ): return array.size else: raise ValueError(F'Type not supported for expand_dims: {type(UpperCamelCase__ )}.' ) def lowerCamelCase_ (UpperCamelCase__ : Any , UpperCamelCase__ : int ): for key, value in auto_map.items(): if isinstance(UpperCamelCase__ , (tuple, list) ): _UpperCAmelCase : List[Any] = [F'{repo_id}--{v}' if (v is not None and '''--''' not in v) else v for v in value] elif value is not None and "--" not in value: _UpperCAmelCase : str = F'{repo_id}--{value}' return auto_map def lowerCamelCase_ (UpperCamelCase__ : str ): for base_class in inspect.getmro(UpperCamelCase__ ): _UpperCAmelCase : List[Any] = base_class.__module__ _UpperCAmelCase : Union[str, Any] = base_class.__name__ if module.startswith('''tensorflow''' ) or module.startswith('''keras''' ) or name == "TFPreTrainedModel": return "tf" elif module.startswith('''torch''' ) or name == "PreTrainedModel": return "pt" elif module.startswith('''flax''' ) or module.startswith('''jax''' ) or name == "FlaxPreTrainedModel": return "flax" else: raise TypeError(F'Could not infer framework from class {model_class}.' )
369
"""simple docstring""" import datasets from .evaluate import evaluate _lowerCAmelCase :int = '\\n@article{hendrycks2021cuad,\n title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n journal={arXiv preprint arXiv:2103.06268},\n year={2021}\n}\n' _lowerCAmelCase :int = '\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n' _lowerCAmelCase :str = '\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair as given in the references (see below)\n - \'prediction_text\': list of possible texts for the answer, as a list of strings\n depending on a threshold on the confidence probability of each prediction.\n references: List of question-answers dictionaries with the following key-values:\n - \'id\': id of the question-answer pair (see above),\n - \'answers\': a Dict in the CUAD dataset format\n {\n \'text\': list of possible texts for the answer, as a list of strings\n \'answer_start\': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n \'f1\': The F-score of predicted tokens versus the gold answer\n \'aupr\': Area Under the Precision-Recall curve\n \'prec_at_80_recall\': Precision at 80% recall\n \'prec_at_90_recall\': Precision at 90% recall\nExamples:\n >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n >>> cuad_metric = datasets.load_metric("cuad")\n >>> results = cuad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class _UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': { '''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.features.Sequence(datasets.Value('''string''' ) ), }, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ) , codebase_urls=['''https://www.atticusprojectai.org/cuad'''] , reference_urls=['''https://www.atticusprojectai.org/cuad'''] , ) def __lowerCAmelCase ( self , A , A ) -> List[Any]: _UpperCAmelCase : Optional[int] = {prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions} _UpperCAmelCase : Optional[Any] = [ { '''paragraphs''': [ { '''qas''': [ { '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']], '''id''': ref['''id'''], } for ref in references ] } ] } ] _UpperCAmelCase : Union[str, Any] = evaluate(dataset=A , predictions=A ) return score
68
0
from arguments import InitializationArguments from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, HfArgumentParser # Configuration a_ :Optional[int] = HfArgumentParser(InitializationArguments) a_ :Optional[int] = parser.parse_args() # Load codeparrot tokenizer trained for Python code tokenization a_ :Dict = AutoTokenizer.from_pretrained(args.tokenizer_name) # Config: "scale_attn_by_layer_idx" and "reorder_and_upcast_attn" are Mistral stability tweaks a_ :Union[str, Any] = { "vocab_size": len(tokenizer), "scale_attn_by_inverse_layer_idx": True, "reorder_and_upcast_attn": True, } # Load model config (GPT-2 large in this case) a_ :Any = AutoConfig.from_pretrained(args.config_name, **config_kwargs) # Initialize new model with config a_ :str = AutoModelForCausalLM.from_config(config) # Save model to the hub model.save_pretrained(args.model_name, push_to_hub=args.push_to_hub)
277
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo a_ :Any = "\\n@misc{wu2016googles,\n title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n" a_ :List[str] = "\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe 'GLEU score'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore's range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n" a_ :List[str] = "\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n 'google_bleu': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.4\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case__ ( datasets.Metric ): """simple docstring""" def lowercase_ ( self : str ) ->MetricInfo: return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string', id='token' ), id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string', id='token' ), id='sequence' ), id='references' ), } ), ) def lowercase_ ( self : str, _snake_case : List[List[List[str]]], _snake_case : List[List[str]], _snake_case : int = 1, _snake_case : int = 4, ) ->Dict[str, float]: return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_snake_case, hypotheses=_snake_case, min_len=_snake_case, max_len=_snake_case ) }
277
1
from __future__ import annotations def snake_case (A_ :str ): '''simple docstring''' if len(UpperCAmelCase__ ) == 0: return [] a, a : Any = min(UpperCAmelCase__ ), max(UpperCAmelCase__ ) a : int = int(max_value - min_value ) + 1 a : str = [[] for _ in range(UpperCAmelCase__ )] for i in my_list: buckets[int(i - min_value )].append(UpperCAmelCase__ ) return [v for bucket in buckets for v in sorted(UpperCAmelCase__ )] if __name__ == "__main__": from doctest import testmod testmod() assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
363
"""simple docstring""" from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging _UpperCamelCase : int = logging.get_logger(__name__) class snake_case ( UpperCAmelCase ): __magic_name__ = ['''input_features''', '''attention_mask'''] def __init__( self : Optional[int] , A : Optional[Any]=8_0 , A : str=1_6_0_0_0 , A : List[str]=0.0 , A : Any=1_0 , A : Union[str, Any]=2_5 , A : str="hamming_window" , A : str=3_27_68.0 , A : Union[str, Any]=0.97 , A : Dict=1.0 , A : Any=True , A : Union[str, Any]=True , A : List[Any]=False , **A : Tuple , ): '''simple docstring''' super().__init__(feature_size=A , sampling_rate=A , padding_value=A , **A ) a : Any = feature_size a : List[Any] = sampling_rate a : Any = padding_value a : str = hop_length a : Any = win_length a : List[Any] = frame_signal_scale a : Tuple = preemphasis_coeff a : Dict = mel_floor a : Optional[int] = normalize_means a : List[str] = normalize_vars a : Dict = win_function a : Union[str, Any] = return_attention_mask a : List[Any] = win_length * sampling_rate // 1_0_0_0 a : Tuple = hop_length * sampling_rate // 1_0_0_0 a : List[Any] = optimal_fft_length(self.sample_size ) a : Any = (self.n_fft // 2) + 1 def lowerCamelCase__ ( self : List[Any] , A : np.array ): '''simple docstring''' if self.win_function == "hamming_window": a : List[str] = window_function(window_length=self.sample_size , name=self.win_function , periodic=A ) else: a : Dict = window_function(window_length=self.sample_size , name=self.win_function ) a : str = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) a : List[Any] = spectrogram( one_waveform * self.frame_signal_scale , window=A , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=A , preemphasis=self.preemphasis_coeff , mel_filters=A , mel_floor=self.mel_floor , log_mel='log' , ) return msfc_features.T def lowerCamelCase__ ( self : int , A : Tuple , A : int , A : Optional[int] ): '''simple docstring''' if self.normalize_means: a : Any = x[:input_length].mean(axis=0 ) a : Dict = np.subtract(A , A ) if self.normalize_vars: a : Dict = x[:input_length].std(axis=0 ) a : Dict = np.divide(A , A ) if input_length < x.shape[0]: a : Dict = padding_value # make sure array is in float32 a : Optional[int] = x.astype(np.floataa ) return x def lowerCamelCase__ ( self : str , A : List[np.ndarray] , A : Optional[np.ndarray] = None ): '''simple docstring''' a : str = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(A , A , self.padding_value ) for x, n in zip(A , A )] def __call__( self : Dict , A : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , A : Union[bool, str, PaddingStrategy] = False , A : Optional[int] = None , A : bool = False , A : Optional[int] = None , A : Optional[bool] = None , A : Optional[Union[str, TensorType]] = None , A : Optional[int] = None , **A : Union[str, Any] , ): '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) a : Optional[int] = isinstance(A , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) a : Dict = is_batched_numpy or ( isinstance(A , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: a : str = [np.asarray(A , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(A , np.ndarray ): a : List[str] = np.asarray(A , dtype=np.floataa ) elif isinstance(A , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): a : Optional[int] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: a : Any = [raw_speech] # extract fbank features a : str = [self._extract_mfsc_features(A ) for one_waveform in raw_speech] # convert into correct format for padding a : int = BatchFeature({'input_features': features} ) a : Union[str, Any] = self.pad( A , padding=A , max_length=A , truncation=A , pad_to_multiple_of=A , return_attention_mask=A , **A , ) # make sure list is in array format a : Optional[Any] = padded_inputs.get('input_features' ) if isinstance(input_features[0] , A ): a : List[str] = [np.asarray(A , dtype=np.floataa ) for feature in input_features] a : List[Any] = padded_inputs.get('attention_mask' ) if attention_mask is not None: a : int = [np.asarray(A , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: a : Any = ( np.array(A , dtype=np.intaa ) if self._get_padding_strategies(A , max_length=A ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) a : List[str] = self.normalize( padded_inputs['input_features'] , attention_mask=A ) if return_tensors is not None: a : Optional[int] = padded_inputs.convert_to_tensors(A ) return padded_inputs
186
0
def UpperCamelCase ( _A ): """simple docstring""" __magic_name__ : str = [1] __magic_name__ ,__magic_name__ ,__magic_name__ : Dict = 0, 0, 0 __magic_name__ : List[Any] = ugly_nums[ia] * 2 __magic_name__ : Dict = ugly_nums[ia] * 3 __magic_name__ : str = ugly_nums[ia] * 5 for _ in range(1, _A ): __magic_name__ : int = min(_A, _A, _A ) ugly_nums.append(_A ) if next_num == next_a: ia += 1 __magic_name__ : str = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 __magic_name__ : int = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 __magic_name__ : Optional[Any] = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(F"""{ugly_numbers(200) = }""")
342
import argparse import torch from transformers import MobileBertConfig, MobileBertForPreTraining, load_tf_weights_in_mobilebert from transformers.utils import logging logging.set_verbosity_info() def UpperCamelCase ( _A, _A, _A ): """simple docstring""" __magic_name__ : Dict = MobileBertConfig.from_json_file(_A ) print(f'Building PyTorch model from configuration: {config}' ) __magic_name__ : Tuple = MobileBertForPreTraining(_A ) # Load weights from tf checkpoint __magic_name__ : int = 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__": __magic_name__: 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." ) __magic_name__: Dict = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.mobilebert_config_file, args.pytorch_dump_path)
342
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import _LazyModule SCREAMING_SNAKE_CASE_: int ={'tokenization_bertweet': ['BertweetTokenizer']} if TYPE_CHECKING: from .tokenization_bertweet import BertweetTokenizer else: import sys SCREAMING_SNAKE_CASE_: int =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
106
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging SCREAMING_SNAKE_CASE_: Dict =logging.get_logger(__name__) class __A ( UpperCamelCase__ ): a__ : Any = ["""pixel_values"""] def __init__(self : Any , __a : bool = True , __a : Optional[Dict[str, int]] = None , __a : PILImageResampling = PILImageResampling.BICUBIC , __a : bool = True , __a : bool = True , __a : Union[int, float] = 1 / 255 , __a : Dict[str, int] = None , __a : bool = True , __a : Optional[Union[float, List[float]]] = None , __a : Optional[Union[float, List[float]]] = None , **__a : Dict , ): super().__init__(**__a ) UpperCAmelCase_ = size if size is not None else {"height": 224, "width": 224} UpperCAmelCase_ = get_size_dict(__a ) UpperCAmelCase_ = crop_size if crop_size is not None else {"height": 224, "width": 224} UpperCAmelCase_ = get_size_dict(__a , default_to_square=__a , param_name="crop_size" ) UpperCAmelCase_ = do_resize UpperCAmelCase_ = do_rescale UpperCAmelCase_ = do_normalize UpperCAmelCase_ = do_center_crop UpperCAmelCase_ = crop_size UpperCAmelCase_ = size UpperCAmelCase_ = resample UpperCAmelCase_ = rescale_factor UpperCAmelCase_ = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN UpperCAmelCase_ = image_std if image_std is not None else IMAGENET_DEFAULT_STD def _lowercase (self : Tuple , __a : np.ndarray , __a : Dict[str, int] , __a : PILImageResampling = PILImageResampling.BILINEAR , __a : Optional[Union[str, ChannelDimension]] = None , **__a : Optional[Any] , ): UpperCAmelCase_ = get_size_dict(__a ) if "shortest_edge" in size: UpperCAmelCase_ = get_resize_output_image_size(__a , size=size["shortest_edge"] , default_to_square=__a ) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: UpperCAmelCase_ = (size["height"], size["width"]) else: raise ValueError(f"""Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got {size.keys()}""" ) return resize(__a , size=__a , resample=__a , data_format=__a , **__a ) def _lowercase (self : List[Any] , __a : np.ndarray , __a : Dict[str, int] , __a : Optional[Union[str, ChannelDimension]] = None , **__a : int , ): UpperCAmelCase_ = get_size_dict(__a ) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}""" ) return center_crop(__a , size=(size["height"], size["width"]) , data_format=__a , **__a ) def _lowercase (self : str , __a : np.ndarray , __a : float , __a : Optional[Union[str, ChannelDimension]] = None , **__a : List[str] ): return rescale(__a , scale=__a , data_format=__a , **__a ) def _lowercase (self : Any , __a : np.ndarray , __a : Union[float, List[float]] , __a : Union[float, List[float]] , __a : Optional[Union[str, ChannelDimension]] = None , **__a : Dict , ): return normalize(__a , mean=__a , std=__a , data_format=__a , **__a ) def _lowercase (self : Dict , __a : ImageInput , __a : Optional[bool] = None , __a : Dict[str, int] = None , __a : PILImageResampling = None , __a : bool = None , __a : int = None , __a : Optional[bool] = None , __a : Optional[float] = None , __a : Optional[bool] = None , __a : Optional[Union[float, List[float]]] = None , __a : Optional[Union[float, List[float]]] = None , __a : Optional[Union[str, TensorType]] = None , __a : Union[str, ChannelDimension] = ChannelDimension.FIRST , **__a : Dict , ): UpperCAmelCase_ = do_resize if do_resize is not None else self.do_resize UpperCAmelCase_ = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase_ = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase_ = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCAmelCase_ = crop_size if crop_size is not None else self.crop_size UpperCAmelCase_ = get_size_dict(__a , param_name="crop_size" , default_to_square=__a ) UpperCAmelCase_ = resample if resample is not None else self.resample UpperCAmelCase_ = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase_ = image_mean if image_mean is not None else self.image_mean UpperCAmelCase_ = image_std if image_std is not None else self.image_std UpperCAmelCase_ = size if size is not None else self.size UpperCAmelCase_ = get_size_dict(__a ) if not is_batched(__a ): UpperCAmelCase_ = [images] if not valid_images(__a ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) # All transformations expect numpy arrays. UpperCAmelCase_ = [to_numpy_array(__a ) for image in images] if do_resize: UpperCAmelCase_ = [self.resize(image=__a , size=__a , resample=__a ) for image in images] if do_center_crop: UpperCAmelCase_ = [self.center_crop(image=__a , size=__a ) for image in images] if do_rescale: UpperCAmelCase_ = [self.rescale(image=__a , scale=__a ) for image in images] if do_normalize: UpperCAmelCase_ = [self.normalize(image=__a , mean=__a , std=__a ) for image in images] UpperCAmelCase_ = [to_channel_dimension_format(__a , __a ) for image in images] UpperCAmelCase_ = {"pixel_values": images} return BatchFeature(data=__a , tensor_type=__a )
106
1
"""simple docstring""" from typing import List import numpy as np def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = {key: len(__lowercase ) for key, value in gen_kwargs.items() if isinstance(__lowercase , __lowercase )} if len(set(lists_lengths.values() ) ) > 1: raise RuntimeError( ( "Sharding is ambiguous for this dataset: " + "we found several data sources lists of different lengths, and we don't know over which list we should parallelize:\n" + "\n".join(F"\t- key {key} has length {length}" for key, length in lists_lengths.items() ) + "\nTo fix this, check the 'gen_kwargs' and make sure to use lists only for data sources, " + "and use tuples otherwise. In the end there should only be one single list, or several lists with the same length." ) ) UpperCamelCase = max(lists_lengths.values() , default=0 ) return max(1 , __lowercase ) def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = [] for group_idx in range(__lowercase ): UpperCamelCase = num_shards // max_num_jobs + (group_idx < (num_shards % max_num_jobs)) if num_shards_to_add == 0: break UpperCamelCase = shards_indices_per_group[-1].stop if shards_indices_per_group else 0 UpperCamelCase = range(__lowercase , start + num_shards_to_add ) shards_indices_per_group.append(__lowercase ) return shards_indices_per_group def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = _number_of_shards_in_gen_kwargs(__lowercase ) if num_shards == 1: return [dict(__lowercase )] else: UpperCamelCase = _distribute_shards(num_shards=__lowercase , max_num_jobs=__lowercase ) return [ { key: [value[shard_idx] for shard_idx in shard_indices_per_group[group_idx]] if isinstance(__lowercase , __lowercase ) else value for key, value in gen_kwargs.items() } for group_idx in range(len(__lowercase ) ) ] def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" return { key: [value for gen_kwargs in gen_kwargs_list for value in gen_kwargs[key]] if isinstance(gen_kwargs_list[0][key] , __lowercase ) else gen_kwargs_list[0][key] for key in gen_kwargs_list[0] } def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = {len(__lowercase ) for value in gen_kwargs.values() if isinstance(__lowercase , __lowercase )} UpperCamelCase = {} for size in list_sizes: UpperCamelCase = list(range(__lowercase ) ) rng.shuffle(indices_per_size[size] ) # Now let's copy the gen_kwargs and shuffle the lists based on their sizes UpperCamelCase = dict(__lowercase ) for key, value in shuffled_kwargs.items(): if isinstance(__lowercase , __lowercase ): UpperCamelCase = [value[i] for i in indices_per_size[len(__lowercase )]] return shuffled_kwargs
153
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class _UpperCAmelCase ( snake_case_ ): """simple docstring""" snake_case = 0 snake_case = False snake_case = 3.0 class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {"a": 2} ) self.assertDictEqual(MockClass(a=2 , b=__UpperCAmelCase ).to_kwargs() , {"a": 2, "b": True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {"a": 2, "c": 2.25} ) @require_cuda def lowerCAmelCase ( self : int ): '''simple docstring''' _A = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() _A = Accelerator(mixed_precision="fp16" , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) _A = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , __UpperCAmelCase ) @require_multi_gpu def lowerCAmelCase ( self : List[Any] ): '''simple docstring''' _A = ["torchrun", f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(__UpperCAmelCase , env=os.environ.copy() ) if __name__ == "__main__": lowerCamelCase_ = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowerCamelCase_ = Accelerator(kwargs_handlers=[ddp_scaler]) lowerCamelCase_ = torch.nn.Linear(1_00, 2_00) lowerCamelCase_ = accelerator.prepare(model) # Check the values changed in kwargs lowerCamelCase_ = '''''' lowerCamelCase_ = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += F"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += F"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += F"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += F"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += F"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
79
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class UpperCAmelCase (_UpperCAmelCase ,unittest.TestCase ): """simple docstring""" _UpperCAmelCase :Dict = ShapEImgaImgPipeline _UpperCAmelCase :Union[str, Any] = ["image"] _UpperCAmelCase :Dict = ["image"] _UpperCAmelCase :Any = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] _UpperCAmelCase :Dict = False @property def _snake_case ( self ): return 32 @property def _snake_case ( self ): return 32 @property def _snake_case ( self ): return self.time_input_dim * 4 @property def _snake_case ( self ): return 8 @property def _snake_case ( self ): torch.manual_seed(0 ) lowercase__: Union[str, Any] = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=64 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=1 , ) lowercase__: Dict = CLIPVisionModel(_UpperCAmelCase ) return model @property def _snake_case ( self ): lowercase__: List[str] = CLIPImageProcessor( crop_size=224 , do_center_crop=_UpperCAmelCase , do_normalize=_UpperCAmelCase , do_resize=_UpperCAmelCase , image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073] , image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711] , resample=3 , size=224 , ) return image_processor @property def _snake_case ( self ): torch.manual_seed(0 ) lowercase__: List[str] = { '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } lowercase__: str = PriorTransformer(**_UpperCAmelCase ) return model @property def _snake_case ( self ): torch.manual_seed(0 ) lowercase__: Dict = { '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } lowercase__: str = ShapERenderer(**_UpperCAmelCase ) return model def _snake_case ( self ): lowercase__: int = self.dummy_prior lowercase__: Optional[int] = self.dummy_image_encoder lowercase__: List[Any] = self.dummy_image_processor lowercase__: Union[str, Any] = self.dummy_renderer lowercase__: Tuple = HeunDiscreteScheduler( beta_schedule='''exp''' , num_train_timesteps=1024 , prediction_type='''sample''' , use_karras_sigmas=_UpperCAmelCase , clip_sample=_UpperCAmelCase , clip_sample_range=1.0 , ) lowercase__: Dict = { '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase=0 ): lowercase__: Tuple = floats_tensor((1, 3, 64, 64) , rng=random.Random(_UpperCAmelCase ) ).to(_UpperCAmelCase ) if str(_UpperCAmelCase ).startswith('''mps''' ): lowercase__: Union[str, Any] = torch.manual_seed(_UpperCAmelCase ) else: lowercase__: str = torch.Generator(device=_UpperCAmelCase ).manual_seed(_UpperCAmelCase ) lowercase__: Optional[int] = { '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def _snake_case ( self ): lowercase__: str = '''cpu''' lowercase__: Optional[Any] = self.get_dummy_components() lowercase__: Dict = self.pipeline_class(**_UpperCAmelCase ) lowercase__: Any = pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: str = pipe(**self.get_dummy_inputs(_UpperCAmelCase ) ) lowercase__: Tuple = output.images[0] lowercase__: Any = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowercase__: Tuple = np.array( [ 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, 0.00_039_216, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _snake_case ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def _snake_case ( self ): lowercase__: List[str] = torch_device == '''cpu''' lowercase__: str = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=_UpperCAmelCase , relax_max_difference=_UpperCAmelCase , ) def _snake_case ( self ): lowercase__: List[str] = self.get_dummy_components() lowercase__: Any = self.pipeline_class(**_UpperCAmelCase ) lowercase__: Any = pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: Union[str, Any] = 1 lowercase__: List[str] = 2 lowercase__: Any = self.get_dummy_inputs(_UpperCAmelCase ) for key in inputs.keys(): if key in self.batch_params: lowercase__: Dict = batch_size * [inputs[key]] lowercase__: List[str] = pipe(**_UpperCAmelCase , num_images_per_prompt=_UpperCAmelCase )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class UpperCAmelCase (unittest.TestCase ): """simple docstring""" def _snake_case ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _snake_case ( self ): lowercase__: Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) lowercase__: int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) lowercase__: List[str] = ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) lowercase__: List[Any] = pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: List[Any] = torch.Generator(device=_UpperCAmelCase ).manual_seed(0 ) lowercase__: Optional[Any] = pipe( _UpperCAmelCase , generator=_UpperCAmelCase , guidance_scale=3.0 , num_inference_steps=64 , frame_size=64 , output_type='''np''' , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(_UpperCAmelCase , _UpperCAmelCase )
2
"""simple docstring""" import unittest from transformers import DonutProcessor __A = "naver-clova-ix/donut-base" class UpperCAmelCase (unittest.TestCase ): """simple docstring""" def _snake_case ( self ): lowercase__: int = DonutProcessor.from_pretrained(_UpperCAmelCase ) def _snake_case ( self ): lowercase__: Tuple = { '''name''': '''John Doe''', '''age''': '''99''', '''city''': '''Atlanta''', '''state''': '''GA''', '''zip''': '''30301''', '''phone''': '''123-4567''', '''nicknames''': [{'''nickname''': '''Johnny'''}, {'''nickname''': '''JD'''}], } lowercase__: Union[str, Any] = ( '''<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>''' ) lowercase__: str = self.processor.tokenajson(_UpperCAmelCase ) self.assertDictEqual(_UpperCAmelCase , _UpperCAmelCase )
2
1
"""simple docstring""" # coding=utf-8 # Copyright 2020 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import sys import transformers _A = """3""" print("""Python version:""", sys.version) print("""transformers version:""", transformers.__version__) try: import torch print("""Torch version:""", torch.__version__) print("""Cuda available:""", torch.cuda.is_available()) print("""Cuda version:""", torch.version.cuda) print("""CuDNN version:""", torch.backends.cudnn.version()) print("""Number of GPUs available:""", torch.cuda.device_count()) print("""NCCL version:""", torch.cuda.nccl.version()) except ImportError: print("""Torch version:""", None) try: import deepspeed print("""DeepSpeed version:""", deepspeed.__version__) except ImportError: print("""DeepSpeed version:""", None) try: import tensorflow as tf print("""TensorFlow version:""", tf.__version__) print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU"""))) print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU"""))) except ImportError: print("""TensorFlow version:""", None)
242
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 convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCAmelCase__ = logging.get_logger(__name__) class a__ ( snake_case ): """simple docstring""" __lowerCamelCase = ['pixel_values'] def __init__( self , lowercase = True , lowercase = None , lowercase = PILImageResampling.BICUBIC , lowercase = True , lowercase = 1 / 255 , lowercase = True , lowercase = None , lowercase = None , lowercase = True , **lowercase , ) -> None: '''simple docstring''' super().__init__(**lowercase ) A__ = size if size is not None else {"height": 384, "width": 384} A__ = get_size_dict(lowercase , default_to_square=lowercase ) A__ = do_resize A__ = size A__ = resample A__ = do_rescale A__ = rescale_factor A__ = do_normalize A__ = image_mean if image_mean is not None else OPENAI_CLIP_MEAN A__ = image_std if image_std is not None else OPENAI_CLIP_STD A__ = do_convert_rgb def UpperCamelCase ( self , lowercase , lowercase , lowercase = PILImageResampling.BICUBIC , lowercase = None , **lowercase , ) -> np.ndarray: '''simple docstring''' A__ = get_size_dict(lowercase , default_to_square=lowercase ) if "height" not in size or "width" not in size: raise ValueError(F'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}' ) A__ = (size["height"], size["width"]) return resize(lowercase , size=lowercase , resample=lowercase , data_format=lowercase , **lowercase ) def UpperCamelCase ( self , lowercase , lowercase , lowercase = None , **lowercase , ) -> Optional[Any]: '''simple docstring''' return rescale(lowercase , scale=lowercase , data_format=lowercase , **lowercase ) def UpperCamelCase ( self , lowercase , lowercase , lowercase , lowercase = None , **lowercase , ) -> np.ndarray: '''simple docstring''' return normalize(lowercase , mean=lowercase , std=lowercase , data_format=lowercase , **lowercase ) def UpperCamelCase ( self , lowercase , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = ChannelDimension.FIRST , **lowercase , ) -> PIL.Image.Image: '''simple docstring''' A__ = do_resize if do_resize is not None else self.do_resize A__ = resample if resample is not None else self.resample A__ = do_rescale if do_rescale is not None else self.do_rescale A__ = rescale_factor if rescale_factor is not None else self.rescale_factor A__ = do_normalize if do_normalize is not None else self.do_normalize A__ = image_mean if image_mean is not None else self.image_mean A__ = image_std if image_std is not None else self.image_std A__ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb A__ = size if size is not None else self.size A__ = get_size_dict(lowercase , default_to_square=lowercase ) A__ = make_list_of_images(lowercase ) if not valid_images(lowercase ): 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_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # PIL RGBA images are converted to RGB if do_convert_rgb: A__ = [convert_to_rgb(lowercase ) for image in images] # All transformations expect numpy arrays. A__ = [to_numpy_array(lowercase ) for image in images] if do_resize: A__ = [self.resize(image=lowercase , size=lowercase , resample=lowercase ) for image in images] if do_rescale: A__ = [self.rescale(image=lowercase , scale=lowercase ) for image in images] if do_normalize: A__ = [self.normalize(image=lowercase , mean=lowercase , std=lowercase ) for image in images] A__ = [to_channel_dimension_format(lowercase , lowercase ) for image in images] A__ = BatchFeature(data={"pixel_values": images} , tensor_type=lowercase ) return encoded_outputs
68
0
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings _A = r""" [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `\" / \"`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `\" // \"`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `\"wiki_dpr\"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `\"train\"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `\"compressed\"`) The index name of the index associated with the `dataset`. One can choose between `\"legacy\"`, `\"exact\"` and `\"compressed\"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a \"dummy\" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. """ @add_start_docstrings(a_ ) class _lowerCamelCase ( a_ ): _lowerCamelCase :Optional[int] = "rag" _lowerCamelCase :Union[str, Any] = True def __init__( self : Union[str, Any] , UpperCamelCase : int=None , UpperCamelCase : Optional[Any]=True , UpperCamelCase : List[Any]=None , UpperCamelCase : List[Any]=None , UpperCamelCase : str=None , UpperCamelCase : Optional[int]=None , UpperCamelCase : Union[str, Any]=None , UpperCamelCase : Union[str, Any]=" / " , UpperCamelCase : Optional[int]=" // " , UpperCamelCase : str=5 , UpperCamelCase : str=3_00 , UpperCamelCase : Any=7_68 , UpperCamelCase : Union[str, Any]=8 , UpperCamelCase : List[Any]="wiki_dpr" , UpperCamelCase : Optional[int]="train" , UpperCamelCase : int="compressed" , UpperCamelCase : List[Any]=None , UpperCamelCase : Optional[Any]=None , UpperCamelCase : List[Any]=False , UpperCamelCase : int=False , UpperCamelCase : Any=0.0 , UpperCamelCase : Dict=True , UpperCamelCase : List[str]=False , UpperCamelCase : Any=False , UpperCamelCase : Optional[int]=False , UpperCamelCase : str=True , UpperCamelCase : Any=None , **UpperCamelCase : str , ) -> List[Any]: """simple docstring""" super().__init__( bos_token_id=UpperCamelCase , pad_token_id=UpperCamelCase , eos_token_id=UpperCamelCase , decoder_start_token_id=UpperCamelCase , forced_eos_token_id=UpperCamelCase , is_encoder_decoder=UpperCamelCase , prefix=UpperCamelCase , vocab_size=UpperCamelCase , **UpperCamelCase , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" lowerCAmelCase__ : Any = kwargs.pop("""question_encoder""" ) lowerCAmelCase__ : Any = question_encoder_config.pop("""model_type""" ) lowerCAmelCase__ : Tuple = kwargs.pop("""generator""" ) lowerCAmelCase__ : Optional[int] = decoder_config.pop("""model_type""" ) from ..auto.configuration_auto import AutoConfig lowerCAmelCase__ : Union[str, Any] = AutoConfig.for_model(UpperCamelCase , **UpperCamelCase ) lowerCAmelCase__ : Tuple = AutoConfig.for_model(UpperCamelCase , **UpperCamelCase ) lowerCAmelCase__ : List[str] = reduce_loss lowerCAmelCase__ : Any = label_smoothing lowerCAmelCase__ : int = exclude_bos_score lowerCAmelCase__ : Tuple = do_marginalize lowerCAmelCase__ : List[str] = title_sep lowerCAmelCase__ : Dict = doc_sep lowerCAmelCase__ : Union[str, Any] = n_docs lowerCAmelCase__ : str = max_combined_length lowerCAmelCase__ : Any = dataset lowerCAmelCase__ : List[Any] = dataset_split lowerCAmelCase__ : List[str] = index_name lowerCAmelCase__ : Tuple = retrieval_vector_size lowerCAmelCase__ : Any = retrieval_batch_size lowerCAmelCase__ : Optional[int] = passages_path lowerCAmelCase__ : str = index_path lowerCAmelCase__ : Tuple = use_dummy_dataset lowerCAmelCase__ : Optional[Any] = output_retrieved lowerCAmelCase__ : Dict = do_deduplication lowerCAmelCase__ : Optional[int] = use_cache if self.forced_eos_token_id is None: lowerCAmelCase__ : Optional[int] = getattr(self.generator , """forced_eos_token_id""" , UpperCamelCase ) @classmethod def _lowerCAmelCase ( cls : Dict , UpperCamelCase : PretrainedConfig , UpperCamelCase : PretrainedConfig , **UpperCamelCase : Dict ) -> PretrainedConfig: """simple docstring""" return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **UpperCamelCase ) def _lowerCAmelCase ( self : int ) -> str: """simple docstring""" lowerCAmelCase__ : List[str] = copy.deepcopy(self.__dict__ ) lowerCAmelCase__ : List[Any] = self.question_encoder.to_dict() lowerCAmelCase__ : List[Any] = self.generator.to_dict() lowerCAmelCase__ : str = self.__class__.model_type return output
365
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _A = logging.get_logger(__name__) _A = {"""vocab_file""": """vocab.json"""} _A = { """vocab_file""": { """mgp-str""": """https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json""", } } _A = {"""mgp-str""": 2_7} class _lowerCamelCase ( a_ ): _lowerCamelCase :Dict = VOCAB_FILES_NAMES _lowerCamelCase :Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase :Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : Optional[int] , UpperCamelCase : int , UpperCamelCase : Union[str, Any]="[GO]" , UpperCamelCase : Any="[GO]" , UpperCamelCase : Tuple="[s]" , UpperCamelCase : List[Any]="[GO]" , **UpperCamelCase : Dict ) -> Union[str, Any]: """simple docstring""" super().__init__( unk_token=UpperCamelCase , bos_token=UpperCamelCase , eos_token=UpperCamelCase , pad_token=UpperCamelCase , **UpperCamelCase , ) with open(UpperCamelCase , encoding="""utf-8""" ) as vocab_handle: lowerCAmelCase__ : Optional[Any] = json.load(UpperCamelCase ) lowerCAmelCase__ : Union[str, Any] = {v: k for k, v in self.vocab.items()} @property def _lowerCAmelCase ( self : int ) -> Dict: """simple docstring""" return len(self.vocab ) def _lowerCAmelCase ( self : Dict ) -> Any: """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def _lowerCAmelCase ( self : List[Any] , UpperCamelCase : Tuple ) -> Optional[Any]: """simple docstring""" lowerCAmelCase__ : Optional[Any] = [] for s in text: char_tokens.extend(UpperCamelCase ) return char_tokens def _lowerCAmelCase ( self : List[Any] , UpperCamelCase : str ) -> Tuple: """simple docstring""" return self.vocab.get(UpperCamelCase , self.vocab.get(self.unk_token ) ) def _lowerCAmelCase ( self : List[str] , UpperCamelCase : int ) -> Any: """simple docstring""" return self.decoder.get(UpperCamelCase ) def _lowerCAmelCase ( self : Dict , UpperCamelCase : str , UpperCamelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(UpperCamelCase ): logger.error("""Vocabulary path ({}) should be a directory""".format(UpperCamelCase ) ) return lowerCAmelCase__ : Union[str, Any] = os.path.join( UpperCamelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) with open(UpperCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=UpperCamelCase , ensure_ascii=UpperCamelCase ) + """\n""" ) return (vocab_file,)
212
0
'''simple docstring''' from transformers import BertTokenizerFast from .custom_tokenization import CustomTokenizer class lowercase ( A__ ): """simple docstring""" _a = CustomTokenizer pass
97
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" @parameterized.expand([(None,), ('''foo.json''',)] ) def _snake_case ( self , _SCREAMING_SNAKE_CASE )->Tuple: '''simple docstring''' A_ : Dict = GenerationConfig( do_sample=_SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) A_ : Tuple = GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , _SCREAMING_SNAKE_CASE ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , _SCREAMING_SNAKE_CASE ) def _snake_case ( self )->Dict: '''simple docstring''' A_ : str = AutoConfig.from_pretrained('''gpt2''' ) A_ : int = GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE ) A_ : Dict = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def _snake_case ( self )->Optional[int]: '''simple docstring''' A_ : List[Any] = GenerationConfig() A_ : Optional[Any] = { '''max_new_tokens''': 1024, '''foo''': '''bar''', } A_ : Dict = copy.deepcopy(_SCREAMING_SNAKE_CASE ) A_ : Optional[int] = generation_config.update(**_SCREAMING_SNAKE_CASE ) # update_kwargs was not modified (no side effects) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(_SCREAMING_SNAKE_CASE , {'''foo''': '''bar'''} ) def _snake_case ( self )->str: '''simple docstring''' A_ : List[str] = GenerationConfig() A_ : int = '''bar''' with tempfile.TemporaryDirectory('''test-generation-config''' ) as tmp_dir: generation_config.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ : Optional[Any] = GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , '''bar''' ) A_ : Optional[int] = GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE ) assert not hasattr(_SCREAMING_SNAKE_CASE , '''foo''' ) # no new kwargs should be initialized if from config def _snake_case ( self )->List[Any]: '''simple docstring''' A_ : str = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , _SCREAMING_SNAKE_CASE ) self.assertEqual(default_config.num_beams , 1 ) A_ : str = GenerationConfig( do_sample=_SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , _SCREAMING_SNAKE_CASE ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ : Dict = GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , _SCREAMING_SNAKE_CASE ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" @classmethod def _snake_case ( cls )->Optional[int]: '''simple docstring''' A_ : str = TOKEN HfFolder.save_token(_SCREAMING_SNAKE_CASE ) @classmethod def _snake_case ( cls )->List[Any]: '''simple docstring''' try: delete_repo(token=cls._token , repo_id='''test-generation-config''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-generation-config-org''' ) except HTTPError: pass def _snake_case ( self )->List[Any]: '''simple docstring''' A_ : List[Any] = GenerationConfig( do_sample=_SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''test-generation-config''' , use_auth_token=self._token ) A_ : Optional[int] = GenerationConfig.from_pretrained(F'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-generation-config''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( _SCREAMING_SNAKE_CASE , repo_id='''test-generation-config''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ : Optional[Any] = GenerationConfig.from_pretrained(F'''{USER}/test-generation-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def _snake_case ( self )->List[str]: '''simple docstring''' A_ : Tuple = GenerationConfig( do_sample=_SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''valid_org/test-generation-config-org''' , use_auth_token=self._token ) A_ : Union[str, Any] = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-generation-config-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( _SCREAMING_SNAKE_CASE , repo_id='''valid_org/test-generation-config-org''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ : Dict = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) )
186
0
'''simple docstring''' import math from datetime import datetime, timedelta def __magic_name__ ( A ) -> datetime: snake_case = year % 1_9 snake_case = year % 4 snake_case = year % 7 snake_case = math.floor(year / 1_0_0 ) snake_case = math.floor((1_3 + 8 * leap_day_inhibits) / 2_5 ) snake_case = leap_day_inhibits / 4 snake_case = ( 1_5 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 3_0 snake_case = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 snake_case = (1_9 * metonic_cycle + secular_moon_shift) % 3_0 # PHM -> Paschal Full Moon snake_case = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 2_9 and days_from_phm_to_sunday == 6: return datetime(_lowercase , 4 , 1_9 ) elif days_to_add == 2_8 and days_from_phm_to_sunday == 6: return datetime(_lowercase , 4 , 1_8 ) else: return datetime(_lowercase , 3 , 2_2 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1_9_9_4, 2_0_0_0, 2_0_1_0, 2_0_2_1, 2_0_2_3): lowerCAmelCase_ = '''will be''' if year > datetime.now().year else '''was''' print(f"Easter in {year} {tense} {gauss_easter(year)}")
363
'''simple docstring''' import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def __magic_name__ ( A ) -> Tuple: snake_case = [] embed.append( ( F'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight''', F'''stage{idx}.patch_embed.proj.weight''', ) ) embed.append( ( F'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias''', F'''stage{idx}.patch_embed.proj.bias''', ) ) embed.append( ( F'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight''', F'''stage{idx}.patch_embed.norm.weight''', ) ) embed.append( ( F'''cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias''', F'''stage{idx}.patch_embed.norm.bias''', ) ) return embed def __magic_name__ ( A , A ) -> Optional[int]: snake_case = [] attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked''', F'''stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight''', F'''stage{idx}.blocks.{cnt}.attn.proj_q.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias''', F'''stage{idx}.blocks.{cnt}.attn.proj_q.bias''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight''', F'''stage{idx}.blocks.{cnt}.attn.proj_k.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias''', F'''stage{idx}.blocks.{cnt}.attn.proj_k.bias''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight''', F'''stage{idx}.blocks.{cnt}.attn.proj_v.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias''', F'''stage{idx}.blocks.{cnt}.attn.proj_v.bias''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight''', F'''stage{idx}.blocks.{cnt}.attn.proj.weight''', ) ) attention_weights.append( ( F'''cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias''', F'''stage{idx}.blocks.{cnt}.attn.proj.bias''', ) ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight''', F'''stage{idx}.blocks.{cnt}.mlp.fc1.weight''') ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias''', F'''stage{idx}.blocks.{cnt}.mlp.fc1.bias''') ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight''', F'''stage{idx}.blocks.{cnt}.mlp.fc2.weight''') ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias''', F'''stage{idx}.blocks.{cnt}.mlp.fc2.bias''') ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight''', F'''stage{idx}.blocks.{cnt}.norm1.weight''') ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias''', F'''stage{idx}.blocks.{cnt}.norm1.bias''') ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight''', F'''stage{idx}.blocks.{cnt}.norm2.weight''') ) attention_weights.append( (F'''cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias''', F'''stage{idx}.blocks.{cnt}.norm2.bias''') ) return attention_weights def __magic_name__ ( A ) -> List[Any]: snake_case = [] token.append((F'''cvt.encoder.stages.{idx}.cls_token''', 'stage2.cls_token') ) return token def __magic_name__ ( ) -> Dict: snake_case = [] head.append(('layernorm.weight', 'norm.weight') ) head.append(('layernorm.bias', 'norm.bias') ) head.append(('classifier.weight', 'head.weight') ) head.append(('classifier.bias', 'head.bias') ) return head def __magic_name__ ( A , A , A , A ) -> int: snake_case = 'imagenet-1k-id2label.json' snake_case = 1_0_0_0 snake_case = 'huggingface/label-files' snake_case = num_labels snake_case = json.load(open(cached_download(hf_hub_url(A , A , repo_type='dataset' ) ) , 'r' ) ) snake_case = {int(A ): v for k, v in idalabel.items()} snake_case = idalabel snake_case = {v: k for k, v in idalabel.items()} snake_case = snake_case = CvtConfig(num_labels=A , idalabel=A , labelaid=A ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit('/' , 1 )[-1][4:6] == "13": snake_case = [1, 2, 1_0] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit('/' , 1 )[-1][4:6] == "21": snake_case = [1, 4, 1_6] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: snake_case = [2, 2, 2_0] snake_case = [3, 1_2, 1_6] snake_case = [1_9_2, 7_6_8, 1_0_2_4] snake_case = CvtForImageClassification(A ) snake_case = AutoImageProcessor.from_pretrained('facebook/convnext-base-224-22k-1k' ) snake_case = image_size snake_case = torch.load(A , map_location=torch.device('cpu' ) ) snake_case = OrderedDict() snake_case = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: snake_case = list_of_state_dict + cls_token(A ) snake_case = list_of_state_dict + embeddings(A ) for cnt in range(config.depth[idx] ): snake_case = list_of_state_dict + attention(A , A ) snake_case = list_of_state_dict + final() for gg in list_of_state_dict: print(A ) for i in range(len(A ) ): snake_case = original_weights[list_of_state_dict[i][1]] model.load_state_dict(A ) model.save_pretrained(A ) image_processor.save_pretrained(A ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument( "--cvt_model", default="cvt-w24", type=str, help="Name of the cvt model you'd like to convert.", ) parser.add_argument( "--image_size", default=3_8_4, type=int, help="Input Image Size", ) parser.add_argument( "--cvt_file_name", default=r"cvtmodels\CvT-w24-384x384-IN-22k.pth", type=str, help="Input Image Size", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) lowerCAmelCase_ = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
332
0
"""simple docstring""" def __SCREAMING_SNAKE_CASE ( A_ , A_ ): lowerCAmelCase__ : list[list[str]] = [[] for _ in range(A_ )] lowerCAmelCase__ : Union[str, Any] = key - 1 if key <= 0: raise ValueError('''Height of grid can\'t be 0 or negative''' ) if key == 1 or len(A_ ) <= key: return input_string for position, character in enumerate(A_ ): lowerCAmelCase__ : Union[str, Any] = position % (lowest * 2) # puts it in bounds lowerCAmelCase__ : Optional[int] = min(A_ , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append(A_ ) lowerCAmelCase__ : str = [''''''.join(A_ ) for row in temp_grid] lowerCAmelCase__ : int = ''''''.join(A_ ) return output_string def __SCREAMING_SNAKE_CASE ( A_ , A_ ): lowerCAmelCase__ : Dict = [] lowerCAmelCase__ : int = key - 1 if key <= 0: raise ValueError('''Height of grid can\'t be 0 or negative''' ) if key == 1: return input_string lowerCAmelCase__ : list[list[str]] = [[] for _ in range(A_ )] # generates template for position in range(len(A_ ) ): lowerCAmelCase__ : int = position % (lowest * 2) # puts it in bounds lowerCAmelCase__ : Dict = min(A_ , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append('''*''' ) lowerCAmelCase__ : Dict = 0 for row in temp_grid: # fills in the characters lowerCAmelCase__ : List[Any] = input_string[counter : counter + len(A_ )] grid.append(list(A_ ) ) counter += len(A_ ) lowerCAmelCase__ : Tuple = '''''' # reads as zigzag for position in range(len(A_ ) ): lowerCAmelCase__ : Tuple = position % (lowest * 2) # puts it in bounds lowerCAmelCase__ : Optional[int] = min(A_ , lowest * 2 - num ) # creates zigzag pattern output_string += grid[num][0] grid[num].pop(0 ) return output_string def __SCREAMING_SNAKE_CASE ( A_ ): lowerCAmelCase__ : Dict = {} for key_guess in range(1 , len(A_ ) ): # tries every key lowerCAmelCase__ : List[str] = decrypt(A_ , A_ ) return results if __name__ == "__main__": import doctest doctest.testmod()
106
"""simple docstring""" import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): """simple docstring""" def __init__( self : Tuple ,lowercase_ : Dict="sayef/fsner-bert-base-uncased" ): super(lowercase_ ,self ).__init__() lowerCAmelCase__ : int = AutoModel.from_pretrained(lowercase_ ,return_dict=lowercase_ ) lowerCAmelCase__ : Optional[int] = torch.nn.CosineSimilarity(3 ,1E-08 ) lowerCAmelCase__ : List[str] = torch.nn.Softmax(dim=1 ) def __lowerCAmelCase ( self : str ,**lowercase_ : int ): return self.bert(**lowercase_ ).last_hidden_state def __lowerCAmelCase ( self : List[Any] ,lowercase_ : Optional[int] ): return token_embeddings.sum(2 ,keepdim=lowercase_ ) def __lowerCAmelCase ( self : Dict ,lowercase_ : int ,lowercase_ : str ,lowercase_ : Tuple=1 ): return self.softmax(T * self.cos(lowercase_ ,lowercase_ ) ) def __lowerCAmelCase ( self : Optional[Any] ,lowercase_ : str ,lowercase_ : Union[str, Any] ): lowerCAmelCase__ : List[Any] = W_supports['''sizes'''].tolist() lowerCAmelCase__ : Dict = W_supports['''start_token_id'''].item() lowerCAmelCase__ : Union[str, Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] lowerCAmelCase__ : Optional[Any] = self.BERT(**lowercase_ ) lowerCAmelCase__ : int = self.BERT(**lowercase_ ) lowerCAmelCase__ : List[str] = None lowerCAmelCase__ : Union[str, Any] = None lowerCAmelCase__ : int = W_supports['''input_ids'''] == start_token_id lowerCAmelCase__ : Optional[Any] = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(lowercase_ ): if i == 0: lowerCAmelCase__ : str = 0 else: lowerCAmelCase__ : List[Any] = support_sizes[i - 1] lowerCAmelCase__ : Optional[Any] = S[s : s + size][start_token_masks[s : s + size]] lowerCAmelCase__ : List[Any] = S[s : s + size][end_token_masks[s : s + size]] lowerCAmelCase__ : Union[str, Any] = torch.matmul(q[i] ,s_start.T ).sum(1 ).softmax(0 ) lowerCAmelCase__ : Any = torch.matmul(q[i] ,s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: lowerCAmelCase__ : List[Any] = torch.vstack((p_starts, p_start) ) lowerCAmelCase__ : List[Any] = torch.vstack((p_ends, p_end) ) else: lowerCAmelCase__ : Union[str, Any] = p_start lowerCAmelCase__ : str = p_end return p_starts, p_ends
106
1
'''simple docstring''' import gc import unittest from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def snake_case__ ( self : List[str] ) -> Optional[int]: '''simple docstring''' super().tearDown() gc.collect() def snake_case__ ( self : int ) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase , _UpperCamelCase = FlaxControlNetModel.from_pretrained( '''lllyasviel/sd-controlnet-canny''' , from_pt=lowerCAmelCase__ , dtype=jnp.bfloataa ) _UpperCamelCase , _UpperCamelCase = FlaxStableDiffusionControlNetPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , controlnet=lowerCAmelCase__ , from_pt=lowerCAmelCase__ , dtype=jnp.bfloataa ) _UpperCamelCase = controlnet_params _UpperCamelCase = '''bird''' _UpperCamelCase = jax.device_count() _UpperCamelCase = pipe.prepare_text_inputs([prompts] * num_samples ) _UpperCamelCase = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png''' ) _UpperCamelCase = pipe.prepare_image_inputs([canny_image] * num_samples ) _UpperCamelCase = jax.random.PRNGKey(0 ) _UpperCamelCase = jax.random.split(lowerCAmelCase__ , jax.device_count() ) _UpperCamelCase = replicate(lowerCAmelCase__ ) _UpperCamelCase = shard(lowerCAmelCase__ ) _UpperCamelCase = shard(lowerCAmelCase__ ) _UpperCamelCase = pipe( prompt_ids=lowerCAmelCase__ , image=lowerCAmelCase__ , params=lowerCAmelCase__ , prng_seed=lowerCAmelCase__ , num_inference_steps=50 , jit=lowerCAmelCase__ , ).images assert images.shape == (jax.device_count(), 1, 768, 512, 3) _UpperCamelCase = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) _UpperCamelCase = images[0, 253:256, 253:256, -1] _UpperCamelCase = jnp.asarray(jax.device_get(image_slice.flatten() ) ) _UpperCamelCase = jnp.array( [0.167969, 0.116699, 0.081543, 0.154297, 0.132812, 0.108887, 0.169922, 0.169922, 0.205078] ) print(f"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2 def snake_case__ ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase , _UpperCamelCase = FlaxControlNetModel.from_pretrained( '''lllyasviel/sd-controlnet-openpose''' , from_pt=lowerCAmelCase__ , dtype=jnp.bfloataa ) _UpperCamelCase , _UpperCamelCase = FlaxStableDiffusionControlNetPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , controlnet=lowerCAmelCase__ , from_pt=lowerCAmelCase__ , dtype=jnp.bfloataa ) _UpperCamelCase = controlnet_params _UpperCamelCase = '''Chef in the kitchen''' _UpperCamelCase = jax.device_count() _UpperCamelCase = pipe.prepare_text_inputs([prompts] * num_samples ) _UpperCamelCase = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png''' ) _UpperCamelCase = pipe.prepare_image_inputs([pose_image] * num_samples ) _UpperCamelCase = jax.random.PRNGKey(0 ) _UpperCamelCase = jax.random.split(lowerCAmelCase__ , jax.device_count() ) _UpperCamelCase = replicate(lowerCAmelCase__ ) _UpperCamelCase = shard(lowerCAmelCase__ ) _UpperCamelCase = shard(lowerCAmelCase__ ) _UpperCamelCase = pipe( prompt_ids=lowerCAmelCase__ , image=lowerCAmelCase__ , params=lowerCAmelCase__ , prng_seed=lowerCAmelCase__ , num_inference_steps=50 , jit=lowerCAmelCase__ , ).images assert images.shape == (jax.device_count(), 1, 768, 512, 3) _UpperCamelCase = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) _UpperCamelCase = images[0, 253:256, 253:256, -1] _UpperCamelCase = jnp.asarray(jax.device_get(image_slice.flatten() ) ) _UpperCamelCase = jnp.array( [[0.271484, 0.261719, 0.275391, 0.277344, 0.279297, 0.291016, 0.294922, 0.302734, 0.302734]] ) print(f"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2
354
'''simple docstring''' def a__ ( lowercase : int ) -> int: """simple docstring""" if not isinstance(lowercase, lowercase ): raise TypeError('''Input value must be an \'int\' type''' ) _UpperCamelCase = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
287
0
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __lowerCAmelCase (lowercase_ , unittest.TestCase ): '''simple docstring''' lowerCAmelCase__ : Dict = ShapEImgaImgPipeline lowerCAmelCase__ : List[str] = ["""image"""] lowerCAmelCase__ : Any = ["""image"""] lowerCAmelCase__ : Any = [ """num_images_per_prompt""", """num_inference_steps""", """generator""", """latents""", """guidance_scale""", """frame_size""", """output_type""", """return_dict""", ] lowerCAmelCase__ : Tuple = False @property def UpperCamelCase__ (self : List[Any] ): '''simple docstring''' return 32 @property def UpperCamelCase__ (self : str ): '''simple docstring''' return 32 @property def UpperCamelCase__ (self : Union[str, Any] ): '''simple docstring''' return self.time_input_dim * 4 @property def UpperCamelCase__ (self : int ): '''simple docstring''' return 8 @property def UpperCamelCase__ (self : Any ): '''simple docstring''' torch.manual_seed(0 ) lowercase__ = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=64 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=1 , ) lowercase__ = CLIPVisionModel(UpperCamelCase ) return model @property def UpperCamelCase__ (self : List[Any] ): '''simple docstring''' lowercase__ = CLIPImageProcessor( crop_size=224 , do_center_crop=UpperCamelCase , do_normalize=UpperCamelCase , do_resize=UpperCamelCase , image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , resample=3 , size=224 , ) return image_processor @property def UpperCamelCase__ (self : str ): '''simple docstring''' torch.manual_seed(0 ) lowercase__ = { '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } lowercase__ = PriorTransformer(**UpperCamelCase ) return model @property def UpperCamelCase__ (self : int ): '''simple docstring''' torch.manual_seed(0 ) lowercase__ = { '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } lowercase__ = ShapERenderer(**UpperCamelCase ) return model def UpperCamelCase__ (self : Union[str, Any] ): '''simple docstring''' lowercase__ = self.dummy_prior lowercase__ = self.dummy_image_encoder lowercase__ = self.dummy_image_processor lowercase__ = self.dummy_renderer lowercase__ = HeunDiscreteScheduler( beta_schedule='''exp''' , num_train_timesteps=1024 , prediction_type='''sample''' , use_karras_sigmas=UpperCamelCase , clip_sample=UpperCamelCase , clip_sample_range=1.0 , ) lowercase__ = { '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def UpperCamelCase__ (self : Optional[int] , UpperCamelCase : Optional[int] , UpperCamelCase : str=0 ): '''simple docstring''' lowercase__ = floats_tensor((1, 3, 64, 64) , rng=random.Random(UpperCamelCase ) ).to(UpperCamelCase ) if str(UpperCamelCase ).startswith('''mps''' ): lowercase__ = torch.manual_seed(UpperCamelCase ) else: lowercase__ = torch.Generator(device=UpperCamelCase ).manual_seed(UpperCamelCase ) lowercase__ = { '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def UpperCamelCase__ (self : Tuple ): '''simple docstring''' lowercase__ = '''cpu''' lowercase__ = self.get_dummy_components() lowercase__ = self.pipeline_class(**UpperCamelCase ) lowercase__ = pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) lowercase__ = pipe(**self.get_dummy_inputs(UpperCamelCase ) ) lowercase__ = output.images[0] lowercase__ = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowercase__ = np.array( [ 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, 0.00_03_92_16, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCamelCase__ (self : Optional[Any] ): '''simple docstring''' self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def UpperCamelCase__ (self : int ): '''simple docstring''' lowercase__ = torch_device == '''cpu''' lowercase__ = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=UpperCamelCase , relax_max_difference=UpperCamelCase , ) def UpperCamelCase__ (self : int ): '''simple docstring''' lowercase__ = self.get_dummy_components() lowercase__ = self.pipeline_class(**UpperCamelCase ) lowercase__ = pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) lowercase__ = 1 lowercase__ = 2 lowercase__ = self.get_dummy_inputs(UpperCamelCase ) for key in inputs.keys(): if key in self.batch_params: lowercase__ = batch_size * [inputs[key]] lowercase__ = pipe(**UpperCamelCase , num_images_per_prompt=UpperCamelCase )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __lowerCAmelCase (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ (self : Optional[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ (self : Union[str, Any] ): '''simple docstring''' lowercase__ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) lowercase__ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) lowercase__ = ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) lowercase__ = pipe.to(UpperCamelCase ) pipe.set_progress_bar_config(disable=UpperCamelCase ) lowercase__ = torch.Generator(device=UpperCamelCase ).manual_seed(0 ) lowercase__ = pipe( UpperCamelCase , generator=UpperCamelCase , guidance_scale=3.0 , num_inference_steps=64 , frame_size=64 , output_type='''np''' , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(UpperCamelCase , UpperCamelCase )
2
'''simple docstring''' import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) lowerCamelCase : Any = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation='relu') ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation='relu')) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation='relu')) classifier.add(layers.Dense(units=1, activation='sigmoid')) # Compiling the CNN classifier.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') lowerCamelCase : Optional[Any] = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) lowerCamelCase : Any = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) lowerCamelCase : List[Any] = train_datagen.flow_from_directory( 'dataset/training_set', target_size=(64, 64), batch_size=32, class_mode='binary' ) lowerCamelCase : List[str] = test_datagen.flow_from_directory( 'dataset/test_set', target_size=(64, 64), batch_size=32, class_mode='binary' ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save('cnn.h5') # Part 3 - Making new predictions lowerCamelCase : List[str] = tf.keras.preprocessing.image.load_img( 'dataset/single_prediction/image.png', target_size=(64, 64) ) lowerCamelCase : Optional[int] = tf.keras.preprocessing.image.img_to_array(test_image) lowerCamelCase : str = np.expand_dims(test_image, axis=0) lowerCamelCase : List[str] = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: lowerCamelCase : Any = 'Normal' if result[0][0] == 1: lowerCamelCase : Any = 'Abnormality detected'
2
1
"""simple docstring""" from math import factorial _UpperCamelCase : Optional[Any] = {str(d): factorial(d) for d in range(10)} def snake_case (A_ :Tuple ) -> Optional[Any]: '''simple docstring''' return sum(DIGIT_FACTORIAL[d] for d in str(_UpperCAmelCase ) ) def snake_case () -> Dict: '''simple docstring''' a : List[Any] = 7 * factorial(9 ) + 1 return sum(i for i in range(3 , _UpperCAmelCase ) if sum_of_digit_factorial(_UpperCAmelCase ) == i ) if __name__ == "__main__": print(f'''{solution() = }''')
365
"""simple docstring""" def snake_case (A_ :int ): '''simple docstring''' if isinstance(A_ , A_ ): raise TypeError('\'float\' object cannot be interpreted as an integer' ) if isinstance(A_ , A_ ): raise TypeError('\'str\' object cannot be interpreted as an integer' ) if num == 0: return "0b0" a : List[Any] = False if num < 0: a : Optional[int] = True a : Dict = -num a : list[int] = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(A_ ) for e in binary ) return "0b" + "".join(str(A_ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
186
0
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=lowerCamelCase__ ) class lowercase_ (lowerCamelCase__ ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) SCREAMING_SNAKE_CASE : Optional[int] = Features({'audio': Audio()} ) SCREAMING_SNAKE_CASE : Dict = Features({'labels': ClassLabel} ) SCREAMING_SNAKE_CASE : int = 'audio' SCREAMING_SNAKE_CASE : List[str] = 'labels' def SCREAMING_SNAKE_CASE ( self : Dict ,lowercase__ : Tuple ): if self.label_column not in features: raise ValueError(F"Column {self.label_column} is not present in features." ) if not isinstance(features[self.label_column] ,lowercase__ ): raise ValueError(F"Column {self.label_column} is not a ClassLabel." ) __lowercase = copy.deepcopy(self ) __lowercase = self.label_schema.copy() __lowercase = features[self.label_column] __lowercase = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : List[str] ): return { self.audio_column: "audio", self.label_column: "labels", }
104
import re def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ ) -> str: if len(re.findall('[ATCG]' , SCREAMING_SNAKE_CASE_ ) ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError('Invalid Strand' ) return dna.translate(dna.maketrans('ATCG' , 'TAGC' ) ) if __name__ == "__main__": import doctest doctest.testmod()
212
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = '''▁''' lowerCAmelCase__ = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ = { '''vocab_file''': { '''facebook/mbart-large-50-one-to-many-mmt''': ( '''https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model''' ), } } lowerCAmelCase__ = { '''facebook/mbart-large-50-one-to-many-mmt''': 1_0_2_4, } # fmt: off lowerCAmelCase__ = ['''ar_AR''', '''cs_CZ''', '''de_DE''', '''en_XX''', '''es_XX''', '''et_EE''', '''fi_FI''', '''fr_XX''', '''gu_IN''', '''hi_IN''', '''it_IT''', '''ja_XX''', '''kk_KZ''', '''ko_KR''', '''lt_LT''', '''lv_LV''', '''my_MM''', '''ne_NP''', '''nl_XX''', '''ro_RO''', '''ru_RU''', '''si_LK''', '''tr_TR''', '''vi_VN''', '''zh_CN''', '''af_ZA''', '''az_AZ''', '''bn_IN''', '''fa_IR''', '''he_IL''', '''hr_HR''', '''id_ID''', '''ka_GE''', '''km_KH''', '''mk_MK''', '''ml_IN''', '''mn_MN''', '''mr_IN''', '''pl_PL''', '''ps_AF''', '''pt_XX''', '''sv_SE''', '''sw_KE''', '''ta_IN''', '''te_IN''', '''th_TH''', '''tl_XX''', '''uk_UA''', '''ur_PK''', '''xh_ZA''', '''gl_ES''', '''sl_SI'''] class snake_case__(_UpperCamelCase ): """simple docstring""" lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = ["""input_ids""", """attention_mask"""] lowercase_ = [] lowercase_ = [] def __init__( self : Any , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int=None , SCREAMING_SNAKE_CASE : str=None , SCREAMING_SNAKE_CASE : List[str]="</s>" , SCREAMING_SNAKE_CASE : List[str]="</s>" , SCREAMING_SNAKE_CASE : Tuple="<s>" , SCREAMING_SNAKE_CASE : Optional[Any]="<unk>" , SCREAMING_SNAKE_CASE : Tuple="<pad>" , SCREAMING_SNAKE_CASE : int="<mask>" , SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **SCREAMING_SNAKE_CASE : Tuple , ): # Mask token behave like a normal word, i.e. include the space before it lowercase__ : str = AddedToken(SCREAMING_SNAKE_CASE , lstrip=SCREAMING_SNAKE_CASE , rstrip=SCREAMING_SNAKE_CASE ) if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) else mask_token lowercase__ : Optional[int] = {} if sp_model_kwargs is None else sp_model_kwargs lowercase__ : Optional[int] = kwargs.get("additional_special_tokens" , [] ) kwargs["additional_special_tokens"] += [ code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=SCREAMING_SNAKE_CASE , tgt_lang=SCREAMING_SNAKE_CASE , eos_token=SCREAMING_SNAKE_CASE , unk_token=SCREAMING_SNAKE_CASE , sep_token=SCREAMING_SNAKE_CASE , cls_token=SCREAMING_SNAKE_CASE , pad_token=SCREAMING_SNAKE_CASE , mask_token=SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **SCREAMING_SNAKE_CASE , ) lowercase__ : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(SCREAMING_SNAKE_CASE ) ) lowercase__ : Tuple = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # Mimic fairseq token-to-id alignment for the first 4 token lowercase__ : Optional[int] = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab lowercase__ : List[Any] = 1 lowercase__ : Tuple = len(self.sp_model ) lowercase__ : str = { code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(SCREAMING_SNAKE_CASE ) } lowercase__ : List[Any] = {v: k for k, v in self.lang_code_to_id.items()} lowercase__ : int = len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset self.fairseq_tokens_to_ids.update(self.lang_code_to_id ) lowercase__ : str = {v: k for k, v in self.fairseq_tokens_to_ids.items()} lowercase__ : Optional[int] = src_lang if src_lang is not None else "en_XX" lowercase__ : Optional[int] = self.lang_code_to_id[self._src_lang] lowercase__ : Optional[Any] = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def snake_case ( self : int ): return len(self.sp_model ) + len(self.lang_code_to_id ) + self.fairseq_offset + 1 # Plus 1 for the mask token @property def snake_case ( self : Tuple ): return self._src_lang @src_lang.setter def snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE : str ): lowercase__ : int = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def __getstate__( self : int ): lowercase__ : Tuple = self.__dict__.copy() lowercase__ : List[Any] = None return state def __setstate__( self : str , SCREAMING_SNAKE_CASE : Dict ): lowercase__ : Optional[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): lowercase__ : Tuple = {} lowercase__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def snake_case ( self : Dict ): lowercase__ : Optional[int] = {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE : str ): return self.sp_model.encode(SCREAMING_SNAKE_CASE , out_type=SCREAMING_SNAKE_CASE ) def snake_case ( self : Tuple , SCREAMING_SNAKE_CASE : str ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] lowercase__ : str = self.sp_model.PieceToId(SCREAMING_SNAKE_CASE ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE : int ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def snake_case ( self : Dict , SCREAMING_SNAKE_CASE : Optional[Any] ): lowercase__ : int = [] lowercase__ : str = "" lowercase__ : str = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE ) + token lowercase__ : Optional[Any] = True lowercase__ : Optional[int] = [] else: current_sub_tokens.append(SCREAMING_SNAKE_CASE ) lowercase__ : Tuple = False out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE ) return out_string.strip() def snake_case ( self : List[str] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[str] = None ): if not os.path.isdir(SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return lowercase__ : Union[str, Any] = os.path.join( SCREAMING_SNAKE_CASE , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(SCREAMING_SNAKE_CASE , "wb" ) as fi: lowercase__ : int = self.sp_model.serialized_model_proto() fi.write(SCREAMING_SNAKE_CASE ) return (out_vocab_file,) def snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE : List[int] , SCREAMING_SNAKE_CASE : Optional[List[int]] = None , SCREAMING_SNAKE_CASE : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE , token_ids_a=SCREAMING_SNAKE_CASE , already_has_special_tokens=SCREAMING_SNAKE_CASE ) lowercase__ : Any = [1] * len(self.prefix_tokens ) lowercase__ : Optional[Any] = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE )) + suffix_ones return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE )) + ([0] * len(SCREAMING_SNAKE_CASE )) + suffix_ones def snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE : List[int] , SCREAMING_SNAKE_CASE : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def snake_case ( self : Dict , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[str] , SCREAMING_SNAKE_CASE : Optional[str] , **SCREAMING_SNAKE_CASE : Any ): if src_lang is None or tgt_lang is None: raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model" ) lowercase__ : int = src_lang lowercase__ : Optional[Any] = self(SCREAMING_SNAKE_CASE , add_special_tokens=SCREAMING_SNAKE_CASE , return_tensors=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) lowercase__ : Dict = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE ) lowercase__ : Dict = tgt_lang_id return inputs def snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : str = "en_XX" , SCREAMING_SNAKE_CASE : Optional[List[str]] = None , SCREAMING_SNAKE_CASE : str = "ro_RO" , **SCREAMING_SNAKE_CASE : Optional[Any] , ): lowercase__ : Optional[Any] = src_lang lowercase__ : int = tgt_lang return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) def snake_case ( self : Any ): return self.set_src_lang_special_tokens(self.src_lang ) def snake_case ( self : str ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def snake_case ( self : Dict , SCREAMING_SNAKE_CASE : str ): lowercase__ : str = self.lang_code_to_id[src_lang] lowercase__ : Any = [self.cur_lang_code_id] lowercase__ : Optional[Any] = [self.eos_token_id] def snake_case ( self : Dict , SCREAMING_SNAKE_CASE : str ): lowercase__ : Optional[Any] = self.lang_code_to_id[tgt_lang] lowercase__ : Union[str, Any] = [self.cur_lang_code_id] lowercase__ : Any = [self.eos_token_id]
121
from __future__ import annotations lowerCAmelCase__ = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , ): """simple docstring""" lowercase__ : Optional[int] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(lowerCamelCase__ ) ) ] # the reference grid lowercase__ : List[Any] = 1 lowercase__ : Tuple = [ [0 for col in range(len(grid[0] ) )] for row in range(len(lowerCamelCase__ ) ) ] # the action grid lowercase__ : Union[str, Any] = init[0] lowercase__ : List[str] = init[1] lowercase__ : Optional[Any] = 0 lowercase__ : Optional[int] = g + heuristic[x][y] # cost from starting cell to destination cell lowercase__ : Tuple = [[f, g, x, y]] lowercase__ : Union[str, Any] = False # flag that is set when search is complete lowercase__ : Any = False # flag set if we can't find expand while not found and not resign: if len(lowerCamelCase__ ) == 0: raise ValueError("Algorithm is unable to find solution" ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() lowercase__ : Tuple = cell.pop() lowercase__ : Optional[Any] = next_cell[2] lowercase__ : int = next_cell[3] lowercase__ : Union[str, Any] = next_cell[1] if x == goal[0] and y == goal[1]: lowercase__ : Tuple = True else: for i in range(len(lowerCamelCase__ ) ): # to try out different valid actions lowercase__ : Tuple = x + DIRECTIONS[i][0] lowercase__ : str = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(lowerCamelCase__ ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: lowercase__ : List[Any] = g + cost lowercase__ : Tuple = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) lowercase__ : Dict = 1 lowercase__ : Union[str, Any] = i lowercase__ : Optional[int] = [] lowercase__ : List[Any] = goal[0] lowercase__ : Optional[int] = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: lowercase__ : int = x - DIRECTIONS[action[x][y]][0] lowercase__ : List[Any] = y - DIRECTIONS[action[x][y]][1] lowercase__ : Optional[Any] = xa lowercase__ : Dict = ya invpath.append([x, y] ) lowercase__ : List[str] = [] for i in range(len(lowerCamelCase__ ) ): path.append(invpath[len(lowerCamelCase__ ) - 1 - i] ) return path, action if __name__ == "__main__": lowerCAmelCase__ = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] lowerCAmelCase__ = [0, 0] # all coordinates are given in format [y,x] lowerCAmelCase__ = [len(grid) - 1, len(grid[0]) - 1] lowerCAmelCase__ = 1 # the cost map which pushes the path closer to the goal lowerCAmelCase__ = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): lowerCAmelCase__ = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map lowerCAmelCase__ = 9_9 lowerCAmelCase__ , lowerCAmelCase__ = search(grid, init, goal, cost, heuristic) print('''ACTION MAP''') for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
121
1
'''simple docstring''' import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation def __lowercase ( __lowercase ) -> Optional[int]: '''simple docstring''' _A = 384 if "tiny" in model_name: _A = [3, 3, 9, 3] _A = [96, 192, 384, 768] if "small" in model_name: _A = [3, 3, 27, 3] _A = [96, 192, 384, 768] if "base" in model_name: _A = [3, 3, 27, 3] _A = [128, 256, 512, 1024] _A = 512 if "large" in model_name: _A = [3, 3, 27, 3] _A = [192, 384, 768, 1536] _A = 768 if "xlarge" in model_name: _A = [3, 3, 27, 3] _A = [256, 512, 1024, 2048] _A = 1024 # set label information _A = 150 _A = "huggingface/label-files" _A = "ade20k-id2label.json" _A = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type="dataset" ) , "r" ) ) _A = {int(__lowercase ): v for k, v in idalabel.items()} _A = {v: k for k, v in idalabel.items()} _A = ConvNextConfig( depths=__lowercase , hidden_sizes=__lowercase , out_features=["stage1", "stage2", "stage3", "stage4"] ) _A = UperNetConfig( backbone_config=__lowercase , auxiliary_in_channels=__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase , ) return config def __lowercase ( __lowercase ) -> List[str]: '''simple docstring''' _A = [] # fmt: off # stem rename_keys.append(("backbone.downsample_layers.0.0.weight", "backbone.embeddings.patch_embeddings.weight") ) rename_keys.append(("backbone.downsample_layers.0.0.bias", "backbone.embeddings.patch_embeddings.bias") ) rename_keys.append(("backbone.downsample_layers.0.1.weight", "backbone.embeddings.layernorm.weight") ) rename_keys.append(("backbone.downsample_layers.0.1.bias", "backbone.embeddings.layernorm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F'''backbone.stages.{i}.{j}.gamma''', F'''backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.depthwise_conv.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.dwconv.weight''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.depthwise_conv.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.dwconv.bias''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.norm.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.layernorm.weight''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.norm.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.layernorm.bias''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv1.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv1.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv2.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight''') ) rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv2.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias''') ) if i > 0: rename_keys.append((F'''backbone.downsample_layers.{i}.0.weight''', F'''backbone.encoder.stages.{i}.downsampling_layer.0.weight''') ) rename_keys.append((F'''backbone.downsample_layers.{i}.0.bias''', F'''backbone.encoder.stages.{i}.downsampling_layer.0.bias''') ) rename_keys.append((F'''backbone.downsample_layers.{i}.1.weight''', F'''backbone.encoder.stages.{i}.downsampling_layer.1.weight''') ) rename_keys.append((F'''backbone.downsample_layers.{i}.1.bias''', F'''backbone.encoder.stages.{i}.downsampling_layer.1.bias''') ) rename_keys.append((F'''backbone.norm{i}.weight''', F'''backbone.hidden_states_norms.stage{i+1}.weight''') ) rename_keys.append((F'''backbone.norm{i}.bias''', F'''backbone.hidden_states_norms.stage{i+1}.bias''') ) # decode head rename_keys.extend( [ ("decode_head.conv_seg.weight", "decode_head.classifier.weight"), ("decode_head.conv_seg.bias", "decode_head.classifier.bias"), ("auxiliary_head.conv_seg.weight", "auxiliary_head.classifier.weight"), ("auxiliary_head.conv_seg.bias", "auxiliary_head.classifier.bias"), ] ) # fmt: on return rename_keys def __lowercase ( __lowercase , __lowercase , __lowercase ) -> int: '''simple docstring''' _A = dct.pop(__lowercase ) _A = val def __lowercase ( __lowercase , __lowercase , __lowercase ) -> List[Any]: '''simple docstring''' _A = { "upernet-convnext-tiny": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth", "upernet-convnext-small": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth", "upernet-convnext-base": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth", "upernet-convnext-large": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth", "upernet-convnext-xlarge": "https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth", } _A = model_name_to_url[model_name] _A = torch.hub.load_state_dict_from_url(__lowercase , map_location="cpu" )["state_dict"] _A = get_upernet_config(__lowercase ) _A = UperNetForSemanticSegmentation(__lowercase ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): _A = state_dict.pop(__lowercase ) if "bn" in key: _A = key.replace("bn" , "batch_norm" ) _A = val # rename keys _A = create_rename_keys(__lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) model.load_state_dict(__lowercase ) # verify on image _A = "https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg" _A = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ).convert("RGB" ) _A = SegformerImageProcessor() _A = processor(__lowercase , return_tensors="pt" ).pixel_values with torch.no_grad(): _A = model(__lowercase ) if model_name == "upernet-convnext-tiny": _A = torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ) elif model_name == "upernet-convnext-small": _A = torch.tensor( [[-8.8236, -8.8236, -8.6771], [-8.8236, -8.8236, -8.6771], [-8.7638, -8.7638, -8.6240]] ) elif model_name == "upernet-convnext-base": _A = torch.tensor( [[-8.8558, -8.8558, -8.6905], [-8.8558, -8.8558, -8.6905], [-8.7669, -8.7669, -8.6021]] ) elif model_name == "upernet-convnext-large": _A = torch.tensor( [[-8.6660, -8.6660, -8.6210], [-8.6660, -8.6660, -8.6210], [-8.6310, -8.6310, -8.5964]] ) elif model_name == "upernet-convnext-xlarge": _A = torch.tensor( [[-8.4980, -8.4980, -8.3977], [-8.4980, -8.4980, -8.3977], [-8.4379, -8.4379, -8.3412]] ) print("Logits:" , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , __lowercase , atol=1e-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__lowercase ) print(F'''Saving processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(__lowercase ) if push_to_hub: print(F'''Pushing model and processor for {model_name} to hub''' ) model.push_to_hub(F'''openmmlab/{model_name}''' ) processor.push_to_hub(F'''openmmlab/{model_name}''' ) if __name__ == "__main__": lowerCamelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''upernet-convnext-tiny''', type=str, choices=[F"""upernet-convnext-{size}""" for size in ['''tiny''', '''small''', '''base''', '''large''', '''xlarge''']], help='''Name of the ConvNext UperNet model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) lowerCamelCase_ = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
79
"""simple docstring""" import pprint import requests _lowercase : Optional[Any] = 'https://zenquotes.io/api' def lowercase__ ( ): return requests.get(API_ENDPOINT_URL + '''/today''' ).json() def lowercase__ ( ): return requests.get(API_ENDPOINT_URL + '''/random''' ).json() if __name__ == "__main__": _lowercase : int = random_quotes() pprint.pprint(response)
332
0
from __future__ import annotations class UpperCAmelCase : '''simple docstring''' def __init__( self : Optional[Any] , lowerCAmelCase_ : int ): """simple docstring""" _A: List[str] = order # a_{0} ... a_{k} _A: Any = [1.0] + [0.0] * order # b_{0} ... b_{k} _A: Dict = [1.0] + [0.0] * order # x[n-1] ... x[n-k] _A: Optional[Any] = [0.0] * self.order # y[n-1] ... y[n-k] _A: Union[str, Any] = [0.0] * self.order def __magic_name__ ( self : List[Any] , lowerCAmelCase_ : list[float] , lowerCAmelCase_ : list[float] ): """simple docstring""" if len(lowerCAmelCase_ ) < self.order: _A: Any = [1.0, *a_coeffs] if len(lowerCAmelCase_ ) != self.order + 1: _A: Union[str, Any] = ( F"""Expected a_coeffs to have {self.order + 1} elements """ F"""for {self.order}-order filter, got {len(lowerCAmelCase_ )}""" ) raise ValueError(lowerCAmelCase_ ) if len(lowerCAmelCase_ ) != self.order + 1: _A: Tuple = ( F"""Expected b_coeffs to have {self.order + 1} elements """ F"""for {self.order}-order filter, got {len(lowerCAmelCase_ )}""" ) raise ValueError(lowerCAmelCase_ ) _A: Dict = a_coeffs _A: Optional[Any] = b_coeffs def __magic_name__ ( self : Any , lowerCAmelCase_ : float ): """simple docstring""" _A: Dict = 0.0 # Start at index 1 and do index 0 at the end. for i in range(1 , self.order + 1 ): result += ( self.b_coeffs[i] * self.input_history[i - 1] - self.a_coeffs[i] * self.output_history[i - 1] ) _A: Any = (result + self.b_coeffs[0] * sample) / self.a_coeffs[0] _A: Tuple = self.input_history[:-1] _A: Optional[int] = self.output_history[:-1] _A: List[str] = sample _A: Optional[Any] = result return result
301
import argparse import json import os from tensorflow.core.protobuf.saved_model_pba import SavedModel # 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 UpperCAmelCase__ : Any = '.' # Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model) UpperCAmelCase__ : Optional[Any] = [ 'Assert', 'AssignVariableOp', 'EmptyTensorList', 'MergeV2Checkpoints', 'ReadVariableOp', 'ResourceGather', 'RestoreV2', 'SaveV2', 'ShardedFilename', 'StatefulPartitionedCall', 'StaticRegexFullMatch', 'VarHandleOp', ] def lowerCamelCase__ ( a , a , a ) -> Union[str, Any]: _A: Optional[int] = SavedModel() _A: int = [] with open(os.path.join(a , '''utils''' , '''tf_ops''' , '''onnx.json''' ) ) as f: _A: List[Any] = json.load(a )['''opsets'''] for i in range(1 , opset + 1 ): onnx_ops.extend(onnx_opsets[str(a )] ) with open(a , '''rb''' ) as f: saved_model.ParseFromString(f.read() ) _A: Optional[Any] = set() # Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs) for meta_graph in saved_model.meta_graphs: # Add operations in the graph definition model_op_names.update(node.op for node in meta_graph.graph_def.node ) # Go through the functions in the graph definition for func in meta_graph.graph_def.library.function: # Add operations in each function model_op_names.update(node.op for node in func.node_def ) # Convert to list, sorted if you want _A: Optional[int] = sorted(a ) _A: Tuple = [] for op in model_op_names: if op not in onnx_ops and op not in INTERNAL_OPS: incompatible_ops.append(a ) if strict and len(a ) > 0: raise Exception(f"""Found the following incompatible ops for the opset {opset}:\n""" + incompatible_ops ) elif len(a ) > 0: print(f"""Found the following incompatible ops for the opset {opset}:""" ) print(*a , sep='''\n''' ) else: print(f"""The saved model {saved_model_path} can properly be converted with ONNX.""" ) if __name__ == "__main__": UpperCAmelCase__ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('--saved_model_path', help='Path of the saved model to check (the .pb file).') parser.add_argument( '--opset', default=12, type=int, help='The ONNX opset against which the model has to be tested.' ) parser.add_argument( '--framework', choices=['onnx'], default='onnx', help='Frameworks against which to test the saved model.' ) parser.add_argument( '--strict', action='store_true', help='Whether make the checking strict (raise errors) or not (raise warnings)' ) UpperCAmelCase__ : int = parser.parse_args() if args.framework == "onnx": onnx_compliancy(args.saved_model_path, args.strict, args.opset)
301
1
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ = { """configuration_mgp_str""": ["""MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MgpstrConfig"""], """processing_mgp_str""": ["""MgpstrProcessor"""], """tokenization_mgp_str""": ["""MgpstrTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ = [ """MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST""", """MgpstrModel""", """MgpstrPreTrainedModel""", """MgpstrForSceneTextRecognition""", ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
296
import json import sys def _a ( lowerCamelCase, lowerCamelCase ): with open(lowerCamelCase, encoding="""utf-8""" ) as f: lowerCamelCase : List[Any] = json.load(lowerCamelCase ) lowerCamelCase : Optional[Any] = ["""<details>""", """<summary>Show updated benchmarks!</summary>""", """ """] for benchmark_name in sorted(lowerCamelCase ): lowerCamelCase : List[Any] = results[benchmark_name] lowerCamelCase : Union[str, Any] = benchmark_name.split("""/""" )[-1] output_md.append(F'''### Benchmark: {benchmark_file_name}''' ) lowerCamelCase : Any = """| metric |""" lowerCamelCase : str = """|--------|""" lowerCamelCase : List[Any] = """| new / old (diff) |""" for metric_name in sorted(lowerCamelCase ): lowerCamelCase : List[Any] = benchmark_res[metric_name] lowerCamelCase : Tuple = metric_vals["""new"""] lowerCamelCase : int = metric_vals.get("""old""", lowerCamelCase ) lowerCamelCase : Dict = metric_vals.get("""diff""", lowerCamelCase ) lowerCamelCase : Dict = F''' {new_val:f}''' if isinstance(lowerCamelCase, (int, float) ) else """None""" if old_val is not None: val_str += F''' / {old_val:f}''' if isinstance(lowerCamelCase, (int, float) ) else "None" if dif_val is not None: val_str += F''' ({dif_val:f})''' if isinstance(lowerCamelCase, (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append("""</details>""" ) with open(lowerCamelCase, """w""", encoding="""utf-8""" ) as f: f.writelines("""\n""".join(lowerCamelCase ) ) if __name__ == "__main__": _lowerCamelCase =sys.argv[1] _lowerCamelCase =sys.argv[2] format_json_to_md(input_json_file, output_md_file)
287
0
'''simple docstring''' import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.layers import LSTM, Dense from tensorflow.keras.models import Sequential if __name__ == "__main__": _lowercase : Tuple = pd.read_csv("sample_data.csv", header=None) _lowercase : List[Any] = df.shape[:1][0] # If you're using some other dataset input the target column _lowercase : List[str] = df.iloc[:, 1:2] _lowercase : Optional[int] = actual_data.values.reshape(len_data, 1) _lowercase : Any = MinMaxScaler().fit_transform(actual_data) _lowercase : Any = 10 _lowercase : List[Any] = 5 _lowercase : str = 20 _lowercase : List[str] = len_data - periods * look_back _lowercase : List[str] = actual_data[:division] _lowercase : int = actual_data[division - look_back :] _lowercase : Optional[int] = [], [] _lowercase : Union[str, Any] = [], [] for i in range(0, len(train_data) - forward_days - look_back + 1): train_x.append(train_data[i : i + look_back]) train_y.append(train_data[i + look_back : i + look_back + forward_days]) for i in range(0, len(test_data) - forward_days - look_back + 1): test_x.append(test_data[i : i + look_back]) test_y.append(test_data[i + look_back : i + look_back + forward_days]) _lowercase : Union[str, Any] = np.array(train_x) _lowercase : Tuple = np.array(test_x) _lowercase : Optional[int] = np.array([list(i.ravel()) for i in train_y]) _lowercase : Dict = np.array([list(i.ravel()) for i in test_y]) _lowercase : Dict = Sequential() model.add(LSTM(128, input_shape=(look_back, 1), return_sequences=True)) model.add(LSTM(64, input_shape=(128, 1))) model.add(Dense(forward_days)) model.compile(loss="mean_squared_error", optimizer="adam") _lowercase : Optional[Any] = model.fit( x_train, y_train, epochs=150, verbose=1, shuffle=True, batch_size=4 ) _lowercase : List[Any] = model.predict(x_test)
358
'''simple docstring''' class __magic_name__ : def __init__( self : int , lowercase_ : list ): lowercase_ : Dict = set_counts lowercase_ : List[Any] = max(lowercase_ ) lowercase_ : str = len(lowercase_ ) lowercase_ : str = [1] * num_sets lowercase_ : Dict = list(range(lowercase_ ) ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , lowercase_ : int , lowercase_ : int ): lowercase_ : List[Any] = self.get_parent(lowercase_ ) lowercase_ : Union[str, Any] = self.get_parent(lowercase_ ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] lowercase_ : List[str] = 0 lowercase_ : Optional[int] = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 lowercase_ : int = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] lowercase_ : int = 0 lowercase_ : List[Any] = src_parent lowercase_ : List[Any] = self.set_counts[src_parent] lowercase_ : Tuple = max(self.max_set , lowercase_ ) return True def SCREAMING_SNAKE_CASE_ ( self : Dict , lowercase_ : int ): if self.parents[disj_set] == disj_set: return disj_set lowercase_ : int = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
21
0
"""simple docstring""" __A : Union[str, Any] = [ (1_000, '''M'''), (900, '''CM'''), (500, '''D'''), (400, '''CD'''), (100, '''C'''), (90, '''XC'''), (50, '''L'''), (40, '''XL'''), (10, '''X'''), (9, '''IX'''), (5, '''V'''), (4, '''IV'''), (1, '''I'''), ] def lowercase ( __snake_case : str ): lowercase_ : List[Any] = {'''I''': 1, '''V''': 5, '''X''': 1_0, '''L''': 5_0, '''C''': 1_0_0, '''D''': 5_0_0, '''M''': 1_0_0_0} lowercase_ : Tuple = 0 lowercase_ : Optional[Any] = 0 while place < len(__snake_case ): if (place + 1 < len(__snake_case )) and (vals[roman[place]] < vals[roman[place + 1]]): total += vals[roman[place + 1]] - vals[roman[place]] place += 2 else: total += vals[roman[place]] place += 1 return total def lowercase ( __snake_case : int ): lowercase_ : List[Any] = [] for arabic, roman in ROMAN: ((lowercase_) , (lowercase_)) : Union[str, Any] = divmod(__snake_case , __snake_case ) result.append(roman * factor ) if number == 0: break return "".join(__snake_case ) if __name__ == "__main__": import doctest doctest.testmod()
33
import inspect import unittest from transformers import RegNetConfig from transformers.file_utils import cached_property, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device 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 RegNetForImageClassification, RegNetModel from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _lowerCamelCase : """simple docstring""" def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=[10, 20, 30, 40] , _SCREAMING_SNAKE_CASE=[1, 1, 2, 1] , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE="relu" , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=None , )->List[str]: '''simple docstring''' A_ : str = parent A_ : int = batch_size A_ : List[str] = image_size A_ : Dict = num_channels A_ : Tuple = embeddings_size A_ : Union[str, Any] = hidden_sizes A_ : Dict = depths A_ : str = is_training A_ : Union[str, Any] = use_labels A_ : Union[str, Any] = hidden_act A_ : Optional[Any] = num_labels A_ : Tuple = scope A_ : Optional[int] = len(_SCREAMING_SNAKE_CASE ) def _snake_case ( self )->Optional[Any]: '''simple docstring''' A_ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : str = None if self.use_labels: A_ : Union[str, Any] = ids_tensor([self.batch_size] , self.num_labels ) A_ : Optional[Any] = self.get_config() return config, pixel_values, labels def _snake_case ( self )->Union[str, Any]: '''simple docstring''' return RegNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )->Union[str, Any]: '''simple docstring''' A_ : Dict = RegNetModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ : Any = model(_SCREAMING_SNAKE_CASE ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )->Union[str, Any]: '''simple docstring''' A_ : Union[str, Any] = self.num_labels A_ : Dict = RegNetForImageClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ : int = model(_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _snake_case ( self )->Union[str, Any]: '''simple docstring''' A_ : Tuple = self.prepare_config_and_inputs() A_ , A_ , A_ : str = config_and_inputs A_ : Any = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class _lowerCamelCase ( UpperCamelCase , UpperCamelCase , unittest.TestCase ): """simple docstring""" snake_case = (RegNetModel, RegNetForImageClassification) if is_torch_available() else () snake_case = ( {"feature-extraction": RegNetModel, "image-classification": RegNetForImageClassification} if is_torch_available() else {} ) snake_case = False snake_case = False snake_case = False snake_case = False def _snake_case ( self )->Union[str, Any]: '''simple docstring''' A_ : Union[str, Any] = RegNetModelTester(self ) A_ : Union[str, Any] = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE ) def _snake_case ( self )->Dict: '''simple docstring''' self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _snake_case ( self )->Tuple: '''simple docstring''' return @unittest.skip(reason='''RegNet does not use inputs_embeds''' ) def _snake_case ( self )->Dict: '''simple docstring''' pass @unittest.skip(reason='''RegNet does not support input and output embeddings''' ) def _snake_case ( self )->str: '''simple docstring''' pass def _snake_case ( self )->List[Any]: '''simple docstring''' A_ , A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : str = model_class(_SCREAMING_SNAKE_CASE ) A_ : Union[str, Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : Any = [*signature.parameters.keys()] A_ : Any = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _SCREAMING_SNAKE_CASE ) def _snake_case ( self )->Any: '''simple docstring''' A_ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )->Optional[Any]: '''simple docstring''' A_ , A_ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Union[str, Any] = model_class(config=_SCREAMING_SNAKE_CASE ) for name, module in model.named_modules(): if isinstance(_SCREAMING_SNAKE_CASE , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) def _snake_case ( self )->List[Any]: '''simple docstring''' def check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ : str = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): A_ : Tuple = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) A_ : Union[str, Any] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states A_ : Optional[int] = self.model_tester.num_stages self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , expected_num_stages + 1 ) # RegNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 2, self.model_tester.image_size // 2] , ) A_ , A_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() A_ : int = ['''basic''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: A_ : int = layer_type A_ : List[Any] = True check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A_ : str = True check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def _snake_case ( self )->Dict: '''simple docstring''' A_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE ) @slow def _snake_case ( self )->str: '''simple docstring''' for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Dict = RegNetModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _SCREAMING_SNAKE_CASE ( ): A_ : int = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" @cached_property def _snake_case ( self )->List[str]: '''simple docstring''' return ( AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _snake_case ( self )->Tuple: '''simple docstring''' A_ : List[Any] = RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_SCREAMING_SNAKE_CASE ) A_ : Optional[Any] = self.default_image_processor A_ : Any = prepare_img() A_ : Optional[Any] = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): A_ : Union[str, Any] = model(**_SCREAMING_SNAKE_CASE ) # verify the logits A_ : Union[str, Any] = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) A_ : Optional[int] = torch.tensor([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1e-4 ) )
186
0
"""simple docstring""" import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets lowerCAmelCase_ = '\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n booktitle = {NeurIPS},\n year = {2021}\n}\n\n' lowerCAmelCase_ = '\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n' lowerCAmelCase_ = '\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n predictions: list of generated text to score. Each predictions\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\nOptional Args:\n num_buckets: the size of the histogram to quantize P and Q. Options: \'auto\' (default) or an integer\n pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n kmeans_max_iter: maximum number of k-means iterations. Default 500\n featurize_model_name: name of the model from which features are obtained. Default \'gpt2-large\' Use one of [\'gpt2\', \'gpt2-medium\', \'gpt2-large\', \'gpt2-xl\'].\n device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU\n max_text_length: maximum number of tokens to consider. Default 1024\n divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n mauve_scaling_factor: "c" from the paper. Default 5.\n verbose: If True (default), print running time updates\n seed: random seed to initialize k-means cluster assignments.\nReturns:\n mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n q_hist: same as above, but with q_text.\nExamples:\n\n >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n >>> import datasets\n >>> mauve = datasets.load_metric(\'mauve\')\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n >>> print(out.mauve) # doctest: +SKIP\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): '''simple docstring''' def UpperCAmelCase ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,homepage='''https://github.com/krishnap25/mauve''' ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { '''predictions''': datasets.Value('''string''' ,id='''sequence''' ), '''references''': datasets.Value('''string''' ,id='''sequence''' ), } ) ,codebase_urls=['''https://github.com/krishnap25/mauve'''] ,reference_urls=[ '''https://arxiv.org/abs/2102.01454''', '''https://github.com/krishnap25/mauve''', ] ,) def UpperCAmelCase ( self : Dict ,_snake_case : Optional[Any] ,_snake_case : Any ,_snake_case : List[str]=None ,_snake_case : Tuple=None ,_snake_case : List[Any]=None ,_snake_case : Any=None ,_snake_case : Optional[int]="auto" ,_snake_case : Optional[int]=-1 ,_snake_case : Optional[int]=0.9 ,_snake_case : Any=5 ,_snake_case : Dict=500 ,_snake_case : Optional[int]="gpt2-large" ,_snake_case : Optional[Any]=-1 ,_snake_case : Tuple=1_024 ,_snake_case : Optional[int]=25 ,_snake_case : Dict=5 ,_snake_case : int=True ,_snake_case : Union[str, Any]=25 ,) -> Any: """simple docstring""" lowercase__ : Any = compute_mauve( p_text=_snake_case ,q_text=_snake_case ,p_features=_snake_case ,q_features=_snake_case ,p_tokens=_snake_case ,q_tokens=_snake_case ,num_buckets=_snake_case ,pca_max_data=_snake_case ,kmeans_explained_var=_snake_case ,kmeans_num_redo=_snake_case ,kmeans_max_iter=_snake_case ,featurize_model_name=_snake_case ,device_id=_snake_case ,max_text_length=_snake_case ,divergence_curve_discretization_size=_snake_case ,mauve_scaling_factor=_snake_case ,verbose=_snake_case ,seed=_snake_case ,) return out
357
"""simple docstring""" from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig lowerCAmelCase_ = [ 'openmmlab/upernet-convnext-tiny', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring lowerCAmelCase_ = 'UperNetConfig' class __A ( nn.Module ): '''simple docstring''' def __init__( self : List[str] ,_snake_case : int ,_snake_case : int ,_snake_case : Union[int, Tuple[int, int]] ,_snake_case : Union[int, Tuple[int, int], str] = 0 ,_snake_case : bool = False ,_snake_case : Union[int, Tuple[int, int]] = 1 ,) -> None: """simple docstring""" super().__init__() lowercase__ : Optional[int] = nn.Convad( in_channels=_snake_case ,out_channels=_snake_case ,kernel_size=_snake_case ,padding=_snake_case ,bias=_snake_case ,dilation=_snake_case ,) lowercase__ : Tuple = nn.BatchNormad(_snake_case ) lowercase__ : List[str] = nn.ReLU() def UpperCAmelCase ( self : str ,_snake_case : torch.Tensor ) -> torch.Tensor: """simple docstring""" lowercase__ : Union[str, Any] = self.conv(_snake_case ) lowercase__ : List[str] = self.batch_norm(_snake_case ) lowercase__ : Tuple = self.activation(_snake_case ) return output class __A ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] ,_snake_case : int ,_snake_case : int ,_snake_case : int ) -> None: """simple docstring""" super().__init__() lowercase__ : List[Any] = [ nn.AdaptiveAvgPoolad(_snake_case ), UperNetConvModule(_snake_case ,_snake_case ,kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(_snake_case ) ,_snake_case ) def UpperCAmelCase ( self : Dict ,_snake_case : torch.Tensor ) -> torch.Tensor: """simple docstring""" lowercase__ : Any = input for layer in self.layers: lowercase__ : int = layer(_snake_case ) return hidden_state class __A ( nn.Module ): '''simple docstring''' def __init__( self : List[str] ,_snake_case : Tuple[int, ...] ,_snake_case : int ,_snake_case : int ,_snake_case : bool ) -> None: """simple docstring""" super().__init__() lowercase__ : int = pool_scales lowercase__ : Dict = align_corners lowercase__ : Optional[Any] = in_channels lowercase__ : Optional[Any] = channels lowercase__ : int = [] for i, pool_scale in enumerate(_snake_case ): lowercase__ : Optional[Any] = UperNetPyramidPoolingBlock(pool_scale=_snake_case ,in_channels=_snake_case ,channels=_snake_case ) self.blocks.append(_snake_case ) self.add_module(str(_snake_case ) ,_snake_case ) def UpperCAmelCase ( self : Any ,_snake_case : torch.Tensor ) -> List[torch.Tensor]: """simple docstring""" lowercase__ : int = [] for ppm in self.blocks: lowercase__ : Any = ppm(_snake_case ) lowercase__ : int = nn.functional.interpolate( _snake_case ,size=x.size()[2:] ,mode='''bilinear''' ,align_corners=self.align_corners ) ppm_outs.append(_snake_case ) return ppm_outs class __A ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] ,_snake_case : List[str] ,_snake_case : Union[str, Any] ) -> str: """simple docstring""" super().__init__() lowercase__ : str = config lowercase__ : Optional[Any] = config.pool_scales # e.g. (1, 2, 3, 6) lowercase__ : Optional[Any] = in_channels lowercase__ : Any = config.hidden_size lowercase__ : Optional[Any] = False lowercase__ : Optional[int] = nn.Convad(self.channels ,config.num_labels ,kernel_size=1 ) # PSP Module lowercase__ : Dict = UperNetPyramidPoolingModule( self.pool_scales ,self.in_channels[-1] ,self.channels ,align_corners=self.align_corners ,) lowercase__ : str = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels ,self.channels ,kernel_size=3 ,padding=1 ,) # FPN Module lowercase__ : Any = nn.ModuleList() lowercase__ : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer lowercase__ : List[Any] = UperNetConvModule(_snake_case ,self.channels ,kernel_size=1 ) lowercase__ : Optional[int] = UperNetConvModule(self.channels ,self.channels ,kernel_size=3 ,padding=1 ) self.lateral_convs.append(_snake_case ) self.fpn_convs.append(_snake_case ) lowercase__ : int = UperNetConvModule( len(self.in_channels ) * self.channels ,self.channels ,kernel_size=3 ,padding=1 ,) def UpperCAmelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" self.apply(self._init_weights ) def UpperCAmelCase ( self : Union[str, Any] ,_snake_case : Optional[Any] ) -> List[str]: """simple docstring""" if isinstance(_snake_case ,nn.Convad ): module.weight.data.normal_(mean=0.0 ,std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def UpperCAmelCase ( self : Optional[Any] ,_snake_case : Optional[Any] ) -> str: """simple docstring""" lowercase__ : Dict = inputs[-1] lowercase__ : Optional[int] = [x] psp_outs.extend(self.psp_modules(_snake_case ) ) lowercase__ : Optional[Any] = torch.cat(_snake_case ,dim=1 ) lowercase__ : List[str] = self.bottleneck(_snake_case ) return output def UpperCAmelCase ( self : List[str] ,_snake_case : torch.Tensor ) -> torch.Tensor: """simple docstring""" lowercase__ : Tuple = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(_snake_case ) ) # build top-down path lowercase__ : List[Any] = len(_snake_case ) for i in range(used_backbone_levels - 1 ,0 ,-1 ): lowercase__ : Union[str, Any] = laterals[i - 1].shape[2:] lowercase__ : int = laterals[i - 1] + nn.functional.interpolate( laterals[i] ,size=_snake_case ,mode='''bilinear''' ,align_corners=self.align_corners ) # build outputs lowercase__ : List[str] = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 ,0 ,-1 ): lowercase__ : Any = nn.functional.interpolate( fpn_outs[i] ,size=fpn_outs[0].shape[2:] ,mode='''bilinear''' ,align_corners=self.align_corners ) lowercase__ : Any = torch.cat(_snake_case ,dim=1 ) lowercase__ : Any = self.fpn_bottleneck(_snake_case ) lowercase__ : str = self.classifier(_snake_case ) return output class __A ( nn.Module ): '''simple docstring''' def __init__( self : Dict ,_snake_case : List[Any] ,_snake_case : int = 2 ,_snake_case : int = 3 ,_snake_case : Union[int, Tuple[int, int]] = 1 ) -> None: """simple docstring""" super().__init__() lowercase__ : int = config lowercase__ : Dict = config.auxiliary_in_channels lowercase__ : Optional[int] = config.auxiliary_channels lowercase__ : List[Any] = config.auxiliary_num_convs lowercase__ : List[Any] = config.auxiliary_concat_input lowercase__ : str = in_index lowercase__ : Any = (kernel_size // 2) * dilation lowercase__ : Optional[Any] = [] convs.append( UperNetConvModule( self.in_channels ,self.channels ,kernel_size=_snake_case ,padding=_snake_case ,dilation=_snake_case ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels ,self.channels ,kernel_size=_snake_case ,padding=_snake_case ,dilation=_snake_case ) ) if self.num_convs == 0: lowercase__ : List[str] = nn.Identity() else: lowercase__ : Dict = nn.Sequential(*_snake_case ) if self.concat_input: lowercase__ : int = UperNetConvModule( self.in_channels + self.channels ,self.channels ,kernel_size=_snake_case ,padding=kernel_size // 2 ) lowercase__ : List[str] = nn.Convad(self.channels ,config.num_labels ,kernel_size=1 ) def UpperCAmelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" self.apply(self._init_weights ) def UpperCAmelCase ( self : List[Any] ,_snake_case : List[Any] ) -> Dict: """simple docstring""" if isinstance(_snake_case ,nn.Convad ): module.weight.data.normal_(mean=0.0 ,std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def UpperCAmelCase ( self : List[str] ,_snake_case : torch.Tensor ) -> torch.Tensor: """simple docstring""" lowercase__ : str = encoder_hidden_states[self.in_index] lowercase__ : List[str] = self.convs(_snake_case ) if self.concat_input: lowercase__ : Any = self.conv_cat(torch.cat([hidden_states, output] ,dim=1 ) ) lowercase__ : Dict = self.classifier(_snake_case ) return output class __A ( A_ ): '''simple docstring''' lowerCAmelCase : Any = UperNetConfig lowerCAmelCase : str = "pixel_values" lowerCAmelCase : Dict = True def UpperCAmelCase ( self : int ,_snake_case : str ) -> Optional[int]: """simple docstring""" if isinstance(_snake_case ,_snake_case ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def UpperCAmelCase ( self : List[str] ) -> Dict: """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def UpperCAmelCase ( self : int ,_snake_case : str ,_snake_case : str=False ) -> List[str]: """simple docstring""" if isinstance(_snake_case ,_snake_case ): lowercase__ : List[Any] = value lowerCAmelCase_ = R'\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' lowerCAmelCase_ = R'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes." ,A_ ,) class __A ( A_ ): '''simple docstring''' def __init__( self : Optional[Any] ,_snake_case : Tuple ) -> int: """simple docstring""" super().__init__(_snake_case ) lowercase__ : int = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) lowercase__ : Any = UperNetHead(_snake_case ,in_channels=self.backbone.channels ) lowercase__ : str = UperNetFCNHead(_snake_case ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=_snake_case ,config_class=_CONFIG_FOR_DOC ) def UpperCAmelCase ( self : Dict ,_snake_case : Optional[torch.Tensor] = None ,_snake_case : Optional[bool] = None ,_snake_case : Optional[bool] = None ,_snake_case : Optional[torch.Tensor] = None ,_snake_case : Optional[bool] = None ,) -> Union[tuple, SemanticSegmenterOutput]: """simple docstring""" lowercase__ : int = return_dict if return_dict is not None else self.config.use_return_dict lowercase__ : Any = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase__ : Any = output_attentions if output_attentions is not None else self.config.output_attentions lowercase__ : Optional[Any] = self.backbone.forward_with_filtered_kwargs( _snake_case ,output_hidden_states=_snake_case ,output_attentions=_snake_case ) lowercase__ : Optional[int] = outputs.feature_maps lowercase__ : Tuple = self.decode_head(_snake_case ) lowercase__ : Optional[int] = nn.functional.interpolate(_snake_case ,size=pixel_values.shape[2:] ,mode='''bilinear''' ,align_corners=_snake_case ) lowercase__ : List[str] = None if self.auxiliary_head is not None: lowercase__ : str = self.auxiliary_head(_snake_case ) lowercase__ : Dict = nn.functional.interpolate( _snake_case ,size=pixel_values.shape[2:] ,mode='''bilinear''' ,align_corners=_snake_case ) lowercase__ : Any = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss lowercase__ : Union[str, Any] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) lowercase__ : List[str] = loss_fct(_snake_case ,_snake_case ) lowercase__ : List[str] = loss_fct(_snake_case ,_snake_case ) lowercase__ : Optional[Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: lowercase__ : Tuple = (logits,) + outputs[1:] else: lowercase__ : int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=_snake_case ,logits=_snake_case ,hidden_states=outputs.hidden_states ,attentions=outputs.attentions ,)
302
0
import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": UpperCAmelCase__ : Optional[Any] = argparse.ArgumentParser() parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') parser.add_argument( '--txt2img_unclip', default='kakaobrain/karlo-v1-alpha', type=str, required=False, help='The pretrained txt2img unclip.', ) UpperCAmelCase__ : str = parser.parse_args() UpperCAmelCase__ : Tuple = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) UpperCAmelCase__ : Union[str, Any] = CLIPImageProcessor() UpperCAmelCase__ : List[str] = CLIPVisionModelWithProjection.from_pretrained('openai/clip-vit-large-patch14') UpperCAmelCase__ : Optional[Any] = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
121
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from .config import config_command_parser from .config_args import default_config_file, load_config_from_file # noqa: F401 from .default import default_command_parser from .update import update_command_parser def lowerCamelCase__ ( a=None ) -> int: _A: Union[str, Any] = argparse.ArgumentParser(add_help=a , allow_abbrev=a ) # The main config parser _A: str = config_command_parser(a ) # The subparser to add commands to _A: str = config_parser.add_subparsers(title='''subcommands''' , dest='''subcommand''' ) # Then add other parsers with the parent parser default_command_parser(a , parents=[parent_parser] ) update_command_parser(a , parents=[parent_parser] ) return config_parser def lowerCamelCase__ ( ) -> Union[str, Any]: _A: Any = get_config_parser() _A: Tuple = config_parser.parse_args() if not hasattr(a , '''func''' ): config_parser.print_help() exit(1 ) # Run args.func(a ) if __name__ == "__main__": main()
121
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType snake_case : Union[str, Any] = logging.get_logger(__name__) snake_case : Optional[int] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class snake_case_ (lowerCamelCase_ ): UpperCAmelCase__ : Union[str, Any] = '''layoutlmv3''' def __init__( self :str ,__snake_case :str=5_02_65 ,__snake_case :Any=7_68 ,__snake_case :Union[str, Any]=12 ,__snake_case :str=12 ,__snake_case :Dict=30_72 ,__snake_case :Tuple="gelu" ,__snake_case :str=0.1 ,__snake_case :Optional[int]=0.1 ,__snake_case :List[Any]=5_12 ,__snake_case :List[str]=2 ,__snake_case :List[Any]=0.02 ,__snake_case :Optional[int]=1E-5 ,__snake_case :List[str]=1 ,__snake_case :Tuple=0 ,__snake_case :Optional[int]=2 ,__snake_case :str=10_24 ,__snake_case :List[Any]=1_28 ,__snake_case :Optional[Any]=1_28 ,__snake_case :Any=True ,__snake_case :Optional[Any]=32 ,__snake_case :Optional[int]=1_28 ,__snake_case :Any=64 ,__snake_case :int=2_56 ,__snake_case :str=True ,__snake_case :Union[str, Any]=True ,__snake_case :Tuple=True ,__snake_case :Optional[int]=2_24 ,__snake_case :int=3 ,__snake_case :str=16 ,__snake_case :List[Any]=None ,**__snake_case :int ,) -> Any: super().__init__( vocab_size=__snake_case ,hidden_size=__snake_case ,num_hidden_layers=__snake_case ,num_attention_heads=__snake_case ,intermediate_size=__snake_case ,hidden_act=__snake_case ,hidden_dropout_prob=__snake_case ,attention_probs_dropout_prob=__snake_case ,max_position_embeddings=__snake_case ,type_vocab_size=__snake_case ,initializer_range=__snake_case ,layer_norm_eps=__snake_case ,pad_token_id=__snake_case ,bos_token_id=__snake_case ,eos_token_id=__snake_case ,**__snake_case ,) a__ = max_ad_position_embeddings a__ = coordinate_size a__ = shape_size a__ = has_relative_attention_bias a__ = rel_pos_bins a__ = max_rel_pos a__ = has_spatial_attention_bias a__ = rel_ad_pos_bins a__ = max_rel_ad_pos a__ = text_embed a__ = visual_embed a__ = input_size a__ = num_channels a__ = patch_size a__ = classifier_dropout class snake_case_ (lowerCamelCase_ ): UpperCAmelCase__ : int = version.parse('''1.12''' ) @property def lowerCamelCase__( self :int ) -> Mapping[str, Mapping[int, str]]: # The order of inputs is different for question answering and sequence classification if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('input_ids', {0: 'batch', 1: 'sequence'}), ('attention_mask', {0: 'batch', 1: 'sequence'}), ('bbox', {0: 'batch', 1: 'sequence'}), ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) else: return OrderedDict( [ ('input_ids', {0: 'batch', 1: 'sequence'}), ('bbox', {0: 'batch', 1: 'sequence'}), ('attention_mask', {0: 'batch', 1: 'sequence'}), ('pixel_values', {0: 'batch', 1: 'num_channels'}), ] ) @property def lowerCamelCase__( self :Optional[Any] ) -> float: return 1E-5 @property def lowerCamelCase__( self :List[Any] ) -> int: return 12 def lowerCamelCase__( self :Dict ,__snake_case :"ProcessorMixin" ,__snake_case :int = -1 ,__snake_case :int = -1 ,__snake_case :bool = False ,__snake_case :Optional["TensorType"] = None ,__snake_case :int = 3 ,__snake_case :int = 40 ,__snake_case :int = 40 ,) -> Mapping[str, Any]: setattr(processor.image_processor ,'apply_ocr' ,__snake_case ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX a__ = 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 a__ = processor.tokenizer.num_special_tokens_to_add(__snake_case ) a__ = 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 a__ = [[' '.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes a__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) a__ = self._generate_dummy_images(__snake_case ,__snake_case ,__snake_case ,__snake_case ) a__ = dict( processor( __snake_case ,text=__snake_case ,boxes=__snake_case ,return_tensors=__snake_case ,) ) return inputs
109
from collections import defaultdict from math import ceil, sqrt def __lowercase ( __lowerCAmelCase : int = 1_0_0_0_0_0_0 , __lowerCAmelCase : int = 1_0 ): a__ = defaultdict(__lowerCAmelCase ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: a__ = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: a__ = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(__lowerCAmelCase , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f"""{solution() = }""")
109
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging 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 lowerCAmelCase_ ( A__ ): '''simple docstring''' _snake_case = '''mobilenet_v1''' def __init__( self , snake_case_=3 , snake_case_=224 , snake_case_=1.0 , snake_case_=8 , snake_case_="relu6" , snake_case_=True , snake_case_=0.999 , snake_case_=0.02 , snake_case_=0.001 , **snake_case_ , ) -> Union[str, Any]: super().__init__(**snake_case_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) __lowerCAmelCase = num_channels __lowerCAmelCase = image_size __lowerCAmelCase = depth_multiplier __lowerCAmelCase = min_depth __lowerCAmelCase = hidden_act __lowerCAmelCase = tf_padding __lowerCAmelCase = classifier_dropout_prob __lowerCAmelCase = initializer_range __lowerCAmelCase = layer_norm_eps class lowerCAmelCase_ ( A__ ): '''simple docstring''' _snake_case = version.parse('''1.11''' ) @property def A__ ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def A__ ( 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 A__ ( self ) -> float: return 1e-4
301
"""simple docstring""" def lowercase (_lowerCAmelCase = 100_0000 ): __lowerCAmelCase = [i - 1 for i in range(limit + 1 )] for i in range(2 , limit + 1 ): if phi[i] == i - 1: for j in range(2 * i , limit + 1 , _lowerCAmelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
301
1
from ..utils import DummyObject, requires_backends class SCREAMING_SNAKE_CASE__ ( metaclass=__UpperCamelCase ): A : List[Any] = ["flax", "transformers"] def __init__( self : List[str] , *_lowerCAmelCase : List[Any] , **_lowerCAmelCase : Dict ): requires_backends(self , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : int , *_lowerCAmelCase : str , **_lowerCAmelCase : int ): requires_backends(cls , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : int , *_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : Optional[int] ): requires_backends(cls , ["""flax""", """transformers"""] ) class SCREAMING_SNAKE_CASE__ ( metaclass=__UpperCamelCase ): A : Any = ["flax", "transformers"] def __init__( self : int , *_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : List[str] ): requires_backends(self , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : int , *_lowerCAmelCase : Any , **_lowerCAmelCase : Union[str, Any] ): requires_backends(cls , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : Tuple , *_lowerCAmelCase : List[Any] , **_lowerCAmelCase : List[str] ): requires_backends(cls , ["""flax""", """transformers"""] ) class SCREAMING_SNAKE_CASE__ ( metaclass=__UpperCamelCase ): A : Tuple = ["flax", "transformers"] def __init__( self : Dict , *_lowerCAmelCase : Tuple , **_lowerCAmelCase : Union[str, Any] ): requires_backends(self , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : int , *_lowerCAmelCase : int , **_lowerCAmelCase : Tuple ): requires_backends(cls , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : Any , *_lowerCAmelCase : int , **_lowerCAmelCase : int ): requires_backends(cls , ["""flax""", """transformers"""] ) class SCREAMING_SNAKE_CASE__ ( metaclass=__UpperCamelCase ): A : Optional[Any] = ["flax", "transformers"] def __init__( self : Any , *_lowerCAmelCase : Dict , **_lowerCAmelCase : int ): requires_backends(self , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : Optional[int] , *_lowerCAmelCase : str , **_lowerCAmelCase : List[Any] ): requires_backends(cls , ["""flax""", """transformers"""] ) @classmethod def snake_case__ ( cls : List[str] , *_lowerCAmelCase : Union[str, Any] , **_lowerCAmelCase : List[Any] ): requires_backends(cls , ["""flax""", """transformers"""] )
20
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class SCREAMING_SNAKE_CASE__ ( __UpperCamelCase ): A : Optional[int] = "microsoft/speecht5_tts" A : List[Any] = ( "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the " "text to read (in English) and returns a waveform object containing the sound." ) A : str = "text_reader" A : Optional[Any] = SpeechTaProcessor A : Any = SpeechTaForTextToSpeech A : Optional[Any] = SpeechTaHifiGan A : str = ["text"] A : Union[str, Any] = ["audio"] def snake_case__ ( self : List[Any] ): if self.post_processor is None: __snake_case : Tuple = """microsoft/speecht5_hifigan""" super().setup() def snake_case__ ( self : Any , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Tuple=None ): __snake_case : str = self.pre_processor(text=_lowerCAmelCase , return_tensors="""pt""" , truncation=_lowerCAmelCase ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError("""Datasets needs to be installed if not passing speaker embeddings.""" ) __snake_case : List[Any] = load_dataset("""Matthijs/cmu-arctic-xvectors""" , split="""validation""" ) __snake_case : str = torch.tensor(embeddings_dataset[73_05]["""xvector"""] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def snake_case__ ( self : List[Any] , _lowerCAmelCase : Dict ): with torch.no_grad(): return self.model.generate_speech(**_lowerCAmelCase ) def snake_case__ ( self : Union[str, Any] , _lowerCAmelCase : int ): with torch.no_grad(): return self.post_processor(_lowerCAmelCase ).cpu().detach()
20
1
"""simple docstring""" import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def _snake_case ( lowercase__ ): _lowerCamelCase : Dict = [ 'decoder.version', 'decoder.output_projection.weight', '_float_tensor', 'decoder.embed_positions._float_tensor', ] for k in ignore_keys: state_dict.pop(lowercase__ , lowercase__ ) def _snake_case ( lowercase__ ): _lowerCamelCase, _lowerCamelCase : List[str] = emb.weight.shape _lowerCamelCase : Union[str, Any] = nn.Linear(lowercase__ , lowercase__ , bias=lowercase__ ) _lowerCamelCase : List[str] = emb.weight.data return lin_layer def _snake_case ( lowercase__ ): _lowerCamelCase : List[str] = torch.load(lowercase__ , map_location='cpu' ) _lowerCamelCase : Tuple = Namespace(**checkpoint['cfg']['model'] ) _lowerCamelCase : Optional[int] = checkpoint['model'] remove_ignore_keys_(lowercase__ ) _lowerCamelCase : int = state_dict['decoder.embed_tokens.weight'].shape[0] _lowerCamelCase : Union[str, Any] = {key.replace('decoder' , 'model' ): val for key, val in state_dict.items()} _lowerCamelCase : Tuple = XGLMConfig( vocab_size=lowercase__ , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='gelu' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) _lowerCamelCase : Union[str, Any] = XGLMForCausalLM(lowercase__ ) _lowerCamelCase : Optional[Any] = model.load_state_dict(lowercase__ , strict=lowercase__ ) print(lowercase__ ) _lowerCamelCase : Union[str, Any] = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() # Required parameters parser.add_argument("""fairseq_path""", type=str, help="""path to a model.pt on local filesystem.""") parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") lowercase__ = parser.parse_args() lowercase__ = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
96
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class _lowerCamelCase( unittest.TestCase ): def UpperCamelCase ( self) -> Optional[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase ( self) -> Optional[int]: """simple docstring""" _lowercase : Optional[Any] = 1 _lowercase : Any = 3 _lowercase : Tuple = (32, 32) _lowercase : Tuple = floats_tensor((batch_size, num_channels) + sizes, rng=random.Random(0)).to(lowerCamelCase) return image @property def UpperCamelCase ( self) -> str: """simple docstring""" torch.manual_seed(0) _lowercase : Dict = UNetaDConditionModel( block_out_channels=(32, 64), layers_per_block=2, sample_size=32, in_channels=4, out_channels=4, down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D'), up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D'), cross_attention_dim=32, ) return model @property def UpperCamelCase ( self) -> List[Any]: """simple docstring""" torch.manual_seed(0) _lowercase : str = AutoencoderKL( block_out_channels=[32, 64], in_channels=3, out_channels=3, down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'], up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'], latent_channels=4, ) return model @property def UpperCamelCase ( self) -> Optional[Any]: """simple docstring""" torch.manual_seed(0) _lowercase : Optional[int] = RobertaSeriesConfig( hidden_size=32, project_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_06, ) return RobertaSeriesModelWithTransformation(lowerCamelCase) @property def UpperCamelCase ( self) -> Optional[int]: """simple docstring""" def extract(*lowerCamelCase, **lowerCamelCase): class _lowerCamelCase: def __init__( self) -> Optional[Any]: """simple docstring""" _lowercase : Optional[int] = torch.ones([0]) def UpperCamelCase ( self, lowerCamelCase) -> int: """simple docstring""" self.pixel_values.to(lowerCamelCase) return self return Out() return extract def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : Any = 'cpu' # ensure determinism for the device-dependent torch.Generator _lowercase : List[Any] = self.dummy_cond_unet _lowercase : Union[str, Any] = PNDMScheduler(skip_prk_steps=lowerCamelCase) _lowercase : Optional[Any] = self.dummy_vae _lowercase : List[Any] = self.dummy_text_encoder _lowercase : Any = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta') _lowercase : Tuple = 77 _lowercase : int = self.dummy_image.to(lowerCamelCase) _lowercase : int = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk _lowercase : Union[str, Any] = AltDiffusionImgaImgPipeline( unet=lowerCamelCase, scheduler=lowerCamelCase, vae=lowerCamelCase, text_encoder=lowerCamelCase, tokenizer=lowerCamelCase, safety_checker=lowerCamelCase, feature_extractor=self.dummy_extractor, ) _lowercase : List[Any] = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor, do_normalize=lowerCamelCase) _lowercase : Optional[int] = alt_pipe.to(lowerCamelCase) alt_pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : Optional[Any] = 'A painting of a squirrel eating a burger' _lowercase : Dict = torch.Generator(device=lowerCamelCase).manual_seed(0) _lowercase : Any = alt_pipe( [prompt], generator=lowerCamelCase, guidance_scale=6.0, num_inference_steps=2, output_type='np', image=lowerCamelCase, ) _lowercase : Optional[int] = output.images _lowercase : Optional[Any] = torch.Generator(device=lowerCamelCase).manual_seed(0) _lowercase : Optional[Any] = alt_pipe( [prompt], generator=lowerCamelCase, guidance_scale=6.0, num_inference_steps=2, output_type='np', image=lowerCamelCase, return_dict=lowerCamelCase, )[0] _lowercase : Optional[int] = image[0, -3:, -3:, -1] _lowercase : Dict = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _lowercase : int = np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9]) assert np.abs(image_slice.flatten() - expected_slice).max() < 5E-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 5E-3 @unittest.skipIf(torch_device != 'cuda', 'This test requires a GPU') def UpperCamelCase ( self) -> str: """simple docstring""" _lowercase : List[Any] = self.dummy_cond_unet _lowercase : Tuple = PNDMScheduler(skip_prk_steps=lowerCamelCase) _lowercase : str = self.dummy_vae _lowercase : Optional[Any] = self.dummy_text_encoder _lowercase : Optional[Any] = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta') _lowercase : Optional[Any] = 77 _lowercase : str = self.dummy_image.to(lowerCamelCase) # put models in fp16 _lowercase : List[str] = unet.half() _lowercase : List[Any] = vae.half() _lowercase : Any = bert.half() # make sure here that pndm scheduler skips prk _lowercase : Union[str, Any] = AltDiffusionImgaImgPipeline( unet=lowerCamelCase, scheduler=lowerCamelCase, vae=lowerCamelCase, text_encoder=lowerCamelCase, tokenizer=lowerCamelCase, safety_checker=lowerCamelCase, feature_extractor=self.dummy_extractor, ) _lowercase : List[str] = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor, do_normalize=lowerCamelCase) _lowercase : Any = alt_pipe.to(lowerCamelCase) alt_pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : int = 'A painting of a squirrel eating a burger' _lowercase : Optional[Any] = torch.manual_seed(0) _lowercase : Union[str, Any] = alt_pipe( [prompt], generator=lowerCamelCase, num_inference_steps=2, output_type='np', image=lowerCamelCase, ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != 'cuda', 'This test requires a GPU') def UpperCamelCase ( self) -> Optional[int]: """simple docstring""" _lowercase : int = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg') # resize to resolution that is divisible by 8 but not 16 or 32 _lowercase : str = init_image.resize((7_60, 5_04)) _lowercase : Optional[int] = 'BAAI/AltDiffusion' _lowercase : str = AltDiffusionImgaImgPipeline.from_pretrained( lowerCamelCase, safety_checker=lowerCamelCase, ) pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) pipe.enable_attention_slicing() _lowercase : List[str] = 'A fantasy landscape, trending on artstation' _lowercase : Any = torch.manual_seed(0) _lowercase : Dict = pipe( prompt=lowerCamelCase, image=lowerCamelCase, strength=0.7_5, guidance_scale=7.5, generator=lowerCamelCase, output_type='np', ) _lowercase : List[str] = output.images[0] _lowercase : Tuple = image[2_55:2_58, 3_83:3_86, -1] assert image.shape == (5_04, 7_60, 3) _lowercase : Optional[Any] = np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-2 @slow @require_torch_gpu class _lowerCamelCase( unittest.TestCase ): def UpperCamelCase ( self) -> Optional[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self) -> List[Any]: """simple docstring""" _lowercase : Union[str, Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/img2img/sketch-mountains-input.jpg') _lowercase : str = init_image.resize((7_68, 5_12)) _lowercase : Any = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy') _lowercase : str = 'BAAI/AltDiffusion' _lowercase : Optional[Any] = AltDiffusionImgaImgPipeline.from_pretrained( lowerCamelCase, safety_checker=lowerCamelCase, ) pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) pipe.enable_attention_slicing() _lowercase : int = 'A fantasy landscape, trending on artstation' _lowercase : List[Any] = torch.manual_seed(0) _lowercase : int = pipe( prompt=lowerCamelCase, image=lowerCamelCase, strength=0.7_5, guidance_scale=7.5, generator=lowerCamelCase, output_type='np', ) _lowercase : Union[str, Any] = output.images[0] assert image.shape == (5_12, 7_68, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image).max() < 1E-2
21
0
'''simple docstring''' import flax.linen as nn import jax import jax.numpy as jnp class A__ ( nn.Module ): A__ = 42 A__ = jnp.floataa def A ( self : Tuple ) -> List[Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : Tuple , _a : Any ) -> Any: '''simple docstring''' _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =hidden_states.shape _SCREAMING_SNAKE_CASE =jax.image.resize( _a , shape=(batch, height * 2, width * 2, channels) , method='nearest' , ) _SCREAMING_SNAKE_CASE =self.conv(_a ) return hidden_states class A__ ( nn.Module ): A__ = 42 A__ = jnp.floataa def A ( self : Optional[int] ) -> Tuple: '''simple docstring''' _SCREAMING_SNAKE_CASE =nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : List[str] , _a : List[str] ) -> List[str]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.conv(_a ) return hidden_states class A__ ( nn.Module ): A__ = 42 A__ = None A__ = 0.0 A__ = None A__ = jnp.floataa def A ( self : Dict ) -> List[Any]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self.in_channels if self.out_channels is None else self.out_channels _SCREAMING_SNAKE_CASE =nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) _SCREAMING_SNAKE_CASE =nn.Conv( _a , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) _SCREAMING_SNAKE_CASE =nn.Dense(_a , dtype=self.dtype ) _SCREAMING_SNAKE_CASE =nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) _SCREAMING_SNAKE_CASE =nn.Dropout(self.dropout_prob ) _SCREAMING_SNAKE_CASE =nn.Conv( _a , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) _SCREAMING_SNAKE_CASE =self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut _SCREAMING_SNAKE_CASE =None if use_nin_shortcut: _SCREAMING_SNAKE_CASE =nn.Conv( _a , kernel_size=(1, 1) , strides=(1, 1) , padding='VALID' , dtype=self.dtype , ) def __call__( self : int , _a : str , _a : Any , _a : List[str]=True ) -> Dict: '''simple docstring''' _SCREAMING_SNAKE_CASE =hidden_states _SCREAMING_SNAKE_CASE =self.norma(_a ) _SCREAMING_SNAKE_CASE =nn.swish(_a ) _SCREAMING_SNAKE_CASE =self.conva(_a ) _SCREAMING_SNAKE_CASE =self.time_emb_proj(nn.swish(_a ) ) _SCREAMING_SNAKE_CASE =jnp.expand_dims(jnp.expand_dims(_a , 1 ) , 1 ) _SCREAMING_SNAKE_CASE =hidden_states + temb _SCREAMING_SNAKE_CASE =self.norma(_a ) _SCREAMING_SNAKE_CASE =nn.swish(_a ) _SCREAMING_SNAKE_CASE =self.dropout(_a , _a ) _SCREAMING_SNAKE_CASE =self.conva(_a ) if self.conv_shortcut is not None: _SCREAMING_SNAKE_CASE =self.conv_shortcut(_a ) return hidden_states + residual
114
'''simple docstring''' from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def _lowerCAmelCase ( _UpperCamelCase : int ) -> bool: """simple docstring""" _SCREAMING_SNAKE_CASE =int(number**0.5 ) return number == sq * sq def _lowerCAmelCase ( _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int ) -> tuple[int, int]: """simple docstring""" _SCREAMING_SNAKE_CASE =x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den _SCREAMING_SNAKE_CASE =x_den * y_den * z_den _SCREAMING_SNAKE_CASE =gcd(_UpperCamelCase , _UpperCamelCase ) top //= hcf bottom //= hcf return top, bottom def _lowerCAmelCase ( _UpperCamelCase : int = 35 ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =set() _SCREAMING_SNAKE_CASE =42 _SCREAMING_SNAKE_CASE =Fraction(0 ) _SCREAMING_SNAKE_CASE =42 for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 _SCREAMING_SNAKE_CASE =x_num * y_den + x_den * y_num _SCREAMING_SNAKE_CASE =x_den * y_den _SCREAMING_SNAKE_CASE =gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE =add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) # n=2 _SCREAMING_SNAKE_CASE =( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) _SCREAMING_SNAKE_CASE =x_den * x_den * y_den * y_den if is_sq(_UpperCamelCase ) and is_sq(_UpperCamelCase ): _SCREAMING_SNAKE_CASE =int(sqrt(_UpperCamelCase ) ) _SCREAMING_SNAKE_CASE =int(sqrt(_UpperCamelCase ) ) _SCREAMING_SNAKE_CASE =gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE =add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) # n=-1 _SCREAMING_SNAKE_CASE =x_num * y_num _SCREAMING_SNAKE_CASE =x_den * y_num + x_num * y_den _SCREAMING_SNAKE_CASE =gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE =add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) # n=2 _SCREAMING_SNAKE_CASE =x_num * x_num * y_num * y_num _SCREAMING_SNAKE_CASE =( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(_UpperCamelCase ) and is_sq(_UpperCamelCase ): _SCREAMING_SNAKE_CASE =int(sqrt(_UpperCamelCase ) ) _SCREAMING_SNAKE_CASE =int(sqrt(_UpperCamelCase ) ) _SCREAMING_SNAKE_CASE =gcd(_UpperCamelCase , _UpperCamelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _SCREAMING_SNAKE_CASE =add_three( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) unique_s.add(_UpperCamelCase ) for num, den in unique_s: total += Fraction(_UpperCamelCase , _UpperCamelCase ) return total.denominator + total.numerator if __name__ == "__main__": print(f'''{solution() = }''')
114
1
from __future__ import annotations from collections import namedtuple def UpperCamelCase ( _A, _A, _A ): """simple docstring""" __magic_name__ : Optional[Any] = namedtuple("""result""", """name value""" ) if (voltage, current, power).count(0 ) != 1: raise ValueError("""Only one argument must be 0""" ) elif power < 0: raise ValueError( """Power cannot be negative in any electrical/electronics system""" ) elif voltage == 0: return result("""voltage""", power / current ) elif current == 0: return result("""current""", power / voltage ) elif power == 0: return result("""power""", float(round(abs(voltage * current ), 2 ) ) ) else: raise ValueError("""Exactly one argument must be 0""" ) if __name__ == "__main__": import doctest doctest.testmod()
342
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging lowerCamelCase__ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( lowerCamelCase__ ): __lowerCamelCase : Dict =['pixel_values'] def __init__( self : Optional[int] , __lowercase : bool = True , __lowercase : Optional[Dict[str, int]] = None , __lowercase : PILImageResampling = PILImageResampling.BICUBIC , __lowercase : bool = True , __lowercase : bool = True , __lowercase : Union[int, float] = 1 / 255 , __lowercase : Dict[str, int] = None , __lowercase : bool = True , __lowercase : Optional[Union[float, List[float]]] = None , __lowercase : Optional[Union[float, List[float]]] = None , **__lowercase : Dict , ): '''simple docstring''' super().__init__(**__lowercase ) __a = size if size is not None else {"""height""": 224, """width""": 224} __a = get_size_dict(__lowercase ) __a = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} __a = get_size_dict(__lowercase , default_to_square=__lowercase , param_name="""crop_size""" ) __a = do_resize __a = do_rescale __a = do_normalize __a = do_center_crop __a = crop_size __a = size __a = resample __a = rescale_factor __a = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN __a = image_std if image_std is not None else IMAGENET_DEFAULT_STD def UpperCamelCase_ ( self : Any , __lowercase : np.ndarray , __lowercase : Dict[str, int] , __lowercase : PILImageResampling = PILImageResampling.BILINEAR , __lowercase : Optional[Union[str, ChannelDimension]] = None , **__lowercase : Optional[Any] , ): '''simple docstring''' __a = get_size_dict(__lowercase ) if "shortest_edge" in size: __a = get_resize_output_image_size(__lowercase , size=size["""shortest_edge"""] , default_to_square=__lowercase ) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: __a = (size["""height"""], size["""width"""]) else: raise ValueError(F"Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got {size.keys()}" ) return resize(__lowercase , size=__lowercase , resample=__lowercase , data_format=__lowercase , **__lowercase ) def UpperCamelCase_ ( self : str , __lowercase : np.ndarray , __lowercase : Dict[str, int] , __lowercase : Optional[Union[str, ChannelDimension]] = None , **__lowercase : List[Any] , ): '''simple docstring''' __a = get_size_dict(__lowercase ) if "height" not in size or "width" not in size: raise ValueError(F"The `size` parameter must contain the keys (height, width). Got {size.keys()}" ) return center_crop(__lowercase , size=(size["""height"""], size["""width"""]) , data_format=__lowercase , **__lowercase ) def UpperCamelCase_ ( self : Any , __lowercase : np.ndarray , __lowercase : float , __lowercase : Optional[Union[str, ChannelDimension]] = None , **__lowercase : str ): '''simple docstring''' return rescale(__lowercase , scale=__lowercase , data_format=__lowercase , **__lowercase ) def UpperCamelCase_ ( self : List[Any] , __lowercase : np.ndarray , __lowercase : Union[float, List[float]] , __lowercase : Union[float, List[float]] , __lowercase : Optional[Union[str, ChannelDimension]] = None , **__lowercase : Any , ): '''simple docstring''' return normalize(__lowercase , mean=__lowercase , std=__lowercase , data_format=__lowercase , **__lowercase ) def UpperCamelCase_ ( self : Tuple , __lowercase : ImageInput , __lowercase : Optional[bool] = None , __lowercase : Dict[str, int] = None , __lowercase : PILImageResampling = None , __lowercase : bool = None , __lowercase : int = None , __lowercase : Optional[bool] = None , __lowercase : Optional[float] = None , __lowercase : Optional[bool] = None , __lowercase : Optional[Union[float, List[float]]] = None , __lowercase : Optional[Union[float, List[float]]] = None , __lowercase : Optional[Union[str, TensorType]] = None , __lowercase : Union[str, ChannelDimension] = ChannelDimension.FIRST , **__lowercase : List[Any] , ): '''simple docstring''' __a = do_resize if do_resize is not None else self.do_resize __a = do_rescale if do_rescale is not None else self.do_rescale __a = do_normalize if do_normalize is not None else self.do_normalize __a = do_center_crop if do_center_crop is not None else self.do_center_crop __a = crop_size if crop_size is not None else self.crop_size __a = get_size_dict(__lowercase , param_name="""crop_size""" , default_to_square=__lowercase ) __a = resample if resample is not None else self.resample __a = rescale_factor if rescale_factor is not None else self.rescale_factor __a = image_mean if image_mean is not None else self.image_mean __a = image_std if image_std is not None else self.image_std __a = size if size is not None else self.size __a = get_size_dict(__lowercase ) if not is_batched(__lowercase ): __a = [images] if not valid_images(__lowercase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) # All transformations expect numpy arrays. __a = [to_numpy_array(__lowercase ) for image in images] if do_resize: __a = [self.resize(image=__lowercase , size=__lowercase , resample=__lowercase ) for image in images] if do_center_crop: __a = [self.center_crop(image=__lowercase , size=__lowercase ) for image in images] if do_rescale: __a = [self.rescale(image=__lowercase , scale=__lowercase ) for image in images] if do_normalize: __a = [self.normalize(image=__lowercase , mean=__lowercase , std=__lowercase ) for image in images] __a = [to_channel_dimension_format(__lowercase , __lowercase ) for image in images] __a = {"""pixel_values""": images} return BatchFeature(data=__lowercase , tensor_type=__lowercase )
302
0
'''simple docstring''' import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A : Dict = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __lowerCamelCase ( a_ , unittest.TestCase ): """simple docstring""" a = XLMProphetNetTokenizer a = False a = True def A ( self : int): super().setUp() # We have a SentencePiece fixture for testing _A : str = XLMProphetNetTokenizer(SCREAMING_SNAKE_CASE , keep_accents=SCREAMING_SNAKE_CASE) tokenizer.save_pretrained(self.tmpdirname) def A ( self : Tuple): _A : int = '[PAD]' _A : Any = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(SCREAMING_SNAKE_CASE) , SCREAMING_SNAKE_CASE) self.assertEqual(self.get_tokenizer()._convert_id_to_token(SCREAMING_SNAKE_CASE) , SCREAMING_SNAKE_CASE) def A ( self : Any): _A : Tuple = list(self.get_tokenizer().get_vocab().keys()) self.assertEqual(vocab_keys[0] , '[PAD]') self.assertEqual(vocab_keys[1] , '[CLS]') self.assertEqual(vocab_keys[-1] , 'j') self.assertEqual(len(SCREAMING_SNAKE_CASE) , 1012) def A ( self : Union[str, Any]): self.assertEqual(self.get_tokenizer().vocab_size , 1012) def A ( self : Optional[int]): _A : Optional[Any] = XLMProphetNetTokenizer(SCREAMING_SNAKE_CASE , keep_accents=SCREAMING_SNAKE_CASE) _A : List[str] = tokenizer.tokenize('This is a test') self.assertListEqual(SCREAMING_SNAKE_CASE , ['▁This', '▁is', '▁a', '▁t', 'est']) self.assertListEqual( tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _A : Tuple = tokenizer.tokenize('I was born in 92000, and this is falsé.') self.assertListEqual( SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) _A : Tuple = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE) self.assertListEqual( SCREAMING_SNAKE_CASE , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4] ] , ) _A : Optional[Any] = tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE) self.assertListEqual( SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '[UNK]', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '[UNK]', '.', ] , ) @cached_property def A ( self : List[Any]): return XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased') @slow def A ( self : Optional[int]): _A : List[Any] = 'Hello World!' _A : Dict = [35389, 6672, 49, 2] self.assertListEqual(SCREAMING_SNAKE_CASE , self.big_tokenizer.encode(SCREAMING_SNAKE_CASE)) @slow def A ( self : Union[str, Any]): # fmt: off _A : Tuple = {'input_ids': [[11073, 82783, 18, 26, 82783, 549, 51540, 248, 17209, 1301, 217, 20, 215186, 1325, 147, 17209, 1301, 217, 20, 56370, 53, 122020, 20, 16477, 27, 87355, 4548, 20, 4728, 78392, 17, 159969, 18, 26, 24491, 629, 15, 538, 22704, 5439, 15, 2788, 24491, 9885, 15, 43534, 605, 15, 814, 18403, 33200, 29, 15, 43534, 24458, 12410, 111, 24966, 83669, 9637, 144068, 26, 850, 22346, 27, 147, 24966, 83669, 83490, 26, 39113, 735, 27, 689, 656, 2800, 1339, 4600, 53, 122020, 115785, 34, 816, 1339, 46887, 18, 147, 53905, 1951, 42238, 41170, 17732, 834, 436, 15, 27523, 98733, 217, 147, 5542, 4981, 930, 17347, 16, 2], [20091, 629, 94, 82786, 58, 490, 20, 1528, 84, 53905, 344, 80592, 110128, 18822, 5267, 1306, 62, 152537, 308, 7997, 401, 124427, 549, 35442, 225, 109, 15055, 25748, 147, 7119, 43712, 34, 767, 135366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 63784, 119466, 17, 147808, 88214, 18, 656, 81, 32, 3296, 10280, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=SCREAMING_SNAKE_CASE , model_name='microsoft/xprophetnet-large-wiki100-cased' , revision='1acad1643ddd54a44df6a1b797ada8373685d90e' , )
227
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase A : Tuple = logging.get_logger(__name__) A : Tuple = { '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json''', '''allenai/longformer-large-4096''': '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json''', '''allenai/longformer-large-4096-finetuned-triviaqa''': ( '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json''' ), '''allenai/longformer-base-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json''' ), '''allenai/longformer-large-4096-extra.pos.embd.only''': ( '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json''' ), } class __lowerCamelCase ( a_ ): """simple docstring""" a = "longformer" def __init__( self : Any , SCREAMING_SNAKE_CASE : Union[List[int], int] = 512 , SCREAMING_SNAKE_CASE : int = 2 , SCREAMING_SNAKE_CASE : int = 1 , SCREAMING_SNAKE_CASE : int = 0 , SCREAMING_SNAKE_CASE : int = 2 , SCREAMING_SNAKE_CASE : int = 30522 , SCREAMING_SNAKE_CASE : int = 768 , SCREAMING_SNAKE_CASE : int = 12 , SCREAMING_SNAKE_CASE : int = 12 , SCREAMING_SNAKE_CASE : int = 3072 , SCREAMING_SNAKE_CASE : str = "gelu" , SCREAMING_SNAKE_CASE : float = 0.1 , SCREAMING_SNAKE_CASE : float = 0.1 , SCREAMING_SNAKE_CASE : int = 512 , SCREAMING_SNAKE_CASE : int = 2 , SCREAMING_SNAKE_CASE : float = 0.02 , SCREAMING_SNAKE_CASE : float = 1e-12 , SCREAMING_SNAKE_CASE : bool = False , **SCREAMING_SNAKE_CASE : List[Any] , ): super().__init__(pad_token_id=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE) _A : List[Any] = attention_window _A : int = sep_token_id _A : Tuple = bos_token_id _A : Any = eos_token_id _A : List[str] = vocab_size _A : Any = hidden_size _A : Optional[int] = num_hidden_layers _A : int = num_attention_heads _A : Dict = hidden_act _A : List[Any] = intermediate_size _A : int = hidden_dropout_prob _A : Optional[int] = attention_probs_dropout_prob _A : int = max_position_embeddings _A : Any = type_vocab_size _A : Dict = initializer_range _A : Any = layer_norm_eps _A : List[Any] = onnx_export class __lowerCamelCase ( a_ ): """simple docstring""" def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE : "PretrainedConfig" , SCREAMING_SNAKE_CASE : str = "default" , SCREAMING_SNAKE_CASE : "List[PatchingSpec]" = None): super().__init__(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE) _A : Optional[Any] = True @property def A ( self : List[str]): if self.task == "multiple-choice": _A : Tuple = {0: 'batch', 1: 'choice', 2: 'sequence'} else: _A : List[Any] = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('global_attention_mask', dynamic_axis), ]) @property def A ( self : str): _A : int = super().outputs if self.task == "default": _A : str = {0: 'batch'} return outputs @property def A ( self : List[Any]): return 1e-4 @property def A ( self : Dict): # needs to be >= 14 to support tril operator return max(super().default_onnx_opset , 14) def A ( self : str , SCREAMING_SNAKE_CASE : "PreTrainedTokenizerBase" , SCREAMING_SNAKE_CASE : int = -1 , SCREAMING_SNAKE_CASE : int = -1 , SCREAMING_SNAKE_CASE : bool = False , SCREAMING_SNAKE_CASE : Optional[TensorType] = None , ): _A : Union[str, Any] = super().generate_dummy_inputs( preprocessor=SCREAMING_SNAKE_CASE , batch_size=SCREAMING_SNAKE_CASE , seq_length=SCREAMING_SNAKE_CASE , is_pair=SCREAMING_SNAKE_CASE , framework=SCREAMING_SNAKE_CASE) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly _A : Tuple = torch.zeros_like(inputs['input_ids']) # make every second token global _A : Dict = 1 return inputs
227
1
"""simple docstring""" 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 SCREAMING_SNAKE_CASE__ : def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=30 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=0.6 , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: '''simple docstring''' UpperCAmelCase : Union[str, Any] = parent UpperCAmelCase : str = batch_size UpperCAmelCase : List[str] = image_size UpperCAmelCase : Tuple = patch_size UpperCAmelCase : Union[str, Any] = num_channels UpperCAmelCase : int = is_training UpperCAmelCase : str = use_labels UpperCAmelCase : List[Any] = hidden_size UpperCAmelCase : Optional[int] = num_hidden_layers UpperCAmelCase : List[str] = num_attention_heads UpperCAmelCase : Dict = intermediate_size UpperCAmelCase : Optional[int] = hidden_act UpperCAmelCase : Union[str, Any] = hidden_dropout_prob UpperCAmelCase : Optional[int] = attention_probs_dropout_prob UpperCAmelCase : str = type_sequence_label_size UpperCAmelCase : Dict = initializer_range UpperCAmelCase : Dict = mask_ratio UpperCAmelCase : Union[str, Any] = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) UpperCAmelCase : Dict = (image_size // patch_size) ** 2 UpperCAmelCase : List[Any] = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: '''simple docstring''' UpperCAmelCase : str = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase : str = None if self.use_labels: UpperCAmelCase : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase : Optional[int] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: '''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=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : List[str] = TFViTMAEModel(config=_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = model(_SCREAMING_SNAKE_CASE , training=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: '''simple docstring''' UpperCAmelCase : Tuple = TFViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Any = model(_SCREAMING_SNAKE_CASE , training=_SCREAMING_SNAKE_CASE ) # expected sequence length = num_patches UpperCAmelCase : Optional[Any] = (self.image_size // self.patch_size) ** 2 UpperCAmelCase : Union[str, Any] = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images UpperCAmelCase : List[Any] = 1 UpperCAmelCase : int = TFViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Any = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase : Tuple = model(_SCREAMING_SNAKE_CASE , training=_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Dict = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' UpperCAmelCase : Any = self.prepare_config_and_inputs() ((UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase)) : Tuple = config_and_inputs UpperCAmelCase : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): __lowerCAmelCase : Dict = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () __lowerCAmelCase : List[Any] = {'feature-extraction': TFViTMAEModel} if is_tf_available() else {} __lowerCAmelCase : str = False __lowerCAmelCase : Optional[int] = False __lowerCAmelCase : List[Any] = False __lowerCAmelCase : Tuple = False def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Optional[int] = TFViTMAEModelTester(self ) UpperCAmelCase : List[Any] = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMAE does not use inputs_embeds""" ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : Any = model_class(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCAmelCase : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE , tf.keras.layers.Layer ) ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase : Any = model_class(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : List[Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase : Optional[int] = [*signature.parameters.keys()] UpperCAmelCase : Dict = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Optional[int] = int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : Any = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCAmelCase : Optional[int] = model_class(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Any = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[int] = model(_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = copy.deepcopy(self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) UpperCAmelCase : str = model(**_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Union[str, Any] = outputs_dict[0].numpy() UpperCAmelCase : Dict = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1E-6 ) def SCREAMING_SNAKE_CASE ( self ) -> Any: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : int = int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(_SCREAMING_SNAKE_CASE ): UpperCAmelCase : Optional[Any] = {} for k, v in inputs_dict.items(): if tf.is_tensor(_SCREAMING_SNAKE_CASE ): UpperCAmelCase : int = v.numpy() else: UpperCAmelCase : str = np.array(_SCREAMING_SNAKE_CASE ) return inputs_np_dict for model_class in self.all_model_classes: UpperCAmelCase : Optional[int] = model_class(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase : Union[str, Any] = prepare_numpy_arrays(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = model(_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Tuple = model(**_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) self.assert_outputs_same(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase : List[Any] = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) UpperCAmelCase : str = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCAmelCase : List[str] = tf.constant(_SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument UpperCAmelCase : Any = tf_noise super().check_pt_tf_models(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Union[str, Any] = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(_SCREAMING_SNAKE_CASE ) 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(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ),) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(_SCREAMING_SNAKE_CASE , """_keras_serializable""" , _SCREAMING_SNAKE_CASE ) } UpperCAmelCase : List[Any] = int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : str = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCAmelCase : str = tf.convert_to_tensor(_SCREAMING_SNAKE_CASE ) inputs_dict.update({"""noise""": noise} ) for main_layer_class in tf_main_layer_classes: UpperCAmelCase : List[Any] = main_layer_class(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } UpperCAmelCase : str = tf.keras.Model(_SCREAMING_SNAKE_CASE , outputs=main_layer(_SCREAMING_SNAKE_CASE ) ) UpperCAmelCase : Dict = model(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCAmelCase : Any = os.path.join(_SCREAMING_SNAKE_CASE , """keras_model.h5""" ) model.save(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Dict = tf.keras.models.load_model( _SCREAMING_SNAKE_CASE , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(_SCREAMING_SNAKE_CASE , tf.keras.Model ) UpperCAmelCase : int = model(_SCREAMING_SNAKE_CASE ) self.assert_outputs_same(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : Dict = int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCAmelCase : Dict = model_class(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = model(_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) if model_class.__name__ == "TFViTMAEModel": UpperCAmelCase : int = outputs.last_hidden_state.numpy() UpperCAmelCase : int = 0 else: UpperCAmelCase : Optional[Any] = outputs.logits.numpy() UpperCAmelCase : int = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE , saved_model=_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = model_class.from_pretrained(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[int] = model(_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) if model_class.__name__ == "TFViTMAEModel": UpperCAmelCase : Tuple = after_outputs["""last_hidden_state"""].numpy() UpperCAmelCase : Optional[int] = 0 else: UpperCAmelCase : str = after_outputs["""logits"""].numpy() UpperCAmelCase : Dict = 0 UpperCAmelCase : List[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-5 ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase , UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : List[str] = int((config.image_size // config.patch_size) ** 2 ) UpperCAmelCase : Union[str, Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCAmelCase : Dict = model_class(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Union[str, Any] = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase : Union[str, Any] = model(_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) UpperCAmelCase : int = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Dict = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config UpperCAmelCase : List[Any] = model_class.from_config(model.config ) UpperCAmelCase : List[Any] = new_model(_SCREAMING_SNAKE_CASE ) # Build model new_model.set_weights(model.get_weights() ) UpperCAmelCase : Union[str, Any] = new_model(_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) self.assert_outputs_same(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: '''simple docstring''' pass @unittest.skip(reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load""" ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' pass @slow def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' UpperCAmelCase : List[str] = TFViTMAEModel.from_pretrained("""google/vit-base-patch16-224""" ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _snake_case ( ): UpperCAmelCase : Union[str, Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): @cached_property def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base""" ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: '''simple docstring''' np.random.seed(2 ) UpperCAmelCase : Optional[int] = TFViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base""" ) UpperCAmelCase : List[str] = self.default_image_processor UpperCAmelCase : Optional[int] = prepare_img() UpperCAmelCase : int = image_processor(images=_SCREAMING_SNAKE_CASE , 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) UpperCAmelCase : int = ViTMAEConfig() UpperCAmelCase : List[Any] = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) UpperCAmelCase : Any = np.random.uniform(size=(1, num_patches) ) # forward pass UpperCAmelCase : str = model(**_SCREAMING_SNAKE_CASE , noise=_SCREAMING_SNAKE_CASE ) # verify the logits UpperCAmelCase : List[str] = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) UpperCAmelCase : Any = 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] , _SCREAMING_SNAKE_CASE , atol=1E-4 )
109
"""simple docstring""" import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def _snake_case ( UpperCamelCase : Dataset , UpperCamelCase : Dict[str, str] ): UpperCAmelCase : Any = args.log_outputs UpperCAmelCase : Any = """_""".join(args.dataset.split("""/""" ) + [args.config, args.split] ) # load metric UpperCAmelCase : List[Any] = load_metric("""wer""" ) UpperCAmelCase : Any = load_metric("""cer""" ) # compute metrics UpperCAmelCase : int = wer.compute(references=result["""target"""] , predictions=result["""prediction"""] ) UpperCAmelCase : str = cer.compute(references=result["""target"""] , predictions=result["""prediction"""] ) # print & log results UpperCAmelCase : Tuple = F"WER: {wer_result}\nCER: {cer_result}" print(UpperCamelCase ) with open(F"{dataset_id}_eval_results.txt" , """w""" ) as f: f.write(UpperCamelCase ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: UpperCAmelCase : str = F"log_{dataset_id}_predictions.txt" UpperCAmelCase : Tuple = F"log_{dataset_id}_targets.txt" with open(UpperCamelCase , """w""" ) as p, open(UpperCamelCase , """w""" ) as t: # mapping function to write output def write_to_file(UpperCamelCase : List[Any] , UpperCamelCase : List[Any] ): p.write(F"{i}" + """\n""" ) p.write(batch["""prediction"""] + """\n""" ) t.write(F"{i}" + """\n""" ) t.write(batch["""target"""] + """\n""" ) result.map(UpperCamelCase , with_indices=UpperCamelCase ) def _snake_case ( UpperCamelCase : str ): UpperCAmelCase : List[str] = """[,?.!\-\;\:\"“%‘”�—’…–]""" # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training UpperCAmelCase : Dict = re.sub(UpperCamelCase , """""" , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! UpperCAmelCase : List[str] = ["""\n\n""", """\n""", """ """, """ """] for t in token_sequences_to_ignore: UpperCAmelCase : Optional[Any] = """ """.join(text.split(UpperCamelCase ) ) return text def _snake_case ( UpperCamelCase : Tuple ): # load dataset UpperCAmelCase : Union[str, Any] = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=UpperCamelCase ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor UpperCAmelCase : Optional[int] = AutoFeatureExtractor.from_pretrained(args.model_id ) UpperCAmelCase : Any = feature_extractor.sampling_rate # resample audio UpperCAmelCase : List[str] = dataset.cast_column("""audio""" , Audio(sampling_rate=UpperCamelCase ) ) # load eval pipeline if args.device is None: UpperCAmelCase : Optional[int] = 0 if torch.cuda.is_available() else -1 UpperCAmelCase : Tuple = pipeline("""automatic-speech-recognition""" , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(UpperCamelCase : Any ): UpperCAmelCase : Any = asr( batch["""audio"""]["""array"""] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) UpperCAmelCase : Tuple = prediction["""text"""] UpperCAmelCase : List[str] = normalize_text(batch["""sentence"""] ) return batch # run inference on all examples UpperCAmelCase : int = dataset.map(UpperCamelCase , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(UpperCamelCase , UpperCamelCase ) if __name__ == "__main__": A: List[Any] = argparse.ArgumentParser() parser.add_argument( "--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers" ) parser.add_argument( "--dataset", type=str, required=True, help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets", ) parser.add_argument( "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice" ) parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`") parser.add_argument( "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds." ) parser.add_argument( "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second." ) parser.add_argument( "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis." ) parser.add_argument( "--device", type=int, default=None, help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.", ) A: Union[str, Any] = parser.parse_args() main(args)
109
1
from PIL import Image def lowerCAmelCase__ ( _a : Image ): snake_case_ , snake_case_ : Optional[int] = image.size snake_case_ : int = 0 snake_case_ : Optional[int] = image.load() for i in range(_a ): for j in range(_a ): snake_case_ : Union[str, Any] = pixels[j, i] mean += pixel mean //= width * height for j in range(_a ): for i in range(_a ): snake_case_ : Tuple = 2_55 if pixels[i, j] > mean else 0 return image if __name__ == "__main__": lowercase : Optional[Any] = mean_threshold(Image.open('''path_to_image''').convert('''L''')) image.save('''output_image_path''')
36
import unittest import numpy as np from transformers import BertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.bert.modeling_flax_bert import ( FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, ) class UpperCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=4 , ) -> List[str]: snake_case_ : Dict = parent snake_case_ : List[Any] = batch_size snake_case_ : Union[str, Any] = seq_length snake_case_ : Tuple = is_training snake_case_ : List[str] = use_attention_mask snake_case_ : Union[str, Any] = use_token_type_ids snake_case_ : Optional[Any] = use_labels snake_case_ : Tuple = vocab_size snake_case_ : Dict = hidden_size snake_case_ : List[str] = num_hidden_layers snake_case_ : Tuple = num_attention_heads snake_case_ : Dict = intermediate_size snake_case_ : str = hidden_act snake_case_ : List[Any] = hidden_dropout_prob snake_case_ : Optional[int] = attention_probs_dropout_prob snake_case_ : Optional[Any] = max_position_embeddings snake_case_ : Optional[Any] = type_vocab_size snake_case_ : Union[str, Any] = type_sequence_label_size snake_case_ : str = initializer_range snake_case_ : List[Any] = num_choices def _lowerCAmelCase ( self ) -> Union[str, Any]: snake_case_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ : str = None if self.use_attention_mask: snake_case_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ : Any = None if self.use_token_type_ids: snake_case_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ : Tuple = BertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ) -> List[Any]: snake_case_ : int = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = config_and_inputs snake_case_ : Union[str, Any] = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict def _lowerCAmelCase ( self ) -> Optional[int]: snake_case_ : Union[str, Any] = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ , snake_case_ : List[Any] = config_and_inputs snake_case_ : str = True snake_case_ : Union[str, Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) snake_case_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, ) @require_flax class UpperCAmelCase_ ( SCREAMING_SNAKE_CASE__ , unittest.TestCase ): '''simple docstring''' A : List[str] = True A : List[str] = ( ( FlaxBertModel, FlaxBertForPreTraining, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForQuestionAnswering, FlaxBertForNextSentencePrediction, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ) -> List[str]: snake_case_ : List[str] = FlaxBertModelTester(self ) @slow def _lowerCAmelCase ( self ) -> Dict: # Only check this for base model, not necessary for all model classes. # This will also help speed-up tests. snake_case_ : int = FlaxBertModel.from_pretrained("bert-base-cased" ) snake_case_ : Tuple = model(np.ones((1, 1) ) ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE )
36
1
import unittest from huggingface_hub import hf_hub_download from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor from transformers.pipelines import VideoClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_decord, require_tf, require_torch, require_torch_or_tf, require_vision, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf @require_vision @require_decord class __snake_case ( unittest.TestCase ): _a : Optional[int]= MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ,snake_case ): '''simple docstring''' lowercase : Any = hf_hub_download( repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : List[str] = VideoClassificationPipeline(model=snake_case ,image_processor=snake_case ,top_k=2 ) lowercase : Dict = [ example_video_filepath, """https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4""", ] return video_classifier, examples def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case ): '''simple docstring''' for example in examples: lowercase : int = video_classifier(snake_case ) self.assertEqual( snake_case ,[ {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, {"""score""": ANY(snake_case ), """label""": ANY(snake_case )}, ] ,) @require_torch def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' lowercase : Any = """hf-internal-testing/tiny-random-VideoMAEForVideoClassification""" lowercase : str = VideoMAEFeatureExtractor( size={"""shortest_edge""": 10} ,crop_size={"""height""": 10, """width""": 10} ) lowercase : List[Any] = pipeline( """video-classification""" ,model=snake_case ,feature_extractor=snake_case ,frame_sampling_rate=4 ) lowercase : Dict = hf_hub_download(repo_id="""nateraw/video-demo""" ,filename="""archery.mp4""" ,repo_type="""dataset""" ) lowercase : Any = video_classifier(snake_case ,top_k=2 ) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}] ,) lowercase : str = video_classifier( [ video_file_path, video_file_path, ] ,top_k=2 ,) self.assertEqual( nested_simplify(snake_case ,decimals=4 ) ,[ [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], [{"""score""": 0.5_199, """label""": """LABEL_0"""}, {"""score""": 0.4_801, """label""": """LABEL_1"""}], ] ,) @require_tf def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' pass
20
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Any: assert isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Union[str, Any]: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : Optional[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowercase : Any = SqlDatasetReader( """dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ , keep_in_memory=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @require_sqlalchemy @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Tuple: lowercase : Union[str, Any] = tmp_path / """cache""" lowercase : int = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowercase : str = features.copy() if features else default_expected_features lowercase : Optional[Any] = ( Features({feature: Value(SCREAMING_SNAKE_CASE__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowercase : Optional[int] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , features=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ ).read() _check_sql_dataset(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _snake_case( SCREAMING_SNAKE_CASE__ ) -> List[Any]: with contextlib.closing(sqlitea.connect(SCREAMING_SNAKE_CASE__ ) ) as con: lowercase : Optional[int] = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase : Any = tmp_path / """cache""" lowercase : int = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=1 ).write() lowercase : List[str] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : Tuple = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Dict: lowercase : Dict = tmp_path / """cache""" lowercase : List[str] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=2 ).write() lowercase : Optional[int] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = iter_sql_file(SCREAMING_SNAKE_CASE__ ) for rowa, rowa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assert rowa == rowa @require_sqlalchemy def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> List[str]: lowercase : str = tmp_path / """cache""" lowercase : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE__ , """tmp.sql""" ) lowercase : Optional[Any] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=SCREAMING_SNAKE_CASE__ ).read() with pytest.raises(SCREAMING_SNAKE_CASE__ ): SqlDatasetWriter(SCREAMING_SNAKE_CASE__ , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=0 ).write()
20
1
"""simple docstring""" from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __magic_name__ = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" __lowercase : List[str] = ['''input_values''', '''padding_mask'''] def __init__( self , lowerCAmelCase__ = 1 , lowerCAmelCase__ = 2_4_0_0_0 , lowerCAmelCase__ = 0.0 , lowerCAmelCase__ = None , lowerCAmelCase__ = None , **lowerCAmelCase__ , ): super().__init__(feature_size=lowerCAmelCase__ , sampling_rate=lowerCAmelCase__ , padding_value=lowerCAmelCase__ , **lowerCAmelCase__) __SCREAMING_SNAKE_CASE = chunk_length_s __SCREAMING_SNAKE_CASE = overlap @property def snake_case_ ( self): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate) @property def snake_case_ ( self): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length)) def __call__( self , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = False , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , ): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of" f" {self.sampling_rate}. Please make sure that the provided audio input was sampled with" f" {self.sampling_rate} and not {sampling_rate}.") else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""") if padding and truncation: raise ValueError("""Both padding and truncation were set. Make sure you only set one.""") elif padding is None: # by default let's pad the inputs __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = bool( isinstance(lowerCAmelCase__ , (list, tuple)) and (isinstance(raw_audio[0] , (np.ndarray, tuple, list)))) if is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(lowerCAmelCase__ , dtype=np.floataa).T for audio in raw_audio] elif not is_batched and not isinstance(lowerCAmelCase__ , np.ndarray): __SCREAMING_SNAKE_CASE = np.asarray(lowerCAmelCase__ , dtype=np.floataa) elif isinstance(lowerCAmelCase__ , np.ndarray) and raw_audio.dtype is np.dtype(np.floataa): __SCREAMING_SNAKE_CASE = raw_audio.astype(np.floataa) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE = [np.asarray(lowerCAmelCase__).T] # verify inputs are valid for idx, example in enumerate(lowerCAmelCase__): if example.ndim > 2: raise ValueError(f"Expected input shape (channels, length) but got shape {example.shape}") if self.feature_size == 1 and example.ndim != 1: raise ValueError(f"Expected mono audio but example has {example.shape[-1]} channels") if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f"Expected stereo audio but example has {example.shape[-1]} channels") __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = BatchFeature({"""input_values""": raw_audio}) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: __SCREAMING_SNAKE_CASE = min(array.shape[0] for array in raw_audio) __SCREAMING_SNAKE_CASE = int(np.floor(max_length / self.chunk_stride)) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: __SCREAMING_SNAKE_CASE = max(array.shape[0] for array in raw_audio) __SCREAMING_SNAKE_CASE = int(np.ceil(max_length / self.chunk_stride)) __SCREAMING_SNAKE_CASE = (nb_step - 1) * self.chunk_stride + self.chunk_length __SCREAMING_SNAKE_CASE = """max_length""" else: __SCREAMING_SNAKE_CASE = input_values # normal padding on batch if padded_inputs is None: __SCREAMING_SNAKE_CASE = self.pad( lowerCAmelCase__ , max_length=lowerCAmelCase__ , truncation=lowerCAmelCase__ , padding=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , ) if padding: __SCREAMING_SNAKE_CASE = padded_inputs.pop("""attention_mask""") __SCREAMING_SNAKE_CASE = [] for example in padded_inputs.pop("""input_values"""): if self.feature_size == 1: __SCREAMING_SNAKE_CASE = example[..., None] input_values.append(example.T) __SCREAMING_SNAKE_CASE = input_values if return_tensors is not None: __SCREAMING_SNAKE_CASE = padded_inputs.convert_to_tensors(lowerCAmelCase__) return padded_inputs
255
"""simple docstring""" import os import tempfile import unittest from transformers import NezhaConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, ) from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST class SCREAMING_SNAKE_CASE_ : """simple docstring""" def __init__( self , lowerCAmelCase__ , lowerCAmelCase__=1_3 , lowerCAmelCase__=7 , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=9_9 , lowerCAmelCase__=3_2 , lowerCAmelCase__=5 , lowerCAmelCase__=4 , lowerCAmelCase__=3_7 , lowerCAmelCase__="gelu" , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.1 , lowerCAmelCase__=1_2_8 , lowerCAmelCase__=3_2 , lowerCAmelCase__=1_6 , lowerCAmelCase__=2 , lowerCAmelCase__=0.02 , lowerCAmelCase__=3 , lowerCAmelCase__=4 , lowerCAmelCase__=None , ): __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 = scope def snake_case_ ( self): __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 = random_attention_mask([self.batch_size, self.seq_length]) __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 snake_case_ ( self): return NezhaConfig( 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=lowerCAmelCase__ , initializer_range=self.initializer_range , ) def snake_case_ ( self): ( ( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) , ) = self.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.seq_length, self.hidden_size]) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = NezhaModel(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__) __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , token_type_ids=lowerCAmelCase__) __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ): __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = NezhaModel(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , ) __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = NezhaForMaskedLM(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = NezhaForNextSentencePrediction(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = NezhaForPreTraining(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ , next_sentence_label=lowerCAmelCase__ , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = NezhaForQuestionAnswering(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , start_positions=lowerCAmelCase__ , end_positions=lowerCAmelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = NezhaForSequenceClassification(lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = NezhaForTokenClassification(config=lowerCAmelCase__) model.to(lowerCAmelCase__) model.eval() __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__): __SCREAMING_SNAKE_CASE = self.num_choices __SCREAMING_SNAKE_CASE = NezhaForMultipleChoice(config=lowerCAmelCase__) model.to(lowerCAmelCase__) 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( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , token_type_ids=lowerCAmelCase__ , labels=lowerCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def snake_case_ ( self): __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 SCREAMING_SNAKE_CASE_ ( __a , __a , __a , unittest.TestCase ): """simple docstring""" __lowercase : int = ( ( NezhaModel, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, ) if is_torch_available() else () ) __lowercase : Optional[Any] = ( { '''feature-extraction''': NezhaModel, '''fill-mask''': NezhaForMaskedLM, '''question-answering''': NezhaForQuestionAnswering, '''text-classification''': NezhaForSequenceClassification, '''token-classification''': NezhaForTokenClassification, '''zero-shot''': NezhaForSequenceClassification, } if is_torch_available() else {} ) __lowercase : List[Any] = True def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=False): __SCREAMING_SNAKE_CASE = super()._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__) if return_labels: if model_class in get_values(lowerCAmelCase__): __SCREAMING_SNAKE_CASE = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=lowerCAmelCase__) __SCREAMING_SNAKE_CASE = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase__) return inputs_dict def snake_case_ ( self): __SCREAMING_SNAKE_CASE = NezhaModelTester(self) __SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=lowerCAmelCase__ , hidden_size=3_7) def snake_case_ ( self): self.config_tester.run_common_tests() def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*lowerCAmelCase__) def snake_case_ ( self): # This regression test was failing with PyTorch < 1.3 ( ( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) ,( __SCREAMING_SNAKE_CASE ) , ) = self.model_tester.prepare_config_and_inputs_for_decoder() __SCREAMING_SNAKE_CASE = None self.model_tester.create_and_check_model_as_decoder( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_next_sequence_prediction(*lowerCAmelCase__) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__) @slow def snake_case_ ( self): for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE = NezhaModel.from_pretrained(lowerCAmelCase__) self.assertIsNotNone(lowerCAmelCase__) @slow @require_torch_gpu def snake_case_ ( self): __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # NezhaForMultipleChoice behaves incorrectly in JIT environments. if model_class == NezhaForMultipleChoice: return __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = model_class(config=lowerCAmelCase__) __SCREAMING_SNAKE_CASE = self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__) __SCREAMING_SNAKE_CASE = torch.jit.trace( lowerCAmelCase__ , (inputs_dict["""input_ids"""].to("""cpu"""), inputs_dict["""attention_mask"""].to("""cpu"""))) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(lowerCAmelCase__ , os.path.join(lowerCAmelCase__ , """bert.pt""")) __SCREAMING_SNAKE_CASE = torch.jit.load(os.path.join(lowerCAmelCase__ , """bert.pt""") , map_location=lowerCAmelCase__) loaded(inputs_dict["""input_ids"""].to(lowerCAmelCase__) , inputs_dict["""attention_mask"""].to(lowerCAmelCase__)) @require_torch class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): """simple docstring""" @slow def snake_case_ ( self): __SCREAMING_SNAKE_CASE = NezhaModel.from_pretrained("""sijunhe/nezha-cn-base""") __SCREAMING_SNAKE_CASE = torch.tensor([[0, 1, 2, 3, 4, 5]]) __SCREAMING_SNAKE_CASE = torch.tensor([[0, 1, 1, 1, 1, 1]]) with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__)[0] __SCREAMING_SNAKE_CASE = torch.Size((1, 6, 7_6_8)) self.assertEqual(output.shape , lowerCAmelCase__) __SCREAMING_SNAKE_CASE = torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]]) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , lowerCAmelCase__ , atol=1E-4)) @slow def snake_case_ ( self): __SCREAMING_SNAKE_CASE = NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base""") __SCREAMING_SNAKE_CASE = torch.tensor([[0, 1, 2, 3, 4, 5]]) __SCREAMING_SNAKE_CASE = torch.tensor([[1, 1, 1, 1, 1, 1]]) with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__)[0] __SCREAMING_SNAKE_CASE = torch.Size((1, 6, 2_1_1_2_8)) self.assertEqual(output.shape , lowerCAmelCase__) __SCREAMING_SNAKE_CASE = torch.tensor( [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]]) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , lowerCAmelCase__ , atol=1E-4))
255
1
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets a : int = "\\n@inproceedings{wang2019glue,\n title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},\n author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},\n note={In the Proceedings of ICLR.},\n year={2019}\n}\n" a : Dict = "\\nGLUE, the General Language Understanding Evaluation benchmark\n(https://gluebenchmark.com/) is a collection of resources for training,\nevaluating, and analyzing natural language understanding systems.\n" a : int = "\nCompute GLUE evaluation metric associated to each GLUE dataset.\nArgs:\n predictions: list of predictions to score.\n Each translation should be tokenized into a list of tokens.\n references: list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\nReturns: depending on the GLUE subset, one or several of:\n \"accuracy\": Accuracy\n \"f1\": F1 score\n \"pearson\": Pearson Correlation\n \"spearmanr\": Spearman Correlation\n \"matthews_correlation\": Matthew Correlation\nExamples:\n\n >>> glue_metric = datasets.load_metric('glue', 'sst2') # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'mrpc') # 'mrpc' or 'qqp'\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0, 'f1': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'stsb')\n >>> references = [0., 1., 2., 3., 4., 5.]\n >>> predictions = [0., 1., 2., 3., 4., 5.]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})\n {'pearson': 1.0, 'spearmanr': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'cola')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'matthews_correlation': 1.0}\n" def lowerCamelCase__ ( __lowerCamelCase : str , __lowerCamelCase : List[Any] ): return float((preds == labels).mean() ) def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : List[Any] ): __UpperCAmelCase : Any = simple_accuracy(__lowerCamelCase , __lowerCamelCase ) __UpperCAmelCase : Union[str, Any] = float(fa_score(y_true=__lowerCamelCase , y_pred=__lowerCamelCase ) ) return { "accuracy": acc, "f1": fa, } def lowerCamelCase__ ( __lowerCamelCase : List[Any] , __lowerCamelCase : Union[str, Any] ): __UpperCAmelCase : Dict = float(pearsonr(__lowerCamelCase , __lowerCamelCase )[0] ) __UpperCAmelCase : int = float(spearmanr(__lowerCamelCase , __lowerCamelCase )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a ( datasets.Metric ): """simple docstring""" def UpperCAmelCase ( self : Dict ) -> Any: if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), """references""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), } ) , codebase_urls=[] , reference_urls=[] , format="""numpy""" , ) def UpperCAmelCase ( self : List[str] , __lowercase : int , __lowercase : Union[str, Any] ) -> int: if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(__lowercase , __lowercase )} elif self.config_name == "stsb": return pearson_and_spearman(__lowercase , __lowercase ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(__lowercase , __lowercase ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(__lowercase , __lowercase )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )
114
import os import tempfile import unittest from transformers import NezhaConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, ) from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST class a : """simple docstring""" def __init__( self : List[Any] , __lowercase : List[Any] , __lowercase : Any=13 , __lowercase : str=7 , __lowercase : Union[str, Any]=True , __lowercase : Any=True , __lowercase : int=True , __lowercase : Optional[int]=True , __lowercase : List[str]=99 , __lowercase : str=32 , __lowercase : Dict=5 , __lowercase : List[str]=4 , __lowercase : Dict=37 , __lowercase : Optional[int]="gelu" , __lowercase : int=0.1 , __lowercase : List[Any]=0.1 , __lowercase : Tuple=128 , __lowercase : Union[str, Any]=32 , __lowercase : str=16 , __lowercase : List[str]=2 , __lowercase : Optional[int]=0.02 , __lowercase : Any=3 , __lowercase : Any=4 , __lowercase : Optional[Any]=None , ) -> Any: __UpperCAmelCase : Optional[Any] = parent __UpperCAmelCase : Optional[int] = batch_size __UpperCAmelCase : int = seq_length __UpperCAmelCase : Optional[Any] = is_training __UpperCAmelCase : int = use_input_mask __UpperCAmelCase : Tuple = use_token_type_ids __UpperCAmelCase : List[str] = use_labels __UpperCAmelCase : List[str] = vocab_size __UpperCAmelCase : Any = hidden_size __UpperCAmelCase : Any = num_hidden_layers __UpperCAmelCase : List[Any] = num_attention_heads __UpperCAmelCase : List[str] = intermediate_size __UpperCAmelCase : Tuple = hidden_act __UpperCAmelCase : Tuple = hidden_dropout_prob __UpperCAmelCase : Tuple = attention_probs_dropout_prob __UpperCAmelCase : List[Any] = max_position_embeddings __UpperCAmelCase : Dict = type_vocab_size __UpperCAmelCase : int = type_sequence_label_size __UpperCAmelCase : Union[str, Any] = initializer_range __UpperCAmelCase : List[Any] = num_labels __UpperCAmelCase : Optional[int] = num_choices __UpperCAmelCase : Any = scope def UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: __UpperCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __UpperCAmelCase : Any = None if self.use_input_mask: __UpperCAmelCase : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) __UpperCAmelCase : Any = None if self.use_token_type_ids: __UpperCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __UpperCAmelCase : Optional[Any] = None __UpperCAmelCase : Dict = None __UpperCAmelCase : List[str] = None if self.use_labels: __UpperCAmelCase : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __UpperCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __UpperCAmelCase : int = ids_tensor([self.batch_size] , self.num_choices ) __UpperCAmelCase : str = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase ( self : str ) -> str: return NezhaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowercase , initializer_range=self.initializer_range , ) def UpperCAmelCase ( self : int ) -> Optional[Any]: ( ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ) : List[str] = self.prepare_config_and_inputs() __UpperCAmelCase : str = True __UpperCAmelCase : Union[str, Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __UpperCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def UpperCAmelCase ( self : str , __lowercase : Union[str, Any] , __lowercase : str , __lowercase : Optional[int] , __lowercase : Dict , __lowercase : Union[str, Any] , __lowercase : str , __lowercase : int ) -> Any: __UpperCAmelCase : Union[str, Any] = NezhaModel(config=__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : int = model(__lowercase , attention_mask=__lowercase , token_type_ids=__lowercase ) __UpperCAmelCase : Optional[Any] = model(__lowercase , token_type_ids=__lowercase ) __UpperCAmelCase : List[Any] = model(__lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase ( self : Optional[int] , __lowercase : Union[str, Any] , __lowercase : Optional[int] , __lowercase : Dict , __lowercase : Optional[int] , __lowercase : Any , __lowercase : str , __lowercase : Tuple , __lowercase : Optional[Any] , __lowercase : Tuple , ) -> Optional[int]: __UpperCAmelCase : List[Any] = True __UpperCAmelCase : Optional[Any] = NezhaModel(__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : List[Any] = model( __lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , encoder_hidden_states=__lowercase , encoder_attention_mask=__lowercase , ) __UpperCAmelCase : Optional[Any] = model( __lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , encoder_hidden_states=__lowercase , ) __UpperCAmelCase : List[str] = model(__lowercase , attention_mask=__lowercase , token_type_ids=__lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def UpperCAmelCase ( self : Any , __lowercase : int , __lowercase : str , __lowercase : Dict , __lowercase : Union[str, Any] , __lowercase : Tuple , __lowercase : Optional[int] , __lowercase : Optional[int] ) -> Optional[int]: __UpperCAmelCase : Optional[int] = NezhaForMaskedLM(config=__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : Optional[int] = model(__lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , labels=__lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase ( self : Dict , __lowercase : str , __lowercase : Optional[Any] , __lowercase : int , __lowercase : List[str] , __lowercase : Dict , __lowercase : str , __lowercase : str ) -> Dict: __UpperCAmelCase : Optional[int] = NezhaForNextSentencePrediction(config=__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : Union[str, Any] = model( __lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , labels=__lowercase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def UpperCAmelCase ( self : Tuple , __lowercase : Any , __lowercase : Dict , __lowercase : Optional[int] , __lowercase : Tuple , __lowercase : Optional[int] , __lowercase : Optional[int] , __lowercase : List[str] ) -> int: __UpperCAmelCase : Optional[Any] = NezhaForPreTraining(config=__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : int = model( __lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , labels=__lowercase , next_sentence_label=__lowercase , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def UpperCAmelCase ( self : Tuple , __lowercase : List[str] , __lowercase : Any , __lowercase : Tuple , __lowercase : Optional[Any] , __lowercase : Dict , __lowercase : List[Any] , __lowercase : Dict ) -> List[Any]: __UpperCAmelCase : Optional[Any] = NezhaForQuestionAnswering(config=__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : Any = model( __lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , start_positions=__lowercase , end_positions=__lowercase , ) 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 UpperCAmelCase ( self : Tuple , __lowercase : Dict , __lowercase : Any , __lowercase : Any , __lowercase : Union[str, Any] , __lowercase : int , __lowercase : Dict , __lowercase : List[Any] ) -> Optional[Any]: __UpperCAmelCase : Optional[int] = self.num_labels __UpperCAmelCase : Union[str, Any] = NezhaForSequenceClassification(__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : Optional[Any] = model(__lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , labels=__lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase ( self : Tuple , __lowercase : List[Any] , __lowercase : Union[str, Any] , __lowercase : str , __lowercase : List[Any] , __lowercase : List[str] , __lowercase : Dict , __lowercase : Dict ) -> str: __UpperCAmelCase : Union[str, Any] = self.num_labels __UpperCAmelCase : Dict = NezhaForTokenClassification(config=__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : Dict = model(__lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , labels=__lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase ( self : Tuple , __lowercase : Any , __lowercase : List[Any] , __lowercase : Dict , __lowercase : Tuple , __lowercase : List[Any] , __lowercase : Any , __lowercase : int ) -> Optional[int]: __UpperCAmelCase : List[str] = self.num_choices __UpperCAmelCase : int = NezhaForMultipleChoice(config=__lowercase ) model.to(__lowercase ) model.eval() __UpperCAmelCase : List[str] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __UpperCAmelCase : Tuple = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __UpperCAmelCase : int = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __UpperCAmelCase : Tuple = model( __lowercase , attention_mask=__lowercase , token_type_ids=__lowercase , labels=__lowercase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCAmelCase ( self : Dict ) -> List[Any]: __UpperCAmelCase : Optional[int] = self.prepare_config_and_inputs() ( ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ) : Tuple = config_and_inputs __UpperCAmelCase : Optional[Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class a ( lowercase__ , lowercase__ , lowercase__ , unittest.TestCase ): """simple docstring""" a : Union[str, Any] = ( ( NezhaModel, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, ) if is_torch_available() else () ) a : Tuple = ( { 'feature-extraction': NezhaModel, 'fill-mask': NezhaForMaskedLM, 'question-answering': NezhaForQuestionAnswering, 'text-classification': NezhaForSequenceClassification, 'token-classification': NezhaForTokenClassification, 'zero-shot': NezhaForSequenceClassification, } if is_torch_available() else {} ) a : Dict = True def UpperCAmelCase ( self : Optional[int] , __lowercase : str , __lowercase : Dict , __lowercase : int=False ) -> Dict: __UpperCAmelCase : Optional[Any] = super()._prepare_for_class(__lowercase , __lowercase , return_labels=__lowercase ) if return_labels: if model_class in get_values(__lowercase ): __UpperCAmelCase : int = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=__lowercase ) __UpperCAmelCase : int = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__lowercase ) return inputs_dict def UpperCAmelCase ( self : Any ) -> int: __UpperCAmelCase : Tuple = NezhaModelTester(self ) __UpperCAmelCase : List[Any] = ConfigTester(self , config_class=__lowercase , hidden_size=37 ) def UpperCAmelCase ( self : Dict ) -> List[str]: self.config_tester.run_common_tests() def UpperCAmelCase ( self : str ) -> List[Any]: __UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowercase ) def UpperCAmelCase ( self : Optional[Any] ) -> Tuple: __UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*__lowercase ) def UpperCAmelCase ( self : int ) -> str: # This regression test was failing with PyTorch < 1.3 ( ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ( __UpperCAmelCase ) , ) : Tuple = self.model_tester.prepare_config_and_inputs_for_decoder() __UpperCAmelCase : int = None self.model_tester.create_and_check_model_as_decoder( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) def UpperCAmelCase ( self : Dict ) -> Union[str, Any]: __UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__lowercase ) def UpperCAmelCase ( self : int ) -> Dict: __UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__lowercase ) def UpperCAmelCase ( self : List[str] ) -> List[Any]: __UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_next_sequence_prediction(*__lowercase ) def UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: __UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*__lowercase ) def UpperCAmelCase ( self : int ) -> Dict: __UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__lowercase ) def UpperCAmelCase ( self : List[str] ) -> str: __UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__lowercase ) def UpperCAmelCase ( self : Optional[Any] ) -> str: __UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__lowercase ) @slow def UpperCAmelCase ( self : str ) -> Any: for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCAmelCase : int = NezhaModel.from_pretrained(__lowercase ) self.assertIsNotNone(__lowercase ) @slow @require_torch_gpu def UpperCAmelCase ( self : Union[str, Any] ) -> List[Any]: __UpperCAmelCase , __UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # NezhaForMultipleChoice behaves incorrectly in JIT environments. if model_class == NezhaForMultipleChoice: return __UpperCAmelCase : Optional[Any] = True __UpperCAmelCase : Dict = model_class(config=__lowercase ) __UpperCAmelCase : Union[str, Any] = self._prepare_for_class(__lowercase , __lowercase ) __UpperCAmelCase : Union[str, Any] = torch.jit.trace( __lowercase , (inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(__lowercase , os.path.join(__lowercase , """bert.pt""" ) ) __UpperCAmelCase : Optional[int] = torch.jit.load(os.path.join(__lowercase , """bert.pt""" ) , map_location=__lowercase ) loaded(inputs_dict["""input_ids"""].to(__lowercase ) , inputs_dict["""attention_mask"""].to(__lowercase ) ) @require_torch class a ( unittest.TestCase ): """simple docstring""" @slow def UpperCAmelCase ( self : Any ) -> Optional[Any]: __UpperCAmelCase : Tuple = NezhaModel.from_pretrained("""sijunhe/nezha-cn-base""" ) __UpperCAmelCase : List[Any] = torch.tensor([[0, 1, 2, 3, 4, 5]] ) __UpperCAmelCase : Any = torch.tensor([[0, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __UpperCAmelCase : Optional[Any] = model(__lowercase , attention_mask=__lowercase )[0] __UpperCAmelCase : Dict = torch.Size((1, 6, 768) ) self.assertEqual(output.shape , __lowercase ) __UpperCAmelCase : Any = torch.tensor([[[0.0_685, 0.2_441, 0.1_102], [0.0_600, 0.1_906, 0.1_349], [0.0_221, 0.0_819, 0.0_586]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , __lowercase , atol=1e-4 ) ) @slow def UpperCAmelCase ( self : str ) -> List[str]: __UpperCAmelCase : int = NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base""" ) __UpperCAmelCase : Tuple = torch.tensor([[0, 1, 2, 3, 4, 5]] ) __UpperCAmelCase : Any = torch.tensor([[1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __UpperCAmelCase : Dict = model(__lowercase , attention_mask=__lowercase )[0] __UpperCAmelCase : Union[str, Any] = torch.Size((1, 6, 21128) ) self.assertEqual(output.shape , __lowercase ) __UpperCAmelCase : int = torch.tensor( [[-2.7_939, -1.7_902, -2.2_189], [-2.8_585, -1.8_908, -2.3_723], [-2.6_499, -1.7_750, -2.2_558]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , __lowercase , atol=1e-4 ) )
114
1
'''simple docstring''' import json import os import unittest from transformers import BatchEncoding, MvpTokenizer, MvpTokenizerFast from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin, filter_roberta_detectors @require_tokenizers class lowerCAmelCase__ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" lowerCAmelCase__ = MvpTokenizer lowerCAmelCase__ = MvpTokenizerFast lowerCAmelCase__ = True lowerCAmelCase__ = filter_roberta_detectors def UpperCAmelCase__ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" super().setUp() __SCREAMING_SNAKE_CASE = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] __SCREAMING_SNAKE_CASE = dict(zip(_a , range(len(_a ) ) ) ) __SCREAMING_SNAKE_CASE = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] __SCREAMING_SNAKE_CASE = {"""unk_token""": """<unk>"""} __SCREAMING_SNAKE_CASE = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) __SCREAMING_SNAKE_CASE = 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(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def UpperCAmelCase__ ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[str] ) -> Union[str, Any]: """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_a ) def UpperCAmelCase__ ( self : Any , **__SCREAMING_SNAKE_CASE : List[str] ) -> Union[str, Any]: """simple docstring""" kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **_a ) def UpperCAmelCase__ ( self : Tuple , __SCREAMING_SNAKE_CASE : Tuple ) -> Dict: """simple docstring""" return "lower newer", "lower newer" @cached_property def UpperCAmelCase__ ( self : str ) -> Optional[Any]: """simple docstring""" return MvpTokenizer.from_pretrained("""RUCAIBox/mvp""" ) @cached_property def UpperCAmelCase__ ( self : int ) -> List[str]: """simple docstring""" return MvpTokenizerFast.from_pretrained("""RUCAIBox/mvp""" ) @require_torch def UpperCAmelCase__ ( self : List[str] ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] __SCREAMING_SNAKE_CASE = [0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __SCREAMING_SNAKE_CASE = tokenizer(_a , max_length=len(_a ) , padding=_a , return_tensors="""pt""" ) self.assertIsInstance(_a , _a ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) __SCREAMING_SNAKE_CASE = batch.input_ids.tolist()[0] self.assertListEqual(_a , _a ) # Test that special tokens are reset @require_torch def UpperCAmelCase__ ( self : Any ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __SCREAMING_SNAKE_CASE = tokenizer(_a , padding=_a , return_tensors="""pt""" ) # check if input_ids are returned and no labels self.assertIn("""input_ids""" , _a ) self.assertIn("""attention_mask""" , _a ) self.assertNotIn("""labels""" , _a ) self.assertNotIn("""decoder_attention_mask""" , _a ) @require_torch def UpperCAmelCase__ ( self : Any ) -> Any: """simple docstring""" __SCREAMING_SNAKE_CASE = [ """Summary of the text.""", """Another summary.""", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __SCREAMING_SNAKE_CASE = tokenizer(text_target=_a , max_length=32 , padding="""max_length""" , return_tensors="""pt""" ) self.assertEqual(32 , targets["""input_ids"""].shape[1] ) @require_torch def UpperCAmelCase__ ( self : int ) -> str: """simple docstring""" for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __SCREAMING_SNAKE_CASE = tokenizer( ["""I am a small frog""" * 1_024, """I am a small frog"""] , padding=_a , truncation=_a , return_tensors="""pt""" ) self.assertIsInstance(_a , _a ) self.assertEqual(batch.input_ids.shape , (2, 1_024) ) @require_torch def UpperCAmelCase__ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = ["""A long paragraph for summarization."""] __SCREAMING_SNAKE_CASE = [ """Summary of the text.""", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __SCREAMING_SNAKE_CASE = tokenizer(_a , text_target=_a , return_tensors="""pt""" ) __SCREAMING_SNAKE_CASE = inputs["""input_ids"""] __SCREAMING_SNAKE_CASE = inputs["""labels"""] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) def UpperCAmelCase__ ( self : List[Any] ) -> Any: """simple docstring""" pass def UpperCAmelCase__ ( self : str ) -> int: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): __SCREAMING_SNAKE_CASE = self.rust_tokenizer_class.from_pretrained(_a , **_a ) __SCREAMING_SNAKE_CASE = self.tokenizer_class.from_pretrained(_a , **_a ) __SCREAMING_SNAKE_CASE = """A, <mask> AllenNLP sentence.""" __SCREAMING_SNAKE_CASE = tokenizer_r.encode_plus(_a , add_special_tokens=_a , return_token_type_ids=_a ) __SCREAMING_SNAKE_CASE = tokenizer_p.encode_plus(_a , add_special_tokens=_a , return_token_type_ids=_a ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["""token_type_ids"""] ) , sum(tokens_p["""token_type_ids"""] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["""attention_mask"""] ) / len(tokens_r["""attention_mask"""] ) , sum(tokens_p["""attention_mask"""] ) / len(tokens_p["""attention_mask"""] ) , ) __SCREAMING_SNAKE_CASE = tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""] ) __SCREAMING_SNAKE_CASE = tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p["""input_ids"""] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual(tokens_r["""input_ids"""] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual( _a , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) self.assertSequenceEqual( _a , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] )
361
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging UpperCAmelCase : List[Any] = logging.get_logger(__name__) # TODO: upload to AWS UpperCAmelCase : Optional[int] = { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json' ), } class lowerCAmelCase__ ( a ): """simple docstring""" lowerCAmelCase__ = "retribert" def __init__( self : int , __SCREAMING_SNAKE_CASE : str=30_522 , __SCREAMING_SNAKE_CASE : int=768 , __SCREAMING_SNAKE_CASE : Any=8 , __SCREAMING_SNAKE_CASE : List[str]=12 , __SCREAMING_SNAKE_CASE : List[str]=3_072 , __SCREAMING_SNAKE_CASE : int="gelu" , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : Dict=512 , __SCREAMING_SNAKE_CASE : int=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : List[str]=1E-12 , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Any=128 , __SCREAMING_SNAKE_CASE : Tuple=0 , **__SCREAMING_SNAKE_CASE : Tuple , ) -> Any: """simple docstring""" super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __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 = 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 = share_encoders __SCREAMING_SNAKE_CASE = projection_dim
331
0
import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _lowercase ( lowerCAmelCase, unittest.TestCase ): """simple docstring""" __A = KandinskyVaaPriorPipeline __A = ["prompt"] __A = ["prompt", "negative_prompt"] __A = [ "num_images_per_prompt", "generator", "num_inference_steps", "latents", "negative_prompt", "guidance_scale", "output_type", "return_dict", ] __A = False @property def UpperCamelCase_ (self ): """simple docstring""" return 32 @property def UpperCamelCase_ (self ): """simple docstring""" return 32 @property def UpperCamelCase_ (self ): """simple docstring""" return self.time_input_dim @property def UpperCamelCase_ (self ): """simple docstring""" return self.time_input_dim * 4 @property def UpperCamelCase_ (self ): """simple docstring""" return 100 @property def UpperCamelCase_ (self ): """simple docstring""" a = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) return tokenizer @property def UpperCamelCase_ (self ): """simple docstring""" torch.manual_seed(0 ) a = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(lowerCamelCase_ ) @property def UpperCamelCase_ (self ): """simple docstring""" torch.manual_seed(0 ) a = { "num_attention_heads": 2, "attention_head_dim": 12, "embedding_dim": self.text_embedder_hidden_size, "num_layers": 1, } a = PriorTransformer(**lowerCamelCase_ ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 a = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def UpperCamelCase_ (self ): """simple docstring""" torch.manual_seed(0 ) a = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) a = CLIPVisionModelWithProjection(lowerCamelCase_ ) return model @property def UpperCamelCase_ (self ): """simple docstring""" a = CLIPImageProcessor( crop_size=224 , do_center_crop=lowerCamelCase_ , do_normalize=lowerCamelCase_ , do_resize=lowerCamelCase_ , image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073] , image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711] , resample=3 , size=224 , ) return image_processor def UpperCamelCase_ (self ): """simple docstring""" a = self.dummy_prior a = self.dummy_image_encoder a = self.dummy_text_encoder a = self.dummy_tokenizer a = self.dummy_image_processor a = UnCLIPScheduler( variance_type="fixed_small_log" , prediction_type="sample" , num_train_timesteps=1000 , clip_sample=lowerCamelCase_ , clip_sample_range=10.0 , ) a = { "prior": prior, "image_encoder": image_encoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "scheduler": scheduler, "image_processor": image_processor, } return components def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_=0 ): """simple docstring""" if str(lowerCamelCase_ ).startswith("mps" ): a = torch.manual_seed(lowerCamelCase_ ) else: a = torch.Generator(device=lowerCamelCase_ ).manual_seed(lowerCamelCase_ ) a = { "prompt": "horse", "generator": generator, "guidance_scale": 4.0, "num_inference_steps": 2, "output_type": "np", } return inputs def UpperCamelCase_ (self ): """simple docstring""" a = "cpu" a = self.get_dummy_components() a = self.pipeline_class(**lowerCamelCase_ ) a = pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) a = pipe(**self.get_dummy_inputs(lowerCamelCase_ ) ) a = output.image_embeds a = pipe( **self.get_dummy_inputs(lowerCamelCase_ ) , return_dict=lowerCamelCase_ , )[0] a = image[0, -10:] a = image_from_tuple[0, -10:] assert image.shape == (1, 32) a = np.array( [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def UpperCamelCase_ (self ): """simple docstring""" a = torch_device == "cpu" a = True a = False self._test_inference_batch_single_identical( test_max_difference=lowerCamelCase_ , relax_max_difference=lowerCamelCase_ , test_mean_pixel_difference=lowerCamelCase_ , ) @skip_mps def UpperCamelCase_ (self ): """simple docstring""" a = torch_device == "cpu" a = False self._test_attention_slicing_forward_pass( test_max_difference=lowerCamelCase_ , test_mean_pixel_difference=lowerCamelCase_ , )
227
from collections import defaultdict from typing import Optional from ..image_utils import load_image from ..utils import ( add_end_docstrings, is_torch_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING _lowercase: Any = logging.get_logger(__name__) @add_end_docstrings(lowerCAmelCase ) class _lowercase ( lowerCAmelCase ): """simple docstring""" def __init__(self , **lowerCamelCase_ ): """simple docstring""" super().__init__(**lowerCamelCase_ ) requires_backends(self , "vision" ) requires_backends(self , "torch" ) if self.framework != "pt": raise ValueError(F'''The {self.__class__} is only available in PyTorch.''' ) self.check_model_type(lowerCamelCase_ ) def UpperCamelCase_ (self , **lowerCamelCase_ ): """simple docstring""" a = {} a = {} a = {} # preprocess args if "points_per_batch" in kwargs: a = kwargs["points_per_batch"] if "points_per_crop" in kwargs: a = kwargs["points_per_crop"] if "crops_n_layers" in kwargs: a = kwargs["crops_n_layers"] if "crop_overlap_ratio" in kwargs: a = kwargs["crop_overlap_ratio"] if "crop_n_points_downscale_factor" in kwargs: a = kwargs["crop_n_points_downscale_factor"] # postprocess args if "pred_iou_thresh" in kwargs: a = kwargs["pred_iou_thresh"] if "stability_score_offset" in kwargs: a = kwargs["stability_score_offset"] if "mask_threshold" in kwargs: a = kwargs["mask_threshold"] if "stability_score_thresh" in kwargs: a = kwargs["stability_score_thresh"] if "crops_nms_thresh" in kwargs: a = kwargs["crops_nms_thresh"] if "output_rle_mask" in kwargs: a = kwargs["output_rle_mask"] if "output_bboxes_mask" in kwargs: a = kwargs["output_bboxes_mask"] return preprocess_kwargs, forward_params, postprocess_kwargs def __call__(self , lowerCamelCase_ , *lowerCamelCase_ , lowerCamelCase_=None , lowerCamelCase_=None , **lowerCamelCase_ ): """simple docstring""" return super().__call__(lowerCamelCase_ , *lowerCamelCase_ , num_workers=lowerCamelCase_ , batch_size=lowerCamelCase_ , **lowerCamelCase_ ) def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_=64 , lowerCamelCase_ = 0 , lowerCamelCase_ = 512 / 1500 , lowerCamelCase_ = 32 , lowerCamelCase_ = 1 , ): """simple docstring""" a = load_image(lowerCamelCase_ ) a = self.image_processor.size["longest_edge"] a , a , a , a = self.image_processor.generate_crop_boxes( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) a = self.image_processor(images=lowerCamelCase_ , return_tensors="pt" ) with self.device_placement(): if self.framework == "pt": a = self.get_inference_context() with inference_context(): a = self._ensure_tensor_on_device(lowerCamelCase_ , device=self.device ) a = self.model.get_image_embeddings(model_inputs.pop("pixel_values" ) ) a = image_embeddings a = grid_points.shape[1] a = points_per_batch if points_per_batch is not None else n_points if points_per_batch <= 0: raise ValueError( "Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. " "To return all points at once, set points_per_batch to None" ) for i in range(0 , lowerCamelCase_ , lowerCamelCase_ ): a = grid_points[:, i : i + points_per_batch, :, :] a = input_labels[:, i : i + points_per_batch] a = i == n_points - points_per_batch yield { "input_points": batched_points, "input_labels": labels, "input_boxes": crop_boxes, "is_last": is_last, **model_inputs, } def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_=0.88 , lowerCamelCase_=0.95 , lowerCamelCase_=0 , lowerCamelCase_=1 , ): """simple docstring""" a = model_inputs.pop("input_boxes" ) a = model_inputs.pop("is_last" ) a = model_inputs.pop("original_sizes" ).tolist() a = model_inputs.pop("reshaped_input_sizes" ).tolist() a = self.model(**lowerCamelCase_ ) # post processing happens here in order to avoid CPU GPU copies of ALL the masks a = model_outputs["pred_masks"] a = self.image_processor.post_process_masks( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , binarize=lowerCamelCase_ ) a = model_outputs["iou_scores"] a , a , a = self.image_processor.filter_masks( masks[0] , iou_scores[0] , original_sizes[0] , input_boxes[0] , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) return { "masks": masks, "is_last": is_last, "boxes": boxes, "iou_scores": iou_scores, } def UpperCamelCase_ (self , lowerCamelCase_ , lowerCamelCase_=False , lowerCamelCase_=False , lowerCamelCase_=0.7 , ): """simple docstring""" a = [] a = [] a = [] for model_output in model_outputs: all_scores.append(model_output.pop("iou_scores" ) ) all_masks.extend(model_output.pop("masks" ) ) all_boxes.append(model_output.pop("boxes" ) ) a = torch.cat(lowerCamelCase_ ) a = torch.cat(lowerCamelCase_ ) a , a , a , a = self.image_processor.post_process_for_mask_generation( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) a = defaultdict(lowerCamelCase_ ) for output in model_outputs: for k, v in output.items(): extra[k].append(lowerCamelCase_ ) a = {} if output_rle_mask: a = rle_mask if output_bboxes_mask: a = bounding_boxes return {"masks": output_masks, "scores": iou_scores, **optional, **extra}
227
1
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def UpperCamelCase__( UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict )->List[str]: A__ = k_size // 2 A__ , A__ = mgrid[0 - center : k_size - center, 0 - center : k_size - center] A__ = 1 / (2 * pi * sigma) * exp(-(square(UpperCAmelCase__ ) + square(UpperCAmelCase__ )) / (2 * square(UpperCAmelCase__ )) ) return g def UpperCamelCase__( UpperCamelCase__ : str , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] )->Optional[Any]: A__ , A__ = image.shape[0], image.shape[1] # dst image height and width A__ = height - k_size + 1 A__ = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows A__ = zeros((dst_height * dst_width, k_size * k_size) ) A__ = 0 for i, j in product(range(UpperCAmelCase__ ) , range(UpperCAmelCase__ ) ): A__ = ravel(image[i : i + k_size, j : j + k_size] ) A__ = window row += 1 # turn the kernel into shape(k*k, 1) A__ = gen_gaussian_kernel(UpperCAmelCase__ , UpperCAmelCase__ ) A__ = ravel(UpperCAmelCase__ ) # reshape and get the dst image A__ = dot(UpperCAmelCase__ , UpperCAmelCase__ ).reshape(UpperCAmelCase__ , UpperCAmelCase__ ).astype(UpperCAmelCase__ ) return dst if __name__ == "__main__": # read original image a__: Dict = imread(r'../image_data/lena.jpg') # turn image in gray scale value a__: Dict = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size a__: Union[str, Any] = gaussian_filter(gray, 3, sigma=1) a__: Union[str, Any] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow('gaussian filter with 3x3 mask', gaussianaxa) imshow('gaussian filter with 5x5 mask', gaussianaxa) waitKey()
362
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class SCREAMING_SNAKE_CASE__ ( UpperCamelCase__ ): __SCREAMING_SNAKE_CASE = ( '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.''' '''It takes two arguments named `image` which should be the original image, and `label` which should be a text ''' '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.''' ) __SCREAMING_SNAKE_CASE = '''CIDAS/clipseg-rd64-refined''' __SCREAMING_SNAKE_CASE = '''image_segmenter''' __SCREAMING_SNAKE_CASE = CLIPSegForImageSegmentation __SCREAMING_SNAKE_CASE = ['''image''', '''text'''] __SCREAMING_SNAKE_CASE = ['''image'''] def __init__( self,*__lowerCamelCase,**__lowerCamelCase ): requires_backends(self,['''vision'''] ) super().__init__(*__lowerCamelCase,**__lowerCamelCase ) def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase ): return self.pre_processor(text=[label],images=[image],padding=__lowerCamelCase,return_tensors='''pt''' ) def UpperCamelCase ( self,__lowerCamelCase ): with torch.no_grad(): A__ = self.model(**__lowerCamelCase ).logits return logits def UpperCamelCase ( self,__lowerCamelCase ): A__ = outputs.cpu().detach().numpy() A__ = 0 A__ = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
39
0
import argparse import hashlib # hashlib is only used inside the Test class import struct class UpperCAmelCase_ : def __init__( self, __a): '''simple docstring''' _lowerCAmelCase : Any = data _lowerCAmelCase : Tuple = [0X67_45_23_01, 0XEF_CD_AB_89, 0X98_BA_DC_FE, 0X10_32_54_76, 0XC3_D2_E1_F0] @staticmethod def snake_case__ ( __a, __a): '''simple docstring''' return ((n << b) | (n >> (32 - b))) & 0XFF_FF_FF_FF def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Optional[Any] = B"\x80" + B"\x00" * (63 - (len(self.data) + 8) % 64) _lowerCAmelCase : int = self.data + padding + struct.pack(">Q", 8 * len(self.data)) return padded_data def snake_case__ ( self): '''simple docstring''' return [ self.padded_data[i : i + 64] for i in range(0, len(self.padded_data), 64) ] def snake_case__ ( self, __a): '''simple docstring''' _lowerCAmelCase : Optional[Any] = list(struct.unpack(">16L", __a)) + [0] * 64 for i in range(16, 80): _lowerCAmelCase : Dict = self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1) return w def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Optional[int] = self.padding() _lowerCAmelCase : int = self.split_blocks() for block in self.blocks: _lowerCAmelCase : str = self.expand_block(__a) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = self.h for i in range(0, 80): if 0 <= i < 20: _lowerCAmelCase : int = (b & c) | ((~b) & d) _lowerCAmelCase : str = 0X5A_82_79_99 elif 20 <= i < 40: _lowerCAmelCase : Optional[Any] = b ^ c ^ d _lowerCAmelCase : Optional[Any] = 0X6E_D9_EB_A1 elif 40 <= i < 60: _lowerCAmelCase : List[str] = (b & c) | (b & d) | (c & d) _lowerCAmelCase : Union[str, Any] = 0X8F_1B_BC_DC elif 60 <= i < 80: _lowerCAmelCase : Optional[int] = b ^ c ^ d _lowerCAmelCase : str = 0XCA_62_C1_D6 _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Dict = ( self.rotate(__a, 5) + f + e + k + expanded_block[i] & 0XFF_FF_FF_FF, a, self.rotate(__a, 30), c, d, ) _lowerCAmelCase : str = ( self.h[0] + a & 0XFF_FF_FF_FF, self.h[1] + b & 0XFF_FF_FF_FF, self.h[2] + c & 0XFF_FF_FF_FF, self.h[3] + d & 0XFF_FF_FF_FF, self.h[4] + e & 0XFF_FF_FF_FF, ) return ("{:08x}" * 5).format(*self.h) def A ( ): '''simple docstring''' _lowerCAmelCase : Any = b"Test String" assert SHAaHash(_lowerCamelCase ).final_hash() == hashlib.shaa(_lowerCamelCase ).hexdigest() # noqa: S324 def A ( ): '''simple docstring''' _lowerCAmelCase : List[str] = argparse.ArgumentParser(description="Process some strings or files" ) parser.add_argument( "--string" , dest="input_string" , default="Hello World!! Welcome to Cryptography" , help="Hash the string" , ) parser.add_argument("--file" , dest="input_file" , help="Hash contents of a file" ) _lowerCAmelCase : Union[str, Any] = parser.parse_args() _lowerCAmelCase : Tuple = args.input_string # In any case hash input should be a bytestring if args.input_file: with open(args.input_file , "rb" ) as f: _lowerCAmelCase : List[Any] = f.read() else: _lowerCAmelCase : Any = bytes(_lowerCamelCase , "utf-8" ) print(SHAaHash(_lowerCamelCase ).final_hash() ) if __name__ == "__main__": main() import doctest doctest.testmod()
36
from __future__ import annotations import bisect def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = 0 , _lowerCamelCase = -1 ): '''simple docstring''' if hi < 0: _lowerCAmelCase : int = len(_lowerCamelCase ) while lo < hi: _lowerCAmelCase : Optional[Any] = lo + (hi - lo) // 2 if sorted_collection[mid] < item: _lowerCAmelCase : Union[str, Any] = mid + 1 else: _lowerCAmelCase : str = mid return lo def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = 0 , _lowerCamelCase = -1 ): '''simple docstring''' if hi < 0: _lowerCAmelCase : str = len(_lowerCamelCase ) while lo < hi: _lowerCAmelCase : Tuple = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: _lowerCAmelCase : Dict = mid + 1 else: _lowerCAmelCase : str = mid return lo def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = 0 , _lowerCamelCase = -1 ): '''simple docstring''' sorted_collection.insert(bisect_left(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase ) def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = 0 , _lowerCamelCase = -1 ): '''simple docstring''' sorted_collection.insert(bisect_right(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase ) def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : int = 0 _lowerCAmelCase : Union[str, Any] = len(_lowerCamelCase ) - 1 while left <= right: _lowerCAmelCase : int = left + (right - left) // 2 _lowerCAmelCase : int = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: _lowerCAmelCase : str = midpoint - 1 else: _lowerCAmelCase : Any = midpoint + 1 return None def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : Tuple = bisect.bisect_left(_lowerCamelCase , _lowerCamelCase ) if index != len(_lowerCamelCase ) and sorted_collection[index] == item: return index return None def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' if right < left: return None _lowerCAmelCase : Optional[int] = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase , midpoint - 1 ) else: return binary_search_by_recursion(_lowerCamelCase , _lowerCamelCase , midpoint + 1 , _lowerCamelCase ) if __name__ == "__main__": _snake_case = input("Enter numbers separated by comma:\n").strip() _snake_case = sorted(int(item) for item in user_input.split(",")) _snake_case = int(input("Enter a single number to be found in the list:\n")) _snake_case = binary_search(collection, target) if result is None: print(f'''{target} was not found in {collection}.''') else: print(f'''{target} was found at position {result} in {collection}.''')
36
1
'''simple docstring''' import random import unittest import numpy as np import torch from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionUpscalePipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __magic_name__ ( _UpperCAmelCase, unittest.TestCase): # TODO: is there an appropriate internal test set? UpperCamelCase__ = '''ssube/stable-diffusion-x4-upscaler-onnx''' def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , lowercase_ : Optional[Any]=0 ): lowercase_ : Any = floats_tensor((1, 3, 128, 128) , rng=random.Random(lowercase_ ) ) lowercase_ : List[str] = torch.manual_seed(lowercase_ ) lowercase_ : str = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """generator""": generator, """num_inference_steps""": 3, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ): lowercase_ : List[Any] = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) pipe.set_progress_bar_config(disable=lowercase_ ) lowercase_ : List[str] = self.get_dummy_inputs() lowercase_ : Dict = pipe(**lowercase_ ).images lowercase_ : Any = image[0, -3:, -3:, -1].flatten() # started as 128, should now be 512 assert image.shape == (1, 512, 512, 3) lowercase_ : Optional[int] = np.array( [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23] ) assert np.abs(image_slice - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE_ ( self : List[str] ): lowercase_ : Dict = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) lowercase_ : List[Any] = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=lowercase_ ) pipe.set_progress_bar_config(disable=lowercase_ ) lowercase_ : Optional[Any] = self.get_dummy_inputs() lowercase_ : int = pipe(**lowercase_ ).images lowercase_ : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase_ : Dict = np.array( [0.6_89_88_92, 0.59_24_05_56, 0.52_49_95_27, 0.58_86_62_15, 0.52_25_82_35, 0.52_57_27_15, 0.62_41_44_73, 0.6_17_43_87, 0.6_21_49_64] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] ): lowercase_ : Optional[int] = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) lowercase_ : Tuple = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=lowercase_ ) lowercase_ : Optional[int] = self.get_dummy_inputs() lowercase_ : Any = pipe(**lowercase_ ).images lowercase_ : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase_ : str = np.array( [0.7_65_92_78, 0.76_43_76_64, 0.75_57_91_07, 0.7_69_11_16, 0.77_66_69_86, 0.7_72_76_72, 0.7_75_86_64, 0.7_81_22_26, 0.76_94_25_15] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] ): lowercase_ : Union[str, Any] = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) lowercase_ : Optional[Any] = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=lowercase_ ) lowercase_ : Any = self.get_dummy_inputs() lowercase_ : List[Any] = pipe(**lowercase_ ).images lowercase_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase_ : Any = np.array( [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ): lowercase_ : Any = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) lowercase_ : Optional[Any] = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=lowercase_ ) lowercase_ : int = self.get_dummy_inputs() lowercase_ : List[str] = pipe(**lowercase_ ).images lowercase_ : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) lowercase_ : Optional[int] = np.array( [0.77_42_44_96, 0.77_36_01, 0.7_64_52_88, 0.7_76_95_98, 0.7_77_27_39, 0.7_73_86_88, 0.78_18_72_33, 0.77_87_95_84, 0.76_70_43] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 @nightly @require_onnxruntime @require_torch_gpu class __magic_name__ ( unittest.TestCase): @property def SCREAMING_SNAKE_CASE_ ( self : Tuple ): return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def SCREAMING_SNAKE_CASE_ ( self : List[str] ): lowercase_ : Optional[Any] = ort.SessionOptions() lowercase_ : List[Any] = False return options def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ): lowercase_ : Any = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) lowercase_ : Optional[Any] = init_image.resize((128, 128) ) # using the PNDM scheduler by default lowercase_ : Optional[int] = OnnxStableDiffusionUpscalePipeline.from_pretrained( """ssube/stable-diffusion-x4-upscaler-onnx""" , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=lowercase_ ) lowercase_ : List[str] = """A fantasy landscape, trending on artstation""" lowercase_ : List[str] = torch.manual_seed(0 ) lowercase_ : str = pipe( prompt=lowercase_ , image=lowercase_ , guidance_scale=7.5 , num_inference_steps=10 , generator=lowercase_ , output_type="""np""" , ) lowercase_ : Optional[Any] = output.images lowercase_ : List[Any] = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 512, 3) lowercase_ : Optional[Any] = np.array([0.48_83, 0.49_47, 0.49_80, 0.49_75, 0.49_82, 0.49_80, 0.50_00, 0.50_06, 0.49_72] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2 def SCREAMING_SNAKE_CASE_ ( self : List[Any] ): lowercase_ : List[Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) lowercase_ : Tuple = init_image.resize((128, 128) ) lowercase_ : Any = LMSDiscreteScheduler.from_pretrained( """ssube/stable-diffusion-x4-upscaler-onnx""" , subfolder="""scheduler""" ) lowercase_ : List[Any] = OnnxStableDiffusionUpscalePipeline.from_pretrained( """ssube/stable-diffusion-x4-upscaler-onnx""" , scheduler=lowercase_ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=lowercase_ ) lowercase_ : int = """A fantasy landscape, trending on artstation""" lowercase_ : Optional[Any] = torch.manual_seed(0 ) lowercase_ : Any = pipe( prompt=lowercase_ , image=lowercase_ , guidance_scale=7.5 , num_inference_steps=20 , generator=lowercase_ , output_type="""np""" , ) lowercase_ : Dict = output.images lowercase_ : Union[str, Any] = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 512, 3) lowercase_ : Union[str, Any] = np.array( [0.50_17_37_53, 0.50_22_33_56, 0.50_20_39, 0.50_23_30_36, 0.5_02_37_25, 0.5_02_26_01, 0.5_01_87_58, 0.50_23_40_85, 0.50_24_15_66] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2
21
'''simple docstring''' from __future__ import annotations from typing import Any def lowerCamelCase ( UpperCAmelCase__ : list ) -> int: if not postfix_notation: return 0 lowercase_ : Any = {"""+""", """-""", """*""", """/"""} lowercase_ : list[Any] = [] for token in postfix_notation: if token in operations: lowercase_ , lowercase_ : Dict = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(UpperCAmelCase__ ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
21
1
"""simple docstring""" def lowercase__ ( _UpperCAmelCase ) -> List[str]: '''simple docstring''' stooge(_UpperCAmelCase , 0 , len(_UpperCAmelCase ) - 1 ) return arr def lowercase__ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: lowercase , lowercase : Tuple = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: lowercase : int = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(_UpperCAmelCase , _UpperCAmelCase , (h - t) ) # Recursively sort last 2/3 elements stooge(_UpperCAmelCase , i + t , (_UpperCAmelCase) ) # Recursively sort first 2/3 elements stooge(_UpperCAmelCase , _UpperCAmelCase , (h - t) ) if __name__ == "__main__": _UpperCamelCase: List[str] = input('Enter numbers separated by a comma:\n').strip() _UpperCamelCase: List[Any] = [int(item) for item in user_input.split(',')] print(stooge_sort(unsorted))
255
"""simple docstring""" 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__ ( SCREAMING_SNAKE_CASE__, unittest.TestCase ): _lowerCamelCase = CLIPTokenizer _lowerCamelCase = CLIPTokenizerFast _lowerCamelCase = True _lowerCamelCase = {} _lowerCamelCase = False def lowercase ( self : Tuple ) -> int: super().setUp() # fmt: off lowercase : Dict = ['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 lowercase : List[Any] = dict(zip(lowerCAmelCase, range(len(lowerCAmelCase ) ) ) ) lowercase : List[str] = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>'] lowercase : Union[str, Any] = {'unk_token': '<unk>'} lowercase : Union[str, Any] = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES['vocab_file'] ) lowercase : Tuple = 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 lowercase ( self : Dict, **lowerCAmelCase : Optional[Any] ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname, **lowerCAmelCase ) def lowercase ( self : Optional[Any], **lowerCAmelCase : Tuple ) -> str: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **lowerCAmelCase ) def lowercase ( self : Optional[Any], lowerCAmelCase : List[Any] ) -> Optional[Any]: lowercase : int = 'lower newer' lowercase : str = 'lower newer' return input_text, output_text def lowercase ( self : Optional[Any] ) -> Optional[Any]: lowercase : str = CLIPTokenizer(self.vocab_file, self.merges_file, **self.special_tokens_map ) lowercase : Union[str, Any] = 'lower newer' lowercase : List[str] = ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>'] lowercase : List[str] = tokenizer.tokenize(lowerCAmelCase ) self.assertListEqual(lowerCAmelCase, lowerCAmelCase ) lowercase : int = tokens + [tokenizer.unk_token] lowercase : Optional[int] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase ), lowerCAmelCase ) @require_ftfy def lowercase ( self : Tuple ) -> List[str]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowercase : List[str] = self.tokenizer_class.from_pretrained(lowerCAmelCase, **lowerCAmelCase ) lowercase : Dict = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase, **lowerCAmelCase ) lowercase : Optional[int] = 'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.' lowercase : int = tokenizer_s.tokenize(lowerCAmelCase ) lowercase : Any = 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 lowercase : Optional[int] = 'xa\u0303y' + ' ' + 'x\xe3y' lowercase : int = tokenizer_s.tokenize(lowerCAmelCase ) lowercase : Optional[Any] = tokenizer_r.tokenize(lowerCAmelCase ) self.assertListEqual(lowerCAmelCase, lowerCAmelCase ) # Test that the tokenization is identical on unicode of space type lowercase : Any = [ '\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: lowercase : Optional[Any] = tokenizer_s.tokenize(lowerCAmelCase ) lowercase : Union[str, Any] = tokenizer_r.tokenize(lowerCAmelCase ) self.assertListEqual(lowerCAmelCase, lowerCAmelCase ) # Test that the tokenization is identical on unicode of line break type lowercase : Optional[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: lowercase : str = tokenizer_s.tokenize(lowerCAmelCase ) lowercase : str = tokenizer_r.tokenize(lowerCAmelCase ) self.assertListEqual(lowerCAmelCase, lowerCAmelCase ) def lowercase ( self : Any ) -> List[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})''' ): lowercase : Optional[int] = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` lowercase : Union[str, Any] = f'''{text_of_1_token} {text_of_1_token}''' lowercase : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase, use_fast=lowerCAmelCase, ) lowercase : Dict = 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 )), ) lowercase : Tuple = f''' {text}''' lowercase : Optional[Any] = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase, use_fast=lowerCAmelCase, ) lowercase : 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 lowercase ( self : Dict ) -> List[Any]: # 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 lowercase ( self : List[Any] ) -> str: super().test_tokenization_python_rust_equals() def lowercase ( self : Dict ) -> Tuple: # CLIP always lower cases letters pass
255
1
from itertools import product def __lowerCamelCase ( lowerCamelCase__ : int , lowerCamelCase__ : int ): '''simple docstring''' lowerCamelCase = sides_number lowerCamelCase = max_face_number * dice_number lowerCamelCase = [0] * (max_total + 1) lowerCamelCase = 1 lowerCamelCase = range(lowerCamelCase_ , max_face_number + 1 ) for dice_numbers in product(lowerCamelCase_ , repeat=lowerCamelCase_ ): lowerCamelCase = sum(lowerCamelCase_ ) totals_frequencies[total] += 1 return totals_frequencies def __lowerCamelCase ( ): '''simple docstring''' lowerCamelCase = total_frequency_distribution( sides_number=4 , dice_number=9 ) lowerCamelCase = total_frequency_distribution( sides_number=6 , dice_number=6 ) lowerCamelCase = 0 lowerCamelCase = 9 lowerCamelCase = 4 * 9 lowerCamelCase = 6 for peter_total in range(lowerCamelCase_ , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) lowerCamelCase = (4**9) * (6**6) lowerCamelCase = peter_wins_count / total_games_number lowerCamelCase = round(lowerCamelCase_ , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(f"""{solution() = }""")
358
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps 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() @skip_mps class __lowercase ( a_ , a_ , unittest.TestCase ): """simple docstring""" UpperCamelCase : Dict = StableDiffusionPanoramaPipeline UpperCamelCase : List[Any] = TEXT_TO_IMAGE_PARAMS UpperCamelCase : Tuple = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase : Tuple = TEXT_TO_IMAGE_IMAGE_PARAMS UpperCamelCase : Optional[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS def __A ( self ) -> Dict: '''simple docstring''' torch.manual_seed(0 ) lowerCamelCase = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , 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() 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 , ) torch.manual_seed(0 ) lowerCamelCase = 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=10_00 , ) lowerCamelCase = CLIPTextModel(A ) lowerCamelCase = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) lowerCamelCase = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def __A ( self , A , A=0 ) -> Optional[Any]: '''simple docstring''' lowerCamelCase = torch.manual_seed(A ) lowerCamelCase = { """prompt""": """a photo of the dolomites""", """generator""": generator, # Setting height and width to None to prevent OOMs on CPU. """height""": None, """width""": None, """num_inference_steps""": 1, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def __A ( self ) -> List[str]: '''simple docstring''' lowerCamelCase = """cpu""" # ensure determinism for the device-dependent torch.Generator lowerCamelCase = self.get_dummy_components() lowerCamelCase = StableDiffusionPanoramaPipeline(**A ) lowerCamelCase = sd_pipe.to(A ) sd_pipe.set_progress_bar_config(disable=A ) lowerCamelCase = self.get_dummy_inputs(A ) lowerCamelCase = sd_pipe(**A ).images lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase = np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ) -> List[Any]: '''simple docstring''' super().test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ) -> Optional[Any]: '''simple docstring''' super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.2_5e-3 ) def __A ( self ) -> str: '''simple docstring''' lowerCamelCase = """cpu""" # ensure determinism for the device-dependent torch.Generator lowerCamelCase = self.get_dummy_components() lowerCamelCase = StableDiffusionPanoramaPipeline(**A ) lowerCamelCase = sd_pipe.to(A ) sd_pipe.set_progress_bar_config(disable=A ) lowerCamelCase = self.get_dummy_inputs(A ) lowerCamelCase = """french fries""" lowerCamelCase = sd_pipe(**A , negative_prompt=A ) lowerCamelCase = output.images lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ) -> Tuple: '''simple docstring''' lowerCamelCase = """cpu""" # ensure determinism for the device-dependent torch.Generator lowerCamelCase = self.get_dummy_components() lowerCamelCase = StableDiffusionPanoramaPipeline(**A ) lowerCamelCase = sd_pipe.to(A ) sd_pipe.set_progress_bar_config(disable=A ) lowerCamelCase = self.get_dummy_inputs(A ) lowerCamelCase = sd_pipe(**A , view_batch_size=2 ) lowerCamelCase = output.images lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase = np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ) -> List[Any]: '''simple docstring''' lowerCamelCase = """cpu""" # ensure determinism for the device-dependent torch.Generator lowerCamelCase = self.get_dummy_components() lowerCamelCase = EulerAncestralDiscreteScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="""scaled_linear""" ) lowerCamelCase = StableDiffusionPanoramaPipeline(**A ) lowerCamelCase = sd_pipe.to(A ) sd_pipe.set_progress_bar_config(disable=A ) lowerCamelCase = self.get_dummy_inputs(A ) lowerCamelCase = sd_pipe(**A ).images lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase = np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ) -> int: '''simple docstring''' lowerCamelCase = """cpu""" # ensure determinism for the device-dependent torch.Generator lowerCamelCase = self.get_dummy_components() lowerCamelCase = PNDMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , skip_prk_steps=A ) lowerCamelCase = StableDiffusionPanoramaPipeline(**A ) lowerCamelCase = sd_pipe.to(A ) sd_pipe.set_progress_bar_config(disable=A ) lowerCamelCase = self.get_dummy_inputs(A ) lowerCamelCase = sd_pipe(**A ).images lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase = np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def __A ( self ) -> List[Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self , A=0 ) -> Dict: '''simple docstring''' lowerCamelCase = torch.manual_seed(A ) lowerCamelCase = { """prompt""": """a photo of the dolomites""", """generator""": generator, """num_inference_steps""": 3, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def __A ( self ) -> Union[str, Any]: '''simple docstring''' lowerCamelCase = """stabilityai/stable-diffusion-2-base""" lowerCamelCase = DDIMScheduler.from_pretrained(A , subfolder="""scheduler""" ) lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(A , scheduler=A , safety_checker=A ) pipe.to(A ) pipe.set_progress_bar_config(disable=A ) pipe.enable_attention_slicing() lowerCamelCase = self.get_inputs() lowerCamelCase = pipe(**A ).images lowerCamelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) lowerCamelCase = np.array( [ 0.36968392, 0.27025372, 0.32446766, 0.28379387, 0.36363274, 0.30733347, 0.27100027, 0.27054125, 0.25536096, ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-2 def __A ( self ) -> Dict: '''simple docstring''' lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained( """stabilityai/stable-diffusion-2-base""" , safety_checker=A ) lowerCamelCase = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(A ) pipe.set_progress_bar_config(disable=A ) pipe.enable_attention_slicing() lowerCamelCase = self.get_inputs() lowerCamelCase = pipe(**A ).images lowerCamelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) lowerCamelCase = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def __A ( self ) -> int: '''simple docstring''' lowerCamelCase = 0 def callback_fn(A , A , A ) -> None: lowerCamelCase = True nonlocal number_of_steps number_of_steps += 1 if step == 1: lowerCamelCase = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) lowerCamelCase = latents[0, -3:, -3:, -1] lowerCamelCase = np.array( [ 0.18681869, 0.33907816, 0.5361276, 0.14432865, -0.02856611, -0.73941123, 0.23397987, 0.47322682, -0.37823164, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: lowerCamelCase = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) lowerCamelCase = latents[0, -3:, -3:, -1] lowerCamelCase = np.array( [ 0.18539645, 0.33987248, 0.5378559, 0.14437142, -0.02455261, -0.7338317, 0.23990755, 0.47356272, -0.3786505, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 lowerCamelCase = False lowerCamelCase = """stabilityai/stable-diffusion-2-base""" lowerCamelCase = DDIMScheduler.from_pretrained(A , subfolder="""scheduler""" ) lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(A , scheduler=A , safety_checker=A ) lowerCamelCase = pipe.to(A ) pipe.set_progress_bar_config(disable=A ) pipe.enable_attention_slicing() lowerCamelCase = self.get_inputs() pipe(**A , callback=A , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __A ( self ) -> str: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() lowerCamelCase = """stabilityai/stable-diffusion-2-base""" lowerCamelCase = DDIMScheduler.from_pretrained(A , subfolder="""scheduler""" ) lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(A , scheduler=A , safety_checker=A ) lowerCamelCase = pipe.to(A ) pipe.set_progress_bar_config(disable=A ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() lowerCamelCase = self.get_inputs() lowerCamelCase = pipe(**A ) lowerCamelCase = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
66
0
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=0.0 , __lowerCamelCase = None , __lowerCamelCase = "geglu" , __lowerCamelCase = None , __lowerCamelCase = False , __lowerCamelCase = False , __lowerCamelCase = False , __lowerCamelCase = False , __lowerCamelCase = True , __lowerCamelCase = "layer_norm" , __lowerCamelCase = False , ) -> Optional[int]: super().__init__() _A : Optional[Any] = only_cross_attention _A : int = (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero" _A : List[Any] = (num_embeds_ada_norm is not None) and norm_type == "ada_norm" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F"`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to" F" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.") # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: _A : Any = AdaLayerNorm(__lowerCamelCase , __lowerCamelCase) elif self.use_ada_layer_norm_zero: _A : Optional[int] = AdaLayerNormZero(__lowerCamelCase , __lowerCamelCase) else: _A : str = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) _A : int = Attention( query_dim=__lowerCamelCase , heads=__lowerCamelCase , dim_head=__lowerCamelCase , dropout=__lowerCamelCase , bias=__lowerCamelCase , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=__lowerCamelCase , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. _A : int = ( AdaLayerNorm(__lowerCamelCase , __lowerCamelCase) if self.use_ada_layer_norm else nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) ) _A : Optional[int] = Attention( query_dim=__lowerCamelCase , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=__lowerCamelCase , dim_head=__lowerCamelCase , dropout=__lowerCamelCase , bias=__lowerCamelCase , upcast_attention=__lowerCamelCase , ) # is self-attn if encoder_hidden_states is none else: _A : Tuple = None _A : List[Any] = None # 3. Feed-forward _A : Any = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) _A : Any = FeedForward(__lowerCamelCase , dropout=__lowerCamelCase , activation_fn=__lowerCamelCase , final_dropout=__lowerCamelCase) # let chunk size default to None _A : Optional[int] = None _A : List[Any] = 0 def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> Union[str, Any]: # Sets chunk feed-forward _A : Optional[int] = chunk_size _A : str = dim def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , __lowerCamelCase = None , ) -> int: # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: _A : Union[str, Any] = self.norma(__lowerCamelCase , __lowerCamelCase) elif self.use_ada_layer_norm_zero: _A , _A , _A , _A , _A : Tuple = self.norma( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , hidden_dtype=hidden_states.dtype) else: _A : Tuple = self.norma(__lowerCamelCase) _A : Dict = cross_attention_kwargs if cross_attention_kwargs is not None else {} _A : Optional[Any] = self.attna( __lowerCamelCase , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=__lowerCamelCase , **__lowerCamelCase , ) if self.use_ada_layer_norm_zero: _A : int = gate_msa.unsqueeze(1) * attn_output _A : str = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: _A : Any = ( self.norma(__lowerCamelCase , __lowerCamelCase) if self.use_ada_layer_norm else self.norma(__lowerCamelCase) ) _A : Union[str, Any] = self.attna( __lowerCamelCase , encoder_hidden_states=__lowerCamelCase , attention_mask=__lowerCamelCase , **__lowerCamelCase , ) _A : Any = attn_output + hidden_states # 3. Feed-forward _A : str = self.norma(__lowerCamelCase) if self.use_ada_layer_norm_zero: _A : Optional[Any] = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F"`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.") _A : Optional[Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size _A : int = torch.cat( [self.ff(__lowerCamelCase) for hid_slice in norm_hidden_states.chunk(__lowerCamelCase , dim=self._chunk_dim)] , dim=self._chunk_dim , ) else: _A : List[str] = self.ff(__lowerCamelCase) if self.use_ada_layer_norm_zero: _A : Optional[int] = gate_mlp.unsqueeze(1) * ff_output _A : Optional[int] = ff_output + hidden_states return hidden_states class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = 4 , __lowerCamelCase = 0.0 , __lowerCamelCase = "geglu" , __lowerCamelCase = False , ) -> Tuple: super().__init__() _A : List[Any] = int(dim * mult) _A : Any = dim_out if dim_out is not None else dim if activation_fn == "gelu": _A : Tuple = GELU(__lowerCamelCase , __lowerCamelCase) if activation_fn == "gelu-approximate": _A : List[Any] = GELU(__lowerCamelCase , __lowerCamelCase , approximate="tanh") elif activation_fn == "geglu": _A : Optional[Any] = GEGLU(__lowerCamelCase , __lowerCamelCase) elif activation_fn == "geglu-approximate": _A : Any = ApproximateGELU(__lowerCamelCase , __lowerCamelCase) _A : List[str] = nn.ModuleList([]) # project in self.net.append(__lowerCamelCase) # project dropout self.net.append(nn.Dropout(__lowerCamelCase)) # project out self.net.append(nn.Linear(__lowerCamelCase , __lowerCamelCase)) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(__lowerCamelCase)) def _lowerCamelCase ( self , __lowerCamelCase) -> List[str]: for module in self.net: _A : List[Any] = module(__lowerCamelCase) return hidden_states class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = "none") -> Any: super().__init__() _A : str = nn.Linear(__lowerCamelCase , __lowerCamelCase) _A : int = approximate def _lowerCamelCase ( self , __lowerCamelCase) -> Optional[int]: if gate.device.type != "mps": return F.gelu(__lowerCamelCase , approximate=self.approximate) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa) , approximate=self.approximate).to(dtype=gate.dtype) def _lowerCamelCase ( self , __lowerCamelCase) -> Union[str, Any]: _A : Dict = self.proj(__lowerCamelCase) _A : int = self.gelu(__lowerCamelCase) return hidden_states class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> Union[str, Any]: super().__init__() _A : int = nn.Linear(__lowerCamelCase , dim_out * 2) def _lowerCamelCase ( self , __lowerCamelCase) -> Optional[int]: if gate.device.type != "mps": return F.gelu(__lowerCamelCase) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa)).to(dtype=gate.dtype) def _lowerCamelCase ( self , __lowerCamelCase) -> Dict: _A , _A : List[Any] = self.proj(__lowerCamelCase).chunk(2 , dim=-1) return hidden_states * self.gelu(__lowerCamelCase) class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> Union[str, Any]: super().__init__() _A : Union[str, Any] = nn.Linear(__lowerCamelCase , __lowerCamelCase) def _lowerCamelCase ( self , __lowerCamelCase) -> Any: _A : Any = self.proj(__lowerCamelCase) return x * torch.sigmoid(1.7_0_2 * x) class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> List[str]: super().__init__() _A : Optional[int] = nn.Embedding(__lowerCamelCase , __lowerCamelCase) _A : Tuple = nn.SiLU() _A : Union[str, Any] = nn.Linear(__lowerCamelCase , embedding_dim * 2) _A : int = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase) def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> List[Any]: _A : Dict = self.linear(self.silu(self.emb(__lowerCamelCase))) _A , _A : Optional[Any] = torch.chunk(__lowerCamelCase , 2) _A : Any = self.norm(__lowerCamelCase) * (1 + scale) + shift return x class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase) -> Optional[Any]: super().__init__() _A : str = CombinedTimestepLabelEmbeddings(__lowerCamelCase , __lowerCamelCase) _A : Optional[int] = nn.SiLU() _A : Dict = nn.Linear(__lowerCamelCase , 6 * embedding_dim , bias=__lowerCamelCase) _A : int = nn.LayerNorm(__lowerCamelCase , elementwise_affine=__lowerCamelCase , eps=1e-6) def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=None) -> Union[str, Any]: _A : List[Any] = self.linear(self.silu(self.emb(__lowerCamelCase , __lowerCamelCase , hidden_dtype=__lowerCamelCase))) _A , _A , _A , _A , _A , _A : Optional[int] = emb.chunk(6 , dim=1) _A : int = self.norm(__lowerCamelCase) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class lowerCAmelCase__ ( nn.Module): '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = 1e-5) -> int: super().__init__() _A : Any = num_groups _A : Any = eps if act_fn is None: _A : Tuple = None else: _A : str = get_activation(__lowerCamelCase) _A : List[Any] = nn.Linear(__lowerCamelCase , out_dim * 2) def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase) -> Optional[Any]: if self.act: _A : Tuple = self.act(__lowerCamelCase) _A : Optional[Any] = self.linear(__lowerCamelCase) _A : Tuple = emb[:, :, None, None] _A , _A : Union[str, Any] = emb.chunk(2 , dim=1) _A : List[str] = F.group_norm(__lowerCamelCase , self.num_groups , eps=self.eps) _A : int = x * (1 + scale) + shift return x
11
'''simple docstring''' import os from itertools import chain from random import randrange, shuffle import pytest from .sola import PokerHand lowerCAmelCase :Tuple = ( '''4S 3H 2C 7S 5H''', '''9D 8H 2C 6S 7H''', '''2D 6D 9D TH 7D''', '''TC 8C 2S JH 6C''', '''JH 8S TH AH QH''', '''TS KS 5S 9S AC''', '''KD 6S 9D TH AD''', '''KS 8D 4D 9S 4S''', # pair '''8C 4S KH JS 4D''', # pair '''QH 8H KD JH 8S''', # pair '''KC 4H KS 2H 8D''', # pair '''KD 4S KC 3H 8S''', # pair '''AH 8S AS KC JH''', # pair '''3H 4C 4H 3S 2H''', # 2 pairs '''5S 5D 2C KH KH''', # 2 pairs '''3C KH 5D 5S KH''', # 2 pairs '''AS 3C KH AD KH''', # 2 pairs '''7C 7S 3S 7H 5S''', # 3 of a kind '''7C 7S KH 2H 7H''', # 3 of a kind '''AC KH QH AH AS''', # 3 of a kind '''2H 4D 3C AS 5S''', # straight (low ace) '''3C 5C 4C 2C 6H''', # straight '''6S 8S 7S 5H 9H''', # straight '''JS QS 9H TS KH''', # straight '''QC KH TS JS AH''', # straight (high ace) '''8C 9C 5C 3C TC''', # flush '''3S 8S 9S 5S KS''', # flush '''4C 5C 9C 8C KC''', # flush '''JH 8H AH KH QH''', # flush '''3D 2H 3H 2C 2D''', # full house '''2H 2C 3S 3H 3D''', # full house '''KH KC 3S 3H 3D''', # full house '''JC 6H JS JD JH''', # 4 of a kind '''JC 7H JS JD JH''', # 4 of a kind '''JC KH JS JD JH''', # 4 of a kind '''2S AS 4S 5S 3S''', # straight flush (low ace) '''2D 6D 3D 4D 5D''', # straight flush '''5C 6C 3C 7C 4C''', # straight flush '''JH 9H TH KH QH''', # straight flush '''JH AH TH KH QH''', # royal flush (high ace straight flush) ) lowerCAmelCase :List[Any] = ( ('''2H 3H 4H 5H 6H''', '''KS AS TS QS JS''', '''Loss'''), ('''2H 3H 4H 5H 6H''', '''AS AD AC AH JD''', '''Win'''), ('''AS AH 2H AD AC''', '''JS JD JC JH 3D''', '''Win'''), ('''2S AH 2H AS AC''', '''JS JD JC JH AD''', '''Loss'''), ('''2S AH 2H AS AC''', '''2H 3H 5H 6H 7H''', '''Win'''), ('''AS 3S 4S 8S 2S''', '''2H 3H 5H 6H 7H''', '''Win'''), ('''2H 3H 5H 6H 7H''', '''2S 3H 4H 5S 6C''', '''Win'''), ('''2S 3H 4H 5S 6C''', '''3D 4C 5H 6H 2S''', '''Tie'''), ('''2S 3H 4H 5S 6C''', '''AH AC 5H 6H AS''', '''Win'''), ('''2S 2H 4H 5S 4C''', '''AH AC 5H 6H AS''', '''Loss'''), ('''2S 2H 4H 5S 4C''', '''AH AC 5H 6H 7S''', '''Win'''), ('''6S AD 7H 4S AS''', '''AH AC 5H 6H 7S''', '''Loss'''), ('''2S AH 4H 5S KC''', '''AH AC 5H 6H 7S''', '''Loss'''), ('''2S 3H 6H 7S 9C''', '''7H 3C TH 6H 9S''', '''Loss'''), ('''4S 5H 6H TS AC''', '''3S 5H 6H TS AC''', '''Win'''), ('''2S AH 4H 5S 6C''', '''AD 4C 5H 6H 2C''', '''Tie'''), ('''AS AH 3H AD AC''', '''AS AH 2H AD AC''', '''Win'''), ('''AH AC 5H 5C QS''', '''AH AC 5H 5C KS''', '''Loss'''), ('''AH AC 5H 5C QS''', '''KH KC 5H 5C QS''', '''Win'''), ('''7C 7S KH 2H 7H''', '''3C 3S AH 2H 3H''', '''Win'''), ('''3C 3S AH 2H 3H''', '''7C 7S KH 2H 7H''', '''Loss'''), ('''6H 5H 4H 3H 2H''', '''5H 4H 3H 2H AH''', '''Win'''), ('''5H 4H 3H 2H AH''', '''5H 4H 3H 2H AH''', '''Tie'''), ('''5H 4H 3H 2H AH''', '''6H 5H 4H 3H 2H''', '''Loss'''), ('''AH AD KS KC AC''', '''AH KD KH AC KC''', '''Win'''), ('''2H 4D 3C AS 5S''', '''2H 4D 3C 6S 5S''', '''Loss'''), ('''2H 3S 3C 3H 2S''', '''3S 3C 2S 2H 2D''', '''Win'''), ('''4D 6D 5D 2D JH''', '''3S 8S 3H TC KH''', '''Loss'''), ('''4S 6C 8S 3S 7S''', '''AD KS 2D 7D 7C''', '''Loss'''), ('''6S 4C 7H 8C 3H''', '''5H JC AH 9D 9C''', '''Loss'''), ('''9D 9H JH TC QH''', '''3C 2S JS 5C 7H''', '''Win'''), ('''2H TC 8S AD 9S''', '''4H TS 7H 2C 5C''', '''Win'''), ('''9D 3S 2C 7S 7C''', '''JC TD 3C TC 9H''', '''Loss'''), ) lowerCAmelCase :str = ( ('''2H 3H 4H 5H 6H''', True), ('''AS AH 2H AD AC''', False), ('''2H 3H 5H 6H 7H''', True), ('''KS AS TS QS JS''', True), ('''8H 9H QS JS TH''', False), ('''AS 3S 4S 8S 2S''', True), ) lowerCAmelCase :str = ( ('''2H 3H 4H 5H 6H''', True), ('''AS AH 2H AD AC''', False), ('''2H 3H 5H 6H 7H''', False), ('''KS AS TS QS JS''', True), ('''8H 9H QS JS TH''', True), ) lowerCAmelCase :Optional[Any] = ( ('''2H 4D 3C AS 5S''', True, [5, 4, 3, 2, 1_4]), ('''2H 5D 3C AS 5S''', False, [1_4, 5, 5, 3, 2]), ('''JH QD KC AS TS''', False, [1_4, 1_3, 1_2, 1_1, 1_0]), ('''9D 3S 2C 7S 7C''', False, [9, 7, 7, 3, 2]), ) lowerCAmelCase :Union[str, Any] = ( ('''JH AH TH KH QH''', 0), ('''JH 9H TH KH QH''', 0), ('''JC KH JS JD JH''', 7), ('''KH KC 3S 3H 3D''', 6), ('''8C 9C 5C 3C TC''', 0), ('''JS QS 9H TS KH''', 0), ('''7C 7S KH 2H 7H''', 3), ('''3C KH 5D 5S KH''', 2), ('''QH 8H KD JH 8S''', 1), ('''2D 6D 9D TH 7D''', 0), ) lowerCAmelCase :Tuple = ( ('''JH AH TH KH QH''', 2_3), ('''JH 9H TH KH QH''', 2_2), ('''JC KH JS JD JH''', 2_1), ('''KH KC 3S 3H 3D''', 2_0), ('''8C 9C 5C 3C TC''', 1_9), ('''JS QS 9H TS KH''', 1_8), ('''7C 7S KH 2H 7H''', 1_7), ('''3C KH 5D 5S KH''', 1_6), ('''QH 8H KD JH 8S''', 1_5), ('''2D 6D 9D TH 7D''', 1_4), ) def lowerCamelCase ( ): """simple docstring""" __magic_name__ , __magic_name__ : Union[str, Any] = randrange(len(lowerCAmelCase ) ), randrange(len(lowerCAmelCase ) ) __magic_name__ : Optional[int] = ['Loss', 'Tie', 'Win'][(play >= oppo) + (play > oppo)] __magic_name__ , __magic_name__ : Optional[int] = SORTED_HANDS[play], SORTED_HANDS[oppo] return hand, other, expected def lowerCamelCase ( lowerCAmelCase : int = 100 ): """simple docstring""" return (generate_random_hand() for _ in range(lowerCAmelCase )) @pytest.mark.parametrize('hand, expected' , lowerCAmelCase ) def lowerCamelCase ( lowerCAmelCase : Tuple , lowerCAmelCase : Union[str, Any] ): """simple docstring""" assert PokerHand(lowerCAmelCase )._is_flush() == expected @pytest.mark.parametrize('hand, expected' , lowerCAmelCase ) def lowerCamelCase ( lowerCAmelCase : List[Any] , lowerCAmelCase : Union[str, Any] ): """simple docstring""" assert PokerHand(lowerCAmelCase )._is_straight() == expected @pytest.mark.parametrize('hand, expected, card_values' , lowerCAmelCase ) def lowerCamelCase ( lowerCAmelCase : Any , lowerCAmelCase : Optional[Any] , lowerCAmelCase : Tuple ): """simple docstring""" __magic_name__ : Any = PokerHand(lowerCAmelCase ) assert player._is_five_high_straight() == expected assert player._card_values == card_values @pytest.mark.parametrize('hand, expected' , lowerCAmelCase ) def lowerCamelCase ( lowerCAmelCase : Any , lowerCAmelCase : str ): """simple docstring""" assert PokerHand(lowerCAmelCase )._is_same_kind() == expected @pytest.mark.parametrize('hand, expected' , lowerCAmelCase ) def lowerCamelCase ( lowerCAmelCase : Dict , lowerCAmelCase : Dict ): """simple docstring""" assert PokerHand(lowerCAmelCase )._hand_type == expected @pytest.mark.parametrize('hand, other, expected' , lowerCAmelCase ) def lowerCamelCase ( lowerCAmelCase : int , lowerCAmelCase : str , lowerCAmelCase : Tuple ): """simple docstring""" assert PokerHand(lowerCAmelCase ).compare_with(PokerHand(lowerCAmelCase ) ) == expected @pytest.mark.parametrize('hand, other, expected' , generate_random_hands() ) def lowerCamelCase ( lowerCAmelCase : int , lowerCAmelCase : Optional[Any] , lowerCAmelCase : Any ): """simple docstring""" assert PokerHand(lowerCAmelCase ).compare_with(PokerHand(lowerCAmelCase ) ) == expected def lowerCamelCase ( ): """simple docstring""" __magic_name__ : Optional[int] = [PokerHand(lowerCAmelCase ) for hand in SORTED_HANDS] __magic_name__ : Tuple = poker_hands.copy() shuffle(lowerCAmelCase ) __magic_name__ : Union[str, Any] = chain(sorted(lowerCAmelCase ) ) for index, hand in enumerate(lowerCAmelCase ): assert hand == poker_hands[index] def lowerCamelCase ( ): """simple docstring""" __magic_name__ : Dict = [PokerHand('2D AC 3H 4H 5S' ), PokerHand('2S 3H 4H 5S 6C' )] pokerhands.sort(reverse=lowerCAmelCase ) assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C" def lowerCamelCase ( ): """simple docstring""" __magic_name__ : Dict = PokerHand('2C 4S AS 3D 5C' ) __magic_name__ : Optional[Any] = True __magic_name__ : Union[str, Any] = [5, 4, 3, 2, 14] for _ in range(10 ): assert pokerhand._is_five_high_straight() == expected assert pokerhand._card_values == expected_card_values def lowerCamelCase ( ): """simple docstring""" __magic_name__ : Dict = 0 __magic_name__ : Dict = os.path.abspath(os.path.dirname(lowerCAmelCase ) ) __magic_name__ : Union[str, Any] = os.path.join(lowerCAmelCase , 'poker_hands.txt' ) with open(lowerCAmelCase ) as file_hand: for line in file_hand: __magic_name__ : Optional[int] = line[:14].strip() __magic_name__ : List[Any] = line[15:].strip() __magic_name__ , __magic_name__ : Tuple = PokerHand(lowerCAmelCase ), PokerHand(lowerCAmelCase ) __magic_name__ : List[Any] = player.compare_with(lowerCAmelCase ) if output == "Win": answer += 1 assert answer == 376
331
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 convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL UpperCAmelCase_ : int = logging.get_logger(__name__) class UpperCamelCase ( _UpperCAmelCase ): lowerCAmelCase : Optional[Any] = ["""pixel_values"""] def __init__( self , UpperCAmelCase__ = True , UpperCAmelCase__ = None , UpperCAmelCase__ = PILImageResampling.BICUBIC , UpperCAmelCase__ = True , UpperCAmelCase__ = 1 / 255 , UpperCAmelCase__ = True , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = True , **UpperCAmelCase__ , ): super().__init__(**UpperCAmelCase__ ) A__ = size if size is not None else {"height": 384, "width": 384} A__ = get_size_dict(UpperCAmelCase__ , default_to_square=UpperCAmelCase__ ) A__ = do_resize A__ = size A__ = resample A__ = do_rescale A__ = rescale_factor A__ = do_normalize A__ = image_mean if image_mean is not None else OPENAI_CLIP_MEAN A__ = image_std if image_std is not None else OPENAI_CLIP_STD A__ = do_convert_rgb def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = PILImageResampling.BICUBIC , UpperCAmelCase__ = None , **UpperCAmelCase__ , ): A__ = get_size_dict(UpperCAmelCase__ , default_to_square=UpperCAmelCase__ ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) A__ = (size["height"], size["width"]) return resize(UpperCAmelCase__ , size=UpperCAmelCase__ , resample=UpperCAmelCase__ , data_format=UpperCAmelCase__ , **UpperCAmelCase__ ) def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = None , **UpperCAmelCase__ , ): return rescale(UpperCAmelCase__ , scale=UpperCAmelCase__ , data_format=UpperCAmelCase__ , **UpperCAmelCase__ ) def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ = None , **UpperCAmelCase__ , ): return normalize(UpperCAmelCase__ , mean=UpperCAmelCase__ , std=UpperCAmelCase__ , data_format=UpperCAmelCase__ , **UpperCAmelCase__ ) def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = None , UpperCAmelCase__ = ChannelDimension.FIRST , **UpperCAmelCase__ , ): A__ = do_resize if do_resize is not None else self.do_resize A__ = resample if resample is not None else self.resample A__ = do_rescale if do_rescale is not None else self.do_rescale A__ = rescale_factor if rescale_factor is not None else self.rescale_factor A__ = do_normalize if do_normalize is not None else self.do_normalize A__ = image_mean if image_mean is not None else self.image_mean A__ = image_std if image_std is not None else self.image_std A__ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb A__ = size if size is not None else self.size A__ = get_size_dict(UpperCAmelCase__ , default_to_square=UpperCAmelCase__ ) A__ = make_list_of_images(UpperCAmelCase__ ) if not valid_images(UpperCAmelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # PIL RGBA images are converted to RGB if do_convert_rgb: A__ = [convert_to_rgb(UpperCAmelCase__ ) for image in images] # All transformations expect numpy arrays. A__ = [to_numpy_array(UpperCAmelCase__ ) for image in images] if do_resize: A__ = [self.resize(image=UpperCAmelCase__ , size=UpperCAmelCase__ , resample=UpperCAmelCase__ ) for image in images] if do_rescale: A__ = [self.rescale(image=UpperCAmelCase__ , scale=UpperCAmelCase__ ) for image in images] if do_normalize: A__ = [self.normalize(image=UpperCAmelCase__ , mean=UpperCAmelCase__ , std=UpperCAmelCase__ ) for image in images] A__ = [to_channel_dimension_format(UpperCAmelCase__ , UpperCAmelCase__ ) for image in images] A__ = BatchFeature(data={"pixel_values": images} , tensor_type=UpperCAmelCase__ ) return encoded_outputs
198
from __future__ import annotations class UpperCamelCase : def __init__( self , UpperCAmelCase__=None ): A__ = data A__ = None def __repr__( self ): A__ = [] A__ = self while temp: string_rep.append(F"""{temp.data}""" ) A__ = temp.next return "->".join(UpperCAmelCase__ ) def UpperCamelCase ( _A : list )-> Dict: """simple docstring""" if not elements_list: raise Exception("The Elements List is empty" ) A__ = A__ = Node(elements_list[0] ) for i in range(1 , len(_A ) ): A__ = Node(elements_list[i] ) A__ = current.next return head def UpperCamelCase ( _A : Node )-> None: """simple docstring""" if head_node is not None and isinstance(_A , _A ): print_reverse(head_node.next ) print(head_node.data ) def UpperCamelCase ( )-> Tuple: """simple docstring""" from doctest import testmod testmod() A__ = make_linked_list([14, 52, 14, 12, 43] ) print("Linked List:" ) print(_A ) print("Elements in Reverse:" ) print_reverse(_A ) if __name__ == "__main__": main()
198
1
"""simple docstring""" def _lowerCAmelCase ( UpperCamelCase_ = 200_0000 ): __SCREAMING_SNAKE_CASE = [0 for i in range(n + 1 )] __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 0 for i in range(UpperCamelCase_ ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(F"""{solution() = }""")
100
import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class __lowerCamelCase : """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=13 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=16 , UpperCAmelCase=[1, 2, 1] , UpperCAmelCase=[2, 2, 4] , UpperCAmelCase=2 , UpperCAmelCase=2.0 , UpperCAmelCase=True , UpperCAmelCase=0.0 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase="gelu" , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=True , UpperCAmelCase=10 , UpperCAmelCase=8 , UpperCAmelCase=["stage1", "stage2", "stage3"] , UpperCAmelCase=[1, 2, 3] , ): """simple docstring""" _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = image_size _UpperCAmelCase = patch_size _UpperCAmelCase = num_channels _UpperCAmelCase = embed_dim _UpperCAmelCase = depths _UpperCAmelCase = num_heads _UpperCAmelCase = window_size _UpperCAmelCase = mlp_ratio _UpperCAmelCase = qkv_bias _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = drop_path_rate _UpperCAmelCase = hidden_act _UpperCAmelCase = use_absolute_embeddings _UpperCAmelCase = patch_norm _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = initializer_range _UpperCAmelCase = is_training _UpperCAmelCase = scope _UpperCAmelCase = use_labels _UpperCAmelCase = type_sequence_label_size _UpperCAmelCase = encoder_stride _UpperCAmelCase = out_features _UpperCAmelCase = out_indices def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _UpperCAmelCase = None if self.use_labels: _UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self ): """simple docstring""" return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinModel(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _UpperCAmelCase = model(UpperCAmelCase ) _UpperCAmelCase = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) _UpperCAmelCase = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinBackbone(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _UpperCAmelCase = model(UpperCAmelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [13, 16, 16, 16] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , [16, 32, 64] ) # verify ValueError with self.parent.assertRaises(UpperCAmelCase ): _UpperCAmelCase = ['stem'] _UpperCAmelCase = MaskFormerSwinBackbone(config=UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.prepare_config_and_inputs() _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = config_and_inputs _UpperCAmelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCamelCase ( snake_case__ , snake_case__ , unittest.TestCase): """simple docstring""" UpperCamelCase__ = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) UpperCamelCase__ = {"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {} UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=UpperCAmelCase , embed_dim=37 ) @require_torch_multi_gpu @unittest.skip( reason=( '`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with' ' `nn.DataParallel`' ) ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self ): """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCamelCase ( self ): """simple docstring""" return def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*UpperCAmelCase ) @unittest.skip('Swin does not use inputs_embeds' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip('Swin does not support feedforward chunking' ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(UpperCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _UpperCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase , nn.Linear ) ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(UpperCAmelCase ) _UpperCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCAmelCase = [*signature.parameters.keys()] _UpperCAmelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase ) @unittest.skip(reason='MaskFormerSwin is only used as backbone and doesn\'t support output_attentions' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip(reason='MaskFormerSwin is only used as an internal backbone' ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) _UpperCAmelCase = outputs.hidden_states _UpperCAmelCase = getattr( self.model_tester , 'expected_num_hidden_layers' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(UpperCAmelCase ) , UpperCAmelCase ) # Swin has a different seq_length _UpperCAmelCase = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) _UpperCAmelCase = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = 3 _UpperCAmelCase = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) _UpperCAmelCase = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) _UpperCAmelCase = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) _UpperCAmelCase = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , (padded_height, padded_width) ) @unittest.skip(reason='MaskFormerSwin doesn\'t have pretrained checkpoints' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin' ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(UpperCAmelCase ): _UpperCAmelCase = 0 return t def check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase={} ): with torch.no_grad(): _UpperCAmelCase = model(**UpperCAmelCase , return_dict=UpperCAmelCase , **UpperCAmelCase ) _UpperCAmelCase = model(**UpperCAmelCase , return_dict=UpperCAmelCase , **UpperCAmelCase ).to_tuple() def recursive_check(UpperCAmelCase , UpperCAmelCase ): if isinstance(UpperCAmelCase , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(UpperCAmelCase , UpperCAmelCase ): recursive_check(UpperCAmelCase , UpperCAmelCase ) elif isinstance(UpperCAmelCase , UpperCAmelCase ): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values() ): recursive_check(UpperCAmelCase , UpperCAmelCase ) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(UpperCAmelCase ) , set_nan_tensor_to_zero(UpperCAmelCase ) , atol=1e-5 ) , msg=( 'Tuple and dict output are not equal. Difference:' F""" {torch.max(torch.abs(tuple_object - dict_object ) )}. Tuple has `nan`:""" F""" {torch.isnan(UpperCAmelCase ).any()} and `inf`: {torch.isinf(UpperCAmelCase )}. Dict has""" F""" `nan`: {torch.isnan(UpperCAmelCase ).any()} and `inf`: {torch.isinf(UpperCAmelCase )}.""" ) , ) recursive_check(UpperCAmelCase , UpperCAmelCase ) for model_class in self.all_model_classes: _UpperCAmelCase = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , {'output_hidden_states': True} ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , {'output_hidden_states': True} ) @require_torch class __lowerCamelCase ( unittest.TestCase , snake_case__): """simple docstring""" UpperCamelCase__ = (MaskFormerSwinBackbone,) if is_torch_available() else () UpperCamelCase__ = MaskFormerSwinConfig def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinModelTester(self ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = inputs_dict['pixel_values'].shape[0] for backbone_class in self.all_model_classes: _UpperCAmelCase = backbone_class(UpperCAmelCase ) backbone.to(UpperCAmelCase ) backbone.eval() _UpperCAmelCase = backbone(**UpperCAmelCase ) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , UpperCAmelCase ) self.assertTrue(len(outputs.feature_maps ) == len(backbone.channels ) ) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels ): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels) ) self.assertIsNone(outputs.hidden_states ) self.assertIsNone(outputs.attentions ) # Test output_hidden_states=True _UpperCAmelCase = backbone(**UpperCAmelCase , output_hidden_states=UpperCAmelCase ) self.assertIsNotNone(outputs.hidden_states ) self.assertTrue(len(outputs.hidden_states ) , len(backbone.stage_names ) ) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels ): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels) ) # Test output_attentions=True if self.has_attentions: _UpperCAmelCase = backbone(**UpperCAmelCase , output_attentions=UpperCAmelCase ) self.assertIsNotNone(outputs.attentions )
39
0
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging _A : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any] , A : AutoencoderKL , A : CLIPTextModel , A : CLIPTokenizer , A : UNetaDConditionModel , A : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , A : StableDiffusionSafetyChecker , A : CLIPImageProcessor , ) ->List[Any]: super().__init__() self.register_modules( vae=A , text_encoder=A , tokenizer=A , unet=A , scheduler=A , safety_checker=A , feature_extractor=A , ) def __lowerCamelCase ( self : Any , A : Optional[Union[str, int]] = "auto" ) ->Tuple: if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory lowerCamelCase__ : List[Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A ) def __lowerCamelCase ( self : List[Any] ) ->List[Any]: self.enable_attention_slicing(A ) @torch.no_grad() def __call__( self : Union[str, Any] , A : Union[str, List[str]] , A : int = 5_1_2 , A : int = 5_1_2 , A : int = 5_0 , A : float = 7.5 , A : Optional[Union[str, List[str]]] = None , A : Optional[int] = 1 , A : float = 0.0 , A : Optional[torch.Generator] = None , A : Optional[torch.FloatTensor] = None , A : Optional[str] = "pil" , A : bool = True , A : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , A : int = 1 , A : Optional[torch.FloatTensor] = None , **A : int , ) ->Tuple: if isinstance(A , A ): lowerCamelCase__ : str = 1 elif isinstance(A , A ): lowerCamelCase__ : Dict = len(A ) else: raise ValueError(F"`prompt` has to be of type `str` or `list` but is {type(A )}" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(A , A ) or callback_steps <= 0) ): raise ValueError( F"`callback_steps` has to be a positive integer but is {callback_steps} of type" F" {type(A )}." ) # get prompt text embeddings lowerCamelCase__ : Optional[Any] = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) lowerCamelCase__ : Dict = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: lowerCamelCase__ : Optional[Any] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' F" {self.tokenizer.model_max_length} tokens: {removed_text}" ) lowerCamelCase__ : Any = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: lowerCamelCase__ : str = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method lowerCamelCase__ : int = text_embeddings.shape lowerCamelCase__ : int = text_embeddings.repeat(1 , A , 1 ) lowerCamelCase__ : str = text_embeddings.view(bs_embed * num_images_per_prompt , A , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. lowerCamelCase__ : Optional[Any] = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: lowerCamelCase__ : List[str] if negative_prompt is None: lowerCamelCase__ : Optional[int] = [''''''] elif type(A ) is not type(A ): raise TypeError( F"`negative_prompt` should be the same type to `prompt`, but got {type(A )} !=" F" {type(A )}." ) elif isinstance(A , A ): lowerCamelCase__ : Optional[int] = [negative_prompt] elif batch_size != len(A ): raise ValueError( F"`negative_prompt`: {negative_prompt} has batch size {len(A )}, but `prompt`:" F" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" ''' the batch size of `prompt`.''' ) else: lowerCamelCase__ : List[Any] = negative_prompt lowerCamelCase__ : int = text_input_ids.shape[-1] lowerCamelCase__ : Optional[int] = self.tokenizer( A , padding='''max_length''' , max_length=A , truncation=A , return_tensors='''pt''' , ) lowerCamelCase__ : Optional[Any] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method lowerCamelCase__ : Any = uncond_embeddings.shape[1] lowerCamelCase__ : Union[str, Any] = uncond_embeddings.repeat(A , A , 1 ) lowerCamelCase__ : str = uncond_embeddings.view(batch_size * num_images_per_prompt , A , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes lowerCamelCase__ : str = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. lowerCamelCase__ : Dict = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) lowerCamelCase__ : List[Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 6_4, 6_4) lowerCamelCase__ : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps lowerCamelCase__ : Dict = torch.randn( A , generator=A , device='''cpu''' , dtype=A ).to(self.device ) lowerCamelCase__ : Dict = torch.randn(A , generator=A , device='''cpu''' , dtype=A ).to( self.device ) else: lowerCamelCase__ : Optional[Any] = torch.randn( A , generator=A , device=self.device , dtype=A ) lowerCamelCase__ : Union[str, Any] = torch.randn(A , generator=A , device=self.device , dtype=A ) else: if latents_reference.shape != latents_shape: raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) lowerCamelCase__ : Optional[Any] = latents_reference.to(self.device ) lowerCamelCase__ : Optional[int] = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images lowerCamelCase__ : List[str] = (latents_shape[3] - latents_shape_reference[3]) // 2 lowerCamelCase__ : List[Any] = (latents_shape[2] - latents_shape_reference[2]) // 2 lowerCamelCase__ : Dict = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx lowerCamelCase__ : str = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy lowerCamelCase__ : int = 0 if dx < 0 else dx lowerCamelCase__ : Optional[int] = 0 if dy < 0 else dy lowerCamelCase__ : Dict = max(-dx , 0 ) lowerCamelCase__ : int = max(-dy , 0 ) # import pdb # pdb.set_trace() lowerCamelCase__ : str = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(A ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand lowerCamelCase__ : Union[str, Any] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler lowerCamelCase__ : List[Any] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] lowerCamelCase__ : Union[str, Any] = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) lowerCamelCase__ : List[Any] = {} if accepts_eta: lowerCamelCase__ : Any = eta for i, t in enumerate(self.progress_bar(A ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ : Optional[int] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ : Any = self.scheduler.scale_model_input(A , A ) # predict the noise residual lowerCamelCase__ : Union[str, Any] = self.unet(A , A , encoder_hidden_states=A ).sample # perform guidance if do_classifier_free_guidance: lowerCamelCase__ : List[str] = noise_pred.chunk(2 ) lowerCamelCase__ : int = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 lowerCamelCase__ : Dict = self.scheduler.step(A , A , A , **A ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(A , A , A ) lowerCamelCase__ : Optional[Any] = 1 / 0.1_82_15 * latents lowerCamelCase__ : int = self.vae.decode(A ).sample lowerCamelCase__ : Dict = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 lowerCamelCase__ : List[str] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: lowerCamelCase__ : Tuple = self.feature_extractor(self.numpy_to_pil(A ) , return_tensors='''pt''' ).to( self.device ) lowerCamelCase__ : int = self.safety_checker( images=A , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: lowerCamelCase__ : List[Any] = None if output_type == "pil": lowerCamelCase__ : Optional[Any] = self.numpy_to_pil(A ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=A , nsfw_content_detected=A )
364
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 : Any = logging.get_logger(__name__) def _a ( UpperCAmelCase , UpperCAmelCase ) -> List[str]: """simple docstring""" def run_func(UpperCAmelCase ): @wraps(UpperCAmelCase ) def run_in_eager_mode(*UpperCAmelCase , **UpperCAmelCase ): return func(*UpperCAmelCase , **UpperCAmelCase ) @wraps(UpperCAmelCase ) @tf.function(experimental_compile=UpperCAmelCase ) def run_in_graph_mode(*UpperCAmelCase , **UpperCAmelCase ): return func(*UpperCAmelCase , **UpperCAmelCase ) 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 _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> ["tf.Tensor"]: """simple docstring""" lowerCamelCase__ : List[Any] = random.Random() lowerCamelCase__ : str = [rng.randint(0 , vocab_size - 1 ) for i in range(batch_size * sequence_length )] return tf.constant(UpperCAmelCase , shape=(batch_size, sequence_length) , dtype=tf.intaa ) class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): _UpperCAmelCase : TensorFlowBenchmarkArguments _UpperCAmelCase : PretrainedConfig _UpperCAmelCase : str = "TensorFlow" @property def __lowerCamelCase ( self : int ) ->Optional[int]: return tf.__version__ def __lowerCamelCase ( self : Optional[int] , A : str , A : int , A : int ) ->float: # initialize GPU on separate process lowerCamelCase__ : Dict = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) lowerCamelCase__ : int = self._prepare_inference_func(A , A , A ) return self._measure_speed(_inference ) def __lowerCamelCase ( self : str , A : str , A : int , A : int ) ->float: lowerCamelCase__ : Optional[int] = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) lowerCamelCase__ : List[Any] = self._prepare_train_func(A , A , A ) return self._measure_speed(_train ) def __lowerCamelCase ( self : int , A : str , A : int , A : int ) ->[Memory, Optional[MemorySummary]]: # initialize GPU on separate process if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , A ) lowerCamelCase__ : int = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) lowerCamelCase__ : str = self._prepare_inference_func(A , A , A ) return self._measure_memory(_inference ) def __lowerCamelCase ( self : List[str] , A : str , A : int , A : int ) ->[Memory, Optional[MemorySummary]]: if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , A ) lowerCamelCase__ : List[Any] = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''' ) lowerCamelCase__ : str = self._prepare_train_func(A , A , A ) return self._measure_memory(_train ) def __lowerCamelCase ( self : Dict , A : str , A : int , A : int ) ->Callable[[], None]: lowerCamelCase__ : Tuple = self.config_dict[model_name] if self.args.fpaa: raise NotImplementedError('''Mixed precision is currently not supported.''' ) lowerCamelCase__ : Tuple = ( hasattr(A , '''architectures''' ) and isinstance(config.architectures , A ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowerCamelCase__ : Any = '''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model lowerCamelCase__ : List[Any] = __import__('''transformers''' , fromlist=[model_class] ) lowerCamelCase__ : int = getattr(A , A ) lowerCamelCase__ : int = model_cls(A ) 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: lowerCamelCase__ : Union[str, Any] = TF_MODEL_MAPPING[config.__class__](A ) # encoder-decoder has vocab size saved differently lowerCamelCase__ : Tuple = config.vocab_size if hasattr(A , '''vocab_size''' ) else config.encoder.vocab_size lowerCamelCase__ : Optional[Any] = random_input_ids(A , A , A ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_forward(): return model(A , decoder_input_ids=A , training=A ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_forward(): return model(A , training=A ) lowerCamelCase__ : int = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward return _inference def __lowerCamelCase ( self : List[str] , A : str , A : int , A : int ) ->Callable[[], None]: lowerCamelCase__ : Tuple = 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.''' ) lowerCamelCase__ : Optional[int] = ( hasattr(A , '''architectures''' ) and isinstance(config.architectures , A ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowerCamelCase__ : Any = '''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model lowerCamelCase__ : List[str] = __import__('''transformers''' , fromlist=[model_class] ) lowerCamelCase__ : Optional[int] = getattr(A , A ) lowerCamelCase__ : Optional[Any] = model_cls(A ) 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: lowerCamelCase__ : List[str] = TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](A ) # encoder-decoder has vocab size saved differently lowerCamelCase__ : Optional[int] = config.vocab_size if hasattr(A , '''vocab_size''' ) else config.encoder.vocab_size lowerCamelCase__ : Dict = random_input_ids(A , A , A ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_train(): lowerCamelCase__ : int = model(A , decoder_input_ids=A , labels=A , training=A )[0] lowerCamelCase__ : List[Any] = tf.gradients(A , model.trainable_variables ) return gradients @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_train(): lowerCamelCase__ : Optional[int] = model(A , labels=A , training=A )[0] lowerCamelCase__ : List[str] = tf.gradients(A , model.trainable_variables ) return gradients lowerCamelCase__ : Tuple = encoder_decoder_train if config.is_encoder_decoder else encoder_train return _train def __lowerCamelCase ( self : Tuple , A : Any ) ->float: 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(A , 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 lowerCamelCase__ : Optional[Any] = timeit.repeat( A , repeat=self.args.repeat , number=1_0 , ) return min(A ) / 10.0 except ResourceExhaustedError as e: self.print_fn(F"Doesn't fit on GPU. {e}" ) def __lowerCamelCase ( self : List[Any] , A : Callable[[], None] ) ->[Memory, MemorySummary]: 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.''' ) lowerCamelCase__ : Union[str, Any] = 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.''' ) lowerCamelCase__ : Union[str, Any] = '''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() lowerCamelCase__ : Any = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx ) lowerCamelCase__ : Optional[int] = nvml.nvmlDeviceGetMemoryInfo(A ) lowerCamelCase__ : List[Any] = meminfo.used lowerCamelCase__ : Union[str, Any] = Memory(A ) # 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.''' ) lowerCamelCase__ : Tuple = None else: lowerCamelCase__ : Dict = measure_peak_memory_cpu(A ) lowerCamelCase__ : Optional[Any] = Memory(A ) if isinstance(A , A ) else memory_bytes if self.args.trace_memory_line_by_line: lowerCamelCase__ : Union[str, Any] = stop_memory_tracing(A ) if memory is None: lowerCamelCase__ : Dict = summary.total else: lowerCamelCase__ : Optional[int] = None return memory, summary except ResourceExhaustedError as e: self.print_fn(F"Doesn't fit on GPU. {e}" ) return "N/A", None
265
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class _lowerCamelCase( _a ): lowercase_ : int = """openai/whisper-base""" lowercase_ : Union[str, Any] = ( """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """ """transcribed text.""" ) lowercase_ : Any = """transcriber""" lowercase_ : List[Any] = WhisperProcessor lowercase_ : List[str] = WhisperForConditionalGeneration lowercase_ : Any = ["""audio"""] lowercase_ : Union[str, Any] = ["""text"""] def UpperCamelCase ( self, lowerCamelCase) -> Tuple: """simple docstring""" return self.pre_processor(lowerCamelCase, return_tensors='pt').input_features def UpperCamelCase ( self, lowerCamelCase) -> List[str]: """simple docstring""" return self.model.generate(inputs=lowerCamelCase) def UpperCamelCase ( self, lowerCamelCase) -> Optional[Any]: """simple docstring""" return self.pre_processor.batch_decode(lowerCamelCase, skip_special_tokens=lowerCamelCase)[0]
21
import collections import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_flax_cross_test, require_flax, require_torch, require_vision, slow, torch_device, ) from transformers.utils import is_flax_available, is_torch_available, is_vision_available from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_flax_bert import FlaxBertModelTester from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester from ..vit.test_modeling_flax_vit import FlaxViTModelTester if is_flax_available(): from transformers import ( FlaxBertModel, FlaxCLIPVisionModel, FlaxVisionTextDualEncoderModel, FlaxViTModel, VisionTextDualEncoderConfig, VisionTextDualEncoderProcessor, ) from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) if is_torch_available(): import torch from transformers import VisionTextDualEncoderModel if is_vision_available(): from PIL import Image def UpperCamelCase_( lowerCamelCase_ ) -> Optional[int]: if isinstance(lowerCamelCase_ , collections.abc.Iterable ): return x return (x, x) @require_flax class _lowerCamelCase: def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase) -> Union[str, Any]: """simple docstring""" pass def UpperCamelCase ( self) -> str: """simple docstring""" pass def UpperCamelCase ( self) -> Optional[int]: """simple docstring""" pass def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase) -> Any: """simple docstring""" _lowercase : str = np.abs((a - b)).max() self.assertLessEqual(lowerCamelCase, lowerCamelCase, F'''Difference between torch and flax is {diff} (>= {tol}).''') def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=None, **lowerCamelCase) -> Union[str, Any]: """simple docstring""" _lowercase : Any = VisionTextDualEncoderConfig.from_vision_text_configs(lowerCamelCase, lowerCamelCase) _lowercase : Optional[int] = FlaxVisionTextDualEncoderModel(lowerCamelCase) _lowercase : Any = model(input_ids=lowerCamelCase, pixel_values=lowerCamelCase, attention_mask=lowerCamelCase) self.assertEqual(output['text_embeds'].shape, (input_ids.shape[0], config.projection_dim)) self.assertEqual(output['image_embeds'].shape, (pixel_values.shape[0], config.projection_dim)) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=None, **lowerCamelCase) -> Any: """simple docstring""" _lowercase , _lowercase : Union[str, Any] = self.get_vision_text_model(lowerCamelCase, lowerCamelCase) _lowercase : str = {'vision_model': vision_model, 'text_model': text_model} _lowercase : Dict = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCamelCase) _lowercase : List[str] = model(input_ids=lowerCamelCase, pixel_values=lowerCamelCase, attention_mask=lowerCamelCase) self.assertEqual(output['text_embeds'].shape, (input_ids.shape[0], model.config.projection_dim)) self.assertEqual(output['image_embeds'].shape, (pixel_values.shape[0], model.config.projection_dim)) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=None, **lowerCamelCase) -> Union[str, Any]: """simple docstring""" _lowercase , _lowercase : Tuple = self.get_vision_text_model(lowerCamelCase, lowerCamelCase) _lowercase : List[str] = {'vision_model': vision_model, 'text_model': text_model} _lowercase : Dict = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCamelCase) _lowercase : List[str] = model(input_ids=lowerCamelCase, pixel_values=lowerCamelCase, attention_mask=lowerCamelCase) _lowercase : Tuple = output[0] with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCamelCase) _lowercase : Any = FlaxVisionTextDualEncoderModel.from_pretrained(lowerCamelCase) _lowercase : Tuple = model(input_ids=lowerCamelCase, pixel_values=lowerCamelCase, attention_mask=lowerCamelCase) _lowercase : str = after_output[0] _lowercase : Optional[Any] = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(lowerCamelCase, 1E-3) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=None, **lowerCamelCase) -> str: """simple docstring""" _lowercase , _lowercase : Any = self.get_vision_text_model(lowerCamelCase, lowerCamelCase) _lowercase : Optional[int] = {'vision_model': vision_model, 'text_model': text_model} _lowercase : Dict = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCamelCase) _lowercase : Tuple = model( input_ids=lowerCamelCase, pixel_values=lowerCamelCase, attention_mask=lowerCamelCase, output_attentions=lowerCamelCase) _lowercase : int = output.vision_model_output.attentions self.assertEqual(len(lowerCamelCase), vision_config.num_hidden_layers) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) _lowercase : Optional[Any] = to_atuple(vision_model.config.image_size) _lowercase : Any = to_atuple(vision_model.config.patch_size) _lowercase : Dict = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) _lowercase : Dict = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:], (vision_config.num_attention_heads, seq_len, seq_len)) _lowercase : List[str] = output.text_model_output.attentions self.assertEqual(len(lowerCamelCase), text_config.num_hidden_layers) self.assertEqual( text_attentions[0].shape[-3:], (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]), ) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase) -> Optional[int]: """simple docstring""" pt_model.to(lowerCamelCase) pt_model.eval() # prepare inputs _lowercase : Any = inputs_dict _lowercase : Optional[int] = {k: torch.tensor(v.tolist()) for k, v in flax_inputs.items()} with torch.no_grad(): _lowercase : Tuple = pt_model(**lowerCamelCase).to_tuple() _lowercase : Any = fx_model(**lowerCamelCase).to_tuple() self.assertEqual(len(lowerCamelCase), len(lowerCamelCase), 'Output lengths differ between Flax and PyTorch') for fx_output, pt_output in zip(fx_outputs[:4], pt_outputs[:4]): self.assert_almost_equals(lowerCamelCase, pt_output.numpy(), 4E-2) # PT -> Flax with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(lowerCamelCase) _lowercase : int = FlaxVisionTextDualEncoderModel.from_pretrained(lowerCamelCase, from_pt=lowerCamelCase) _lowercase : List[Any] = fx_model_loaded(**lowerCamelCase).to_tuple() self.assertEqual(len(lowerCamelCase), len(lowerCamelCase), 'Output lengths differ between Flax and PyTorch') for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4], pt_outputs[:4]): self.assert_almost_equals(lowerCamelCase, pt_output.numpy(), 4E-2) # Flax -> PT with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(lowerCamelCase) _lowercase : List[Any] = VisionTextDualEncoderModel.from_pretrained(lowerCamelCase, from_flax=lowerCamelCase) pt_model_loaded.to(lowerCamelCase) pt_model_loaded.eval() with torch.no_grad(): _lowercase : Optional[Any] = pt_model_loaded(**lowerCamelCase).to_tuple() self.assertEqual(len(lowerCamelCase), len(lowerCamelCase), 'Output lengths differ between Flax and PyTorch') for fx_output, pt_output_loaded in zip(fx_outputs[:4], pt_outputs_loaded[:4]): self.assert_almost_equals(lowerCamelCase, pt_output_loaded.numpy(), 4E-2) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase) -> Tuple: """simple docstring""" _lowercase : Dict = VisionTextDualEncoderConfig.from_vision_text_configs(lowerCamelCase, lowerCamelCase) _lowercase : Optional[Any] = VisionTextDualEncoderModel(lowerCamelCase) _lowercase : str = FlaxVisionTextDualEncoderModel(lowerCamelCase) _lowercase : Tuple = convert_pytorch_state_dict_to_flax(pt_model.state_dict(), lowerCamelCase) _lowercase : List[Any] = fx_state self.check_pt_flax_equivalence(lowerCamelCase, lowerCamelCase, lowerCamelCase) def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase, lowerCamelCase) -> Dict: """simple docstring""" _lowercase : str = VisionTextDualEncoderConfig.from_vision_text_configs(lowerCamelCase, lowerCamelCase) _lowercase : Tuple = VisionTextDualEncoderModel(lowerCamelCase) _lowercase : Optional[int] = FlaxVisionTextDualEncoderModel(lowerCamelCase) _lowercase : List[str] = load_flax_weights_in_pytorch_model(lowerCamelCase, fx_model.params) self.check_pt_flax_equivalence(lowerCamelCase, lowerCamelCase, lowerCamelCase) def UpperCamelCase ( self) -> List[Any]: """simple docstring""" _lowercase : int = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**lowerCamelCase) def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : List[str] = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**lowerCamelCase) def UpperCamelCase ( self) -> Union[str, Any]: """simple docstring""" _lowercase : Optional[int] = self.prepare_config_and_inputs() self.check_save_load(**lowerCamelCase) def UpperCamelCase ( self) -> str: """simple docstring""" _lowercase : str = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**lowerCamelCase) @is_pt_flax_cross_test def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : List[Any] = self.prepare_config_and_inputs() _lowercase : List[str] = config_inputs_dict.pop('vision_config') _lowercase : str = config_inputs_dict.pop('text_config') _lowercase : int = config_inputs_dict self.check_equivalence_pt_to_flax(lowerCamelCase, lowerCamelCase, lowerCamelCase) self.check_equivalence_flax_to_pt(lowerCamelCase, lowerCamelCase, lowerCamelCase) @slow def UpperCamelCase ( self) -> List[str]: """simple docstring""" _lowercase , _lowercase : Optional[Any] = self.get_pretrained_model_and_inputs() _lowercase : Optional[int] = model_a(**lowerCamelCase) _lowercase : Tuple = outputs[0] with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(lowerCamelCase) _lowercase : int = FlaxVisionTextDualEncoderModel.from_pretrained(lowerCamelCase) _lowercase : List[Any] = model_a(**lowerCamelCase) _lowercase : Tuple = after_outputs[0] _lowercase : Dict = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(lowerCamelCase, 1E-5) @require_flax class _lowerCamelCase( _a, unittest.TestCase ): def UpperCamelCase ( self) -> List[str]: """simple docstring""" _lowercase : Union[str, Any] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-vit', 'hf-internal-testing/tiny-bert', vision_from_pt=lowerCamelCase, text_from_pt=lowerCamelCase, ) _lowercase : List[Any] = 13 _lowercase : str = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ]) _lowercase : Tuple = ids_tensor([batch_size, 4], model.config.text_config.vocab_size) _lowercase : Union[str, Any] = random_attention_mask([batch_size, 4]) _lowercase : int = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase) -> Any: """simple docstring""" _lowercase : List[Any] = FlaxViTModel(lowerCamelCase) _lowercase : Optional[Any] = FlaxBertModel(lowerCamelCase) return vision_model, text_model def UpperCamelCase ( self) -> str: """simple docstring""" _lowercase : List[Any] = FlaxViTModelTester(self) _lowercase : Any = FlaxBertModelTester(self) _lowercase : Dict = vit_model_tester.prepare_config_and_inputs() _lowercase : Any = bert_model_tester.prepare_config_and_inputs() _lowercase , _lowercase : List[str] = vision_config_and_inputs _lowercase , _lowercase , _lowercase , _lowercase : Tuple = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_torch class _lowerCamelCase( _a, unittest.TestCase ): def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : str = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-clip', 'hf-internal-testing/tiny-bert', vision_from_pt=lowerCamelCase, text_from_pt=lowerCamelCase, ) _lowercase : Tuple = 13 _lowercase : Any = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ]) _lowercase : Union[str, Any] = ids_tensor([batch_size, 4], model.config.text_config.vocab_size) _lowercase : Any = random_attention_mask([batch_size, 4]) _lowercase : Dict = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase) -> Optional[int]: """simple docstring""" _lowercase : Any = FlaxCLIPVisionModel(lowerCamelCase) _lowercase : Optional[Any] = FlaxBertModel(lowerCamelCase) return vision_model, text_model def UpperCamelCase ( self) -> Dict: """simple docstring""" _lowercase : Tuple = FlaxCLIPVisionModelTester(self) _lowercase : Union[str, Any] = FlaxBertModelTester(self) _lowercase : Tuple = clip_model_tester.prepare_config_and_inputs() _lowercase : str = bert_model_tester.prepare_config_and_inputs() _lowercase , _lowercase : Dict = vision_config_and_inputs _lowercase , _lowercase , _lowercase , _lowercase : Optional[int] = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_flax @require_vision class _lowerCamelCase( unittest.TestCase ): @slow def UpperCamelCase ( self) -> Optional[int]: """simple docstring""" _lowercase : List[str] = FlaxVisionTextDualEncoderModel.from_pretrained('clip-italian/clip-italian', logit_scale_init_value=1.0) _lowercase : List[str] = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian') _lowercase : List[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') _lowercase : List[Any] = processor( text=['una foto di un gatto', 'una foto di un cane'], images=lowerCamelCase, padding=lowerCamelCase, return_tensors='np') _lowercase : List[Any] = model(**lowerCamelCase) # verify the logits self.assertEqual(outputs.logits_per_image.shape, (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])) self.assertEqual( outputs.logits_per_text.shape, (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]), ) _lowercase : Optional[int] = np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]]) self.assertTrue(np.allclose(outputs.logits_per_image, lowerCamelCase, atol=1E-3))
21
1
'''simple docstring''' 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. __UpperCAmelCase :List[str] = {"LayoutLMv2Config", "LayoutLMv3Config"} @is_pipeline_test class a ( unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING SCREAMING_SNAKE_CASE : Union[str, Any] = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: SCREAMING_SNAKE_CASE : Any = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: SCREAMING_SNAKE_CASE : Any = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } @require_torch def lowerCamelCase__ ( self : List[Any] ) -> Optional[Any]: __UpperCAmelCase : List[str] = pipeline( task='''text-classification''' , model='''hf-internal-testing/tiny-random-distilbert''' , framework='''pt''' ) __UpperCAmelCase : Dict = text_classifier('''This is great !''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''LABEL_0''', '''score''': 0.504}] ) __UpperCAmelCase : Optional[int] = text_classifier('''This is great !''' , top_k=2 ) self.assertEqual( nested_simplify(snake_case ) , [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}] ) __UpperCAmelCase : Optional[Any] = text_classifier(['''This is great !''', '''This is bad'''] , top_k=2 ) self.assertEqual( nested_simplify(snake_case ) , [ [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}], [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}], ] , ) __UpperCAmelCase : int = text_classifier('''This is great !''' , top_k=1 ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''LABEL_0''', '''score''': 0.504}] ) # Legacy behavior __UpperCAmelCase : Optional[int] = text_classifier('''This is great !''' , return_all_scores=snake_case ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''LABEL_0''', '''score''': 0.504}] ) __UpperCAmelCase : int = text_classifier('''This is great !''' , return_all_scores=snake_case ) self.assertEqual( nested_simplify(snake_case ) , [[{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}]] ) __UpperCAmelCase : Tuple = text_classifier(['''This is great !''', '''Something else'''] , return_all_scores=snake_case ) self.assertEqual( nested_simplify(snake_case ) , [ [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}], [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}], ] , ) __UpperCAmelCase : Optional[int] = text_classifier(['''This is great !''', '''Something else'''] , return_all_scores=snake_case ) self.assertEqual( nested_simplify(snake_case ) , [ {'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_0''', '''score''': 0.504}, ] , ) @require_torch def lowerCamelCase__ ( self : List[Any] ) -> Union[str, Any]: import torch __UpperCAmelCase : Optional[Any] = pipeline( task='''text-classification''' , model='''hf-internal-testing/tiny-random-distilbert''' , framework='''pt''' , device=torch.device('''cpu''' ) , ) __UpperCAmelCase : Any = text_classifier('''This is great !''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''LABEL_0''', '''score''': 0.504}] ) @require_tf def lowerCamelCase__ ( self : List[Any] ) -> Tuple: __UpperCAmelCase : List[Any] = pipeline( task='''text-classification''' , model='''hf-internal-testing/tiny-random-distilbert''' , framework='''tf''' ) __UpperCAmelCase : str = text_classifier('''This is great !''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''LABEL_0''', '''score''': 0.504}] ) @slow @require_torch def lowerCamelCase__ ( self : str ) -> Optional[Any]: __UpperCAmelCase : Tuple = pipeline('''text-classification''' ) __UpperCAmelCase : List[Any] = text_classifier('''This is great !''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''POSITIVE''', '''score''': 1.0}] ) __UpperCAmelCase : Tuple = text_classifier('''This is bad !''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''NEGATIVE''', '''score''': 1.0}] ) __UpperCAmelCase : Union[str, Any] = text_classifier('''Birds are a type of animal''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''POSITIVE''', '''score''': 0.988}] ) @slow @require_tf def lowerCamelCase__ ( self : Tuple ) -> str: __UpperCAmelCase : Optional[Any] = pipeline('''text-classification''' , framework='''tf''' ) __UpperCAmelCase : Any = text_classifier('''This is great !''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''POSITIVE''', '''score''': 1.0}] ) __UpperCAmelCase : Dict = text_classifier('''This is bad !''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''NEGATIVE''', '''score''': 1.0}] ) __UpperCAmelCase : Union[str, Any] = text_classifier('''Birds are a type of animal''' ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': '''POSITIVE''', '''score''': 0.988}] ) def lowerCamelCase__ ( self : List[Any] , snake_case : str , snake_case : Any , snake_case : Any ) -> Union[str, Any]: __UpperCAmelCase : str = TextClassificationPipeline(model=snake_case , tokenizer=snake_case ) return text_classifier, ["HuggingFace is in", "This is another test"] def lowerCamelCase__ ( self : int , snake_case : List[str] , snake_case : Any ) -> List[str]: __UpperCAmelCase : Optional[Any] = text_classifier.model # Small inputs because BartTokenizer tiny has maximum position embeddings = 22 __UpperCAmelCase : Tuple = '''HuggingFace is in''' __UpperCAmelCase : Union[str, Any] = text_classifier(snake_case ) self.assertEqual(nested_simplify(snake_case ) , [{'''label''': ANY(snake_case ), '''score''': ANY(snake_case )}] ) self.assertTrue(outputs[0]['''label'''] in model.config.idalabel.values() ) __UpperCAmelCase : Tuple = ['''HuggingFace is in ''', '''Paris is in France'''] __UpperCAmelCase : int = text_classifier(snake_case ) self.assertEqual( nested_simplify(snake_case ) , [{'''label''': ANY(snake_case ), '''score''': ANY(snake_case )}, {'''label''': ANY(snake_case ), '''score''': ANY(snake_case )}] , ) 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 __UpperCAmelCase : List[str] = text_classifier(snake_case , top_k=snake_case ) __UpperCAmelCase : str = len(model.config.idalabel.values() ) self.assertEqual( nested_simplify(snake_case ) , [[{'''label''': ANY(snake_case ), '''score''': ANY(snake_case )}] * N, [{'''label''': ANY(snake_case ), '''score''': ANY(snake_case )}] * N] , ) __UpperCAmelCase : Optional[int] = {'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''} __UpperCAmelCase : Union[str, Any] = text_classifier(snake_case ) self.assertEqual( nested_simplify(snake_case ) , {'''label''': ANY(snake_case ), '''score''': ANY(snake_case )} , ) 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. __UpperCAmelCase : Any = [['''HuggingFace is in ''', '''Paris is in France''']] with self.assertRaises(snake_case ): text_classifier(snake_case ) # This used to be valid for doing text pairs # We're keeping it working because of backward compatibility __UpperCAmelCase : str = text_classifier([[['''HuggingFace is in ''', '''Paris is in France''']]] ) self.assertEqual( nested_simplify(snake_case ) , [{'''label''': ANY(snake_case ), '''score''': ANY(snake_case )}] , ) self.assertTrue(outputs[0]['''label'''] in model.config.idalabel.values() )
240
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter __UpperCAmelCase :Tuple = "Create a default config file for Accelerate with only a few flags set." def _a ( _lowercase : List[Any]="no" , _lowercase : str = default_json_config_file , _lowercase : bool = False ): '''simple docstring''' __UpperCAmelCase : Dict = Path(_lowercase ) path.parent.mkdir(parents=_lowercase , exist_ok=_lowercase ) if path.exists(): print( F'Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.' ) return False __UpperCAmelCase : List[str] = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( F'`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}' ) __UpperCAmelCase : int = { '''compute_environment''': '''LOCAL_MACHINE''', '''mixed_precision''': mixed_precision, } if torch.cuda.is_available(): __UpperCAmelCase : Optional[Any] = torch.cuda.device_count() __UpperCAmelCase : List[str] = num_gpus __UpperCAmelCase : int = False if num_gpus > 1: __UpperCAmelCase : Any = '''MULTI_GPU''' else: __UpperCAmelCase : int = '''NO''' elif is_xpu_available() and use_xpu: __UpperCAmelCase : List[Any] = torch.xpu.device_count() __UpperCAmelCase : List[Any] = num_xpus __UpperCAmelCase : Optional[int] = False if num_xpus > 1: __UpperCAmelCase : Any = '''MULTI_XPU''' else: __UpperCAmelCase : Optional[Any] = '''NO''' elif is_npu_available(): __UpperCAmelCase : Dict = torch.npu.device_count() __UpperCAmelCase : Any = num_npus __UpperCAmelCase : Any = False if num_npus > 1: __UpperCAmelCase : Dict = '''MULTI_NPU''' else: __UpperCAmelCase : Optional[int] = '''NO''' else: __UpperCAmelCase : List[str] = 0 __UpperCAmelCase : Dict = True __UpperCAmelCase : Dict = 1 __UpperCAmelCase : Tuple = '''NO''' __UpperCAmelCase : List[Any] = ClusterConfig(**_lowercase ) config.to_json_file(_lowercase ) return path def _a ( _lowercase : Union[str, Any] , _lowercase : str ): '''simple docstring''' __UpperCAmelCase : Optional[int] = parser.add_parser('''default''' , parents=_lowercase , help=_lowercase , formatter_class=_lowercase ) parser.add_argument( '''--config_file''' , default=_lowercase , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , dest='''save_location''' , ) parser.add_argument( '''--mixed_precision''' , choices=['''no''', '''fp16''', '''bf16'''] , type=_lowercase , help='''Whether or not to use mixed precision training. ''' '''Choose between FP16 and BF16 (bfloat16) training. ''' '''BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.''' , default='''no''' , ) parser.set_defaults(func=_lowercase ) return parser def _a ( _lowercase : List[Any] ): '''simple docstring''' __UpperCAmelCase : str = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(F'accelerate configuration saved at {config_file}' )
240
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { 'tiiuae/falcon-40b': 'https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json', 'tiiuae/falcon-7b': 'https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json', } class __A( a ): snake_case_ = '''falcon''' snake_case_ = ['''past_key_values'''] def __init__( self , _snake_case=65_024 , _snake_case=4_544 , _snake_case=32 , _snake_case=71 , _snake_case=1E-5 , _snake_case=0.02 , _snake_case=True , _snake_case=0.0 , _snake_case=0.0 , _snake_case=None , _snake_case=False , _snake_case=False , _snake_case=True , _snake_case=True , _snake_case=False , _snake_case=11 , _snake_case=11 , **_snake_case , ) -> List[Any]: '''simple docstring''' __a = vocab_size # Backward compatibility with n_embed kwarg __a = kwargs.pop('''n_embed''' , _snake_case ) __a = hidden_size if n_embed is None else n_embed __a = num_hidden_layers __a = num_attention_heads __a = layer_norm_epsilon __a = initializer_range __a = use_cache __a = hidden_dropout __a = attention_dropout __a = bos_token_id __a = eos_token_id __a = num_attention_heads if num_kv_heads is None else num_kv_heads __a = alibi __a = new_decoder_architecture __a = multi_query # Ignored when new_decoder_architecture is True __a = parallel_attn __a = bias super().__init__(bos_token_id=_snake_case , eos_token_id=_snake_case , **_snake_case ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Tuple: '''simple docstring''' return self.hidden_size // self.num_attention_heads @property def SCREAMING_SNAKE_CASE_ ( self ) -> List[str]: '''simple docstring''' return not self.alibi
6
"""simple docstring""" import re def A_ ( _lowercase ): '''simple docstring''' snake_case_ :Optional[int] = re.compile( r"""^(?:0|94|\+94|0{2}94)""" r"""7(0|1|2|4|5|6|7|8)""" r"""(-| |)""" r"""\d{7}$""" ) return bool(re.search(_lowercase, _lowercase ) ) if __name__ == "__main__": __a = "0094702343221" print(is_sri_lankan_phone_number(phone))
66
0
'''simple docstring''' import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def UpperCAmelCase ( *lowerCamelCase_ :Dict ): '''simple docstring''' if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): snake_case_ : Union[str, Any] = list(lowerCamelCase_ ) for i in range(len(lowerCamelCase_ ) ): snake_case_ : int = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def UpperCAmelCase ( lowerCamelCase_ :Exception ): '''simple docstring''' snake_case_ : Any = [ """CUDA out of memory.""", # CUDA OOM """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""", # CUDNN SNAFU """DefaultCPUAllocator: can't allocate memory""", # CPU OOM ] if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def UpperCAmelCase ( lowerCamelCase_ :callable = None , lowerCamelCase_ :int = 1_28 ): '''simple docstring''' if function is None: return functools.partial(lowerCamelCase_ , starting_batch_size=lowerCamelCase_ ) snake_case_ : Union[str, Any] = starting_batch_size def decorator(*lowerCamelCase_ :List[Any] , **lowerCamelCase_ :List[str] ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() snake_case_ : Dict = list(inspect.signature(lowerCamelCase_ ).parameters.keys() ) # Guard against user error if len(lowerCamelCase_ ) < (len(lowerCamelCase_ ) + 1): snake_case_ : Any = """, """.join([F'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:] )] ) raise TypeError( F'''Batch size was passed into `{function.__name__}` as the first argument when called.''' F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError("""No executable batch size found, reached zero.""" ) try: return function(lowerCamelCase_ , *lowerCamelCase_ , **lowerCamelCase_ ) except Exception as e: if should_reduce_batch_size(lowerCamelCase_ ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
8
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __A : List[Any] = logging.get_logger(__name__) __A : str = { 'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json', # See all CANINE models at https://huggingface.co/models?filter=canine } class __UpperCamelCase ( lowercase__ ): lowercase : List[Any] = 'canine' def __init__( self :Optional[int] ,_UpperCamelCase :Dict=7_6_8 ,_UpperCamelCase :Union[str, Any]=1_2 ,_UpperCamelCase :int=1_2 ,_UpperCamelCase :int=3_0_7_2 ,_UpperCamelCase :int="gelu" ,_UpperCamelCase :Any=0.1 ,_UpperCamelCase :int=0.1 ,_UpperCamelCase :Any=1_6_3_8_4 ,_UpperCamelCase :Tuple=1_6 ,_UpperCamelCase :List[str]=0.02 ,_UpperCamelCase :Any=1E-1_2 ,_UpperCamelCase :Tuple=0 ,_UpperCamelCase :List[str]=0xE_0_0_0 ,_UpperCamelCase :Optional[Any]=0xE_0_0_1 ,_UpperCamelCase :str=4 ,_UpperCamelCase :Optional[int]=4 ,_UpperCamelCase :str=8 ,_UpperCamelCase :int=1_6_3_8_4 ,_UpperCamelCase :int=1_2_8 ,**_UpperCamelCase :str ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : List[str] = max_position_embeddings snake_case_ : Union[str, Any] = hidden_size snake_case_ : Dict = num_hidden_layers snake_case_ : Optional[int] = num_attention_heads snake_case_ : Tuple = intermediate_size snake_case_ : str = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Dict = attention_probs_dropout_prob snake_case_ : Optional[Any] = initializer_range snake_case_ : Optional[int] = type_vocab_size snake_case_ : List[str] = layer_norm_eps # Character config: snake_case_ : Any = downsampling_rate snake_case_ : List[str] = upsampling_kernel_size snake_case_ : int = num_hash_functions snake_case_ : Tuple = num_hash_buckets snake_case_ : Tuple = local_transformer_stride
8
1
'''simple docstring''' import operator as op def __UpperCamelCase ( UpperCAmelCase ): lowercase__ : List[str] = [] lowercase__ : Tuple = lambda UpperCAmelCase , UpperCAmelCase : int(x / y ) # noqa: E731 integer division operation lowercase__ : List[str] = { '''^''': op.pow, '''*''': op.mul, '''/''': div, '''+''': op.add, '''-''': op.sub, } # operators & their respective operation # print table header print('''Symbol'''.center(8 ) , '''Action'''.center(12 ) , '''Stack''' , sep=''' | ''' ) print('''-''' * (30 + len(UpperCAmelCase )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(UpperCAmelCase ) # append x to stack # output in tabular format print(x.rjust(8 ) , ('''push(''' + x + ''')''').ljust(12 ) , ''','''.join(UpperCAmelCase ) , sep=''' | ''' ) else: lowercase__ : Tuple = stack.pop() # pop stack # output in tabular format print(''''''.rjust(8 ) , ('''pop(''' + b + ''')''').ljust(12 ) , ''','''.join(UpperCAmelCase ) , sep=''' | ''' ) lowercase__ : int = stack.pop() # pop stack # output in tabular format print(''''''.rjust(8 ) , ('''pop(''' + a + ''')''').ljust(12 ) , ''','''.join(UpperCAmelCase ) , sep=''' | ''' ) stack.append( str(opr[x](int(UpperCAmelCase ) , int(UpperCAmelCase ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ('''push(''' + a + x + b + ''')''').ljust(12 ) , ''','''.join(UpperCAmelCase ) , sep=''' | ''' , ) return int(stack[0] ) if __name__ == "__main__": __a: str = input("""\n\nEnter a Postfix Equation (space separated) = """).split(""" """) print("""\n\tResult = """, solve(Postfix))
198
'''simple docstring''' import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing the experiment tracking capability, # and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## __a: Optional[Any] = 16 __a: Any = 32 def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase = 16 ): lowercase__ : Dict = AutoTokenizer.from_pretrained('''bert-base-cased''' ) lowercase__ : Optional[int] = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) lowercase__ : Optional[Any] = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=UpperCAmelCase , max_length=UpperCAmelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): lowercase__ : List[Any] = datasets.map( UpperCAmelCase , batched=UpperCAmelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library lowercase__ : str = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. lowercase__ : List[str] = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": lowercase__ : List[str] = 16 elif accelerator.mixed_precision != "no": lowercase__ : Dict = 8 else: lowercase__ : Optional[int] = None return tokenizer.pad( UpperCAmelCase , padding='''longest''' , max_length=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. lowercase__ : str = DataLoader( tokenized_datasets['''train'''] , shuffle=UpperCAmelCase , collate_fn=UpperCAmelCase , batch_size=UpperCAmelCase ) lowercase__ : Optional[int] = DataLoader( tokenized_datasets['''validation'''] , shuffle=UpperCAmelCase , collate_fn=UpperCAmelCase , batch_size=UpperCAmelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders __a: Tuple = mocked_dataloaders # noqa: F811 def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ): # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , UpperCAmelCase ) == "1": lowercase__ : Optional[int] = 2 # Initialize Accelerator # New Code # # We pass in "all" to `log_with` to grab all available trackers in the environment # Note: If using a custom `Tracker` class, should be passed in here such as: # >>> log_with = ["all", MyCustomTrackerClassInstance()] if args.with_tracking: lowercase__ : Union[str, Any] = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with='''all''' , project_dir=args.project_dir ) else: lowercase__ : Any = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowercase__ : int = config['''lr'''] lowercase__ : Optional[int] = int(config['''num_epochs'''] ) lowercase__ : Optional[Any] = int(config['''seed'''] ) lowercase__ : int = int(config['''batch_size'''] ) set_seed(UpperCAmelCase ) lowercase__ , lowercase__ : str = get_dataloaders(UpperCAmelCase , UpperCAmelCase ) lowercase__ : Union[str, Any] = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation lowercase__ : Any = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: lowercase__ : Union[str, Any] = batch_size // MAX_GPU_BATCH_SIZE lowercase__ : Any = MAX_GPU_BATCH_SIZE # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowercase__ : Optional[int] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=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 ) # Instantiate optimizer lowercase__ : List[Any] = AdamW(params=model.parameters() , lr=UpperCAmelCase ) # Instantiate scheduler lowercase__ : List[str] = get_linear_schedule_with_warmup( optimizer=UpperCAmelCase , num_warmup_steps=100 , num_training_steps=(len(UpperCAmelCase ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ : List[str] = accelerator.prepare( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # New Code # # We need to initialize the trackers we use. Overall configurations can also be stored if args.with_tracking: lowercase__ : Optional[Any] = os.path.split(UpperCAmelCase )[-1].split('''.''' )[0] accelerator.init_trackers(UpperCAmelCase , UpperCAmelCase ) # Now we train the model for epoch in range(UpperCAmelCase ): model.train() # New Code # # For our tracking example, we will log the total loss of each epoch if args.with_tracking: lowercase__ : str = 0 for step, batch in enumerate(UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowercase__ : List[str] = model(**UpperCAmelCase ) lowercase__ : List[str] = outputs.loss # New Code # if args.with_tracking: total_loss += loss.detach().float() lowercase__ : List[str] = loss / gradient_accumulation_steps accelerator.backward(UpperCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(UpperCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True` (the default). batch.to(accelerator.device ) with torch.no_grad(): lowercase__ : List[str] = model(**UpperCAmelCase ) lowercase__ : Optional[int] = outputs.logits.argmax(dim=-1 ) lowercase__ , lowercase__ : Optional[int] = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=UpperCAmelCase , references=UpperCAmelCase , ) lowercase__ : int = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , UpperCAmelCase ) # New Code # # To actually log, we call `Accelerator.log` # The values passed can be of `str`, `int`, `float` or `dict` of `str` to `float`/`int` if args.with_tracking: accelerator.log( { '''accuracy''': eval_metric['''accuracy'''], '''f1''': eval_metric['''f1'''], '''train_loss''': total_loss.item() / len(UpperCAmelCase ), '''epoch''': epoch, } , step=UpperCAmelCase , ) # New Code # # When a run is finished, you should call `accelerator.end_training()` # to close all of the open trackers if args.with_tracking: accelerator.end_training() def __UpperCamelCase ( ): lowercase__ : Any = argparse.ArgumentParser(description='''Simple example of training script.''' ) 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( '''--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__ : str = parser.parse_args() lowercase__ : Tuple = {'''lr''': 2E-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(UpperCAmelCase , UpperCAmelCase ) if __name__ == "__main__": main()
198
1
'''simple docstring''' from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) UpperCamelCase_ = logging.get_logger(__name__) # pylint: disable=invalid-name UpperCamelCase_ = "\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n >>> from diffusers.utils import load_image\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16\n ... )\n >>> pipe_prior.to(\"cuda\")\n\n >>> prompt = \"A red cartoon frog, 4k\"\n >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-decoder\", torch_dtype=torch.float16\n ... )\n >>> pipe.to(\"cuda\")\n\n >>> init_image = load_image(\n ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\"\n ... \"/kandinsky/frog.png\"\n ... )\n\n >>> image = pipe(\n ... image=init_image,\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... strength=0.2,\n ... ).images\n\n >>> image[0].save(\"red_frog.png\")\n ```\n" def lowercase__( __UpperCamelCase: str ,__UpperCamelCase: int ,__UpperCamelCase: List[Any]=8 ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 SCREAMING_SNAKE_CASE : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def lowercase__( __UpperCamelCase: List[str] ,__UpperCamelCase: str=5_12 ,__UpperCamelCase: Dict=5_12 ): """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = pil_image.resize((w, h) ,resample=Image.BICUBIC ,reducing_gap=1 ) SCREAMING_SNAKE_CASE : int = np.array(pil_image.convert('RGB' ) ) SCREAMING_SNAKE_CASE : Dict = arr.astype(np.floataa ) / 1_2_7.5 - 1 SCREAMING_SNAKE_CASE : Union[str, Any] = np.transpose(__UpperCamelCase ,[2, 0, 1] ) SCREAMING_SNAKE_CASE : List[Any] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ) return image class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self, A, A, A, ): '''simple docstring''' super().__init__() self.register_modules( unet=A, scheduler=A, movq=A, ) SCREAMING_SNAKE_CASE : Dict = 2 ** (len(self.movq.config.block_out_channels ) - 1) def UpperCamelCase_ ( self, A, A, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = min(int(num_inference_steps * strength ), A ) SCREAMING_SNAKE_CASE : Union[str, Any] = max(num_inference_steps - init_timestep, 0 ) SCREAMING_SNAKE_CASE : Any = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def UpperCamelCase_ ( self, A, A, A, A, A, A, A=None ): '''simple docstring''' if not isinstance(A, (torch.Tensor, PIL.Image.Image, list) ): raise ValueError( F"`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(A )}" ) SCREAMING_SNAKE_CASE : Any = image.to(device=A, dtype=A ) SCREAMING_SNAKE_CASE : int = batch_size * num_images_per_prompt if image.shape[1] == 4: SCREAMING_SNAKE_CASE : str = image else: if isinstance(A, A ) and len(A ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(A )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) elif isinstance(A, A ): SCREAMING_SNAKE_CASE : int = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(A ) ] SCREAMING_SNAKE_CASE : Dict = torch.cat(A, dim=0 ) else: SCREAMING_SNAKE_CASE : List[str] = self.movq.encode(A ).latent_dist.sample(A ) SCREAMING_SNAKE_CASE : List[Any] = self.movq.config.scaling_factor * init_latents SCREAMING_SNAKE_CASE : int = torch.cat([init_latents], dim=0 ) SCREAMING_SNAKE_CASE : Dict = init_latents.shape SCREAMING_SNAKE_CASE : str = randn_tensor(A, generator=A, device=A, dtype=A ) # get latents SCREAMING_SNAKE_CASE : Union[str, Any] = self.scheduler.add_noise(A, A, A ) SCREAMING_SNAKE_CASE : Tuple = init_latents return latents def UpperCamelCase_ ( self, A=0 ): '''simple docstring''' if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) SCREAMING_SNAKE_CASE : Dict = torch.device(F"cuda:{gpu_id}" ) SCREAMING_SNAKE_CASE : Any = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(A, A ) def UpperCamelCase_ ( self, A=0 ): '''simple docstring''' if is_accelerate_available() and is_accelerate_version('>=', '0.17.0.dev0' ): from accelerate import cpu_offload_with_hook else: raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.' ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to('cpu', silence_dtype_warnings=A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) SCREAMING_SNAKE_CASE : int = None for cpu_offloaded_model in [self.unet, self.movq]: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = cpu_offload_with_hook(A, A, prev_module_hook=A ) # We'll offload the last model manually. SCREAMING_SNAKE_CASE : Any = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCamelCase_ ( self ): '''simple docstring''' if not hasattr(self.unet, '_hf_hook' ): return self.device for module in self.unet.modules(): if ( hasattr(A, '_hf_hook' ) and hasattr(module._hf_hook, 'execution_device' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(A ) def __call__( self, A, A, A, A = 512, A = 512, A = 100, A = 4.0, A = 0.3, A = 1, A = None, A = "pil", A = True, ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._execution_device SCREAMING_SNAKE_CASE : Dict = guidance_scale > 1.0 if isinstance(A, A ): SCREAMING_SNAKE_CASE : Optional[int] = torch.cat(A, dim=0 ) SCREAMING_SNAKE_CASE : int = image_embeds.shape[0] if isinstance(A, A ): SCREAMING_SNAKE_CASE : Dict = torch.cat(A, dim=0 ) if do_classifier_free_guidance: SCREAMING_SNAKE_CASE : Tuple = image_embeds.repeat_interleave(A, dim=0 ) SCREAMING_SNAKE_CASE : Any = negative_image_embeds.repeat_interleave(A, dim=0 ) SCREAMING_SNAKE_CASE : Dict = torch.cat([negative_image_embeds, image_embeds], dim=0 ).to(dtype=self.unet.dtype, device=A ) if not isinstance(A, A ): SCREAMING_SNAKE_CASE : int = [image] if not all(isinstance(A, (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( F"Input is in incorrect format: {[type(A ) for i in image]}. Currently, we only support PIL image and pytorch tensor" ) SCREAMING_SNAKE_CASE : Optional[int] = torch.cat([prepare_image(A, A, A ) for i in image], dim=0 ) SCREAMING_SNAKE_CASE : Any = image.to(dtype=image_embeds.dtype, device=A ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.movq.encode(A )['latents'] SCREAMING_SNAKE_CASE : Union[str, Any] = latents.repeat_interleave(A, dim=0 ) self.scheduler.set_timesteps(A, device=A ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = self.get_timesteps(A, A, A ) SCREAMING_SNAKE_CASE : int = timesteps[:1].repeat(batch_size * num_images_per_prompt ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = downscale_height_and_width(A, A, self.movq_scale_factor ) SCREAMING_SNAKE_CASE : int = self.prepare_latents( A, A, A, A, image_embeds.dtype, A, A ) for i, t in enumerate(self.progress_bar(A ) ): # expand the latents if we are doing classifier free guidance SCREAMING_SNAKE_CASE : List[Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents SCREAMING_SNAKE_CASE : str = {'image_embeds': image_embeds} SCREAMING_SNAKE_CASE : Tuple = self.unet( sample=A, timestep=A, encoder_hidden_states=A, added_cond_kwargs=A, return_dict=A, )[0] if do_classifier_free_guidance: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = noise_pred.split(latents.shape[1], dim=1 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = noise_pred.chunk(2 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = variance_pred.chunk(2 ) SCREAMING_SNAKE_CASE : int = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat([noise_pred, variance_pred_text], dim=1 ) if not ( hasattr(self.scheduler.config, 'variance_type' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = noise_pred.split(latents.shape[1], dim=1 ) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE : List[str] = self.scheduler.step( A, A, A, generator=A, )[0] # post-processing SCREAMING_SNAKE_CASE : Optional[int] = self.movq.decode(A, force_not_quantize=A )['sample'] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: SCREAMING_SNAKE_CASE : Tuple = image * 0.5 + 0.5 SCREAMING_SNAKE_CASE : Tuple = image.clamp(0, 1 ) SCREAMING_SNAKE_CASE : int = image.cpu().permute(0, 2, 3, 1 ).float().numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : str = self.numpy_to_pil(A ) if not return_dict: return (image,) return ImagePipelineOutput(images=A )
246
'''simple docstring''' from __future__ import annotations import unittest from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available from transformers.testing_utils import 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, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel @require_tf class _a : '''simple docstring''' A : Tuple = BlenderbotSmallConfig A : Optional[int] = {} A : Any = '''gelu''' def __init__( self, A, A=13, A=7, A=True, A=False, A=99, A=32, A=2, A=4, A=37, A=0.1, A=0.1, A=20, A=2, A=1, A=0, ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : Optional[int] = seq_length SCREAMING_SNAKE_CASE : Dict = is_training SCREAMING_SNAKE_CASE : Optional[int] = use_labels SCREAMING_SNAKE_CASE : List[str] = vocab_size SCREAMING_SNAKE_CASE : Dict = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Tuple = num_attention_heads SCREAMING_SNAKE_CASE : Dict = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Any = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[int] = eos_token_id SCREAMING_SNAKE_CASE : List[str] = pad_token_id SCREAMING_SNAKE_CASE : Union[str, Any] = bos_token_id def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length - 1], self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ), 1 ) SCREAMING_SNAKE_CASE : str = tf.concat([input_ids, eos_tensor], axis=1 ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) SCREAMING_SNAKE_CASE : Union[str, Any] = 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 : List[str] = prepare_blenderbot_small_inputs_dict(A, A, A ) return config, inputs_dict def UpperCamelCase_ ( self, A, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = TFBlenderbotSmallModel(config=A ).get_decoder() SCREAMING_SNAKE_CASE : Optional[int] = inputs_dict['input_ids'] SCREAMING_SNAKE_CASE : List[Any] = input_ids[:1, :] SCREAMING_SNAKE_CASE : List[Any] = inputs_dict['attention_mask'][:1, :] SCREAMING_SNAKE_CASE : Dict = inputs_dict['head_mask'] SCREAMING_SNAKE_CASE : int = 1 # first forward pass SCREAMING_SNAKE_CASE : Union[str, Any] = model(A, attention_mask=A, head_mask=A, use_cache=A ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE : List[Any] = ids_tensor((self.batch_size, 3), config.vocab_size ) SCREAMING_SNAKE_CASE : Tuple = tf.cast(ids_tensor((self.batch_size, 3), 2 ), tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE : Union[str, Any] = tf.concat([input_ids, next_tokens], axis=-1 ) SCREAMING_SNAKE_CASE : str = tf.concat([attention_mask, next_attn_mask], axis=-1 ) SCREAMING_SNAKE_CASE : Any = model(A, attention_mask=A )[0] SCREAMING_SNAKE_CASE : List[str] = model(A, attention_mask=A, past_key_values=A )[0] self.parent.assertEqual(next_tokens.shape[1], output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE : str = int(ids_tensor((1,), output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE : List[str] = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE : str = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(A, A, rtol=1E-3 ) def lowercase__( __UpperCamelCase: int ,__UpperCamelCase: Optional[int] ,__UpperCamelCase: int ,__UpperCamelCase: Optional[Any]=None ,__UpperCamelCase: List[str]=None ,__UpperCamelCase: int=None ,__UpperCamelCase: Any=None ,__UpperCamelCase: Union[str, Any]=None ,): """simple docstring""" if attention_mask is None: SCREAMING_SNAKE_CASE : Optional[Any] = tf.cast(tf.math.not_equal(__UpperCamelCase ,config.pad_token_id ) ,tf.inta ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE : Any = 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 : List[str] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE : Dict = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE : Optional[Any] = 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 _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' A : List[str] = ( (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else () ) A : List[str] = (TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else () A : List[str] = ( { '''conversational''': TFBlenderbotSmallForConditionalGeneration, '''feature-extraction''': TFBlenderbotSmallModel, '''summarization''': TFBlenderbotSmallForConditionalGeneration, '''text2text-generation''': TFBlenderbotSmallForConditionalGeneration, '''translation''': TFBlenderbotSmallForConditionalGeneration, } if is_tf_available() else {} ) A : int = True A : Optional[int] = False A : str = False def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = TFBlenderbotSmallModelTester(self ) SCREAMING_SNAKE_CASE : str = ConfigTester(self, config_class=A ) def UpperCamelCase_ ( self ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*A ) @require_tokenizers @require_tf class _a ( unittest.TestCase ): '''simple docstring''' A : Union[str, Any] = [ '''Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like ''' ''' i\'m going to throw up.\nand why is that?''' ] A : List[Any] = '''facebook/blenderbot_small-90M''' @cached_property def UpperCamelCase_ ( self ): '''simple docstring''' return BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M' ) @cached_property def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.tokenizer(self.src_text, return_tensors='tf' ) SCREAMING_SNAKE_CASE : int = self.model.generate( model_inputs.input_ids, attention_mask=model_inputs.attention_mask, num_beams=2, use_cache=A, ) SCREAMING_SNAKE_CASE : Optional[int] = self.tokenizer.batch_decode(generated_ids.numpy(), skip_special_tokens=A )[0] assert generated_words in ( "i don't know. i just feel like i'm going to throw up. it's not fun.", "i'm not sure. i just feel like i've been feeling like i have to be in a certain place", "i'm not sure. i just feel like i've been in a bad situation.", )
246
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ : Union[str, Any] = logging.get_logger(__name__) lowercase_ : Tuple = { """facebook/levit-128S""": """https://huggingface.co/facebook/levit-128S/resolve/main/config.json""", # See all LeViT models at https://huggingface.co/models?filter=levit } class __lowerCAmelCase ( UpperCAmelCase__ ): snake_case_ : Optional[int] = "levit" def __init__( self : int , snake_case__ : Tuple=224 , snake_case__ : Optional[int]=3 , snake_case__ : Any=3 , snake_case__ : Union[str, Any]=2 , snake_case__ : Dict=1 , snake_case__ : int=16 , snake_case__ : List[str]=[128, 256, 384] , snake_case__ : Optional[int]=[4, 8, 12] , snake_case__ : Dict=[4, 4, 4] , snake_case__ : str=[16, 16, 16] , snake_case__ : Tuple=0 , snake_case__ : List[str]=[2, 2, 2] , snake_case__ : int=[2, 2, 2] , snake_case__ : Dict=0.02 , **snake_case__ : Tuple , ): """simple docstring""" super().__init__(**snake_case__ ) _UpperCAmelCase = image_size _UpperCAmelCase = num_channels _UpperCAmelCase = kernel_size _UpperCAmelCase = stride _UpperCAmelCase = padding _UpperCAmelCase = hidden_sizes _UpperCAmelCase = num_attention_heads _UpperCAmelCase = depths _UpperCAmelCase = key_dim _UpperCAmelCase = drop_path_rate _UpperCAmelCase = patch_size _UpperCAmelCase = attention_ratio _UpperCAmelCase = mlp_ratio _UpperCAmelCase = initializer_range _UpperCAmelCase = [ ["""Subsample""", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ["""Subsample""", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __lowerCAmelCase ( UpperCAmelCase__ ): snake_case_ : Tuple = version.parse("1.11" ) @property def UpperCamelCase ( self : Dict ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCamelCase ( self : List[Any] ): """simple docstring""" return 1e-4
133
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCamelCase_ ( __magic_name__ , unittest.TestCase ): lowercase = LongformerTokenizer lowercase = True lowercase = LongformerTokenizerFast lowercase = True def _lowercase( self ) -> List[Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCAmelCase : List[str] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] UpperCAmelCase : int = dict(zip(A , range(len(A ) ) ) ) UpperCAmelCase : Any = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] UpperCAmelCase : Dict = {"""unk_token""": """<unk>"""} UpperCAmelCase : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) UpperCAmelCase : Optional[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(A ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(A ) ) def _lowercase( self , **A ) -> Optional[Any]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **A ) def _lowercase( self , **A ) -> int: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **A ) def _lowercase( self , A ) -> Optional[int]: UpperCAmelCase : Optional[Any] = """lower newer""" UpperCAmelCase : Optional[int] = """lower newer""" return input_text, output_text def _lowercase( self ) -> Optional[Any]: UpperCAmelCase : Tuple = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) UpperCAmelCase : Dict = """lower newer""" UpperCAmelCase : int = ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] UpperCAmelCase : Tuple = tokenizer.tokenize(A ) # , add_prefix_space=True) self.assertListEqual(A , A ) UpperCAmelCase : Any = tokens + [tokenizer.unk_token] UpperCAmelCase : Tuple = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A ) def _lowercase( self ) -> Union[str, Any]: UpperCAmelCase : str = self.get_tokenizer() self.assertListEqual(tokenizer.encode("""Hello world!""" , add_special_tokens=A ) , [0, 31414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode("""Hello world! cécé herlolip 418""" , add_special_tokens=A ) , [0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2] , ) @slow def _lowercase( self ) -> Optional[int]: UpperCAmelCase : Any = self.tokenizer_class.from_pretrained("""allenai/longformer-base-4096""" ) UpperCAmelCase : List[Any] = tokenizer.encode("""sequence builders""" , add_special_tokens=A ) UpperCAmelCase : Optional[Any] = tokenizer.encode("""multi-sequence build""" , add_special_tokens=A ) UpperCAmelCase : List[str] = tokenizer.encode( """sequence builders""" , add_special_tokens=A , add_prefix_space=A ) UpperCAmelCase : List[str] = tokenizer.encode( """sequence builders""" , """multi-sequence build""" , add_special_tokens=A , add_prefix_space=A ) UpperCAmelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(A ) UpperCAmelCase : Any = tokenizer.build_inputs_with_special_tokens(A , A ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _lowercase( self ) -> List[Any]: UpperCAmelCase : str = self.get_tokenizer() UpperCAmelCase : List[Any] = """Encode this sequence.""" UpperCAmelCase : List[str] = tokenizer.byte_encoder[""" """.encode("""utf-8""" )[0]] # Testing encoder arguments UpperCAmelCase : Union[str, Any] = tokenizer.encode(A , add_special_tokens=A , add_prefix_space=A ) UpperCAmelCase : Dict = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(A , A ) UpperCAmelCase : Tuple = tokenizer.encode(A , add_special_tokens=A , add_prefix_space=A ) UpperCAmelCase : int = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(A , A ) tokenizer.add_special_tokens({"""bos_token""": """<s>"""} ) UpperCAmelCase : int = tokenizer.encode(A , add_special_tokens=A ) UpperCAmelCase : List[Any] = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(A , A ) # Testing spaces after special tokens UpperCAmelCase : Union[str, Any] = """<mask>""" tokenizer.add_special_tokens( {"""mask_token""": AddedToken(A , lstrip=A , rstrip=A )} ) # mask token has a left space UpperCAmelCase : str = tokenizer.convert_tokens_to_ids(A ) UpperCAmelCase : Union[str, Any] = """Encode <mask> sequence""" UpperCAmelCase : Union[str, Any] = """Encode <mask>sequence""" UpperCAmelCase : Union[str, Any] = tokenizer.encode(A ) UpperCAmelCase : Union[str, Any] = encoded.index(A ) UpperCAmelCase : List[str] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(A , A ) UpperCAmelCase : Tuple = tokenizer.encode(A ) UpperCAmelCase : Optional[int] = encoded.index(A ) UpperCAmelCase : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(A , A ) def _lowercase( self ) -> Optional[int]: pass def _lowercase( self ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): UpperCAmelCase : str = self.rust_tokenizer_class.from_pretrained(A , **A ) UpperCAmelCase : int = self.tokenizer_class.from_pretrained(A , **A ) UpperCAmelCase : Dict = """A, <mask> AllenNLP sentence.""" UpperCAmelCase : Dict = tokenizer_r.encode_plus(A , add_special_tokens=A , return_token_type_ids=A ) UpperCAmelCase : Tuple = tokenizer_p.encode_plus(A , add_special_tokens=A , return_token_type_ids=A ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["""token_type_ids"""] ) , sum(tokens_p["""token_type_ids"""] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["""attention_mask"""] ) / len(tokens_r["""attention_mask"""] ) , sum(tokens_p["""attention_mask"""] ) / len(tokens_p["""attention_mask"""] ) , ) UpperCAmelCase : List[Any] = tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""] ) UpperCAmelCase : int = tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p["""input_ids"""] , [0, 250, 6, 50264, 3823, 487, 21992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r["""input_ids"""] , [0, 250, 6, 50264, 3823, 487, 21992, 3645, 4, 2] ) self.assertSequenceEqual( A , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) self.assertSequenceEqual( A , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) def _lowercase( self ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): UpperCAmelCase : Optional[int] = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : Optional[int] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) UpperCAmelCase : Optional[int] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state["""add_prefix_space"""] , A ) self.assertEqual(post_processor_state["""add_prefix_space"""] , A ) self.assertEqual(post_processor_state["""trim_offsets"""] , A ) def _lowercase( self ) -> Optional[Any]: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): UpperCAmelCase : Union[str, Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` UpperCAmelCase : int = f'''{text_of_1_token} {text_of_1_token}''' UpperCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : str = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (len(A ) + 1, len(A ) + 1 + len(A )) , ) UpperCAmelCase : Optional[Any] = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : Dict = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (len(A ) + 1, len(A ) + 1 + len(A )) , ) UpperCAmelCase : int = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : List[Any] = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (len(A ), len(A ) + 1 + len(A )) , ) UpperCAmelCase : Any = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : str = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (len(A ), len(A ) + 1 + len(A )) , ) UpperCAmelCase : Optional[Any] = f''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) UpperCAmelCase : Any = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : str = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(A ) + 1, 1 + len(A ) + 1 + len(A )) , ) UpperCAmelCase : Optional[int] = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : Union[str, Any] = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(A ), 1 + len(A ) + 1 + len(A )) , ) UpperCAmelCase : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , add_prefix_space=A , trim_offsets=A ) UpperCAmelCase : Optional[int] = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(A ), 1 + len(A ) + 1 + len(A )) , )
265
0
from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { 'google/umt5-small': 'https://huggingface.co/google/umt5-small/resolve/main/config.json', # See all umt5 models at https://huggingface.co/models?filter=umt5 } class lowercase__ ( __lowerCamelCase ): '''simple docstring''' a : Dict = "umt5" a : Dict = ["past_key_values"] def __init__( self, __magic_name__=250112, __magic_name__=512, __magic_name__=64, __magic_name__=1024, __magic_name__=8, __magic_name__=None, __magic_name__=6, __magic_name__=32, __magic_name__=128, __magic_name__=0.1, __magic_name__=1E-6, __magic_name__=1.0, __magic_name__="gated-gelu", __magic_name__=True, __magic_name__=True, __magic_name__="T5Tokenizer", __magic_name__=True, __magic_name__=0, __magic_name__=1, __magic_name__=0, **__magic_name__, ) -> Optional[Any]: """simple docstring""" super().__init__( is_encoder_decoder=__magic_name__, tokenizer_class=__magic_name__, tie_word_embeddings=__magic_name__, pad_token_id=__magic_name__, eos_token_id=__magic_name__, decoder_start_token_id=__magic_name__, **__magic_name__, ) UpperCamelCase__ : str = vocab_size UpperCamelCase__ : Dict = d_model UpperCamelCase__ : Dict = d_kv UpperCamelCase__ : str = d_ff UpperCamelCase__ : Optional[Any] = num_layers UpperCamelCase__ : int = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry UpperCamelCase__ : List[str] = num_heads UpperCamelCase__ : Dict = relative_attention_num_buckets UpperCamelCase__ : Optional[int] = relative_attention_max_distance UpperCamelCase__ : List[str] = dropout_rate UpperCamelCase__ : Tuple = layer_norm_epsilon UpperCamelCase__ : Optional[int] = initializer_factor UpperCamelCase__ : Tuple = feed_forward_proj UpperCamelCase__ : Optional[int] = use_cache UpperCamelCase__ : Dict = self.feed_forward_proj.split('''-''' ) UpperCamelCase__ : Optional[int] = act_info[-1] UpperCamelCase__ : List[str] = act_info[0] == '''gated''' if len(__magic_name__ ) > 1 and act_info[0] != "gated" or len(__magic_name__ ) > 2: raise ValueError( f"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer." '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ''' '''\'gated-gelu\' or \'relu\'''' ) if feed_forward_proj == "gated-gelu": UpperCamelCase__ : str = '''gelu_new''' @property def UpperCamelCase__ ( self ) -> List[str]: """simple docstring""" return self.d_model @property def UpperCamelCase__ ( self ) -> Dict: """simple docstring""" return self.num_heads @property def UpperCamelCase__ ( self ) -> Union[str, Any]: """simple docstring""" return self.num_layers class lowercase__ ( __lowerCamelCase ): '''simple docstring''' @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs def UpperCamelCase__ ( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" UpperCamelCase__ : Optional[int] = { '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''}, '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''}, } if self.use_past: UpperCamelCase__ : List[str] = '''past_encoder_sequence + sequence''' UpperCamelCase__ : str = {0: '''batch'''} UpperCamelCase__ : Tuple = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: UpperCamelCase__ : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} UpperCamelCase__ : Tuple = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(__magic_name__, direction='''inputs''' ) return common_inputs @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset def UpperCamelCase__ ( self ) -> int: """simple docstring""" return 13 @property def UpperCamelCase__ ( self ) -> float: """simple docstring""" return 5E-4
364
from collections import deque def lowerCAmelCase_ ( __UpperCAmelCase: int ) -> str: UpperCamelCase__ : Optional[int] = len(__UpperCAmelCase ) UpperCamelCase__ : str = deque() UpperCamelCase__ : int = [False for _ in range(__UpperCAmelCase )] UpperCamelCase__ : Optional[int] = [-1 for _ in range(__UpperCAmelCase )] UpperCamelCase__ : str = index_of[:] def strong_connect(__UpperCAmelCase: Optional[int] , __UpperCAmelCase: Union[str, Any] , __UpperCAmelCase: Union[str, Any] ): UpperCamelCase__ : str = index # the number when this node is seen UpperCamelCase__ : Any = index # lowest rank node reachable from here index += 1 stack.append(__UpperCAmelCase ) UpperCamelCase__ : Optional[Any] = True for w in g[v]: if index_of[w] == -1: UpperCamelCase__ : str = strong_connect(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) UpperCamelCase__ : List[str] = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: UpperCamelCase__ : Dict = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: UpperCamelCase__ : Tuple = [] UpperCamelCase__ : str = stack.pop() UpperCamelCase__ : int = False component.append(__UpperCAmelCase ) while w != v: UpperCamelCase__ : int = stack.pop() UpperCamelCase__ : Optional[Any] = False component.append(__UpperCAmelCase ) components.append(__UpperCAmelCase ) return index UpperCamelCase__ : Optional[Any] = [] for v in range(__UpperCAmelCase ): if index_of[v] == -1: strong_connect(__UpperCAmelCase , 0 , __UpperCAmelCase ) return components def lowerCAmelCase_ ( __UpperCAmelCase: Union[str, Any] , __UpperCAmelCase: List[Any] ) -> str: UpperCamelCase__ : Dict = [[] for _ in range(__UpperCAmelCase )] for u, v in edges: g[u].append(__UpperCAmelCase ) return g if __name__ == "__main__": # Test UpperCAmelCase_ = 7 UpperCAmelCase_ = [0, 0, 1, 2, 3, 3, 4, 4, 6] UpperCAmelCase_ = [1, 3, 2, 0, 1, 4, 5, 6, 5] UpperCAmelCase_ = [(u, v) for u, v in zip(source, target)] UpperCAmelCase_ = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
247
0
def __lowercase ( ): return [list(range(1_0_0_0 - i , -1_0_0_0 - i , -1 ) ) for i in range(1_0_0_0 )] snake_case : Optional[int] = generate_large_matrix() snake_case : 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 __lowercase ( __lowerCAmelCase : list[list[int]] ): assert all(row == sorted(__lowerCAmelCase , reverse=__lowerCAmelCase ) for row in grid ) assert all(list(__lowerCAmelCase ) == sorted(__lowerCAmelCase , reverse=__lowerCAmelCase ) for col in zip(*__lowerCAmelCase ) ) def __lowercase ( __lowerCAmelCase : list[int] ): a__ = 0 a__ = len(__lowerCAmelCase ) - 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: a__ = (left + right) // 2 a__ = 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: a__ = mid + 1 else: a__ = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__lowerCAmelCase ) def __lowercase ( __lowerCAmelCase : list[list[int]] ): a__ = 0 a__ = len(grid[0] ) for i in range(len(__lowerCAmelCase ) ): a__ = find_negative_index(grid[i][:bound] ) total += bound return (len(__lowerCAmelCase ) * len(grid[0] )) - total def __lowercase ( __lowerCAmelCase : list[list[int]] ): return len([number for row in grid for number in row if number < 0] ) def __lowercase ( __lowerCAmelCase : list[list[int]] ): a__ = 0 for row in grid: for i, number in enumerate(__lowerCAmelCase ): if number < 0: total += len(__lowerCAmelCase ) - i break return total def __lowercase ( ): from timeit import timeit print('Running benchmarks' ) a__ = ( '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 ): a__ = timeit(F'{func}(grid=grid)' , setup=__lowerCAmelCase , number=5_0_0 ) print(F'{func}() took {time:0.4f} seconds' ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
240
import inspect import logging import os import random import shutil import tempfile import unittest import pytest import torch from torch import nn from torch.utils.data import DataLoader, TensorDataset from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_cuda from accelerate.utils import ProjectConfiguration, set_seed snake_case : List[Any] = logging.getLogger(__name__) def __lowercase ( __lowerCAmelCase : str=2 , __lowerCAmelCase : Optional[int]=3 , __lowerCAmelCase : List[str]=1_6 , __lowerCAmelCase : int = 1_0 , __lowerCAmelCase : int = 2 ): def get_dataset(__lowerCAmelCase : Dict ): a__ = torch.randn(batch_size * n_batches , 1 ) return TensorDataset(__lowerCAmelCase , a * x + b + 0.1 * torch.randn(batch_size * n_batches , 1 ) ) a__ = get_dataset(__lowerCAmelCase ) a__ = get_dataset(__lowerCAmelCase ) a__ = DataLoader(__lowerCAmelCase , shuffle=__lowerCAmelCase , batch_size=__lowerCAmelCase , num_workers=4 ) a__ = DataLoader(__lowerCAmelCase , shuffle=__lowerCAmelCase , batch_size=__lowerCAmelCase , num_workers=4 ) return (train_dataloader, valid_dataloader) def __lowercase ( __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int]=None ): a__ = [] for epoch in range(__lowerCAmelCase ): # Train quickly model.train() for batch in dataloader: a__ , a__ = batch a__ = model(__lowerCAmelCase ) a__ = torch.nn.functional.mse_loss(__lowerCAmelCase , __lowerCAmelCase ) accelerator.backward(__lowerCAmelCase ) optimizer.step() optimizer.zero_grad() rands.append(random.random() ) # Introduce some randomness if scheduler is not None: scheduler.step() return rands class snake_case_ (nn.Module ): def __init__( self :Any ) -> Union[str, Any]: super().__init__() a__ = nn.Parameter(torch.randn(1 ) ) a__ = nn.Parameter(torch.randn(1 ) ) def lowerCamelCase__( self :List[str] ,__snake_case :Union[str, Any] ) -> str: return x * self.a + self.b class snake_case_ (unittest.TestCase ): def lowerCamelCase__( self :Tuple ) -> Dict: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) a__ = DummyModel() a__ = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) a__ , a__ = dummy_dataloaders() a__ = ProjectConfiguration(total_limit=1 ,project_dir=__snake_case ,automatic_checkpoint_naming=__snake_case ) # Train baseline a__ = Accelerator(project_config=__snake_case ) a__ , a__ , a__ , a__ = accelerator.prepare( __snake_case ,__snake_case ,__snake_case ,__snake_case ) # Save initial accelerator.save_state() # Save second state accelerator.save_state() self.assertEqual(len(os.listdir(accelerator.project_dir ) ) ,1 ) def lowerCamelCase__( self :List[Any] ) -> str: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) a__ = DummyModel() a__ = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) a__ , a__ = dummy_dataloaders() # Train baseline a__ = Accelerator() a__ , a__ , a__ , a__ = accelerator.prepare( __snake_case ,__snake_case ,__snake_case ,__snake_case ) # Save initial a__ = os.path.join(__snake_case ,'initial' ) accelerator.save_state(__snake_case ) ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() a__ = train(3 ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() # Train partially set_seed(42 ) a__ = DummyModel() a__ = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) a__ , a__ = dummy_dataloaders() a__ = Accelerator() a__ , a__ , a__ , a__ = accelerator.prepare( __snake_case ,__snake_case ,__snake_case ,__snake_case ) accelerator.load_state(__snake_case ) ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) a__ = train(2 ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) # Save everything a__ = os.path.join(__snake_case ,'checkpoint' ) accelerator.save_state(__snake_case ) # Load everything back in and make sure all states work accelerator.load_state(__snake_case ) test_rands += train(1 ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) def lowerCamelCase__( self :str ) -> List[str]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) a__ = DummyModel() a__ = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) a__ , a__ = dummy_dataloaders() a__ = ProjectConfiguration(automatic_checkpoint_naming=__snake_case ) # Train baseline a__ = Accelerator(project_dir=__snake_case ,project_config=__snake_case ) a__ , a__ , a__ , a__ = accelerator.prepare( __snake_case ,__snake_case ,__snake_case ,__snake_case ) # Save initial accelerator.save_state() ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() a__ = train(3 ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() # Train partially set_seed(42 ) a__ = DummyModel() a__ = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) a__ , a__ = dummy_dataloaders() a__ = ProjectConfiguration(iteration=1 ,automatic_checkpoint_naming=__snake_case ) a__ = Accelerator(project_dir=__snake_case ,project_config=__snake_case ) a__ , a__ , a__ , a__ = accelerator.prepare( __snake_case ,__snake_case ,__snake_case ,__snake_case ) accelerator.load_state(os.path.join(__snake_case ,'checkpoints' ,'checkpoint_0' ) ) ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) a__ = train(2 ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) # Save everything accelerator.save_state() # Load everything back in and make sure all states work accelerator.load_state(os.path.join(__snake_case ,'checkpoints' ,'checkpoint_1' ) ) test_rands += train(1 ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) ((a__) , (a__)) = model.a.item(), model.b.item() a__ = optimizer.state_dict() self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) self.assertEqual(__snake_case ,__snake_case ) def lowerCamelCase__( self :Union[str, Any] ) -> List[str]: a__ = torch.tensor([1, 2, 3] ) a__ = torch.tensor([2, 3, 4] ) a__ = DummyModel() a__ = torch.optim.Adam(net.parameters() ) a__ = Accelerator() with self.assertRaises(__snake_case ) as ve: accelerator.register_for_checkpointing(__snake_case ,__snake_case ,__snake_case ,__snake_case ) a__ = str(ve.exception ) self.assertTrue('Item at index 0' in message ) self.assertTrue('Item at index 1' in message ) self.assertFalse('Item at index 2' in message ) self.assertFalse('Item at index 3' in message ) def lowerCamelCase__( self :List[Any] ) -> Dict: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) a__ = DummyModel() a__ = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) a__ = torch.optim.lr_scheduler.StepLR(__snake_case ,step_size=1 ,gamma=0.99 ) a__ , a__ = dummy_dataloaders() a__ = ProjectConfiguration(automatic_checkpoint_naming=__snake_case ) # Train baseline a__ = Accelerator(project_dir=__snake_case ,project_config=__snake_case ) a__ , a__ , a__ , a__ , a__ = accelerator.prepare( __snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) # Save initial accelerator.save_state() a__ = scheduler.state_dict() train(3 ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) self.assertNotEqual(__snake_case ,scheduler.state_dict() ) # Load everything back in and make sure all states work accelerator.load_state(os.path.join(__snake_case ,'checkpoints' ,'checkpoint_0' ) ) self.assertEqual(__snake_case ,scheduler.state_dict() ) def lowerCamelCase__( self :Optional[int] ) -> List[str]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) a__ = DummyModel() a__ = ProjectConfiguration(automatic_checkpoint_naming=__snake_case ,total_limit=2 ) # Train baseline a__ = Accelerator(project_dir=__snake_case ,project_config=__snake_case ) a__ = accelerator.prepare(__snake_case ) # Save 3 states: for _ in range(11 ): accelerator.save_state() self.assertTrue(not os.path.exists(os.path.join(__snake_case ,'checkpoints' ,'checkpoint_0' ) ) ) self.assertTrue(os.path.exists(os.path.join(__snake_case ,'checkpoints' ,'checkpoint_9' ) ) ) self.assertTrue(os.path.exists(os.path.join(__snake_case ,'checkpoints' ,'checkpoint_10' ) ) ) @require_cuda def lowerCamelCase__( self :Dict ) -> str: a__ = ['torchrun', F'--nproc_per_node={torch.cuda.device_count()}', inspect.getfile(self.__class__ )] execute_subprocess_async(__snake_case ,env=os.environ.copy() ) if __name__ == "__main__": snake_case : Tuple = '''/tmp/accelerate/state_checkpointing''' snake_case : str = DummyModel() snake_case : List[Any] = torch.optim.Adam(params=model.parameters(), lr=1e-3) snake_case : Union[str, Any] = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.99) snake_case , snake_case : str = dummy_dataloaders() snake_case : Union[str, Any] = ProjectConfiguration(automatic_checkpoint_naming=True) # Train baseline snake_case : Dict = Accelerator(project_dir=savedir, project_config=project_config, mixed_precision='''no''') if accelerator.process_index == 0: if os.path.exists(savedir): shutil.rmtree(savedir) os.makedirs(savedir) snake_case , snake_case , snake_case , snake_case , snake_case : List[str] = accelerator.prepare( model, optimizer, train_dataloader, valid_dataloader, scheduler ) snake_case , snake_case : Any = accelerator.prepare(model, optimizer) train(3, model, train_dataloader, optimizer, accelerator, scheduler) # Check that the intial optimizer is loaded on the GPU for group in optimizer.param_groups: snake_case : Any = group['''params'''][0].device break assert param_device.type == accelerator.device.type snake_case : Union[str, Any] = model.cpu() accelerator.wait_for_everyone() accelerator.save_state() accelerator.wait_for_everyone() # Check CPU state accelerator.load_state(os.path.join(savedir, '''checkpoints''', '''checkpoint_0'''), map_location='''cpu''') for group in optimizer.param_groups: snake_case : int = group['''params'''][0].device break assert ( param_device.type == torch.device('''cpu''').type ), f"Loaded optimizer states did not match, expected to be loaded on the CPU but got {param_device}" # Check device state model.to(accelerator.device) accelerator.load_state(os.path.join(savedir, '''checkpoints''', '''checkpoint_0'''), map_location='''on_device''') for group in optimizer.param_groups: snake_case : Optional[int] = group['''params'''][0].device break assert ( param_device.type == accelerator.device.type ), f"Loaded optimizer states did not match, expected to be loaded on {accelerator.device} but got {param_device}" # Check error with pytest.raises(TypeError, match='''Unsupported optimizer map location passed'''): accelerator.load_state(os.path.join(savedir, '''checkpoints''', '''checkpoint_0'''), map_location='''invalid''') accelerator.wait_for_everyone() if accelerator.process_index == 0: shutil.rmtree(savedir) accelerator.wait_for_everyone()
240
1
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTConfig, DeiTForImageClassificationWithTeacher, DeiTImageProcessor from transformers.utils import logging logging.set_verbosity_info() a_ = logging.get_logger(__name__) def _a( UpperCamelCase__ : Optional[Any], UpperCamelCase__ : List[Any]=False ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] =[] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"blocks.{i}.norm1.weight", f"deit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((f"blocks.{i}.norm1.bias", f"deit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((f"blocks.{i}.attn.proj.weight", f"deit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((f"blocks.{i}.attn.proj.bias", f"deit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((f"blocks.{i}.norm2.weight", f"deit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((f"blocks.{i}.norm2.bias", f"deit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc1.weight", f"deit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc1.bias", f"deit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc2.weight", f"deit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc2.bias", f"deit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''deit.embeddings.cls_token'''), ('''dist_token''', '''deit.embeddings.distillation_token'''), ('''patch_embed.proj.weight''', '''deit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''deit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''deit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ('''pre_logits.fc.weight''', '''pooler.dense.weight'''), ('''pre_logits.fc.bias''', '''pooler.dense.bias'''), ] ) # if just the base model, we should remove "deit" from all keys that start with "deit" SCREAMING_SNAKE_CASE__ : Optional[int] =[(pair[0], pair[1][4:]) if pair[1].startswith('''deit''' ) else pair for pair in rename_keys] else: # layernorm + classification heads rename_keys.extend( [ ('''norm.weight''', '''deit.layernorm.weight'''), ('''norm.bias''', '''deit.layernorm.bias'''), ('''head.weight''', '''cls_classifier.weight'''), ('''head.bias''', '''cls_classifier.bias'''), ('''head_dist.weight''', '''distillation_classifier.weight'''), ('''head_dist.bias''', '''distillation_classifier.bias'''), ] ) return rename_keys def _a( UpperCamelCase__ : Dict, UpperCamelCase__ : List[str], UpperCamelCase__ : Any=False ): '''simple docstring''' for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE__ : List[Any] ='''''' else: SCREAMING_SNAKE_CASE__ : List[Any] ='''deit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE__ : List[Any] =state_dict.pop(f"blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] =state_dict.pop(f"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE__ : int =in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE__ : Union[str, Any] =in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE__ : Dict =in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE__ : Optional[int] =in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE__ : List[Any] =in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE__ : int =in_proj_bias[-config.hidden_size :] def _a( UpperCamelCase__ : List[Any], UpperCamelCase__ : Optional[Any], UpperCamelCase__ : List[str] ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict =dct.pop(UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ : int =val def _a( ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str ='''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE__ : Any =Image.open(requests.get(UpperCamelCase__, stream=UpperCamelCase__ ).raw ) return im @torch.no_grad() def _a( UpperCamelCase__ : str, UpperCamelCase__ : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Dict =DeiTConfig() # all deit models have fine-tuned heads SCREAMING_SNAKE_CASE__ : Optional[Any] =False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size SCREAMING_SNAKE_CASE__ : Union[str, Any] =1_0_0_0 SCREAMING_SNAKE_CASE__ : List[str] ='''huggingface/label-files''' SCREAMING_SNAKE_CASE__ : Optional[Any] ='''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE__ : Union[str, Any] =json.load(open(hf_hub_download(UpperCamelCase__, UpperCamelCase__, repo_type='''dataset''' ), '''r''' ) ) SCREAMING_SNAKE_CASE__ : str ={int(UpperCamelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE__ : Dict =idalabel SCREAMING_SNAKE_CASE__ : List[Any] ={v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE__ : Any =int(deit_name[-6:-4] ) SCREAMING_SNAKE_CASE__ : Union[str, Any] =int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith('''tiny''' ): SCREAMING_SNAKE_CASE__ : Dict =1_9_2 SCREAMING_SNAKE_CASE__ : int =7_6_8 SCREAMING_SNAKE_CASE__ : str =1_2 SCREAMING_SNAKE_CASE__ : Optional[Any] =3 elif deit_name[9:].startswith('''small''' ): SCREAMING_SNAKE_CASE__ : int =3_8_4 SCREAMING_SNAKE_CASE__ : List[str] =1_5_3_6 SCREAMING_SNAKE_CASE__ : Union[str, Any] =1_2 SCREAMING_SNAKE_CASE__ : Any =6 if deit_name[9:].startswith('''base''' ): pass elif deit_name[4:].startswith('''large''' ): SCREAMING_SNAKE_CASE__ : List[str] =1_0_2_4 SCREAMING_SNAKE_CASE__ : str =4_0_9_6 SCREAMING_SNAKE_CASE__ : Optional[Any] =2_4 SCREAMING_SNAKE_CASE__ : Union[str, Any] =1_6 # load original model from timm SCREAMING_SNAKE_CASE__ : Any =timm.create_model(UpperCamelCase__, pretrained=UpperCamelCase__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys SCREAMING_SNAKE_CASE__ : Optional[Any] =timm_model.state_dict() SCREAMING_SNAKE_CASE__ : List[str] =create_rename_keys(UpperCamelCase__, UpperCamelCase__ ) for src, dest in rename_keys: rename_key(UpperCamelCase__, UpperCamelCase__, UpperCamelCase__ ) read_in_q_k_v(UpperCamelCase__, UpperCamelCase__, UpperCamelCase__ ) # load HuggingFace model SCREAMING_SNAKE_CASE__ : Dict =DeiTForImageClassificationWithTeacher(UpperCamelCase__ ).eval() model.load_state_dict(UpperCamelCase__ ) # Check outputs on an image, prepared by DeiTImageProcessor SCREAMING_SNAKE_CASE__ : Dict =int( (2_5_6 / 2_2_4) * config.image_size ) # to maintain same ratio w.r.t. 224 images, see https://github.com/facebookresearch/deit/blob/ab5715372db8c6cad5740714b2216d55aeae052e/datasets.py#L103 SCREAMING_SNAKE_CASE__ : Optional[int] =DeiTImageProcessor(size=UpperCamelCase__, crop_size=config.image_size ) SCREAMING_SNAKE_CASE__ : List[Any] =image_processor(images=prepare_img(), return_tensors='''pt''' ) SCREAMING_SNAKE_CASE__ : Any =encoding['''pixel_values'''] SCREAMING_SNAKE_CASE__ : List[str] =model(UpperCamelCase__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] =timm_model(UpperCamelCase__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(UpperCamelCase__, outputs.logits, atol=1e-3 ) Path(UpperCamelCase__ ).mkdir(exist_ok=UpperCamelCase__ ) print(f"Saving model {deit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(UpperCamelCase__ ) print(f"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": a_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--deit_name', default='vit_deit_base_distilled_patch16_224', type=str, help='Name of the DeiT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) a_ = parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
222
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __magic_name__ ( self : List[str] ) -> Optional[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __magic_name__ ( self : Union[str, Any] ) -> Dict: SCREAMING_SNAKE_CASE__ : Optional[int] =StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) SCREAMING_SNAKE_CASE__ : str =sd_pipe.to(__lowercase ) sd_pipe.set_progress_bar_config(disable=__lowercase ) sd_pipe.set_scheduler('''sample_euler''' ) SCREAMING_SNAKE_CASE__ : List[Any] ='''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE__ : List[str] =torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] =sd_pipe([prompt] , generator=__lowercase , guidance_scale=9.0 , num_inference_steps=20 , output_type='''np''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] =output.images SCREAMING_SNAKE_CASE__ : int =image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) SCREAMING_SNAKE_CASE__ : Any =np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __magic_name__ ( self : List[str] ) -> List[Any]: SCREAMING_SNAKE_CASE__ : List[str] =StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) SCREAMING_SNAKE_CASE__ : int =sd_pipe.to(__lowercase ) sd_pipe.set_progress_bar_config(disable=__lowercase ) sd_pipe.set_scheduler('''sample_euler''' ) SCREAMING_SNAKE_CASE__ : Any ='''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE__ : Dict =torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : Tuple =sd_pipe([prompt] , generator=__lowercase , guidance_scale=9.0 , num_inference_steps=20 , output_type='''np''' ) SCREAMING_SNAKE_CASE__ : Dict =output.images SCREAMING_SNAKE_CASE__ : Dict =image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) SCREAMING_SNAKE_CASE__ : List[str] =np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-1 def __magic_name__ ( self : List[str] ) -> List[Any]: SCREAMING_SNAKE_CASE__ : Any =StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) SCREAMING_SNAKE_CASE__ : Tuple =sd_pipe.to(__lowercase ) sd_pipe.set_progress_bar_config(disable=__lowercase ) sd_pipe.set_scheduler('''sample_dpmpp_2m''' ) SCREAMING_SNAKE_CASE__ : Tuple ='''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE__ : Optional[Any] =torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : List[Any] =sd_pipe( [prompt] , generator=__lowercase , guidance_scale=7.5 , num_inference_steps=15 , output_type='''np''' , use_karras_sigmas=__lowercase , ) SCREAMING_SNAKE_CASE__ : Optional[int] =output.images SCREAMING_SNAKE_CASE__ : Dict =image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) SCREAMING_SNAKE_CASE__ : Optional[Any] =np.array( [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
222
1
import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def __SCREAMING_SNAKE_CASE (*SCREAMING_SNAKE_CASE__ ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = list(SCREAMING_SNAKE_CASE__ ) for i in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [ '''CUDA out of memory.''', # CUDA OOM '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''', # CUDNN SNAFU '''DefaultCPUAllocator: can\'t allocate memory''', # CPU OOM ] if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and len(exception.args ) == 1: return any(err in exception.args[0] for err in _statements ) return False def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = 128 ): if function is None: return functools.partial(SCREAMING_SNAKE_CASE__ , starting_batch_size=SCREAMING_SNAKE_CASE__ ) snake_case_ = starting_batch_size def decorator(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() snake_case_ = list(inspect.signature(SCREAMING_SNAKE_CASE__ ).parameters.keys() ) # Guard against user error if len(SCREAMING_SNAKE_CASE__ ) < (len(SCREAMING_SNAKE_CASE__ ) + 1): snake_case_ = ''', '''.join([F'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:] )] ) raise TypeError( F'''Batch size was passed into `{function.__name__}` as the first argument when called.''' F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''' ) while True: if batch_size == 0: raise RuntimeError('''No executable batch size found, reached zero.''' ) try: return function(SCREAMING_SNAKE_CASE__ , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) except Exception as e: if should_reduce_batch_size(SCREAMING_SNAKE_CASE__ ): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
8
from __future__ import annotations from math import pi, sqrt def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if inductance <= 0: raise ValueError('''Inductance cannot be 0 or negative''' ) elif capacitance <= 0: raise ValueError('''Capacitance cannot be 0 or negative''' ) else: return ( "Resonant frequency", float(1 / (2 * pi * (sqrt(inductance * capacitance ))) ), ) if __name__ == "__main__": import doctest doctest.testmod()
8
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) _a : Optional[int] = { 'configuration_mega': ['MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegaConfig', 'MegaOnnxConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'MEGA_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegaForCausalLM', 'MegaForMaskedLM', 'MegaForMultipleChoice', 'MegaForQuestionAnswering', 'MegaForSequenceClassification', 'MegaForTokenClassification', 'MegaModel', 'MegaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mega import ( MEGA_PRETRAINED_MODEL_ARCHIVE_LIST, MegaForCausalLM, MegaForMaskedLM, MegaForMultipleChoice, MegaForQuestionAnswering, MegaForSequenceClassification, MegaForTokenClassification, MegaModel, MegaPreTrainedModel, ) else: import sys _a : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
126
"""simple docstring""" _a : Any = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _a : List[Any] = [{'type': 'code', 'content': INSTALL_CONTENT}] _a : Union[str, Any] = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
126
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase__ : Any = { '''microsoft/focalnet-tiny''': '''https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json''', } class _UpperCAmelCase ( __a , __a): __a : Dict = """focalnet""" def __init__( self , _A=2_24 , _A=4 , _A=3 , _A=96 , _A=False , _A=[1_92, 3_84, 7_68, 7_68] , _A=[2, 2, 6, 2] , _A=[2, 2, 2, 2] , _A=[3, 3, 3, 3] , _A="gelu" , _A=4.0 , _A=0.0 , _A=0.1 , _A=False , _A=1e-4 , _A=False , _A=False , _A=False , _A=0.02 , _A=1e-5 , _A=32 , _A=None , _A=None , **_A , ) -> int: '''simple docstring''' super().__init__(**_A ) _UpperCAmelCase : int = image_size _UpperCAmelCase : int = patch_size _UpperCAmelCase : Dict = num_channels _UpperCAmelCase : str = embed_dim _UpperCAmelCase : Any = use_conv_embed _UpperCAmelCase : int = hidden_sizes _UpperCAmelCase : Optional[int] = depths _UpperCAmelCase : Union[str, Any] = focal_levels _UpperCAmelCase : int = focal_windows _UpperCAmelCase : Tuple = hidden_act _UpperCAmelCase : Any = mlp_ratio _UpperCAmelCase : str = hidden_dropout_prob _UpperCAmelCase : str = drop_path_rate _UpperCAmelCase : Optional[Any] = use_layerscale _UpperCAmelCase : Dict = layerscale_value _UpperCAmelCase : Optional[int] = use_post_layernorm _UpperCAmelCase : List[str] = use_post_layernorm_in_modulation _UpperCAmelCase : Tuple = normalize_modulator _UpperCAmelCase : Any = initializer_range _UpperCAmelCase : Optional[Any] = layer_norm_eps _UpperCAmelCase : List[Any] = encoder_stride _UpperCAmelCase : Optional[Any] = ["""stem"""] + [f'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] _UpperCAmelCase , _UpperCAmelCase : Optional[int] = get_aligned_output_features_output_indices( out_features=_A , out_indices=_A , stage_names=self.stage_names )
246
"""simple docstring""" import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: lowerCamelCase__ : Union[str, Any] = None lowerCamelCase__ : Any = logging.get_logger(__name__) lowerCamelCase__ : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} lowerCamelCase__ : List[Any] = { '''vocab_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/spiece.model''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/spiece.model''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/spiece.model''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/spiece.model''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/tokenizer.json''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/tokenizer.json''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/tokenizer.json''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/tokenizer.json''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/tokenizer.json''', }, } # TODO(PVP) - this should be removed in Transformers v5 lowerCamelCase__ : Optional[Any] = { '''t5-small''': 5_12, '''t5-base''': 5_12, '''t5-large''': 5_12, '''t5-3b''': 5_12, '''t5-11b''': 5_12, } class _UpperCAmelCase ( __a): __a : Optional[int] = VOCAB_FILES_NAMES __a : int = PRETRAINED_VOCAB_FILES_MAP __a : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __a : Union[str, Any] = ["""input_ids""", """attention_mask"""] __a : Dict = TaTokenizer __a : List[int] = [] def __init__( self , _A=None , _A=None , _A="</s>" , _A="<unk>" , _A="<pad>" , _A=1_00 , _A=None , **_A , ) -> Union[str, Any]: '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: _UpperCAmelCase : Any = [f'''<extra_id_{i}>''' for i in range(_A )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens _UpperCAmelCase : List[str] = len(set(filter(lambda _A : bool("""extra_id_""" in str(_A ) ) , _A ) ) ) if extra_tokens != extra_ids: raise ValueError( f'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are''' """ provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids""" """ tokens""" ) super().__init__( _A , tokenizer_file=_A , eos_token=_A , unk_token=_A , pad_token=_A , extra_ids=_A , additional_special_tokens=_A , **_A , ) _UpperCAmelCase : int = vocab_file _UpperCAmelCase : Any = False if not self.vocab_file else True _UpperCAmelCase : Optional[Any] = extra_ids @staticmethod def __snake_case ( _A , _A , _A ) -> Optional[int]: '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: _UpperCAmelCase : Union[str, Any] = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( """This tokenizer was incorrectly instantiated with a model max length of""" f''' {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this''' """ behavior is kept to avoid breaking backwards compatibility when padding/encoding with""" """ `truncation is True`.\n- Be aware that you SHOULD NOT rely on""" f''' {pretrained_model_name_or_path} automatically truncating your input to''' f''' {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences''' f''' longer than {deprecated_max_model_length} you can either instantiate this tokenizer with''' """ `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please""" """ instantiate this tokenizer with `model_max_length` set to your preferred value.""" , _A , ) return max_model_length def __snake_case ( self , _A , _A = None ) -> Tuple[str]: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(_A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _UpperCAmelCase : List[Any] = os.path.join( _A , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_A ): copyfile(self.vocab_file , _A ) logger.info(f'''Copy vocab file to {out_vocab_file}''' ) return (out_vocab_file,) def __snake_case ( self , _A , _A = None ) -> List[int]: '''simple docstring''' _UpperCAmelCase : List[Any] = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: _UpperCAmelCase : int = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def __snake_case ( self , _A , _A = None ) -> List[int]: '''simple docstring''' _UpperCAmelCase : str = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def __snake_case ( self ) -> List[str]: '''simple docstring''' return list( set(filter(lambda _A : bool(re.search(r"""<extra_id_\d+>""" , _A ) ) is not None , self.additional_special_tokens ) ) ) def __snake_case ( self ) -> int: '''simple docstring''' return [self.convert_tokens_to_ids(_A ) for token in self.get_sentinel_tokens()]
246
1
"""simple docstring""" __lowerCamelCase = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
360
"""simple docstring""" import unittest from transformers import SPIECE_UNDERLINE from transformers.models.speechta import SpeechTaTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.tokenization_utils import AddedToken from ...test_tokenization_common import TokenizerTesterMixin __lowerCamelCase = get_tests_dir("fixtures/test_sentencepiece_bpe_char.model") @require_sentencepiece @require_tokenizers class UpperCamelCase__( __A , unittest.TestCase ): lowerCAmelCase__ : Any = SpeechTaTokenizer lowerCAmelCase__ : List[str] = False lowerCAmelCase__ : List[str] = True def snake_case__ ( self ) -> Optional[Any]: super().setUp() # We have a SentencePiece fixture for testing A__ = SpeechTaTokenizer(__UpperCAmelCase ) A__ = AddedToken('<mask>' ,lstrip=__UpperCAmelCase ,rstrip=__UpperCAmelCase ) A__ = mask_token tokenizer.add_special_tokens({'mask_token': mask_token} ) tokenizer.add_tokens(['<ctc_blank>'] ) tokenizer.save_pretrained(self.tmpdirname ) def snake_case__ ( self ,__UpperCAmelCase ) -> Optional[Any]: A__ = 'this is a test' A__ = 'this is a test' return input_text, output_text def snake_case__ ( self ,__UpperCAmelCase ,__UpperCAmelCase=False ,__UpperCAmelCase=20 ,__UpperCAmelCase=5 ) -> Union[str, Any]: A__ , A__ = self.get_input_output_texts(__UpperCAmelCase ) A__ = tokenizer.encode(__UpperCAmelCase ,add_special_tokens=__UpperCAmelCase ) A__ = tokenizer.decode(__UpperCAmelCase ,clean_up_tokenization_spaces=__UpperCAmelCase ) return text, ids def snake_case__ ( self ) -> Optional[Any]: A__ = '<pad>' A__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) ,__UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) ,__UpperCAmelCase ) def snake_case__ ( self ) -> Tuple: A__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,'<s>' ) self.assertEqual(vocab_keys[1] ,'<pad>' ) self.assertEqual(vocab_keys[-4] ,'œ' ) self.assertEqual(vocab_keys[-2] ,'<mask>' ) self.assertEqual(vocab_keys[-1] ,'<ctc_blank>' ) self.assertEqual(len(__UpperCAmelCase ) ,81 ) def snake_case__ ( self ) -> str: self.assertEqual(self.get_tokenizer().vocab_size ,79 ) def snake_case__ ( self ) -> Tuple: A__ = self.get_tokenizers(do_lower_case=__UpperCAmelCase ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): A__ = tokenizer.vocab_size A__ = len(__UpperCAmelCase ) self.assertNotEqual(__UpperCAmelCase ,0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) A__ = ['aaaaa bbbbbb', 'cccccccccdddddddd'] A__ = tokenizer.add_tokens(__UpperCAmelCase ) A__ = tokenizer.vocab_size A__ = len(__UpperCAmelCase ) self.assertNotEqual(__UpperCAmelCase ,0 ) self.assertEqual(__UpperCAmelCase ,__UpperCAmelCase ) self.assertEqual(__UpperCAmelCase ,len(__UpperCAmelCase ) ) self.assertEqual(__UpperCAmelCase ,all_size + len(__UpperCAmelCase ) ) A__ = tokenizer.encode('aaaaa bbbbbb low cccccccccdddddddd l' ,add_special_tokens=__UpperCAmelCase ) self.assertGreaterEqual(len(__UpperCAmelCase ) ,4 ) self.assertGreater(tokens[0] ,tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] ,tokenizer.vocab_size - 1 ) A__ = {'eos_token': '>>>>|||<||<<|<<', 'pad_token': '<<<<<|||>|>>>>|>'} A__ = tokenizer.add_special_tokens(__UpperCAmelCase ) A__ = tokenizer.vocab_size A__ = len(__UpperCAmelCase ) self.assertNotEqual(__UpperCAmelCase ,0 ) self.assertEqual(__UpperCAmelCase ,__UpperCAmelCase ) self.assertEqual(__UpperCAmelCase ,len(__UpperCAmelCase ) ) self.assertEqual(__UpperCAmelCase ,all_size_a + len(__UpperCAmelCase ) ) A__ = tokenizer.encode( '>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l' ,add_special_tokens=__UpperCAmelCase ) self.assertGreaterEqual(len(__UpperCAmelCase ) ,6 ) self.assertGreater(tokens[0] ,tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] ,tokens[1] ) self.assertGreater(tokens[-3] ,tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] ,tokens[-4] ) self.assertEqual(tokens[0] ,tokenizer.eos_token_id ) self.assertEqual(tokens[-3] ,tokenizer.pad_token_id ) def snake_case__ ( self ) -> List[str]: pass def snake_case__ ( self ) -> List[str]: pass def snake_case__ ( self ) -> Dict: A__ = self.get_tokenizer() A__ = tokenizer.tokenize('This is a test' ) # fmt: off self.assertListEqual(__UpperCAmelCase ,[SPIECE_UNDERLINE, 'T', 'h', 'i', 's', SPIECE_UNDERLINE, 'i', 's', SPIECE_UNDERLINE, 'a', SPIECE_UNDERLINE, 't', 'e', 's', 't'] ) # fmt: on self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) ,[4, 32, 11, 10, 12, 4, 10, 12, 4, 7, 4, 6, 5, 12, 6] ,) A__ = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCAmelCase ,[SPIECE_UNDERLINE, 'I', SPIECE_UNDERLINE, 'w', 'a', 's', SPIECE_UNDERLINE, 'b', 'o', 'r', 'n', SPIECE_UNDERLINE, 'i', 'n', SPIECE_UNDERLINE, '92000', ',', SPIECE_UNDERLINE, 'a', 'n', 'd', SPIECE_UNDERLINE, 't', 'h', 'i', 's', SPIECE_UNDERLINE, 'i', 's', SPIECE_UNDERLINE, 'f', 'a', 'l', 's', 'é', '.'] ) A__ = tokenizer.convert_tokens_to_ids(__UpperCAmelCase ) # fmt: off self.assertListEqual(__UpperCAmelCase ,[4, 30, 4, 20, 7, 12, 4, 25, 8, 13, 9, 4, 10, 9, 4, 3, 23, 4, 7, 9, 14, 4, 6, 11, 10, 12, 4, 10, 12, 4, 19, 7, 15, 12, 73, 26] ) # fmt: on A__ = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) self.assertListEqual( __UpperCAmelCase ,[SPIECE_UNDERLINE, 'I', SPIECE_UNDERLINE, 'w', 'a', 's', SPIECE_UNDERLINE, 'b', 'o', 'r', 'n', SPIECE_UNDERLINE, 'i', 'n', SPIECE_UNDERLINE, '<unk>', ',', SPIECE_UNDERLINE, 'a', 'n', 'd', SPIECE_UNDERLINE, 't', 'h', 'i', 's', SPIECE_UNDERLINE, 'i', 's', SPIECE_UNDERLINE, 'f', 'a', 'l', 's', 'é', '.'] ) @slow def snake_case__ ( self ) -> Union[str, Any]: # Use custom sequence because this tokenizer does not handle numbers. A__ = [ 'Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides ' 'general-purpose architectures (BERT, GPT, RoBERTa, XLM, DistilBert, XLNet...) for Natural ' 'Language Understanding (NLU) and Natural Language Generation (NLG) with over thirty-two pretrained ' 'models in one hundred plus languages and deep interoperability between Jax, PyTorch and TensorFlow.', 'BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly ' 'conditioning on both left and right context in all layers.', 'The quick brown fox jumps over the lazy dog.', ] # fmt: off A__ = { 'input_ids': [ [4, 32, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 64, 19, 8, 13, 18, 5, 13, 15, 22, 4, 28, 9, 8, 20, 9, 4, 7, 12, 4, 24, 22, 6, 8, 13, 17, 11, 39, 6, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 7, 9, 14, 4, 24, 22, 6, 8, 13, 17, 11, 39, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 39, 25, 5, 13, 6, 63, 4, 24, 13, 8, 27, 10, 14, 5, 12, 4, 21, 5, 9, 5, 13, 7, 15, 39, 24, 16, 13, 24, 8, 12, 5, 4, 7, 13, 17, 11, 10, 6, 5, 17, 6, 16, 13, 5, 12, 4, 64, 40, 47, 54, 32, 23, 4, 53, 49, 32, 23, 4, 54, 8, 40, 47, 54, 32, 7, 23, 4, 69, 52, 43, 23, 4, 51, 10, 12, 6, 10, 15, 40, 5, 13, 6, 23, 4, 69, 52, 48, 5, 6, 26, 26, 26, 63, 4, 19, 8, 13, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 61, 9, 14, 5, 13, 12, 6, 7, 9, 14, 10, 9, 21, 4, 64, 48, 52, 61, 63, 4, 7, 9, 14, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 53, 5, 9, 5, 13, 7, 6, 10, 8, 9, 4, 64, 48, 52, 53, 63, 4, 20, 10, 6, 11, 4, 8, 27, 5, 13, 4, 6, 11, 10, 13, 6, 22, 39, 6, 20, 8, 4, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 4, 18, 8, 14, 5, 15, 12, 4, 10, 9, 4, 8, 9, 5, 4, 11, 16, 9, 14, 13, 5, 14, 4, 24, 15, 16, 12, 4, 15, 7, 9, 21, 16, 7, 21, 5, 12, 4, 7, 9, 14, 4, 14, 5, 5, 24, 4, 10, 9, 6, 5, 13, 8, 24, 5, 13, 7, 25, 10, 15, 10, 6, 22, 4, 25, 5, 6, 20, 5, 5, 9, 4, 58, 7, 37, 23, 4, 49, 22, 32, 8, 13, 17, 11, 4, 7, 9, 14, 4, 32, 5, 9, 12, 8, 13, 55, 15, 8, 20, 26, 2], [4, 40, 47, 54, 32, 4, 10, 12, 4, 14, 5, 12, 10, 21, 9, 5, 14, 4, 6, 8, 4, 24, 13, 5, 39, 6, 13, 7, 10, 9, 4, 14, 5, 5, 24, 4, 25, 10, 14, 10, 13, 5, 17, 6, 10, 8, 9, 7, 15, 4, 13, 5, 24, 13, 5, 12, 5, 9, 6, 7, 6, 10, 8, 9, 12, 4, 19, 13, 8, 18, 4, 16, 9, 15, 7, 25, 5, 15, 5, 14, 4, 6, 5, 37, 6, 4, 25, 22, 4, 46, 8, 10, 9, 6, 15, 22, 4, 17, 8, 9, 14, 10, 6, 10, 8, 9, 10, 9, 21, 4, 8, 9, 4, 25, 8, 6, 11, 4, 15, 5, 19, 6, 4, 7, 9, 14, 4, 13, 10, 21, 11, 6, 4, 17, 8, 9, 6, 5, 37, 6, 4, 10, 9, 4, 7, 15, 15, 4, 15, 7, 22, 5, 13, 12, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [4, 32, 11, 5, 4, 45, 16, 10, 17, 28, 4, 25, 13, 8, 20, 9, 4, 19, 8, 37, 4, 46, 16, 18, 24, 12, 4, 8, 27, 5, 13, 4, 6, 11, 5, 4, 15, 7, 57, 22, 4, 14, 8, 21, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ], 'attention_mask': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] } # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase ,model_name='microsoft/speecht5_asr' ,revision='c5ef64c71905caeccde0e4462ef3f9077224c524' ,sequences=__UpperCAmelCase ,)
154
0
"""simple docstring""" import json import os from functools import lru_cache from typing import Dict, List, Optional, Tuple, Union import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...tokenization_utils_base import BatchEncoding, EncodedInput from ...utils import PaddingStrategy, logging UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'} # See all LED models at https://huggingface.co/models?filter=LED UpperCamelCase_ = { '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_ = { 'allenai/led-base-16384': 16384, } @lru_cache() # Copied from transformers.models.bart.tokenization_bart.bytes_to_unicode def UpperCamelCase ( ) ->List[str]: """simple docstring""" a_ = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) a_ = bs[:] a_ = 0 for b in range(2**8 ): if b not in bs: bs.append(lowercase_ ) cs.append(2**8 + n ) n += 1 a_ = [chr(lowercase_ ) for n in cs] return dict(zip(lowercase_ , lowercase_ ) ) def UpperCamelCase ( UpperCAmelCase ) ->List[Any]: """simple docstring""" a_ = set() a_ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) a_ = char return pairs class snake_case ( A_ ): a_ : Tuple = VOCAB_FILES_NAMES a_ : str = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Any = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase="replace" , __UpperCAmelCase="<s>" , __UpperCAmelCase="</s>" , __UpperCAmelCase="</s>" , __UpperCAmelCase="<s>" , __UpperCAmelCase="<unk>" , __UpperCAmelCase="<pad>" , __UpperCAmelCase="<mask>" , __UpperCAmelCase=False , **__UpperCAmelCase , ) ->List[str]: a_ = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_) if isinstance(snake_case_ , snake_case_) else bos_token a_ = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_) if isinstance(snake_case_ , snake_case_) else eos_token a_ = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_) if isinstance(snake_case_ , snake_case_) else sep_token a_ = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_) if isinstance(snake_case_ , snake_case_) else cls_token a_ = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_) if isinstance(snake_case_ , snake_case_) else unk_token a_ = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_) if isinstance(snake_case_ , snake_case_) else pad_token # Mask token behave like a normal word, i.e. include the space before it a_ = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_) if isinstance(snake_case_ , snake_case_) else mask_token super().__init__( errors=snake_case_ , bos_token=snake_case_ , eos_token=snake_case_ , unk_token=snake_case_ , sep_token=snake_case_ , cls_token=snake_case_ , pad_token=snake_case_ , mask_token=snake_case_ , add_prefix_space=snake_case_ , **snake_case_ , ) with open(snake_case_ , encoding="utf-8") as vocab_handle: a_ = json.load(snake_case_) a_ = {v: k for k, v in self.encoder.items()} a_ = errors # how to handle errors in decoding a_ = bytes_to_unicode() a_ = {v: k for k, v in self.byte_encoder.items()} with open(snake_case_ , encoding="utf-8") as merges_handle: a_ = merges_handle.read().split("\n")[1:-1] a_ = [tuple(merge.split()) for merge in bpe_merges] a_ = dict(zip(snake_case_ , range(len(snake_case_)))) a_ = {} a_ = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions a_ = re.compile(r"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+") @property # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.vocab_size def UpperCAmelCase__ ( self) ->Dict: return len(self.encoder) def UpperCAmelCase__ ( self) ->str: return dict(self.encoder , **self.added_tokens_encoder) def UpperCAmelCase__ ( self , __UpperCAmelCase) ->List[str]: if token in self.cache: return self.cache[token] a_ = tuple(snake_case_) a_ = get_pairs(snake_case_) if not pairs: return token while True: a_ = min(snake_case_ , key=lambda __UpperCAmelCase: self.bpe_ranks.get(snake_case_ , float("inf"))) if bigram not in self.bpe_ranks: break a_ , a_ = bigram a_ = [] a_ = 0 while i < len(snake_case_): try: a_ = word.index(snake_case_ , snake_case_) except ValueError: new_word.extend(word[i:]) break else: new_word.extend(word[i:j]) a_ = j if word[i] == first and i < len(snake_case_) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 a_ = tuple(snake_case_) a_ = new_word if len(snake_case_) == 1: break else: a_ = get_pairs(snake_case_) a_ = " ".join(snake_case_) a_ = word return word def UpperCAmelCase__ ( self , __UpperCAmelCase) ->List[Any]: a_ = [] for token in re.findall(self.pat , snake_case_): a_ = "".join( self.byte_encoder[b] for b in token.encode("utf-8")) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(snake_case_).split(" ")) return bpe_tokens def UpperCAmelCase__ ( self , __UpperCAmelCase) ->Any: return self.encoder.get(snake_case_ , self.encoder.get(self.unk_token)) def UpperCAmelCase__ ( self , __UpperCAmelCase) ->int: return self.decoder.get(snake_case_) def UpperCAmelCase__ ( self , __UpperCAmelCase) ->Optional[int]: a_ = "".join(snake_case_) a_ = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8" , errors=self.errors) return text def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->Tuple[str]: if not os.path.isdir(snake_case_): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''') return a_ = os.path.join( snake_case_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) a_ = os.path.join( snake_case_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]) with open(snake_case_ , "w" , encoding="utf-8") as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=snake_case_ , ensure_ascii=snake_case_) + "\n") a_ = 0 with open(snake_case_ , "w" , encoding="utf-8") as writer: writer.write("#version: 0.2\n") for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda __UpperCAmelCase: kv[1]): if index != token_index: logger.warning( F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' " Please check that the tokenizer is not corrupted!") a_ = token_index writer.write(" ".join(snake_case_) + "\n") index += 1 return vocab_file, merge_file def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ = [self.cls_token_id] a_ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = False) ->List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case_ , token_ids_a=snake_case_ , already_has_special_tokens=snake_case_) if token_ids_a is None: return [1] + ([0] * len(snake_case_)) + [1] return [1] + ([0] * len(snake_case_)) + [1, 1] + ([0] * len(snake_case_)) + [1] def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->List[int]: a_ = [self.sep_token_id] a_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase=False , **__UpperCAmelCase) ->Tuple: a_ = kwargs.pop("add_prefix_space" , self.add_prefix_space) if (is_split_into_words or add_prefix_space) and (len(snake_case_) > 0 and not text[0].isspace()): a_ = " " + text return (text, kwargs) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = PaddingStrategy.DO_NOT_PAD , __UpperCAmelCase = None , __UpperCAmelCase = None , ) ->dict: a_ = super()._pad( encoded_inputs=snake_case_ , max_length=snake_case_ , padding_strategy=snake_case_ , pad_to_multiple_of=snake_case_ , return_attention_mask=snake_case_ , ) # Load from model defaults if return_attention_mask is None: a_ = "attention_mask" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: a_ = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. a_ = len(encoded_inputs["global_attention_mask"]) != len(snake_case_) if needs_to_be_padded: a_ = len(snake_case_) - 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_ = ( encoded_inputs["global_attention_mask"] + [-1] * difference ) elif self.padding_side == "left": a_ = [-1] * difference + encoded_inputs[ "global_attention_mask" ] else: raise ValueError("Invalid padding strategy:" + str(self.padding_side)) return encoded_inputs
243
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE = logging.get_logger(__name__) SCREAMING_SNAKE_CASE = { # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert } class UpperCAmelCase_ ( A_ ): lowercase__ = '''megatron-bert''' def __init__( self : Optional[Any] , snake_case_ : Optional[Any]=29_056 , snake_case_ : int=1_024 , snake_case_ : Optional[int]=24 , snake_case_ : str=16 , snake_case_ : str=4_096 , snake_case_ : Tuple="gelu" , snake_case_ : List[str]=0.1 , snake_case_ : Optional[int]=0.1 , snake_case_ : List[str]=512 , snake_case_ : Optional[int]=2 , snake_case_ : Dict=0.02 , snake_case_ : Optional[Any]=1e-12 , snake_case_ : Optional[Any]=0 , snake_case_ : int="absolute" , snake_case_ : List[str]=True , **snake_case_ : Tuple , ) -> int: '''simple docstring''' super().__init__(pad_token_id=snake_case_ , **snake_case_ ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache
247
0
"""simple docstring""" import numpy as np def _lowerCAmelCase ( UpperCamelCase_ ): return 1 / (1 + np.exp(-vector )) if __name__ == "__main__": import doctest doctest.testmod()
357
"""simple docstring""" import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) __magic_name__ = { "sample_size": 32, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": 1000, "block_out_channels": [32, 64], "attention_head_dim": 8, "down_block_types": [ "ResnetDownsampleBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "scale_shift", "upsample_type": "resnet", "downsample_type": "resnet", } __magic_name__ = { "sample_size": 64, "in_channels": 3, "out_channels": 3, "layers_per_block": 3, "num_class_embeds": 1000, "block_out_channels": [192, 192 * 2, 192 * 3, 192 * 4], "attention_head_dim": 64, "down_block_types": [ "ResnetDownsampleBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "scale_shift", "upsample_type": "resnet", "downsample_type": "resnet", } __magic_name__ = { "sample_size": 256, "in_channels": 3, "out_channels": 3, "layers_per_block": 2, "num_class_embeds": None, "block_out_channels": [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4], "attention_head_dim": 64, "down_block_types": [ "ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", ], "up_block_types": [ "AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D", ], "resnet_time_scale_shift": "default", "upsample_type": "resnet", "downsample_type": "resnet", } __magic_name__ = { "num_train_timesteps": 40, "sigma_min": 0.002, "sigma_max": 80.0, } __magic_name__ = { "num_train_timesteps": 201, "sigma_min": 0.002, "sigma_max": 80.0, } __magic_name__ = { "num_train_timesteps": 151, "sigma_min": 0.002, "sigma_max": 80.0, } def _lowerCAmelCase ( UpperCamelCase_ ): if isinstance(UpperCamelCase_ , UpperCamelCase_ ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise argparse.ArgumentTypeError("""boolean value expected""" ) def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_=False ): __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.in_layers.0.weight"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.in_layers.0.bias"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.in_layers.2.weight"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.in_layers.2.bias"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.emb_layers.1.weight"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.emb_layers.1.bias"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.out_layers.0.weight"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.out_layers.0.bias"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.out_layers.3.weight"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.out_layers.3.bias"] if has_skip: __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.skip_connection.weight"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.skip_connection.bias"] return new_checkpoint def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_=None ): __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.qkv.weight"].chunk(3 , dim=0 ) __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.qkv.bias"].chunk(3 , dim=0 ) __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.norm.weight"] __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.norm.bias"] __SCREAMING_SNAKE_CASE = weight_q.squeeze(-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = bias_q.squeeze(-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = weight_k.squeeze(-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = bias_k.squeeze(-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = weight_v.squeeze(-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = bias_v.squeeze(-1 ).squeeze(-1 ) __SCREAMING_SNAKE_CASE = ( checkpoint[f"{old_prefix}.proj_out.weight"].squeeze(-1 ).squeeze(-1 ) ) __SCREAMING_SNAKE_CASE = checkpoint[f"{old_prefix}.proj_out.bias"].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = torch.load(UpperCamelCase_ , map_location="""cpu""" ) __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = checkpoint["""time_embed.0.weight"""] __SCREAMING_SNAKE_CASE = checkpoint["""time_embed.0.bias"""] __SCREAMING_SNAKE_CASE = checkpoint["""time_embed.2.weight"""] __SCREAMING_SNAKE_CASE = checkpoint["""time_embed.2.bias"""] if unet_config["num_class_embeds"] is not None: __SCREAMING_SNAKE_CASE = checkpoint["""label_emb.weight"""] __SCREAMING_SNAKE_CASE = checkpoint["""input_blocks.0.0.weight"""] __SCREAMING_SNAKE_CASE = checkpoint["""input_blocks.0.0.bias"""] __SCREAMING_SNAKE_CASE = unet_config["""down_block_types"""] __SCREAMING_SNAKE_CASE = unet_config["""layers_per_block"""] __SCREAMING_SNAKE_CASE = unet_config["""attention_head_dim"""] __SCREAMING_SNAKE_CASE = unet_config["""block_out_channels"""] __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = channels_list[0] for i, layer_type in enumerate(UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = channels_list[i] __SCREAMING_SNAKE_CASE = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = f"down_blocks.{i}.resnets.{j}" __SCREAMING_SNAKE_CASE = f"input_blocks.{current_layer}.0" __SCREAMING_SNAKE_CASE = True if j == 0 and downsample_block_has_skip else False __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , has_skip=UpperCamelCase_ ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = f"down_blocks.{i}.resnets.{j}" __SCREAMING_SNAKE_CASE = f"input_blocks.{current_layer}.0" __SCREAMING_SNAKE_CASE = True if j == 0 and downsample_block_has_skip else False __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , has_skip=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = f"down_blocks.{i}.attentions.{j}" __SCREAMING_SNAKE_CASE = f"input_blocks.{current_layer}.1" __SCREAMING_SNAKE_CASE = convert_attention( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) current_layer += 1 if i != len(UpperCamelCase_ ) - 1: __SCREAMING_SNAKE_CASE = f"down_blocks.{i}.downsamplers.0" __SCREAMING_SNAKE_CASE = f"input_blocks.{current_layer}.0" __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) current_layer += 1 __SCREAMING_SNAKE_CASE = current_channels # hardcoded the mid-block for now __SCREAMING_SNAKE_CASE = """mid_block.resnets.0""" __SCREAMING_SNAKE_CASE = """middle_block.0""" __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = """mid_block.attentions.0""" __SCREAMING_SNAKE_CASE = """middle_block.1""" __SCREAMING_SNAKE_CASE = convert_attention(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = """mid_block.resnets.1""" __SCREAMING_SNAKE_CASE = """middle_block.2""" __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = unet_config["""up_block_types"""] for i, layer_type in enumerate(UpperCamelCase_ ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): __SCREAMING_SNAKE_CASE = f"up_blocks.{i}.resnets.{j}" __SCREAMING_SNAKE_CASE = f"output_blocks.{current_layer}.0" __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , has_skip=UpperCamelCase_ ) current_layer += 1 if i != len(UpperCamelCase_ ) - 1: __SCREAMING_SNAKE_CASE = f"up_blocks.{i}.upsamplers.0" __SCREAMING_SNAKE_CASE = f"output_blocks.{current_layer-1}.1" __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): __SCREAMING_SNAKE_CASE = f"up_blocks.{i}.resnets.{j}" __SCREAMING_SNAKE_CASE = f"output_blocks.{current_layer}.0" __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , has_skip=UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = f"up_blocks.{i}.attentions.{j}" __SCREAMING_SNAKE_CASE = f"output_blocks.{current_layer}.1" __SCREAMING_SNAKE_CASE = convert_attention( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) current_layer += 1 if i != len(UpperCamelCase_ ) - 1: __SCREAMING_SNAKE_CASE = f"up_blocks.{i}.upsamplers.0" __SCREAMING_SNAKE_CASE = f"output_blocks.{current_layer-1}.2" __SCREAMING_SNAKE_CASE = convert_resnet(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = checkpoint["""out.0.weight"""] __SCREAMING_SNAKE_CASE = checkpoint["""out.0.bias"""] __SCREAMING_SNAKE_CASE = checkpoint["""out.2.weight"""] __SCREAMING_SNAKE_CASE = checkpoint["""out.2.bias"""] return new_checkpoint if __name__ == "__main__": __magic_name__ = argparse.ArgumentParser() parser.add_argument("--unet_path", default=None, type=str, required=True, help="Path to the unet.pt to convert.") parser.add_argument( "--dump_path", default=None, type=str, required=True, help="Path to output the converted UNet model." ) parser.add_argument("--class_cond", default=True, type=str, help="Whether the model is class-conditional.") __magic_name__ = parser.parse_args() __magic_name__ = strabool(args.class_cond) __magic_name__ = os.path.basename(args.unet_path) print(F"""Checkpoint: {ckpt_name}""") # Get U-Net config if "imagenet64" in ckpt_name: __magic_name__ = IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): __magic_name__ = LSUN_256_UNET_CONFIG elif "test" in ckpt_name: __magic_name__ = TEST_UNET_CONFIG else: raise ValueError(F"""Checkpoint type {ckpt_name} is not currently supported.""") if not args.class_cond: __magic_name__ = None __magic_name__ = con_pt_to_diffuser(args.unet_path, unet_config) __magic_name__ = UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: __magic_name__ = CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: __magic_name__ = CT_IMAGENET_64_SCHEDULER_CONFIG elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): __magic_name__ = CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(F"""Checkpoint type {ckpt_name} is not currently supported.""") __magic_name__ = CMStochasticIterativeScheduler(**scheduler_config) __magic_name__ = ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
255
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) _UpperCAmelCase : List[str] = { "EleutherAI/gpt-neox-20b": "https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json", # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox } class lowercase ( _SCREAMING_SNAKE_CASE ): __lowercase : List[str] = "gpt_neox" def __init__( self , A_=50_432 , A_=6_144 , A_=44 , A_=64 , A_=24_576 , A_="gelu" , A_=0.25 , A_=10_000 , A_=0.0 , A_=0.0 , A_=0.1 , A_=2_048 , A_=0.02 , A_=1e-5 , A_=True , A_=0 , A_=2 , A_=False , A_=True , A_=None , **A_ , ) -> Tuple: """simple docstring""" super().__init__(bos_token_id=A_ , eos_token_id=A_ , **A_ ) UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = rotary_pct UpperCamelCase = rotary_emb_base UpperCamelCase = attention_dropout UpperCamelCase = hidden_dropout UpperCamelCase = classifier_dropout UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = use_cache UpperCamelCase = tie_word_embeddings UpperCamelCase = use_parallel_residual UpperCamelCase = rope_scaling self._rope_scaling_validation() if self.hidden_size % self.num_attention_heads != 0: raise ValueError( 'The hidden size is not divisble by the number of attention heads! Make sure to update them!' ) def __UpperCamelCase ( self ) -> Tuple: """simple docstring""" if self.rope_scaling is None: return if not isinstance(self.rope_scaling , A_ ) or len(self.rope_scaling ) != 2: raise ValueError( '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ' F'''got {self.rope_scaling}''' ) UpperCamelCase = self.rope_scaling.get('type' , A_ ) UpperCamelCase = self.rope_scaling.get('factor' , A_ ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( F'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''' ) if rope_scaling_factor is None or not isinstance(A_ , A_ ) or rope_scaling_factor <= 1.0: raise ValueError(F'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''' )
222
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _UpperCAmelCase : Union[str, Any] = { "configuration_instructblip": [ "INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "InstructBlipConfig", "InstructBlipQFormerConfig", "InstructBlipVisionConfig", ], "processing_instructblip": ["InstructBlipProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase : List[Any] = [ "INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "InstructBlipQFormerModel", "InstructBlipPreTrainedModel", "InstructBlipForConditionalGeneration", "InstructBlipVisionModel", ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys _UpperCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
222
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _UpperCAmelCase : Dict = {"""configuration_swin""": ["""SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SwinConfig""", """SwinOnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase : Union[str, Any] = [ """SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""", """SwinForImageClassification""", """SwinForMaskedImageModeling""", """SwinModel""", """SwinPreTrainedModel""", """SwinBackbone""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase : int = [ """TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFSwinForImageClassification""", """TFSwinForMaskedImageModeling""", """TFSwinModel""", """TFSwinPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys _UpperCAmelCase : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
9
'''simple docstring''' import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class a__ ( __A , unittest.TestCase ): """simple docstring""" __UpperCamelCase : str = DebertaTokenizer __UpperCamelCase : str = True __UpperCamelCase : Any = DebertaTokenizerFast def _snake_case (self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __lowerCAmelCase = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] __lowerCAmelCase = dict(zip(__lowercase , range(len(__lowercase ) ) ) ) __lowerCAmelCase = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __lowerCAmelCase = {'''unk_token''': '''[UNK]'''} __lowerCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) __lowerCAmelCase = 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(__lowercase ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__lowercase ) ) def _snake_case (self , **__lowercase ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__lowercase ) def _snake_case (self , __lowercase ): __lowerCAmelCase = '''lower newer''' __lowerCAmelCase = '''lower newer''' return input_text, output_text def _snake_case (self ): __lowerCAmelCase = self.get_tokenizer() __lowerCAmelCase = '''lower newer''' __lowerCAmelCase = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] __lowerCAmelCase = tokenizer.tokenize(__lowercase ) self.assertListEqual(__lowercase , __lowercase ) __lowerCAmelCase = tokens + [tokenizer.unk_token] __lowerCAmelCase = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase ) , __lowercase ) def _snake_case (self ): __lowerCAmelCase = self.get_tokenizer() __lowerCAmelCase = tokenizer('''Hello''' , '''World''' ) __lowerCAmelCase = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , __lowercase ) @slow def _snake_case (self ): __lowerCAmelCase = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) __lowerCAmelCase = tokenizer.encode('''sequence builders''' , add_special_tokens=__lowercase ) __lowerCAmelCase = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__lowercase ) __lowerCAmelCase = tokenizer.encode( '''sequence builders''' , add_special_tokens=__lowercase , add_prefix_space=__lowercase ) __lowerCAmelCase = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__lowercase , add_prefix_space=__lowercase ) __lowerCAmelCase = tokenizer.build_inputs_with_special_tokens(__lowercase ) __lowerCAmelCase = tokenizer.build_inputs_with_special_tokens(__lowercase , __lowercase ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _snake_case (self ): __lowerCAmelCase = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: __lowerCAmelCase = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) __lowerCAmelCase = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] __lowerCAmelCase = tokenizer(__lowercase , padding=__lowercase ) __lowerCAmelCase = [tokenizer.decode(__lowercase , skip_special_tokens=__lowercase ) for seq in encoding['''input_ids''']] # fmt: off __lowerCAmelCase = { '''input_ids''': [ [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on __lowerCAmelCase = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , __lowercase ) for expected, decoded in zip(__lowercase , __lowercase ): self.assertEqual(__lowercase , __lowercase )
9
1
"""simple docstring""" import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer lowerCAmelCase = """bart""" lowerCAmelCase = True @st.cache(allow_output_mutation=snake_case_ ) def lowerCAmelCase_ ( ) ->int: if LOAD_DENSE_INDEX: lowerCamelCase__ : Optional[Any] =AutoTokenizer.from_pretrained('yjernite/retribert-base-uncased' ) lowerCamelCase__ : int =AutoModel.from_pretrained('yjernite/retribert-base-uncased' ).to('cuda:0' ) lowerCamelCase__ : Optional[int] =qar_model.eval() else: lowerCamelCase__ , lowerCamelCase__ : List[str] =(None, None) if MODEL_TYPE == "bart": lowerCamelCase__ : Any =AutoTokenizer.from_pretrained('yjernite/bart_eli5' ) lowerCamelCase__ : str =AutoModelForSeqaSeqLM.from_pretrained('yjernite/bart_eli5' ).to('cuda:0' ) lowerCamelCase__ : Optional[Any] =torch.load('seq2seq_models/eli5_bart_model_blm_2.pth' ) sas_model.load_state_dict(save_dict['model'] ) lowerCamelCase__ : Any =sas_model.eval() else: lowerCamelCase__ , lowerCamelCase__ : Dict =make_qa_sas_model( model_name='t5-small' , from_file='seq2seq_models/eli5_t5_model_1024_4.pth' , device='cuda:0' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=snake_case_ ) def lowerCAmelCase_ ( ) ->List[Any]: if LOAD_DENSE_INDEX: lowerCamelCase__ : Optional[Any] =faiss.StandardGpuResources() lowerCamelCase__ : str =datasets.load_dataset(path='wiki_snippets' , name='wiki40b_en_100_0' )['train'] lowerCamelCase__ : int =np.memmap( 'wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat' , dtype='float32' , mode='r' , shape=(wikiaab_passages.num_rows, 1_2_8) , ) lowerCamelCase__ : Dict =faiss.IndexFlatIP(1_2_8 ) lowerCamelCase__ : Any =faiss.index_cpu_to_gpu(snake_case_ , 1 , snake_case_ ) wikiaab_gpu_index_flat.add(snake_case_ ) # TODO fix for larger GPU else: lowerCamelCase__ , lowerCamelCase__ : str =(None, None) lowerCamelCase__ : Optional[Any] =Elasticsearch([{'host': 'localhost', 'port': '9200'}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=snake_case_ ) def lowerCAmelCase_ ( ) ->Optional[int]: lowerCamelCase__ : List[str] =datasets.load_dataset('eli5' , name='LFQA_reddit' ) lowerCamelCase__ : List[Any] =elia['train_eli5'] lowerCamelCase__ : Optional[int] =np.memmap( 'eli5_questions_reps.dat' , dtype='float32' , mode='r' , shape=(elia_train.num_rows, 1_2_8) ) lowerCamelCase__ : Optional[Any] =faiss.IndexFlatIP(1_2_8 ) eli5_train_q_index.add(snake_case_ ) return (elia_train, eli5_train_q_index) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = load_indexes() lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = load_models() lowerCAmelCase , lowerCAmelCase = load_train_data() def lowerCAmelCase_ ( snake_case_ : str , snake_case_ : Any=1_0 ) ->List[Any]: lowerCamelCase__ : List[str] =embed_questions_for_retrieval([question] , snake_case_ , snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Tuple =eli5_train_q_index.search(snake_case_ , snake_case_ ) lowerCamelCase__ : List[str] =[elia_train[int(snake_case_ )] for i in I[0]] return nn_examples def lowerCAmelCase_ ( snake_case_ : int , snake_case_ : Any="wiki40b" , snake_case_ : Union[str, Any]="dense" , snake_case_ : Any=1_0 ) ->str: if source == "none": lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =(' <P> '.join(['' for _ in range(1_1 )] ).strip(), []) else: if method == "dense": lowerCamelCase__ , lowerCamelCase__ : Dict =query_qa_dense_index( snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) else: lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =query_es_index( snake_case_ , snake_case_ , index_name='english_wiki40b_snippets_100w' , n_results=snake_case_ , ) lowerCamelCase__ : List[Any] =[ (res['article_title'], res['section_title'].strip(), res['score'], res['passage_text']) for res in hit_lst ] lowerCamelCase__ : List[Any] ='question: {} context: {}'.format(snake_case_ , snake_case_ ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda snake_case_ : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda snake_case_ : None), } ) def lowerCAmelCase_ ( snake_case_ : int , snake_case_ : Any , snake_case_ : str , snake_case_ : List[Any]=6_4 , snake_case_ : List[str]=2_5_6 , snake_case_ : Union[str, Any]=False , snake_case_ : int=2 , snake_case_ : Optional[int]=0.95 , snake_case_ : str=0.8 ) ->Tuple: with torch.no_grad(): lowerCamelCase__ : Optional[int] =qa_sas_generate( snake_case_ , snake_case_ , snake_case_ , num_answers=1 , num_beams=snake_case_ , min_len=snake_case_ , max_len=snake_case_ , do_sample=snake_case_ , temp=snake_case_ , top_p=snake_case_ , top_k=snake_case_ , max_input_length=1_0_2_4 , device='cuda:0' , )[0] return (answer, support_list) st.title("""Long Form Question Answering with ELI5""") # Start sidebar lowerCAmelCase = """<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>""" lowerCAmelCase = """ <html> <head> <style> .img-container { padding-left: 90px; padding-right: 90px; padding-top: 50px; padding-bottom: 50px; background-color: #f0f3f9; } </style> </head> <body> <span class=\"img-container\"> <!-- Inline parent element --> %s </span> </body> </html> """ % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia lowerCAmelCase = """ This demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html). First, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset, a pre-processed fixed snapshot of Wikipedia. """ st.sidebar.markdown(description, unsafe_allow_html=True) lowerCAmelCase = [ """Answer the question""", """View the retrieved document only""", """View the most similar ELI5 question and answer""", """Show me everything, please!""", ] lowerCAmelCase = st.sidebar.checkbox("""Demo options""") if demo_options: lowerCAmelCase = st.sidebar.selectbox( """""", action_list, index=3, ) lowerCAmelCase = action_list.index(action_st) lowerCAmelCase = st.sidebar.selectbox( """""", ["""Show full text of passages""", """Show passage section titles"""], index=0, ) lowerCAmelCase = show_type == """Show full text of passages""" else: lowerCAmelCase = 3 lowerCAmelCase = True lowerCAmelCase = st.sidebar.checkbox("""Retrieval options""") if retrieval_options: lowerCAmelCase = """ ### Information retriever options The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs. The answer is then generated by sequence to sequence model which takes the question and retrieved document as input. """ st.sidebar.markdown(retriever_info) lowerCAmelCase = st.sidebar.selectbox("""Which Wikipedia format should the model use?""", ["""wiki40b""", """none"""]) lowerCAmelCase = st.sidebar.selectbox("""Which Wikipedia indexer should the model use?""", ["""dense""", """sparse""", """mixed"""]) else: lowerCAmelCase = """wiki40b""" lowerCAmelCase = """dense""" lowerCAmelCase = """beam""" lowerCAmelCase = 2 lowerCAmelCase = 64 lowerCAmelCase = 2_56 lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = st.sidebar.checkbox("""Generation options""") if generate_options: lowerCAmelCase = """ ### Answer generation options The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large) weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with **beam** search, or **sample** from the decoder's output probabilities. """ st.sidebar.markdown(generate_info) lowerCAmelCase = st.sidebar.selectbox("""Would you like to use beam search or sample an answer?""", ["""beam""", """sampled"""]) lowerCAmelCase = st.sidebar.slider( """Minimum generation length""", min_value=8, max_value=2_56, value=64, step=8, format=None, key=None ) lowerCAmelCase = st.sidebar.slider( """Maximum generation length""", min_value=64, max_value=5_12, value=2_56, step=16, format=None, key=None ) if sampled == "beam": lowerCAmelCase = st.sidebar.slider("""Beam size""", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: lowerCAmelCase = st.sidebar.slider( """Nucleus sampling p""", min_value=0.1, max_value=1.0, value=0.9_5, step=0.0_1, format=None, key=None ) lowerCAmelCase = st.sidebar.slider( """Temperature""", min_value=0.1, max_value=1.0, value=0.7, step=0.0_1, format=None, key=None ) lowerCAmelCase = None # start main text lowerCAmelCase = [ """<MY QUESTION>""", """How do people make chocolate?""", """Why do we get a fever when we are sick?""", """How can different animals perceive different colors?""", """What is natural language processing?""", """What's the best way to treat a sunburn?""", """What exactly are vitamins ?""", """How does nuclear energy provide electricity?""", """What's the difference between viruses and bacteria?""", """Why are flutes classified as woodwinds when most of them are made out of metal ?""", """Why do people like drinking coffee even though it tastes so bad?""", """What happens when wine ages? How does it make the wine taste better?""", """If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?""", """How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?""", """How does New Zealand have so many large bird predators?""", ] lowerCAmelCase = st.selectbox( """What would you like to ask? ---- select <MY QUESTION> to enter a new query""", questions_list, index=1, ) if question_s == "<MY QUESTION>": lowerCAmelCase = st.text_input("""Enter your question here:""", """""") else: lowerCAmelCase = question_s if st.button("""Show me!"""): if action in [0, 1, 3]: if index_type == "mixed": lowerCAmelCase , lowerCAmelCase = make_support(question, source=wiki_source, method="""dense""", n_results=10) lowerCAmelCase , lowerCAmelCase = make_support(question, source=wiki_source, method="""sparse""", n_results=10) lowerCAmelCase = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] lowerCAmelCase = support_list[:10] lowerCAmelCase = """<P> """ + """ <P> """.join([res[-1] for res in support_list]) else: lowerCAmelCase , lowerCAmelCase = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: lowerCAmelCase , lowerCAmelCase = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == """sampled"""), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("""### The model generated answer is:""") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("""--- \n ### The model is drawing information from the following Wikipedia passages:""") for i, res in enumerate(support_list): lowerCAmelCase = """https://en.wikipedia.org/wiki/{}""".format(res[0].replace(""" """, """_""")) lowerCAmelCase = res[1].strip() if sec_titles == "": lowerCAmelCase = """[{}]({})""".format(res[0], wiki_url) else: lowerCAmelCase = sec_titles.split(""" & """) lowerCAmelCase = """ & """.join( ["""[{}]({}#{})""".format(sec.strip(), wiki_url, sec.strip().replace(""" """, """_""")) for sec in sec_list] ) st.markdown( """{0:02d} - **Article**: {1:<18} <br> _Section_: {2}""".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( """> <span style=\"font-family:arial; font-size:10pt;\">""" + res[-1] + """</span>""", unsafe_allow_html=True ) if action in [2, 3]: lowerCAmelCase = find_nearest_training(question) lowerCAmelCase = nn_train_list[0] st.markdown( """--- \n ### The most similar question in the ELI5 training set was: \n\n {}""".format(train_exple["""title"""]) ) lowerCAmelCase = [ """{}. {}""".format(i + 1, """ \n""".join([line.strip() for line in ans.split("""\n""") if line.strip() != """"""])) for i, (ans, sc) in enumerate(zip(train_exple["""answers"""]["""text"""], train_exple["""answers"""]["""score"""])) if i == 0 or sc > 2 ] st.markdown("""##### Its answers were: \n\n {}""".format("""\n""".join(answers_st))) lowerCAmelCase = """ --- **Disclaimer** *The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system. Evaluating biases of such a model and ensuring factual generations are still very much open research problems. Therefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.* """ st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
126
"""simple docstring""" import unittest import numpy as np from transformers import BertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.bert.modeling_flax_bert import ( FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForNextSentencePrediction, FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertModel, ) class A_ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[str] , lowerCamelCase_ :str , lowerCamelCase_ :List[Any]=13 , lowerCamelCase_ :Any=7 , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :Optional[int]=True , lowerCamelCase_ :Any=True , lowerCamelCase_ :List[str]=99 , lowerCamelCase_ :Dict=32 , lowerCamelCase_ :Union[str, Any]=5 , lowerCamelCase_ :int=4 , lowerCamelCase_ :Optional[Any]=37 , lowerCamelCase_ :Optional[Any]="gelu" , lowerCamelCase_ :List[str]=0.1 , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :List[Any]=512 , lowerCamelCase_ :List[str]=16 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :Tuple=0.02 , lowerCamelCase_ :Tuple=4 , ): """simple docstring""" lowerCamelCase__ : Optional[Any] =parent lowerCamelCase__ : List[Any] =batch_size lowerCamelCase__ : Optional[int] =seq_length lowerCamelCase__ : Optional[int] =is_training lowerCamelCase__ : Optional[Any] =use_attention_mask lowerCamelCase__ : List[Any] =use_token_type_ids lowerCamelCase__ : List[Any] =use_labels lowerCamelCase__ : Any =vocab_size lowerCamelCase__ : int =hidden_size lowerCamelCase__ : Dict =num_hidden_layers lowerCamelCase__ : int =num_attention_heads lowerCamelCase__ : List[str] =intermediate_size lowerCamelCase__ : Dict =hidden_act lowerCamelCase__ : str =hidden_dropout_prob lowerCamelCase__ : Tuple =attention_probs_dropout_prob lowerCamelCase__ : List[Any] =max_position_embeddings lowerCamelCase__ : Tuple =type_vocab_size lowerCamelCase__ : Any =type_sequence_label_size lowerCamelCase__ : Dict =initializer_range lowerCamelCase__ : str =num_choices def UpperCAmelCase__ ( self :Union[str, Any] ): """simple docstring""" lowerCamelCase__ : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ : Any =None if self.use_attention_mask: lowerCamelCase__ : Any =random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase__ : Any =None if self.use_token_type_ids: lowerCamelCase__ : Dict =ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase__ : str =BertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCamelCase_ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase__ ( self :Dict ): """simple docstring""" lowerCamelCase__ : Dict =self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict =config_and_inputs lowerCamelCase__ : int ={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict def UpperCAmelCase__ ( self :Tuple ): """simple docstring""" lowerCamelCase__ : int =self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] =config_and_inputs lowerCamelCase__ : Optional[Any] =True lowerCamelCase__ : Any =floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCamelCase__ : str =ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, ) @require_flax class A_ ( A__ , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = ( ( FlaxBertModel, FlaxBertForPreTraining, FlaxBertForMaskedLM, FlaxBertForMultipleChoice, FlaxBertForQuestionAnswering, FlaxBertForNextSentencePrediction, FlaxBertForSequenceClassification, FlaxBertForTokenClassification, FlaxBertForQuestionAnswering, ) if is_flax_available() else () ) def UpperCAmelCase__ ( self :Tuple ): """simple docstring""" lowerCamelCase__ : str =FlaxBertModelTester(self ) @slow def UpperCAmelCase__ ( self :Optional[int] ): """simple docstring""" lowerCamelCase__ : Dict =FlaxBertModel.from_pretrained('bert-base-cased' ) lowerCamelCase__ : List[str] =model(np.ones((1, 1) ) ) self.assertIsNotNone(lowerCamelCase_ )
126
1
from __future__ import annotations class _UpperCAmelCase : """simple docstring""" def __init__( self : Tuple , lowerCAmelCase_ : int ) -> None: __lowerCAmelCase = data __lowerCAmelCase = None __lowerCAmelCase = None def a_ ( lowerCAmelCase_ : Node | None ): # In Order traversal of the tree if tree: display(tree.left ) print(tree.data ) display(tree.right ) def a_ ( lowerCAmelCase_ : Node | None ): return 1 + max(depth_of_tree(tree.left ), depth_of_tree(tree.right ) ) if tree else 0 def a_ ( lowerCAmelCase_ : Node ): if not tree: return True if tree.left and tree.right: return is_full_binary_tree(tree.left ) and is_full_binary_tree(tree.right ) else: return not tree.left and not tree.right def a_ ( ): # Main function for testing. __lowerCAmelCase = Node(1 ) __lowerCAmelCase = Node(2 ) __lowerCAmelCase = Node(3 ) __lowerCAmelCase = Node(4 ) __lowerCAmelCase = Node(5 ) __lowerCAmelCase = Node(6 ) __lowerCAmelCase = Node(7 ) __lowerCAmelCase = Node(8 ) __lowerCAmelCase = Node(9 ) print(is_full_binary_tree(lowerCAmelCase_ ) ) print(depth_of_tree(lowerCAmelCase_ ) ) print('Tree is: ' ) display(lowerCAmelCase_ ) if __name__ == "__main__": main()
207
import gc import unittest import numpy as np import torch from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _UpperCAmelCase ( _UpperCamelCase , unittest.TestCase ): """simple docstring""" a_ = DanceDiffusionPipeline a_ = UNCONDITIONAL_AUDIO_GENERATION_PARAMS a_ = PipelineTesterMixin.required_optional_params - { """callback""", """latents""", """callback_steps""", """output_type""", """num_images_per_prompt""", } a_ = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS a_ = False a_ = False def lowercase ( self : List[Any] ) -> Dict: torch.manual_seed(0 ) __lowerCAmelCase = UNetaDModel( block_out_channels=(3_2, 3_2, 6_4) , extra_in_channels=1_6 , sample_size=5_1_2 , sample_rate=1_6_0_0_0 , in_channels=2 , out_channels=2 , flip_sin_to_cos=lowerCAmelCase_ , use_timestep_embedding=lowerCAmelCase_ , time_embedding_type='fourier' , mid_block_type='UNetMidBlock1D' , down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D') , up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip') , ) __lowerCAmelCase = IPNDMScheduler() __lowerCAmelCase = { 'unet': unet, 'scheduler': scheduler, } return components def lowercase ( self : Tuple , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : List[str]=0 ) -> Any: if str(lowerCAmelCase_ ).startswith('mps' ): __lowerCAmelCase = torch.manual_seed(lowerCAmelCase_ ) else: __lowerCAmelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(lowerCAmelCase_ ) __lowerCAmelCase = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 4, } return inputs def lowercase ( self : Union[str, Any] ) -> int: __lowerCAmelCase = 'cpu' # ensure determinism for the device-dependent torch.Generator __lowerCAmelCase = self.get_dummy_components() __lowerCAmelCase = DanceDiffusionPipeline(**lowerCAmelCase_ ) __lowerCAmelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) __lowerCAmelCase = self.get_dummy_inputs(lowerCAmelCase_ ) __lowerCAmelCase = pipe(**lowerCAmelCase_ ) __lowerCAmelCase = output.audios __lowerCAmelCase = audio[0, -3:, -3:] assert audio.shape == (1, 2, components["unet"].sample_size) __lowerCAmelCase = np.array([-0.72_65, 1.00_00, -0.83_88, 0.11_75, 0.94_98, -1.00_00] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def lowercase ( self : Union[str, Any] ) -> Tuple: return super().test_save_load_local() @skip_mps def lowercase ( self : List[str] ) -> Dict: return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) @skip_mps def lowercase ( self : str ) -> List[str]: return super().test_save_load_optional_components() @skip_mps def lowercase ( self : List[Any] ) -> List[str]: return super().test_attention_slicing_forward_pass() def lowercase ( self : str ) -> int: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" def lowercase ( self : Any ) -> Union[str, Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase ( self : List[str] ) -> List[str]: __lowerCAmelCase = torch_device __lowerCAmelCase = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' ) __lowerCAmelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = pipe(generator=lowerCAmelCase_ , num_inference_steps=1_0_0 , audio_length_in_s=4.0_96 ) __lowerCAmelCase = output.audios __lowerCAmelCase = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) __lowerCAmelCase = np.array([-0.01_92, -0.02_31, -0.03_18, -0.00_59, 0.00_02, -0.00_20] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase ( self : Tuple ) -> Dict: __lowerCAmelCase = torch_device __lowerCAmelCase = DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k' , torch_dtype=torch.floataa ) __lowerCAmelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = pipe(generator=lowerCAmelCase_ , num_inference_steps=1_0_0 , audio_length_in_s=4.0_96 ) __lowerCAmelCase = output.audios __lowerCAmelCase = audio[0, -3:, -3:] assert audio.shape == (1, 2, pipe.unet.sample_size) __lowerCAmelCase = np.array([-0.03_67, -0.04_88, -0.07_71, -0.05_25, -0.04_44, -0.03_41] ) assert np.abs(audio_slice.flatten() - expected_slice ).max() < 1e-2
207
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __lowercase (lowerCAmelCase__ ): _UpperCamelCase = ["image_processor", "tokenizer"] _UpperCamelCase = "ChineseCLIPImageProcessor" _UpperCamelCase = ("BertTokenizer", "BertTokenizerFast") def __init__( self , A_=None , A_=None , **A_ ) ->Tuple: '''simple docstring''' __lowerCAmelCase : int = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : int = kwargs.pop('''feature_extractor''' ) __lowerCAmelCase : Union[str, 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__(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = self.image_processor def __call__( self , A_=None , A_=None , A_=None , **A_ ) ->Optional[Any]: '''simple docstring''' if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: __lowerCAmelCase : Any = self.tokenizer(_SCREAMING_SNAKE_CASE , return_tensors=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) if images is not None: __lowerCAmelCase : Tuple = self.image_processor(_SCREAMING_SNAKE_CASE , return_tensors=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) if text is not None and images is not None: __lowerCAmelCase : str = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**_SCREAMING_SNAKE_CASE ) , tensor_type=_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , *A_ , **A_ ) ->List[str]: '''simple docstring''' return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , *A_ , **A_ ) ->Any: '''simple docstring''' return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) @property def UpperCamelCase__ ( self ) ->Union[str, Any]: '''simple docstring''' __lowerCAmelCase : int = self.tokenizer.model_input_names __lowerCAmelCase : Optional[int] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def UpperCamelCase__ ( self ) ->int: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _SCREAMING_SNAKE_CASE , ) return self.image_processor_class
275
import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class _SCREAMING_SNAKE_CASE ( lowerCAmelCase__): def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=0 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.0_2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE="last" , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , )-> Optional[Any]: lowerCamelCase_ =parent lowerCamelCase_ =batch_size lowerCamelCase_ =seq_length lowerCamelCase_ =is_training lowerCamelCase_ =use_input_lengths lowerCamelCase_ =use_token_type_ids lowerCamelCase_ =use_labels lowerCamelCase_ =gelu_activation lowerCamelCase_ =sinusoidal_embeddings lowerCamelCase_ =causal lowerCamelCase_ =asm lowerCamelCase_ =n_langs lowerCamelCase_ =vocab_size lowerCamelCase_ =n_special lowerCamelCase_ =hidden_size lowerCamelCase_ =num_hidden_layers lowerCamelCase_ =num_attention_heads lowerCamelCase_ =hidden_dropout_prob lowerCamelCase_ =attention_probs_dropout_prob lowerCamelCase_ =max_position_embeddings lowerCamelCase_ =type_vocab_size lowerCamelCase_ =type_sequence_label_size lowerCamelCase_ =initializer_range lowerCamelCase_ =num_labels lowerCamelCase_ =num_choices lowerCamelCase_ =summary_type lowerCamelCase_ =use_proj lowerCamelCase_ =scope def _snake_case ( self )-> Dict: lowerCamelCase_ =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase_ =random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase_ =None if self.use_input_lengths: lowerCamelCase_ =( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length lowerCamelCase_ =None if self.use_token_type_ids: lowerCamelCase_ =ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) lowerCamelCase_ =None lowerCamelCase_ =None lowerCamelCase_ =None if self.use_labels: lowerCamelCase_ =ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase_ =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCamelCase_ =ids_tensor([self.batch_size] , 2 ).float() lowerCamelCase_ =ids_tensor([self.batch_size] , self.num_choices ) lowerCamelCase_ =self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _snake_case ( self )-> List[str]: return FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> str: lowerCamelCase_ =FlaubertModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE , lengths=_SCREAMING_SNAKE_CASE , langs=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE , langs=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> List[Any]: lowerCamelCase_ =FlaubertWithLMHeadModel(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> Optional[Any]: lowerCamelCase_ =FlaubertForQuestionAnsweringSimple(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> Optional[int]: lowerCamelCase_ =FlaubertForQuestionAnswering(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =model( _SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE , cls_index=_SCREAMING_SNAKE_CASE , is_impossible=_SCREAMING_SNAKE_CASE , p_mask=_SCREAMING_SNAKE_CASE , ) lowerCamelCase_ =model( _SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE , cls_index=_SCREAMING_SNAKE_CASE , is_impossible=_SCREAMING_SNAKE_CASE , ) ((lowerCamelCase_) , ) =result_with_labels.to_tuple() lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE ) ((lowerCamelCase_) , ) =result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> Any: lowerCamelCase_ =FlaubertForSequenceClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> List[Any]: lowerCamelCase_ =self.num_labels lowerCamelCase_ =FlaubertForTokenClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> Dict: lowerCamelCase_ =self.num_choices lowerCamelCase_ =FlaubertForMultipleChoice(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() lowerCamelCase_ =input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase_ =token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase_ =input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase_ =model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _snake_case ( self )-> int: lowerCamelCase_ =self.prepare_config_and_inputs() ( ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ( lowerCamelCase_ ) , ) =config_and_inputs lowerCamelCase_ ={ """input_ids""": input_ids, """token_type_ids""": token_type_ids, """lengths""": input_lengths, """attention_mask""": input_mask, } return config, inputs_dict @require_torch class _SCREAMING_SNAKE_CASE ( lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase): _UpperCamelCase:str = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) _UpperCamelCase:str = ( { "feature-extraction": FlaubertModel, "fill-mask": FlaubertWithLMHeadModel, "question-answering": FlaubertForQuestionAnsweringSimple, "text-classification": FlaubertForSequenceClassification, "token-classification": FlaubertForTokenClassification, "zero-shot": FlaubertForSequenceClassification, } if is_torch_available() else {} ) def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )-> Optional[Any]: if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False )-> List[Any]: lowerCamelCase_ =super()._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , return_labels=_SCREAMING_SNAKE_CASE ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": lowerCamelCase_ =torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_SCREAMING_SNAKE_CASE ) return inputs_dict def _snake_case ( self )-> Optional[Any]: lowerCamelCase_ =FlaubertModelTester(self ) lowerCamelCase_ =ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , emb_dim=37 ) def _snake_case ( self )-> Optional[Any]: self.config_tester.run_common_tests() def _snake_case ( self )-> List[Any]: lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> int: lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> Tuple: lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> List[Any]: lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> Optional[Any]: lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> List[Any]: lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> List[str]: lowerCamelCase_ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*_SCREAMING_SNAKE_CASE ) @slow def _snake_case ( self )-> Optional[Any]: for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase_ =FlaubertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) @slow @require_torch_gpu def _snake_case ( self )-> Optional[Any]: lowerCamelCase_ , lowerCamelCase_ =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return lowerCamelCase_ =True lowerCamelCase_ =model_class(config=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =torch.jit.trace( _SCREAMING_SNAKE_CASE , (inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , """traced_model.pt""" ) ) lowerCamelCase_ =torch.jit.load(os.path.join(_SCREAMING_SNAKE_CASE , """traced_model.pt""" ) , map_location=_SCREAMING_SNAKE_CASE ) loaded(inputs_dict["""input_ids"""].to(_SCREAMING_SNAKE_CASE ) , inputs_dict["""attention_mask"""].to(_SCREAMING_SNAKE_CASE ) ) @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase): @slow def _snake_case ( self )-> Union[str, Any]: lowerCamelCase_ =FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased""" ) lowerCamelCase_ =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) with torch.no_grad(): lowerCamelCase_ =model(_SCREAMING_SNAKE_CASE )[0] lowerCamelCase_ =torch.Size((1, 11, 768) ) self.assertEqual(output.shape , _SCREAMING_SNAKE_CASE ) lowerCamelCase_ =torch.tensor( [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
154
0
def snake_case (UpperCAmelCase__ ) -> bool: if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): UpperCamelCase_: Optional[Any] = F'''Input value of [number={number}] must be an integer''' raise TypeError(UpperCAmelCase__ ) if number < 0: return False UpperCamelCase_: Any = number * number while number > 0: if number % 1_0 != number_square % 1_0: return False number //= 1_0 number_square //= 1_0 return True if __name__ == "__main__": import doctest doctest.testmod()
292
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params A_ : Tuple = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['memory_attention', 'encoder_attn'], ['attention', 'attn'], ['/', '.'], ['.LayerNorm.gamma', '_layer_norm.weight'], ['.LayerNorm.beta', '_layer_norm.bias'], ['r.layer_', 'r.layers.'], ['output_proj', 'out_proj'], ['ffn.dense_1.', 'fc2.'], ['ffn.dense.', 'fc1.'], ['ffn_layer_norm', 'final_layer_norm'], ['kernel', 'weight'], ['encoder_layer_norm.', 'encoder.layer_norm.'], ['decoder_layer_norm.', 'decoder.layer_norm.'], ['embeddings.weights', 'shared.weight'], ] def snake_case (UpperCAmelCase__ ) -> str: for pegasus_name, hf_name in PATTERNS: UpperCamelCase_: List[str] = k.replace(UpperCAmelCase__ , UpperCAmelCase__ ) return k def snake_case (UpperCAmelCase__ , UpperCAmelCase__ ) -> PegasusForConditionalGeneration: UpperCamelCase_: List[str] = DEFAULTS.copy() cfg_kwargs.update(UpperCAmelCase__ ) UpperCamelCase_: Tuple = PegasusConfig(**UpperCAmelCase__ ) UpperCamelCase_: Tuple = PegasusForConditionalGeneration(UpperCAmelCase__ ) UpperCamelCase_: List[Any] = torch_model.model.state_dict() UpperCamelCase_: str = {} for k, v in tf_weights.items(): UpperCamelCase_: Dict = rename_state_dict_key(UpperCAmelCase__ ) if new_k not in sd: raise ValueError(F'''could not find new key {new_k} in state dict. (converted from {k})''' ) if "dense" in k or "proj" in new_k: UpperCamelCase_: int = v.T UpperCamelCase_: Union[str, Any] = torch.tensor(UpperCAmelCase__ , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, F'''{new_k}, {k}, {v.shape}, {sd[new_k].shape}''' # make sure embedding.padding_idx is respected UpperCamelCase_: Tuple = torch.zeros_like(mapping['shared.weight'][cfg.pad_token_id + 1] ) UpperCamelCase_: int = mapping['shared.weight'] UpperCamelCase_: Union[str, Any] = mapping['shared.weight'] UpperCamelCase_: Dict = {k: torch.zeros_like(UpperCAmelCase__ ) for k, v in sd.items() if k.endswith('bias' ) and k not in mapping} mapping.update(**UpperCAmelCase__ ) UpperCamelCase_ ,UpperCamelCase_: Optional[int] = torch_model.model.load_state_dict(UpperCAmelCase__ , strict=UpperCAmelCase__ ) UpperCamelCase_: List[str] = [ k for k in missing if k not in ['encoder.embed_positions.weight', 'decoder.embed_positions.weight'] ] assert unexpected_missing == [], F'''no matches found for the following torch keys {unexpected_missing}''' assert extra == [], F'''no matches found for the following tf keys {extra}''' return torch_model def snake_case (UpperCAmelCase__="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: UpperCamelCase_: Union[str, Any] = tf.train.list_variables(UpperCAmelCase__ ) UpperCamelCase_: Tuple = {} UpperCamelCase_: Dict = ['Adafactor', 'global_step'] for name, shape in tqdm(UpperCAmelCase__ , desc='converting tf checkpoint to dict' ): UpperCamelCase_: Union[str, Any] = any(pat in name for pat in ignore_name ) if skip_key: continue UpperCamelCase_: Dict = tf.train.load_variable(UpperCAmelCase__ , UpperCAmelCase__ ) UpperCamelCase_: Optional[Any] = array return tf_weights def snake_case (UpperCAmelCase__ , UpperCAmelCase__ ) -> int: # save tokenizer first UpperCamelCase_: Any = Path(UpperCAmelCase__ ).parent.name UpperCamelCase_: Tuple = task_specific_params[F'''summarization_{dataset}''']['max_position_embeddings'] UpperCamelCase_: Optional[Any] = PegasusTokenizer.from_pretrained('sshleifer/pegasus' , model_max_length=UpperCAmelCase__ ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(UpperCAmelCase__ ) # convert model UpperCamelCase_: Optional[Any] = get_tf_weights_as_numpy(UpperCAmelCase__ ) UpperCamelCase_: Any = task_specific_params[F'''summarization_{dataset}'''] if dataset == "large": UpperCamelCase_: Union[str, Any] = task_specific_params UpperCamelCase_: Tuple = convert_pegasus(UpperCAmelCase__ , UpperCAmelCase__ ) torch_model.save_pretrained(UpperCAmelCase__ ) UpperCamelCase_: int = torch_model.state_dict() sd.pop('model.decoder.embed_positions.weight' ) sd.pop('model.encoder.embed_positions.weight' ) torch.save(UpperCAmelCase__ , Path(UpperCAmelCase__ ) / 'pytorch_model.bin' ) if __name__ == "__main__": A_ : str = argparse.ArgumentParser() # Required parameters parser.add_argument('tf_ckpt_path', type=str, help='passed to tf.train.list_variables') parser.add_argument('save_dir', default=None, type=str, help='Path to the output PyTorch model.') A_ : Optional[Any] = parser.parse_args() if args.save_dir is None: A_ : Union[str, Any] = Path(args.tf_ckpt_path).parent.name A_ : Optional[Any] = os.path.join('pegasus', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
292
1
'''simple docstring''' import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class __magic_name__ ( unittest.TestCase ): def __lowercase ( self : Optional[int] ): _a : Tuple = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ) ) def __lowercase ( self : str ): _a : List[str] = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ) ) def __lowercase ( self : Dict ): _a : Any = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(_UpperCAmelCase ) ) def __lowercase ( self : Union[str, Any] ): _a : Tuple = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ) ) def __lowercase ( self : Optional[Any] ): _a : List[Any] = [ 'safety_checker/pytorch_model.bin', 'safety_checker/model.safetensors', 'vae/diffusion_pytorch_model.bin', 'vae/diffusion_pytorch_model.safetensors', 'text_encoder/pytorch_model.bin', # Removed: 'text_encoder/model.safetensors', 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(_UpperCAmelCase ) ) def __lowercase ( self : List[str] ): _a : Optional[int] = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] _a : Dict = 'fp16' self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ,variant=_UpperCAmelCase ) ) def __lowercase ( self : Union[str, Any] ): _a : Dict = [ 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] _a : Any = 'fp16' self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ,variant=_UpperCAmelCase ) ) def __lowercase ( self : List[str] ): # pass variant but use the non-variant filenames _a : Dict = [ 'unet/diffusion_pytorch_model.bin', 'unet/diffusion_pytorch_model.safetensors', ] _a : Optional[int] = 'fp16' self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ,variant=_UpperCAmelCase ) ) def __lowercase ( self : str ): _a : Any = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] _a : int = 'fp16' self.assertFalse(is_safetensors_compatible(_UpperCAmelCase ,variant=_UpperCAmelCase ) ) def __lowercase ( self : Dict ): _a : List[Any] = [ 'text_encoder/pytorch_model.fp16.bin', 'text_encoder/model.fp16.safetensors', ] _a : Dict = 'fp16' self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ,variant=_UpperCAmelCase ) ) def __lowercase ( self : List[str] ): # pass variant but use the non-variant filenames _a : Any = [ 'text_encoder/pytorch_model.bin', 'text_encoder/model.safetensors', ] _a : Optional[Any] = 'fp16' self.assertTrue(is_safetensors_compatible(_UpperCAmelCase ,variant=_UpperCAmelCase ) ) def __lowercase ( self : Union[str, Any] ): _a : Any = [ 'safety_checker/pytorch_model.fp16.bin', 'safety_checker/model.fp16.safetensors', 'vae/diffusion_pytorch_model.fp16.bin', 'vae/diffusion_pytorch_model.fp16.safetensors', 'text_encoder/pytorch_model.fp16.bin', # 'text_encoder/model.fp16.safetensors', 'unet/diffusion_pytorch_model.fp16.bin', 'unet/diffusion_pytorch_model.fp16.safetensors', ] _a : Dict = 'fp16' self.assertFalse(is_safetensors_compatible(_UpperCAmelCase ,variant=_UpperCAmelCase ) )
89
"""simple docstring""" import math def lowercase__ ( _UpperCAmelCase = 1_00 ) -> int: '''simple docstring''' lowercase : List[str] = sum(i * i for i in range(1 , n + 1 ) ) lowercase : Dict = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f'''{solution() = }''')
255
0
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor __UpperCAmelCase = logging.get_logger(__name__) class UpperCamelCase__ ( _lowerCamelCase ): """simple docstring""" def __init__( self , *_A , **_A ) -> Union[str, Any]: warnings.warn( '''The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use LayoutLMv2ImageProcessor instead.''' , lowercase_ , ) super().__init__(*lowercase_ , **lowercase_ )
356
from __future__ import annotations __UpperCAmelCase = 10 def A__ ( __lowerCamelCase ): SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = max(__lowerCamelCase ) while placement <= max_digit: # declare and initialize empty buckets SCREAMING_SNAKE_CASE_ = [[] for _ in range(__lowerCamelCase )] # split list_of_ints between the buckets for i in list_of_ints: SCREAMING_SNAKE_CASE_ = int((i / placement) % RADIX ) buckets[tmp].append(__lowerCamelCase ) # put each buckets' contents into list_of_ints SCREAMING_SNAKE_CASE_ = 0 for b in range(__lowerCamelCase ): for i in buckets[b]: SCREAMING_SNAKE_CASE_ = i a += 1 # move to next placement *= RADIX return list_of_ints if __name__ == "__main__": import doctest doctest.testmod()
257
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __lowerCAmelCase : Optional[int] ={'configuration_swin': ['SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SwinConfig', 'SwinOnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : str =[ 'SWIN_PRETRAINED_MODEL_ARCHIVE_LIST', 'SwinForImageClassification', 'SwinForMaskedImageModeling', 'SwinModel', 'SwinPreTrainedModel', 'SwinBackbone', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : List[str] =[ 'TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFSwinForImageClassification', 'TFSwinForMaskedImageModeling', 'TFSwinModel', 'TFSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys __lowerCAmelCase : int =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase : Any ={'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : int =[ 'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTMSNModel', 'ViTMSNForImageClassification', 'ViTMSNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys __lowerCAmelCase : Union[str, Any] =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _SCREAMING_SNAKE_CASE = { 'configuration_vivit': ['VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VivitConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = ['VivitImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ 'VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'VivitModel', 'VivitPreTrainedModel', 'VivitForVideoClassification', ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
363
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _SCREAMING_SNAKE_CASE = {'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ 'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST', 'ViTMSNModel', 'ViTMSNForImageClassification', 'ViTMSNPreTrainedModel', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
81
0
def a ( lowerCamelCase_ ): '''simple docstring''' lowercase__ = len(lowerCamelCase_ ) for i in range(1 , lowerCamelCase_ ): lowercase__ = collection[i] lowercase__ = 0 lowercase__ = i - 1 while low <= high: lowercase__ = (low + high) // 2 if val < collection[mid]: lowercase__ = mid - 1 else: lowercase__ = mid + 1 for j in range(lowerCamelCase_ , lowerCamelCase_ , -1 ): lowercase__ = collection[j - 1] lowercase__ = val return collection if __name__ == "__main__": A__ : str = input('Enter numbers separated by a comma:\n').strip() A__ : Any = [int(item) for item in user_input.split(',')] print(binary_insertion_sort(unsorted))
207
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 _UpperCAmelCase ( A__ ): """simple docstring""" def __init__( self : Dict, lowerCamelCase : pyspark.sql.DataFrame, lowerCamelCase : Optional[NamedSplit] = None, lowerCamelCase : Optional[Features] = None, lowerCamelCase : bool = True, lowerCamelCase : str = None, lowerCamelCase : bool = False, lowerCamelCase : str = None, lowerCamelCase : bool = True, lowerCamelCase : str = "arrow", **lowerCamelCase : str, ): '''simple docstring''' super().__init__( split=lowerCamelCase, features=lowerCamelCase, cache_dir=lowerCamelCase, keep_in_memory=lowerCamelCase, streaming=lowerCamelCase, **lowerCamelCase, ) lowercase__ = load_from_cache_file lowercase__ = file_format lowercase__ = Spark( df=lowerCamelCase, features=lowerCamelCase, cache_dir=lowerCamelCase, working_dir=lowerCamelCase, **lowerCamelCase, ) def lowercase__ ( self : List[Any] ): '''simple docstring''' 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=lowerCamelCase, file_format=self._file_format, ) return self.builder.as_dataset(split=self.split )
207
1
def _a ( ): __lowerCAmelCase = 0 for i in range(1 , 10_01 ): total += i**i return str(SCREAMING_SNAKE_CASE_ )[-10:] if __name__ == "__main__": print(solution())
102
import math def _a ( SCREAMING_SNAKE_CASE_ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _a ( SCREAMING_SNAKE_CASE_ : int = 1_00_01 ): try: __lowerCAmelCase = int(SCREAMING_SNAKE_CASE_ ) except (TypeError, ValueError): raise TypeError("Parameter nth must be int or castable to int." ) from None if nth <= 0: raise ValueError("Parameter nth must be greater than or equal to one." ) __lowerCAmelCase = [] __lowerCAmelCase = 2 while len(SCREAMING_SNAKE_CASE_ ) < nth: if is_prime(SCREAMING_SNAKE_CASE_ ): primes.append(SCREAMING_SNAKE_CASE_ ) num += 1 else: num += 1 return primes[len(SCREAMING_SNAKE_CASE_ ) - 1] if __name__ == "__main__": print(f'''{solution() = }''')
102
1
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _snake_case : Dict = logging.get_logger(__name__) _snake_case : str = { 'ut/deta': 'https://huggingface.co/ut/deta/resolve/main/config.json', } class _UpperCAmelCase ( lowercase_ ): UpperCamelCase = '''deta''' UpperCamelCase = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self :Union[str, Any] , __UpperCamelCase :List[Any]=None , __UpperCamelCase :Dict=9_00 , __UpperCamelCase :Optional[Any]=20_48 , __UpperCamelCase :List[Any]=6 , __UpperCamelCase :Tuple=20_48 , __UpperCamelCase :str=8 , __UpperCamelCase :List[Any]=6 , __UpperCamelCase :Dict=10_24 , __UpperCamelCase :str=8 , __UpperCamelCase :Optional[int]=0.0 , __UpperCamelCase :Any=True , __UpperCamelCase :Optional[int]="relu" , __UpperCamelCase :Union[str, Any]=2_56 , __UpperCamelCase :Dict=0.1 , __UpperCamelCase :Any=0.0 , __UpperCamelCase :List[str]=0.0 , __UpperCamelCase :Union[str, Any]=0.02 , __UpperCamelCase :List[Any]=1.0 , __UpperCamelCase :Tuple=True , __UpperCamelCase :Union[str, Any]=False , __UpperCamelCase :Union[str, Any]="sine" , __UpperCamelCase :Union[str, Any]=5 , __UpperCamelCase :List[str]=4 , __UpperCamelCase :Optional[Any]=4 , __UpperCamelCase :Dict=True , __UpperCamelCase :Optional[Any]=3_00 , __UpperCamelCase :str=True , __UpperCamelCase :Tuple=True , __UpperCamelCase :str=1 , __UpperCamelCase :List[str]=5 , __UpperCamelCase :Tuple=2 , __UpperCamelCase :Optional[Any]=1 , __UpperCamelCase :int=1 , __UpperCamelCase :Optional[Any]=5 , __UpperCamelCase :str=2 , __UpperCamelCase :int=0.1 , __UpperCamelCase :int=0.25 , **__UpperCamelCase :List[str] , ): if backbone_config is None: logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone." ) A = CONFIG_MAPPING["resnet"](out_features=["stage2", "stage3", "stage4"] ) else: if isinstance(__UpperCamelCase , __UpperCamelCase ): A = backbone_config.pop("model_type" ) A = CONFIG_MAPPING[backbone_model_type] A = config_class.from_dict(__UpperCamelCase ) A = backbone_config A = num_queries A = max_position_embeddings A = d_model A = encoder_ffn_dim A = encoder_layers A = encoder_attention_heads A = decoder_ffn_dim A = decoder_layers A = decoder_attention_heads A = dropout A = attention_dropout A = activation_dropout A = activation_function A = init_std A = init_xavier_std A = encoder_layerdrop A = auxiliary_loss A = position_embedding_type # deformable attributes A = num_feature_levels A = encoder_n_points A = decoder_n_points A = two_stage A = two_stage_num_proposals A = with_box_refine A = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError("If two_stage is True, with_box_refine must be True." ) # Hungarian matcher A = class_cost A = bbox_cost A = giou_cost # Loss coefficients A = mask_loss_coefficient A = dice_loss_coefficient A = bbox_loss_coefficient A = giou_loss_coefficient A = eos_coefficient A = focal_alpha super().__init__(is_encoder_decoder=__UpperCamelCase , **__UpperCamelCase ) @property def lowerCamelCase ( self :Optional[Any] ): return self.encoder_attention_heads @property def lowerCamelCase ( self :str ): return self.d_model def lowerCamelCase ( self :Dict ): A = copy.deepcopy(self.__dict__ ) A = self.backbone_config.to_dict() A = self.__class__.model_type return output
292
"""simple docstring""" _snake_case : Optional[int] = [ 'DownloadConfig', 'DownloadManager', 'DownloadMode', 'StreamingDownloadManager', ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
292
1
'''simple docstring''' import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class snake_case__ ( lowercase__ , lowercase__ ): A__ = 1 @register_to_config def __init__( self : Dict , __a : Dict=2000 , __a : Tuple=0.1 , __a : List[str]=20 , __a : Union[str, Any]=1e-3 ) -> Any: '''simple docstring''' __snake_case : Dict = None __snake_case : Dict = None __snake_case : Any = None def A_ ( self : List[Any] , __a : int , __a : Union[str, torch.device] = None ) -> Any: '''simple docstring''' __snake_case : List[str] = torch.linspace(1 , self.config.sampling_eps , _a , device=_a ) def A_ ( self : List[str] , __a : Dict , __a : Optional[Any] , __a : List[str] , __a : List[Any]=None ) -> Optional[Any]: '''simple docstring''' if self.timesteps is None: raise ValueError( '`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score __snake_case : Any = ( -0.2_5 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) __snake_case : List[str] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) __snake_case : Dict = std.flatten() while len(std.shape ) < len(score.shape ): __snake_case : List[Any] = std.unsqueeze(-1 ) __snake_case : Dict = -score / std # compute __snake_case : Optional[Any] = -1.0 / len(self.timesteps ) __snake_case : str = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) __snake_case : Optional[int] = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): __snake_case : str = beta_t.unsqueeze(-1 ) __snake_case : Optional[Any] = -0.5 * beta_t * x __snake_case : Tuple = torch.sqrt(_a ) __snake_case : Optional[int] = drift - diffusion**2 * score __snake_case : Optional[int] = x + drift * dt # add noise __snake_case : str = randn_tensor(x.shape , layout=x.layout , generator=_a , device=x.device , dtype=x.dtype ) __snake_case : Tuple = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self : str ) -> Union[str, Any]: '''simple docstring''' return self.config.num_train_timesteps
354
'''simple docstring''' from tempfile import TemporaryDirectory from unittest import TestCase from unittest.mock import MagicMock, patch from transformers import AutoModel, TFAutoModel from transformers.onnx import FeaturesManager from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch @require_torch @require_tf class snake_case__ ( SCREAMING_SNAKE_CASE_ ): def A_ ( self : List[Any] ) -> int: '''simple docstring''' __snake_case : Optional[int] = SMALL_MODEL_IDENTIFIER __snake_case : str = 'pt' __snake_case : Union[str, Any] = 'tf' def A_ ( self : Dict , __a : Tuple ) -> Dict: '''simple docstring''' __snake_case : Optional[int] = AutoModel.from_pretrained(self.test_model ) model_pt.save_pretrained(__a ) def A_ ( self : Any , __a : Optional[Any] ) -> Dict: '''simple docstring''' __snake_case : Union[str, Any] = TFAutoModel.from_pretrained(self.test_model , from_pt=__a ) model_tf.save_pretrained(__a ) def A_ ( self : Any ) -> Tuple: '''simple docstring''' __snake_case : Tuple = 'mock_framework' # Framework provided - return whatever the user provides __snake_case : int = FeaturesManager.determine_framework(self.test_model , __a ) self.assertEqual(__a , __a ) # Local checkpoint and framework provided - return provided framework # PyTorch checkpoint with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(__a ) __snake_case : List[Any] = FeaturesManager.determine_framework(__a , __a ) self.assertEqual(__a , __a ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(__a ) __snake_case : Tuple = FeaturesManager.determine_framework(__a , __a ) self.assertEqual(__a , __a ) def A_ ( self : Union[str, Any] ) -> Any: '''simple docstring''' # PyTorch checkpoint with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(__a ) __snake_case : Tuple = FeaturesManager.determine_framework(__a ) self.assertEqual(__a , self.framework_pt ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(__a ) __snake_case : Union[str, Any] = FeaturesManager.determine_framework(__a ) self.assertEqual(__a , self.framework_tf ) # Invalid local checkpoint with TemporaryDirectory() as local_invalid_ckpt: with self.assertRaises(__a ): __snake_case : Optional[int] = FeaturesManager.determine_framework(__a ) def A_ ( self : Any ) -> List[Any]: '''simple docstring''' __snake_case : Union[str, Any] = MagicMock(return_value=__a ) with patch('transformers.onnx.features.is_tf_available' , __a ): __snake_case : int = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(__a , self.framework_pt ) # PyTorch not in environment -> use TensorFlow __snake_case : Tuple = MagicMock(return_value=__a ) with patch('transformers.onnx.features.is_torch_available' , __a ): __snake_case : Dict = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(__a , self.framework_tf ) # Both in environment -> use PyTorch __snake_case : Optional[Any] = MagicMock(return_value=__a ) __snake_case : Tuple = MagicMock(return_value=__a ) with patch('transformers.onnx.features.is_tf_available' , __a ), patch( 'transformers.onnx.features.is_torch_available' , __a ): __snake_case : Dict = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(__a , self.framework_pt ) # Both not in environment -> raise error __snake_case : str = MagicMock(return_value=__a ) __snake_case : List[Any] = MagicMock(return_value=__a ) with patch('transformers.onnx.features.is_tf_available' , __a ), patch( 'transformers.onnx.features.is_torch_available' , __a ): with self.assertRaises(__a ): __snake_case : Tuple = FeaturesManager.determine_framework(self.test_model )
0
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class lowerCamelCase : '''simple docstring''' def __init__( self: Optional[Any] , snake_case: Tuple , snake_case: Union[str, Any]=13 , snake_case: Optional[int]=7 , snake_case: Union[str, Any]=True , snake_case: Any=True , snake_case: str=True , snake_case: Dict=True , snake_case: int=99 , snake_case: Optional[Any]=32 , snake_case: Optional[Any]=2 , snake_case: Tuple=4 , snake_case: int=37 , snake_case: Any="gelu" , snake_case: List[str]=0.1 , snake_case: List[Any]=0.1 , snake_case: Optional[int]=512 , snake_case: int=16 , snake_case: str=2 , snake_case: int=0.0_2 , snake_case: str=False , snake_case: Any=True , snake_case: List[Any]="None" , snake_case: Optional[Any]=3 , snake_case: Optional[Any]=4 , snake_case: List[str]=None , ) -> str: snake_case_ :Optional[Any] = parent snake_case_ :str = batch_size snake_case_ :Tuple = seq_length snake_case_ :Any = is_training snake_case_ :Dict = use_input_mask snake_case_ :str = use_token_type_ids snake_case_ :List[str] = use_labels snake_case_ :Optional[int] = vocab_size snake_case_ :Dict = hidden_size snake_case_ :List[Any] = num_hidden_layers snake_case_ :Optional[Any] = num_attention_heads snake_case_ :int = intermediate_size snake_case_ :Optional[Any] = hidden_act snake_case_ :Dict = hidden_dropout_prob snake_case_ :str = attention_probs_dropout_prob snake_case_ :int = max_position_embeddings snake_case_ :int = type_vocab_size snake_case_ :Tuple = type_sequence_label_size snake_case_ :Dict = initializer_range snake_case_ :List[str] = num_labels snake_case_ :Union[str, Any] = num_choices snake_case_ :str = relative_attention snake_case_ :Any = position_biased_input snake_case_ :str = pos_att_type snake_case_ :int = scope def lowerCAmelCase_ ( self: List[str] ) -> Any: snake_case_ :str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ :List[Any] = None if self.use_input_mask: snake_case_ :Tuple = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ :List[str] = None if self.use_token_type_ids: snake_case_ :Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ :Union[str, Any] = None snake_case_ :Dict = None snake_case_ :List[str] = None if self.use_labels: snake_case_ :Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ :List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ :int = DebertaVaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=snake_case , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase_ ( self: Optional[int] , snake_case: List[str] , snake_case: Optional[int] , snake_case: str , snake_case: Any , snake_case: List[str] , snake_case: List[str] , snake_case: str ) -> Any: snake_case_ :Any = TFDebertaVaModel(config=snake_case ) snake_case_ :List[str] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} snake_case_ :int = [input_ids, input_mask] snake_case_ :Dict = model(snake_case ) snake_case_ :List[Any] = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase_ ( self: Optional[Any] , snake_case: Optional[Any] , snake_case: List[Any] , snake_case: str , snake_case: int , snake_case: List[Any] , snake_case: int , snake_case: int ) -> Optional[Any]: snake_case_ :str = TFDebertaVaForMaskedLM(config=snake_case ) snake_case_ :Optional[Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case_ :Optional[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase_ ( self: int , snake_case: str , snake_case: List[Any] , snake_case: int , snake_case: Union[str, Any] , snake_case: Tuple , snake_case: int , snake_case: str ) -> str: snake_case_ :Tuple = self.num_labels snake_case_ :Tuple = TFDebertaVaForSequenceClassification(config=snake_case ) snake_case_ :List[str] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case_ :int = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase_ ( self: List[Any] , snake_case: Union[str, Any] , snake_case: int , snake_case: Tuple , snake_case: int , snake_case: Optional[int] , snake_case: str , snake_case: List[Any] ) -> List[str]: snake_case_ :Optional[Any] = self.num_labels snake_case_ :Any = TFDebertaVaForTokenClassification(config=snake_case ) snake_case_ :Union[str, Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case_ :int = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase_ ( self: List[str] , snake_case: Optional[int] , snake_case: str , snake_case: List[Any] , snake_case: Any , snake_case: Dict , snake_case: Union[str, Any] , snake_case: Optional[int] ) -> str: snake_case_ :Dict = TFDebertaVaForQuestionAnswering(config=snake_case ) snake_case_ :Any = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case_ :Tuple = model(snake_case ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> Any: snake_case_ :Optional[int] = self.prepare_config_and_inputs() ( ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ) :Tuple = config_and_inputs snake_case_ :List[str] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class lowerCamelCase ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): '''simple docstring''' _A : Dict = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) _A : Any = ( { """feature-extraction""": TFDebertaVaModel, """fill-mask""": TFDebertaVaForMaskedLM, """question-answering""": TFDebertaVaForQuestionAnswering, """text-classification""": TFDebertaVaForSequenceClassification, """token-classification""": TFDebertaVaForTokenClassification, """zero-shot""": TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) _A : Any = False _A : List[Any] = False def lowerCAmelCase_ ( self: Dict ) -> List[str]: snake_case_ :Optional[Any] = TFDebertaVaModelTester(self ) snake_case_ :List[Any] = ConfigTester(self , config_class=snake_case , hidden_size=37 ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> Any: self.config_tester.run_common_tests() def lowerCAmelCase_ ( self: Optional[Any] ) -> str: snake_case_ :Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def lowerCAmelCase_ ( self: Optional[int] ) -> Optional[Any]: snake_case_ :Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case ) def lowerCAmelCase_ ( self: str ) -> List[Any]: snake_case_ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case ) def lowerCAmelCase_ ( self: List[str] ) -> Optional[int]: snake_case_ :List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case ) def lowerCAmelCase_ ( self: Any ) -> Optional[Any]: snake_case_ :Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case ) @slow def lowerCAmelCase_ ( self: Tuple ) -> List[Any]: snake_case_ :List[Any] = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) self.assertIsNotNone(snake_case ) @require_tf class lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @unittest.skip(reason="""Model not available yet""" ) def lowerCAmelCase_ ( self: Tuple ) -> int: pass @slow def lowerCAmelCase_ ( self: List[Any] ) -> int: snake_case_ :List[Any] = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) snake_case_ :Optional[int] = tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]] ) snake_case_ :int = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) snake_case_ :Optional[int] = model(snake_case , attention_mask=snake_case )[0] snake_case_ :Tuple = tf.constant( [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] , snake_case , atol=1E-4 )
66
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase__ : Optional[Any] ={ '''configuration_mobilenet_v2''': [ '''MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MobileNetV2Config''', '''MobileNetV2OnnxConfig''', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict =['''MobileNetV2FeatureExtractor'''] lowerCAmelCase__ : str =['''MobileNetV2ImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Union[str, Any] =[ '''MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MobileNetV2ForImageClassification''', '''MobileNetV2ForSemanticSegmentation''', '''MobileNetV2Model''', '''MobileNetV2PreTrainedModel''', '''load_tf_weights_in_mobilenet_v2''', ] if TYPE_CHECKING: from .configuration_mobilenet_va import ( MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileNetVaConfig, MobileNetVaOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor from .image_processing_mobilenet_va import MobileNetVaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilenet_va import ( MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel, MobileNetVaPreTrainedModel, load_tf_weights_in_mobilenet_va, ) else: import sys lowerCAmelCase__ : Union[str, Any] =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
257
0
'''simple docstring''' import re def _UpperCamelCase ( UpperCamelCase__ ): if len(re.findall("""[ATCG]""" , UpperCamelCase__ ) ) != len(UpperCamelCase__ ): raise ValueError("""Invalid Strand""" ) return dna.translate(dna.maketrans("""ATCG""" , """TAGC""" ) ) if __name__ == "__main__": import doctest doctest.testmod()
352
'''simple docstring''' import functools def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): # Validation if not isinstance(UpperCamelCase__ , UpperCamelCase__ ) or not all(isinstance(UpperCamelCase__ , UpperCamelCase__ ) for day in days ): raise ValueError("""The parameter days should be a list of integers""" ) if len(UpperCamelCase__ ) != 3 or not all(isinstance(UpperCamelCase__ , UpperCamelCase__ ) for cost in costs ): raise ValueError("""The parameter costs should be a list of three integers""" ) if len(UpperCamelCase__ ) == 0: return 0 if min(UpperCamelCase__ ) <= 0: raise ValueError("""All days elements should be greater than 0""" ) if max(UpperCamelCase__ ) >= 3_6_6: raise ValueError("""All days elements should be less than 366""" ) UpperCAmelCase__ : Union[str, Any] = set(UpperCamelCase__ ) @functools.cache def dynamic_programming(UpperCamelCase__ ) -> int: if index > 3_6_5: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 3_0 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
283
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available lowerCAmelCase__ = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['GPTSw3Tokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
11
"""simple docstring""" def _A ( lowercase , lowercase ): """simple docstring""" return number | (1 << position) def _A ( lowercase , lowercase ): """simple docstring""" return number & ~(1 << position) def _A ( lowercase , lowercase ): """simple docstring""" return number ^ (1 << position) def _A ( lowercase , lowercase ): """simple docstring""" return ((number >> position) & 1) == 1 def _A ( lowercase , lowercase ): """simple docstring""" return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
81
0
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging __A : List[str] = logging.get_logger(__name__) # pylint: disable=invalid-name class _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_): def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , )-> Optional[Any]: super().__init__() self.register_modules( vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , unet=snake_case__ , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , ) def _snake_case ( self , _SCREAMING_SNAKE_CASE = "auto" )-> Dict: if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory lowerCamelCase_ =self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case__ ) def _snake_case ( self )-> List[Any]: self.enable_attention_slicing(snake_case__ ) @torch.no_grad() def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 50 , _SCREAMING_SNAKE_CASE = 7.5 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 0.0 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = "pil" , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , )-> int: if isinstance(snake_case__ , snake_case__ ): lowerCamelCase_ =1 elif isinstance(snake_case__ , snake_case__ ): lowerCamelCase_ =len(snake_case__ ) else: raise ValueError(f'`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'`height` and `width` have to be divisible by 8 but are {height} and {width}.' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( f'`callback_steps` has to be a positive integer but is {callback_steps} of type' f' {type(snake_case__ )}.' ) # get prompt text embeddings lowerCamelCase_ =self.tokenizer( snake_case__ , padding="""max_length""" , max_length=self.tokenizer.model_max_length , return_tensors="""pt""" , ) lowerCamelCase_ =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: lowerCamelCase_ =self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( """The following part of your input was truncated because CLIP can only handle sequences up to""" f' {self.tokenizer.model_max_length} tokens: {removed_text}' ) lowerCamelCase_ =text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: lowerCamelCase_ =self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method lowerCamelCase_ =text_embeddings.shape lowerCamelCase_ =text_embeddings.repeat(1 , snake_case__ , 1 ) lowerCamelCase_ =text_embeddings.view(bs_embed * num_images_per_prompt , snake_case__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. lowerCamelCase_ =guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: lowerCamelCase_ =42 if negative_prompt is None: lowerCamelCase_ =[''] elif type(snake_case__ ) is not type(snake_case__ ): raise TypeError( f'`negative_prompt` should be the same type to `prompt`, but got {type(snake_case__ )} !=' f' {type(snake_case__ )}.' ) elif isinstance(snake_case__ , snake_case__ ): lowerCamelCase_ =[negative_prompt] elif batch_size != len(snake_case__ ): raise ValueError( f'`negative_prompt`: {negative_prompt} has batch size {len(snake_case__ )}, but `prompt`:' f' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches' """ the batch size of `prompt`.""" ) else: lowerCamelCase_ =negative_prompt lowerCamelCase_ =text_input_ids.shape[-1] lowerCamelCase_ =self.tokenizer( snake_case__ , padding="""max_length""" , max_length=snake_case__ , truncation=snake_case__ , return_tensors="""pt""" , ) lowerCamelCase_ =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method lowerCamelCase_ =uncond_embeddings.shape[1] lowerCamelCase_ =uncond_embeddings.repeat(snake_case__ , snake_case__ , 1 ) lowerCamelCase_ =uncond_embeddings.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes lowerCamelCase_ =torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. lowerCamelCase_ =(batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) lowerCamelCase_ =(batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) lowerCamelCase_ =text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps lowerCamelCase_ =torch.randn( snake_case__ , generator=snake_case__ , device="""cpu""" , dtype=snake_case__ ).to(self.device ) lowerCamelCase_ =torch.randn(snake_case__ , generator=snake_case__ , device="""cpu""" , dtype=snake_case__ ).to( self.device ) else: lowerCamelCase_ =torch.randn( snake_case__ , generator=snake_case__ , device=self.device , dtype=snake_case__ ) lowerCamelCase_ =torch.randn(snake_case__ , generator=snake_case__ , device=self.device , dtype=snake_case__ ) else: if latents_reference.shape != latents_shape: raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {latents_shape}' ) lowerCamelCase_ =latents_reference.to(self.device ) lowerCamelCase_ =latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images lowerCamelCase_ =(latents_shape[3] - latents_shape_reference[3]) // 2 lowerCamelCase_ =(latents_shape[2] - latents_shape_reference[2]) // 2 lowerCamelCase_ =latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx lowerCamelCase_ =latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy lowerCamelCase_ =0 if dx < 0 else dx lowerCamelCase_ =0 if dy < 0 else dy lowerCamelCase_ =max(-dx , 0 ) lowerCamelCase_ =max(-dy , 0 ) # import pdb # pdb.set_trace() lowerCamelCase_ =latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(snake_case__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand lowerCamelCase_ =self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler lowerCamelCase_ =latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] lowerCamelCase_ ='eta' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) lowerCamelCase_ ={} if accepts_eta: lowerCamelCase_ =eta for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase_ =torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase_ =self.scheduler.scale_model_input(snake_case__ , snake_case__ ) # predict the noise residual lowerCamelCase_ =self.unet(snake_case__ , snake_case__ , encoder_hidden_states=snake_case__ ).sample # perform guidance if do_classifier_free_guidance: lowerCamelCase_ =noise_pred.chunk(2 ) lowerCamelCase_ =noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 lowerCamelCase_ =self.scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase_ =1 / 0.1_8_2_1_5 * latents lowerCamelCase_ =self.vae.decode(snake_case__ ).sample lowerCamelCase_ =(image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 lowerCamelCase_ =image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: lowerCamelCase_ =self.feature_extractor(self.numpy_to_pil(snake_case__ ) , return_tensors="""pt""" ).to( self.device ) lowerCamelCase_ =self.safety_checker( images=snake_case__ , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: lowerCamelCase_ =None if output_type == "pil": lowerCamelCase_ =self.numpy_to_pil(snake_case__ ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=snake_case__ , nsfw_content_detected=snake_case__ )
353
from tempfile import TemporaryDirectory from unittest import TestCase from unittest.mock import MagicMock, patch from transformers import AutoModel, TFAutoModel from transformers.onnx import FeaturesManager from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch @require_torch @require_tf class _SCREAMING_SNAKE_CASE ( lowerCAmelCase__): def _snake_case ( self )-> Union[str, Any]: lowerCamelCase_ =SMALL_MODEL_IDENTIFIER lowerCamelCase_ ="""pt""" lowerCamelCase_ ="""tf""" def _snake_case ( self , _SCREAMING_SNAKE_CASE )-> List[str]: lowerCamelCase_ =AutoModel.from_pretrained(self.test_model ) model_pt.save_pretrained(_SCREAMING_SNAKE_CASE ) def _snake_case ( self , _SCREAMING_SNAKE_CASE )-> Optional[Any]: lowerCamelCase_ =TFAutoModel.from_pretrained(self.test_model , from_pt=_SCREAMING_SNAKE_CASE ) model_tf.save_pretrained(_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> Optional[Any]: lowerCamelCase_ ="""mock_framework""" # Framework provided - return whatever the user provides lowerCamelCase_ =FeaturesManager.determine_framework(self.test_model , _SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Local checkpoint and framework provided - return provided framework # PyTorch checkpoint with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FeaturesManager.determine_framework(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FeaturesManager.determine_framework(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> Tuple: # PyTorch checkpoint with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FeaturesManager.determine_framework(_SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , self.framework_pt ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =FeaturesManager.determine_framework(_SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , self.framework_tf ) # Invalid local checkpoint with TemporaryDirectory() as local_invalid_ckpt: with self.assertRaises(_SCREAMING_SNAKE_CASE ): lowerCamelCase_ =FeaturesManager.determine_framework(_SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> Optional[Any]: lowerCamelCase_ =MagicMock(return_value=_SCREAMING_SNAKE_CASE ) with patch("""transformers.onnx.features.is_tf_available""" , _SCREAMING_SNAKE_CASE ): lowerCamelCase_ =FeaturesManager.determine_framework(self.test_model ) self.assertEqual(_SCREAMING_SNAKE_CASE , self.framework_pt ) # PyTorch not in environment -> use TensorFlow lowerCamelCase_ =MagicMock(return_value=_SCREAMING_SNAKE_CASE ) with patch("""transformers.onnx.features.is_torch_available""" , _SCREAMING_SNAKE_CASE ): lowerCamelCase_ =FeaturesManager.determine_framework(self.test_model ) self.assertEqual(_SCREAMING_SNAKE_CASE , self.framework_tf ) # Both in environment -> use PyTorch lowerCamelCase_ =MagicMock(return_value=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =MagicMock(return_value=_SCREAMING_SNAKE_CASE ) with patch("""transformers.onnx.features.is_tf_available""" , _SCREAMING_SNAKE_CASE ), patch( """transformers.onnx.features.is_torch_available""" , _SCREAMING_SNAKE_CASE ): lowerCamelCase_ =FeaturesManager.determine_framework(self.test_model ) self.assertEqual(_SCREAMING_SNAKE_CASE , self.framework_pt ) # Both not in environment -> raise error lowerCamelCase_ =MagicMock(return_value=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =MagicMock(return_value=_SCREAMING_SNAKE_CASE ) with patch("""transformers.onnx.features.is_tf_available""" , _SCREAMING_SNAKE_CASE ), patch( """transformers.onnx.features.is_torch_available""" , _SCREAMING_SNAKE_CASE ): with self.assertRaises(_SCREAMING_SNAKE_CASE ): lowerCamelCase_ =FeaturesManager.determine_framework(self.test_model )
49
0
"""simple docstring""" from pathlib import Path import numpy as np from PIL import Image def lowercase ( _snake_case : np.ndarray ) ->np.ndarray: """simple docstring""" __snake_case , __snake_case , __snake_case : Optional[Any] = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.2989 * r + 0.5870 * g + 0.1140 * b def lowercase ( _snake_case : np.ndarray ) ->np.ndarray: """simple docstring""" return (gray > 127) & (gray <= 255) def lowercase ( _snake_case : np.ndarray , _snake_case : np.ndarray ) ->np.ndarray: """simple docstring""" __snake_case : Union[str, Any] = np.zeros_like(_snake_case ) __snake_case : List[Any] = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image __snake_case : Tuple = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): __snake_case : Optional[int] = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() __snake_case : Optional[int] = int(summation > 0 ) return output if __name__ == "__main__": # read original image SCREAMING_SNAKE_CASE : str = Path(__file__).resolve().parent / """image_data""" / """lena.jpg""" SCREAMING_SNAKE_CASE : Optional[Any] = np.array(Image.open(lena_path)) # kernel to be applied SCREAMING_SNAKE_CASE : List[Any] = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) SCREAMING_SNAKE_CASE : Optional[int] = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image SCREAMING_SNAKE_CASE : Tuple = Image.fromarray(output).convert("""RGB""") pil_img.save("""result_dilation.png""")
102
"""simple docstring""" def lowercase ( _snake_case : int , _snake_case : int ) ->str: """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) __snake_case : Tuple = str(bin(_snake_case ) )[2:] # remove the leading "0b" __snake_case : List[Any] = str(bin(_snake_case ) )[2:] __snake_case : Any = max(len(_snake_case ) , len(_snake_case ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(_snake_case ) , b_binary.zfill(_snake_case ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
102
1
"""simple docstring""" from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline __A : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class lowerCamelCase ( _UpperCAmelCase ): def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): super().__init__() self.register_modules(unet=SCREAMING_SNAKE_CASE_ , scheduler=SCREAMING_SNAKE_CASE_ ) @torch.no_grad() def __call__( self , SCREAMING_SNAKE_CASE_ = 1 , SCREAMING_SNAKE_CASE_ = 100 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = True , ): if audio_length_in_s is None: UpperCamelCase : Any = self.unet.config.sample_size / self.unet.config.sample_rate UpperCamelCase : int = audio_length_in_s * self.unet.config.sample_rate UpperCamelCase : Dict = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( f'{audio_length_in_s} is too small. Make sure it\'s bigger or equal to' f' {3 * down_scale_factor / self.unet.config.sample_rate}.' ) UpperCamelCase : Dict = int(SCREAMING_SNAKE_CASE_ ) if sample_size % down_scale_factor != 0: UpperCamelCase : Dict = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( f'{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled' f' by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising' """ process.""" ) UpperCamelCase : List[Any] = int(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[int] = next(iter(self.unet.parameters() ) ).dtype UpperCamelCase : int = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and len(SCREAMING_SNAKE_CASE_ ) != batch_size: raise ValueError( f'You have passed a list of generators of length {len(SCREAMING_SNAKE_CASE_ )}, but requested an effective batch' f' size of {batch_size}. Make sure the batch size matches the length of the generators.' ) UpperCamelCase : Optional[int] = randn_tensor(SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , device=self.device , dtype=SCREAMING_SNAKE_CASE_ ) # set step values self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ , device=audio.device ) UpperCamelCase : Optional[Any] = self.scheduler.timesteps.to(SCREAMING_SNAKE_CASE_ ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output UpperCamelCase : Dict = self.unet(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).sample # 2. compute previous image: x_t -> t_t-1 UpperCamelCase : Optional[Any] = self.scheduler.step(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).prev_sample UpperCamelCase : Any = audio.clamp(-1 , 1 ).float().cpu().numpy() UpperCamelCase : Dict = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=SCREAMING_SNAKE_CASE_ )
27
"""simple docstring""" import argparse import os import re __A : Dict = '''src/diffusers''' # Pattern that looks at the indentation in a line. __A : Union[str, Any] = re.compile(R'''^(\s*)\S''') # Pattern that matches `"key":" and puts `key` in group 0. __A : Dict = re.compile(R'''^\s*"([^"]+)":''') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. __A : List[str] = re.compile(R'''^\s*_import_structure\["([^"]+)"\]''') # Pattern that matches `"key",` and puts `key` in group 0. __A : Tuple = re.compile(R'''^\s*"([^"]+)",\s*$''') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. __A : Tuple = re.compile(R'''\[([^\]]+)\]''') def A_ ( snake_case_ : Dict ): '''simple docstring''' UpperCamelCase : Union[str, Any] = _re_indent.search(snake_case_ ) return "" if search is None else search.groups()[0] def A_ ( snake_case_ : Union[str, Any] ,snake_case_ : Dict="" ,snake_case_ : Dict=None ,snake_case_ : Any=None ): '''simple docstring''' UpperCamelCase : Optional[int] = 0 UpperCamelCase : List[Any] = code.split("""\n""" ) if start_prompt is not None: while not lines[index].startswith(snake_case_ ): index += 1 UpperCamelCase : Optional[Any] = ["""\n""".join(lines[:index] )] else: UpperCamelCase : int = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). UpperCamelCase : Any = [lines[index]] index += 1 while index < len(snake_case_ ) and (end_prompt is None or not lines[index].startswith(snake_case_ )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(snake_case_ ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + """ """ ): current_block.append(lines[index] ) blocks.append("""\n""".join(snake_case_ ) ) if index < len(snake_case_ ) - 1: UpperCamelCase : Any = [lines[index + 1]] index += 1 else: UpperCamelCase : List[str] = [] else: blocks.append("""\n""".join(snake_case_ ) ) UpperCamelCase : int = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(snake_case_ ) > 0: blocks.append("""\n""".join(snake_case_ ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(snake_case_ ): blocks.append("""\n""".join(lines[index:] ) ) return blocks def A_ ( snake_case_ : Optional[Any] ): '''simple docstring''' def _inner(snake_case_ : Tuple ): return key(snake_case_ ).lower().replace("""_""" ,"""""" ) return _inner def A_ ( snake_case_ : List[Any] ,snake_case_ : Optional[int]=None ): '''simple docstring''' # If no key is provided, we use a noop. def noop(snake_case_ : Dict ): return x if key is None: UpperCamelCase : int = noop # Constants are all uppercase, they go first. UpperCamelCase : List[Any] = [obj for obj in objects if key(snake_case_ ).isupper()] # Classes are not all uppercase but start with a capital, they go second. UpperCamelCase : str = [obj for obj in objects if key(snake_case_ )[0].isupper() and not key(snake_case_ ).isupper()] # Functions begin with a lowercase, they go last. UpperCamelCase : List[str] = [obj for obj in objects if not key(snake_case_ )[0].isupper()] UpperCamelCase : Tuple = ignore_underscore(snake_case_ ) return sorted(snake_case_ ,key=snake_case_ ) + sorted(snake_case_ ,key=snake_case_ ) + sorted(snake_case_ ,key=snake_case_ ) def A_ ( snake_case_ : int ): '''simple docstring''' # This inner function sort imports between [ ]. def _replace(snake_case_ : List[Any] ): UpperCamelCase : Any = match.groups()[0] if "," not in imports: return f'[{imports}]' UpperCamelCase : Union[str, Any] = [part.strip().replace("""\"""" ,"""""" ) for part in imports.split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCamelCase : List[str] = keys[:-1] return "[" + ", ".join([f'"{k}"' for k in sort_objects(snake_case_ )] ) + "]" UpperCamelCase : str = import_statement.split("""\n""" ) if len(snake_case_ ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. UpperCamelCase : str = 2 if lines[1].strip() == """[""" else 1 UpperCamelCase : Dict = [(i, _re_strip_line.search(snake_case_ ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] UpperCamelCase : int = sort_objects(snake_case_ ,key=lambda snake_case_ : x[1] ) UpperCamelCase : Any = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(snake_case_ ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: UpperCamelCase : List[Any] = _re_bracket_content.sub(_replace ,lines[1] ) else: UpperCamelCase : Optional[Any] = [part.strip().replace("""\"""" ,"""""" ) for part in lines[1].split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCamelCase : List[Any] = keys[:-1] UpperCamelCase : int = get_indent(lines[1] ) + """, """.join([f'"{k}"' for k in sort_objects(snake_case_ )] ) return "\n".join(snake_case_ ) else: # Finally we have to deal with imports fitting on one line UpperCamelCase : List[str] = _re_bracket_content.sub(_replace ,snake_case_ ) return import_statement def A_ ( snake_case_ : Tuple ,snake_case_ : str=True ): '''simple docstring''' with open(snake_case_ ,"""r""" ) as f: UpperCamelCase : int = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 UpperCamelCase : Dict = split_code_in_indented_blocks( snake_case_ ,start_prompt="""_import_structure = {""" ,end_prompt="""if TYPE_CHECKING:""" ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 ,len(snake_case_ ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. UpperCamelCase : Optional[Any] = main_blocks[block_idx] UpperCamelCase : Optional[int] = block.split("""\n""" ) # Get to the start of the imports. UpperCamelCase : Union[str, Any] = 0 while line_idx < len(snake_case_ ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: UpperCamelCase : List[str] = len(snake_case_ ) else: line_idx += 1 if line_idx >= len(snake_case_ ): continue # Ignore beginning and last line: they don't contain anything. UpperCamelCase : Dict = """\n""".join(block_lines[line_idx:-1] ) UpperCamelCase : Union[str, Any] = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. UpperCamelCase : Optional[int] = split_code_in_indented_blocks(snake_case_ ,indent_level=snake_case_ ) # We have two categories of import key: list or _import_structure[key].append/extend UpperCamelCase : Union[str, Any] = _re_direct_key if """_import_structure""" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. UpperCamelCase : Union[str, Any] = [(pattern.search(snake_case_ ).groups()[0] if pattern.search(snake_case_ ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. UpperCamelCase : Optional[Any] = [(i, key) for i, key in enumerate(snake_case_ ) if key is not None] UpperCamelCase : List[Any] = [x[0] for x in sorted(snake_case_ ,key=lambda snake_case_ : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. UpperCamelCase : str = 0 UpperCamelCase : List[Any] = [] for i in range(len(snake_case_ ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: UpperCamelCase : str = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(snake_case_ ) count += 1 # And we put our main block back together with its first and last line. UpperCamelCase : Tuple = """\n""".join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(snake_case_ ): if check_only: return True else: print(f'Overwriting {file}.' ) with open(snake_case_ ,"""w""" ) as f: f.write("""\n""".join(snake_case_ ) ) def A_ ( snake_case_ : int=True ): '''simple docstring''' UpperCamelCase : Any = [] for root, _, files in os.walk(snake_case_ ): if "__init__.py" in files: UpperCamelCase : Union[str, Any] = sort_imports(os.path.join(snake_case_ ,"""__init__.py""" ) ,check_only=snake_case_ ) if result: UpperCamelCase : Any = [os.path.join(snake_case_ ,"""__init__.py""" )] if len(snake_case_ ) > 0: raise ValueError(f'Would overwrite {len(snake_case_ )} files, run `make style`.' ) if __name__ == "__main__": __A : Any = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') __A : str = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
27
1
import logging import os from dataclasses import dataclass, field from functools import partial from pathlib import Path from tempfile import TemporaryDirectory from typing import List, Optional import faiss import torch from datasets import Features, Sequence, Value, load_dataset from transformers import DPRContextEncoder, DPRContextEncoderTokenizerFast, HfArgumentParser UpperCAmelCase__ = logging.getLogger(__name__) torch.set_grad_enabled(False) UpperCAmelCase__ = '''cuda''' if torch.cuda.is_available() else '''cpu''' def UpperCAmelCase_ ( __snake_case , __snake_case=100 , __snake_case=" " ) -> List[str]: """simple docstring""" _lowercase =text.split(__snake_case ) return [character.join(text[i : i + n] ).strip() for i in range(0 , len(__snake_case ) , __snake_case )] def UpperCAmelCase_ ( __snake_case ) -> dict: """simple docstring""" _lowercase , _lowercase =[], [] for title, text in zip(documents['''title'''] , documents['''text'''] ): if text is not None: for passage in split_text(__snake_case ): titles.append(title if title is not None else '''''' ) texts.append(__snake_case ) return {"title": titles, "text": texts} def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> dict: """simple docstring""" _lowercase =ctx_tokenizer( documents['''title'''] , documents['''text'''] , truncation=__snake_case , padding='''longest''' , return_tensors='''pt''' )['''input_ids'''] _lowercase =ctx_encoder(input_ids.to(device=__snake_case ) , return_dict=__snake_case ).pooler_output return {"embeddings": embeddings.detach().cpu().numpy()} def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , ) -> Dict: """simple docstring""" logger.info('''Step 1 - Create the dataset''' ) ###################################### # The dataset needed for RAG must have three columns: # - title (string): title of the document # - text (string): text of a passage of the document # - embeddings (array of dimension d): DPR representation of the passage # Let's say you have documents in tab-separated csv files with columns "title" and "text" assert os.path.isfile(rag_example_args.csv_path ), "Please provide a valid path to a csv file" # You can load a Dataset object this way _lowercase =load_dataset( '''csv''' , data_files=[rag_example_args.csv_path] , split='''train''' , delimiter='''\t''' , column_names=['''title''', '''text'''] ) # More info about loading csv files in the documentation: https://huggingface.co/docs/datasets/loading_datasets.html?highlight=csv#csv-files # Then split the documents into passages of 100 words _lowercase =dataset.map(__snake_case , batched=__snake_case , num_proc=processing_args.num_proc ) # And compute the embeddings _lowercase =DPRContextEncoder.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ).to(device=__snake_case ) _lowercase =DPRContextEncoderTokenizerFast.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name ) _lowercase =Features( {'''text''': Value('''string''' ), '''title''': Value('''string''' ), '''embeddings''': Sequence(Value('''float32''' ) )} ) # optional, save as float32 instead of float64 to save space _lowercase =dataset.map( partial(__snake_case , ctx_encoder=__snake_case , ctx_tokenizer=__snake_case ) , batched=__snake_case , batch_size=processing_args.batch_size , features=__snake_case , ) # And finally save your dataset _lowercase =os.path.join(rag_example_args.output_dir , '''my_knowledge_dataset''' ) dataset.save_to_disk(__snake_case ) # from datasets import load_from_disk # dataset = load_from_disk(passages_path) # to reload the dataset ###################################### logger.info('''Step 2 - Index the dataset''' ) ###################################### # Let's use the Faiss implementation of HNSW for fast approximate nearest neighbor search _lowercase =faiss.IndexHNSWFlat(index_hnsw_args.d , index_hnsw_args.m , faiss.METRIC_INNER_PRODUCT ) dataset.add_faiss_index('''embeddings''' , custom_index=__snake_case ) # And save the index _lowercase =os.path.join(rag_example_args.output_dir , '''my_knowledge_dataset_hnsw_index.faiss''' ) dataset.get_index('''embeddings''' ).save(__snake_case ) # dataset.load_faiss_index("embeddings", index_path) # to reload the index @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = field( default=str(Path(lowerCAmelCase).parent / '''test_run''' / '''dummy-kb''' / '''my_knowledge_dataset.csv''') , metadata={'''help''': '''Path to a tab-separated csv file with columns \'title\' and \'text\''''} , ) SCREAMING_SNAKE_CASE__ = field( default=lowerCAmelCase , metadata={'''help''': '''Question that is passed as input to RAG. Default is \'What does Moses\' rod turn into ?\'.'''} , ) SCREAMING_SNAKE_CASE__ = field( default='''facebook/rag-sequence-nq''' , metadata={'''help''': '''The RAG model to use. Either \'facebook/rag-sequence-nq\' or \'facebook/rag-token-nq\''''} , ) SCREAMING_SNAKE_CASE__ = field( default='''facebook/dpr-ctx_encoder-multiset-base''' , metadata={ '''help''': ( '''The DPR context encoder model to use. Either \'facebook/dpr-ctx_encoder-single-nq-base\' or''' ''' \'facebook/dpr-ctx_encoder-multiset-base\'''' ) } , ) SCREAMING_SNAKE_CASE__ = field( default=str(Path(lowerCAmelCase).parent / '''test_run''' / '''dummy-kb''') , metadata={'''help''': '''Path to a directory where the dataset passages and the index will be saved'''} , ) @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = field( default=lowerCAmelCase , metadata={ '''help''': '''The number of processes to use to split the documents into passages. Default is single process.''' } , ) SCREAMING_SNAKE_CASE__ = field( default=16 , metadata={ '''help''': '''The batch size to use when computing the passages embeddings using the DPR context encoder.''' } , ) @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = field( default=768 , metadata={'''help''': '''The dimension of the embeddings to pass to the HNSW Faiss index.'''} , ) SCREAMING_SNAKE_CASE__ = field( default=128 , metadata={ '''help''': ( '''The number of bi-directional links created for every new element during the HNSW index construction.''' ) } , ) if __name__ == "__main__": logging.basicConfig(level=logging.WARNING) logger.setLevel(logging.INFO) UpperCAmelCase__ = HfArgumentParser((RagExampleArguments, ProcessingArguments, IndexHnswArguments)) UpperCAmelCase__ ,UpperCAmelCase__ ,UpperCAmelCase__ = parser.parse_args_into_dataclasses() with TemporaryDirectory() as tmp_dir: UpperCAmelCase__ = rag_example_args.output_dir or tmp_dir main(rag_example_args, processing_args, index_hnsw_args)
5
from tempfile import TemporaryDirectory from unittest import TestCase from unittest.mock import MagicMock, patch from transformers import AutoModel, TFAutoModel from transformers.onnx import FeaturesManager from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch @require_torch @require_tf class lowercase_ ( lowercase ): '''simple docstring''' def __lowerCAmelCase ( self : str ) ->int: """simple docstring""" a = SMALL_MODEL_IDENTIFIER a = '''pt''' a = '''tf''' def __lowerCAmelCase ( self : Optional[Any] , __UpperCAmelCase : str ) ->Union[str, Any]: """simple docstring""" a = AutoModel.from_pretrained(self.test_model ) model_pt.save_pretrained(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Union[str, Any] ) ->List[str]: """simple docstring""" a = TFAutoModel.from_pretrained(self.test_model , from_pt=__UpperCAmelCase ) model_tf.save_pretrained(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any ) ->int: """simple docstring""" a = '''mock_framework''' # Framework provided - return whatever the user provides a = FeaturesManager.determine_framework(self.test_model , __UpperCAmelCase ) self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) # Local checkpoint and framework provided - return provided framework # PyTorch checkpoint with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(__UpperCAmelCase ) a = FeaturesManager.determine_framework(__UpperCAmelCase , __UpperCAmelCase ) self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(__UpperCAmelCase ) a = FeaturesManager.determine_framework(__UpperCAmelCase , __UpperCAmelCase ) self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : str ) ->int: """simple docstring""" with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(__UpperCAmelCase ) a = FeaturesManager.determine_framework(__UpperCAmelCase ) self.assertEqual(__UpperCAmelCase , self.framework_pt ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(__UpperCAmelCase ) a = FeaturesManager.determine_framework(__UpperCAmelCase ) self.assertEqual(__UpperCAmelCase , self.framework_tf ) # Invalid local checkpoint with TemporaryDirectory() as local_invalid_ckpt: with self.assertRaises(__UpperCAmelCase ): a = FeaturesManager.determine_framework(__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" a = MagicMock(return_value=__UpperCAmelCase ) with patch('''transformers.onnx.features.is_tf_available''' , __UpperCAmelCase ): a = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(__UpperCAmelCase , self.framework_pt ) # PyTorch not in environment -> use TensorFlow a = MagicMock(return_value=__UpperCAmelCase ) with patch('''transformers.onnx.features.is_torch_available''' , __UpperCAmelCase ): a = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(__UpperCAmelCase , self.framework_tf ) # Both in environment -> use PyTorch a = MagicMock(return_value=__UpperCAmelCase ) a = MagicMock(return_value=__UpperCAmelCase ) with patch('''transformers.onnx.features.is_tf_available''' , __UpperCAmelCase ), patch( '''transformers.onnx.features.is_torch_available''' , __UpperCAmelCase ): a = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(__UpperCAmelCase , self.framework_pt ) # Both not in environment -> raise error a = MagicMock(return_value=__UpperCAmelCase ) a = MagicMock(return_value=__UpperCAmelCase ) with patch('''transformers.onnx.features.is_tf_available''' , __UpperCAmelCase ), patch( '''transformers.onnx.features.is_torch_available''' , __UpperCAmelCase ): with self.assertRaises(__UpperCAmelCase ): a = FeaturesManager.determine_framework(self.test_model )
0
0
"""simple docstring""" import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase_ : str = logging.get_logger(__name__) UpperCAmelCase_ : Optional[Any] = ["""model.decoder.embed_positions.weights"""] def _A (__a ) -> Dict: """simple docstring""" if "emb" in name: SCREAMING_SNAKE_CASE_ : Optional[int] = name.replace('''emb''' , '''model.decoder.embed_tokens''' ) if "transformer" in name: SCREAMING_SNAKE_CASE_ : Tuple = name.replace('''transformer''' , '''model.decoder''' ) if "cross_attention" in name: SCREAMING_SNAKE_CASE_ : str = name.replace('''cross_attention''' , '''encoder_attn''' ) if "linear1" in name: SCREAMING_SNAKE_CASE_ : Optional[int] = name.replace('''linear1''' , '''fc1''' ) if "linear2" in name: SCREAMING_SNAKE_CASE_ : str = name.replace('''linear2''' , '''fc2''' ) if "norm1" in name: SCREAMING_SNAKE_CASE_ : Any = name.replace('''norm1''' , '''self_attn_layer_norm''' ) if "norm_cross" in name: SCREAMING_SNAKE_CASE_ : List[str] = name.replace('''norm_cross''' , '''encoder_attn_layer_norm''' ) if "norm2" in name: SCREAMING_SNAKE_CASE_ : Tuple = name.replace('''norm2''' , '''final_layer_norm''' ) if "out_norm" in name: SCREAMING_SNAKE_CASE_ : List[str] = name.replace('''out_norm''' , '''model.decoder.layer_norm''' ) if "linears" in name: SCREAMING_SNAKE_CASE_ : Dict = name.replace('''linears''' , '''lm_heads''' ) if "condition_provider.conditioners.description.output_proj" in name: SCREAMING_SNAKE_CASE_ : List[str] = name.replace('''condition_provider.conditioners.description.output_proj''' , '''enc_to_dec_proj''' ) return name def _A (__a , __a ) -> Tuple[Dict, Dict]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(state_dict.keys() ) SCREAMING_SNAKE_CASE_ : int = {} for key in keys: SCREAMING_SNAKE_CASE_ : int = state_dict.pop(__a ) SCREAMING_SNAKE_CASE_ : int = rename_keys(__a ) if "in_proj_weight" in key: # split fused qkv proj SCREAMING_SNAKE_CASE_ : List[str] = val[:hidden_size, :] SCREAMING_SNAKE_CASE_ : List[str] = val[hidden_size : 2 * hidden_size, :] SCREAMING_SNAKE_CASE_ : Optional[Any] = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: SCREAMING_SNAKE_CASE_ : int = val else: SCREAMING_SNAKE_CASE_ : Any = val return state_dict, enc_dec_proj_state_dict def _A (__a ) -> MusicgenDecoderConfig: """simple docstring""" if checkpoint == "small": # default config values SCREAMING_SNAKE_CASE_ : Optional[int] = 10_24 SCREAMING_SNAKE_CASE_ : Tuple = 24 SCREAMING_SNAKE_CASE_ : Optional[Any] = 16 elif checkpoint == "medium": SCREAMING_SNAKE_CASE_ : List[str] = 15_36 SCREAMING_SNAKE_CASE_ : Optional[int] = 48 SCREAMING_SNAKE_CASE_ : Optional[int] = 24 elif checkpoint == "large": SCREAMING_SNAKE_CASE_ : Optional[Any] = 20_48 SCREAMING_SNAKE_CASE_ : Optional[int] = 48 SCREAMING_SNAKE_CASE_ : int = 32 else: raise ValueError(f'Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.' ) SCREAMING_SNAKE_CASE_ : List[Any] = MusicgenDecoderConfig( hidden_size=__a , ffn_dim=hidden_size * 4 , num_hidden_layers=__a , num_attention_heads=__a , ) return config @torch.no_grad() def _A (__a , __a=None , __a=None , __a="cpu" ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = MusicGen.get_pretrained(__a , device=__a ) SCREAMING_SNAKE_CASE_ : Dict = decoder_config_from_checkpoint(__a ) SCREAMING_SNAKE_CASE_ : Optional[Any] = fairseq_model.lm.state_dict() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = rename_state_dict( __a , hidden_size=decoder_config.hidden_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = TaEncoderModel.from_pretrained('''t5-base''' ) SCREAMING_SNAKE_CASE_ : List[str] = EncodecModel.from_pretrained('''facebook/encodec_32khz''' ) SCREAMING_SNAKE_CASE_ : int = MusicgenForCausalLM(__a ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = decoder.load_state_dict(__a , strict=__a ) for key in missing_keys.copy(): if key.startswith(('''text_encoder''', '''audio_encoder''') ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(__a ) if len(__a ) > 0: raise ValueError(f'Missing key(s) in state_dict: {missing_keys}' ) if len(__a ) > 0: raise ValueError(f'Unexpected key(s) in state_dict: {unexpected_keys}' ) # init the composite model SCREAMING_SNAKE_CASE_ : str = MusicgenForConditionalGeneration(text_encoder=__a , audio_encoder=__a , decoder=__a ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(__a ) # check we can do a forward pass SCREAMING_SNAKE_CASE_ : Dict = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ : List[Any] = model(input_ids=__a , decoder_input_ids=__a ).logits if logits.shape != (8, 1, 20_48): raise ValueError('''Incorrect shape for logits''' ) # now construct the processor SCREAMING_SNAKE_CASE_ : str = AutoTokenizer.from_pretrained('''t5-base''' ) SCREAMING_SNAKE_CASE_ : str = AutoFeatureExtractor.from_pretrained('''facebook/encodec_32khz''' , padding_side='''left''' ) SCREAMING_SNAKE_CASE_ : Tuple = MusicgenProcessor(feature_extractor=__a , tokenizer=__a ) # set the appropriate bos/pad token ids SCREAMING_SNAKE_CASE_ : str = 20_48 SCREAMING_SNAKE_CASE_ : List[Any] = 20_48 # set other default generation config params SCREAMING_SNAKE_CASE_ : int = int(30 * audio_encoder.config.frame_rate ) SCREAMING_SNAKE_CASE_ : str = True SCREAMING_SNAKE_CASE_ : Optional[Any] = 3.0 if pytorch_dump_folder is not None: Path(__a ).mkdir(exist_ok=__a ) logger.info(f'Saving model {checkpoint} to {pytorch_dump_folder}' ) model.save_pretrained(__a ) processor.save_pretrained(__a ) if repo_id: logger.info(f'Pushing model {checkpoint} to {repo_id}' ) model.push_to_hub(__a ) processor.push_to_hub(__a ) if __name__ == "__main__": UpperCAmelCase_ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint""", default="""small""", type=str, help="""Checkpoint size of the MusicGen model you'd like to convert. Can be one of: `['small', 'medium', 'large']`.""", ) parser.add_argument( """--pytorch_dump_folder""", required=True, default=None, type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument( """--push_to_hub""", default=None, type=str, help="""Where to upload the converted model on the 🤗 hub.""" ) parser.add_argument( """--device""", default="""cpu""", type=str, help="""Torch device to run the conversion, either cpu or cuda.""" ) UpperCAmelCase_ : Dict = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
318
"""simple docstring""" from collections import defaultdict def _A (__a , __a ) -> bool: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = first_str.lower().strip() SCREAMING_SNAKE_CASE_ : List[Any] = second_str.lower().strip() # Remove whitespace SCREAMING_SNAKE_CASE_ : Dict = first_str.replace(''' ''' , '''''' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = second_str.replace(''' ''' , '''''' ) # Strings of different lengths are not anagrams if len(__a ) != len(__a ): return False # Default values for count should be 0 SCREAMING_SNAKE_CASE_ : defaultdict[str, int] = defaultdict(__a ) # For each character in input strings, # increment count in the corresponding for i in range(len(__a ) ): count[first_str[i]] += 1 count[second_str[i]] -= 1 return all(_count == 0 for _count in count.values() ) if __name__ == "__main__": from doctest import testmod testmod() UpperCAmelCase_ : Any = input("""Enter the first string """).strip() UpperCAmelCase_ : Optional[int] = input("""Enter the second string """).strip() UpperCAmelCase_ : Union[str, Any] = check_anagrams(input_a, input_b) print(f'''{input_a} and {input_b} are {'' if status else 'not '}anagrams.''')
318
1
from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 _UpperCAmelCase = { # 1536-bit 5: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 2048-bit 14: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AACAA68FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 3072-bit 15: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 4096-bit 16: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7""" + """88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA""" + """2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6""" + """287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED""" + """1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9""" + """93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199""" + """FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 6144-bit 17: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08""" + """8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B""" + """302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9""" + """A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6""" + """49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8""" + """FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C""" + """180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718""" + """3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D""" + """04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D""" + """B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226""" + """1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC""" + """E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26""" + """99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB""" + """04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2""" + """233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127""" + """D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492""" + """36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406""" + """AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918""" + """DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151""" + """2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03""" + """F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F""" + """BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA""" + """CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B""" + """B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632""" + """387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E""" + """6DCC4024FFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, # 8192-bit 18: { """prime""": int( """FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1""" + """29024E088A67CC74020BBEA63B139B22514A08798E3404DD""" + """EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245""" + """E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED""" + """EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D""" + """C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F""" + """83655D23DCA3AD961C62F356208552BB9ED529077096966D""" + """670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B""" + """E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9""" + """DE2BCBF6955817183995497CEA956AE515D2261898FA0510""" + """15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64""" + """ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7""" + """ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B""" + """F12FFA06D98A0864D87602733EC86A64521F2B18177B200C""" + """BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31""" + """43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7""" + """88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA""" + """2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6""" + """287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED""" + """1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9""" + """93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492""" + """36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD""" + """F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831""" + """179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B""" + """DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF""" + """5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6""" + """D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3""" + """23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA""" + """CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328""" + """06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C""" + """DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE""" + """12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4""" + """38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300""" + """741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568""" + """3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9""" + """22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B""" + """4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A""" + """062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36""" + """4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1""" + """B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92""" + """4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47""" + """9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71""" + """60C980DD98EDD3DFFFFFFFFFFFFFFFFF""", base=16, ), """generator""": 2, }, } class UpperCAmelCase : '''simple docstring''' def __init__( self , lowercase = 1_4 ): """simple docstring""" if group not in primes: raise ValueError('Unsupported Group' ) A_ : Optional[int] = primes[group]["prime"] A_ : Dict = primes[group]["generator"] A_ : Union[str, Any] = int(hexlify(urandom(3_2 ) ) , base=1_6 ) def lowerCAmelCase_ ( self ): """simple docstring""" return hex(self.__private_key )[2:] def lowerCAmelCase_ ( self ): """simple docstring""" A_ : Tuple = pow(self.generator , self.__private_key , self.prime ) return hex(__A )[2:] def lowerCAmelCase_ ( self , lowercase ): """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(__A , (self.prime - 1) // 2 , self.prime ) == 1 ) def lowerCAmelCase_ ( self , lowercase ): """simple docstring""" A_ : Any = int(__A , base=1_6 ) if not self.is_valid_public_key(__A ): raise ValueError('Invalid public key' ) A_ : Tuple = pow(__A , self.__private_key , self.prime ) return shaaaa(str(__A ).encode() ).hexdigest() @staticmethod def lowerCAmelCase_ ( lowercase , lowercase ): """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(__A , (prime - 1) // 2 , __A ) == 1 ) @staticmethod def lowerCAmelCase_ ( lowercase , lowercase , lowercase = 1_4 ): """simple docstring""" A_ : List[str] = int(__A , base=1_6 ) A_ : Tuple = int(__A , base=1_6 ) A_ : Any = primes[group]["prime"] if not DiffieHellman.is_valid_public_key_static(__A , __A ): raise ValueError('Invalid public key' ) A_ : List[Any] = pow(__A , __A , __A ) return shaaaa(str(__A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
140
def lowercase_( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if divisor % 5 == 0 or divisor % 2 == 0: return 0 lowerCamelCase : List[Any] = 1 lowerCamelCase : Union[str, Any] = 1 while repunit: lowerCamelCase : Union[str, Any] = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def lowercase_( SCREAMING_SNAKE_CASE_ = 1000000 ): '''simple docstring''' lowerCamelCase : List[str] = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(SCREAMING_SNAKE_CASE_ ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'''{solution() = }''')
283
0
"""simple docstring""" import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem A_ = importlib.util.find_spec('''s3fs''') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 A_ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(F'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def UpperCAmelCase__ (snake_case__ : str ): """simple docstring""" if "://" in dataset_path: _snake_case : List[str] = dataset_path.split("""://""" )[1] return dataset_path def UpperCAmelCase__ (snake_case__ : fsspec.AbstractFileSystem ): """simple docstring""" if fs is not None and fs.protocol != "file": return True else: return False def UpperCAmelCase__ (snake_case__ : fsspec.AbstractFileSystem , snake_case__ : str , snake_case__ : str ): """simple docstring""" _snake_case : Union[str, Any] = not is_remote_filesystem(snake_case__ ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(snake_case__ ) , fs._strip_protocol(snake_case__ ) ) else: fs.mv(snake_case__ , snake_case__ , recursive=snake_case__ ) def UpperCAmelCase__ (): """simple docstring""" if hasattr(fsspec.asyn , """reset_lock""" ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: _snake_case : Tuple = None _snake_case : Union[str, Any] = None _snake_case : List[Any] = threading.Lock()
132
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ = logging.get_logger(__name__) A_ = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class lowercase( __a , __a ): '''simple docstring''' lowercase__ = "swin" lowercase__ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self: Any, a_: List[str]=224, a_: List[Any]=4, a_: List[Any]=3, a_: Dict=96, a_: List[str]=[2, 2, 6, 2], a_: int=[3, 6, 12, 24], a_: int=7, a_: str=4.0, a_: Optional[Any]=True, a_: Dict=0.0, a_: List[Any]=0.0, a_: List[str]=0.1, a_: Union[str, Any]="gelu", a_: Dict=False, a_: Union[str, Any]=0.02, a_: Optional[int]=1E-5, a_: Optional[int]=32, a_: Tuple=None, a_: Union[str, Any]=None, **a_: Any, ): '''simple docstring''' super().__init__(**a_ ) _snake_case : Any = image_size _snake_case : List[Any] = patch_size _snake_case : Tuple = num_channels _snake_case : str = embed_dim _snake_case : Union[str, Any] = depths _snake_case : int = len(a_ ) _snake_case : Union[str, Any] = num_heads _snake_case : List[str] = window_size _snake_case : str = mlp_ratio _snake_case : Union[str, Any] = qkv_bias _snake_case : Dict = hidden_dropout_prob _snake_case : str = attention_probs_dropout_prob _snake_case : Union[str, Any] = drop_path_rate _snake_case : Optional[int] = hidden_act _snake_case : str = use_absolute_embeddings _snake_case : Tuple = layer_norm_eps _snake_case : List[Any] = initializer_range _snake_case : Optional[Any] = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model _snake_case : Any = int(embed_dim * 2 ** (len(a_ ) - 1) ) _snake_case : Any = ["""stem"""] + [f"stage{idx}" for idx in range(1, len(a_ ) + 1 )] _snake_case , _snake_case : List[str] = get_aligned_output_features_output_indices( out_features=a_, out_indices=a_, stage_names=self.stage_names ) class lowercase( __a ): '''simple docstring''' lowercase__ = version.parse("1.11" ) @property def UpperCamelCase_ ( self: Any ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def UpperCamelCase_ ( self: List[str] ): '''simple docstring''' return 1E-4
132
1
'''simple docstring''' import random from .binary_exp_mod import bin_exp_mod def snake_case_ ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Optional[Any]=1000 ) -> int: if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd UpperCAmelCase : str = n - 1 UpperCAmelCase : List[Any] = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) UpperCAmelCase : List[str] = 0 while count < prec: UpperCAmelCase : int = random.randint(2 , n - 1 ) UpperCAmelCase : List[str] = bin_exp_mod(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) if b != 1: UpperCAmelCase : int = True for _ in range(_lowerCAmelCase ): if b == n - 1: UpperCAmelCase : Dict = False break UpperCAmelCase : str = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": UpperCamelCase__: Optional[int] = abs(int(input("Enter bound : ").strip())) print("Here's the list of primes:") print(", ".join(str(i) for i in range(n + 1) if is_prime_big(i)))
23
import argparse from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird from transformers.utils import logging logging.set_verbosity_info() def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): # Initialise PyTorch model __a = BigBirdConfig.from_json_file(_UpperCAmelCase ) print(f'Building PyTorch model from configuration: {config}' ) if is_trivia_qa: __a = BigBirdForQuestionAnswering(_UpperCAmelCase ) else: __a = BigBirdForPreTraining(_UpperCAmelCase ) # Load weights from tf checkpoint load_tf_weights_in_big_bird(_UpperCAmelCase , _UpperCAmelCase , is_trivia_qa=_UpperCAmelCase ) # Save pytorch-model print(f'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": __snake_case :Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--big_bird_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained BERT 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.''' ) parser.add_argument( '''--is_trivia_qa''', action='''store_true''', help='''Whether to convert a model with a trivia_qa head.''' ) __snake_case :Any = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa )
49
0
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm _lowercase : Any =re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex _lowercase : Union[str, Any] =10 _lowercase : Optional[Any] =256 def lowerCAmelCase_ ( _lowercase : List[str]) -> Optional[MinHash]: """simple docstring""" if len(a__) < MIN_NUM_TOKENS: return None a__ : List[str] = MinHash(num_perm=a__) for token in set(a__): min_hash.update(token.encode()) return min_hash def lowerCAmelCase_ ( _lowercase : str) -> Set[str]: """simple docstring""" return {t for t in NON_ALPHA.split(a__) if len(t.strip()) > 0} class snake_case__ : """simple docstring""" def __init__( self , *, __lowercase = 0.8_5 , ) -> Dict: """simple docstring""" a__ : Union[str, Any] = duplication_jaccard_threshold a__ : int = NUM_PERM a__ : int = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a__ : Tuple = defaultdict(__UpperCamelCase ) def SCREAMING_SNAKE_CASE__( self , __lowercase , __lowercase ) -> None: """simple docstring""" a__ : Optional[int] = self._index.query(__UpperCamelCase ) if code_key in self._index.keys: print(F'''Duplicate key {code_key}''' ) return self._index.insert(__UpperCamelCase , __UpperCamelCase ) if len(__UpperCamelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCamelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCamelCase ) def SCREAMING_SNAKE_CASE__( self ) -> List[List[Dict]]: """simple docstring""" a__ : Optional[Any] = [] for base, duplicates in self._duplicate_clusters.items(): a__ : Dict = [base] + list(__UpperCamelCase ) # reformat the cluster to be a list of dict a__ : str = [{"""base_index""": el[0], """repo_name""": el[1], """path""": el[2]} for el in cluster] duplicate_clusters.append(__UpperCamelCase ) return duplicate_clusters def SCREAMING_SNAKE_CASE__( self , __lowercase ) -> None: """simple docstring""" a__ : Dict = self.get_duplicate_clusters() with open(__UpperCamelCase , """w""" ) as f: json.dump(__UpperCamelCase , __UpperCamelCase ) def lowerCAmelCase_ ( _lowercase : List[str]) -> Optional[Any]: """simple docstring""" a__ , a__ : int = element a__ : int = get_min_hash([t for t in NON_ALPHA.split(data["""content"""]) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase_ ( _lowercase : Type[Dataset]) -> int: """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a__ , max_queue_size=1_0000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase_ ( _lowercase : Type[Dataset] , _lowercase : float) -> List[str]: """simple docstring""" a__ : str = DuplicationIndex(duplication_jaccard_threshold=a__) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a__)) , max_queue_size=100)): di.add(a__ , a__) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase_ ( _lowercase : str , _lowercase : str) -> float: """simple docstring""" a__ : Union[str, Any] = get_tokens(a__) a__ : Union[str, Any] = get_tokens(a__) return len(tokensa & tokensa) / len(tokensa | tokensa) _lowercase : Optional[int] =None def lowerCAmelCase_ ( _lowercase : Dict , _lowercase : Any) -> int: """simple docstring""" a__ : Optional[int] = [] for elementa in cluster: a__ : str = _shared_dataset[elementa["""base_index"""]]["""content"""] for elementa in extremes: a__ : Optional[int] = _shared_dataset[elementa["""base_index"""]]["""content"""] if jaccard_similarity(a__ , a__) >= jaccard_threshold: elementa["copies"] += 1 break else: a__ : List[Any] = 1 extremes.append(a__) return extremes def lowerCAmelCase_ ( _lowercase : str , _lowercase : Optional[int] , _lowercase : Optional[Any]) -> Union[str, Any]: """simple docstring""" global _shared_dataset a__ : int = dataset a__ : Dict = [] a__ : Any = partial(_find_cluster_extremes_shared , jaccard_threshold=a__) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a__ , a__ , ) , total=len(a__) , ): extremes_list.append(a__) return extremes_list def lowerCAmelCase_ ( _lowercase : Type[Dataset] , _lowercase : float = 0.85) -> Tuple[Type[Dataset], List[List[Dict]]]: """simple docstring""" a__ : str = make_duplicate_clusters(a__ , a__) a__ : Optional[Any] = {x["""base_index"""] for cluster in duplicate_clusters for x in cluster} a__ : List[Any] = {} a__ : Union[str, Any] = find_extremes(a__ , a__ , a__) for extremes in extremes_clusters: for element in extremes: a__ : str = element a__ : str = duplicate_indices - set(extreme_dict.keys()) a__ : List[Any] = dataset.filter(lambda _lowercase , _lowercase: idx not in remove_indices , with_indices=a__) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a__ : Tuple = element["""base_index"""] in extreme_dict if element["is_extreme"]: a__ : List[str] = extreme_dict[element["""base_index"""]]["""copies"""] print(F'''Original dataset size: {len(a__)}''') print(F'''Number of duplicate clusters: {len(a__)}''') print(F'''Files in duplicate cluster: {len(a__)}''') print(F'''Unique files in duplicate cluster: {len(a__)}''') print(F'''Filtered dataset size: {len(a__)}''') return ds_filter, duplicate_clusters
360
from __future__ import annotations import requests _lowercase : Dict =set( "approved_at_utc approved_by author_flair_background_color\nauthor_flair_css_class author_flair_richtext author_flair_template_id author_fullname\nauthor_premium can_mod_post category clicked content_categories created_utc downs\nedited gilded gildings hidden hide_score is_created_from_ads_ui is_meta\nis_original_content is_reddit_media_domain is_video link_flair_css_class\nlink_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title\nname permalink pwls quarantine saved score secure_media secure_media_embed selftext\nsubreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type\ntotal_awards_received ups upvote_ratio url user_reports".split() ) def lowerCAmelCase_ ( _lowercase : str , _lowercase : int = 1 , _lowercase : str = "new" , _lowercase : list | None = None) -> dict: """simple docstring""" a__ : Optional[Any] = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(_lowercase) - valid_terms)): a__ : Any = F'''Invalid search term: {invalid_search_terms}''' raise ValueError(_lowercase) a__ : int = requests.get( F'''https://reddit.com/r/{subreddit}/{age}.json?limit={limit}''' , headers={"""User-agent""": """A random string"""} , ) if response.status_code == 429: raise requests.HTTPError a__ : int = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(_lowercase)} a__ : int = {} for id_ in range(_lowercase): a__ : List[Any] = { item: data["""data"""]["""children"""][id_]["""data"""][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data("learnpython", wanted_data=["title", "url", "selftext"]))
266
0
'''simple docstring''' import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowercase : Dict = logging.get_logger(__name__) __lowercase : Any = {'vocab_file': 'spiece.model'} __lowercase : List[Any] = { 'vocab_file': { 'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model', 'google/bigbird-roberta-large': ( 'https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model' ), 'google/bigbird-base-trivia-itc': ( 'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model' ), } } __lowercase : Tuple = { 'google/bigbird-roberta-base': 40_96, 'google/bigbird-roberta-large': 40_96, 'google/bigbird-base-trivia-itc': 40_96, } class __UpperCamelCase ( lowerCAmelCase_ ): A_ = VOCAB_FILES_NAMES A_ = PRETRAINED_VOCAB_FILES_MAP A_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A_ = ["input_ids", "attention_mask"] A_ = [] def __init__( self , __a , __a="<unk>" , __a="<s>" , __a="</s>" , __a="<pad>" , __a="[SEP]" , __a="[MASK]" , __a="[CLS]" , __a = None , **__a , ): '''simple docstring''' __a : int = AddedToken(__a , lstrip=__a , rstrip=__a ) if isinstance(__a , __a ) else bos_token __a : Any = AddedToken(__a , lstrip=__a , rstrip=__a ) if isinstance(__a , __a ) else eos_token __a : Dict = AddedToken(__a , lstrip=__a , rstrip=__a ) if isinstance(__a , __a ) else unk_token __a : Optional[Any] = AddedToken(__a , lstrip=__a , rstrip=__a ) if isinstance(__a , __a ) else pad_token __a : Optional[int] = AddedToken(__a , lstrip=__a , rstrip=__a ) if isinstance(__a , __a ) else cls_token __a : List[Any] = AddedToken(__a , lstrip=__a , rstrip=__a ) if isinstance(__a , __a ) else sep_token # Mask token behave like a normal word, i.e. include the space before it __a : List[Any] = AddedToken(__a , lstrip=__a , rstrip=__a ) if isinstance(__a , __a ) else mask_token __a : List[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__a , eos_token=__a , unk_token=__a , pad_token=__a , sep_token=__a , mask_token=__a , cls_token=__a , sp_model_kwargs=self.sp_model_kwargs , **__a , ) __a : Tuple = vocab_file __a : Union[str, Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__a ) @property def __UpperCAmelCase ( self ): '''simple docstring''' return self.sp_model.get_piece_size() def __UpperCAmelCase ( self ): '''simple docstring''' __a : Optional[int] = {self.convert_ids_to_tokens(__a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): '''simple docstring''' __a : Dict = self.__dict__.copy() __a : Tuple = None return state def __setstate__( self , __a ): '''simple docstring''' __a : Tuple = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): __a : Any = {} __a : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __UpperCAmelCase ( self , __a ): '''simple docstring''' return self.sp_model.encode(__a , out_type=__a ) def __UpperCAmelCase ( self , __a ): '''simple docstring''' return self.sp_model.piece_to_id(__a ) def __UpperCAmelCase ( self , __a ): '''simple docstring''' __a : str = self.sp_model.IdToPiece(__a ) return token def __UpperCAmelCase ( self , __a ): '''simple docstring''' __a : List[str] = [] __a : Tuple = '' __a : Any = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__a ) + token __a : Any = True __a : Optional[int] = [] else: current_sub_tokens.append(__a ) __a : Optional[int] = False out_string += self.sp_model.decode(__a ) return out_string.strip() def __UpperCAmelCase ( self , __a , __a = False , __a = None , __a = True , **__a , ): '''simple docstring''' __a : Tuple = kwargs.pop('use_source_tokenizer' , __a ) __a : Optional[int] = self.convert_ids_to_tokens(__a , skip_special_tokens=__a ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __a : int = [] __a : Union[str, Any] = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(__a ) ) __a : Union[str, Any] = [] sub_texts.append(__a ) else: current_sub_text.append(__a ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(__a ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __a : List[str] = re.sub(r' (\[(MASK|SEP)\])' , r'\1' , ' '.join(__a ) ) else: __a : str = ''.join(__a ) __a : str = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __a : Any = self.clean_up_tokenization(__a ) return clean_text else: return text def __UpperCAmelCase ( self , __a , __a = None ): '''simple docstring''' if not os.path.isdir(__a ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return __a : List[Any] = os.path.join( __a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __a ) elif not os.path.isfile(self.vocab_file ): with open(__a , 'wb' ) as fi: __a : Tuple = self.sp_model.serialized_model_proto() fi.write(__a ) return (out_vocab_file,) def __UpperCAmelCase ( self , __a , __a = None ): '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __a : Optional[int] = [self.cls_token_id] __a : Dict = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def __UpperCAmelCase ( self , __a , __a = None , __a = False ): '''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] + ([0] * len(__a )) + [1] def __UpperCAmelCase ( self , __a , __a = None ): '''simple docstring''' __a : Any = [self.sep_token_id] __a : Dict = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
27
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Dict ): __a : Optional[Any] = tmp_path / 'file.csv' __a : Union[str, Any] = textwrap.dedent( '\\n header1,header2\n 1,2\n 10,20\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Union[str, Any] ): __a : str = tmp_path / 'malformed_file.csv' __a : int = textwrap.dedent( '\\n header1,header2\n 1,2\n 10,20,\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[str] ): __a : Optional[Any] = tmp_path / 'csv_with_image.csv' __a : Dict = textwrap.dedent( F"""\ image {image_file} """ ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[Any] ): __a : Union[str, Any] = tmp_path / 'csv_with_label.csv' __a : Any = textwrap.dedent( '\\n label\n good\n bad\n good\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[Any] ): __a : Dict = tmp_path / 'csv_with_int_list.csv' __a : Tuple = textwrap.dedent( '\\n int_list\n 1 2 3\n 4 5 6\n 7 8 9\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : List[str] ): __a : int = Csv() __a : str = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(_SCREAMING_SNAKE_CASE , match='Error tokenizing data' ): for _ in generator: pass assert any( record.levelname == 'ERROR' and 'Failed to read file' in record.message and os.path.basename(_SCREAMING_SNAKE_CASE ) in record.message for record in caplog.records ) @require_pil def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[str] ): with open(_SCREAMING_SNAKE_CASE , encoding='utf-8' ) as f: __a : Tuple = f.read().splitlines()[1] __a : Tuple = Csv(encoding='utf-8' , features=Features({'image': Image()} ) ) __a : Any = csv._generate_tables([[csv_file_with_image]] ) __a : int = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('image' ).type == Image()() __a : Any = pa_table.to_pydict()['image'] assert generated_content == [{"path": image_file, "bytes": None}] def lowerCamelCase (_SCREAMING_SNAKE_CASE : Union[str, Any] ): with open(_SCREAMING_SNAKE_CASE , encoding='utf-8' ) as f: __a : Tuple = f.read().splitlines()[1:] __a : Optional[int] = Csv(encoding='utf-8' , features=Features({'label': ClassLabel(names=['good', 'bad'] )} ) ) __a : List[str] = csv._generate_tables([[csv_file_with_label]] ) __a : Dict = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('label' ).type == ClassLabel(names=['good', 'bad'] )() __a : int = pa_table.to_pydict()['label'] assert generated_content == [ClassLabel(names=['good', 'bad'] ).straint(_SCREAMING_SNAKE_CASE ) for label in labels] def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[int] ): __a : str = Csv(encoding='utf-8' , sep=',' , converters={'int_list': lambda _SCREAMING_SNAKE_CASE : [int(_SCREAMING_SNAKE_CASE ) for i in x.split()]} ) __a : Any = csv._generate_tables([[csv_file_with_int_list]] ) __a : Any = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('int_list' ).type ) __a : Tuple = pa_table.to_pydict()['int_list'] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
27
1
'''simple docstring''' import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def lowerCAmelCase__ ( lowerCamelCase : Optional[int] ,lowerCamelCase : List[Any] ,lowerCamelCase : Tuple ,lowerCamelCase : List[str] ): _A : Dict = multiprocessing.Manager() _A : List[Any] = manager.list() _A : Dict = multiprocessing.Process(target=lowerCamelCase ,args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append('timed out' ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def lowerCAmelCase__ ( lowerCamelCase : Optional[int] ,lowerCamelCase : Union[str, Any] ,lowerCamelCase : Optional[Any] ): with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil _A : Any = shutil.rmtree _A : Optional[int] = os.rmdir _A : str = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: _A : str = {} with swallow_io(): with time_limit(lowerCamelCase ): exec(lowerCamelCase ,lowerCamelCase ) result.append('passed' ) except TimeoutException: result.append('timed out' ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. _A : Optional[int] = rmtree _A : Optional[Any] = rmdir _A : Dict = chdir @contextlib.contextmanager def lowerCAmelCase__ ( lowerCamelCase : int ): def signal_handler(lowerCamelCase : str ,lowerCamelCase : Any ): raise TimeoutException('Timed out!' ) signal.setitimer(signal.ITIMER_REAL ,lowerCamelCase ) signal.signal(signal.SIGALRM ,lowerCamelCase ) try: yield finally: signal.setitimer(signal.ITIMER_REAL ,0 ) @contextlib.contextmanager def lowerCAmelCase__ ( ): _A : Any = WriteOnlyStringIO() with contextlib.redirect_stdout(lowerCamelCase ): with contextlib.redirect_stderr(lowerCamelCase ): with redirect_stdin(lowerCamelCase ): yield @contextlib.contextmanager def lowerCAmelCase__ ( ): with tempfile.TemporaryDirectory() as dirname: with chdir(lowerCamelCase ): yield dirname class __lowerCamelCase ( a_ ): """simple docstring""" pass class __lowerCamelCase ( io.StringIO ): """simple docstring""" def A ( self : Tuple , *SCREAMING_SNAKE_CASE : Any , **SCREAMING_SNAKE_CASE : Dict): raise OSError def A ( self : Optional[int] , *SCREAMING_SNAKE_CASE : List[str] , **SCREAMING_SNAKE_CASE : Optional[Any]): raise OSError def A ( self : Optional[int] , *SCREAMING_SNAKE_CASE : str , **SCREAMING_SNAKE_CASE : Optional[int]): raise OSError def A ( self : Union[str, Any] , *SCREAMING_SNAKE_CASE : Optional[int] , **SCREAMING_SNAKE_CASE : Tuple): return False class __lowerCamelCase ( contextlib._RedirectStream ): # type: ignore """simple docstring""" a = "stdin" @contextlib.contextmanager def lowerCAmelCase__ ( lowerCamelCase : Tuple ): if root == ".": yield return _A : Any = os.getcwd() os.chdir(lowerCamelCase ) try: yield except BaseException as exc: raise exc finally: os.chdir(lowerCamelCase ) def lowerCAmelCase__ ( lowerCamelCase : List[Any]=None ): if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS ,(maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA ,(maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK ,(maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins _A : List[Any] = None _A : Dict = None import os _A : Union[str, Any] = '1' _A : int = None _A : Optional[int] = None _A : int = None _A : Any = None _A : Optional[int] = None _A : Union[str, Any] = None _A : List[Any] = None _A : int = None _A : List[Any] = None _A : Tuple = None _A : Any = None _A : Tuple = None _A : Optional[int] = None _A : Optional[Any] = None _A : str = None _A : Dict = None _A : List[str] = None _A : Union[str, Any] = None _A : Union[str, Any] = None _A : str = None _A : str = None _A : str = None _A : Any = None _A : Union[str, Any] = None _A : str = None _A : List[str] = None _A : Union[str, Any] = None import shutil _A : int = None _A : Any = None _A : List[Any] = None import subprocess _A : Optional[Any] = None # type: ignore _A : List[Any] = None import sys _A : Any = None _A : Tuple = None _A : str = None _A : Tuple = None _A : List[str] = None
361
'''simple docstring''' A : Dict = '''Alexander Joslin''' import operator as op from .stack import Stack def lowerCAmelCase__ ( lowerCamelCase : str ): _A : Dict = {'*': op.mul, '/': op.truediv, '+': op.add, '-': op.sub} _A : Stack[int] = Stack() _A : Stack[str] = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(lowerCamelCase ) ) elif i in operators: # RULE 2 operator_stack.push(lowerCamelCase ) elif i == ")": # RULE 4 _A : List[str] = operator_stack.peek() operator_stack.pop() _A : Dict = operand_stack.peek() operand_stack.pop() _A : Optional[int] = operand_stack.peek() operand_stack.pop() _A : Dict = operators[opr](lowerCamelCase ,lowerCamelCase ) operand_stack.push(lowerCamelCase ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": A : Dict = '''(5 + ((4 * 2) * (2 + 3)))''' # answer = 45 print(f"""{equation} = {dijkstras_two_stack_algorithm(equation)}""")
227
0