code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def __lowerCamelCase (UpperCAmelCase__ : Any ): SCREAMING_SNAKE_CASE = FileLock(str(tmpdir / "foo.lock" ) ) SCREAMING_SNAKE_CASE = FileLock(str(tmpdir / "foo.lock" ) ) SCREAMING_SNAKE_CASE = 0.01 with locka.acquire(): with pytest.raises(__lowerCAmelCase ): SCREAMING_SNAKE_CASE = time.time() locka.acquire(__lowerCAmelCase ) assert time.time() - _start > timeout def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = """a""" * 1_0_0_0 + """.lock""" SCREAMING_SNAKE_CASE = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith(".lock" ) assert not locka._lock_file.endswith(__lowerCAmelCase ) assert len(os.path.basename(locka._lock_file ) ) <= 2_5_5 SCREAMING_SNAKE_CASE = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(__lowerCAmelCase ): locka.acquire(0 )
712
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
0
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
713
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels 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 = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
0
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, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class lowercase ( a ): def __snake_case( self : Dict ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_UpperCAmelCase , "tf_padding" ) ) self.parent.assertTrue(hasattr(_UpperCAmelCase , "depth_multiplier" ) ) class lowercase : def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : int=13 , _UpperCamelCase : List[Any]=3 , _UpperCamelCase : str=32 , _UpperCamelCase : Any=0.2_5 , _UpperCamelCase : Union[str, Any]=8 , _UpperCamelCase : Dict=True , _UpperCamelCase : Optional[int]=1_024 , _UpperCamelCase : Optional[Any]=32 , _UpperCamelCase : int="relu6" , _UpperCamelCase : int=0.1 , _UpperCamelCase : Union[str, Any]=0.0_2 , _UpperCamelCase : int=True , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : Optional[Any]=10 , _UpperCamelCase : Tuple=None , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = depth_multiplier SCREAMING_SNAKE_CASE = min_depth SCREAMING_SNAKE_CASE = tf_padding SCREAMING_SNAKE_CASE = int(last_hidden_size * depth_multiplier ) SCREAMING_SNAKE_CASE = output_stride SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = classifier_dropout_prob SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.num_labels ) SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels, pixel_labels def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , min_depth=self.min_depth , tf_padding=self.tf_padding , hidden_act=self.hidden_act , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def __snake_case( self : List[str] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = MobileNetVaModel(config=_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCAmelCase ) 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, ) , ) def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str , _UpperCamelCase : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = MobileNetVaForImageClassification(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCAmelCase , labels=_UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Dict = (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else () lowercase__ : Union[str, Any] = ( {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification} if is_torch_available() else {} ) lowercase__ : Any = False lowercase__ : List[Any] = False lowercase__ : Tuple = False lowercase__ : str = False def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = MobileNetVaModelTester(self ) SCREAMING_SNAKE_CASE = MobileNetVaConfigTester(self , config_class=_UpperCAmelCase , has_text_modality=_UpperCAmelCase ) def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="MobileNetV1 does not use inputs_embeds" ) def __snake_case( self : int ) -> str: '''simple docstring''' pass @unittest.skip(reason="MobileNetV1 does not support input and output embeddings" ) def __snake_case( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason="MobileNetV1 does not output attentions" ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' pass def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCAmelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCAmelCase ) def __snake_case( self : Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCAmelCase ) def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' def check_hidden_states_output(_UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : int ): SCREAMING_SNAKE_CASE = model_class(_UpperCAmelCase ) model.to(_UpperCAmelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) ) SCREAMING_SNAKE_CASE = outputs.hidden_states SCREAMING_SNAKE_CASE = 26 self.assertEqual(len(_UpperCAmelCase ) , _UpperCAmelCase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def __snake_case( self : List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase ) @slow def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = MobileNetVaModel.from_pretrained(_UpperCAmelCase ) self.assertIsNotNone(_UpperCAmelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : Tuple ) -> Tuple: '''simple docstring''' return ( MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224" ) if is_vision_available() else None ) @slow def __snake_case( self : Optional[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224" ).to(_UpperCAmelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCAmelCase , return_tensors="pt" ).to(_UpperCAmelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCAmelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_001) ) self.assertEqual(outputs.logits.shape , _UpperCAmelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5] ).to(_UpperCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCAmelCase , atol=1e-4 ) )
714
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_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()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
0
# Copyright 2022 The HuggingFace Team and The OpenBMB 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 # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _lowerCamelCase : Union[str, Any] = { "configuration_cpmant": ["CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP", "CpmAntConfig"], "tokenization_cpmant": ["CpmAntTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : int = [ "CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST", "CpmAntForCausalLM", "CpmAntModel", "CpmAntPreTrainedModel", ] if TYPE_CHECKING: from .configuration_cpmant import CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP, CpmAntConfig from .tokenization_cpmant import CpmAntTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_cpmant import ( CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST, CpmAntForCausalLM, CpmAntModel, CpmAntPreTrainedModel, ) else: import sys _lowerCamelCase : List[str] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
715
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
0
import qiskit def __lowerCamelCase (UpperCAmelCase__ : List[Any] = 2 ): SCREAMING_SNAKE_CASE = qubits # Using Aer's simulator SCREAMING_SNAKE_CASE = qiskit.Aer.get_backend("aer_simulator" ) # Creating a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE = qiskit.QuantumCircuit(_lowerCamelCase , _lowerCamelCase ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , _lowerCamelCase ): # Adding CX (CNOT) gate circuit.cx(i - 1 , _lowerCamelCase ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(_lowerCamelCase ) ) , list(range(_lowerCamelCase ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator SCREAMING_SNAKE_CASE = qiskit.execute(_lowerCamelCase , _lowerCamelCase , shots=1_0_0_0 ) return job.result().get_counts(_lowerCamelCase ) if __name__ == "__main__": print(f"""Total count for various states are: {quantum_entanglement(3)}""")
716
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
0
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : str = logging.get_logger(__name__) _lowerCamelCase : Optional[int] = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } _lowerCamelCase : str = { '''b0''': { '''hidden_dim''': 12_80, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 2_24, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 12_80, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 2_40, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 14_08, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 2_60, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 15_36, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 3_00, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 17_92, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 3_80, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 20_48, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 4_56, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 23_04, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 5_28, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 25_60, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 6_00, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = EfficientNetConfig() SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["hidden_dim"] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["width_coef"] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["depth_coef"] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["image_size"] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["dropout_rate"] SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["dw_padding"] SCREAMING_SNAKE_CASE = "huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type="dataset" ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(_lowerCamelCase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} return config def __lowerCamelCase (): SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return im def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["image_size"] SCREAMING_SNAKE_CASE = EfficientNetImageProcessor( size={"height": size, "width": size} , image_mean=[0.485, 0.456, 0.406] , image_std=[0.4785_3944, 0.473_2864, 0.4743_4163] , do_center_crop=_lowerCamelCase , ) return preprocessor def __lowerCamelCase (UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = [v.split("_" )[0].split("block" )[1] for v in original_param_names if v.startswith("block" )] SCREAMING_SNAKE_CASE = sorted(set(_lowerCamelCase ) ) SCREAMING_SNAKE_CASE = len(_lowerCamelCase ) SCREAMING_SNAKE_CASE = {b: str(_lowerCamelCase ) for b, i in zip(_lowerCamelCase , range(_lowerCamelCase ) )} SCREAMING_SNAKE_CASE = [] rename_keys.append(("stem_conv/kernel:0", "embeddings.convolution.weight") ) rename_keys.append(("stem_bn/gamma:0", "embeddings.batchnorm.weight") ) rename_keys.append(("stem_bn/beta:0", "embeddings.batchnorm.bias") ) rename_keys.append(("stem_bn/moving_mean:0", "embeddings.batchnorm.running_mean") ) rename_keys.append(("stem_bn/moving_variance:0", "embeddings.batchnorm.running_var") ) for b in block_names: SCREAMING_SNAKE_CASE = block_name_mapping[b] rename_keys.append((F"block{b}_expand_conv/kernel:0", F"encoder.blocks.{hf_b}.expansion.expand_conv.weight") ) rename_keys.append((F"block{b}_expand_bn/gamma:0", F"encoder.blocks.{hf_b}.expansion.expand_bn.weight") ) rename_keys.append((F"block{b}_expand_bn/beta:0", F"encoder.blocks.{hf_b}.expansion.expand_bn.bias") ) rename_keys.append( (F"block{b}_expand_bn/moving_mean:0", F"encoder.blocks.{hf_b}.expansion.expand_bn.running_mean") ) rename_keys.append( (F"block{b}_expand_bn/moving_variance:0", F"encoder.blocks.{hf_b}.expansion.expand_bn.running_var") ) rename_keys.append( (F"block{b}_dwconv/depthwise_kernel:0", F"encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight") ) rename_keys.append((F"block{b}_bn/gamma:0", F"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight") ) rename_keys.append((F"block{b}_bn/beta:0", F"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias") ) rename_keys.append( (F"block{b}_bn/moving_mean:0", F"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean") ) rename_keys.append( (F"block{b}_bn/moving_variance:0", F"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var") ) rename_keys.append((F"block{b}_se_reduce/kernel:0", F"encoder.blocks.{hf_b}.squeeze_excite.reduce.weight") ) rename_keys.append((F"block{b}_se_reduce/bias:0", F"encoder.blocks.{hf_b}.squeeze_excite.reduce.bias") ) rename_keys.append((F"block{b}_se_expand/kernel:0", F"encoder.blocks.{hf_b}.squeeze_excite.expand.weight") ) rename_keys.append((F"block{b}_se_expand/bias:0", F"encoder.blocks.{hf_b}.squeeze_excite.expand.bias") ) rename_keys.append( (F"block{b}_project_conv/kernel:0", F"encoder.blocks.{hf_b}.projection.project_conv.weight") ) rename_keys.append((F"block{b}_project_bn/gamma:0", F"encoder.blocks.{hf_b}.projection.project_bn.weight") ) rename_keys.append((F"block{b}_project_bn/beta:0", F"encoder.blocks.{hf_b}.projection.project_bn.bias") ) rename_keys.append( (F"block{b}_project_bn/moving_mean:0", F"encoder.blocks.{hf_b}.projection.project_bn.running_mean") ) rename_keys.append( (F"block{b}_project_bn/moving_variance:0", F"encoder.blocks.{hf_b}.projection.project_bn.running_var") ) rename_keys.append(("top_conv/kernel:0", "encoder.top_conv.weight") ) rename_keys.append(("top_bn/gamma:0", "encoder.top_bn.weight") ) rename_keys.append(("top_bn/beta:0", "encoder.top_bn.bias") ) rename_keys.append(("top_bn/moving_mean:0", "encoder.top_bn.running_mean") ) rename_keys.append(("top_bn/moving_variance:0", "encoder.top_bn.running_var") ) SCREAMING_SNAKE_CASE = {} for item in rename_keys: if item[0] in original_param_names: SCREAMING_SNAKE_CASE = "efficientnet." + item[1] SCREAMING_SNAKE_CASE = "classifier.weight" SCREAMING_SNAKE_CASE = "classifier.bias" return key_mapping def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Tuple ): for key, value in tf_params.items(): if "normalization" in key: continue SCREAMING_SNAKE_CASE = key_mapping[key] if "_conv" in key and "kernel" in key: SCREAMING_SNAKE_CASE = torch.from_numpy(_lowerCamelCase ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: SCREAMING_SNAKE_CASE = torch.from_numpy(_lowerCamelCase ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: SCREAMING_SNAKE_CASE = torch.from_numpy(np.transpose(_lowerCamelCase ) ) else: SCREAMING_SNAKE_CASE = torch.from_numpy(_lowerCamelCase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_lowerCamelCase ) @torch.no_grad() def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Any ): SCREAMING_SNAKE_CASE = model_classes[model_name]( include_top=_lowerCamelCase , weights="imagenet" , input_tensor=_lowerCamelCase , input_shape=_lowerCamelCase , pooling=_lowerCamelCase , classes=1_0_0_0 , classifier_activation="softmax" , ) SCREAMING_SNAKE_CASE = original_model.trainable_variables SCREAMING_SNAKE_CASE = original_model.non_trainable_variables SCREAMING_SNAKE_CASE = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: SCREAMING_SNAKE_CASE = param.numpy() SCREAMING_SNAKE_CASE = list(tf_params.keys() ) # Load HuggingFace model SCREAMING_SNAKE_CASE = get_efficientnet_config(_lowerCamelCase ) SCREAMING_SNAKE_CASE = EfficientNetForImageClassification(_lowerCamelCase ).eval() SCREAMING_SNAKE_CASE = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print("Converting parameters..." ) SCREAMING_SNAKE_CASE = rename_keys(_lowerCamelCase ) replace_params(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Initialize preprocessor and preprocess input image SCREAMING_SNAKE_CASE = convert_image_processor(_lowerCamelCase ) SCREAMING_SNAKE_CASE = preprocessor(images=prepare_img() , return_tensors="pt" ) # HF model inference hf_model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = hf_model(**_lowerCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits.detach().numpy() # Original model inference SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = CONFIG_MAP[model_name]["image_size"] SCREAMING_SNAKE_CASE = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) SCREAMING_SNAKE_CASE = image.img_to_array(_lowerCamelCase ) SCREAMING_SNAKE_CASE = np.expand_dims(_lowerCamelCase , axis=0 ) SCREAMING_SNAKE_CASE = original_model.predict(_lowerCamelCase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_lowerCamelCase , _lowerCamelCase , atol=1e-3 ), "The predicted logits are not the same." print("Model outputs match!" ) if save_model: # Create folder to save model if not os.path.isdir(_lowerCamelCase ): os.mkdir(_lowerCamelCase ) # Save converted model and image processor hf_model.save_pretrained(_lowerCamelCase ) preprocessor.save_pretrained(_lowerCamelCase ) if push_to_hub: # Push model and image processor to hub print(F"Pushing converted {model_name} to the hub..." ) SCREAMING_SNAKE_CASE = F"efficientnet-{model_name}" preprocessor.push_to_hub(_lowerCamelCase ) hf_model.push_to_hub(_lowerCamelCase ) if __name__ == "__main__": _lowerCamelCase : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') _lowerCamelCase : Dict = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
717
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # 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" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> 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()
647
0
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline _lowerCamelCase : Optional[int] = logging.get_logger(__name__) # pylint: disable=invalid-name class lowercase ( _UpperCAmelCase ): def __init__( self : Any , _UpperCamelCase : Dict , _UpperCamelCase : List[Any] ) -> Dict: '''simple docstring''' super().__init__() self.register_modules(unet=lowerCamelCase_ , scheduler=lowerCamelCase_ ) @torch.no_grad() def __call__( self : Dict , _UpperCamelCase : int = 1 , _UpperCamelCase : int = 100 , _UpperCamelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _UpperCamelCase : Optional[float] = None , _UpperCamelCase : bool = True , ) -> Union[AudioPipelineOutput, Tuple]: '''simple docstring''' if audio_length_in_s is None: SCREAMING_SNAKE_CASE = self.unet.config.sample_size / self.unet.config.sample_rate SCREAMING_SNAKE_CASE = audio_length_in_s * self.unet.config.sample_rate SCREAMING_SNAKE_CASE = 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}." ) SCREAMING_SNAKE_CASE = int(lowerCamelCase_ ) if sample_size % down_scale_factor != 0: SCREAMING_SNAKE_CASE = ( (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." ) SCREAMING_SNAKE_CASE = int(lowerCamelCase_ ) SCREAMING_SNAKE_CASE = next(iter(self.unet.parameters() ) ).dtype SCREAMING_SNAKE_CASE = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and len(lowerCamelCase_ ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(lowerCamelCase_ )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) SCREAMING_SNAKE_CASE = randn_tensor(lowerCamelCase_ , generator=lowerCamelCase_ , device=self.device , dtype=lowerCamelCase_ ) # set step values self.scheduler.set_timesteps(lowerCamelCase_ , device=audio.device ) SCREAMING_SNAKE_CASE = self.scheduler.timesteps.to(lowerCamelCase_ ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output SCREAMING_SNAKE_CASE = self.unet(lowerCamelCase_ , lowerCamelCase_ ).sample # 2. compute previous image: x_t -> t_t-1 SCREAMING_SNAKE_CASE = self.scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ).prev_sample SCREAMING_SNAKE_CASE = audio.clamp(-1 , 1 ).float().cpu().numpy() SCREAMING_SNAKE_CASE = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=lowerCamelCase_ )
718
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : 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(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
0
class lowercase : def __init__( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = name SCREAMING_SNAKE_CASE = val def __str__( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' return F"{self.__class__.__name__}({self.name}, {self.val})" def __lt__( self : Optional[Any] , _UpperCamelCase : List[str] ) -> Any: '''simple docstring''' return self.val < other.val class lowercase : def __init__( self : str , _UpperCamelCase : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = self.build_heap(__UpperCamelCase ) def __getitem__( self : List[str] , _UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' return self.get_value(__UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : int ) -> Union[str, Any]: '''simple docstring''' return (idx - 1) // 2 def __snake_case( self : Optional[Any] , _UpperCamelCase : Dict ) -> List[Any]: '''simple docstring''' return idx * 2 + 1 def __snake_case( self : Any , _UpperCamelCase : Optional[int] ) -> Tuple: '''simple docstring''' return idx * 2 + 2 def __snake_case( self : List[str] , _UpperCamelCase : str ) -> List[Any]: '''simple docstring''' return self.heap_dict[key] def __snake_case( self : Tuple , _UpperCamelCase : int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = len(__UpperCamelCase ) - 1 SCREAMING_SNAKE_CASE = self.get_parent_idx(__UpperCamelCase ) for idx, i in enumerate(__UpperCamelCase ): SCREAMING_SNAKE_CASE = idx SCREAMING_SNAKE_CASE = i.val for i in range(__UpperCamelCase , -1 , -1 ): self.sift_down(__UpperCamelCase , __UpperCamelCase ) return array def __snake_case( self : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' while True: SCREAMING_SNAKE_CASE = self.get_left_child_idx(__UpperCamelCase ) # noqa: E741 SCREAMING_SNAKE_CASE = self.get_right_child_idx(__UpperCamelCase ) SCREAMING_SNAKE_CASE = idx if l < len(__UpperCamelCase ) and array[l] < array[idx]: SCREAMING_SNAKE_CASE = l if r < len(__UpperCamelCase ) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE = r if smallest != idx: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE = smallest else: break def __snake_case( self : Optional[Any] , _UpperCamelCase : Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_parent_idx(__UpperCamelCase ) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE = p SCREAMING_SNAKE_CASE = self.get_parent_idx(__UpperCamelCase ) def __snake_case( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' return self.heap[0] def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE = self.heap.pop() del self.idx_of_element[x] self.sift_down(0 , self.heap ) return x def __snake_case( self : Optional[Any] , _UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' self.heap.append(__UpperCamelCase ) SCREAMING_SNAKE_CASE = len(self.heap ) - 1 SCREAMING_SNAKE_CASE = node.val self.sift_up(len(self.heap ) - 1 ) def __snake_case( self : List[Any] ) -> str: '''simple docstring''' return len(self.heap ) == 0 def __snake_case( self : List[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : List[str] ) -> Optional[Any]: '''simple docstring''' assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE = new_value SCREAMING_SNAKE_CASE = new_value self.sift_up(self.idx_of_element[node] ) _lowerCamelCase : int = Node('''R''', -1) _lowerCamelCase : Tuple = Node('''B''', 6) _lowerCamelCase : Union[str, Any] = Node('''A''', 3) _lowerCamelCase : List[str] = Node('''X''', 1) _lowerCamelCase : Dict = Node('''E''', 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array _lowerCamelCase : Tuple = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print('''Min Heap - before decrease key''') for i in my_min_heap.heap: print(i) print('''Min Heap - After decrease key of node [B -> -17]''') my_min_heap.decrease_key(b, -17) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
719
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
0
from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _lowerCamelCase : Optional[int] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : Optional[int] ): if isinstance(UpperCAmelCase__ , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(UpperCAmelCase__ , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(UpperCAmelCase__ ): return [[videos]] raise ValueError(F"Could not make batched video from {videos}" ) class lowercase ( _a ): lowercase__ : Tuple = ["""pixel_values"""] def __init__( self : List[str] , _UpperCamelCase : List[Any] = True , _UpperCamelCase : Tuple = None , _UpperCamelCase : str = PILImageResampling.BILINEAR , _UpperCamelCase : int = True , _UpperCamelCase : Tuple = None , _UpperCamelCase : int = True , _UpperCamelCase : Dict = 1 / 255 , _UpperCamelCase : List[str] = True , _UpperCamelCase : List[str] = True , _UpperCamelCase : Dict = None , _UpperCamelCase : int = None , **_UpperCamelCase : int , ) -> None: '''simple docstring''' super().__init__(**snake_case_ ) SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 256} SCREAMING_SNAKE_CASE = get_size_dict(snake_case_ , default_to_square=snake_case_ ) SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(snake_case_ , param_name="crop_size" ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_center_crop SCREAMING_SNAKE_CASE = crop_size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = offset SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE = image_std if image_std is not None else IMAGENET_STANDARD_STD def __snake_case( self : Dict , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] = PILImageResampling.BILINEAR , _UpperCamelCase : List[Any] = None , **_UpperCamelCase : Union[str, Any] , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(snake_case_ , default_to_square=snake_case_ ) if "shortest_edge" in size: SCREAMING_SNAKE_CASE = get_resize_output_image_size(snake_case_ , size["shortest_edge"] , default_to_square=snake_case_ ) elif "height" in size and "width" in size: SCREAMING_SNAKE_CASE = (size["height"], size["width"]) else: raise ValueError(F"Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}" ) return resize(snake_case_ , size=snake_case_ , resample=snake_case_ , data_format=snake_case_ , **snake_case_ ) def __snake_case( self : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple = None , **_UpperCamelCase : List[str] , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(snake_case_ ) if "height" not in size or "width" not in size: raise ValueError(F"Size must have \'height\' and \'width\' as keys. Got {size.keys()}" ) return center_crop(snake_case_ , size=(size["height"], size["width"]) , data_format=snake_case_ , **snake_case_ ) def __snake_case( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : Tuple , _UpperCamelCase : Union[str, Any] = True , _UpperCamelCase : Optional[int] = None , **_UpperCamelCase : Dict , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = image.astype(np.floataa ) if offset: SCREAMING_SNAKE_CASE = image - (scale / 2) return rescale(snake_case_ , scale=snake_case_ , data_format=snake_case_ , **snake_case_ ) def __snake_case( self : int , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] = None , **_UpperCamelCase : List[str] , ) -> np.ndarray: '''simple docstring''' return normalize(snake_case_ , mean=snake_case_ , std=snake_case_ , data_format=snake_case_ , **snake_case_ ) def __snake_case( self : str , _UpperCamelCase : List[Any] , _UpperCamelCase : Any = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : List[str] = None , _UpperCamelCase : Union[str, Any] = None , _UpperCamelCase : str = None , _UpperCamelCase : Dict = None , _UpperCamelCase : Any = None , _UpperCamelCase : List[str] = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : List[str] = None , _UpperCamelCase : str = ChannelDimension.FIRST , ) -> np.ndarray: '''simple docstring''' if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) if offset and not do_rescale: raise ValueError("For offset, do_rescale must also be set to True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = to_numpy_array(snake_case_ ) if do_resize: SCREAMING_SNAKE_CASE = self.resize(image=snake_case_ , size=snake_case_ , resample=snake_case_ ) if do_center_crop: SCREAMING_SNAKE_CASE = self.center_crop(snake_case_ , size=snake_case_ ) if do_rescale: SCREAMING_SNAKE_CASE = self.rescale(image=snake_case_ , scale=snake_case_ , offset=snake_case_ ) if do_normalize: SCREAMING_SNAKE_CASE = self.normalize(image=snake_case_ , mean=snake_case_ , std=snake_case_ ) SCREAMING_SNAKE_CASE = to_channel_dimension_format(snake_case_ , snake_case_ ) return image def __snake_case( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : str = None , _UpperCamelCase : Any = None , _UpperCamelCase : Any = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : Dict = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : Union[str, Any] = None , _UpperCamelCase : Any = None , _UpperCamelCase : List[str] = None , _UpperCamelCase : Any = None , _UpperCamelCase : str = None , _UpperCamelCase : Tuple = None , _UpperCamelCase : List[str] = ChannelDimension.FIRST , **_UpperCamelCase : Union[str, Any] , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = do_center_crop if do_center_crop is not None else self.do_center_crop SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE = offset if offset is not None else self.offset SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(snake_case_ , default_to_square=snake_case_ ) SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else self.crop_size SCREAMING_SNAKE_CASE = get_size_dict(snake_case_ , param_name="crop_size" ) if not valid_images(snake_case_ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) SCREAMING_SNAKE_CASE = make_batched(snake_case_ ) SCREAMING_SNAKE_CASE = [ [ self._preprocess_image( image=snake_case_ , do_resize=snake_case_ , size=snake_case_ , resample=snake_case_ , do_center_crop=snake_case_ , crop_size=snake_case_ , do_rescale=snake_case_ , rescale_factor=snake_case_ , offset=snake_case_ , do_normalize=snake_case_ , image_mean=snake_case_ , image_std=snake_case_ , data_format=snake_case_ , ) for img in video ] for video in videos ] SCREAMING_SNAKE_CASE = {"pixel_values": videos} return BatchFeature(data=snake_case_ , tensor_type=snake_case_ )
720
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model 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__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
0
import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( SCREAMING_SNAKE_CASE__ ): lowercase__ : Union[str, Any] = ["""input_ids""", """attention_mask"""] def __init__( self : Dict , _UpperCamelCase : List[Any]="</s>" , _UpperCamelCase : Union[str, Any]="<unk>" , _UpperCamelCase : int="<pad>" , _UpperCamelCase : List[str]=125 , _UpperCamelCase : Union[str, Any]=None , **_UpperCamelCase : Union[str, Any] , ) -> None: '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: SCREAMING_SNAKE_CASE = [F"<extra_id_{i}>" for i in range(_lowercase )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens SCREAMING_SNAKE_CASE = len(set(filter(lambda _UpperCamelCase : bool("extra_id" in str(_lowercase ) ) , _lowercase ) ) ) if extra_tokens != extra_ids: raise ValueError( F"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are" " provided to ByT5Tokenizer. In this case the additional_special_tokens must include the" " extra_ids tokens" ) SCREAMING_SNAKE_CASE = AddedToken(_lowercase , lstrip=_lowercase , rstrip=_lowercase ) if isinstance(_lowercase , _lowercase ) else pad_token SCREAMING_SNAKE_CASE = AddedToken(_lowercase , lstrip=_lowercase , rstrip=_lowercase ) if isinstance(_lowercase , _lowercase ) else eos_token SCREAMING_SNAKE_CASE = AddedToken(_lowercase , lstrip=_lowercase , rstrip=_lowercase ) if isinstance(_lowercase , _lowercase ) else unk_token super().__init__( eos_token=_lowercase , unk_token=_lowercase , pad_token=_lowercase , extra_ids=_lowercase , additional_special_tokens=_lowercase , **_lowercase , ) SCREAMING_SNAKE_CASE = extra_ids SCREAMING_SNAKE_CASE = 2**8 # utf is 8 bits # define special tokens dict SCREAMING_SNAKE_CASE = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } SCREAMING_SNAKE_CASE = len(self.special_tokens_encoder ) SCREAMING_SNAKE_CASE = len(_lowercase ) for i, token in enumerate(_lowercase ): SCREAMING_SNAKE_CASE = self.vocab_size + i - n SCREAMING_SNAKE_CASE = {v: k for k, v in self.special_tokens_encoder.items()} @property def __snake_case( self : List[str] ) -> int: '''simple docstring''' return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def __snake_case( self : str , _UpperCamelCase : Optional[Any] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[Any] = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_lowercase , token_ids_a=_lowercase , already_has_special_tokens=_lowercase ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(_lowercase )) + [1] return ([0] * len(_lowercase )) + [1] + ([0] * len(_lowercase )) + [1] def __snake_case( self : Optional[Any] , _UpperCamelCase : Optional[int] ) -> List[int]: '''simple docstring''' if len(_lowercase ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( F"This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated" " eos tokens being added." ) return token_ids else: return token_ids + [self.eos_token_id] def __snake_case( self : str , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [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 : Any , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[Any] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self._add_eos_if_not_present(_lowercase ) if token_ids_a is None: return token_ids_a else: SCREAMING_SNAKE_CASE = self._add_eos_if_not_present(_lowercase ) return token_ids_a + token_ids_a def __snake_case( self : Tuple , _UpperCamelCase : Optional[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = [chr(_lowercase ) for i in text.encode("utf-8" )] return tokens def __snake_case( self : str , _UpperCamelCase : Optional[Any] ) -> int: '''simple docstring''' if token in self.special_tokens_encoder: SCREAMING_SNAKE_CASE = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: SCREAMING_SNAKE_CASE = self.added_tokens_encoder[token] elif len(_lowercase ) != 1: SCREAMING_SNAKE_CASE = self.unk_token_id else: SCREAMING_SNAKE_CASE = ord(_lowercase ) + self._num_special_tokens return token_id def __snake_case( self : List[str] , _UpperCamelCase : Any ) -> Optional[Any]: '''simple docstring''' if index in self.special_tokens_decoder: SCREAMING_SNAKE_CASE = self.special_tokens_decoder[index] else: SCREAMING_SNAKE_CASE = chr(index - self._num_special_tokens ) return token def __snake_case( self : Union[str, Any] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = b"""""" for token in tokens: if token in self.special_tokens_decoder: SCREAMING_SNAKE_CASE = self.special_tokens_decoder[token].encode("utf-8" ) elif token in self.added_tokens_decoder: SCREAMING_SNAKE_CASE = self.special_tokens_decoder[token].encode("utf-8" ) elif token in self.special_tokens_encoder: SCREAMING_SNAKE_CASE = token.encode("utf-8" ) elif token in self.added_tokens_encoder: SCREAMING_SNAKE_CASE = token.encode("utf-8" ) else: SCREAMING_SNAKE_CASE = bytes([ord(_lowercase )] ) bstring += tok_string SCREAMING_SNAKE_CASE = bstring.decode("utf-8" , errors="ignore" ) return string def __snake_case( self : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[Any] = None ) -> Tuple[str]: '''simple docstring''' return ()
721
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = 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' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # 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 : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
0
from __future__ import annotations import bisect def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 0 , UpperCAmelCase__ : int = -1 ): if hi < 0: SCREAMING_SNAKE_CASE = len(snake_case__ ) while lo < hi: SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] < item: SCREAMING_SNAKE_CASE = mid + 1 else: SCREAMING_SNAKE_CASE = mid return lo def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 0 , UpperCAmelCase__ : int = -1 ): if hi < 0: SCREAMING_SNAKE_CASE = len(snake_case__ ) while lo < hi: SCREAMING_SNAKE_CASE = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: SCREAMING_SNAKE_CASE = mid + 1 else: SCREAMING_SNAKE_CASE = mid return lo def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 0 , UpperCAmelCase__ : int = -1 ): sorted_collection.insert(bisect_left(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) , snake_case__ ) def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int = 0 , UpperCAmelCase__ : int = -1 ): sorted_collection.insert(bisect_right(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) , snake_case__ ) def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = len(snake_case__ ) - 1 while left <= right: SCREAMING_SNAKE_CASE = left + (right - left) // 2 SCREAMING_SNAKE_CASE = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: SCREAMING_SNAKE_CASE = midpoint - 1 else: SCREAMING_SNAKE_CASE = midpoint + 1 return None def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = bisect.bisect_left(snake_case__ , snake_case__ ) if index != len(snake_case__ ) and sorted_collection[index] == item: return index return None def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int ): if right < left: return None SCREAMING_SNAKE_CASE = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(snake_case__ , snake_case__ , snake_case__ , midpoint - 1 ) else: return binary_search_by_recursion(snake_case__ , snake_case__ , midpoint + 1 , snake_case__ ) if __name__ == "__main__": _lowerCamelCase : Dict = input('''Enter numbers separated by comma:\n''').strip() _lowerCamelCase : Tuple = sorted(int(item) for item in user_input.split(''',''')) _lowerCamelCase : Union[str, Any] = int(input('''Enter a single number to be found in the list:\n''')) _lowerCamelCase : List[str] = 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}.""")
700
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
0
import argparse import datetime import json import time import warnings from logging import getLogger from pathlib import Path from typing import Dict, List import torch from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import calculate_bleu, calculate_rouge, chunks, parse_numeric_n_bool_cl_kwargs, use_task_specific_params _lowerCamelCase : Dict = getLogger(__name__) _lowerCamelCase : Optional[int] = '''cuda''' if torch.cuda.is_available() else '''cpu''' def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[int] = 8 , UpperCAmelCase__ : Any = DEFAULT_DEVICE , UpperCAmelCase__ : Optional[Any]=False , UpperCAmelCase__ : Any="summarization" , UpperCAmelCase__ : List[str]=None , **UpperCAmelCase__ : Tuple , ): SCREAMING_SNAKE_CASE = Path(__SCREAMING_SNAKE_CASE ).open("w" , encoding="utf-8" ) SCREAMING_SNAKE_CASE = str(__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained(__SCREAMING_SNAKE_CASE ).to(__SCREAMING_SNAKE_CASE ) if fpaa: SCREAMING_SNAKE_CASE = model.half() SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) logger.info(F"Inferred tokenizer type: {tokenizer.__class__}" ) # if this is wrong, check config.model_type. SCREAMING_SNAKE_CASE = time.time() # update config with task specific params use_task_specific_params(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if prefix is None: SCREAMING_SNAKE_CASE = prefix or getattr(model.config , "prefix" , "" ) or "" for examples_chunk in tqdm(list(chunks(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) ): SCREAMING_SNAKE_CASE = [prefix + text for text in examples_chunk] SCREAMING_SNAKE_CASE = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors="pt" , truncation=__SCREAMING_SNAKE_CASE , padding="longest" ).to(__SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = model.generate( input_ids=batch.input_ids , attention_mask=batch.attention_mask , **__SCREAMING_SNAKE_CASE , ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=__SCREAMING_SNAKE_CASE ) for hypothesis in dec: fout.write(hypothesis + "\n" ) fout.flush() fout.close() SCREAMING_SNAKE_CASE = int(time.time() - start_time ) # seconds SCREAMING_SNAKE_CASE = len(__SCREAMING_SNAKE_CASE ) return {"n_obs": n_obs, "runtime": runtime, "seconds_per_sample": round(runtime / n_obs , 4 )} def __lowerCamelCase (): return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S" ) def __lowerCamelCase (UpperCAmelCase__ : Tuple=True ): SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("model_name" , type=__SCREAMING_SNAKE_CASE , help="like facebook/bart-large-cnn,t5-base, etc." ) parser.add_argument("input_path" , type=__SCREAMING_SNAKE_CASE , help="like cnn_dm/test.source" ) parser.add_argument("save_path" , type=__SCREAMING_SNAKE_CASE , help="where to save summaries" ) parser.add_argument("--reference_path" , type=__SCREAMING_SNAKE_CASE , required=__SCREAMING_SNAKE_CASE , help="like cnn_dm/test.target" ) parser.add_argument("--score_path" , type=__SCREAMING_SNAKE_CASE , required=__SCREAMING_SNAKE_CASE , default="metrics.json" , help="where to save metrics" ) parser.add_argument("--device" , type=__SCREAMING_SNAKE_CASE , required=__SCREAMING_SNAKE_CASE , default=__SCREAMING_SNAKE_CASE , help="cuda, cuda:1, cpu etc." ) parser.add_argument( "--prefix" , type=__SCREAMING_SNAKE_CASE , required=__SCREAMING_SNAKE_CASE , default=__SCREAMING_SNAKE_CASE , help="will be added to the begininng of src examples" ) parser.add_argument("--task" , type=__SCREAMING_SNAKE_CASE , default="summarization" , help="used for task_specific_params + metrics" ) parser.add_argument("--bs" , type=__SCREAMING_SNAKE_CASE , default=8 , required=__SCREAMING_SNAKE_CASE , help="batch size" ) parser.add_argument( "--n_obs" , type=__SCREAMING_SNAKE_CASE , default=-1 , required=__SCREAMING_SNAKE_CASE , help="How many observations. Defaults to all." ) parser.add_argument("--fp16" , action="store_true" ) parser.add_argument("--dump-args" , action="store_true" , help="print the custom hparams with the results" ) parser.add_argument( "--info" , nargs="?" , type=__SCREAMING_SNAKE_CASE , const=datetime_now() , help=( "use in conjunction w/ --dump-args to print with the results whatever other info you'd like, e.g." " lang=en-ru. If no value is passed, the current datetime string will be used." ) , ) # Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = parser.parse_known_args() SCREAMING_SNAKE_CASE = parse_numeric_n_bool_cl_kwargs(__SCREAMING_SNAKE_CASE ) if parsed_args and verbose: print(F"parsed the following generate kwargs: {parsed_args}" ) SCREAMING_SNAKE_CASE = [" " + x.rstrip() if "t5" in args.model_name else x.rstrip() for x in open(args.input_path ).readlines()] if args.n_obs > 0: SCREAMING_SNAKE_CASE = examples[: args.n_obs] Path(args.save_path ).parent.mkdir(exist_ok=__SCREAMING_SNAKE_CASE ) if args.reference_path is None and Path(args.score_path ).exists(): warnings.warn(F"score_path {args.score_path} will be overwritten unless you type ctrl-c." ) if args.device == "cpu" and args.fpaa: # this mix leads to RuntimeError: "threshold_cpu" not implemented for 'Half' raise ValueError("Can't mix --fp16 and --device cpu" ) SCREAMING_SNAKE_CASE = generate_summaries_or_translations( __SCREAMING_SNAKE_CASE , args.save_path , args.model_name , batch_size=args.bs , device=args.device , fpaa=args.fpaa , task=args.task , prefix=args.prefix , **__SCREAMING_SNAKE_CASE , ) if args.reference_path is None: return {} # Compute scores SCREAMING_SNAKE_CASE = calculate_bleu if "translation" in args.task else calculate_rouge SCREAMING_SNAKE_CASE = [x.rstrip() for x in open(args.save_path ).readlines()] SCREAMING_SNAKE_CASE = [x.rstrip() for x in open(args.reference_path ).readlines()][: len(__SCREAMING_SNAKE_CASE )] SCREAMING_SNAKE_CASE = score_fn(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) scores.update(__SCREAMING_SNAKE_CASE ) if args.dump_args: scores.update(__SCREAMING_SNAKE_CASE ) if args.info: SCREAMING_SNAKE_CASE = args.info if verbose: print(__SCREAMING_SNAKE_CASE ) if args.score_path is not None: json.dump(__SCREAMING_SNAKE_CASE , open(args.score_path , "w" ) ) return scores if __name__ == "__main__": # Usage for MT: # python run_eval.py MODEL_NAME $DATA_DIR/test.source $save_dir/test_translations.txt --reference_path $DATA_DIR/test.target --score_path $save_dir/test_bleu.json --task translation $@ run_generate(verbose=True)
701
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {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." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , 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}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
0
_lowerCamelCase : Tuple = { '''Pillow''': '''Pillow<10.0.0''', '''accelerate''': '''accelerate>=0.20.3''', '''av''': '''av==9.2.0''', '''beautifulsoup4''': '''beautifulsoup4''', '''black''': '''black~=23.1''', '''codecarbon''': '''codecarbon==1.2.0''', '''cookiecutter''': '''cookiecutter==1.7.3''', '''dataclasses''': '''dataclasses''', '''datasets''': '''datasets!=2.5.0''', '''decord''': '''decord==0.6.0''', '''deepspeed''': '''deepspeed>=0.9.3''', '''diffusers''': '''diffusers''', '''dill''': '''dill<0.3.5''', '''evaluate''': '''evaluate>=0.2.0''', '''fairscale''': '''fairscale>0.3''', '''faiss-cpu''': '''faiss-cpu''', '''fastapi''': '''fastapi''', '''filelock''': '''filelock''', '''flax''': '''flax>=0.4.1,<=0.7.0''', '''ftfy''': '''ftfy''', '''fugashi''': '''fugashi>=1.0''', '''GitPython''': '''GitPython<3.1.19''', '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''', '''huggingface-hub''': '''huggingface-hub>=0.14.1,<1.0''', '''importlib_metadata''': '''importlib_metadata''', '''ipadic''': '''ipadic>=1.0.0,<2.0''', '''isort''': '''isort>=5.5.4''', '''jax''': '''jax>=0.2.8,!=0.3.2,<=0.4.13''', '''jaxlib''': '''jaxlib>=0.1.65,<=0.4.13''', '''jieba''': '''jieba''', '''kenlm''': '''kenlm''', '''keras-nlp''': '''keras-nlp>=0.3.1''', '''librosa''': '''librosa''', '''nltk''': '''nltk''', '''natten''': '''natten>=0.14.6''', '''numpy''': '''numpy>=1.17''', '''onnxconverter-common''': '''onnxconverter-common''', '''onnxruntime-tools''': '''onnxruntime-tools>=1.4.2''', '''onnxruntime''': '''onnxruntime>=1.4.0''', '''opencv-python''': '''opencv-python''', '''optuna''': '''optuna''', '''optax''': '''optax>=0.0.8,<=0.1.4''', '''packaging''': '''packaging>=20.0''', '''parameterized''': '''parameterized''', '''phonemizer''': '''phonemizer''', '''protobuf''': '''protobuf''', '''psutil''': '''psutil''', '''pyyaml''': '''pyyaml>=5.1''', '''pydantic''': '''pydantic<2''', '''pytest''': '''pytest>=7.2.0''', '''pytest-timeout''': '''pytest-timeout''', '''pytest-xdist''': '''pytest-xdist''', '''python''': '''python>=3.8.0''', '''ray[tune]''': '''ray[tune]''', '''regex''': '''regex!=2019.12.17''', '''requests''': '''requests''', '''rhoknp''': '''rhoknp>=1.1.0,<1.3.1''', '''rjieba''': '''rjieba''', '''rouge-score''': '''rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1''', '''ruff''': '''ruff>=0.0.241,<=0.0.259''', '''sacrebleu''': '''sacrebleu>=1.4.12,<2.0.0''', '''sacremoses''': '''sacremoses''', '''safetensors''': '''safetensors>=0.3.1''', '''sagemaker''': '''sagemaker>=2.31.0''', '''scikit-learn''': '''scikit-learn''', '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''', '''sigopt''': '''sigopt''', '''starlette''': '''starlette''', '''sudachipy''': '''sudachipy>=0.6.6''', '''sudachidict_core''': '''sudachidict_core>=20220729''', '''tensorflow-cpu''': '''tensorflow-cpu>=2.6,<2.14''', '''tensorflow''': '''tensorflow>=2.6,<2.14''', '''tensorflow-text''': '''tensorflow-text<2.14''', '''tf2onnx''': '''tf2onnx''', '''timeout-decorator''': '''timeout-decorator''', '''timm''': '''timm''', '''tokenizers''': '''tokenizers>=0.11.1,!=0.11.3,<0.14''', '''torch''': '''torch>=1.9,!=1.12.0''', '''torchaudio''': '''torchaudio''', '''torchvision''': '''torchvision''', '''pyctcdecode''': '''pyctcdecode>=0.4.0''', '''tqdm''': '''tqdm>=4.27''', '''unidic''': '''unidic>=1.0.2''', '''unidic_lite''': '''unidic_lite>=1.0.7''', '''urllib3''': '''urllib3<2.0.0''', '''uvicorn''': '''uvicorn''', }
702
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
0
from ...configuration_utils import PretrainedConfig _lowerCamelCase : str = { """google/tapas-base-finetuned-sqa""": ( """https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json""" ), """google/tapas-base-finetuned-wtq""": ( """https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json""" ), """google/tapas-base-finetuned-wikisql-supervised""": ( """https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json""" ), """google/tapas-base-finetuned-tabfact""": ( """https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json""" ), } class lowercase ( UpperCamelCase_ ): lowercase__ : List[str] = """tapas""" def __init__( self : Any , _UpperCamelCase : str=30_522 , _UpperCamelCase : Optional[Any]=768 , _UpperCamelCase : Optional[Any]=12 , _UpperCamelCase : Tuple=12 , _UpperCamelCase : Optional[Any]=3_072 , _UpperCamelCase : Optional[Any]="gelu" , _UpperCamelCase : Optional[int]=0.1 , _UpperCamelCase : Dict=0.1 , _UpperCamelCase : Optional[Any]=1_024 , _UpperCamelCase : Optional[Any]=[3, 256, 256, 2, 256, 256, 10] , _UpperCamelCase : List[Any]=0.0_2 , _UpperCamelCase : Union[str, Any]=1e-12 , _UpperCamelCase : List[Any]=0 , _UpperCamelCase : Dict=10.0 , _UpperCamelCase : Optional[int]=0 , _UpperCamelCase : Dict=1.0 , _UpperCamelCase : int=None , _UpperCamelCase : Dict=1.0 , _UpperCamelCase : Dict=False , _UpperCamelCase : List[str]=None , _UpperCamelCase : List[Any]=1.0 , _UpperCamelCase : Dict=1.0 , _UpperCamelCase : str=False , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : List[str]="ratio" , _UpperCamelCase : List[Any]=None , _UpperCamelCase : str=None , _UpperCamelCase : Dict=64 , _UpperCamelCase : int=32 , _UpperCamelCase : int=False , _UpperCamelCase : Dict=True , _UpperCamelCase : List[str]=False , _UpperCamelCase : Dict=False , _UpperCamelCase : Dict=True , _UpperCamelCase : Dict=False , _UpperCamelCase : str=None , _UpperCamelCase : int=None , **_UpperCamelCase : int , ) -> Optional[int]: '''simple docstring''' super().__init__(pad_token_id=__A , **__A ) # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes) 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_sizes SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps # Fine-tuning task hyperparameters SCREAMING_SNAKE_CASE = positive_label_weight SCREAMING_SNAKE_CASE = num_aggregation_labels SCREAMING_SNAKE_CASE = aggregation_loss_weight SCREAMING_SNAKE_CASE = use_answer_as_supervision SCREAMING_SNAKE_CASE = answer_loss_importance SCREAMING_SNAKE_CASE = use_normalized_answer_loss SCREAMING_SNAKE_CASE = huber_loss_delta SCREAMING_SNAKE_CASE = temperature SCREAMING_SNAKE_CASE = aggregation_temperature SCREAMING_SNAKE_CASE = use_gumbel_for_cells SCREAMING_SNAKE_CASE = use_gumbel_for_aggregation SCREAMING_SNAKE_CASE = average_approximation_function SCREAMING_SNAKE_CASE = cell_selection_preference SCREAMING_SNAKE_CASE = answer_loss_cutoff SCREAMING_SNAKE_CASE = max_num_rows SCREAMING_SNAKE_CASE = max_num_columns SCREAMING_SNAKE_CASE = average_logits_per_cell SCREAMING_SNAKE_CASE = select_one_column SCREAMING_SNAKE_CASE = allow_empty_column_selection SCREAMING_SNAKE_CASE = init_cell_selection_weights_to_zero SCREAMING_SNAKE_CASE = reset_position_index_per_cell SCREAMING_SNAKE_CASE = disable_per_token_loss # Aggregation hyperparameters SCREAMING_SNAKE_CASE = aggregation_labels SCREAMING_SNAKE_CASE = no_aggregation_label_index if isinstance(self.aggregation_labels , __A ): SCREAMING_SNAKE_CASE = {int(__A ): v for k, v in aggregation_labels.items()}
703
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
0
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ConvNextConfig, UperNetConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import UperNetForSemanticSegmentation from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase : def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple=13 , _UpperCamelCase : Union[str, Any]=32 , _UpperCamelCase : str=3 , _UpperCamelCase : Dict=4 , _UpperCamelCase : List[Any]=[10, 20, 30, 40] , _UpperCamelCase : Optional[int]=[2, 2, 3, 2] , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : List[Any]=True , _UpperCamelCase : str=37 , _UpperCamelCase : Union[str, Any]="gelu" , _UpperCamelCase : Any=10 , _UpperCamelCase : List[Any]=0.0_2 , _UpperCamelCase : Dict=["stage2", "stage3", "stage4"] , _UpperCamelCase : str=3 , _UpperCamelCase : List[Any]=None , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = num_stages SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = out_features SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = num_stages def __snake_case( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Union[str, Any] ) -> int: '''simple docstring''' return ConvNextConfig( num_channels=self.num_channels , num_stages=self.num_stages , hidden_sizes=self.hidden_sizes , depths=self.depths , is_training=self.is_training , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , out_features=self.out_features , ) def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return UperNetConfig( backbone_config=self.get_backbone_config() , hidden_size=512 , pool_scales=[1, 2, 3, 6] , use_auxiliary_head=_UpperCamelCase , auxiliary_loss_weight=0.4 , auxiliary_in_channels=40 , auxiliary_channels=256 , auxiliary_num_convs=1 , auxiliary_concat_input=_UpperCamelCase , loss_ignore_index=255 , num_labels=self.num_labels , ) def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = UperNetForSemanticSegmentation(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def __snake_case( self : Optional[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( SCREAMING_SNAKE_CASE ) = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( lowercase__ , lowercase__ , unittest.TestCase ): lowercase__ : Dict = (UperNetForSemanticSegmentation,) if is_torch_available() else () lowercase__ : int = {"""image-segmentation""": UperNetForSemanticSegmentation} if is_torch_available() else {} lowercase__ : Union[str, Any] = False lowercase__ : int = False lowercase__ : Optional[Any] = False lowercase__ : Any = False lowercase__ : Optional[int] = False lowercase__ : Dict = False def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = UperNetModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Dict ) -> Union[str, Any]: '''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 : List[str] ) -> Optional[Any]: '''simple docstring''' return def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_UpperCamelCase ) @unittest.skip(reason="UperNet does not use inputs_embeds" ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' pass @unittest.skip(reason="UperNet does not support input and output embeddings" ) def __snake_case( self : Any ) -> str: '''simple docstring''' pass @unittest.skip(reason="UperNet does not have a base model" ) def __snake_case( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason="UperNet does not have a base model" ) def __snake_case( self : Any ) -> Dict: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason="UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' def check_hidden_states_output(_UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states SCREAMING_SNAKE_CASE = self.model_tester.num_stages self.assertEqual(len(_UpperCamelCase ) , expected_num_stages + 1 ) # ConvNext'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 // 4, self.model_tester.image_size // 4] , ) SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def __snake_case( self : Tuple ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) SCREAMING_SNAKE_CASE = _config_zero_init(configs_no_init.backbone_config ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @unittest.skip(reason="UperNet does not have tied weights" ) def __snake_case( self : List[str] ) -> List[str]: '''simple docstring''' pass @slow def __snake_case( self : Any ) -> List[str]: '''simple docstring''' for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = UperNetForSemanticSegmentation.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = hf_hub_download( repo_id="hf-internal-testing/fixtures_ade20k" , repo_type="dataset" , filename="ADE_val_00000001.jpg" ) SCREAMING_SNAKE_CASE = Image.open(_lowerCamelCase ).convert("RGB" ) return image @require_torch @require_vision @slow class lowercase ( unittest.TestCase ): def __snake_case( self : Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny" ) SCREAMING_SNAKE_CASE = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , _UpperCamelCase , atol=1e-4 ) ) def __snake_case( self : Tuple ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny" ) SCREAMING_SNAKE_CASE = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , _UpperCamelCase , atol=1e-4 ) )
704
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
0
import sys _lowerCamelCase : List[Any] = ( '''73167176531330624919225119674426574742355349194934''' '''96983520312774506326239578318016984801869478851843''' '''85861560789112949495459501737958331952853208805511''' '''12540698747158523863050715693290963295227443043557''' '''66896648950445244523161731856403098711121722383113''' '''62229893423380308135336276614282806444486645238749''' '''30358907296290491560440772390713810515859307960866''' '''70172427121883998797908792274921901699720888093776''' '''65727333001053367881220235421809751254540594752243''' '''52584907711670556013604839586446706324415722155397''' '''53697817977846174064955149290862569321978468622482''' '''83972241375657056057490261407972968652414535100474''' '''82166370484403199890008895243450658541227588666881''' '''16427171479924442928230863465674813919123162824586''' '''17866458359124566529476545682848912883142607690042''' '''24219022671055626321111109370544217506941658960408''' '''07198403850962455444362981230987879927244284909188''' '''84580156166097919133875499200524063689912560717606''' '''05886116467109405077541002256983155200055935729725''' '''71636269561882670428252483600823257530420752963450''' ) def __lowerCamelCase (UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = 1 for digit in s: product *= int(UpperCAmelCase__ ) return product def __lowerCamelCase (UpperCAmelCase__ : str = N ): SCREAMING_SNAKE_CASE = -sys.maxsize - 1 SCREAMING_SNAKE_CASE = n[:1_3] SCREAMING_SNAKE_CASE = 1_3 while cur_index < len(UpperCAmelCase__ ) - 1_3: if int(n[cur_index] ) >= int(substr[0] ): SCREAMING_SNAKE_CASE = substr[1:] + n[cur_index] cur_index += 1 else: SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , str_eval(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = n[cur_index : cur_index + 1_3] cur_index += 1_3 return largest_product if __name__ == "__main__": print(f"""{solution() = }""")
705
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[Any] = { '''configuration_convbert''': ['''CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ConvBertConfig''', '''ConvBertOnnxConfig'''], '''tokenization_convbert''': ['''ConvBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = ['''ConvBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[Any] = [ '''CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ConvBertForMaskedLM''', '''ConvBertForMultipleChoice''', '''ConvBertForQuestionAnswering''', '''ConvBertForSequenceClassification''', '''ConvBertForTokenClassification''', '''ConvBertLayer''', '''ConvBertModel''', '''ConvBertPreTrainedModel''', '''load_tf_weights_in_convbert''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFConvBertForMaskedLM''', '''TFConvBertForMultipleChoice''', '''TFConvBertForQuestionAnswering''', '''TFConvBertForSequenceClassification''', '''TFConvBertForTokenClassification''', '''TFConvBertLayer''', '''TFConvBertModel''', '''TFConvBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys _lowerCamelCase : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
706
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) 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 = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
0
import os import re import shutil import sys import tempfile import unittest import black _lowerCamelCase : List[Any] = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. _lowerCamelCase : Dict = ''' \"\"\"\n Output class for the scheduler\'s step function output.\n\n Args:\n prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n denoising loop.\n pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n `pred_original_sample` can be used to preview progress or for guidance.\n \"\"\"\n\n prev_sample: torch.FloatTensor\n pred_original_sample: Optional[torch.FloatTensor] = None\n''' class lowercase ( unittest.TestCase ): def __snake_case( self : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , "schedulers/" ) ) SCREAMING_SNAKE_CASE = self.diffusers_dir shutil.copy( os.path.join(__lowerCAmelCase , "src/diffusers/schedulers/scheduling_ddpm.py" ) , os.path.join(self.diffusers_dir , "schedulers/scheduling_ddpm.py" ) , ) def __snake_case( self : Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "src/diffusers" shutil.rmtree(self.diffusers_dir ) def __snake_case( self : List[str] , _UpperCamelCase : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict , _UpperCamelCase : str=None ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = comment + F"\nclass {class_name}(nn.Module):\n" + class_code if overwrite_result is not None: SCREAMING_SNAKE_CASE = comment + F"\nclass {class_name}(nn.Module):\n" + overwrite_result SCREAMING_SNAKE_CASE = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) SCREAMING_SNAKE_CASE = black.format_str(__lowerCAmelCase , mode=__lowerCAmelCase ) SCREAMING_SNAKE_CASE = os.path.join(self.diffusers_dir , "new_code.py" ) with open(__lowerCAmelCase , "w" , newline="\n" ) as f: f.write(__lowerCAmelCase ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(__lowerCAmelCase ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=__lowerCAmelCase ) with open(__lowerCAmelCase , "r" ) as f: self.assertTrue(f.read() , __lowerCAmelCase ) def __snake_case( self : Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput" ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput" , "DDPMSchedulerOutput" , __lowerCAmelCase , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , re.sub("DDPM" , "Test" , __lowerCAmelCase ) , ) # Copy consistency with a really long name SCREAMING_SNAKE_CASE = "TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( F"# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}" , F"{long_class_name}SchedulerOutput" , re.sub("Bert" , __lowerCAmelCase , __lowerCAmelCase ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test" , "TestSchedulerOutput" , __lowerCAmelCase , overwrite_result=re.sub("DDPM" , "Test" , __lowerCAmelCase ) , )
707
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
0
import enum import shutil import sys _lowerCamelCase : List[Any] = shutil.get_terminal_size() _lowerCamelCase : Any = {"UP": "A", "DOWN": "B", "RIGHT": "C", "LEFT": "D"} class lowercase ( enum.Enum ): lowercase__ : Any = 0 lowercase__ : List[Any] = 1 def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Tuple="" ): sys.stdout.write(str(UpperCAmelCase__ ) + end ) sys.stdout.flush() def __lowerCamelCase (UpperCAmelCase__ : Dict , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Dict="" ): forceWrite(F"\u001b[{color}m{content}\u001b[0m" , UpperCAmelCase__ ) def __lowerCamelCase (): forceWrite("\r" ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : str ): forceWrite(F"\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}" ) def __lowerCamelCase (): forceWrite(" " * TERMINAL_WIDTH ) reset_cursor() def __lowerCamelCase (): reset_cursor() forceWrite("-" * TERMINAL_WIDTH )
708
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
0
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Tuple = logging.get_logger(__name__) _lowerCamelCase : List[str] = '''▁''' _lowerCamelCase : Optional[int] = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', } _lowerCamelCase : Optional[Any] = { '''vocab_file''': { '''facebook/s2t-small-librispeech-asr''': ( '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json''' ), }, '''spm_file''': { '''facebook/s2t-small-librispeech-asr''': ( '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model''' ) }, } _lowerCamelCase : Tuple = { '''facebook/s2t-small-librispeech-asr''': 10_24, } _lowerCamelCase : Optional[int] = ['''pt''', '''fr''', '''ru''', '''nl''', '''ro''', '''it''', '''es''', '''de'''] _lowerCamelCase : str = {'''mustc''': MUSTC_LANGS} class lowercase ( a__ ): lowercase__ : Optional[Any] = VOCAB_FILES_NAMES lowercase__ : List[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : List[Any] = MAX_MODEL_INPUT_SIZES lowercase__ : List[str] = ["""input_ids""", """attention_mask"""] lowercase__ : List[int] = [] def __init__( self : Optional[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : List[str]="<s>" , _UpperCamelCase : Union[str, Any]="</s>" , _UpperCamelCase : Optional[int]="<pad>" , _UpperCamelCase : Dict="<unk>" , _UpperCamelCase : Optional[int]=False , _UpperCamelCase : Optional[int]=False , _UpperCamelCase : Tuple=None , _UpperCamelCase : int=None , _UpperCamelCase : Optional[Any] = None , **_UpperCamelCase : List[Any] , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , do_upper_case=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__ , tgt_lang=lowerCAmelCase__ , lang_codes=lowerCAmelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE = do_upper_case SCREAMING_SNAKE_CASE = do_lower_case SCREAMING_SNAKE_CASE = load_json(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE = {v: k for k, v in self.encoder.items()} SCREAMING_SNAKE_CASE = spm_file SCREAMING_SNAKE_CASE = load_spm(lowerCAmelCase__ , self.sp_model_kwargs ) if lang_codes is not None: SCREAMING_SNAKE_CASE = lang_codes SCREAMING_SNAKE_CASE = LANGUAGES[lang_codes] SCREAMING_SNAKE_CASE = [F"<lang:{lang}>" for lang in self.langs] SCREAMING_SNAKE_CASE = {lang: self.sp_model.PieceToId(F"<lang:{lang}>" ) for lang in self.langs} SCREAMING_SNAKE_CASE = self.lang_tokens SCREAMING_SNAKE_CASE = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang ) else: SCREAMING_SNAKE_CASE = {} @property def __snake_case( self : int ) -> int: '''simple docstring''' return len(self.encoder ) @property def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' return self._tgt_lang @tgt_lang.setter def __snake_case( self : str , _UpperCamelCase : List[Any] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = new_tgt_lang self.set_tgt_lang_special_tokens(lowerCAmelCase__ ) def __snake_case( self : Optional[Any] , _UpperCamelCase : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = self.lang_code_to_id[tgt_lang] SCREAMING_SNAKE_CASE = [lang_code_id] def __snake_case( self : str , _UpperCamelCase : int ) -> List[str]: '''simple docstring''' return self.sp_model.encode(lowerCAmelCase__ , out_type=lowerCAmelCase__ ) def __snake_case( self : Dict , _UpperCamelCase : Any ) -> Any: '''simple docstring''' return self.encoder.get(lowerCAmelCase__ , self.encoder[self.unk_token] ) def __snake_case( self : int , _UpperCamelCase : Optional[int] ) -> str: '''simple docstring''' return self.decoder.get(lowerCAmelCase__ , self.unk_token ) def __snake_case( self : Tuple , _UpperCamelCase : Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = "" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: SCREAMING_SNAKE_CASE = self.sp_model.decode(lowerCAmelCase__ ) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " SCREAMING_SNAKE_CASE = [] else: current_sub_tokens.append(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE = self.sp_model.decode(lowerCAmelCase__ ) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def __snake_case( self : Any , _UpperCamelCase : int , _UpperCamelCase : Optional[int]=None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # 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.eos_token_id] def __snake_case( self : Tuple , _UpperCamelCase : Tuple , _UpperCamelCase : Any = None , _UpperCamelCase : Any = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE = [1] * len(self.prefix_tokens ) SCREAMING_SNAKE_CASE = [1] if token_ids_a is None: return prefix_ones + ([0] * len(lowerCAmelCase__ )) + suffix_ones return prefix_ones + ([0] * len(lowerCAmelCase__ )) + ([0] * len(lowerCAmelCase__ )) + suffix_ones def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.encoder.copy() vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : Dict , _UpperCamelCase : str ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = load_spm(self.spm_file , self.sp_model_kwargs ) def __snake_case( self : Dict , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] = None ) -> Tuple[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = Path(lowerCAmelCase__ ) assert save_dir.is_dir(), F"{save_directory} should be a directory" SCREAMING_SNAKE_CASE = save_dir / ( (filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["vocab_file"] ) SCREAMING_SNAKE_CASE = save_dir / ( (filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["spm_file"] ) save_json(self.encoder , lowerCAmelCase__ ) if os.path.abspath(self.spm_file ) != os.path.abspath(lowerCAmelCase__ ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , lowerCAmelCase__ ) elif not os.path.isfile(self.spm_file ): with open(lowerCAmelCase__ , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(lowerCAmelCase__ ) return (str(lowerCAmelCase__ ), str(lowerCAmelCase__ )) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Dict[str, Any] ): SCREAMING_SNAKE_CASE = sentencepiece.SentencePieceProcessor(**_lowercase ) spm.Load(str(_lowercase ) ) return spm def __lowerCamelCase (UpperCAmelCase__ : str ): with open(_lowercase , "r" ) as f: return json.load(_lowercase ) def __lowerCamelCase (UpperCAmelCase__ : Dict , UpperCAmelCase__ : str ): with open(_lowercase , "w" ) as f: json.dump(_lowercase , _lowercase , indent=2 )
709
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
0
_lowerCamelCase : Any = { '''A''': '''.-''', '''B''': '''-...''', '''C''': '''-.-.''', '''D''': '''-..''', '''E''': '''.''', '''F''': '''..-.''', '''G''': '''--.''', '''H''': '''....''', '''I''': '''..''', '''J''': '''.---''', '''K''': '''-.-''', '''L''': '''.-..''', '''M''': '''--''', '''N''': '''-.''', '''O''': '''---''', '''P''': '''.--.''', '''Q''': '''--.-''', '''R''': '''.-.''', '''S''': '''...''', '''T''': '''-''', '''U''': '''..-''', '''V''': '''...-''', '''W''': '''.--''', '''X''': '''-..-''', '''Y''': '''-.--''', '''Z''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on _lowerCamelCase : int = {value: key for key, value in MORSE_CODE_DICT.items()} def __lowerCamelCase (UpperCAmelCase__ : str ): return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def __lowerCamelCase (UpperCAmelCase__ : str ): return "".join(REVERSE_DICT[char] for char in message.split() ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = "Morse code here!" print(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = encrypt(UpperCAmelCase__ ) print(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = decrypt(UpperCAmelCase__ ) print(UpperCAmelCase__ ) if __name__ == "__main__": main()
710
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
0
from functools import lru_cache @lru_cache def __lowerCamelCase (UpperCAmelCase__ : Dict ): if num < 0: raise ValueError("Number should not be negative." ) return 1 if num in (0, 1) else num * factorial(num - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
711
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
0
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 : List[str] = [ 'openmmlab/upernet-convnext-tiny', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring _lowerCamelCase : Optional[Any] = 'UperNetConfig' class lowercase ( nn.Module ): def __init__( self : Any , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : Union[int, Tuple[int, int]] , _UpperCamelCase : Union[int, Tuple[int, int], str] = 0 , _UpperCamelCase : bool = False , _UpperCamelCase : Union[int, Tuple[int, int]] = 1 , ) -> str: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = nn.Convad( in_channels=UpperCamelCase__ , out_channels=UpperCamelCase__ , kernel_size=UpperCamelCase__ , padding=UpperCamelCase__ , bias=UpperCamelCase__ , dilation=UpperCamelCase__ , ) SCREAMING_SNAKE_CASE = nn.BatchNormad(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = nn.ReLU() def __snake_case( self : Tuple , _UpperCamelCase : torch.Tensor ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.conv(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = self.batch_norm(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = self.activation(UpperCamelCase__ ) return output class lowercase ( nn.Module ): def __init__( self : Optional[int] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int ) -> Any: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = [ nn.AdaptiveAvgPoolad(UpperCamelCase__ ), UperNetConvModule(UpperCamelCase__ , UpperCamelCase__ , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(UpperCamelCase__ ) , UpperCamelCase__ ) def __snake_case( self : List[Any] , _UpperCamelCase : torch.Tensor ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = input for layer in self.layers: SCREAMING_SNAKE_CASE = layer(UpperCamelCase__ ) return hidden_state class lowercase ( nn.Module ): def __init__( self : str , _UpperCamelCase : Tuple[int, ...] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : bool ) -> Tuple: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = pool_scales SCREAMING_SNAKE_CASE = align_corners SCREAMING_SNAKE_CASE = in_channels SCREAMING_SNAKE_CASE = channels SCREAMING_SNAKE_CASE = [] for i, pool_scale in enumerate(UpperCamelCase__ ): SCREAMING_SNAKE_CASE = UperNetPyramidPoolingBlock(pool_scale=UpperCamelCase__ , in_channels=UpperCamelCase__ , channels=UpperCamelCase__ ) self.blocks.append(UpperCamelCase__ ) self.add_module(str(UpperCamelCase__ ) , UpperCamelCase__ ) def __snake_case( self : Dict , _UpperCamelCase : torch.Tensor ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = [] for ppm in self.blocks: SCREAMING_SNAKE_CASE = ppm(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = nn.functional.interpolate( UpperCamelCase__ , size=x.size()[2:] , mode="bilinear" , align_corners=self.align_corners ) ppm_outs.append(UpperCamelCase__ ) return ppm_outs class lowercase ( nn.Module ): def __init__( self : Union[str, Any] , _UpperCamelCase : Dict , _UpperCamelCase : Tuple ) -> Optional[int]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = config SCREAMING_SNAKE_CASE = config.pool_scales # e.g. (1, 2, 3, 6) SCREAMING_SNAKE_CASE = in_channels SCREAMING_SNAKE_CASE = config.hidden_size SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module SCREAMING_SNAKE_CASE = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) SCREAMING_SNAKE_CASE = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module SCREAMING_SNAKE_CASE = nn.ModuleList() SCREAMING_SNAKE_CASE = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer SCREAMING_SNAKE_CASE = UperNetConvModule(UpperCamelCase__ , self.channels , kernel_size=1 ) SCREAMING_SNAKE_CASE = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(UpperCamelCase__ ) self.fpn_convs.append(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' self.apply(self._init_weights ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Tuple ) -> Dict: '''simple docstring''' if isinstance(UpperCamelCase__ , 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 __snake_case( self : Any , _UpperCamelCase : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = inputs[-1] SCREAMING_SNAKE_CASE = [x] psp_outs.extend(self.psp_modules(UpperCamelCase__ ) ) SCREAMING_SNAKE_CASE = torch.cat(UpperCamelCase__ , dim=1 ) SCREAMING_SNAKE_CASE = self.bottleneck(UpperCamelCase__ ) return output def __snake_case( self : List[Any] , _UpperCamelCase : torch.Tensor ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(UpperCamelCase__ ) ) # build top-down path SCREAMING_SNAKE_CASE = len(UpperCamelCase__ ) for i in range(used_backbone_levels - 1 , 0 , -1 ): SCREAMING_SNAKE_CASE = laterals[i - 1].shape[2:] SCREAMING_SNAKE_CASE = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=UpperCamelCase__ , mode="bilinear" , align_corners=self.align_corners ) # build outputs SCREAMING_SNAKE_CASE = [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 ): SCREAMING_SNAKE_CASE = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode="bilinear" , align_corners=self.align_corners ) SCREAMING_SNAKE_CASE = torch.cat(UpperCamelCase__ , dim=1 ) SCREAMING_SNAKE_CASE = self.fpn_bottleneck(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = self.classifier(UpperCamelCase__ ) return output class lowercase ( nn.Module ): def __init__( self : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : int = 2 , _UpperCamelCase : int = 3 , _UpperCamelCase : Union[int, Tuple[int, int]] = 1 ) -> Optional[int]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = config SCREAMING_SNAKE_CASE = config.auxiliary_in_channels SCREAMING_SNAKE_CASE = config.auxiliary_channels SCREAMING_SNAKE_CASE = config.auxiliary_num_convs SCREAMING_SNAKE_CASE = config.auxiliary_concat_input SCREAMING_SNAKE_CASE = in_index SCREAMING_SNAKE_CASE = (kernel_size // 2) * dilation SCREAMING_SNAKE_CASE = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=UpperCamelCase__ , padding=UpperCamelCase__ , dilation=UpperCamelCase__ ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=UpperCamelCase__ , padding=UpperCamelCase__ , dilation=UpperCamelCase__ ) ) if self.num_convs == 0: SCREAMING_SNAKE_CASE = nn.Identity() else: SCREAMING_SNAKE_CASE = nn.Sequential(*UpperCamelCase__ ) if self.concat_input: SCREAMING_SNAKE_CASE = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=UpperCamelCase__ , padding=kernel_size // 2 ) SCREAMING_SNAKE_CASE = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' self.apply(self._init_weights ) def __snake_case( self : Optional[Any] , _UpperCamelCase : Tuple ) -> Any: '''simple docstring''' if isinstance(UpperCamelCase__ , 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 __snake_case( self : Dict , _UpperCamelCase : torch.Tensor ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = encoder_hidden_states[self.in_index] SCREAMING_SNAKE_CASE = self.convs(UpperCamelCase__ ) if self.concat_input: SCREAMING_SNAKE_CASE = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) SCREAMING_SNAKE_CASE = self.classifier(UpperCamelCase__ ) return output class lowercase ( UpperCamelCase_ ): lowercase__ : Optional[Any] = UperNetConfig lowercase__ : Optional[Any] = """pixel_values""" lowercase__ : int = True def __snake_case( self : Optional[int] , _UpperCamelCase : Dict ) -> int: '''simple docstring''' if isinstance(UpperCamelCase__ , UpperCamelCase__ ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def __snake_case( self : List[Any] , _UpperCamelCase : Any , _UpperCamelCase : int=False ) -> Tuple: '''simple docstring''' if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE = value _lowerCamelCase : str = 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 : int = 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.""" , UpperCamelCase_ , ) class lowercase ( UpperCamelCase_ ): def __init__( self : Any , _UpperCamelCase : List[str] ) -> int: '''simple docstring''' super().__init__(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) SCREAMING_SNAKE_CASE = UperNetHead(UpperCamelCase__ , in_channels=self.backbone.channels ) SCREAMING_SNAKE_CASE = UperNetFCNHead(UpperCamelCase__ ) 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=UpperCamelCase__ , config_class=_CONFIG_FOR_DOC ) def __snake_case( self : List[str] , _UpperCamelCase : Optional[torch.Tensor] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[torch.Tensor] = None , _UpperCamelCase : Optional[bool] = None , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE = output_attentions if output_attentions is not None else self.config.output_attentions SCREAMING_SNAKE_CASE = self.backbone.forward_with_filtered_kwargs( UpperCamelCase__ , output_hidden_states=UpperCamelCase__ , output_attentions=UpperCamelCase__ ) SCREAMING_SNAKE_CASE = outputs.feature_maps SCREAMING_SNAKE_CASE = self.decode_head(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = nn.functional.interpolate(UpperCamelCase__ , size=pixel_values.shape[2:] , mode="bilinear" , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE = None if self.auxiliary_head is not None: SCREAMING_SNAKE_CASE = self.auxiliary_head(UpperCamelCase__ ) SCREAMING_SNAKE_CASE = nn.functional.interpolate( UpperCamelCase__ , size=pixel_values.shape[2:] , mode="bilinear" , align_corners=UpperCamelCase__ ) SCREAMING_SNAKE_CASE = 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 SCREAMING_SNAKE_CASE = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) SCREAMING_SNAKE_CASE = loss_fct(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE = loss_fct(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: SCREAMING_SNAKE_CASE = (logits,) + outputs[1:] else: SCREAMING_SNAKE_CASE = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=UpperCamelCase__ , logits=UpperCamelCase__ , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
712
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
0
'''simple docstring''' from typing import Any, Callable, Dict, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker _lowerCamelCase : str = '''CompVis/stable-diffusion-v1-1''' _lowerCamelCase : Any = '''CompVis/stable-diffusion-v1-2''' _lowerCamelCase : Union[str, Any] = '''CompVis/stable-diffusion-v1-3''' _lowerCamelCase : List[str] = '''CompVis/stable-diffusion-v1-4''' class lowercase ( a ): def __init__( self : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] = True , ) -> Any: '''simple docstring''' super()._init_() SCREAMING_SNAKE_CASE = StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE = StableDiffusionPipeline( vae=_SCREAMING_SNAKE_CASE , text_encoder=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , unet=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , safety_checker=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE , requires_safety_checker=_SCREAMING_SNAKE_CASE , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __snake_case( self : Optional[int] ) -> Dict[str, Any]: '''simple docstring''' return {k: getattr(self , _SCREAMING_SNAKE_CASE ) for k in self.config.keys() if not k.startswith("_" )} def __snake_case( self : List[str] , _UpperCamelCase : List[str] = "auto" ) -> Optional[Any]: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory SCREAMING_SNAKE_CASE = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(_SCREAMING_SNAKE_CASE ) def __snake_case( self : Any ) -> Tuple: '''simple docstring''' self.enable_attention_slicing(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def __snake_case( self : str , _UpperCamelCase : Any , _UpperCamelCase : Dict = 512 , _UpperCamelCase : Tuple = 512 , _UpperCamelCase : List[Any] = 50 , _UpperCamelCase : Optional[Any] = 7.5 , _UpperCamelCase : Tuple = None , _UpperCamelCase : Tuple = 1 , _UpperCamelCase : Tuple = 0.0 , _UpperCamelCase : List[Any] = None , _UpperCamelCase : Optional[Any] = None , _UpperCamelCase : Optional[Any] = "pil" , _UpperCamelCase : Tuple = True , _UpperCamelCase : List[str] = None , _UpperCamelCase : str = 1 , **_UpperCamelCase : Optional[int] , ) -> List[Any]: '''simple docstring''' return self.pipea( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @torch.no_grad() def __snake_case( self : Tuple , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] = 512 , _UpperCamelCase : Optional[int] = 512 , _UpperCamelCase : List[Any] = 50 , _UpperCamelCase : List[str] = 7.5 , _UpperCamelCase : List[Any] = None , _UpperCamelCase : str = 1 , _UpperCamelCase : Optional[int] = 0.0 , _UpperCamelCase : int = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : List[str] = "pil" , _UpperCamelCase : Union[str, Any] = True , _UpperCamelCase : List[str] = None , _UpperCamelCase : Any = 1 , **_UpperCamelCase : int , ) -> List[Any]: '''simple docstring''' return self.pipea( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @torch.no_grad() def __snake_case( self : Tuple , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] = 512 , _UpperCamelCase : Tuple = 512 , _UpperCamelCase : Any = 50 , _UpperCamelCase : List[str] = 7.5 , _UpperCamelCase : List[str] = None , _UpperCamelCase : Dict = 1 , _UpperCamelCase : str = 0.0 , _UpperCamelCase : Optional[Any] = None , _UpperCamelCase : Any = None , _UpperCamelCase : Tuple = "pil" , _UpperCamelCase : Tuple = True , _UpperCamelCase : Dict = None , _UpperCamelCase : Union[str, Any] = 1 , **_UpperCamelCase : List[str] , ) -> Dict: '''simple docstring''' return self.pipea( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @torch.no_grad() def __snake_case( self : str , _UpperCamelCase : int , _UpperCamelCase : Dict = 512 , _UpperCamelCase : List[Any] = 512 , _UpperCamelCase : List[str] = 50 , _UpperCamelCase : Any = 7.5 , _UpperCamelCase : int = None , _UpperCamelCase : Dict = 1 , _UpperCamelCase : Tuple = 0.0 , _UpperCamelCase : int = None , _UpperCamelCase : Any = None , _UpperCamelCase : Optional[Any] = "pil" , _UpperCamelCase : Optional[int] = True , _UpperCamelCase : Dict = None , _UpperCamelCase : List[Any] = 1 , **_UpperCamelCase : Any , ) -> str: '''simple docstring''' return self.pipea( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @torch.no_grad() def __snake_case( self : str , _UpperCamelCase : Optional[Any] , _UpperCamelCase : str = 512 , _UpperCamelCase : Any = 512 , _UpperCamelCase : str = 50 , _UpperCamelCase : Tuple = 7.5 , _UpperCamelCase : Optional[Any] = None , _UpperCamelCase : Union[str, Any] = 1 , _UpperCamelCase : int = 0.0 , _UpperCamelCase : Dict = None , _UpperCamelCase : Optional[Any] = None , _UpperCamelCase : Dict = "pil" , _UpperCamelCase : Optional[Any] = True , _UpperCamelCase : Tuple = None , _UpperCamelCase : Union[str, Any] = 1 , **_UpperCamelCase : Dict , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = "cuda" if torch.cuda.is_available() else "cpu" self.to(_SCREAMING_SNAKE_CASE ) # Checks if the height and width are divisible by 8 or not if height % 8 != 0 or width % 8 != 0: raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}." ) # Get first result from Stable Diffusion Checkpoint v1.1 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) # Get first result from Stable Diffusion Checkpoint v1.2 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) # Get first result from Stable Diffusion Checkpoint v1.3 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) # Get first result from Stable Diffusion Checkpoint v1.4 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_SCREAMING_SNAKE_CASE , height=_SCREAMING_SNAKE_CASE , width=_SCREAMING_SNAKE_CASE , num_inference_steps=_SCREAMING_SNAKE_CASE , guidance_scale=_SCREAMING_SNAKE_CASE , negative_prompt=_SCREAMING_SNAKE_CASE , num_images_per_prompt=_SCREAMING_SNAKE_CASE , eta=_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , latents=_SCREAMING_SNAKE_CASE , output_type=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , callback=_SCREAMING_SNAKE_CASE , callback_steps=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]] )
713
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels 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 = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
0
import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : List[Any] = logging.get_logger(__name__) _lowerCamelCase : List[str] = { '''facebook/encodec_24khz''': '''https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json''', '''facebook/encodec_48khz''': '''https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json''', } class lowercase ( UpperCAmelCase__ ): lowercase__ : Union[str, Any] = 'encodec' def __init__( self : Optional[Any] , _UpperCamelCase : int=[1.5, 3.0, 6.0, 12.0, 24.0] , _UpperCamelCase : Optional[int]=24_000 , _UpperCamelCase : int=1 , _UpperCamelCase : Tuple=False , _UpperCamelCase : Optional[int]=None , _UpperCamelCase : str=None , _UpperCamelCase : Optional[int]=128 , _UpperCamelCase : Optional[Any]=32 , _UpperCamelCase : str=1 , _UpperCamelCase : Optional[Any]=[8, 5, 4, 2] , _UpperCamelCase : Tuple="weight_norm" , _UpperCamelCase : int=7 , _UpperCamelCase : Tuple=7 , _UpperCamelCase : str=3 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Union[str, Any]="reflect" , _UpperCamelCase : List[str]=2 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : Any=1.0 , _UpperCamelCase : Tuple=1_024 , _UpperCamelCase : Optional[int]=None , _UpperCamelCase : List[Any]=True , **_UpperCamelCase : Optional[int] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = target_bandwidths SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = audio_channels SCREAMING_SNAKE_CASE = normalize SCREAMING_SNAKE_CASE = chunk_length_s SCREAMING_SNAKE_CASE = overlap SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_filters SCREAMING_SNAKE_CASE = num_residual_layers SCREAMING_SNAKE_CASE = upsampling_ratios SCREAMING_SNAKE_CASE = norm_type SCREAMING_SNAKE_CASE = kernel_size SCREAMING_SNAKE_CASE = last_kernel_size SCREAMING_SNAKE_CASE = residual_kernel_size SCREAMING_SNAKE_CASE = dilation_growth_rate SCREAMING_SNAKE_CASE = use_causal_conv SCREAMING_SNAKE_CASE = pad_mode SCREAMING_SNAKE_CASE = compress SCREAMING_SNAKE_CASE = num_lstm_layers SCREAMING_SNAKE_CASE = trim_right_ratio SCREAMING_SNAKE_CASE = codebook_size SCREAMING_SNAKE_CASE = codebook_dim if codebook_dim is not None else hidden_size SCREAMING_SNAKE_CASE = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( F"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**_UpperCamelCase ) @property def __snake_case( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __snake_case( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' 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 ) ) @property def __snake_case( self : Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def __snake_case( self : Any ) -> int: '''simple docstring''' return int(1_000 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
714
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_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()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
0
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() _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : int=False ): SCREAMING_SNAKE_CASE = [] 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 = [(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 __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Any=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "deit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = 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 = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Any , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = dct.pop(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (): SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ) return im @torch.no_grad() def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = DeiTConfig() # all deit models have fine-tuned heads SCREAMING_SNAKE_CASE = False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , repo_type="dataset" ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(SCREAMING_SNAKE_CASE_ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = int(deit_name[-6:-4] ) SCREAMING_SNAKE_CASE = int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith("tiny" ): SCREAMING_SNAKE_CASE = 1_9_2 SCREAMING_SNAKE_CASE = 7_6_8 SCREAMING_SNAKE_CASE = 1_2 SCREAMING_SNAKE_CASE = 3 elif deit_name[9:].startswith("small" ): SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 1_2 SCREAMING_SNAKE_CASE = 6 if deit_name[9:].startswith("base" ): pass elif deit_name[4:].startswith("large" ): SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 # load original model from timm SCREAMING_SNAKE_CASE = timm.create_model(SCREAMING_SNAKE_CASE_ , pretrained=SCREAMING_SNAKE_CASE_ ) timm_model.eval() # load state_dict of original model, remove and rename some keys SCREAMING_SNAKE_CASE = timm_model.state_dict() SCREAMING_SNAKE_CASE = create_rename_keys(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) read_in_q_k_v(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # load HuggingFace model SCREAMING_SNAKE_CASE = DeiTForImageClassificationWithTeacher(SCREAMING_SNAKE_CASE_ ).eval() model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # Check outputs on an image, prepared by DeiTImageProcessor SCREAMING_SNAKE_CASE = 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 = DeiTImageProcessor(size=SCREAMING_SNAKE_CASE_ , crop_size=config.image_size ) SCREAMING_SNAKE_CASE = image_processor(images=prepare_img() , return_tensors="pt" ) SCREAMING_SNAKE_CASE = encoding["pixel_values"] SCREAMING_SNAKE_CASE = model(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = timm_model(SCREAMING_SNAKE_CASE_ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(SCREAMING_SNAKE_CASE_ , outputs.logits , atol=1e-3 ) Path(SCREAMING_SNAKE_CASE_ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE_ ) print(F"Saving model {deit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": _lowerCamelCase : Any = 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.''' ) _lowerCamelCase : List[str] = parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
715
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
0
_lowerCamelCase : dict[str, float] = { "joule": 1.0, "kilojoule": 10_00, "megajoule": 1_00_00_00, "gigajoule": 10_00_00_00_00, "wattsecond": 1.0, "watthour": 36_00, "kilowatthour": 3_60_00_00, "newtonmeter": 1.0, "calorie_nutr": 4_186.8, "kilocalorie_nutr": 4_18_68_00.00, "electronvolt": 1.6_0_2_1_7_6_6_3_4e-1_9, "britishthermalunit_it": 1_055.05_585, "footpound": 1.355_818, } def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict ): if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: SCREAMING_SNAKE_CASE = ( F"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" F"Valid values are: {', '.join(_lowerCAmelCase )}" ) raise ValueError(_lowerCAmelCase ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
716
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
0
from __future__ import annotations import requests def __lowerCamelCase (UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = F"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty" return requests.get(UpperCAmelCase__ ).json() def __lowerCamelCase (UpperCAmelCase__ : int = 1_0 ): SCREAMING_SNAKE_CASE = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" SCREAMING_SNAKE_CASE = requests.get(UpperCAmelCase__ ).json()[:max_stories] return [get_hackernews_story(UpperCAmelCase__ ) for story_id in story_ids] def __lowerCamelCase (UpperCAmelCase__ : int = 1_0 ): SCREAMING_SNAKE_CASE = hackernews_top_stories(UpperCAmelCase__ ) return "\n".join("* [{title}]({url})".format(**UpperCAmelCase__ ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
717
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # 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" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> 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()
647
0
import argparse import os import re import packaging.version _lowerCamelCase : Union[str, Any] = '''examples/''' _lowerCamelCase : Dict = { '''examples''': (re.compile(r'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''), '''init''': (re.compile(r'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''), '''setup''': (re.compile(r'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), r'''\1version="VERSION",'''), '''doc''': (re.compile(r'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''), } _lowerCamelCase : Optional[int] = { '''init''': '''src/transformers/__init__.py''', '''setup''': '''setup.py''', } _lowerCamelCase : List[Any] = '''README.md''' def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Optional[Any] ): with open(__lowerCAmelCase , "r" , encoding="utf-8" , newline="\n" ) as f: SCREAMING_SNAKE_CASE = f.read() SCREAMING_SNAKE_CASE = REPLACE_PATTERNS[pattern] SCREAMING_SNAKE_CASE = replace.replace("VERSION" , __lowerCAmelCase ) SCREAMING_SNAKE_CASE = re_pattern.sub(__lowerCAmelCase , __lowerCAmelCase ) with open(__lowerCAmelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(__lowerCAmelCase ) def __lowerCamelCase (UpperCAmelCase__ : Dict ): for folder, directories, fnames in os.walk(__lowerCAmelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("research_projects" ) if "legacy" in directories: directories.remove("legacy" ) for fname in fnames: if fname.endswith(".py" ): update_version_in_file(os.path.join(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase , pattern="examples" ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : List[str]=False ): for pattern, fname in REPLACE_FILES.items(): update_version_in_file(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if not patch: update_version_in_examples(__lowerCAmelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = "🤗 Transformers currently provides the following architectures" SCREAMING_SNAKE_CASE = "1. Want to contribute a new model?" with open(__lowerCAmelCase , "r" , encoding="utf-8" , newline="\n" ) as f: SCREAMING_SNAKE_CASE = f.readlines() # Find the start of the list. SCREAMING_SNAKE_CASE = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 SCREAMING_SNAKE_CASE = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): SCREAMING_SNAKE_CASE = lines[index].replace( "https://huggingface.co/docs/transformers/main/model_doc" , "https://huggingface.co/docs/transformers/model_doc" , ) index += 1 with open(__lowerCAmelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(__lowerCAmelCase ) def __lowerCamelCase (): with open(REPLACE_FILES["init"] , "r" ) as f: SCREAMING_SNAKE_CASE = f.read() SCREAMING_SNAKE_CASE = REPLACE_PATTERNS["init"][0].search(__lowerCAmelCase ).groups()[0] return packaging.version.parse(__lowerCAmelCase ) def __lowerCamelCase (UpperCAmelCase__ : Optional[int]=False ): SCREAMING_SNAKE_CASE = get_version() if patch and default_version.is_devrelease: raise ValueError("Can't create a patch version from the dev branch, checkout a released version!" ) if default_version.is_devrelease: SCREAMING_SNAKE_CASE = default_version.base_version elif patch: SCREAMING_SNAKE_CASE = F"{default_version.major}.{default_version.minor}.{default_version.micro + 1}" else: SCREAMING_SNAKE_CASE = F"{default_version.major}.{default_version.minor + 1}.0" # Now let's ask nicely if that's the right one. SCREAMING_SNAKE_CASE = input(F"Which version are you releasing? [{default_version}]" ) if len(__lowerCAmelCase ) == 0: SCREAMING_SNAKE_CASE = default_version print(F"Updating version to {version}." ) global_version_update(__lowerCAmelCase , patch=__lowerCAmelCase ) if not patch: print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() def __lowerCamelCase (): SCREAMING_SNAKE_CASE = get_version() SCREAMING_SNAKE_CASE = F"{current_version.major}.{current_version.minor + 1}.0.dev0" SCREAMING_SNAKE_CASE = current_version.base_version # Check with the user we got that right. SCREAMING_SNAKE_CASE = input(F"Which version are we developing now? [{dev_version}]" ) if len(__lowerCAmelCase ) == 0: SCREAMING_SNAKE_CASE = dev_version print(F"Updating version to {version}." ) global_version_update(__lowerCAmelCase ) print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''') parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''') _lowerCamelCase : List[str] = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('''Nothing to do after a patch :-)''') else: post_release_work()
718
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : 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(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
0
import os import string import sys _lowerCamelCase : Optional[Any] = 1 << 8 _lowerCamelCase : List[Any] = { '''tab''': ord('''\t'''), '''newline''': ord('''\r'''), '''esc''': 27, '''up''': 65 + ARROW_KEY_FLAG, '''down''': 66 + ARROW_KEY_FLAG, '''right''': 67 + ARROW_KEY_FLAG, '''left''': 68 + ARROW_KEY_FLAG, '''mod_int''': 91, '''undefined''': sys.maxsize, '''interrupt''': 3, '''insert''': 50, '''delete''': 51, '''pg_up''': 53, '''pg_down''': 54, } _lowerCamelCase : Union[str, Any] = KEYMAP['''up'''] _lowerCamelCase : List[Any] = KEYMAP['''left'''] if sys.platform == "win32": _lowerCamelCase : Optional[int] = [] _lowerCamelCase : List[str] = { b'''\xe0H''': KEYMAP['''up'''] - ARROW_KEY_FLAG, b'''\x00H''': KEYMAP['''up'''] - ARROW_KEY_FLAG, b'''\xe0P''': KEYMAP['''down'''] - ARROW_KEY_FLAG, b'''\x00P''': KEYMAP['''down'''] - ARROW_KEY_FLAG, b'''\xe0M''': KEYMAP['''right'''] - ARROW_KEY_FLAG, b'''\x00M''': KEYMAP['''right'''] - ARROW_KEY_FLAG, b'''\xe0K''': KEYMAP['''left'''] - ARROW_KEY_FLAG, b'''\x00K''': KEYMAP['''left'''] - ARROW_KEY_FLAG, } for i in range(10): _lowerCamelCase : Optional[int] = ord(str(i)) def __lowerCamelCase (): '''simple docstring''' if os.name == "nt": import msvcrt SCREAMING_SNAKE_CASE = "mbcs" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(lowercase__ ) == 0: # Read the keystroke SCREAMING_SNAKE_CASE = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): SCREAMING_SNAKE_CASE = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: SCREAMING_SNAKE_CASE = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["mod_int"] ) ) WIN_CH_BUFFER.append(lowercase__ ) if ord(lowercase__ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(1_2_6 ) ) SCREAMING_SNAKE_CASE = chr(KEYMAP["esc"] ) except KeyError: SCREAMING_SNAKE_CASE = cha[1] else: SCREAMING_SNAKE_CASE = ch.decode(lowercase__ ) else: SCREAMING_SNAKE_CASE = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty SCREAMING_SNAKE_CASE = sys.stdin.fileno() SCREAMING_SNAKE_CASE = termios.tcgetattr(lowercase__ ) try: tty.setraw(lowercase__ ) SCREAMING_SNAKE_CASE = sys.stdin.read(1 ) finally: termios.tcsetattr(lowercase__ , termios.TCSADRAIN , lowercase__ ) return ch def __lowerCamelCase (): '''simple docstring''' SCREAMING_SNAKE_CASE = get_raw_chars() if ord(lowercase__ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(lowercase__ ) == KEYMAP["esc"]: SCREAMING_SNAKE_CASE = get_raw_chars() if ord(lowercase__ ) == KEYMAP["mod_int"]: SCREAMING_SNAKE_CASE = get_raw_chars() if ord(lowercase__ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(lowercase__ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(lowercase__ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
719
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
0
import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger _lowerCamelCase : Union[str, Any] = get_logger(__name__) class lowercase : def __init__( self : Dict , _UpperCamelCase : Optional[str] = None ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ( os.path.join(__a , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) SCREAMING_SNAKE_CASE = Extractor def __snake_case( self : Optional[int] , _UpperCamelCase : str ) -> str: '''simple docstring''' from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" SCREAMING_SNAKE_CASE = os.path.abspath(__a ) return os.path.join(self.extract_dir , hash_url_to_filename(__a ) ) def __snake_case( self : str , _UpperCamelCase : str , _UpperCamelCase : bool ) -> bool: '''simple docstring''' return force_extract or ( not os.path.isfile(__a ) and not (os.path.isdir(__a ) and os.listdir(__a )) ) def __snake_case( self : str , _UpperCamelCase : str , _UpperCamelCase : bool = False ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.extractor.infer_extractor_format(__a ) if not extractor_format: return input_path SCREAMING_SNAKE_CASE = self._get_output_path(__a ) if self._do_extract(__a , __a ): self.extractor.extract(__a , __a , __a ) return output_path class lowercase ( __lowercase ): @classmethod @abstractmethod def __snake_case( cls : Union[str, Any] , _UpperCamelCase : Union[Path, str] , **_UpperCamelCase : Union[str, Any] ) -> bool: '''simple docstring''' ... @staticmethod @abstractmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' ... class lowercase ( __lowercase , __lowercase ): lowercase__ : List[bytes] = [] @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : int ) -> List[Any]: '''simple docstring''' with open(__a , "rb" ) as f: return f.read(__a ) @classmethod def __snake_case( cls : Dict , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bytes = b"" ) -> bool: '''simple docstring''' if not magic_number: SCREAMING_SNAKE_CASE = max(len(__a ) for cls_magic_number in cls.magic_numbers ) try: SCREAMING_SNAKE_CASE = cls.read_magic_number(__a , __a ) except OSError: return False return any(magic_number.startswith(__a ) for cls_magic_number in cls.magic_numbers ) class lowercase ( __lowercase ): @classmethod def __snake_case( cls : Any , _UpperCamelCase : Union[Path, str] , **_UpperCamelCase : Dict ) -> bool: '''simple docstring''' return tarfile.is_tarfile(__a ) @staticmethod def __snake_case( _UpperCamelCase : Any , _UpperCamelCase : Any ) -> int: '''simple docstring''' def resolved(_UpperCamelCase : str ) -> str: return os.path.realpath(os.path.abspath(__a ) ) def badpath(_UpperCamelCase : str , _UpperCamelCase : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(__a , __a ) ).startswith(__a ) def badlink(_UpperCamelCase : int , _UpperCamelCase : str ) -> bool: # Links are interpreted relative to the directory containing the link SCREAMING_SNAKE_CASE = resolved(os.path.join(__a , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=__a ) SCREAMING_SNAKE_CASE = resolved(__a ) for finfo in members: if badpath(finfo.name , __a ): logger.error(F"Extraction of {finfo.name} is blocked (illegal path)" ) elif finfo.issym() and badlink(__a , __a ): logger.error(F"Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}" ) elif finfo.islnk() and badlink(__a , __a ): logger.error(F"Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}" ) else: yield finfo @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' os.makedirs(__a , exist_ok=__a ) SCREAMING_SNAKE_CASE = tarfile.open(__a ) tar_file.extractall(__a , members=TarExtractor.safemembers(__a , __a ) ) tar_file.close() class lowercase ( __lowercase ): lowercase__ : Tuple = [B'''\x1F\x8B'''] @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' with gzip.open(__a , "rb" ) as gzip_file: with open(__a , "wb" ) as extracted_file: shutil.copyfileobj(__a , __a ) class lowercase ( __lowercase ): lowercase__ : Any = [ B'''PK\x03\x04''', B'''PK\x05\x06''', # empty archive B'''PK\x07\x08''', # spanned archive ] @classmethod def __snake_case( cls : Any , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bytes = b"" ) -> bool: '''simple docstring''' if super().is_extractable(__a , magic_number=__a ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(__a , "rb" ) as fp: SCREAMING_SNAKE_CASE = _EndRecData(__a ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: SCREAMING_SNAKE_CASE = fp.read(__a ) # CD is where we expect it to be if len(__a ) == sizeCentralDir: SCREAMING_SNAKE_CASE = struct.unpack(__a , __a ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' os.makedirs(__a , exist_ok=__a ) with zipfile.ZipFile(__a , "r" ) as zip_file: zip_file.extractall(__a ) zip_file.close() class lowercase ( __lowercase ): lowercase__ : Dict = [B'''\xFD\x37\x7A\x58\x5A\x00'''] @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' with lzma.open(__a ) as compressed_file: with open(__a , "wb" ) as extracted_file: shutil.copyfileobj(__a , __a ) class lowercase ( __lowercase ): lowercase__ : int = [B'''Rar!\x1a\x07\x00''', B'''Rar!\x1a\x07\x01\x00'''] # RAR_ID # RAR5_ID @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' if not config.RARFILE_AVAILABLE: raise ImportError("Please pip install rarfile" ) import rarfile os.makedirs(__a , exist_ok=__a ) SCREAMING_SNAKE_CASE = rarfile.RarFile(__a ) rf.extractall(__a ) rf.close() class lowercase ( __lowercase ): lowercase__ : Union[str, Any] = [B'''\x28\xb5\x2F\xFD'''] @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' if not config.ZSTANDARD_AVAILABLE: raise ImportError("Please pip install zstandard" ) import zstandard as zstd SCREAMING_SNAKE_CASE = zstd.ZstdDecompressor() with open(__a , "rb" ) as ifh, open(__a , "wb" ) as ofh: dctx.copy_stream(__a , __a ) class lowercase ( __lowercase ): lowercase__ : Optional[int] = [B'''\x42\x5A\x68'''] @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' with bza.open(__a , "rb" ) as compressed_file: with open(__a , "wb" ) as extracted_file: shutil.copyfileobj(__a , __a ) class lowercase ( __lowercase ): lowercase__ : str = [B'''\x37\x7A\xBC\xAF\x27\x1C'''] @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' if not config.PY7ZR_AVAILABLE: raise ImportError("Please pip install py7zr" ) import pyazr os.makedirs(__a , exist_ok=__a ) with pyazr.SevenZipFile(__a , "r" ) as archive: archive.extractall(__a ) class lowercase ( __lowercase ): lowercase__ : Optional[Any] = [B'''\x04\x22\x4D\x18'''] @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) -> None: '''simple docstring''' if not config.LZ4_AVAILABLE: raise ImportError("Please pip install lz4" ) import lza.frame with lza.frame.open(__a , "rb" ) as compressed_file: with open(__a , "wb" ) as extracted_file: shutil.copyfileobj(__a , __a ) class lowercase : lowercase__ : Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def __snake_case( cls : Optional[int] ) -> List[str]: '''simple docstring''' return max( len(__a ) for extractor in cls.extractors.values() if issubclass(__a , __a ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def __snake_case( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : int ) -> List[Any]: '''simple docstring''' try: return MagicNumberBaseExtractor.read_magic_number(__a , magic_number_length=__a ) except OSError: return b"" @classmethod def __snake_case( cls : Optional[int] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bool = False ) -> bool: '''simple docstring''' warnings.warn( "Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'infer_extractor_format' instead." , category=__a , ) SCREAMING_SNAKE_CASE = cls.infer_extractor_format(__a ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def __snake_case( cls : Tuple , _UpperCamelCase : Union[Path, str] ) -> str: # <Added version="2.4.0"/> '''simple docstring''' SCREAMING_SNAKE_CASE = cls._get_magic_number_max_length() SCREAMING_SNAKE_CASE = cls._read_magic_number(__a , __a ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(__a , magic_number=__a ): return extractor_format @classmethod def __snake_case( cls : Any , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[BaseExtractor] = "deprecated" , ) -> None: '''simple docstring''' os.makedirs(os.path.dirname(__a ) , exist_ok=__a ) # Prevent parallel extractions SCREAMING_SNAKE_CASE = str(Path(__a ).with_suffix(".lock" ) ) with FileLock(__a ): shutil.rmtree(__a , ignore_errors=__a ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(__a , __a ): # passed as positional arg warnings.warn( "Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'extractor_format' instead." , category=__a , ) SCREAMING_SNAKE_CASE = extractor if extractor != """deprecated""" else extractor_format else: SCREAMING_SNAKE_CASE = cls.extractors[extractor_format] return extractor.extract(__a , __a ) else: warnings.warn( "Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an " "exception in 3.0.0." , category=__a , ) for extractor in cls.extractors.values(): if extractor.is_extractable(__a ): return extractor.extract(__a , __a )
720
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model 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__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
0
import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def __lowerCamelCase(UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "decoder.output_projection.weight", "_float_tensor", "encoder.embed_positions._float_tensor", "decoder.embed_positions._float_tensor", ] for k in ignore_keys: state_dict.pop(_UpperCamelCase , _UpperCamelCase ) def __lowerCamelCase(UpperCAmelCase__ : List[Any] ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = emb.weight.shape SCREAMING_SNAKE_CASE = nn.Linear(_UpperCamelCase , _UpperCamelCase , bias=_UpperCamelCase ) SCREAMING_SNAKE_CASE = emb.weight.data return lin_layer def __lowerCamelCase(UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Dict=None ): SCREAMING_SNAKE_CASE = {} for old_key in state_dict.keys(): SCREAMING_SNAKE_CASE = old_key if "moe_layer.experts." in key: if expert_idx is not None: SCREAMING_SNAKE_CASE = key.replace("moe_layer.experts.0" , F"ffn.experts.expert_{expert_idx}" ) else: SCREAMING_SNAKE_CASE = key.replace("moe_layer.experts." , "ffn.experts.expert_" ) if "gate" in key: SCREAMING_SNAKE_CASE = key.replace(".moe_layer.gate.wg" , ".ffn.router.classifier" ) if "fc2" and "experts" not in key: SCREAMING_SNAKE_CASE = key.replace(".fc2." , ".ffn.fc2." ) if "fc1" and "experts" not in key: SCREAMING_SNAKE_CASE = key.replace(".fc1." , ".ffn.fc1." ) if ".encoder_attn." in key: SCREAMING_SNAKE_CASE = key.replace(".encoder_attn." , ".cross_attention." ) if "encoder_attn_layer_norm" in key: SCREAMING_SNAKE_CASE = key.replace("encoder_attn_layer_norm" , "cross_attention_layer_norm" ) if "final_layer_norm" in key: SCREAMING_SNAKE_CASE = key.replace("final_layer_norm" , "ff_layer_norm" ) SCREAMING_SNAKE_CASE = state_dict[old_key] return new_dict def __lowerCamelCase(UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : str , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : str = WEIGHTS_NAME ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) for expert in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = switch_checkpoint_path + F"-rank-{expert}.pt" if os.path.isfile(_UpperCamelCase ): SCREAMING_SNAKE_CASE = torch.load(_UpperCamelCase )["model"] remove_ignore_keys_(_UpperCamelCase ) SCREAMING_SNAKE_CASE = rename_fairseq_keys(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , weights_name.replace(".bin" , F"-{len(_UpperCamelCase )+1:05d}-of-???.bin" ) ) torch.save(_UpperCamelCase , _UpperCamelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_UpperCamelCase )[0]].dtype ) # Add the last block SCREAMING_SNAKE_CASE = os.path.join(_UpperCamelCase , weights_name.replace(".bin" , F"-{len(_UpperCamelCase )+1:05d}-of-???.bin" ) ) SCREAMING_SNAKE_CASE = torch.load(switch_checkpoint_path + "-shared.pt" )["model"] remove_ignore_keys_(_UpperCamelCase ) SCREAMING_SNAKE_CASE = rename_fairseq_keys(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = shared_weights["decoder.embed_tokens.weight"] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_UpperCamelCase ) == 1: SCREAMING_SNAKE_CASE = os.path.join(_UpperCamelCase , _UpperCamelCase ) torch.save(_UpperCamelCase , _UpperCamelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_UpperCamelCase , _UpperCamelCase ) # Otherwise, let's build the index SCREAMING_SNAKE_CASE = {} for idx, shard in enumerate(_UpperCamelCase ): SCREAMING_SNAKE_CASE = weights_name.replace(".bin" , F"-{idx+1:05d}-of-{len(_UpperCamelCase ):05d}.bin" ) SCREAMING_SNAKE_CASE = os.path.join(_UpperCamelCase , weights_name.replace(".bin" , F"-{idx+1:05d}-of-???.bin" ) ) os.rename(_UpperCamelCase , os.path.join(_UpperCamelCase , _UpperCamelCase ) ) for key in shard: SCREAMING_SNAKE_CASE = shard_file # Add the metadata SCREAMING_SNAKE_CASE = {"total_size": total_size} SCREAMING_SNAKE_CASE = {"metadata": metadata, "weight_map": weight_map} with open(os.path.join(_UpperCamelCase , _UpperCamelCase ) , "w" , encoding="utf-8" ) as f: SCREAMING_SNAKE_CASE = json.dumps(_UpperCamelCase , indent=2 , sort_keys=_UpperCamelCase ) + "\n" f.write(_UpperCamelCase ) return metadata, index if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--nllb_moe_checkpoint_path''', default='''/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000''', type=str, required=False, help='''Path to a directory containing a folder per layer. Follows the original Google format.''', ) parser.add_argument('''--dtype''', default='''float32''', type=str, required=False, help='''dtype of the saved model''') parser.add_argument( '''--pytorch_dump_folder_path''', default='''/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b''', type=str, required=False, help='''Path to the output pytorch model.''', ) _lowerCamelCase : Dict = parser.parse_args() _lowerCamelCase : Union[str, Any] = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 1_28, args.dtype, ) _lowerCamelCase : Tuple = NllbMoeConfig.from_pretrained( '''facebook/nllb-200-3.3B''', encoder_sparse_step=4, decoder_sparse_step=4, num_experts=1_28 ) config.save_pretrained(args.pytorch_dump_folder_path) _lowerCamelCase : List[Any] = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print('''Done''') model.save_pretrained(args.pytorch_dump_folder_path)
721
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = 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' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # 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 : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
0
import ast import os import re import shutil import tempfile import unittest from unittest import mock import torch from accelerate.test_utils.examples import compare_against_test from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow from accelerate.utils import write_basic_config # DataLoaders built from `test_samples/MRPC` for quick testing # Should mock `{script_name}.get_dataloaders` via: # @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders) _lowerCamelCase : Dict = [ '''cross_validation.py''', '''gradient_accumulation.py''', '''local_sgd.py''', '''multi_process_metrics.py''', '''memory.py''', '''automatic_gradient_accumulation.py''', '''fsdp_with_peak_mem_tracking.py''', '''deepspeed_with_config_support.py''', '''megatron_lm_gpt_pretraining.py''', ] class lowercase ( unittest.TestCase ): def __snake_case( self : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str , _UpperCamelCase : List[Any] = None , _UpperCamelCase : Tuple = None ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = os.path.abspath(os.path.join("examples" , "by_feature" ) ) SCREAMING_SNAKE_CASE = os.path.abspath("examples" ) for item in os.listdir(A__ ): if item not in EXCLUDE_EXAMPLES: SCREAMING_SNAKE_CASE = os.path.join(A__ , A__ ) if os.path.isfile(A__ ) and ".py" in item_path: with self.subTest( tested_script=A__ , feature_script=A__ , tested_section="main()" if parser_only else "training_function()" , ): SCREAMING_SNAKE_CASE = compare_against_test( os.path.join(A__ , A__ ) , A__ , A__ , A__ ) SCREAMING_SNAKE_CASE = "\n".join(A__ ) if special_strings is not None: for string in special_strings: SCREAMING_SNAKE_CASE = diff.replace(A__ , "" ) self.assertEqual(A__ , "" ) def __snake_case( self : Tuple ) -> Optional[int]: '''simple docstring''' self.one_complete_example("complete_nlp_example.py" , A__ ) self.one_complete_example("complete_nlp_example.py" , A__ ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = os.path.abspath(os.path.join("examples" , "cv_example.py" ) ) SCREAMING_SNAKE_CASE = [ " " * 16 + "{\n\n", " " * 20 + "\"accuracy\": eval_metric[\"accuracy\"],\n\n", " " * 20 + "\"f1\": eval_metric[\"f1\"],\n\n", " " * 20 + "\"train_loss\": total_loss.item() / len(train_dataloader),\n\n", " " * 20 + "\"epoch\": epoch,\n\n", " " * 16 + "},\n\n", " " * 16 + "step=epoch,\n", " " * 12, " " * 8 + "for step, batch in enumerate(active_dataloader):\n", ] self.one_complete_example("complete_cv_example.py" , A__ , A__ , A__ ) self.one_complete_example("complete_cv_example.py" , A__ , A__ , A__ ) @mock.patch.dict(os.environ , {"""TESTING_MOCKED_DATALOADERS""": """1"""} ) class lowercase ( __a ): lowercase__ : List[Any] = False @classmethod def __snake_case( cls : List[str] ) -> Union[str, Any]: '''simple docstring''' super().setUpClass() SCREAMING_SNAKE_CASE = tempfile.mkdtemp() SCREAMING_SNAKE_CASE = os.path.join(cls._tmpdir , "default_config.yml" ) write_basic_config(save_location=cls.configPath ) SCREAMING_SNAKE_CASE = ["accelerate", "launch", "--config_file", cls.configPath] @classmethod def __snake_case( cls : Optional[int] ) -> str: '''simple docstring''' super().tearDownClass() shutil.rmtree(cls._tmpdir ) def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = F"\n examples/by_feature/checkpointing.py\n --checkpointing_steps epoch\n --output_dir {self.tmpdir}\n ".split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "epoch_0" ) ) ) def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = F"\n examples/by_feature/checkpointing.py\n --checkpointing_steps 1\n --output_dir {self.tmpdir}\n ".split() SCREAMING_SNAKE_CASE = run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "step_2" ) ) ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = F"\n examples/by_feature/checkpointing.py\n --resume_from_checkpoint {os.path.join(self.tmpdir , 'epoch_0' )}\n ".split() SCREAMING_SNAKE_CASE = run_command(self._launch_args + testargs , return_stdout=A__ ) self.assertNotIn("epoch 0:" , A__ ) self.assertIn("epoch 1:" , A__ ) def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = F"\n examples/by_feature/checkpointing.py\n --resume_from_checkpoint {os.path.join(self.tmpdir , 'step_2' )}\n ".split() SCREAMING_SNAKE_CASE = run_command(self._launch_args + testargs , return_stdout=A__ ) if torch.cuda.is_available(): SCREAMING_SNAKE_CASE = torch.cuda.device_count() else: SCREAMING_SNAKE_CASE = 1 if num_processes > 1: self.assertNotIn("epoch 0:" , A__ ) self.assertIn("epoch 1:" , A__ ) else: self.assertIn("epoch 0:" , A__ ) self.assertIn("epoch 1:" , A__ ) @slow def __snake_case( self : Optional[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = "\n examples/by_feature/cross_validation.py\n --num_folds 2\n ".split() with mock.patch.dict(os.environ , {"TESTING_MOCKED_DATALOADERS": "0"} ): SCREAMING_SNAKE_CASE = run_command(self._launch_args + testargs , return_stdout=A__ ) SCREAMING_SNAKE_CASE = re.findall("({.+})" , A__ ) SCREAMING_SNAKE_CASE = [r for r in results if "accuracy" in r][-1] SCREAMING_SNAKE_CASE = ast.literal_eval(A__ ) self.assertGreaterEqual(results["accuracy"] , 0.7_5 ) def __snake_case( self : List[str] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["examples/by_feature/multi_process_metrics.py"] run_command(self._launch_args + testargs ) @require_trackers @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def __snake_case( self : Dict ) -> Tuple: '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdir: SCREAMING_SNAKE_CASE = F"\n examples/by_feature/tracking.py\n --with_tracking\n --project_dir {tmpdir}\n ".split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(A__ , "tracking" ) ) ) def __snake_case( self : int ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = ["examples/by_feature/gradient_accumulation.py"] run_command(self._launch_args + testargs ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["examples/by_feature/local_sgd.py"] run_command(self._launch_args + testargs )
700
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
0
import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class lowercase : def __init__( self : Optional[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : str=13 , _UpperCamelCase : Any=7 , _UpperCamelCase : Union[str, Any]=6 , _UpperCamelCase : Optional[Any]=17 , _UpperCamelCase : Optional[Any]=23 , _UpperCamelCase : Optional[int]=11 , _UpperCamelCase : Any=True , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = seq_length SCREAMING_SNAKE_CASE = act_dim SCREAMING_SNAKE_CASE = state_dim SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = max_length SCREAMING_SNAKE_CASE = is_training def __snake_case( self : Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) SCREAMING_SNAKE_CASE = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) SCREAMING_SNAKE_CASE = floats_tensor((self.batch_size, self.seq_length, 1) ) SCREAMING_SNAKE_CASE = floats_tensor((self.batch_size, self.seq_length, 1) ) SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) SCREAMING_SNAKE_CASE = random_attention_mask((self.batch_size, self.seq_length) ) SCREAMING_SNAKE_CASE = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def __snake_case( self : List[Any] , _UpperCamelCase : Any , _UpperCamelCase : str , _UpperCamelCase : Any , _UpperCamelCase : Tuple , _UpperCamelCase : List[str] , _UpperCamelCase : str , _UpperCamelCase : List[str] , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = DecisionTransformerModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = config_and_inputs SCREAMING_SNAKE_CASE = { "states": states, "actions": actions, "rewards": rewards, "returns_to_go": returns_to_go, "timesteps": timesteps, "attention_mask": attention_mask, } return config, inputs_dict @require_torch class lowercase ( a , a , a , unittest.TestCase ): lowercase__ : str = (DecisionTransformerModel,) if is_torch_available() else () lowercase__ : Optional[int] = () lowercase__ : int = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids lowercase__ : List[Any] = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features lowercase__ : List[str] = False lowercase__ : int = False lowercase__ : Optional[Any] = False lowercase__ : Optional[int] = False lowercase__ : List[str] = False lowercase__ : List[Any] = False lowercase__ : Any = False lowercase__ : Optional[Any] = False lowercase__ : Optional[Any] = False def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DecisionTransformerModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : List[str] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) @slow def __snake_case( self : str ) -> List[str]: '''simple docstring''' for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = DecisionTransformerModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __snake_case( self : Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = [ "states", "actions", "rewards", "returns_to_go", "timesteps", "attention_mask", ] self.assertListEqual(arg_names[: len(_UpperCamelCase )] , _UpperCamelCase ) @require_torch class lowercase ( unittest.TestCase ): @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = 2 # number of steps of autoregressive prediction we will perform SCREAMING_SNAKE_CASE = 10 # defined by the RL environment, may be normalized SCREAMING_SNAKE_CASE = DecisionTransformerModel.from_pretrained("edbeeching/decision-transformer-gym-hopper-expert" ) SCREAMING_SNAKE_CASE = model.to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model.config torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = torch.randn(1 , 1 , config.state_dim ).to(device=_UpperCamelCase , dtype=torch.floataa ) # env.reset() SCREAMING_SNAKE_CASE = torch.tensor( [[0.2_4_2_7_9_3, -0.2_8_6_9_3_0_7_4, 0.8_7_4_2_6_1_3], [0.6_7_8_1_5_2_7_4, -0.0_8_1_0_1_0_8_5, -0.1_2_9_5_2_1_4_7]] , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor(_UpperCamelCase , device=_UpperCamelCase , dtype=torch.floataa ).reshape(1 , 1 , 1 ) SCREAMING_SNAKE_CASE = state SCREAMING_SNAKE_CASE = torch.zeros(1 , 0 , config.act_dim , device=_UpperCamelCase , dtype=torch.floataa ) SCREAMING_SNAKE_CASE = torch.zeros(1 , 0 , device=_UpperCamelCase , dtype=torch.floataa ) SCREAMING_SNAKE_CASE = torch.tensor(0 , device=_UpperCamelCase , dtype=torch.long ).reshape(1 , 1 ) for step in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=_UpperCamelCase )] , dim=1 ) SCREAMING_SNAKE_CASE = torch.cat([rewards, torch.zeros(1 , 1 , device=_UpperCamelCase )] , dim=1 ) SCREAMING_SNAKE_CASE = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = model( states=_UpperCamelCase , actions=_UpperCamelCase , rewards=_UpperCamelCase , returns_to_go=_UpperCamelCase , timesteps=_UpperCamelCase , attention_mask=_UpperCamelCase , return_dict=_UpperCamelCase , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1e-4 ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=_UpperCamelCase , dtype=torch.floataa ), 1.0, False, {}, ) SCREAMING_SNAKE_CASE = action_pred[0, -1] SCREAMING_SNAKE_CASE = torch.cat([states, state] , dim=1 ) SCREAMING_SNAKE_CASE = returns_to_go[0, -1] - reward SCREAMING_SNAKE_CASE = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) SCREAMING_SNAKE_CASE = torch.cat( [timesteps, torch.ones((1, 1) , device=_UpperCamelCase , dtype=torch.long ) * (step + 1)] , dim=1 )
701
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {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." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , 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}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
0
import argparse import math import os import torch from neural_compressor.utils.pytorch import load from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel def __lowerCamelCase (): SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument( "-m" , "--pretrained_model_name_or_path" , type=lowercase__ , default=lowercase__ , required=lowercase__ , help="Path to pretrained model or model identifier from huggingface.co/models." , ) parser.add_argument( "-c" , "--caption" , type=lowercase__ , default="robotic cat with wings" , help="Text used to generate images." , ) parser.add_argument( "-n" , "--images_num" , type=lowercase__ , default=4 , help="How much images to generate." , ) parser.add_argument( "-s" , "--seed" , type=lowercase__ , default=4_2 , help="Seed for random process." , ) parser.add_argument( "-ci" , "--cuda_id" , type=lowercase__ , default=0 , help="cuda_id." , ) SCREAMING_SNAKE_CASE = parser.parse_args() return args def __lowerCamelCase (UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Dict ): if not len(lowercase__ ) == rows * cols: raise ValueError("The specified number of rows and columns are not correct." ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = imgs[0].size SCREAMING_SNAKE_CASE = Image.new("RGB" , size=(cols * w, rows * h) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = grid.size for i, img in enumerate(lowercase__ ): grid.paste(lowercase__ , box=(i % cols * w, i // cols * h) ) return grid def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : List[str]="robotic cat with wings" , UpperCAmelCase__ : Optional[int]=7.5 , UpperCAmelCase__ : Any=5_0 , UpperCAmelCase__ : Dict=1 , UpperCAmelCase__ : int=4_2 , ): SCREAMING_SNAKE_CASE = torch.Generator(pipeline.device ).manual_seed(lowercase__ ) SCREAMING_SNAKE_CASE = pipeline( lowercase__ , guidance_scale=lowercase__ , num_inference_steps=lowercase__ , generator=lowercase__ , num_images_per_prompt=lowercase__ , ).images SCREAMING_SNAKE_CASE = int(math.sqrt(lowercase__ ) ) SCREAMING_SNAKE_CASE = image_grid(lowercase__ , rows=_rows , cols=num_images_per_prompt // _rows ) return grid, images _lowerCamelCase : Optional[int] = parse_args() # Load models and create wrapper for stable diffusion _lowerCamelCase : Dict = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder='''tokenizer''') _lowerCamelCase : List[str] = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='''text_encoder''') _lowerCamelCase : Tuple = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder='''vae''') _lowerCamelCase : str = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='''unet''') _lowerCamelCase : List[str] = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) _lowerCamelCase : Any = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, '''best_model.pt''')): _lowerCamelCase : List[str] = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, '''unet''', unet) else: _lowerCamelCase : Optional[int] = unet.to(torch.device('''cuda''', args.cuda_id)) _lowerCamelCase : Tuple = pipeline.to(unet.device) _lowerCamelCase , _lowerCamelCase : List[str] = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed) grid.save(os.path.join(args.pretrained_model_name_or_path, '''{}.png'''.format('''_'''.join(args.caption.split())))) _lowerCamelCase : Optional[int] = os.path.join(args.pretrained_model_name_or_path, '''_'''.join(args.caption.split())) os.makedirs(dirname, exist_ok=True) for idx, image in enumerate(images): image.save(os.path.join(dirname, '''{}.png'''.format(idx + 1)))
702
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
0
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : int , _UpperCamelCase : List[str] , _UpperCamelCase : str=7 , _UpperCamelCase : str=3 , _UpperCamelCase : Optional[int]=18 , _UpperCamelCase : Dict=30 , _UpperCamelCase : Tuple=400 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : List[Any]=True , _UpperCamelCase : Union[str, Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {'''shortest_edge''': 18} SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_center_crop SCREAMING_SNAKE_CASE = crop_size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : int ) -> Dict: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "do_center_crop": self.do_center_crop, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowercase ( a__ , unittest.TestCase ): lowercase__ : Tuple = LevitImageProcessor if is_vision_available() else None def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = LevitImageProcessingTester(self ) @property def __snake_case( self : str ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowercase__ , "image_mean" ) ) self.assertTrue(hasattr(lowercase__ , "image_std" ) ) self.assertTrue(hasattr(lowercase__ , "do_normalize" ) ) self.assertTrue(hasattr(lowercase__ , "do_resize" ) ) self.assertTrue(hasattr(lowercase__ , "do_center_crop" ) ) self.assertTrue(hasattr(lowercase__ , "size" ) ) def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' pass def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowercase__ ) for image in image_inputs: self.assertIsInstance(lowercase__ , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(lowercase__ , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowercase__ , numpify=lowercase__ ) for image in image_inputs: self.assertIsInstance(lowercase__ , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(lowercase__ , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowercase__ , torchify=lowercase__ ) for image in image_inputs: self.assertIsInstance(lowercase__ , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(lowercase__ , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
703
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
0
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : List[str] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else """""" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(SCREAMING_SNAKE_CASE__ , lang=SCREAMING_SNAKE_CASE__ , output_type="dict" , config=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE = data["""text"""], data["""left"""], data["""top"""], data["""width"""], data["""height"""] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(SCREAMING_SNAKE_CASE__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(SCREAMING_SNAKE_CASE__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(SCREAMING_SNAKE_CASE__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(SCREAMING_SNAKE_CASE__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) assert len(SCREAMING_SNAKE_CASE__ ) == len(SCREAMING_SNAKE_CASE__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( _UpperCAmelCase ): lowercase__ : int = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : Union[str, Any] = True , _UpperCamelCase : List[Any] = None , _UpperCamelCase : Dict = PILImageResampling.BILINEAR , _UpperCamelCase : Union[str, Any] = True , _UpperCamelCase : str = None , _UpperCamelCase : int = "" , **_UpperCamelCase : Optional[Any] , ) -> Tuple: '''simple docstring''' super().__init__(**lowercase__ ) SCREAMING_SNAKE_CASE = size if size is not None else {"""height""": 224, """width""": 224} SCREAMING_SNAKE_CASE = get_size_dict(lowercase__ ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : Dict , _UpperCamelCase : Dict , _UpperCamelCase : List[str] = PILImageResampling.BILINEAR , _UpperCamelCase : int = None , **_UpperCamelCase : Optional[int] , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(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()}" ) SCREAMING_SNAKE_CASE = (size["""height"""], size["""width"""]) return resize(lowercase__ , size=lowercase__ , resample=lowercase__ , data_format=lowercase__ , **lowercase__ ) def __snake_case( self : Optional[int] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] = None , _UpperCamelCase : Union[str, Any] = None , _UpperCamelCase : int = None , _UpperCamelCase : Dict = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : List[str] = None , _UpperCamelCase : Optional[int] = ChannelDimension.FIRST , **_UpperCamelCase : int , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(lowercase__ ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = 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: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(lowercase__ ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE = apply_tesseract(lowercase__ , lowercase__ , lowercase__ ) words_batch.append(lowercase__ ) boxes_batch.append(lowercase__ ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=lowercase__ , size=lowercase__ , resample=lowercase__ ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(lowercase__ ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(lowercase__ , lowercase__ ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=lowercase__ ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
704
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) _lowerCamelCase : Tuple = { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/config.json''', '''umberto-commoncrawl-cased-v1''': ( '''https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json''' ), '''umberto-wikipedia-uncased-v1''': ( '''https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json''' ), } class lowercase ( __SCREAMING_SNAKE_CASE ): lowercase__ : Optional[Any] = """camembert""" def __init__( self : int , _UpperCamelCase : Union[str, Any]=30_522 , _UpperCamelCase : Optional[int]=768 , _UpperCamelCase : List[Any]=12 , _UpperCamelCase : Tuple=12 , _UpperCamelCase : Union[str, Any]=3_072 , _UpperCamelCase : Any="gelu" , _UpperCamelCase : Optional[int]=0.1 , _UpperCamelCase : Any=0.1 , _UpperCamelCase : int=512 , _UpperCamelCase : Any=2 , _UpperCamelCase : List[str]=0.0_2 , _UpperCamelCase : List[str]=1e-12 , _UpperCamelCase : Optional[int]=1 , _UpperCamelCase : int=0 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : Union[str, Any]="absolute" , _UpperCamelCase : Any=True , _UpperCamelCase : Optional[int]=None , **_UpperCamelCase : Any , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=__snake_case , bos_token_id=__snake_case , eos_token_id=__snake_case , **__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 = position_embedding_type SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = classifier_dropout class lowercase ( __SCREAMING_SNAKE_CASE ): @property def __snake_case( self : Dict ) -> int: '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
705
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCamelCase : int = { """configuration_xmod""": [ """XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XmodConfig""", """XmodOnnxConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : str = [ """XMOD_PRETRAINED_MODEL_ARCHIVE_LIST""", """XmodForCausalLM""", """XmodForMaskedLM""", """XmodForMultipleChoice""", """XmodForQuestionAnswering""", """XmodForSequenceClassification""", """XmodForTokenClassification""", """XmodModel""", """XmodPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys _lowerCamelCase : List[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
706
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) 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 = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : List[str] = logging.get_logger(__name__) _lowerCamelCase : Tuple = { '''facebook/vit-mae-base''': '''https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json''', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class lowercase ( _UpperCamelCase ): lowercase__ : List[str] = "vit_mae" def __init__( self : List[Any] , _UpperCamelCase : Optional[int]=768 , _UpperCamelCase : List[Any]=12 , _UpperCamelCase : Optional[Any]=12 , _UpperCamelCase : Optional[int]=3_072 , _UpperCamelCase : int="gelu" , _UpperCamelCase : str=0.0 , _UpperCamelCase : Union[str, Any]=0.0 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Optional[int]=1e-12 , _UpperCamelCase : Any=224 , _UpperCamelCase : Dict=16 , _UpperCamelCase : int=3 , _UpperCamelCase : str=True , _UpperCamelCase : Dict=16 , _UpperCamelCase : Optional[int]=512 , _UpperCamelCase : List[Any]=8 , _UpperCamelCase : Optional[int]=2_048 , _UpperCamelCase : Tuple=0.7_5 , _UpperCamelCase : Optional[Any]=False , **_UpperCamelCase : int , ) -> str: '''simple docstring''' super().__init__(**__a ) 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 = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = qkv_bias SCREAMING_SNAKE_CASE = decoder_num_attention_heads SCREAMING_SNAKE_CASE = decoder_hidden_size SCREAMING_SNAKE_CASE = decoder_num_hidden_layers SCREAMING_SNAKE_CASE = decoder_intermediate_size SCREAMING_SNAKE_CASE = mask_ratio SCREAMING_SNAKE_CASE = norm_pix_loss
707
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = {'''configuration_opt''': ['''OPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OPTConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : int = [ '''OPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OPTForCausalLM''', '''OPTModel''', '''OPTPreTrainedModel''', '''OPTForSequenceClassification''', '''OPTForQuestionAnswering''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Union[str, Any] = ['''TFOPTForCausalLM''', '''TFOPTModel''', '''TFOPTPreTrainedModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : int = [ '''FlaxOPTForCausalLM''', '''FlaxOPTModel''', '''FlaxOPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_opt import OPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_opt import ( OPT_PRETRAINED_MODEL_ARCHIVE_LIST, OPTForCausalLM, OPTForQuestionAnswering, OPTForSequenceClassification, OPTModel, OPTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_opt import TFOPTForCausalLM, TFOPTModel, TFOPTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_opt import FlaxOPTForCausalLM, FlaxOPTModel, FlaxOPTPreTrainedModel else: import sys _lowerCamelCase : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
708
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
0
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, ) _lowerCamelCase : int = { """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: _lowerCamelCase : Any = ["""CLIPTokenizerFast"""] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ["""CLIPFeatureExtractor"""] _lowerCamelCase : Optional[int] = ["""CLIPImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = [ """CLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """CLIPModel""", """CLIPPreTrainedModel""", """CLIPTextModel""", """CLIPTextModelWithProjection""", """CLIPVisionModel""", """CLIPVisionModelWithProjection""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = [ """TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFCLIPModel""", """TFCLIPPreTrainedModel""", """TFCLIPTextModel""", """TFCLIPVisionModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Dict = [ """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 _lowerCamelCase : Optional[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
709
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
0
def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Any ): if len(snake_case_ ) != len(snake_case_ ): raise ValueError("The length of profit and weight must be same." ) if max_weight <= 0: raise ValueError("max_weight must greater than zero." ) if any(p < 0 for p in profit ): raise ValueError("Profit can not be negative." ) if any(w < 0 for w in weight ): raise ValueError("Weight can not be negative." ) # List created to store profit gained for the 1kg in case of each weight # respectively. Calculate and append profit/weight for each element. SCREAMING_SNAKE_CASE = [p / w for p, w in zip(snake_case_ , snake_case_ )] # Creating a copy of the list and sorting profit/weight in ascending order SCREAMING_SNAKE_CASE = sorted(snake_case_ ) # declaring useful variables SCREAMING_SNAKE_CASE = len(snake_case_ ) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 # loop till the total weight do not reach max limit e.g. 15 kg and till i<length while limit <= max_weight and i < length: # flag value for encountered greatest element in sorted_profit_by_weight SCREAMING_SNAKE_CASE = sorted_profit_by_weight[length - i - 1] SCREAMING_SNAKE_CASE = profit_by_weight.index(snake_case_ ) SCREAMING_SNAKE_CASE = -1 # check if the weight encountered is less than the total weight # encountered before. if max_weight - limit >= weight[index]: limit += weight[index] # Adding profit gained for the given weight 1 === # weight[index]/weight[index] gain += 1 * profit[index] else: # Since the weight encountered is greater than limit, therefore take the # required number of remaining kgs and calculate profit for it. # weight remaining / weight[index] gain += (max_weight - limit) / weight[index] * profit[index] break i += 1 return gain if __name__ == "__main__": print( 'Input profits, weights, and then max_weight (all positive ints) separated by ' 'spaces.' ) _lowerCamelCase : Tuple = [int(x) for x in input('Input profits separated by spaces: ').split()] _lowerCamelCase : List[str] = [int(x) for x in input('Input weights separated by spaces: ').split()] _lowerCamelCase : int = int(input('Max weight allowed: ')) # Function Call calc_profit(profit, weight, max_weight)
710
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
0
import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast _lowerCamelCase : Optional[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class SCREAMING_SNAKE_CASE__ ( datasets.BuilderConfig ): lowercase__ : Optional[Any] = 10_000 lowercase__ : int = None lowercase__ : Optional[Any] = None class SCREAMING_SNAKE_CASE__ ( datasets.ArrowBasedBuilder ): lowercase__ : Any = ParquetConfig def __snake_case( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' return datasets.DatasetInfo(features=self.config.features ) def __snake_case( self : str , _UpperCamelCase : List[str] ) -> str: '''simple docstring''' if not self.config.data_files: raise ValueError(F"At least one data file must be specified, but got data_files={self.config.data_files}" ) SCREAMING_SNAKE_CASE = dl_manager.download_and_extract(self.config.data_files ) if isinstance(UpperCamelCase__ , (str, list, tuple) ): SCREAMING_SNAKE_CASE = data_files if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive SCREAMING_SNAKE_CASE = [dl_manager.iter_files(UpperCamelCase__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"files": files} )] SCREAMING_SNAKE_CASE = [] for split_name, files in data_files.items(): if isinstance(UpperCamelCase__ , UpperCamelCase__ ): SCREAMING_SNAKE_CASE = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive SCREAMING_SNAKE_CASE = [dl_manager.iter_files(UpperCamelCase__ ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(UpperCamelCase__ ): with open(UpperCamelCase__ , "rb" ) as f: SCREAMING_SNAKE_CASE = datasets.Features.from_arrow_schema(pq.read_schema(UpperCamelCase__ ) ) break splits.append(datasets.SplitGenerator(name=UpperCamelCase__ , gen_kwargs={"files": files} ) ) return splits def __snake_case( self : Union[str, Any] , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example SCREAMING_SNAKE_CASE = table_cast(UpperCamelCase__ , self.info.features.arrow_schema ) return pa_table def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( F"Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'" ) for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCamelCase__ ) ): with open(UpperCamelCase__ , "rb" ) as f: SCREAMING_SNAKE_CASE = pq.ParquetFile(UpperCamelCase__ ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): SCREAMING_SNAKE_CASE = pa.Table.from_batches([record_batch] ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield F"{file_idx}_{batch_idx}", self._cast_table(UpperCamelCase__ ) except ValueError as e: logger.error(F"Failed to read file '{file}' with error {type(UpperCamelCase__ )}: {e}" ) raise
711
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
0
import json from typing import Iterator, List, Union from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers from tokenizers.implementations.base_tokenizer import BaseTokenizer from tokenizers.models import Unigram from tokenizers.processors import TemplateProcessing class lowercase ( lowerCAmelCase__ ): def __init__( self : Any , _UpperCamelCase : Tuple = "▁" , _UpperCamelCase : Optional[int] = True , _UpperCamelCase : Union[str, Any] = "<unk>" , _UpperCamelCase : Dict = "</s>" , _UpperCamelCase : Dict = "<pad>" , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "pad": {"id": 0, "token": pad_token}, "eos": {"id": 1, "token": eos_token}, "unk": {"id": 2, "token": unk_token}, } SCREAMING_SNAKE_CASE = [None] * len(self.special_tokens ) for token_dict in self.special_tokens.values(): SCREAMING_SNAKE_CASE = token_dict["token"] SCREAMING_SNAKE_CASE = Tokenizer(Unigram() ) SCREAMING_SNAKE_CASE = normalizers.Sequence( [ normalizers.Nmt(), normalizers.NFKC(), normalizers.Replace(Regex(" {2,}" ) , " " ), normalizers.Lowercase(), ] ) SCREAMING_SNAKE_CASE = pre_tokenizers.Sequence( [ pre_tokenizers.Metaspace(replacement=_lowerCamelCase , add_prefix_space=_lowerCamelCase ), pre_tokenizers.Digits(individual_digits=_lowerCamelCase ), pre_tokenizers.Punctuation(), ] ) SCREAMING_SNAKE_CASE = decoders.Metaspace(replacement=_lowerCamelCase , add_prefix_space=_lowerCamelCase ) SCREAMING_SNAKE_CASE = TemplateProcessing( single=F"$A {self.special_tokens['eos']['token']}" , special_tokens=[(self.special_tokens["eos"]["token"], self.special_tokens["eos"]["id"])] , ) SCREAMING_SNAKE_CASE = { "model": "SentencePieceUnigram", "replacement": replacement, "add_prefix_space": add_prefix_space, } super().__init__(_lowerCamelCase , _lowerCamelCase ) def __snake_case( self : int , _UpperCamelCase : Any , _UpperCamelCase : Dict = 8_000 , _UpperCamelCase : List[Any] = True , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = trainers.UnigramTrainer( vocab_size=_lowerCamelCase , special_tokens=self.special_tokens_list , show_progress=_lowerCamelCase , ) if isinstance(_lowerCamelCase , _lowerCamelCase ): SCREAMING_SNAKE_CASE = [files] self._tokenizer.train(_lowerCamelCase , trainer=_lowerCamelCase ) self.add_unk_id() def __snake_case( self : Any , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any = 8_000 , _UpperCamelCase : Tuple = True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = trainers.UnigramTrainer( vocab_size=_lowerCamelCase , special_tokens=self.special_tokens_list , show_progress=_lowerCamelCase , ) self._tokenizer.train_from_iterator(_lowerCamelCase , trainer=_lowerCamelCase ) self.add_unk_id() def __snake_case( self : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = json.loads(self._tokenizer.to_str() ) SCREAMING_SNAKE_CASE = self.special_tokens["unk"]["id"] SCREAMING_SNAKE_CASE = Tokenizer.from_str(json.dumps(_lowerCamelCase ) )
712
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class lowercase : lowercase__ : Tuple = PegasusConfig lowercase__ : Dict = {} lowercase__ : str = """gelu""" def __init__( self : Union[str, Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : str=True , _UpperCamelCase : Tuple=False , _UpperCamelCase : List[str]=99 , _UpperCamelCase : Optional[Any]=32 , _UpperCamelCase : Tuple=2 , _UpperCamelCase : List[Any]=4 , _UpperCamelCase : Optional[Any]=37 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[Any]=0.1 , _UpperCamelCase : List[Any]=40 , _UpperCamelCase : Tuple=2 , _UpperCamelCase : Union[str, Any]=1 , _UpperCamelCase : Union[str, Any]=0 , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = seq_length SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = eos_token_id SCREAMING_SNAKE_CASE = pad_token_id SCREAMING_SNAKE_CASE = bos_token_id def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) SCREAMING_SNAKE_CASE = prepare_pegasus_inputs_dict(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) return config, inputs_dict def __snake_case( self : str , _UpperCamelCase : Any , _UpperCamelCase : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = TFPegasusModel(config=_UpperCamelCase ).get_decoder() SCREAMING_SNAKE_CASE = inputs_dict["input_ids"] SCREAMING_SNAKE_CASE = input_ids[:1, :] SCREAMING_SNAKE_CASE = inputs_dict["attention_mask"][:1, :] SCREAMING_SNAKE_CASE = inputs_dict["head_mask"] SCREAMING_SNAKE_CASE = 1 # first forward pass SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , head_mask=_UpperCamelCase , use_cache=_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE = tf.concat([input_ids, next_tokens] , axis=-1 ) SCREAMING_SNAKE_CASE = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase )[0] SCREAMING_SNAKE_CASE = model(_UpperCamelCase , attention_mask=_UpperCamelCase , past_key_values=_UpperCamelCase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_UpperCamelCase , _UpperCamelCase , rtol=1e-3 ) def __lowerCamelCase (UpperCAmelCase__ : Dict , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Union[str, Any]=None , UpperCAmelCase__ : Optional[int]=None , UpperCAmelCase__ : Optional[int]=None , ): if attention_mask is None: SCREAMING_SNAKE_CASE = tf.cast(tf.math.not_equal(__snake_case , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: SCREAMING_SNAKE_CASE = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowercase ( _A , _A , unittest.TestCase ): lowercase__ : Union[str, Any] = (TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () lowercase__ : str = (TFPegasusForConditionalGeneration,) if is_tf_available() else () lowercase__ : str = ( { """conversational""": TFPegasusForConditionalGeneration, """feature-extraction""": TFPegasusModel, """summarization""": TFPegasusForConditionalGeneration, """text2text-generation""": TFPegasusForConditionalGeneration, """translation""": TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) lowercase__ : List[Any] = True lowercase__ : Optional[int] = False lowercase__ : Optional[int] = False def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = TFPegasusModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase ) def __snake_case( self : Dict ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_UpperCamelCase ) @require_sentencepiece @require_tokenizers @require_tf class lowercase ( unittest.TestCase ): lowercase__ : List[Any] = [ """ PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.""", """ The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning \'Oh I think you\'re nominated\'\", said Dappy.\"And I was like \'Oh yeah, which one?\' And now we\'ve got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it\'s best of us to come down and mingle with everyone and say hello to the cameras. And now we find we\'ve got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn\'t be too disappointed if they didn\'t win this time around.\"At the end of the day we\'re grateful to be where we are in our careers.\"If it don\'t happen then it don\'t happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers\' All These Things That I\'ve Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year\'s Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" """, ] lowercase__ : Optional[int] = [ """California\'s largest electricity provider has cut power to hundreds of thousands of customers in an effort to""" """ reduce the risk of wildfires.""", """N-Dubz have revealed they\'re \"grateful\" to have been nominated for four Mobo Awards.""", ] # differs slightly from pytorch, likely due to numerical differences in linear layers lowercase__ : Tuple = """google/pegasus-xsum""" @cached_property def __snake_case( self : Dict ) -> Tuple: '''simple docstring''' return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def __snake_case( self : Tuple , **_UpperCamelCase : Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.translate_src_text(**_UpperCamelCase ) assert self.expected_text == generated_words def __snake_case( self : List[str] , **_UpperCamelCase : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.tokenizer(self.src_text , **_UpperCamelCase , padding=_UpperCamelCase , return_tensors="tf" ) SCREAMING_SNAKE_CASE = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=_UpperCamelCase , ) SCREAMING_SNAKE_CASE = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=_UpperCamelCase ) return generated_words @slow def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' self._assert_generated_batch_equal_expected()
713
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels 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 = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
0
from __future__ import annotations from random import random from typing import Generic, TypeVar _lowerCamelCase : List[str] = TypeVar('''KT''') _lowerCamelCase : Dict = TypeVar('''VT''') class lowercase ( Generic[KT, VT] ): def __init__( self : Tuple , _UpperCamelCase : Dict = "root" , _UpperCamelCase : List[Any] = None ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = key SCREAMING_SNAKE_CASE = value SCREAMING_SNAKE_CASE = [] def __repr__( self : str ) -> str: '''simple docstring''' return F"Node({self.key}: {self.value})" @property def __snake_case( self : List[str] ) -> int: '''simple docstring''' return len(self.forward ) class lowercase ( Generic[KT, VT] ): def __init__( self : List[Any] , _UpperCamelCase : Optional[int] = 0.5 , _UpperCamelCase : Optional[int] = 16 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = Node[KT, VT]() SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = p SCREAMING_SNAKE_CASE = max_level def __str__( self : str ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = list(self ) if len(__A ) == 0: return F"SkipList(level={self.level})" SCREAMING_SNAKE_CASE = max((len(str(__A ) ) for item in items) , default=4 ) SCREAMING_SNAKE_CASE = max(__A , 4 ) + 4 SCREAMING_SNAKE_CASE = self.head SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = node.forward.copy() lines.append(F"[{node.key}]".ljust(__A , "-" ) + "* " * len(__A ) ) lines.append(" " * label_size + "| " * len(__A ) ) while len(node.forward ) != 0: SCREAMING_SNAKE_CASE = node.forward[0] lines.append( F"[{node.key}]".ljust(__A , "-" ) + " ".join(str(n.key ) if n.key == node.key else "|" for n in forwards ) ) lines.append(" " * label_size + "| " * len(__A ) ) SCREAMING_SNAKE_CASE = node.forward lines.append("None".ljust(__A ) + "* " * len(__A ) ) return F"SkipList(level={self.level})\n" + "\n".join(__A ) def __iter__( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.head while len(node.forward ) != 0: yield node.forward[0].key SCREAMING_SNAKE_CASE = node.forward[0] def __snake_case( self : Union[str, Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = 1 while random() < self.p and level < self.max_level: level += 1 return level def __snake_case( self : str , _UpperCamelCase : Union[str, Any] ) -> tuple[Node[KT, VT] | None, list[Node[KT, VT]]]: '''simple docstring''' SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = self.head for i in reversed(range(self.level ) ): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: SCREAMING_SNAKE_CASE = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(__A ) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward ) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector def __snake_case( self : Any , _UpperCamelCase : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self._locate_node(__A ) if node is not None: for i, update_node in enumerate(__A ): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: SCREAMING_SNAKE_CASE = node.forward[i] else: SCREAMING_SNAKE_CASE = update_node.forward[:i] def __snake_case( self : str , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self._locate_node(__A ) if node is not None: SCREAMING_SNAKE_CASE = value else: SCREAMING_SNAKE_CASE = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1 , __A ): update_vector.append(self.head ) SCREAMING_SNAKE_CASE = level SCREAMING_SNAKE_CASE = Node(__A , __A ) for i, update_node in enumerate(update_vector[:level] ): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i] ) if update_node.level < i + 1: update_node.forward.append(__A ) else: SCREAMING_SNAKE_CASE = new_node def __snake_case( self : List[Any] , _UpperCamelCase : Any ) -> VT | None: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self._locate_node(__A ) if node is not None: return node.value return None def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 3 ) skip_list.insert("Key2" , 1_2 ) skip_list.insert("Key3" , 4_1 ) skip_list.insert("Key4" , -1_9 ) SCREAMING_SNAKE_CASE = skip_list.head SCREAMING_SNAKE_CASE = {} while node.level != 0: SCREAMING_SNAKE_CASE = node.forward[0] SCREAMING_SNAKE_CASE = node.value assert len(a__ ) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 1_2 assert all_values["Key3"] == 4_1 assert all_values["Key4"] == -1_9 def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 1_0 ) skip_list.insert("Key1" , 1_2 ) skip_list.insert("Key5" , 7 ) skip_list.insert("Key7" , 1_0 ) skip_list.insert("Key10" , 5 ) skip_list.insert("Key7" , 7 ) skip_list.insert("Key5" , 5 ) skip_list.insert("Key10" , 1_0 ) SCREAMING_SNAKE_CASE = skip_list.head SCREAMING_SNAKE_CASE = {} while node.level != 0: SCREAMING_SNAKE_CASE = node.forward[0] SCREAMING_SNAKE_CASE = node.value if len(a__ ) != 4: print() assert len(a__ ) == 4 assert all_values["Key1"] == 1_2 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 1_0 def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() assert skip_list.find("Some key" ) is None def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key2" , 2_0 ) assert skip_list.find("Key2" ) == 2_0 skip_list.insert("Some Key" , 1_0 ) skip_list.insert("Key2" , 8 ) skip_list.insert("V" , 1_3 ) assert skip_list.find("Y" ) is None assert skip_list.find("Key2" ) == 8 assert skip_list.find("Some Key" ) == 1_0 assert skip_list.find("V" ) == 1_3 def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.delete("Some key" ) assert len(skip_list.head.forward ) == 0 def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 1_2 ) skip_list.insert("V" , 1_3 ) skip_list.insert("X" , 1_4 ) skip_list.insert("Key2" , 1_5 ) skip_list.delete("V" ) skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("Key2" ) is None def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 1_2 ) skip_list.insert("V" , 1_3 ) skip_list.insert("X" , 1_4 ) skip_list.insert("Key2" , 1_5 ) skip_list.delete("V" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) == 1_4 assert skip_list.find("Key1" ) == 1_2 assert skip_list.find("Key2" ) == 1_5 skip_list.delete("X" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) == 1_2 assert skip_list.find("Key2" ) == 1_5 skip_list.delete("Key1" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) == 1_5 skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) is None def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.insert("Key1" , 1_2 ) skip_list.insert("V" , 1_3 ) skip_list.insert("X" , 1_4_2 ) skip_list.insert("Key2" , 1_5 ) skip_list.delete("X" ) def traverse_keys(UpperCAmelCase__ : List[Any] ): yield node.key for forward_node in node.forward: yield from traverse_keys(a__ ) assert len(set(traverse_keys(skip_list.head ) ) ) == 4 def __lowerCamelCase (): def is_sorted(UpperCAmelCase__ : Union[str, Any] ): return all(next_item >= item for item, next_item in zip(a__ , lst[1:] ) ) SCREAMING_SNAKE_CASE = SkipList() for i in range(1_0 ): skip_list.insert(a__ , a__ ) assert is_sorted(list(a__ ) ) skip_list.delete(5 ) skip_list.delete(8 ) skip_list.delete(2 ) assert is_sorted(list(a__ ) ) skip_list.insert(-1_2 , -1_2 ) skip_list.insert(7_7 , 7_7 ) assert is_sorted(list(a__ ) ) def __lowerCamelCase (): for _ in range(1_0_0 ): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values() def __lowerCamelCase (): SCREAMING_SNAKE_CASE = SkipList() skip_list.insert(2 , "2" ) skip_list.insert(4 , "4" ) skip_list.insert(6 , "4" ) skip_list.insert(4 , "5" ) skip_list.insert(8 , "4" ) skip_list.insert(9 , "4" ) skip_list.delete(4 ) print(a__ ) if __name__ == "__main__": import doctest doctest.testmod() main()
714
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_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()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
0
import pytest _lowerCamelCase : str = '''__dummy_dataset1__''' _lowerCamelCase : Any = '''\nimport json\nimport os\n\nimport datasets\n\n\nREPO_URL = "https://huggingface.co/datasets/albertvillanova/tests-raw-jsonl/resolve/main/"\nURLS = {"train": REPO_URL + "wikiann-bn-train.jsonl", "validation": REPO_URL + "wikiann-bn-validation.jsonl"}\n\n\nclass __DummyDataset1__(datasets.GeneratorBasedBuilder):\n\n def _info(self):\n features = datasets.Features(\n {\n "tokens": datasets.Sequence(datasets.Value("string")),\n "ner_tags": datasets.Sequence(\n datasets.features.ClassLabel(\n names=[\n "O",\n "B-PER",\n "I-PER",\n "B-ORG",\n "I-ORG",\n "B-LOC",\n "I-LOC",\n ]\n )\n ),\n "langs": datasets.Sequence(datasets.Value("string")),\n "spans": datasets.Sequence(datasets.Value("string")),\n }\n )\n return datasets.DatasetInfo(features=features)\n\n def _split_generators(self, dl_manager):\n dl_path = dl_manager.download(URLS)\n return [\n datasets.SplitGenerator(datasets.Split.TRAIN, gen_kwargs={"filepath": dl_path["train"]}),\n datasets.SplitGenerator(datasets.Split.VALIDATION, gen_kwargs={"filepath": dl_path["validation"]}),\n ]\n\n def _generate_examples(self, filepath):\n with open(filepath, "r", encoding="utf-8") as f:\n for i, line in enumerate(f):\n yield i, json.loads(line)\n''' @pytest.fixture def __lowerCamelCase (): return DATASET_LOADING_SCRIPT_NAME @pytest.fixture def __lowerCamelCase (): return DATASET_LOADING_SCRIPT_CODE @pytest.fixture def __lowerCamelCase (UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dataset_loading_script_name SCREAMING_SNAKE_CASE = tmp_path / "datasets" / script_name script_dir.mkdir(parents=UpperCamelCase__ ) SCREAMING_SNAKE_CASE = script_dir / F"{script_name}.py" with open(UpperCamelCase__ , "w" ) as f: f.write(UpperCamelCase__ ) return str(UpperCamelCase__ )
715
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
0
import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def __lowerCamelCase (): with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(_UpperCAmelCase ): requests.request("GET" , "https://huggingface.co" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("GET" , "https://huggingface.co" , timeout=1.0 ) @pytest.mark.integration def __lowerCamelCase (): with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("GET" , "https://huggingface.co" ) def __lowerCamelCase (): with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(_UpperCAmelCase ): http_head("https://huggingface.co" )
716
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
0
from decimal import Decimal, getcontext from math import ceil, factorial def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise TypeError("Undefined for non-integers" ) elif precision < 1: raise ValueError("Undefined for non-natural numbers" ) SCREAMING_SNAKE_CASE = precision SCREAMING_SNAKE_CASE = ceil(precision / 1_4 ) SCREAMING_SNAKE_CASE = 4_2_6_8_8_0 * Decimal(1_0_0_0_5 ).sqrt() SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = 1_3_5_9_1_4_0_9 SCREAMING_SNAKE_CASE = Decimal(UpperCAmelCase__ ) for k in range(1 , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = factorial(6 * k ) // (factorial(3 * k ) * factorial(UpperCAmelCase__ ) ** 3) linear_term += 5_4_5_1_4_0_1_3_4 exponential_term *= -2_6_2_5_3_7_4_1_2_6_4_0_7_6_8_0_0_0 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": _lowerCamelCase : List[str] = 50 print(f"""The first {n} digits of pi is: {pi(n)}""")
717
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # 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" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> 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()
647
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = { '''naver-clova-ix/donut-base''': '''https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json''', # See all Donut models at https://huggingface.co/models?filter=donut-swin } class lowercase ( a ): lowercase__ : List[Any] = """donut-swin""" lowercase__ : Any = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self : Tuple , _UpperCamelCase : Union[str, Any]=224 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : int=3 , _UpperCamelCase : List[str]=96 , _UpperCamelCase : Dict=[2, 2, 6, 2] , _UpperCamelCase : Any=[3, 6, 12, 24] , _UpperCamelCase : Any=7 , _UpperCamelCase : int=4.0 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=0.0 , _UpperCamelCase : Optional[Any]=0.0 , _UpperCamelCase : str=0.1 , _UpperCamelCase : str="gelu" , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : Optional[int]=0.0_2 , _UpperCamelCase : int=1e-5 , **_UpperCamelCase : int , ) -> Tuple: '''simple docstring''' super().__init__(**__A ) SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = embed_dim SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = len(__A ) SCREAMING_SNAKE_CASE = num_heads SCREAMING_SNAKE_CASE = window_size SCREAMING_SNAKE_CASE = mlp_ratio SCREAMING_SNAKE_CASE = qkv_bias SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = drop_path_rate SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = use_absolute_embeddings SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = initializer_range # 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 SCREAMING_SNAKE_CASE = int(embed_dim * 2 ** (len(__A ) - 1) )
718
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : 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(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
0
from manim import * class lowercase ( __snake_case ): def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Rectangle(height=0.5 , width=0.5 ) SCREAMING_SNAKE_CASE = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0 ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*_lowercase ).arrange(_lowercase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(*_lowercase ).arrange(_lowercase , buff=0 ) SCREAMING_SNAKE_CASE = VGroup(_lowercase , _lowercase ).arrange(_lowercase , buff=0 ) SCREAMING_SNAKE_CASE = Text("CPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(_lowercase , _lowercase ).arrange(_lowercase , buff=0.5 , aligned_edge=_lowercase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(_lowercase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(4 )] SCREAMING_SNAKE_CASE = VGroup(*_lowercase ).arrange(_lowercase , buff=0 ) SCREAMING_SNAKE_CASE = Text("GPU" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(_lowercase , _lowercase ).arrange(_lowercase , buff=0.5 , aligned_edge=_lowercase ) gpu.move_to([-1, -1, 0] ) self.add(_lowercase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*_lowercase ).arrange(_lowercase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Model" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(_lowercase , _lowercase ).arrange(_lowercase , buff=0.5 , aligned_edge=_lowercase ) model.move_to([3, -1.0, 0] ) self.add(_lowercase ) SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(_lowercase ): rect.set_stroke(_lowercase ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) SCREAMING_SNAKE_CASE = Rectangle(height=0.4_6 / 4 , width=0.4_6 / 3 ).set_stroke(width=0.0 ).set_fill(_lowercase , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.0_2 , direction=_lowercase ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=_lowercase , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=_lowercase , buff=0.0 ) self.add(_lowercase ) cpu_targs.append(_lowercase ) SCREAMING_SNAKE_CASE = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE = VGroup(*_lowercase ).arrange(_lowercase , buff=0 ) SCREAMING_SNAKE_CASE = Text("Loaded Checkpoint" , font_size=24 ) SCREAMING_SNAKE_CASE = Group(_lowercase , _lowercase ).arrange(_lowercase , aligned_edge=_lowercase , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) SCREAMING_SNAKE_CASE = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) SCREAMING_SNAKE_CASE = MarkupText( F"<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE = MarkupText( F"<span fgcolor=\'{BLUE}\'>●</span> Checkpoint" , font_size=18 , ) blue_text.next_to(_lowercase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) SCREAMING_SNAKE_CASE = MarkupText( F"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>." , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(_lowercase ) , Write(_lowercase ) ) self.play(Write(_lowercase , run_time=1 ) , Create(_lowercase , run_time=1 ) ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for i, rect in enumerate(_lowercase ): SCREAMING_SNAKE_CASE = fill.copy().set_fill(_lowercase , opacity=0.7 ) target.move_to(_lowercase ) first_animations.append(GrowFromCenter(_lowercase , run_time=1 ) ) SCREAMING_SNAKE_CASE = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(_lowercase , run_time=1.5 ) ) self.play(*_lowercase ) self.play(*_lowercase ) self.wait()
719
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
0
import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Optional[int] ): # Initialise PyTorch model SCREAMING_SNAKE_CASE = LxmertConfig.from_json_file(lowercase_ ) print(F"Building PyTorch model from configuration: {config}" ) SCREAMING_SNAKE_CASE = LxmertForPreTraining(lowercase_ ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(lowercase_ , lowercase_ , lowercase_ ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict() , lowercase_ ) if __name__ == "__main__": _lowerCamelCase : 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( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) _lowerCamelCase : List[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
720
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model 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__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
0
import os import sys _lowerCamelCase : Optional[Any] = os.path.join(os.path.dirname(__file__), '''src''') sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) _lowerCamelCase : List[Any] = [ '''torch''', '''numpy''', '''tokenizers''', '''filelock''', '''requests''', '''tqdm''', '''regex''', '''sentencepiece''', '''sacremoses''', '''importlib_metadata''', '''huggingface_hub''', ] @add_start_docstrings(AutoConfig.__doc__ ) def __lowerCamelCase(*UpperCAmelCase__ : Optional[int] , **UpperCAmelCase__ : Optional[Any] ): return AutoConfig.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) @add_start_docstrings(AutoTokenizer.__doc__ ) def __lowerCamelCase(*UpperCAmelCase__ : List[Any] , **UpperCAmelCase__ : Dict ): return AutoTokenizer.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) @add_start_docstrings(AutoModel.__doc__ ) def __lowerCamelCase(*UpperCAmelCase__ : Optional[int] , **UpperCAmelCase__ : List[str] ): return AutoModel.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def __lowerCamelCase(*UpperCAmelCase__ : Dict , **UpperCAmelCase__ : Optional[Any] ): return AutoModelForCausalLM.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def __lowerCamelCase(*UpperCAmelCase__ : Optional[int] , **UpperCAmelCase__ : Union[str, Any] ): return AutoModelForMaskedLM.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def __lowerCamelCase(*UpperCAmelCase__ : Optional[Any] , **UpperCAmelCase__ : Optional[Any] ): return AutoModelForSequenceClassification.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def __lowerCamelCase(*UpperCAmelCase__ : List[Any] , **UpperCAmelCase__ : Optional[Any] ): return AutoModelForQuestionAnswering.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ )
721
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = 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' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # 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 : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
0
from __future__ import annotations import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.shape(_lowerCamelCase ) if rows != columns: SCREAMING_SNAKE_CASE = ( "\'table\' has to be of square shaped array but got a " F"{rows}x{columns} array:\n{table}" ) raise ValueError(_lowerCamelCase ) SCREAMING_SNAKE_CASE = np.zeros((rows, columns) ) SCREAMING_SNAKE_CASE = np.zeros((rows, columns) ) for i in range(_lowerCamelCase ): for j in range(_lowerCamelCase ): SCREAMING_SNAKE_CASE = sum(lower[i][k] * upper[k][j] for k in range(_lowerCamelCase ) ) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists" ) SCREAMING_SNAKE_CASE = (table[i][j] - total) / upper[j][j] SCREAMING_SNAKE_CASE = 1 for j in range(_lowerCamelCase , _lowerCamelCase ): SCREAMING_SNAKE_CASE = sum(lower[i][k] * upper[k][j] for k in range(_lowerCamelCase ) ) SCREAMING_SNAKE_CASE = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
700
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
0
from __future__ import annotations def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Optional[Any] ): if len(UpperCAmelCase__ ) == 0: raise ValueError("find_max() arg is an empty sequence" ) if ( left >= len(UpperCAmelCase__ ) or left < -len(UpperCAmelCase__ ) or right >= len(UpperCAmelCase__ ) or right < -len(UpperCAmelCase__ ) ): raise IndexError("list index out of range" ) if left == right: return nums[left] SCREAMING_SNAKE_CASE = (left + right) >> 1 # the middle SCREAMING_SNAKE_CASE = find_max(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) # find max in range[left, mid] SCREAMING_SNAKE_CASE = find_max(UpperCAmelCase__ , mid + 1 , UpperCAmelCase__ ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
701
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {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." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , 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}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
0
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray ): return 1 / (1 + np.exp(-vector )) def __lowerCamelCase (UpperCAmelCase__ : np.ndarray ): return vector * sigmoid(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": import doctest doctest.testmod()
702
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
0
from typing import Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images from ...utils import TensorType, logging _lowerCamelCase : List[Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Tuple = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Union[int, float] = 1 / 255 , _UpperCamelCase : bool = True , _UpperCamelCase : int = 8 , **_UpperCamelCase : Optional[Any] , ) -> List[str]: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad SCREAMING_SNAKE_CASE = pad_size def __snake_case( self : Optional[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : float , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' return rescale(_UpperCamelCase , scale=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : np.ndarray , _UpperCamelCase : int , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = get_image_size(_UpperCamelCase ) SCREAMING_SNAKE_CASE = (old_height // size + 1) * size - old_height SCREAMING_SNAKE_CASE = (old_width // size + 1) * size - old_width return pad(_UpperCamelCase , ((0, pad_height), (0, pad_width)) , mode="symmetric" , data_format=_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : ImageInput , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[float] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : Union[str, ChannelDimension] = ChannelDimension.FIRST , **_UpperCamelCase : Optional[Any] , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE = do_pad if do_pad is not None else self.do_pad SCREAMING_SNAKE_CASE = pad_size if pad_size is not None else self.pad_size SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) 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. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE = [self.rescale(image=_UpperCamelCase , scale=_UpperCamelCase ) for image in images] if do_pad: SCREAMING_SNAKE_CASE = [self.pad(_UpperCamelCase , size=_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = {'pixel_values': images} return BatchFeature(data=_UpperCamelCase , tensor_type=_UpperCamelCase )
703
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
0
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 _lowerCamelCase : str = logging.get_logger(__name__) _lowerCamelCase : Union[str, Any] = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class lowercase ( __A ): lowercase__ : int = """vit""" def __init__( self : List[Any] , _UpperCamelCase : Optional[Any]=768 , _UpperCamelCase : Union[str, Any]=12 , _UpperCamelCase : Any=12 , _UpperCamelCase : int=3_072 , _UpperCamelCase : List[Any]="gelu" , _UpperCamelCase : Optional[int]=0.0 , _UpperCamelCase : Optional[Any]=0.0 , _UpperCamelCase : Dict=0.0_2 , _UpperCamelCase : List[str]=1e-12 , _UpperCamelCase : Optional[int]=224 , _UpperCamelCase : Optional[int]=16 , _UpperCamelCase : List[Any]=3 , _UpperCamelCase : List[str]=True , _UpperCamelCase : Any=16 , **_UpperCamelCase : Union[str, Any] , ) -> List[Any]: '''simple docstring''' super().__init__(**_UpperCamelCase ) 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 = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = qkv_bias SCREAMING_SNAKE_CASE = encoder_stride class lowercase ( __A ): lowercase__ : Optional[Any] = version.parse("""1.11""" ) @property def __snake_case( self : str ) -> int: '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' return 1e-4
704
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
0
from typing import Any, Callable, Dict, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker _lowerCamelCase : Optional[int] = '''CompVis/stable-diffusion-v1-1''' _lowerCamelCase : List[Any] = '''CompVis/stable-diffusion-v1-2''' _lowerCamelCase : str = '''CompVis/stable-diffusion-v1-3''' _lowerCamelCase : Union[str, Any] = '''CompVis/stable-diffusion-v1-4''' class lowercase ( __lowercase ): def __init__( self : Dict , _UpperCamelCase : AutoencoderKL , _UpperCamelCase : CLIPTextModel , _UpperCamelCase : CLIPTokenizer , _UpperCamelCase : UNetaDConditionModel , _UpperCamelCase : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , _UpperCamelCase : StableDiffusionSafetyChecker , _UpperCamelCase : CLIPImageProcessor , _UpperCamelCase : bool = True , ) -> Any: '''simple docstring''' super()._init_() SCREAMING_SNAKE_CASE = StableDiffusionPipeline.from_pretrained(_A ) SCREAMING_SNAKE_CASE = StableDiffusionPipeline.from_pretrained(_A ) SCREAMING_SNAKE_CASE = StableDiffusionPipeline.from_pretrained(_A ) SCREAMING_SNAKE_CASE = StableDiffusionPipeline( vae=_A , text_encoder=_A , tokenizer=_A , unet=_A , scheduler=_A , safety_checker=_A , feature_extractor=_A , requires_safety_checker=_A , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' return {k: getattr(self , _A ) for k in self.config.keys() if not k.startswith("_" )} def __snake_case( self : List[str] , _UpperCamelCase : Optional[Union[str, int]] = "auto" ) -> Tuple: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory SCREAMING_SNAKE_CASE = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(_A ) def __snake_case( self : Any ) -> int: '''simple docstring''' self.enable_attention_slicing(_A ) @torch.no_grad() def __snake_case( self : Optional[Any] , _UpperCamelCase : Union[str, List[str]] , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 50 , _UpperCamelCase : float = 7.5 , _UpperCamelCase : Optional[Union[str, List[str]]] = None , _UpperCamelCase : Optional[int] = 1 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : Optional[torch.Generator] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[str] = "pil" , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , _UpperCamelCase : int = 1 , **_UpperCamelCase : Dict , ) -> List[str]: '''simple docstring''' return self.pipea( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) @torch.no_grad() def __snake_case( self : Any , _UpperCamelCase : Union[str, List[str]] , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 50 , _UpperCamelCase : float = 7.5 , _UpperCamelCase : Optional[Union[str, List[str]]] = None , _UpperCamelCase : Optional[int] = 1 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : Optional[torch.Generator] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[str] = "pil" , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , _UpperCamelCase : int = 1 , **_UpperCamelCase : Optional[Any] , ) -> Any: '''simple docstring''' return self.pipea( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) @torch.no_grad() def __snake_case( self : List[str] , _UpperCamelCase : Union[str, List[str]] , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 50 , _UpperCamelCase : float = 7.5 , _UpperCamelCase : Optional[Union[str, List[str]]] = None , _UpperCamelCase : Optional[int] = 1 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : Optional[torch.Generator] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[str] = "pil" , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , _UpperCamelCase : int = 1 , **_UpperCamelCase : List[Any] , ) -> int: '''simple docstring''' return self.pipea( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) @torch.no_grad() def __snake_case( self : Dict , _UpperCamelCase : Union[str, List[str]] , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 50 , _UpperCamelCase : float = 7.5 , _UpperCamelCase : Optional[Union[str, List[str]]] = None , _UpperCamelCase : Optional[int] = 1 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : Optional[torch.Generator] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[str] = "pil" , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , _UpperCamelCase : int = 1 , **_UpperCamelCase : Optional[int] , ) -> Optional[int]: '''simple docstring''' return self.pipea( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) @torch.no_grad() def __snake_case( self : Dict , _UpperCamelCase : Union[str, List[str]] , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 512 , _UpperCamelCase : int = 50 , _UpperCamelCase : float = 7.5 , _UpperCamelCase : Optional[Union[str, List[str]]] = None , _UpperCamelCase : Optional[int] = 1 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : Optional[torch.Generator] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[str] = "pil" , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , _UpperCamelCase : int = 1 , **_UpperCamelCase : List[Any] , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = "cuda" if torch.cuda.is_available() else "cpu" self.to(_A ) # Checks if the height and width are divisible by 8 or not if height % 8 != 0 or width % 8 != 0: raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}." ) # Get first result from Stable Diffusion Checkpoint v1.1 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) # Get first result from Stable Diffusion Checkpoint v1.2 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) # Get first result from Stable Diffusion Checkpoint v1.3 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) # Get first result from Stable Diffusion Checkpoint v1.4 SCREAMING_SNAKE_CASE = self.textaimg_sda_a( prompt=_A , height=_A , width=_A , num_inference_steps=_A , guidance_scale=_A , negative_prompt=_A , num_images_per_prompt=_A , eta=_A , generator=_A , latents=_A , output_type=_A , return_dict=_A , callback=_A , callback_steps=_A , **_A , ) # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]] )
705
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCamelCase : List[Any] = { '''configuration_clap''': [ '''CLAP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ClapAudioConfig''', '''ClapConfig''', '''ClapTextConfig''', ], '''processing_clap''': ['''ClapProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''CLAP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ClapModel''', '''ClapPreTrainedModel''', '''ClapTextModel''', '''ClapTextModelWithProjection''', '''ClapAudioModel''', '''ClapAudioModelWithProjection''', ] _lowerCamelCase : Optional[Any] = ['''ClapFeatureExtractor'''] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys _lowerCamelCase : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
706
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) 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 = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
0
import argparse 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 # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase : List[str] = 16 _lowerCamelCase : Any = 32 def __lowerCamelCase (UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Optional[Any] = 1_6 ): SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("bert-base-cased" ) SCREAMING_SNAKE_CASE = load_dataset("glue" , "mrpc" ) def tokenize_function(UpperCAmelCase__ : Any ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=lowerCAmelCase_ , max_length=lowerCAmelCase_ ) 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(): SCREAMING_SNAKE_CASE = datasets.map( lowerCAmelCase_ , batched=lowerCAmelCase_ , 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 SCREAMING_SNAKE_CASE = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(UpperCAmelCase__ : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE = 1_2_8 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE = 1_6 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE = 8 else: SCREAMING_SNAKE_CASE = None return tokenizer.pad( lowerCAmelCase_ , padding="longest" , max_length=lowerCAmelCase_ , pad_to_multiple_of=lowerCAmelCase_ , return_tensors="pt" , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE = DataLoader( tokenized_datasets["train"] , shuffle=lowerCAmelCase_ , collate_fn=lowerCAmelCase_ , batch_size=lowerCAmelCase_ , drop_last=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = DataLoader( tokenized_datasets["validation"] , shuffle=lowerCAmelCase_ , collate_fn=lowerCAmelCase_ , batch_size=lowerCAmelCase_ , drop_last=(accelerator.mixed_precision == "fp8") , ) return train_dataloader, eval_dataloader def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE = config["lr"] SCREAMING_SNAKE_CASE = int(config["num_epochs"] ) SCREAMING_SNAKE_CASE = int(config["seed"] ) SCREAMING_SNAKE_CASE = int(config["batch_size"] ) SCREAMING_SNAKE_CASE = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation SCREAMING_SNAKE_CASE = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: SCREAMING_SNAKE_CASE = batch_size // MAX_GPU_BATCH_SIZE SCREAMING_SNAKE_CASE = MAX_GPU_BATCH_SIZE set_seed(lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = get_dataloaders(lowerCAmelCase_ , lowerCAmelCase_ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=lowerCAmelCase_ ) # 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). SCREAMING_SNAKE_CASE = model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE = AdamW(params=model.parameters() , lr=lowerCAmelCase_ ) # Instantiate scheduler SCREAMING_SNAKE_CASE = get_linear_schedule_with_warmup( optimizer=lowerCAmelCase_ , num_warmup_steps=1_0_0 , num_training_steps=(len(lowerCAmelCase_ ) * 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. SCREAMING_SNAKE_CASE = accelerator.prepare( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) # Now we train the model for epoch in range(lowerCAmelCase_ ): model.train() for step, batch in enumerate(lowerCAmelCase_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) SCREAMING_SNAKE_CASE = model(**lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = outputs.loss SCREAMING_SNAKE_CASE = loss / gradient_accumulation_steps accelerator.backward(lowerCAmelCase_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(lowerCAmelCase_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=lowerCAmelCase_ , references=lowerCAmelCase_ , ) SCREAMING_SNAKE_CASE = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , lowerCAmelCase_ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=lowerCAmelCase_ , default=lowerCAmelCase_ , 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." ) SCREAMING_SNAKE_CASE = parser.parse_args() SCREAMING_SNAKE_CASE = {"lr": 2e-5, "num_epochs": 3, "seed": 4_2, "batch_size": 1_6} training_function(lowerCAmelCase_ , lowerCAmelCase_ ) if __name__ == "__main__": main()
707
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
0
from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class lowercase ( a ): lowercase__ : int = ["""vqvae"""] def __init__( self : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any] , _UpperCamelCase : int , ) -> Dict: '''simple docstring''' super().__init__() self.register_modules(unet=_UpperCamelCase , scheduler=_UpperCamelCase , mel=_UpperCamelCase , vqvae=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> int: '''simple docstring''' return 50 if isinstance(self.scheduler , _UpperCamelCase ) else 1_000 @torch.no_grad() def __call__( self : Any , _UpperCamelCase : Optional[int] = 1 , _UpperCamelCase : Dict = None , _UpperCamelCase : Any = None , _UpperCamelCase : List[Any] = 0 , _UpperCamelCase : Dict = 0 , _UpperCamelCase : Union[str, Any] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Any = 0 , _UpperCamelCase : Optional[int] = 0 , _UpperCamelCase : Union[str, Any] = None , _UpperCamelCase : Any = 0 , _UpperCamelCase : Tuple = None , _UpperCamelCase : List[Any] = None , _UpperCamelCase : Optional[int]=True , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = steps or self.get_default_steps() self.scheduler.set_timesteps(_UpperCamelCase ) SCREAMING_SNAKE_CASE = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: SCREAMING_SNAKE_CASE = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: SCREAMING_SNAKE_CASE = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=_UpperCamelCase , device=self.device , ) SCREAMING_SNAKE_CASE = noise SCREAMING_SNAKE_CASE = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.mel.audio_slice_to_image(_UpperCamelCase ) SCREAMING_SNAKE_CASE = np.frombuffer(input_image.tobytes() , dtype="uint8" ).reshape( (input_image.height, input_image.width) ) SCREAMING_SNAKE_CASE = (input_image / 255) * 2 - 1 SCREAMING_SNAKE_CASE = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: SCREAMING_SNAKE_CASE = self.vqvae.encode(torch.unsqueeze(_UpperCamelCase , 0 ) ).latent_dist.sample( generator=_UpperCamelCase )[0] SCREAMING_SNAKE_CASE = self.vqvae.config.scaling_factor * input_images if start_step > 0: SCREAMING_SNAKE_CASE = self.scheduler.add_noise(_UpperCamelCase , _UpperCamelCase , self.scheduler.timesteps[start_step - 1] ) SCREAMING_SNAKE_CASE = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) SCREAMING_SNAKE_CASE = int(mask_start_secs * pixels_per_second ) SCREAMING_SNAKE_CASE = int(mask_end_secs * pixels_per_second ) SCREAMING_SNAKE_CASE = self.scheduler.add_noise(_UpperCamelCase , _UpperCamelCase , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , _UpperCamelCase ): SCREAMING_SNAKE_CASE = self.unet(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase )["sample"] else: SCREAMING_SNAKE_CASE = self.unet(_UpperCamelCase , _UpperCamelCase )["sample"] if isinstance(self.scheduler , _UpperCamelCase ): SCREAMING_SNAKE_CASE = self.scheduler.step( model_output=_UpperCamelCase , timestep=_UpperCamelCase , sample=_UpperCamelCase , eta=_UpperCamelCase , generator=_UpperCamelCase , )["prev_sample"] else: SCREAMING_SNAKE_CASE = self.scheduler.step( model_output=_UpperCamelCase , timestep=_UpperCamelCase , sample=_UpperCamelCase , generator=_UpperCamelCase , )["prev_sample"] if mask is not None: if mask_start > 0: SCREAMING_SNAKE_CASE = mask[:, step, :, :mask_start] if mask_end > 0: SCREAMING_SNAKE_CASE = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance SCREAMING_SNAKE_CASE = 1 / self.vqvae.config.scaling_factor * images SCREAMING_SNAKE_CASE = self.vqvae.decode(_UpperCamelCase )["sample"] SCREAMING_SNAKE_CASE = (images / 2 + 0.5).clamp(0 , 1 ) SCREAMING_SNAKE_CASE = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() SCREAMING_SNAKE_CASE = (images * 255).round().astype("uint8" ) SCREAMING_SNAKE_CASE = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(_UpperCamelCase , mode="RGB" ).convert("L" ) for _ in images) ) SCREAMING_SNAKE_CASE = [self.mel.image_to_audio(_UpperCamelCase ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(_UpperCamelCase )[:, np.newaxis, :] ) , **ImagePipelineOutput(_UpperCamelCase ) ) @torch.no_grad() def __snake_case( self : Union[str, Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[Any] = 50 ) -> Union[str, Any]: '''simple docstring''' assert isinstance(self.scheduler , _UpperCamelCase ) self.scheduler.set_timesteps(_UpperCamelCase ) SCREAMING_SNAKE_CASE = np.array( [np.frombuffer(image.tobytes() , dtype="uint8" ).reshape((1, image.height, image.width) ) for image in images] ) SCREAMING_SNAKE_CASE = (sample / 255) * 2 - 1 SCREAMING_SNAKE_CASE = torch.Tensor(_UpperCamelCase ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): SCREAMING_SNAKE_CASE = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps SCREAMING_SNAKE_CASE = self.scheduler.alphas_cumprod[t] SCREAMING_SNAKE_CASE = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) SCREAMING_SNAKE_CASE = 1 - alpha_prod_t SCREAMING_SNAKE_CASE = self.unet(_UpperCamelCase , _UpperCamelCase )["sample"] SCREAMING_SNAKE_CASE = (1 - alpha_prod_t_prev) ** 0.5 * model_output SCREAMING_SNAKE_CASE = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) SCREAMING_SNAKE_CASE = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def __snake_case( _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = acos(torch.dot(torch.flatten(_UpperCamelCase ) , torch.flatten(_UpperCamelCase ) ) / torch.norm(_UpperCamelCase ) / torch.norm(_UpperCamelCase ) ) return sin((1 - alpha) * theta ) * xa / sin(_UpperCamelCase ) + sin(alpha * theta ) * xa / sin(_UpperCamelCase )
708
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
0
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input _lowerCamelCase : List[str] = 'Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine' def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _ask_options( "In which compute environment are you running?" , ["This machine", "AWS (Amazon SageMaker)"] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: SCREAMING_SNAKE_CASE = get_sagemaker_input() else: SCREAMING_SNAKE_CASE = get_cluster_input() return config def __lowerCamelCase (UpperCAmelCase__ : List[str]=None ): if subparsers is not None: SCREAMING_SNAKE_CASE = subparsers.add_parser("config" , description=_A ) else: SCREAMING_SNAKE_CASE = argparse.ArgumentParser("Accelerate config command" , description=_A ) parser.add_argument( "--config_file" , default=_A , 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'." ) , ) if subparsers is not None: parser.set_defaults(func=_A ) return parser def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = get_user_input() if args.config_file is not None: SCREAMING_SNAKE_CASE = args.config_file else: if not os.path.isdir(_A ): os.makedirs(_A ) SCREAMING_SNAKE_CASE = default_yaml_config_file if config_file.endswith(".json" ): config.to_json_file(_A ) else: config.to_yaml_file(_A ) print(F"accelerate configuration saved at {config_file}" ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = config_command_parser() SCREAMING_SNAKE_CASE = parser.parse_args() config_command(_A ) if __name__ == "__main__": main()
709
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
0
def __lowerCamelCase (UpperCAmelCase__ : Any ): if n == 1 or not isinstance(A_ , A_ ): return 0 elif n == 2: return 1 else: SCREAMING_SNAKE_CASE = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def __lowerCamelCase (UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 2 while digits < n: index += 1 SCREAMING_SNAKE_CASE = len(str(fibonacci(A_ ) ) ) return index def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] = 1_0_0_0 ): return fibonacci_digits_index(A_ ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
710
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
0
import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version _lowerCamelCase : Dict = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('''4.31.0''') require_version('''datasets>=1.14.0''', '''To fix: pip install -r examples/pytorch/audio-classification/requirements.txt''') def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] = 1_6_0_0_0 ): SCREAMING_SNAKE_CASE = int(round(sample_rate * max_length ) ) if len(UpperCAmelCase__ ) <= sample_length: return wav SCREAMING_SNAKE_CASE = randint(0 , len(UpperCAmelCase__ ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class SCREAMING_SNAKE_CASE__ : lowercase__ : Optional[str] = field(default=snake_case__ , metadata={"""help""": """Name of a dataset from the datasets package"""} ) lowercase__ : Optional[str] = field( default=snake_case__ , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) lowercase__ : Optional[str] = field( default=snake_case__ , metadata={"""help""": """A file containing the training audio paths and labels."""} ) lowercase__ : Optional[str] = field( default=snake_case__ , metadata={"""help""": """A file containing the validation audio paths and labels."""} ) lowercase__ : str = field( default="""train""" , metadata={ """help""": """The name of the training data set split to use (via the datasets library). Defaults to \'train\'""" } , ) lowercase__ : str = field( default="""validation""" , metadata={ """help""": ( """The name of the training data set split to use (via the datasets library). Defaults to \'validation\'""" ) } , ) lowercase__ : str = field( default="""audio""" , metadata={"""help""": """The name of the dataset column containing the audio data. Defaults to \'audio\'"""} , ) lowercase__ : str = field( default="""label""" , metadata={"""help""": """The name of the dataset column containing the labels. Defaults to \'label\'"""} ) lowercase__ : Optional[int] = field( default=snake_case__ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) lowercase__ : Optional[int] = field( default=snake_case__ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) lowercase__ : float = field( default=20 , metadata={"""help""": """Audio clips will be randomly cut to this length during training if the value is set."""} , ) @dataclass class SCREAMING_SNAKE_CASE__ : lowercase__ : str = field( default="""facebook/wav2vec2-base""" , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} , ) lowercase__ : Optional[str] = field( default=snake_case__ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) lowercase__ : Optional[str] = field( default=snake_case__ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from the Hub"""} ) lowercase__ : str = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) lowercase__ : Optional[str] = field( default=snake_case__ , metadata={"""help""": """Name or path of preprocessor config."""} ) lowercase__ : bool = field( default=snake_case__ , metadata={"""help""": """Whether to freeze the feature encoder layers of the model."""} ) lowercase__ : bool = field( default=snake_case__ , metadata={"""help""": """Whether to generate an attention mask in the feature extractor."""} ) lowercase__ : bool = field( default=snake_case__ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) lowercase__ : Optional[bool] = field( default=snake_case__ , metadata={"""help""": """Whether to freeze the feature extractor layers of the model."""} ) lowercase__ : bool = field( default=snake_case__ , metadata={"""help""": """Will enable to load a pretrained model whose head dimensions are different."""} , ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( "The argument `--freeze_feature_extractor` is deprecated and " "will be removed in a future version. Use `--freeze_feature_encoder`" "instead. Setting `freeze_feature_encoder==True`." , _A , ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( "The argument `--freeze_feature_extractor` is deprecated and " "should not be used in combination with `--freeze_feature_encoder`." "Only make use of `--freeze_feature_encoder`." ) def __lowerCamelCase (): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. SCREAMING_SNAKE_CASE = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_audio_classification" , UpperCAmelCase__ , UpperCAmelCase__ ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() SCREAMING_SNAKE_CASE = training_args.get_process_log_level() logger.setLevel(UpperCAmelCase__ ) transformers.utils.logging.set_verbosity(UpperCAmelCase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} " + F"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(F"Training/evaluation parameters {training_args}" ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. SCREAMING_SNAKE_CASE = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: SCREAMING_SNAKE_CASE = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. " "Use --overwrite_output_dir to train from scratch." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Initialize our dataset and prepare it for the audio classification task. SCREAMING_SNAKE_CASE = DatasetDict() SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) SCREAMING_SNAKE_CASE = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F"--audio_column_name {data_args.audio_column_name} not found in dataset '{data_args.dataset_name}'. " "Make sure to set `--audio_column_name` to the correct audio column - one of " F"{', '.join(raw_datasets['train'].column_names )}." ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F"--label_column_name {data_args.label_column_name} not found in dataset '{data_args.dataset_name}'. " "Make sure to set `--label_column_name` to the correct text column - one of " F"{', '.join(raw_datasets['train'].column_names )}." ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. SCREAMING_SNAKE_CASE = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) SCREAMING_SNAKE_CASE = feature_extractor.model_input_names[0] def train_transforms(UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = [] for audio in batch[data_args.audio_column_name]: SCREAMING_SNAKE_CASE = random_subsample( audio["array"] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = feature_extractor(UpperCAmelCase__ , sampling_rate=feature_extractor.sampling_rate ) SCREAMING_SNAKE_CASE = {model_input_name: inputs.get(UpperCAmelCase__ )} SCREAMING_SNAKE_CASE = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = [audio['array'] for audio in batch[data_args.audio_column_name]] SCREAMING_SNAKE_CASE = feature_extractor(UpperCAmelCase__ , sampling_rate=feature_extractor.sampling_rate ) SCREAMING_SNAKE_CASE = {model_input_name: inputs.get(UpperCAmelCase__ )} SCREAMING_SNAKE_CASE = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. SCREAMING_SNAKE_CASE = raw_datasets['train'].features[data_args.label_column_name].names SCREAMING_SNAKE_CASE = {}, {} for i, label in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = str(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = label # Load the accuracy metric from the datasets package SCREAMING_SNAKE_CASE = evaluate.load("accuracy" ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=UpperCAmelCase__ , references=eval_pred.label_ids ) SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(UpperCAmelCase__ ) , labelaid=UpperCAmelCase__ , idalabel=UpperCAmelCase__ , finetuning_task="audio-classification" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) SCREAMING_SNAKE_CASE = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=UpperCAmelCase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE = ( raw_datasets['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(UpperCAmelCase__ , output_all_columns=UpperCAmelCase__ ) if training_args.do_eval: if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE = ( raw_datasets['eval'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(UpperCAmelCase__ , output_all_columns=UpperCAmelCase__ ) # Initialize our trainer SCREAMING_SNAKE_CASE = Trainer( model=UpperCAmelCase__ , args=UpperCAmelCase__ , train_dataset=raw_datasets["train"] if training_args.do_train else None , eval_dataset=raw_datasets["eval"] if training_args.do_eval else None , compute_metrics=UpperCAmelCase__ , tokenizer=UpperCAmelCase__ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE = last_checkpoint SCREAMING_SNAKE_CASE = trainer.train(resume_from_checkpoint=UpperCAmelCase__ ) trainer.save_model() trainer.log_metrics("train" , train_result.metrics ) trainer.save_metrics("train" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: SCREAMING_SNAKE_CASE = trainer.evaluate() trainer.log_metrics("eval" , UpperCAmelCase__ ) trainer.save_metrics("eval" , UpperCAmelCase__ ) # Write model card and (optionally) push to hub SCREAMING_SNAKE_CASE = { 'finetuned_from': model_args.model_name_or_path, 'tasks': 'audio-classification', 'dataset': data_args.dataset_name, 'tags': ['audio-classification'], } if training_args.push_to_hub: trainer.push_to_hub(**UpperCAmelCase__ ) else: trainer.create_model_card(**UpperCAmelCase__ ) if __name__ == "__main__": main()
711
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_encodec''': [ '''ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EncodecConfig''', ], '''feature_extraction_encodec''': ['''EncodecFeatureExtractor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = [ '''ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EncodecModel''', '''EncodecPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
712
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
0
'''simple docstring''' import functools from typing import Any def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Optional[int] ): if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) or len(lowerCamelCase_ ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) or not all( isinstance(lowerCamelCase_ , lowerCamelCase_ ) and len(lowerCamelCase_ ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = """WORD_KEEPER""" for word in words: SCREAMING_SNAKE_CASE = trie for c in word: if c not in trie_node: SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = trie_node[c] SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = len(lowerCamelCase_ ) # Dynamic programming method @functools.cache def is_breakable(UpperCAmelCase__ : int ) -> bool: if index == len_string: return True SCREAMING_SNAKE_CASE = trie for i in range(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE = trie_node.get(string[i] , lowerCamelCase_ ) if trie_node is None: return False if trie_node.get(lowerCamelCase_ , lowerCamelCase_ ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
713
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels 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 = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
0
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def __lowerCamelCase (UpperCAmelCase__ : Dict=None ): if subparsers is not None: SCREAMING_SNAKE_CASE = subparsers.add_parser("test" ) else: SCREAMING_SNAKE_CASE = argparse.ArgumentParser("Accelerate test command" ) parser.add_argument( "--config_file" , default=A__ , 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'." ) , ) if subparsers is not None: parser.set_defaults(func=A__ ) return parser def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] ): SCREAMING_SNAKE_CASE = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ["test_utils", "scripts", "test_script.py"] ) if args.config_file is None: SCREAMING_SNAKE_CASE = script_name else: SCREAMING_SNAKE_CASE = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE = ["accelerate-launch"] + test_args.split() SCREAMING_SNAKE_CASE = execute_subprocess_async(A__ , env=os.environ.copy() ) if result.returncode == 0: print("Test is a success! You are ready for your distributed training!" ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = test_command_parser() SCREAMING_SNAKE_CASE = parser.parse_args() test_command(A__ ) if __name__ == "__main__": main()
714
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_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()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
0
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
715
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
0
import unittest from transformers import MraConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class lowercase : def __init__( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Dict=2 , _UpperCamelCase : int=8 , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : List[Any]=True , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Dict=99 , _UpperCamelCase : str=16 , _UpperCamelCase : Union[str, Any]=5 , _UpperCamelCase : Optional[Any]=2 , _UpperCamelCase : List[str]=36 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : int=0.0 , _UpperCamelCase : Optional[Any]=512 , _UpperCamelCase : Optional[Any]=16 , _UpperCamelCase : Dict=2 , _UpperCamelCase : List[str]=0.0_2 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : List[str]=4 , _UpperCamelCase : str=None , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = seq_length SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_input_mask SCREAMING_SNAKE_CASE = use_token_type_ids SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = type_vocab_size SCREAMING_SNAKE_CASE = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = num_choices SCREAMING_SNAKE_CASE = scope def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE = None if self.use_input_mask: SCREAMING_SNAKE_CASE = 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 : Optional[Any] ) -> List[str]: '''simple docstring''' return MraConfig( 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 __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.get_config() SCREAMING_SNAKE_CASE = 300 return config def __snake_case( self : List[str] ) -> int: '''simple docstring''' ( ( 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 : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : int , _UpperCamelCase : List[str] , _UpperCamelCase : Dict , _UpperCamelCase : Dict ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = MraModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() SCREAMING_SNAKE_CASE = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ ) SCREAMING_SNAKE_CASE = model(lowercase_ , token_type_ids=lowercase_ ) SCREAMING_SNAKE_CASE = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : int , _UpperCamelCase : str , _UpperCamelCase : Tuple , _UpperCamelCase : Dict , _UpperCamelCase : Optional[int] , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : int , _UpperCamelCase : str , _UpperCamelCase : Tuple , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = MraModel(lowercase_ ) model.to(lowercase_ ) model.eval() SCREAMING_SNAKE_CASE = model( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , encoder_hidden_states=lowercase_ , encoder_attention_mask=lowercase_ , ) SCREAMING_SNAKE_CASE = model( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , encoder_hidden_states=lowercase_ , ) SCREAMING_SNAKE_CASE = 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) ) def __snake_case( self : int , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[int] , _UpperCamelCase : Tuple , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : Dict , _UpperCamelCase : Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = MraForMaskedLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() SCREAMING_SNAKE_CASE = 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 __snake_case( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : Dict , _UpperCamelCase : Any , _UpperCamelCase : Optional[int] , _UpperCamelCase : int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = MraForQuestionAnswering(config=lowercase_ ) model.to(lowercase_ ) model.eval() SCREAMING_SNAKE_CASE = 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 __snake_case( self : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : str , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : int , _UpperCamelCase : Any , _UpperCamelCase : int ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = MraForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() SCREAMING_SNAKE_CASE = model(lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case( self : Tuple , _UpperCamelCase : int , _UpperCamelCase : str , _UpperCamelCase : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Dict ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = MraForTokenClassification(config=lowercase_ ) model.to(lowercase_ ) model.eval() SCREAMING_SNAKE_CASE = 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 __snake_case( self : str , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] , _UpperCamelCase : str , _UpperCamelCase : int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_choices SCREAMING_SNAKE_CASE = MraForMultipleChoice(config=lowercase_ ) model.to(lowercase_ ) 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( lowercase_ , attention_mask=lowercase_ , token_type_ids=lowercase_ , labels=lowercase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __snake_case( self : List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = config_and_inputs SCREAMING_SNAKE_CASE = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowercase ( a , unittest.TestCase ): lowercase__ : str = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) lowercase__ : Optional[int] = False lowercase__ : Union[str, Any] = False lowercase__ : Any = False lowercase__ : Union[str, Any] = False lowercase__ : int = () def __snake_case( self : Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = MraModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=lowercase_ , hidden_size=37 ) def __snake_case( self : Dict ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_ ) def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE = type self.model_tester.create_and_check_model(*lowercase_ ) def __snake_case( self : List[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowercase_ ) def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowercase_ ) def __snake_case( self : Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowercase_ ) def __snake_case( self : Optional[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowercase_ ) def __snake_case( self : Optional[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowercase_ ) @slow def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = MraModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) @unittest.skip(reason="MRA does not output attentions" ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' return @require_torch class lowercase ( unittest.TestCase ): @slow def __snake_case( self : Dict ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = MraModel.from_pretrained("uw-madison/mra-base-512-4" ) SCREAMING_SNAKE_CASE = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(lowercase_ )[0] SCREAMING_SNAKE_CASE = torch.Size((1, 256, 768) ) self.assertEqual(output.shape , lowercase_ ) SCREAMING_SNAKE_CASE = torch.tensor( [[[-0.0_1_4_0, 0.0_8_3_0, -0.0_3_8_1], [0.1_5_4_6, 0.1_4_0_2, 0.0_2_2_0], [0.1_1_6_2, 0.0_8_5_1, 0.0_1_6_5]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , lowercase_ , atol=1e-4 ) ) @slow def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = MraForMaskedLM.from_pretrained("uw-madison/mra-base-512-4" ) SCREAMING_SNAKE_CASE = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(lowercase_ )[0] SCREAMING_SNAKE_CASE = 50_265 SCREAMING_SNAKE_CASE = torch.Size((1, 256, vocab_size) ) self.assertEqual(output.shape , lowercase_ ) SCREAMING_SNAKE_CASE = torch.tensor( [[[9.2_5_9_5, -3.6_0_3_8, 1_1.8_8_1_9], [9.3_8_6_9, -3.2_6_9_3, 1_1.0_9_5_6], [1_1.8_5_2_4, -3.4_9_3_8, 1_3.1_2_1_0]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , lowercase_ , atol=1e-4 ) ) @slow def __snake_case( self : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = MraForMaskedLM.from_pretrained("uw-madison/mra-base-4096-8-d3" ) SCREAMING_SNAKE_CASE = torch.arange(4_096 ).unsqueeze(0 ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(lowercase_ )[0] SCREAMING_SNAKE_CASE = 50_265 SCREAMING_SNAKE_CASE = torch.Size((1, 4_096, vocab_size) ) self.assertEqual(output.shape , lowercase_ ) SCREAMING_SNAKE_CASE = torch.tensor( [[[5.4_7_8_9, -2.3_5_6_4, 7.5_0_6_4], [7.9_0_6_7, -1.3_3_6_9, 9.9_6_6_8], [9.0_7_1_2, -1.8_1_0_6, 7.0_3_8_0]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , lowercase_ , atol=1e-4 ) )
716
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
0
import time import warnings from abc import ABC from copy import deepcopy from typing import Optional import torch from ..utils import add_start_docstrings, logging _lowerCamelCase : str = logging.get_logger(__name__) _lowerCamelCase : List[str] = r'''\n Args:\n input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):\n Indices of input sequence tokens in the vocabulary.\n\n Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and\n [`PreTrainedTokenizer.__call__`] for details.\n\n [What are input IDs?](../glossary#input-ids)\n scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`):\n Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax\n or scores for each vocabulary token after SoftMax.\n kwargs (`Dict[str, Any]`, *optional*):\n Additional stopping criteria specific kwargs.\n\n Return:\n `bool`. `False` indicates we should continue, `True` indicates we should stop.\n\n''' class lowercase ( __lowerCAmelCase ): @add_start_docstrings(lowerCAmelCase_ ) def __call__( self : Optional[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> bool: '''simple docstring''' raise NotImplementedError("StoppingCriteria needs to be subclassed" ) class lowercase ( __lowerCAmelCase ): def __init__( self : Tuple , _UpperCamelCase : List[Any] , _UpperCamelCase : str = None ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = max_length SCREAMING_SNAKE_CASE = max_position_embeddings @add_start_docstrings(lowerCAmelCase_ ) def __call__( self : Any , _UpperCamelCase : str , _UpperCamelCase : str , **_UpperCamelCase : Optional[Any] ) -> bool: '''simple docstring''' SCREAMING_SNAKE_CASE = input_ids.shape[-1] SCREAMING_SNAKE_CASE = cur_len >= self.max_length if self.max_position_embeddings is not None and not is_done and cur_len >= self.max_position_embeddings: logger.warning_once( "This is a friendly reminder - the current text generation call will exceed the model's predefined " F"maximum length ({self.max_position_embeddings}). Depending on the model, you may observe " "exceptions, performance degradation, or nothing at all." ) return is_done class lowercase ( __lowerCAmelCase ): def __init__( self : Optional[Any] , _UpperCamelCase : Dict , _UpperCamelCase : Tuple ) -> Dict: '''simple docstring''' warnings.warn( "The class `MaxNewTokensCriteria` is deprecated. " F"Please use `MaxLengthCriteria(max_length={start_length + max_new_tokens})` " "with `max_length = start_length + max_new_tokens` instead." , lowerCAmelCase_ , ) SCREAMING_SNAKE_CASE = start_length SCREAMING_SNAKE_CASE = max_new_tokens SCREAMING_SNAKE_CASE = start_length + max_new_tokens @add_start_docstrings(lowerCAmelCase_ ) def __call__( self : Tuple , _UpperCamelCase : Tuple , _UpperCamelCase : int , **_UpperCamelCase : Optional[int] ) -> bool: '''simple docstring''' return input_ids.shape[-1] >= self.max_length class lowercase ( __lowerCAmelCase ): def __init__( self : int , _UpperCamelCase : str , _UpperCamelCase : Optional[int] = None ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = max_time SCREAMING_SNAKE_CASE = time.time() if initial_timestamp is None else initial_timestamp @add_start_docstrings(lowerCAmelCase_ ) def __call__( self : int , _UpperCamelCase : int , _UpperCamelCase : Optional[int] , **_UpperCamelCase : List[Any] ) -> bool: '''simple docstring''' return time.time() - self.initial_timestamp > self.max_time class lowercase ( __lowerCAmelCase ): @add_start_docstrings(lowerCAmelCase_ ) def __call__( self : str , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] , **_UpperCamelCase : Any ) -> bool: '''simple docstring''' return any(criteria(lowerCAmelCase_ , lowerCAmelCase_ ) for criteria in self ) @property def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' for stopping_criterium in self: if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): return stopping_criterium.max_length elif isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): return stopping_criterium.max_length return None def __lowerCamelCase (UpperCAmelCase__ : StoppingCriteriaList , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = stopping_criteria.max_length SCREAMING_SNAKE_CASE = deepcopy(snake_case__ ) if stopping_max_length is not None and stopping_max_length != max_length: warnings.warn("You set different `max_length` for stopping criteria and `max_length` parameter" , snake_case__ ) elif stopping_max_length is None: new_stopping_criteria.append(MaxLengthCriteria(max_length=snake_case__ ) ) return new_stopping_criteria
717
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # 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" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> 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()
647
0
import requests _lowerCamelCase : Tuple = 'https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=' def __lowerCamelCase (UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page["articles"] , 1 ): print(F"{i}.) {article['title']}" ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
718
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : 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(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
0
from __future__ import annotations def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : int ): '''simple docstring''' if b == 0: return (1, 0) (SCREAMING_SNAKE_CASE) = extended_euclid(UpperCAmelCase__ , a % b ) SCREAMING_SNAKE_CASE = a // b return (y, x - k * y) def __lowerCamelCase (UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Tuple ): '''simple docstring''' (SCREAMING_SNAKE_CASE) = extended_euclid(UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = na * na SCREAMING_SNAKE_CASE = ra * x * na + ra * y * na return (n % m + m) % m def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[Any] ): '''simple docstring''' (SCREAMING_SNAKE_CASE) = extended_euclid(UpperCAmelCase__ , UpperCAmelCase__ ) if b < 0: SCREAMING_SNAKE_CASE = (b % n + n) % n return b def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Dict ): '''simple docstring''' SCREAMING_SNAKE_CASE = invert_modulo(UpperCAmelCase__ , UpperCAmelCase__ ), invert_modulo(UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = na * na SCREAMING_SNAKE_CASE = ra * x * na + ra * y * na return (n % m + m) % m if __name__ == "__main__": from doctest import testmod testmod(name='''chinese_remainder_theorem''', verbose=True) testmod(name='''chinese_remainder_theorem2''', verbose=True) testmod(name='''invert_modulo''', verbose=True) testmod(name='''extended_euclid''', verbose=True)
719
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
0
from math import isqrt def __lowerCamelCase (UpperCAmelCase__ : int ): return all(number % divisor != 0 for divisor in range(2 , isqrt(__a ) + 1 ) ) def __lowerCamelCase (UpperCAmelCase__ : int = 1_0**6 ): SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = 7 while prime_candidate < max_prime: primes_count += is_prime(__a ) cube_index += 1 prime_candidate += 6 * cube_index return primes_count if __name__ == "__main__": print(f"""{solution() = }""")
720
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model 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__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
0
import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input _lowerCamelCase : Optional[int] = """Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine""" def __lowerCamelCase(): SCREAMING_SNAKE_CASE = _ask_options( "In which compute environment are you running?" , ["This machine", "AWS (Amazon SageMaker)"] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: SCREAMING_SNAKE_CASE = get_sagemaker_input() else: SCREAMING_SNAKE_CASE = get_cluster_input() return config def __lowerCamelCase(UpperCAmelCase__ : int=None ): if subparsers is not None: SCREAMING_SNAKE_CASE = subparsers.add_parser("config" , description=UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = argparse.ArgumentParser("Accelerate config command" , description=UpperCAmelCase__ ) parser.add_argument( "--config_file" , default=UpperCAmelCase__ , 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'." ) , ) if subparsers is not None: parser.set_defaults(func=UpperCAmelCase__ ) return parser def __lowerCamelCase(UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = get_user_input() if args.config_file is not None: SCREAMING_SNAKE_CASE = args.config_file else: if not os.path.isdir(UpperCAmelCase__ ): os.makedirs(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = default_yaml_config_file if config_file.endswith(".json" ): config.to_json_file(UpperCAmelCase__ ) else: config.to_yaml_file(UpperCAmelCase__ ) print(F"accelerate configuration saved at {config_file}" ) def __lowerCamelCase(): SCREAMING_SNAKE_CASE = config_command_parser() SCREAMING_SNAKE_CASE = parser.parse_args() config_command(UpperCAmelCase__ ) if __name__ == "__main__": main()
721
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = 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' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # 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 : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
0
import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowercase : def __init__( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : int=2 , _UpperCamelCase : Optional[int]=3 , _UpperCamelCase : Any=4 , _UpperCamelCase : Optional[Any]=2 , _UpperCamelCase : str=7 , _UpperCamelCase : List[str]=True , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : List[str]=True , _UpperCamelCase : List[str]=99 , _UpperCamelCase : Tuple=36 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=4 , _UpperCamelCase : List[Any]=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : Dict=0.1 , _UpperCamelCase : List[str]=0.1 , _UpperCamelCase : Optional[int]=512 , _UpperCamelCase : Optional[int]=16 , _UpperCamelCase : Optional[Any]=2 , _UpperCamelCase : int=0.0_2 , _UpperCamelCase : Optional[Any]=6 , _UpperCamelCase : Optional[int]=6 , _UpperCamelCase : Any=3 , _UpperCamelCase : Union[str, Any]=4 , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Dict=1_000 , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = text_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 = coordinate_size SCREAMING_SNAKE_CASE = shape_size SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = num_choices SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE = text_seq_length SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE = self.text_seq_length + self.image_seq_length def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE = bbox[i, j, 3] SCREAMING_SNAKE_CASE = bbox[i, j, 1] SCREAMING_SNAKE_CASE = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE = bbox[i, j, 2] SCREAMING_SNAKE_CASE = bbox[i, j, 0] SCREAMING_SNAKE_CASE = t SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_input_mask: SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) 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.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __snake_case( self : Any , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] , _UpperCamelCase : Any , _UpperCamelCase : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : Any , _UpperCamelCase : Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = LayoutLMvaModel(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() # text + image SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , pixel_values=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = model( UpperCAmelCase__ , bbox=UpperCAmelCase__ , pixel_values=UpperCAmelCase__ , attention_mask=UpperCAmelCase__ , token_type_ids=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , bbox=UpperCAmelCase__ , pixel_values=UpperCAmelCase__ , token_type_ids=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , bbox=UpperCAmelCase__ , pixel_values=UpperCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE = model(pixel_values=UpperCAmelCase__ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __snake_case( self : List[str] , _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : Dict , _UpperCamelCase : Optional[int] , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = LayoutLMvaForSequenceClassification(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = model( UpperCAmelCase__ , bbox=UpperCAmelCase__ , pixel_values=UpperCAmelCase__ , attention_mask=UpperCAmelCase__ , token_type_ids=UpperCAmelCase__ , labels=UpperCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case( self : Tuple , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = LayoutLMvaForTokenClassification(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = model( UpperCAmelCase__ , bbox=UpperCAmelCase__ , pixel_values=UpperCAmelCase__ , attention_mask=UpperCAmelCase__ , token_type_ids=UpperCAmelCase__ , labels=UpperCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __snake_case( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = LayoutLMvaForQuestionAnswering(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = model( UpperCAmelCase__ , bbox=UpperCAmelCase__ , pixel_values=UpperCAmelCase__ , attention_mask=UpperCAmelCase__ , token_type_ids=UpperCAmelCase__ , start_positions=UpperCAmelCase__ , end_positions=UpperCAmelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( SCREAMING_SNAKE_CASE ) = config_and_inputs SCREAMING_SNAKE_CASE = { '''input_ids''': input_ids, '''bbox''': bbox, '''pixel_values''': pixel_values, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class lowercase ( lowercase__ , lowercase__ , unittest.TestCase ): lowercase__ : Dict = False lowercase__ : Any = False lowercase__ : Optional[int] = False lowercase__ : List[str] = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) lowercase__ : List[str] = ( {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel} if is_torch_available() else {} ) def __snake_case( self : str , _UpperCamelCase : int , _UpperCamelCase : Optional[int] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' return True def __snake_case( self : Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = LayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=UpperCAmelCase__ , hidden_size=37 ) def __snake_case( self : str , _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Dict=False ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(UpperCAmelCase__ ) if model_class in get_values(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = { k: v.unsqueeze(1 ).expand(-1 , self.model_tester.num_choices , -1 ).contiguous() if isinstance(UpperCAmelCase__ , torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase__ ) elif model_class in get_values(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase__ ) elif model_class in [ *get_values(UpperCAmelCase__ ), ]: SCREAMING_SNAKE_CASE = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase__ ) elif model_class in [ *get_values(UpperCAmelCase__ ), ]: SCREAMING_SNAKE_CASE = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=UpperCAmelCase__ , ) return inputs_dict def __snake_case( self : Tuple ) -> str: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase__ ) def __snake_case( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE = type self.model_tester.create_and_check_model(*UpperCAmelCase__ ) def __snake_case( self : Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__ ) def __snake_case( self : Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__ ) def __snake_case( self : List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__ ) @slow def __snake_case( self : Any ) -> int: '''simple docstring''' for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = LayoutLMvaModel.from_pretrained(UpperCAmelCase__ ) self.assertIsNotNone(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=UpperCAmelCase__ ) if is_vision_available() else None @slow def __snake_case( self : Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base" ).to(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ).pixel_values.to(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.tensor([[1, 2]] ) SCREAMING_SNAKE_CASE = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass SCREAMING_SNAKE_CASE = model( input_ids=input_ids.to(UpperCAmelCase__ ) , bbox=bbox.to(UpperCAmelCase__ ) , pixel_values=pixel_values.to(UpperCAmelCase__ ) , ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 199, 768) ) self.assertEqual(outputs.last_hidden_state.shape , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.tensor( [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]] ).to(UpperCAmelCase__ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCAmelCase__ , atol=1e-4 ) )
700
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
0
import os from typing import Optional import fsspec from fsspec.archive import AbstractArchiveFileSystem from fsspec.utils import DEFAULT_BLOCK_SIZE class lowercase ( __UpperCAmelCase ): lowercase__ : Any = "" lowercase__ : str = ( None # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz ) lowercase__ : str = None # compression type in fsspec. ex: "gzip" lowercase__ : str = None # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz def __init__( self : List[str] , _UpperCamelCase : str = "" , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[dict] = None , **_UpperCamelCase : List[Any] ) -> List[Any]: '''simple docstring''' super().__init__(self , **_lowerCamelCase ) # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode SCREAMING_SNAKE_CASE = fsspec.open( _lowerCamelCase , mode="rb" , protocol=_lowerCamelCase , compression=self.compression , client_kwargs={ "requote_redirect_url": False, # see https://github.com/huggingface/datasets/pull/5459 "trust_env": True, # Enable reading proxy env variables. **(target_options or {}).pop("client_kwargs" , {} ), # To avoid issues if it was already passed. } , **(target_options or {}) , ) SCREAMING_SNAKE_CASE = os.path.basename(self.file.path.split("::" )[0] ) SCREAMING_SNAKE_CASE = ( self.compressed_name[: self.compressed_name.rindex("." )] if "." in self.compressed_name else self.compressed_name ) SCREAMING_SNAKE_CASE = None @classmethod def __snake_case( cls : List[Any] , _UpperCamelCase : Tuple ) -> List[str]: '''simple docstring''' return super()._strip_protocol(_lowerCamelCase ).lstrip("/" ) def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' if self.dir_cache is None: SCREAMING_SNAKE_CASE = {**self.file.fs.info(self.file.path ), "name": self.uncompressed_name} SCREAMING_SNAKE_CASE = {f["name"]: f} def __snake_case( self : str , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' return self.file.open().read() def __snake_case( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : str = "rb" , _UpperCamelCase : Dict=None , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[Any]=None , **_UpperCamelCase : Optional[int] , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self._strip_protocol(_lowerCamelCase ) if mode != "rb": raise ValueError(F"Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'" ) return self.file.open() class lowercase ( __UpperCAmelCase ): lowercase__ : str = "bz2" lowercase__ : Dict = "bz2" lowercase__ : Any = ".bz2" class lowercase ( __UpperCAmelCase ): lowercase__ : List[Any] = "gzip" lowercase__ : Any = "gzip" lowercase__ : List[Any] = ".gz" class lowercase ( __UpperCAmelCase ): lowercase__ : str = "lz4" lowercase__ : Any = "lz4" lowercase__ : Tuple = ".lz4" class lowercase ( __UpperCAmelCase ): lowercase__ : Optional[Any] = "xz" lowercase__ : int = "xz" lowercase__ : Union[str, Any] = ".xz" class lowercase ( __UpperCAmelCase ): lowercase__ : Any = "zstd" lowercase__ : Dict = "zstd" lowercase__ : Tuple = ".zst" def __init__( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : str = "rb" , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[dict] = None , _UpperCamelCase : int = DEFAULT_BLOCK_SIZE , **_UpperCamelCase : int , ) -> List[Any]: '''simple docstring''' super().__init__( fo=_lowerCamelCase , mode=_lowerCamelCase , target_protocol=_lowerCamelCase , target_options=_lowerCamelCase , block_size=_lowerCamelCase , **_lowerCamelCase , ) # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2: # # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open # out.close = close # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only # # see https://github.com/intake/filesystem_spec/issues/725 SCREAMING_SNAKE_CASE = self.file.__enter__ class lowercase : def __init__( self : str , _UpperCamelCase : List[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = file_ def __enter__( self : Tuple ) -> Optional[int]: '''simple docstring''' self._file.__enter__() return self def __exit__( self : int , *_UpperCamelCase : Tuple , **_UpperCamelCase : int ) -> List[Any]: '''simple docstring''' self._file.__exit__(*_lowerCamelCase , **_lowerCamelCase ) def __iter__( self : Optional[int] ) -> Optional[int]: '''simple docstring''' return iter(self._file ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' return next(self._file ) def __getattr__( self : Union[str, Any] , _UpperCamelCase : Optional[Any] ) -> Dict: '''simple docstring''' return getattr(self._file , _lowerCamelCase ) def fixed_enter(*_UpperCamelCase : Dict , **_UpperCamelCase : Any ): return WrappedFile(_enter(*_lowerCamelCase , **_lowerCamelCase ) ) SCREAMING_SNAKE_CASE = fixed_enter
701
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {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." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , 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}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
0
import random import unittest from torch.utils.data import BatchSampler, DataLoader, IterableDataset from accelerate import Accelerator from accelerate.data_loader import ( BatchSamplerShard, DataLoaderDispatcher, DataLoaderShard, IterableDatasetShard, SkipBatchSampler, SkipDataLoader, skip_first_batches, ) class lowercase ( lowerCamelCase__ ): def __init__( self : str , _UpperCamelCase : Tuple=0.0_1 , _UpperCamelCase : Union[str, Any]=1_000 ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = p_stop SCREAMING_SNAKE_CASE = max_length def __iter__( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = False while not stop and count < self.max_length: yield count count += 1 SCREAMING_SNAKE_CASE = random.random() < self.p_stop class lowercase ( unittest.TestCase ): def __snake_case( self : List[str] , _UpperCamelCase : str , _UpperCamelCase : List[Any] , _UpperCamelCase : str=False , _UpperCamelCase : List[Any]=True ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = [ BatchSamplerShard(__lowerCamelCase , 2 , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) for i in range(2 ) ] SCREAMING_SNAKE_CASE = [list(__lowerCamelCase ) for batch_sampler_shard in batch_sampler_shards] if not split_batches: self.assertListEqual([len(__lowerCamelCase ) for shard in batch_sampler_shards] , [len(__lowerCamelCase ) for e in expected] ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase ) def __snake_case( self : List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=3 , drop_last=__lowerCamelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [0, 1, 2]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 0, 1]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. SCREAMING_SNAKE_CASE = BatchSampler(range(20 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 0]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [1, 2, 3]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(20 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) # Check the shards when the dataset is very small. SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[[0, 1, 0]], [[1, 0, 1]]] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[], []] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase ) def __snake_case( self : List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=4 , drop_last=__lowerCamelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size. SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [0, 1]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 0]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [1, 2]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) # Check the shards when the dataset is very small. SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[[0, 1]], [[0, 1]]] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[], []] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=3 , drop_last=__lowerCamelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. SCREAMING_SNAKE_CASE = BatchSampler(range(20 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(20 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) # Check the shards when the dataset is very small. SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[[0, 1]], []] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=3 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[], []] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , even_batches=__lowerCamelCase ) def __snake_case( self : int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(24 ) , batch_size=4 , drop_last=__lowerCamelCase ) # Expected shouldn't change self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size. SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(22 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(21 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) # Check the shards when the dataset is very small. SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[[0, 1]], []] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) SCREAMING_SNAKE_CASE = BatchSampler(range(2 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = [[], []] self.check_batch_sampler_shards(__lowerCamelCase , __lowerCamelCase , split_batches=__lowerCamelCase , even_batches=__lowerCamelCase ) def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = [[0, 1, 2], [3, 4], [5, 6, 7, 8], [9, 10, 11], [12, 13]] SCREAMING_SNAKE_CASE = [BatchSamplerShard(__lowerCamelCase , 2 , __lowerCamelCase , even_batches=__lowerCamelCase ) for i in range(2 )] self.assertEqual(len(batch_sampler_shards[0] ) , 3 ) self.assertEqual(len(batch_sampler_shards[1] ) , 2 ) self.assertListEqual(list(batch_sampler_shards[0] ) , [[0, 1, 2], [5, 6, 7, 8], [12, 13]] ) self.assertListEqual(list(batch_sampler_shards[1] ) , [[3, 4], [9, 10, 11]] ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : Optional[int]=2 , _UpperCamelCase : List[Any]=False ) -> Dict: '''simple docstring''' random.seed(__lowerCamelCase ) SCREAMING_SNAKE_CASE = list(__lowerCamelCase ) SCREAMING_SNAKE_CASE = [ IterableDatasetShard( __lowerCamelCase , batch_size=__lowerCamelCase , drop_last=__lowerCamelCase , num_processes=__lowerCamelCase , process_index=__lowerCamelCase , split_batches=__lowerCamelCase , ) for i in range(__lowerCamelCase ) ] SCREAMING_SNAKE_CASE = [] for iterable_dataset_shard in iterable_dataset_shards: # Since our random iterable dataset will be... random... we need to use a seed to get reproducible results. random.seed(__lowerCamelCase ) iterable_dataset_lists.append(list(__lowerCamelCase ) ) SCREAMING_SNAKE_CASE = batch_size // num_processes if split_batches else batch_size # All iterable dataset shard should have the same length, a round multiple of shard_batch_size SCREAMING_SNAKE_CASE = iterable_dataset_lists[0] for l in iterable_dataset_lists[1:]: self.assertEqual(len(__lowerCamelCase ) , len(__lowerCamelCase ) ) self.assertTrue(len(__lowerCamelCase ) % shard_batch_size == 0 ) SCREAMING_SNAKE_CASE = [] for idx in range(0 , len(__lowerCamelCase ) , __lowerCamelCase ): for l in iterable_dataset_lists: observed += l[idx : idx + shard_batch_size] if not drop_last: while len(__lowerCamelCase ) < len(__lowerCamelCase ): reference += reference self.assertListEqual(__lowerCamelCase , reference[: len(__lowerCamelCase )] ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = 42 SCREAMING_SNAKE_CASE = RandomIterableDataset() self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) # Edge case with a very small dataset SCREAMING_SNAKE_CASE = RandomIterableDataset(max_length=2 ) self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) self.check_iterable_dataset_shards(__lowerCamelCase , __lowerCamelCase , batch_size=4 , drop_last=__lowerCamelCase , split_batches=__lowerCamelCase ) def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = BatchSampler(range(16 ) , batch_size=4 , drop_last=__lowerCamelCase ) SCREAMING_SNAKE_CASE = SkipBatchSampler(__lowerCamelCase , 2 ) self.assertListEqual(list(__lowerCamelCase ) , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = SkipDataLoader(list(range(16 ) ) , batch_size=4 , skip_batches=2 ) self.assertListEqual([t.tolist() for t in dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def __snake_case( self : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = DataLoader(list(range(16 ) ) , batch_size=4 ) SCREAMING_SNAKE_CASE = skip_first_batches(__lowerCamelCase , num_batches=2 ) self.assertListEqual([t.tolist() for t in new_dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def __snake_case( self : List[str] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = DataLoaderShard(list(range(16 ) ) , batch_size=4 ) for idx, _ in enumerate(__lowerCamelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(__lowerCamelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) def __snake_case( self : List[Any] ) -> Dict: '''simple docstring''' Accelerator() SCREAMING_SNAKE_CASE = DataLoaderDispatcher(range(16 ) , batch_size=4 ) for idx, _ in enumerate(__lowerCamelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(__lowerCamelCase ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 )
702
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) _lowerCamelCase : Optional[int] = logging.getLogger(__name__) _lowerCamelCase : Optional[int] = '''Hello world! cécé herlolip''' _lowerCamelCase : List[Any] = namedtuple( '''BertAbsConfig''', [ '''temp_dir''', '''large''', '''use_bert_emb''', '''finetune_bert''', '''encoder''', '''share_emb''', '''max_pos''', '''enc_layers''', '''enc_hidden_size''', '''enc_heads''', '''enc_ff_size''', '''enc_dropout''', '''dec_layers''', '''dec_hidden_size''', '''dec_heads''', '''dec_ff_size''', '''dec_dropout''', ], ) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = BertAbsConfig( temp_dir="." , finetune_bert=UpperCAmelCase__ , large=UpperCAmelCase__ , share_emb=UpperCAmelCase__ , use_bert_emb=UpperCAmelCase__ , encoder="bert" , max_pos=5_1_2 , enc_layers=6 , enc_hidden_size=5_1_2 , enc_heads=8 , enc_ff_size=5_1_2 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=7_6_8 , dec_heads=8 , dec_ff_size=2_0_4_8 , dec_dropout=0.2 , ) SCREAMING_SNAKE_CASE = torch.load(UpperCAmelCase__ , lambda UpperCAmelCase__ , UpperCAmelCase__ : storage ) SCREAMING_SNAKE_CASE = AbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) , UpperCAmelCase__ ) original.eval() SCREAMING_SNAKE_CASE = BertAbsSummarizer(UpperCAmelCase__ , torch.device("cpu" ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info("convert the model" ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info("Make sure that the models' outputs are identical" ) SCREAMING_SNAKE_CASE = BertTokenizer.from_pretrained("bert-base-uncased" ) # prepare the model inputs SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample éàalj'-." ) encoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) SCREAMING_SNAKE_CASE = tokenizer.encode("This is sample 3 éàalj'-." ) decoder_input_ids.extend([tokenizer.pad_token_id] * (5_1_2 - len(UpperCAmelCase__ )) ) SCREAMING_SNAKE_CASE = torch.tensor(UpperCAmelCase__ ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass SCREAMING_SNAKE_CASE = encoder_input_ids SCREAMING_SNAKE_CASE = decoder_input_ids SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical SCREAMING_SNAKE_CASE = original(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = original.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = new_model( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ )[0] SCREAMING_SNAKE_CASE = new_model.generator(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print("Maximum absolute difference beween weights: {:.2f}".format(UpperCAmelCase__ ) ) SCREAMING_SNAKE_CASE = torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1e-3 ) if are_identical: logging.info("all weights are equal up to 1e-3" ) else: raise ValueError("the weights are different. The new model is likely different from the original one." ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info("saving the model's state dictionary" ) torch.save( new_model.state_dict() , "./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin" ) if __name__ == "__main__": _lowerCamelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--bertabs_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''', ) _lowerCamelCase : Any = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
647
0
from __future__ import annotations import math class lowercase : def __init__( self : Optional[int] , _UpperCamelCase : int ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size # approximate the overall size of segment tree with given value SCREAMING_SNAKE_CASE = [0 for i in range(0 , 4 * size )] # create array to store lazy update SCREAMING_SNAKE_CASE = [0 for i in range(0 , 4 * size )] SCREAMING_SNAKE_CASE = [0 for i in range(0 , 4 * size )] # flag for lazy update def __snake_case( self : Union[str, Any] , _UpperCamelCase : int ) -> Any: '''simple docstring''' return idx * 2 def __snake_case( self : Tuple , _UpperCamelCase : int ) -> str: '''simple docstring''' return idx * 2 + 1 def __snake_case( self : Optional[int] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : list[int] ) -> Optional[Any]: '''simple docstring''' if left_element == right_element: SCREAMING_SNAKE_CASE = a[left_element - 1] else: SCREAMING_SNAKE_CASE = (left_element + right_element) // 2 self.build(self.left(_lowercase ) , _lowercase , _lowercase , _lowercase ) self.build(self.right(_lowercase ) , mid + 1 , _lowercase , _lowercase ) SCREAMING_SNAKE_CASE = max( self.segment_tree[self.left(_lowercase )] , self.segment_tree[self.right(_lowercase )] ) def __snake_case( self : List[Any] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int ) -> Tuple: '''simple docstring''' if self.flag[idx] is True: SCREAMING_SNAKE_CASE = self.lazy[idx] SCREAMING_SNAKE_CASE = False if left_element != right_element: SCREAMING_SNAKE_CASE = self.lazy[idx] SCREAMING_SNAKE_CASE = self.lazy[idx] SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: SCREAMING_SNAKE_CASE = val if left_element != right_element: SCREAMING_SNAKE_CASE = val SCREAMING_SNAKE_CASE = val SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True return True SCREAMING_SNAKE_CASE = (left_element + right_element) // 2 self.update(self.left(_lowercase ) , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) self.update(self.right(_lowercase ) , mid + 1 , _lowercase , _lowercase , _lowercase , _lowercase ) SCREAMING_SNAKE_CASE = max( self.segment_tree[self.left(_lowercase )] , self.segment_tree[self.right(_lowercase )] ) return True def __snake_case( self : List[Any] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int ) -> Optional[Any]: '''simple docstring''' if self.flag[idx] is True: SCREAMING_SNAKE_CASE = self.lazy[idx] SCREAMING_SNAKE_CASE = False if left_element != right_element: SCREAMING_SNAKE_CASE = self.lazy[idx] SCREAMING_SNAKE_CASE = self.lazy[idx] SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] SCREAMING_SNAKE_CASE = (left_element + right_element) // 2 SCREAMING_SNAKE_CASE = self.query(self.left(_lowercase ) , _lowercase , _lowercase , _lowercase , _lowercase ) SCREAMING_SNAKE_CASE = self.query(self.right(_lowercase ) , mid + 1 , _lowercase , _lowercase , _lowercase ) return max(_lowercase , _lowercase ) def __str__( self : Tuple ) -> Union[str, Any]: '''simple docstring''' return str([self.query(1 , 1 , self.size , _lowercase , _lowercase ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": _lowerCamelCase : List[Any] = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] _lowerCamelCase : str = 15 _lowerCamelCase : int = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 11)) print(segt.query(1, 1, size, 7, 12)) segt.update(1, 1, size, 1, 3, 1_11) print(segt.query(1, 1, size, 1, 15)) segt.update(1, 1, size, 7, 8, 2_35) print(segt)
703
def __lowerCamelCase (UpperCAmelCase__ : int ): assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE = F"The input value of [n={number}] has to be > 0" raise ValueError(UpperCAmelCase__ ) else: SCREAMING_SNAKE_CASE = sylvester(number - 1 ) SCREAMING_SNAKE_CASE = num - 1 SCREAMING_SNAKE_CASE = num return lower * upper + 1 if __name__ == "__main__": print(f"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
647
0
from ....configuration_utils import PretrainedConfig from ....utils import logging _lowerCamelCase : Optional[int] = logging.get_logger(__name__) _lowerCamelCase : Dict = { '''CarlCochet/trajectory-transformer-halfcheetah-medium-v2''': ( '''https://huggingface.co/CarlCochet/trajectory-transformer-halfcheetah-medium-v2/resolve/main/config.json''' ), # See all TrajectoryTransformer models at https://huggingface.co/models?filter=trajectory_transformer } class lowercase ( snake_case__ ): lowercase__ : Union[str, Any] = """trajectory_transformer""" lowercase__ : str = ["""past_key_values"""] lowercase__ : Union[str, Any] = { """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self : Union[str, Any] , _UpperCamelCase : str=100 , _UpperCamelCase : Optional[int]=5 , _UpperCamelCase : Union[str, Any]=1 , _UpperCamelCase : Union[str, Any]=1 , _UpperCamelCase : int=249 , _UpperCamelCase : Union[str, Any]=6 , _UpperCamelCase : Optional[int]=17 , _UpperCamelCase : Optional[Any]=25 , _UpperCamelCase : Optional[Any]=4 , _UpperCamelCase : Optional[Any]=4 , _UpperCamelCase : Optional[int]=128 , _UpperCamelCase : List[str]=0.1 , _UpperCamelCase : List[Any]=0.1 , _UpperCamelCase : Any=0.1 , _UpperCamelCase : List[Any]=0.0_0_0_6 , _UpperCamelCase : Optional[Any]=512 , _UpperCamelCase : Optional[int]=0.0_2 , _UpperCamelCase : Optional[Any]=1e-12 , _UpperCamelCase : Any=1 , _UpperCamelCase : Dict=True , _UpperCamelCase : Optional[int]=1 , _UpperCamelCase : int=50_256 , _UpperCamelCase : Union[str, Any]=50_256 , **_UpperCamelCase : Union[str, Any] , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = action_weight SCREAMING_SNAKE_CASE = reward_weight SCREAMING_SNAKE_CASE = value_weight SCREAMING_SNAKE_CASE = max_position_embeddings SCREAMING_SNAKE_CASE = block_size SCREAMING_SNAKE_CASE = action_dim SCREAMING_SNAKE_CASE = observation_dim SCREAMING_SNAKE_CASE = transition_dim SCREAMING_SNAKE_CASE = learning_rate SCREAMING_SNAKE_CASE = n_layer SCREAMING_SNAKE_CASE = n_head SCREAMING_SNAKE_CASE = n_embd SCREAMING_SNAKE_CASE = embd_pdrop SCREAMING_SNAKE_CASE = attn_pdrop SCREAMING_SNAKE_CASE = resid_pdrop SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = kaiming_initializer_range SCREAMING_SNAKE_CASE = use_cache super().__init__(pad_token_id=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , **lowercase_ )
704
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_script.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["scripts", "test_distributed_data_loop.py"] ) SCREAMING_SNAKE_CASE = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_ops.py"] ) @require_multi_gpu def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : List[Any] ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices." ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.operation_file_path] print(F"Command: {cmd}" ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) @require_multi_gpu def __snake_case( self : int ) -> int: '''simple docstring''' print(F"Found {torch.cuda.device_count()} devices, using 2 devices only" ) SCREAMING_SNAKE_CASE = ["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices="0,1" ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() ) if __name__ == "__main__": _lowerCamelCase : str = Accelerator() _lowerCamelCase : List[str] = (accelerator.state.process_index + 2, 10) _lowerCamelCase : str = torch.randint(0, 10, shape).to(accelerator.device) _lowerCamelCase : Optional[Any] = '''''' _lowerCamelCase : str = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." _lowerCamelCase : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." _lowerCamelCase : int = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
647
0
from torch import nn class lowercase ( nn.Module ): def __init__( self : Union[str, Any] , _UpperCamelCase : List[str] , _UpperCamelCase : str ) -> Union[str, Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE = class_size SCREAMING_SNAKE_CASE = embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) SCREAMING_SNAKE_CASE = nn.Linear(A_ , A_ ) def __snake_case( self : List[Any] , _UpperCamelCase : List[str] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.mlp(A_ ) return logits
705
import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowercase ( a ): lowercase__ : Tuple = (KDPMaDiscreteScheduler,) lowercase__ : Optional[int] = 10 def __snake_case( self : Optional[Any] , **_UpperCamelCase : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 1_100, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**_UpperCamelCase ) return config def __snake_case( self : int ) -> List[Any]: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=_UpperCamelCase , beta_end=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> str: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config(prediction_type="v_prediction" ) SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_934e-07 ) < 1e-2 assert abs(result_mean.item() - 6.1_112e-10 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.693_428_650_170_972e-07 ) < 1e-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1e-3 def __snake_case( self : Dict ) -> int: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE = sample.to(_UpperCamelCase ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 def __snake_case( self : Tuple ) -> str: '''simple docstring''' if torch_device == "mps": return SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_UpperCamelCase ) scheduler.set_timesteps(self.num_inference_steps , device=_UpperCamelCase ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter.to(_UpperCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = scheduler.step(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = output.prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_UpperCamelCase ) ) if str(_UpperCamelCase ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 2_0.4_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1e-3
647
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _lowerCamelCase : Any = { 'configuration_roc_bert': ['ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RoCBertConfig'], 'tokenization_roc_bert': ['RoCBertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ 'ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RoCBertForCausalLM', 'RoCBertForMaskedLM', 'RoCBertForMultipleChoice', 'RoCBertForPreTraining', 'RoCBertForQuestionAnswering', 'RoCBertForSequenceClassification', 'RoCBertForTokenClassification', 'RoCBertLayer', 'RoCBertModel', 'RoCBertPreTrainedModel', 'load_tf_weights_in_roc_bert', ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys _lowerCamelCase : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
706
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig _lowerCamelCase : Tuple = { '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class lowercase ( a ): lowercase__ : Optional[Any] = """ernie_m""" lowercase__ : Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self : Optional[int] , _UpperCamelCase : int = 250_002 , _UpperCamelCase : int = 768 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 12 , _UpperCamelCase : int = 3_072 , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 514 , _UpperCamelCase : float = 0.0_2 , _UpperCamelCase : int = 1 , _UpperCamelCase : float = 1e-05 , _UpperCamelCase : int=None , _UpperCamelCase : int=False , _UpperCamelCase : int=0.0 , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=_UpperCamelCase , **_UpperCamelCase ) 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 = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = classifier_dropout SCREAMING_SNAKE_CASE = is_decoder SCREAMING_SNAKE_CASE = act_dropout
647
0
def __lowerCamelCase (UpperCAmelCase__ : list[list[int]] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : set ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = len(_SCREAMING_SNAKE_CASE ), len(grid[0] ) if ( min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) SCREAMING_SNAKE_CASE = 0 count += depth_first_search(_SCREAMING_SNAKE_CASE , row + 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) count += depth_first_search(_SCREAMING_SNAKE_CASE , row - 1 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) count += depth_first_search(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , col + 1 , _SCREAMING_SNAKE_CASE ) count += depth_first_search(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , col - 1 , _SCREAMING_SNAKE_CASE ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
707
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Optional[int] = { '''configuration_blenderbot''': [ '''BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotConfig''', '''BlenderbotOnnxConfig''', ], '''tokenization_blenderbot''': ['''BlenderbotTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[Any] = ['''BlenderbotTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = [ '''BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotForCausalLM''', '''BlenderbotForConditionalGeneration''', '''BlenderbotModel''', '''BlenderbotPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ '''TFBlenderbotForConditionalGeneration''', '''TFBlenderbotModel''', '''TFBlenderbotPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Optional[int] = [ '''FlaxBlenderbotForConditionalGeneration''', '''FlaxBlenderbotModel''', '''FlaxBlenderbotPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys _lowerCamelCase : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
647
0
from heapq import heappop, heappush import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : bool , ): SCREAMING_SNAKE_CASE = grid.shape SCREAMING_SNAKE_CASE = [-1, 1, 0, 0] SCREAMING_SNAKE_CASE = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] SCREAMING_SNAKE_CASE = [(0, source)], set() SCREAMING_SNAKE_CASE = np.full((rows, cols) , np.inf ) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = np.empty((rows, cols) , dtype=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = None while queue: (SCREAMING_SNAKE_CASE) = heappop(UpperCAmelCase__ ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: SCREAMING_SNAKE_CASE = [] while (x, y) != source: path.append((x, y) ) SCREAMING_SNAKE_CASE = predecessors[x, y] path.append(UpperCAmelCase__ ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: SCREAMING_SNAKE_CASE = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(UpperCAmelCase__ , (dist + 1, (nx, ny)) ) SCREAMING_SNAKE_CASE = dist + 1 SCREAMING_SNAKE_CASE = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
708
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar _lowerCamelCase : Optional[Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : Any , _UpperCamelCase : T ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = data SCREAMING_SNAKE_CASE = None def __str__( self : Union[str, Any] ) -> str: '''simple docstring''' return F"{self.data}" class lowercase ( Generic[T] ): def __init__( self : Optional[int] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None def __iter__( self : str ) -> Iterator[T]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.top while node: yield node.data SCREAMING_SNAKE_CASE = node.next def __str__( self : int ) -> str: '''simple docstring''' return "->".join([str(_UpperCamelCase ) for item in self] ) def __len__( self : Tuple ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __snake_case( self : Union[str, Any] ) -> bool: '''simple docstring''' return self.top is None def __snake_case( self : str , _UpperCamelCase : T ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = Node(_UpperCamelCase ) if not self.is_empty(): SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = node def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("pop from empty stack" ) assert isinstance(self.top , _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.top SCREAMING_SNAKE_CASE = self.top.next return pop_node.data def __snake_case( self : Union[str, Any] ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError("peek from empty stack" ) assert self.top is not None return self.top.data def __snake_case( self : Dict ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = None if __name__ == "__main__": from doctest import testmod testmod()
647
0
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : str = logging.get_logger(__name__) _lowerCamelCase : List[Any] = { '''BridgeTower/bridgetower-base''': '''https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json''', '''BridgeTower/bridgetower-base-itm-mlm''': ( '''https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json''' ), } class lowercase ( __A ): lowercase__ : Tuple = """bridgetower_vision_model""" def __init__( self : Union[str, Any] , _UpperCamelCase : List[str]=768 , _UpperCamelCase : List[str]=12 , _UpperCamelCase : Optional[int]=3 , _UpperCamelCase : List[Any]=16 , _UpperCamelCase : List[Any]=288 , _UpperCamelCase : str=1 , _UpperCamelCase : int=1e-05 , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Dict=False , **_UpperCamelCase : int , ) -> List[str]: '''simple docstring''' super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = initializer_factor SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = stop_gradient SCREAMING_SNAKE_CASE = share_layernorm SCREAMING_SNAKE_CASE = remove_last_layer @classmethod def __snake_case( cls : str , _UpperCamelCase : Optional[int] , **_UpperCamelCase : List[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = cls.get_config_dict(UpperCamelCase__ , **UpperCamelCase__ ) if config_dict.get("model_type" ) == "bridgetower": SCREAMING_SNAKE_CASE = config_dict['text_config'] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( F"You are using a model of type {config_dict['model_type']} to instantiate a model of type " F"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(UpperCamelCase__ , **UpperCamelCase__ ) class lowercase ( __A ): lowercase__ : Optional[Any] = """bridgetower_text_model""" def __init__( self : str , _UpperCamelCase : List[str]=50_265 , _UpperCamelCase : Optional[int]=768 , _UpperCamelCase : Union[str, Any]=12 , _UpperCamelCase : int=12 , _UpperCamelCase : Optional[Any]=1 , _UpperCamelCase : Optional[Any]=3_072 , _UpperCamelCase : Any="gelu" , _UpperCamelCase : Tuple=0.1 , _UpperCamelCase : Optional[int]=0.1 , _UpperCamelCase : int=514 , _UpperCamelCase : Optional[int]=1 , _UpperCamelCase : Optional[Any]=1e-05 , _UpperCamelCase : str=1 , _UpperCamelCase : Dict=0 , _UpperCamelCase : Dict=2 , _UpperCamelCase : str="absolute" , _UpperCamelCase : int=True , **_UpperCamelCase : Union[str, Any] , ) -> Optional[Any]: '''simple docstring''' super().__init__(**UpperCamelCase__ ) 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 = initializer_factor 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 = layer_norm_eps SCREAMING_SNAKE_CASE = position_embedding_type SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = pad_token_id SCREAMING_SNAKE_CASE = bos_token_id SCREAMING_SNAKE_CASE = eos_token_id @classmethod def __snake_case( cls : Dict , _UpperCamelCase : Tuple , **_UpperCamelCase : List[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = cls.get_config_dict(UpperCamelCase__ , **UpperCamelCase__ ) if config_dict.get("model_type" ) == "bridgetower": SCREAMING_SNAKE_CASE = config_dict['text_config'] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( F"You are using a model of type {config_dict['model_type']} to instantiate a model of type " F"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(UpperCamelCase__ , **UpperCamelCase__ ) class lowercase ( __A ): lowercase__ : Dict = """bridgetower""" def __init__( self : Dict , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : Dict="gelu" , _UpperCamelCase : List[str]=768 , _UpperCamelCase : List[str]=1 , _UpperCamelCase : Dict=1e-05 , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : str="add" , _UpperCamelCase : Optional[int]=12 , _UpperCamelCase : Optional[Any]=6 , _UpperCamelCase : Tuple=False , _UpperCamelCase : Union[str, Any]=False , _UpperCamelCase : List[Any]=None , _UpperCamelCase : Optional[int]=None , **_UpperCamelCase : Tuple , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = kwargs.pop("text_config_dict" , UpperCamelCase__ ) SCREAMING_SNAKE_CASE = kwargs.pop("vision_config_dict" , UpperCamelCase__ ) super().__init__(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE = share_cross_modal_transformer_layers SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = initializer_factor SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = share_link_tower_layers SCREAMING_SNAKE_CASE = link_tower_type SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = tie_word_embeddings SCREAMING_SNAKE_CASE = init_layernorm_from_vision_encoder if text_config is None: SCREAMING_SNAKE_CASE = {} logger.info("`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values." ) if vision_config is None: SCREAMING_SNAKE_CASE = {} logger.info("`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values." ) SCREAMING_SNAKE_CASE = BridgeTowerTextConfig(**UpperCamelCase__ ) SCREAMING_SNAKE_CASE = BridgeTowerVisionConfig(**UpperCamelCase__ ) @classmethod def __snake_case( cls : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] , **_UpperCamelCase : Any ) -> int: '''simple docstring''' return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **UpperCamelCase__ ) def __snake_case( self : List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.text_config.to_dict() SCREAMING_SNAKE_CASE = self.vision_config.to_dict() SCREAMING_SNAKE_CASE = self.__class__.model_type return output
709
# coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # this script dumps information about the environment import os import platform import sys _lowerCamelCase : List[Any] = '''3''' print('''Python version:''', sys.version) print('''OS platform:''', platform.platform()) print('''OS architecture:''', platform.machine()) try: import torch print('''Torch version:''', torch.__version__) print('''Cuda available:''', torch.cuda.is_available()) print('''Cuda version:''', torch.version.cuda) print('''CuDNN version:''', torch.backends.cudnn.version()) print('''Number of GPUs available:''', torch.cuda.device_count()) except ImportError: print('''Torch version:''', None) try: import transformers print('''transformers version:''', transformers.__version__) except ImportError: print('''transformers version:''', None)
647
0
def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[int] ): SCREAMING_SNAKE_CASE = [0 for i in range(r + 1 )] # nc0 = 1 SCREAMING_SNAKE_CASE = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , UpperCAmelCase__ ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
710
def __lowerCamelCase (UpperCAmelCase__ : list[int] ): if not numbers: return 0 if not isinstance(UpperCAmelCase__ , (list, tuple) ) or not all( isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = numbers[0] for i in range(1 , len(UpperCAmelCase__ ) ): # update the maximum and minimum subarray products SCREAMING_SNAKE_CASE = numbers[i] if number < 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = min_till_now, max_till_now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , max_till_now * number ) SCREAMING_SNAKE_CASE = min(UpperCAmelCase__ , min_till_now * number ) # update the maximum product found till now SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , UpperCAmelCase__ ) return max_prod
647
0
from collections.abc import Callable def __lowerCamelCase (UpperCAmelCase__ : Callable[[float], float] , UpperCAmelCase__ : float , UpperCAmelCase__ : float ): SCREAMING_SNAKE_CASE = a SCREAMING_SNAKE_CASE = b if function(UpperCAmelCase__ ) == 0: # one of the a or b is a root for the function return a elif function(UpperCAmelCase__ ) == 0: return b elif ( function(UpperCAmelCase__ ) * function(UpperCAmelCase__ ) > 0 ): # if none of these are root and they are both positive or negative, # then this algorithm can't find the root raise ValueError("could not find root in given interval." ) else: SCREAMING_SNAKE_CASE = start + (end - start) / 2.0 while abs(start - mid ) > 1_0**-7: # until precisely equals to 10^-7 if function(UpperCAmelCase__ ) == 0: return mid elif function(UpperCAmelCase__ ) * function(UpperCAmelCase__ ) < 0: SCREAMING_SNAKE_CASE = mid else: SCREAMING_SNAKE_CASE = mid SCREAMING_SNAKE_CASE = start + (end - start) / 2.0 return mid def __lowerCamelCase (UpperCAmelCase__ : float ): return x**3 - 2 * x - 5 if __name__ == "__main__": print(bisection(f, 1, 10_00)) import doctest doctest.testmod()
711
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib _lowerCamelCase : str = threading.Lock() _lowerCamelCase : Optional[logging.Handler] = None _lowerCamelCase : Any = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } _lowerCamelCase : Union[str, Any] = logging.WARNING _lowerCamelCase : List[Any] = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_VERBOSITY" , UpperCAmelCase__ ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def __lowerCamelCase (): return __name__.split("." )[0] def __lowerCamelCase (): return logging.getLogger(_get_library_name() ) def __lowerCamelCase (): global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return SCREAMING_SNAKE_CASE = logging.StreamHandler() # Set sys.stderr as stream. SCREAMING_SNAKE_CASE = sys.stderr.flush # Apply our default configuration to the library root logger. SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): global _default_handler with _lock: if not _default_handler: return SCREAMING_SNAKE_CASE = _get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) SCREAMING_SNAKE_CASE = None def __lowerCamelCase (): return log_levels def __lowerCamelCase (UpperCAmelCase__ : Optional[str] = None ): if name is None: SCREAMING_SNAKE_CASE = _get_library_name() _configure_library_root_logger() return logging.getLogger(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def __lowerCamelCase (UpperCAmelCase__ : int ): _configure_library_root_logger() _get_library_root_logger().setLevel(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): return set_verbosity(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def __lowerCamelCase (): _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : logging.Handler ): _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(UpperCAmelCase__ ) def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = False def __lowerCamelCase (): _configure_library_root_logger() SCREAMING_SNAKE_CASE = True def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: SCREAMING_SNAKE_CASE = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s" ) handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = _get_library_root_logger().handlers for handler in handlers: handler.setFormatter(UpperCAmelCase__ ) def __lowerCamelCase (self : str , *UpperCAmelCase__ : Union[str, Any] , **UpperCAmelCase__ : List[str] ): SCREAMING_SNAKE_CASE = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS" , UpperCAmelCase__ ) if no_advisory_warnings: return self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : str = warning_advice @functools.lru_cache(UpperCAmelCase__ ) def __lowerCamelCase (self : List[str] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : int ): self.warning(*UpperCAmelCase__ , **UpperCAmelCase__ ) _lowerCamelCase : Dict = warning_once class lowercase : def __init__( self : List[Any] , *_UpperCamelCase : Union[str, Any] , **_UpperCamelCase : str ) -> List[Any]: # pylint: disable=unused-argument '''simple docstring''' SCREAMING_SNAKE_CASE = args[0] if args else None def __iter__( self : Optional[Any] ) -> str: '''simple docstring''' return iter(self._iterator ) def __getattr__( self : List[str] , _UpperCamelCase : Any ) -> List[Any]: '''simple docstring''' def empty_fn(*_UpperCamelCase : List[str] , **_UpperCamelCase : Union[str, Any] ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Any ) -> Optional[Any]: '''simple docstring''' return self def __exit__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : List[str] ) -> Union[str, Any]: '''simple docstring''' return class lowercase : def __call__( self : Union[str, Any] , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : Tuple ) -> Tuple: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm(*_UpperCamelCase , **_UpperCamelCase ) else: return EmptyTqdm(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Dict , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if _tqdm_active: return tqdm_lib.tqdm.get_lock() _lowerCamelCase : Union[str, Any] = _tqdm_cls() def __lowerCamelCase (): global _tqdm_active return bool(_tqdm_active ) def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = True hf_hub_utils.enable_progress_bars() def __lowerCamelCase (): global _tqdm_active SCREAMING_SNAKE_CASE = False hf_hub_utils.disable_progress_bars()
647
0
import json import os import unittest from transformers import AutoTokenizer, GPTaTokenizer, GPTaTokenizerFast from transformers.models.gpta.tokenization_gpta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( __UpperCAmelCase , unittest.TestCase ): lowercase__ : List[Any] = GPTaTokenizer lowercase__ : int = GPTaTokenizerFast lowercase__ : str = True lowercase__ : Tuple = {"""add_prefix_space""": True} lowercase__ : Tuple = False def __snake_case( self : Optional[int] ) -> List[Any]: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", "<|endoftext|>", ] SCREAMING_SNAKE_CASE = dict(zip(lowerCAmelCase_ , range(len(lowerCAmelCase_ ) ) ) ) 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(lowerCAmelCase_ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCAmelCase_ ) ) def __snake_case( self : Union[str, Any] , **_UpperCamelCase : Any ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return GPTaTokenizer.from_pretrained(self.tmpdirname , **lowerCAmelCase_ ) def __snake_case( self : str , **_UpperCamelCase : List[str] ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return GPTaTokenizerFast.from_pretrained(self.tmpdirname , **lowerCAmelCase_ ) def __snake_case( self : Optional[int] , _UpperCamelCase : Tuple ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = "lower newer" return input_text, output_text def __snake_case( self : List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = GPTaTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) SCREAMING_SNAKE_CASE = "lower newer" SCREAMING_SNAKE_CASE = ["\u0120low", "er", "\u0120", "n", "e", "w", "er"] SCREAMING_SNAKE_CASE = tokenizer.tokenize(lowerCAmelCase_ , add_prefix_space=lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase_ ) , lowerCAmelCase_ ) def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE = self.get_tokenizer() SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(add_prefix_space=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = "lower newer" # Testing tokenization SCREAMING_SNAKE_CASE = tokenizer.tokenize(lowerCAmelCase_ , add_prefix_space=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) # Testing conversion to ids without special tokens SCREAMING_SNAKE_CASE = tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ , add_prefix_space=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = rust_tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) # Testing conversion to ids with special tokens SCREAMING_SNAKE_CASE = self.get_rust_tokenizer(add_prefix_space=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = tokenizer.encode(lowerCAmelCase_ , add_prefix_space=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = rust_tokenizer.encode(lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) # Testing the unknown token SCREAMING_SNAKE_CASE = tokens + [rust_tokenizer.unk_token] SCREAMING_SNAKE_CASE = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(lowerCAmelCase_ ) , lowerCAmelCase_ ) def __snake_case( self : List[str] , *_UpperCamelCase : Tuple , **_UpperCamelCase : Any ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : Dict , _UpperCamelCase : Dict=15 ) -> List[str]: '''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(lowerCAmelCase_ , **lowerCAmelCase_ ) # Simple input SCREAMING_SNAKE_CASE = "This is a simple input" SCREAMING_SNAKE_CASE = ["This is a simple input 1", "This is a simple input 2"] SCREAMING_SNAKE_CASE = ("This is a simple input", "This is a pair") SCREAMING_SNAKE_CASE = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests self.assertRaises(lowerCAmelCase_ , tokenizer_r.encode , lowerCAmelCase_ , max_length=lowerCAmelCase_ , padding="max_length" ) # Simple input self.assertRaises(lowerCAmelCase_ , tokenizer_r.encode_plus , lowerCAmelCase_ , max_length=lowerCAmelCase_ , padding="max_length" ) # Simple input self.assertRaises( lowerCAmelCase_ , tokenizer_r.batch_encode_plus , lowerCAmelCase_ , max_length=lowerCAmelCase_ , padding="max_length" , ) # Pair input self.assertRaises(lowerCAmelCase_ , tokenizer_r.encode , lowerCAmelCase_ , max_length=lowerCAmelCase_ , padding="max_length" ) # Pair input self.assertRaises(lowerCAmelCase_ , tokenizer_r.encode_plus , lowerCAmelCase_ , max_length=lowerCAmelCase_ , padding="max_length" ) # Pair input self.assertRaises( lowerCAmelCase_ , tokenizer_r.batch_encode_plus , lowerCAmelCase_ , max_length=lowerCAmelCase_ , padding="max_length" , ) def __snake_case( self : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = GPTaTokenizer.from_pretrained(self.tmpdirname , pad_token="<pad>" ) # Simple input SCREAMING_SNAKE_CASE = "This is a simple input" SCREAMING_SNAKE_CASE = ["This is a simple input looooooooong", "This is a simple input"] SCREAMING_SNAKE_CASE = ("This is a simple input", "This is a pair") SCREAMING_SNAKE_CASE = [ ("This is a simple input loooooong", "This is a simple input"), ("This is a simple pair loooooong", "This is a simple pair"), ] SCREAMING_SNAKE_CASE = tokenizer.pad_token_id SCREAMING_SNAKE_CASE = tokenizer(lowerCAmelCase_ , padding="max_length" , max_length=30 , return_tensors="np" ) SCREAMING_SNAKE_CASE = tokenizer(lowerCAmelCase_ , padding=lowerCAmelCase_ , truncate=lowerCAmelCase_ , return_tensors="np" ) SCREAMING_SNAKE_CASE = tokenizer(*lowerCAmelCase_ , padding="max_length" , max_length=60 , return_tensors="np" ) SCREAMING_SNAKE_CASE = tokenizer(lowerCAmelCase_ , padding=lowerCAmelCase_ , truncate=lowerCAmelCase_ , return_tensors="np" ) # s # test single string max_length padding self.assertEqual(out_s["input_ids"].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["input_ids"] ) self.assertTrue(0 in out_s["attention_mask"] ) # s2 # test automatic padding self.assertEqual(out_sa["input_ids"].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["input_ids"][0] ) self.assertFalse(0 in out_sa["attention_mask"][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["input_ids"][1] ) self.assertTrue(0 in out_sa["attention_mask"][1] ) # p # test single pair max_length padding self.assertEqual(out_p["input_ids"].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["input_ids"] ) self.assertTrue(0 in out_p["attention_mask"] ) # p2 # test automatic padding pair self.assertEqual(out_pa["input_ids"].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["input_ids"][0] ) self.assertFalse(0 in out_pa["attention_mask"][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["input_ids"][1] ) self.assertTrue(0 in out_pa["attention_mask"][1] ) def __snake_case( self : Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = "$$$" SCREAMING_SNAKE_CASE = GPTaTokenizer.from_pretrained(self.tmpdirname , bos_token=lowerCAmelCase_ , add_bos_token=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = "This is a simple input" SCREAMING_SNAKE_CASE = ["This is a simple input 1", "This is a simple input 2"] SCREAMING_SNAKE_CASE = tokenizer.bos_token_id SCREAMING_SNAKE_CASE = tokenizer(lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = tokenizer(lowerCAmelCase_ ) self.assertEqual(out_s.input_ids[0] , lowerCAmelCase_ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) SCREAMING_SNAKE_CASE = tokenizer.decode(out_s.input_ids ) SCREAMING_SNAKE_CASE = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , lowerCAmelCase_ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) def __snake_case( self : str ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.get_tokenizer(do_lower_case=lowerCAmelCase_ , add_bos_token=lowerCAmelCase_ )] for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}" ): SCREAMING_SNAKE_CASE = "Encode this." SCREAMING_SNAKE_CASE = "This one too please." SCREAMING_SNAKE_CASE = tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ ) encoded_sequence += tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = tokenizer.encode_plus( lowerCAmelCase_ , lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ , return_special_tokens_mask=lowerCAmelCase_ , ) SCREAMING_SNAKE_CASE = encoded_sequence_dict["input_ids"] SCREAMING_SNAKE_CASE = encoded_sequence_dict["special_tokens_mask"] self.assertEqual(len(lowerCAmelCase_ ) , len(lowerCAmelCase_ ) ) SCREAMING_SNAKE_CASE = [ (x if not special_tokens_mask[i] else None) for i, x in enumerate(lowerCAmelCase_ ) ] SCREAMING_SNAKE_CASE = [x for x in filtered_sequence if x is not None] self.assertEqual(lowerCAmelCase_ , lowerCAmelCase_ ) @require_tokenizers class lowercase ( unittest.TestCase ): def __snake_case( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("facebook/opt-350m" , from_slow=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = "A photo of a cat" SCREAMING_SNAKE_CASE = tokenizer.encode( lowerCAmelCase_ , ) self.assertEqual(lowerCAmelCase_ , [2, 250, 1_345, 9, 10, 4_758] ) tokenizer.save_pretrained("test_opt" ) SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("./test_opt" ) SCREAMING_SNAKE_CASE = tokenizer.encode( lowerCAmelCase_ , ) self.assertEqual(lowerCAmelCase_ , [2, 250, 1_345, 9, 10, 4_758] ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("facebook/opt-350m" , use_slow=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = "A photo of a cat" SCREAMING_SNAKE_CASE = tokenizer.encode( lowerCAmelCase_ , ) # Same as above self.assertEqual(lowerCAmelCase_ , [2, 250, 1_345, 9, 10, 4_758] ) @unittest.skip("This test is failing because of a bug in the fast tokenizer" ) def __snake_case( self : List[str] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("facebook/opt-350m" , from_slow=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE = "bos" SCREAMING_SNAKE_CASE = tokenizer.get_vocab()["bos"] SCREAMING_SNAKE_CASE = "A photo of a cat" SCREAMING_SNAKE_CASE = tokenizer.encode( lowerCAmelCase_ , ) # We changed the bos token self.assertEqual(lowerCAmelCase_ , [31_957, 250, 1_345, 9, 10, 4_758] ) tokenizer.save_pretrained("./tok" ) SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("./tok" ) self.assertTrue(tokenizer.is_fast ) SCREAMING_SNAKE_CASE = tokenizer.encode( lowerCAmelCase_ , ) self.assertEqual(lowerCAmelCase_ , [31_957, 250, 1_345, 9, 10, 4_758] )
712
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(a ) , """Tatoeba directory does not exist.""" ) class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = tempfile.mkdtemp() return TatoebaConverter(save_dir=_UpperCamelCase ) @slow def __snake_case( self : Tuple ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def __snake_case( self : Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.resolver.write_model_card("opus-mt-he-en" , dry_run=_UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
647
0
'''simple docstring''' import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class lowercase ( _UpperCamelCase ): def __init__( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int=1_024 , _UpperCamelCase : str=1_024 , _UpperCamelCase : str=3.6 ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = tokenizer SCREAMING_SNAKE_CASE = tokenizer.bos_token_id SCREAMING_SNAKE_CASE = dataset SCREAMING_SNAKE_CASE = seq_length SCREAMING_SNAKE_CASE = seq_length * chars_per_token * num_of_sequences def __iter__( self : int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = iter(self.dataset ) SCREAMING_SNAKE_CASE = True while more_examples: SCREAMING_SNAKE_CASE = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(_UpperCAmelCase )["content"] ) buffer_len += len(buffer[-1] ) except StopIteration: SCREAMING_SNAKE_CASE = False break SCREAMING_SNAKE_CASE = tokenizer(_UpperCAmelCase , truncation=_UpperCAmelCase )['''input_ids'''] SCREAMING_SNAKE_CASE = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(_UpperCAmelCase ) , self.seq_length ): SCREAMING_SNAKE_CASE = all_token_ids[i : i + self.seq_length] if len(_UpperCAmelCase ) == self.seq_length: yield torch.tensor(_UpperCAmelCase ) def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = {'''streaming''': True} SCREAMING_SNAKE_CASE = load_dataset(args.dataset_name , split="train" , **a_ ) SCREAMING_SNAKE_CASE = ConstantLengthDataset(a_ , a_ , seq_length=args.seq_length ) SCREAMING_SNAKE_CASE = DataLoader(a_ , batch_size=args.batch_size ) return eval_dataloader def __lowerCamelCase (UpperCAmelCase__ : Optional[int] ): model.eval() SCREAMING_SNAKE_CASE = [] for step, batch in enumerate(a_ ): with torch.no_grad(): SCREAMING_SNAKE_CASE = model(a_ , labels=a_ ) SCREAMING_SNAKE_CASE = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(a_ ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break SCREAMING_SNAKE_CASE = torch.mean(torch.cat(a_ ) ) try: SCREAMING_SNAKE_CASE = torch.exp(a_ ) except OverflowError: SCREAMING_SNAKE_CASE = float("inf" ) return loss.item(), perplexity.item() # Setup Accelerator _lowerCamelCase : int = Accelerator() # Parse configuration _lowerCamelCase : Union[str, Any] = HfArgumentParser(EvaluationArguments) _lowerCamelCase : Dict = parser.parse_args() set_seed(args.seed) # Logging _lowerCamelCase : Tuple = logging.getLogger(__name__) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) # Load model and tokenizer _lowerCamelCase : List[Any] = AutoModelForCausalLM.from_pretrained(args.model_ckpt) _lowerCamelCase : List[Any] = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader _lowerCamelCase : Optional[int] = create_dataloader(args) # Prepare everything with our `accelerator`. _lowerCamelCase , _lowerCamelCase : Any = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info('''Evaluating and saving model after training''') _lowerCamelCase , _lowerCamelCase : Dict = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
713
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class lowercase : def __init__( self : Any , _UpperCamelCase : Any , _UpperCamelCase : Dict=13 , _UpperCamelCase : List[Any]=64 , _UpperCamelCase : Union[str, Any]=2 , _UpperCamelCase : int=3 , _UpperCamelCase : Union[str, Any]=True , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=32 , _UpperCamelCase : str=5 , _UpperCamelCase : Tuple=4 , _UpperCamelCase : Any=37 , _UpperCamelCase : List[str]="gelu" , _UpperCamelCase : int=0.1 , _UpperCamelCase : int=0.1 , _UpperCamelCase : Optional[int]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Union[str, Any]=[1, 16, 4, 4] , _UpperCamelCase : Optional[Any]=None , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels 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 = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size SCREAMING_SNAKE_CASE = (self.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = num_patches + 1 def __snake_case( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [4, 8, 16, 32], "num_groups": 2, } return ViTHybridConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=_UpperCamelCase , ) def __snake_case( self : Dict , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : Any , _UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = ViTHybridForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( a , a , unittest.TestCase ): lowercase__ : Optional[int] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () lowercase__ : List[Any] = ( {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : Any = False lowercase__ : Optional[int] = False def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Optional[Any] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Optional[int] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = _config_zero_init(_UpperCamelCase ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(config=_UpperCamelCase ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": SCREAMING_SNAKE_CASE = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @slow def __snake_case( self : Any ) -> List[Any]: '''simple docstring''' for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = ViTHybridModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate def __snake_case( self : Optional[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384" ) SCREAMING_SNAKE_CASE = ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384" , device_map="auto" ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits # model predicts one of the 1000 ImageNet classes SCREAMING_SNAKE_CASE = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , "tabby, tabby cat" )
647
0
import torch from diffusers import CMStochasticIterativeScheduler from .test_schedulers import SchedulerCommonTest class lowercase ( _a ): lowercase__ : Any = (CMStochasticIterativeScheduler,) lowercase__ : int = 10 def __snake_case( self : Dict , **_UpperCamelCase : str ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = { "num_train_timesteps": 201, "sigma_min": 0.0_0_2, "sigma_max": 8_0.0, } config.update(**_A ) return config def __snake_case( self : Union[str, Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = 10 SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = self.scheduler_classes[0](**_A ) scheduler.set_timesteps(_A ) SCREAMING_SNAKE_CASE = scheduler.timesteps[0] SCREAMING_SNAKE_CASE = scheduler.timesteps[1] SCREAMING_SNAKE_CASE = self.dummy_sample SCREAMING_SNAKE_CASE = 0.1 * sample SCREAMING_SNAKE_CASE = scheduler.step(_A , _A , _A ).prev_sample SCREAMING_SNAKE_CASE = scheduler.step(_A , _A , _A ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def __snake_case( self : Tuple ) -> Tuple: '''simple docstring''' for timesteps in [10, 50, 100, 1_000]: self.check_over_configs(num_train_timesteps=_A ) def __snake_case( self : Dict ) -> int: '''simple docstring''' for clip_denoised in [True, False]: self.check_over_configs(clip_denoised=_A ) def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_A ) SCREAMING_SNAKE_CASE = 1 scheduler.set_timesteps(_A ) SCREAMING_SNAKE_CASE = scheduler.timesteps SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma for i, t in enumerate(_A ): # 1. scale model input SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_A , _A ) # 2. predict noise residual SCREAMING_SNAKE_CASE = model(_A , _A ) # 3. predict previous sample x_t-1 SCREAMING_SNAKE_CASE = scheduler.step(_A , _A , _A , generator=_A ).prev_sample SCREAMING_SNAKE_CASE = pred_prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_A ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 1_9_2.7_6_1_4 ) < 1e-2 assert abs(result_mean.item() - 0.2_5_1_0 ) < 1e-3 def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_A ) SCREAMING_SNAKE_CASE = [106, 0] scheduler.set_timesteps(timesteps=_A ) SCREAMING_SNAKE_CASE = scheduler.timesteps SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE = self.dummy_model() SCREAMING_SNAKE_CASE = self.dummy_sample_deter * scheduler.init_noise_sigma for t in timesteps: # 1. scale model input SCREAMING_SNAKE_CASE = scheduler.scale_model_input(_A , _A ) # 2. predict noise residual SCREAMING_SNAKE_CASE = model(_A , _A ) # 3. predict previous sample x_t-1 SCREAMING_SNAKE_CASE = scheduler.step(_A , _A , _A , generator=_A ).prev_sample SCREAMING_SNAKE_CASE = pred_prev_sample SCREAMING_SNAKE_CASE = torch.sum(torch.abs(_A ) ) SCREAMING_SNAKE_CASE = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 3_4_7.6_3_5_7 ) < 1e-2 assert abs(result_mean.item() - 0.4_5_2_7 ) < 1e-3 def __snake_case( self : List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_A ) SCREAMING_SNAKE_CASE = [39, 30, 12, 15, 0] with self.assertRaises(_A , msg="`timesteps` must be in descending order." ): scheduler.set_timesteps(timesteps=_A ) def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_A ) SCREAMING_SNAKE_CASE = [39, 30, 12, 1, 0] SCREAMING_SNAKE_CASE = len(_A ) with self.assertRaises(_A , msg="Can only pass one of `num_inference_steps` or `timesteps`." ): scheduler.set_timesteps(num_inference_steps=_A , timesteps=_A ) def __snake_case( self : Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.scheduler_classes[0] SCREAMING_SNAKE_CASE = self.get_scheduler_config() SCREAMING_SNAKE_CASE = scheduler_class(**_A ) SCREAMING_SNAKE_CASE = [scheduler.config.num_train_timesteps] with self.assertRaises( _A , msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}" , ): scheduler.set_timesteps(timesteps=_A )
714
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] ): return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : Optional[str] , UpperCAmelCase__ : Optional[str] = None ): SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else "" # apply OCR SCREAMING_SNAKE_CASE = to_pil_image(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = pil_image.size SCREAMING_SNAKE_CASE = pytesseract.image_to_data(UpperCAmelCase__ , lang=UpperCAmelCase__ , output_type="dict" , config=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates SCREAMING_SNAKE_CASE = [idx for idx, word in enumerate(UpperCAmelCase__ ) if not word.strip()] SCREAMING_SNAKE_CASE = [word for idx, word in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] SCREAMING_SNAKE_CASE = [coord for idx, coord in enumerate(UpperCAmelCase__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format SCREAMING_SNAKE_CASE = [] for x, y, w, h in zip(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = [x, y, x + w, y + h] actual_boxes.append(UpperCAmelCase__ ) # finally, normalize the bounding boxes SCREAMING_SNAKE_CASE = [] for box in actual_boxes: normalized_boxes.append(normalize_box(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowercase ( a ): lowercase__ : Optional[int] = ["""pixel_values"""] def __init__( self : int , _UpperCamelCase : bool = True , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : bool = True , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = "" , **_UpperCamelCase : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE = size if size is not None else {"height": 224, "width": 224} SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = apply_ocr SCREAMING_SNAKE_CASE = ocr_lang SCREAMING_SNAKE_CASE = tesseract_config def __snake_case( self : List[Any] , _UpperCamelCase : np.ndarray , _UpperCamelCase : Dict[str, int] , _UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCamelCase : Any , ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = get_size_dict(_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()}" ) SCREAMING_SNAKE_CASE = (size["height"], size["width"]) return resize(_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase , data_format=_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : Tuple , _UpperCamelCase : ImageInput , _UpperCamelCase : bool = None , _UpperCamelCase : Dict[str, int] = None , _UpperCamelCase : PILImageResampling = None , _UpperCamelCase : bool = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , _UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCamelCase : str , ) -> PIL.Image.Image: '''simple docstring''' SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(_UpperCamelCase ) SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = apply_ocr if apply_ocr is not None else self.apply_ocr SCREAMING_SNAKE_CASE = ocr_lang if ocr_lang is not None else self.ocr_lang SCREAMING_SNAKE_CASE = tesseract_config if tesseract_config is not None else self.tesseract_config SCREAMING_SNAKE_CASE = make_list_of_images(_UpperCamelCase ) if not valid_images(_UpperCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(_UpperCamelCase ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for image in images: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = apply_tesseract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) words_batch.append(_UpperCamelCase ) boxes_batch.append(_UpperCamelCase ) if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=_UpperCamelCase , size=_UpperCamelCase , resample=_UpperCamelCase ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) SCREAMING_SNAKE_CASE = [flip_channel_order(_UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(_UpperCamelCase , _UpperCamelCase ) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={"pixel_values": images} , tensor_type=_UpperCamelCase ) if apply_ocr: SCREAMING_SNAKE_CASE = words_batch SCREAMING_SNAKE_CASE = boxes_batch return data
647
0
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = filter(lambda UpperCAmelCase__ : p.requires_grad , model.parameters() ) SCREAMING_SNAKE_CASE = sum([np.prod(p.size() ) for p in model_parameters] ) return params _lowerCamelCase : Optional[int] = logging.getLogger(__name__) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] ): if metric == "rouge2": SCREAMING_SNAKE_CASE = "{val_avg_rouge2:.4f}-{step_count}" elif metric == "bleu": SCREAMING_SNAKE_CASE = "{val_avg_bleu:.4f}-{step_count}" elif metric == "em": SCREAMING_SNAKE_CASE = "{val_avg_em:.4f}-{step_count}" elif metric == "loss": SCREAMING_SNAKE_CASE = "{val_avg_loss:.4f}-{step_count}" else: raise NotImplementedError( F"seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this" " function." ) SCREAMING_SNAKE_CASE = ModelCheckpoint( dirpath=UpperCAmelCase__ , filename=UpperCAmelCase__ , monitor=F"val_{metric}" , mode="max" , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : str ): return EarlyStopping( monitor=F"val_{metric}" , mode="min" if "loss" in metric else "max" , patience=UpperCAmelCase__ , verbose=UpperCAmelCase__ , ) class lowercase ( pl.Callback ): def __snake_case( self : Optional[int] , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = {F"lr_group_{i}": param["lr"] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(__UpperCamelCase ) @rank_zero_only def __snake_case( self : Union[str, Any] , _UpperCamelCase : pl.Trainer , _UpperCamelCase : pl.LightningModule , _UpperCamelCase : str , _UpperCamelCase : List[str]=True ) -> Any: '''simple docstring''' logger.info(F"***** {type_path} results at step {trainer.global_step:05d} *****" ) SCREAMING_SNAKE_CASE = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ["log", "progress_bar", "preds"]} ) # Log results SCREAMING_SNAKE_CASE = Path(pl_module.hparams.output_dir ) if type_path == "test": SCREAMING_SNAKE_CASE = od / "test_results.txt" SCREAMING_SNAKE_CASE = od / "test_generations.txt" else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. SCREAMING_SNAKE_CASE = od / F"{type_path}_results/{trainer.global_step:05d}.txt" SCREAMING_SNAKE_CASE = od / F"{type_path}_generations/{trainer.global_step:05d}.txt" results_file.parent.mkdir(exist_ok=__UpperCamelCase ) generations_file.parent.mkdir(exist_ok=__UpperCamelCase ) with open(__UpperCamelCase , "a+" ) as writer: for key in sorted(__UpperCamelCase ): if key in ["log", "progress_bar", "preds"]: continue SCREAMING_SNAKE_CASE = metrics[key] if isinstance(__UpperCamelCase , torch.Tensor ): SCREAMING_SNAKE_CASE = val.item() SCREAMING_SNAKE_CASE = F"{key}: {val:.6f}\n" writer.write(__UpperCamelCase ) if not save_generations: return if "preds" in metrics: SCREAMING_SNAKE_CASE = "\n".join(metrics["preds"] ) generations_file.open("w+" ).write(__UpperCamelCase ) @rank_zero_only def __snake_case( self : Optional[int] , _UpperCamelCase : Optional[int] , _UpperCamelCase : List[str] ) -> Tuple: '''simple docstring''' try: SCREAMING_SNAKE_CASE = pl_module.model.model.num_parameters() except AttributeError: SCREAMING_SNAKE_CASE = pl_module.model.num_parameters() SCREAMING_SNAKE_CASE = count_trainable_parameters(__UpperCamelCase ) # mp stands for million parameters trainer.logger.log_metrics({"n_params": npars, "mp": npars / 1e6, "grad_mp": n_trainable_pars / 1e6} ) @rank_zero_only def __snake_case( self : str , _UpperCamelCase : pl.Trainer , _UpperCamelCase : pl.LightningModule ) -> Tuple: '''simple docstring''' save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(__UpperCamelCase , __UpperCamelCase , "test" ) @rank_zero_only def __snake_case( self : List[str] , _UpperCamelCase : pl.Trainer , _UpperCamelCase : int ) -> Any: '''simple docstring''' save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
715
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetaImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Optional[int] , _UpperCamelCase : Any , _UpperCamelCase : Dict=7 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Optional[int]=30 , _UpperCamelCase : List[Any]=400 , _UpperCamelCase : Dict=True , _UpperCamelCase : Union[str, Any]=None , _UpperCamelCase : Any=True , _UpperCamelCase : List[Any]=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=[0.5, 0.5, 0.5] , _UpperCamelCase : Tuple=True , _UpperCamelCase : List[Any]=1 / 255 , _UpperCamelCase : Optional[Any]=True , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_pad def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __snake_case( self : Any , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any]=False ) -> List[Any]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE = image_inputs[0] if isinstance(_UpperCamelCase , Image.Image ): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.size else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * h / w ) SCREAMING_SNAKE_CASE = self.size["shortest_edge"] elif w > h: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = int(self.size["shortest_edge"] * w / h ) else: SCREAMING_SNAKE_CASE = self.size["shortest_edge"] SCREAMING_SNAKE_CASE = self.size["shortest_edge"] else: SCREAMING_SNAKE_CASE = [] for image in image_inputs: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[0] )[0] SCREAMING_SNAKE_CASE = max(_UpperCamelCase , key=lambda _UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Optional[int] = DetaImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = DetaImageProcessingTester(self ) @property def __snake_case( self : int ) -> Tuple: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_rescale" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_pad" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18, "longest_edge": 1_333} ) self.assertEqual(image_processor.do_pad , _UpperCamelCase ) def __snake_case( self : str ) -> List[Any]: '''simple docstring''' pass def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __snake_case( self : str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.image_processor_tester.get_expected_values(_UpperCamelCase , batched=_UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"image_id": 39_769, "annotations": target} # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor() SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) ) @slow def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt" , "r" ) as f: SCREAMING_SNAKE_CASE = json.loads(f.read() ) SCREAMING_SNAKE_CASE = {"file_name": "000000039769.png", "image_id": 39_769, "segments_info": target} SCREAMING_SNAKE_CASE = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic" ) # encode them SCREAMING_SNAKE_CASE = DetaImageProcessor(format="coco_panoptic" ) SCREAMING_SNAKE_CASE = image_processing(images=_UpperCamelCase , annotations=_UpperCamelCase , masks_path=_UpperCamelCase , return_tensors="pt" ) # verify pixel values SCREAMING_SNAKE_CASE = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["pixel_values"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3] , _UpperCamelCase , atol=1e-4 ) ) # verify area SCREAMING_SNAKE_CASE = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding["labels"][0]["area"] , _UpperCamelCase ) ) # verify boxes SCREAMING_SNAKE_CASE = torch.Size([6, 4] ) self.assertEqual(encoding["labels"][0]["boxes"].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0] , _UpperCamelCase , atol=1e-3 ) ) # verify image_id SCREAMING_SNAKE_CASE = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"] , _UpperCamelCase ) ) # verify is_crowd SCREAMING_SNAKE_CASE = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"] , _UpperCamelCase ) ) # verify class_labels SCREAMING_SNAKE_CASE = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"] , _UpperCamelCase ) ) # verify masks SCREAMING_SNAKE_CASE = 822_873 self.assertEqual(encoding["labels"][0]["masks"].sum().item() , _UpperCamelCase ) # verify orig_size SCREAMING_SNAKE_CASE = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"] , _UpperCamelCase ) ) # verify size SCREAMING_SNAKE_CASE = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["labels"][0]["size"] , _UpperCamelCase ) )
647
0
from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) _lowerCamelCase : List[Any] = { '''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 ( a ): lowercase__ : Dict = """umt5""" lowercase__ : List[Any] = ["""past_key_values"""] def __init__( self : int , _UpperCamelCase : Any=250_112 , _UpperCamelCase : int=512 , _UpperCamelCase : Any=64 , _UpperCamelCase : Tuple=1_024 , _UpperCamelCase : Union[str, Any]=8 , _UpperCamelCase : List[str]=None , _UpperCamelCase : List[Any]=6 , _UpperCamelCase : Optional[int]=32 , _UpperCamelCase : Tuple=128 , _UpperCamelCase : Union[str, Any]=0.1 , _UpperCamelCase : List[Any]=1e-6 , _UpperCamelCase : int=1.0 , _UpperCamelCase : Dict="gated-gelu" , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=True , _UpperCamelCase : Union[str, Any]="T5Tokenizer" , _UpperCamelCase : List[Any]=True , _UpperCamelCase : str=0 , _UpperCamelCase : Dict=1 , _UpperCamelCase : str=0 , **_UpperCamelCase : Tuple , ) -> Dict: '''simple docstring''' super().__init__( is_encoder_decoder=_UpperCamelCase , tokenizer_class=_UpperCamelCase , tie_word_embeddings=_UpperCamelCase , pad_token_id=_UpperCamelCase , eos_token_id=_UpperCamelCase , decoder_start_token_id=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = vocab_size SCREAMING_SNAKE_CASE = d_model SCREAMING_SNAKE_CASE = d_kv SCREAMING_SNAKE_CASE = d_ff SCREAMING_SNAKE_CASE = num_layers SCREAMING_SNAKE_CASE = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry SCREAMING_SNAKE_CASE = num_heads SCREAMING_SNAKE_CASE = relative_attention_num_buckets SCREAMING_SNAKE_CASE = relative_attention_max_distance SCREAMING_SNAKE_CASE = dropout_rate SCREAMING_SNAKE_CASE = layer_norm_epsilon SCREAMING_SNAKE_CASE = initializer_factor SCREAMING_SNAKE_CASE = feed_forward_proj SCREAMING_SNAKE_CASE = use_cache SCREAMING_SNAKE_CASE = self.feed_forward_proj.split("-" ) SCREAMING_SNAKE_CASE = act_info[-1] SCREAMING_SNAKE_CASE = act_info[0] == "gated" if len(_UpperCamelCase ) > 1 and act_info[0] != "gated" or len(_UpperCamelCase ) > 2: raise ValueError( F"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer." "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. " "\'gated-gelu\' or \'relu\'" ) if feed_forward_proj == "gated-gelu": SCREAMING_SNAKE_CASE = "gelu_new" @property def __snake_case( self : List[Any] ) -> Tuple: '''simple docstring''' return self.d_model @property def __snake_case( self : Union[str, Any] ) -> int: '''simple docstring''' return self.num_heads @property def __snake_case( self : Dict ) -> Union[str, Any]: '''simple docstring''' return self.num_layers class lowercase ( a ): @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = { "input_ids": {0: "batch", 1: "encoder_sequence"}, "attention_mask": {0: "batch", 1: "encoder_sequence"}, } if self.use_past: SCREAMING_SNAKE_CASE = "past_encoder_sequence + sequence" SCREAMING_SNAKE_CASE = {0: "batch"} SCREAMING_SNAKE_CASE = {0: "batch", 1: "past_decoder_sequence + sequence"} else: SCREAMING_SNAKE_CASE = {0: "batch", 1: "decoder_sequence"} SCREAMING_SNAKE_CASE = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(_UpperCamelCase , direction="inputs" ) return common_inputs @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset def __snake_case( self : List[str] ) -> Tuple: '''simple docstring''' return 13 @property def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' return 5e-4
716
from typing import Dict, List, Optional, Union import numpy as np from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) class lowercase ( a ): def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : float , **_UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = feature_size SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = padding_value SCREAMING_SNAKE_CASE = kwargs.pop("padding_side" , "right" ) SCREAMING_SNAKE_CASE = kwargs.pop("return_attention_mask" , _UpperCamelCase ) super().__init__(**_UpperCamelCase ) def __snake_case( self : List[Any] , _UpperCamelCase : Union[ BatchFeature, List[BatchFeature], Dict[str, BatchFeature], Dict[str, List[BatchFeature]], List[Dict[str, BatchFeature]], ] , _UpperCamelCase : Union[bool, str, PaddingStrategy] = True , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , ) -> BatchFeature: '''simple docstring''' if isinstance(_UpperCamelCase , (list, tuple) ) and isinstance(processed_features[0] , (dict, BatchFeature) ): SCREAMING_SNAKE_CASE = { key: [example[key] for example in processed_features] for key in processed_features[0].keys() } # The model's main input name, usually `input_values`, has be passed for padding if self.model_input_names[0] not in processed_features: raise ValueError( "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`" F" to this method that includes {self.model_input_names[0]}, but you provided" F" {list(processed_features.keys() )}" ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = ( return_attention_mask if return_attention_mask is not None else self.return_attention_mask ) if len(_UpperCamelCase ) == 0: if return_attention_mask: SCREAMING_SNAKE_CASE = [] return processed_features # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays # and rebuild them afterwards if no return_tensors is specified # Note that we lose the specific device the tensor may be on for PyTorch SCREAMING_SNAKE_CASE = required_input[0] if isinstance(_UpperCamelCase , (list, tuple) ): # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element. SCREAMING_SNAKE_CASE = 0 while len(required_input[index] ) == 0: index += 1 if index < len(_UpperCamelCase ): SCREAMING_SNAKE_CASE = required_input[index][0] if return_tensors is None: if is_tf_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "tf" elif is_torch_tensor(_UpperCamelCase ): SCREAMING_SNAKE_CASE = "pt" elif isinstance(_UpperCamelCase , (int, float, list, tuple, np.ndarray) ): SCREAMING_SNAKE_CASE = "np" else: raise ValueError( F"type of {first_element} unknown: {type(_UpperCamelCase )}. " "Should be one of a python, numpy, pytorch or tensorflow object." ) for key, value in processed_features.items(): if isinstance(value[0] , (int, float) ): SCREAMING_SNAKE_CASE = to_numpy(_UpperCamelCase ) else: SCREAMING_SNAKE_CASE = [to_numpy(_UpperCamelCase ) for v in value] # Convert padding_strategy in PaddingStrategy SCREAMING_SNAKE_CASE = self._get_padding_strategies(padding=_UpperCamelCase , max_length=_UpperCamelCase ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if not all(len(_UpperCamelCase ) == batch_size for v in processed_features.values() ): raise ValueError("Some items in the output dictionary have a different batch size than others." ) SCREAMING_SNAKE_CASE = [] for i in range(_UpperCamelCase ): SCREAMING_SNAKE_CASE = {k: v[i] for k, v in processed_features.items()} # truncation SCREAMING_SNAKE_CASE = self._truncate( _UpperCamelCase , max_length=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) truncated_inputs.append(_UpperCamelCase ) if padding_strategy == PaddingStrategy.LONGEST: # make sure that `max_length` cannot be longer than the longest truncated length SCREAMING_SNAKE_CASE = max(len(input_slice[self.model_input_names[0]] ) for input_slice in truncated_inputs ) SCREAMING_SNAKE_CASE = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE = {} for i in range(_UpperCamelCase ): # padding SCREAMING_SNAKE_CASE = self._pad( truncated_inputs[i] , max_length=_UpperCamelCase , padding_strategy=_UpperCamelCase , pad_to_multiple_of=_UpperCamelCase , return_attention_mask=_UpperCamelCase , ) for key, value in outputs.items(): if key not in batch_outputs: SCREAMING_SNAKE_CASE = [] if value.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = value.astype(np.floataa ) batch_outputs[key].append(_UpperCamelCase ) return BatchFeature(_UpperCamelCase , tensor_type=_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> dict: '''simple docstring''' SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] if padding_strategy == PaddingStrategy.LONGEST: SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_UpperCamelCase ) < max_length if return_attention_mask and "attention_mask" not in processed_features: SCREAMING_SNAKE_CASE = np.ones(len(_UpperCamelCase ) , dtype=np.intaa ) if needs_to_be_padded: SCREAMING_SNAKE_CASE = max_length - len(_UpperCamelCase ) if self.padding_side == "right": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (0, difference) ) SCREAMING_SNAKE_CASE = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) elif self.padding_side == "left": if return_attention_mask: SCREAMING_SNAKE_CASE = np.pad( processed_features["attention_mask"] , (difference, 0) ) SCREAMING_SNAKE_CASE = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0) SCREAMING_SNAKE_CASE = np.pad( _UpperCamelCase , _UpperCamelCase , "constant" , constant_values=self.padding_value ) else: raise ValueError("Invalid padding strategy:" + str(self.padding_side ) ) return processed_features def __snake_case( self : Dict , _UpperCamelCase : Union[Dict[str, np.ndarray], BatchFeature] , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[bool] = None , ) -> Optional[int]: '''simple docstring''' if not truncation: return processed_features elif truncation and max_length is None: raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined." ) SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]] # find `max_length` that fits `pad_to_multiple_of` if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0): SCREAMING_SNAKE_CASE = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) > max_length if needs_to_be_truncated: SCREAMING_SNAKE_CASE = processed_features[self.model_input_names[0]][:max_length] if "attention_mask" in processed_features: SCREAMING_SNAKE_CASE = processed_features["attention_mask"][:max_length] return processed_features def __snake_case( self : Optional[Any] , _UpperCamelCase : int=False , _UpperCamelCase : Tuple=None ) -> Tuple: '''simple docstring''' if padding is not False: if padding is True: SCREAMING_SNAKE_CASE = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch elif not isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = PaddingStrategy(_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE = padding else: SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD # Set max length if needed if max_length is None: if padding_strategy == PaddingStrategy.MAX_LENGTH: raise ValueError( F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined" ) # Test if we have a padding value if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None): raise ValueError( "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use" " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`." ) return padding_strategy
647
0
import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, 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 transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class lowercase ( __UpperCAmelCase ): def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_UpperCamelCase , "hidden_sizes" ) ) self.parent.assertTrue(hasattr(_UpperCamelCase , "num_attention_heads" ) ) self.parent.assertTrue(hasattr(_UpperCamelCase , "num_encoder_blocks" ) ) class lowercase : def __init__( self : Optional[int] , _UpperCamelCase : Dict , _UpperCamelCase : Any=13 , _UpperCamelCase : Any=64 , _UpperCamelCase : Optional[int]=3 , _UpperCamelCase : Dict=4 , _UpperCamelCase : Dict=[2, 2, 2, 2] , _UpperCamelCase : Optional[Any]=[8, 4, 2, 1] , _UpperCamelCase : Optional[int]=[16, 32, 64, 128] , _UpperCamelCase : Dict=[1, 4, 8, 16] , _UpperCamelCase : int=[1, 2, 4, 8] , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Tuple=True , _UpperCamelCase : Union[str, Any]="gelu" , _UpperCamelCase : str=0.1 , _UpperCamelCase : Tuple=0.1 , _UpperCamelCase : Union[str, Any]=0.0_2 , _UpperCamelCase : List[str]=3 , _UpperCamelCase : Tuple=None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = num_encoder_blocks SCREAMING_SNAKE_CASE = sr_ratios SCREAMING_SNAKE_CASE = depths SCREAMING_SNAKE_CASE = hidden_sizes SCREAMING_SNAKE_CASE = downsampling_rates SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = num_labels SCREAMING_SNAKE_CASE = scope def __snake_case( self : Optional[int] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Optional[Any] ) -> Dict: '''simple docstring''' return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def __snake_case( self : Optional[int] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = SegformerModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) SCREAMING_SNAKE_CASE = SCREAMING_SNAKE_CASE = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def __snake_case( self : Tuple , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.num_labels SCREAMING_SNAKE_CASE = SegformerForSemanticSegmentation(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def __snake_case( self : str , _UpperCamelCase : str , _UpperCamelCase : int , _UpperCamelCase : str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = SegformerForSemanticSegmentation(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertGreater(result.loss , 0.0 ) def __snake_case( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( __UpperCAmelCase , __UpperCAmelCase , unittest.TestCase ): lowercase__ : List[str] = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) lowercase__ : str = ( { """feature-extraction""": SegformerModel, """image-classification""": SegformerForImageClassification, """image-segmentation""": SegformerForSemanticSegmentation, } if is_torch_available() else {} ) lowercase__ : int = True lowercase__ : Optional[Any] = False lowercase__ : List[str] = False lowercase__ : Union[str, Any] = False def __snake_case( self : Union[str, Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = SegformerModelTester(self ) SCREAMING_SNAKE_CASE = SegformerConfigTester(self , config_class=_UpperCamelCase ) def __snake_case( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case( self : Tuple ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : str ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*_UpperCamelCase ) def __snake_case( self : int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*_UpperCamelCase ) @unittest.skip("SegFormer does not use inputs_embeds" ) def __snake_case( self : str ) -> str: '''simple docstring''' pass @unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods" ) def __snake_case( self : Optional[int] ) -> Dict: '''simple docstring''' pass def __snake_case( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : Optional[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.attentions SCREAMING_SNAKE_CASE = sum(self.model_tester.depths ) self.assertEqual(len(_UpperCamelCase ) , _UpperCamelCase ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(_UpperCamelCase ) , _UpperCamelCase ) # verify the first attentions (first block, first layer) SCREAMING_SNAKE_CASE = (self.model_tester.image_size // 4) ** 2 SCREAMING_SNAKE_CASE = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) SCREAMING_SNAKE_CASE = (self.model_tester.image_size // 32) ** 2 SCREAMING_SNAKE_CASE = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) self.assertEqual(out_len + 1 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.attentions self.assertEqual(len(_UpperCamelCase ) , _UpperCamelCase ) # verify the first attentions (first block, first layer) SCREAMING_SNAKE_CASE = (self.model_tester.image_size // 4) ** 2 SCREAMING_SNAKE_CASE = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' def check_hidden_states_output(_UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[Any] ): SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = outputs.hidden_states SCREAMING_SNAKE_CASE = self.model_tester.num_encoder_blocks self.assertEqual(len(_UpperCamelCase ) , _UpperCamelCase ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def __snake_case( self : List[str] ) -> Any: '''simple docstring''' if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: if model_class in get_values(_UpperCamelCase ): continue SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(_UpperCamelCase , _UpperCamelCase , return_labels=_UpperCamelCase ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ).loss loss.backward() @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def __snake_case( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' pass @slow def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = SegformerModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch class lowercase ( unittest.TestCase ): @slow def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = SegformerImageProcessor( image_scale=(512, 512) , keep_ratio=_UpperCamelCase , align=_UpperCamelCase , do_random_crop=_UpperCamelCase ) SCREAMING_SNAKE_CASE = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512" ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = encoded_inputs.pixel_values.to(_UpperCamelCase ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.Size((1, model.config.num_labels, 128, 128) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [ [[-4.6_3_1_0, -5.5_2_3_2, -6.2_3_5_6], [-5.1_9_2_1, -6.1_4_4_4, -6.5_9_9_6], [-5.4_4_2_4, -6.2_7_9_0, -6.7_5_7_4]], [[-1_2.1_3_9_1, -1_3.3_1_2_2, -1_3.9_5_5_4], [-1_2.8_7_3_2, -1_3.9_3_5_2, -1_4.3_5_6_3], [-1_2.9_4_3_8, -1_3.8_2_2_6, -1_4.2_5_1_3]], [[-1_2.5_1_3_4, -1_3.4_6_8_6, -1_4.4_9_1_5], [-1_2.8_6_6_9, -1_4.4_3_4_3, -1_4.7_7_5_8], [-1_3.2_5_2_3, -1_4.5_8_1_9, -1_5.0_6_9_4]], ] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow def __snake_case( self : List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = SegformerImageProcessor( image_scale=(512, 512) , keep_ratio=_UpperCamelCase , align=_UpperCamelCase , do_random_crop=_UpperCamelCase ) SCREAMING_SNAKE_CASE = SegformerForSemanticSegmentation.from_pretrained( "nvidia/segformer-b1-finetuned-cityscapes-1024-1024" ).to(_UpperCamelCase ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = encoded_inputs.pixel_values.to(_UpperCamelCase ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.Size((1, model.config.num_labels, 128, 128) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor( [ [[-1_3.5_7_4_8, -1_3.9_1_1_1, -1_2.6_5_0_0], [-1_4.3_5_0_0, -1_5.3_6_8_3, -1_4.2_3_2_8], [-1_4.7_5_3_2, -1_6.0_4_2_4, -1_5.6_0_8_7]], [[-1_7.1_6_5_1, -1_5.8_7_2_5, -1_2.9_6_5_3], [-1_7.2_5_8_0, -1_7.3_7_1_8, -1_4.8_2_2_3], [-1_6.6_0_5_8, -1_6.8_7_8_3, -1_6.7_4_5_2]], [[-3.6_4_5_6, -3.0_2_0_9, -1.4_2_0_3], [-3.0_7_9_7, -3.1_9_5_9, -2.0_0_0_0], [-1.8_7_5_7, -1.9_2_1_7, -1.6_9_9_7]], ] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , _UpperCamelCase , atol=1e-1 ) ) @slow def __snake_case( self : List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = SegformerImageProcessor( image_scale=(512, 512) , keep_ratio=_UpperCamelCase , align=_UpperCamelCase , do_random_crop=_UpperCamelCase ) SCREAMING_SNAKE_CASE = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512" ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = encoded_inputs.pixel_values.to(_UpperCamelCase ) with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) SCREAMING_SNAKE_CASE = outputs.logits.detach().cpu() SCREAMING_SNAKE_CASE = image_processor.post_process_semantic_segmentation(outputs=_UpperCamelCase , target_sizes=[(500, 300)] ) SCREAMING_SNAKE_CASE = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = image_processor.post_process_semantic_segmentation(outputs=_UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.Size((128, 128) ) self.assertEqual(segmentation[0].shape , _UpperCamelCase )
717
import functools def __lowerCamelCase (UpperCAmelCase__ : list[int] , UpperCAmelCase__ : list[int] ): # 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" ) SCREAMING_SNAKE_CASE = set(UpperCAmelCase__ ) @functools.cache def dynamic_programming(UpperCAmelCase__ : int ) -> 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()
647
0
from typing import TYPE_CHECKING from ...utils import _LazyModule _lowerCamelCase : str = {"processing_wav2vec2_with_lm": ["Wav2Vec2ProcessorWithLM"]} if TYPE_CHECKING: from .processing_wavaveca_with_lm import WavaVecaProcessorWithLM else: import sys _lowerCamelCase : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
718
from __future__ import annotations import math def __lowerCamelCase (UpperCAmelCase__ : 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(UpperCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCamelCase : Tuple = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def __lowerCamelCase (UpperCAmelCase__ : int ): if not isinstance(UpperCAmelCase__ , UpperCAmelCase__ ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) SCREAMING_SNAKE_CASE = [] for num in range(len(UpperCAmelCase__ ) ): SCREAMING_SNAKE_CASE = 0 while 2 * i * i <= odd_composites[num]: SCREAMING_SNAKE_CASE = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def __lowerCamelCase (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
647
0
def __lowerCamelCase (UpperCAmelCase__ : list[list[int]] , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : set ): '''simple docstring''' SCREAMING_SNAKE_CASE = len(snake_case_ ), len(grid[0] ) if ( min(snake_case_ , snake_case_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) SCREAMING_SNAKE_CASE = 0 count += depth_first_search(snake_case_ , row + 1 , snake_case_ , snake_case_ ) count += depth_first_search(snake_case_ , row - 1 , snake_case_ , snake_case_ ) count += depth_first_search(snake_case_ , snake_case_ , col + 1 , snake_case_ ) count += depth_first_search(snake_case_ , snake_case_ , col - 1 , snake_case_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
719
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any]=7 , _UpperCamelCase : Any=3 , _UpperCamelCase : str=18 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Optional[int]=400 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : int=True , _UpperCamelCase : Optional[int]=[0.5, 0.5, 0.5] , _UpperCamelCase : List[str]=[0.5, 0.5, 0.5] , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = size if size is not None else {"height": 18, "width": 18} SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = min_resolution SCREAMING_SNAKE_CASE = max_resolution SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean SCREAMING_SNAKE_CASE = image_std def __snake_case( self : List[Any] ) -> Any: '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class lowercase ( a , unittest.TestCase ): lowercase__ : Any = DPTImageProcessor if is_vision_available() else None def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DPTImageProcessingTester(self ) @property def __snake_case( self : List[Any] ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __snake_case( self : str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , "image_mean" ) ) self.assertTrue(hasattr(_UpperCamelCase , "image_std" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_normalize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "do_resize" ) ) self.assertTrue(hasattr(_UpperCamelCase , "size" ) ) def __snake_case( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"height": 18, "width": 18} ) SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"height": 42, "width": 42} ) def __snake_case( self : Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) def __snake_case( self : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) # Test batched SCREAMING_SNAKE_CASE = image_processing(_UpperCamelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , )
647
0
import inspect import unittest import warnings from transformers import DeiTConfig from transformers.models.auto import get_values from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_MAPPING, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, ) from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class lowercase : def __init__( self : List[str] , _UpperCamelCase : str , _UpperCamelCase : int=13 , _UpperCamelCase : Tuple=30 , _UpperCamelCase : Dict=2 , _UpperCamelCase : Optional[int]=3 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : str=32 , _UpperCamelCase : int=5 , _UpperCamelCase : Dict=4 , _UpperCamelCase : Dict=37 , _UpperCamelCase : int="gelu" , _UpperCamelCase : Optional[int]=0.1 , _UpperCamelCase : Any=0.1 , _UpperCamelCase : Union[str, Any]=10 , _UpperCamelCase : Tuple=0.0_2 , _UpperCamelCase : Tuple=3 , _UpperCamelCase : Tuple=None , _UpperCamelCase : Optional[Any]=2 , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = parent SCREAMING_SNAKE_CASE = batch_size SCREAMING_SNAKE_CASE = image_size SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = num_channels SCREAMING_SNAKE_CASE = is_training SCREAMING_SNAKE_CASE = use_labels 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 = type_sequence_label_size SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = scope SCREAMING_SNAKE_CASE = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE = num_patches + 2 def __snake_case( self : str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = None if self.use_labels: SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def __snake_case( self : Tuple ) -> List[str]: '''simple docstring''' return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __snake_case( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] , _UpperCamelCase : Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = DeiTModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case( self : List[str] , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = DeiTForMaskedImageModeling(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = DeiTForMaskedImageModeling(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __snake_case( self : Optional[int] , _UpperCamelCase : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : int ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.type_sequence_label_size SCREAMING_SNAKE_CASE = DeiTForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = DeiTForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = config_and_inputs SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowercase ( lowercase_ , lowercase_ , unittest.TestCase ): lowercase__ : str = ( ( DeiTModel, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, ) if is_torch_available() else () ) lowercase__ : Optional[int] = ( { '''feature-extraction''': DeiTModel, '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher), } if is_torch_available() else {} ) lowercase__ : Optional[int] = False lowercase__ : Dict = False lowercase__ : str = False def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = DeiTModelTester(self ) SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="DeiT does not use inputs_embeds" ) def __snake_case( self : Any ) -> Union[str, Any]: '''simple docstring''' pass def __snake_case( self : Optional[int] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def __snake_case( self : Tuple ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def __snake_case( self : Optional[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def __snake_case( self : List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCamelCase ) def __snake_case( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : List[Any]=False ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = super()._prepare_for_class(_UpperCamelCase , _UpperCamelCase , return_labels=_UpperCamelCase ) if return_labels: if model_class.__name__ == "DeiTForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def __snake_case( self : Optional[int] ) -> List[Any]: '''simple docstring''' if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: # DeiTForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(_UpperCamelCase ) or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(_UpperCamelCase , _UpperCamelCase , return_labels=_UpperCamelCase ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ).loss loss.backward() def __snake_case( self : List[str] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = True for model_class in self.all_model_classes: if model_class in get_values(_UpperCamelCase ) or not model_class.supports_gradient_checkpointing: continue # DeiTForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "DeiTForImageClassificationWithTeacher": continue SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.gradient_checkpointing_enable() model.to(_UpperCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(_UpperCamelCase , _UpperCamelCase , return_labels=_UpperCamelCase ) SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ).loss loss.backward() def __snake_case( self : List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE = [ {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float}, {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long}, {"title": "regression", "num_labels": 1, "dtype": torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(_UpperCamelCase ), *get_values(_UpperCamelCase ), ] or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=F"Testing {model_class} with {problem_type['title']}" ): SCREAMING_SNAKE_CASE = problem_type["title"] SCREAMING_SNAKE_CASE = problem_type["num_labels"] SCREAMING_SNAKE_CASE = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.train() SCREAMING_SNAKE_CASE = self._prepare_for_class(_UpperCamelCase , _UpperCamelCase , return_labels=_UpperCamelCase ) if problem_type["num_labels"] > 1: SCREAMING_SNAKE_CASE = inputs["labels"].unsqueeze(1 ).repeat(1 , problem_type["num_labels"] ) SCREAMING_SNAKE_CASE = inputs["labels"].to(problem_type["dtype"] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=_UpperCamelCase ) as warning_list: SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( F"Something is going wrong in the regression problem: intercepted {w.message}" ) loss.backward() @slow def __snake_case( self : List[Any] ) -> Optional[Any]: '''simple docstring''' for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE = DeiTModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __lowerCamelCase (): SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowercase ( unittest.TestCase ): @cached_property def __snake_case( self : Tuple ) -> Optional[int]: '''simple docstring''' return ( DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" ) if is_vision_available() else None ) @slow def __snake_case( self : Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" ).to( _UpperCamelCase ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE = model(**_UpperCamelCase ) # verify the logits SCREAMING_SNAKE_CASE = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) SCREAMING_SNAKE_CASE = torch.tensor([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow @require_accelerate @require_torch_gpu def __snake_case( self : List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = DeiTModel.from_pretrained( "facebook/deit-base-distilled-patch16-224" , torch_dtype=torch.floataa , device_map="auto" ) SCREAMING_SNAKE_CASE = self.default_image_processor SCREAMING_SNAKE_CASE = prepare_img() SCREAMING_SNAKE_CASE = image_processor(images=_UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE = inputs.pixel_values.to(_UpperCamelCase ) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE = model(_UpperCamelCase )
720
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def __lowerCamelCase (UpperCAmelCase__ : Any , UpperCAmelCase__ : Dict=False ): SCREAMING_SNAKE_CASE = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"module.blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"module.blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append( (F"module.blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"module.blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"module.blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"module.blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ("module.cls_token", "vit.embeddings.cls_token"), ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"), ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"), ("module.pos_embed", "vit.embeddings.position_embeddings"), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("module.norm.weight", "layernorm.weight"), ("module.norm.bias", "layernorm.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) return rename_keys def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : str=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE = "" else: SCREAMING_SNAKE_CASE = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE = state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE = in_proj_bias[-config.hidden_size :] def __lowerCamelCase (UpperCAmelCase__ : Tuple ): SCREAMING_SNAKE_CASE = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : Any ): # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. SCREAMING_SNAKE_CASE = [ "module.fc.fc1.weight", "module.fc.fc1.bias", "module.fc.bn1.weight", "module.fc.bn1.bias", "module.fc.bn1.running_mean", "module.fc.bn1.running_var", "module.fc.bn1.num_batches_tracked", "module.fc.fc2.weight", "module.fc.fc2.bias", "module.fc.bn2.weight", "module.fc.bn2.bias", "module.fc.bn2.running_mean", "module.fc.bn2.running_var", "module.fc.bn2.num_batches_tracked", "module.fc.fc3.weight", "module.fc.fc3.bias", ] for k in ignore_keys: state_dict.pop(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = dct.pop(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = val def __lowerCamelCase (UpperCAmelCase__ : Tuple , UpperCAmelCase__ : str ): SCREAMING_SNAKE_CASE = ViTMSNConfig() SCREAMING_SNAKE_CASE = 1_0_0_0 SCREAMING_SNAKE_CASE = "datasets/huggingface/label-files" SCREAMING_SNAKE_CASE = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(UpperCAmelCase__ , UpperCAmelCase__ ) , "r" ) ) SCREAMING_SNAKE_CASE = {int(UpperCAmelCase__ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE = idalabel SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = 3_8_4 SCREAMING_SNAKE_CASE = 1_5_3_6 SCREAMING_SNAKE_CASE = 6 elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = 4 elif "l7" in checkpoint_url: SCREAMING_SNAKE_CASE = 7 SCREAMING_SNAKE_CASE = 1_0_2_4 SCREAMING_SNAKE_CASE = 4_0_9_6 SCREAMING_SNAKE_CASE = 2_4 SCREAMING_SNAKE_CASE = 1_6 SCREAMING_SNAKE_CASE = 0.1 SCREAMING_SNAKE_CASE = ViTMSNModel(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = torch.hub.load_state_dict_from_url(UpperCAmelCase__ , map_location="cpu" )["target_encoder"] SCREAMING_SNAKE_CASE = ViTImageProcessor(size=config.image_size ) remove_projection_head(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = create_rename_keys(UpperCAmelCase__ , base_model=UpperCAmelCase__ ) for src, dest in rename_keys: rename_key(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) read_in_q_k_v(UpperCAmelCase__ , UpperCAmelCase__ , base_model=UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE = Image.open(requests.get(UpperCAmelCase__ , stream=UpperCAmelCase__ ).raw ) SCREAMING_SNAKE_CASE = ViTImageProcessor( size=config.image_size , image_mean=UpperCAmelCase__ , image_std=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ) # forward pass torch.manual_seed(2 ) SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: SCREAMING_SNAKE_CASE = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: SCREAMING_SNAKE_CASE = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , UpperCAmelCase__ , atol=1e-4 ) print(F"Saving model 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__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
647
0
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 __lowerCamelCase(UpperCAmelCase__ : int , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[int]=1e-12 ): SCREAMING_SNAKE_CASE = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(lowerCamelCase__ , axis=1 ) , a_min=lowerCamelCase__ ) ).T SCREAMING_SNAKE_CASE = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(lowerCamelCase__ , axis=1 ) , a_min=lowerCamelCase__ ) ).T return jnp.matmul(lowerCamelCase__ , norm_emb_a.T ) class lowercase ( nn.Module ): lowercase__ : CLIPConfig lowercase__ : jnp.dtype = jnp.floataa def __snake_case( self : Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = FlaxCLIPVisionModule(self.config.vision_config ) SCREAMING_SNAKE_CASE = nn.Dense(self.config.projection_dim , use_bias=_UpperCamelCase , dtype=self.dtype ) SCREAMING_SNAKE_CASE = self.param("concept_embeds" , jax.nn.initializers.ones , (17, self.config.projection_dim) ) SCREAMING_SNAKE_CASE = self.param( "special_care_embeds" , jax.nn.initializers.ones , (3, self.config.projection_dim) ) SCREAMING_SNAKE_CASE = self.param("concept_embeds_weights" , jax.nn.initializers.ones , (17,) ) SCREAMING_SNAKE_CASE = self.param("special_care_embeds_weights" , jax.nn.initializers.ones , (3,) ) def __call__( self : str , _UpperCamelCase : str ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.vision_model(_UpperCamelCase )[1] SCREAMING_SNAKE_CASE = self.visual_projection(_UpperCamelCase ) SCREAMING_SNAKE_CASE = jax_cosine_distance(_UpperCamelCase , self.special_care_embeds ) SCREAMING_SNAKE_CASE = jax_cosine_distance(_UpperCamelCase , self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs SCREAMING_SNAKE_CASE = 0.0 SCREAMING_SNAKE_CASE = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment SCREAMING_SNAKE_CASE = jnp.round(_UpperCamelCase , 3 ) SCREAMING_SNAKE_CASE = jnp.any(special_scores > 0 , axis=1 , keepdims=_UpperCamelCase ) # Use a lower threshold if an image has any special care concept SCREAMING_SNAKE_CASE = is_special_care * 0.0_1 SCREAMING_SNAKE_CASE = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment SCREAMING_SNAKE_CASE = jnp.round(_UpperCamelCase , 3 ) SCREAMING_SNAKE_CASE = jnp.any(concept_scores > 0 , axis=1 ) return has_nsfw_concepts class lowercase ( a ): lowercase__ : Dict = CLIPConfig lowercase__ : Dict = """clip_input""" lowercase__ : List[str] = FlaxStableDiffusionSafetyCheckerModule def __init__( self : Optional[int] , _UpperCamelCase : CLIPConfig , _UpperCamelCase : Optional[Tuple] = None , _UpperCamelCase : int = 0 , _UpperCamelCase : jnp.dtype = jnp.floataa , _UpperCamelCase : bool = True , **_UpperCamelCase : List[str] , ) -> List[str]: '''simple docstring''' if input_shape is None: SCREAMING_SNAKE_CASE = (1, 224, 224, 3) SCREAMING_SNAKE_CASE = self.module_class(config=_UpperCamelCase , dtype=_UpperCamelCase , **_UpperCamelCase ) super().__init__(_UpperCamelCase , _UpperCamelCase , input_shape=_UpperCamelCase , seed=_UpperCamelCase , dtype=_UpperCamelCase , _do_init=_do_init ) def __snake_case( self : List[Any] , _UpperCamelCase : jax.random.KeyArray , _UpperCamelCase : Tuple , _UpperCamelCase : FrozenDict = None ) -> FrozenDict: '''simple docstring''' SCREAMING_SNAKE_CASE = jax.random.normal(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = jax.random.split(_UpperCamelCase ) SCREAMING_SNAKE_CASE = {"params": params_rng, "dropout": dropout_rng} SCREAMING_SNAKE_CASE = self.module.init(_UpperCamelCase , _UpperCamelCase )["params"] return random_params def __call__( self : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : dict = None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = jnp.transpose(_UpperCamelCase , (0, 2, 3, 1) ) return self.module.apply( {"params": params or self.params} , jnp.array(_UpperCamelCase , dtype=jnp.floataa ) , rngs={} , )
721
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : List[Any] = '''▁''' _lowerCamelCase : Optional[int] = {'''vocab_file''': '''prophetnet.tokenizer'''} _lowerCamelCase : str = { '''vocab_file''': { '''microsoft/xprophetnet-large-wiki100-cased''': ( '''https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer''' ), } } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': {'''do_lower_case''': False}, } _lowerCamelCase : Optional[Any] = { '''microsoft/xprophetnet-large-wiki100-cased''': 5_12, } def __lowerCamelCase (UpperCAmelCase__ : Optional[Any] ): SCREAMING_SNAKE_CASE = collections.OrderedDict() with open(UpperCAmelCase__ , "r" , encoding="utf-8" ) as reader: SCREAMING_SNAKE_CASE = reader.readlines() for index, token in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = token.rstrip("\n" ) SCREAMING_SNAKE_CASE = index return vocab class lowercase ( a ): lowercase__ : Optional[int] = VOCAB_FILES_NAMES lowercase__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Any = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , _UpperCamelCase : List[str] , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : str="[SEP]" , _UpperCamelCase : Dict="[SEP]" , _UpperCamelCase : Tuple="[UNK]" , _UpperCamelCase : Dict="[PAD]" , _UpperCamelCase : Any="[CLS]" , _UpperCamelCase : Optional[Any]="[MASK]" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , sep_token=_UpperCamelCase , unk_token=_UpperCamelCase , pad_token=_UpperCamelCase , cls_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = 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' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE = {"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4} for i in range(10 ): SCREAMING_SNAKE_CASE = F"[unused{i}]" SCREAMING_SNAKE_CASE = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 12 SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_UpperCamelCase ) def __getstate__( self : Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None return state def __setstate__( self : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = d try: import sentencepiece as spm except ImportError: logger.warning( "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece" " pip install sentencepiece" ) raise # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE = {} SCREAMING_SNAKE_CASE = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __snake_case( self : Dict , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_UpperCamelCase , token_ids_a=_UpperCamelCase , already_has_special_tokens=_UpperCamelCase ) if token_ids_a is None: return ([0] * len(_UpperCamelCase )) + [1] return ([0] * len(_UpperCamelCase )) + [1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : str , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Dict ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __snake_case( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {self.convert_ids_to_tokens(_UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> str: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE = self.sp_model.PieceToId(_UpperCamelCase ) # 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 : str , _UpperCamelCase : str ) -> int: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __snake_case( self : List[str] , _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Optional[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(_UpperCamelCase ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE = os.path.join( _UpperCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_UpperCamelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _UpperCamelCase ) elif not os.path.isfile(self.vocab_file ): with open(_UpperCamelCase , "wb" ) as fi: SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() fi.write(_UpperCamelCase ) return (out_vocab_file,) def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
647
0
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_torch_available(): import torch if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) @dataclass class lowercase ( a ): lowercase__ : Optional[int] = [ """no_inference""", """no_cuda""", """no_tpu""", """no_speed""", """no_memory""", """no_env_print""", """no_multi_process""", ] def __init__( self : Optional[Any] , **_UpperCamelCase : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: SCREAMING_SNAKE_CASE = deprecated_arg[3:] setattr(self , _UpperCamelCase , not kwargs.pop(_UpperCamelCase ) ) logger.warning( F"{deprecated_arg} is depreciated. Please use --no_{positive_arg} or" F" {positive_arg}={kwargs[positive_arg]}" ) SCREAMING_SNAKE_CASE = kwargs.pop("torchscript" , self.torchscript ) SCREAMING_SNAKE_CASE = kwargs.pop("torch_xla_tpu_print_metrics" , self.torch_xla_tpu_print_metrics ) SCREAMING_SNAKE_CASE = kwargs.pop("fp16_opt_level" , self.fpaa_opt_level ) super().__init__(**_UpperCamelCase ) lowercase__ : bool = field(default=a , metadata={"""help""": """Trace the models using torchscript"""} ) lowercase__ : bool = field(default=a , metadata={"""help""": """Print Xla/PyTorch tpu metrics"""} ) lowercase__ : str = field( default="""O1""" , metadata={ """help""": ( """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']. """ """See details at https://nvidia.github.io/apex/amp.html""" ) } , ) @cached_property def __snake_case( self : Any ) -> Tuple["torch.device", int]: '''simple docstring''' requires_backends(self , ["torch"] ) logger.info("PyTorch: setting up devices" ) if not self.cuda: SCREAMING_SNAKE_CASE = torch.device("cpu" ) SCREAMING_SNAKE_CASE = 0 elif is_torch_tpu_available(): SCREAMING_SNAKE_CASE = xm.xla_device() SCREAMING_SNAKE_CASE = 0 else: SCREAMING_SNAKE_CASE = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) SCREAMING_SNAKE_CASE = torch.cuda.device_count() return device, n_gpu @property def __snake_case( self : Any ) -> List[str]: '''simple docstring''' return is_torch_tpu_available() and self.tpu @property def __snake_case( self : Dict ) -> int: '''simple docstring''' requires_backends(self , ["torch"] ) # TODO(PVP): currently only single GPU is supported return torch.cuda.current_device() @property def __snake_case( self : Optional[Any] ) -> "torch.device": '''simple docstring''' requires_backends(self , ["torch"] ) return self._setup_devices[0] @property def __snake_case( self : Dict ) -> List[Any]: '''simple docstring''' requires_backends(self , ["torch"] ) return self._setup_devices[1] @property def __snake_case( self : int ) -> List[str]: '''simple docstring''' return self.n_gpu > 0
700
import numpy as np def __lowerCamelCase (UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : np.ndarray , UpperCAmelCase__ : float = 1e-12 , UpperCAmelCase__ : int = 1_0_0 , ): assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[1] # Ensure proper dimensionality. assert np.shape(UpperCAmelCase__ )[0] == np.shape(UpperCAmelCase__ )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(UpperCAmelCase__ ) == np.iscomplexobj(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = np.iscomplexobj(UpperCAmelCase__ ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(UpperCAmelCase__ , input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE = False SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1e12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE = w / np.linalg.norm(UpperCAmelCase__ ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE = np.dot(UpperCAmelCase__ , np.dot(UpperCAmelCase__ , UpperCAmelCase__ ) ) # Check convergence. SCREAMING_SNAKE_CASE = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = lambda_ if is_complex: SCREAMING_SNAKE_CASE = np.real(lambda_ ) return lambda_, vector def __lowerCamelCase (): SCREAMING_SNAKE_CASE = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]] ) SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ) SCREAMING_SNAKE_CASE = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE = np.triu(1j * complex_input_matrix , 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE = np.array([4_1, 4, 2_0] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE = real_input_matrix SCREAMING_SNAKE_CASE = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE = complex_input_matrix SCREAMING_SNAKE_CASE = complex_vector # Our implementation. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = power_iteration(UpperCAmelCase__ , UpperCAmelCase__ ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = np.linalg.eigh(UpperCAmelCase__ ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1e-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(UpperCAmelCase__ ) - np.abs(UpperCAmelCase__ ) ) <= 1e-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
647
0
import jax.numpy as jnp from ...utils import logging from ..ta.modeling_flax_ta import FlaxTaEncoderModel, FlaxTaForConditionalGeneration, FlaxTaModel from .configuration_mta import MTaConfig _lowerCamelCase : Tuple = logging.get_logger(__name__) _lowerCamelCase : Tuple = '''T5Config''' def __lowerCamelCase (UpperCAmelCase__ : jnp.array , UpperCAmelCase__ : int , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = jnp.zeros_like(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = shifted_input_ids.at[:, 1:].set(input_ids[:, :-1] ) SCREAMING_SNAKE_CASE = shifted_input_ids.at[:, 0].set(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = jnp.where(shifted_input_ids == -1_0_0 , UpperCAmelCase__ , UpperCAmelCase__ ) return shifted_input_ids class lowercase ( a ): lowercase__ : int = """mt5""" lowercase__ : Dict = MTaConfig class lowercase ( a ): lowercase__ : str = """mt5""" lowercase__ : List[str] = MTaConfig class lowercase ( a ): lowercase__ : Optional[int] = """mt5""" lowercase__ : Any = MTaConfig
701
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging _lowerCamelCase : Union[str, Any] = logging.get_logger(__name__) class lowercase ( a ): lowercase__ : Optional[Any] = ["""input_features""", """is_longer"""] def __init__( self : str , _UpperCamelCase : Optional[int]=64 , _UpperCamelCase : Any=48_000 , _UpperCamelCase : Optional[Any]=480 , _UpperCamelCase : List[Any]=10 , _UpperCamelCase : Any=1_024 , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Any=False , _UpperCamelCase : float = 0 , _UpperCamelCase : float = 14_000 , _UpperCamelCase : int = None , _UpperCamelCase : str = "fusion" , _UpperCamelCase : str = "repeatpad" , **_UpperCamelCase : Optional[Any] , ) -> Dict: '''simple docstring''' super().__init__( feature_size=_UpperCamelCase , sampling_rate=_UpperCamelCase , padding_value=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) SCREAMING_SNAKE_CASE = top_db SCREAMING_SNAKE_CASE = truncation SCREAMING_SNAKE_CASE = padding SCREAMING_SNAKE_CASE = fft_window_size SCREAMING_SNAKE_CASE = (fft_window_size >> 1) + 1 SCREAMING_SNAKE_CASE = hop_length SCREAMING_SNAKE_CASE = max_length_s SCREAMING_SNAKE_CASE = max_length_s * sampling_rate SCREAMING_SNAKE_CASE = sampling_rate SCREAMING_SNAKE_CASE = frequency_min SCREAMING_SNAKE_CASE = frequency_max SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm=_UpperCamelCase , mel_scale="htk" , ) SCREAMING_SNAKE_CASE = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=_UpperCamelCase , min_frequency=_UpperCamelCase , max_frequency=_UpperCamelCase , sampling_rate=_UpperCamelCase , norm="slaney" , mel_scale="slaney" , ) def __snake_case( self : str ) -> Dict[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __snake_case( self : Optional[Any] , _UpperCamelCase : np.array , _UpperCamelCase : Optional[np.array] = None ) -> np.ndarray: '''simple docstring''' SCREAMING_SNAKE_CASE = spectrogram( _UpperCamelCase , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=_UpperCamelCase , log_mel="dB" , ) return log_mel_spectrogram.T def __snake_case( self : str , _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk SCREAMING_SNAKE_CASE = [0] # randomly choose index for each part SCREAMING_SNAKE_CASE = np.random.choice(ranges[0] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[1] ) SCREAMING_SNAKE_CASE = np.random.choice(ranges[2] ) SCREAMING_SNAKE_CASE = mel[idx_front : idx_front + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_middle : idx_middle + chunk_frames, :] SCREAMING_SNAKE_CASE = mel[idx_back : idx_back + chunk_frames, :] SCREAMING_SNAKE_CASE = torch.tensor(mel[None, None, :] ) SCREAMING_SNAKE_CASE = torch.nn.functional.interpolate( _UpperCamelCase , size=[chunk_frames, 64] , mode="bilinear" , align_corners=_UpperCamelCase ) SCREAMING_SNAKE_CASE = mel_shrink[0][0].numpy() SCREAMING_SNAKE_CASE = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def __snake_case( self : Optional[int] , _UpperCamelCase : np.array , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": SCREAMING_SNAKE_CASE = True # random crop to max_length (for compatibility) -> this should be handled by self.pad SCREAMING_SNAKE_CASE = len(_UpperCamelCase ) - max_length SCREAMING_SNAKE_CASE = np.random.randint(0 , overflow + 1 ) SCREAMING_SNAKE_CASE = waveform[idx : idx + max_length] SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] elif truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed SCREAMING_SNAKE_CASE = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. SCREAMING_SNAKE_CASE = np.stack([mel, mel, mel, mel] , axis=0 ) SCREAMING_SNAKE_CASE = False else: SCREAMING_SNAKE_CASE = self._random_mel_fusion(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE = True else: raise NotImplementedError(F"data_truncating {truncation} not implemented" ) else: SCREAMING_SNAKE_CASE = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": SCREAMING_SNAKE_CASE = int(max_length / len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.stack(np.tile(_UpperCamelCase , _UpperCamelCase ) ) SCREAMING_SNAKE_CASE = np.pad(_UpperCamelCase , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters ) SCREAMING_SNAKE_CASE = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: SCREAMING_SNAKE_CASE = self._np_extract_fbank_features(_UpperCamelCase , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Dict , _UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCamelCase : str = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[Union[str, TensorType]] = None , **_UpperCamelCase : Tuple , ) -> BatchFeature: '''simple docstring''' SCREAMING_SNAKE_CASE = truncation if truncation is not None else self.truncation SCREAMING_SNAKE_CASE = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a" F" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input" F" was sampled with {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." ) SCREAMING_SNAKE_CASE = isinstance(_UpperCamelCase , 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}" ) SCREAMING_SNAKE_CASE = is_batched_numpy or ( isinstance(_UpperCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE = np.asarray(_UpperCamelCase , dtype=np.floataa ) elif isinstance(_UpperCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase )] # convert to mel spectrogram, truncate and pad if needed. SCREAMING_SNAKE_CASE = [ self._get_input_mel(_UpperCamelCase , max_length if max_length else self.nb_max_samples , _UpperCamelCase , _UpperCamelCase ) for waveform in raw_speech ] SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for mel, longer in padded_inputs: input_mel.append(_UpperCamelCase ) is_longer.append(_UpperCamelCase ) if truncation == "fusion" and sum(_UpperCamelCase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer SCREAMING_SNAKE_CASE = np.random.randint(0 , len(_UpperCamelCase ) ) SCREAMING_SNAKE_CASE = True if isinstance(input_mel[0] , _UpperCamelCase ): SCREAMING_SNAKE_CASE = [np.asarray(_UpperCamelCase , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool SCREAMING_SNAKE_CASE = [[longer] for longer in is_longer] SCREAMING_SNAKE_CASE = {"input_features": input_mel, "is_longer": is_longer} SCREAMING_SNAKE_CASE = BatchFeature(_UpperCamelCase ) if return_tensors is not None: SCREAMING_SNAKE_CASE = input_features.convert_to_tensors(_UpperCamelCase ) return input_features
647
0