code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : 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: lowercase : Any = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[Any] = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = [ '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 lowercase : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
232
from torch import nn class _lowercase ( nn.Module ): def __init__( self : Any , snake_case : Dict , snake_case : Union[str, Any] ) -> Dict: """simple docstring""" super().__init__() UpperCamelCase_ : List[Any] = class_size UpperCamelCase_ : List[Any] = embed_size # self.mlp1 = nn.Linear(embed_size, embed_size) # self.mlp2 = (nn.Linear(embed_size, class_size)) UpperCamelCase_ : int = nn.Linear(snake_case , snake_case ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , snake_case : Any ) -> str: """simple docstring""" UpperCamelCase_ : Dict = self.mlp(snake_case ) return logits
175
0
import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging UpperCAmelCase__ : Any =logging.get_logger(__name__) def _lowercase ( _UpperCAmelCase ) -> Any: lowerCamelCase =r"""\w+[.]\d+""" lowerCamelCase =re.findall(__a , __a ) for pat in pats: lowerCamelCase =key.replace(__a , """_""".join(pat.split(""".""" ) ) ) return key def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> List[str]: lowerCamelCase =pt_tuple_key[:-1] + ("""scale""",) if ( any("""norm""" in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): lowerCamelCase =pt_tuple_key[:-1] + ("""scale""",) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: lowerCamelCase =pt_tuple_key[:-1] + ("""scale""",) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: lowerCamelCase =pt_tuple_key[:-1] + ("""embedding""",) return renamed_pt_tuple_key, pt_tensor # conv layer lowerCamelCase =pt_tuple_key[:-1] + ("""kernel""",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: lowerCamelCase =pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer lowerCamelCase =pt_tuple_key[:-1] + ("""kernel""",) if pt_tuple_key[-1] == "weight": lowerCamelCase =pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight lowerCamelCase =pt_tuple_key[:-1] + ("""weight""",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias lowerCamelCase =pt_tuple_key[:-1] + ("""bias""",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=42 ) -> List[Any]: # Step 1: Convert pytorch tensor to numpy lowerCamelCase ={k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params lowerCamelCase =flax_model.init_weights(PRNGKey(__a ) ) lowerCamelCase =flatten_dict(__a ) lowerCamelCase ={} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): lowerCamelCase =rename_key(__a ) lowerCamelCase =tuple(renamed_pt_key.split(""".""" ) ) # Correctly rename weight parameters lowerCamelCase , lowerCamelCase =rename_key_and_reshape_tensor(__a , __a , __a ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """ F"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) # also add unexpected weight so that warning is thrown lowerCamelCase =jnp.asarray(__a ) return unflatten_dict(__a )
350
from math import cos, sin, sqrt, tau from audio_filters.iir_filter import IIRFilter def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 / sqrt(2 ) ) -> IIRFilter: lowerCamelCase =tau * frequency / samplerate lowerCamelCase =sin(_UpperCAmelCase ) lowerCamelCase =cos(_UpperCAmelCase ) lowerCamelCase =_sin / (2 * q_factor) lowerCamelCase =(1 - _cos) / 2 lowerCamelCase =1 - _cos lowerCamelCase =1 + alpha lowerCamelCase =-2 * _cos lowerCamelCase =1 - alpha lowerCamelCase =IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 / sqrt(2 ) ) -> IIRFilter: lowerCamelCase =tau * frequency / samplerate lowerCamelCase =sin(_UpperCAmelCase ) lowerCamelCase =cos(_UpperCAmelCase ) lowerCamelCase =_sin / (2 * q_factor) lowerCamelCase =(1 + _cos) / 2 lowerCamelCase =-1 - _cos lowerCamelCase =1 + alpha lowerCamelCase =-2 * _cos lowerCamelCase =1 - alpha lowerCamelCase =IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 / sqrt(2 ) ) -> IIRFilter: lowerCamelCase =tau * frequency / samplerate lowerCamelCase =sin(_UpperCAmelCase ) lowerCamelCase =cos(_UpperCAmelCase ) lowerCamelCase =_sin / (2 * q_factor) lowerCamelCase =_sin / 2 lowerCamelCase =0 lowerCamelCase =-ba lowerCamelCase =1 + alpha lowerCamelCase =-2 * _cos lowerCamelCase =1 - alpha lowerCamelCase =IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 / sqrt(2 ) ) -> IIRFilter: lowerCamelCase =tau * frequency / samplerate lowerCamelCase =sin(_UpperCAmelCase ) lowerCamelCase =cos(_UpperCAmelCase ) lowerCamelCase =_sin / (2 * q_factor) lowerCamelCase =1 - alpha lowerCamelCase =-2 * _cos lowerCamelCase =1 + alpha lowerCamelCase =IIRFilter(2 ) filt.set_coefficients([ba, ba, ba] , [ba, ba, ba] ) return filt def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 / sqrt(2 ) , ) -> IIRFilter: lowerCamelCase =tau * frequency / samplerate lowerCamelCase =sin(_UpperCAmelCase ) lowerCamelCase =cos(_UpperCAmelCase ) lowerCamelCase =_sin / (2 * q_factor) lowerCamelCase =10 ** (gain_db / 40) lowerCamelCase =1 + alpha * big_a lowerCamelCase =-2 * _cos lowerCamelCase =1 - alpha * big_a lowerCamelCase =1 + alpha / big_a lowerCamelCase =-2 * _cos lowerCamelCase =1 - alpha / big_a lowerCamelCase =IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 / sqrt(2 ) , ) -> IIRFilter: lowerCamelCase =tau * frequency / samplerate lowerCamelCase =sin(_UpperCAmelCase ) lowerCamelCase =cos(_UpperCAmelCase ) lowerCamelCase =_sin / (2 * q_factor) lowerCamelCase =10 ** (gain_db / 40) lowerCamelCase =(big_a + 1) - (big_a - 1) * _cos lowerCamelCase =(big_a + 1) + (big_a - 1) * _cos lowerCamelCase =(big_a - 1) - (big_a + 1) * _cos lowerCamelCase =(big_a - 1) + (big_a + 1) * _cos lowerCamelCase =2 * sqrt(_UpperCAmelCase ) * alpha lowerCamelCase =big_a * (pmc + aaa) lowerCamelCase =2 * big_a * mpc lowerCamelCase =big_a * (pmc - aaa) lowerCamelCase =ppmc + aaa lowerCamelCase =-2 * pmpc lowerCamelCase =ppmc - aaa lowerCamelCase =IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 / sqrt(2 ) , ) -> IIRFilter: lowerCamelCase =tau * frequency / samplerate lowerCamelCase =sin(_UpperCAmelCase ) lowerCamelCase =cos(_UpperCAmelCase ) lowerCamelCase =_sin / (2 * q_factor) lowerCamelCase =10 ** (gain_db / 40) lowerCamelCase =(big_a + 1) - (big_a - 1) * _cos lowerCamelCase =(big_a + 1) + (big_a - 1) * _cos lowerCamelCase =(big_a - 1) - (big_a + 1) * _cos lowerCamelCase =(big_a - 1) + (big_a + 1) * _cos lowerCamelCase =2 * sqrt(_UpperCAmelCase ) * alpha lowerCamelCase =big_a * (ppmc + aaa) lowerCamelCase =-2 * big_a * pmpc lowerCamelCase =big_a * (ppmc - aaa) lowerCamelCase =pmc + aaa lowerCamelCase =2 * mpc lowerCamelCase =pmc - aaa lowerCamelCase =IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt
262
0
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device A__ : Union[str, Any] = False class __snake_case ( unittest.TestCase ): pass @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): def UpperCAmelCase__ ( self : Union[str, Any]): lowerCAmelCase_ : List[Any] = VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''') pipe.to(A_) pipe.set_progress_bar_config(disable=A_) lowerCAmelCase_ : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''') lowerCAmelCase_ : Union[str, Any] = torch.manual_seed(0) lowerCAmelCase_ : Optional[int] = pipe( image=A_ , generator=A_ , guidance_scale=7.5 , num_inference_steps=5_0 , output_type='''numpy''' , ).images lowerCAmelCase_ : Union[str, Any] = image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) lowerCAmelCase_ : str = np.array([0.0441, 0.0469, 0.0507, 0.0575, 0.0632, 0.0650, 0.0865, 0.0909, 0.0945]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
103
'''simple docstring''' 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 UpperCAmelCase ( snake_case_ ): def lowercase__ ( self : List[Any] ) -> Union[str, Any]: _lowerCAmelCase = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__snake_case , """hidden_sizes""" ) ) self.parent.assertTrue(hasattr(__snake_case , """num_attention_heads""" ) ) self.parent.assertTrue(hasattr(__snake_case , """num_encoder_blocks""" ) ) class UpperCAmelCase : def __init__( self : Optional[int] , __snake_case : str , __snake_case : Dict=13 , __snake_case : str=64 , __snake_case : Dict=3 , __snake_case : Dict=4 , __snake_case : Tuple=[2, 2, 2, 2] , __snake_case : int=[8, 4, 2, 1] , __snake_case : List[str]=[16, 32, 64, 1_28] , __snake_case : Optional[Any]=[1, 4, 8, 16] , __snake_case : Dict=[1, 2, 4, 8] , __snake_case : Optional[Any]=True , __snake_case : List[str]=True , __snake_case : int="gelu" , __snake_case : Optional[Any]=0.1 , __snake_case : Any=0.1 , __snake_case : Tuple=0.02 , __snake_case : Union[str, Any]=3 , __snake_case : Tuple=None , ) -> List[str]: _lowerCAmelCase = parent _lowerCAmelCase = batch_size _lowerCAmelCase = image_size _lowerCAmelCase = num_channels _lowerCAmelCase = num_encoder_blocks _lowerCAmelCase = sr_ratios _lowerCAmelCase = depths _lowerCAmelCase = hidden_sizes _lowerCAmelCase = downsampling_rates _lowerCAmelCase = num_attention_heads _lowerCAmelCase = is_training _lowerCAmelCase = use_labels _lowerCAmelCase = hidden_act _lowerCAmelCase = hidden_dropout_prob _lowerCAmelCase = attention_probs_dropout_prob _lowerCAmelCase = initializer_range _lowerCAmelCase = num_labels _lowerCAmelCase = scope def lowercase__ ( self : int ) -> Union[str, Any]: _lowerCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _lowerCAmelCase = None if self.use_labels: _lowerCAmelCase = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _lowerCAmelCase = self.get_config() return config, pixel_values, labels def lowercase__ ( self : List[Any] ) -> List[str]: 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 lowercase__ ( self : Tuple , __snake_case : Optional[Any] , __snake_case : Union[str, Any] , __snake_case : Optional[int] ) -> Tuple: _lowerCAmelCase = SegformerModel(config=__snake_case ) model.to(__snake_case ) model.eval() _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = _lowerCAmelCase = 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 lowercase__ ( self : List[str] , __snake_case : List[Any] , __snake_case : Optional[Any] , __snake_case : Optional[int] ) -> List[str]: _lowerCAmelCase = self.num_labels _lowerCAmelCase = SegformerForSemanticSegmentation(__snake_case ) model.to(__snake_case ) model.eval() _lowerCAmelCase = model(__snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) _lowerCAmelCase = model(__snake_case , labels=__snake_case ) 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 lowercase__ ( self : str , __snake_case : Union[str, Any] , __snake_case : Optional[int] , __snake_case : Dict ) -> List[str]: _lowerCAmelCase = 1 _lowerCAmelCase = SegformerForSemanticSegmentation(config=__snake_case ) model.to(__snake_case ) model.eval() _lowerCAmelCase = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(__snake_case ) _lowerCAmelCase = model(__snake_case , labels=__snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def lowercase__ ( self : Optional[int] ) -> int: _lowerCAmelCase = self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = config_and_inputs _lowerCAmelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase ( snake_case_ , snake_case_ , unittest.TestCase ): _lowercase: Any = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) _lowercase: Tuple = ( { '''feature-extraction''': SegformerModel, '''image-classification''': SegformerForImageClassification, '''image-segmentation''': SegformerForSemanticSegmentation, } if is_torch_available() else {} ) _lowercase: Tuple = True _lowercase: Union[str, Any] = False _lowercase: Dict = False _lowercase: Optional[Any] = False def lowercase__ ( self : Tuple ) -> Any: _lowerCAmelCase = SegformerModelTester(self ) _lowerCAmelCase = SegformerConfigTester(self , config_class=__snake_case ) def lowercase__ ( self : Optional[Any] ) -> Dict: self.config_tester.run_common_tests() def lowercase__ ( self : int ) -> Union[str, Any]: _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__snake_case ) def lowercase__ ( self : Dict ) -> int: _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*__snake_case ) def lowercase__ ( self : Dict ) -> Dict: _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*__snake_case ) @unittest.skip("""SegFormer does not use inputs_embeds""" ) def lowercase__ ( self : int ) -> Union[str, Any]: pass @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods""" ) def lowercase__ ( self : Optional[int] ) -> int: pass def lowercase__ ( self : Union[str, Any] ) -> Optional[Any]: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase = model_class(__snake_case ) _lowerCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCAmelCase = [*signature.parameters.keys()] _lowerCAmelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __snake_case ) def lowercase__ ( self : Tuple ) -> Tuple: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase = True for model_class in self.all_model_classes: _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = True _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) _lowerCAmelCase = outputs.attentions _lowerCAmelCase = sum(self.model_tester.depths ) self.assertEqual(len(__snake_case ) , __snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _lowerCAmelCase = True _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) _lowerCAmelCase = outputs.attentions self.assertEqual(len(__snake_case ) , __snake_case ) # verify the first attentions (first block, first layer) _lowerCAmelCase = (self.model_tester.image_size // 4) ** 2 _lowerCAmelCase = (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) _lowerCAmelCase = (self.model_tester.image_size // 32) ** 2 _lowerCAmelCase = (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] , ) _lowerCAmelCase = len(__snake_case ) # Check attention is always last and order is fine _lowerCAmelCase = True _lowerCAmelCase = True _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) self.assertEqual(out_len + 1 , len(__snake_case ) ) _lowerCAmelCase = outputs.attentions self.assertEqual(len(__snake_case ) , __snake_case ) # verify the first attentions (first block, first layer) _lowerCAmelCase = (self.model_tester.image_size // 4) ** 2 _lowerCAmelCase = (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 lowercase__ ( self : int ) -> List[str]: def check_hidden_states_output(__snake_case : str , __snake_case : Tuple , __snake_case : Optional[int] ): _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) _lowerCAmelCase = outputs.hidden_states _lowerCAmelCase = self.model_tester.num_encoder_blocks self.assertEqual(len(__snake_case ) , __snake_case ) # 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, ] , ) _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase = True check_hidden_states_output(__snake_case , __snake_case , __snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _lowerCAmelCase = True check_hidden_states_output(__snake_case , __snake_case , __snake_case ) def lowercase__ ( self : Optional[Any] ) -> Any: if not self.model_tester.is_training: return _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase = True for model_class in self.all_model_classes: if model_class in get_values(__snake_case ): continue _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.train() _lowerCAmelCase = self._prepare_for_class(__snake_case , __snake_case , return_labels=__snake_case ) _lowerCAmelCase = model(**__snake_case ).loss loss.backward() @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def lowercase__ ( self : Tuple ) -> Dict: pass @slow def lowercase__ ( self : str ) -> Optional[int]: for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase = SegformerModel.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) def UpperCamelCase__ ( ): """simple docstring""" _lowerCAmelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch class UpperCAmelCase ( unittest.TestCase ): @slow def lowercase__ ( self : Union[str, Any] ) -> Any: # only resize + normalize _lowerCAmelCase = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=__snake_case , align=__snake_case , do_random_crop=__snake_case ) _lowerCAmelCase = SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512""" ).to( __snake_case ) _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=__snake_case , return_tensors="""pt""" ) _lowerCAmelCase = encoded_inputs.pixel_values.to(__snake_case ) with torch.no_grad(): _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , __snake_case ) _lowerCAmelCase = torch.tensor( [ [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]], [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]], [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]], ] ).to(__snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , __snake_case , atol=1E-4 ) ) @slow def lowercase__ ( self : Optional[Any] ) -> Any: # only resize + normalize _lowerCAmelCase = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=__snake_case , align=__snake_case , do_random_crop=__snake_case ) _lowerCAmelCase = SegformerForSemanticSegmentation.from_pretrained( """nvidia/segformer-b1-finetuned-cityscapes-1024-1024""" ).to(__snake_case ) _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=__snake_case , return_tensors="""pt""" ) _lowerCAmelCase = encoded_inputs.pixel_values.to(__snake_case ) with torch.no_grad(): _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , __snake_case ) _lowerCAmelCase = torch.tensor( [ [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]], [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]], [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]], ] ).to(__snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , __snake_case , atol=1E-1 ) ) @slow def lowercase__ ( self : Any ) -> str: # only resize + normalize _lowerCAmelCase = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=__snake_case , align=__snake_case , do_random_crop=__snake_case ) _lowerCAmelCase = SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512""" ).to( __snake_case ) _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=__snake_case , return_tensors="""pt""" ) _lowerCAmelCase = encoded_inputs.pixel_values.to(__snake_case ) with torch.no_grad(): _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = outputs.logits.detach().cpu() _lowerCAmelCase = image_processor.post_process_semantic_segmentation(outputs=__snake_case , target_sizes=[(5_00, 3_00)] ) _lowerCAmelCase = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , __snake_case ) _lowerCAmelCase = image_processor.post_process_semantic_segmentation(outputs=__snake_case ) _lowerCAmelCase = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , __snake_case )
70
0
def _A ( SCREAMING_SNAKE_CASE : str ): """simple docstring""" return [ txt[:a] + txt[a].upper() + txt[a + 1 :] for a in range(len(SCREAMING_SNAKE_CASE ) ) if txt[a].isalpha() ] if __name__ == "__main__": __import__("""doctest""").testmod()
148
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _A ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Tuple ): """simple docstring""" a__ : List[Any] ={ "en": "Machine learning is great, isn't it?", "ru": "Машинное обучение - это здорово, не так ли?", "de": "Maschinelles Lernen ist großartig, oder?", } # BLUE scores as follows: # "pair": [fairseq, transformers] a__ : List[Any] ={ "ru-en": ["[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)", "39.20"], "en-ru": ["[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)", "33.47"], "en-de": ["[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)", "42.83"], "de-en": ["[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)", "41.35"], } a__ : Optional[int] =f'''{src_lang}-{tgt_lang}''' a__ : Any =f''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = "facebook/wmt19-{src_lang}-{tgt_lang}" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = "{texts[src_lang]}" input_ids = tokenizer.encode(input, return_tensors="pt") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) a__ : Tuple =os.path.join(SCREAMING_SNAKE_CASE , "README.md" ) print(f'''Generating {path}''' ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write(SCREAMING_SNAKE_CASE ) # make sure we are under the root of the project UpperCAmelCase : str = Path(__file__).resolve().parent.parent.parent UpperCAmelCase : Dict = repo_dir / """model_cards""" for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[str] = model_name.split("""-""") UpperCAmelCase : Tuple = model_cards_dir / """facebook""" / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
148
1
from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class UpperCamelCase__ (unittest.TestCase ): '''simple docstring''' def _lowercase ( self ) -> Union[str, Any]: lowerCamelCase : Optional[int] = tf.convert_to_tensor( [ [ 8.2220991, # 3rd highest value; idx. 0 -0.5620044, 5.23229752, 4.0386393, -6.8798378, -0.54785802, -3.2012153, 2.92777176, 1.88171953, 7.35341276, # 5th highest value; idx. 9 8.43207833, # 2nd highest value; idx. 10 -9.85711836, -5.96209236, -1.13039161, -7.1115294, -0.8369633, -5.3186408, 7.06427407, 0.81369344, -0.82023817, -5.9179796, 0.58813443, -6.99778438, 4.71551189, -0.18771637, 7.44020759, # 4th highest value; idx. 25 9.38450987, # 1st highest value; idx. 26 2.12662941, -9.32562038, 2.35652522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.58425518, 4.53139238, -5.57510464, -6.28030699, -7.19529503, -4.02122551, 1.39337037, -6.06707057, 1.59480517, -9.643119, 0.03907799, 0.67231762, -8.88206726, 6.27115922, # 4th highest value; idx. 13 2.28520723, 4.82767506, 4.30421368, 8.8275313, # 2nd highest value; idx. 17 5.44029958, # 5th highest value; idx. 18 -4.4735794, 7.38579536, # 3rd highest value; idx. 20 -2.91051663, 2.61946077, -2.5674762, -9.48959302, -4.02922645, -1.35416918, 9.67702323, # 1st highest value; idx. 27 -5.89478553, 1.85370467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) lowerCamelCase : List[Any] = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]] , dtype=tf.intaa , ) # expected non filtered idx as noted above lowerCamelCase : str = tf.convert_to_tensor( [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023] , dtype=tf.floataa , ) # expected non filtered values as noted above lowerCamelCase : Any = tf_top_k_top_p_filtering(UpperCAmelCase_ , top_k=10 , top_p=0.6 , min_tokens_to_keep=4 ) lowerCamelCase : List[str] = output[output != -float("inf" )] lowerCamelCase : List[Any] = tf.cast( tf.where(tf.not_equal(UpperCAmelCase_ , tf.constant(-float("inf" ) , dtype=tf.floataa ) ) ) , dtype=tf.intaa , ) tf.debugging.assert_near(UpperCAmelCase_ , UpperCAmelCase_ , rtol=1e-12 ) tf.debugging.assert_equal(UpperCAmelCase_ , UpperCAmelCase_ ) @require_tf class UpperCamelCase__ (unittest.TestCase , __SCREAMING_SNAKE_CASE ): '''simple docstring''' if is_tf_available(): lowerCamelCase_ : List[str] = { """AutoModelForCausalLM""": TFAutoModelForCausalLM, """AutoModelForSpeechSeq2Seq""": TFAutoModelForSpeechSeqaSeq, """AutoModelForSeq2SeqLM""": TFAutoModelForSeqaSeqLM, """AutoModelForVision2Seq""": TFAutoModelForVisionaSeq, """LogitsProcessorList""": TFLogitsProcessorList, """MinLengthLogitsProcessor""": TFMinLengthLogitsProcessor, """create_tensor_fn""": tf.convert_to_tensor, """floats_tensor""": floats_tensor, """return_tensors""": """tf""", } @slow def _lowercase ( self ) -> Dict: lowerCamelCase : Optional[int] = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) lowerCamelCase : Any = 2 lowerCamelCase : List[Any] = 2 class UpperCamelCase__ (tf.Module ): '''simple docstring''' def __init__( self , UpperCamelCase__ ) -> Union[str, Any]: super(UpperCAmelCase_ , self ).__init__() lowerCamelCase : Union[str, Any] = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name="input_ids" ), tf.TensorSpec((None, input_length) , tf.intaa , name="attention_mask" ), ) , jit_compile=UpperCAmelCase_ , ) def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__ ) -> int: lowerCamelCase : Any = self.model.generate( input_ids=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , max_new_tokens=UpperCAmelCase_ , return_dict_in_generate=UpperCAmelCase_ , ) return {"sequences": outputs["sequences"]} lowerCamelCase : Dict = [[2, 0], [102, 103]] lowerCamelCase : List[Any] = [[1, 0], [1, 1]] lowerCamelCase : Union[str, Any] = DummyModel(model=UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(UpperCAmelCase_ , UpperCAmelCase_ , signatures={"serving_default": dummy_model.serving} ) lowerCamelCase : Tuple = tf.saved_model.load(UpperCAmelCase_ ).signatures["serving_default"] for batch_size in range(1 , len(UpperCAmelCase_ ) + 1 ): lowerCamelCase : Optional[Any] = { "input_ids": tf.constant(dummy_input_ids[:batch_size] ), "attention_mask": tf.constant(dummy_attention_masks[:batch_size] ), } lowerCamelCase : int = serving_func(**UpperCAmelCase_ )["sequences"] lowerCamelCase : int = test_model.generate(**UpperCAmelCase_ , max_new_tokens=UpperCAmelCase_ ) tf.debugging.assert_equal(UpperCAmelCase_ , UpperCAmelCase_ ) @slow def _lowercase ( self ) -> Dict: lowerCamelCase : Tuple = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) lowerCamelCase : Tuple = 1 lowerCamelCase : int = 2 class UpperCamelCase__ (tf.Module ): '''simple docstring''' def __init__( self , UpperCamelCase__ ) -> Tuple: super(UpperCAmelCase_ , self ).__init__() lowerCamelCase : str = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name="input_ids" ), tf.TensorSpec((batch_size, None) , tf.intaa , name="attention_mask" ), ) , jit_compile=UpperCAmelCase_ , ) def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__ ) -> Tuple: lowerCamelCase : Dict = self.model.generate( input_ids=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , max_new_tokens=UpperCAmelCase_ , return_dict_in_generate=UpperCAmelCase_ , ) return {"sequences": outputs["sequences"]} lowerCamelCase : Dict = [[2], [102, 103]] lowerCamelCase : Tuple = [[1], [1, 1]] lowerCamelCase : List[str] = DummyModel(model=UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(UpperCAmelCase_ , UpperCAmelCase_ , signatures={"serving_default": dummy_model.serving} ) lowerCamelCase : Dict = tf.saved_model.load(UpperCAmelCase_ ).signatures["serving_default"] for input_row in range(len(UpperCAmelCase_ ) ): lowerCamelCase : Optional[Any] = { "input_ids": tf.constant([dummy_input_ids[input_row]] ), "attention_mask": tf.constant([dummy_attention_masks[input_row]] ), } lowerCamelCase : List[Any] = serving_func(**UpperCAmelCase_ )["sequences"] lowerCamelCase : Optional[Any] = test_model.generate(**UpperCAmelCase_ , max_new_tokens=UpperCAmelCase_ ) tf.debugging.assert_equal(UpperCAmelCase_ , UpperCAmelCase_ ) @slow @require_tensorflow_text def _lowercase ( self ) -> Any: with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id="google/flan-t5-small" , filename="spiece.model" , local_dir=UpperCAmelCase_ ) class UpperCamelCase__ (tf.keras.layers.Layer ): '''simple docstring''' def __init__( self ) -> Optional[int]: super().__init__() lowerCamelCase : Dict = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(UpperCAmelCase_ , "spiece.model" ) , "rb" ).read() ) lowerCamelCase : Dict = TFAutoModelForSeqaSeqLM.from_pretrained("hf-internal-testing/tiny-random-t5" ) def _lowercase ( self , UpperCamelCase__ , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: lowerCamelCase : Any = self.tokenizer.tokenize(UpperCAmelCase_ ) lowerCamelCase : Dict = text.pad_model_inputs( UpperCAmelCase_ , max_seq_length=64 , pad_value=self.model.config.pad_token_id ) lowerCamelCase : Dict = self.model.generate(input_ids=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ ) return self.tokenizer.detokenize(UpperCAmelCase_ ) lowerCamelCase : int = CompleteSentenceTransformer() lowerCamelCase : Dict = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name="inputs" ) lowerCamelCase : Optional[Any] = complete_model(UpperCAmelCase_ ) lowerCamelCase : Optional[int] = tf.keras.Model(UpperCAmelCase_ , UpperCAmelCase_ ) keras_model.save(UpperCAmelCase_ ) def _lowercase ( self ) -> int: lowerCamelCase : str = { "do_sample": True, "num_beams": 1, "top_p": 0.7, "top_k": 10, "temperature": 0.7, } lowerCamelCase : Any = 14 lowerCamelCase : Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) lowerCamelCase : int = "Hello, my dog is cute and" lowerCamelCase : Optional[int] = tokenizer(UpperCAmelCase_ , return_tensors="tf" ) lowerCamelCase : Tuple = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) lowerCamelCase : Union[str, Any] = 638 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(":/CPU:0" ): tf.random.set_seed(0 ) lowerCamelCase : int = model.generate(**UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ , **UpperCAmelCase_ ) self.assertTrue(expectation == len(generated_tokens[0] ) ) lowerCamelCase : List[str] = [638, 198] with tf.device(":/CPU:0" ): tf.random.set_seed(0 ) lowerCamelCase : Dict = model.generate(**UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ , **UpperCAmelCase_ ) self.assertTrue(expectation == len(generated_tokens[0] ) ) def _lowercase ( self ) -> Optional[Any]: lowerCamelCase : Tuple = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bart" ) lowerCamelCase : Optional[int] = "Hugging Face is a technology company based in New York and Paris." lowerCamelCase : Any = bart_tokenizer(UpperCAmelCase_ , return_tensors="tf" ).input_ids lowerCamelCase : Optional[int] = TFBartForConditionalGeneration.from_pretrained("hf-internal-testing/tiny-random-bart" ) lowerCamelCase : Optional[int] = bart_model.generate(UpperCAmelCase_ ).numpy() class UpperCamelCase__ (__SCREAMING_SNAKE_CASE ): '''simple docstring''' def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__=None , **UpperCamelCase__ ) -> Any: return super().call(UpperCAmelCase_ , **UpperCAmelCase_ ) lowerCamelCase : Optional[Any] = FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart" ) lowerCamelCase : Union[str, Any] = bart_model.generate(UpperCAmelCase_ , foo="bar" ).numpy() self.assertTrue(np.array_equal(UpperCAmelCase_ , UpperCAmelCase_ ) ) class UpperCamelCase__ (bart_model.model.encoder.__class__ ): '''simple docstring''' def _lowercase ( self , UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: return super().call(UpperCAmelCase_ , **UpperCAmelCase_ ) lowerCamelCase : Union[str, Any] = FakeEncoder(bart_model.config , bart_model.model.shared ) lowerCamelCase : int = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) lowerCamelCase : Any = bart_model.generate(UpperCAmelCase_ ).numpy() with self.assertRaises(UpperCAmelCase_ ): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(UpperCAmelCase_ , foo="bar" )
48
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __A = logging.get_logger(__name__) __A = { "microsoft/unispeech-large-1500h-cv": ( "https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json" ), # See all UniSpeech models at https://huggingface.co/models?filter=unispeech } class _SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "unispeech" def __init__(self : Any , UpperCAmelCase_ : Any=32 , UpperCAmelCase_ : List[str]=768 , UpperCAmelCase_ : Any=12 , UpperCAmelCase_ : Union[str, Any]=12 , UpperCAmelCase_ : Optional[Any]=3_072 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Any=0.0 , UpperCAmelCase_ : str=0.0 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : Union[str, Any]=1E-5 , UpperCAmelCase_ : str="group" , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Tuple=(512, 512, 512, 512, 512, 512, 512) , UpperCAmelCase_ : str=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : Any=(10, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : Optional[Any]=False , UpperCAmelCase_ : str=128 , UpperCAmelCase_ : int=16 , UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : Optional[int]=True , UpperCAmelCase_ : Dict=0.05 , UpperCAmelCase_ : Optional[int]=10 , UpperCAmelCase_ : Tuple=2 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : int=10 , UpperCAmelCase_ : List[Any]=0 , UpperCAmelCase_ : Optional[Any]=320 , UpperCAmelCase_ : int=2 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : str=100 , UpperCAmelCase_ : Any=256 , UpperCAmelCase_ : int=256 , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : str="mean" , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : List[Any]=256 , UpperCAmelCase_ : Optional[int]=80 , UpperCAmelCase_ : Optional[int]=0 , UpperCAmelCase_ : Optional[Any]=1 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : Dict=0.5 , **UpperCAmelCase_ : Optional[int] , ) ->str: '''simple docstring''' super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) lowerCamelCase__: Union[str, Any] =hidden_size lowerCamelCase__: List[str] =feat_extract_norm lowerCamelCase__: Dict =feat_extract_activation lowerCamelCase__: Optional[Any] =list(UpperCAmelCase_) lowerCamelCase__: Any =list(UpperCAmelCase_) lowerCamelCase__: Union[str, Any] =list(UpperCAmelCase_) lowerCamelCase__: Dict =conv_bias lowerCamelCase__: Optional[Any] =num_conv_pos_embeddings lowerCamelCase__: Dict =num_conv_pos_embedding_groups lowerCamelCase__: int =len(self.conv_dim) lowerCamelCase__: Union[str, Any] =num_hidden_layers lowerCamelCase__: Union[str, Any] =intermediate_size lowerCamelCase__: Dict =hidden_act lowerCamelCase__: List[Any] =num_attention_heads lowerCamelCase__: Dict =hidden_dropout lowerCamelCase__: Optional[Any] =attention_dropout lowerCamelCase__: Optional[Any] =activation_dropout lowerCamelCase__: Tuple =feat_proj_dropout lowerCamelCase__: int =final_dropout lowerCamelCase__: Optional[Any] =layerdrop lowerCamelCase__: Dict =layer_norm_eps lowerCamelCase__: Optional[Any] =initializer_range lowerCamelCase__: int =num_ctc_classes lowerCamelCase__: Tuple =vocab_size lowerCamelCase__: Dict =do_stable_layer_norm lowerCamelCase__: List[Any] =use_weighted_layer_sum lowerCamelCase__: Dict =classifier_proj_size if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==" " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =" F""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,""" F""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase__: int =apply_spec_augment lowerCamelCase__: List[str] =mask_time_prob lowerCamelCase__: Union[str, Any] =mask_time_length lowerCamelCase__: List[Any] =mask_time_min_masks lowerCamelCase__: Any =mask_feature_prob lowerCamelCase__: Optional[Any] =mask_feature_length lowerCamelCase__: List[str] =mask_feature_min_masks # parameters for pretraining with codevector quantized representations lowerCamelCase__: Optional[Any] =num_codevectors_per_group lowerCamelCase__: str =num_codevector_groups lowerCamelCase__: Tuple =contrastive_logits_temperature lowerCamelCase__: int =feat_quantizer_dropout lowerCamelCase__: Any =num_negatives lowerCamelCase__: List[str] =codevector_dim lowerCamelCase__: Union[str, Any] =proj_codevector_dim lowerCamelCase__: Any =diversity_loss_weight # ctc loss lowerCamelCase__: Any =ctc_loss_reduction lowerCamelCase__: Dict =ctc_zero_infinity # pretraining loss lowerCamelCase__: Dict =replace_prob @property def SCREAMING_SNAKE_CASE_ (self : List[Any]) ->Optional[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1)
10
0
from functools import reduce __A : List[str] = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def __UpperCamelCase ( _A : Optional[Any] = N ) ->int: """simple docstring""" return max( # mypy cannot properly interpret reduce int(reduce(lambda _A , _A : str(int(a__ ) * int(a__ ) ) , n[i : i + 13] ) ) for i in range(len(a__ ) - 12 ) ) if __name__ == "__main__": print(F"""{solution() = }""")
361
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __A : Any = '▁' __A : Union[str, Any] = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece class _SCREAMING_SNAKE_CASE ( lowerCAmelCase__ , unittest.TestCase): _UpperCamelCase:Any = BertGenerationTokenizer _UpperCamelCase:List[str] = False _UpperCamelCase:List[Any] = True def _snake_case ( self )-> Optional[int]: super().setUp() lowerCamelCase_ =BertGenerationTokenizer(_SCREAMING_SNAKE_CASE , keep_accents=_SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(self.tmpdirname ) def _snake_case ( self )-> Any: lowerCamelCase_ ="""<s>""" lowerCamelCase_ =1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) def _snake_case ( self )-> Union[str, Any]: lowerCamelCase_ =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<unk>""" ) self.assertEqual(vocab_keys[1] , """<s>""" ) self.assertEqual(vocab_keys[-1] , """<pad>""" ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , 1002 ) def _snake_case ( self )-> Union[str, Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def _snake_case ( self )-> Any: lowerCamelCase_ =BertGenerationTokenizer(_SCREAMING_SNAKE_CASE , keep_accents=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =tokenizer.tokenize("""This is a test""" ) self.assertListEqual(_SCREAMING_SNAKE_CASE , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE ) , [285, 46, 10, 170, 382] , ) lowerCamelCase_ =tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( _SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) lowerCamelCase_ =tokenizer.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE ) self.assertListEqual( _SCREAMING_SNAKE_CASE , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) lowerCamelCase_ =tokenizer.convert_ids_to_tokens(_SCREAMING_SNAKE_CASE ) self.assertListEqual( _SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) @cached_property def _snake_case ( self )-> str: return BertGenerationTokenizer.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder""" ) @slow def _snake_case ( self )-> Optional[int]: lowerCamelCase_ ="""Hello World!""" lowerCamelCase_ =[1_8536, 2260, 101] self.assertListEqual(_SCREAMING_SNAKE_CASE , self.big_tokenizer.encode(_SCREAMING_SNAKE_CASE ) ) @slow def _snake_case ( self )-> List[str]: lowerCamelCase_ =( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) lowerCamelCase_ =[ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 3_4324, 497, 391, 408, 1_1342, 1244, 385, 100, 938, 985, 456, 574, 362, 1_2597, 3200, 3129, 1172, ] self.assertListEqual(_SCREAMING_SNAKE_CASE , self.big_tokenizer.encode(_SCREAMING_SNAKE_CASE ) ) @require_torch @slow def _snake_case ( self )-> Any: import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence lowerCamelCase_ =list(self.big_tokenizer.get_vocab().keys() )[:10] lowerCamelCase_ =""" """.join(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =self.big_tokenizer.encode_plus(_SCREAMING_SNAKE_CASE , return_tensors="""pt""" , return_token_type_ids=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =self.big_tokenizer.batch_encode_plus( [sequence + """ """ + sequence] , return_tensors="""pt""" , return_token_type_ids=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =BertGenerationConfig() lowerCamelCase_ =BertGenerationEncoder(_SCREAMING_SNAKE_CASE ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_SCREAMING_SNAKE_CASE ) model(**_SCREAMING_SNAKE_CASE ) @slow def _snake_case ( self )-> int: # fmt: off lowerCamelCase_ ={"""input_ids""": [[3_9286, 458, 3_6335, 2001, 456, 1_3073, 1_3266, 455, 113, 7746, 1741, 1_1157, 391, 1_3073, 1_3266, 455, 113, 3967, 3_5412, 113, 4936, 109, 3870, 2377, 113, 3_0084, 4_5720, 458, 134, 1_7496, 112, 503, 1_1672, 113, 118, 112, 5665, 1_3347, 3_8687, 112, 1496, 3_1389, 112, 3268, 4_7264, 134, 962, 112, 1_6377, 8035, 2_3130, 430, 1_2169, 1_5518, 2_8592, 458, 146, 4_1697, 109, 391, 1_2169, 1_5518, 1_6689, 458, 146, 4_1358, 109, 452, 726, 4034, 111, 763, 3_5412, 5082, 388, 1903, 111, 9051, 391, 2870, 4_8918, 1900, 1123, 550, 998, 112, 9586, 1_5985, 455, 391, 410, 2_2955, 3_7636, 114], [448, 1_7496, 419, 3663, 385, 763, 113, 2_7533, 2870, 3283, 1_3043, 1639, 2_4713, 523, 656, 2_4013, 1_8550, 2521, 517, 2_7014, 2_1244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 1_1786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 2_1932, 1_8146, 726, 363, 1_7032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_SCREAMING_SNAKE_CASE , model_name="""google/bert_for_seq_generation_L-24_bbc_encoder""" , revision="""c817d1fd1be2ffa69431227a1fe320544943d4db""" , )
49
0
"""simple docstring""" import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features _UpperCamelCase : Tuple = logging.get_logger(__name__) _UpperCamelCase : str = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) _UpperCamelCase : Tuple = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class a : UpperCAmelCase_ : str =field( default=a_, metadata={"help": "Model type selected in the list: " + ", ".join(a_ )} ) UpperCAmelCase_ : str =field( default=a_, metadata={"help": "The input data dir. Should contain the .json files for the SQuAD task."} ) UpperCAmelCase_ : int =field( default=128, metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) }, ) UpperCAmelCase_ : int =field( default=128, metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."}, ) UpperCAmelCase_ : int =field( default=64, metadata={ "help": ( "The maximum number of tokens for the question. Questions longer than this will " "be truncated to this length." ) }, ) UpperCAmelCase_ : int =field( default=30, metadata={ "help": ( "The maximum length of an answer that can be generated. This is needed because the start " "and end predictions are not conditioned on one another." ) }, ) UpperCAmelCase_ : bool =field( default=a_, metadata={"help": "Overwrite the cached training and evaluation sets"} ) UpperCAmelCase_ : bool =field( default=a_, metadata={"help": "If true, the SQuAD examples contain some that do not have an answer."} ) UpperCAmelCase_ : float =field( default=0.0, metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) UpperCAmelCase_ : int =field( default=20, metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) UpperCAmelCase_ : int =field( default=0, metadata={ "help": ( "language id of input for language-specific xlm models (see" " tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)" ) }, ) UpperCAmelCase_ : int =field(default=1, metadata={"help": "multiple threads for converting example to features"} ) class a ( a_ ): UpperCAmelCase_ : List[Any] ="train" UpperCAmelCase_ : str ="dev" class a ( a_ ): UpperCAmelCase_ : SquadDataTrainingArguments UpperCAmelCase_ : List[SquadFeatures] UpperCAmelCase_ : Split UpperCAmelCase_ : bool def __init__( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = Split.train , _lowerCamelCase = False , _lowerCamelCase = None , _lowerCamelCase = "pt" , ): lowercase = args lowercase = is_language_sensitive lowercase = SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(_lowerCamelCase , _lowerCamelCase ): try: lowercase = Split[mode] except KeyError: raise KeyError('mode is not a valid split name' ) lowercase = mode # Load data features from cache or dataset file lowercase = 'v2' if args.version_2_with_negative else 'v1' lowercase = os.path.join( cache_dir if cache_dir is not None else args.data_dir , F'cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}' , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. lowercase = cached_features_file + '.lock' with FileLock(_lowerCamelCase ): if os.path.exists(_lowerCamelCase ) and not args.overwrite_cache: lowercase = time.time() lowercase = torch.load(_lowerCamelCase ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. lowercase = self.old_features['features'] lowercase = self.old_features.get('dataset' , _lowerCamelCase ) lowercase = self.old_features.get('examples' , _lowerCamelCase ) logger.info( F'Loading features from cached file {cached_features_file} [took %.3f s]' , time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( F'Deleting cached file {cached_features_file} will allow dataset and examples to be cached in' ' future run' ) else: if mode == Split.dev: lowercase = self.processor.get_dev_examples(args.data_dir ) else: lowercase = self.processor.get_train_examples(args.data_dir ) lowercase , lowercase = squad_convert_examples_to_features( examples=self.examples , tokenizer=_lowerCamelCase , max_seq_length=args.max_seq_length , doc_stride=args.doc_stride , max_query_length=args.max_query_length , is_training=mode == Split.train , threads=args.threads , return_dataset=_lowerCamelCase , ) lowercase = time.time() torch.save( {'features': self.features, 'dataset': self.dataset, 'examples': self.examples} , _lowerCamelCase , ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( F'Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]' ) def __len__( self ): return len(self.features ) def __getitem__( self , _lowerCamelCase ): # Convert to Tensors and build dataset lowercase = self.features[i] lowercase = torch.tensor(feature.input_ids , dtype=torch.long ) lowercase = torch.tensor(feature.attention_mask , dtype=torch.long ) lowercase = torch.tensor(feature.token_type_ids , dtype=torch.long ) lowercase = torch.tensor(feature.cls_index , dtype=torch.long ) lowercase = torch.tensor(feature.p_mask , dtype=torch.float ) lowercase = torch.tensor(feature.is_impossible , dtype=torch.float ) lowercase = { 'input_ids': input_ids, 'attention_mask': attention_mask, 'token_type_ids': token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({'cls_index': cls_index, 'p_mask': p_mask} ) if self.args.version_2_with_negative: inputs.update({'is_impossible': is_impossible} ) if self.is_language_sensitive: inputs.update({'langs': (torch.ones(input_ids.shape , dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: lowercase = torch.tensor(feature.start_position , dtype=torch.long ) lowercase = torch.tensor(feature.end_position , dtype=torch.long ) inputs.update({'start_positions': start_positions, 'end_positions': end_positions} ) return inputs
220
"""simple docstring""" from argparse import ArgumentParser from .env import EnvironmentCommand def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' lowercase = ArgumentParser('Diffusers CLI tool' , usage='diffusers-cli <command> [<args>]' ) lowercase = parser.add_subparsers(help='diffusers-cli command helpers' ) # Register commands EnvironmentCommand.register_subcommand(__snake_case ) # Let's go lowercase = parser.parse_args() if not hasattr(__snake_case , 'func' ): parser.print_help() exit(1 ) # Run lowercase = args.func(__snake_case ) service.run() if __name__ == "__main__": main()
220
1
"""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 a :List[Any] = "CompVis/stable-diffusion-v1-1" a :Dict = "CompVis/stable-diffusion-v1-2" a :Any = "CompVis/stable-diffusion-v1-3" a :Any = "CompVis/stable-diffusion-v1-4" class __a (UpperCamelCase_): '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a , _a , _a = True , ) -> Optional[int]: """simple docstring""" super()._init_() SCREAMING_SNAKE_CASE__ : List[str] = StableDiffusionPipeline.from_pretrained(_a ) SCREAMING_SNAKE_CASE__ : str = StableDiffusionPipeline.from_pretrained(_a ) SCREAMING_SNAKE_CASE__ : Optional[Any] = StableDiffusionPipeline.from_pretrained(_a ) SCREAMING_SNAKE_CASE__ : Tuple = 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 _a ( self ) -> Dict[str, Any]: """simple docstring""" return {k: getattr(self , _a ) for k in self.config.keys() if not k.startswith("""_""" )} def _a ( self , _a = "auto" ) -> List[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__ : Union[str, Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(_a ) def _a ( self ) -> Any: """simple docstring""" self.enable_attention_slicing(_a ) @torch.no_grad() def _a ( self , _a , _a = 512 , _a = 512 , _a = 50 , _a = 7.5 , _a = None , _a = 1 , _a = 0.0 , _a = None , _a = None , _a = "pil" , _a = True , _a = None , _a = 1 , **_a , ) -> Optional[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 _a ( self , _a , _a = 512 , _a = 512 , _a = 50 , _a = 7.5 , _a = None , _a = 1 , _a = 0.0 , _a = None , _a = None , _a = "pil" , _a = True , _a = None , _a = 1 , **_a , ) -> List[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 _a ( self , _a , _a = 512 , _a = 512 , _a = 50 , _a = 7.5 , _a = None , _a = 1 , _a = 0.0 , _a = None , _a = None , _a = "pil" , _a = True , _a = None , _a = 1 , **_a , ) -> Dict: """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 _a ( self , _a , _a = 512 , _a = 512 , _a = 50 , _a = 7.5 , _a = None , _a = 1 , _a = 0.0 , _a = None , _a = None , _a = "pil" , _a = True , _a = None , _a = 1 , **_a , ) -> 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 _a ( self , _a , _a = 512 , _a = 512 , _a = 50 , _a = 7.5 , _a = None , _a = 1 , _a = 0.0 , _a = None , _a = None , _a = "pil" , _a = True , _a = None , _a = 1 , **_a , ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : str = """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__ : str = 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__ : List[str] = 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__ : Optional[int] = 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__ : List[str] = 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]] )
56
"""simple docstring""" import unittest from transformers import DebertaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, ) from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST class __a (UpperCamelCase_): '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=False , _a=True , _a="None" , _a=3 , _a=4 , _a=None , ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = parent SCREAMING_SNAKE_CASE__ : Union[str, Any] = batch_size SCREAMING_SNAKE_CASE__ : str = seq_length SCREAMING_SNAKE_CASE__ : str = is_training SCREAMING_SNAKE_CASE__ : List[Any] = use_input_mask SCREAMING_SNAKE_CASE__ : str = use_token_type_ids SCREAMING_SNAKE_CASE__ : Tuple = use_labels SCREAMING_SNAKE_CASE__ : List[str] = vocab_size SCREAMING_SNAKE_CASE__ : Optional[int] = hidden_size SCREAMING_SNAKE_CASE__ : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE__ : str = num_attention_heads SCREAMING_SNAKE_CASE__ : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE__ : Tuple = hidden_act SCREAMING_SNAKE_CASE__ : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE__ : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ : Any = max_position_embeddings SCREAMING_SNAKE_CASE__ : List[str] = type_vocab_size SCREAMING_SNAKE_CASE__ : Dict = type_sequence_label_size SCREAMING_SNAKE_CASE__ : List[str] = initializer_range SCREAMING_SNAKE_CASE__ : List[str] = num_labels SCREAMING_SNAKE_CASE__ : Optional[int] = num_choices SCREAMING_SNAKE_CASE__ : List[str] = relative_attention SCREAMING_SNAKE_CASE__ : str = position_biased_input SCREAMING_SNAKE_CASE__ : List[str] = pos_att_type SCREAMING_SNAKE_CASE__ : Union[str, Any] = scope def _a ( self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) SCREAMING_SNAKE_CASE__ : str = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE__ : Optional[Any] = None SCREAMING_SNAKE_CASE__ : int = None SCREAMING_SNAKE_CASE__ : Any = None if self.use_labels: SCREAMING_SNAKE_CASE__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE__ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE__ : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _a ( self ) -> Tuple: """simple docstring""" return DebertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , pos_att_type=self.pos_att_type , ) def _a ( self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = self.get_config() SCREAMING_SNAKE_CASE__ : Any = 300 return config def _a ( self , _a ) -> List[str]: """simple docstring""" self.parent.assertListEqual(list(result.loss.size() ) , [] ) def _a ( self , _a , _a , _a , _a , _a , _a , _a ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = DebertaModel(config=_a ) model.to(_a ) model.eval() SCREAMING_SNAKE_CASE__ : Any = model(_a , attention_mask=_a , token_type_ids=_a )[0] SCREAMING_SNAKE_CASE__ : Union[str, Any] = model(_a , token_type_ids=_a )[0] SCREAMING_SNAKE_CASE__ : Union[str, Any] = model(_a )[0] self.parent.assertListEqual(list(sequence_output.size() ) , [self.batch_size, self.seq_length, self.hidden_size] ) def _a ( self , _a , _a , _a , _a , _a , _a , _a ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = DebertaForMaskedLM(config=_a ) model.to(_a ) model.eval() SCREAMING_SNAKE_CASE__ : Optional[int] = model(_a , attention_mask=_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _a ( self , _a , _a , _a , _a , _a , _a , _a ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = self.num_labels SCREAMING_SNAKE_CASE__ : Tuple = DebertaForSequenceClassification(_a ) model.to(_a ) model.eval() SCREAMING_SNAKE_CASE__ : Any = model(_a , attention_mask=_a , token_type_ids=_a , labels=_a ) self.parent.assertListEqual(list(result.logits.size() ) , [self.batch_size, self.num_labels] ) self.check_loss_output(_a ) def _a ( self , _a , _a , _a , _a , _a , _a , _a ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = self.num_labels SCREAMING_SNAKE_CASE__ : Optional[Any] = DebertaForTokenClassification(config=_a ) model.to(_a ) model.eval() SCREAMING_SNAKE_CASE__ : int = model(_a , attention_mask=_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _a ( self , _a , _a , _a , _a , _a , _a , _a ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = DebertaForQuestionAnswering(config=_a ) model.to(_a ) model.eval() SCREAMING_SNAKE_CASE__ : List[str] = model( _a , attention_mask=_a , token_type_ids=_a , start_positions=_a , end_positions=_a , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _a ( self ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = 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__ ) , ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE__ : List[Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class __a (UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): '''simple docstring''' _SCREAMING_SNAKE_CASE :List[str] = ( ( DebertaModel, DebertaForMaskedLM, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaForQuestionAnswering, ) if is_torch_available() else () ) _SCREAMING_SNAKE_CASE :str = ( { """feature-extraction""": DebertaModel, """fill-mask""": DebertaForMaskedLM, """question-answering""": DebertaForQuestionAnswering, """text-classification""": DebertaForSequenceClassification, """token-classification""": DebertaForTokenClassification, """zero-shot""": DebertaForSequenceClassification, } if is_torch_available() else {} ) _SCREAMING_SNAKE_CASE :Union[str, Any] = True _SCREAMING_SNAKE_CASE :str = False _SCREAMING_SNAKE_CASE :Dict = False _SCREAMING_SNAKE_CASE :Dict = False _SCREAMING_SNAKE_CASE :Union[str, Any] = False def _a ( self ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = DebertaModelTester(self ) SCREAMING_SNAKE_CASE__ : str = ConfigTester(self , config_class=_a , hidden_size=37 ) def _a ( self ) -> Any: """simple docstring""" self.config_tester.run_common_tests() def _a ( self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*_a ) def _a ( self ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*_a ) def _a ( self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*_a ) def _a ( self ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*_a ) def _a ( self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*_a ) @slow def _a ( self ) -> Optional[int]: """simple docstring""" for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE__ : Dict = DebertaModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @require_torch @require_sentencepiece @require_tokenizers class __a (unittest.TestCase): '''simple docstring''' @unittest.skip(reason="""Model not available yet""" ) def _a ( self ) -> Any: """simple docstring""" pass @slow def _a ( self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = DebertaModel.from_pretrained("""microsoft/deberta-base""" ) SCREAMING_SNAKE_CASE__ : Tuple = torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]] ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE__ : Optional[int] = model(_a , attention_mask=_a )[0] # compare the actual values for a slice. SCREAMING_SNAKE_CASE__ : Union[str, Any] = torch.tensor( [[[-0.5_986, -0.8_055, -0.8_462], [1.4_484, -0.9_348, -0.8_059], [0.3_123, 0.0_032, -1.4_131]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _a , atol=1E-4 ) , f'''{output[:, 1:4, 1:4]}''' )
56
1
from __future__ import annotations import math def _SCREAMING_SNAKE_CASE ( a , a ) -> float: __A : Tuple = u for i in range(1 , a ): __A : int = temp * (u - i) return temp def _SCREAMING_SNAKE_CASE ( ) -> None: __A : str = int(input('enter the numbers of values: ' ) ) __A : list[list[float]] = [] for _ in range(a ): y.append([] ) for i in range(a ): for j in range(a ): y[i].append(a ) __A : Dict = 0 print('enter the values of parameters in a list: ' ) __A : str = list(map(a , input().split() ) ) print('enter the values of corresponding parameters: ' ) for i in range(a ): __A : Optional[int] = float(input() ) __A : str = int(input('enter the value to interpolate: ' ) ) __A : Union[str, Any] = (value - x[0]) / (x[1] - x[0]) # for calculating forward difference table for i in range(1 , a ): for j in range(n - i ): __A : List[Any] = y[j + 1][i - 1] - y[j][i - 1] __A : List[str] = y[0][0] for i in range(1 , a ): summ += (ucal(a , a ) * y[0][i]) / math.factorial(a ) print(F"""the value at {value} is {summ}""" ) if __name__ == "__main__": main()
280
def _SCREAMING_SNAKE_CASE ( a , a = 0 ) -> list: __A : int = length or len(a ) __A : str = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: __A , __A : Optional[int] = list_data[i + 1], list_data[i] __A : Union[str, Any] = True return list_data if not swapped else bubble_sort(a , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
280
1
import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger UpperCAmelCase__ = get_logger(__name__) UpperCAmelCase__ = r"\n Args:\n input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):\n Indices of input sequence tokens in the vocabulary.\n\n Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and\n [`PreTrainedTokenizer.__call__`] for details.\n\n [What are input IDs?](../glossary#input-ids)\n scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):\n Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam\n search or log softmax for each vocabulary token when using beam search\n kwargs (`Dict[str, Any]`, *optional*):\n Additional logits processor specific kwargs.\n\n Return:\n `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.\n\n" class __lowerCAmelCase : @add_start_docstrings(A) def __call__( self : Optional[int] , A : jnp.ndarray , A : jnp.ndarray) -> jnp.ndarray: """simple docstring""" raise NotImplementedError( F"{self.__class__} is an abstract class. Only classes inheriting this class can be called.") class __lowerCAmelCase : @add_start_docstrings(A) def __call__( self : List[Any] , A : jnp.ndarray , A : jnp.ndarray) -> jnp.ndarray: """simple docstring""" raise NotImplementedError( F"{self.__class__} is an abstract class. Only classes inheriting this class can be called.") class __lowerCAmelCase ( A ): @add_start_docstrings(A) def __call__( self : Union[str, Any] , A : jnp.ndarray , A : jnp.ndarray , A : int , **A : List[Any]) -> jnp.ndarray: """simple docstring""" for processor in self: _UpperCAmelCase = inspect.signature(processor.__call__).parameters if len(A) > 3: if not all(arg in kwargs for arg in list(function_args.keys())[2:]): raise ValueError( F"Make sure that all the required parameters: {list(function_args.keys())} for " F"{processor.__class__} are passed to the logits processor.") _UpperCAmelCase = processor(A , A , A , **A) else: _UpperCAmelCase = processor(A , A , A) return scores class __lowerCAmelCase ( A ): def __init__( self : List[str] , A : float) -> Optional[int]: """simple docstring""" if not isinstance(A , A) or not (temperature > 0): raise ValueError(F"`temperature` has to be a strictly positive float, but is {temperature}") _UpperCAmelCase = temperature def __call__( self : Union[str, Any] , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" _UpperCAmelCase = scores / self.temperature return scores class __lowerCAmelCase ( A ): def __init__( self : Union[str, Any] , A : float , A : float = -float('Inf') , A : int = 1) -> str: """simple docstring""" if not isinstance(A , A) or (top_p < 0 or top_p > 1.0): raise ValueError(F"`top_p` has to be a float > 0 and < 1, but is {top_p}") if not isinstance(A , A) or (min_tokens_to_keep < 1): raise ValueError(F"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}") _UpperCAmelCase = top_p _UpperCAmelCase = filter_value _UpperCAmelCase = min_tokens_to_keep def __call__( self : Optional[Any] , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = lax.top_k(A , scores.shape[-1]) _UpperCAmelCase = jnp.full_like(A , self.filter_value) _UpperCAmelCase = jax.nn.softmax(A , axis=-1).cumsum(axis=-1) _UpperCAmelCase = cumulative_probs < self.top_p # include the token that is higher than top_p as well _UpperCAmelCase = jnp.roll(A , 1) score_mask |= score_mask.at[:, 0].set(A) # min tokens to keep _UpperCAmelCase = score_mask.at[:, : self.min_tokens_to_keep].set(A) _UpperCAmelCase = jnp.where(A , A , A) _UpperCAmelCase = jax.lax.sort_key_val(A , A)[-1] return next_scores class __lowerCAmelCase ( A ): def __init__( self : Any , A : int , A : float = -float('Inf') , A : int = 1) -> Optional[Any]: """simple docstring""" if not isinstance(A , A) or top_k <= 0: raise ValueError(F"`top_k` has to be a strictly positive integer, but is {top_k}") _UpperCAmelCase = max(A , A) _UpperCAmelCase = filter_value def __call__( self : Any , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = scores.shape _UpperCAmelCase = jnp.full(batch_size * vocab_size , self.filter_value) _UpperCAmelCase = min(self.top_k , scores.shape[-1]) # Safety check _UpperCAmelCase , _UpperCAmelCase = lax.top_k(A , A) _UpperCAmelCase = jnp.broadcast_to((jnp.arange(A) * vocab_size)[:, None] , (batch_size, topk)).flatten() _UpperCAmelCase = topk_scores.flatten() _UpperCAmelCase = topk_indices.flatten() + shift _UpperCAmelCase = next_scores_flat.at[topk_indices_flat].set(A) _UpperCAmelCase = next_scores_flat.reshape(A , A) return next_scores class __lowerCAmelCase ( A ): def __init__( self : List[str] , A : int) -> Any: """simple docstring""" _UpperCAmelCase = bos_token_id def __call__( self : List[str] , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" _UpperCAmelCase = jnp.full(scores.shape , -float('inf')) _UpperCAmelCase = 1 - jnp.bool_(cur_len - 1) _UpperCAmelCase = jnp.where(A , new_scores.at[:, self.bos_token_id].set(0) , A) return scores class __lowerCAmelCase ( A ): def __init__( self : List[Any] , A : int , A : int) -> Dict: """simple docstring""" _UpperCAmelCase = max_length _UpperCAmelCase = eos_token_id def __call__( self : int , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" _UpperCAmelCase = jnp.full(scores.shape , -float('inf')) _UpperCAmelCase = 1 - jnp.bool_(cur_len - self.max_length + 1) _UpperCAmelCase = jnp.where(A , new_scores.at[:, self.eos_token_id].set(0) , A) return scores class __lowerCAmelCase ( A ): def __init__( self : Union[str, Any] , A : int , A : int) -> Tuple: """simple docstring""" if not isinstance(A , A) or min_length < 0: raise ValueError(F"`min_length` has to be a positive integer, but is {min_length}") if not isinstance(A , A) or eos_token_id < 0: raise ValueError(F"`eos_token_id` has to be a positive integer, but is {eos_token_id}") _UpperCAmelCase = min_length _UpperCAmelCase = eos_token_id def __call__( self : List[str] , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" _UpperCAmelCase = 1 - jnp.clip(cur_len - self.min_length , 0 , 1) _UpperCAmelCase = jnp.where(A , scores.at[:, self.eos_token_id].set(-float('inf')) , A) return scores class __lowerCAmelCase ( A ): def __init__( self : Dict , A : List[Any] , A : List[Any]) -> Any: """simple docstring""" _UpperCAmelCase = list(A) _UpperCAmelCase = begin_index def __call__( self : Any , A : Optional[Any] , A : Tuple , A : int) -> Optional[Any]: """simple docstring""" _UpperCAmelCase = 1 - jnp.bool_(cur_len - self.begin_index) _UpperCAmelCase = jnp.where(A , scores.at[:, self.begin_suppress_tokens].set(-float('inf')) , A) return scores class __lowerCAmelCase ( A ): def __init__( self : Dict , A : list) -> List[Any]: """simple docstring""" _UpperCAmelCase = list(A) def __call__( self : Union[str, Any] , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" _UpperCAmelCase = scores.at[..., self.suppress_tokens].set(-float('inf')) return scores class __lowerCAmelCase ( A ): def __init__( self : Optional[int] , A : Dict) -> int: """simple docstring""" _UpperCAmelCase = dict(A) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. _UpperCAmelCase = jnp.ones((max(force_token_map.keys()) + 1) , dtype=jnp.intaa) * -1 for index, token in force_token_map.items(): if token is not None: _UpperCAmelCase = force_token_array.at[index].set(A) _UpperCAmelCase = jnp.intaa(A) def __call__( self : Union[str, Any] , A : jnp.ndarray , A : jnp.ndarray , A : int) -> jnp.ndarray: """simple docstring""" def _force_token(A : Dict): _UpperCAmelCase = scores.shape[0] _UpperCAmelCase = self.force_token_array[generation_idx] _UpperCAmelCase = jnp.ones_like(A , dtype=scores.dtype) * -float('inf') _UpperCAmelCase = jnp.zeros((batch_size, 1) , dtype=scores.dtype) _UpperCAmelCase = lax.dynamic_update_slice(A , A , (0, current_token)) return new_scores _UpperCAmelCase = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(A) , lambda: scores , ) , ) return scores class __lowerCAmelCase ( A ): def __init__( self : int , A : str , A : List[Any] , A : List[Any]) -> List[Any]: """simple docstring""" _UpperCAmelCase = generate_config.eos_token_id _UpperCAmelCase = generate_config.no_timestamps_token_id _UpperCAmelCase = generate_config.no_timestamps_token_id + 1 _UpperCAmelCase = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(A , 'max_initial_timestamp_index'): _UpperCAmelCase = generate_config.max_initial_timestamp_index else: _UpperCAmelCase = model_config.vocab_size if self.max_initial_timestamp_index is None: _UpperCAmelCase = model_config.vocab_size def __call__( self : List[str] , A : Tuple , A : Optional[int] , A : Optional[int]) -> Any: """simple docstring""" _UpperCAmelCase = scores.at[:, self.no_timestamps_token_id].set(-float('inf')) def handle_pairs(A : Optional[Any] , A : Optional[Any]): _UpperCAmelCase = jnp.where((cur_len - self.begin_index) >= 1 , A , A) _UpperCAmelCase = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , A , ) _UpperCAmelCase = jnp.where((cur_len - self.begin_index) < 2 , A , A) _UpperCAmelCase = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , A , A , ) return jnp.where( A , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf')) , scores_k.at[: self.eos_token_id].set(-float('inf')) , ) , A , ) _UpperCAmelCase = jax.vmap(A)(A , A) _UpperCAmelCase = jnp.where(cur_len == self.begin_index , A , A) _UpperCAmelCase = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , A , ) _UpperCAmelCase = self.timestamp_begin + self.max_initial_timestamp_index _UpperCAmelCase = jnp.where( A , scores.at[:, last_allowed + 1 :].set(-float('inf')) , A , ) # if sum of probability over timestamps is above any other token, sample timestamp _UpperCAmelCase = jax.nn.log_softmax(A , axis=-1) def handle_cumulative_probs(A : Union[str, Any] , A : str): _UpperCAmelCase = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1) _UpperCAmelCase = jnp.max(logprobs_k[: self.timestamp_begin]) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf')) , A , ) _UpperCAmelCase = jax.vmap(A)(A , A) return scores
290
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase__ = { "vocab_file": { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt" ), } } UpperCAmelCase__ = { "junnyu/roformer_chinese_small": 1536, "junnyu/roformer_chinese_base": 1536, "junnyu/roformer_chinese_char_small": 512, "junnyu/roformer_chinese_char_base": 512, "junnyu/roformer_small_discriminator": 128, "junnyu/roformer_small_generator": 128, } UpperCAmelCase__ = { "junnyu/roformer_chinese_small": {"do_lower_case": True}, "junnyu/roformer_chinese_base": {"do_lower_case": True}, "junnyu/roformer_chinese_char_small": {"do_lower_case": True}, "junnyu/roformer_chinese_char_base": {"do_lower_case": True}, "junnyu/roformer_small_discriminator": {"do_lower_case": True}, "junnyu/roformer_small_generator": {"do_lower_case": True}, } class __lowerCAmelCase ( A ): UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = PRETRAINED_INIT_CONFIGURATION UpperCamelCase = RoFormerTokenizer def __init__( self : Dict , A : Any=None , A : Optional[Any]=None , A : Union[str, Any]=True , A : List[str]="[UNK]" , A : List[str]="[SEP]" , A : Union[str, Any]="[PAD]" , A : Any="[CLS]" , A : str="[MASK]" , A : Optional[int]=True , A : str=None , **A : Dict , ) -> Union[str, Any]: """simple docstring""" super().__init__( A , tokenizer_file=A , do_lower_case=A , unk_token=A , sep_token=A , pad_token=A , cls_token=A , mask_token=A , tokenize_chinese_chars=A , strip_accents=A , **A , ) _UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__()) if ( pre_tok_state.get('lowercase' , A) != do_lower_case or pre_tok_state.get('strip_accents' , A) != strip_accents ): _UpperCAmelCase = getattr(A , pre_tok_state.pop('type')) _UpperCAmelCase = do_lower_case _UpperCAmelCase = strip_accents _UpperCAmelCase = pre_tok_class(**A) _UpperCAmelCase = do_lower_case def __getstate__( self : Optional[Any]) -> Optional[Any]: """simple docstring""" _UpperCAmelCase = self.__dict__.copy() _UpperCAmelCase = BertPreTokenizer() return state def __setstate__( self : List[Any] , A : Optional[Any]) -> List[str]: """simple docstring""" _UpperCAmelCase = d _UpperCAmelCase = self.__dict__['_tokenizer'].get_vocab() _UpperCAmelCase = PreTokenizer.custom(JiebaPreTokenizer(A)) def _lowerCamelCase ( self : List[Any] , A : Optional[int] , A : List[Any]=None) -> Optional[Any]: """simple docstring""" _UpperCAmelCase = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def _lowerCamelCase ( self : List[str] , A : List[int] , A : Optional[List[int]] = None) -> List[int]: """simple docstring""" _UpperCAmelCase = [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] def _lowerCamelCase ( self : Union[str, Any] , A : str , A : Optional[str] = None) -> Tuple[str]: """simple docstring""" _UpperCAmelCase = self._tokenizer.model.save(A , name=A) return tuple(A) def _lowerCamelCase ( self : List[Any] , A : Tuple , A : str=None , A : Union[str, Any]=None , A : Union[str, Any]=False , **A : Tuple , ) -> str: """simple docstring""" _UpperCAmelCase = BertPreTokenizer() return super().save_pretrained(A , A , A , A , **A)
290
1
"""simple docstring""" import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @property def _UpperCAmelCase ( self ) -> Optional[Any]: torch.manual_seed(0 ) _a = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def _UpperCAmelCase ( self ) -> Tuple: _a = self.dummy_uncond_unet _a = ScoreSdeVeScheduler() _a = ScoreSdeVePipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) sde_ve.to(__UpperCAmelCase ) sde_ve.set_progress_bar_config(disable=__UpperCAmelCase ) _a = torch.manual_seed(0 ) _a = sde_ve(num_inference_steps=2 , output_type='''numpy''' , generator=__UpperCAmelCase ).images _a = torch.manual_seed(0 ) _a = sde_ve(num_inference_steps=2 , output_type='''numpy''' , generator=__UpperCAmelCase , return_dict=__UpperCAmelCase )[ 0 ] _a = image[0, -3:, -3:, -1] _a = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _a = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __lowerCamelCase ( unittest.TestCase ): '''simple docstring''' def _UpperCAmelCase ( self ) -> str: _a = '''google/ncsnpp-church-256''' _a = UNetaDModel.from_pretrained(__UpperCAmelCase ) _a = ScoreSdeVeScheduler.from_pretrained(__UpperCAmelCase ) _a = ScoreSdeVePipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) sde_ve.to(__UpperCAmelCase ) sde_ve.set_progress_bar_config(disable=__UpperCAmelCase ) _a = torch.manual_seed(0 ) _a = sde_ve(num_inference_steps=10 , output_type='''numpy''' , generator=__UpperCAmelCase ).images _a = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) _a = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
320
"""simple docstring""" from collections import deque from math import floor from random import random from time import time class __lowerCamelCase : '''simple docstring''' def __init__( self ) -> Tuple: _a = {} def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=1 ) -> int: if self.graph.get(__UpperCAmelCase ): if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: _a = [[w, v]] if not self.graph.get(__UpperCAmelCase ): _a = [] def _UpperCAmelCase ( self ) -> int: return list(self.graph ) def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase ) -> List[str]: if self.graph.get(__UpperCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(__UpperCAmelCase ) def _UpperCAmelCase ( self , __UpperCAmelCase=-2 , __UpperCAmelCase=-1 ) -> Optional[int]: if s == d: return [] _a = [] _a = [] if s == -2: _a = list(self.graph )[0] stack.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) _a = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _a = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(__UpperCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) _a = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(__UpperCAmelCase ) != 0: _a = stack[len(__UpperCAmelCase ) - 1] else: _a = ss # check if se have reached the starting point if len(__UpperCAmelCase ) == 0: return visited def _UpperCAmelCase ( self , __UpperCAmelCase=-1 ) -> Tuple: if c == -1: _a = floor(random() * 10000 ) + 10 for i in range(__UpperCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): _a = floor(random() * c ) + 1 if n != i: self.add_pair(__UpperCAmelCase , __UpperCAmelCase , 1 ) def _UpperCAmelCase ( self , __UpperCAmelCase=-2 ) -> List[str]: _a = deque() _a = [] if s == -2: _a = list(self.graph )[0] d.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) while d: _a = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def _UpperCAmelCase ( self , __UpperCAmelCase ) -> Tuple: _a = 0 for x in self.graph: for y in self.graph[x]: if y[1] == u: count += 1 return count def _UpperCAmelCase ( self , __UpperCAmelCase ) -> Dict: return len(self.graph[u] ) def _UpperCAmelCase ( self , __UpperCAmelCase=-2 ) -> Tuple: _a = [] _a = [] if s == -2: _a = list(self.graph )[0] stack.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) _a = s _a = [] while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _a = s for node in self.graph[s]: if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _a = node[1] break # check if all the children are visited if s == ss: sorted_nodes.append(stack.pop() ) if len(__UpperCAmelCase ) != 0: _a = stack[len(__UpperCAmelCase ) - 1] else: _a = ss # check if se have reached the starting point if len(__UpperCAmelCase ) == 0: return sorted_nodes def _UpperCAmelCase ( self ) -> Optional[int]: _a = [] _a = [] _a = list(self.graph )[0] stack.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) _a = -2 _a = [] _a = s _a = False _a = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _a = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _a = len(__UpperCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _a = node[1] break # check if all the children are visited if s == ss: stack.pop() _a = True if len(__UpperCAmelCase ) != 0: _a = stack[len(__UpperCAmelCase ) - 1] else: _a = False indirect_parents.append(__UpperCAmelCase ) _a = s _a = ss # check if se have reached the starting point if len(__UpperCAmelCase ) == 0: return list(__UpperCAmelCase ) def _UpperCAmelCase ( self ) -> Any: _a = [] _a = [] _a = list(self.graph )[0] stack.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) _a = -2 _a = [] _a = s _a = False _a = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _a = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _a = len(__UpperCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _a = node[1] break # check if all the children are visited if s == ss: stack.pop() _a = True if len(__UpperCAmelCase ) != 0: _a = stack[len(__UpperCAmelCase ) - 1] else: _a = False indirect_parents.append(__UpperCAmelCase ) _a = s _a = ss # check if se have reached the starting point if len(__UpperCAmelCase ) == 0: return False def _UpperCAmelCase ( self , __UpperCAmelCase=-2 , __UpperCAmelCase=-1 ) -> Optional[int]: _a = time() self.dfs(__UpperCAmelCase , __UpperCAmelCase ) _a = time() return end - begin def _UpperCAmelCase ( self , __UpperCAmelCase=-2 ) -> Optional[Any]: _a = time() self.bfs(__UpperCAmelCase ) _a = time() return end - begin class __lowerCamelCase : '''simple docstring''' def __init__( self ) -> Optional[int]: _a = {} def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=1 ) -> Dict: # check if the u exists if self.graph.get(__UpperCAmelCase ): # if there already is a edge if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: # if u does not exist _a = [[w, v]] # add the other way if self.graph.get(__UpperCAmelCase ): # if there already is a edge if self.graph[v].count([w, u] ) == 0: self.graph[v].append([w, u] ) else: # if u does not exist _a = [[w, u]] def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: if self.graph.get(__UpperCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(__UpperCAmelCase ) # the other way round if self.graph.get(__UpperCAmelCase ): for _ in self.graph[v]: if _[1] == u: self.graph[v].remove(__UpperCAmelCase ) def _UpperCAmelCase ( self , __UpperCAmelCase=-2 , __UpperCAmelCase=-1 ) -> Dict: if s == d: return [] _a = [] _a = [] if s == -2: _a = list(self.graph )[0] stack.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) _a = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _a = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(__UpperCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) _a = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(__UpperCAmelCase ) != 0: _a = stack[len(__UpperCAmelCase ) - 1] else: _a = ss # check if se have reached the starting point if len(__UpperCAmelCase ) == 0: return visited def _UpperCAmelCase ( self , __UpperCAmelCase=-1 ) -> Tuple: if c == -1: _a = floor(random() * 10000 ) + 10 for i in range(__UpperCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): _a = floor(random() * c ) + 1 if n != i: self.add_pair(__UpperCAmelCase , __UpperCAmelCase , 1 ) def _UpperCAmelCase ( self , __UpperCAmelCase=-2 ) -> List[Any]: _a = deque() _a = [] if s == -2: _a = list(self.graph )[0] d.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) while d: _a = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def _UpperCAmelCase ( self , __UpperCAmelCase ) -> Dict: return len(self.graph[u] ) def _UpperCAmelCase ( self ) -> int: _a = [] _a = [] _a = list(self.graph )[0] stack.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) _a = -2 _a = [] _a = s _a = False _a = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _a = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _a = len(__UpperCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _a = node[1] break # check if all the children are visited if s == ss: stack.pop() _a = True if len(__UpperCAmelCase ) != 0: _a = stack[len(__UpperCAmelCase ) - 1] else: _a = False indirect_parents.append(__UpperCAmelCase ) _a = s _a = ss # check if se have reached the starting point if len(__UpperCAmelCase ) == 0: return list(__UpperCAmelCase ) def _UpperCAmelCase ( self ) -> Optional[Any]: _a = [] _a = [] _a = list(self.graph )[0] stack.append(__UpperCAmelCase ) visited.append(__UpperCAmelCase ) _a = -2 _a = [] _a = s _a = False _a = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _a = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _a = len(__UpperCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _a = node[1] break # check if all the children are visited if s == ss: stack.pop() _a = True if len(__UpperCAmelCase ) != 0: _a = stack[len(__UpperCAmelCase ) - 1] else: _a = False indirect_parents.append(__UpperCAmelCase ) _a = s _a = ss # check if se have reached the starting point if len(__UpperCAmelCase ) == 0: return False def _UpperCAmelCase ( self ) -> Union[str, Any]: return list(self.graph ) def _UpperCAmelCase ( self , __UpperCAmelCase=-2 , __UpperCAmelCase=-1 ) -> Tuple: _a = time() self.dfs(__UpperCAmelCase , __UpperCAmelCase ) _a = time() return end - begin def _UpperCAmelCase ( self , __UpperCAmelCase=-2 ) -> Tuple: _a = time() self.bfs(__UpperCAmelCase ) _a = time() return end - begin
320
1
'''simple docstring''' import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class UpperCAmelCase ( UpperCamelCase__ ): __lowercase = (UniPCMultistepScheduler,) __lowercase = (("""num_inference_steps""", 25),) def UpperCAmelCase_ ( self :List[Any] , **lowercase_ :Optional[int] )-> List[str]: A__ = { "num_train_timesteps": 10_00, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", "solver_order": 2, "solver_type": "bh2", } config.update(**lowercase_ ) return config def UpperCAmelCase_ ( self :List[str] , lowercase_ :Union[str, Any]=0 , **lowercase_ :Dict )-> str: A__ = dict(self.forward_default_kwargs ) A__ = kwargs.pop("num_inference_steps" , lowercase_ ) A__ = self.dummy_sample A__ = 0.1 * sample A__ = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: A__ = self.get_scheduler_config(**lowercase_ ) A__ = scheduler_class(**lowercase_ ) scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals A__ = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_ ) A__ = scheduler_class.from_pretrained(lowercase_ ) new_scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals A__ = dummy_past_residuals[: new_scheduler.config.solver_order] A__, A__ = sample, sample for t in range(lowercase_ , time_step + scheduler.config.solver_order + 1 ): A__ = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample A__ = new_scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCAmelCase_ ( self :int , lowercase_ :Union[str, Any]=0 , **lowercase_ :Any )-> Optional[Any]: A__ = dict(self.forward_default_kwargs ) A__ = kwargs.pop("num_inference_steps" , lowercase_ ) A__ = self.dummy_sample A__ = 0.1 * sample A__ = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: A__ = self.get_scheduler_config() A__ = scheduler_class(**lowercase_ ) scheduler.set_timesteps(lowercase_ ) # copy over dummy past residuals (must be after setting timesteps) A__ = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowercase_ ) A__ = scheduler_class.from_pretrained(lowercase_ ) # copy over dummy past residuals new_scheduler.set_timesteps(lowercase_ ) # copy over dummy past residual (must be after setting timesteps) A__ = dummy_past_residuals[: new_scheduler.config.solver_order] A__ = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample A__ = new_scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def UpperCAmelCase_ ( self :str , lowercase_ :Any=None , **lowercase_ :Dict )-> Dict: if scheduler is None: A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config(**lowercase_ ) A__ = scheduler_class(**lowercase_ ) A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config(**lowercase_ ) A__ = scheduler_class(**lowercase_ ) A__ = 10 A__ = self.dummy_model() A__ = self.dummy_sample_deter scheduler.set_timesteps(lowercase_ ) for i, t in enumerate(scheduler.timesteps ): A__ = model(lowercase_ , lowercase_ ) A__ = scheduler.step(lowercase_ , lowercase_ , lowercase_ ).prev_sample return sample def UpperCAmelCase_ ( self :int )-> Any: A__ = dict(self.forward_default_kwargs ) A__ = kwargs.pop("num_inference_steps" , lowercase_ ) for scheduler_class in self.scheduler_classes: A__ = self.get_scheduler_config() A__ = scheduler_class(**lowercase_ ) A__ = self.dummy_sample A__ = 0.1 * sample if num_inference_steps is not None and hasattr(lowercase_ , "set_timesteps" ): scheduler.set_timesteps(lowercase_ ) elif num_inference_steps is not None and not hasattr(lowercase_ , "set_timesteps" ): A__ = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) A__ = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] A__ = dummy_past_residuals[: scheduler.config.solver_order] A__ = scheduler.timesteps[5] A__ = scheduler.timesteps[6] A__ = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample A__ = scheduler.step(lowercase_ , lowercase_ , lowercase_ , **lowercase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def UpperCAmelCase_ ( self :Tuple )-> str: # make sure that iterating over schedulers with same config names gives same results # for defaults A__ = UniPCMultistepScheduler(**self.get_scheduler_config() ) A__ = self.full_loop(scheduler=lowercase_ ) A__ = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2_4_6_4 ) < 1E-3 A__ = DPMSolverSinglestepScheduler.from_config(scheduler.config ) A__ = DEISMultistepScheduler.from_config(scheduler.config ) A__ = DPMSolverMultistepScheduler.from_config(scheduler.config ) A__ = UniPCMultistepScheduler.from_config(scheduler.config ) A__ = self.full_loop(scheduler=lowercase_ ) A__ = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2_4_6_4 ) < 1E-3 def UpperCAmelCase_ ( self :Union[str, Any] )-> Union[str, Any]: for timesteps in [25, 50, 1_00, 9_99, 10_00]: self.check_over_configs(num_train_timesteps=lowercase_ ) def UpperCAmelCase_ ( self :List[str] )-> str: self.check_over_configs(thresholding=lowercase_ ) for order in [1, 2, 3]: for solver_type in ["bh1", "bh2"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=lowercase_ , prediction_type=lowercase_ , sample_max_value=lowercase_ , solver_order=lowercase_ , solver_type=lowercase_ , ) def UpperCAmelCase_ ( self :str )-> Dict: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowercase_ ) def UpperCAmelCase_ ( self :str )-> Union[str, Any]: for solver_type in ["bh1", "bh2"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=lowercase_ , solver_type=lowercase_ , prediction_type=lowercase_ , ) A__ = self.full_loop( solver_order=lowercase_ , solver_type=lowercase_ , prediction_type=lowercase_ , ) assert not torch.isnan(lowercase_ ).any(), "Samples have nan numbers" def UpperCAmelCase_ ( self :Dict )-> Any: self.check_over_configs(lower_order_final=lowercase_ ) self.check_over_configs(lower_order_final=lowercase_ ) def UpperCAmelCase_ ( self :str )-> Union[str, Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 1_00, 9_99, 10_00]: self.check_over_forward(num_inference_steps=lowercase_ , time_step=0 ) def UpperCAmelCase_ ( self :Optional[Any] )-> str: A__ = self.full_loop() A__ = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.2_4_6_4 ) < 1E-3 def UpperCAmelCase_ ( self :Optional[int] )-> str: A__ = self.full_loop(prediction_type="v_prediction" ) A__ = torch.mean(torch.abs(lowercase_ ) ) assert abs(result_mean.item() - 0.1_0_1_4 ) < 1E-3 def UpperCAmelCase_ ( self :Tuple )-> List[Any]: A__ = self.scheduler_classes[0] A__ = self.get_scheduler_config(thresholding=lowercase_ , dynamic_thresholding_ratio=0 ) A__ = scheduler_class(**lowercase_ ) A__ = 10 A__ = self.dummy_model() A__ = self.dummy_sample_deter.half() scheduler.set_timesteps(lowercase_ ) for i, t in enumerate(scheduler.timesteps ): A__ = model(lowercase_ , lowercase_ ) A__ = scheduler.step(lowercase_ , lowercase_ , lowercase_ ).prev_sample assert sample.dtype == torch.floataa def UpperCAmelCase_ ( self :Optional[Any] , **lowercase_ :str )-> Any: for scheduler_class in self.scheduler_classes: A__ = self.get_scheduler_config(**lowercase_ ) A__ = scheduler_class(**lowercase_ ) scheduler.set_timesteps(scheduler.config.num_train_timesteps ) assert len(scheduler.timesteps.unique() ) == scheduler.num_inference_steps
123
'''simple docstring''' def UpperCamelCase ( ): A__ = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] A__ = 6 A__ = 1 A__ = 19_01 A__ = 0 while year < 20_01: day += 7 if (year % 4 == 0 and year % 1_00 != 0) or (year % 4_00 == 0): if day > days_per_month[month - 1] and month != 2: month += 1 A__ = day - days_per_month[month - 2] elif day > 29 and month == 2: month += 1 A__ = day - 29 else: if day > days_per_month[month - 1]: month += 1 A__ = day - days_per_month[month - 2] if month > 12: year += 1 A__ = 1 if year < 20_01 and day == 1: sundays += 1 return sundays if __name__ == "__main__": print(solution())
123
1
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging __snake_case = logging.get_logger(__name__) def a ( __a , __a , __a ) -> None: '''simple docstring''' UpperCamelCase__ :Dict = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(__a ) == len(__a ), f'''{len(__a )} != {len(__a )}''' dest_layers.load_state_dict(layers_to_copy.state_dict() ) __snake_case = { # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 12: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 11], 4: [0, 4, 8, 11], 6: [0, 2, 4, 7, 9, 11], 9: [0, 1, 2, 4, 5, 7, 9, 10, 11], 12: list(range(12)), }, 16: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 15], 3: [0, 8, 15], 4: [0, 5, 10, 15], 6: [0, 3, 6, 9, 12, 15], 8: [0, 2, 4, 6, 8, 10, 12, 15], 9: [0, 1, 3, 5, 7, 9, 11, 13, 15], 12: [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15], 16: list(range(16)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } __snake_case = { # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 12: {1: [11], 2: [5, 11], 3: [3, 7, 11], 6: [1, 3, 5, 8, 10, 11]}, 16: {1: [15], 4: [4, 9, 12, 15], 8: [1, 3, 5, 7, 9, 11, 13, 15]}, } def a ( __a , __a ) -> Any: '''simple docstring''' try: UpperCamelCase__ :List[str] = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( f'''no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first''' f''' {n_student}''' ) return list(range(__a ) ) def a ( __a , __a ) -> List[int]: '''simple docstring''' if n_student > n_teacher: raise ValueError(f'''Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}''' ) elif n_teacher == n_student: return list(range(__a ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def a ( __a , __a = "student" , __a = None , __a = None , __a=False , __a=None , __a=None , **__a , ) -> Tuple[PreTrainedModel, List[int], List[int]]: '''simple docstring''' UpperCamelCase__ :Optional[int] = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(__a , __a ): AutoTokenizer.from_pretrained(__a ).save_pretrained(__a ) # purely for convenience UpperCamelCase__ :Union[str, Any] = AutoModelForSeqaSeqLM.from_pretrained(__a ).eval() else: assert isinstance(__a , __a ), f'''teacher must be a model or string got type {type(__a )}''' UpperCamelCase__ :Optional[int] = teacher.config.to_diff_dict() try: UpperCamelCase__ , UpperCamelCase__ :Union[str, Any] = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: UpperCamelCase__ :Dict = teacher_e if d is None: UpperCamelCase__ :Tuple = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): UpperCamelCase__ , UpperCamelCase__ :Dict = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: UpperCamelCase__ , UpperCamelCase__ :int = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: UpperCamelCase__ :List[str] = teacher_e if d is None: UpperCamelCase__ :Dict = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(__a ) # Copy weights UpperCamelCase__ :Union[str, Any] = teacher.config_class(**__a ) UpperCamelCase__ :str = AutoModelForSeqaSeqLM.from_config(__a ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. UpperCamelCase__ :Tuple = student.load_state_dict(teacher.state_dict() , strict=__a ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save UpperCamelCase__ , UpperCamelCase__ :Any = list(range(__a ) ), list(range(__a ) ) logger.info( f'''Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to''' f''' {save_path}''' ) student.save_pretrained(__a ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: UpperCamelCase__ :List[int] = pick_layers_to_copy(__a , __a ) if d_layers_to_copy is None: UpperCamelCase__ :List[int] = pick_layers_to_copy(__a , __a ) try: if hasattr( __a , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , __a ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , __a ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , __a ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , __a ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , __a ) copy_layers(teacher.decoder.block , student.decoder.block , __a ) logger.info( f'''Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}''' ) UpperCamelCase__ :Union[str, Any] = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(__a ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
97
'''simple docstring''' from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class A__ : lowercase = 42 lowercase = None lowercase = None def UpperCAmelCase__ ( ) -> Node | None: A_ = Node(1 ) A_ = Node(2 ) A_ = Node(3 ) A_ = Node(4 ) A_ = Node(5 ) return tree def UpperCAmelCase__ ( UpperCAmelCase__ ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: return (max(height(root.left ), height(root.right ) ) + 1) if root else 0 def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Sequence[Node | None]: A_ = [] if root is None: return output A_ = deque([root] ) while process_queue: A_ = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Sequence[Node | None]: A_ = [] def populate_output(UpperCAmelCase__, UpperCAmelCase__ ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left, level - 1 ) populate_output(root.right, level - 1 ) populate_output(UpperCAmelCase__, UpperCAmelCase__ ) return output def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Sequence[Node | None]: A_ = [] def populate_output(UpperCAmelCase__, UpperCAmelCase__ ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right, level - 1 ) populate_output(root.left, level - 1 ) populate_output(UpperCAmelCase__, UpperCAmelCase__ ) return output def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Sequence[Node | None] | list[Any]: if root is None: return [] A_ = [] A_ = 0 A_ = height(UpperCAmelCase__ ) for h in range(1, height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(UpperCAmelCase__, UpperCAmelCase__ ) ) A_ = 1 else: output.append(get_nodes_from_right_to_left(UpperCAmelCase__, UpperCAmelCase__ ) ) A_ = 0 return output def UpperCAmelCase__ ( ) -> None: # Main function for testing. A_ = make_tree() print(F'''In-order Traversal: {inorder(UpperCAmelCase__ )}''' ) print(F'''Pre-order Traversal: {preorder(UpperCAmelCase__ )}''' ) print(F'''Post-order Traversal: {postorder(UpperCAmelCase__ )}''', """\n""" ) print(F'''Height of Tree: {height(UpperCAmelCase__ )}''', """\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(UpperCAmelCase__ ), """\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1, height(UpperCAmelCase__ ) + 1 ): print(F'''Level {level}:''', get_nodes_from_left_to_right(UpperCAmelCase__, level=UpperCAmelCase__ ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(UpperCAmelCase__ ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
162
0
"""simple docstring""" import unittest from transformers import SPIECE_UNDERLINE, ReformerTokenizer, ReformerTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A : Optional[int] = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = ReformerTokenizer __lowerCamelCase : Tuple = ReformerTokenizerFast __lowerCamelCase : List[Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = True def a_ ( self : str ) -> List[str]: """simple docstring""" super().setUp() A__ = ReformerTokenizer(__lowerCAmelCase , keep_accents=__lowerCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def a_ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" A__ = """<s>""" A__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCAmelCase ) , __lowerCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCAmelCase ) , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" A__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<unk>""" ) self.assertEqual(vocab_keys[1] , """<s>""" ) self.assertEqual(vocab_keys[-1] , """j""" ) self.assertEqual(len(__lowerCAmelCase ) , 10_00 ) def a_ ( self : List[Any] ) -> Tuple: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 10_00 ) def a_ ( self : Any ) -> int: """simple docstring""" if not self.test_rust_tokenizer: return A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = """I was born in 92000, and this is falsé.""" A__ = tokenizer.tokenize(__lowerCAmelCase ) A__ = rust_tokenizer.tokenize(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) A__ = tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) A__ = rust_tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) A__ = self.get_rust_tokenizer() A__ = tokenizer.encode(__lowerCAmelCase ) A__ = rust_tokenizer.encode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : Optional[int]=15 ) -> Union[str, Any]: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): A__ = self.rust_tokenizer_class.from_pretrained(__lowerCAmelCase , **__lowerCAmelCase ) # Simple input A__ = """This is a simple input""" A__ = ["""This is a simple input 1""", """This is a simple input 2"""] A__ = ("""This is a simple input""", """This is a pair""") A__ = [ ("""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 a_ ( self : Any ) -> Any: """simple docstring""" pass def a_ ( self : str ) -> Any: """simple docstring""" A__ = ReformerTokenizer(__lowerCAmelCase , keep_accents=__lowerCAmelCase ) A__ = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(__lowerCAmelCase , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) , [2_85, 46, 10, 1_70, 3_82] , ) A__ = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) A__ = tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) self.assertListEqual( __lowerCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) A__ = tokenizer.convert_ids_to_tokens(__lowerCAmelCase ) self.assertListEqual( __lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) @cached_property def a_ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" return ReformerTokenizer.from_pretrained("""google/reformer-crime-and-punishment""" ) @slow def a_ ( self : List[str] ) -> str: """simple docstring""" A__ = """Hello World!""" A__ = [1_26, 32, 2_62, 1_52, 38, 72, 2_87] self.assertListEqual(__lowerCAmelCase , self.big_tokenizer.encode(__lowerCAmelCase ) ) @slow def a_ ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" A__ = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) A__ = [ 1_08, 2_65, 24, 1_11, 4, 2_58, 1_56, 35, 28, 2_75, 3, 2_59, 2_97, 2_60, 84, 4, 35, 1_10, 44, 8, 2_59, 91, 2_68, 21, 11, 2_09, 2_74, 1_09, 2_66, 2_77, 1_17, 86, 93, 3_15, 2_58, 2_78, 2_58, 2_77, 2_58, 0, 2_58, 2_88, 2_58, 3_19, 2_58, 0, 2_58, 0, 2_58, 0, 2_58, 0, 2_58, 2_87, 2_58, 3_15, 2_58, 2_89, 2_58, 2_78, 99, 2_69, 2_66, 2_62, 8, 2_59, 2_41, 4, 2_17, 2_30, 2_68, 2_66, 55, 1_68, 1_06, 75, 1_93, 2_66, 2_23, 27, 49, 26, 2_82, 25, 2_64, 2_99, 19, 26, 0, 2_58, 2_77, 1_17, 86, 93, 1_76, 1_83, 2_70, 11, 2_62, 42, 61, 2_65, ] self.assertListEqual(__lowerCAmelCase , self.big_tokenizer.encode(__lowerCAmelCase ) ) @require_torch @slow def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" import torch from transformers import ReformerConfig, ReformerModel # Build sequence A__ = list(self.big_tokenizer.get_vocab().keys() )[:10] A__ = """ """.join(__lowerCAmelCase ) A__ = self.big_tokenizer.encode_plus(__lowerCAmelCase , return_tensors="""pt""" ) A__ = self.big_tokenizer.batch_encode_plus([sequence, sequence] , return_tensors="""pt""" ) A__ = ReformerConfig() # The input gets padded during training so adjust the axial position encodings from the pretrained model value of (512, 1024) A__ = encoded_sequence["""input_ids"""].shape A__ = ReformerModel(__lowerCAmelCase ) # Reformer has config.vocab_size == tokenizer.vocab_size == len(tokenizer) - 1 = 320; len(tokenizer) is 321 (including a pad token with id 320) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__lowerCAmelCase ) model(**__lowerCAmelCase ) @slow def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = {"""input_ids""": [[1_08, 2_65, 24, 1_11, 4, 2_58, 1_56, 7, 51, 2_79, 58, 7, 76, 25, 69, 2_78], [1_40, 2_43, 2_64, 1_34, 17, 2_67, 77, 2_63, 22, 2_62, 2_97, 2_58, 3_04, 1_77, 2_79, 2_66, 14, 89, 13, 35, 2_61, 2_99, 2_72, 1_37, 2_75, 2_78]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # This tokenizer does not know some characters like ")". # That is the reason why we use very simple texts here. # Also see https://github.com/huggingface/transformers/pull/11737#issuecomment-850769064 A__ = [ """This is a very simple sentence.""", """The quick brown fox jumps over the lazy dog.""", ] self.tokenizer_integration_test_util( expected_encoding=__lowerCAmelCase , model_name="""google/reformer-crime-and-punishment""" , revision="""0e6c3decb8211d49bf881013425dc8b0448b3f5a""" , padding=__lowerCAmelCase , sequences=__lowerCAmelCase , )
367
def __lowerCamelCase ( __a :int = 3 , __a :int = 7 , __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = 0 A__ = 1 for current_denominator in range(1 , limit + 1 ): A__ = current_denominator * numerator // denominator if current_denominator % denominator == 0: current_numerator -= 1 if current_numerator * max_denominator > current_denominator * max_numerator: A__ = current_numerator A__ = current_denominator return max_numerator if __name__ == "__main__": print(solution(numerator=3, denominator=7, limit=1_0_0_0_0_0_0))
276
0
'''simple docstring''' import unittest from transformers import MobileBertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertModel, ) class lowercase : """simple docstring""" def __init__( self ,a_ ,a_=13 ,a_=7 ,a_=True ,a_=True ,a_=True ,a_=True ,a_=99 ,a_=64 ,a_=32 ,a_=5 ,a_=4 ,a_=37 ,a_="gelu" ,a_=0.1 ,a_=0.1 ,a_=512 ,a_=16 ,a_=2 ,a_=0.02 ,a_=3 ,a_=4 ,a_=None ,) -> int: _UpperCAmelCase : Optional[Any] = parent _UpperCAmelCase : Optional[int] = batch_size _UpperCAmelCase : Dict = seq_length _UpperCAmelCase : Dict = is_training _UpperCAmelCase : Any = use_input_mask _UpperCAmelCase : List[str] = use_token_type_ids _UpperCAmelCase : Optional[Any] = use_labels _UpperCAmelCase : Union[str, Any] = vocab_size _UpperCAmelCase : Optional[Any] = hidden_size _UpperCAmelCase : List[str] = embedding_size _UpperCAmelCase : Optional[Any] = num_hidden_layers _UpperCAmelCase : Tuple = num_attention_heads _UpperCAmelCase : str = intermediate_size _UpperCAmelCase : Optional[int] = hidden_act _UpperCAmelCase : List[Any] = hidden_dropout_prob _UpperCAmelCase : int = attention_probs_dropout_prob _UpperCAmelCase : Union[str, Any] = max_position_embeddings _UpperCAmelCase : List[Any] = type_vocab_size _UpperCAmelCase : List[Any] = type_sequence_label_size _UpperCAmelCase : Tuple = initializer_range _UpperCAmelCase : Tuple = num_labels _UpperCAmelCase : Union[str, Any] = num_choices _UpperCAmelCase : int = scope def _snake_case ( self ) -> Union[str, Any]: _UpperCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) _UpperCAmelCase : Union[str, Any] = None if self.use_input_mask: _UpperCAmelCase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase : Optional[Any] = None if self.use_token_type_ids: _UpperCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) _UpperCAmelCase : int = None _UpperCAmelCase : Optional[Any] = None _UpperCAmelCase : List[Any] = None if self.use_labels: _UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) _UpperCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) _UpperCAmelCase : Tuple = ids_tensor([self.batch_size] ,self.num_choices ) _UpperCAmelCase : Any = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _snake_case ( self ) -> Optional[Any]: return MobileBertConfig( 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 ,embedding_size=self.embedding_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,is_decoder=a_ ,initializer_range=self.initializer_range ,) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> Optional[int]: _UpperCAmelCase : Dict = MobileBertModel(config=a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : List[str] = model(a_ ,attention_mask=a_ ,token_type_ids=a_ ) _UpperCAmelCase : Optional[Any] = model(a_ ,token_type_ids=a_ ) _UpperCAmelCase : Dict = model(a_ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape ,(self.batch_size, self.hidden_size) ) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> Optional[int]: _UpperCAmelCase : Optional[int] = MobileBertForMaskedLM(config=a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : Optional[Any] = model(a_ ,attention_mask=a_ ,token_type_ids=a_ ,labels=a_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> List[str]: _UpperCAmelCase : List[str] = MobileBertForNextSentencePrediction(config=a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : List[str] = model( a_ ,attention_mask=a_ ,token_type_ids=a_ ,labels=a_ ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, 2) ) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> str: _UpperCAmelCase : Any = MobileBertForPreTraining(config=a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : str = model( a_ ,attention_mask=a_ ,token_type_ids=a_ ,labels=a_ ,next_sentence_label=a_ ,) self.parent.assertEqual(result.prediction_logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape ,(self.batch_size, 2) ) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> Dict: _UpperCAmelCase : Tuple = MobileBertForQuestionAnswering(config=a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : Union[str, Any] = model( a_ ,attention_mask=a_ ,token_type_ids=a_ ,start_positions=a_ ,end_positions=a_ ,) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> Tuple: _UpperCAmelCase : int = self.num_labels _UpperCAmelCase : int = MobileBertForSequenceClassification(a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : Any = model(a_ ,attention_mask=a_ ,token_type_ids=a_ ,labels=a_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> List[Any]: _UpperCAmelCase : List[str] = self.num_labels _UpperCAmelCase : Any = MobileBertForTokenClassification(config=a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : Optional[Any] = model(a_ ,attention_mask=a_ ,token_type_ids=a_ ,labels=a_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def _snake_case ( self ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ,a_ ) -> int: _UpperCAmelCase : str = self.num_choices _UpperCAmelCase : Any = MobileBertForMultipleChoice(config=a_ ) model.to(a_ ) model.eval() _UpperCAmelCase : int = input_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() _UpperCAmelCase : List[str] = token_type_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() _UpperCAmelCase : Tuple = input_mask.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() _UpperCAmelCase : Dict = model( a_ ,attention_mask=a_ ,token_type_ids=a_ ,labels=a_ ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def _snake_case ( self ) -> Any: _UpperCAmelCase : Optional[int] = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) ,( _UpperCAmelCase ) ,( _UpperCAmelCase ) ,( _UpperCAmelCase ) ,( _UpperCAmelCase ) ,( _UpperCAmelCase ) ,( _UpperCAmelCase ) , ) : Optional[int] = config_and_inputs _UpperCAmelCase : Union[str, Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class lowercase ( _lowerCamelCase , _lowerCamelCase , unittest.TestCase ): """simple docstring""" UpperCAmelCase = ( ( MobileBertModel, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, ) if is_torch_available() else () ) UpperCAmelCase = ( { """feature-extraction""": MobileBertModel, """fill-mask""": MobileBertForMaskedLM, """question-answering""": MobileBertForQuestionAnswering, """text-classification""": MobileBertForSequenceClassification, """token-classification""": MobileBertForTokenClassification, """zero-shot""": MobileBertForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase = True def _snake_case ( self ,a_ ,a_ ,a_=False ) -> int: _UpperCAmelCase : Union[str, Any] = super()._prepare_for_class(a_ ,a_ ,return_labels=a_ ) if return_labels: if model_class in get_values(a_ ): _UpperCAmelCase : Union[str, Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) ,dtype=torch.long ,device=a_ ) _UpperCAmelCase : Union[str, Any] = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=a_ ) return inputs_dict def _snake_case ( self ) -> Any: _UpperCAmelCase : Union[str, Any] = MobileBertModelTester(self ) _UpperCAmelCase : Optional[int] = ConfigTester(self ,config_class=a_ ,hidden_size=37 ) def _snake_case ( self ) -> Union[str, Any]: self.config_tester.run_common_tests() def _snake_case ( self ) -> str: _UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*a_ ) def _snake_case ( self ) -> Any: _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*a_ ) def _snake_case ( self ) -> Any: _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*a_ ) def _snake_case ( self ) -> List[str]: _UpperCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*a_ ) def _snake_case ( self ) -> Tuple: _UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*a_ ) def _snake_case ( self ) -> Union[str, Any]: _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*a_ ) def _snake_case ( self ) -> List[Any]: _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*a_ ) def _snake_case ( self ) -> str: _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*a_ ) def snake_case_ ( lowerCAmelCase_ )-> Optional[Any]: '''simple docstring''' return torch.tensor( lowerCAmelCase_ , dtype=torch.long , device=lowerCAmelCase_ , ) A_ : Dict = 1e-3 @require_torch @require_sentencepiece @require_tokenizers class lowercase ( unittest.TestCase ): """simple docstring""" @slow def _snake_case ( self ) -> Tuple: _UpperCAmelCase : Optional[Any] = MobileBertModel.from_pretrained("""google/mobilebert-uncased""" ).to(a_ ) _UpperCAmelCase : str = _long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]] ) with torch.no_grad(): _UpperCAmelCase : List[str] = model(a_ )[0] _UpperCAmelCase : int = torch.Size((1, 9, 512) ) self.assertEqual(output.shape ,a_ ) _UpperCAmelCase : Dict = torch.tensor( [ [ [-2.4_7_3_6_5_2_6E0_7, 8.2_6_9_1_6_5_6E0_4, 1.6_5_2_1_8_3_8E0_5], [-5.7_5_4_1_7_0_4E-0_1, 3.9_0_5_6_0_2_2E0_0, 4.4_0_1_1_5_0_7E0_0], [2.6_0_4_7_3_5_9E0_0, 1.5_6_7_7_6_5_2E0_0, -1.7_3_2_4_1_8_8E-0_1], ] ] ,device=a_ ,) # MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a # ~1 difference, it's therefore not a good idea to measure using addition. # Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the # result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE _UpperCAmelCase : List[str] = torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE ) _UpperCAmelCase : List[Any] = torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE ) self.assertTrue(lower_bound and upper_bound )
215
'''simple docstring''' from numpy import exp, pi, sqrt def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ = 0.0 , lowerCAmelCase_ = 1.0 )-> int: '''simple docstring''' return 1 / sqrt(2 * pi * sigma**2 ) * exp(-((x - mu) ** 2) / (2 * sigma**2) ) if __name__ == "__main__": import doctest doctest.testmod()
215
1
"""simple docstring""" import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process _a = logging.getLogger(__name__) def lowerCamelCase__ ( __snake_case, __snake_case ) -> Optional[int]: """simple docstring""" return (preds == labels).mean() @dataclass class _UpperCAmelCase: lowercase__ = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) @dataclass class _UpperCAmelCase: lowercase__ = field(metadata={'help': 'The name of the task to train on: ' + ', '.join(processors.keys() )} ) lowercase__ = field(metadata={'help': 'Should contain the data files for the task.'} ) lowercase__ = field( default=1_28 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) def lowerCamelCase__ ( ) -> Union[str, Any]: """simple docstring""" _UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ''' --overwrite_output_dir to overcome.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN, ) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''', training_args.local_rank, training_args.device, training_args.n_gpu, bool(training_args.local_rank != -1 ), training_args.fpaa, ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''', __snake_case ) # Set seed set_seed(training_args.seed ) try: _UpperCamelCase = processors[data_args.task_name]() _UpperCamelCase = processor.get_labels() _UpperCamelCase = len(__snake_case ) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _UpperCamelCase = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path, num_labels=__snake_case, finetuning_task=data_args.task_name, cache_dir=model_args.cache_dir, ) _UpperCamelCase = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, cache_dir=model_args.cache_dir, ) _UpperCamelCase = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path, from_tf=bool('''.ckpt''' in model_args.model_name_or_path ), config=__snake_case, cache_dir=model_args.cache_dir, ) # Get datasets _UpperCamelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir, tokenizer=__snake_case, task=data_args.task_name, max_seq_length=data_args.max_seq_length, overwrite_cache=data_args.overwrite_cache, mode=Split.train, ) if training_args.do_train else None ) _UpperCamelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir, tokenizer=__snake_case, task=data_args.task_name, max_seq_length=data_args.max_seq_length, overwrite_cache=data_args.overwrite_cache, mode=Split.dev, ) if training_args.do_eval else None ) def compute_metrics(__snake_case ) -> Dict: _UpperCamelCase = np.argmax(p.predictions, axis=1 ) return {"acc": simple_accuracy(__snake_case, p.label_ids )} # Data collator _UpperCamelCase = DataCollatorWithPadding(__snake_case, pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer _UpperCamelCase = Trainer( model=__snake_case, args=__snake_case, train_dataset=__snake_case, eval_dataset=__snake_case, compute_metrics=__snake_case, data_collator=__snake_case, ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation _UpperCamelCase = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) _UpperCamelCase = trainer.evaluate() _UpperCamelCase = os.path.join(training_args.output_dir, '''eval_results.txt''' ) if trainer.is_world_master(): with open(__snake_case, '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in result.items(): logger.info(''' %s = %s''', __snake_case, __snake_case ) writer.write('''%s = %s\n''' % (key, value) ) results.update(__snake_case ) return results def lowerCamelCase__ ( __snake_case ) -> str: """simple docstring""" main() if __name__ == "__main__": main()
100
"""simple docstring""" from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class _UpperCAmelCase: def __init__( self , __a , __a=13 , __a=7 , __a=True , __a=True , __a=True , __a=True , __a=99 , __a=32 , __a=2 , __a=4 , __a=37 , __a="gelu" , __a=0.1 , __a=0.1 , __a=5_12 , __a=16 , __a=2 , __a=0.02 , __a=3 , __a=4 , __a=None , __a=10_00 , ) -> str: '''simple docstring''' _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = seq_length _UpperCamelCase = is_training _UpperCamelCase = use_input_mask _UpperCamelCase = use_token_type_ids _UpperCamelCase = use_labels _UpperCamelCase = vocab_size _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = intermediate_size _UpperCamelCase = hidden_act _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = max_position_embeddings _UpperCamelCase = type_vocab_size _UpperCamelCase = type_sequence_label_size _UpperCamelCase = initializer_range _UpperCamelCase = num_labels _UpperCamelCase = num_choices _UpperCamelCase = scope _UpperCamelCase = range_bbox def UpperCAmelCase ( self) -> Tuple: '''simple docstring''' _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) # convert bbox to numpy since TF does not support item assignment _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0]): for j in range(bbox.shape[1]): if bbox[i, j, 3] < bbox[i, j, 1]: _UpperCamelCase = bbox[i, j, 3] _UpperCamelCase = bbox[i, j, 1] _UpperCamelCase = t if bbox[i, j, 2] < bbox[i, j, 0]: _UpperCamelCase = bbox[i, j, 2] _UpperCamelCase = bbox[i, j, 0] _UpperCamelCase = t _UpperCamelCase = tf.convert_to_tensor(__a) _UpperCamelCase = None if self.use_input_mask: _UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length]) _UpperCamelCase = None if self.use_token_type_ids: _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None if self.use_labels: _UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size) _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) _UpperCamelCase = ids_tensor([self.batch_size] , self.num_choices) _UpperCamelCase = LayoutLMConfig( 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 , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a , __a) -> List[str]: '''simple docstring''' _UpperCamelCase = TFLayoutLMModel(config=__a) _UpperCamelCase = model(__a , __a , attention_mask=__a , token_type_ids=__a) _UpperCamelCase = model(__a , __a , token_type_ids=__a) _UpperCamelCase = model(__a , __a) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size)) def UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a , __a) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = TFLayoutLMForMaskedLM(config=__a) _UpperCamelCase = model(__a , __a , attention_mask=__a , token_type_ids=__a , labels=__a) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a , __a) -> int: '''simple docstring''' _UpperCamelCase = self.num_labels _UpperCamelCase = TFLayoutLMForSequenceClassification(config=__a) _UpperCamelCase = model(__a , __a , attention_mask=__a , token_type_ids=__a) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a , __a) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = self.num_labels _UpperCamelCase = TFLayoutLMForTokenClassification(config=__a) _UpperCamelCase = model(__a , __a , attention_mask=__a , token_type_ids=__a , labels=__a) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def UpperCAmelCase ( self , __a , __a , __a , __a , __a , __a , __a , __a) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = TFLayoutLMForQuestionAnswering(config=__a) _UpperCamelCase = model(__a , __a , attention_mask=__a , token_type_ids=__a) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' _UpperCamelCase = self.prepare_config_and_inputs() ( ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ) = config_and_inputs _UpperCamelCase = { '''input_ids''': input_ids, '''bbox''': bbox, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_tf class _UpperCAmelCase( lowerCamelCase , lowerCamelCase , unittest.TestCase ): lowercase__ = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) lowercase__ = ( { 'feature-extraction': TFLayoutLMModel, 'fill-mask': TFLayoutLMForMaskedLM, 'text-classification': TFLayoutLMForSequenceClassification, 'token-classification': TFLayoutLMForTokenClassification, 'zero-shot': TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) lowercase__ = False lowercase__ = True lowercase__ = 10 def UpperCAmelCase ( self) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = TFLayoutLMModelTester(self) _UpperCamelCase = ConfigTester(self , config_class=__a , hidden_size=37) def UpperCAmelCase ( self) -> str: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase ( self) -> int: '''simple docstring''' _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__a) def UpperCAmelCase ( self) -> Dict: '''simple docstring''' _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__a) def UpperCAmelCase ( self) -> int: '''simple docstring''' _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__a) def UpperCAmelCase ( self) -> Optional[int]: '''simple docstring''' _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__a) def UpperCAmelCase ( self) -> Tuple: '''simple docstring''' _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__a) @slow def UpperCAmelCase ( self) -> str: '''simple docstring''' for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCamelCase = TFLayoutLMModel.from_pretrained(__a) self.assertIsNotNone(__a) @unittest.skip('''Onnx compliancy broke with TF 2.10''') def UpperCAmelCase ( self) -> List[str]: '''simple docstring''' pass def lowerCamelCase__ ( ) -> Union[str, Any]: """simple docstring""" _UpperCamelCase = tf.convert_to_tensor([[1_01,10_19,10_14,10_16,10_37,1_28_49,47_47,10_04,1_42_46,22_78,54_39,45_24,50_02,29_30,21_93,29_30,43_41,32_08,10_05,10_55,21_71,28_48,1_13_00,35_31,1_02],[1_01,40_70,40_34,70_20,10_24,30_58,10_15,10_13,28_61,10_13,60_70,1_92_74,27_72,62_05,2_78_14,1_61_47,1_61_47,43_43,20_47,1_02_83,1_09_69,1_43_89,10_12,23_38,1_02]] ) # noqa: E231 _UpperCamelCase = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 _UpperCamelCase = tf.convert_to_tensor([[[0,0,0,0],[4_23,2_37,4_40,2_51],[4_27,2_72,4_41,2_87],[4_19,1_15,4_37,1_29],[9_61,8_85,9_92,9_12],[2_56,38,3_30,58],[2_56,38,3_30,58],[3_36,42,3_53,57],[3_60,39,4_01,56],[3_60,39,4_01,56],[4_11,39,4_71,59],[4_79,41,5_28,59],[5_33,39,6_30,60],[67,1_13,1_34,1_31],[1_41,1_15,2_09,1_32],[68,1_49,1_33,1_66],[1_41,1_49,1_87,1_64],[1_95,1_48,2_87,1_65],[1_95,1_48,2_87,1_65],[1_95,1_48,2_87,1_65],[2_95,1_48,3_49,1_65],[4_41,1_49,4_92,1_66],[4_97,1_49,5_46,1_64],[64,2_01,1_25,2_18],[10_00,10_00,10_00,10_00]],[[0,0,0,0],[6_62,1_50,7_54,1_66],[6_65,1_99,7_42,2_11],[5_19,2_13,5_54,2_28],[5_19,2_13,5_54,2_28],[1_34,4_33,1_87,4_54],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[1_30,4_67,2_04,4_80],[3_14,4_69,3_76,4_82],[5_04,6_84,5_82,7_06],[9_41,8_25,9_73,9_00],[9_41,8_25,9_73,9_00],[9_41,8_25,9_73,9_00],[9_41,8_25,9_73,9_00],[6_10,7_49,6_52,7_65],[1_30,6_59,1_68,6_72],[1_76,6_57,2_37,6_72],[2_38,6_57,3_12,6_72],[4_43,6_53,6_28,6_72],[4_43,6_53,6_28,6_72],[7_16,3_01,8_25,3_17],[10_00,10_00,10_00,10_00]]] ) # noqa: E231 _UpperCamelCase = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) _UpperCamelCase = tf.convert_to_tensor([[-1_00,10,10,10,9,1,-1_00,7,7,-1_00,7,7,4,2,5,2,8,8,-1_00,-1_00,5,0,3,2,-1_00],[-1_00,12,12,12,-1_00,12,10,-1_00,-1_00,-1_00,-1_00,10,12,9,-1_00,-1_00,-1_00,10,10,10,9,12,-1_00,10,-1_00]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class _UpperCAmelCase( unittest.TestCase ): @slow def UpperCAmelCase ( self) -> Optional[Any]: '''simple docstring''' _UpperCamelCase = TFLayoutLMModel.from_pretrained('''microsoft/layoutlm-base-uncased''') _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = prepare_layoutlm_batch_inputs() # forward pass _UpperCamelCase = model(input_ids=__a , bbox=__a , attention_mask=__a , token_type_ids=__a) # test the sequence output on [0, :3, :3] _UpperCamelCase = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , __a , atol=1e-3)) # test the pooled output on [1, :3] _UpperCamelCase = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552]) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , __a , atol=1e-3)) @slow def UpperCAmelCase ( self) -> Any: '''simple docstring''' # initialize model with randomly initialized sequence classification head _UpperCamelCase = TFLayoutLMForSequenceClassification.from_pretrained('''microsoft/layoutlm-base-uncased''' , num_labels=2) _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = prepare_layoutlm_batch_inputs() # forward pass _UpperCamelCase = model( input_ids=__a , bbox=__a , attention_mask=__a , token_type_ids=__a , labels=tf.convert_to_tensor([1, 1]) , ) # test whether we get a loss as a scalar _UpperCamelCase = outputs.loss _UpperCamelCase = (2,) self.assertEqual(loss.shape , __a) # test the shape of the logits _UpperCamelCase = outputs.logits _UpperCamelCase = (2, 2) self.assertEqual(logits.shape , __a) @slow def UpperCAmelCase ( self) -> List[str]: '''simple docstring''' # initialize model with randomly initialized token classification head _UpperCamelCase = TFLayoutLMForTokenClassification.from_pretrained('''microsoft/layoutlm-base-uncased''' , num_labels=13) _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = prepare_layoutlm_batch_inputs() # forward pass _UpperCamelCase = model( input_ids=__a , bbox=__a , attention_mask=__a , token_type_ids=__a , labels=__a) # test the shape of the logits _UpperCamelCase = outputs.logits _UpperCamelCase = tf.convert_to_tensor((2, 25, 13)) self.assertEqual(logits.shape , __a) @slow def UpperCAmelCase ( self) -> Dict: '''simple docstring''' # initialize model with randomly initialized token classification head _UpperCamelCase = TFLayoutLMForQuestionAnswering.from_pretrained('''microsoft/layoutlm-base-uncased''') _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = prepare_layoutlm_batch_inputs() # forward pass _UpperCamelCase = model(input_ids=__a , bbox=__a , attention_mask=__a , token_type_ids=__a) # test the shape of the logits _UpperCamelCase = tf.convert_to_tensor((2, 25)) self.assertEqual(outputs.start_logits.shape , __a) self.assertEqual(outputs.end_logits.shape , __a)
100
1
'''simple docstring''' import os import re from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase :List[Any] = logging.get_logger(__name__) lowerCAmelCase :List[str] = {'''vocab_file''': '''spiece.model'''} lowerCAmelCase :Any = { '''vocab_file''': { '''google/bigbird-roberta-base''': '''https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model''', '''google/bigbird-roberta-large''': ( '''https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model''' ), '''google/bigbird-base-trivia-itc''': ( '''https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model''' ), } } lowerCAmelCase :List[Any] = { '''google/bigbird-roberta-base''': 4_0_9_6, '''google/bigbird-roberta-large''': 4_0_9_6, '''google/bigbird-base-trivia-itc''': 4_0_9_6, } class _lowerCamelCase ( UpperCamelCase_ ): '''simple docstring''' A_ : str = VOCAB_FILES_NAMES A_ : Dict = PRETRAINED_VOCAB_FILES_MAP A_ : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A_ : Optional[Any] = ['input_ids', 'attention_mask'] A_ : List[int] = [] def __init__( self : Dict , _A : Union[str, Any] , _A : Union[str, Any]="<unk>" , _A : Union[str, Any]="<s>" , _A : Union[str, Any]="</s>" , _A : List[Any]="<pad>" , _A : Dict="[SEP]" , _A : Optional[Any]="[MASK]" , _A : Optional[Any]="[CLS]" , _A : Optional[Dict[str, Any]] = None , **_A : str , ) -> None: __magic_name__ : List[str] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else bos_token __magic_name__ : Union[str, Any] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else eos_token __magic_name__ : Optional[Any] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else unk_token __magic_name__ : Union[str, Any] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else pad_token __magic_name__ : Dict = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else cls_token __magic_name__ : Optional[int] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it __magic_name__ : Dict = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else mask_token __magic_name__ : Optional[int] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=lowercase_ , eos_token=lowercase_ , unk_token=lowercase_ , pad_token=lowercase_ , sep_token=lowercase_ , mask_token=lowercase_ , cls_token=lowercase_ , sp_model_kwargs=self.sp_model_kwargs , **lowercase_ , ) __magic_name__ : List[Any] = vocab_file __magic_name__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowercase_ ) @property def __lowerCAmelCase ( self : str ) -> Union[str, Any]: return self.sp_model.get_piece_size() def __lowerCAmelCase ( self : Dict ) -> List[Any]: __magic_name__ : str = {self.convert_ids_to_tokens(lowercase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : List[str] ) -> List[Any]: __magic_name__ : Tuple = self.__dict__.copy() __magic_name__ : int = None return state def __setstate__( self : List[str] , _A : List[Any] ) -> str: __magic_name__ : int = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): __magic_name__ : List[str] = {} __magic_name__ : Union[str, Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self : Union[str, Any] , _A : str ) -> List[str]: return self.sp_model.encode(lowercase_ , out_type=lowercase_ ) def __lowerCAmelCase ( self : int , _A : List[str] ) -> str: return self.sp_model.piece_to_id(lowercase_ ) def __lowerCAmelCase ( self : int , _A : Dict ) -> Optional[Any]: __magic_name__ : str = self.sp_model.IdToPiece(lowercase_ ) return token def __lowerCAmelCase ( self : Union[str, Any] , _A : str ) -> Optional[Any]: __magic_name__ : Any = [] __magic_name__ : Optional[Any] = '' __magic_name__ : Optional[Any] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(lowercase_ ) + token __magic_name__ : Any = True __magic_name__ : List[str] = [] else: current_sub_tokens.append(lowercase_ ) __magic_name__ : int = False out_string += self.sp_model.decode(lowercase_ ) return out_string.strip() def __lowerCAmelCase ( self : int , _A : List[int] , _A : bool = False , _A : bool = None , _A : bool = True , **_A : Any , ) -> str: __magic_name__ : Optional[int] = kwargs.pop('use_source_tokenizer' , lowercase_ ) __magic_name__ : List[str] = self.convert_ids_to_tokens(lowercase_ , skip_special_tokens=lowercase_ ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __magic_name__ : Dict = [] __magic_name__ : Tuple = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(lowercase_ ) ) __magic_name__ : str = [] sub_texts.append(lowercase_ ) else: current_sub_text.append(lowercase_ ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(lowercase_ ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: __magic_name__ : List[Any] = re.sub(R' (\[(MASK|SEP)\])' , R'\1' , ' '.join(lowercase_ ) ) else: __magic_name__ : Any = ''.join(lowercase_ ) __magic_name__ : str = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __magic_name__ : Union[str, Any] = self.clean_up_tokenization(lowercase_ ) return clean_text else: return text def __lowerCAmelCase ( self : Union[str, Any] , _A : str , _A : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(lowercase_ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return __magic_name__ : Dict = os.path.join( lowercase_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowercase_ ) elif not os.path.isfile(self.vocab_file ): with open(lowercase_ , 'wb' ) as fi: __magic_name__ : Tuple = self.sp_model.serialized_model_proto() fi.write(lowercase_ ) return (out_vocab_file,) def __lowerCAmelCase ( self : Optional[int] , _A : List[int] , _A : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] __magic_name__ : Tuple = [self.cls_token_id] __magic_name__ : List[Any] = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def __lowerCAmelCase ( self : Optional[Any] , _A : List[int] , _A : Optional[List[int]] = None , _A : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowercase_ , token_ids_a=lowercase_ , already_has_special_tokens=lowercase_ ) if token_ids_a is None: return [1] + ([0] * len(lowercase_ )) + [1] return [1] + ([0] * len(lowercase_ )) + [1] + ([0] * len(lowercase_ )) + [1] def __lowerCAmelCase ( self : Optional[Any] , _A : List[int] , _A : Optional[List[int]] = None ) -> List[int]: __magic_name__ : Union[str, Any] = [self.sep_token_id] __magic_name__ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]
331
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCamelCase = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCamelCase = TaTokenizerFast lowerCamelCase = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCamelCase = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
199
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase = { '''configuration_jukebox''': [ '''JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''JukeboxConfig''', '''JukeboxPriorConfig''', '''JukeboxVQVAEConfig''', ], '''tokenization_jukebox''': ['''JukeboxTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase = [ '''JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST''', '''JukeboxModel''', '''JukeboxPreTrainedModel''', '''JukeboxVQVAE''', '''JukeboxPrior''', ] if TYPE_CHECKING: from .configuration_jukebox import ( JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP, JukeboxConfig, JukeboxPriorConfig, JukeboxVQVAEConfig, ) from .tokenization_jukebox import JukeboxTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_jukebox import ( JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST, JukeboxModel, JukeboxPreTrainedModel, JukeboxPrior, JukeboxVQVAE, ) else: import sys lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
304
import math from datetime import datetime, timedelta def _lowerCamelCase( lowercase__ ) -> datetime: '''simple docstring''' __lowercase= year % 1_9 __lowercase= year % 4 __lowercase= year % 7 __lowercase= math.floor(year / 1_0_0 ) __lowercase= math.floor((1_3 + 8 * leap_day_inhibits) / 2_5 ) __lowercase= leap_day_inhibits / 4 __lowercase= ( 1_5 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 3_0 __lowercase= (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 __lowercase= (1_9 * metonic_cycle + secular_moon_shift) % 3_0 # PHM -> Paschal Full Moon __lowercase= ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 2_9 and days_from_phm_to_sunday == 6: return datetime(lowercase__ , 4 , 1_9 ) elif days_to_add == 2_8 and days_from_phm_to_sunday == 6: return datetime(lowercase__ , 4 , 1_8 ) else: return datetime(lowercase__ , 3 , 2_2 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1_9_9_4, 2_0_0_0, 2_0_1_0, 2_0_2_1, 2_0_2_3): lowerCAmelCase = '''will be''' if year > datetime.now().year else '''was''' print(F'Easter in {year} {tense} {gauss_easter(year)}')
304
1
"""simple docstring""" from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import ( BaseOutput, OptionalDependencyNotAvailable, is_flax_available, is_k_diffusion_available, is_k_diffusion_version, is_onnx_available, is_torch_available, is_transformers_available, is_transformers_version, ) @dataclass class lowerCAmelCase__ ( lowercase ): '''simple docstring''' lowerCamelCase__ = 42 lowerCamelCase__ = 42 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 .pipeline_cycle_diffusion import CycleDiffusionPipeline from .pipeline_stable_diffusion import StableDiffusionPipeline from .pipeline_stable_diffusion_attend_and_excite import StableDiffusionAttendAndExcitePipeline from .pipeline_stable_diffusion_imgaimg import StableDiffusionImgaImgPipeline from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy from .pipeline_stable_diffusion_instruct_pixapix import StableDiffusionInstructPixaPixPipeline from .pipeline_stable_diffusion_latent_upscale import StableDiffusionLatentUpscalePipeline from .pipeline_stable_diffusion_ldmad import StableDiffusionLDMaDPipeline from .pipeline_stable_diffusion_model_editing import StableDiffusionModelEditingPipeline from .pipeline_stable_diffusion_panorama import StableDiffusionPanoramaPipeline from .pipeline_stable_diffusion_paradigms import StableDiffusionParadigmsPipeline from .pipeline_stable_diffusion_sag import StableDiffusionSAGPipeline from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline from .pipeline_stable_unclip import StableUnCLIPPipeline from .pipeline_stable_unclip_imgaimg import StableUnCLIPImgaImgPipeline from .safety_checker import StableDiffusionSafetyChecker from .stable_unclip_image_normalizer import StableUnCLIPImageNormalizer try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import StableDiffusionImageVariationPipeline else: from .pipeline_stable_diffusion_image_variation import StableDiffusionImageVariationPipeline try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.26.0""")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionPixaPixZeroPipeline, ) else: from .pipeline_stable_diffusion_depthaimg import StableDiffusionDepthaImgPipeline from .pipeline_stable_diffusion_diffedit import StableDiffusionDiffEditPipeline from .pipeline_stable_diffusion_pixapix_zero import StableDiffusionPixaPixZeroPipeline try: if not ( is_torch_available() and is_transformers_available() and is_k_diffusion_available() and is_k_diffusion_version(""">=""", """0.0.12""") ): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipeline_stable_diffusion_k_diffusion import StableDiffusionKDiffusionPipeline try: if not (is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_onnx_objects import * # noqa F403 else: from .pipeline_onnx_stable_diffusion import OnnxStableDiffusionPipeline, StableDiffusionOnnxPipeline from .pipeline_onnx_stable_diffusion_imgaimg import OnnxStableDiffusionImgaImgPipeline from .pipeline_onnx_stable_diffusion_inpaint import OnnxStableDiffusionInpaintPipeline from .pipeline_onnx_stable_diffusion_inpaint_legacy import OnnxStableDiffusionInpaintPipelineLegacy from .pipeline_onnx_stable_diffusion_upscale import OnnxStableDiffusionUpscalePipeline if is_transformers_available() and is_flax_available(): import flax @flax.struct.dataclass class lowerCAmelCase__ ( lowercase ): '''simple docstring''' lowerCamelCase__ = 42 lowerCamelCase__ = 42 from ...schedulers.scheduling_pndm_flax import PNDMSchedulerState from .pipeline_flax_stable_diffusion import FlaxStableDiffusionPipeline from .pipeline_flax_stable_diffusion_imgaimg import FlaxStableDiffusionImgaImgPipeline from .pipeline_flax_stable_diffusion_inpaint import FlaxStableDiffusionInpaintPipeline from .safety_checker_flax import FlaxStableDiffusionSafetyChecker
96
class SCREAMING_SNAKE_CASE__ : '''simple docstring''' def __init__( self : List[str] , lowercase : list[int] ): '''simple docstring''' _snake_case = len(lowercase ) _snake_case = [0] * len_array if len_array > 0: _snake_case = array[0] for i in range(1 , lowercase ): _snake_case = self.prefix_sum[i - 1] + array[i] def A ( self : Optional[Any] , lowercase : int , lowercase : int ): '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def A ( self : Union[str, Any] , lowercase : int ): '''simple docstring''' _snake_case = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(lowercase ) return False if __name__ == "__main__": import doctest doctest.testmod()
282
0
def UpperCamelCase ( lowerCAmelCase__ = 400_0000 ): '''simple docstring''' lowercase = [] lowercase , lowercase = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(lowerCAmelCase__ ) lowercase , lowercase = b, a + b return sum(lowerCAmelCase__ ) if __name__ == "__main__": print(F'{solution() = }')
97
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable lowercase__ :Union[str, Any] = {"configuration_dpt": ["DPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DPTConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ :List[Any] = ["DPTFeatureExtractor"] lowercase__ :List[Any] = ["DPTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ :Optional[int] = [ "DPT_PRETRAINED_MODEL_ARCHIVE_LIST", "DPTForDepthEstimation", "DPTForSemanticSegmentation", "DPTModel", "DPTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys lowercase__ :List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
97
1
import re import string import numpy as np import datasets _SCREAMING_SNAKE_CASE : Union[str, Any] = "\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n" _SCREAMING_SNAKE_CASE : Tuple = "\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results[\"exact_match\"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results[\"exact_match\"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results[\"exact_match\"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]\n >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results[\"exact_match\"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric(\"exact_match\")\n >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"]\n >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results[\"exact_match\"], 1))\n 33.3\n\n" _SCREAMING_SNAKE_CASE : Any = "\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): """simple docstring""" def a_ ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def a_ ( self , __snake_case , __snake_case , __snake_case=None , __snake_case=False , __snake_case=False , __snake_case=False , ): if regexes_to_ignore is not None: for s in regexes_to_ignore: snake_case = np.array([re.sub(__snake_case , '''''' , __snake_case ) for x in predictions] ) snake_case = np.array([re.sub(__snake_case , '''''' , __snake_case ) for x in references] ) else: snake_case = np.asarray(__snake_case ) snake_case = np.asarray(__snake_case ) if ignore_case: snake_case = np.char.lower(__snake_case ) snake_case = np.char.lower(__snake_case ) if ignore_punctuation: snake_case = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) snake_case = np.char.translate(__snake_case , table=__snake_case ) snake_case = np.char.translate(__snake_case , table=__snake_case ) if ignore_numbers: snake_case = string.digits.maketrans('''''' , '''''' , string.digits ) snake_case = np.char.translate(__snake_case , table=__snake_case ) snake_case = np.char.translate(__snake_case , table=__snake_case ) snake_case = predictions == references return {"exact_match": np.mean(__snake_case ) * 1_0_0}
127
import json import os import tempfile import unittest import unittest.mock as mock from pathlib import Path from requests.exceptions import HTTPError from transformers.utils import ( CONFIG_NAME, FLAX_WEIGHTS_NAME, TF2_WEIGHTS_NAME, TRANSFORMERS_CACHE, WEIGHTS_NAME, cached_file, get_file_from_repo, has_file, ) _SCREAMING_SNAKE_CASE : List[str] = "hf-internal-testing/tiny-random-bert" _SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert") _SCREAMING_SNAKE_CASE : Optional[int] = "9b8c223d42b2188cb49d29af482996f9d0f3e5a6" class A__ ( unittest.TestCase ): """simple docstring""" def a_ ( self ): snake_case = cached_file(__snake_case , __snake_case ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(__snake_case ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(__snake_case , __snake_case ) ) ) with open(os.path.join(__snake_case , '''refs''' , '''main''' ) ) as f: snake_case = f.read() self.assertEqual(__snake_case , os.path.join(__snake_case , '''snapshots''' , __snake_case , __snake_case ) ) self.assertTrue(os.path.isfile(__snake_case ) ) # File is cached at the same place the second time. snake_case = cached_file(__snake_case , __snake_case ) self.assertEqual(__snake_case , __snake_case ) # Using a specific revision to test the full commit hash. snake_case = cached_file(__snake_case , __snake_case , revision='''9b8c223''' ) self.assertEqual(__snake_case , os.path.join(__snake_case , '''snapshots''' , __snake_case , __snake_case ) ) def a_ ( self ): with self.assertRaisesRegex(__snake_case , '''is not a valid model identifier''' ): snake_case = cached_file('''tiny-random-bert''' , __snake_case ) with self.assertRaisesRegex(__snake_case , '''is not a valid git identifier''' ): snake_case = cached_file(__snake_case , __snake_case , revision='''aaaa''' ) with self.assertRaisesRegex(__snake_case , '''does not appear to have a file named''' ): snake_case = cached_file(__snake_case , '''conf''' ) def a_ ( self ): with self.assertRaisesRegex(__snake_case , '''does not appear to have a file named''' ): snake_case = cached_file(__snake_case , '''conf''' ) with open(os.path.join(__snake_case , '''refs''' , '''main''' ) ) as f: snake_case = f.read() self.assertTrue(os.path.isfile(os.path.join(__snake_case , '''.no_exist''' , __snake_case , '''conf''' ) ) ) snake_case = cached_file(__snake_case , '''conf''' , _raise_exceptions_for_missing_entries=__snake_case ) self.assertIsNone(__snake_case ) snake_case = cached_file(__snake_case , '''conf''' , local_files_only=__snake_case , _raise_exceptions_for_missing_entries=__snake_case ) self.assertIsNone(__snake_case ) snake_case = mock.Mock() snake_case = 5_0_0 snake_case = {} snake_case = HTTPError snake_case = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('''requests.Session.request''' , return_value=__snake_case ) as mock_head: snake_case = cached_file(__snake_case , '''conf''' , _raise_exceptions_for_connection_errors=__snake_case ) self.assertIsNone(__snake_case ) # This check we did call the fake head request mock_head.assert_called() def a_ ( self ): self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' , __snake_case ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , __snake_case ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , __snake_case ) ) def a_ ( self ): # `get_file_from_repo` returns None if the file does not exist self.assertIsNone(get_file_from_repo('''bert-base-cased''' , '''ahah.txt''' ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(__snake_case , '''is not a valid model identifier''' ): get_file_from_repo('''bert-base-case''' , __snake_case ) # The function raises if the revision does not exist. with self.assertRaisesRegex(__snake_case , '''is not a valid git identifier''' ): get_file_from_repo('''bert-base-cased''' , __snake_case , revision='''ahaha''' ) snake_case = get_file_from_repo('''bert-base-cased''' , __snake_case ) # The name is the cached name which is not very easy to test, so instead we load the content. snake_case = json.loads(open(__snake_case , '''r''' ).read() ) self.assertEqual(config['''hidden_size'''] , 7_6_8 ) def a_ ( self ): with tempfile.TemporaryDirectory() as tmp_dir: snake_case = Path(__snake_case ) / '''a.txt''' filename.touch() self.assertEqual(get_file_from_repo(__snake_case , '''a.txt''' ) , str(__snake_case ) ) self.assertIsNone(get_file_from_repo(__snake_case , '''b.txt''' ) )
127
1
'''simple docstring''' from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def a__ ( a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = analyze_text(a__ ) __SCREAMING_SNAKE_CASE = list(""" """ + ascii_lowercase ) # what is our total sum of probabilities. __SCREAMING_SNAKE_CASE = sum(single_char_strings.values() ) # one length string __SCREAMING_SNAKE_CASE = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: __SCREAMING_SNAKE_CASE = single_char_strings[ch] __SCREAMING_SNAKE_CASE = my_str / all_sum my_fir_sum += prob * math.loga(a__ ) # entropy formula. # print entropy print(F'{round(-1 * my_fir_sum ):.1f}' ) # two len string __SCREAMING_SNAKE_CASE = sum(two_char_strings.values() ) __SCREAMING_SNAKE_CASE = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: __SCREAMING_SNAKE_CASE = cha + cha if sequence in two_char_strings: __SCREAMING_SNAKE_CASE = two_char_strings[sequence] __SCREAMING_SNAKE_CASE = int(a__ ) / all_sum my_sec_sum += prob * math.loga(a__ ) # print second entropy print(F'{round(-1 * my_sec_sum ):.1f}' ) # print the difference between them print(F'{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}' ) def a__ ( a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE = Counter() # type: ignore __SCREAMING_SNAKE_CASE = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(a__ ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def a__ ( ): """simple docstring""" import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
331
'''simple docstring''' import argparse import logging import os from pathlib import Path from typing import Any, Dict import pytorch_lightning as pl from pytorch_lightning.utilities import rank_zero_info from transformers import ( AdamW, AutoConfig, AutoModel, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelWithLMHead, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer, ) from transformers.optimization import ( Adafactor, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.utils.versions import require_version UpperCAmelCase : Any = logging.getLogger(__name__) require_version('pytorch_lightning>=1.0.4') UpperCAmelCase : Optional[Any] = { 'base': AutoModel, 'sequence-classification': AutoModelForSequenceClassification, 'question-answering': AutoModelForQuestionAnswering, 'pretraining': AutoModelForPreTraining, 'token-classification': AutoModelForTokenClassification, 'language-modeling': AutoModelWithLMHead, 'summarization': AutoModelForSeqaSeqLM, 'translation': AutoModelForSeqaSeqLM, } # update this and the import above to support new schedulers from transformers.optimization UpperCAmelCase : Dict = { 'linear': get_linear_schedule_with_warmup, 'cosine': get_cosine_schedule_with_warmup, 'cosine_w_restarts': get_cosine_with_hard_restarts_schedule_with_warmup, 'polynomial': get_polynomial_decay_schedule_with_warmup, # '': get_constant_schedule, # not supported for now # '': get_constant_schedule_with_warmup, # not supported for now } UpperCAmelCase : Optional[Any] = sorted(arg_to_scheduler.keys()) UpperCAmelCase : str = '{' + ', '.join(arg_to_scheduler_choices) + '}' class lowerCAmelCase__ ( pl.LightningModule ): """simple docstring""" def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : argparse.Namespace , __SCREAMING_SNAKE_CASE : Optional[Any]=None , __SCREAMING_SNAKE_CASE : Dict="base" , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : str=None , __SCREAMING_SNAKE_CASE : List[str]=None , **__SCREAMING_SNAKE_CASE : Union[str, Any] , ) -> Any: """simple docstring""" super().__init__() # TODO: move to self.save_hyperparameters() # self.save_hyperparameters() # can also expand arguments into trainer signature for easier reading self.save_hyperparameters(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = Path(self.hparams.output_dir ) __SCREAMING_SNAKE_CASE = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path , **({"""num_labels""": num_labels} if num_labels is not None else {}) , cache_dir=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) else: __SCREAMING_SNAKE_CASE = config __SCREAMING_SNAKE_CASE = ("""encoder_layerdrop""", """decoder_layerdrop""", """dropout""", """attention_dropout""") for p in extra_model_params: if getattr(self.hparams , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): assert hasattr(self.config , __SCREAMING_SNAKE_CASE ), f'model config doesn\'t have a `{p}` attribute' setattr(self.config , __SCREAMING_SNAKE_CASE , getattr(self.hparams , __SCREAMING_SNAKE_CASE ) ) if tokenizer is None: __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=__SCREAMING_SNAKE_CASE , ) else: __SCREAMING_SNAKE_CASE = tokenizer __SCREAMING_SNAKE_CASE = MODEL_MODES[mode] if model is None: __SCREAMING_SNAKE_CASE = self.model_type.from_pretrained( self.hparams.model_name_or_path , from_tf=bool(""".ckpt""" in self.hparams.model_name_or_path ) , config=self.config , cache_dir=__SCREAMING_SNAKE_CASE , ) else: __SCREAMING_SNAKE_CASE = model def UpperCAmelCase__ ( self : List[str] , *__SCREAMING_SNAKE_CASE : List[Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = self.model_type.from_pretrained(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : List[Any] ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = arg_to_scheduler[self.hparams.lr_scheduler] __SCREAMING_SNAKE_CASE = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) __SCREAMING_SNAKE_CASE = {"""scheduler""": scheduler, """interval""": """step""", """frequency""": 1} return scheduler def UpperCAmelCase__ ( self : int ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = self.model __SCREAMING_SNAKE_CASE = ["""bias""", """LayerNorm.weight"""] __SCREAMING_SNAKE_CASE = [ { """params""": [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay ) ], # check this named paramters """weight_decay""": self.hparams.weight_decay, }, { """params""": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] if self.hparams.adafactor: __SCREAMING_SNAKE_CASE = Adafactor( __SCREAMING_SNAKE_CASE , lr=self.hparams.learning_rate , scale_parameter=__SCREAMING_SNAKE_CASE , relative_step=__SCREAMING_SNAKE_CASE ) else: __SCREAMING_SNAKE_CASE = AdamW( __SCREAMING_SNAKE_CASE , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) __SCREAMING_SNAKE_CASE = optimizer __SCREAMING_SNAKE_CASE = self.get_lr_scheduler() return [optimizer], [scheduler] def UpperCAmelCase__ ( self : Any , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> int: """simple docstring""" return self.validation_step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : int , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Dict: """simple docstring""" return self.validation_end(__SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : Tuple ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores __SCREAMING_SNAKE_CASE = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def UpperCAmelCase__ ( self : Any , __SCREAMING_SNAKE_CASE : int ) -> Union[str, Any]: """simple docstring""" if stage == "test": __SCREAMING_SNAKE_CASE = len(self.test_dataloader().dataset ) else: __SCREAMING_SNAKE_CASE = self.get_dataloader("""train""" , self.hparams.train_batch_size , shuffle=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = len(self.train_dataloader().dataset ) def UpperCAmelCase__ ( self : List[str] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : bool = False ) -> int: """simple docstring""" raise NotImplementedError("""You must implement this for your task""" ) def UpperCAmelCase__ ( self : Optional[int] ) -> List[str]: """simple docstring""" return self.train_loader def UpperCAmelCase__ ( self : str ) -> Optional[Any]: """simple docstring""" return self.get_dataloader("""dev""" , self.hparams.eval_batch_size , shuffle=__SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : str ) -> Any: """simple docstring""" return self.get_dataloader("""test""" , self.hparams.eval_batch_size , shuffle=__SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : str , __SCREAMING_SNAKE_CASE : Dict ) -> Union[str, Any]: """simple docstring""" return os.path.join( self.hparams.data_dir , """cached_{}_{}_{}""".format( __SCREAMING_SNAKE_CASE , list(filter(__SCREAMING_SNAKE_CASE , self.hparams.model_name_or_path.split("""/""" ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def UpperCAmelCase__ ( self : List[str] , __SCREAMING_SNAKE_CASE : Dict[str, Any] ) -> None: """simple docstring""" __SCREAMING_SNAKE_CASE = self.output_dir.joinpath("""best_tfmr""" ) __SCREAMING_SNAKE_CASE = self.step_count self.model.save_pretrained(__SCREAMING_SNAKE_CASE ) self.tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) @staticmethod def UpperCAmelCase__ ( __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Any ) -> int: """simple docstring""" parser.add_argument( """--model_name_or_path""" , default=__SCREAMING_SNAKE_CASE , type=__SCREAMING_SNAKE_CASE , required=__SCREAMING_SNAKE_CASE , help="""Path to pretrained model or model identifier from huggingface.co/models""" , ) parser.add_argument( """--config_name""" , default="""""" , type=__SCREAMING_SNAKE_CASE , help="""Pretrained config name or path if not the same as model_name""" ) parser.add_argument( """--tokenizer_name""" , default=__SCREAMING_SNAKE_CASE , type=__SCREAMING_SNAKE_CASE , help="""Pretrained tokenizer name or path if not the same as model_name""" , ) parser.add_argument( """--cache_dir""" , default=str(Path(__SCREAMING_SNAKE_CASE ).parent / """test_run""" / """cache""" ) , type=__SCREAMING_SNAKE_CASE , help="""Where do you want to store the pre-trained models downloaded from huggingface.co""" , ) parser.add_argument( """--encoder_layerdrop""" , type=__SCREAMING_SNAKE_CASE , help="""Encoder layer dropout probability (Optional). Goes into model.config""" , ) parser.add_argument( """--decoder_layerdrop""" , type=__SCREAMING_SNAKE_CASE , help="""Decoder layer dropout probability (Optional). Goes into model.config""" , ) parser.add_argument( """--dropout""" , type=__SCREAMING_SNAKE_CASE , help="""Dropout probability (Optional). Goes into model.config""" , ) parser.add_argument( """--attention_dropout""" , type=__SCREAMING_SNAKE_CASE , help="""Attention dropout probability (Optional). Goes into model.config""" , ) parser.add_argument("""--learning_rate""" , default=5E-5 , type=__SCREAMING_SNAKE_CASE , help="""The initial learning rate for Adam.""" ) parser.add_argument( """--lr_scheduler""" , default="""linear""" , choices=__SCREAMING_SNAKE_CASE , metavar=__SCREAMING_SNAKE_CASE , type=__SCREAMING_SNAKE_CASE , help="""Learning rate scheduler""" , ) parser.add_argument("""--weight_decay""" , default=0.0 , type=__SCREAMING_SNAKE_CASE , help="""Weight decay if we apply some.""" ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__SCREAMING_SNAKE_CASE , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--warmup_steps""" , default=0 , type=__SCREAMING_SNAKE_CASE , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--num_workers""" , default=4 , type=__SCREAMING_SNAKE_CASE , help="""kwarg passed to DataLoader""" ) parser.add_argument("""--num_train_epochs""" , dest="""max_epochs""" , default=3 , type=__SCREAMING_SNAKE_CASE ) parser.add_argument("""--train_batch_size""" , default=32 , type=__SCREAMING_SNAKE_CASE ) parser.add_argument("""--eval_batch_size""" , default=32 , type=__SCREAMING_SNAKE_CASE ) parser.add_argument("""--adafactor""" , action="""store_true""" ) class lowerCAmelCase__ ( pl.Callback ): """simple docstring""" def UpperCAmelCase__ ( self : Tuple , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> List[Any]: """simple docstring""" if ( trainer.is_global_zero and trainer.global_rank == 0 ): # we initialize the retriever only on master worker with RAY. In new pytorch-lightning accelorators are removed. pl_module.model.rag.retriever.init_retrieval() # better to use hook functions. class lowerCAmelCase__ ( pl.Callback ): """simple docstring""" def UpperCAmelCase__ ( self : Dict , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Any: """simple docstring""" for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(__SCREAMING_SNAKE_CASE ) class lowerCAmelCase__ ( pl.Callback ): """simple docstring""" def UpperCAmelCase__ ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : str ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = trainer.lr_schedulers[0]["""scheduler"""] __SCREAMING_SNAKE_CASE = {f'lr_group_{i}': lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(__SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( self : Optional[int] , __SCREAMING_SNAKE_CASE : pl.Trainer , __SCREAMING_SNAKE_CASE : pl.LightningModule ) -> List[Any]: """simple docstring""" rank_zero_info("""***** Validation results *****""" ) __SCREAMING_SNAKE_CASE = trainer.callback_metrics # Log results for key in sorted(__SCREAMING_SNAKE_CASE ): if key not in ["log", "progress_bar"]: rank_zero_info("""{} = {}\n""".format(__SCREAMING_SNAKE_CASE , str(metrics[key] ) ) ) def UpperCAmelCase__ ( self : Tuple , __SCREAMING_SNAKE_CASE : pl.Trainer , __SCREAMING_SNAKE_CASE : pl.LightningModule ) -> str: """simple docstring""" rank_zero_info("""***** Test results *****""" ) __SCREAMING_SNAKE_CASE = trainer.callback_metrics # Log and save results to file __SCREAMING_SNAKE_CASE = os.path.join(pl_module.hparams.output_dir , """test_results.txt""" ) with open(__SCREAMING_SNAKE_CASE , """w""" ) as writer: for key in sorted(__SCREAMING_SNAKE_CASE ): if key not in ["log", "progress_bar"]: rank_zero_info("""{} = {}\n""".format(__SCREAMING_SNAKE_CASE , str(metrics[key] ) ) ) writer.write("""{} = {}\n""".format(__SCREAMING_SNAKE_CASE , str(metrics[key] ) ) ) def a__ ( a__ , a__ ): """simple docstring""" parser.add_argument( """--output_dir""" , default=str(Path(a__ ).parent / """test_run""" / """model_checkpoints""" ) , type=a__ , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument( """--fp16""" , action="""store_true""" , help="""Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit""" , ) parser.add_argument( """--fp16_opt_level""" , type=a__ , default="""O2""" , help=( """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3'].""" """See details at https://nvidia.github.io/apex/amp.html""" ) , ) parser.add_argument("""--n_tpu_cores""" , dest="""tpu_cores""" , type=a__ ) parser.add_argument("""--max_grad_norm""" , dest="""gradient_clip_val""" , default=1.0 , type=a__ , help="""Max gradient norm""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_predict""" , action="""store_true""" , help="""Whether to run predictions on the test set.""" ) parser.add_argument( """--gradient_accumulation_steps""" , dest="""accumulate_grad_batches""" , type=a__ , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--seed""" , type=a__ , default=42 , help="""random seed for initialization""" ) parser.add_argument( """--data_dir""" , default=str(Path(a__ ).parent / """test_run""" / """dummy-train-data""" ) , type=a__ , help="""The input data dir. Should contain the training files for the CoNLL-2003 NER task.""" , ) def a__ ( a__ , a__ , a__=None , a__=True , a__=[] , a__=None , a__=None , **a__ , ): """simple docstring""" pl.seed_everything(args.seed ) # init model __SCREAMING_SNAKE_CASE = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=a__ ) # add custom checkpoints if checkpoint_callback is None: __SCREAMING_SNAKE_CASE = pl.callbacks.ModelCheckpoint( filepath=args.output_dir , prefix="""checkpoint""" , monitor="""val_loss""" , mode="""min""" , save_top_k=1 ) if early_stopping_callback: extra_callbacks.append(a__ ) if logging_callback is None: __SCREAMING_SNAKE_CASE = LoggingCallback() __SCREAMING_SNAKE_CASE = {} if args.fpaa: __SCREAMING_SNAKE_CASE = 16 if args.gpus > 1: __SCREAMING_SNAKE_CASE = """auto""" __SCREAMING_SNAKE_CASE = """ddp""" __SCREAMING_SNAKE_CASE = args.accumulate_grad_batches __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = """auto""" __SCREAMING_SNAKE_CASE = pl.Trainer.from_argparse_args( a__ , weights_summary=a__ , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=a__ , val_check_interval=1 , num_sanity_val_steps=2 , **a__ , ) if args.do_train: trainer.fit(a__ ) else: print("""RAG modeling tests with new set functions successfuly executed!""" ) return trainer
331
1
'''simple docstring''' import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class SCREAMING_SNAKE_CASE( lowerCAmelCase_ ): """simple docstring""" lowerCamelCase__ = (UniPCMultistepScheduler,) lowerCamelCase__ = (("""num_inference_steps""", 25),) def A ( self : List[Any] , **__snake_case : str ) -> Any: UpperCAmelCase : Any = { '''num_train_timesteps''': 1000, '''beta_start''': 0.00_01, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''solver_order''': 2, '''solver_type''': '''bh2''', } config.update(**__snake_case ) return config def A ( self : List[Any] , __snake_case : Union[str, Any]=0 , **__snake_case : Any ) -> List[Any]: UpperCAmelCase : int = dict(self.forward_default_kwargs ) UpperCAmelCase : str = kwargs.pop('''num_inference_steps''' , __snake_case ) UpperCAmelCase : int = self.dummy_sample UpperCAmelCase : str = 0.1 * sample UpperCAmelCase : List[str] = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: UpperCAmelCase : List[Any] = self.get_scheduler_config(**__snake_case ) UpperCAmelCase : Union[str, Any] = scheduler_class(**__snake_case ) scheduler.set_timesteps(__snake_case ) # copy over dummy past residuals UpperCAmelCase : Optional[int] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__snake_case ) UpperCAmelCase : List[str] = scheduler_class.from_pretrained(__snake_case ) new_scheduler.set_timesteps(__snake_case ) # copy over dummy past residuals UpperCAmelCase : Optional[int] = dummy_past_residuals[: new_scheduler.config.solver_order] UpperCAmelCase : Union[str, Any] = sample, sample for t in range(__snake_case , time_step + scheduler.config.solver_order + 1 ): UpperCAmelCase : int = scheduler.step(__snake_case , __snake_case , __snake_case , **__snake_case ).prev_sample UpperCAmelCase : Any = new_scheduler.step(__snake_case , __snake_case , __snake_case , **__snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def A ( self : List[Any] , __snake_case : List[str]=0 , **__snake_case : str ) -> Optional[Any]: UpperCAmelCase : Tuple = dict(self.forward_default_kwargs ) UpperCAmelCase : Optional[int] = kwargs.pop('''num_inference_steps''' , __snake_case ) UpperCAmelCase : List[Any] = self.dummy_sample UpperCAmelCase : List[str] = 0.1 * sample UpperCAmelCase : List[str] = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: UpperCAmelCase : Tuple = self.get_scheduler_config() UpperCAmelCase : int = scheduler_class(**__snake_case ) scheduler.set_timesteps(__snake_case ) # copy over dummy past residuals (must be after setting timesteps) UpperCAmelCase : Optional[int] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__snake_case ) UpperCAmelCase : str = scheduler_class.from_pretrained(__snake_case ) # copy over dummy past residuals new_scheduler.set_timesteps(__snake_case ) # copy over dummy past residual (must be after setting timesteps) UpperCAmelCase : Optional[int] = dummy_past_residuals[: new_scheduler.config.solver_order] UpperCAmelCase : Dict = scheduler.step(__snake_case , __snake_case , __snake_case , **__snake_case ).prev_sample UpperCAmelCase : str = new_scheduler.step(__snake_case , __snake_case , __snake_case , **__snake_case ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def A ( self : Any , __snake_case : Union[str, Any]=None , **__snake_case : List[Any] ) -> Optional[int]: if scheduler is None: UpperCAmelCase : Any = self.scheduler_classes[0] UpperCAmelCase : Dict = self.get_scheduler_config(**__snake_case ) UpperCAmelCase : List[str] = scheduler_class(**__snake_case ) UpperCAmelCase : int = self.scheduler_classes[0] UpperCAmelCase : Optional[Any] = self.get_scheduler_config(**__snake_case ) UpperCAmelCase : Optional[int] = scheduler_class(**__snake_case ) UpperCAmelCase : List[str] = 10 UpperCAmelCase : Any = self.dummy_model() UpperCAmelCase : Optional[int] = self.dummy_sample_deter scheduler.set_timesteps(__snake_case ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : Optional[int] = model(__snake_case , __snake_case ) UpperCAmelCase : Union[str, Any] = scheduler.step(__snake_case , __snake_case , __snake_case ).prev_sample return sample def A ( self : Union[str, Any] ) -> str: UpperCAmelCase : Optional[Any] = dict(self.forward_default_kwargs ) UpperCAmelCase : str = kwargs.pop('''num_inference_steps''' , __snake_case ) for scheduler_class in self.scheduler_classes: UpperCAmelCase : Optional[int] = self.get_scheduler_config() UpperCAmelCase : Union[str, Any] = scheduler_class(**__snake_case ) UpperCAmelCase : List[Any] = self.dummy_sample UpperCAmelCase : str = 0.1 * sample if num_inference_steps is not None and hasattr(__snake_case , '''set_timesteps''' ): scheduler.set_timesteps(__snake_case ) elif num_inference_steps is not None and not hasattr(__snake_case , '''set_timesteps''' ): UpperCAmelCase : Optional[int] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) UpperCAmelCase : Tuple = [residual + 0.2, residual + 0.15, residual + 0.10] UpperCAmelCase : Any = dummy_past_residuals[: scheduler.config.solver_order] UpperCAmelCase : str = scheduler.timesteps[5] UpperCAmelCase : Any = scheduler.timesteps[6] UpperCAmelCase : Dict = scheduler.step(__snake_case , __snake_case , __snake_case , **__snake_case ).prev_sample UpperCAmelCase : Dict = scheduler.step(__snake_case , __snake_case , __snake_case , **__snake_case ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def A ( self : Union[str, Any] ) -> Dict: # make sure that iterating over schedulers with same config names gives same results # for defaults UpperCAmelCase : Union[str, Any] = UniPCMultistepScheduler(**self.get_scheduler_config() ) UpperCAmelCase : List[str] = self.full_loop(scheduler=__snake_case ) UpperCAmelCase : Optional[int] = torch.mean(torch.abs(__snake_case ) ) assert abs(result_mean.item() - 0.24_64 ) < 1E-3 UpperCAmelCase : int = DPMSolverSinglestepScheduler.from_config(scheduler.config ) UpperCAmelCase : Union[str, Any] = DEISMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase : str = DPMSolverMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase : Optional[int] = UniPCMultistepScheduler.from_config(scheduler.config ) UpperCAmelCase : Dict = self.full_loop(scheduler=__snake_case ) UpperCAmelCase : Any = torch.mean(torch.abs(__snake_case ) ) assert abs(result_mean.item() - 0.24_64 ) < 1E-3 def A ( self : Optional[Any] ) -> Dict: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__snake_case ) def A ( self : int ) -> Dict: self.check_over_configs(thresholding=__snake_case ) for order in [1, 2, 3]: for solver_type in ["bh1", "bh2"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__snake_case , prediction_type=__snake_case , sample_max_value=__snake_case , solver_order=__snake_case , solver_type=__snake_case , ) def A ( self : Optional[int] ) -> Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__snake_case ) def A ( self : Tuple ) -> Optional[int]: for solver_type in ["bh1", "bh2"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__snake_case , solver_type=__snake_case , prediction_type=__snake_case , ) UpperCAmelCase : List[str] = self.full_loop( solver_order=__snake_case , solver_type=__snake_case , prediction_type=__snake_case , ) assert not torch.isnan(__snake_case ).any(), "Samples have nan numbers" def A ( self : Dict ) -> str: self.check_over_configs(lower_order_final=__snake_case ) self.check_over_configs(lower_order_final=__snake_case ) def A ( self : int ) -> List[str]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__snake_case , time_step=0 ) def A ( self : Union[str, Any] ) -> Optional[Any]: UpperCAmelCase : Optional[Any] = self.full_loop() UpperCAmelCase : str = torch.mean(torch.abs(__snake_case ) ) assert abs(result_mean.item() - 0.24_64 ) < 1E-3 def A ( self : List[str] ) -> Any: UpperCAmelCase : Optional[Any] = self.full_loop(prediction_type='''v_prediction''' ) UpperCAmelCase : Union[str, Any] = torch.mean(torch.abs(__snake_case ) ) assert abs(result_mean.item() - 0.10_14 ) < 1E-3 def A ( self : List[Any] ) -> Union[str, Any]: UpperCAmelCase : str = self.scheduler_classes[0] UpperCAmelCase : List[Any] = self.get_scheduler_config(thresholding=__snake_case , dynamic_thresholding_ratio=0 ) UpperCAmelCase : str = scheduler_class(**__snake_case ) UpperCAmelCase : Tuple = 10 UpperCAmelCase : List[Any] = self.dummy_model() UpperCAmelCase : List[Any] = self.dummy_sample_deter.half() scheduler.set_timesteps(__snake_case ) for i, t in enumerate(scheduler.timesteps ): UpperCAmelCase : List[str] = model(__snake_case , __snake_case ) UpperCAmelCase : Optional[int] = scheduler.step(__snake_case , __snake_case , __snake_case ).prev_sample assert sample.dtype == torch.floataa def A ( self : Optional[int] , **__snake_case : Optional[int] ) -> List[str]: for scheduler_class in self.scheduler_classes: UpperCAmelCase : Optional[Any] = self.get_scheduler_config(**__snake_case ) UpperCAmelCase : List[str] = scheduler_class(**__snake_case ) scheduler.set_timesteps(scheduler.config.num_train_timesteps ) assert len(scheduler.timesteps.unique() ) == scheduler.num_inference_steps
23
"""simple docstring""" from functools import lru_cache def snake_case__ ( __lowerCamelCase : int ): """simple docstring""" lowerCamelCase__ : Optional[Any] =2 lowerCamelCase__ : Optional[int] =set() while i * i <= n: if n % i: i += 1 else: n //= i factors.add(__lowerCamelCase ) if n > 1: factors.add(__lowerCamelCase ) return factors @lru_cache def snake_case__ ( __lowerCamelCase : int ): """simple docstring""" return len(unique_prime_factors(__lowerCamelCase ) ) def snake_case__ ( __lowerCamelCase : list ): """simple docstring""" return len(set(__lowerCamelCase ) ) in (0, 1) def snake_case__ ( __lowerCamelCase : int ): """simple docstring""" lowerCamelCase__ : Tuple =2 while True: # Increment each value of a generated range lowerCamelCase__ : Tuple =[base + i for i in range(__lowerCamelCase )] # Run elements through out unique_prime_factors function # Append our target number to the end. lowerCamelCase__ : Optional[Any] =[upf_len(__lowerCamelCase ) for x in group] checker.append(__lowerCamelCase ) # If all numbers in the list are equal, return the group variable. if equality(__lowerCamelCase ): return group # Increment our base variable by 1 base += 1 def snake_case__ ( __lowerCamelCase : int = 4 ): """simple docstring""" lowerCamelCase__ : List[Any] =run(__lowerCamelCase ) return results[0] if len(__lowerCamelCase ) else None if __name__ == "__main__": print(solution())
238
0
"""simple docstring""" _a : Any = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _a : Dict = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] _a : List[Any] = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', } def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : int ,_lowerCamelCase : int ) -> str: assert len(str(_lowerCamelCase ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: _lowerCAmelCase : Any = year // 100 _lowerCAmelCase : int = (5 * (century % 4) + 2) % 7 _lowerCAmelCase : Tuple = year % 100 _lowerCAmelCase : str = centurian % 12 _lowerCAmelCase : Union[str, Any] = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 _lowerCAmelCase : Optional[Any] = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) _lowerCAmelCase : List[str] = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
126
"""simple docstring""" import os from pathlib import Path import numpy as np import pytest from pack_dataset import pack_data_dir from parameterized import parameterized from save_len_file import save_len_file from torch.utils.data import DataLoader from transformers import AutoTokenizer from transformers.models.mbart.modeling_mbart import shift_tokens_right from transformers.testing_utils import TestCasePlus, slow from utils import FAIRSEQ_AVAILABLE, DistributedSortishSampler, LegacySeqaSeqDataset, SeqaSeqDataset _a : Optional[int] = 'bert-base-cased' _a : Optional[Any] = 'google/pegasus-xsum' _a : Union[str, Any] = [' Sam ate lunch today.', 'Sams lunch ingredients.'] _a : int = ['A very interesting story about what I ate for lunch.', 'Avocado, celery, turkey, coffee'] _a : Union[str, Any] = 'patrickvonplaten/t5-tiny-random' _a : Tuple = 'sshleifer/bart-tiny-random' _a : str = 'sshleifer/tiny-mbart' _a : Optional[int] = 'sshleifer/tiny-marian-en-de' def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Path ,_lowerCamelCase : list ) -> str: _lowerCAmelCase : List[Any] = """\n""".join(_lowerCamelCase ) Path(_lowerCamelCase ).open("""w""" ).writelines(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ) -> Union[str, Any]: for split in ["train", "val", "test"]: _dump_articles(os.path.join(_lowerCamelCase ,f"{split}.source" ) ,_lowerCamelCase ) _dump_articles(os.path.join(_lowerCamelCase ,f"{split}.target" ) ,_lowerCamelCase ) return tmp_dir class __A ( SCREAMING_SNAKE_CASE_ ): @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) @slow def __A ( self , a__ ): _lowerCAmelCase : str = AutoTokenizer.from_pretrained(a__ ) _lowerCAmelCase : Any = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) _lowerCAmelCase : Union[str, Any] = max(len(tokenizer.encode(a__ ) ) for a in ARTICLES ) _lowerCAmelCase : Optional[int] = max(len(tokenizer.encode(a__ ) ) for a in SUMMARIES ) _lowerCAmelCase : str = 4 _lowerCAmelCase : Optional[int] = 8 assert max_len_target > max_src_len # Will be truncated assert max_len_source > max_src_len # Will be truncated _lowerCAmelCase , _lowerCAmelCase : Optional[int] = """ro_RO""", """de_DE""" # ignored for all but mbart, but never causes error. _lowerCAmelCase : Optional[int] = SeqaSeqDataset( a__ , data_dir=a__ , type_path="""train""" , max_source_length=a__ , max_target_length=a__ , src_lang=a__ , tgt_lang=a__ , ) _lowerCAmelCase : int = DataLoader(a__ , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert isinstance(a__ , a__ ) assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_src_len # show that targets are the same len assert batch["labels"].shape[1] == max_tgt_len if tok_name != MBART_TINY: continue # check language codes in correct place _lowerCAmelCase : Any = shift_tokens_right(batch["""labels"""] , tokenizer.pad_token_id ) assert batch["decoder_input_ids"][0, 0].item() == tokenizer.lang_code_to_id[tgt_lang] assert batch["decoder_input_ids"][0, -1].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -2].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -1].item() == tokenizer.lang_code_to_id[src_lang] break # No need to test every batch @parameterized.expand([BART_TINY, BERT_BASE_CASED] ) def __A ( self , a__ ): _lowerCAmelCase : Any = AutoTokenizer.from_pretrained(a__ ) _lowerCAmelCase : Union[str, Any] = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) _lowerCAmelCase : Optional[int] = max(len(tokenizer.encode(a__ ) ) for a in ARTICLES ) _lowerCAmelCase : Any = max(len(tokenizer.encode(a__ ) ) for a in SUMMARIES ) _lowerCAmelCase : int = 4 _lowerCAmelCase : List[str] = LegacySeqaSeqDataset( a__ , data_dir=a__ , type_path="""train""" , max_source_length=20 , max_target_length=a__ , ) _lowerCAmelCase : List[Any] = DataLoader(a__ , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_len_source assert 20 >= batch["input_ids"].shape[1] # trimmed significantly # show that targets were truncated assert batch["labels"].shape[1] == trunc_target # Truncated assert max_len_target > trunc_target # Truncated break # No need to test every batch def __A ( self ): _lowerCAmelCase : Any = AutoTokenizer.from_pretrained("""facebook/mbart-large-cc25""" ) _lowerCAmelCase : List[Any] = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) _lowerCAmelCase : List[Any] = tmp_dir.joinpath("""train.source""" ).open().readlines() _lowerCAmelCase : str = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) pack_data_dir(a__ , a__ , 128 , a__ ) _lowerCAmelCase : List[Any] = {x.name for x in tmp_dir.iterdir()} _lowerCAmelCase : Tuple = {x.name for x in save_dir.iterdir()} _lowerCAmelCase : Union[str, Any] = save_dir.joinpath("""train.source""" ).open().readlines() # orig: [' Sam ate lunch today.\n', 'Sams lunch ingredients.'] # desired_packed: [' Sam ate lunch today.\n Sams lunch ingredients.'] assert len(a__ ) < len(a__ ) assert len(a__ ) == 1 assert len(packed_examples[0] ) == sum(len(a__ ) for x in orig_examples ) assert orig_paths == new_paths @pytest.mark.skipif(not FAIRSEQ_AVAILABLE , reason="""This test requires fairseq""" ) def __A ( self ): if not FAIRSEQ_AVAILABLE: return _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Tuple = self._get_dataset(max_len=64 ) _lowerCAmelCase : Optional[int] = 64 _lowerCAmelCase : str = ds.make_dynamic_sampler(a__ , required_batch_size_multiple=a__ ) _lowerCAmelCase : int = [len(a__ ) for x in batch_sampler] assert len(set(a__ ) ) > 1 # it's not dynamic batch size if every batch is the same length assert sum(a__ ) == len(a__ ) # no dropped or added examples _lowerCAmelCase : List[str] = DataLoader(a__ , batch_sampler=a__ , collate_fn=ds.collate_fn , num_workers=2 ) _lowerCAmelCase : List[Any] = [] _lowerCAmelCase : Optional[int] = [] for batch in data_loader: _lowerCAmelCase : int = batch["""input_ids"""].shape _lowerCAmelCase : Union[str, Any] = src_shape[0] assert bs % required_batch_size_multiple == 0 or bs < required_batch_size_multiple _lowerCAmelCase : List[str] = np.product(batch["""input_ids"""].shape ) num_src_per_batch.append(a__ ) if num_src_tokens > (max_tokens * 1.1): failures.append(a__ ) assert num_src_per_batch[0] == max(a__ ) if failures: raise AssertionError(F"too many tokens in {len(a__ )} batches" ) def __A ( self ): _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Dict = self._get_dataset(max_len=512 ) _lowerCAmelCase : int = 2 _lowerCAmelCase : List[str] = ds.make_sortish_sampler(a__ , shuffle=a__ ) _lowerCAmelCase : Dict = DataLoader(a__ , batch_size=a__ , collate_fn=ds.collate_fn , num_workers=2 ) _lowerCAmelCase : int = DataLoader(a__ , batch_size=a__ , collate_fn=ds.collate_fn , num_workers=2 , sampler=a__ ) _lowerCAmelCase : int = tokenizer.pad_token_id def count_pad_tokens(a__ , a__="input_ids" ): return [batch[k].eq(a__ ).sum().item() for batch in data_loader] assert sum(count_pad_tokens(a__ , k="""labels""" ) ) < sum(count_pad_tokens(a__ , k="""labels""" ) ) assert sum(count_pad_tokens(a__ ) ) < sum(count_pad_tokens(a__ ) ) assert len(a__ ) == len(a__ ) def __A ( self , a__=1000 , a__=128 ): if os.getenv("""USE_REAL_DATA""" , a__ ): _lowerCAmelCase : List[str] = """examples/seq2seq/wmt_en_ro""" _lowerCAmelCase : str = max_len * 2 * 64 if not Path(a__ ).joinpath("""train.len""" ).exists(): save_len_file(a__ , a__ ) else: _lowerCAmelCase : List[str] = """examples/seq2seq/test_data/wmt_en_ro""" _lowerCAmelCase : Dict = max_len * 4 save_len_file(a__ , a__ ) _lowerCAmelCase : Optional[Any] = AutoTokenizer.from_pretrained(a__ ) _lowerCAmelCase : Dict = SeqaSeqDataset( a__ , data_dir=a__ , type_path="""train""" , max_source_length=a__ , max_target_length=a__ , n_obs=a__ , ) return ds, max_tokens, tokenizer def __A ( self ): _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = self._get_dataset() _lowerCAmelCase : Any = set(DistributedSortishSampler(a__ , 256 , num_replicas=2 , rank=0 , add_extra_examples=a__ ) ) _lowerCAmelCase : Optional[int] = set(DistributedSortishSampler(a__ , 256 , num_replicas=2 , rank=1 , add_extra_examples=a__ ) ) assert idsa.intersection(a__ ) == set() @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) def __A ( self , a__ ): _lowerCAmelCase : int = AutoTokenizer.from_pretrained(a__ , use_fast=a__ ) if tok_name == MBART_TINY: _lowerCAmelCase : Dict = SeqaSeqDataset( a__ , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path="""train""" , max_source_length=4 , max_target_length=8 , src_lang="""EN""" , tgt_lang="""FR""" , ) _lowerCAmelCase : Optional[Any] = train_dataset.dataset_kwargs assert "src_lang" in kwargs and "tgt_lang" in kwargs else: _lowerCAmelCase : List[Any] = SeqaSeqDataset( a__ , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path="""train""" , max_source_length=4 , max_target_length=8 , ) _lowerCAmelCase : Tuple = train_dataset.dataset_kwargs assert "add_prefix_space" not in kwargs if tok_name != BART_TINY else "add_prefix_space" in kwargs assert len(a__ ) == 1 if tok_name == BART_TINY else len(a__ ) == 0
126
1
'''simple docstring''' import argparse import math import traceback import dateutil.parser as date_parser import requests def A__ ( UpperCAmelCase_ ): _UpperCamelCase : Union[str, Any] = {} _UpperCamelCase : List[Any] = job['started_at'] _UpperCamelCase : List[str] = job['completed_at'] _UpperCamelCase : Dict = date_parser.parse(_A ) _UpperCamelCase : Tuple = date_parser.parse(_A ) _UpperCamelCase : str = round((end_datetime - start_datetime).total_seconds() / 60.0 ) _UpperCamelCase : Optional[Any] = start _UpperCamelCase : Optional[int] = end _UpperCamelCase : List[Any] = duration_in_min return job_info def A__ ( UpperCAmelCase_ , UpperCAmelCase_=None ): _UpperCamelCase : Union[str, Any] = None if token is not None: _UpperCamelCase : List[Any] = {'Accept': 'application/vnd.github+json', 'Authorization': f'Bearer {token}'} _UpperCamelCase : Tuple = f'https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100' _UpperCamelCase : List[str] = requests.get(_A , headers=_A ).json() _UpperCamelCase : Dict = {} try: job_time.update({job['name']: extract_time_from_single_job(_A ) for job in result['jobs']} ) _UpperCamelCase : Tuple = math.ceil((result['total_count'] - 1_0_0) / 1_0_0 ) for i in range(_A ): _UpperCamelCase : Optional[int] = requests.get(url + f'&page={i + 2}' , headers=_A ).json() job_time.update({job['name']: extract_time_from_single_job(_A ) for job in result['jobs']} ) return job_time except Exception: print(f'Unknown error, could not fetch links:\n{traceback.format_exc()}' ) return {} if __name__ == "__main__": snake_case_ : str = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') snake_case_ : List[Any] = parser.parse_args() snake_case_ : Any = get_job_time(args.workflow_run_id) snake_case_ : List[Any] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(F"""{k}: {v['duration']}""")
83
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowercase = { '''configuration_instructblip''': [ '''INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''InstructBlipConfig''', '''InstructBlipQFormerConfig''', '''InstructBlipVisionConfig''', ], '''processing_instructblip''': ['''InstructBlipProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = [ '''INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''InstructBlipQFormerModel''', '''InstructBlipPreTrainedModel''', '''InstructBlipForConditionalGeneration''', '''InstructBlipVisionModel''', ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys __lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
272
0
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType lowerCAmelCase : List[Any] = logging.get_logger(__name__) lowerCAmelCase : Optional[int] = { 'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json', 'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json', 'microsoft/deberta-v2-xlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json' ), 'microsoft/deberta-v2-xxlarge-mnli': ( 'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json' ), } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """deberta-v2""" def __init__( self , A_=128100 , A_=1536 , A_=24 , A_=24 , A_=6144 , A_="gelu" , A_=0.1 , A_=0.1 , A_=512 , A_=0 , A_=0.02 , A_=1e-7 , A_=False , A_=-1 , A_=0 , A_=True , A_=None , A_=0 , A_="gelu" , **A_ , )-> Any: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = max_position_embeddings UpperCamelCase = type_vocab_size UpperCamelCase = initializer_range UpperCamelCase = relative_attention UpperCamelCase = max_relative_positions UpperCamelCase = pad_token_id UpperCamelCase = position_biased_input # Backwards compatibility if type(A_ ) == str: UpperCamelCase = [x.strip() for x in pos_att_type.lower().split('|' )] UpperCamelCase = pos_att_type UpperCamelCase = vocab_size UpperCamelCase = layer_norm_eps UpperCamelCase = kwargs.get('pooler_hidden_size' , A_ ) UpperCamelCase = pooler_dropout UpperCamelCase = pooler_hidden_act class SCREAMING_SNAKE_CASE__ ( snake_case_): @property def UpperCAmelCase_ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase = {0: 'batch', 1: 'sequence'} if self._config.type_vocab_size > 0: return OrderedDict( [('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('token_type_ids', dynamic_axis)] ) else: return OrderedDict([('input_ids', dynamic_axis), ('attention_mask', dynamic_axis)] ) @property def UpperCAmelCase_ ( self )-> int: '''simple docstring''' return 12 def UpperCAmelCase_ ( self , A_ , A_ = -1 , A_ = -1 , A_ = -1 , A_ = False , A_ = None , A_ = 3 , A_ = 40 , A_ = 40 , A_ = None , )-> Mapping[str, Any]: '''simple docstring''' UpperCamelCase = super().generate_dummy_inputs(preprocessor=A_ , framework=A_ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
251
'''simple docstring''' from itertools import product def A_( A : int , A : int): UpperCamelCase = sides_number UpperCamelCase = max_face_number * dice_number UpperCamelCase = [0] * (max_total + 1) UpperCamelCase = 1 UpperCamelCase = range(A , max_face_number + 1) for dice_numbers in product(A , repeat=A): UpperCamelCase = sum(A) totals_frequencies[total] += 1 return totals_frequencies def A_( ): UpperCamelCase = total_frequency_distribution( sides_number=4 , dice_number=9) UpperCamelCase = total_frequency_distribution( sides_number=6 , dice_number=6) UpperCamelCase = 0 UpperCamelCase = 9 UpperCamelCase = 4 * 9 UpperCamelCase = 6 for peter_total in range(A , max_peter_total + 1): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total]) UpperCamelCase = (4**9) * (6**6) UpperCamelCase = peter_wins_count / total_games_number UpperCamelCase = round(A , ndigits=7) return rounded_peter_win_probability if __name__ == "__main__": print(f"""{solution() = }""")
251
1
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv('TEST_SAGEMAKER' , 'False' ) ) is not True , reason='Skipping test because should only be run when releasing minor transformers version' , ) @pytest.mark.usefixtures('sm_env' ) @parameterized_class( [ { 'framework': 'pytorch', 'script': 'run_glue.py', 'model_name_or_path': 'distilbert-base-cased', 'instance_type': 'ml.g4dn.xlarge', 'results': {'train_runtime': 6_50, 'eval_accuracy': 0.6, 'eval_loss': 0.9}, }, { 'framework': 'tensorflow', 'script': 'run_tf.py', 'model_name_or_path': 'distilbert-base-cased', 'instance_type': 'ml.g4dn.xlarge', 'results': {'train_runtime': 6_00, 'eval_accuracy': 0.3, 'eval_loss': 0.9}, }, ] ) class A ( unittest.TestCase ): """simple docstring""" def snake_case__ ( self : List[str] )-> List[str]: '''simple docstring''' if self.framework == "pytorch": subprocess.run( F'cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'.split(),encoding='utf-8',check=lowercase_,) assert hasattr(self,'env' ) def snake_case__ ( self : int,lowercase_ : Optional[Any]=1 )-> Optional[int]: '''simple docstring''' return HuggingFace( entry_point=self.script,source_dir=self.env.test_path,role=self.env.role,image_uri=self.env.image_uri,base_job_name=F'{self.env.base_job_name}-single',instance_count=lowercase_,instance_type=self.instance_type,debugger_hook_config=lowercase_,hyperparameters={**self.env.hyperparameters, 'model_name_or_path': self.model_name_or_path},metric_definitions=self.env.metric_definitions,py_version='py36',) def snake_case__ ( self : List[Any],lowercase_ : List[Any] )-> Optional[int]: '''simple docstring''' TrainingJobAnalytics(lowercase_ ).export_csv(F'{self.env.test_path}/{job_name}_metrics.csv' ) def snake_case__ ( self : Any )-> Optional[int]: '''simple docstring''' A__ = self.create_estimator() # run training estimator.fit() # result dataframe A__ = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis A__ = list(result_metrics_df[result_metrics_df.metric_name == 'eval_accuracy']['value'] ) A__ = list(result_metrics_df[result_metrics_df.metric_name == 'eval_loss']['value'] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping A__ = ( Session().describe_training_job(estimator.latest_training_job.name ).get('TrainingTimeInSeconds',9_9_9_9_9_9 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['eval_accuracy'] for t in eval_accuracy ) assert all(t <= self.results['eval_loss'] for t in eval_loss ) # dump tests result into json file to share in PR with open(F'{estimator.latest_training_job.name}.json','w' ) as outfile: json.dump({'train_time': train_runtime, 'eval_accuracy': eval_accuracy, 'eval_loss': eval_loss},lowercase_ )
7
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
0
"""simple docstring""" 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 snake_case : def __init__( self , __UpperCAmelCase , __UpperCAmelCase=2 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=2 , __UpperCAmelCase=7 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=99 , __UpperCAmelCase=36 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=37 , __UpperCAmelCase="gelu" , __UpperCAmelCase=0.1 , __UpperCAmelCase=0.1 , __UpperCAmelCase=5_12 , __UpperCAmelCase=16 , __UpperCAmelCase=2 , __UpperCAmelCase=0.02 , __UpperCAmelCase=6 , __UpperCAmelCase=6 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=None , __UpperCAmelCase=10_00 , ) ->Dict: a_ = parent a_ = batch_size a_ = num_channels a_ = image_size a_ = patch_size a_ = text_seq_length a_ = is_training a_ = use_input_mask a_ = use_token_type_ids a_ = use_labels a_ = vocab_size a_ = hidden_size a_ = num_hidden_layers a_ = num_attention_heads a_ = intermediate_size a_ = hidden_act a_ = hidden_dropout_prob a_ = attention_probs_dropout_prob a_ = max_position_embeddings a_ = type_vocab_size a_ = type_sequence_label_size a_ = initializer_range a_ = coordinate_size a_ = shape_size a_ = num_labels a_ = num_choices a_ = scope a_ = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) a_ = text_seq_length a_ = (image_size // patch_size) ** 2 + 1 a_ = self.text_seq_length + self.image_seq_length def UpperCAmelCase__ ( self) ->Optional[int]: a_ = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size) a_ = 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]: a_ = bbox[i, j, 3] a_ = bbox[i, j, 1] a_ = t if bbox[i, j, 2] < bbox[i, j, 0]: a_ = bbox[i, j, 2] a_ = bbox[i, j, 0] a_ = t a_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) a_ = None if self.use_input_mask: a_ = random_attention_mask([self.batch_size, self.text_seq_length]) a_ = None if self.use_token_type_ids: a_ = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size) a_ = None a_ = None if self.use_labels: a_ = ids_tensor([self.batch_size] , self.type_sequence_label_size) a_ = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels) a_ = 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 UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->Optional[int]: a_ = LayoutLMvaModel(config=snake_case__) model.to(snake_case__) model.eval() # text + image a_ = model(snake_case__ , pixel_values=snake_case__) a_ = model( snake_case__ , bbox=snake_case__ , pixel_values=snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__) a_ = model(snake_case__ , bbox=snake_case__ , pixel_values=snake_case__ , token_type_ids=snake_case__) a_ = model(snake_case__ , bbox=snake_case__ , pixel_values=snake_case__) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) # text only a_ = model(snake_case__) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size)) # image only a_ = model(pixel_values=snake_case__) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size)) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->Optional[int]: a_ = self.num_labels a_ = LayoutLMvaForSequenceClassification(snake_case__) model.to(snake_case__) model.eval() a_ = model( snake_case__ , bbox=snake_case__ , pixel_values=snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->Dict: a_ = self.num_labels a_ = LayoutLMvaForTokenClassification(config=snake_case__) model.to(snake_case__) model.eval() a_ = model( snake_case__ , bbox=snake_case__ , pixel_values=snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels)) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->Any: a_ = LayoutLMvaForQuestionAnswering(config=snake_case__) model.to(snake_case__) model.eval() a_ = model( snake_case__ , bbox=snake_case__ , pixel_values=snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , start_positions=snake_case__ , end_positions=snake_case__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def UpperCAmelCase__ ( self) ->str: a_ = self.prepare_config_and_inputs() ( a_ ) = config_and_inputs a_ = { "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 snake_case ( A_ , A_ , unittest.TestCase ): a_ : str = False a_ : Any = False a_ : List[Any] = False a_ : int = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) a_ : Dict = ( {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel} if is_torch_available() else {} ) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase) ->Any: return True def UpperCAmelCase__ ( self) ->List[Any]: a_ = LayoutLMvaModelTester(self) a_ = ConfigTester(self , config_class=snake_case__ , hidden_size=37) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=False) ->Any: a_ = copy.deepcopy(snake_case__) if model_class in get_values(snake_case__): a_ = { k: v.unsqueeze(1).expand(-1 , self.model_tester.num_choices , -1).contiguous() if isinstance(snake_case__ , torch.Tensor) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(snake_case__): a_ = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=snake_case__) elif model_class in get_values(snake_case__): a_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case__) a_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case__) elif model_class in [ *get_values(snake_case__), ]: a_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case__) elif model_class in [ *get_values(snake_case__), ]: a_ = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=snake_case__ , ) return inputs_dict def UpperCAmelCase__ ( self) ->Any: self.config_tester.run_common_tests() def UpperCAmelCase__ ( self) ->Tuple: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__) def UpperCAmelCase__ ( self) ->str: a_ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a_ = type self.model_tester.create_and_check_model(*snake_case__) def UpperCAmelCase__ ( self) ->Union[str, Any]: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case__) def UpperCAmelCase__ ( self) ->Dict: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case__) def UpperCAmelCase__ ( self) ->Tuple: a_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case__) @slow def UpperCAmelCase__ ( self) ->List[str]: for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a_ = LayoutLMvaModel.from_pretrained(snake_case__) self.assertIsNotNone(snake_case__) def UpperCamelCase ( ) ->int: """simple docstring""" a_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch class snake_case ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self) ->List[str]: return LayoutLMvaImageProcessor(apply_ocr=snake_case__) if is_vision_available() else None @slow def UpperCAmelCase__ ( self) ->Dict: a_ = LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base").to(snake_case__) a_ = self.default_image_processor a_ = prepare_img() a_ = image_processor(images=snake_case__ , return_tensors="pt").pixel_values.to(snake_case__) a_ = torch.tensor([[1, 2]]) a_ = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]).unsqueeze(0) # forward pass a_ = model( input_ids=input_ids.to(snake_case__) , bbox=bbox.to(snake_case__) , pixel_values=pixel_values.to(snake_case__) , ) # verify the logits a_ = torch.Size((1, 1_99, 7_68)) self.assertEqual(outputs.last_hidden_state.shape , snake_case__) a_ = torch.tensor( [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]).to(snake_case__) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , snake_case__ , atol=1E-4))
360
"""simple docstring""" import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision import transforms from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def UpperCamelCase ( UpperCAmelCase ) ->Tuple: """simple docstring""" a_ = [2, 2, 6, 2] if "tiny" in model_name else [2, 2, 18, 2] a_ = True if "large" in model_name or "huge" in model_name else False a_ = True if "large" in model_name or "huge" in model_name else False a_ = True if "large" in model_name or "huge" in model_name else False if "large" in model_name or "xlarge" in model_name or "huge" in model_name: if "fl3" in model_name: a_ = [3, 3, 3, 3] a_ = [5, 5, 5, 5] elif "fl4" in model_name: a_ = [4, 4, 4, 4] a_ = [3, 3, 3, 3] if "tiny" in model_name or "small" in model_name or "base" in model_name: a_ = [3, 3, 3, 3] if "lrf" in model_name: a_ = [3, 3, 3, 3] else: a_ = [2, 2, 2, 2] if "tiny" in model_name: a_ = 96 elif "small" in model_name: a_ = 96 elif "base" in model_name: a_ = 128 elif "large" in model_name: a_ = 192 elif "xlarge" in model_name: a_ = 256 elif "huge" in model_name: a_ = 352 # set label information a_ = "huggingface/label-files" if "large" in model_name or "huge" in model_name: a_ = "imagenet-22k-id2label.json" else: a_ = "imagenet-1k-id2label.json" a_ = json.load(open(hf_hub_download(UpperCAmelCase , UpperCAmelCase , repo_type="dataset" ) , "r" ) ) a_ = {int(UpperCAmelCase ): v for k, v in idalabel.items()} a_ = {v: k for k, v in idalabel.items()} a_ = FocalNetConfig( embed_dim=UpperCAmelCase , depths=UpperCAmelCase , focal_levels=UpperCAmelCase , focal_windows=UpperCAmelCase , use_conv_embed=UpperCAmelCase , idalabel=UpperCAmelCase , labelaid=UpperCAmelCase , use_post_layernorm=UpperCAmelCase , use_layerscale=UpperCAmelCase , ) return config def UpperCamelCase ( UpperCAmelCase ) ->Any: """simple docstring""" if "patch_embed.proj" in name: a_ = name.replace("patch_embed.proj" , "embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: a_ = name.replace("patch_embed.norm" , "embeddings.norm" ) if "layers" in name: a_ = "encoder." + name if "encoder.layers" in name: a_ = name.replace("encoder.layers" , "encoder.stages" ) if "downsample.proj" in name: a_ = name.replace("downsample.proj" , "downsample.projection" ) if "blocks" in name: a_ = name.replace("blocks" , "layers" ) if "modulation.f.weight" in name or "modulation.f.bias" in name: a_ = name.replace("modulation.f" , "modulation.projection_in" ) if "modulation.h.weight" in name or "modulation.h.bias" in name: a_ = name.replace("modulation.h" , "modulation.projection_context" ) if "modulation.proj.weight" in name or "modulation.proj.bias" in name: a_ = name.replace("modulation.proj" , "modulation.projection_out" ) if name == "norm.weight": a_ = "layernorm.weight" if name == "norm.bias": a_ = "layernorm.bias" if "head" in name: a_ = name.replace("head" , "classifier" ) else: a_ = "focalnet." + name return name def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=False ) ->Dict: """simple docstring""" a_ = { "focalnet-tiny": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth", "focalnet-tiny-lrf": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth", "focalnet-small": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth", "focalnet-small-lrf": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth", "focalnet-base": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth", "focalnet-base-lrf": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth", "focalnet-large-lrf-fl3": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth", "focalnet-large-lrf-fl4": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth", "focalnet-xlarge-lrf-fl3": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth", "focalnet-xlarge-lrf-fl4": "https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth", } # fmt: on a_ = model_name_to_url[model_name] print("Checkpoint URL: " , UpperCAmelCase ) a_ = torch.hub.load_state_dict_from_url(UpperCAmelCase , map_location="cpu" )["model"] # rename keys for key in state_dict.copy().keys(): a_ = state_dict.pop(UpperCAmelCase ) a_ = val a_ = get_focalnet_config(UpperCAmelCase ) a_ = FocalNetForImageClassification(UpperCAmelCase ) model.eval() # load state dict model.load_state_dict(UpperCAmelCase ) # verify conversion a_ = "http://images.cocodataset.org/val2017/000000039769.jpg" a_ = BitImageProcessor( do_resize=UpperCAmelCase , size={"shortest_edge": 256} , resample=PILImageResampling.BILINEAR , do_center_crop=UpperCAmelCase , crop_size=224 , do_normalize=UpperCAmelCase , image_mean=UpperCAmelCase , image_std=UpperCAmelCase , ) a_ = Image.open(requests.get(UpperCAmelCase , stream=UpperCAmelCase ).raw ) a_ = processor(images=UpperCAmelCase , return_tensors="pt" ) a_ = transforms.Compose( [ transforms.Resize(256 ), transforms.CenterCrop(224 ), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406] , std=[0.229, 0.224, 0.225] ), ] ) a_ = image_transforms(UpperCAmelCase ).unsqueeze(0 ) # verify pixel_values assert torch.allclose(inputs.pixel_values , UpperCAmelCase , atol=1E-4 ) a_ = model(**UpperCAmelCase ) a_ = outputs.logits.argmax(-1 ).item() print("Predicted class:" , model.config.idalabel[predicted_class_idx] ) print("First values of logits:" , outputs.logits[0, :3] ) if model_name == "focalnet-tiny": a_ = torch.tensor([0.2166, -0.4368, 0.2191] ) elif model_name == "focalnet-tiny-lrf": a_ = torch.tensor([1.1669, 0.0125, -0.1695] ) elif model_name == "focalnet-small": a_ = torch.tensor([0.4917, -0.0430, 0.1341] ) elif model_name == "focalnet-small-lrf": a_ = torch.tensor([-0.2588, -0.5342, -0.2331] ) elif model_name == "focalnet-base": a_ = torch.tensor([-0.1655, -0.4090, -0.1730] ) elif model_name == "focalnet-base-lrf": a_ = torch.tensor([0.5306, -0.0483, -0.3928] ) assert torch.allclose(outputs.logits[0, :3] , UpperCAmelCase , atol=1E-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F'''Saving model and processor of {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(UpperCAmelCase ) processor.save_pretrained(UpperCAmelCase ) if push_to_hub: print(F'''Pushing model and processor of {model_name} to the hub...''' ) model.push_to_hub(F'''{model_name}''' ) processor.push_to_hub(F'''{model_name}''' ) if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='focalnet-tiny', type=str, help='Name of the FocalNet model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model and processor to the hub.', ) UpperCamelCase_ = parser.parse_args() convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
303
0
import unittest from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowercase : """simple docstring""" @staticmethod def __A ( *A , **A ) -> Tuple: '''simple docstring''' pass @is_pipeline_test @require_torch @require_vision class __lowercase ( unittest.TestCase ): """simple docstring""" UpperCamelCase : str = MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING def __A ( self , A , A , A ) -> Optional[int]: '''simple docstring''' lowerCamelCase = pipeline("""visual-question-answering""" , model="""hf-internal-testing/tiny-vilt-random-vqa""" ) lowerCamelCase = [ { """image""": Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ), """question""": """How many cats are there?""", }, { """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""", """question""": """How many cats are there?""", }, ] return vqa_pipeline, examples def __A ( self , A , A ) -> List[str]: '''simple docstring''' lowerCamelCase = vqa_pipeline(A , top_k=1 ) self.assertEqual( A , [ [{"""score""": ANY(A ), """answer""": ANY(A )}], [{"""score""": ANY(A ), """answer""": ANY(A )}], ] , ) @require_torch def __A ( self ) -> Any: '''simple docstring''' lowerCamelCase = pipeline("""visual-question-answering""" , model="""hf-internal-testing/tiny-vilt-random-vqa""" ) lowerCamelCase = """./tests/fixtures/tests_samples/COCO/000000039769.png""" lowerCamelCase = """How many cats are there?""" lowerCamelCase = vqa_pipeline(image=A , question="""How many cats are there?""" , top_k=2 ) self.assertEqual( A , [{"""score""": ANY(A ), """answer""": ANY(A )}, {"""score""": ANY(A ), """answer""": ANY(A )}] ) lowerCamelCase = vqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( A , [{"""score""": ANY(A ), """answer""": ANY(A )}, {"""score""": ANY(A ), """answer""": ANY(A )}] ) @slow @require_torch def __A ( self ) -> str: '''simple docstring''' lowerCamelCase = pipeline("""visual-question-answering""" , model="""dandelin/vilt-b32-finetuned-vqa""" ) lowerCamelCase = """./tests/fixtures/tests_samples/COCO/000000039769.png""" lowerCamelCase = """How many cats are there?""" lowerCamelCase = vqa_pipeline(image=A , question=A , top_k=2 ) self.assertEqual( nested_simplify(A , decimals=4 ) , [{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}] ) lowerCamelCase = vqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(A , decimals=4 ) , [{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}] ) lowerCamelCase = vqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(A , decimals=4 ) , [[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]] * 2 , ) @require_tf @unittest.skip("""Visual question answering not implemented in TF""" ) def __A ( self ) -> Dict: '''simple docstring''' pass
252
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: UpperCAmelCase : Any = None UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) UpperCAmelCase : Any = {"vocab_file": "sentencepiece.model", "tokenizer_file": "tokenizer.json"} UpperCAmelCase : Optional[int] = { "vocab_file": { "google/rembert": "https://huggingface.co/google/rembert/resolve/main/sentencepiece.model", }, "tokenizer_file": { "google/rembert": "https://huggingface.co/google/rembert/resolve/main/tokenizer.json", }, } UpperCAmelCase : List[str] = { "google/rembert": 2_56, } UpperCAmelCase : List[str] = "▁" class __lowercase ( a_ ): """simple docstring""" UpperCamelCase : Dict = VOCAB_FILES_NAMES UpperCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase : Tuple = RemBertTokenizer def __init__( self , A=None , A=None , A=True , A=True , A=False , A="[CLS]" , A="[SEP]" , A="<unk>" , A="[SEP]" , A="<pad>" , A="[CLS]" , A="[MASK]" , **A , ) -> Any: '''simple docstring''' lowerCamelCase = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else mask_token super().__init__( A , tokenizer_file=A , do_lower_case=A , remove_space=A , keep_accents=A , bos_token=A , eos_token=A , unk_token=A , sep_token=A , pad_token=A , cls_token=A , mask_token=A , **A , ) lowerCamelCase = do_lower_case lowerCamelCase = remove_space lowerCamelCase = keep_accents lowerCamelCase = vocab_file lowerCamelCase = False if not self.vocab_file else True def __A ( self , A , A = None ) -> List[int]: '''simple docstring''' lowerCamelCase = [self.sep_token_id] lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def __A ( self , A , A = None , A = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( """You should not supply a second sequence if the provided sequence of """ """ids is already formatted with special tokens for the model.""" ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(A )) + [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1] def __A ( self , A , A = None ) -> List[int]: '''simple docstring''' lowerCamelCase = [self.sep_token_id] lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self , A , A = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(A ): logger.error("""Vocabulary path ({}) should be a directory""".format(A ) ) return lowerCamelCase = os.path.join( A , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A ): copyfile(self.vocab_file , A ) return (out_vocab_file,)
252
1
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def lowerCAmelCase_ ( snake_case_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase_ = tmp_path / "file.csv" UpperCAmelCase_ = textwrap.dedent( "\\n header1,header2\n 1,2\n 10,20\n " ) with open(snake_case_ , "w" ) as f: f.write(snake_case_ ) return str(snake_case_ ) @pytest.fixture def lowerCAmelCase_ ( snake_case_ : str ) -> int: '''simple docstring''' UpperCAmelCase_ = tmp_path / "malformed_file.csv" UpperCAmelCase_ = textwrap.dedent( "\\n header1,header2\n 1,2\n 10,20,\n " ) with open(snake_case_ , "w" ) as f: f.write(snake_case_ ) return str(snake_case_ ) @pytest.fixture def lowerCAmelCase_ ( snake_case_ : List[Any] , snake_case_ : Optional[int] ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase_ = tmp_path / "csv_with_image.csv" UpperCAmelCase_ = textwrap.dedent( f"""\ image {image_file} """ ) with open(snake_case_ , "w" ) as f: f.write(snake_case_ ) return str(snake_case_ ) @pytest.fixture def lowerCAmelCase_ ( snake_case_ : Union[str, Any] ) -> int: '''simple docstring''' UpperCAmelCase_ = tmp_path / "csv_with_label.csv" UpperCAmelCase_ = textwrap.dedent( "\\n label\n good\n bad\n good\n " ) with open(snake_case_ , "w" ) as f: f.write(snake_case_ ) return str(snake_case_ ) @pytest.fixture def lowerCAmelCase_ ( snake_case_ : Any ) -> Dict: '''simple docstring''' UpperCAmelCase_ = tmp_path / "csv_with_int_list.csv" UpperCAmelCase_ = textwrap.dedent( "\\n int_list\n 1 2 3\n 4 5 6\n 7 8 9\n " ) with open(snake_case_ , "w" ) as f: f.write(snake_case_ ) return str(snake_case_ ) def lowerCAmelCase_ ( snake_case_ : Optional[int] , snake_case_ : Optional[int] , snake_case_ : Tuple ) -> Any: '''simple docstring''' UpperCAmelCase_ = Csv() UpperCAmelCase_ = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(snake_case_ , match="Error tokenizing data" ): for _ in generator: pass assert any( record.levelname == "ERROR" and "Failed to read file" in record.message and os.path.basename(snake_case_ ) in record.message for record in caplog.records ) @require_pil def lowerCAmelCase_ ( snake_case_ : Dict ) -> List[str]: '''simple docstring''' with open(snake_case_ , encoding="utf-8" ) as f: UpperCAmelCase_ = f.read().splitlines()[1] UpperCAmelCase_ = Csv(encoding="utf-8" , features=Features({"image": Image()} ) ) UpperCAmelCase_ = csv._generate_tables([[csv_file_with_image]] ) UpperCAmelCase_ = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("image" ).type == Image()() UpperCAmelCase_ = pa_table.to_pydict()["image"] assert generated_content == [{"path": image_file, "bytes": None}] def lowerCAmelCase_ ( snake_case_ : Optional[int] ) -> Any: '''simple docstring''' with open(snake_case_ , encoding="utf-8" ) as f: UpperCAmelCase_ = f.read().splitlines()[1:] UpperCAmelCase_ = Csv(encoding="utf-8" , features=Features({"label": ClassLabel(names=["good", "bad"] )} ) ) UpperCAmelCase_ = csv._generate_tables([[csv_file_with_label]] ) UpperCAmelCase_ = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field("label" ).type == ClassLabel(names=["good", "bad"] )() UpperCAmelCase_ = pa_table.to_pydict()["label"] assert generated_content == [ClassLabel(names=["good", "bad"] ).straint(snake_case_ ) for label in labels] def lowerCAmelCase_ ( snake_case_ : Union[str, Any] ) -> str: '''simple docstring''' UpperCAmelCase_ = Csv(encoding="utf-8" , sep="," , converters={"int_list": lambda snake_case_ : [int(snake_case_ ) for i in x.split()]} ) UpperCAmelCase_ = csv._generate_tables([[csv_file_with_int_list]] ) UpperCAmelCase_ = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field("int_list" ).type ) UpperCAmelCase_ = pa_table.to_pydict()["int_list"] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
106
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available SCREAMING_SNAKE_CASE_: int ={ 'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'], 'configuration_maskformer_swin': ['MaskFormerSwinConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_: List[str] =['MaskFormerFeatureExtractor'] SCREAMING_SNAKE_CASE_: Union[str, Any] =['MaskFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_: Dict =[ 'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'MaskFormerForInstanceSegmentation', 'MaskFormerModel', 'MaskFormerPreTrainedModel', ] SCREAMING_SNAKE_CASE_: List[str] =[ 'MaskFormerSwinBackbone', 'MaskFormerSwinModel', 'MaskFormerSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig from .configuration_maskformer_swin import MaskFormerSwinConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_maskformer import MaskFormerFeatureExtractor from .image_processing_maskformer import MaskFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskformer import ( MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskFormerForInstanceSegmentation, MaskFormerModel, MaskFormerPreTrainedModel, ) from .modeling_maskformer_swin import ( MaskFormerSwinBackbone, MaskFormerSwinModel, MaskFormerSwinPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_: Dict =_LazyModule(__name__, globals()['__file__'], _import_structure)
106
1
"""simple docstring""" def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' if graph[path[curr_ind - 1]][next_ver] == 0: return False # 2. Validate that next vertex is not already in path return not any(vertex == next_ver for vertex in path ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' if curr_ind == len(lowerCAmelCase_ ): # return whether path exists between current and starting vertices return graph[path[curr_ind - 1]][path[0]] == 1 # Recursive Step for next_ver in range(0 , len(lowerCAmelCase_ ) ): if valid_connection(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): # Insert current vertex into path as next transition __SCREAMING_SNAKE_CASE = next_ver # Validate created path if util_hamilton_cycle(lowerCAmelCase_ , lowerCAmelCase_ , curr_ind + 1 ): return True # Backtrack __SCREAMING_SNAKE_CASE = -1 return False def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ = 0 ): '''simple docstring''' __SCREAMING_SNAKE_CASE = [-1] * (len(lowerCAmelCase_ ) + 1) # initialize start and end of path with starting index __SCREAMING_SNAKE_CASE = __SCREAMING_SNAKE_CASE = start_index # evaluate and if we find answer return path either return empty array return path if util_hamilton_cycle(lowerCAmelCase_ , lowerCAmelCase_ , 1 ) else []
54
"""simple docstring""" from jiwer import compute_measures import datasets a__ : Optional[int] = '''\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } ''' a__ : List[str] = '''\ Word error rate (WER) is a common metric of the performance of an automatic speech recognition system. The general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort. This problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate. Word error rate can then be computed as: WER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct words, N is the number of words in the reference (N=S+D+C). This value indicates the average number of errors per reference word. The lower the value, the better the performance of the ASR system with a WER of 0 being a perfect score. ''' a__ : Dict = ''' Compute WER score of transcribed segments against references. Args: references: List of references for each speech input. predictions: List of transcriptions to score. concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively. Returns: (float): the word error rate Examples: >>> predictions = ["this is the prediction", "there is an other sample"] >>> references = ["this is the reference", "there is another one"] >>> wer = datasets.load_metric("wer") >>> wer_score = wer.compute(predictions=predictions, references=references) >>> print(wer_score) 0.5 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class UpperCamelCase_ ( datasets.Metric): """simple docstring""" def UpperCAmelCase_ ( self : List[Any] ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/jitsi/jiwer/"] , reference_urls=[ "https://en.wikipedia.org/wiki/Word_error_rate", ] , ) def UpperCAmelCase_ ( self : Tuple , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : Any=False ) -> Optional[int]: if concatenate_texts: return compute_measures(UpperCAmelCase__ , UpperCAmelCase__ )["wer"] else: __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 0 for prediction, reference in zip(UpperCAmelCase__ , UpperCAmelCase__ ): __SCREAMING_SNAKE_CASE = compute_measures(UpperCAmelCase__ , UpperCAmelCase__ ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
54
1
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 snake_case_ () -> Dict: __lowerCAmelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument( """-m""" , """--pretrained_model_name_or_path""" , type=__A , default=__A , required=__A , help="""Path to pretrained model or model identifier from huggingface.co/models.""" , ) parser.add_argument( """-c""" , """--caption""" , type=__A , default="""robotic cat with wings""" , help="""Text used to generate images.""" , ) parser.add_argument( """-n""" , """--images_num""" , type=__A , default=4 , help="""How much images to generate.""" , ) parser.add_argument( """-s""" , """--seed""" , type=__A , default=4_2 , help="""Seed for random process.""" , ) parser.add_argument( """-ci""" , """--cuda_id""" , type=__A , default=0 , help="""cuda_id.""" , ) __lowerCAmelCase : Union[str, Any] = parser.parse_args() return args def snake_case_ (__A : Dict , __A : int , __A : Optional[int] ) -> List[str]: if not len(__A ) == rows * cols: raise ValueError("""The specified number of rows and columns are not correct.""" ) __lowerCAmelCase ,__lowerCAmelCase : List[str] = imgs[0].size __lowerCAmelCase : Optional[int] = Image.new("""RGB""" , size=(cols * w, rows * h) ) __lowerCAmelCase ,__lowerCAmelCase : Any = grid.size for i, img in enumerate(__A ): grid.paste(__A , box=(i % cols * w, i // cols * h) ) return grid def snake_case_ (__A : List[Any] , __A : Any="robotic cat with wings" , __A : Optional[Any]=7.5 , __A : int=5_0 , __A : Tuple=1 , __A : Dict=4_2 , ) -> int: __lowerCAmelCase : List[str] = torch.Generator(pipeline.device ).manual_seed(__A ) __lowerCAmelCase : Dict = pipeline( __A , guidance_scale=__A , num_inference_steps=__A , generator=__A , num_images_per_prompt=__A , ).images __lowerCAmelCase : Tuple = int(math.sqrt(__A ) ) __lowerCAmelCase : List[str] = image_grid(__A , rows=_rows , cols=num_images_per_prompt // _rows ) return grid, images __UpperCAmelCase = parse_args() # Load models and create wrapper for stable diffusion __UpperCAmelCase = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="""tokenizer""") __UpperCAmelCase = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="""text_encoder""") __UpperCAmelCase = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder="""vae""") __UpperCAmelCase = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="""unet""") __UpperCAmelCase = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) __UpperCAmelCase = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, """best_model.pt""")): __UpperCAmelCase = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, """unet""", unet) else: __UpperCAmelCase = unet.to(torch.device("""cuda""", args.cuda_id)) __UpperCAmelCase = pipeline.to(unet.device) __UpperCAmelCase , __UpperCAmelCase = 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())))) __UpperCAmelCase = 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)))
358
import unittest from transformers import BigBirdConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" def __init__( self : Dict , lowerCAmelCase : Tuple , lowerCAmelCase : str=2 , lowerCAmelCase : Optional[Any]=56 , lowerCAmelCase : Any=True , lowerCAmelCase : Optional[int]=True , lowerCAmelCase : Any=True , lowerCAmelCase : Union[str, Any]=True , lowerCAmelCase : Tuple=99 , lowerCAmelCase : Optional[Any]=32 , lowerCAmelCase : List[str]=2 , lowerCAmelCase : int=2 , lowerCAmelCase : str=7 , lowerCAmelCase : List[Any]="gelu_new" , lowerCAmelCase : Optional[int]=0.1 , lowerCAmelCase : Optional[Any]=0.1 , lowerCAmelCase : Optional[Any]=5_12 , lowerCAmelCase : Dict=16 , lowerCAmelCase : int=2 , lowerCAmelCase : Optional[Any]=0.02 , lowerCAmelCase : Tuple=4 , lowerCAmelCase : Union[str, Any]="block_sparse" , lowerCAmelCase : List[str]=True , lowerCAmelCase : Optional[int]=False , lowerCAmelCase : Optional[Any]=2 , lowerCAmelCase : List[Any]=3 , ) -> Tuple: """simple docstring""" __lowerCAmelCase : Tuple = parent __lowerCAmelCase : Union[str, Any] = batch_size __lowerCAmelCase : List[Any] = seq_length __lowerCAmelCase : int = is_training __lowerCAmelCase : Union[str, Any] = use_attention_mask __lowerCAmelCase : Tuple = use_token_type_ids __lowerCAmelCase : Union[str, Any] = use_labels __lowerCAmelCase : List[str] = vocab_size __lowerCAmelCase : int = hidden_size __lowerCAmelCase : Tuple = num_hidden_layers __lowerCAmelCase : List[str] = num_attention_heads __lowerCAmelCase : Optional[Any] = intermediate_size __lowerCAmelCase : List[Any] = hidden_act __lowerCAmelCase : Optional[Any] = hidden_dropout_prob __lowerCAmelCase : List[str] = attention_probs_dropout_prob __lowerCAmelCase : Optional[int] = max_position_embeddings __lowerCAmelCase : str = type_vocab_size __lowerCAmelCase : List[Any] = type_sequence_label_size __lowerCAmelCase : Tuple = initializer_range __lowerCAmelCase : str = num_choices __lowerCAmelCase : Any = rescale_embeddings __lowerCAmelCase : str = attention_type __lowerCAmelCase : List[Any] = use_bias __lowerCAmelCase : List[str] = block_size __lowerCAmelCase : Union[str, Any] = num_random_blocks def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> str: """simple docstring""" __lowerCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase : Optional[Any] = None if self.use_attention_mask: __lowerCAmelCase : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCAmelCase : Any = None if self.use_token_type_ids: __lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCAmelCase : List[Any] = BigBirdConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCAmelCase , initializer_range=self.initializer_range , attention_type=self.attention_type , block_size=self.block_size , num_random_blocks=self.num_random_blocks , use_bias=self.use_bias , rescale_embeddings=self.rescale_embeddings , ) return config, input_ids, token_type_ids, attention_mask def SCREAMING_SNAKE_CASE ( self : Tuple ) -> int: """simple docstring""" __lowerCAmelCase : List[str] = self.prepare_config_and_inputs() __lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase ,__lowerCAmelCase : Any = config_and_inputs __lowerCAmelCase : Union[str, Any] = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask, } return config, inputs_dict @require_flax class SCREAMING_SNAKE_CASE ( a_ , unittest.TestCase ): """simple docstring""" lowerCamelCase : List[Any] =( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) lowerCamelCase : List[str] =False lowerCamelCase : Union[str, Any] =False def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Any: """simple docstring""" __lowerCAmelCase : str = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: """simple docstring""" super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[str]: """simple docstring""" super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: """simple docstring""" super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: """simple docstring""" super().test_hidden_states_output() @slow def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: """simple docstring""" for model_class_name in self.all_model_classes: __lowerCAmelCase : List[str] = model_class_name.from_pretrained("""google/bigbird-roberta-base""" ) self.assertIsNotNone(lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> List[str]: """simple docstring""" if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> List[Any]: """simple docstring""" __lowerCAmelCase ,__lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __lowerCAmelCase : List[str] = self._prepare_for_class(lowerCAmelCase , lowerCAmelCase ) __lowerCAmelCase : Dict = model_class(lowerCAmelCase ) @jax.jit def model_jitted(lowerCAmelCase : Tuple , lowerCAmelCase : Optional[Any]=None , **lowerCAmelCase : Union[str, Any] ): return model(input_ids=lowerCAmelCase , attention_mask=lowerCAmelCase , **lowerCAmelCase ) with self.subTest("""JIT Enabled""" ): __lowerCAmelCase : str = model_jitted(**lowerCAmelCase ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): __lowerCAmelCase : List[Any] = model_jitted(**lowerCAmelCase ).to_tuple() self.assertEqual(len(lowerCAmelCase ) , len(lowerCAmelCase ) ) for jitted_output, output in zip(lowerCAmelCase , lowerCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : Optional[int] , lowerCAmelCase : Union[str, Any]=1e-5 , lowerCAmelCase : Union[str, Any]="outputs" , lowerCAmelCase : Union[str, Any]=None ) -> Optional[int]: """simple docstring""" if name.startswith("""outputs.attentions""" ): return else: super().check_pt_flax_outputs(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase )
139
0
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=7 , SCREAMING_SNAKE_CASE_=3 , SCREAMING_SNAKE_CASE_=18 , SCREAMING_SNAKE_CASE_=30 , SCREAMING_SNAKE_CASE_=400 , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=True , )-> List[str]: '''simple docstring''' __UpperCamelCase = size if size is not None else {'''height''': 18, '''width''': 18} __UpperCamelCase = parent __UpperCamelCase = batch_size __UpperCamelCase = num_channels __UpperCamelCase = image_size __UpperCamelCase = min_resolution __UpperCamelCase = max_resolution __UpperCamelCase = do_resize __UpperCamelCase = size __UpperCamelCase = apply_ocr def A__ ( self )-> int: '''simple docstring''' return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" _snake_case = LayoutLMvaImageProcessor if is_pytesseract_available() else None def A__ ( self )-> Tuple: '''simple docstring''' __UpperCamelCase = LayoutLMvaImageProcessingTester(self ) @property def A__ ( self )-> str: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def A__ ( self )-> Optional[Any]: '''simple docstring''' __UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''do_resize''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''size''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''apply_ocr''' ) ) def A__ ( self )-> List[Any]: '''simple docstring''' __UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) __UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def A__ ( self )-> Optional[Any]: '''simple docstring''' pass def A__ ( self )-> List[str]: '''simple docstring''' __UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , Image.Image ) # Test not batched input __UpperCamelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) self.assertIsInstance(encoding.words , SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(encoding.boxes , SCREAMING_SNAKE_CASE_ ) # Test batched __UpperCamelCase = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def A__ ( self )-> Dict: '''simple docstring''' __UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , numpify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) # Test not batched input __UpperCamelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __UpperCamelCase = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def A__ ( self )-> Union[str, Any]: '''simple docstring''' __UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , torchify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ) # Test not batched input __UpperCamelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched __UpperCamelCase = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def A__ ( self )-> Optional[int]: '''simple docstring''' __UpperCamelCase = LayoutLMvaImageProcessor() from datasets import load_dataset __UpperCamelCase = load_dataset('''hf-internal-testing/fixtures_docvqa''' , split='''test''' ) __UpperCamelCase = Image.open(ds[0]['''file'''] ).convert('''RGB''' ) __UpperCamelCase = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 __UpperCamelCase = [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']] # noqa: E231 __UpperCamelCase = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(encoding.boxes , SCREAMING_SNAKE_CASE_ ) # with apply_OCR = False __UpperCamelCase = LayoutLMvaImageProcessor(apply_ocr=SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
328
import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": lowercase__ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument( "--txt2img_unclip", default="kakaobrain/karlo-v1-alpha", type=str, required=False, help="The pretrained txt2img unclip.", ) lowercase__ : Any = parser.parse_args() lowercase__ : Union[str, Any] = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) lowercase__ : List[str] = CLIPImageProcessor() lowercase__ : Optional[Any] = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14") lowercase__ : Optional[Any] = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
328
1
"""simple docstring""" from __future__ import annotations def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' print(f"Vertex\tShortest Distance from vertex {src}" ) for i, d in enumerate(_UpperCamelCase ): print(f"{i}\t\t{d}" ) def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' for j in range(_UpperCamelCase ): __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = (graph[j][k] for k in ["src", "dst", "weight"]) if distance[u] != float("inf" ) and distance[u] + w < distance[v]: return True return False def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = [float("inf" )] * vertex_count __lowerCAmelCase = 0.0 for _ in range(vertex_count - 1 ): for j in range(_UpperCamelCase ): __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = (graph[j][k] for k in ["src", "dst", "weight"]) if distance[u] != float("inf" ) and distance[u] + w < distance[v]: __lowerCAmelCase = distance[u] + w __lowerCAmelCase = check_negative_cycle(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if negative_cycle_exists: raise Exception("Negative cycle found" ) return distance if __name__ == "__main__": import doctest doctest.testmod() A : Union[str, Any] = int(input("Enter number of vertices: ").strip()) A : int = int(input("Enter number of edges: ").strip()) A : list[dict[str, int]] = [{} for _ in range(E)] for i in range(E): print("Edge ", i + 1) A , A , A : str = ( int(x) for x in input("Enter source, destination, weight: ").strip().split(" ") ) A : int = {"src": src, "dst": dest, "weight": weight} A : Tuple = int(input("\nEnter shortest path source:").strip()) A : Optional[Any] = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
259
"""simple docstring""" import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' if is_torch_version("<" , "2.0.0" ) or not hasattr(_UpperCamelCase , "_dynamo" ): return False return isinstance(_UpperCamelCase , torch._dynamo.eval_frame.OptimizedModule ) def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase = True ): '''simple docstring''' __lowerCAmelCase = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) __lowerCAmelCase = is_compiled_module(_UpperCamelCase ) if is_compiled: __lowerCAmelCase = model __lowerCAmelCase = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(_UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase = model.module if not keep_fpaa_wrapper: __lowerCAmelCase = getattr(_UpperCamelCase , "forward" ) __lowerCAmelCase = model.__dict__.pop("_original_forward" , _UpperCamelCase ) if original_forward is not None: while hasattr(_UpperCamelCase , "__wrapped__" ): __lowerCAmelCase = forward.__wrapped__ if forward == original_forward: break __lowerCAmelCase = forward if getattr(_UpperCamelCase , "_converted_to_transformer_engine" , _UpperCamelCase ): convert_model(_UpperCamelCase , to_transformer_engine=_UpperCamelCase ) if is_compiled: __lowerCAmelCase = model __lowerCAmelCase = compiled_model return model def _lowerCamelCase ( ): '''simple docstring''' PartialState().wait_for_everyone() def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' if PartialState().distributed_type == DistributedType.TPU: xm.save(_UpperCamelCase , _UpperCamelCase ) elif PartialState().local_process_index == 0: torch.save(_UpperCamelCase , _UpperCamelCase ) @contextmanager def _lowerCamelCase ( **_UpperCamelCase ): '''simple docstring''' for key, value in kwargs.items(): __lowerCAmelCase = str(_UpperCamelCase ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' if not hasattr(_UpperCamelCase , "__qualname__" ) and not hasattr(_UpperCamelCase , "__name__" ): __lowerCAmelCase = getattr(_UpperCamelCase , "__class__" , _UpperCamelCase ) if hasattr(_UpperCamelCase , "__qualname__" ): return obj.__qualname__ if hasattr(_UpperCamelCase , "__name__" ): return obj.__name__ return str(_UpperCamelCase ) def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' for key, value in source.items(): if isinstance(_UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase = destination.setdefault(_UpperCamelCase , {} ) merge_dicts(_UpperCamelCase , _UpperCamelCase ) else: __lowerCAmelCase = value return destination def _lowerCamelCase ( _UpperCamelCase = None ): '''simple docstring''' if port is None: __lowerCAmelCase = 2_9500 with socket.socket(socket.AF_INET , socket.SOCK_STREAM ) as s: return s.connect_ex(("localhost", port) ) == 0
259
1
'''simple docstring''' import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class snake_case : """simple docstring""" def __init__( self : int , __A : Optional[Any] ): if isinstance(__A , __A ): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden __UpperCamelCase = deepcopy(__A ) elif os.path.exists(__A ): with io.open(__A , 'r' , encoding='utf-8' ) as f: __UpperCamelCase = json.load(__A ) else: try: __UpperCamelCase = baseaa.urlsafe_baadecode(__A ).decode('utf-8' ) __UpperCamelCase = json.loads(__A ) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( f'''Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}''' ) __UpperCamelCase = config self.set_stage_and_offload() def _lowerCamelCase ( self : str ): # zero stage - this is done as early as possible, before model is created, to allow # ``is_deepspeed_zero3_enabled`` query and getting to the early deepspeed config object # during ``zero.Init()`` which needs to know the dtype, and some other hparams. __UpperCamelCase = self.get_value('zero_optimization.stage' , -1 ) # offload __UpperCamelCase = False if self.is_zeroa() or self.is_zeroa(): __UpperCamelCase = set(['cpu', 'nvme'] ) __UpperCamelCase = set( [ self.get_value('zero_optimization.offload_optimizer.device' ), self.get_value('zero_optimization.offload_param.device' ), ] ) if len(offload_devices & offload_devices_valid ) > 0: __UpperCamelCase = True def _lowerCamelCase ( self : Union[str, Any] , __A : List[str] ): __UpperCamelCase = self.config # find the config node of interest if it exists __UpperCamelCase = ds_key_long.split('.' ) __UpperCamelCase = nodes.pop() for node in nodes: __UpperCamelCase = config.get(__A ) if config is None: return None, ds_key return config, ds_key def _lowerCamelCase ( self : int , __A : Any , __A : Optional[int]=None ): __UpperCamelCase , __UpperCamelCase = self.find_config_node(__A ) if config is None: return default return config.get(__A , __A ) def _lowerCamelCase ( self : str , __A : Optional[int] , __A : Optional[int]=False ): __UpperCamelCase = self.config # find the config node of interest if it exists __UpperCamelCase = ds_key_long.split('.' ) for node in nodes: __UpperCamelCase = config __UpperCamelCase = config.get(__A ) if config is None: if must_exist: raise ValueError(f'''Can\'t find {ds_key_long} entry in the config: {self.config}''' ) else: return # if found remove it if parent_config is not None: parent_config.pop(__A ) def _lowerCamelCase ( self : Dict , __A : Tuple ): __UpperCamelCase = self.get_value(__A ) return False if value is None else bool(__A ) def _lowerCamelCase ( self : List[Any] , __A : Union[str, Any] ): __UpperCamelCase = self.get_value(__A ) return False if value is None else not bool(__A ) def _lowerCamelCase ( self : List[Any] ): return self._stage == 2 def _lowerCamelCase ( self : int ): return self._stage == 3 def _lowerCamelCase ( self : Any ): return self._offload class snake_case : """simple docstring""" def __init__( self : int , __A : List[Any] ): __UpperCamelCase = engine def _lowerCamelCase ( self : Tuple , __A : Optional[int] , **__A : List[Any] ): # runs backpropagation and handles mixed precision self.engine.backward(__A , **__A ) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class snake_case ( __lowerCamelCase ): """simple docstring""" def __init__( self : Union[str, Any] , __A : List[str] ): super().__init__(__A , device_placement=__A , scaler=__A ) __UpperCamelCase = hasattr(self.optimizer , 'overflow' ) def _lowerCamelCase ( self : str , __A : Dict=None ): pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def _lowerCamelCase ( self : Dict ): pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def _lowerCamelCase ( self : Optional[int] ): if self.__has_overflow__: return self.optimizer.overflow return False class snake_case ( __lowerCamelCase ): """simple docstring""" def __init__( self : Any , __A : int , __A : Dict ): super().__init__(__A , __A ) def _lowerCamelCase ( self : Dict ): pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class snake_case : """simple docstring""" def __init__( self : List[str] , __A : Tuple , __A : Optional[int]=0.001 , __A : str=0 , **__A : List[str] ): __UpperCamelCase = params __UpperCamelCase = lr __UpperCamelCase = weight_decay __UpperCamelCase = kwargs class snake_case : """simple docstring""" def __init__( self : str , __A : str , __A : List[Any]=None , __A : str=0 , **__A : Optional[int] ): __UpperCamelCase = optimizer __UpperCamelCase = total_num_steps __UpperCamelCase = warmup_num_steps __UpperCamelCase = kwargs
53
'''simple docstring''' import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def lowercase__ ( __lowercase : List[str] , __lowercase : Union[str, Any]=False ) -> Tuple: """simple docstring""" try: __UpperCamelCase = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __UpperCamelCase = default else: # KEY is set, convert it to True or False. try: __UpperCamelCase = strtobool(__lowercase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F'''If set, {key} must be yes or no.''' ) return _value a__ : str =parse_flag_from_env('''RUN_SLOW''', default=False) a__ : Union[str, Any] =parse_flag_from_env('''RUN_REMOTE''', default=False) a__ : List[str] =parse_flag_from_env('''RUN_LOCAL''', default=True) a__ : Optional[int] =parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression a__ : Any =pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') a__ : Optional[int] =pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') a__ : List[str] =pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio a__ : Any =pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam a__ : Tuple =pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility a__ : Union[str, Any] =pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows a__ : int =pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def lowercase__ ( __lowercase : Optional[Any] ) -> Optional[int]: """simple docstring""" try: import faiss # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires faiss' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Union[str, Any] ) -> Any: """simple docstring""" try: import regex # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires regex' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Tuple ) -> List[Any]: """simple docstring""" try: import elasticsearch # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires elasticsearch' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Union[str, Any] ) -> Tuple: """simple docstring""" try: import sqlalchemy # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires sqlalchemy' )(__lowercase ) return test_case def lowercase__ ( __lowercase : List[str] ) -> List[str]: """simple docstring""" if not config.TORCH_AVAILABLE: __UpperCamelCase = unittest.skip('test requires PyTorch' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Optional[Any] ) -> List[str]: """simple docstring""" if not config.TF_AVAILABLE: __UpperCamelCase = unittest.skip('test requires TensorFlow' )(__lowercase ) return test_case def lowercase__ ( __lowercase : int ) -> Union[str, Any]: """simple docstring""" if not config.JAX_AVAILABLE: __UpperCamelCase = unittest.skip('test requires JAX' )(__lowercase ) return test_case def lowercase__ ( __lowercase : str ) -> Optional[Any]: """simple docstring""" if not config.PIL_AVAILABLE: __UpperCamelCase = unittest.skip('test requires Pillow' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Dict ) -> Any: """simple docstring""" try: import transformers # noqa F401 except ImportError: return unittest.skip('test requires transformers' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : int ) -> int: """simple docstring""" try: import tiktoken # noqa F401 except ImportError: return unittest.skip('test requires tiktoken' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : str ) -> int: """simple docstring""" try: import spacy # noqa F401 except ImportError: return unittest.skip('test requires spacy' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : str ) -> Any: """simple docstring""" def _require_spacy_model(__lowercase : Any ): try: import spacy # noqa F401 spacy.load(__lowercase ) except ImportError: return unittest.skip('test requires spacy' )(__lowercase ) except OSError: return unittest.skip('test requires spacy model \'{}\''.format(__lowercase ) )(__lowercase ) else: return test_case return _require_spacy_model def lowercase__ ( __lowercase : Union[str, Any] ) -> str: """simple docstring""" try: import pyspark # noqa F401 except ImportError: return unittest.skip('test requires pyspark' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : Optional[int] ) -> Optional[Any]: """simple docstring""" try: import joblibspark # noqa F401 except ImportError: return unittest.skip('test requires joblibspark' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : List[Any] ) -> List[str]: """simple docstring""" if not _run_slow_tests or _run_slow_tests == 0: __UpperCamelCase = unittest.skip('test is slow' )(__lowercase ) return test_case def lowercase__ ( __lowercase : List[Any] ) -> List[str]: """simple docstring""" if not _run_local_tests or _run_local_tests == 0: __UpperCamelCase = unittest.skip('test is local' )(__lowercase ) return test_case def lowercase__ ( __lowercase : str ) -> List[str]: """simple docstring""" if not _run_packaged_tests or _run_packaged_tests == 0: __UpperCamelCase = unittest.skip('test is packaged' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Optional[int] ) -> Any: """simple docstring""" if not _run_remote_tests or _run_remote_tests == 0: __UpperCamelCase = unittest.skip('test requires remote' )(__lowercase ) return test_case def lowercase__ ( *__lowercase : Optional[Any] ) -> Tuple: """simple docstring""" def decorate(cls : int ): for name, fn in cls.__dict__.items(): if callable(__lowercase ) and name.startswith('test' ): for decorator in decorators: __UpperCamelCase = decorator(__lowercase ) setattr(cls , __lowercase , __lowercase ) return cls return decorate class snake_case ( __lowerCamelCase ): """simple docstring""" pass class snake_case ( __lowerCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Any =0 SCREAMING_SNAKE_CASE_ : List[Any] =1 SCREAMING_SNAKE_CASE_ : Union[str, Any] =2 @contextmanager def lowercase__ ( __lowercase : List[str]=OfflineSimulationMode.CONNECTION_FAILS , __lowercase : Dict=1e-16 ) -> List[Any]: """simple docstring""" __UpperCamelCase = requests.Session().request def timeout_request(__lowercase : List[Any] , __lowercase : Tuple , __lowercase : List[Any] , **__lowercase : List[str] ): # Change the url to an invalid url so that the connection hangs __UpperCamelCase = 'https://10.255.255.1' if kwargs.get('timeout' ) is None: raise RequestWouldHangIndefinitelyError( F'''Tried a call to {url} in offline mode with no timeout set. Please set a timeout.''' ) __UpperCamelCase = timeout try: return online_request(__lowercase , __lowercase , **__lowercase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier __UpperCamelCase = url __UpperCamelCase = e.args[0] __UpperCamelCase = (max_retry_error.args[0].replace('10.255.255.1' , F'''OfflineMock[{url}]''' ),) __UpperCamelCase = (max_retry_error,) raise def raise_connection_error(__lowercase : int , __lowercase : List[str] , **__lowercase : Union[str, Any] ): raise requests.ConnectionError('Offline mode is enabled.' , request=__lowercase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('requests.Session.send' , __lowercase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('requests.Session.request' , __lowercase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('datasets.config.HF_DATASETS_OFFLINE' , __lowercase ): yield else: raise ValueError('Please use a value from the OfflineSimulationMode enum.' ) @contextmanager def lowercase__ ( *__lowercase : Any , **__lowercase : Dict ) -> Dict: """simple docstring""" __UpperCamelCase = str(Path().resolve() ) with tempfile.TemporaryDirectory(*__lowercase , **__lowercase ) as tmp_dir: try: os.chdir(__lowercase ) yield finally: os.chdir(__lowercase ) @contextmanager def lowercase__ ( ) -> Optional[Any]: """simple docstring""" import gc gc.collect() __UpperCamelCase = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def lowercase__ ( ) -> Optional[Any]: """simple docstring""" import gc gc.collect() __UpperCamelCase = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def lowercase__ ( __lowercase : List[str] , __lowercase : int ) -> Union[str, Any]: """simple docstring""" return deepcopy(__lowercase ).integers(0 , 100 , 10 ).tolist() == deepcopy(__lowercase ).integers(0 , 100 , 10 ).tolist() def lowercase__ ( __lowercase : str ) -> List[str]: """simple docstring""" import decorator from requests.exceptions import HTTPError def _wrapper(__lowercase : List[Any] , *__lowercase : Tuple , **__lowercase : Union[str, Any] ): try: return func(*__lowercase , **__lowercase ) except HTTPError as err: if str(__lowercase ).startswith('500' ) or str(__lowercase ).startswith('502' ): pytest.xfail(str(__lowercase ) ) raise err return decorator.decorator(_wrapper , __lowercase ) class snake_case : """simple docstring""" def __init__( self : int , __A : Any , __A : str , __A : List[Any] ): __UpperCamelCase = returncode __UpperCamelCase = stdout __UpperCamelCase = stderr async def lowercase__ ( __lowercase : Any , __lowercase : Optional[int] ) -> str: """simple docstring""" while True: __UpperCamelCase = await stream.readline() if line: callback(__lowercase ) else: break async def lowercase__ ( __lowercase : Optional[int] , __lowercase : Union[str, Any]=None , __lowercase : Any=None , __lowercase : Optional[Any]=None , __lowercase : int=False , __lowercase : List[Any]=False ) -> _RunOutput: """simple docstring""" if echo: print('\nRunning: ' , ' '.join(__lowercase ) ) __UpperCamelCase = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=__lowercase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=__lowercase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __UpperCamelCase = [] __UpperCamelCase = [] def tee(__lowercase : Optional[Any] , __lowercase : Dict , __lowercase : List[str] , __lowercase : Tuple="" ): __UpperCamelCase = line.decode('utf-8' ).rstrip() sink.append(__lowercase ) if not quiet: print(__lowercase , __lowercase , file=__lowercase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda __lowercase : tee(__lowercase , __lowercase , sys.stdout , label='stdout:' ) ), _read_stream(p.stderr , lambda __lowercase : tee(__lowercase , __lowercase , sys.stderr , label='stderr:' ) ), ] , timeout=__lowercase , ) return _RunOutput(await p.wait() , __lowercase , __lowercase ) def lowercase__ ( __lowercase : Dict , __lowercase : Any=None , __lowercase : int=None , __lowercase : int=180 , __lowercase : int=False , __lowercase : str=True ) -> _RunOutput: """simple docstring""" __UpperCamelCase = asyncio.get_event_loop() __UpperCamelCase = loop.run_until_complete( _stream_subprocess(__lowercase , env=__lowercase , stdin=__lowercase , timeout=__lowercase , quiet=__lowercase , echo=__lowercase ) ) __UpperCamelCase = ' '.join(__lowercase ) if result.returncode > 0: __UpperCamelCase = '\n'.join(result.stderr ) raise RuntimeError( F'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' F'''The combined stderr from workers follows:\n{stderr}''' ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(F'''\'{cmd_str}\' produced no output.''' ) return result def lowercase__ ( ) -> List[str]: """simple docstring""" __UpperCamelCase = os.environ.get('PYTEST_XDIST_WORKER' , 'gw0' ) __UpperCamelCase = re.sub(R'^gw' , '' , __lowercase , 0 , re.M ) return int(__lowercase ) def lowercase__ ( ) -> List[Any]: """simple docstring""" __UpperCamelCase = 29500 __UpperCamelCase = pytest_xdist_worker_id() return port + uniq_delta
53
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _lowerCamelCase : Any = logging.get_logger(__name__) _lowerCamelCase : Union[str, Any] = '''▁''' _lowerCamelCase : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} _lowerCamelCase : Optional[Any] = { '''vocab_file''': { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model''' ), } } _lowerCamelCase : List[Any] = { '''xlm-roberta-base''': 5_12, '''xlm-roberta-large''': 5_12, '''xlm-roberta-large-finetuned-conll02-dutch''': 5_12, '''xlm-roberta-large-finetuned-conll02-spanish''': 5_12, '''xlm-roberta-large-finetuned-conll03-english''': 5_12, '''xlm-roberta-large-finetuned-conll03-german''': 5_12, } class lowercase ( a ): lowercase__ : Dict = VOCAB_FILES_NAMES lowercase__ : List[str] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : Union[str, Any] = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : Dict="<s>" , _UpperCamelCase : int="</s>" , _UpperCamelCase : Union[str, Any]="</s>" , _UpperCamelCase : Union[str, Any]="<s>" , _UpperCamelCase : Tuple="<unk>" , _UpperCamelCase : Dict="<pad>" , _UpperCamelCase : Tuple="<mask>" , _UpperCamelCase : Optional[Dict[str, Any]] = None , **_UpperCamelCase : int , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = AddedToken(_UpperCamelCase , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ) else mask_token SCREAMING_SNAKE_CASE = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_UpperCamelCase , eos_token=_UpperCamelCase , unk_token=_UpperCamelCase , sep_token=_UpperCamelCase , cls_token=_UpperCamelCase , pad_token=_UpperCamelCase , mask_token=_UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **_UpperCamelCase , ) 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' # Mimic fairseq token-to-id alignment for the first 4 token SCREAMING_SNAKE_CASE = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE = 1 SCREAMING_SNAKE_CASE = len(self.sp_model ) + self.fairseq_offset SCREAMING_SNAKE_CASE = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self : Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = self.__dict__.copy() SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = self.sp_model.serialized_model_proto() return state def __setstate__( self : List[Any] , _UpperCamelCase : List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE = d # 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.LoadFromSerializedProto(self.sp_model_proto ) def __snake_case( self : int , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.cls_token_id] SCREAMING_SNAKE_CASE = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __snake_case( self : Any , _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 [1] + ([0] * len(_UpperCamelCase )) + [1] return [1] + ([0] * len(_UpperCamelCase )) + [1, 1] + ([0] * len(_UpperCamelCase )) + [1] def __snake_case( self : Optional[Any] , _UpperCamelCase : List[int] , _UpperCamelCase : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = [self.sep_token_id] SCREAMING_SNAKE_CASE = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __snake_case( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + 1 # Add the <mask> token def __snake_case( self : Tuple ) -> Tuple: '''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 : List[str] , _UpperCamelCase : str ) -> List[str]: '''simple docstring''' return self.sp_model.encode(_UpperCamelCase , out_type=_UpperCamelCase ) def __snake_case( self : Any , _UpperCamelCase : Optional[int] ) -> Optional[Any]: '''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 : int , _UpperCamelCase : str ) -> List[Any]: '''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 : Dict , _UpperCamelCase : int ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = "".join(_UpperCamelCase ).replace(_UpperCamelCase , " " ).strip() return out_string def __snake_case( self : Union[str, 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,)
206
def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : str = " " ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 for index, char in enumerate(UpperCAmelCase__ ): if char == separator: split_words.append(string[last_index:index] ) SCREAMING_SNAKE_CASE = index + 1 elif index + 1 == len(UpperCAmelCase__ ): split_words.append(string[last_index : index + 1] ) return split_words if __name__ == "__main__": from doctest import testmod testmod()
206
1
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def snake_case_ ( ) -> Dict: lowercase__: Tuple = { """repo_name""": ["""test_repo1""", """test_repo2""", """test_repo3"""], """path""": ["""test_1.py""", """test_2.py""", """unit_test.py"""], """content""": ["""a """ * 20, """a """ * 30, """b """ * 7], } lowercase__: int = Dataset.from_dict(A__ ) return dataset class __a ( _SCREAMING_SNAKE_CASE ): def SCREAMING_SNAKE_CASE__ ( self ) -> Dict: '''simple docstring''' lowercase__: List[Any] = get_dataset() lowercase__: str = make_duplicate_clusters(lowerCAmelCase__ , 0.8_5 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def SCREAMING_SNAKE_CASE__ ( self ) -> List[str]: '''simple docstring''' lowercase__: Any = get_dataset() lowercase__: Optional[int] = deduplicate_dataset(lowerCAmelCase__ ) self.assertEqual(len(lowerCAmelCase__ ) , 2 ) print(lowerCAmelCase__ ) self.assertEqual(duplicate_clusters[0][0]['copies'] , 2 ) self.assertEqual(duplicate_clusters[0][0]['is_extreme'] , lowerCAmelCase__ )
196
'''simple docstring''' import json import os import unittest from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES, BioGptTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class __snake_case ( _SCREAMING_SNAKE_CASE ,unittest.TestCase): """simple docstring""" lowercase = BioGptTokenizer lowercase = False def __lowercase ( self : List[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCAmelCase_ : Optional[Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """w</w>""", """r</w>""", """t</w>""", """lo""", """low""", """er</w>""", """low</w>""", """lowest</w>""", """newer</w>""", """wider</w>""", """<unk>""", ] lowerCAmelCase_ : int = dict(zip(lowerCamelCase , range(len(lowerCamelCase ) ) ) ) lowerCAmelCase_ : int = ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""] lowerCAmelCase_ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCAmelCase_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" ) as fp: fp.write(json.dumps(lowerCamelCase ) ) with open(self.merges_file , """w""" ) as fp: fp.write("""\n""".join(lowerCamelCase ) ) def __lowercase ( self : Union[str, Any] , lowerCamelCase : Any ) -> List[str]: lowerCAmelCase_ : Dict = """lower newer""" lowerCAmelCase_ : str = """lower newer""" return input_text, output_text def __lowercase ( self : Optional[int] ) -> str: lowerCAmelCase_ : Any = BioGptTokenizer(self.vocab_file , self.merges_file ) lowerCAmelCase_ : int = """lower""" lowerCAmelCase_ : str = ["""low""", """er</w>"""] lowerCAmelCase_ : Any = tokenizer.tokenize(lowerCamelCase ) self.assertListEqual(lowerCamelCase , lowerCamelCase ) lowerCAmelCase_ : Dict = tokens + ["""<unk>"""] lowerCAmelCase_ : Optional[Any] = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase ) , lowerCamelCase ) @slow def __lowercase ( self : str ) -> Optional[Any]: lowerCAmelCase_ : Dict = BioGptTokenizer.from_pretrained("""microsoft/biogpt""" ) lowerCAmelCase_ : int = tokenizer.encode("""sequence builders""" , add_special_tokens=lowerCamelCase ) lowerCAmelCase_ : Optional[Any] = tokenizer.encode("""multi-sequence build""" , add_special_tokens=lowerCamelCase ) lowerCAmelCase_ : str = tokenizer.build_inputs_with_special_tokens(lowerCamelCase ) lowerCAmelCase_ : Optional[int] = tokenizer.build_inputs_with_special_tokens(lowerCamelCase , lowerCamelCase ) self.assertTrue(encoded_sentence == [2] + text ) self.assertTrue(encoded_pair == [2] + text + [2] + text_a )
120
0
'''simple docstring''' def _lowerCamelCase ( lowercase : int = 6008_5147_5143 ) -> int: try: _a = int(lowercase ) except (TypeError, ValueError): raise TypeError("Parameter n must be int or castable to int." ) if n <= 0: raise ValueError("Parameter n must be greater than or equal to one." ) _a = 2 _a = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 _a = i while n % i == 0: _a = n // i i += 1 return int(lowercase ) if __name__ == "__main__": print(f"""{solution() = }""")
346
'''simple docstring''' from typing import Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING lowerCAmelCase_ : Union[str, Any] = logging.get_logger(__name__) @add_end_docstrings(lowerCamelCase_ ) class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : List[Any] , *__a : Optional[int] , **__a : List[str] ): super().__init__(*__a , **__a ) self.check_model_type(__a ) def UpperCamelCase__ ( self : Optional[Any] , __a : Dict=None , __a : int=None , __a : Optional[Any]=None , **__a : List[Any] ): _a , _a = {}, {} if padding is not None: _a = padding if truncation is not None: _a = truncation if top_k is not None: _a = top_k return preprocess_params, {}, postprocess_params def __call__( self : Union[str, Any] , __a : Union["Image.Image", str] , __a : str = None , **__a : Any ): if isinstance(__a , (Image.Image, str) ) and isinstance(__a , __a ): _a = {"image": image, "question": question} else: _a = image _a = super().__call__(__a , **__a ) return results def UpperCamelCase__ ( self : Tuple , __a : Tuple , __a : Optional[Any]=False , __a : List[Any]=False ): _a = load_image(inputs["image"] ) _a = self.tokenizer( inputs["question"] , return_tensors=self.framework , padding=__a , truncation=__a ) _a = self.image_processor(images=__a , return_tensors=self.framework ) model_inputs.update(__a ) return model_inputs def UpperCamelCase__ ( self : List[Any] , __a : List[str] ): _a = self.model(**__a ) return model_outputs def UpperCamelCase__ ( self : int , __a : Optional[int] , __a : Dict=5 ): if top_k > self.model.config.num_labels: _a = self.model.config.num_labels if self.framework == "pt": _a = model_outputs.logits.sigmoid()[0] _a , _a = probs.topk(__a ) else: raise ValueError(f'Unsupported framework: {self.framework}' ) _a = scores.tolist() _a = ids.tolist() return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(__a , __a )]
346
1
def a__ ( UpperCAmelCase : int , UpperCAmelCase : int ) -> str: if not isinstance(UpperCAmelCase , UpperCAmelCase ): raise ValueError('''iterations must be defined as integers''' ) if not isinstance(UpperCAmelCase , UpperCAmelCase ) or not number >= 1: raise ValueError( '''starting number must be and integer and be more than 0''' ) if not iterations >= 1: raise ValueError('''Iterations must be done more than 0 times to play FizzBuzz''' ) UpperCAmelCase : int = '''''' while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(UpperCAmelCase ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
336
def a__ ( UpperCAmelCase : List[Any] , UpperCAmelCase : Optional[int] ) -> Optional[Any]: UpperCAmelCase : List[str] = 0 UpperCAmelCase : List[Any] = len(UpperCAmelCase ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None UpperCAmelCase : Optional[int] = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(UpperCAmelCase ): return None UpperCAmelCase : Optional[Any] = sorted_collection[point] if current_item == item: return point else: if point < left: UpperCAmelCase : Any = left UpperCAmelCase : List[str] = point elif point > right: UpperCAmelCase : Any = right UpperCAmelCase : List[str] = point else: if item < current_item: UpperCAmelCase : Optional[int] = point - 1 else: UpperCAmelCase : str = point + 1 return None def a__ ( UpperCAmelCase : Optional[Any] , UpperCAmelCase : int , UpperCAmelCase : str , UpperCAmelCase : Union[str, Any] ) -> Dict: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None UpperCAmelCase : List[str] = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(UpperCAmelCase ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) elif point > right: return interpolation_search_by_recursion(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , point - 1 ) else: return interpolation_search_by_recursion( UpperCAmelCase , UpperCAmelCase , point + 1 , UpperCAmelCase ) def a__ ( UpperCAmelCase : Union[str, Any] ) -> int: if collection != sorted(UpperCAmelCase ): raise ValueError('''Collection must be ascending sorted''' ) return True if __name__ == "__main__": import sys _lowerCamelCase : Optional[int] = 0 if debug == 1: _lowerCamelCase : Dict = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit("Sequence must be ascending sorted to apply interpolation search") _lowerCamelCase : List[Any] = 6_7 _lowerCamelCase : Optional[Any] = interpolation_search(collection, target) if result is not None: print(f"""{target} found at positions: {result}""") else: print("Not found")
336
1
from pathlib import Path import numpy as np from PIL import Image def lowercase ( A_ )-> str: '''simple docstring''' a : List[Any] = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.2_9_8_9 * r + 0.5_8_7_0 * g + 0.1_1_4_0 * b def lowercase ( A_ )-> Dict: '''simple docstring''' return (gray > 127) & (gray <= 255) def lowercase ( A_ , A_ )-> Tuple: '''simple docstring''' a : Union[str, Any] = np.zeros_like(lowerCamelCase_ ) a : Union[str, Any] = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image a : List[str] = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): a : Optional[int] = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() a : Dict = int(summation > 0 ) return output if __name__ == "__main__": # read original image __lowercase = Path(__file__).resolve().parent / 'image_data' / 'lena.jpg' __lowercase = np.array(Image.open(lena_path)) # kernel to be applied __lowercase = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) __lowercase = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image __lowercase = Image.fromarray(output).convert("""RGB""") pil_img.save("""result_dilation.png""")
358
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _A ( unittest.TestCase ): """simple docstring""" def __init__( self : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Tuple=7 , __UpperCAmelCase : List[str]=3 , __UpperCAmelCase : int=18 , __UpperCAmelCase : int=30 , __UpperCAmelCase : Optional[int]=400 , __UpperCAmelCase : int=True , __UpperCAmelCase : Optional[Any]=None , __UpperCAmelCase : Union[str, Any]=True , ): a : Optional[int] = size if size is not None else {"height": 18, "width": 18} a : Any = parent a : int = batch_size a : str = num_channels a : Dict = image_size a : Dict = min_resolution a : Optional[int] = max_resolution a : Optional[int] = do_resize a : Any = size a : Dict = apply_ocr def __snake_case ( self : Optional[int]): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class _A ( _a ,unittest.TestCase ): """simple docstring""" UpperCAmelCase : List[str] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __snake_case ( self : List[Any]): a : Optional[int] = LayoutLMvaImageProcessingTester(self) @property def __snake_case ( self : Optional[int]): return self.image_processor_tester.prepare_image_processor_dict() def __snake_case ( self : List[Any]): a : Union[str, Any] = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(__UpperCAmelCase , "do_resize")) self.assertTrue(hasattr(__UpperCAmelCase , "size")) self.assertTrue(hasattr(__UpperCAmelCase , "apply_ocr")) def __snake_case ( self : str): a : Dict = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {"height": 18, "width": 18}) a : List[str] = 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]): pass def __snake_case ( self : List[str]): # Initialize image_processing a : Any = self.image_processing_class(**self.image_processor_dict) # create random PIL images a : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , Image.Image) # Test not batched input a : str = image_processing(image_inputs[0] , return_tensors="pt") self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["height"], self.image_processor_tester.size["width"], ) , ) self.assertIsInstance(encoding.words , __UpperCAmelCase) self.assertIsInstance(encoding.boxes , __UpperCAmelCase) # Test batched a : Dict = 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 : Union[str, Any]): # Initialize image_processing a : List[str] = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , numpify=__UpperCAmelCase) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , np.ndarray) # Test not batched input a : Dict = 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 a : List[str] = 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 : List[str]): # Initialize image_processing a : str = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors a : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=__UpperCAmelCase , torchify=__UpperCAmelCase) for image in image_inputs: self.assertIsInstance(__UpperCAmelCase , torch.Tensor) # Test not batched input a : Optional[int] = 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 a : List[str] = 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 : List[str]): # with apply_OCR = True a : List[Any] = LayoutLMvaImageProcessor() from datasets import load_dataset a : List[str] = load_dataset("hf-internal-testing/fixtures_docvqa" , split="test") a : int = Image.open(ds[0]["file"]).convert("RGB") a : Dict = image_processing(__UpperCAmelCase , return_tensors="pt") self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224)) self.assertEqual(len(encoding.words) , len(encoding.boxes)) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 a : Tuple = [["11:14", "to", "11:39", "a.m", "11:39", "to", "11:44", "a.m.", "11:44", "a.m.", "to", "12:25", "p.m.", "12:25", "to", "12:58", "p.m.", "12:58", "to", "4:00", "p.m.", "2:00", "to", "5:00", "p.m.", "Coffee", "Break", "Coffee", "will", "be", "served", "for", "men", "and", "women", "in", "the", "lobby", "adjacent", "to", "exhibit", "area.", "Please", "move", "into", "exhibit", "area.", "(Exhibits", "Open)", "TRRF", "GENERAL", "SESSION", "(PART", "|)", "Presiding:", "Lee", "A.", "Waller", "TRRF", "Vice", "President", "“Introductory", "Remarks”", "Lee", "A.", "Waller,", "TRRF", "Vice", "Presi-", "dent", "Individual", "Interviews", "with", "TRRF", "Public", "Board", "Members", "and", "Sci-", "entific", "Advisory", "Council", "Mem-", "bers", "Conducted", "by", "TRRF", "Treasurer", "Philip", "G.", "Kuehn", "to", "get", "answers", "which", "the", "public", "refrigerated", "warehousing", "industry", "is", "looking", "for.", "Plus", "questions", "from", "the", "floor.", "Dr.", "Emil", "M.", "Mrak,", "University", "of", "Cal-", "ifornia,", "Chairman,", "TRRF", "Board;", "Sam", "R.", "Cecil,", "University", "of", "Georgia", "College", "of", "Agriculture;", "Dr.", "Stanley", "Charm,", "Tufts", "University", "School", "of", "Medicine;", "Dr.", "Robert", "H.", "Cotton,", "ITT", "Continental", "Baking", "Company;", "Dr.", "Owen", "Fennema,", "University", "of", "Wis-", "consin;", "Dr.", "Robert", "E.", "Hardenburg,", "USDA.", "Questions", "and", "Answers", "Exhibits", "Open", "Capt.", "Jack", "Stoney", "Room", "TRRF", "Scientific", "Advisory", "Council", "Meeting", "Ballroom", "Foyer"]] # noqa: E231 a : str = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , __UpperCAmelCase) self.assertListEqual(encoding.boxes , __UpperCAmelCase) # with apply_OCR = False a : Optional[int] = LayoutLMvaImageProcessor(apply_ocr=__UpperCAmelCase) a : Dict = image_processing(__UpperCAmelCase , return_tensors="pt") self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224))
226
0
"""simple docstring""" import argparse import os import transformers from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS from .utils import logging logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = {name: getattr(transformers, name + '''Fast''') for name in SLOW_TO_FAST_CONVERTERS} def snake_case_ ( A_ : Union[str, Any], A_ : Dict, A_ : Any, A_ : Optional[int] ): '''simple docstring''' if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES: raise ValueError(F'''Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys() )}.''' ) if tokenizer_name is None: _lowerCamelCase : List[str] = TOKENIZER_CLASSES else: _lowerCamelCase : List[str] = {tokenizer_name: getattr(A_, tokenizer_name + '''Fast''' )} logger.info(F'''Loading tokenizer classes: {tokenizer_names}''' ) for tokenizer_name in tokenizer_names: _lowerCamelCase : Optional[int] = TOKENIZER_CLASSES[tokenizer_name] _lowerCamelCase : List[str] = True if checkpoint_name is None: _lowerCamelCase : int = list(tokenizer_class.max_model_input_sizes.keys() ) else: _lowerCamelCase : List[str] = [checkpoint_name] logger.info(F'''For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}''' ) for checkpoint in checkpoint_names: logger.info(F'''Loading {tokenizer_class.__class__.__name__} {checkpoint}''' ) # Load tokenizer _lowerCamelCase : int = tokenizer_class.from_pretrained(A_, force_download=A_ ) # Save fast tokenizer logger.info(F'''Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}''' ) # For organization names we create sub-directories if "/" in checkpoint: _lowerCamelCase , _lowerCamelCase : Union[str, Any] = checkpoint.split('''/''' ) _lowerCamelCase : Dict = os.path.join(A_, A_ ) elif add_prefix: _lowerCamelCase : List[Any] = checkpoint _lowerCamelCase : str = dump_path else: _lowerCamelCase : str = None _lowerCamelCase : List[str] = dump_path logger.info(F'''=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}''' ) if checkpoint in list(tokenizer.pretrained_vocab_files_map.values() )[0]: _lowerCamelCase : int = list(tokenizer.pretrained_vocab_files_map.values() )[0][checkpoint] _lowerCamelCase : Union[str, Any] = file_path.split(A_ )[-1][0] if next_char == "/": _lowerCamelCase : Any = os.path.join(A_, A_ ) _lowerCamelCase : Union[str, Any] = None logger.info(F'''=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}''' ) _lowerCamelCase : Union[str, Any] = tokenizer.save_pretrained( A_, legacy_format=A_, filename_prefix=A_ ) logger.info(F'''=> File names {file_names}''' ) for file_name in file_names: if not file_name.endswith('''tokenizer.json''' ): os.remove(A_ ) logger.info(F'''=> removing {file_name}''' ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--dump_path''', default=None, type=str, required=True, help='''Path to output generated fast tokenizer files.''' ) parser.add_argument( '''--tokenizer_name''', default=None, type=str, help=( F"""Optional tokenizer type selected in the list of {list(TOKENIZER_CLASSES.keys())}. If not given, will """ '''download and convert all the checkpoints from AWS.''' ), ) parser.add_argument( '''--checkpoint_name''', default=None, type=str, help='''Optional checkpoint name. If not given, will download and convert the canonical checkpoints from AWS.''', ) parser.add_argument( '''--force_download''', action='''store_true''', help='''Re-download checkpoints.''', ) lowerCAmelCase__ = parser.parse_args() convert_slow_checkpoint_to_fast(args.tokenizer_name, args.checkpoint_name, args.dump_path, args.force_download)
72
import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class a : """simple docstring""" def __init__( self , lowerCAmelCase_ , lowerCAmelCase_=13 , lowerCAmelCase_=30 , lowerCAmelCase_=2 , lowerCAmelCase_=3 , lowerCAmelCase_=True , lowerCAmelCase_=True , lowerCAmelCase_=32 , lowerCAmelCase_=5 , lowerCAmelCase_=4 , lowerCAmelCase_=37 , lowerCAmelCase_="gelu" , lowerCAmelCase_=0.1 , lowerCAmelCase_=0.1 , lowerCAmelCase_=10 , lowerCAmelCase_=0.02 , lowerCAmelCase_=3 , lowerCAmelCase_=0.6 , lowerCAmelCase_=None , ) -> int: _A = parent _A = batch_size _A = image_size _A = patch_size _A = num_channels _A = is_training _A = use_labels _A = hidden_size _A = num_hidden_layers _A = num_attention_heads _A = intermediate_size _A = hidden_act _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = type_sequence_label_size _A = initializer_range _A = mask_ratio _A = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) _A = (image_size // patch_size) ** 2 _A = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def UpperCAmelCase ( self ) -> Union[str, Any]: _A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _A = None if self.use_labels: _A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A = self.get_config() return config, pixel_values, labels def UpperCAmelCase ( self ) -> str: return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCAmelCase_ , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> Any: _A = ViTMAEModel(config=lowerCAmelCase_ ) model.to(lowerCAmelCase_ ) model.eval() _A = model(lowerCAmelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> List[Any]: _A = ViTMAEForPreTraining(lowerCAmelCase_ ) model.to(lowerCAmelCase_ ) model.eval() _A = model(lowerCAmelCase_ ) _A = (self.image_size // self.patch_size) ** 2 _A = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images _A = 1 _A = ViTMAEForPreTraining(lowerCAmelCase_ ) model.to(lowerCAmelCase_ ) model.eval() _A = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _A = model(lowerCAmelCase_ ) _A = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def UpperCAmelCase ( self ) -> Dict: _A = self.prepare_config_and_inputs() _A , _A , _A = config_and_inputs _A = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class a ( __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase :Union[str, Any] = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () lowerCamelCase :List[Any] = {'''feature-extraction''': ViTMAEModel} if is_torch_available() else {} lowerCamelCase :List[Any] = False lowerCamelCase :Tuple = False lowerCamelCase :int = False lowerCamelCase :Any = False def UpperCAmelCase ( self ) -> str: _A = ViTMAEModelTester(self ) _A = ConfigTester(self , config_class=lowerCAmelCase_ , has_text_modality=lowerCAmelCase_ , hidden_size=37 ) def UpperCAmelCase ( self ) -> str: self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMAE does not use inputs_embeds""" ) def UpperCAmelCase ( self ) -> Optional[Any]: pass def UpperCAmelCase ( self ) -> str: _A , _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(lowerCAmelCase_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _A = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase_ , nn.Linear ) ) def UpperCAmelCase ( self ) -> Union[str, Any]: _A , _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(lowerCAmelCase_ ) _A = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _A = [*signature.parameters.keys()] _A = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , lowerCAmelCase_ ) def UpperCAmelCase ( self ) -> Union[str, Any]: _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase_ ) def UpperCAmelCase ( self ) -> Dict: _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase_ ) def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> Any: # make masks reproducible np.random.seed(2 ) _A = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) _A = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) _A = torch.from_numpy(lowerCAmelCase_ ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument _A = pt_noise super().check_pt_tf_models(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) def UpperCAmelCase ( self ) -> Any: _A , _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(lowerCAmelCase_ ) model.to(lowerCAmelCase_ ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): _A = model(**self._prepare_for_class(lowerCAmelCase_ , lowerCAmelCase_ ) ) _A = outputs[0].cpu().numpy() _A = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCAmelCase_ ) _A = model_class.from_pretrained(lowerCAmelCase_ ) model.to(lowerCAmelCase_ ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): _A = model(**self._prepare_for_class(lowerCAmelCase_ , lowerCAmelCase_ ) ) # Make sure we don't have nans _A = after_outputs[0].cpu().numpy() _A = 0 _A = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(lowerCAmelCase_ , 1E-5 ) @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def UpperCAmelCase ( self ) -> Tuple: pass @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def UpperCAmelCase ( self ) -> str: pass @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def UpperCAmelCase ( self ) -> str: pass @unittest.skip(reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load""" ) def UpperCAmelCase ( self ) -> Dict: pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def UpperCAmelCase ( self ) -> str: pass @slow def UpperCAmelCase ( self ) -> Optional[Any]: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _A = ViTMAEModel.from_pretrained(lowerCAmelCase_ ) self.assertIsNotNone(lowerCAmelCase_ ) def snake_case ( ) -> List[str]: _A = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""") return image @require_torch @require_vision class a ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase ( self ) -> int: return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base""" ) if is_vision_available() else None @slow def UpperCAmelCase ( self ) -> Any: # make random mask reproducible across the PT and TF model np.random.seed(2 ) _A = ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base""" ).to(lowerCAmelCase_ ) _A = self.default_image_processor _A = prepare_img() _A = image_processor(images=lowerCAmelCase_ , return_tensors="""pt""" ).to(lowerCAmelCase_ ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) _A = ViTMAEConfig() _A = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) _A = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): _A = model(**lowerCAmelCase_ , noise=torch.from_numpy(lowerCAmelCase_ ).to(device=lowerCAmelCase_ ) ) # verify the logits _A = torch.Size((1, 1_96, 7_68) ) self.assertEqual(outputs.logits.shape , lowerCAmelCase_ ) _A = torch.tensor( [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(lowerCAmelCase_ ) , atol=1E-4 ) )
180
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
366
'''simple docstring''' import re def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' snake_case_ : List[Any] = re.compile( R"""^(?:0|94|\+94|0{2}94)""" R"""7(0|1|2|4|5|6|7|8)""" R"""(-| |)""" R"""\d{7}$""" ) return bool(re.search(lowerCamelCase_ , lowerCamelCase_ ) ) if __name__ == "__main__": __A : int = '0094702343221' print(is_sri_lankan_phone_number(phone))
8
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) _lowerCAmelCase : Dict = {'vocab_file': 'spm_char.model'} _lowerCAmelCase : int = { 'vocab_file': { 'microsoft/speecht5_asr': 'https://huggingface.co/microsoft/speecht5_asr/resolve/main/spm_char.model', 'microsoft/speecht5_tts': 'https://huggingface.co/microsoft/speecht5_tts/resolve/main/spm_char.model', 'microsoft/speecht5_vc': 'https://huggingface.co/microsoft/speecht5_vc/resolve/main/spm_char.model', } } _lowerCAmelCase : Any = { 'microsoft/speecht5_asr': 1_024, 'microsoft/speecht5_tts': 1_024, 'microsoft/speecht5_vc': 1_024, } class __magic_name__ ( UpperCamelCase__ ): """simple docstring""" __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self :int , snake_case :List[str] , snake_case :Optional[Any]="<s>" , snake_case :Optional[Any]="</s>" , snake_case :Tuple="<unk>" , snake_case :Optional[Any]="<pad>" , snake_case :Optional[Dict[str, Any]] = None , **snake_case :Tuple , ): '''simple docstring''' A_ : Tuple = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__a , eos_token=__a , unk_token=__a , pad_token=__a , sp_model_kwargs=self.sp_model_kwargs , **__a , ) A_ : List[str] = vocab_file A_ : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__a ) @property def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' return self.sp_model.get_piece_size() def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Any = {self.convert_ids_to_tokens(__a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self :Optional[Any] ): '''simple docstring''' A_ : Any = self.__dict__.copy() A_ : str = None return state def __setstate__( self :int , snake_case :Any ): '''simple docstring''' A_ : Tuple = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): A_ : List[str] = {} A_ : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :str ): '''simple docstring''' return self.sp_model.encode(__a , out_type=__a ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :Union[str, Any] ): '''simple docstring''' return self.sp_model.piece_to_id(__a ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :Any ): '''simple docstring''' A_ : Any = self.sp_model.IdToPiece(__a ) return token def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[Any] ): '''simple docstring''' A_ : Any = [] A_ : Any = "" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__a ) + token A_ : List[str] = [] else: current_sub_tokens.append(__a ) out_string += self.sp_model.decode(__a ) return out_string.strip() def SCREAMING_SNAKE_CASE ( self :str , snake_case :Union[str, Any] , snake_case :List[Any]=None ): '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :List[int] , snake_case :Optional[List[int]] = None , snake_case :bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__a , token_ids_a=__a , already_has_special_tokens=__a ) A_ : Union[str, Any] = [1] if token_ids_a is None: return ([0] * len(__a )) + suffix_ones return ([0] * len(__a )) + ([0] * len(__a )) + suffix_ones def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :str , snake_case :Optional[str] = None ): '''simple docstring''' if not os.path.isdir(__a ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return A_ : Optional[Any] = os.path.join( __a , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __a ) elif not os.path.isfile(self.vocab_file ): with open(__a , "wb" ) as fi: A_ : List[Any] = self.sp_model.serialized_model_proto() fi.write(__a ) return (out_vocab_file,)
300
'''simple docstring''' import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE_: Optional[int] =logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) SCREAMING_SNAKE_CASE_: Tuple =[] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (f"transformer.encoder.layers.{i}.self_attn.out_proj.weight", f"encoder.layers.{i}.self_attn.out_proj.weight") ) rename_keys.append( (f"transformer.encoder.layers.{i}.self_attn.out_proj.bias", f"encoder.layers.{i}.self_attn.out_proj.bias") ) rename_keys.append((f"transformer.encoder.layers.{i}.linear1.weight", f"encoder.layers.{i}.fc1.weight")) rename_keys.append((f"transformer.encoder.layers.{i}.linear1.bias", f"encoder.layers.{i}.fc1.bias")) rename_keys.append((f"transformer.encoder.layers.{i}.linear2.weight", f"encoder.layers.{i}.fc2.weight")) rename_keys.append((f"transformer.encoder.layers.{i}.linear2.bias", f"encoder.layers.{i}.fc2.bias")) rename_keys.append( (f"transformer.encoder.layers.{i}.norm1.weight", f"encoder.layers.{i}.self_attn_layer_norm.weight") ) rename_keys.append((f"transformer.encoder.layers.{i}.norm1.bias", f"encoder.layers.{i}.self_attn_layer_norm.bias")) rename_keys.append((f"transformer.encoder.layers.{i}.norm2.weight", f"encoder.layers.{i}.final_layer_norm.weight")) rename_keys.append((f"transformer.encoder.layers.{i}.norm2.bias", f"encoder.layers.{i}.final_layer_norm.bias")) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (f"transformer.decoder.layers.{i}.self_attn.out_proj.weight", f"decoder.layers.{i}.self_attn.out_proj.weight") ) rename_keys.append( (f"transformer.decoder.layers.{i}.self_attn.out_proj.bias", f"decoder.layers.{i}.self_attn.out_proj.bias") ) rename_keys.append( ( f"transformer.decoder.layers.{i}.cross_attn.out_proj.weight", f"decoder.layers.{i}.encoder_attn.out_proj.weight", ) ) rename_keys.append( ( f"transformer.decoder.layers.{i}.cross_attn.out_proj.bias", f"decoder.layers.{i}.encoder_attn.out_proj.bias", ) ) rename_keys.append((f"transformer.decoder.layers.{i}.linear1.weight", f"decoder.layers.{i}.fc1.weight")) rename_keys.append((f"transformer.decoder.layers.{i}.linear1.bias", f"decoder.layers.{i}.fc1.bias")) rename_keys.append((f"transformer.decoder.layers.{i}.linear2.weight", f"decoder.layers.{i}.fc2.weight")) rename_keys.append((f"transformer.decoder.layers.{i}.linear2.bias", f"decoder.layers.{i}.fc2.bias")) rename_keys.append( (f"transformer.decoder.layers.{i}.norm1.weight", f"decoder.layers.{i}.self_attn_layer_norm.weight") ) rename_keys.append((f"transformer.decoder.layers.{i}.norm1.bias", f"decoder.layers.{i}.self_attn_layer_norm.bias")) rename_keys.append( (f"transformer.decoder.layers.{i}.norm2.weight", f"decoder.layers.{i}.encoder_attn_layer_norm.weight") ) rename_keys.append( (f"transformer.decoder.layers.{i}.norm2.bias", f"decoder.layers.{i}.encoder_attn_layer_norm.bias") ) rename_keys.append((f"transformer.decoder.layers.{i}.norm3.weight", f"decoder.layers.{i}.final_layer_norm.weight")) rename_keys.append((f"transformer.decoder.layers.{i}.norm3.bias", f"decoder.layers.{i}.final_layer_norm.bias")) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (f"transformer.decoder.layers.{i}.sa_qcontent_proj.weight", f"decoder.layers.{i}.sa_qcontent_proj.weight") ) rename_keys.append( (f"transformer.decoder.layers.{i}.sa_kcontent_proj.weight", f"decoder.layers.{i}.sa_kcontent_proj.weight") ) rename_keys.append( (f"transformer.decoder.layers.{i}.sa_qpos_proj.weight", f"decoder.layers.{i}.sa_qpos_proj.weight") ) rename_keys.append( (f"transformer.decoder.layers.{i}.sa_kpos_proj.weight", f"decoder.layers.{i}.sa_kpos_proj.weight") ) rename_keys.append((f"transformer.decoder.layers.{i}.sa_v_proj.weight", f"decoder.layers.{i}.sa_v_proj.weight")) rename_keys.append( (f"transformer.decoder.layers.{i}.ca_qcontent_proj.weight", f"decoder.layers.{i}.ca_qcontent_proj.weight") ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (f"transformer.decoder.layers.{i}.ca_kcontent_proj.weight", f"decoder.layers.{i}.ca_kcontent_proj.weight") ) rename_keys.append( (f"transformer.decoder.layers.{i}.ca_kpos_proj.weight", f"decoder.layers.{i}.ca_kpos_proj.weight") ) rename_keys.append((f"transformer.decoder.layers.{i}.ca_v_proj.weight", f"decoder.layers.{i}.ca_v_proj.weight")) rename_keys.append( (f"transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight", f"decoder.layers.{i}.ca_qpos_sine_proj.weight") ) rename_keys.append( (f"transformer.decoder.layers.{i}.sa_qcontent_proj.bias", f"decoder.layers.{i}.sa_qcontent_proj.bias") ) rename_keys.append( (f"transformer.decoder.layers.{i}.sa_kcontent_proj.bias", f"decoder.layers.{i}.sa_kcontent_proj.bias") ) rename_keys.append((f"transformer.decoder.layers.{i}.sa_qpos_proj.bias", f"decoder.layers.{i}.sa_qpos_proj.bias")) rename_keys.append((f"transformer.decoder.layers.{i}.sa_kpos_proj.bias", f"decoder.layers.{i}.sa_kpos_proj.bias")) rename_keys.append((f"transformer.decoder.layers.{i}.sa_v_proj.bias", f"decoder.layers.{i}.sa_v_proj.bias")) rename_keys.append( (f"transformer.decoder.layers.{i}.ca_qcontent_proj.bias", f"decoder.layers.{i}.ca_qcontent_proj.bias") ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (f"transformer.decoder.layers.{i}.ca_kcontent_proj.bias", f"decoder.layers.{i}.ca_kcontent_proj.bias") ) rename_keys.append((f"transformer.decoder.layers.{i}.ca_kpos_proj.bias", f"decoder.layers.{i}.ca_kpos_proj.bias")) rename_keys.append((f"transformer.decoder.layers.{i}.ca_v_proj.bias", f"decoder.layers.{i}.ca_v_proj.bias")) rename_keys.append( (f"transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias", f"decoder.layers.{i}.ca_qpos_sine_proj.bias") ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('input_proj.weight', 'input_projection.weight'), ('input_proj.bias', 'input_projection.bias'), ('query_embed.weight', 'query_position_embeddings.weight'), ('transformer.decoder.norm.weight', 'decoder.layernorm.weight'), ('transformer.decoder.norm.bias', 'decoder.layernorm.bias'), ('class_embed.weight', 'class_labels_classifier.weight'), ('class_embed.bias', 'class_labels_classifier.bias'), ('bbox_embed.layers.0.weight', 'bbox_predictor.layers.0.weight'), ('bbox_embed.layers.0.bias', 'bbox_predictor.layers.0.bias'), ('bbox_embed.layers.1.weight', 'bbox_predictor.layers.1.weight'), ('bbox_embed.layers.1.bias', 'bbox_predictor.layers.1.bias'), ('bbox_embed.layers.2.weight', 'bbox_predictor.layers.2.weight'), ('bbox_embed.layers.2.bias', 'bbox_predictor.layers.2.bias'), ('transformer.decoder.ref_point_head.layers.0.weight', 'decoder.ref_point_head.layers.0.weight'), ('transformer.decoder.ref_point_head.layers.0.bias', 'decoder.ref_point_head.layers.0.bias'), ('transformer.decoder.ref_point_head.layers.1.weight', 'decoder.ref_point_head.layers.1.weight'), ('transformer.decoder.ref_point_head.layers.1.bias', 'decoder.ref_point_head.layers.1.bias'), ('transformer.decoder.query_scale.layers.0.weight', 'decoder.query_scale.layers.0.weight'), ('transformer.decoder.query_scale.layers.0.bias', 'decoder.query_scale.layers.0.bias'), ('transformer.decoder.query_scale.layers.1.weight', 'decoder.query_scale.layers.1.weight'), ('transformer.decoder.query_scale.layers.1.bias', 'decoder.query_scale.layers.1.bias'), ('transformer.decoder.layers.0.ca_qpos_proj.weight', 'decoder.layers.0.ca_qpos_proj.weight'), ('transformer.decoder.layers.0.ca_qpos_proj.bias', 'decoder.layers.0.ca_qpos_proj.bias'), ] ) def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : Any , snake_case_ : Optional[int] ) -> Dict: '''simple docstring''' UpperCAmelCase_ = state_dict.pop(snake_case_ ) UpperCAmelCase_ = val def lowerCAmelCase_ ( snake_case_ : int ) -> List[Any]: '''simple docstring''' UpperCAmelCase_ = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: UpperCAmelCase_ = key.replace("backbone.0.body" , "backbone.conv_encoder.model" ) UpperCAmelCase_ = value else: UpperCAmelCase_ = value return new_state_dict def lowerCAmelCase_ ( snake_case_ : List[Any] , snake_case_ : Dict=False ) -> List[str]: '''simple docstring''' UpperCAmelCase_ = "" if is_panoptic: UpperCAmelCase_ = "conditional_detr." # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) UpperCAmelCase_ = state_dict.pop(f"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) UpperCAmelCase_ = state_dict.pop(f"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict UpperCAmelCase_ = in_proj_weight[:2_56, :] UpperCAmelCase_ = in_proj_bias[:2_56] UpperCAmelCase_ = in_proj_weight[2_56:5_12, :] UpperCAmelCase_ = in_proj_bias[2_56:5_12] UpperCAmelCase_ = in_proj_weight[-2_56:, :] UpperCAmelCase_ = in_proj_bias[-2_56:] def lowerCAmelCase_ ( ) -> Dict: '''simple docstring''' UpperCAmelCase_ = "http://images.cocodataset.org/val2017/000000039769.jpg" UpperCAmelCase_ = Image.open(requests.get(snake_case_ , stream=snake_case_ ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( snake_case_ : Optional[int] , snake_case_ : Dict ) -> Optional[int]: '''simple docstring''' UpperCAmelCase_ = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: UpperCAmelCase_ = "resnet101" if "dc5" in model_name: UpperCAmelCase_ = True UpperCAmelCase_ = "panoptic" in model_name if is_panoptic: UpperCAmelCase_ = 2_50 else: UpperCAmelCase_ = 91 UpperCAmelCase_ = "huggingface/label-files" UpperCAmelCase_ = "coco-detection-id2label.json" UpperCAmelCase_ = json.load(open(hf_hub_download(snake_case_ , snake_case_ , repo_type="dataset" ) , "r" ) ) UpperCAmelCase_ = {int(snake_case_ ): v for k, v in idalabel.items()} UpperCAmelCase_ = idalabel UpperCAmelCase_ = {v: k for k, v in idalabel.items()} # load image processor UpperCAmelCase_ = "coco_panoptic" if is_panoptic else "coco_detection" UpperCAmelCase_ = ConditionalDetrImageProcessor(format=snake_case_ ) # prepare image UpperCAmelCase_ = prepare_img() UpperCAmelCase_ = image_processor(images=snake_case_ , return_tensors="pt" ) UpperCAmelCase_ = encoding["pixel_values"] logger.info(f"""Converting model {model_name}...""" ) # load original model from torch hub UpperCAmelCase_ = torch.hub.load("DeppMeng/ConditionalDETR" , snake_case_ , pretrained=snake_case_ ).eval() UpperCAmelCase_ = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: UpperCAmelCase_ = "conditional_detr." + src rename_key(snake_case_ , snake_case_ , snake_case_ ) UpperCAmelCase_ = rename_backbone_keys(snake_case_ ) # query, key and value matrices need special treatment read_in_q_k_v(snake_case_ , is_panoptic=snake_case_ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them UpperCAmelCase_ = "conditional_detr.model." if is_panoptic else "model." for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith("conditional_detr" ) and not key.startswith("class_labels_classifier" ) and not key.startswith("bbox_predictor" ) ): UpperCAmelCase_ = state_dict.pop(snake_case_ ) UpperCAmelCase_ = val elif "class_labels_classifier" in key or "bbox_predictor" in key: UpperCAmelCase_ = state_dict.pop(snake_case_ ) UpperCAmelCase_ = val elif key.startswith("bbox_attention" ) or key.startswith("mask_head" ): continue else: UpperCAmelCase_ = state_dict.pop(snake_case_ ) UpperCAmelCase_ = val else: if not key.startswith("class_labels_classifier" ) and not key.startswith("bbox_predictor" ): UpperCAmelCase_ = state_dict.pop(snake_case_ ) UpperCAmelCase_ = val # finally, create HuggingFace model and load state dict UpperCAmelCase_ = ConditionalDetrForSegmentation(snake_case_ ) if is_panoptic else ConditionalDetrForObjectDetection(snake_case_ ) model.load_state_dict(snake_case_ ) model.eval() model.push_to_hub(repo_id=snake_case_ , organization="DepuMeng" , commit_message="Add model" ) # verify our conversion UpperCAmelCase_ = conditional_detr(snake_case_ ) UpperCAmelCase_ = model(snake_case_ ) assert torch.allclose(outputs.logits , original_outputs["pred_logits"] , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs["pred_boxes"] , atol=1E-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs["pred_masks"] , atol=1E-4 ) # Save model and image processor logger.info(f"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) model.save_pretrained(snake_case_ ) image_processor.save_pretrained(snake_case_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_: List[str] =argparse.ArgumentParser() parser.add_argument( '--model_name', default='conditional_detr_resnet50', type=str, help='Name of the CONDITIONAL_DETR model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.' ) SCREAMING_SNAKE_CASE_: int =parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
1
0
'''simple docstring''' import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class A_ ( unittest.TestCase ): '''simple docstring''' def UpperCAmelCase_ ( self : str ) -> Tuple: UpperCAmelCase : Any = inspect.getfile(accelerate.test_utils ) UpperCAmelCase : Dict = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['scripts', 'test_script.py'] ) UpperCAmelCase : Dict = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['scripts', 'test_distributed_data_loop.py'] ) UpperCAmelCase : List[Any] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['scripts', 'test_ops.py'] ) @require_multi_gpu def UpperCAmelCase_ ( self : Optional[Any] ) -> Optional[int]: print(f"""Found {torch.cuda.device_count()} devices.""" ) UpperCAmelCase : Dict = ['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowercase_ , env=os.environ.copy() ) @require_multi_gpu def UpperCAmelCase_ ( self : Union[str, Any] ) -> List[Any]: print(f"""Found {torch.cuda.device_count()} devices.""" ) UpperCAmelCase : int = ['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(lowercase_ , env=os.environ.copy() ) @require_multi_gpu def UpperCAmelCase_ ( self : str ) -> List[Any]: UpperCAmelCase : Optional[int] = ['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowercase_ , env=os.environ.copy() ) @require_multi_gpu def UpperCAmelCase_ ( self : List[str] ) -> Optional[Any]: print(f"""Found {torch.cuda.device_count()} devices, using 2 devices only""" ) UpperCAmelCase : Dict = ['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(lowercase_ , env=os.environ.copy() ) if __name__ == "__main__": lowercase__ = Accelerator() lowercase__ = (accelerator.state.process_index + 2, 10) lowercase__ = torch.randint(0, 10, shape).to(accelerator.device) lowercase__ = "" lowercase__ = 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)." lowercase__ = 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." lowercase__ = 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)
362
'''simple docstring''' import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() lowercase__ = logging.get_logger("transformers.models.speecht5") def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): hf_model.apply_weight_norm() UpperCAmelCase : Dict = checkpoint['input_conv.weight_g'] UpperCAmelCase : Any = checkpoint['input_conv.weight_v'] UpperCAmelCase : Any = checkpoint['input_conv.bias'] for i in range(len(config.upsample_rates ) ): UpperCAmelCase : Any = checkpoint[F"""upsamples.{i}.1.weight_g"""] UpperCAmelCase : Union[str, Any] = checkpoint[F"""upsamples.{i}.1.weight_v"""] UpperCAmelCase : str = checkpoint[F"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): UpperCAmelCase : Any = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_g"""] UpperCAmelCase : Optional[int] = checkpoint[F"""blocks.{i}.convs1.{j}.1.weight_v"""] UpperCAmelCase : Dict = checkpoint[F"""blocks.{i}.convs1.{j}.1.bias"""] UpperCAmelCase : Union[str, Any] = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_g"""] UpperCAmelCase : int = checkpoint[F"""blocks.{i}.convs2.{j}.1.weight_v"""] UpperCAmelCase : Optional[int] = checkpoint[F"""blocks.{i}.convs2.{j}.1.bias"""] UpperCAmelCase : Dict = checkpoint['output_conv.1.weight_g'] UpperCAmelCase : str = checkpoint['output_conv.1.weight_v'] UpperCAmelCase : Union[str, Any] = checkpoint['output_conv.1.bias'] hf_model.remove_weight_norm() @torch.no_grad() def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_=None , UpperCAmelCase_=None , ): if config_path is not None: UpperCAmelCase : Any = SpeechTaHifiGanConfig.from_pretrained(UpperCAmelCase_ ) else: UpperCAmelCase : Optional[Any] = SpeechTaHifiGanConfig() UpperCAmelCase : List[Any] = SpeechTaHifiGan(UpperCAmelCase_ ) UpperCAmelCase : int = torch.load(UpperCAmelCase_ ) load_weights(orig_checkpoint['model']['generator'] , UpperCAmelCase_ , UpperCAmelCase_ ) UpperCAmelCase : int = np.load(UpperCAmelCase_ ) UpperCAmelCase : str = stats[0].reshape(-1 ) UpperCAmelCase : List[str] = stats[1].reshape(-1 ) UpperCAmelCase : Union[str, Any] = torch.from_numpy(UpperCAmelCase_ ).float() UpperCAmelCase : List[str] = torch.from_numpy(UpperCAmelCase_ ).float() model.save_pretrained(UpperCAmelCase_ ) if repo_id: print('Pushing to the hub...' ) model.push_to_hub(UpperCAmelCase_ ) if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--pytorch_dump_folder_path", required=True, default=None, type=str, help="Path to the output PyTorch model." ) parser.add_argument( "--push_to_hub", default=None, type=str, help="Where to upload the converted model on the 🤗 hub." ) lowercase__ = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
280
0
_lowerCAmelCase : Dict = {"a": ["c", "b"], "b": ["d", "e"], "c": [], "d": [], "e": []} _lowerCAmelCase : str = ["a", "b", "c", "d", "e"] def lowerCAmelCase ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : int ): """simple docstring""" UpperCAmelCase__ = start # add current to visited visited.append(_lowerCAmelCase ) UpperCAmelCase__ = edges[current] for neighbor in neighbors: # if neighbor not in visited, visit if neighbor not in visited: UpperCAmelCase__ = topological_sort(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # if all neighbors visited add current to sort sort.append(_lowerCAmelCase ) # if all vertices haven't been visited select a new one to visit if len(_lowerCAmelCase ) != len(_lowerCAmelCase ): for vertice in vertices: if vertice not in visited: UpperCAmelCase__ = topological_sort(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # return sort return sort if __name__ == "__main__": _lowerCAmelCase : Optional[int] = topological_sort("a", [], []) print(sort)
169
from ...processing_utils import ProcessorMixin class _UpperCamelCase ( lowerCAmelCase ): UpperCAmelCase_ = ["""image_processor""", """feature_extractor"""] UpperCAmelCase_ = """TvltImageProcessor""" UpperCAmelCase_ = """TvltFeatureExtractor""" def __init__( self :List[str] , lowerCamelCase :Dict , lowerCamelCase :Tuple ) -> Any: super().__init__(image_processor=lowerCamelCase , feature_extractor=lowerCamelCase ) UpperCAmelCase__ = image_processor UpperCAmelCase__ = feature_extractor def __call__( self :Union[str, Any] , lowerCamelCase :List[str]=None , lowerCamelCase :int=None , lowerCamelCase :List[Any]=None , lowerCamelCase :Dict=None , lowerCamelCase :List[str]=False , lowerCamelCase :Optional[Any]=False , *lowerCamelCase :List[Any] , **lowerCamelCase :Dict , ) -> List[str]: if images is None and audio is None: raise ValueError("You need to specify either an `images` or `audio` input to process." ) UpperCAmelCase__ = None if images is not None: UpperCAmelCase__ = self.image_processor(lowerCamelCase , mask_pixel=lowerCamelCase , *lowerCamelCase , **lowerCamelCase ) if images_mixed is not None: UpperCAmelCase__ = self.image_processor(lowerCamelCase , is_mixed=lowerCamelCase , *lowerCamelCase , **lowerCamelCase ) if audio is not None: UpperCAmelCase__ = self.feature_extractor( lowerCamelCase , *lowerCamelCase , sampling_rate=lowerCamelCase , mask_audio=lowerCamelCase , **lowerCamelCase ) UpperCAmelCase__ = {} if audio is not None: output_dict.update(lowerCamelCase ) if images is not None: output_dict.update(lowerCamelCase ) if images_mixed_dict is not None: output_dict.update(lowerCamelCase ) return output_dict @property def UpperCAmelCase_ ( self :Dict ) -> Optional[Any]: UpperCAmelCase__ = self.image_processor.model_input_names UpperCAmelCase__ = self.feature_extractor.model_input_names return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names ) )
169
1
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Value from .base import TaskTemplate @dataclass(frozen=lowerCamelCase__ ) class UpperCamelCase__ ( lowerCamelCase__ ): """simple docstring""" __magic_name__ = field(default="text-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) __magic_name__ = Features({"text": Value("string" )} ) __magic_name__ = Features({"labels": ClassLabel} ) __magic_name__ = "text" __magic_name__ = "labels" def a ( self , snake_case__ ): '''simple docstring''' if self.label_column not in features: raise ValueError(F'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __snake_case ): raise ValueError(F'Column {self.label_column} is not a ClassLabel.' ) _lowerCAmelCase : List[Any] = copy.deepcopy(self ) _lowerCAmelCase : str = self.label_schema.copy() _lowerCAmelCase : List[Any] = features[self.label_column] _lowerCAmelCase : Optional[Any] = label_schema return task_template @property def a ( self ): '''simple docstring''' return { self.text_column: "text", self.label_column: "labels", }
357
'''simple docstring''' import copy from dataclasses import dataclass from pathlib import Path from typing import Dict, Optional, Union @dataclass class UpperCamelCase__ : """simple docstring""" __magic_name__ = None __magic_name__ = False __magic_name__ = False __magic_name__ = False __magic_name__ = None __magic_name__ = None __magic_name__ = False __magic_name__ = False __magic_name__ = False __magic_name__ = True __magic_name__ = None __magic_name__ = 1 __magic_name__ = None __magic_name__ = False __magic_name__ = None __magic_name__ = None def a ( self ): '''simple docstring''' return self.__class__(**{k: copy.deepcopy(snake_case__ ) for k, v in self.__dict__.items()} )
25
0
"""simple docstring""" import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) 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 A = 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.8.0''', '''To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt''') @dataclass class __lowercase : '''simple docstring''' __lowerCAmelCase = field( default='''cifar10''' , metadata={'''help''': '''Name of a dataset from the datasets package'''} ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={'''help''': '''The column name of the images in the files.'''} ) __lowerCAmelCase = field(default=_UpperCamelCase , metadata={'''help''': '''A folder containing the training data.'''} ) __lowerCAmelCase = field(default=_UpperCamelCase , metadata={'''help''': '''A folder containing the validation data.'''} ) __lowerCAmelCase = field( default=0.15 , metadata={'''help''': '''Percent to split off of train for validation.'''} ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of training examples to this ''' '''value if set.''' ) } , ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of evaluation examples to this ''' '''value if set.''' ) } , ) def _lowerCamelCase ( self ): __a : Union[str, Any] = {} if self.train_dir is not None: __a : Dict = self.train_dir if self.validation_dir is not None: __a : int = self.validation_dir __a : Union[str, Any] = data_files if data_files else None @dataclass class __lowercase : '''simple docstring''' __lowerCAmelCase = field( default=_UpperCamelCase , metadata={ '''help''': ( '''The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.''' ) } , ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={'''help''': '''Pretrained config name or path if not the same as model_name_or_path'''} ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={ '''help''': ( '''Override some existing default config settings when a model is trained from scratch. Example: ''' '''n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index''' ) } , ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from s3'''} ) __lowerCAmelCase = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) __lowerCAmelCase = field(default=_UpperCamelCase , metadata={'''help''': '''Name or path of preprocessor config.'''} ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) __lowerCAmelCase = field( default=0.75 , metadata={'''help''': '''The ratio of the number of masked tokens in the input sequence.'''} ) __lowerCAmelCase = field( default=_UpperCamelCase , metadata={'''help''': '''Whether or not to train with normalized pixel values as target.'''} ) @dataclass class __lowercase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = field( default=1E-3 , metadata={'''help''': '''Base learning rate: absolute_lr = base_lr * total_batch_size / 256.'''} ) def __A ( a_ :Tuple) -> List[Any]: __a : Optional[int] = torch.stack([example['''pixel_values'''] for example in examples]) return {"pixel_values": pixel_values} def __A ( ) -> Any: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. __a : Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments)) 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. __a , __a , __a : Optional[Any] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) else: __a , __a , __a : str = 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_mae''' , a_ , a_) # 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() __a : List[str] = training_args.get_process_log_level() logger.setLevel(a_) transformers.utils.logging.set_verbosity(a_) 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}""") # Detecting last checkpoint. __a : Union[str, Any] = None if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir: __a : str = get_last_checkpoint(training_args.output_dir) if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ '''Use --overwrite_output_dir to overcome.''') elif last_checkpoint is not None 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. __a : Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. __a : Any = None if '''validation''' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , a_) and data_args.train_val_split > 0.0: __a : Union[str, Any] = ds['''train'''].train_test_split(data_args.train_val_split) __a : List[Any] = split['''train'''] __a : str = split['''test'''] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __a : Tuple = { '''cache_dir''': model_args.cache_dir, '''revision''': model_args.model_revision, '''use_auth_token''': True if model_args.use_auth_token else None, } if model_args.config_name: __a : Dict = ViTMAEConfig.from_pretrained(model_args.config_name , **a_) elif model_args.model_name_or_path: __a : Optional[Any] = ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **a_) else: __a : Optional[Any] = ViTMAEConfig() logger.warning('''You are instantiating a new config instance from scratch.''') if model_args.config_overrides is not None: logger.info(F"""Overriding config: {model_args.config_overrides}""") config.update_from_string(model_args.config_overrides) logger.info(F"""New config: {config}""") # adapt config config.update( { '''mask_ratio''': model_args.mask_ratio, '''norm_pix_loss''': model_args.norm_pix_loss, }) # create image processor if model_args.image_processor_name: __a : Union[str, Any] = ViTImageProcessor.from_pretrained(model_args.image_processor_name , **a_) elif model_args.model_name_or_path: __a : List[Any] = ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **a_) else: __a : Dict = ViTImageProcessor() # create model if model_args.model_name_or_path: __a : List[Any] = ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('''Training new model from scratch''') __a : Dict = ViTMAEForPreTraining(a_) if training_args.do_train: __a : List[str] = ds['''train'''].column_names else: __a : str = ds['''validation'''].column_names if data_args.image_column_name is not None: __a : Any = data_args.image_column_name elif "image" in column_names: __a : Union[str, Any] = '''image''' elif "img" in column_names: __a : Union[str, Any] = '''img''' else: __a : List[str] = column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: __a : List[str] = image_processor.size['''shortest_edge'''] else: __a : Union[str, Any] = (image_processor.size['''height'''], image_processor.size['''width''']) __a : str = Compose( [ Lambda(lambda a_: img.convert('''RGB''') if img.mode != "RGB" else img), RandomResizedCrop(a_ , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std), ]) def preprocess_images(a_ :Union[str, Any]): __a : List[Any] = [transforms(a_) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError('''--do_train requires a train dataset''') if data_args.max_train_samples is not None: __a : Dict = ds['''train'''].shuffle(seed=training_args.seed).select(range(data_args.max_train_samples)) # Set the training transforms ds["train"].set_transform(a_) if training_args.do_eval: if "validation" not in ds: raise ValueError('''--do_eval requires a validation dataset''') if data_args.max_eval_samples is not None: __a : Union[str, Any] = ( ds['''validation'''].shuffle(seed=training_args.seed).select(range(data_args.max_eval_samples)) ) # Set the validation transforms ds["validation"].set_transform(a_) # Compute absolute learning rate __a : Union[str, Any] = ( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: __a : Any = training_args.base_learning_rate * total_train_batch_size / 2_56 # Initialize our trainer __a : Tuple = Trainer( model=a_ , args=a_ , train_dataset=ds['''train'''] if training_args.do_train else None , eval_dataset=ds['''validation'''] if training_args.do_eval else None , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: __a : Union[str, Any] = None if training_args.resume_from_checkpoint is not None: __a : Optional[Any] = training_args.resume_from_checkpoint elif last_checkpoint is not None: __a : int = last_checkpoint __a : Dict = trainer.train(resume_from_checkpoint=a_) 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: __a : Union[str, Any] = trainer.evaluate() trainer.log_metrics('''eval''' , a_) trainer.save_metrics('''eval''' , a_) # Write model card and (optionally) push to hub __a : Dict = { '''tasks''': '''masked-auto-encoding''', '''dataset''': data_args.dataset_name, '''tags''': ['''masked-auto-encoding'''], } if training_args.push_to_hub: trainer.push_to_hub(**a_) else: trainer.create_model_card(**a_) def __A ( a_ :List[str]) -> Optional[int]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
160
"""simple docstring""" import numpy as np from cva import destroyAllWindows, imread, imshow, waitKey class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): if dst_width < 0 or dst_height < 0: raise ValueError('''Destination width/height should be > 0''' ) __a : Optional[int] = img __a : Any = img.shape[1] __a : Optional[int] = img.shape[0] __a : Tuple = dst_width __a : List[Any] = dst_height __a : Optional[int] = self.src_w / self.dst_w __a : Tuple = self.src_h / self.dst_h __a : Union[str, Any] = ( np.ones((self.dst_h, self.dst_w, 3) , np.uinta ) * 255 ) def _lowerCamelCase ( self ): for i in range(self.dst_h ): for j in range(self.dst_w ): __a : Optional[int] = self.img[self.get_y(_UpperCAmelCase )][self.get_x(_UpperCAmelCase )] def _lowerCamelCase ( self , _UpperCAmelCase ): return int(self.ratio_x * x ) def _lowerCamelCase ( self , _UpperCAmelCase ): return int(self.ratio_y * y ) if __name__ == "__main__": A , A = 800, 600 A = imread('''image_data/lena.jpg''', 1) A = NearestNeighbour(im, dst_w, dst_h) n.process() imshow( F'Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}', n.output ) waitKey(0) destroyAllWindows()
160
1
"""simple docstring""" from collections.abc import Iterable from typing import Generic, TypeVar lowerCamelCase = TypeVar("""_T""") class lowercase__ ( Generic[_T] ): '''simple docstring''' def __init__( self : int , _UpperCAmelCase : Iterable[_T] | None = None ) -> None: '''simple docstring''' UpperCAmelCase_ = list(iterable or [] ) UpperCAmelCase_ = [] def __len__( self : Optional[int] ) -> int: '''simple docstring''' return len(self._stacka ) + len(self._stacka ) def __repr__( self : Optional[Any] ) -> str: '''simple docstring''' return F"""Queue({tuple(self._stacka[::-1] + self._stacka )})""" def lowercase__ ( self : Optional[Any] , _UpperCAmelCase : _T ) -> None: '''simple docstring''' self._stacka.append(_UpperCAmelCase ) def lowercase__ ( self : Dict ) -> _T: '''simple docstring''' UpperCAmelCase_ = self._stacka.pop UpperCAmelCase_ = self._stacka.append if not self._stacka: while self._stacka: stacka_append(stacka_pop() ) if not self._stacka: raise IndexError("Queue is empty" ) return self._stacka.pop() if __name__ == "__main__": from doctest import testmod testmod()
241
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase = logging.get_logger(__name__) lowerCamelCase = { """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""", """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""", """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""", """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""", """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""", """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""", } class lowercase__ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' UpperCamelCase = '''roberta''' def __init__( self : int , _UpperCAmelCase : List[Any]=50265 , _UpperCAmelCase : str=768 , _UpperCAmelCase : List[str]=12 , _UpperCAmelCase : int=12 , _UpperCAmelCase : Tuple=3072 , _UpperCAmelCase : Dict="gelu" , _UpperCAmelCase : int=0.1 , _UpperCAmelCase : Optional[int]=0.1 , _UpperCAmelCase : Optional[int]=512 , _UpperCAmelCase : Optional[Any]=2 , _UpperCAmelCase : Optional[int]=0.02 , _UpperCAmelCase : Optional[Any]=1e-12 , _UpperCAmelCase : Dict=1 , _UpperCAmelCase : str=0 , _UpperCAmelCase : int=2 , _UpperCAmelCase : Tuple="absolute" , _UpperCAmelCase : Any=True , _UpperCAmelCase : int=None , **_UpperCAmelCase : List[str] , ) -> Any: '''simple docstring''' super().__init__(pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase ) UpperCAmelCase_ = vocab_size UpperCAmelCase_ = hidden_size UpperCAmelCase_ = num_hidden_layers UpperCAmelCase_ = num_attention_heads UpperCAmelCase_ = hidden_act UpperCAmelCase_ = intermediate_size UpperCAmelCase_ = hidden_dropout_prob UpperCAmelCase_ = attention_probs_dropout_prob UpperCAmelCase_ = max_position_embeddings UpperCAmelCase_ = type_vocab_size UpperCAmelCase_ = initializer_range UpperCAmelCase_ = layer_norm_eps UpperCAmelCase_ = position_embedding_type UpperCAmelCase_ = use_cache UpperCAmelCase_ = classifier_dropout class lowercase__ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def lowercase__ ( self : List[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCAmelCase_ = {0: "batch", 1: "choice", 2: "sequence"} else: UpperCAmelCase_ = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
241
1
'''simple docstring''' import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( '''The `image_to_image.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionImg2ImgPipeline` instead.''' )
53
'''simple docstring''' from __future__ import annotations class snake_case : """simple docstring""" def __init__( self : Optional[int] , __A : list[list[int]] ): __UpperCamelCase = TypeError( 'Matrices must be formed from a list of zero or more lists containing at ' 'least one and the same number of values, each of which must be of type ' 'int or float.' ) if len(__A ) != 0: __UpperCamelCase = len(rows[0] ) if cols == 0: raise error for row in rows: if len(__A ) != cols: raise error for value in row: if not isinstance(__A , (int, float) ): raise error __UpperCamelCase = rows else: __UpperCamelCase = [] def _lowerCamelCase ( self : int ): return [[row[i] for row in self.rows] for i in range(len(self.rows[0] ) )] @property def _lowerCamelCase ( self : str ): return len(self.rows ) @property def _lowerCamelCase ( self : Any ): return len(self.rows[0] ) @property def _lowerCamelCase ( self : Optional[Any] ): return (self.num_rows, self.num_columns) @property def _lowerCamelCase ( self : Dict ): return self.order[0] == self.order[1] def _lowerCamelCase ( self : Any ): __UpperCamelCase = [ [0 if column_num != row_num else 1 for column_num in range(self.num_rows )] for row_num in range(self.num_rows ) ] return Matrix(__A ) def _lowerCamelCase ( self : Any ): if not self.is_square: return 0 if self.order == (0, 0): return 1 if self.order == (1, 1): return int(self.rows[0][0] ) if self.order == (2, 2): return int( (self.rows[0][0] * self.rows[1][1]) - (self.rows[0][1] * self.rows[1][0]) ) else: return sum( self.rows[0][column] * self.cofactors().rows[0][column] for column in range(self.num_columns ) ) def _lowerCamelCase ( self : List[str] ): return bool(self.determinant() ) def _lowerCamelCase ( self : Dict , __A : int , __A : int ): __UpperCamelCase = [ [ self.rows[other_row][other_column] for other_column in range(self.num_columns ) if other_column != column ] for other_row in range(self.num_rows ) if other_row != row ] return Matrix(__A ).determinant() def _lowerCamelCase ( self : Dict , __A : int , __A : int ): if (row + column) % 2 == 0: return self.get_minor(__A , __A ) return -1 * self.get_minor(__A , __A ) def _lowerCamelCase ( self : List[str] ): return Matrix( [ [self.get_minor(__A , __A ) for column in range(self.num_columns )] for row in range(self.num_rows ) ] ) def _lowerCamelCase ( self : Union[str, Any] ): return Matrix( [ [ self.minors().rows[row][column] if (row + column) % 2 == 0 else self.minors().rows[row][column] * -1 for column in range(self.minors().num_columns ) ] for row in range(self.minors().num_rows ) ] ) def _lowerCamelCase ( self : List[str] ): __UpperCamelCase = [ [self.cofactors().rows[column][row] for column in range(self.num_columns )] for row in range(self.num_rows ) ] return Matrix(__A ) def _lowerCamelCase ( self : Dict ): __UpperCamelCase = self.determinant() if not determinant: raise TypeError('Only matrices with a non-zero determinant have an inverse' ) return self.adjugate() * (1 / determinant) def __repr__( self : Optional[Any] ): return str(self.rows ) def __str__( self : Union[str, Any] ): if self.num_rows == 0: return "[]" if self.num_rows == 1: return "[[" + ". ".join(str(self.rows[0] ) ) + "]]" return ( "[" + "\n ".join( [ '[' + '. '.join([str(__A ) for value in row] ) + '.]' for row in self.rows ] ) + "]" ) def _lowerCamelCase ( self : List[Any] , __A : list[int] , __A : int | None = None ): __UpperCamelCase = TypeError('Row must be a list containing all ints and/or floats' ) if not isinstance(__A , __A ): raise type_error for value in row: if not isinstance(__A , (int, float) ): raise type_error if len(__A ) != self.num_columns: raise ValueError( 'Row must be equal in length to the other rows in the matrix' ) if position is None: self.rows.append(__A ) else: __UpperCamelCase = self.rows[0:position] + [row] + self.rows[position:] def _lowerCamelCase ( self : Optional[Any] , __A : list[int] , __A : int | None = None ): __UpperCamelCase = TypeError( 'Column must be a list containing all ints and/or floats' ) if not isinstance(__A , __A ): raise type_error for value in column: if not isinstance(__A , (int, float) ): raise type_error if len(__A ) != self.num_rows: raise ValueError( 'Column must be equal in length to the other columns in the matrix' ) if position is None: __UpperCamelCase = [self.rows[i] + [column[i]] for i in range(self.num_rows )] else: __UpperCamelCase = [ self.rows[i][0:position] + [column[i]] + self.rows[i][position:] for i in range(self.num_rows ) ] def __eq__( self : Tuple , __A : object ): if not isinstance(__A , __A ): return NotImplemented return self.rows == other.rows def __ne__( self : Any , __A : object ): return not self == other def __neg__( self : List[Any] ): return self * -1 def __add__( self : List[str] , __A : Matrix ): if self.order != other.order: raise ValueError('Addition requires matrices of the same order' ) return Matrix( [ [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns )] for i in range(self.num_rows ) ] ) def __sub__( self : str , __A : Matrix ): if self.order != other.order: raise ValueError('Subtraction requires matrices of the same order' ) return Matrix( [ [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns )] for i in range(self.num_rows ) ] ) def __mul__( self : str , __A : Matrix | int | float ): if isinstance(__A , (int, float) ): return Matrix( [[int(element * other ) for element in row] for row in self.rows] ) elif isinstance(__A , __A ): if self.num_columns != other.num_rows: raise ValueError( 'The number of columns in the first matrix must ' 'be equal to the number of rows in the second' ) return Matrix( [ [Matrix.dot_product(__A , __A ) for column in other.columns()] for row in self.rows ] ) else: raise TypeError( 'A Matrix can only be multiplied by an int, float, or another matrix' ) def __pow__( self : Union[str, Any] , __A : int ): if not isinstance(__A , __A ): raise TypeError('A Matrix can only be raised to the power of an int' ) if not self.is_square: raise ValueError('Only square matrices can be raised to a power' ) if other == 0: return self.identity() if other < 0: if self.is_invertable(): return self.inverse() ** (-other) raise ValueError( 'Only invertable matrices can be raised to a negative power' ) __UpperCamelCase = self for _ in range(other - 1 ): result *= self return result @classmethod def _lowerCamelCase ( cls : Tuple , __A : list[int] , __A : list[int] ): return sum(row[i] * column[i] for i in range(len(__A ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
53
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def _a ( ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = ArgumentParser("Accelerate CLI tool" , usage="accelerate <command> [<args>]" , allow_abbrev=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : List[Any] = parser.add_subparsers(help="accelerate command helpers" ) # Register commands get_config_parser(subparsers=SCREAMING_SNAKE_CASE__ ) env_command_parser(subparsers=SCREAMING_SNAKE_CASE__ ) launch_command_parser(subparsers=SCREAMING_SNAKE_CASE__ ) tpu_command_parser(subparsers=SCREAMING_SNAKE_CASE__ ) test_command_parser(subparsers=SCREAMING_SNAKE_CASE__ ) # Let's go SCREAMING_SNAKE_CASE__ : Dict = parser.parse_args() if not hasattr(SCREAMING_SNAKE_CASE__ , "func" ): parser.print_help() exit(1 ) # Run args.func(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": main()
191
import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer _lowerCamelCase : Optional[int] = logging.get_logger(__name__) class lowerCamelCase (__lowerCamelCase ): """simple docstring""" UpperCAmelCase_ = "AutoTokenizer" UpperCAmelCase_ = ["tokenizer"] UpperCAmelCase_ = { "semantic_prompt": 1, "coarse_prompt": 2, "fine_prompt": 2, } def __init__( self : Union[str, Any], _UpperCAmelCase : Optional[int], _UpperCAmelCase : Union[str, Any]=None ) -> Union[str, Any]: """simple docstring""" super().__init__(_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = speaker_embeddings @classmethod def A_ ( cls : Any, _UpperCAmelCase : List[str], _UpperCAmelCase : Dict="speaker_embeddings_path.json", **_UpperCAmelCase : Optional[Any] ) -> List[Any]: """simple docstring""" if speaker_embeddings_dict_path is not None: SCREAMING_SNAKE_CASE__ : Any = get_file_from_repo( _UpperCAmelCase, _UpperCAmelCase, subfolder=kwargs.pop("subfolder", _UpperCAmelCase ), cache_dir=kwargs.pop("cache_dir", _UpperCAmelCase ), force_download=kwargs.pop("force_download", _UpperCAmelCase ), proxies=kwargs.pop("proxies", _UpperCAmelCase ), resume_download=kwargs.pop("resume_download", _UpperCAmelCase ), local_files_only=kwargs.pop("local_files_only", _UpperCAmelCase ), use_auth_token=kwargs.pop("use_auth_token", _UpperCAmelCase ), revision=kwargs.pop("revision", _UpperCAmelCase ), ) if speaker_embeddings_path is None: logger.warning( F'''`{os.path.join(_UpperCAmelCase, _UpperCAmelCase )}` does not exists , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.''' ) SCREAMING_SNAKE_CASE__ : Dict = None else: with open(_UpperCAmelCase ) as speaker_embeddings_json: SCREAMING_SNAKE_CASE__ : Union[str, Any] = json.load(_UpperCAmelCase ) else: SCREAMING_SNAKE_CASE__ : List[Any] = None SCREAMING_SNAKE_CASE__ : List[str] = AutoTokenizer.from_pretrained(_UpperCAmelCase, **_UpperCAmelCase ) return cls(tokenizer=_UpperCAmelCase, speaker_embeddings=_UpperCAmelCase ) def A_ ( self : str, _UpperCAmelCase : Optional[int], _UpperCAmelCase : List[str]="speaker_embeddings_path.json", _UpperCAmelCase : Optional[Any]="speaker_embeddings", _UpperCAmelCase : bool = False, **_UpperCAmelCase : List[str], ) -> Union[str, Any]: """simple docstring""" if self.speaker_embeddings is not None: os.makedirs(os.path.join(_UpperCAmelCase, _UpperCAmelCase, "v2" ), exist_ok=_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : Optional[int] = {} SCREAMING_SNAKE_CASE__ : Any = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": SCREAMING_SNAKE_CASE__ : List[Any] = self._load_voice_preset(_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : int = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict["repo_or_path"], _UpperCAmelCase, F'''{prompt_key}_{key}''' ), voice_preset[key], allow_pickle=_UpperCAmelCase, ) SCREAMING_SNAKE_CASE__ : List[str] = os.path.join(_UpperCAmelCase, F'''{prompt_key}_{key}.npy''' ) SCREAMING_SNAKE_CASE__ : Optional[int] = tmp_dict with open(os.path.join(_UpperCAmelCase, _UpperCAmelCase ), "w" ) as fp: json.dump(_UpperCAmelCase, _UpperCAmelCase ) super().save_pretrained(_UpperCAmelCase, _UpperCAmelCase, **_UpperCAmelCase ) def A_ ( self : List[Any], _UpperCAmelCase : str = None, **_UpperCAmelCase : List[Any] ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE__ : str = self.speaker_embeddings[voice_preset] SCREAMING_SNAKE_CASE__ : Optional[int] = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F'''Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].''' ) SCREAMING_SNAKE_CASE__ : List[Any] = get_file_from_repo( self.speaker_embeddings.get("repo_or_path", "/" ), voice_preset_paths[key], subfolder=kwargs.pop("subfolder", _UpperCAmelCase ), cache_dir=kwargs.pop("cache_dir", _UpperCAmelCase ), force_download=kwargs.pop("force_download", _UpperCAmelCase ), proxies=kwargs.pop("proxies", _UpperCAmelCase ), resume_download=kwargs.pop("resume_download", _UpperCAmelCase ), local_files_only=kwargs.pop("local_files_only", _UpperCAmelCase ), use_auth_token=kwargs.pop("use_auth_token", _UpperCAmelCase ), revision=kwargs.pop("revision", _UpperCAmelCase ), ) if path is None: raise ValueError( F'''`{os.path.join(self.speaker_embeddings.get("repo_or_path", "/" ), voice_preset_paths[key] )}` does not exists , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset} embeddings.''' ) SCREAMING_SNAKE_CASE__ : int = np.load(_UpperCAmelCase ) return voice_preset_dict def A_ ( self : int, _UpperCAmelCase : Optional[dict] = None ) -> Optional[int]: """simple docstring""" for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F'''Voice preset unrecognized, missing {key} as a key.''' ) if not isinstance(voice_preset[key], np.ndarray ): raise ValueError(F'''{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.''' ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F'''{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.''' ) def __call__( self : List[Any], _UpperCAmelCase : Optional[Any]=None, _UpperCAmelCase : Union[str, Any]=None, _UpperCAmelCase : Optional[int]="pt", _UpperCAmelCase : List[str]=2_5_6, _UpperCAmelCase : int=False, _UpperCAmelCase : Optional[int]=True, _UpperCAmelCase : Any=False, **_UpperCAmelCase : List[str], ) -> List[Any]: """simple docstring""" if voice_preset is not None and not isinstance(_UpperCAmelCase, _UpperCAmelCase ): if ( isinstance(_UpperCAmelCase, _UpperCAmelCase ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): SCREAMING_SNAKE_CASE__ : List[str] = self._load_voice_preset(_UpperCAmelCase ) else: if isinstance(_UpperCAmelCase, _UpperCAmelCase ) and not voice_preset.endswith(".npz" ): SCREAMING_SNAKE_CASE__ : Optional[int] = voice_preset + ".npz" SCREAMING_SNAKE_CASE__ : List[str] = np.load(_UpperCAmelCase ) if voice_preset is not None: self._validate_voice_preset_dict(_UpperCAmelCase, **_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : Any = BatchFeature(data=_UpperCAmelCase, tensor_type=_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : Dict = self.tokenizer( _UpperCAmelCase, return_tensors=_UpperCAmelCase, padding="max_length", max_length=_UpperCAmelCase, return_attention_mask=_UpperCAmelCase, return_token_type_ids=_UpperCAmelCase, add_special_tokens=_UpperCAmelCase, **_UpperCAmelCase, ) if voice_preset is not None: SCREAMING_SNAKE_CASE__ : str = voice_preset return encoded_text
191
1
def __lowercase ( a__ ) -> Union[str, Any]: if n_term == "": return [] __SCREAMING_SNAKE_CASE = [] for temp in range(int(_SCREAMING_SNAKE_CASE ) ): series.append(f"""1/{temp + 1}""" if series else '1' ) return series if __name__ == "__main__": lowerCAmelCase__ : Optional[Any] =input('''Enter the last number (nth term) of the Harmonic Series''') print('''Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n''') print(harmonic_series(nth_term))
257
"""simple docstring""" def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" if p < 2: raise ValueError("p should not be less than 2!" ) elif p == 2: return True UpperCamelCase = 4 UpperCamelCase = (1 << p) - 1 for _ in range(p - 2 ): UpperCamelCase = ((s * s) - 2) % m return s == 0 if __name__ == "__main__": print(lucas_lehmer_test(7)) print(lucas_lehmer_test(11))
153
0
import inspect import logging import os import random import shutil import tempfile import unittest import pytest import torch from torch import nn from torch.utils.data import DataLoader, TensorDataset from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_cuda from accelerate.utils import ProjectConfiguration, set_seed _snake_case = logging.getLogger(__name__) def lowerCAmelCase_ ( snake_case_=2,snake_case_=3,snake_case_=16,snake_case_ = 10,snake_case_ = 2 ): def get_dataset(snake_case_ ): _A : int = torch.randn(batch_size * n_batches,1 ) return TensorDataset(snake_case_,a * x + b + 0.1 * torch.randn(batch_size * n_batches,1 ) ) _A : int = get_dataset(snake_case_ ) _A : Union[str, Any] = get_dataset(snake_case_ ) _A : int = DataLoader(snake_case_,shuffle=snake_case_,batch_size=snake_case_,num_workers=4 ) _A : int = DataLoader(snake_case_,shuffle=snake_case_,batch_size=snake_case_,num_workers=4 ) return (train_dataloader, valid_dataloader) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_,snake_case_=None ): _A : Dict = [] for epoch in range(snake_case_ ): # Train quickly model.train() for batch in dataloader: _A , _A : Any = batch _A : Optional[int] = model(snake_case_ ) _A : str = torch.nn.functional.mse_loss(snake_case_,snake_case_ ) accelerator.backward(snake_case_ ) optimizer.step() optimizer.zero_grad() rands.append(random.random() ) # Introduce some randomness if scheduler is not None: scheduler.step() return rands class lowercase ( nn.Module ): def __init__( self ) -> Optional[int]: super().__init__() _A : List[str] = nn.Parameter(torch.randn(1 ) ) _A : Union[str, Any] = nn.Parameter(torch.randn(1 ) ) def a__ ( self , _a ) -> List[str]: return x * self.a + self.b class lowercase ( unittest.TestCase ): def a__ ( self ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) _A : List[Any] = DummyModel() _A : Optional[Any] = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) _A , _A : Union[str, Any] = dummy_dataloaders() _A : str = ProjectConfiguration(total_limit=1 , project_dir=_a , automatic_checkpoint_naming=_a ) # Train baseline _A : Union[str, Any] = Accelerator(project_config=_a ) _A , _A , _A , _A : str = accelerator.prepare( _a , _a , _a , _a ) # Save initial accelerator.save_state() # Save second state accelerator.save_state() self.assertEqual(len(os.listdir(accelerator.project_dir ) ) , 1 ) def a__ ( self ) -> List[str]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) _A : str = DummyModel() _A : Dict = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) _A , _A : Dict = dummy_dataloaders() # Train baseline _A : Tuple = Accelerator() _A , _A , _A , _A : Union[str, Any] = accelerator.prepare( _a , _a , _a , _a ) # Save initial _A : Any = os.path.join(_a , """initial""" ) accelerator.save_state(_a ) ((_A) , (_A)) : List[Any] = model.a.item(), model.b.item() _A : int = optimizer.state_dict() _A : int = train(3 , _a , _a , _a , _a ) ((_A) , (_A)) : str = model.a.item(), model.b.item() _A : Tuple = optimizer.state_dict() # Train partially set_seed(42 ) _A : Optional[Any] = DummyModel() _A : Optional[Any] = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) _A , _A : List[Any] = dummy_dataloaders() _A : Tuple = Accelerator() _A , _A , _A , _A : str = accelerator.prepare( _a , _a , _a , _a ) accelerator.load_state(_a ) ((_A) , (_A)) : str = model.a.item(), model.b.item() _A : str = optimizer.state_dict() self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) _A : int = train(2 , _a , _a , _a , _a ) # Save everything _A : Tuple = os.path.join(_a , """checkpoint""" ) accelerator.save_state(_a ) # Load everything back in and make sure all states work accelerator.load_state(_a ) test_rands += train(1 , _a , _a , _a , _a ) ((_A) , (_A)) : Dict = model.a.item(), model.b.item() _A : Tuple = optimizer.state_dict() self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) def a__ ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) _A : Optional[Any] = DummyModel() _A : List[str] = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) _A , _A : int = dummy_dataloaders() _A : str = ProjectConfiguration(automatic_checkpoint_naming=_a ) # Train baseline _A : str = Accelerator(project_dir=_a , project_config=_a ) _A , _A , _A , _A : Union[str, Any] = accelerator.prepare( _a , _a , _a , _a ) # Save initial accelerator.save_state() ((_A) , (_A)) : str = model.a.item(), model.b.item() _A : Tuple = optimizer.state_dict() _A : Any = train(3 , _a , _a , _a , _a ) ((_A) , (_A)) : Optional[int] = model.a.item(), model.b.item() _A : Any = optimizer.state_dict() # Train partially set_seed(42 ) _A : List[str] = DummyModel() _A : Any = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) _A , _A : Optional[int] = dummy_dataloaders() _A : int = ProjectConfiguration(iteration=1 , automatic_checkpoint_naming=_a ) _A : Tuple = Accelerator(project_dir=_a , project_config=_a ) _A , _A , _A , _A : int = accelerator.prepare( _a , _a , _a , _a ) accelerator.load_state(os.path.join(_a , """checkpoints""" , """checkpoint_0""" ) ) ((_A) , (_A)) : Tuple = model.a.item(), model.b.item() _A : Optional[Any] = optimizer.state_dict() self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) _A : Union[str, Any] = train(2 , _a , _a , _a , _a ) # Save everything accelerator.save_state() # Load everything back in and make sure all states work accelerator.load_state(os.path.join(_a , """checkpoints""" , """checkpoint_1""" ) ) test_rands += train(1 , _a , _a , _a , _a ) ((_A) , (_A)) : Tuple = model.a.item(), model.b.item() _A : Union[str, Any] = optimizer.state_dict() self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) self.assertEqual(_a , _a ) def a__ ( self ) -> List[Any]: _A : Tuple = torch.tensor([1, 2, 3] ) _A : List[Any] = torch.tensor([2, 3, 4] ) _A : Any = DummyModel() _A : str = torch.optim.Adam(net.parameters() ) _A : Optional[Any] = Accelerator() with self.assertRaises(_a ) as ve: accelerator.register_for_checkpointing(_a , _a , _a , _a ) _A : List[str] = str(ve.exception ) self.assertTrue("""Item at index 0""" in message ) self.assertTrue("""Item at index 1""" in message ) self.assertFalse("""Item at index 2""" in message ) self.assertFalse("""Item at index 3""" in message ) def a__ ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) _A : Dict = DummyModel() _A : int = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) _A : List[str] = torch.optim.lr_scheduler.StepLR(_a , step_size=1 , gamma=0.99 ) _A , _A : Union[str, Any] = dummy_dataloaders() _A : Optional[int] = ProjectConfiguration(automatic_checkpoint_naming=_a ) # Train baseline _A : Optional[Any] = Accelerator(project_dir=_a , project_config=_a ) _A , _A , _A , _A , _A : str = accelerator.prepare( _a , _a , _a , _a , _a ) # Save initial accelerator.save_state() _A : Union[str, Any] = scheduler.state_dict() train(3 , _a , _a , _a , _a , _a ) self.assertNotEqual(_a , scheduler.state_dict() ) # Load everything back in and make sure all states work accelerator.load_state(os.path.join(_a , """checkpoints""" , """checkpoint_0""" ) ) self.assertEqual(_a , scheduler.state_dict() ) def a__ ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) _A : List[Any] = DummyModel() _A : Dict = ProjectConfiguration(automatic_checkpoint_naming=_a , total_limit=2 ) # Train baseline _A : List[Any] = Accelerator(project_dir=_a , project_config=_a ) _A : str = accelerator.prepare(_a ) # Save 3 states: for _ in range(11 ): accelerator.save_state() self.assertTrue(not os.path.exists(os.path.join(_a , """checkpoints""" , """checkpoint_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_a , """checkpoints""" , """checkpoint_9""" ) ) ) self.assertTrue(os.path.exists(os.path.join(_a , """checkpoints""" , """checkpoint_10""" ) ) ) @require_cuda def a__ ( self ) -> Any: _A : Union[str, Any] = ["""torchrun""", F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": _snake_case = "/tmp/accelerate/state_checkpointing" _snake_case = DummyModel() _snake_case = torch.optim.Adam(params=model.parameters(), lr=1e-3) _snake_case = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.9_9) _snake_case , _snake_case = dummy_dataloaders() _snake_case = ProjectConfiguration(automatic_checkpoint_naming=True) # Train baseline _snake_case = Accelerator(project_dir=savedir, project_config=project_config, mixed_precision="no") if accelerator.process_index == 0: if os.path.exists(savedir): shutil.rmtree(savedir) os.makedirs(savedir) _snake_case , _snake_case , _snake_case , _snake_case , _snake_case = accelerator.prepare( model, optimizer, train_dataloader, valid_dataloader, scheduler ) _snake_case , _snake_case = accelerator.prepare(model, optimizer) train(3, model, train_dataloader, optimizer, accelerator, scheduler) # Check that the intial optimizer is loaded on the GPU for group in optimizer.param_groups: _snake_case = group["params"][0].device break assert param_device.type == accelerator.device.type _snake_case = model.cpu() accelerator.wait_for_everyone() accelerator.save_state() accelerator.wait_for_everyone() # Check CPU state accelerator.load_state(os.path.join(savedir, "checkpoints", "checkpoint_0"), map_location="cpu") for group in optimizer.param_groups: _snake_case = group["params"][0].device break assert ( param_device.type == torch.device("cpu").type ), f"Loaded optimizer states did not match, expected to be loaded on the CPU but got {param_device}" # Check device state model.to(accelerator.device) accelerator.load_state(os.path.join(savedir, "checkpoints", "checkpoint_0"), map_location="on_device") for group in optimizer.param_groups: _snake_case = group["params"][0].device break assert ( param_device.type == accelerator.device.type ), f"Loaded optimizer states did not match, expected to be loaded on {accelerator.device} but got {param_device}" # Check error with pytest.raises(TypeError, match="Unsupported optimizer map location passed"): accelerator.load_state(os.path.join(savedir, "checkpoints", "checkpoint_0"), map_location="invalid") accelerator.wait_for_everyone() if accelerator.process_index == 0: shutil.rmtree(savedir) accelerator.wait_for_everyone()
343
from __future__ import annotations def lowerCAmelCase_ ( snake_case_ ): create_state_space_tree(snake_case_,[],0,[0 for i in range(len(snake_case_ ) )] ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,): if index == len(snake_case_ ): print(snake_case_ ) return for i in range(len(snake_case_ ) ): if not index_used[i]: current_sequence.append(sequence[i] ) _A : Optional[Any] = True create_state_space_tree(snake_case_,snake_case_,index + 1,snake_case_ ) current_sequence.pop() _A : str = False _snake_case = [3, 1, 2, 4] generate_all_permutations(sequence) _snake_case = ["A", "B", "C"] generate_all_permutations(sequence_a)
343
1
"""simple docstring""" import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, unittest.TestCase ): """simple docstring""" __lowerCAmelCase = StableDiffusionDiffEditPipeline __lowerCAmelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"height", "width", "image"} | {"image_latents"} __lowerCAmelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"image"} | {"image_latents"} __lowerCAmelCase = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __lowerCAmelCase = frozenset([] ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: torch.manual_seed(0 ) a =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=__A , ) a =DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__A , set_alpha_to_one=__A , ) a =DDIMInverseScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__A , set_alpha_to_zero=__A , ) torch.manual_seed(0 ) a =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) a =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='''gelu''' , projection_dim=512 , ) a =CLIPTextModel(__A ) a =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) a ={ '''unet''': unet, '''scheduler''': scheduler, '''inverse_scheduler''': inverse_scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE ( self , __A , __A=0 ) -> str: a =floats_tensor((1, 16, 16) , rng=random.Random(__A ) ).to(__A ) a =floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(__A ) ).to(__A ) if str(__A ).startswith('''mps''' ): a =torch.manual_seed(__A ) else: a =torch.Generator(device=__A ).manual_seed(__A ) a ={ '''prompt''': '''a dog and a newt''', '''mask_image''': mask, '''image_latents''': latents, '''generator''': generator, '''num_inference_steps''': 2, '''inpaint_strength''': 1.0, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self , __A , __A=0 ) -> Optional[Any]: a =floats_tensor((1, 3, 32, 32) , rng=random.Random(__A ) ).to(__A ) a =image.cpu().permute(0 , 2 , 3 , 1 )[0] a =Image.fromarray(np.uinta(__A ) ).convert('''RGB''' ) if str(__A ).startswith('''mps''' ): a =torch.manual_seed(__A ) else: a =torch.Generator(device=__A ).manual_seed(__A ) a ={ '''image''': image, '''source_prompt''': '''a cat and a frog''', '''target_prompt''': '''a dog and a newt''', '''generator''': generator, '''num_inference_steps''': 2, '''num_maps_per_mask''': 2, '''mask_encode_strength''': 1.0, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self , __A , __A=0 ) -> str: a =floats_tensor((1, 3, 32, 32) , rng=random.Random(__A ) ).to(__A ) a =image.cpu().permute(0 , 2 , 3 , 1 )[0] a =Image.fromarray(np.uinta(__A ) ).convert('''RGB''' ) if str(__A ).startswith('''mps''' ): a =torch.manual_seed(__A ) else: a =torch.Generator(device=__A ).manual_seed(__A ) a ={ '''image''': image, '''prompt''': '''a cat and a frog''', '''generator''': generator, '''num_inference_steps''': 2, '''inpaint_strength''': 1.0, '''guidance_scale''': 6.0, '''decode_latents''': True, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self ) -> List[str]: if not hasattr(self.pipeline_class , '''_optional_components''' ): return a =self.get_dummy_components() a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(__A , __A , __A ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) a =self.get_dummy_inputs(__A ) a =pipe(**__A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(__A ) a =self.pipeline_class.from_pretrained(__A ) pipe_loaded.to(__A ) pipe_loaded.set_progress_bar_config(disable=__A ) for optional_component in pipe._optional_components: self.assertTrue( getattr(__A , __A ) is None , f'''`{optional_component}` did not stay set to None after loading.''' , ) a =self.get_dummy_inputs(__A ) a =pipe_loaded(**__A )[0] a =np.abs(output - output_loaded ).max() self.assertLess(__A , 1E-4 ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: a ='''cpu''' a =self.get_dummy_components() a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) a =self.get_dummy_mask_inputs(__A ) a =pipe.generate_mask(**__A ) a =mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) a =np.array([0] * 9 ) a =np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(__A , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def SCREAMING_SNAKE_CASE ( self ) -> Any: a ='''cpu''' a =self.get_dummy_components() a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) a =self.get_dummy_inversion_inputs(__A ) a =pipe.invert(**__A ).images a =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) a =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) a =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__A , 1E-3 ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: a ='''cpu''' a =self.get_dummy_components() a ={'''beta_start''': 0.00_085, '''beta_end''': 0.012, '''beta_schedule''': '''scaled_linear'''} a =DPMSolverMultistepScheduler(**__A ) a =DPMSolverMultistepInverseScheduler(**__A ) a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) a =self.get_dummy_inversion_inputs(__A ) a =pipe.invert(**__A ).images a =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) a =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) a =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__A , 1E-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self ) -> List[str]: super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def SCREAMING_SNAKE_CASE ( cls ) -> List[Any]: a =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png''' ) a =raw_image.convert('''RGB''' ).resize((768, 768) ) a =raw_image def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: a =torch.manual_seed(0 ) a =StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=__A , torch_dtype=torch.floataa ) a =DDIMScheduler.from_config(pipe.scheduler.config ) a =DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=__A ) a ='''a bowl of fruit''' a ='''a bowl of pears''' a =pipe.generate_mask( image=self.raw_image , source_prompt=__A , target_prompt=__A , generator=__A , ) a =pipe.invert( prompt=__A , image=self.raw_image , inpaint_strength=0.7 , generator=__A ).latents a =pipe( prompt=__A , mask_image=__A , image_latents=__A , generator=__A , negative_prompt=__A , inpaint_strength=0.7 , output_type='''numpy''' , ).images[0] a =( np.array( load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/diffedit/pears.png''' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1 def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: a =torch.manual_seed(0 ) a =StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=__A , torch_dtype=torch.floataa ) a =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) a =DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=__A ) a ='''a bowl of fruit''' a ='''a bowl of pears''' a =pipe.generate_mask( image=self.raw_image , source_prompt=__A , target_prompt=__A , generator=__A , ) a =pipe.invert( prompt=__A , image=self.raw_image , inpaint_strength=0.7 , generator=__A , num_inference_steps=25 , ).latents a =pipe( prompt=__A , mask_image=__A , image_latents=__A , generator=__A , negative_prompt=__A , inpaint_strength=0.7 , num_inference_steps=25 , output_type='''numpy''' , ).images[0] a =( np.array( load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/diffedit/pears.png''' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1
81
"""simple docstring""" import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) lowerCamelCase_ : Tuple = logging.get_logger(__name__) lowerCamelCase_ : str = OrderedDict( [ ("""align""", """EfficientNetImageProcessor"""), ("""beit""", """BeitImageProcessor"""), ("""bit""", """BitImageProcessor"""), ("""blip""", """BlipImageProcessor"""), ("""blip-2""", """BlipImageProcessor"""), ("""bridgetower""", """BridgeTowerImageProcessor"""), ("""chinese_clip""", """ChineseCLIPImageProcessor"""), ("""clip""", """CLIPImageProcessor"""), ("""clipseg""", """ViTImageProcessor"""), ("""conditional_detr""", """ConditionalDetrImageProcessor"""), ("""convnext""", """ConvNextImageProcessor"""), ("""convnextv2""", """ConvNextImageProcessor"""), ("""cvt""", """ConvNextImageProcessor"""), ("""data2vec-vision""", """BeitImageProcessor"""), ("""deformable_detr""", """DeformableDetrImageProcessor"""), ("""deit""", """DeiTImageProcessor"""), ("""deta""", """DetaImageProcessor"""), ("""detr""", """DetrImageProcessor"""), ("""dinat""", """ViTImageProcessor"""), ("""donut-swin""", """DonutImageProcessor"""), ("""dpt""", """DPTImageProcessor"""), ("""efficientformer""", """EfficientFormerImageProcessor"""), ("""efficientnet""", """EfficientNetImageProcessor"""), ("""flava""", """FlavaImageProcessor"""), ("""focalnet""", """BitImageProcessor"""), ("""git""", """CLIPImageProcessor"""), ("""glpn""", """GLPNImageProcessor"""), ("""groupvit""", """CLIPImageProcessor"""), ("""imagegpt""", """ImageGPTImageProcessor"""), ("""instructblip""", """BlipImageProcessor"""), ("""layoutlmv2""", """LayoutLMv2ImageProcessor"""), ("""layoutlmv3""", """LayoutLMv3ImageProcessor"""), ("""levit""", """LevitImageProcessor"""), ("""mask2former""", """Mask2FormerImageProcessor"""), ("""maskformer""", """MaskFormerImageProcessor"""), ("""mgp-str""", """ViTImageProcessor"""), ("""mobilenet_v1""", """MobileNetV1ImageProcessor"""), ("""mobilenet_v2""", """MobileNetV2ImageProcessor"""), ("""mobilevit""", """MobileViTImageProcessor"""), ("""mobilevit""", """MobileViTImageProcessor"""), ("""mobilevitv2""", """MobileViTImageProcessor"""), ("""nat""", """ViTImageProcessor"""), ("""oneformer""", """OneFormerImageProcessor"""), ("""owlvit""", """OwlViTImageProcessor"""), ("""perceiver""", """PerceiverImageProcessor"""), ("""pix2struct""", """Pix2StructImageProcessor"""), ("""poolformer""", """PoolFormerImageProcessor"""), ("""regnet""", """ConvNextImageProcessor"""), ("""resnet""", """ConvNextImageProcessor"""), ("""sam""", """SamImageProcessor"""), ("""segformer""", """SegformerImageProcessor"""), ("""swiftformer""", """ViTImageProcessor"""), ("""swin""", """ViTImageProcessor"""), ("""swin2sr""", """Swin2SRImageProcessor"""), ("""swinv2""", """ViTImageProcessor"""), ("""table-transformer""", """DetrImageProcessor"""), ("""timesformer""", """VideoMAEImageProcessor"""), ("""tvlt""", """TvltImageProcessor"""), ("""upernet""", """SegformerImageProcessor"""), ("""van""", """ConvNextImageProcessor"""), ("""videomae""", """VideoMAEImageProcessor"""), ("""vilt""", """ViltImageProcessor"""), ("""vit""", """ViTImageProcessor"""), ("""vit_hybrid""", """ViTHybridImageProcessor"""), ("""vit_mae""", """ViTImageProcessor"""), ("""vit_msn""", """ViTImageProcessor"""), ("""xclip""", """CLIPImageProcessor"""), ("""yolos""", """YolosImageProcessor"""), ] ) lowerCamelCase_ : Union[str, Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def _A ( lowercase ): """simple docstring""" for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: a =model_type_to_module_name(lowercase ) a =importlib.import_module(f'''.{module_name}''' , '''transformers.models''' ) try: return getattr(lowercase , lowercase ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(lowercase , '''__name__''' , lowercase ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. a =importlib.import_module('''transformers''' ) if hasattr(lowercase , lowercase ): return getattr(lowercase , lowercase ) return None def _A ( lowercase , lowercase = None , lowercase = False , lowercase = False , lowercase = None , lowercase = None , lowercase = None , lowercase = False , **lowercase , ): """simple docstring""" a =get_file_from_repo( lowercase , lowercase , cache_dir=lowercase , force_download=lowercase , resume_download=lowercase , proxies=lowercase , use_auth_token=lowercase , revision=lowercase , local_files_only=lowercase , ) if resolved_config_file is None: logger.info( '''Could not locate the image processor configuration file, will try to use the model config instead.''' ) return {} with open(lowercase , encoding='''utf-8''' ) as reader: return json.load(lowercase ) class __A : """simple docstring""" def __init__( self ) -> Optional[Any]: raise EnvironmentError( '''AutoImageProcessor is designed to be instantiated ''' '''using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(__A ) def SCREAMING_SNAKE_CASE ( cls , __A , **__A ) -> Dict: a =kwargs.pop('''config''' , __A ) a =kwargs.pop('''trust_remote_code''' , __A ) a =True a , a =ImageProcessingMixin.get_image_processor_dict(__A , **__A ) a =config_dict.get('''image_processor_type''' , __A ) a =None if "AutoImageProcessor" in config_dict.get('''auto_map''' , {} ): a =config_dict['''auto_map''']['''AutoImageProcessor'''] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: a =config_dict.pop('''feature_extractor_type''' , __A ) if feature_extractor_class is not None: logger.warning( '''Could not find image processor class in the image processor config or the model config. Loading''' ''' based on pattern matching with the model\'s feature extractor configuration.''' ) a =feature_extractor_class.replace('''FeatureExtractor''' , '''ImageProcessor''' ) if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): a =config_dict['''auto_map''']['''AutoFeatureExtractor'''] a =feature_extractor_auto_map.replace('''FeatureExtractor''' , '''ImageProcessor''' ) logger.warning( '''Could not find image processor auto map in the image processor config or the model config.''' ''' Loading based on pattern matching with the model\'s feature extractor configuration.''' ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(__A , __A ): a =AutoConfig.from_pretrained(__A , **__A ) # It could be in `config.image_processor_type`` a =getattr(__A , '''image_processor_type''' , __A ) if hasattr(__A , '''auto_map''' ) and "AutoImageProcessor" in config.auto_map: a =config.auto_map['''AutoImageProcessor'''] if image_processor_class is not None: a =image_processor_class_from_name(__A ) a =image_processor_auto_map is not None a =image_processor_class is not None or type(__A ) in IMAGE_PROCESSOR_MAPPING a =resolve_trust_remote_code( __A , __A , __A , __A ) if has_remote_code and trust_remote_code: a =get_class_from_dynamic_module( __A , __A , **__A ) a =kwargs.pop('''code_revision''' , __A ) if os.path.isdir(__A ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(__A , **__A ) elif image_processor_class is not None: return image_processor_class.from_dict(__A , **__A ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(__A ) in IMAGE_PROCESSOR_MAPPING: a =IMAGE_PROCESSOR_MAPPING[type(__A )] return image_processor_class.from_dict(__A , **__A ) raise ValueError( f'''Unrecognized image processor in {pretrained_model_name_or_path}. Should have a ''' f'''`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following ''' f'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def SCREAMING_SNAKE_CASE ( __A , __A ) -> Any: IMAGE_PROCESSOR_MAPPING.register(__A , __A )
81
1
'''simple docstring''' import json import sys def UpperCAmelCase ( lowerCamelCase_ :Any , lowerCamelCase_ :str ): '''simple docstring''' with open(lowerCamelCase_ , encoding="""utf-8""" ) as f: snake_case_ : Union[str, Any] = json.load(lowerCamelCase_ ) snake_case_ : List[Any] = ["""<details>""", """<summary>Show updated benchmarks!</summary>""", """ """] for benchmark_name in sorted(lowerCamelCase_ ): snake_case_ : Dict = results[benchmark_name] snake_case_ : List[Any] = benchmark_name.split("""/""" )[-1] output_md.append(F'''### Benchmark: {benchmark_file_name}''' ) snake_case_ : List[Any] = """| metric |""" snake_case_ : str = """|--------|""" snake_case_ : Tuple = """| new / old (diff) |""" for metric_name in sorted(lowerCamelCase_ ): snake_case_ : Optional[int] = benchmark_res[metric_name] snake_case_ : Any = metric_vals["""new"""] snake_case_ : Union[str, Any] = metric_vals.get("""old""" , lowerCamelCase_ ) snake_case_ : int = metric_vals.get("""diff""" , lowerCamelCase_ ) snake_case_ : Union[str, Any] = F''' {new_val:f}''' if isinstance(lowerCamelCase_ , (int, float) ) else """None""" if old_val is not None: val_str += F''' / {old_val:f}''' if isinstance(lowerCamelCase_ , (int, float) ) else "None" if dif_val is not None: val_str += F''' ({dif_val:f})''' if isinstance(lowerCamelCase_ , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append("""</details>""" ) with open(lowerCamelCase_ , """w""" , encoding="""utf-8""" ) as f: f.writelines("""\n""".join(lowerCamelCase_ ) ) if __name__ == "__main__": __A : Dict = sys.argv[1] __A : Union[str, Any] = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
8
'''simple docstring''' from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __A : Tuple = logging.get_logger(__name__) class __UpperCamelCase ( lowercase__ ): lowercase : str = ['input_values', 'padding_mask'] def __init__( self :Optional[int] ,_UpperCamelCase :int = 1 ,_UpperCamelCase :int = 2_4_0_0_0 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :float = None ,_UpperCamelCase :float = None ,**_UpperCamelCase :List[Any] ,): super().__init__(feature_size=_UpperCamelCase ,sampling_rate=_UpperCamelCase ,padding_value=_UpperCamelCase ,**_UpperCamelCase ) snake_case_ : Dict = chunk_length_s snake_case_ : str = overlap @property def a__ ( self :Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def a__ ( self :List[str] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self :Optional[Any] ,_UpperCamelCase :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,_UpperCamelCase :Optional[Union[bool, str, PaddingStrategy]] = None ,_UpperCamelCase :Optional[bool] = False ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :Optional[Union[str, TensorType]] = None ,_UpperCamelCase :Optional[int] = None ,): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""" ) if padding and truncation: raise ValueError("""Both padding and truncation were set. Make sure you only set one.""" ) elif padding is None: # by default let's pad the inputs snake_case_ : Tuple = True snake_case_ : str = bool( isinstance(_UpperCamelCase ,(list, tuple) ) and (isinstance(raw_audio[0] ,(np.ndarray, tuple, list) )) ) if is_batched: snake_case_ : Any = [np.asarray(_UpperCamelCase ,dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(_UpperCamelCase ,np.ndarray ): snake_case_ : Optional[int] = np.asarray(_UpperCamelCase ,dtype=np.floataa ) elif isinstance(_UpperCamelCase ,np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): snake_case_ : List[str] = raw_audio.astype(np.floataa ) # always return batch if not is_batched: snake_case_ : Optional[Any] = [np.asarray(_UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(_UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) snake_case_ : Tuple = None snake_case_ : Optional[Any] = BatchFeature({"""input_values""": raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: snake_case_ : Union[str, Any] = min(array.shape[0] for array in raw_audio ) snake_case_ : Dict = int(np.floor(max_length / self.chunk_stride ) ) snake_case_ : Union[str, Any] = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: snake_case_ : Any = max(array.shape[0] for array in raw_audio ) snake_case_ : List[Any] = int(np.ceil(max_length / self.chunk_stride ) ) snake_case_ : Any = (nb_step - 1) * self.chunk_stride + self.chunk_length snake_case_ : Union[str, Any] = """max_length""" else: snake_case_ : int = input_values # normal padding on batch if padded_inputs is None: snake_case_ : Optional[int] = self.pad( _UpperCamelCase ,max_length=_UpperCamelCase ,truncation=_UpperCamelCase ,padding=_UpperCamelCase ,return_attention_mask=_UpperCamelCase ,) if padding: snake_case_ : Tuple = padded_inputs.pop("""attention_mask""" ) snake_case_ : Optional[int] = [] for example in padded_inputs.pop("""input_values""" ): if self.feature_size == 1: snake_case_ : Dict = example[..., None] input_values.append(example.T ) snake_case_ : List[Any] = input_values if return_tensors is not None: snake_case_ : Tuple = padded_inputs.convert_to_tensors(_UpperCamelCase ) return padded_inputs
8
1
import argparse import logging import pickle from collections import Counter logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO ) __A : Optional[int] = logging.getLogger(__name__) if __name__ == "__main__": __A : Optional[int] = argparse.ArgumentParser( description="Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)" ) parser.add_argument( "--data_file", type=str, default="data/dump.bert-base-uncased.pickle", help="The binarized dataset." ) parser.add_argument( "--token_counts_dump", type=str, default="data/token_counts.bert-base-uncased.pickle", help="The dump file." ) parser.add_argument("--vocab_size", default=30_522, type=int) __A : List[str] = parser.parse_args() logger.info(F'Loading data from {args.data_file}') with open(args.data_file, "rb") as fp: __A : int = pickle.load(fp) logger.info("Counting occurrences for MLM.") __A : List[str] = Counter() for tk_ids in data: counter.update(tk_ids) __A : Tuple = [0] * args.vocab_size for k, v in counter.items(): __A : Union[str, Any] = v logger.info(F'Dump to {args.token_counts_dump}') with open(args.token_counts_dump, "wb") as handle: pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)
273
def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> list[int]: '''simple docstring''' if length <= 0 or not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''Length must be a positive integer.''' ) return [n * (2 * n - 1) for n in range(UpperCamelCase__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
273
1
"""simple docstring""" from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowercase__ : Optional[int] = logging.get_logger(__name__) # pylint: disable=invalid-name class _UpperCAmelCase ( lowerCAmelCase__ , lowerCAmelCase__): @register_to_config def __init__( self : List[Any] , lowercase_ : bool , lowercase_ : Optional[int] = None , lowercase_ : Optional[int] = None ): super().__init__() snake_case_ : Dict = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" snake_case_ : Tuple = torch.zeros(lowercase_ , lowercase_ ) else: snake_case_ : str = None snake_case_ : Dict = torch.nn.Parameter(lowercase_ ) class _UpperCAmelCase ( lowerCAmelCase__): _lowerCAmelCase : VQModel _lowerCAmelCase : CLIPTextModel _lowerCAmelCase : CLIPTokenizer _lowerCAmelCase : TransformeraDModel _lowerCAmelCase : LearnedClassifierFreeSamplingEmbeddings _lowerCAmelCase : VQDiffusionScheduler def __init__( self : Optional[int] , lowercase_ : VQModel , lowercase_ : CLIPTextModel , lowercase_ : CLIPTokenizer , lowercase_ : TransformeraDModel , lowercase_ : VQDiffusionScheduler , lowercase_ : LearnedClassifierFreeSamplingEmbeddings , ): super().__init__() self.register_modules( vqvae=lowercase_ , transformer=lowercase_ , text_encoder=lowercase_ , tokenizer=lowercase_ , scheduler=lowercase_ , learned_classifier_free_sampling_embeddings=lowercase_ , ) def _snake_case ( self : Tuple , lowercase_ : Optional[int] , lowercase_ : Tuple , lowercase_ : Optional[Any] ): snake_case_ : Any = len(lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else 1 # get prompt text embeddings snake_case_ : Any = self.tokenizer( lowercase_ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) snake_case_ : Dict = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: snake_case_ : Tuple = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f" {self.tokenizer.model_max_length} tokens: {removed_text}" ) snake_case_ : Any = text_input_ids[:, : self.tokenizer.model_max_length] snake_case_ : Optional[int] = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 snake_case_ : List[str] = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=lowercase_ ) # duplicate text embeddings for each generation per prompt snake_case_ : Any = prompt_embeds.repeat_interleave(lowercase_ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: snake_case_ : Union[str, Any] = self.learned_classifier_free_sampling_embeddings.embeddings snake_case_ : str = negative_prompt_embeds.unsqueeze(0 ).repeat(lowercase_ , 1 , 1 ) else: snake_case_ : List[str] = [''''''] * batch_size snake_case_ : Tuple = text_input_ids.shape[-1] snake_case_ : Union[str, Any] = self.tokenizer( lowercase_ , padding='''max_length''' , max_length=lowercase_ , truncation=lowercase_ , return_tensors='''pt''' , ) snake_case_ : List[Any] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings snake_case_ : Tuple = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=lowercase_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method snake_case_ : Optional[int] = negative_prompt_embeds.shape[1] snake_case_ : List[str] = negative_prompt_embeds.repeat(1 , lowercase_ , 1 ) snake_case_ : Tuple = negative_prompt_embeds.view(batch_size * num_images_per_prompt , lowercase_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes snake_case_ : Optional[Any] = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self : Optional[int] , lowercase_ : Union[str, List[str]] , lowercase_ : int = 100 , lowercase_ : float = 5.0 , lowercase_ : float = 1.0 , lowercase_ : int = 1 , lowercase_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowercase_ : Optional[torch.FloatTensor] = None , lowercase_ : Optional[str] = "pil" , lowercase_ : bool = True , lowercase_ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowercase_ : int = 1 , ): if isinstance(lowercase_ , lowercase_ ): snake_case_ : str = 1 elif isinstance(lowercase_ , lowercase_ ): snake_case_ : Optional[Any] = len(lowercase_ ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(lowercase_ )}" ) snake_case_ : str = batch_size * num_images_per_prompt snake_case_ : str = guidance_scale > 1.0 snake_case_ : List[str] = self._encode_prompt(lowercase_ , lowercase_ , lowercase_ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(lowercase_ , lowercase_ ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(lowercase_ )}." ) # get the initial completely masked latents unless the user supplied it snake_case_ : Optional[int] = (batch_size, self.transformer.num_latent_pixels) if latents is None: snake_case_ : Dict = self.transformer.num_vector_embeds - 1 snake_case_ : Tuple = torch.full(lowercase_ , lowercase_ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f" {self.transformer.num_vector_embeds - 1} (inclusive)." ) snake_case_ : Union[str, Any] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(lowercase_ , device=self.device ) snake_case_ : Tuple = self.scheduler.timesteps.to(self.device ) snake_case_ : List[Any] = latents for i, t in enumerate(self.progress_bar(lowercase_ ) ): # expand the sample if we are doing classifier free guidance snake_case_ : int = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` snake_case_ : int = self.transformer(lowercase_ , encoder_hidden_states=lowercase_ , timestep=lowercase_ ).sample if do_classifier_free_guidance: snake_case_ : Tuple = model_output.chunk(2 ) snake_case_ : Any = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(lowercase_ , dim=1 , keepdim=lowercase_ ) snake_case_ : Any = self.truncate(lowercase_ , lowercase_ ) # remove `log(0)`'s (`-inf`s) snake_case_ : Union[str, Any] = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 snake_case_ : int = self.scheduler.step(lowercase_ , timestep=lowercase_ , sample=lowercase_ , generator=lowercase_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(lowercase_ , lowercase_ , lowercase_ ) snake_case_ : int = self.vqvae.config.vq_embed_dim snake_case_ : Dict = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) snake_case_ : Tuple = self.vqvae.quantize.get_codebook_entry(lowercase_ , shape=lowercase_ ) snake_case_ : Optional[Any] = self.vqvae.decode(lowercase_ , force_not_quantize=lowercase_ ).sample snake_case_ : Optional[Any] = (image / 2 + 0.5).clamp(0 , 1 ) snake_case_ : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": snake_case_ : Optional[Any] = self.numpy_to_pil(lowercase_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=lowercase_ ) def _snake_case ( self : int , lowercase_ : torch.FloatTensor , lowercase_ : float ): snake_case_ : str = torch.sort(lowercase_ , 1 , descending=lowercase_ ) snake_case_ : int = torch.exp(lowercase_ ) snake_case_ : Union[str, Any] = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out snake_case_ : Optional[Any] = torch.full_like(keep_mask[:, 0:1, :] , lowercase_ ) snake_case_ : Optional[int] = torch.cat((all_true, keep_mask) , dim=1 ) snake_case_ : int = keep_mask[:, :-1, :] snake_case_ : Tuple = keep_mask.gather(1 , indices.argsort(1 ) ) snake_case_ : Optional[int] = log_p_x_0.clone() snake_case_ : Dict = -torch.inf # -inf = log(0) return rv
364
"""simple docstring""" def __lowercase ( _a ): assert column_title.isupper() snake_case_ : Any = 0 snake_case_ : Any = len(_a ) - 1 snake_case_ : Optional[Any] = 0 while index >= 0: snake_case_ : Optional[Any] = (ord(column_title[index] ) - 64) * pow(26 , _a ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
155
0
'''simple docstring''' from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class A__ ( A__ ): def __init__( self : Union[str, Any] , _a : pyspark.sql.DataFrame , _a : Optional[NamedSplit] = None , _a : Optional[Features] = None , _a : bool = True , _a : str = None , _a : bool = False , _a : str = None , _a : bool = True , _a : str = "arrow" , **_a : str , ) -> int: '''simple docstring''' super().__init__( split=_a , features=_a , cache_dir=_a , keep_in_memory=_a , streaming=_a , **_a , ) _SCREAMING_SNAKE_CASE =load_from_cache_file _SCREAMING_SNAKE_CASE =file_format _SCREAMING_SNAKE_CASE =Spark( df=_a , features=_a , cache_dir=_a , working_dir=_a , **_a , ) def A ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) _SCREAMING_SNAKE_CASE =None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=_a , file_format=self._file_format , ) return self.builder.as_dataset(split=self.split )
47
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) 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 : Optional[int] = 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.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class A__ : A__ = field( default='cifar10' , metadata={'help': 'Name of a dataset from the datasets package'} ) A__ = field( default=A__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) A__ = field( default=A__ , metadata={'help': 'The column name of the images in the files.'} ) A__ = field(default=A__ , metadata={'help': 'A folder containing the training data.'} ) A__ = field(default=A__ , metadata={'help': 'A folder containing the validation data.'} ) A__ = field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) A__ = field( default=A__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) A__ = field( default=A__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def A ( self : Union[str, Any] ) -> int: '''simple docstring''' _SCREAMING_SNAKE_CASE ={} if self.train_dir is not None: _SCREAMING_SNAKE_CASE =self.train_dir if self.validation_dir is not None: _SCREAMING_SNAKE_CASE =self.validation_dir _SCREAMING_SNAKE_CASE =data_files if data_files else None @dataclass class A__ : A__ = field( default=A__ , metadata={ 'help': ( 'The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.' ) } , ) A__ = field( default=A__ , metadata={'help': 'Pretrained config name or path if not the same as model_name_or_path'} ) A__ = field( default=A__ , metadata={ 'help': ( 'Override some existing default config settings when a model is trained from scratch. Example: ' 'n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index' ) } , ) A__ = field( default=A__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) A__ = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) A__ = field(default=A__ , metadata={'help': 'Name or path of preprocessor config.'} ) A__ = field( default=A__ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) A__ = field( default=0.75 , metadata={'help': 'The ratio of the number of masked tokens in the input sequence.'} ) A__ = field( default=A__ , metadata={'help': 'Whether or not to train with normalized pixel values as target.'} ) @dataclass class A__ ( A__ ): A__ = field( default=1E-3 , metadata={'help': 'Base learning rate: absolute_lr = base_lr * total_batch_size / 256.'} ) def _lowerCAmelCase ( _UpperCamelCase : int ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =torch.stack([example['pixel_values'] for example in examples] ) return {"pixel_values": pixel_values} def _lowerCAmelCase ( ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) 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 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _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_mae' , _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}" ) # 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 overcome.' ) 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. _SCREAMING_SNAKE_CASE =load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. _SCREAMING_SNAKE_CASE =None if 'validation' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , _UpperCamelCase ) and data_args.train_val_split > 0.0: _SCREAMING_SNAKE_CASE =ds['train'].train_test_split(data_args.train_val_split ) _SCREAMING_SNAKE_CASE =split['train'] _SCREAMING_SNAKE_CASE =split['test'] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _SCREAMING_SNAKE_CASE ={ 'cache_dir': model_args.cache_dir, 'revision': model_args.model_revision, 'use_auth_token': True if model_args.use_auth_token else None, } if model_args.config_name: _SCREAMING_SNAKE_CASE =ViTMAEConfig.from_pretrained(model_args.config_name , **_UpperCamelCase ) elif model_args.model_name_or_path: _SCREAMING_SNAKE_CASE =ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **_UpperCamelCase ) else: _SCREAMING_SNAKE_CASE =ViTMAEConfig() logger.warning('You are instantiating a new config instance from scratch.' ) if model_args.config_overrides is not None: logger.info(f"Overriding config: {model_args.config_overrides}" ) config.update_from_string(model_args.config_overrides ) logger.info(f"New config: {config}" ) # adapt config config.update( { 'mask_ratio': model_args.mask_ratio, 'norm_pix_loss': model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: _SCREAMING_SNAKE_CASE =ViTImageProcessor.from_pretrained(model_args.image_processor_name , **_UpperCamelCase ) elif model_args.model_name_or_path: _SCREAMING_SNAKE_CASE =ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **_UpperCamelCase ) else: _SCREAMING_SNAKE_CASE =ViTImageProcessor() # create model if model_args.model_name_or_path: _SCREAMING_SNAKE_CASE =ViTMAEForPreTraining.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 , ) else: logger.info('Training new model from scratch' ) _SCREAMING_SNAKE_CASE =ViTMAEForPreTraining(_UpperCamelCase ) if training_args.do_train: _SCREAMING_SNAKE_CASE =ds['train'].column_names else: _SCREAMING_SNAKE_CASE =ds['validation'].column_names if data_args.image_column_name is not None: _SCREAMING_SNAKE_CASE =data_args.image_column_name elif "image" in column_names: _SCREAMING_SNAKE_CASE ='image' elif "img" in column_names: _SCREAMING_SNAKE_CASE ='img' else: _SCREAMING_SNAKE_CASE =column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: _SCREAMING_SNAKE_CASE =image_processor.size['shortest_edge'] else: _SCREAMING_SNAKE_CASE =(image_processor.size['height'], image_processor.size['width']) _SCREAMING_SNAKE_CASE =Compose( [ Lambda(lambda _UpperCamelCase : img.convert('RGB' ) if img.mode != "RGB" else img ), RandomResizedCrop(_UpperCamelCase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(_UpperCamelCase : Dict ): _SCREAMING_SNAKE_CASE =[transforms(_UpperCamelCase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError('--do_train requires a train dataset' ) if data_args.max_train_samples is not None: _SCREAMING_SNAKE_CASE =ds['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(_UpperCamelCase ) if training_args.do_eval: if "validation" not in ds: raise ValueError('--do_eval requires a validation dataset' ) if data_args.max_eval_samples is not None: _SCREAMING_SNAKE_CASE =( ds['validation'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(_UpperCamelCase ) # Compute absolute learning rate _SCREAMING_SNAKE_CASE =( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: _SCREAMING_SNAKE_CASE =training_args.base_learning_rate * total_train_batch_size / 2_56 # Initialize our trainer _SCREAMING_SNAKE_CASE =Trainer( model=_UpperCamelCase , args=_UpperCamelCase , train_dataset=ds['train'] if training_args.do_train else None , eval_dataset=ds['validation'] if training_args.do_eval else None , tokenizer=_UpperCamelCase , data_collator=_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 ={ 'tasks': 'masked-auto-encoding', 'dataset': data_args.dataset_name, 'tags': ['masked-auto-encoding'], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) def _lowerCAmelCase ( _UpperCamelCase : List[str] ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
47
1
'''simple docstring''' from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time _lowerCAmelCase = Lock() def UpperCamelCase ( a , a , a , a , a , a , a ) -> int: '''simple docstring''' global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 10 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() __magic_name__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left __magic_name__ = min(a , a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() __magic_name__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right __magic_name__ = max(a , a ) # after all swaps are performed, send the values back to main result_pipe[1].send(a ) def UpperCamelCase ( a ) -> int: '''simple docstring''' __magic_name__ = [] __magic_name__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop __magic_name__ = Pipe() __magic_name__ = Pipe() process_array_.append( Process( target=a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) __magic_name__ = temp_rs __magic_name__ = temp_rr for i in range(1 , len(a ) - 1 ): __magic_name__ = Pipe() __magic_name__ = Pipe() process_array_.append( Process( target=a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) __magic_name__ = temp_rs __magic_name__ = temp_rr process_array_.append( Process( target=a , args=( len(a ) - 1, arr[len(a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(a ) ): __magic_name__ = result_pipe[p][0].recv() process_array_[p].join() return arr def UpperCamelCase ( ) -> List[Any]: '''simple docstring''' __magic_name__ = list(range(10 , 0 , -1 ) ) print('''Initial List''' ) print(*a ) __magic_name__ = odd_even_transposition(a ) print('''Sorted List\n''' ) print(*a ) if __name__ == "__main__": main()
98
'''simple docstring''' import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase = logging.get_logger(__name__) def UpperCamelCase ( a ) -> str: '''simple docstring''' __magic_name__ = SwinConfig.from_pretrained( '''microsoft/swin-tiny-patch4-window7-224''' , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) __magic_name__ = MaskFormerConfig(backbone_config=a ) __magic_name__ = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok __magic_name__ = 847 __magic_name__ = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok __magic_name__ = 150 __magic_name__ = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok __magic_name__ = 171 __magic_name__ = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO __magic_name__ = 133 __magic_name__ = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok __magic_name__ = 19 __magic_name__ = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok __magic_name__ = 65 __magic_name__ = '''mapillary-vistas-id2label.json''' __magic_name__ = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) __magic_name__ = {int(a ): v for k, v in idalabel.items()} return config def UpperCamelCase ( a ) -> Tuple: '''simple docstring''' __magic_name__ = [] # stem # fmt: off rename_keys.append(('''backbone.patch_embed.proj.weight''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.proj.bias''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''model.pixel_level_module.encoder.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''model.pixel_level_module.encoder.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm1.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm1.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.relative_position_index''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.proj.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.attn.proj.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm2.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.norm2.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc1.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc1.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc2.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight''') ) rename_keys.append((F'''backbone.layers.{i}.blocks.{j}.mlp.fc2.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias''') ) if i < 3: rename_keys.append((F'''backbone.layers.{i}.downsample.reduction.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight''') ) rename_keys.append((F'''backbone.layers.{i}.downsample.norm.weight''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight''') ) rename_keys.append((F'''backbone.layers.{i}.downsample.norm.bias''', F'''model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias''') ) rename_keys.append((F'''backbone.norm{i}.weight''', F'''model.pixel_level_module.encoder.hidden_states_norms.{i}.weight''') ) rename_keys.append((F'''backbone.norm{i}.bias''', F'''model.pixel_level_module.encoder.hidden_states_norms.{i}.bias''') ) # FPN rename_keys.append(('''sem_seg_head.layer_4.weight''', '''model.pixel_level_module.decoder.fpn.stem.0.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.weight''', '''model.pixel_level_module.decoder.fpn.stem.1.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.bias''', '''model.pixel_level_module.decoder.fpn.stem.1.bias''') ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((F'''sem_seg_head.adapter_{source_index}.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight''') ) rename_keys.append((F'''sem_seg_head.adapter_{source_index}.norm.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight''') ) rename_keys.append((F'''sem_seg_head.adapter_{source_index}.norm.bias''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.norm.weight''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight''') ) rename_keys.append((F'''sem_seg_head.layer_{source_index}.norm.bias''', F'''model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias''') ) rename_keys.append(('''sem_seg_head.mask_features.weight''', '''model.pixel_level_module.decoder.mask_projection.weight''') ) rename_keys.append(('''sem_seg_head.mask_features.bias''', '''model.pixel_level_module.decoder.mask_projection.bias''') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight''', F'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias''', F'''model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias''') ) # cross-attention out projection rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias''') ) # MLP 1 rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight''', F'''model.transformer_module.decoder.layers.{idx}.fc1.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias''', F'''model.transformer_module.decoder.layers.{idx}.fc1.bias''') ) # MLP 2 rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight''', F'''model.transformer_module.decoder.layers.{idx}.fc2.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias''', F'''model.transformer_module.decoder.layers.{idx}.fc2.bias''') ) # layernorm 1 (self-attention layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight''', F'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias''', F'''model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias''') ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias''', F'''model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias''') ) # layernorm 3 (final layernorm) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight''', F'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias''', F'''model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.weight''', '''model.transformer_module.decoder.layernorm.weight''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.bias''', '''model.transformer_module.decoder.layernorm.bias''') ) # heads on top rename_keys.append(('''sem_seg_head.predictor.query_embed.weight''', '''model.transformer_module.queries_embedder.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.weight''', '''model.transformer_module.input_projection.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.bias''', '''model.transformer_module.input_projection.bias''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.weight''', '''class_predictor.weight''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.bias''', '''class_predictor.bias''') ) for i in range(3 ): rename_keys.append((F'''sem_seg_head.predictor.mask_embed.layers.{i}.weight''', F'''mask_embedder.{i}.0.weight''') ) rename_keys.append((F'''sem_seg_head.predictor.mask_embed.layers.{i}.bias''', F'''mask_embedder.{i}.0.bias''') ) # fmt: on return rename_keys def UpperCamelCase ( a , a , a ) -> str: '''simple docstring''' __magic_name__ = dct.pop(a ) __magic_name__ = val def UpperCamelCase ( a , a ) -> List[str]: '''simple docstring''' __magic_name__ = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): __magic_name__ = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) __magic_name__ = state_dict.pop(F'''backbone.layers.{i}.blocks.{j}.attn.qkv.weight''' ) __magic_name__ = state_dict.pop(F'''backbone.layers.{i}.blocks.{j}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict __magic_name__ = in_proj_weight[:dim, :] __magic_name__ = in_proj_bias[: dim] __magic_name__ = in_proj_weight[ dim : dim * 2, : ] __magic_name__ = in_proj_bias[ dim : dim * 2 ] __magic_name__ = in_proj_weight[ -dim :, : ] __magic_name__ = in_proj_bias[-dim :] # fmt: on def UpperCamelCase ( a , a ) -> int: '''simple docstring''' # fmt: off __magic_name__ = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight''' ) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict __magic_name__ = in_proj_weight[: hidden_size, :] __magic_name__ = in_proj_bias[:config.hidden_size] __magic_name__ = in_proj_weight[hidden_size : hidden_size * 2, :] __magic_name__ = in_proj_bias[hidden_size : hidden_size * 2] __magic_name__ = in_proj_weight[-hidden_size :, :] __magic_name__ = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight''' ) __magic_name__ = state_dict.pop(F'''sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias''' ) # next, add query, keys and values (in that order) to the state dict __magic_name__ = in_proj_weight[: hidden_size, :] __magic_name__ = in_proj_bias[:config.hidden_size] __magic_name__ = in_proj_weight[hidden_size : hidden_size * 2, :] __magic_name__ = in_proj_bias[hidden_size : hidden_size * 2] __magic_name__ = in_proj_weight[-hidden_size :, :] __magic_name__ = in_proj_bias[-hidden_size :] # fmt: on def UpperCamelCase ( ) -> torch.Tensor: '''simple docstring''' __magic_name__ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __magic_name__ = Image.open(requests.get(a , stream=a ).raw ) return im @torch.no_grad() def UpperCamelCase ( a , a , a , a = False ) -> Dict: '''simple docstring''' __magic_name__ = get_maskformer_config(a ) # load original state_dict with open(a , '''rb''' ) as f: __magic_name__ = pickle.load(a ) __magic_name__ = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys __magic_name__ = create_rename_keys(a ) for src, dest in rename_keys: rename_key(a , a , a ) read_in_swin_q_k_v(a , config.backbone_config ) read_in_decoder_q_k_v(a , a ) # update to torch tensors for key, value in state_dict.items(): __magic_name__ = torch.from_numpy(a ) # load 🤗 model __magic_name__ = MaskFormerForInstanceSegmentation(a ) model.eval() for name, param in model.named_parameters(): print(a , param.shape ) __magic_name__ , __magic_name__ = model.load_state_dict(a , strict=a ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(a ) == 0, F'''Unexpected keys: {unexpected_keys}''' # verify results __magic_name__ = prepare_img() if "vistas" in model_name: __magic_name__ = 65 elif "cityscapes" in model_name: __magic_name__ = 6_5535 else: __magic_name__ = 255 __magic_name__ = True if '''ade''' in model_name else False __magic_name__ = MaskFormerImageProcessor(ignore_index=a , reduce_labels=a ) __magic_name__ = image_processor(a , return_tensors='''pt''' ) __magic_name__ = model(**a ) print('''Logits:''' , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": __magic_name__ = torch.tensor( [[3.63_53, -4.47_70, -2.60_65], [0.50_81, -4.23_94, -3.53_43], [2.19_09, -5.03_53, -1.93_23]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , a , atol=1e-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F'''Saving model and image processor to {pytorch_dump_folder_path}''' ) Path(a ).mkdir(exist_ok=a ) model.save_pretrained(a ) image_processor.save_pretrained(a ) if push_to_hub: print('''Pushing model and image processor to the hub...''' ) model.push_to_hub(F'''nielsr/{model_name}''' ) image_processor.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": _lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="maskformer-swin-tiny-ade", type=str, help=("Name of the MaskFormer model you'd like to convert",), ) parser.add_argument( "--checkpoint_path", default="/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl", type=str, help="Path to the original state dict (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) _lowerCAmelCase = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
98
1
'''simple docstring''' import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch UpperCamelCase_ = True except ImportError: UpperCamelCase_ = False try: from torch.hub import _get_torch_home UpperCamelCase_ = _get_torch_home() except ImportError: UpperCamelCase_ = os.path.expanduser( os.getenv("""TORCH_HOME""", os.path.join(os.getenv("""XDG_CACHE_HOME""", """~/.cache"""), """torch""")) ) UpperCamelCase_ = os.path.join(torch_cache_home, """transformers""") UpperCamelCase_ = """https://cdn.huggingface.co""" UpperCamelCase_ = """https://s3.amazonaws.com/models.huggingface.co/bert""" UpperCamelCase_ = """/""".join(str(Path(__file__).resolve()).split("""/""")[:-1]) UpperCamelCase_ = os.path.join(PATH, """config.yaml""") UpperCamelCase_ = os.path.join(PATH, """attributes.txt""") UpperCamelCase_ = os.path.join(PATH, """objects.txt""") UpperCamelCase_ = os.getenv("""PYTORCH_PRETRAINED_BERT_CACHE""", default_cache_path) UpperCamelCase_ = os.getenv("""PYTORCH_TRANSFORMERS_CACHE""", PYTORCH_PRETRAINED_BERT_CACHE) UpperCamelCase_ = os.getenv("""TRANSFORMERS_CACHE""", PYTORCH_TRANSFORMERS_CACHE) UpperCamelCase_ = """pytorch_model.bin""" UpperCamelCase_ = """config.yaml""" def _UpperCAmelCase ( _lowerCamelCase : Any=OBJECTS , _lowerCamelCase : Dict=ATTRIBUTES ) -> Optional[int]: _lowerCAmelCase : List[Any] = [] with open(_lowerCamelCase ) as f: for object in f.readlines(): vg_classes.append(object.split(""",""" )[0].lower().strip() ) _lowerCAmelCase : Union[str, Any] = [] with open(_lowerCamelCase ) as f: for object in f.readlines(): vg_attrs.append(object.split(""",""" )[0].lower().strip() ) return vg_classes, vg_attrs def _UpperCAmelCase ( _lowerCamelCase : Tuple ) -> Optional[int]: _lowerCAmelCase : Tuple = OrderedDict() with open(_lowerCamelCase , """rb""" ) as f: _lowerCAmelCase : int = pkl.load(_lowerCamelCase )["""model"""] for k in copy.deepcopy(list(ckp.keys() ) ): _lowerCAmelCase : Union[str, Any] = ckp.pop(_lowerCamelCase ) if isinstance(_lowerCamelCase , np.ndarray ): _lowerCAmelCase : List[str] = torch.tensor(_lowerCamelCase ) else: assert isinstance(_lowerCamelCase , torch.tensor ), type(_lowerCamelCase ) _lowerCAmelCase : Any = v return r class a_ : __lowerCAmelCase : Dict = {} def __init__( self , snake_case_ , snake_case_ = "root" , snake_case_=0 ): _lowerCAmelCase : int = name _lowerCAmelCase : Any = level _lowerCAmelCase : Optional[Any] = {} for k, v in dictionary.items(): if v is None: raise ValueError() _lowerCAmelCase : Union[str, Any] = copy.deepcopy(snake_case_ ) _lowerCAmelCase : List[Any] = copy.deepcopy(snake_case_ ) if isinstance(snake_case_ , snake_case_ ): _lowerCAmelCase : List[Any] = Config(snake_case_ , name=snake_case_ , level=level + 1 ) _lowerCAmelCase : str = v setattr(self , snake_case_ , snake_case_ ) _lowerCAmelCase : Optional[Any] = d def __repr__( self ): return str(list((self._pointer.keys()) ) ) def __setattr__( self , snake_case_ , snake_case_ ): _lowerCAmelCase : int = val _lowerCAmelCase : Optional[Any] = val _lowerCAmelCase : Optional[int] = key.split(""".""" ) _lowerCAmelCase : Union[str, Any] = len(snake_case_ ) - 1 _lowerCAmelCase : str = self._pointer if len(snake_case_ ) > 1: for i, l in enumerate(snake_case_ ): if hasattr(self , snake_case_ ) and isinstance(getattr(self , snake_case_ ) , snake_case_ ): setattr(getattr(self , snake_case_ ) , """.""".join(levels[i:] ) , snake_case_ ) if l == last_level: _lowerCAmelCase : Optional[Any] = val else: _lowerCAmelCase : int = pointer[l] def __UpperCamelCase ( self ): return self._pointer def __UpperCamelCase ( self , snake_case_ , snake_case_ ): with open(f'{file_name}' , """w""" ) as stream: dump(snake_case_ , snake_case_ ) def __UpperCamelCase ( self , snake_case_ , snake_case_ ): with open(f'{file_name}' , """w""" ) as stream: json.dump(snake_case_ , snake_case_ ) @staticmethod def __UpperCamelCase ( snake_case_ ): with open(snake_case_ ) as stream: _lowerCAmelCase : List[str] = load(snake_case_ , Loader=snake_case_ ) return data def __str__( self ): _lowerCAmelCase : Any = """ """ if self._name != "root": _lowerCAmelCase : int = f'{t * (self._level-1)}{self._name}:\n' else: _lowerCAmelCase : str = """""" _lowerCAmelCase : str = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(snake_case_ , snake_case_ ): r += f'{t * (self._level)}{v}\n' self._level += 1 else: r += f'{t * (self._level)}{k}: {v} ({type(snake_case_ ).__name__})\n' _lowerCAmelCase : int = level return r[:-1] @classmethod def __UpperCamelCase ( cls , snake_case_ , **snake_case_ ): _lowerCAmelCase , _lowerCAmelCase : str = cls.get_config_dict(snake_case_ , **snake_case_ ) return cls(snake_case_ ) @classmethod def __UpperCamelCase ( cls , snake_case_ , **snake_case_ ): _lowerCAmelCase : Tuple = kwargs.pop("""cache_dir""" , snake_case_ ) _lowerCAmelCase : List[str] = kwargs.pop("""force_download""" , snake_case_ ) _lowerCAmelCase : Optional[int] = kwargs.pop("""resume_download""" , snake_case_ ) _lowerCAmelCase : List[str] = kwargs.pop("""proxies""" , snake_case_ ) _lowerCAmelCase : int = kwargs.pop("""local_files_only""" , snake_case_ ) if os.path.isdir(snake_case_ ): _lowerCAmelCase : int = os.path.join(snake_case_ , snake_case_ ) elif os.path.isfile(snake_case_ ) or is_remote_url(snake_case_ ): _lowerCAmelCase : List[Any] = pretrained_model_name_or_path else: _lowerCAmelCase : int = hf_bucket_url(snake_case_ , filename=snake_case_ , use_cdn=snake_case_ ) try: # Load from URL or cache if already cached _lowerCAmelCase : Tuple = cached_path( snake_case_ , cache_dir=snake_case_ , force_download=snake_case_ , proxies=snake_case_ , resume_download=snake_case_ , local_files_only=snake_case_ , ) # Load config dict if resolved_config_file is None: raise EnvironmentError _lowerCAmelCase : Any = Config.load_yaml(snake_case_ ) except EnvironmentError: _lowerCAmelCase : List[str] = """Can't load config for""" raise EnvironmentError(snake_case_ ) if resolved_config_file == config_file: print("""loading configuration file from path""" ) else: print("""loading configuration file cache""" ) return Config.load_yaml(snake_case_ ), kwargs def _UpperCAmelCase ( _lowerCamelCase : List[str] ) -> Tuple: _lowerCAmelCase : Any = torch.load("""dump.pt""" , map_location=in_tensor.device ) _lowerCAmelCase : str = in_tensor.numpy() _lowerCAmelCase : Optional[int] = out_tensor.numpy()[0] print(na.shape , na[0, 0, :5] ) print(na.shape , na[0, 0, :5] ) assert np.allclose(_lowerCamelCase , _lowerCamelCase , rtol=0.01 , atol=0.1 ), ( f'{sum([1 for x in np.isclose(_lowerCamelCase , _lowerCamelCase , rtol=0.01 , atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*1_00:.4f} %' " element-wise mismatch" ) raise Exception("""tensors are all good""" ) # Hugging face functions below def _UpperCAmelCase ( _lowerCamelCase : Optional[int] ) -> int: _lowerCAmelCase : Optional[Any] = urlparse(_lowerCamelCase ) return parsed.scheme in ("http", "https") def _UpperCAmelCase ( _lowerCamelCase : str , _lowerCamelCase : str , _lowerCamelCase : List[str]=True ) -> str: _lowerCAmelCase : Union[str, Any] = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX _lowerCAmelCase : Optional[Any] = """/""" not in model_id if legacy_format: return f'{endpoint}/{model_id}-{filename}' else: return f'{endpoint}/{model_id}/{filename}' def _UpperCAmelCase ( _lowerCamelCase : Optional[int] , _lowerCamelCase : List[Any] , _lowerCamelCase : Dict=None , _lowerCamelCase : Dict=0 , _lowerCamelCase : str=None , ) -> str: _lowerCAmelCase : Any = """python/{}""".format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(_lowerCamelCase , _lowerCamelCase ): ua += "; " + "; ".join("""{}/{}""".format(_lowerCamelCase , _lowerCamelCase ) for k, v in user_agent.items() ) elif isinstance(_lowerCamelCase , _lowerCamelCase ): ua += "; " + user_agent _lowerCAmelCase : Optional[Any] = {"""user-agent""": ua} if resume_size > 0: _lowerCAmelCase : List[str] = """bytes=%d-""" % (resume_size,) _lowerCAmelCase : List[str] = requests.get(_lowerCamelCase , stream=_lowerCamelCase , proxies=_lowerCamelCase , headers=_lowerCamelCase ) if response.status_code == 4_16: # Range not satisfiable return _lowerCAmelCase : Tuple = response.headers.get("""Content-Length""" ) _lowerCAmelCase : List[Any] = resume_size + int(_lowerCamelCase ) if content_length is not None else None _lowerCAmelCase : int = tqdm( unit="""B""" , unit_scale=_lowerCamelCase , total=_lowerCamelCase , initial=_lowerCamelCase , desc="""Downloading""" , ) for chunk in response.iter_content(chunk_size=10_24 ): if chunk: # filter out keep-alive new chunks progress.update(len(_lowerCamelCase ) ) temp_file.write(_lowerCamelCase ) progress.close() def _UpperCAmelCase ( _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Dict=None , _lowerCamelCase : str=False , _lowerCamelCase : Tuple=None , _lowerCamelCase : List[str]=10 , _lowerCamelCase : int=False , _lowerCamelCase : Dict=None , _lowerCamelCase : List[str]=False , ) -> Dict: if cache_dir is None: _lowerCAmelCase : str = TRANSFORMERS_CACHE if isinstance(_lowerCamelCase , _lowerCamelCase ): _lowerCAmelCase : int = str(_lowerCamelCase ) os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) _lowerCAmelCase : int = None if not local_files_only: try: _lowerCAmelCase : Tuple = requests.head(_lowerCamelCase , allow_redirects=_lowerCamelCase , proxies=_lowerCamelCase , timeout=_lowerCamelCase ) if response.status_code == 2_00: _lowerCAmelCase : Tuple = response.headers.get("""ETag""" ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass _lowerCAmelCase : int = url_to_filename(_lowerCamelCase , _lowerCamelCase ) # get cache path to put the file _lowerCAmelCase : List[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(_lowerCamelCase ): return cache_path else: _lowerCAmelCase : Any = [ file for file in fnmatch.filter(os.listdir(_lowerCamelCase ) , filename + """.*""" ) if not file.endswith(""".json""" ) and not file.endswith(""".lock""" ) ] if len(_lowerCamelCase ) > 0: return os.path.join(_lowerCamelCase , matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( """Cannot find the requested files in the cached path and outgoing traffic has been""" """ disabled. To enable model look-ups and downloads online, set 'local_files_only'""" """ to False.""" ) return None # From now on, etag is not None. if os.path.exists(_lowerCamelCase ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. _lowerCAmelCase : Union[str, Any] = cache_path + """.lock""" with FileLock(_lowerCamelCase ): # If the download just completed while the lock was activated. if os.path.exists(_lowerCamelCase ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: _lowerCAmelCase : Optional[int] = cache_path + """.incomplete""" @contextmanager def _resumable_file_manager(): with open(_lowerCamelCase , """a+b""" ) as f: yield f _lowerCAmelCase : Optional[int] = _resumable_file_manager if os.path.exists(_lowerCamelCase ): _lowerCAmelCase : Dict = os.stat(_lowerCamelCase ).st_size else: _lowerCAmelCase : Tuple = 0 else: _lowerCAmelCase : Dict = partial(tempfile.NamedTemporaryFile , dir=_lowerCamelCase , delete=_lowerCamelCase ) _lowerCAmelCase : Dict = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( """%s not found in cache or force_download set to True, downloading to %s""" , _lowerCamelCase , temp_file.name , ) http_get( _lowerCamelCase , _lowerCamelCase , proxies=_lowerCamelCase , resume_size=_lowerCamelCase , user_agent=_lowerCamelCase , ) os.replace(temp_file.name , _lowerCamelCase ) _lowerCAmelCase : List[str] = {"""url""": url, """etag""": etag} _lowerCAmelCase : Optional[Any] = cache_path + """.json""" with open(_lowerCamelCase , """w""" ) as meta_file: json.dump(_lowerCamelCase , _lowerCamelCase ) return cache_path def _UpperCAmelCase ( _lowerCamelCase : Dict , _lowerCamelCase : int=None ) -> List[str]: _lowerCAmelCase : Dict = url.encode("""utf-8""" ) _lowerCAmelCase : str = shaaaa(_lowerCamelCase ) _lowerCAmelCase : List[Any] = url_hash.hexdigest() if etag: _lowerCAmelCase : Optional[int] = etag.encode("""utf-8""" ) _lowerCAmelCase : str = shaaaa(_lowerCamelCase ) filename += "." + etag_hash.hexdigest() if url.endswith(""".h5""" ): filename += ".h5" return filename def _UpperCAmelCase ( _lowerCamelCase : Tuple , _lowerCamelCase : Any=None , _lowerCamelCase : str=False , _lowerCamelCase : List[str]=None , _lowerCamelCase : Union[str, Any]=False , _lowerCamelCase : Any=None , _lowerCamelCase : Optional[Any]=False , _lowerCamelCase : Tuple=False , _lowerCamelCase : Any=False , ) -> List[str]: if cache_dir is None: _lowerCAmelCase : Union[str, Any] = TRANSFORMERS_CACHE if isinstance(_lowerCamelCase , _lowerCamelCase ): _lowerCAmelCase : Any = str(_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ): _lowerCAmelCase : List[str] = str(_lowerCamelCase ) if is_remote_url(_lowerCamelCase ): # URL, so get it from the cache (downloading if necessary) _lowerCAmelCase : Dict = get_from_cache( _lowerCamelCase , cache_dir=_lowerCamelCase , force_download=_lowerCamelCase , proxies=_lowerCamelCase , resume_download=_lowerCamelCase , user_agent=_lowerCamelCase , local_files_only=_lowerCamelCase , ) elif os.path.exists(_lowerCamelCase ): # File, and it exists. _lowerCAmelCase : Dict = url_or_filename elif urlparse(_lowerCamelCase ).scheme == "": # File, but it doesn't exist. raise EnvironmentError("""file {} not found""".format(_lowerCamelCase ) ) else: # Something unknown raise ValueError("""unable to parse {} as a URL or as a local path""".format(_lowerCamelCase ) ) if extract_compressed_file: if not is_zipfile(_lowerCamelCase ) and not tarfile.is_tarfile(_lowerCamelCase ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" _lowerCAmelCase , _lowerCAmelCase : Tuple = os.path.split(_lowerCamelCase ) _lowerCAmelCase : Any = output_file.replace(""".""" , """-""" ) + """-extracted""" _lowerCAmelCase : Union[str, Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) if os.path.isdir(_lowerCamelCase ) and os.listdir(_lowerCamelCase ) and not force_extract: return output_path_extracted # Prevent parallel extractions _lowerCAmelCase : Union[str, Any] = output_path + """.lock""" with FileLock(_lowerCamelCase ): shutil.rmtree(_lowerCamelCase , ignore_errors=_lowerCamelCase ) os.makedirs(_lowerCamelCase ) if is_zipfile(_lowerCamelCase ): with ZipFile(_lowerCamelCase , """r""" ) as zip_file: zip_file.extractall(_lowerCamelCase ) zip_file.close() elif tarfile.is_tarfile(_lowerCamelCase ): _lowerCAmelCase : Any = tarfile.open(_lowerCamelCase ) tar_file.extractall(_lowerCamelCase ) tar_file.close() else: raise EnvironmentError("""Archive format of {} could not be identified""".format(_lowerCamelCase ) ) return output_path_extracted return output_path def _UpperCAmelCase ( _lowerCamelCase : Any , _lowerCamelCase : int="," ) -> Tuple: assert isinstance(_lowerCamelCase , _lowerCamelCase ) if os.path.isfile(_lowerCamelCase ): with open(_lowerCamelCase ) as f: _lowerCAmelCase : int = eval(f.read() ) else: _lowerCAmelCase : Tuple = requests.get(_lowerCamelCase ) try: _lowerCAmelCase : List[str] = requests.json() except Exception: _lowerCAmelCase : int = req.content.decode() assert data is not None, "could not connect" try: _lowerCAmelCase : Tuple = eval(_lowerCamelCase ) except Exception: _lowerCAmelCase : Optional[Any] = data.split("""\n""" ) req.close() return data def _UpperCAmelCase ( _lowerCamelCase : Tuple ) -> Optional[int]: _lowerCAmelCase : List[Any] = requests.get(_lowerCamelCase ) _lowerCAmelCase : Optional[Any] = np.array(Image.open(BytesIO(response.content ) ) ) return img def _UpperCAmelCase ( _lowerCamelCase : Tuple ) -> int: _lowerCAmelCase : Optional[int] = url.split("""/""" )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(_lowerCamelCase ) with open(_lowerCamelCase , """rb""" ) as stream: _lowerCAmelCase : int = pkl.load(_lowerCamelCase ) _lowerCAmelCase : Optional[Any] = weights.pop("""model""" ) _lowerCAmelCase : List[str] = {} for k, v in model.items(): _lowerCAmelCase : Optional[Any] = torch.from_numpy(_lowerCamelCase ) if "running_var" in k: _lowerCAmelCase : int = torch.tensor([0] ) _lowerCAmelCase : str = k.replace("""running_var""" , """num_batches_tracked""" ) _lowerCAmelCase : Tuple = zero return new def _UpperCAmelCase ( ) -> int: print(f'{os.path.abspath(os.path.join(_lowerCamelCase , os.pardir ) )}/demo.ipynb' ) def _UpperCAmelCase ( _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Dict="RGB" ) -> Optional[int]: assert isinstance(_lowerCamelCase , _lowerCamelCase ) if os.path.isfile(_lowerCamelCase ): _lowerCAmelCase : Union[str, Any] = cva.imread(_lowerCamelCase ) else: _lowerCAmelCase : str = get_image_from_url(_lowerCamelCase ) assert img is not None, f'could not connect to: {im}' _lowerCAmelCase : Any = cva.cvtColor(_lowerCamelCase , cva.COLOR_BGR2RGB ) if input_format == "RGB": _lowerCAmelCase : Tuple = img[:, :, ::-1] return img def _UpperCAmelCase ( _lowerCamelCase : int , _lowerCamelCase : Union[str, Any]=1 ) -> List[str]: return (images[i : i + batch] for i in range(0 , len(_lowerCamelCase ) , _lowerCamelCase ))
309
'''simple docstring''' from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch("""socket.socket""" ) @patch("""builtins.open""" ) def _UpperCAmelCase ( _lowerCamelCase : Dict , _lowerCamelCase : Optional[int] ) -> Union[str, Any]: # ===== initialization ===== _lowerCAmelCase : Tuple = Mock() _lowerCAmelCase : Any = conn, Mock() _lowerCAmelCase : Optional[Any] = iter([1, None] ) _lowerCAmelCase : str = lambda _lowerCamelCase : next(_lowerCamelCase ) # ===== invoke ===== send_file(filename="""mytext.txt""" , testing=_lowerCamelCase ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
309
1
"""simple docstring""" import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConfig, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaForPreTraining, WavaVecaProcessor, logging, ) from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'adapter_layer': 'encoder.layers.*.adapter_layer', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', 'pooling_layer.linear': 'projector', 'pooling_layer.projection': 'classifier', } UpperCamelCase = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'projector', 'classifier', ] def _A ( lowerCAmelCase_ : Optional[int] ): """simple docstring""" lowerCAmelCase__ = {} with open(lowerCAmelCase_ , "r" ) as file: for line_number, line in enumerate(lowerCAmelCase_ ): lowerCAmelCase__ = line.strip() if line: lowerCAmelCase__ = line.split() lowerCAmelCase__ = line_number lowerCAmelCase__ = words[0] lowerCAmelCase__ = value return result def _A ( lowerCAmelCase_ : Any , lowerCAmelCase_ : int , lowerCAmelCase_ : int , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : int ): """simple docstring""" for attribute in key.split("." ): lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(lowerCAmelCase_ ): lowerCAmelCase__ = PARAM_MAPPING[full_name.split("." )[-1]] lowerCAmelCase__ = "param" if weight_type is not None and weight_type != "param": lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ).shape elif weight_type is not None and weight_type == "param": lowerCAmelCase__ = hf_pointer for attribute in hf_param_name.split("." ): lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = shape_pointer.shape # let's reduce dimension lowerCAmelCase__ = value[0] else: lowerCAmelCase__ = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be' F' {value.shape} for {full_name}' ) if weight_type == "weight": lowerCAmelCase__ = value elif weight_type == "weight_g": lowerCAmelCase__ = value elif weight_type == "weight_v": lowerCAmelCase__ = value elif weight_type == "bias": lowerCAmelCase__ = value elif weight_type == "param": for attribute in hf_param_name.split("." ): lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = value else: lowerCAmelCase__ = value logger.info(F'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' ) def _A ( lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : int , lowerCAmelCase_ : Optional[Any] ): """simple docstring""" lowerCAmelCase__ = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(lowerCAmelCase_ ): lowerCAmelCase__ = PARAM_MAPPING[full_name.split("." )[-1]] lowerCAmelCase__ = "param" if weight_type is not None and weight_type != "param": lowerCAmelCase__ = ".".join([key, weight_type] ) elif weight_type is not None and weight_type == "param": lowerCAmelCase__ = ".".join([key, hf_param_name] ) else: lowerCAmelCase__ = key lowerCAmelCase__ = value if "lm_head" in full_key else value[0] UpperCamelCase = { 'W_a': 'linear_1.weight', 'W_b': 'linear_2.weight', 'b_a': 'linear_1.bias', 'b_b': 'linear_2.bias', 'ln_W': 'norm.weight', 'ln_b': 'norm.bias', } def _A ( lowerCAmelCase_ : int , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : str=None , lowerCAmelCase_ : Any=None ): """simple docstring""" lowerCAmelCase__ = False for key, mapped_key in MAPPING.items(): lowerCAmelCase__ = "wav2vec2." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: lowerCAmelCase__ = True if "*" in mapped_key: lowerCAmelCase__ = name.split(lowerCAmelCase_ )[0].split("." )[-2] lowerCAmelCase__ = mapped_key.replace("*" , lowerCAmelCase_ ) if "weight_g" in name: lowerCAmelCase__ = "weight_g" elif "weight_v" in name: lowerCAmelCase__ = "weight_v" elif "bias" in name: lowerCAmelCase__ = "bias" elif "weight" in name: # TODO: don't match quantizer.weight_proj lowerCAmelCase__ = "weight" else: lowerCAmelCase__ = None if hf_dict is not None: rename_dict(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) else: set_recursively(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) return is_used return is_used def _A ( lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Dict ): """simple docstring""" lowerCAmelCase__ = [] lowerCAmelCase__ = fairseq_model.state_dict() lowerCAmelCase__ = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): lowerCAmelCase__ = False if "conv_layers" in name: load_conv_layer( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , hf_model.config.feat_extract_norm == "group" , ) lowerCAmelCase__ = True else: lowerCAmelCase__ = load_wavaveca_layer(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) if not is_used: unused_weights.append(lowerCAmelCase_ ) logger.warning(F'Unused weights: {unused_weights}' ) def _A ( lowerCAmelCase_ : str , lowerCAmelCase_ : Dict , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Any , lowerCAmelCase_ : Optional[int] ): """simple docstring""" lowerCAmelCase__ = full_name.split("conv_layers." )[-1] lowerCAmelCase__ = name.split("." ) lowerCAmelCase__ = int(items[0] ) lowerCAmelCase__ = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(lowerCAmelCase_ ) @torch.no_grad() def _A ( lowerCAmelCase_ : str , lowerCAmelCase_ : Dict , lowerCAmelCase_ : List[Any]=None , lowerCAmelCase_ : Optional[Any]=None , lowerCAmelCase_ : Optional[int]=True , lowerCAmelCase_ : List[str]=False ): """simple docstring""" if config_path is not None: lowerCAmelCase__ = WavaVecaConfig.from_pretrained(lowerCAmelCase_ ) else: lowerCAmelCase__ = WavaVecaConfig() if is_seq_class: lowerCAmelCase__ = read_txt_into_dict(lowerCAmelCase_ ) lowerCAmelCase__ = idalabel lowerCAmelCase__ = WavaVecaForSequenceClassification(lowerCAmelCase_ ) lowerCAmelCase__ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , ) feature_extractor.save_pretrained(lowerCAmelCase_ ) elif is_finetuned: if dict_path: lowerCAmelCase__ = Dictionary.load(lowerCAmelCase_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq lowerCAmelCase__ = target_dict.pad_index lowerCAmelCase__ = target_dict.bos_index lowerCAmelCase__ = target_dict.eos_index lowerCAmelCase__ = len(target_dict.symbols ) lowerCAmelCase__ = os.path.join(lowerCAmelCase_ , "vocab.json" ) if not os.path.isdir(lowerCAmelCase_ ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(lowerCAmelCase_ ) ) return os.makedirs(lowerCAmelCase_ , exist_ok=lowerCAmelCase_ ) lowerCAmelCase__ = target_dict.indices # fairseq has the <pad> and <s> switched lowerCAmelCase__ = 0 lowerCAmelCase__ = 1 with open(lowerCAmelCase_ , "w" , encoding="utf-8" ) as vocab_handle: json.dump(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = WavaVecaCTCTokenizer( lowerCAmelCase_ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=lowerCAmelCase_ , ) lowerCAmelCase__ = True if config.feat_extract_norm == "layer" else False lowerCAmelCase__ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , ) lowerCAmelCase__ = WavaVecaProcessor(feature_extractor=lowerCAmelCase_ , tokenizer=lowerCAmelCase_ ) processor.save_pretrained(lowerCAmelCase_ ) lowerCAmelCase__ = WavaVecaForCTC(lowerCAmelCase_ ) else: lowerCAmelCase__ = WavaVecaForPreTraining(lowerCAmelCase_ ) if is_finetuned or is_seq_class: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: lowerCAmelCase__ = argparse.Namespace(task="audio_pretraining" ) lowerCAmelCase__ = fairseq.tasks.setup_task(lowerCAmelCase_ ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=lowerCAmelCase_ ) lowerCAmelCase__ = model[0].eval() recursively_load_weights(lowerCAmelCase_ , lowerCAmelCase_ , not is_finetuned ) hf_wavavec.save_pretrained(lowerCAmelCase_ ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) parser.add_argument( '--is_seq_class', action='store_true', help='Whether the model to convert is a fine-tuned sequence classification model or not', ) UpperCamelCase = parser.parse_args() UpperCamelCase = not args.not_finetuned and not args.is_seq_class convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, is_finetuned, args.is_seq_class, )
359
import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder UpperCamelCase = '__DUMMY_TRANSFORMERS_USER__' UpperCamelCase = 'Dummy User' UpperCamelCase = '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' UpperCamelCase = 'https://hub-ci.huggingface.co' UpperCamelCase = CI_HUB_ENDPOINT + '/datasets/{repo_id}/resolve/{revision}/{path}' UpperCamelCase = CI_HUB_ENDPOINT + '/{repo_id}/resolve/{revision}/{filename}' UpperCamelCase = Path('~/.huggingface/hub_ci_token').expanduser() @pytest.fixture def _A ( lowerCAmelCase_ : Dict ): """simple docstring""" monkeypatch.setattr( "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE" , lowerCAmelCase_ ) @pytest.fixture def _A ( lowerCAmelCase_ : int ): """simple docstring""" monkeypatch.setattr("datasets.config.HF_ENDPOINT" , lowerCAmelCase_ ) monkeypatch.setattr("datasets.config.HUB_DATASETS_URL" , lowerCAmelCase_ ) @pytest.fixture def _A ( lowerCAmelCase_ : str ): """simple docstring""" monkeypatch.setattr("huggingface_hub.hf_api.HfFolder.path_token" , lowerCAmelCase_ ) @pytest.fixture def _A ( lowerCAmelCase_ : int , lowerCAmelCase_ : List[Any] ): """simple docstring""" HfFolder.save_token(lowerCAmelCase_ ) yield HfFolder.delete_token() @pytest.fixture(scope="session" ) def _A ( ): """simple docstring""" return HfApi(endpoint=lowerCAmelCase_ ) @pytest.fixture(scope="session" ) def _A ( lowerCAmelCase_ : HfApi ): """simple docstring""" lowerCAmelCase__ = HfFolder.get_token() HfFolder.save_token(lowerCAmelCase_ ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(lowerCAmelCase_ ) @pytest.fixture def _A ( lowerCAmelCase_ : Tuple ): """simple docstring""" def _cleanup_repo(lowerCAmelCase_ : Optional[Any] ): hf_api.delete_repo(lowerCAmelCase_ , token=lowerCAmelCase_ , repo_type="dataset" ) return _cleanup_repo @pytest.fixture def _A ( lowerCAmelCase_ : Union[str, Any] ): """simple docstring""" @contextmanager def _temporary_repo(lowerCAmelCase_ : str ): try: yield repo_id finally: cleanup_repo(lowerCAmelCase_ ) return _temporary_repo @pytest.fixture(scope="session" ) def _A ( lowerCAmelCase_ : HfApi , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Any ): """simple docstring""" lowerCAmelCase__ = F'repo_txt_data-{int(time.time() * 1_0E3 )}' lowerCAmelCase__ = F'{CI_HUB_USER}/{repo_name}' hf_api.create_repo(lowerCAmelCase_ , token=lowerCAmelCase_ , repo_type="dataset" , private=lowerCAmelCase_ ) hf_api.upload_file( token=lowerCAmelCase_ , path_or_fileobj=str(lowerCAmelCase_ ) , path_in_repo="data/text_data.txt" , repo_id=lowerCAmelCase_ , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(lowerCAmelCase_ , token=lowerCAmelCase_ , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def _A ( lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : int ): """simple docstring""" return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="session" ) def _A ( lowerCAmelCase_ : HfApi , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Tuple ): """simple docstring""" lowerCAmelCase__ = F'repo_zipped_txt_data-{int(time.time() * 1_0E3 )}' lowerCAmelCase__ = F'{CI_HUB_USER}/{repo_name}' hf_api.create_repo(lowerCAmelCase_ , token=lowerCAmelCase_ , repo_type="dataset" , private=lowerCAmelCase_ ) hf_api.upload_file( token=lowerCAmelCase_ , path_or_fileobj=str(lowerCAmelCase_ ) , path_in_repo="data.zip" , repo_id=lowerCAmelCase_ , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(lowerCAmelCase_ , token=lowerCAmelCase_ , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def _A ( lowerCAmelCase_ : int , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : Tuple ): """simple docstring""" return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="session" ) def _A ( lowerCAmelCase_ : HfApi , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : int ): """simple docstring""" lowerCAmelCase__ = F'repo_zipped_img_data-{int(time.time() * 1_0E3 )}' lowerCAmelCase__ = F'{CI_HUB_USER}/{repo_name}' hf_api.create_repo(lowerCAmelCase_ , token=lowerCAmelCase_ , repo_type="dataset" , private=lowerCAmelCase_ ) hf_api.upload_file( token=lowerCAmelCase_ , path_or_fileobj=str(lowerCAmelCase_ ) , path_in_repo="data.zip" , repo_id=lowerCAmelCase_ , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(lowerCAmelCase_ , token=lowerCAmelCase_ , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def _A ( lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Dict , lowerCAmelCase_ : List[str] ): """simple docstring""" return hf_private_dataset_repo_zipped_img_data_
221
0
"""simple docstring""" from itertools import permutations def lowerCamelCase_ (UpperCamelCase__ : tuple ): if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False _UpperCAmelCase : Optional[Any] = [7, 11, 13, 17] for i, test in enumerate(UpperCamelCase__ ): if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def lowerCamelCase_ (UpperCamelCase__ : int = 10 ): return sum( int(''''''.join(map(UpperCamelCase__ , UpperCamelCase__ ) ) ) for num in permutations(range(UpperCamelCase__ ) ) if is_substring_divisible(UpperCamelCase__ ) ) if __name__ == "__main__": print(f"{solution() = }")
263
"""simple docstring""" def lowerCamelCase_ (UpperCamelCase__ : int , UpperCamelCase__ : int ): if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) _UpperCAmelCase : List[str] = str(bin(UpperCamelCase__ ) )[2:] # remove the leading "0b" _UpperCAmelCase : str = str(bin(UpperCamelCase__ ) )[2:] _UpperCAmelCase : List[str] = max(len(UpperCamelCase__ ) , len(UpperCamelCase__ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(UpperCamelCase__ ) , b_binary.zfill(UpperCamelCase__ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
263
1
'''simple docstring''' _lowercase = """ # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git """ _lowercase = [{"""type""": """code""", """content""": INSTALL_CONTENT}] _lowercase = { """{processor_class}""": """FakeProcessorClass""", """{model_class}""": """FakeModelClass""", """{object_class}""": """FakeObjectClass""", }
229
'''simple docstring''' from __future__ import annotations from typing import Any class UpperCAmelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class UpperCAmelCase_ : '''simple docstring''' def __init__( self , _lowercase ): """simple docstring""" _lowerCAmelCase = data _lowerCAmelCase = None def __iter__( self ): """simple docstring""" _lowerCAmelCase = self _lowerCAmelCase = [] while node: if node in visited: raise ContainsLoopError visited.append(_lowercase ) yield node.data _lowerCAmelCase = node.next_node @property def _lowercase ( self ): """simple docstring""" try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": _lowercase = Node(1) _lowercase = Node(2) _lowercase = Node(3) _lowercase = Node(4) print(root_node.has_loop) # False _lowercase = root_node.next_node print(root_node.has_loop) # True _lowercase = Node(5) _lowercase = Node(6) _lowercase = Node(5) _lowercase = Node(6) print(root_node.has_loop) # False _lowercase = Node(1) print(root_node.has_loop) # False
229
1
"""simple docstring""" from __future__ import annotations def snake_case ( A__ ,A__ ): UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = set(A__ ), [start] while stack: UpperCAmelCase_ : Union[str, Any] = stack.pop() explored.add(A__ ) # Differences from BFS: # 1) pop last element instead of first one # 2) add adjacent elements to stack without exploring them for adj in reversed(graph[v] ): if adj not in explored: stack.append(A__ ) return explored lowerCamelCase_ = { 'A': ['B', 'C', 'D'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['B', 'D'], 'E': ['B', 'F'], 'F': ['C', 'E', 'G'], 'G': ['F'], } if __name__ == "__main__": import doctest doctest.testmod() print(depth_first_search(G, '''A'''))
268
"""simple docstring""" def a_ ( lowerCamelCase ): return str(lowerCamelCase ) == str(lowerCamelCase )[::-1] def a_ ( lowerCamelCase ): return int(lowerCamelCase ) + int(str(lowerCamelCase )[::-1] ) def a_ ( lowerCamelCase = 1_0_0_0_0 ): UpperCAmelCase__ = [] for num in range(1 , lowerCamelCase ): UpperCAmelCase__ = 0 UpperCAmelCase__ = num while iterations < 5_0: UpperCAmelCase__ = sum_reverse(lowerCamelCase ) iterations += 1 if is_palindrome(lowerCamelCase ): break else: lychrel_nums.append(lowerCamelCase ) return len(lowerCamelCase ) if __name__ == "__main__": print(F"""{solution() = }""")
98
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 lowerCAmelCase__( __lowercase ): '''simple docstring''' def UpperCamelCase_ ( self ) -> Optional[int]: _SCREAMING_SNAKE_CASE : List[Any] = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__lowerCamelCase , "tf_padding" ) ) self.parent.assertTrue(hasattr(__lowerCamelCase , "depth_multiplier" ) ) class lowerCAmelCase__: '''simple docstring''' def __init__( self , __lowerCamelCase , __lowerCamelCase=1_3 , __lowerCamelCase=3 , __lowerCamelCase=3_2 , __lowerCamelCase=0.25 , __lowerCamelCase=8 , __lowerCamelCase=True , __lowerCamelCase=1_0_2_4 , __lowerCamelCase=3_2 , __lowerCamelCase="relu6" , __lowerCamelCase=0.1 , __lowerCamelCase=0.02 , __lowerCamelCase=True , __lowerCamelCase=True , __lowerCamelCase=1_0 , __lowerCamelCase=None , ) -> int: _SCREAMING_SNAKE_CASE : Any = parent _SCREAMING_SNAKE_CASE : Optional[int] = batch_size _SCREAMING_SNAKE_CASE : int = num_channels _SCREAMING_SNAKE_CASE : Any = image_size _SCREAMING_SNAKE_CASE : Optional[Any] = depth_multiplier _SCREAMING_SNAKE_CASE : Optional[Any] = min_depth _SCREAMING_SNAKE_CASE : Union[str, Any] = tf_padding _SCREAMING_SNAKE_CASE : List[Any] = int(last_hidden_size * depth_multiplier ) _SCREAMING_SNAKE_CASE : Dict = output_stride _SCREAMING_SNAKE_CASE : Any = hidden_act _SCREAMING_SNAKE_CASE : Any = classifier_dropout_prob _SCREAMING_SNAKE_CASE : int = use_labels _SCREAMING_SNAKE_CASE : Optional[Any] = is_training _SCREAMING_SNAKE_CASE : Dict = num_labels _SCREAMING_SNAKE_CASE : Optional[int] = initializer_range _SCREAMING_SNAKE_CASE : Optional[int] = scope def UpperCamelCase_ ( self ) -> Dict: _SCREAMING_SNAKE_CASE : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE : List[Any] = None _SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: _SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) _SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _SCREAMING_SNAKE_CASE : int = self.get_config() return config, pixel_values, labels, pixel_labels def UpperCamelCase_ ( self ) -> List[str]: 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 UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Optional[Any]: _SCREAMING_SNAKE_CASE : Optional[int] = MobileNetVaModel(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() _SCREAMING_SNAKE_CASE : Dict = model(__lowerCamelCase ) 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 UpperCamelCase_ ( self , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Optional[int]: _SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels _SCREAMING_SNAKE_CASE : int = MobileNetVaForImageClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() _SCREAMING_SNAKE_CASE : int = model(__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self ) -> str: _SCREAMING_SNAKE_CASE : List[Any] = self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE : List[str] = config_and_inputs _SCREAMING_SNAKE_CASE : int = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCAmelCase__( __lowercase , __lowercase , unittest.TestCase ): '''simple docstring''' __snake_case = (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else () __snake_case = ( {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification} if is_torch_available() else {} ) __snake_case = False __snake_case = False __snake_case = False __snake_case = False def UpperCamelCase_ ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE : List[Any] = MobileNetVaModelTester(self ) _SCREAMING_SNAKE_CASE : Optional[Any] = MobileNetVaConfigTester(self , config_class=__lowerCamelCase , has_text_modality=__lowerCamelCase ) def UpperCamelCase_ ( self ) -> str: self.config_tester.run_common_tests() @unittest.skip(reason="MobileNetV1 does not use inputs_embeds" ) def UpperCamelCase_ ( self ) -> Optional[int]: pass @unittest.skip(reason="MobileNetV1 does not support input and output embeddings" ) def UpperCamelCase_ ( self ) -> Any: pass @unittest.skip(reason="MobileNetV1 does not output attentions" ) def UpperCamelCase_ ( self ) -> Any: pass def UpperCamelCase_ ( self ) -> Tuple: _SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE : Any = model_class(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Union[str, Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE : Optional[int] = [*signature.parameters.keys()] _SCREAMING_SNAKE_CASE : str = ["pixel_values"] self.assertListEqual(arg_names[:1] , __lowerCamelCase ) def UpperCamelCase_ ( self ) -> Optional[Any]: _SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCamelCase ) def UpperCamelCase_ ( self ) -> Optional[int]: def check_hidden_states_output(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ): _SCREAMING_SNAKE_CASE : Optional[Any] = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE : str = model(**self._prepare_for_class(__lowerCamelCase , __lowerCamelCase ) ) _SCREAMING_SNAKE_CASE : Optional[Any] = outputs.hidden_states _SCREAMING_SNAKE_CASE : Optional[Any] = 2_6 self.assertEqual(len(__lowerCamelCase ) , __lowerCamelCase ) _SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE : Union[str, Any] = True check_hidden_states_output(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE : str = True check_hidden_states_output(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def UpperCamelCase_ ( self ) -> int: _SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase ) @slow def UpperCamelCase_ ( self ) -> Dict: for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE : Dict = MobileNetVaModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) def lowerCamelCase__ (): _SCREAMING_SNAKE_CASE : str = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowerCAmelCase__( unittest.TestCase ): '''simple docstring''' @cached_property def UpperCamelCase_ ( self ) -> Tuple: return ( MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224" ) if is_vision_available() else None ) @slow def UpperCamelCase_ ( self ) -> str: _SCREAMING_SNAKE_CASE : Optional[int] = MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224" ).to(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[str] = self.default_image_processor _SCREAMING_SNAKE_CASE : str = prepare_img() _SCREAMING_SNAKE_CASE : List[str] = image_processor(images=__lowerCamelCase , return_tensors="pt" ).to(__lowerCamelCase ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE : int = model(**__lowerCamelCase ) # verify the logits _SCREAMING_SNAKE_CASE : Tuple = torch.Size((1, 1_0_0_1) ) self.assertEqual(outputs.logits.shape , __lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[str] = torch.tensor([-4.1739, -1.1233, 3.1205] ).to(__lowerCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , __lowerCamelCase , atol=1E-4 ) )
369
from math import isqrt, loga def lowerCamelCase__ (__lowerCamelCase ): _SCREAMING_SNAKE_CASE : List[Any] = [True] * max_number for i in range(2, isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2, __lowerCamelCase, __lowerCamelCase ): _SCREAMING_SNAKE_CASE : Optional[Any] = False return [i for i in range(2, __lowerCamelCase ) if is_prime[i]] def lowerCamelCase__ (__lowerCamelCase = 800800, __lowerCamelCase = 800800 ): _SCREAMING_SNAKE_CASE : Optional[int] = degree * loga(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : Any = int(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : List[Any] = calculate_prime_numbers(__lowerCamelCase ) _SCREAMING_SNAKE_CASE : str = 0 _SCREAMING_SNAKE_CASE : int = 0 _SCREAMING_SNAKE_CASE : Dict = len(__lowerCamelCase ) - 1 while left < right: while ( prime_numbers[right] * loga(prime_numbers[left] ) + prime_numbers[left] * loga(prime_numbers[right] ) > upper_bound ): right -= 1 hybrid_integers_count += right - left left += 1 return hybrid_integers_count if __name__ == "__main__": print(f"{solution() = }")
325
0
"""simple docstring""" from math import pow def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ): """simple docstring""" if current_sum == needed_sum: # If the sum of the powers is equal to needed_sum, then we have a solution. solutions_count += 1 return current_sum, solutions_count A__ = int(pow(_lowerCamelCase , _lowerCamelCase ) ) if current_sum + i_to_n <= needed_sum: # If the sum of the powers is less than needed_sum, then continue adding powers. current_sum += i_to_n A__ = backtrack( _lowerCamelCase , _lowerCamelCase , current_number + 1 , _lowerCamelCase , _lowerCamelCase ) current_sum -= i_to_n if i_to_n < needed_sum: # If the power of i is less than needed_sum, then try with the next power. A__ = backtrack( _lowerCamelCase , _lowerCamelCase , current_number + 1 , _lowerCamelCase , _lowerCamelCase ) return current_sum, solutions_count def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" if not (1 <= needed_sum <= 1_000 and 2 <= power <= 10): raise ValueError( 'Invalid input\n' 'needed_sum must be between 1 and 1000, power between 2 and 10.' ) return backtrack(_lowerCamelCase , _lowerCamelCase , 1 , 0 , 0 )[1] # Return the solutions_count if __name__ == "__main__": import doctest doctest.testmod()
221
import os import unittest from transformers.models.bartpho.tokenization_bartpho import VOCAB_FILES_NAMES, BartphoTokenizer from transformers.testing_utils import get_tests_dir from ...test_tokenization_common import TokenizerTesterMixin _snake_case = get_tests_dir("fixtures/test_sentencepiece_bpe.model") class UpperCAmelCase_ ( a , unittest.TestCase): lowerCamelCase__ = BartphoTokenizer lowerCamelCase__ = False lowerCamelCase__ = True def snake_case__ ( self): '''simple docstring''' super().setUp() _lowerCAmelCase : str = ["▁This", "▁is", "▁a", "▁t", "est"] _lowerCAmelCase : List[str] = dict(zip(__a, range(len(__a)))) _lowerCAmelCase : Optional[Any] = {"unk_token": "<unk>"} _lowerCAmelCase : Optional[int] = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["monolingual_vocab_file"]) with open(self.monolingual_vocab_file, "w", encoding="utf-8") as fp: for token in vocab_tokens: fp.write(f"{token} {vocab_tokens[token]}\n") _lowerCAmelCase : Optional[Any] = BartphoTokenizer(__a, self.monolingual_vocab_file, **self.special_tokens_map) tokenizer.save_pretrained(self.tmpdirname) def snake_case__ ( self, **__a): '''simple docstring''' kwargs.update(self.special_tokens_map) return BartphoTokenizer.from_pretrained(self.tmpdirname, **__a) def snake_case__ ( self, __a): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = "This is a là test" _lowerCAmelCase : Optional[int] = "This is a<unk><unk> test" return input_text, output_text def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Optional[int] = BartphoTokenizer(__a, self.monolingual_vocab_file, **self.special_tokens_map) _lowerCAmelCase : List[Any] = "This is a là test" _lowerCAmelCase : str = "▁This ▁is ▁a ▁l à ▁t est".split() _lowerCAmelCase : str = tokenizer.tokenize(__a) self.assertListEqual(__a, __a) _lowerCAmelCase : Tuple = tokens + [tokenizer.unk_token] _lowerCAmelCase : List[str] = [4, 5, 6, 3, 3, 7, 8, 3] self.assertListEqual(tokenizer.convert_tokens_to_ids(__a), __a)
36
0
import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class snake_case__(unittest.TestCase ): """simple docstring""" def snake_case ( self : str , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : int ): return f"""gaussian_noise_s={seed}_shape={"_".join([str(SCREAMING_SNAKE_CASE ) for s in shape] )}.npy""" def snake_case ( self : Tuple ): # clean up the VRAM after each test super().tearDown() gc.collect() def snake_case ( self : Tuple , SCREAMING_SNAKE_CASE : Optional[Any]=0 , SCREAMING_SNAKE_CASE : Dict=(4, 4, 64, 64) , SCREAMING_SNAKE_CASE : Dict=False ): lowercase__ : int = jnp.bfloataa if fpaa else jnp.floataa lowercase__ : List[str] = jnp.array(load_hf_numpy(self.get_file_format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) , dtype=SCREAMING_SNAKE_CASE ) return image def snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE : Tuple=False , SCREAMING_SNAKE_CASE : int="CompVis/stable-diffusion-v1-4" ): lowercase__ : str = jnp.bfloataa if fpaa else jnp.floataa lowercase__ : int = "bf16" if fpaa else None lowercase__ , lowercase__ : List[str] = FlaxUNetaDConditionModel.from_pretrained( SCREAMING_SNAKE_CASE , subfolder="unet" , dtype=SCREAMING_SNAKE_CASE , revision=SCREAMING_SNAKE_CASE ) return model, params def snake_case ( self : str , SCREAMING_SNAKE_CASE : List[Any]=0 , SCREAMING_SNAKE_CASE : List[str]=(4, 77, 768) , SCREAMING_SNAKE_CASE : Optional[int]=False ): lowercase__ : Optional[Any] = jnp.bfloataa if fpaa else jnp.floataa lowercase__ : int = jnp.array(load_hf_numpy(self.get_file_format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) , dtype=SCREAMING_SNAKE_CASE ) return hidden_states @parameterized.expand( [ # fmt: off [83, 4, [-0.2_323, -0.1_304, 0.0_813, -0.3_093, -0.0_919, -0.1_571, -0.1_125, -0.5_806]], [17, 0.55, [-0.0_831, -0.2_443, 0.0_901, -0.0_919, 0.3_396, 0.0_103, -0.3_743, 0.0_701]], [8, 0.89, [-0.4_863, 0.0_859, 0.0_875, -0.1_658, 0.9_199, -0.0_114, 0.4_839, 0.4_639]], [3, 1_000, [-0.5_649, 0.2_402, -0.5_518, 0.1_248, 1.1_328, -0.2_443, -0.0_325, -1.0_078]], # fmt: on ] ) def snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Dict ): lowercase__ , lowercase__ : Any = self.get_unet_model(model_id="CompVis/stable-diffusion-v1-4" , fpaa=SCREAMING_SNAKE_CASE ) lowercase__ : Dict = self.get_latents(SCREAMING_SNAKE_CASE , fpaa=SCREAMING_SNAKE_CASE ) lowercase__ : Optional[int] = self.get_encoder_hidden_states(SCREAMING_SNAKE_CASE , fpaa=SCREAMING_SNAKE_CASE ) lowercase__ : int = model.apply( {"params": params} , SCREAMING_SNAKE_CASE , jnp.array(SCREAMING_SNAKE_CASE , dtype=jnp.intaa ) , encoder_hidden_states=SCREAMING_SNAKE_CASE , ).sample assert sample.shape == latents.shape lowercase__ : Any = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) lowercase__ : List[str] = jnp.array(SCREAMING_SNAKE_CASE , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , atol=1E-2 ) @parameterized.expand( [ # fmt: off [83, 4, [0.1_514, 0.0_807, 0.1_624, 0.1_016, -0.1_896, 0.0_263, 0.0_677, 0.2_310]], [17, 0.55, [0.1_164, -0.0_216, 0.0_170, 0.1_589, -0.3_120, 0.1_005, -0.0_581, -0.1_458]], [8, 0.89, [-0.1_758, -0.0_169, 0.1_004, -0.1_411, 0.1_312, 0.1_103, -0.1_996, 0.2_139]], [3, 1_000, [0.1_214, 0.0_352, -0.0_731, -0.1_562, -0.0_994, -0.0_906, -0.2_340, -0.0_539]], # fmt: on ] ) def snake_case ( self : Tuple , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : List[Any] ): lowercase__ , lowercase__ : Dict = self.get_unet_model(model_id="stabilityai/stable-diffusion-2" , fpaa=SCREAMING_SNAKE_CASE ) lowercase__ : Any = self.get_latents(SCREAMING_SNAKE_CASE , shape=(4, 4, 96, 96) , fpaa=SCREAMING_SNAKE_CASE ) lowercase__ : List[Any] = self.get_encoder_hidden_states(SCREAMING_SNAKE_CASE , shape=(4, 77, 1_024) , fpaa=SCREAMING_SNAKE_CASE ) lowercase__ : List[Any] = model.apply( {"params": params} , SCREAMING_SNAKE_CASE , jnp.array(SCREAMING_SNAKE_CASE , dtype=jnp.intaa ) , encoder_hidden_states=SCREAMING_SNAKE_CASE , ).sample assert sample.shape == latents.shape lowercase__ : Dict = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) lowercase__ : Optional[Any] = jnp.array(SCREAMING_SNAKE_CASE , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , atol=1E-2 )
121
import argparse import json import torch from diffusers import DDPMScheduler, LDMPipeline, UNetaDModel, VQModel def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__=1 ): """simple docstring""" if n_shave_prefix_segments >= 0: return ".".join(path.split("." )[n_shave_prefix_segments:] ) else: return ".".join(path.split("." )[:n_shave_prefix_segments] ) def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" lowercase__ : List[str] = [] for old_item in old_list: lowercase__ : Optional[Any] = old_item.replace("in_layers.0" , "norm1" ) lowercase__ : Union[str, Any] = new_item.replace("in_layers.2" , "conv1" ) lowercase__ : Optional[Any] = new_item.replace("out_layers.0" , "norm2" ) lowercase__ : Union[str, Any] = new_item.replace("out_layers.3" , "conv2" ) lowercase__ : Dict = new_item.replace("emb_layers.1" , "time_emb_proj" ) lowercase__ : int = new_item.replace("skip_connection" , "conv_shortcut" ) lowercase__ : Tuple = shave_segments(lowerCamelCase__ , n_shave_prefix_segments=lowerCamelCase__ ) mapping.append({"old": old_item, "new": new_item} ) return mapping def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__=0 ): """simple docstring""" lowercase__ : str = [] for old_item in old_list: lowercase__ : Optional[int] = old_item lowercase__ : Dict = new_item.replace("norm.weight" , "group_norm.weight" ) lowercase__ : Optional[int] = new_item.replace("norm.bias" , "group_norm.bias" ) lowercase__ : Tuple = new_item.replace("proj_out.weight" , "proj_attn.weight" ) lowercase__ : List[Any] = new_item.replace("proj_out.bias" , "proj_attn.bias" ) lowercase__ : Optional[Any] = shave_segments(lowerCamelCase__ , n_shave_prefix_segments=lowerCamelCase__ ) mapping.append({"old": old_item, "new": new_item} ) return mapping def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=None , lowerCamelCase__=None , lowerCamelCase__=None ): """simple docstring""" assert isinstance(lowerCamelCase__ , lowerCamelCase__ ), "Paths should be a list of dicts containing 'old' and 'new' keys." # Splits the attention layers into three variables. if attention_paths_to_split is not None: for path, path_map in attention_paths_to_split.items(): lowercase__ : List[str] = old_checkpoint[path] lowercase__ : str = old_tensor.shape[0] // 3 lowercase__ : List[str] = (-1, channels) if len(old_tensor.shape ) == 3 else (-1) lowercase__ : Union[str, Any] = old_tensor.shape[0] // config["num_head_channels"] // 3 lowercase__ : Union[str, Any] = old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:] ) lowercase__ , lowercase__ , lowercase__ : Optional[Any] = old_tensor.split(channels // num_heads , dim=1 ) lowercase__ : Dict = query.reshape(lowerCamelCase__ ) lowercase__ : Dict = key.reshape(lowerCamelCase__ ) lowercase__ : int = value.reshape(lowerCamelCase__ ) for path in paths: lowercase__ : Union[str, Any] = path["new"] # These have already been assigned if attention_paths_to_split is not None and new_path in attention_paths_to_split: continue # Global renaming happens here lowercase__ : List[Any] = new_path.replace("middle_block.0" , "mid_block.resnets.0" ) lowercase__ : Optional[Any] = new_path.replace("middle_block.1" , "mid_block.attentions.0" ) lowercase__ : List[str] = new_path.replace("middle_block.2" , "mid_block.resnets.1" ) if additional_replacements is not None: for replacement in additional_replacements: lowercase__ : Tuple = new_path.replace(replacement["old"] , replacement["new"] ) # proj_attn.weight has to be converted from conv 1D to linear if "proj_attn.weight" in new_path: lowercase__ : List[Any] = old_checkpoint[path["old"]][:, :, 0] else: lowercase__ : List[Any] = old_checkpoint[path["old"]] def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" lowercase__ : Union[str, Any] = {} lowercase__ : Optional[Any] = checkpoint["time_embed.0.weight"] lowercase__ : Tuple = checkpoint["time_embed.0.bias"] lowercase__ : Dict = checkpoint["time_embed.2.weight"] lowercase__ : Optional[Any] = checkpoint["time_embed.2.bias"] lowercase__ : Optional[int] = checkpoint["input_blocks.0.0.weight"] lowercase__ : List[Any] = checkpoint["input_blocks.0.0.bias"] lowercase__ : Tuple = checkpoint["out.0.weight"] lowercase__ : List[Any] = checkpoint["out.0.bias"] lowercase__ : Tuple = checkpoint["out.2.weight"] lowercase__ : Optional[Any] = checkpoint["out.2.bias"] # Retrieves the keys for the input blocks only lowercase__ : Dict = len({".".join(layer.split("." )[:2] ) for layer in checkpoint if "input_blocks" in layer} ) lowercase__ : str = { layer_id: [key for key in checkpoint if F"""input_blocks.{layer_id}""" in key] for layer_id in range(lowerCamelCase__ ) } # Retrieves the keys for the middle blocks only lowercase__ : Tuple = len({".".join(layer.split("." )[:2] ) for layer in checkpoint if "middle_block" in layer} ) lowercase__ : Union[str, Any] = { layer_id: [key for key in checkpoint if F"""middle_block.{layer_id}""" in key] for layer_id in range(lowerCamelCase__ ) } # Retrieves the keys for the output blocks only lowercase__ : Tuple = len({".".join(layer.split("." )[:2] ) for layer in checkpoint if "output_blocks" in layer} ) lowercase__ : Tuple = { layer_id: [key for key in checkpoint if F"""output_blocks.{layer_id}""" in key] for layer_id in range(lowerCamelCase__ ) } for i in range(1 , lowerCamelCase__ ): lowercase__ : Tuple = (i - 1) // (config["num_res_blocks"] + 1) lowercase__ : Optional[int] = (i - 1) % (config["num_res_blocks"] + 1) lowercase__ : List[Any] = [key for key in input_blocks[i] if F"""input_blocks.{i}.0""" in key] lowercase__ : Dict = [key for key in input_blocks[i] if F"""input_blocks.{i}.1""" in key] if F"""input_blocks.{i}.0.op.weight""" in checkpoint: lowercase__ : int = checkpoint[ F"""input_blocks.{i}.0.op.weight""" ] lowercase__ : List[str] = checkpoint[ F"""input_blocks.{i}.0.op.bias""" ] continue lowercase__ : Union[str, Any] = renew_resnet_paths(lowerCamelCase__ ) lowercase__ : Optional[int] = {"old": F"""input_blocks.{i}.0""", "new": F"""down_blocks.{block_id}.resnets.{layer_in_block_id}"""} lowercase__ : Optional[int] = {"old": "resnets.2.op", "new": "downsamplers.0.op"} assign_to_checkpoint( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , additional_replacements=[meta_path, resnet_op] , config=lowerCamelCase__ ) if len(lowerCamelCase__ ): lowercase__ : Tuple = renew_attention_paths(lowerCamelCase__ ) lowercase__ : str = { "old": F"""input_blocks.{i}.1""", "new": F"""down_blocks.{block_id}.attentions.{layer_in_block_id}""", } lowercase__ : List[str] = { F"""input_blocks.{i}.1.qkv.bias""": { "key": F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""", "query": F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""", "value": F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""", }, F"""input_blocks.{i}.1.qkv.weight""": { "key": F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""", "query": F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""", "value": F"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""", }, } assign_to_checkpoint( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , additional_replacements=[meta_path] , attention_paths_to_split=lowerCamelCase__ , config=lowerCamelCase__ , ) lowercase__ : int = middle_blocks[0] lowercase__ : Dict = middle_blocks[1] lowercase__ : Dict = middle_blocks[2] lowercase__ : Any = renew_resnet_paths(lowerCamelCase__ ) assign_to_checkpoint(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , config=lowerCamelCase__ ) lowercase__ : List[Any] = renew_resnet_paths(lowerCamelCase__ ) assign_to_checkpoint(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , config=lowerCamelCase__ ) lowercase__ : Optional[int] = renew_attention_paths(lowerCamelCase__ ) lowercase__ : Optional[int] = { "middle_block.1.qkv.bias": { "key": "mid_block.attentions.0.key.bias", "query": "mid_block.attentions.0.query.bias", "value": "mid_block.attentions.0.value.bias", }, "middle_block.1.qkv.weight": { "key": "mid_block.attentions.0.key.weight", "query": "mid_block.attentions.0.query.weight", "value": "mid_block.attentions.0.value.weight", }, } assign_to_checkpoint( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , attention_paths_to_split=lowerCamelCase__ , config=lowerCamelCase__ ) for i in range(lowerCamelCase__ ): lowercase__ : List[Any] = i // (config["num_res_blocks"] + 1) lowercase__ : Optional[int] = i % (config["num_res_blocks"] + 1) lowercase__ : List[Any] = [shave_segments(lowerCamelCase__ , 2 ) for name in output_blocks[i]] lowercase__ : Optional[Any] = {} for layer in output_block_layers: lowercase__ , lowercase__ : str = layer.split("." )[0], shave_segments(lowerCamelCase__ , 1 ) if layer_id in output_block_list: output_block_list[layer_id].append(lowerCamelCase__ ) else: lowercase__ : Tuple = [layer_name] if len(lowerCamelCase__ ) > 1: lowercase__ : Dict = [key for key in output_blocks[i] if F"""output_blocks.{i}.0""" in key] lowercase__ : Dict = [key for key in output_blocks[i] if F"""output_blocks.{i}.1""" in key] lowercase__ : Optional[Any] = renew_resnet_paths(lowerCamelCase__ ) lowercase__ : Optional[Any] = renew_resnet_paths(lowerCamelCase__ ) lowercase__ : Tuple = {"old": F"""output_blocks.{i}.0""", "new": F"""up_blocks.{block_id}.resnets.{layer_in_block_id}"""} assign_to_checkpoint(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , additional_replacements=[meta_path] , config=lowerCamelCase__ ) if ["conv.weight", "conv.bias"] in output_block_list.values(): lowercase__ : List[str] = list(output_block_list.values() ).index(["conv.weight", "conv.bias"] ) lowercase__ : Tuple = checkpoint[ F"""output_blocks.{i}.{index}.conv.weight""" ] lowercase__ : Optional[Any] = checkpoint[ F"""output_blocks.{i}.{index}.conv.bias""" ] # Clear attentions as they have been attributed above. if len(lowerCamelCase__ ) == 2: lowercase__ : int = [] if len(lowerCamelCase__ ): lowercase__ : Tuple = renew_attention_paths(lowerCamelCase__ ) lowercase__ : str = { "old": F"""output_blocks.{i}.1""", "new": F"""up_blocks.{block_id}.attentions.{layer_in_block_id}""", } lowercase__ : Union[str, Any] = { F"""output_blocks.{i}.1.qkv.bias""": { "key": F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""", "query": F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""", "value": F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""", }, F"""output_blocks.{i}.1.qkv.weight""": { "key": F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""", "query": F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""", "value": F"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""", }, } assign_to_checkpoint( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , additional_replacements=[meta_path] , attention_paths_to_split=to_split if any("qkv" in key for key in attentions ) else None , config=lowerCamelCase__ , ) else: lowercase__ : int = renew_resnet_paths(lowerCamelCase__ , n_shave_prefix_segments=1 ) for path in resnet_0_paths: lowercase__ : List[Any] = ".".join(["output_blocks", str(lowerCamelCase__ ), path["old"]] ) lowercase__ : Any = ".".join(["up_blocks", str(lowerCamelCase__ ), "resnets", str(lowerCamelCase__ ), path["new"]] ) lowercase__ : List[Any] = checkpoint[old_path] return new_checkpoint if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the checkpoint to convert.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the architecture.''', ) parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') lowerCAmelCase__ = parser.parse_args() lowerCAmelCase__ = torch.load(args.checkpoint_path) with open(args.config_file) as f: lowerCAmelCase__ = json.loads(f.read()) lowerCAmelCase__ = convert_ldm_checkpoint(checkpoint, config) if "ldm" in config: del config["ldm"] lowerCAmelCase__ = UNetaDModel(**config) model.load_state_dict(converted_checkpoint) try: lowerCAmelCase__ = DDPMScheduler.from_config('''/'''.join(args.checkpoint_path.split('''/''')[:-1])) lowerCAmelCase__ = VQModel.from_pretrained('''/'''.join(args.checkpoint_path.split('''/''')[:-1])) lowerCAmelCase__ = LDMPipeline(unet=model, scheduler=scheduler, vae=vqvae) pipe.save_pretrained(args.dump_path) except: # noqa: E722 model.save_pretrained(args.dump_path)
121
1
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def __A ( __lowerCamelCase , __lowerCamelCase=0.999 , __lowerCamelCase="cosine" , ) -> Dict: if alpha_transform_type == "cosine": def alpha_bar_fn(__lowerCamelCase ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(__lowerCamelCase ): return math.exp(t * -12.0 ) else: raise ValueError(f'Unsupported alpha_tranform_type: {alpha_transform_type}' ) a = [] for i in range(__lowerCamelCase ): a = i / num_diffusion_timesteps a = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(__lowerCamelCase ) / alpha_bar_fn(__lowerCamelCase ) , __lowerCamelCase ) ) return torch.tensor(__lowerCamelCase , dtype=torch.floataa ) class __lowerCAmelCase ( __magic_name__ , __magic_name__ ): UpperCamelCase__ = [e.name for e in KarrasDiffusionSchedulers] UpperCamelCase__ = 2 @register_to_config def __init__( self :str , __magic_name__ :int = 1000 , __magic_name__ :float = 0.00085 , __magic_name__ :float = 0.012 , __magic_name__ :str = "linear" , __magic_name__ :Optional[Union[np.ndarray, List[float]]] = None , __magic_name__ :str = "epsilon" , __magic_name__ :str = "linspace" , __magic_name__ :int = 0 , ): '''simple docstring''' if trained_betas is not None: a = torch.tensor(__magic_name__ , dtype=torch.floataa ) elif beta_schedule == "linear": a = torch.linspace(__magic_name__ , __magic_name__ , __magic_name__ , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. a = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , __magic_name__ , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule a = betas_for_alpha_bar(__magic_name__ ) else: raise NotImplementedError(F'{beta_schedule} does is not implemented for {self.__class__}' ) a = 1.0 - self.betas a = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(__magic_name__ , __magic_name__ , __magic_name__ ) def lowerCamelCase__ ( self :List[str] , __magic_name__ :Tuple , __magic_name__ :int=None ): '''simple docstring''' if schedule_timesteps is None: a = self.timesteps a = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: a = 1 if len(__magic_name__ ) > 1 else 0 else: a = timestep.cpu().item() if torch.is_tensor(__magic_name__ ) else timestep a = self._index_counter[timestep_int] return indices[pos].item() @property def lowerCamelCase__ ( self :List[str] ): '''simple docstring''' if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def lowerCamelCase__ ( self :Any , __magic_name__ :torch.FloatTensor , __magic_name__ :Union[float, torch.FloatTensor] , ): '''simple docstring''' a = self.index_for_timestep(__magic_name__ ) if self.state_in_first_order: a = self.sigmas[step_index] else: a = self.sigmas_interpol[step_index] a = sample / ((sigma**2 + 1) ** 0.5) return sample def lowerCamelCase__ ( self :Optional[int] , __magic_name__ :int , __magic_name__ :Union[str, torch.device] = None , __magic_name__ :Optional[int] = None , ): '''simple docstring''' a = num_inference_steps a = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": a = np.linspace(0 , num_train_timesteps - 1 , __magic_name__ , dtype=__magic_name__ )[::-1].copy() elif self.config.timestep_spacing == "leading": a = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 a = (np.arange(0 , __magic_name__ ) * step_ratio).round()[::-1].copy().astype(__magic_name__ ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": a = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 a = (np.arange(__magic_name__ , 0 , -step_ratio )).round().copy().astype(__magic_name__ ) timesteps -= 1 else: raise ValueError( F'{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.' ) a = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) a = torch.from_numpy(np.log(__magic_name__ ) ).to(__magic_name__ ) a = np.interp(__magic_name__ , np.arange(0 , len(__magic_name__ ) ) , __magic_name__ ) a = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) a = torch.from_numpy(__magic_name__ ).to(device=__magic_name__ ) # interpolate sigmas a = sigmas.log().lerp(sigmas.roll(1 ).log() , 0.5 ).exp() a = torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2 ), sigmas[-1:]] ) a = torch.cat( [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2 ), sigmas_interpol[-1:]] ) if str(__magic_name__ ).startswith("""mps""" ): # mps does not support float64 a = torch.from_numpy(__magic_name__ ).to(__magic_name__ , dtype=torch.floataa ) else: a = torch.from_numpy(__magic_name__ ).to(__magic_name__ ) # interpolate timesteps a = self.sigma_to_t(__magic_name__ ).to(__magic_name__ , dtype=timesteps.dtype ) a = torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None]) , dim=-1 ).flatten() a = torch.cat([timesteps[:1], interleaved_timesteps] ) a = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter a = defaultdict(__magic_name__ ) def lowerCamelCase__ ( self :Union[str, Any] , __magic_name__ :Dict ): '''simple docstring''' a = sigma.log() # get distribution a = log_sigma - self.log_sigmas[:, None] # get sigmas range a = dists.ge(0 ).cumsum(dim=0 ).argmax(dim=0 ).clamp(max=self.log_sigmas.shape[0] - 2 ) a = low_idx + 1 a = self.log_sigmas[low_idx] a = self.log_sigmas[high_idx] # interpolate sigmas a = (low - log_sigma) / (low - high) a = w.clamp(0 , 1 ) # transform interpolation to time range a = (1 - w) * low_idx + w * high_idx a = t.view(sigma.shape ) return t @property def lowerCamelCase__ ( self :Dict ): '''simple docstring''' return self.sample is None def lowerCamelCase__ ( self :List[str] , __magic_name__ :Union[torch.FloatTensor, np.ndarray] , __magic_name__ :Union[float, torch.FloatTensor] , __magic_name__ :Union[torch.FloatTensor, np.ndarray] , __magic_name__ :bool = True , ): '''simple docstring''' a = self.index_for_timestep(__magic_name__ ) # advance index counter by 1 a = timestep.cpu().item() if torch.is_tensor(__magic_name__ ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: a = self.sigmas[step_index] a = self.sigmas_interpol[step_index + 1] a = self.sigmas[step_index + 1] else: # 2nd order / KDPM2's method a = self.sigmas[step_index - 1] a = self.sigmas_interpol[step_index] a = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API a = 0 a = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": a = sigma_hat if self.state_in_first_order else sigma_interpol a = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": a = sigma_hat if self.state_in_first_order else sigma_interpol a = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": raise NotImplementedError("""prediction_type not implemented yet: sample""" ) else: raise ValueError( F'prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`' ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order a = (sample - pred_original_sample) / sigma_hat # 3. delta timestep a = sigma_interpol - sigma_hat # store for 2nd order step a = sample else: # DPM-Solver-2 # 2. Convert to an ODE derivative for 2nd order a = (sample - pred_original_sample) / sigma_interpol # 3. delta timestep a = sigma_next - sigma_hat a = self.sample a = None a = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=__magic_name__ ) def lowerCamelCase__ ( self :List[Any] , __magic_name__ :torch.FloatTensor , __magic_name__ :torch.FloatTensor , __magic_name__ :torch.FloatTensor , ): '''simple docstring''' a = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(__magic_name__ ): # mps does not support float64 a = self.timesteps.to(original_samples.device , dtype=torch.floataa ) a = timesteps.to(original_samples.device , dtype=torch.floataa ) else: a = self.timesteps.to(original_samples.device ) a = timesteps.to(original_samples.device ) a = [self.index_for_timestep(__magic_name__ , __magic_name__ ) for t in timesteps] a = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): a = sigma.unsqueeze(-1 ) a = original_samples + noise * sigma return noisy_samples def __len__( self :List[str] ): '''simple docstring''' return self.config.num_train_timesteps
228
__UpperCamelCase : Optional[int] = { "Pillow": "Pillow", "accelerate": "accelerate>=0.11.0", "compel": "compel==0.1.8", "black": "black~=23.1", "datasets": "datasets", "filelock": "filelock", "flax": "flax>=0.4.1", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.13.2", "requests-mock": "requests-mock==1.10.0", "importlib_metadata": "importlib_metadata", "invisible-watermark": "invisible-watermark", "isort": "isort>=5.5.4", "jax": "jax>=0.2.8,!=0.3.2", "jaxlib": "jaxlib>=0.1.65", "Jinja2": "Jinja2", "k-diffusion": "k-diffusion>=0.0.12", "torchsde": "torchsde", "note_seq": "note_seq", "librosa": "librosa", "numpy": "numpy", "omegaconf": "omegaconf", "parameterized": "parameterized", "protobuf": "protobuf>=3.20.3,<4", "pytest": "pytest", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "ruff": "ruff>=0.0.241", "safetensors": "safetensors", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "scipy": "scipy", "onnx": "onnx", "regex": "regex!=2019.12.17", "requests": "requests", "tensorboard": "tensorboard", "torch": "torch>=1.4", "torchvision": "torchvision", "transformers": "transformers>=4.25.1", "urllib3": "urllib3<=2.0.0", }
228
1
'''simple docstring''' import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class _UpperCAmelCase ( snake_case_ , snake_case_ , unittest.TestCase ): """simple docstring""" snake_case = IFPipeline snake_case = TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''} snake_case = TEXT_TO_IMAGE_BATCH_PARAMS snake_case = PipelineTesterMixin.required_optional_params - {'''latents'''} def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' return self._get_dummy_components() def lowerCAmelCase ( self : int , __UpperCAmelCase : str , __UpperCAmelCase : List[Any]=0 ): '''simple docstring''' if str(__UpperCAmelCase ).startswith("mps" ): _A = torch.manual_seed(__UpperCAmelCase ) else: _A = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) _A = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA" ) def lowerCAmelCase ( self : Dict ): '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def lowerCAmelCase ( self : Tuple ): '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' self._test_save_load_local() def lowerCAmelCase ( self : str ): '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def lowerCAmelCase ( self : int ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @slow @require_torch_gpu class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" def lowerCAmelCase ( self : Any ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase ( self : Tuple ): '''simple docstring''' _A = IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0" , variant="fp16" , torch_dtype=torch.floataa ) _A = IFSuperResolutionPipeline.from_pretrained( "DeepFloyd/IF-II-L-v1.0" , variant="fp16" , torch_dtype=torch.floataa , text_encoder=__UpperCAmelCase , tokenizer=__UpperCAmelCase ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to("cuda" ) _A , _A = pipe_a.encode_prompt("anime turtle" , device="cuda" ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() _A = None _A = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img _A = IFImgaImgPipeline(**pipe_a.components ) _A = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting _A = IFInpaintingPipeline(**pipe_a.components ) _A = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) def lowerCAmelCase ( self : List[str] , __UpperCAmelCase : int , __UpperCAmelCase : str , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : int ): '''simple docstring''' _start_torch_memory_measurement() _A = torch.Generator(device="cpu" ).manual_seed(0 ) _A = pipe_a( prompt_embeds=__UpperCAmelCase , negative_prompt_embeds=__UpperCAmelCase , num_inference_steps=2 , generator=__UpperCAmelCase , output_type="np" , ) _A = output.images[0] assert image.shape == (64, 64, 3) _A = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 _A = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy" ) assert_mean_pixel_difference(__UpperCAmelCase , __UpperCAmelCase ) # pipeline 2 _start_torch_memory_measurement() _A = torch.Generator(device="cpu" ).manual_seed(0 ) _A = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__UpperCAmelCase ) _A = pipe_a( prompt_embeds=__UpperCAmelCase , negative_prompt_embeds=__UpperCAmelCase , image=__UpperCAmelCase , generator=__UpperCAmelCase , num_inference_steps=2 , output_type="np" , ) _A = output.images[0] assert image.shape == (256, 256, 3) _A = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 _A = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy" ) assert_mean_pixel_difference(__UpperCAmelCase , __UpperCAmelCase ) def lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Any , __UpperCAmelCase : List[Any] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : List[str] ): '''simple docstring''' _start_torch_memory_measurement() _A = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__UpperCAmelCase ) _A = torch.Generator(device="cpu" ).manual_seed(0 ) _A = pipe_a( prompt_embeds=__UpperCAmelCase , negative_prompt_embeds=__UpperCAmelCase , image=__UpperCAmelCase , num_inference_steps=2 , generator=__UpperCAmelCase , output_type="np" , ) _A = output.images[0] assert image.shape == (64, 64, 3) _A = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 _A = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy" ) assert_mean_pixel_difference(__UpperCAmelCase , __UpperCAmelCase ) # pipeline 2 _start_torch_memory_measurement() _A = torch.Generator(device="cpu" ).manual_seed(0 ) _A = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(__UpperCAmelCase ) _A = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__UpperCAmelCase ) _A = pipe_a( prompt_embeds=__UpperCAmelCase , negative_prompt_embeds=__UpperCAmelCase , image=__UpperCAmelCase , original_image=__UpperCAmelCase , generator=__UpperCAmelCase , num_inference_steps=2 , output_type="np" , ) _A = output.images[0] assert image.shape == (256, 256, 3) _A = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 _A = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy" ) assert_mean_pixel_difference(__UpperCAmelCase , __UpperCAmelCase ) def lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Dict , __UpperCAmelCase : Dict , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[int] ): '''simple docstring''' _start_torch_memory_measurement() _A = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__UpperCAmelCase ) _A = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(__UpperCAmelCase ) _A = torch.Generator(device="cpu" ).manual_seed(0 ) _A = pipe_a( prompt_embeds=__UpperCAmelCase , negative_prompt_embeds=__UpperCAmelCase , image=__UpperCAmelCase , mask_image=__UpperCAmelCase , num_inference_steps=2 , generator=__UpperCAmelCase , output_type="np" , ) _A = output.images[0] assert image.shape == (64, 64, 3) _A = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 _A = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy" ) assert_mean_pixel_difference(__UpperCAmelCase , __UpperCAmelCase ) # pipeline 2 _start_torch_memory_measurement() _A = torch.Generator(device="cpu" ).manual_seed(0 ) _A = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__UpperCAmelCase ) _A = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(__UpperCAmelCase ) _A = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(__UpperCAmelCase ) _A = pipe_a( prompt_embeds=__UpperCAmelCase , negative_prompt_embeds=__UpperCAmelCase , image=__UpperCAmelCase , mask_image=__UpperCAmelCase , original_image=__UpperCAmelCase , generator=__UpperCAmelCase , num_inference_steps=2 , output_type="np" , ) _A = output.images[0] assert image.shape == (256, 256, 3) _A = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 _A = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy" ) assert_mean_pixel_difference(__UpperCAmelCase , __UpperCAmelCase ) def __lowercase ( ) -> List[str]: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
174
'''simple docstring''' from collections import defaultdict from math import gcd def __lowercase ( __lowercase = 150_0000 ) -> int: '''simple docstring''' _A = defaultdict(__lowercase ) _A = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 , __lowercase , 2 ): if gcd(__lowercase , __lowercase ) > 1: continue _A = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(__lowercase , limit + 1 , __lowercase ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F"""{solution() = }""")
174
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __a = {"configuration_reformer": ["REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "ReformerConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ["ReformerTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ["ReformerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ "REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "ReformerAttention", "ReformerForMaskedLM", "ReformerForQuestionAnswering", "ReformerForSequenceClassification", "ReformerLayer", "ReformerModel", "ReformerModelWithLMHead", "ReformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer import ReformerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer_fast import ReformerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_reformer import ( REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ReformerAttention, ReformerForMaskedLM, ReformerForQuestionAnswering, ReformerForSequenceClassification, ReformerLayer, ReformerModel, ReformerModelWithLMHead, ReformerPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
66
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig 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, _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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowerCamelCase : '''simple docstring''' def __init__( self: Optional[int] , snake_case: Any , snake_case: Optional[Any]=13 , snake_case: Tuple=32 , snake_case: Optional[int]=2 , snake_case: Tuple=3 , snake_case: Tuple=16 , snake_case: Optional[Any]=[1, 2, 1] , snake_case: Optional[int]=[2, 2, 4] , snake_case: Optional[int]=2 , snake_case: int=2.0 , snake_case: Union[str, Any]=True , snake_case: List[str]=0.0 , snake_case: List[Any]=0.0 , snake_case: Optional[Any]=0.1 , snake_case: List[Any]="gelu" , snake_case: Optional[int]=False , snake_case: Union[str, Any]=True , snake_case: Union[str, Any]=0.0_2 , snake_case: Optional[int]=1E-5 , snake_case: Optional[Any]=True , snake_case: List[Any]=None , snake_case: List[Any]=True , snake_case: Optional[Any]=10 , snake_case: str=8 , ) -> Tuple: snake_case_ :Dict = parent snake_case_ :Any = batch_size snake_case_ :List[Any] = image_size snake_case_ :List[Any] = patch_size snake_case_ :int = num_channels snake_case_ :Tuple = embed_dim snake_case_ :str = depths snake_case_ :str = num_heads snake_case_ :Optional[int] = window_size snake_case_ :Tuple = mlp_ratio snake_case_ :Any = qkv_bias snake_case_ :List[Any] = hidden_dropout_prob snake_case_ :Optional[Any] = attention_probs_dropout_prob snake_case_ :Union[str, Any] = drop_path_rate snake_case_ :Any = hidden_act snake_case_ :Optional[Any] = use_absolute_embeddings snake_case_ :Union[str, Any] = patch_norm snake_case_ :Dict = layer_norm_eps snake_case_ :str = initializer_range snake_case_ :Tuple = is_training snake_case_ :Tuple = scope snake_case_ :Union[str, Any] = use_labels snake_case_ :Optional[Any] = type_sequence_label_size snake_case_ :Dict = encoder_stride def lowerCAmelCase_ ( self: int ) -> int: snake_case_ :List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ :Any = None if self.use_labels: snake_case_ :str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ :int = self.get_config() return config, pixel_values, labels def lowerCAmelCase_ ( self: str ) -> Union[str, Any]: return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def lowerCAmelCase_ ( self: str , snake_case: Optional[int] , snake_case: Dict , snake_case: str ) -> List[Any]: snake_case_ :Union[str, Any] = SwinvaModel(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Optional[int] = model(snake_case ) snake_case_ :Optional[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) snake_case_ :int = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def lowerCAmelCase_ ( self: int , snake_case: List[str] , snake_case: Tuple , snake_case: int ) -> Any: snake_case_ :Dict = SwinvaForMaskedImageModeling(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Tuple = model(snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images snake_case_ :List[Any] = 1 snake_case_ :int = SwinvaForMaskedImageModeling(snake_case ) model.to(snake_case ) model.eval() snake_case_ :Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case_ :int = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def lowerCAmelCase_ ( self: List[Any] , snake_case: Any , snake_case: List[str] , snake_case: Union[str, Any] ) -> Tuple: snake_case_ :int = self.type_sequence_label_size snake_case_ :List[Any] = SwinvaForImageClassification(snake_case ) model.to(snake_case ) model.eval() snake_case_ :Dict = model(snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowerCAmelCase_ ( self: int ) -> str: snake_case_ :Any = self.prepare_config_and_inputs() snake_case_, snake_case_, snake_case_ :List[str] = config_and_inputs snake_case_ :List[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): '''simple docstring''' _A : Optional[Any] = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) _A : Any = ( {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification} if is_torch_available() else {} ) _A : List[Any] = False _A : List[str] = False _A : Tuple = False _A : List[str] = False def lowerCAmelCase_ ( self: Dict ) -> List[Any]: snake_case_ :Optional[int] = SwinvaModelTester(self ) snake_case_ :List[str] = ConfigTester(self , config_class=snake_case , embed_dim=37 ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> List[Any]: 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 lowerCAmelCase_ ( self: Union[str, Any] ) -> Tuple: snake_case_ :List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0.""" ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> str: pass @unittest.skip(reason="""Swinv2 does not use inputs_embeds""" ) def lowerCAmelCase_ ( self: int ) -> Dict: pass def lowerCAmelCase_ ( self: List[str] ) -> Union[str, Any]: snake_case_, snake_case_ :List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ :Optional[int] = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) snake_case_ :List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def lowerCAmelCase_ ( self: Dict ) -> Optional[int]: snake_case_, snake_case_ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ :Optional[int] = model_class(snake_case ) snake_case_ :List[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ :int = [*signature.parameters.keys()] snake_case_ :List[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , snake_case ) def lowerCAmelCase_ ( self: List[str] ) -> Optional[Any]: snake_case_, snake_case_ :List[str] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :List[str] = True for model_class in self.all_model_classes: snake_case_ :List[Any] = True snake_case_ :Any = False snake_case_ :Optional[int] = True snake_case_ :Tuple = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Any = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :str = outputs.attentions snake_case_ :Dict = len(self.model_tester.depths ) self.assertEqual(len(snake_case ) , snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] snake_case_ :Union[str, Any] = True snake_case_ :Tuple = config.window_size**2 snake_case_ :Any = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Union[str, Any] = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :int = outputs.attentions self.assertEqual(len(snake_case ) , snake_case ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) snake_case_ :Any = len(snake_case ) # Check attention is always last and order is fine snake_case_ :int = True snake_case_ :Dict = True snake_case_ :Optional[int] = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Dict = model(**self._prepare_for_class(snake_case , snake_case ) ) if hasattr(self.model_tester , """num_hidden_states_types""" ): snake_case_ :Any = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states snake_case_ :int = 2 self.assertEqual(out_len + added_hidden_states , len(snake_case ) ) snake_case_ :str = outputs.attentions self.assertEqual(len(snake_case ) , snake_case ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def lowerCAmelCase_ ( self: int , snake_case: Dict , snake_case: Dict , snake_case: Optional[Any] , snake_case: Dict ) -> List[str]: snake_case_ :Dict = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Optional[int] = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :str = outputs.hidden_states snake_case_ :List[Any] = getattr( self.model_tester , """expected_num_hidden_layers""" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(snake_case ) , snake_case ) # Swinv2 has a different seq_length snake_case_ :List[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :Optional[int] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) snake_case_ :str = outputs.reshaped_hidden_states self.assertEqual(len(snake_case ) , snake_case ) snake_case_, snake_case_, snake_case_, snake_case_ :Any = reshaped_hidden_states[0].shape snake_case_ :int = ( reshaped_hidden_states[0].view(snake_case , snake_case , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def lowerCAmelCase_ ( self: Any ) -> Any: snake_case_, snake_case_ :List[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Union[str, Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: snake_case_ :Union[str, Any] = True self.check_hidden_states_output(snake_case , snake_case , snake_case , snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ :List[str] = True self.check_hidden_states_output(snake_case , snake_case , snake_case , snake_case ) def lowerCAmelCase_ ( self: Tuple ) -> Any: snake_case_, snake_case_ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Optional[int] = 3 snake_case_ :Union[str, Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) snake_case_ :str = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :Any = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) snake_case_ :int = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: snake_case_ :str = True self.check_hidden_states_output(snake_case , snake_case , snake_case , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ :Tuple = True self.check_hidden_states_output(snake_case , snake_case , snake_case , (padded_height, padded_width) ) def lowerCAmelCase_ ( self: Any ) -> Tuple: snake_case_ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*snake_case ) def lowerCAmelCase_ ( self: Optional[int] ) -> Dict: snake_case_ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case ) @slow def lowerCAmelCase_ ( self: List[Any] ) -> Dict: for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ :List[str] = SwinvaModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def lowerCAmelCase_ ( self: Optional[int] ) -> List[Any]: snake_case_, snake_case_ :str = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Optional[int] = _config_zero_init(snake_case ) for model_class in self.all_model_classes: snake_case_ :Tuple = model_class(config=snake_case ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and 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""" , ) @require_vision @require_torch class lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase_ ( self: Optional[int] ) -> List[Any]: return ( AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ) if is_vision_available() else None ) @slow def lowerCAmelCase_ ( self: List[str] ) -> List[str]: snake_case_ :Tuple = SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ).to( snake_case ) snake_case_ :str = self.default_image_processor snake_case_ :List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case_ :str = image_processor(images=snake_case , return_tensors="""pt""" ).to(snake_case ) # forward pass with torch.no_grad(): snake_case_ :Tuple = model(**snake_case ) # verify the logits snake_case_ :Dict = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case ) snake_case_ :int = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case , atol=1E-4 ) )
66
1
from __future__ import annotations def UpperCamelCase (lowercase_: list , lowercase_: int ) -> List[str]: # Checks if the entire collection has been sorted if len(lowercase_ ) <= 1 or n <= 1: return insert_next(lowercase_ , n - 1 ) rec_insertion_sort(lowercase_ , n - 1 ) def UpperCamelCase (lowercase_: list , lowercase_: int ) -> Tuple: # Checks order between adjacent elements if index >= len(lowercase_ ) or collection[index - 1] <= collection[index]: return # Swaps adjacent elements since they are not in ascending order A__ , A__ : Optional[int] = ( collection[index], collection[index - 1], ) insert_next(lowercase_ , index + 1 ) if __name__ == "__main__": A_ : Tuple = input('Enter integers separated by spaces: ') A_ : list[int] = [int(num) for num in numbers.split()] rec_insertion_sort(number_list, len(number_list)) print(number_list)
141
import argparse import re from pathlib import Path import requests import torch from PIL import Image from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor from transformers import ( EfficientFormerConfig, EfficientFormerForImageClassificationWithTeacher, EfficientFormerImageProcessor, ) from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def UpperCamelCase (lowercase_: str , lowercase_: Optional[int] ) -> str: A__ : Union[str, Any] = old_name if "patch_embed" in old_name: A__ , A__ , A__ : Any = old_name.split(""".""" ) if layer == "0": A__ : List[Any] = old_name.replace("""0""" , """convolution1""" ) elif layer == "1": A__ : Optional[int] = old_name.replace("""1""" , """batchnorm_before""" ) elif layer == "3": A__ : int = old_name.replace("""3""" , """convolution2""" ) else: A__ : Dict = old_name.replace("""4""" , """batchnorm_after""" ) if "network" in old_name and re.search(r"""\d\.\d""" , lowercase_ ): A__ : str = r"""\b\d{2}\b""" if bool(re.search(lowercase_ , lowercase_ ) ): A__ : Optional[Any] = re.search(r"""\d\.\d\d.""" , lowercase_ ).group() else: A__ : int = re.search(r"""\d\.\d.""" , lowercase_ ).group() if int(match[0] ) < 6: A__ : Optional[Any] = old_name.replace(lowercase_ , """""" ) A__ : Tuple = trimmed_name.replace("""network""" , match[0] + """.meta4D_layers.blocks.""" + match[2:-1] ) A__ : int = """intermediate_stages.""" + trimmed_name else: A__ : Dict = old_name.replace(lowercase_ , """""" ) if int(match[2] ) < num_meta4D_last_stage: A__ : Optional[int] = trimmed_name.replace("""network""" , """meta4D_layers.blocks.""" + match[2] ) else: A__ : Optional[Any] = str(int(match[2] ) - num_meta4D_last_stage ) A__ : Dict = trimmed_name.replace("""network""" , """meta3D_layers.blocks.""" + layer_index ) if "norm1" in old_name: A__ : str = trimmed_name.replace("""norm1""" , """layernorm1""" ) elif "norm2" in old_name: A__ : Optional[int] = trimmed_name.replace("""norm2""" , """layernorm2""" ) elif "fc1" in old_name: A__ : List[Any] = trimmed_name.replace("""fc1""" , """linear_in""" ) elif "fc2" in old_name: A__ : Optional[Any] = trimmed_name.replace("""fc2""" , """linear_out""" ) A__ : str = """last_stage.""" + trimmed_name elif "network" in old_name and re.search(r""".\d.""" , lowercase_ ): A__ : List[str] = old_name.replace("""network""" , """intermediate_stages""" ) if "fc" in new_name: A__ : Optional[int] = new_name.replace("""fc""" , """convolution""" ) elif ("norm1" in new_name) and ("layernorm1" not in new_name): A__ : Optional[int] = new_name.replace("""norm1""" , """batchnorm_before""" ) elif ("norm2" in new_name) and ("layernorm2" not in new_name): A__ : int = new_name.replace("""norm2""" , """batchnorm_after""" ) if "proj" in new_name: A__ : Tuple = new_name.replace("""proj""" , """projection""" ) if "dist_head" in new_name: A__ : Optional[int] = new_name.replace("""dist_head""" , """distillation_classifier""" ) elif "head" in new_name: A__ : Optional[Any] = new_name.replace("""head""" , """classifier""" ) elif "patch_embed" in new_name: A__ : Optional[Any] = """efficientformer.""" + new_name elif new_name == "norm.weight" or new_name == "norm.bias": A__ : Union[str, Any] = new_name.replace("""norm""" , """layernorm""" ) A__ : Union[str, Any] = """efficientformer.""" + new_name else: A__ : int = """efficientformer.encoder.""" + new_name return new_name def UpperCamelCase (lowercase_: Optional[Any] , lowercase_: Union[str, Any] ) -> Tuple: for key in checkpoint.copy().keys(): A__ : List[Any] = checkpoint.pop(lowercase_ ) A__ : Dict = val return checkpoint def UpperCamelCase () -> Optional[int]: A__ : List[str] = """http://images.cocodataset.org/val2017/000000039769.jpg""" A__ : List[str] = Image.open(requests.get(lowercase_ , stream=lowercase_ ).raw ) return image def UpperCamelCase (lowercase_: Path , lowercase_: Path , lowercase_: Path , lowercase_: bool ) -> Tuple: A__ : Any = torch.load(lowercase_ , map_location="""cpu""" )["""model"""] A__ : List[Any] = EfficientFormerConfig.from_json_file(lowercase_ ) A__ : Any = EfficientFormerForImageClassificationWithTeacher(lowercase_ ) A__ : List[str] = """_""".join(checkpoint_path.split("""/""" )[-1].split(""".""" )[0].split("""_""" )[:-1] ) A__ : Union[str, Any] = config.depths[-1] - config.num_metaad_blocks + 1 A__ : Any = convert_torch_checkpoint(lowercase_ , lowercase_ ) model.load_state_dict(lowercase_ ) model.eval() A__ : Tuple = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } # prepare image A__ : Optional[int] = prepare_img() A__ : Optional[Any] = 256 A__ : str = 224 A__ : List[str] = EfficientFormerImageProcessor( size={"""shortest_edge""": image_size} , crop_size={"""height""": crop_size, """width""": crop_size} , resample=pillow_resamplings["""bicubic"""] , ) A__ : Tuple = processor(images=lowercase_ , return_tensors="""pt""" ).pixel_values # original processing pipeline A__ : List[Any] = Compose( [ Resize(lowercase_ , interpolation=pillow_resamplings["""bicubic"""] ), CenterCrop(lowercase_ ), ToTensor(), Normalize(lowercase_ , lowercase_ ), ] ) A__ : Any = image_transforms(lowercase_ ).unsqueeze(0 ) assert torch.allclose(lowercase_ , lowercase_ ) A__ : Optional[int] = model(lowercase_ ) A__ : List[str] = outputs.logits A__ : Tuple = (1, 1000) if "l1" in model_name: A__ : List[str] = torch.Tensor( [-0.1312, 0.4353, -1.0499, -0.5124, 0.4183, -0.6793, -1.3777, -0.0893, -0.7358, -2.4328] ) assert torch.allclose(logits[0, :10] , lowercase_ , atol=1E-3 ) assert logits.shape == expected_shape elif "l3" in model_name: A__ : Any = torch.Tensor( [-1.3150, -1.5456, -1.2556, -0.8496, -0.7127, -0.7897, -0.9728, -0.3052, 0.3751, -0.3127] ) assert torch.allclose(logits[0, :10] , lowercase_ , atol=1E-3 ) assert logits.shape == expected_shape elif "l7" in model_name: A__ : Union[str, Any] = torch.Tensor( [-1.0283, -1.4131, -0.5644, -1.3115, -0.5785, -1.2049, -0.7528, 0.1992, -0.3822, -0.0878] ) assert logits.shape == expected_shape else: raise ValueError( f"""Unknown model checkpoint: {checkpoint_path}. Supported version of efficientformer are l1, l3 and l7""" ) # Save Checkpoints Path(lowercase_ ).mkdir(exist_ok=lowercase_ ) model.save_pretrained(lowercase_ ) print(f"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" ) processor.save_pretrained(lowercase_ ) print(f"""Processor successfuly saved at {pytorch_dump_path}""" ) if push_to_hub: print("""Pushing model to the hub...""" ) model.push_to_hub( repo_id=f"""Bearnardd/{pytorch_dump_path}""" , commit_message="""Add model""" , use_temp_dir=lowercase_ , ) processor.push_to_hub( repo_id=f"""Bearnardd/{pytorch_dump_path}""" , commit_message="""Add image processor""" , use_temp_dir=lowercase_ , ) if __name__ == "__main__": A_ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--pytorch_model_path', default=None, type=str, required=True, help='Path to EfficientFormer pytorch checkpoint.', ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The json file for EfficientFormer model config.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument('--push_to_hub', action='store_true', help='Push model and image processor to the hub') parser.add_argument( '--no-push_to_hub', dest='push_to_hub', action='store_false', help='Do not push model and image processor to the hub', ) parser.set_defaults(push_to_hub=True) A_ : List[Any] = parser.parse_args() convert_efficientformer_checkpoint( checkpoint_path=args.pytorch_model_path, efficientformer_config_file=args.config_file, pytorch_dump_path=args.pytorch_dump_path, push_to_hub=args.push_to_hub, )
141
1
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case : Any = logging.get_logger(__name__) _snake_case : str = { 'microsoft/git-base': 'https://huggingface.co/microsoft/git-base/resolve/main/config.json', } class _UpperCAmelCase ( __UpperCAmelCase ): UpperCamelCase = """git_vision_model""" def __init__( self :Any , __UpperCamelCase :Optional[Any]=7_68 , __UpperCamelCase :Optional[int]=30_72 , __UpperCamelCase :List[str]=12 , __UpperCamelCase :Optional[int]=12 , __UpperCamelCase :int=3 , __UpperCamelCase :Optional[int]=2_24 , __UpperCamelCase :int=16 , __UpperCamelCase :int="quick_gelu" , __UpperCamelCase :str=1e-5 , __UpperCamelCase :int=0.0 , __UpperCamelCase :Optional[Any]=0.02 , **__UpperCamelCase :Optional[int] , ): super().__init__(**lowerCamelCase__ ) A = hidden_size A = intermediate_size A = num_hidden_layers A = num_attention_heads A = num_channels A = patch_size A = image_size A = initializer_range A = attention_dropout A = layer_norm_eps A = hidden_act @classmethod def lowerCamelCase ( cls :Optional[Any] , __UpperCamelCase :Any , **__UpperCamelCase :Dict ): cls._set_token_in_kwargs(lowerCamelCase__ ) A, A = cls.get_config_dict(lowerCamelCase__ , **lowerCamelCase__ ) # get the vision config dict if we are loading from GITConfig if config_dict.get("model_type" ) == "git": A = config_dict["vision_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(lowerCamelCase__ , **lowerCamelCase__ ) class _UpperCAmelCase ( __UpperCAmelCase ): UpperCamelCase = """git""" def __init__( self :List[str] , __UpperCamelCase :Dict=None , __UpperCamelCase :List[str]=3_05_22 , __UpperCamelCase :Optional[int]=7_68 , __UpperCamelCase :Optional[int]=6 , __UpperCamelCase :Any=12 , __UpperCamelCase :int=30_72 , __UpperCamelCase :Union[str, Any]="gelu" , __UpperCamelCase :Optional[int]=0.1 , __UpperCamelCase :int=0.1 , __UpperCamelCase :Union[str, Any]=10_24 , __UpperCamelCase :str=0.02 , __UpperCamelCase :Optional[Any]=1e-12 , __UpperCamelCase :List[Any]=0 , __UpperCamelCase :int="absolute" , __UpperCamelCase :str=True , __UpperCamelCase :Optional[Any]=False , __UpperCamelCase :Any=1_01 , __UpperCamelCase :Dict=1_02 , __UpperCamelCase :Optional[int]=None , **__UpperCamelCase :str , ): super().__init__(bos_token_id=lowerCamelCase__ , eos_token_id=lowerCamelCase__ , pad_token_id=lowerCamelCase__ , **lowerCamelCase__ ) if vision_config is None: A = {} logger.info("vision_config is None. initializing the GitVisionConfig with default values." ) A = GitVisionConfig(**lowerCamelCase__ ) A = vocab_size A = hidden_size A = num_hidden_layers A = num_attention_heads A = hidden_act A = intermediate_size A = hidden_dropout_prob A = attention_probs_dropout_prob A = max_position_embeddings A = initializer_range A = layer_norm_eps A = position_embedding_type A = use_cache A = tie_word_embeddings A = num_image_with_embedding A = bos_token_id A = eos_token_id def lowerCamelCase ( self :Tuple ): A = copy.deepcopy(self.__dict__ ) A = self.vision_config.to_dict() A = self.__class__.model_type return output
292
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __A = logging.get_logger(__name__) __A = {"vocab_file": "spm_char.model"} __A = { "vocab_file": { "microsoft/speecht5_asr": "https://huggingface.co/microsoft/speecht5_asr/resolve/main/spm_char.model", "microsoft/speecht5_tts": "https://huggingface.co/microsoft/speecht5_tts/resolve/main/spm_char.model", "microsoft/speecht5_vc": "https://huggingface.co/microsoft/speecht5_vc/resolve/main/spm_char.model", } } __A = { "microsoft/speecht5_asr": 1_024, "microsoft/speecht5_tts": 1_024, "microsoft/speecht5_vc": 1_024, } class A ( __UpperCAmelCase ): lowerCamelCase : List[str] = VOCAB_FILES_NAMES lowerCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : List[Any] = ["""input_ids""", """attention_mask"""] def __init__( self , lowerCamelCase__ , lowerCamelCase__="<s>" , lowerCamelCase__="</s>" , lowerCamelCase__="<unk>" , lowerCamelCase__="<pad>" , lowerCamelCase__ = None , **lowerCamelCase__ , ) -> None: '''simple docstring''' lowercase__ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCamelCase__ , ) lowercase__ = vocab_file lowercase__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCamelCase__ ) @property def A__ ( self ) -> str: '''simple docstring''' return self.sp_model.get_piece_size() def A__ ( self ) -> int: '''simple docstring''' lowercase__ = {self.convert_ids_to_tokens(lowerCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> Dict: '''simple docstring''' lowercase__ = self.__dict__.copy() lowercase__ = None return state def __setstate__( self , lowerCamelCase__ ) -> Union[str, Any]: '''simple docstring''' lowercase__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): lowercase__ = {} lowercase__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def A__ ( self , lowerCamelCase__ ) -> List[str]: '''simple docstring''' return self.sp_model.encode(lowerCamelCase__ , out_type=lowerCamelCase__ ) def A__ ( self , lowerCamelCase__ ) -> Optional[int]: '''simple docstring''' return self.sp_model.piece_to_id(lowerCamelCase__ ) def A__ ( self , lowerCamelCase__ ) -> int: '''simple docstring''' lowercase__ = self.sp_model.IdToPiece(lowerCamelCase__ ) return token def A__ ( self , lowerCamelCase__ ) -> str: '''simple docstring''' lowercase__ = [] lowercase__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(lowerCamelCase__ ) + token lowercase__ = [] else: current_sub_tokens.append(lowerCamelCase__ ) out_string += self.sp_model.decode(lowerCamelCase__ ) return out_string.strip() def A__ ( self , lowerCamelCase__ , lowerCamelCase__=None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def A__ ( self , lowerCamelCase__ , lowerCamelCase__ = None , lowerCamelCase__ = 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__ ) lowercase__ = [1] if token_ids_a is None: return ([0] * len(lowerCamelCase__ )) + suffix_ones return ([0] * len(lowerCamelCase__ )) + ([0] * len(lowerCamelCase__ )) + suffix_ones def A__ ( self , lowerCamelCase__ , lowerCamelCase__ = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCamelCase__ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return lowercase__ = os.path.join( lowerCamelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase__ , """wb""" ) as fi: lowercase__ = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase__ ) return (out_vocab_file,)
164
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __a : List[str] = { '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''], '''tokenization_roformer''': ['''RoFormerTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a : str = ['''RoFormerTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a : int = [ '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RoFormerForCausalLM''', '''RoFormerForMaskedLM''', '''RoFormerForMultipleChoice''', '''RoFormerForQuestionAnswering''', '''RoFormerForSequenceClassification''', '''RoFormerForTokenClassification''', '''RoFormerLayer''', '''RoFormerModel''', '''RoFormerPreTrainedModel''', '''load_tf_weights_in_roformer''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a : List[str] = [ '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFRoFormerForCausalLM''', '''TFRoFormerForMaskedLM''', '''TFRoFormerForMultipleChoice''', '''TFRoFormerForQuestionAnswering''', '''TFRoFormerForSequenceClassification''', '''TFRoFormerForTokenClassification''', '''TFRoFormerLayer''', '''TFRoFormerModel''', '''TFRoFormerPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a : Union[str, Any] = [ '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxRoFormerForMaskedLM''', '''FlaxRoFormerForMultipleChoice''', '''FlaxRoFormerForQuestionAnswering''', '''FlaxRoFormerForSequenceClassification''', '''FlaxRoFormerForTokenClassification''', '''FlaxRoFormerModel''', '''FlaxRoFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys __a : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
371
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase_ : str = logging.get_logger(__name__) UpperCamelCase_ : Optional[Any] = { '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''', # See all SEW models at https://huggingface.co/models?filter=sew } class _a ( __lowerCAmelCase ): SCREAMING_SNAKE_CASE_ : Dict = """sew""" def __init__( self ,_SCREAMING_SNAKE_CASE=32 ,_SCREAMING_SNAKE_CASE=768 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=3_072 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE="gelu" ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.0 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.0_2 ,_SCREAMING_SNAKE_CASE=1e-5 ,_SCREAMING_SNAKE_CASE="group" ,_SCREAMING_SNAKE_CASE="gelu" ,_SCREAMING_SNAKE_CASE=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,_SCREAMING_SNAKE_CASE=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,_SCREAMING_SNAKE_CASE=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=128 ,_SCREAMING_SNAKE_CASE=16 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=0.0_5 ,_SCREAMING_SNAKE_CASE=10 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=0.0 ,_SCREAMING_SNAKE_CASE=10 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE="mean" ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=256 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE=1 ,_SCREAMING_SNAKE_CASE=2 ,**_SCREAMING_SNAKE_CASE ,) -> str: super().__init__(**_SCREAMING_SNAKE_CASE ,pad_token_id=_SCREAMING_SNAKE_CASE ,bos_token_id=_SCREAMING_SNAKE_CASE ,eos_token_id=_SCREAMING_SNAKE_CASE ) _snake_case = hidden_size _snake_case = feat_extract_norm _snake_case = feat_extract_activation _snake_case = list(_SCREAMING_SNAKE_CASE ) _snake_case = list(_SCREAMING_SNAKE_CASE ) _snake_case = list(_SCREAMING_SNAKE_CASE ) _snake_case = conv_bias _snake_case = num_conv_pos_embeddings _snake_case = num_conv_pos_embedding_groups _snake_case = len(self.conv_dim ) _snake_case = num_hidden_layers _snake_case = intermediate_size _snake_case = squeeze_factor _snake_case = hidden_act _snake_case = num_attention_heads _snake_case = hidden_dropout _snake_case = attention_dropout _snake_case = activation_dropout _snake_case = feat_proj_dropout _snake_case = final_dropout _snake_case = layerdrop _snake_case = layer_norm_eps _snake_case = initializer_range _snake_case = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( "Configuration for convolutional layers is incorrect." "It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`," f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" f"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _snake_case = apply_spec_augment _snake_case = mask_time_prob _snake_case = mask_time_length _snake_case = mask_time_min_masks _snake_case = mask_feature_prob _snake_case = mask_feature_length _snake_case = mask_feature_min_masks # ctc loss _snake_case = ctc_loss_reduction _snake_case = ctc_zero_infinity # sequence classification _snake_case = use_weighted_layer_sum _snake_case = classifier_proj_size @property def _lowercase ( self ) -> Optional[Any]: return functools.reduce(operator.mul ,self.conv_stride ,1 )
142
0
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _A ( __UpperCAmelCase ,unittest.TestCase ): UpperCamelCase__ : Any = LEDTokenizer UpperCamelCase__ : str = LEDTokenizerFast UpperCamelCase__ : List[Any] = True def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' super().setUp() __a = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] __a = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE)))) __a = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] __a = {'''unk_token''': '''<unk>'''} __a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file''']) __a = 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(__SCREAMING_SNAKE_CASE) + '''\n''') with open(self.merges_file , '''w''' , encoding='''utf-8''') as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE)) def _lowerCamelCase ( self : str , **__SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' kwargs.update(self.special_tokens_map) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : int , **__SCREAMING_SNAKE_CASE : int): '''simple docstring''' kwargs.update(self.special_tokens_map) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' return "lower newer", "lower newer" @cached_property def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' return LEDTokenizer.from_pretrained('''allenai/led-base-16384''') @cached_property def _lowerCamelCase ( self : Any): '''simple docstring''' return LEDTokenizerFast.from_pretrained('''allenai/led-base-16384''') @require_torch def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' __a = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] __a = [0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __a = tokenizer(__SCREAMING_SNAKE_CASE , max_length=len(__SCREAMING_SNAKE_CASE) , padding=__SCREAMING_SNAKE_CASE , return_tensors='''pt''') self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) self.assertEqual((2, 9) , batch.input_ids.shape) self.assertEqual((2, 9) , batch.attention_mask.shape) __a = batch.input_ids.tolist()[0] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) @require_torch def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __a = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors='''pt''') self.assertIn('''input_ids''' , __SCREAMING_SNAKE_CASE) self.assertIn('''attention_mask''' , __SCREAMING_SNAKE_CASE) self.assertNotIn('''labels''' , __SCREAMING_SNAKE_CASE) self.assertNotIn('''decoder_attention_mask''' , __SCREAMING_SNAKE_CASE) @require_torch def _lowerCamelCase ( self : List[Any]): '''simple docstring''' __a = [ '''Summary of the text.''', '''Another summary.''', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __a = tokenizer(text_target=__SCREAMING_SNAKE_CASE , max_length=32 , padding='''max_length''' , return_tensors='''pt''') self.assertEqual(32 , targets['''input_ids'''].shape[1]) @require_torch def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __a = tokenizer( ['''I am a small frog''' * 1_024, '''I am a small frog'''] , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''pt''') self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) self.assertEqual(batch.input_ids.shape , (2, 5_122)) @require_torch def _lowerCamelCase ( self : List[Any]): '''simple docstring''' __a = ['''A long paragraph for summarization.'''] __a = [ '''Summary of the text.''', ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __a = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''pt''') __a = tokenizer(text_target=__SCREAMING_SNAKE_CASE , return_tensors='''pt''') __a = inputs['''input_ids'''] __a = targets['''input_ids'''] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item()) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item()) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item()) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item()) @require_torch def _lowerCamelCase ( self : Tuple): '''simple docstring''' for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: __a = ['''Summary of the text.''', '''Another summary.'''] __a = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] __a = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE) __a = [[0] * len(__SCREAMING_SNAKE_CASE) for x in encoded_output['''input_ids''']] __a = tokenizer.pad(__SCREAMING_SNAKE_CASE) self.assertSequenceEqual(outputs['''global_attention_mask'''] , __SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Dict): '''simple docstring''' pass def _lowerCamelCase ( self : Tuple): '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})'): __a = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) __a = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) __a = '''A, <mask> AllenNLP sentence.''' __a = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE) __a = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE) self.assertEqual(sum(tokens_r['''token_type_ids''']) , sum(tokens_p['''token_type_ids'''])) self.assertEqual( sum(tokens_r['''attention_mask''']) / len(tokens_r['''attention_mask''']) , sum(tokens_p['''attention_mask''']) / len(tokens_p['''attention_mask''']) , ) __a = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids''']) __a = tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids''']) self.assertSequenceEqual(tokens_p['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>''']) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>'''])
49
import copy from typing import Dict, List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING UpperCAmelCase_ : Optional[int] = { 'facebook/mask2former-swin-small-coco-instance': ( 'https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json' ) # See all Mask2Former models at https://huggingface.co/models?filter=mask2former } UpperCAmelCase_ : List[str] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( lowercase__ ): snake_case__ : Any = '''mask2former''' snake_case__ : Any = ['''swin'''] snake_case__ : str = {'''hidden_size''': '''hidden_dim'''} def __init__( self : Any , SCREAMING_SNAKE_CASE__ : Optional[Dict] = None , SCREAMING_SNAKE_CASE__ : int = 2_5_6 , SCREAMING_SNAKE_CASE__ : int = 2_5_6 , SCREAMING_SNAKE_CASE__ : int = 2_5_6 , SCREAMING_SNAKE_CASE__ : int = 1_0_2_4 , SCREAMING_SNAKE_CASE__ : str = "relu" , SCREAMING_SNAKE_CASE__ : int = 6 , SCREAMING_SNAKE_CASE__ : int = 1_0 , SCREAMING_SNAKE_CASE__ : int = 8 , SCREAMING_SNAKE_CASE__ : float = 0.0 , SCREAMING_SNAKE_CASE__ : int = 2_0_4_8 , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : int = 4 , SCREAMING_SNAKE_CASE__ : int = 2_5_5 , SCREAMING_SNAKE_CASE__ : int = 1_0_0 , SCREAMING_SNAKE_CASE__ : float = 0.1 , SCREAMING_SNAKE_CASE__ : float = 2.0 , SCREAMING_SNAKE_CASE__ : float = 5.0 , SCREAMING_SNAKE_CASE__ : float = 5.0 , SCREAMING_SNAKE_CASE__ : int = 1_2_5_4_4 , SCREAMING_SNAKE_CASE__ : float = 3.0 , SCREAMING_SNAKE_CASE__ : float = 0.75 , SCREAMING_SNAKE_CASE__ : float = 0.02 , SCREAMING_SNAKE_CASE__ : float = 1.0 , SCREAMING_SNAKE_CASE__ : bool = True , SCREAMING_SNAKE_CASE__ : List[int] = [4, 8, 1_6, 3_2] , SCREAMING_SNAKE_CASE__ : bool = None , **SCREAMING_SNAKE_CASE__ : int , ) -> List[Any]: if backbone_config is None: logger.info('`backbone_config` is `None`. Initializing the config with the default `Swin` backbone.' ) a_ : Dict = CONFIG_MAPPING['swin']( image_size=2_2_4 , in_channels=3 , patch_size=4 , embed_dim=9_6 , depths=[2, 2, 1_8, 2] , num_heads=[3, 6, 1_2, 2_4] , window_size=7 , drop_path_rate=0.3 , use_absolute_embeddings=SCREAMING_SNAKE_CASE__ , out_features=['stage1', 'stage2', 'stage3', 'stage4'] , ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): a_ : Any = backbone_config.pop('model_type' ) a_ : Optional[Any] = CONFIG_MAPPING[backbone_model_type] a_ : List[str] = config_class.from_dict(SCREAMING_SNAKE_CASE__ ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( F"""Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. """ F"""Supported model types: {",".join(self.backbones_supported )}""" ) a_ : Dict = backbone_config a_ : List[str] = feature_size a_ : List[str] = mask_feature_size a_ : int = hidden_dim a_ : Dict = encoder_feedforward_dim a_ : str = activation_function a_ : List[str] = encoder_layers a_ : List[str] = decoder_layers a_ : Dict = num_attention_heads a_ : str = dropout a_ : Tuple = dim_feedforward a_ : List[str] = pre_norm a_ : Optional[int] = enforce_input_projection a_ : Any = common_stride a_ : Optional[int] = ignore_value a_ : int = num_queries a_ : Tuple = no_object_weight a_ : Dict = class_weight a_ : Optional[int] = mask_weight a_ : Optional[int] = dice_weight a_ : str = train_num_points a_ : List[str] = oversample_ratio a_ : List[Any] = importance_sample_ratio a_ : Any = init_std a_ : Union[str, Any] = init_xavier_std a_ : Union[str, Any] = use_auxiliary_loss a_ : Dict = feature_strides a_ : List[str] = output_auxiliary_logits a_ : Dict = decoder_layers super().__init__(**SCREAMING_SNAKE_CASE__ ) @classmethod def SCREAMING_SNAKE_CASE ( cls : str , SCREAMING_SNAKE_CASE__ : PretrainedConfig , **SCREAMING_SNAKE_CASE__ : Union[str, Any] ) -> List[Any]: return cls( backbone_config=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, any]: a_ : Optional[int] = copy.deepcopy(self.__dict__ ) a_ : List[Any] = self.backbone_config.to_dict() a_ : Optional[Any] = self.__class__.model_type return output
32
0
import copy import json import os import tempfile from transformers import is_torch_available from .test_configuration_utils import config_common_kwargs class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Dict , _lowerCAmelCase : List[Any] , _lowerCAmelCase : int=None , _lowerCAmelCase : Dict=True , _lowerCAmelCase : int=None , **_lowerCAmelCase : str ): SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = config_class SCREAMING_SNAKE_CASE_ = has_text_modality SCREAMING_SNAKE_CASE_ = kwargs SCREAMING_SNAKE_CASE_ = common_properties def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict ) SCREAMING_SNAKE_CASE_ = ( ['hidden_size', 'num_attention_heads', 'num_hidden_layers'] if self.common_properties is None else self.common_properties ) # Add common fields for text models if self.has_text_modality: common_properties.extend(['vocab_size'] ) # Test that config has the common properties as getters for prop in common_properties: self.parent.assertTrue(hasattr(_lowerCAmelCase , _lowerCAmelCase ) , msg=F"`{prop}` does not exist" ) # Test that config has the common properties as setter for idx, name in enumerate(_lowerCAmelCase ): try: setattr(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) self.parent.assertEqual( getattr(_lowerCAmelCase , _lowerCAmelCase ) , _lowerCAmelCase , msg=F"`{name} value {idx} expected, but was {getattr(_lowerCAmelCase , _lowerCAmelCase )}" ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass # Test if config class can be called with Config(prop_name=..) for idx, name in enumerate(_lowerCAmelCase ): try: SCREAMING_SNAKE_CASE_ = self.config_class(**{name: idx} ) self.parent.assertEqual( getattr(_lowerCAmelCase , _lowerCAmelCase ) , _lowerCAmelCase , msg=F"`{name} value {idx} expected, but was {getattr(_lowerCAmelCase , _lowerCAmelCase )}" ) except NotImplementedError: # Some models might not be able to implement setters for common_properties # In that case, a NotImplementedError is raised pass def lowerCAmelCase_ ( self : List[Any] ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict ) SCREAMING_SNAKE_CASE_ = json.loads(config.to_json_string() ) for key, value in self.inputs_dict.items(): self.parent.assertEqual(obj[key] , _lowerCAmelCase ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = os.path.join(_lowerCAmelCase , 'config.json' ) config_first.to_json_file(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.config_class.from_json_file(_lowerCAmelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def lowerCAmelCase_ ( self : str ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict ) with tempfile.TemporaryDirectory() as tmpdirname: config_first.save_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.config_class.from_pretrained(_lowerCAmelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def lowerCAmelCase_ ( self : List[str] ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict ) SCREAMING_SNAKE_CASE_ = 'test' with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = os.path.join(_lowerCAmelCase , _lowerCAmelCase ) config_first.save_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.config_class.from_pretrained(_lowerCAmelCase , subfolder=_lowerCAmelCase ) self.parent.assertEqual(config_second.to_dict() , config_first.to_dict() ) def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.config_class(**self.inputs_dict , num_labels=5 ) self.parent.assertEqual(len(config.idalabel ) , 5 ) self.parent.assertEqual(len(config.labelaid ) , 5 ) SCREAMING_SNAKE_CASE_ = 3 self.parent.assertEqual(len(config.idalabel ) , 3 ) self.parent.assertEqual(len(config.labelaid ) , 3 ) def lowerCAmelCase_ ( self : Any ): if self.config_class.is_composition: return SCREAMING_SNAKE_CASE_ = self.config_class() self.parent.assertIsNotNone(_lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = copy.deepcopy(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.config_class(**_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [] for key, value in config_common_kwargs.items(): if key == "torch_dtype": if not is_torch_available(): continue else: import torch if config.torch_dtype != torch.floataa: wrong_values.append(('torch_dtype', config.torch_dtype, torch.floataa) ) elif getattr(_lowerCAmelCase , _lowerCAmelCase ) != value: wrong_values.append((key, getattr(_lowerCAmelCase , _lowerCAmelCase ), value) ) if len(_lowerCAmelCase ) > 0: SCREAMING_SNAKE_CASE_ = '\n'.join([F"- {v[0]}: got {v[1]} instead of {v[2]}" for v in wrong_values] ) raise ValueError(F"The following keys were not properly set in the config:\n{errors}" ) def lowerCAmelCase_ ( self : str ): self.create_and_test_config_common_properties() self.create_and_test_config_to_json_string() self.create_and_test_config_to_json_file() self.create_and_test_config_from_and_save_pretrained() self.create_and_test_config_from_and_save_pretrained_subfolder() self.create_and_test_config_with_num_labels() self.check_config_can_be_init_without_params() self.check_config_arguments_init()
210
def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> str: SCREAMING_SNAKE_CASE_ = '' for ch in key: if ch == " " or ch not in key_no_dups and ch.isalpha(): key_no_dups += ch return key_no_dups def UpperCAmelCase_ ( __UpperCAmelCase : str ) -> dict[str, str]: SCREAMING_SNAKE_CASE_ = [chr(i + 65 ) for i in range(26 )] # Remove duplicate characters from key SCREAMING_SNAKE_CASE_ = remove_duplicates(key.upper() ) SCREAMING_SNAKE_CASE_ = len(__UpperCAmelCase ) # First fill cipher with key characters SCREAMING_SNAKE_CASE_ = {alphabet[i]: char for i, char in enumerate(__UpperCAmelCase )} # Then map remaining characters in alphabet to # the alphabet from the beginning for i in range(len(__UpperCAmelCase ) , 26 ): SCREAMING_SNAKE_CASE_ = alphabet[i - offset] # Ensure we are not mapping letters to letters previously mapped while char in key: offset -= 1 SCREAMING_SNAKE_CASE_ = alphabet[i - offset] SCREAMING_SNAKE_CASE_ = char return cipher_alphabet def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : dict[str, str] ) -> str: return "".join(cipher_map.get(__UpperCAmelCase , __UpperCAmelCase ) for ch in message.upper() ) def UpperCAmelCase_ ( __UpperCAmelCase : str , __UpperCAmelCase : dict[str, str] ) -> str: SCREAMING_SNAKE_CASE_ = {v: k for k, v in cipher_map.items()} return "".join(rev_cipher_map.get(__UpperCAmelCase , __UpperCAmelCase ) for ch in message.upper() ) def UpperCAmelCase_ ( ) -> None: SCREAMING_SNAKE_CASE_ = input('Enter message to encode or decode: ' ).strip() SCREAMING_SNAKE_CASE_ = input('Enter keyword: ' ).strip() SCREAMING_SNAKE_CASE_ = input('Encipher or decipher? E/D:' ).strip()[0].lower() try: SCREAMING_SNAKE_CASE_ = {'e': encipher, 'd': decipher}[option] except KeyError: raise KeyError('invalid input option' ) SCREAMING_SNAKE_CASE_ = create_cipher_map(__UpperCAmelCase ) print(func(__UpperCAmelCase , __UpperCAmelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
210
1
from itertools import count def lowerCAmelCase_ ( __UpperCAmelCase: int = 50 ) -> int: UpperCamelCase__ : Dict = [1] * min_block_length for n in count(__UpperCAmelCase ): fill_count_functions.append(1 ) for block_length in range(__UpperCAmelCase , n + 1 ): for block_start in range(n - block_length ): fill_count_functions[n] += fill_count_functions[ n - block_start - block_length - 1 ] fill_count_functions[n] += 1 if fill_count_functions[n] > 100_0000: break return n if __name__ == "__main__": print(F'''{solution() = }''')
201
from sympy import diff, lambdify, symbols from sympy.functions import * # noqa: F403 def lowerCAmelCase_ ( __UpperCAmelCase: str , __UpperCAmelCase: complex , __UpperCAmelCase: str = "x" , __UpperCAmelCase: float = 10**-10 , __UpperCAmelCase: int = 1 , ) -> complex: UpperCamelCase__ : Optional[int] = symbols(__UpperCAmelCase ) UpperCamelCase__ : Dict = lambdify(__UpperCAmelCase , __UpperCAmelCase ) UpperCamelCase__ : List[str] = lambdify(__UpperCAmelCase , diff(__UpperCAmelCase , __UpperCAmelCase ) ) UpperCamelCase__ : int = starting_point while True: if diff_function(__UpperCAmelCase ) != 0: UpperCamelCase__ : Optional[int] = prev_guess - multiplicity * func(__UpperCAmelCase ) / diff_function( __UpperCAmelCase ) else: raise ZeroDivisionError('''Could not find root''' ) from None # Precision is checked by comparing the difference of consecutive guesses if abs(next_guess - prev_guess ) < precision: return next_guess UpperCamelCase__ : Tuple = next_guess # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial # Find fourth Root of 5 print(F'''The root of x**4 - 5 = 0 is {newton_raphson("x**4 -5", 0.4 +5J)}''') # Find value of e print( 'The root of log(y) - 1 = 0 is ', F'''{newton_raphson("log(y) - 1", 2, variable="y")}''', ) # Exponential Roots print( 'The root of exp(x) - 1 = 0 is', F'''{newton_raphson("exp(x) - 1", 10, precision=0.005)}''', ) # Find root of cos(x) print(F'''The root of cos(x) = 0 is {newton_raphson("cos(x)", 0)}''')
201
1
"""simple docstring""" import math from enum import Enum from typing import Optional, Union from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .utils import logging A_ : Union[str, Any] =logging.get_logger(__name__) class __a ( _UpperCamelCase ): SCREAMING_SNAKE_CASE__ : Any = 'linear' SCREAMING_SNAKE_CASE__ : int = 'cosine' SCREAMING_SNAKE_CASE__ : str = 'cosine_with_restarts' SCREAMING_SNAKE_CASE__ : int = 'polynomial' SCREAMING_SNAKE_CASE__ : str = 'constant' SCREAMING_SNAKE_CASE__ : Optional[Any] = 'constant_with_warmup' SCREAMING_SNAKE_CASE__ : Tuple = 'piecewise_constant' def SCREAMING_SNAKE_CASE_ ( snake_case : Optimizer , snake_case : int = -1 )-> Any: return LambdaLR(_UpperCamelCase , lambda snake_case : 1 , last_epoch=_UpperCamelCase ) def SCREAMING_SNAKE_CASE_ ( snake_case : Optimizer , snake_case : int , snake_case : int = -1 )-> List[str]: def lr_lambda(snake_case : int ): if current_step < num_warmup_steps: return float(_UpperCamelCase ) / float(max(1.0 , _UpperCamelCase ) ) return 1.0 return LambdaLR(_UpperCamelCase , _UpperCamelCase , last_epoch=_UpperCamelCase ) def SCREAMING_SNAKE_CASE_ ( snake_case : Optimizer , snake_case : str , snake_case : int = -1 )-> Optional[int]: _lowerCamelCase = {} _lowerCamelCase = step_rules.split(',' ) for rule_str in rule_list[:-1]: _lowerCamelCase , _lowerCamelCase = rule_str.split(':' ) _lowerCamelCase = int(_UpperCamelCase ) _lowerCamelCase = float(_UpperCamelCase ) _lowerCamelCase = value _lowerCamelCase = float(rule_list[-1] ) def create_rules_function(snake_case : Union[str, Any] , snake_case : Any ): def rule_func(snake_case : int ) -> float: _lowerCamelCase = sorted(rules_dict.keys() ) for i, sorted_step in enumerate(_UpperCamelCase ): if steps < sorted_step: return rules_dict[sorted_steps[i]] return last_lr_multiple return rule_func _lowerCamelCase = create_rules_function(_UpperCamelCase , _UpperCamelCase ) return LambdaLR(_UpperCamelCase , _UpperCamelCase , last_epoch=_UpperCamelCase ) def SCREAMING_SNAKE_CASE_ ( snake_case : Any , snake_case : Optional[Any] , snake_case : Any , snake_case : Optional[int]=-1 )-> int: def lr_lambda(snake_case : int ): if current_step < num_warmup_steps: return float(_UpperCamelCase ) / float(max(1 , _UpperCamelCase ) ) return max( 0.0 , float(num_training_steps - current_step ) / float(max(1 , num_training_steps - num_warmup_steps ) ) ) return LambdaLR(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def SCREAMING_SNAKE_CASE_ ( snake_case : Optimizer , snake_case : int , snake_case : int , snake_case : float = 0.5 , snake_case : int = -1 )-> str: def lr_lambda(snake_case : Optional[int] ): if current_step < num_warmup_steps: return float(_UpperCamelCase ) / float(max(1 , _UpperCamelCase ) ) _lowerCamelCase = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * float(_UpperCamelCase ) * 2.0 * progress )) ) return LambdaLR(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def SCREAMING_SNAKE_CASE_ ( snake_case : Optimizer , snake_case : int , snake_case : int , snake_case : int = 1 , snake_case : int = -1 )-> Any: def lr_lambda(snake_case : List[Any] ): if current_step < num_warmup_steps: return float(_UpperCamelCase ) / float(max(1 , _UpperCamelCase ) ) _lowerCamelCase = float(current_step - num_warmup_steps ) / float(max(1 , num_training_steps - num_warmup_steps ) ) if progress >= 1.0: return 0.0 return max(0.0 , 0.5 * (1.0 + math.cos(math.pi * ((float(_UpperCamelCase ) * progress) % 1.0) )) ) return LambdaLR(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def SCREAMING_SNAKE_CASE_ ( snake_case : List[str] , snake_case : Optional[int] , snake_case : List[Any] , snake_case : Dict=1e-7 , snake_case : Dict=1.0 , snake_case : Tuple=-1 )-> Optional[Any]: _lowerCamelCase = optimizer.defaults['lr'] if not (lr_init > lr_end): raise ValueError(f'lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})' ) def lr_lambda(snake_case : int ): if current_step < num_warmup_steps: return float(_UpperCamelCase ) / float(max(1 , _UpperCamelCase ) ) elif current_step > num_training_steps: return lr_end / lr_init # as LambdaLR multiplies by lr_init else: _lowerCamelCase = lr_init - lr_end _lowerCamelCase = num_training_steps - num_warmup_steps _lowerCamelCase = 1 - (current_step - num_warmup_steps) / decay_steps _lowerCamelCase = lr_range * pct_remaining**power + lr_end return decay / lr_init # as LambdaLR multiplies by lr_init return LambdaLR(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) A_ : List[Any] ={ SchedulerType.LINEAR: get_linear_schedule_with_warmup, SchedulerType.COSINE: get_cosine_schedule_with_warmup, SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup, SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup, SchedulerType.CONSTANT: get_constant_schedule, SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup, SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule, } def SCREAMING_SNAKE_CASE_ ( snake_case : Union[str, SchedulerType] , snake_case : Optimizer , snake_case : Optional[str] = None , snake_case : Optional[int] = None , snake_case : Optional[int] = None , snake_case : int = 1 , snake_case : float = 1.0 , snake_case : int = -1 , )-> List[str]: _lowerCamelCase = SchedulerType(_UpperCamelCase ) _lowerCamelCase = TYPE_TO_SCHEDULER_FUNCTION[name] if name == SchedulerType.CONSTANT: return schedule_func(_UpperCamelCase , last_epoch=_UpperCamelCase ) if name == SchedulerType.PIECEWISE_CONSTANT: return schedule_func(_UpperCamelCase , step_rules=_UpperCamelCase , last_epoch=_UpperCamelCase ) # All other schedulers require `num_warmup_steps` if num_warmup_steps is None: raise ValueError(f'{name} requires `num_warmup_steps`, please provide that argument.' ) if name == SchedulerType.CONSTANT_WITH_WARMUP: return schedule_func(_UpperCamelCase , num_warmup_steps=_UpperCamelCase , last_epoch=_UpperCamelCase ) # All other schedulers require `num_training_steps` if num_training_steps is None: raise ValueError(f'{name} requires `num_training_steps`, please provide that argument.' ) if name == SchedulerType.COSINE_WITH_RESTARTS: return schedule_func( _UpperCamelCase , num_warmup_steps=_UpperCamelCase , num_training_steps=_UpperCamelCase , num_cycles=_UpperCamelCase , last_epoch=_UpperCamelCase , ) if name == SchedulerType.POLYNOMIAL: return schedule_func( _UpperCamelCase , num_warmup_steps=_UpperCamelCase , num_training_steps=_UpperCamelCase , power=_UpperCamelCase , last_epoch=_UpperCamelCase , ) return schedule_func( _UpperCamelCase , num_warmup_steps=_UpperCamelCase , num_training_steps=_UpperCamelCase , last_epoch=_UpperCamelCase )
368
"""simple docstring""" import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor A_ : Union[str, Any] =logging.get_logger(__name__) class __a ( lowerCAmelCase__ ): def __init__( self , *a__ , **a__ ): warnings.warn( 'The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use DPTImageProcessor instead.' , a__ , ) super().__init__(*a__ , **a__ )
80
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class _snake_case ( snake_case ): UpperCamelCase__ = 'Salesforce/blip-image-captioning-base' UpperCamelCase__ = ( 'This is a tool that generates a description of an image. It takes an input named `image` which should be the ' 'image to caption, and returns a text that contains the description in English.' ) UpperCamelCase__ = 'image_captioner' UpperCamelCase__ = AutoModelForVisionaSeq UpperCamelCase__ = ['image'] UpperCamelCase__ = ['text'] def __init__( self , *_a , **_a ): requires_backends(self , ["vision"] ) super().__init__(*_a , **_a ) def SCREAMING_SNAKE_CASE ( self , _a ): return self.pre_processor(images=_a , return_tensors="pt" ) def SCREAMING_SNAKE_CASE ( self , _a ): return self.model.generate(**_a ) def SCREAMING_SNAKE_CASE ( self , _a ): return self.pre_processor.batch_decode(_a , skip_special_tokens=_a )[0].strip()
281
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO ) snake_case : Optional[int] = logging.getLogger(__name__) def lowerCAmelCase_ ( _snake_case : Dict , _snake_case : Union[str, Any] ) -> Tuple: '''simple docstring''' __magic_name__ : List[str] = np.argmax(_snake_case , axis=1 ) return np.sum(outputs == labels ) def lowerCAmelCase_ ( _snake_case : Optional[Any] ) -> Dict: '''simple docstring''' with open(_snake_case , encoding="utf_8" ) as f: __magic_name__ : List[str] = csv.reader(_snake_case ) __magic_name__ : List[Any] = [] next(_snake_case ) # skip the first line for line in tqdm(_snake_case ): output.append((" ".join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def lowerCAmelCase_ ( _snake_case : str , _snake_case : Tuple , _snake_case : Union[str, Any] , _snake_case : List[Any] , _snake_case : Tuple , _snake_case : Optional[int] ) -> int: '''simple docstring''' __magic_name__ : Optional[int] = [] for dataset in encoded_datasets: __magic_name__ : Union[str, Any] = len(_snake_case ) __magic_name__ : Dict = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) __magic_name__ : List[str] = np.zeros((n_batch, 2) , dtype=np.intaa ) __magic_name__ : Optional[int] = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) __magic_name__ : int = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(_snake_case ): __magic_name__ : Dict = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __magic_name__ : Dict = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] __magic_name__ : str = with_conta __magic_name__ : Tuple = with_conta __magic_name__ : Union[str, Any] = len(_snake_case ) - 1 __magic_name__ : int = len(_snake_case ) - 1 __magic_name__ : Optional[Any] = with_conta __magic_name__ : Optional[Any] = with_conta __magic_name__ : Optional[int] = mc_label __magic_name__ : str = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(_snake_case ) for t in all_inputs ) ) return tensor_datasets def lowerCAmelCase_ ( ) -> List[Any]: '''simple docstring''' __magic_name__ : Any = argparse.ArgumentParser() parser.add_argument("--model_name" , type=_snake_case , default="openai-gpt" , help="pretrained model name" ) parser.add_argument("--do_train" , action="store_true" , help="Whether to run training." ) parser.add_argument("--do_eval" , action="store_true" , help="Whether to run eval on the dev set." ) parser.add_argument( "--output_dir" , default=_snake_case , type=_snake_case , required=_snake_case , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument("--train_dataset" , type=_snake_case , default="" ) parser.add_argument("--eval_dataset" , type=_snake_case , default="" ) parser.add_argument("--seed" , type=_snake_case , default=42 ) parser.add_argument("--num_train_epochs" , type=_snake_case , default=3 ) parser.add_argument("--train_batch_size" , type=_snake_case , default=8 ) parser.add_argument("--eval_batch_size" , type=_snake_case , default=16 ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=_snake_case , help="Epsilon for Adam optimizer." ) parser.add_argument("--max_grad_norm" , type=_snake_case , default=1 ) parser.add_argument( "--max_steps" , default=-1 , type=_snake_case , help=( "If > 0: set total number of training steps to perform. Override num_train_epochs." ) , ) parser.add_argument( "--gradient_accumulation_steps" , type=_snake_case , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--learning_rate" , type=_snake_case , default=6.25E-5 ) parser.add_argument("--warmup_steps" , default=0 , type=_snake_case , help="Linear warmup over warmup_steps." ) parser.add_argument("--lr_schedule" , type=_snake_case , default="warmup_linear" ) parser.add_argument("--weight_decay" , type=_snake_case , default=0.01 ) parser.add_argument("--lm_coef" , type=_snake_case , default=0.9 ) parser.add_argument("--n_valid" , type=_snake_case , default=374 ) parser.add_argument("--server_ip" , type=_snake_case , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=_snake_case , default="" , help="Can be used for distant debugging." ) __magic_name__ : List[Any] = parser.parse_args() print(_snake_case ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("Waiting for debugger attach" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=_snake_case ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) __magic_name__ : Dict = torch.device("cuda" if torch.cuda.is_available() else "cpu" ) __magic_name__ : Optional[int] = torch.cuda.device_count() logger.info("device: {}, n_gpu {}".format(_snake_case , _snake_case ) ) if not args.do_train and not args.do_eval: raise ValueError("At least one of `do_train` or `do_eval` must be True." ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset __magic_name__ : List[Any] = ["_start_", "_delimiter_", "_classify_"] __magic_name__ : Optional[int] = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(_snake_case ) __magic_name__ : Optional[Any] = tokenizer.convert_tokens_to_ids(_snake_case ) __magic_name__ : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(_snake_case ) ) model.to(_snake_case ) # Load and encode the datasets def tokenize_and_encode(_snake_case : str ): if isinstance(_snake_case , _snake_case ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(_snake_case ) ) elif isinstance(_snake_case , _snake_case ): return obj return [tokenize_and_encode(_snake_case ) for o in obj] logger.info("Encoding dataset..." ) __magic_name__ : Optional[int] = load_rocstories_dataset(args.train_dataset ) __magic_name__ : str = load_rocstories_dataset(args.eval_dataset ) __magic_name__ : int = (train_dataset, eval_dataset) __magic_name__ : List[str] = tokenize_and_encode(_snake_case ) # Compute the max input length for the Transformer __magic_name__ : Optional[Any] = model.config.n_positions // 2 - 2 __magic_name__ : Optional[int] = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) __magic_name__ : List[str] = min(_snake_case , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders __magic_name__ : List[Any] = pre_process_datasets(_snake_case , _snake_case , _snake_case , *_snake_case ) __magic_name__ , __magic_name__ : Optional[int] = tensor_datasets[0], tensor_datasets[1] __magic_name__ : Tuple = TensorDataset(*_snake_case ) __magic_name__ : Union[str, Any] = RandomSampler(_snake_case ) __magic_name__ : Dict = DataLoader(_snake_case , sampler=_snake_case , batch_size=args.train_batch_size ) __magic_name__ : Any = TensorDataset(*_snake_case ) __magic_name__ : Optional[Any] = SequentialSampler(_snake_case ) __magic_name__ : int = DataLoader(_snake_case , sampler=_snake_case , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: __magic_name__ : Tuple = args.max_steps __magic_name__ : List[str] = args.max_steps // (len(_snake_case ) // args.gradient_accumulation_steps) + 1 else: __magic_name__ : List[str] = len(_snake_case ) // args.gradient_accumulation_steps * args.num_train_epochs __magic_name__ : str = list(model.named_parameters() ) __magic_name__ : Dict = ["bias", "LayerNorm.bias", "LayerNorm.weight"] __magic_name__ : str = [ { "params": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], "weight_decay": args.weight_decay, }, {"params": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], "weight_decay": 0.0}, ] __magic_name__ : str = AdamW(_snake_case , lr=args.learning_rate , eps=args.adam_epsilon ) __magic_name__ : List[str] = get_linear_schedule_with_warmup( _snake_case , num_warmup_steps=args.warmup_steps , num_training_steps=_snake_case ) if args.do_train: __magic_name__ , __magic_name__ , __magic_name__ : Union[str, Any] = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="Epoch" ): __magic_name__ : List[str] = 0 __magic_name__ : Tuple = 0 __magic_name__ : Dict = tqdm(_snake_case , desc="Training" ) for step, batch in enumerate(_snake_case ): __magic_name__ : Optional[Any] = tuple(t.to(_snake_case ) for t in batch ) __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ : Dict = batch __magic_name__ : Optional[Any] = model(_snake_case , mc_token_ids=_snake_case , lm_labels=_snake_case , mc_labels=_snake_case ) __magic_name__ : Optional[Any] = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() __magic_name__ : List[str] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 __magic_name__ : int = "Training loss: {:.2e} lr: {:.2e}".format(_snake_case , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer __magic_name__ : Dict = model.module if hasattr(_snake_case , "module" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` __magic_name__ : List[Any] = os.path.join(args.output_dir , _snake_case ) __magic_name__ : Dict = os.path.join(args.output_dir , _snake_case ) torch.save(model_to_save.state_dict() , _snake_case ) model_to_save.config.to_json_file(_snake_case ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned __magic_name__ : Dict = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) __magic_name__ : Optional[int] = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(_snake_case ) if args.do_eval: model.eval() __magic_name__ , __magic_name__ : Any = 0, 0 __magic_name__ , __magic_name__ : Union[str, Any] = 0, 0 for batch in tqdm(_snake_case , desc="Evaluating" ): __magic_name__ : int = tuple(t.to(_snake_case ) for t in batch ) __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ : Union[str, Any] = batch with torch.no_grad(): __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ : Dict = model( _snake_case , mc_token_ids=_snake_case , lm_labels=_snake_case , mc_labels=_snake_case ) __magic_name__ : Tuple = mc_logits.detach().cpu().numpy() __magic_name__ : Any = mc_labels.to("cpu" ).numpy() __magic_name__ : str = accuracy(_snake_case , _snake_case ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 __magic_name__ : Tuple = eval_loss / nb_eval_steps __magic_name__ : List[Any] = eval_accuracy / nb_eval_examples __magic_name__ : int = tr_loss / nb_tr_steps if args.do_train else None __magic_name__ : Any = {"eval_loss": eval_loss, "eval_accuracy": eval_accuracy, "train_loss": train_loss} __magic_name__ : int = os.path.join(args.output_dir , "eval_results.txt" ) with open(_snake_case , "w" ) as writer: logger.info("***** Eval results *****" ) for key in sorted(result.keys() ): logger.info(" %s = %s" , _snake_case , str(result[key] ) ) writer.write("%s = %s\n" % (key, str(result[key] )) ) if __name__ == "__main__": main()
281
1
from collections import Counter from timeit import timeit def lowerCamelCase ( a_ = "" , ) -> List[Any]: return sum(c % 2 for c in Counter(input_str.replace(' ' , '' ).lower() ).values() ) < 2 def lowerCamelCase ( a_ = "" ) -> Optional[Any]: if len(lowerCamelCase_ ) == 0: return True lowerCAmelCase_ = input_str.replace(' ' , '' ).lower() # character_freq_dict: Stores the frequency of every character in the input string lowerCAmelCase_ = {} for character in lower_case_input_str: lowerCAmelCase_ = character_freq_dict.get(lowerCamelCase_ , 0 ) + 1 lowerCAmelCase_ = 0 for character_count in character_freq_dict.values(): if character_count % 2: odd_char += 1 if odd_char > 1: return False return True def lowerCamelCase ( a_ = "" ) -> Any: print('\nFor string = ' , lowerCamelCase_ , ':' ) print( '> can_string_be_rearranged_as_palindrome_counter()' , '\tans =' , can_string_be_rearranged_as_palindrome_counter(lowerCamelCase_ ) , '\ttime =' , timeit( 'z.can_string_be_rearranged_as_palindrome_counter(z.check_str)' , setup='import __main__ as z' , ) , 'seconds' , ) print( '> can_string_be_rearranged_as_palindrome()' , '\tans =' , can_string_be_rearranged_as_palindrome(lowerCamelCase_ ) , '\ttime =' , timeit( 'z.can_string_be_rearranged_as_palindrome(z.check_str)' , setup='import __main__ as z' , ) , 'seconds' , ) if __name__ == "__main__": lowerCamelCase_ = input( """Enter string to determine if it can be rearranged as a palindrome or not: """ ).strip() benchmark(check_str) lowerCamelCase_ = can_string_be_rearranged_as_palindrome_counter(check_str) print(f'''{check_str} can {"" if status else "not "}be rearranged as a palindrome''')
358
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { """shi-labs/nat-mini-in1k-224""": """https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json""", # See all Nat models at https://huggingface.co/models?filter=nat } class a_ ( a_ , a_ ): '''simple docstring''' __a: Optional[Any] = '''nat''' __a: int = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , lowercase_=4 , lowercase_=3 , lowercase_=6_4 , lowercase_=[3, 4, 6, 5] , lowercase_=[2, 4, 8, 1_6] , lowercase_=7 , lowercase_=3.0 , lowercase_=True , lowercase_=0.0 , lowercase_=0.0 , lowercase_=0.1 , lowercase_="gelu" , lowercase_=0.02 , lowercase_=1e-5 , lowercase_=0.0 , lowercase_=None , lowercase_=None , **lowercase_ , ) -> Optional[int]: '''simple docstring''' super().__init__(**lowercase_ ) lowerCAmelCase_ = patch_size lowerCAmelCase_ = num_channels lowerCAmelCase_ = embed_dim lowerCAmelCase_ = depths lowerCAmelCase_ = len(lowercase_ ) lowerCAmelCase_ = num_heads lowerCAmelCase_ = kernel_size lowerCAmelCase_ = mlp_ratio lowerCAmelCase_ = qkv_bias lowerCAmelCase_ = hidden_dropout_prob lowerCAmelCase_ = attention_probs_dropout_prob lowerCAmelCase_ = drop_path_rate lowerCAmelCase_ = hidden_act lowerCAmelCase_ = layer_norm_eps lowerCAmelCase_ = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCAmelCase_ = int(embed_dim * 2 ** (len(lowercase_ ) - 1) ) lowerCAmelCase_ = layer_scale_init_value lowerCAmelCase_ = ['stem'] + [f'''stage{idx}''' for idx in range(1 , len(lowercase_ ) + 1 )] lowerCAmelCase_ , lowerCAmelCase_ = get_aligned_output_features_output_indices( out_features=lowercase_ , out_indices=lowercase_ , stage_names=self.stage_names )
14
0
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=0 ) -> str: # Format the message. if name is None: __lowerCamelCase = None else: __lowerCamelCase = '''.''' * max(0 , spaces - 2 ) + '''# {:''' + str(50 - spaces ) + '''s}''' __lowerCamelCase = fmt.format(UpperCamelCase__ ) # Print and recurse (if needed). if isinstance(UpperCamelCase__ , UpperCamelCase__ ): if msg is not None: print(UpperCamelCase__ ) for k in val.keys(): recursive_print(UpperCamelCase__ , val[k] , spaces + 2 ) elif isinstance(UpperCamelCase__ , torch.Tensor ): print(UpperCamelCase__ , ''':''' , val.size() ) else: print(UpperCamelCase__ , ''':''' , UpperCamelCase__ ) def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. __lowerCamelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] __lowerCamelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] __lowerCamelCase = param.view(*UpperCamelCase__ ) __lowerCamelCase = param.transpose(0 , 2 ) __lowerCamelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] __lowerCamelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] __lowerCamelCase = param.view(*UpperCamelCase__ ) __lowerCamelCase = param.transpose(0 , 1 ).contiguous() __lowerCamelCase = param.view(*UpperCamelCase__ ) return param def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Any: # The converted output model. __lowerCamelCase = {} # old versions did not store training args __lowerCamelCase = input_state_dict.get('''args''' , UpperCamelCase__ ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) __lowerCamelCase = ds_args.padded_vocab_size __lowerCamelCase = ds_args.max_position_embeddings __lowerCamelCase = ds_args.hidden_size __lowerCamelCase = ds_args.num_layers __lowerCamelCase = ds_args.num_attention_heads __lowerCamelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. __lowerCamelCase = config.n_head # The hidden_size per head. __lowerCamelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): __lowerCamelCase = input_state_dict['''checkpoint_version'''] else: __lowerCamelCase = 0.0 # The model. __lowerCamelCase = input_state_dict['''model'''] # The language model. __lowerCamelCase = model['''language_model'''] # The embeddings. __lowerCamelCase = lm['''embedding'''] # The word embeddings. __lowerCamelCase = embeddings['''word_embeddings''']['''weight'''] # Truncate the embedding table to vocab_size rows. __lowerCamelCase = word_embeddings[: config.vocab_size, :] __lowerCamelCase = word_embeddings # The position embeddings. __lowerCamelCase = embeddings['''position_embeddings''']['''weight'''] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] __lowerCamelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f"""pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don't match""" ) # Store the position embeddings. __lowerCamelCase = pos_embeddings # The transformer. __lowerCamelCase = lm['''transformer'''] if '''transformer''' in lm.keys() else lm['''encoder'''] # The regex to extract layer names. __lowerCamelCase = re.compile(r'''layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)''' ) # The simple map of names for "automated" rules. __lowerCamelCase = { '''attention.dense''': '''.attn.c_proj.''', '''self_attention.dense''': '''.attn.c_proj.''', '''mlp.dense_h_to_4h''': '''.mlp.c_fc.''', '''mlp.dense_4h_to_h''': '''.mlp.c_proj.''', } # Extract the layers. for key, val in transformer.items(): # Match the name. __lowerCamelCase = layer_re.match(UpperCamelCase__ ) # Stop if that's not a layer if m is None: break # The index of the layer. __lowerCamelCase = int(m.group(1 ) ) # The name of the operation. __lowerCamelCase = m.group(2 ) # Is it a weight or a bias? __lowerCamelCase = m.group(3 ) # The name of the layer. __lowerCamelCase = f"""transformer.h.{layer_idx}""" # For layernorm(s), simply store the layer norm. if op_name.endswith('''layernorm''' ): __lowerCamelCase = '''ln_1''' if op_name.startswith('''input''' ) else '''ln_2''' __lowerCamelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. __lowerCamelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = causal_mask # Insert a "dummy" tensor for masked_bias. __lowerCamelCase = torch.tensor(-1E4 , dtype=torch.floataa ) __lowerCamelCase = masked_bias __lowerCamelCase = fix_query_key_value_ordering(UpperCamelCase__ , UpperCamelCase__ , 3 , UpperCamelCase__ , UpperCamelCase__ ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. __lowerCamelCase = out_val.transpose(0 , 1 ).contiguous() # Store. __lowerCamelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": __lowerCamelCase = fix_query_key_value_ordering(UpperCamelCase__ , UpperCamelCase__ , 3 , UpperCamelCase__ , UpperCamelCase__ ) # Store. No change of shape. __lowerCamelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": __lowerCamelCase = megatron_to_transformers[op_name] __lowerCamelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": __lowerCamelCase = megatron_to_transformers[op_name] __lowerCamelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. __lowerCamelCase = transformer['''final_layernorm.weight'''] __lowerCamelCase = transformer['''final_layernorm.bias'''] # For LM head, transformers' wants the matrix to weight embeddings. __lowerCamelCase = word_embeddings # It should be done! return output_state_dict def __lowerCAmelCase ( ) -> Optional[int]: # Create the argument parser. __lowerCamelCase = argparse.ArgumentParser() parser.add_argument('''--print-checkpoint-structure''' , action='''store_true''' ) parser.add_argument( '''path_to_checkpoint''' , type=UpperCamelCase__ , help='''Path to the checkpoint file (.zip archive or direct .pt file)''' , ) parser.add_argument( '''--config_file''' , default='''''' , type=UpperCamelCase__ , help='''An optional config json file describing the pre-trained model.''' , ) __lowerCamelCase = parser.parse_args() # Extract the basename. __lowerCamelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f"""Extracting PyTorch state dictionary from {args.path_to_checkpoint}""" ) if args.path_to_checkpoint.endswith('''.zip''' ): with zipfile.ZipFile(args.path_to_checkpoint , '''r''' ) as checkpoint: with checkpoint.open('''release/mp_rank_00/model_optim_rng.pt''' ) as pytorch_dict: __lowerCamelCase = torch.load(UpperCamelCase__ , map_location='''cpu''' ) else: __lowerCamelCase = torch.load(args.path_to_checkpoint , map_location='''cpu''' ) __lowerCamelCase = input_state_dict.get('''args''' , UpperCamelCase__ ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: __lowerCamelCase = '''gelu_fast''' elif ds_args.openai_gelu: __lowerCamelCase = '''gelu_new''' else: __lowerCamelCase = '''gelu''' else: # in the very early days this used to be "gelu_new" __lowerCamelCase = '''gelu_new''' # Spell out all parameters in case the defaults change. __lowerCamelCase = GPTaConfig( vocab_size=5_02_57 , n_positions=10_24 , n_embd=10_24 , n_layer=24 , n_head=16 , n_inner=40_96 , activation_function=UpperCamelCase__ , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1E-5 , initializer_range=0.0_2 , summary_type='''cls_index''' , summary_use_proj=UpperCamelCase__ , summary_activation=UpperCamelCase__ , summary_proj_to_labels=UpperCamelCase__ , summary_first_dropout=0.1 , scale_attn_weights=UpperCamelCase__ , use_cache=UpperCamelCase__ , bos_token_id=5_02_56 , eos_token_id=5_02_56 , ) else: __lowerCamelCase = GPTaConfig.from_json_file(args.config_file ) __lowerCamelCase = ['''GPT2LMHeadModel'''] # Convert. print('''Converting''' ) __lowerCamelCase = convert_megatron_checkpoint(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(UpperCamelCase__ , UpperCamelCase__ ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: __lowerCamelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": __lowerCamelCase = '''gpt2''' elif tokenizer_type == "PretrainedFromHF": __lowerCamelCase = ds_args.tokenizer_name_or_path else: raise ValueError(f"""Unrecognized tokenizer_type {tokenizer_type}""" ) else: __lowerCamelCase = '''gpt2''' __lowerCamelCase = AutoTokenizer.from_pretrained(UpperCamelCase__ ) __lowerCamelCase = type(UpperCamelCase__ ).__name__ __lowerCamelCase = tokenizer_class # Store the config to file. print('''Saving config''' ) config.save_pretrained(UpperCamelCase__ ) # Save tokenizer based on args print(f"""Adding {tokenizer_class} tokenizer files""" ) tokenizer.save_pretrained(UpperCamelCase__ ) # Store the state_dict to file. __lowerCamelCase = os.path.join(UpperCamelCase__ , '''pytorch_model.bin''' ) print(f"""Saving checkpoint to \"{output_checkpoint_file}\"""" ) torch.save(UpperCamelCase__ , UpperCamelCase__ ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
67
"""simple docstring""" from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=lowercase_ ) class UpperCamelCase__ ( lowercase_ ): """simple docstring""" _SCREAMING_SNAKE_CASE = field(default="""summarization""", metadata={"""include_in_asdict_even_if_is_default""": True} ) _SCREAMING_SNAKE_CASE = Features({"""text""": Value("""string""" )} ) _SCREAMING_SNAKE_CASE = Features({"""summary""": Value("""string""" )} ) _SCREAMING_SNAKE_CASE = "text" _SCREAMING_SNAKE_CASE = "summary" @property def SCREAMING_SNAKE_CASE__ ( self : Dict ): return {self.text_column: "text", self.summary_column: "summary"}
224
0
"""simple docstring""" from __future__ import annotations _snake_case = list[list[int]] # assigning initial values to the grid _snake_case = [ [3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] # a grid with no solution _snake_case = [ [5, 0, 6, 5, 0, 8, 4, 0, 3], [5, 2, 0, 0, 0, 0, 0, 0, 2], [1, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] def lowerCAmelCase__ ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' for i in range(9 ): if grid[row][i] == n or grid[i][column] == n: return False for i in range(3 ): for j in range(3 ): if grid[(row - row % 3) + i][(column - column % 3) + j] == n: return False return True def lowerCAmelCase__ ( UpperCamelCase__ ): '''simple docstring''' for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def lowerCAmelCase__ ( UpperCamelCase__ ): '''simple docstring''' if location := find_empty_location(UpperCamelCase__ ): _a , _a : Dict = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1 , 1_0 ): if is_safe(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): _a : int = digit if sudoku(UpperCamelCase__ ) is not None: return grid _a : List[str] = 0 return None def lowerCAmelCase__ ( UpperCamelCase__ ): '''simple docstring''' for row in grid: for cell in row: print(UpperCamelCase__ , end=""" """ ) print() if __name__ == "__main__": # make a copy of grid so that you can compare with the unmodified grid for example_grid in (initial_grid, no_solution): print('\nExample grid:\n' + '=' * 20) print_solution(example_grid) print('\nExample grid solution:') _snake_case = sudoku(example_grid) if solution is not None: print_solution(solution) else: print('Cannot find a solution.')
324
"""simple docstring""" def lowerCAmelCase__ ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' _a , _a : Dict = len(UpperCamelCase__ ), len(grid[0] ) if ( min(UpperCamelCase__ , UpperCamelCase__ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) _a : Any = 0 count += depth_first_search(UpperCamelCase__ , row + 1 , UpperCamelCase__ , UpperCamelCase__ ) count += depth_first_search(UpperCamelCase__ , row - 1 , UpperCamelCase__ , UpperCamelCase__ ) count += depth_first_search(UpperCamelCase__ , UpperCamelCase__ , col + 1 , UpperCamelCase__ ) count += depth_first_search(UpperCamelCase__ , UpperCamelCase__ , col - 1 , UpperCamelCase__ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
324
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor __SCREAMING_SNAKE_CASE :Dict = logging.get_logger(__name__) class A_ ( lowerCAmelCase_ ): def __init__( self : Dict , *snake_case_ : int , **snake_case_ : Optional[Any] ): warnings.warn( "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use BeitImageProcessor instead." , snake_case_ , ) super().__init__(*snake_case_ , **snake_case_ )
22
import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import MaMaaaTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from transformers.utils import is_sentencepiece_available if is_sentencepiece_available(): from transformers.models.mam_aaa.tokenization_mam_aaa import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin if is_sentencepiece_available(): A_ : Tuple = get_tests_dir('fixtures/test_sentencepiece.model') if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right A_ : Tuple = 12_8022 A_ : Optional[Any] = 12_8028 @require_sentencepiece class _a (__magic_name__ , unittest.TestCase ): '''simple docstring''' UpperCAmelCase__: Tuple = MaMaaaTokenizer UpperCAmelCase__: List[Any] = False UpperCAmelCase__: Any = False UpperCAmelCase__: Optional[Any] = True def __A ( self ): super().setUp() A__ : Union[str, Any] = ["""</s>""", """<unk>""", """▁This""", """▁is""", """▁a""", """▁t""", """est""", """\u0120""", """<pad>"""] A__ : Optional[Any] = dict(zip(A__ , range(len(A__ ) ) ) ) A__ : Optional[int] = Path(self.tmpdirname ) save_json(A__ , save_dir / VOCAB_FILES_NAMES["""vocab_file"""] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(A__ , save_dir / VOCAB_FILES_NAMES["""spm_file"""] ) A__ : Tuple = MaMaaaTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def __A ( self , **A__ ): return MaMaaaTokenizer.from_pretrained(self.tmpdirname , **A__ ) def __A ( self , A__ ): return ( "This is a test", "This is a test", ) def __A ( self ): A__ : Any = """</s>""" A__ : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A__ ) , A__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A__ ) , A__ ) def __A ( self ): A__ : str = self.get_tokenizer() A__ : Dict = list(tokenizer.get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """</s>""" ) self.assertEqual(vocab_keys[1] , """<unk>""" ) self.assertEqual(vocab_keys[-1] , """<s>""" ) self.assertEqual(len(A__ ) , tokenizer.vocab_size + len(tokenizer.get_added_vocab() ) ) @unittest.skip("""Skip this test while all models are still to be uploaded.""" ) def __A ( self ): pass def __A ( self ): A__ : Optional[int] = self.get_tokenizer() A__ : int = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(A__ , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(A__ ) , [2, 3, 4, 5, 6] , ) A__ : Dict = tokenizer.convert_ids_to_tokens([2, 3, 4, 5, 6] ) self.assertListEqual(A__ , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) A__ : Any = tokenizer.convert_tokens_to_string(A__ ) self.assertEqual(A__ , """This is a test""" ) @slow def __A ( self ): # fmt: off A__ : int = {"""input_ids""": [[12_8022, 11_0108, 397, 11, 3_8272, 2247, 12_4811, 285, 1_8105, 1586, 207, 7, 3_9534, 4428, 397, 1019, 1_8105, 1586, 207, 7, 4_1337, 1_6786, 241, 7, 2_0214, 17, 12_5690, 1_0398, 7, 4_4378, 5_8069, 6_8342, 7798, 7343, 11, 299, 3_3310, 4, 158, 3_7350, 9_4077, 4569, 299, 3_3310, 90, 4, 5_2840, 290, 4, 3_1270, 112, 299, 682, 4, 5_2840, 3_9953, 1_4079, 193, 5_2519, 9_0894, 1_7894, 12_0697, 11, 4_0445, 551, 17, 1019, 5_2519, 9_0894, 1_7756, 963, 11, 4_0445, 480, 17, 9792, 1120, 5173, 1393, 6240, 1_6786, 241, 12_0996, 28, 1245, 1393, 11_8240, 1_1123, 1019, 9_3612, 2691, 1_0618, 9_8058, 12_0409, 1928, 279, 4, 4_0683, 367, 178, 207, 1019, 103, 10_3121, 506, 6_5296, 5, 2], [12_8022, 2_1217, 367, 117, 12_5450, 128, 719, 7, 7308, 40, 9_3612, 1_2669, 1116, 1_6704, 71, 1_7785, 3699, 1_5592, 35, 144, 9584, 241, 1_1943, 713, 950, 799, 2247, 8_8427, 150, 149, 11_8813, 12_0706, 1019, 10_6906, 8_1518, 28, 1224, 2_2799, 397, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [12_8022, 1658, 12_3311, 5155, 5578, 4722, 279, 1_4947, 2366, 1120, 1197, 14, 1348, 9232, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=A__ , model_name="""facebook/m2m100_418M""" , revision="""c168bae485c864188cf9aa0e4108b0b6934dc91e""" , ) @require_torch @require_sentencepiece @require_tokenizers class _a (unittest.TestCase ): '''simple docstring''' UpperCAmelCase__: Optional[int] = '''facebook/m2m100_418M''' UpperCAmelCase__: Any = [ '''In my opinion, there are two levels of response from the French government.''', '''NSA Affair Emphasizes Complete Lack of Debate on Intelligence''', ] UpperCAmelCase__: Any = [ '''Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.''', '''L\'affaire NSA souligne l\'absence totale de débat sur le renseignement''', ] # fmt: off UpperCAmelCase__: List[str] = [EN_CODE, 5_93, 19_49, 11_57_81, 4, 7_15_86, 42_34, 6_06_33, 12_62_33, 4_32, 12_38_08, 1_55_92, 11_97, 11_71_32, 12_06_18, 5, 2] @classmethod def __A ( cls ): A__ : MaMaaaTokenizer = MaMaaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang="""en""" , tgt_lang="""fr""" ) A__ : int = 1 return cls def __A ( self ): self.assertEqual(self.tokenizer.get_lang_id("""ar""" ) , 12_8006 ) self.assertEqual(self.tokenizer.get_lang_id("""en""" ) , 12_8022 ) self.assertEqual(self.tokenizer.get_lang_id("""ro""" ) , 12_8076 ) self.assertEqual(self.tokenizer.get_lang_id("""mr""" ) , 12_8063 ) def __A ( self ): A__ : Optional[Any] = self.tokenizer.get_vocab() self.assertEqual(len(A__ ) , self.tokenizer.vocab_size ) self.assertEqual(vocab["""<unk>"""] , 3 ) self.assertIn(self.tokenizer.get_lang_token("""en""" ) , A__ ) def __A ( self ): A__ : List[Any] = """en""" A__ : str = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , A__ ) def __A ( self ): self.assertIn(A__ , self.tokenizer.all_special_ids ) # fmt: off A__ : Dict = [FR_CODE, 5364, 82, 8642, 4, 294, 47, 8, 1_4028, 136, 3286, 9706, 6, 9_0797, 6, 14_4012, 162, 8_8128, 3_0061, 5, 2] # fmt: on A__ : Dict = self.tokenizer.decode(A__ , skip_special_tokens=A__ ) A__ : List[str] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=A__ ) self.assertEqual(A__ , A__ ) self.assertNotIn(self.tokenizer.eos_token , A__ ) def __A ( self ): A__ : str = tempfile.mkdtemp() A__ : Dict = self.tokenizer.lang_token_to_id self.tokenizer.save_pretrained(A__ ) A__ : List[Any] = MaMaaaTokenizer.from_pretrained(A__ ) self.assertDictEqual(new_tok.lang_token_to_id , A__ ) @require_torch def __A ( self ): A__ : List[str] = """en""" A__ : List[str] = """fr""" A__ : int = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=A__ , return_tensors="""pt""" ) A__ : int = shift_tokens_right( batch["""labels"""] , self.tokenizer.pad_token_id , self.tokenizer.eos_token_id ) for k in batch: A__ : Any = batch[k].tolist() # batch = {k: v.tolist() for k,v in batch.items()} # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 # batch.decoder_inputs_ids[0][0] == assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == FR_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2] == [2, FR_CODE] @require_torch def __A ( self ): A__ : List[str] = """mr""" self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id("""mr""" )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) A__ : Any = """zh""" self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id("""zh""" )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) @require_torch def __A ( self ): A__ : Optional[int] = """mr""" self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id("""mr""" )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) A__ : Any = """zh""" self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id("""zh""" )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) @require_torch def __A ( self ): A__ : Any = self.tokenizer._build_translation_inputs("""A test""" , return_tensors="""pt""" , src_lang="""en""" , tgt_lang="""ar""" ) self.assertEqual( nested_simplify(A__ ) , { # en_XX, A, test, EOS """input_ids""": [[12_8022, 58, 4183, 2]], """attention_mask""": [[1, 1, 1, 1]], # ar_AR """forced_bos_token_id""": 12_8006, } , )
192
0
'''simple docstring''' import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL UpperCAmelCase_ = logging.get_logger(__name__) def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : np.ndarray , SCREAMING_SNAKE_CASE__ : Union[int, Iterable[int]] , SCREAMING_SNAKE_CASE__ : bool , SCREAMING_SNAKE_CASE__ : int ): '''simple docstring''' def constraint_to_multiple_of(SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any]=0 , SCREAMING_SNAKE_CASE__ : Any=None ): UpperCAmelCase__ = round(val / multiple ) * multiple if max_val is not None and x > max_val: UpperCAmelCase__ = math.floor(val / multiple ) * multiple if x < min_val: UpperCAmelCase__ = math.ceil(val / multiple ) * multiple return x UpperCAmelCase__ = (output_size, output_size) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else output_size UpperCAmelCase__ , UpperCAmelCase__ = get_image_size(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ , UpperCAmelCase__ = output_size # determine new height and width UpperCAmelCase__ = output_height / input_height UpperCAmelCase__ = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width UpperCAmelCase__ = scale_width else: # fit height UpperCAmelCase__ = scale_height UpperCAmelCase__ = constraint_to_multiple_of(scale_height * input_height , multiple=SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = constraint_to_multiple_of(scale_width * input_width , multiple=SCREAMING_SNAKE_CASE__ ) return (new_height, new_width) class lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Union[str, Any] = ["""pixel_values"""] def __init__( self : Optional[Any] , _UpperCAmelCase : bool = True , _UpperCAmelCase : Dict[str, int] = None , _UpperCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , _UpperCAmelCase : bool = False , _UpperCAmelCase : int = 1 , _UpperCAmelCase : bool = True , _UpperCAmelCase : Union[int, float] = 1 / 2_55 , _UpperCAmelCase : bool = True , _UpperCAmelCase : Optional[Union[float, List[float]]] = None , _UpperCAmelCase : Optional[Union[float, List[float]]] = None , **_UpperCAmelCase : List[Any] , ): """simple docstring""" super().__init__(**_UpperCAmelCase ) UpperCAmelCase__ = size if size is not None else {"""height""": 3_84, """width""": 3_84} UpperCAmelCase__ = get_size_dict(_UpperCAmelCase ) UpperCAmelCase__ = do_resize UpperCAmelCase__ = size UpperCAmelCase__ = keep_aspect_ratio UpperCAmelCase__ = ensure_multiple_of UpperCAmelCase__ = resample UpperCAmelCase__ = do_rescale UpperCAmelCase__ = rescale_factor UpperCAmelCase__ = do_normalize UpperCAmelCase__ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN UpperCAmelCase__ = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] , _UpperCAmelCase : np.ndarray , _UpperCAmelCase : Dict[str, int] , _UpperCAmelCase : bool = False , _UpperCAmelCase : int = 1 , _UpperCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , _UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCAmelCase : Tuple , ): """simple docstring""" UpperCAmelCase__ = 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()}''' ) UpperCAmelCase__ = get_resize_output_image_size( _UpperCAmelCase , output_size=(size["""height"""], size["""width"""]) , keep_aspect_ratio=_UpperCAmelCase , multiple=_UpperCAmelCase , ) return resize(_UpperCAmelCase , size=_UpperCAmelCase , resample=_UpperCAmelCase , data_format=_UpperCAmelCase , **_UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : Any , _UpperCAmelCase : np.ndarray , _UpperCAmelCase : Union[int, float] , _UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCAmelCase : Dict , ): """simple docstring""" return rescale(_UpperCAmelCase , scale=_UpperCAmelCase , data_format=_UpperCAmelCase , **_UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , _UpperCAmelCase : np.ndarray , _UpperCAmelCase : Union[float, List[float]] , _UpperCAmelCase : Union[float, List[float]] , _UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **_UpperCAmelCase : Dict , ): """simple docstring""" return normalize(_UpperCAmelCase , mean=_UpperCAmelCase , std=_UpperCAmelCase , data_format=_UpperCAmelCase , **_UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : Dict , _UpperCAmelCase : ImageInput , _UpperCAmelCase : bool = None , _UpperCAmelCase : int = None , _UpperCAmelCase : bool = None , _UpperCAmelCase : int = None , _UpperCAmelCase : PILImageResampling = None , _UpperCAmelCase : bool = None , _UpperCAmelCase : float = None , _UpperCAmelCase : bool = None , _UpperCAmelCase : Optional[Union[float, List[float]]] = None , _UpperCAmelCase : Optional[Union[float, List[float]]] = None , _UpperCAmelCase : Optional[Union[str, TensorType]] = None , _UpperCAmelCase : ChannelDimension = ChannelDimension.FIRST , **_UpperCAmelCase : Tuple , ): """simple docstring""" UpperCAmelCase__ = do_resize if do_resize is not None else self.do_resize UpperCAmelCase__ = size if size is not None else self.size UpperCAmelCase__ = get_size_dict(_UpperCAmelCase ) UpperCAmelCase__ = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio UpperCAmelCase__ = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of UpperCAmelCase__ = resample if resample is not None else self.resample UpperCAmelCase__ = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase__ = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase__ = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase__ = image_mean if image_mean is not None else self.image_mean UpperCAmelCase__ = image_std if image_std is not None else self.image_std UpperCAmelCase__ = make_list_of_images(_UpperCAmelCase ) if not valid_images(_UpperCAmelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. UpperCAmelCase__ = [to_numpy_array(_UpperCAmelCase ) for image in images] if do_resize: UpperCAmelCase__ = [self.resize(image=_UpperCAmelCase , size=_UpperCAmelCase , resample=_UpperCAmelCase ) for image in images] if do_rescale: UpperCAmelCase__ = [self.rescale(image=_UpperCAmelCase , scale=_UpperCAmelCase ) for image in images] if do_normalize: UpperCAmelCase__ = [self.normalize(image=_UpperCAmelCase , mean=_UpperCAmelCase , std=_UpperCAmelCase ) for image in images] UpperCAmelCase__ = [to_channel_dimension_format(_UpperCAmelCase , _UpperCAmelCase ) for image in images] UpperCAmelCase__ = {"""pixel_values""": images} return BatchFeature(data=_UpperCAmelCase , tensor_type=_UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , _UpperCAmelCase : str , _UpperCAmelCase : List[Tuple] = None ): """simple docstring""" UpperCAmelCase__ = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_UpperCAmelCase ) != len(_UpperCAmelCase ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_UpperCAmelCase ): UpperCAmelCase__ = target_sizes.numpy() UpperCAmelCase__ = [] for idx in range(len(_UpperCAmelCase ) ): UpperCAmelCase__ = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_UpperCAmelCase ) UpperCAmelCase__ = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_UpperCAmelCase ) else: UpperCAmelCase__ = logits.argmax(dim=1 ) UpperCAmelCase__ = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
363
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { 'google/pix2struct-textcaps-base': ( 'https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json' ), } class lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Dict = """pix2struct_text_model""" lowerCAmelCase_ : str = ["""past_key_values"""] lowerCAmelCase_ : Dict = { """hidden_size""": """hidden_size""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self : List[str] , _UpperCAmelCase : Dict=5_02_44 , _UpperCAmelCase : Tuple=7_68 , _UpperCAmelCase : List[Any]=64 , _UpperCAmelCase : Dict=20_48 , _UpperCAmelCase : List[str]=12 , _UpperCAmelCase : Tuple=12 , _UpperCAmelCase : Dict=32 , _UpperCAmelCase : Any=1_28 , _UpperCAmelCase : Optional[int]=0.1 , _UpperCAmelCase : str=1E-6 , _UpperCAmelCase : List[str]=1.0 , _UpperCAmelCase : str="gelu_new" , _UpperCAmelCase : str=0 , _UpperCAmelCase : Any=False , _UpperCAmelCase : List[Any]=0 , _UpperCAmelCase : Union[str, Any]=1 , _UpperCAmelCase : Dict=False , _UpperCAmelCase : Any=True , **_UpperCAmelCase : str , ): """simple docstring""" UpperCAmelCase__ = vocab_size UpperCAmelCase__ = hidden_size UpperCAmelCase__ = d_kv UpperCAmelCase__ = d_ff UpperCAmelCase__ = num_layers UpperCAmelCase__ = num_heads UpperCAmelCase__ = relative_attention_num_buckets UpperCAmelCase__ = relative_attention_max_distance UpperCAmelCase__ = dropout_rate UpperCAmelCase__ = layer_norm_epsilon UpperCAmelCase__ = initializer_factor UpperCAmelCase__ = use_cache UpperCAmelCase__ = eos_token_id UpperCAmelCase__ = decoder_start_token_id # for backwards compatibility UpperCAmelCase__ = dense_act_fn super().__init__( pad_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , decoder_start_token_id=_UpperCAmelCase , tie_word_embeddings=_UpperCAmelCase , is_decoder=_UpperCAmelCase , **_UpperCAmelCase , ) @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Optional[Any] , _UpperCAmelCase : Union[str, os.PathLike] , **_UpperCAmelCase : int ): """simple docstring""" cls._set_token_in_kwargs(_UpperCAmelCase ) UpperCAmelCase__ , UpperCAmelCase__ = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the text config dict if we are loading from Pix2StructConfig if config_dict.get("""model_type""" ) == "pix2struct": UpperCAmelCase__ = 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 lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : Optional[int] = """pix2struct_vision_model""" def __init__( self : Any , _UpperCAmelCase : List[Any]=7_68 , _UpperCAmelCase : Optional[int]=7_68 , _UpperCAmelCase : Dict=20_48 , _UpperCAmelCase : Tuple=64 , _UpperCAmelCase : Any=12 , _UpperCAmelCase : int=12 , _UpperCAmelCase : Dict="gelu_new" , _UpperCAmelCase : List[Any]=1E-6 , _UpperCAmelCase : int=0.0 , _UpperCAmelCase : str=0.0 , _UpperCAmelCase : Union[str, Any]=1E-10 , _UpperCAmelCase : Union[str, Any]=1.0 , _UpperCAmelCase : Optional[int]=40_96 , _UpperCAmelCase : Optional[int]=32 , _UpperCAmelCase : Dict=1_28 , **_UpperCAmelCase : int , ): """simple docstring""" super().__init__(**_UpperCAmelCase ) UpperCAmelCase__ = hidden_size UpperCAmelCase__ = patch_embed_hidden_size UpperCAmelCase__ = d_ff UpperCAmelCase__ = dropout_rate UpperCAmelCase__ = num_hidden_layers UpperCAmelCase__ = num_attention_heads UpperCAmelCase__ = initializer_range UpperCAmelCase__ = initializer_factor UpperCAmelCase__ = attention_dropout UpperCAmelCase__ = layer_norm_eps UpperCAmelCase__ = dense_act_fn UpperCAmelCase__ = seq_len UpperCAmelCase__ = relative_attention_num_buckets UpperCAmelCase__ = relative_attention_max_distance UpperCAmelCase__ = d_kv @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Optional[Any] , _UpperCAmelCase : Union[str, os.PathLike] , **_UpperCAmelCase : Optional[int] ): """simple docstring""" cls._set_token_in_kwargs(_UpperCAmelCase ) UpperCAmelCase__ , UpperCAmelCase__ = cls.get_config_dict(_UpperCAmelCase , **_UpperCAmelCase ) # get the vision config dict if we are loading from Pix2StructConfig if config_dict.get("""model_type""" ) == "pix2struct": UpperCAmelCase__ = config_dict["""vision_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 lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : str = """pix2struct""" lowerCAmelCase_ : Union[str, Any] = True def __init__( self : int , _UpperCAmelCase : Tuple=None , _UpperCAmelCase : Dict=None , _UpperCAmelCase : Any=1.0 , _UpperCAmelCase : Tuple=0.02 , _UpperCAmelCase : Optional[Any]=False , _UpperCAmelCase : Union[str, Any]=False , _UpperCAmelCase : List[str]=True , **_UpperCAmelCase : Optional[int] , ): """simple docstring""" super().__init__(tie_word_embeddings=_UpperCAmelCase , is_encoder_decoder=_UpperCAmelCase , **_UpperCAmelCase ) if text_config is None: UpperCAmelCase__ = {} logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values.""" ) if vision_config is None: UpperCAmelCase__ = {} logger.info("""vision_config is None. Initializing the Pix2StructVisionConfig with default values.""" ) UpperCAmelCase__ = PixaStructTextConfig(**_UpperCAmelCase ) UpperCAmelCase__ = PixaStructVisionConfig(**_UpperCAmelCase ) UpperCAmelCase__ = self.text_config.decoder_start_token_id UpperCAmelCase__ = self.text_config.pad_token_id UpperCAmelCase__ = self.text_config.eos_token_id UpperCAmelCase__ = initializer_factor UpperCAmelCase__ = initializer_range UpperCAmelCase__ = self.initializer_range UpperCAmelCase__ = self.initializer_range UpperCAmelCase__ = is_vqa @classmethod def SCREAMING_SNAKE_CASE__ ( cls : Any , _UpperCAmelCase : PixaStructTextConfig , _UpperCAmelCase : PixaStructVisionConfig , **_UpperCAmelCase : Any ): """simple docstring""" return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **_UpperCAmelCase ) def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" UpperCAmelCase__ = copy.deepcopy(self.__dict__ ) UpperCAmelCase__ = self.text_config.to_dict() UpperCAmelCase__ = self.vision_config.to_dict() UpperCAmelCase__ = self.__class__.model_type return output
61
0
from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record SCREAMING_SNAKE_CASE :Optional[int] = '\\n@article{wang2019superglue,\n title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},\n author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},\n journal={arXiv preprint arXiv:1905.00537},\n year={2019}\n}\n' SCREAMING_SNAKE_CASE :Union[str, Any] = '\\nSuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after\nGLUE with a new set of more difficult language understanding tasks, improved\nresources, and a new public leaderboard.\n' SCREAMING_SNAKE_CASE :str = '\nCompute SuperGLUE evaluation metric associated to each SuperGLUE dataset.\nArgs:\n predictions: list of predictions to score. Depending on the SuperGlUE subset:\n - for \'record\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'prediction_text\': the predicted answer text\n - for \'multirc\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question-answer pair as specified by the dataset\n - \'prediction\': the predicted answer label\n - otherwise: list of predicted labels\n references: list of reference labels. Depending on the SuperGLUE subset:\n - for \'record\': list of question-answers dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'answers\': list of possible answers\n - otherwise: list of reference labels\nReturns: depending on the SuperGLUE subset:\n - for \'record\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1\': F1 score\n - for \'multirc\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1_m\': Per-question macro-F1 score\n - \'f1_a\': Average F1 score over all answers\n - for \'axb\':\n \'matthews_correlation\': Matthew Correlation\n - for \'cb\':\n - \'accuracy\': Accuracy\n - \'f1\': F1 score\n - for all others:\n - \'accuracy\': Accuracy\nExamples:\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\')\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\')\n >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}]\n >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\')\n >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'matthews_correlation\': 1.0}\n' def UpperCAmelCase ( a_ , a_ ) -> Dict: """simple docstring""" return float((preds == labels).mean() ) def UpperCAmelCase ( a_ , a_ , a_="binary" ) -> Union[str, Any]: """simple docstring""" __A = simple_accuracy(a_ , a_ ) __A = float(fa_score(y_true=a_ , y_pred=a_ , average=a_ ) ) return { "accuracy": acc, "f1": fa, } def UpperCAmelCase ( a_ , a_ ) -> Union[str, Any]: """simple docstring""" __A = {} for id_pred, label in zip(a_ , a_ ): __A = F'''{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}''' __A = id_pred["prediction"] if question_id in question_map: question_map[question_id].append((pred, label) ) else: __A = [(pred, label)] __A , __A = [], [] for question, preds_labels in question_map.items(): __A , __A = zip(*a_ ) __A = fa_score(y_true=a_ , y_pred=a_ , average="macro" ) fas.append(a_ ) __A = int(sum(pred == label for pred, label in preds_labels ) == len(a_ ) ) ems.append(a_ ) __A = float(sum(a_ ) / len(a_ ) ) __A = sum(a_ ) / len(a_ ) __A = float(fa_score(y_true=a_ , y_pred=[id_pred["prediction"] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[str] ): if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" ) return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features(self._get_feature_types() ) ,codebase_urls=[] ,reference_urls=[] ,format="numpy" if not self.config_name == "record" and not self.config_name == "multirc" else None ,) def UpperCamelCase_ ( self : List[Any] ): if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "prediction_text": datasets.Value("string" ), }, "references": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "answers": datasets.Sequence(datasets.Value("string" ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value("int64" ), "paragraph": datasets.Value("int64" ), "question": datasets.Value("int64" ), }, "prediction": datasets.Value("int64" ), }, "references": datasets.Value("int64" ), } else: return { "predictions": datasets.Value("int64" ), "references": datasets.Value("int64" ), } def UpperCamelCase_ ( self : Union[str, Any] ,A : Union[str, Any] ,A : Dict ): if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(A ,A )} elif self.config_name == "cb": return acc_and_fa(A ,A ,fa_avg="macro" ) elif self.config_name == "record": __A = [ { "qas": [ {"id": ref["idx"]["query"], "answers": [{"text": ans} for ans in ref["answers"]]} for ref in references ] } ] __A = {pred["idx"]["query"]: pred["prediction_text"] for pred in predictions} return evaluate_record(A ,A )[0] elif self.config_name == "multirc": return evaluate_multirc(A ,A ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(A ,A )} else: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" )
15
"""simple docstring""" from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCAmelCase = logging.get_logger(__name__) class UpperCAmelCase_ ( _lowercase): snake_case__ = ['''input_values''', '''padding_mask'''] def __init__( self : Optional[Any] , __UpperCamelCase : int = 1 , __UpperCamelCase : int = 2_4000 , __UpperCamelCase : float = 0.0 , __UpperCamelCase : float = None , __UpperCamelCase : float = None , **__UpperCamelCase : Optional[Any] , ) -> Optional[int]: super().__init__(feature_size=__UpperCamelCase , sampling_rate=__UpperCamelCase , padding_value=__UpperCamelCase , **__UpperCamelCase ) _UpperCamelCase = chunk_length_s _UpperCamelCase = overlap @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def _UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self : Union[str, Any] , __UpperCamelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , __UpperCamelCase : Optional[Union[bool, str, PaddingStrategy]] = None , __UpperCamelCase : Optional[bool] = False , __UpperCamelCase : Optional[int] = None , __UpperCamelCase : Optional[Union[str, TensorType]] = None , __UpperCamelCase : Optional[int] = None , ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided audio input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) if padding and truncation: raise ValueError('''Both padding and truncation were set. Make sure you only set one.''' ) elif padding is None: # by default let's pad the inputs _UpperCamelCase = True _UpperCamelCase = bool( isinstance(__UpperCamelCase , (list, tuple) ) and (isinstance(raw_audio[0] , (np.ndarray, tuple, list) )) ) if is_batched: _UpperCamelCase = [np.asarray(__UpperCamelCase , dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(__UpperCamelCase , np.ndarray ): _UpperCamelCase = np.asarray(__UpperCamelCase , dtype=np.floataa ) elif isinstance(__UpperCamelCase , np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): _UpperCamelCase = raw_audio.astype(np.floataa ) # always return batch if not is_batched: _UpperCamelCase = [np.asarray(__UpperCamelCase ).T] # verify inputs are valid for idx, example in enumerate(__UpperCamelCase ): if example.ndim > 2: raise ValueError(F'''Expected input shape (channels, length) but got shape {example.shape}''' ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(F'''Expected mono audio but example has {example.shape[-1]} channels''' ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(F'''Expected stereo audio but example has {example.shape[-1]} channels''' ) _UpperCamelCase = None _UpperCamelCase = BatchFeature({'''input_values''': raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: _UpperCamelCase = min(array.shape[0] for array in raw_audio ) _UpperCamelCase = int(np.floor(max_length / self.chunk_stride ) ) _UpperCamelCase = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: _UpperCamelCase = max(array.shape[0] for array in raw_audio ) _UpperCamelCase = int(np.ceil(max_length / self.chunk_stride ) ) _UpperCamelCase = (nb_step - 1) * self.chunk_stride + self.chunk_length _UpperCamelCase = '''max_length''' else: _UpperCamelCase = input_values # normal padding on batch if padded_inputs is None: _UpperCamelCase = self.pad( __UpperCamelCase , max_length=__UpperCamelCase , truncation=__UpperCamelCase , padding=__UpperCamelCase , return_attention_mask=__UpperCamelCase , ) if padding: _UpperCamelCase = padded_inputs.pop('''attention_mask''' ) _UpperCamelCase = [] for example in padded_inputs.pop('''input_values''' ): if self.feature_size == 1: _UpperCamelCase = example[..., None] input_values.append(example.T ) _UpperCamelCase = input_values if return_tensors is not None: _UpperCamelCase = padded_inputs.convert_to_tensors(__UpperCamelCase ) return padded_inputs
256
0
import datasets from .evaluate import evaluate a__ = '''\ @inproceedings{Rajpurkar2016SQuAD10, title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text}, author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang}, booktitle={EMNLP}, year={2016} } ''' a__ = ''' This metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD). Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span, from the corresponding reading passage, or the question might be unanswerable. ''' a__ = ''' Computes SQuAD scores (F1 and EM). Args: predictions: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair as given in the references (see below) - \'prediction_text\': the text of the answer references: List of question-answers dictionaries with the following key-values: - \'id\': id of the question-answer pair (see above), - \'answers\': a Dict in the SQuAD dataset format { \'text\': list of possible texts for the answer, as a list of strings \'answer_start\': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: \'exact_match\': Exact match (the normalized answer exactly match the gold answer) \'f1\': The F-score of predicted tokens versus the gold answer Examples: >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\'}] >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}] >>> squad_metric = datasets.load_metric("squad") >>> results = squad_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 100.0, \'f1\': 100.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> Tuple: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': {'''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.Value('''string''' )}, '''references''': { '''id''': datasets.Value('''string''' ), '''answers''': datasets.features.Sequence( { '''text''': datasets.Value('''string''' ), '''answer_start''': datasets.Value('''int32''' ), } ), }, } ) , codebase_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , reference_urls=['''https://rajpurkar.github.io/SQuAD-explorer/'''] , ) def __lowercase ( self , _a , _a ) -> List[Any]: _a : Optional[Any] = {prediction["id"]: prediction["prediction_text"] for prediction in predictions} _a : Optional[Any] = [ { "paragraphs": [ { "qas": [ { "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]], "id": ref["id"], } for ref in references ] } ] } ] _a : int = evaluate(dataset=_a , predictions=_a ) return score
369
# 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 __UpperCAmelCase ( __a : Dict=None ) -> str: """simple docstring""" if subparsers is not None: _a : Union[str, Any] = subparsers.add_parser('''test''' ) else: _a : List[str] = 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 __UpperCAmelCase ( __a : List[Any] ) -> Union[str, Any]: """simple docstring""" _a : Dict = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: _a : List[Any] = script_name else: _a : Union[str, Any] = F"""--config_file={args.config_file} {script_name}""" _a : str = ['''accelerate-launch'''] + test_args.split() _a : str = 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 __UpperCAmelCase ( ) -> List[Any]: """simple docstring""" _a : Optional[int] = test_command_parser() _a : List[Any] = parser.parse_args() test_command(__a ) if __name__ == "__main__": main()
15
0
'''simple docstring''' 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 # ######################################################################## __a = 16 __a = 32 def __snake_case( _lowerCAmelCase , _lowerCAmelCase = 16 ) -> List[Any]: snake_case__ : Any = AutoTokenizer.from_pretrained("""bert-base-cased""" ) snake_case__ : Optional[int] = load_dataset("""glue""" , """mrpc""" ) def tokenize_function(_lowerCAmelCase ): # max_length=None => use the model max length (it's actually the default) snake_case__ : Any = 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(): snake_case__ : str = 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 snake_case__ : Optional[Any] = tokenized_datasets.rename_column("""label""" , """labels""" ) def collate_fn(_lowerCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. snake_case__ : List[Any] = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": snake_case__ : str = 16 elif accelerator.mixed_precision != "no": snake_case__ : Union[str, Any] = 8 else: snake_case__ : int = None return tokenizer.pad( _lowerCAmelCase , padding="""longest""" , max_length=_lowerCAmelCase , pad_to_multiple_of=_lowerCAmelCase , return_tensors="""pt""" , ) # Instantiate dataloaders. snake_case__ : List[str] = DataLoader( tokenized_datasets["""train"""] , shuffle=_lowerCAmelCase , collate_fn=_lowerCAmelCase , batch_size=_lowerCAmelCase , drop_last=_lowerCAmelCase ) snake_case__ : Any = DataLoader( tokenized_datasets["""validation"""] , shuffle=_lowerCAmelCase , collate_fn=_lowerCAmelCase , batch_size=_lowerCAmelCase , drop_last=(accelerator.mixed_precision == """fp8""") , ) return train_dataloader, eval_dataloader def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> List[str]: # Initialize accelerator snake_case__ : Tuple = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs snake_case__ : Optional[Any] = config["""lr"""] snake_case__ : Tuple = int(config["""num_epochs"""] ) snake_case__ : Optional[int] = int(config["""seed"""] ) snake_case__ : Tuple = int(config["""batch_size"""] ) snake_case__ : int = evaluate.load("""glue""" , """mrpc""" ) # If the batch size is too big we use gradient accumulation snake_case__ : Optional[Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: snake_case__ : int = batch_size // MAX_GPU_BATCH_SIZE snake_case__ : List[Any] = MAX_GPU_BATCH_SIZE set_seed(_lowerCAmelCase ) snake_case__ , snake_case__ : Any = get_dataloaders(_lowerCAmelCase , _lowerCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) snake_case__ : int = 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). snake_case__ : List[str] = model.to(accelerator.device ) # Instantiate optimizer snake_case__ : Union[str, Any] = AdamW(params=model.parameters() , lr=_lowerCAmelCase ) # Instantiate scheduler snake_case__ : str = get_linear_schedule_with_warmup( optimizer=_lowerCAmelCase , num_warmup_steps=100 , 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. snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = 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 ) snake_case__ : Union[str, Any] = model(**_lowerCAmelCase ) snake_case__ : Union[str, Any] = outputs.loss snake_case__ : Dict = 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(): snake_case__ : str = model(**_lowerCAmelCase ) snake_case__ : Dict = outputs.logits.argmax(dim=-1 ) snake_case__ , snake_case__ : List[Any] = accelerator.gather_for_metrics((predictions, batch["""labels"""]) ) metric.add_batch( predictions=_lowerCAmelCase , references=_lowerCAmelCase , ) snake_case__ : Optional[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" , _lowerCAmelCase ) def __snake_case( ) -> Any: snake_case__ : Dict = 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.""" ) snake_case__ : Dict = parser.parse_args() snake_case__ : List[Any] = {"""lr""": 2e-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16} training_function(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": main()
35
'''simple docstring''' import inspect import unittest from math import floor from transformers import CvtConfig from transformers.file_utils import cached_property, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import CvtForImageClassification, CvtModel from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class snake_case__ ( UpperCamelCase): def A ( self : List[str] ) -> List[Any]: UpperCAmelCase_ : int = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_A , '''embed_dim''' ) ) self.parent.assertTrue(hasattr(_A , '''num_heads''' ) ) class snake_case__ : def __init__( self : List[Any] , _A : List[str] , _A : Optional[Any]=13 , _A : List[str]=64 , _A : Tuple=3 , _A : int=[16, 48, 96] , _A : int=[1, 3, 6] , _A : Union[str, Any]=[1, 2, 10] , _A : List[Any]=[7, 3, 3] , _A : Optional[Any]=[4, 2, 2] , _A : List[Any]=[2, 1, 1] , _A : Union[str, Any]=[2, 2, 2] , _A : Tuple=[False, False, True] , _A : str=[0.0, 0.0, 0.0] , _A : List[Any]=0.02 , _A : int=1e-12 , _A : Optional[int]=True , _A : List[str]=True , _A : Union[str, Any]=2 , ) -> List[Any]: UpperCAmelCase_ : int = parent UpperCAmelCase_ : List[Any] = batch_size UpperCAmelCase_ : Any = image_size UpperCAmelCase_ : Tuple = patch_sizes UpperCAmelCase_ : int = patch_stride UpperCAmelCase_ : Any = patch_padding UpperCAmelCase_ : List[Any] = is_training UpperCAmelCase_ : Union[str, Any] = use_labels UpperCAmelCase_ : Union[str, Any] = num_labels UpperCAmelCase_ : List[str] = num_channels UpperCAmelCase_ : int = embed_dim UpperCAmelCase_ : Optional[int] = num_heads UpperCAmelCase_ : Tuple = stride_kv UpperCAmelCase_ : Optional[Any] = depth UpperCAmelCase_ : Dict = cls_token UpperCAmelCase_ : Dict = attention_drop_rate UpperCAmelCase_ : Any = initializer_range UpperCAmelCase_ : List[str] = layer_norm_eps def A ( self : int ) -> List[str]: UpperCAmelCase_ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase_ : Union[str, Any] = None if self.use_labels: UpperCAmelCase_ : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) UpperCAmelCase_ : List[str] = self.get_config() return config, pixel_values, labels def A ( self : List[str] ) -> int: return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def A ( self : Dict , _A : List[Any] , _A : Tuple , _A : Optional[Any] ) -> List[str]: UpperCAmelCase_ : List[Any] = CvtModel(config=_A ) model.to(_A ) model.eval() UpperCAmelCase_ : Tuple = model(_A ) UpperCAmelCase_ : List[str] = (self.image_size, self.image_size) UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = image_size[0], image_size[1] for i in range(len(self.depth ) ): UpperCAmelCase_ : int = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) UpperCAmelCase_ : Optional[Any] = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def A ( self : Any , _A : int , _A : str , _A : Union[str, Any] ) -> Optional[int]: UpperCAmelCase_ : str = self.num_labels UpperCAmelCase_ : str = CvtForImageClassification(_A ) model.to(_A ) model.eval() UpperCAmelCase_ : int = model(_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def A ( self : Dict ) -> Any: UpperCAmelCase_ : Union[str, Any] = self.prepare_config_and_inputs() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Tuple = config_and_inputs UpperCAmelCase_ : Optional[int] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class snake_case__ ( UpperCamelCase , UpperCamelCase , unittest.TestCase): a_ = (CvtModel, CvtForImageClassification) if is_torch_available() else () a_ = ( {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification} if is_torch_available() else {} ) a_ = False a_ = False a_ = False a_ = False a_ = False def A ( self : int ) -> List[str]: UpperCAmelCase_ : Optional[int] = CvtModelTester(self ) UpperCAmelCase_ : List[Any] = ConfigTester(self , config_class=_A , has_text_modality=_A , hidden_size=37 ) def A ( self : Any ) -> Dict: 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 A ( self : int ) -> List[str]: return @unittest.skip(reason='''Cvt does not output attentions''' ) def A ( self : Optional[int] ) -> Optional[int]: pass @unittest.skip(reason='''Cvt does not use inputs_embeds''' ) def A ( self : Any ) -> Optional[Any]: pass @unittest.skip(reason='''Cvt does not support input and output embeddings''' ) def A ( self : List[Any] ) -> Any: pass def A ( self : int ) -> str: UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ : Tuple = model_class(_A ) UpperCAmelCase_ : Union[str, Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase_ : Tuple = [*signature.parameters.keys()] UpperCAmelCase_ : str = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _A ) def A ( self : Tuple ) -> int: UpperCAmelCase_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def A ( self : Dict ) -> List[str]: def check_hidden_states_output(_A : Dict , _A : str , _A : int ): UpperCAmelCase_ : str = model_class(_A ) model.to(_A ) model.eval() with torch.no_grad(): UpperCAmelCase_ : Union[str, Any] = model(**self._prepare_for_class(_A , _A ) ) UpperCAmelCase_ : Optional[Any] = outputs.hidden_states UpperCAmelCase_ : Any = len(self.model_tester.depth ) self.assertEqual(len(_A ) , _A ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ : Optional[Any] = True check_hidden_states_output(_A , _A , _A ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase_ : Dict = True check_hidden_states_output(_A , _A , _A ) def A ( self : Union[str, Any] ) -> List[str]: UpperCAmelCase_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_A ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def A ( self : List[Any] ) -> Optional[Any]: pass @slow def A ( self : Optional[int] ) -> int: for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Optional[Any] = CvtModel.from_pretrained(_A ) self.assertIsNotNone(_A ) def __UpperCAmelCase ( ) -> str: UpperCAmelCase_ : List[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class snake_case__ ( unittest.TestCase): @cached_property def A ( self : Union[str, Any] ) -> Union[str, Any]: return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def A ( self : str ) -> str: UpperCAmelCase_ : str = CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_A ) UpperCAmelCase_ : Optional[int] = self.default_image_processor UpperCAmelCase_ : List[str] = prepare_img() UpperCAmelCase_ : List[Any] = image_processor(images=_A , return_tensors='''pt''' ).to(_A ) # forward pass with torch.no_grad(): UpperCAmelCase_ : Any = model(**_A ) # verify the logits UpperCAmelCase_ : Tuple = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , _A ) UpperCAmelCase_ : Union[str, Any] = torch.tensor([0.9_285, 0.9_015, -0.3_150] ).to(_A ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _A , atol=1e-4 ) )
304
0
import enum import os from hashlib import shaaaa from typing import Optional from .. import config from .logging import get_logger lowercase : int = get_logger(__name__) class lowerCamelCase__ ( enum.Enum): '''simple docstring''' _A = 'all_checks' _A = 'basic_checks' _A = 'no_checks' class lowerCamelCase__ ( __lowercase): '''simple docstring''' class lowerCamelCase__ ( __lowercase): '''simple docstring''' class lowerCamelCase__ ( __lowercase): '''simple docstring''' class lowerCamelCase__ ( __lowercase): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[dict] , _lowerCamelCase : dict , _lowerCamelCase : int=None) -> List[Any]: '''simple docstring''' if expected_checksums is None: logger.info("Unable to verify checksums.") return if len(set(_lowerCamelCase) - set(_lowerCamelCase)) > 0: raise ExpectedMoreDownloadedFiles(str(set(_lowerCamelCase) - set(_lowerCamelCase))) if len(set(_lowerCamelCase) - set(_lowerCamelCase)) > 0: raise UnexpectedDownloadedFile(str(set(_lowerCamelCase) - set(_lowerCamelCase))) __UpperCamelCase : int = [url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]] __UpperCamelCase : List[str] = " for " + verification_name if verification_name is not None else "" if len(_lowerCamelCase) > 0: raise NonMatchingChecksumError( F'Checksums didn\'t match{for_verification_name}:\n' F'{bad_urls}\n' "Set `verification_mode='no_checks'` to skip checksums verification and ignore this error") logger.info("All the checksums matched successfully" + for_verification_name) class lowerCamelCase__ ( __lowercase): '''simple docstring''' class lowerCamelCase__ ( __lowercase): '''simple docstring''' class lowerCamelCase__ ( __lowercase): '''simple docstring''' class lowerCamelCase__ ( __lowercase): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[dict] , _lowerCamelCase : dict) -> Union[str, Any]: '''simple docstring''' if expected_splits is None: logger.info("Unable to verify splits sizes.") return if len(set(_lowerCamelCase) - set(_lowerCamelCase)) > 0: raise ExpectedMoreSplits(str(set(_lowerCamelCase) - set(_lowerCamelCase))) if len(set(_lowerCamelCase) - set(_lowerCamelCase)) > 0: raise UnexpectedSplits(str(set(_lowerCamelCase) - set(_lowerCamelCase))) __UpperCamelCase : int = [ {"expected": expected_splits[name], "recorded": recorded_splits[name]} for name in expected_splits if expected_splits[name].num_examples != recorded_splits[name].num_examples ] if len(_lowerCamelCase) > 0: raise NonMatchingSplitsSizesError(str(_lowerCamelCase)) logger.info("All the splits matched successfully.") def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : str , _lowerCamelCase : bool = True) -> dict: '''simple docstring''' if record_checksum: __UpperCamelCase : Tuple = shaaaa() with open(_lowerCamelCase , "rb") as f: for chunk in iter(lambda: f.read(1 << 20) , b""): m.update(_lowerCamelCase) __UpperCamelCase : Any = m.hexdigest() else: __UpperCamelCase : Optional[Any] = None return {"num_bytes": os.path.getsize(_lowerCamelCase), "checksum": checksum} def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any]) -> Optional[int]: '''simple docstring''' if dataset_size and config.IN_MEMORY_MAX_SIZE: return dataset_size < config.IN_MEMORY_MAX_SIZE else: return False
151
from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class lowerCamelCase__ : '''simple docstring''' _A = 42 _A = 42 class lowerCamelCase__ : '''simple docstring''' def __init__( self :Optional[Any] , a :int ) -> Tuple: __UpperCamelCase : list[list[Edge]] = [[] for _ in range(a )] __UpperCamelCase : str = size def __getitem__( self :str , a :int ) -> Iterator[Edge]: return iter(self._graph[vertex] ) @property def _lowerCamelCase ( self :Any ) -> List[str]: return self._size def _lowerCamelCase ( self :Dict , a :int , a :int , a :int ) -> Any: if weight not in (0, 1): raise ValueError("Edge weight must be either 0 or 1." ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError("Vertex indexes must be in [0; size)." ) self._graph[from_vertex].append(Edge(a , a ) ) def _lowerCamelCase ( self :List[str] , a :int , a :int ) -> int | None: __UpperCamelCase : Union[str, Any] = deque([start_vertex] ) __UpperCamelCase : list[int | None] = [None] * self.size __UpperCamelCase : Dict = 0 while queue: __UpperCamelCase : Tuple = queue.popleft() __UpperCamelCase : int = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: __UpperCamelCase : Optional[Any] = current_distance + edge.weight __UpperCamelCase : Dict = distances[edge.destination_vertex] if ( isinstance(a , a ) and new_distance >= dest_vertex_distance ): continue __UpperCamelCase : Optional[Any] = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError("No path from start_vertex to finish_vertex." ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
151
1
"""simple docstring""" import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger UpperCAmelCase__ = get_logger(__name__) UpperCAmelCase__ = Path(__file__).parent / """model_card_template.md""" UpperCAmelCase__ = uuida().hex UpperCAmelCase__ = os.getenv("""HF_HUB_OFFLINE""", """""").upper() in ENV_VARS_TRUE_VALUES UpperCAmelCase__ = os.getenv("""DISABLE_TELEMETRY""", """""").upper() in ENV_VARS_TRUE_VALUES UpperCAmelCase__ = HUGGINGFACE_CO_RESOLVE_ENDPOINT + """/api/telemetry/""" def __UpperCAmelCase ( lowercase = None ): """simple docstring""" _UpperCAmelCase = f'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'''; torch/{_torch_version}''' if is_flax_available(): ua += f'''; jax/{_jax_version}''' ua += f'''; flax/{_flax_version}''' if is_onnx_available(): ua += f'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get("""DIFFUSERS_IS_CI""" ,"""""" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(lowercase ,lowercase ): ua += "; " + "; ".join(f'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(lowercase ,lowercase ): ua += "; " + user_agent return ua def __UpperCAmelCase ( lowercase ,lowercase = None ,lowercase = None ): """simple docstring""" if token is None: _UpperCAmelCase = HfFolder.get_token() if organization is None: _UpperCAmelCase = whoami(lowercase )["""name"""] return f'''{username}/{model_id}''' else: return f'''{organization}/{model_id}''' def __UpperCAmelCase ( lowercase ,lowercase ): """simple docstring""" if not is_jinja_available(): raise ValueError( """Modelcard rendering is based on Jinja templates.""" """ Please make sure to have `jinja` installed before using `create_model_card`.""" """ To install it, please run `pip install Jinja2`.""" ) if hasattr(lowercase ,"""local_rank""" ) and args.local_rank not in [-1, 0]: return _UpperCAmelCase = args.hub_token if hasattr(lowercase ,"""hub_token""" ) else None _UpperCAmelCase = get_full_repo_name(lowercase ,token=lowercase ) _UpperCAmelCase = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="""en""" ,license="""apache-2.0""" ,library_name="""diffusers""" ,tags=[] ,datasets=args.dataset_name ,metrics=[] ,) ,template_path=lowercase ,model_name=lowercase ,repo_name=lowercase ,dataset_name=args.dataset_name if hasattr(lowercase ,"""dataset_name""" ) else None ,learning_rate=args.learning_rate ,train_batch_size=args.train_batch_size ,eval_batch_size=args.eval_batch_size ,gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(lowercase ,"""gradient_accumulation_steps""" ) else None ) ,adam_betaa=args.adam_betaa if hasattr(lowercase ,"""adam_beta1""" ) else None ,adam_betaa=args.adam_betaa if hasattr(lowercase ,"""adam_beta2""" ) else None ,adam_weight_decay=args.adam_weight_decay if hasattr(lowercase ,"""adam_weight_decay""" ) else None ,adam_epsilon=args.adam_epsilon if hasattr(lowercase ,"""adam_epsilon""" ) else None ,lr_scheduler=args.lr_scheduler if hasattr(lowercase ,"""lr_scheduler""" ) else None ,lr_warmup_steps=args.lr_warmup_steps if hasattr(lowercase ,"""lr_warmup_steps""" ) else None ,ema_inv_gamma=args.ema_inv_gamma if hasattr(lowercase ,"""ema_inv_gamma""" ) else None ,ema_power=args.ema_power if hasattr(lowercase ,"""ema_power""" ) else None ,ema_max_decay=args.ema_max_decay if hasattr(lowercase ,"""ema_max_decay""" ) else None ,mixed_precision=args.mixed_precision ,) _UpperCAmelCase = os.path.join(args.output_dir ,"""README.md""" ) model_card.save(lowercase ) def __UpperCAmelCase ( lowercase ,lowercase = None ): """simple docstring""" if resolved_file is None or commit_hash is not None: return commit_hash _UpperCAmelCase = str(Path(lowercase ).as_posix() ) _UpperCAmelCase = re.search(R"""snapshots/([^/]+)/""" ,lowercase ) if search is None: return None _UpperCAmelCase = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(lowercase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. UpperCAmelCase__ = os.path.expanduser( os.getenv("""HF_HOME""", os.path.join(os.getenv("""XDG_CACHE_HOME""", """~/.cache"""), """huggingface""")) ) UpperCAmelCase__ = os.path.join(hf_cache_home, """diffusers""") def __UpperCAmelCase ( lowercase = None ,lowercase = None ): """simple docstring""" if new_cache_dir is None: _UpperCAmelCase = DIFFUSERS_CACHE if old_cache_dir is None: _UpperCAmelCase = old_diffusers_cache _UpperCAmelCase = Path(lowercase ).expanduser() _UpperCAmelCase = Path(lowercase ).expanduser() for old_blob_path in old_cache_dir.glob("""**/blobs/*""" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): _UpperCAmelCase = new_cache_dir / old_blob_path.relative_to(lowercase ) new_blob_path.parent.mkdir(parents=lowercase ,exist_ok=lowercase ) os.replace(lowercase ,lowercase ) try: os.symlink(lowercase ,lowercase ) except OSError: logger.warning( """Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.""" ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). UpperCAmelCase__ = os.path.join(DIFFUSERS_CACHE, """version_diffusers_cache.txt""") if not os.path.isfile(cache_version_file): UpperCAmelCase__ = 0 else: with open(cache_version_file) as f: try: UpperCAmelCase__ = int(f.read()) except ValueError: UpperCAmelCase__ = 0 if cache_version < 1: UpperCAmelCase__ = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( """The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your """ """existing cached models. This is a one-time operation, you can interrupt it or run it """ """later by calling `diffusers.utils.hub_utils.move_cache()`.""" ) try: move_cache() except Exception as e: UpperCAmelCase__ = """\n""".join(traceback.format_tb(e.__traceback__)) logger.error( F'''There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease ''' """file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole """ """message and we will do our best to help.""" ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, """w""") as f: f.write("""1""") except Exception: logger.warning( F'''There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure ''' """the directory exists and can be written to.""" ) def __UpperCAmelCase ( lowercase ,lowercase = None ): """simple docstring""" if variant is not None: _UpperCAmelCase = weights_name.split(""".""" ) _UpperCAmelCase = splits[:-1] + [variant] + splits[-1:] _UpperCAmelCase = """.""".join(lowercase ) return weights_name def __UpperCAmelCase ( lowercase ,*, lowercase ,lowercase ,lowercase ,lowercase ,lowercase ,lowercase ,lowercase ,lowercase ,lowercase ,lowercase ,lowercase=None ,): """simple docstring""" _UpperCAmelCase = str(lowercase ) if os.path.isfile(lowercase ): return pretrained_model_name_or_path elif os.path.isdir(lowercase ): if os.path.isfile(os.path.join(lowercase ,lowercase ) ): # Load from a PyTorch checkpoint _UpperCAmelCase = os.path.join(lowercase ,lowercase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(lowercase ,lowercase ,lowercase ) ): _UpperCAmelCase = os.path.join(lowercase ,lowercase ,lowercase ) return model_file else: raise EnvironmentError( f'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(lowercase ).base_version ) >= version.parse("""0.20.0""" ) ): try: _UpperCAmelCase = hf_hub_download( lowercase ,filename=_add_variant(lowercase ,lowercase ) ,cache_dir=lowercase ,force_download=lowercase ,proxies=lowercase ,resume_download=lowercase ,local_files_only=lowercase ,use_auth_token=lowercase ,user_agent=lowercase ,subfolder=lowercase ,revision=revision or commit_hash ,) warnings.warn( f'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' ,lowercase ,) return model_file except: # noqa: E722 warnings.warn( f'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(lowercase ,lowercase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(lowercase ,lowercase )}\' so that the correct variant file can be added.''' ,lowercase ,) try: # 2. Load model file as usual _UpperCAmelCase = hf_hub_download( lowercase ,filename=lowercase ,cache_dir=lowercase ,force_download=lowercase ,proxies=lowercase ,resume_download=lowercase ,local_files_only=lowercase ,use_auth_token=lowercase ,user_agent=lowercase ,subfolder=lowercase ,revision=revision or commit_hash ,) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' """listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a """ """token having permission to this repo with `use_auth_token` or log in with `huggingface-cli """ """login`.""" ) except RevisionNotFoundError: raise EnvironmentError( f'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' """this model name. Check the model page at """ f'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( f'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( f'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' f''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' f''' directory containing a file named {weights_name} or''' """ \nCheckout your internet connection or see how to run the library in""" """ offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'.""" ) except EnvironmentError: raise EnvironmentError( f'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' """'https://huggingface.co/models', make sure you don't have a local directory with the same name. """ f'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' f'''containing a file named {weights_name}''' )
289
"""simple docstring""" import math def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [] _UpperCAmelCase = 2 _UpperCAmelCase = int(math.sqrt(lowercase ) ) # Size of every segment _UpperCAmelCase = [True] * (end + 1) _UpperCAmelCase = [] while start <= end: if temp[start] is True: in_prime.append(lowercase ) for i in range(start * start ,end + 1 ,lowercase ): _UpperCAmelCase = False start += 1 prime += in_prime _UpperCAmelCase = end + 1 _UpperCAmelCase = min(2 * end ,lowercase ) while low <= n: _UpperCAmelCase = [True] * (high - low + 1) for each in in_prime: _UpperCAmelCase = math.floor(low / each ) * each if t < low: t += each for j in range(lowercase ,high + 1 ,lowercase ): _UpperCAmelCase = False for j in range(len(lowercase ) ): if temp[j] is True: prime.append(j + low ) _UpperCAmelCase = high + 1 _UpperCAmelCase = min(high + end ,lowercase ) return prime print(sieve(1_0**6))
289
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available a ={} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a =["""GPTSw3Tokenizer"""] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys a =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
113
from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> bool: if len(lowerCamelCase__ ) == 0: return False __lowerCamelCase : List[Any] = len(lowerCamelCase__ ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , lowerCamelCase__ ) else: return binary_search(a_list[midpoint + 1 :] , lowerCamelCase__ ) if __name__ == "__main__": a =input("""Enter numbers separated by comma:\n""").strip() a =[int(item.strip()) for item in user_input.split(""",""")] a =int(input("""Enter the number to be found in the list:\n""").strip()) a ="""""" if binary_search(sequence, target) else """not """ print(F"""{target} was {not_str}found in {sequence}""")
113
1
'''simple docstring''' import unittest from transformers import AutoTokenizer, is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow if is_flax_available(): import jax.numpy as jnp from transformers import FlaxXLMRobertaModel @require_sentencepiece @require_tokenizers @require_flax class __magic_name__ ( unittest.TestCase ): @slow def __lowercase ( self : Union[str, Any] ): _a : List[Any] = FlaxXLMRobertaModel.from_pretrained('xlm-roberta-base' ) _a : List[Any] = AutoTokenizer.from_pretrained('xlm-roberta-base' ) _a : Optional[Any] = 'The dog is cute and lives in the garden house' _a : int = jnp.array([tokenizer.encode(_UpperCAmelCase )] ) _a : Optional[int] = (1, 12, 768) # batch_size, sequence_length, embedding_vector_dim _a : Dict = jnp.array( [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]] ) _a : Tuple = model(_UpperCAmelCase )['last_hidden_state'] self.assertEqual(output.shape ,_UpperCAmelCase ) # compare the actual values for a slice of last dim self.assertTrue(jnp.allclose(output[:, :, -1] ,_UpperCAmelCase ,atol=1E-3 ) )
89
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available SCREAMING_SNAKE_CASE__:List[str] = {"""configuration_van""": ["""VAN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """VanConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__:Optional[Any] = [ """VAN_PRETRAINED_MODEL_ARCHIVE_LIST""", """VanForImageClassification""", """VanModel""", """VanPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_van import VAN_PRETRAINED_CONFIG_ARCHIVE_MAP, VanConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_van import ( VAN_PRETRAINED_MODEL_ARCHIVE_LIST, VanForImageClassification, VanModel, VanPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__:Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
261
0
"""simple docstring""" from typing import List, Optional, Tuple, Union import PIL import torch from torchvision import transforms from diffusers.pipeline_utils import DiffusionPipeline, ImagePipelineOutput from diffusers.schedulers import DDIMScheduler from diffusers.utils import randn_tensor __UpperCamelCase : Union[str, Any] = transforms.Compose( [ transforms.Resize((256, 256)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ] ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Optional[Any] ): if isinstance(UpperCamelCase__ , torch.Tensor ): return image elif isinstance(UpperCamelCase__ , PIL.Image.Image ): lowerCAmelCase = [image] lowerCAmelCase = [trans(img.convert('RGB' ) ) for img in image] lowerCAmelCase = torch.stack(UpperCamelCase__ ) return image class a ( UpperCamelCase__ ): def __init__( self , _snake_case , _snake_case ): """simple docstring""" super().__init__() # make sure scheduler can always be converted to DDIM lowerCAmelCase = DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=__lowerCamelCase , scheduler=__lowerCamelCase ) def UpperCamelCase__ ( self , _snake_case ): """simple docstring""" if strength < 0 or strength > 1: raise ValueError(F'The value of strength should in [0.0, 1.0] but is {strength}' ) def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case ): """simple docstring""" lowerCAmelCase = min(int(num_inference_steps * strength ) , __lowerCamelCase ) lowerCAmelCase = max(num_inference_steps - init_timestep , 0 ) lowerCAmelCase = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def UpperCamelCase__ ( self , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case=None ): """simple docstring""" if not isinstance(__lowerCamelCase , (torch.Tensor, PIL.Image.Image, list) ): raise ValueError( F'`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(__lowerCamelCase )}' ) lowerCAmelCase = image.to(device=__lowerCamelCase , dtype=__lowerCamelCase ) 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.' ) lowerCAmelCase = init_latents.shape lowerCAmelCase = randn_tensor(__lowerCamelCase , generator=__lowerCamelCase , device=__lowerCamelCase , dtype=__lowerCamelCase ) # get latents print('add noise to latents at timestep' , __lowerCamelCase ) lowerCAmelCase = self.scheduler.add_noise(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) lowerCAmelCase = init_latents return latents @torch.no_grad() def __call__( self , _snake_case = None , _snake_case = 0.8 , _snake_case = 1 , _snake_case = None , _snake_case = 0.0 , _snake_case = 50 , _snake_case = None , _snake_case = "pil" , _snake_case = True , ): """simple docstring""" self.check_inputs(__lowerCamelCase ) # 2. Preprocess image lowerCAmelCase = preprocess(__lowerCamelCase ) # 3. set timesteps self.scheduler.set_timesteps(__lowerCamelCase , device=self.device ) lowerCAmelCase ,lowerCAmelCase = self.get_timesteps(__lowerCamelCase , __lowerCamelCase , self.device ) lowerCAmelCase = timesteps[:1].repeat(__lowerCamelCase ) # 4. Prepare latent variables lowerCAmelCase = self.prepare_latents(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , self.unet.dtype , self.device , __lowerCamelCase ) lowerCAmelCase = latents # 5. Denoising loop for t in self.progress_bar(__lowerCamelCase ): # 1. predict noise model_output lowerCAmelCase = self.unet(__lowerCamelCase , __lowerCamelCase ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 lowerCAmelCase = self.scheduler.step( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , eta=__lowerCamelCase , use_clipped_model_output=__lowerCamelCase , generator=__lowerCamelCase , ).prev_sample lowerCAmelCase = (image / 2 + 0.5).clamp(0 , 1 ) lowerCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowerCAmelCase = self.numpy_to_pil(__lowerCamelCase ) if not return_dict: return (image, latent_timestep.item()) return ImagePipelineOutput(images=__lowerCamelCase )
367
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileViTImageProcessor class a ( unittest.TestCase ): def __init__( self , _snake_case , _snake_case=7 , _snake_case=3 , _snake_case=18 , _snake_case=30 , _snake_case=4_00 , _snake_case=True , _snake_case=None , _snake_case=True , _snake_case=None , _snake_case=True , ): """simple docstring""" lowerCAmelCase = size if size is not None else {'shortest_edge': 20} lowerCAmelCase = crop_size if crop_size is not None else {'height': 18, 'width': 18} lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = num_channels lowerCAmelCase = image_size lowerCAmelCase = min_resolution lowerCAmelCase = max_resolution lowerCAmelCase = do_resize lowerCAmelCase = size lowerCAmelCase = do_center_crop lowerCAmelCase = crop_size lowerCAmelCase = do_flip_channel_order def UpperCamelCase__ ( self ): """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class a ( a__ , unittest.TestCase ): snake_case__ = MobileViTImageProcessor if is_vision_available() else None def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = MobileViTImageProcessingTester(self ) @property def UpperCamelCase__ ( self ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_snake_case , 'do_resize' ) ) self.assertTrue(hasattr(_snake_case , 'size' ) ) self.assertTrue(hasattr(_snake_case , 'do_center_crop' ) ) self.assertTrue(hasattr(_snake_case , 'center_crop' ) ) self.assertTrue(hasattr(_snake_case , 'do_flip_channel_order' ) ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 20} ) self.assertEqual(image_processor.crop_size , {'height': 18, 'width': 18} ) lowerCAmelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size , {'height': 84, 'width': 84} ) def UpperCamelCase__ ( self ): """simple docstring""" pass def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , Image.Image ) # Test not batched input lowerCAmelCase = 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 lowerCAmelCase = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case , numpify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , np.ndarray ) # Test not batched input lowerCAmelCase = 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 lowerCAmelCase = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_snake_case , torchify=_snake_case ) for image in image_inputs: self.assertIsInstance(_snake_case , torch.Tensor ) # Test not batched input lowerCAmelCase = 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 lowerCAmelCase = image_processing(_snake_case , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , )
309
0
'''simple docstring''' # Function to print upper half of diamond (pyramid) def a__ ( a__ ): """simple docstring""" for i in range(0 , a__ ): for _ in range(0 , n - i - 1 ): # printing spaces print(""" """ , end="""""" ) for _ in range(0 , i + 1 ): # printing stars print("""* """ , end="""""" ) print() def a__ ( a__ ): """simple docstring""" for i in range(a__ , 0 , -1 ): for _ in range(a__ , 0 , -1 ): # printing stars print("""* """ , end="""""" ) print() for _ in range(n - i + 1 , 0 , -1 ): # printing spaces print(""" """ , end="""""" ) def a__ ( a__ ): """simple docstring""" if n <= 0: print(""" ... .... nothing printing :(""" ) return floyd(a__ ) # upper half reverse_floyd(a__ ) # lower half if __name__ == "__main__": print(R'| /\ | |- | |- |--| |\ /| |-') print(R'|/ \| |- |_ |_ |__| | \/ | |_') UpperCAmelCase : Dict = 1 while K: UpperCAmelCase : str = int(input('enter the number and , and see the magic : ')) print() pretty_print(user_number) UpperCAmelCase : Tuple = int(input('press 0 to exit... and 1 to continue...')) print('Good Bye...')
267
'''simple docstring''' def a__ ( a__ ): """simple docstring""" if isinstance(a__ , a__ ): raise TypeError("""'float' object cannot be interpreted as an integer""" ) if isinstance(a__ , a__ ): raise TypeError("""'str' object cannot be interpreted as an integer""" ) if num == 0: return "0b0" __SCREAMING_SNAKE_CASE = False if num < 0: __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = -num __SCREAMING_SNAKE_CASE = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(a__ ) for e in binary ) return "0b" + "".join(str(a__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
267
1
"""simple docstring""" from typing import Callable, List, Optional, Union import PIL import torch from transformers import ( CLIPImageProcessor, CLIPSegForImageSegmentation, CLIPSegProcessor, CLIPTextModel, CLIPTokenizer, ) from diffusers import DiffusionPipeline from diffusers.configuration_utils import FrozenDict from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionInpaintPipeline from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import deprecate, is_accelerate_available, logging __A : str = logging.get_logger(__name__) # pylint: disable=invalid-name class __UpperCamelCase ( _A ): def __init__(self : Any , __SCREAMING_SNAKE_CASE : CLIPSegForImageSegmentation , __SCREAMING_SNAKE_CASE : CLIPSegProcessor , __SCREAMING_SNAKE_CASE : AutoencoderKL , __SCREAMING_SNAKE_CASE : CLIPTextModel , __SCREAMING_SNAKE_CASE : CLIPTokenizer , __SCREAMING_SNAKE_CASE : UNetaDConditionModel , __SCREAMING_SNAKE_CASE : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , __SCREAMING_SNAKE_CASE : StableDiffusionSafetyChecker , __SCREAMING_SNAKE_CASE : CLIPImageProcessor , ): super().__init__() if hasattr(scheduler.config , "steps_offset") and scheduler.config.steps_offset != 1: A = ( F"""The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`""" F""" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure """ "to update the config accordingly as leaving `steps_offset` might led to incorrect results" " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub," " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`" " file" ) deprecate("steps_offset!=1" , "1.0.0" , __SCREAMING_SNAKE_CASE , standard_warn=__SCREAMING_SNAKE_CASE) A = dict(scheduler.config) A = 1 A = FrozenDict(__SCREAMING_SNAKE_CASE) if hasattr(scheduler.config , "skip_prk_steps") and scheduler.config.skip_prk_steps is False: A = ( F"""The configuration file of this scheduler: {scheduler} has not set the configuration""" " `skip_prk_steps`. `skip_prk_steps` should be set to True in the configuration file. Please make" " sure to update the config accordingly as not setting `skip_prk_steps` in the config might lead to" " incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face" " Hub, it would be very nice if you could open a Pull request for the" " `scheduler/scheduler_config.json` file" ) deprecate("skip_prk_steps not set" , "1.0.0" , __SCREAMING_SNAKE_CASE , standard_warn=__SCREAMING_SNAKE_CASE) A = dict(scheduler.config) A = True A = FrozenDict(__SCREAMING_SNAKE_CASE) if safety_checker is None: logger.warning( F"""You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure""" " that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered" " results in services or applications open to the public. Both the diffusers team and Hugging Face" " strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling" " it only for use-cases that involve analyzing network behavior or auditing its results. For more" " information, please have a look at https://github.com/huggingface/diffusers/pull/254 .") self.register_modules( segmentation_model=__SCREAMING_SNAKE_CASE , segmentation_processor=__SCREAMING_SNAKE_CASE , vae=__SCREAMING_SNAKE_CASE , text_encoder=__SCREAMING_SNAKE_CASE , tokenizer=__SCREAMING_SNAKE_CASE , unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE , safety_checker=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE__ (self : int , __SCREAMING_SNAKE_CASE : Optional[Union[str, int]] = "auto"): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE__ (self : Any): self.enable_attention_slicing(__SCREAMING_SNAKE_CASE) def SCREAMING_SNAKE_CASE__ (self : Optional[int]): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`") A = torch.device("cuda") for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae, self.safety_checker]: if cpu_offloaded_model is not None: cpu_offload(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def SCREAMING_SNAKE_CASE__ (self : List[Any]): if self.device != torch.device("meta") or not hasattr(self.unet , "_hf_hook"): return self.device for module in self.unet.modules(): if ( hasattr(__SCREAMING_SNAKE_CASE , "_hf_hook") and hasattr(module._hf_hook , "execution_device") and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device) return self.device @torch.no_grad() def __call__(self : List[str] , __SCREAMING_SNAKE_CASE : Union[str, List[str]] , __SCREAMING_SNAKE_CASE : Union[torch.FloatTensor, PIL.Image.Image] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : int = 5_1_2 , __SCREAMING_SNAKE_CASE : int = 5_1_2 , __SCREAMING_SNAKE_CASE : int = 5_0 , __SCREAMING_SNAKE_CASE : float = 7.5 , __SCREAMING_SNAKE_CASE : Optional[Union[str, List[str]]] = None , __SCREAMING_SNAKE_CASE : Optional[int] = 1 , __SCREAMING_SNAKE_CASE : float = 0.0 , __SCREAMING_SNAKE_CASE : Optional[torch.Generator] = None , __SCREAMING_SNAKE_CASE : Optional[torch.FloatTensor] = None , __SCREAMING_SNAKE_CASE : Optional[str] = "pil" , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __SCREAMING_SNAKE_CASE : int = 1 , **__SCREAMING_SNAKE_CASE : List[str] , ): A = self.segmentation_processor( text=[text] , images=[image] , padding="max_length" , return_tensors="pt").to(self.device) A = self.segmentation_model(**__SCREAMING_SNAKE_CASE) A = torch.sigmoid(outputs.logits).cpu().detach().unsqueeze(-1).numpy() A = self.numpy_to_pil(__SCREAMING_SNAKE_CASE)[0].resize(image.size) # Run inpainting pipeline with the generated mask A = StableDiffusionInpaintPipeline( vae=self.vae , text_encoder=self.text_encoder , tokenizer=self.tokenizer , unet=self.unet , scheduler=self.scheduler , safety_checker=self.safety_checker , feature_extractor=self.feature_extractor , ) return inpainting_pipeline( prompt=__SCREAMING_SNAKE_CASE , image=__SCREAMING_SNAKE_CASE , mask_image=__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 , )
57
"""simple docstring""" import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() __A : int = logging.get_logger(__name__) __A : Optional[int] = [ ('bert.bert', 'visual_bert'), ('bert.cls', 'cls'), ('bert.classifier', 'cls'), ('token_type_embeddings_visual', 'visual_token_type_embeddings'), ('position_embeddings_visual', 'visual_position_embeddings'), ('projection', 'visual_projection'), ] __A : Union[str, Any] = [ 'nlvr2_coco_pre_trained.th', 'nlvr2_fine_tuned.th', 'nlvr2_pre_trained.th', 'vcr_coco_pre_train.th', 'vcr_fine_tune.th', 'vcr_pre_train.th', 'vqa_coco_pre_trained.th', 'vqa_fine_tuned.th', 'vqa_pre_trained.th', ] def __SCREAMING_SNAKE_CASE ( lowercase__ ): """simple docstring""" A = torch.load(lowercase__ , map_location="cpu" ) return sd def __SCREAMING_SNAKE_CASE ( lowercase__ , lowercase__ , lowercase__=rename_keys_prefix ): """simple docstring""" A = OrderedDict() A = torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue A = key for name_pair in rename_keys_prefix: A = new_key.replace(name_pair[0] , name_pair[1] ) A = d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately A = new_d["cls.predictions.bias"] return new_d @torch.no_grad() def __SCREAMING_SNAKE_CASE ( lowercase__ , lowercase__ ): """simple docstring""" assert ( checkpoint_path.split("/" )[-1] in ACCEPTABLE_CHECKPOINTS ), F"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.""" # Get Config if "pre" in checkpoint_path: A = "pretraining" if "vcr" in checkpoint_path: A = {"visual_embedding_dim": 512} elif "vqa_advanced" in checkpoint_path: A = {"visual_embedding_dim": 2_048} elif "vqa" in checkpoint_path: A = {"visual_embedding_dim": 2_048} elif "nlvr" in checkpoint_path: A = {"visual_embedding_dim": 1_024} else: raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`.""" ) else: if "vcr" in checkpoint_path: A = {"visual_embedding_dim": 512} A = "multichoice" elif "vqa_advanced" in checkpoint_path: A = {"visual_embedding_dim": 2_048} A = "vqa_advanced" elif "vqa" in checkpoint_path: A = {"visual_embedding_dim": 2_048, "num_labels": 3_129} A = "vqa" elif "nlvr" in checkpoint_path: A = { "visual_embedding_dim": 1_024, "num_labels": 2, } A = "nlvr" A = VisualBertConfig(**lowercase__ ) # Load State Dict A = load_state_dict(lowercase__ ) A = get_new_dict(lowercase__ , lowercase__ ) if model_type == "pretraining": A = VisualBertForPreTraining(lowercase__ ) elif model_type == "vqa": A = VisualBertForQuestionAnswering(lowercase__ ) elif model_type == "nlvr": A = VisualBertForVisualReasoning(lowercase__ ) elif model_type == "multichoice": A = VisualBertForMultipleChoice(lowercase__ ) model.load_state_dict(lowercase__ ) # Save Checkpoints Path(lowercase__ ).mkdir(exist_ok=lowercase__ ) model.save_pretrained(lowercase__ ) if __name__ == "__main__": __A : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument('orig_checkpoint_path', type=str, help='A path to .th on local filesystem.') parser.add_argument('pytorch_dump_folder_path', type=str, help='Path to the output PyTorch model.') __A : Any = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
57
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __A =logging.get_logger(__name__) __A ={ "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class UpperCAmelCase__ ( __UpperCamelCase ): '''simple docstring''' UpperCamelCase = """deberta-v2""" def __init__( self : Optional[int] , a_ : List[str]=12_81_00 , a_ : Optional[Any]=15_36 , a_ : Optional[Any]=24 , a_ : List[Any]=24 , a_ : Optional[int]=61_44 , a_ : List[Any]="gelu" , a_ : Any=0.1 , a_ : Tuple=0.1 , a_ : Optional[Any]=5_12 , a_ : Tuple=0 , a_ : Dict=0.0_2 , a_ : Optional[Any]=1e-7 , a_ : List[str]=False , a_ : List[Any]=-1 , a_ : List[str]=0 , a_ : Optional[Any]=True , a_ : List[Any]=None , a_ : Optional[int]=0 , a_ : Tuple="gelu" , **a_ : List[str] , ): '''simple docstring''' super().__init__(**a_ ) __UpperCAmelCase : Any = hidden_size __UpperCAmelCase : Any = num_hidden_layers __UpperCAmelCase : List[Any] = num_attention_heads __UpperCAmelCase : Union[str, Any] = intermediate_size __UpperCAmelCase : List[str] = hidden_act __UpperCAmelCase : int = hidden_dropout_prob __UpperCAmelCase : List[str] = attention_probs_dropout_prob __UpperCAmelCase : str = max_position_embeddings __UpperCAmelCase : int = type_vocab_size __UpperCAmelCase : Tuple = initializer_range __UpperCAmelCase : Optional[int] = relative_attention __UpperCAmelCase : int = max_relative_positions __UpperCAmelCase : Any = pad_token_id __UpperCAmelCase : int = position_biased_input # Backwards compatibility if type(a_ ) == str: __UpperCAmelCase : Optional[Any] = [x.strip() for x in pos_att_type.lower().split('''|''' )] __UpperCAmelCase : Tuple = pos_att_type __UpperCAmelCase : int = vocab_size __UpperCAmelCase : Optional[Any] = layer_norm_eps __UpperCAmelCase : str = kwargs.get('''pooler_hidden_size''' , a_ ) __UpperCAmelCase : Union[str, Any] = pooler_dropout __UpperCAmelCase : Tuple = pooler_hidden_act class UpperCAmelCase__ ( __UpperCamelCase ): '''simple docstring''' @property def snake_case__ ( self : Optional[Any] ): '''simple docstring''' if self.task == "multiple-choice": __UpperCAmelCase : List[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: __UpperCAmelCase : int = {0: '''batch''', 1: '''sequence'''} if self._config.type_vocab_size > 0: return OrderedDict( [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)] ) else: return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)] ) @property def snake_case__ ( self : Union[str, Any] ): '''simple docstring''' return 12 def snake_case__ ( self : Any , a_ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , a_ : int = -1 , a_ : int = -1 , a_ : int = -1 , a_ : bool = False , a_ : Optional["TensorType"] = None , a_ : int = 3 , a_ : int = 40 , a_ : int = 40 , a_ : "PreTrainedTokenizerBase" = None , ): '''simple docstring''' __UpperCAmelCase : List[Any] = super().generate_dummy_inputs(preprocessor=a_ , framework=a_ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
226
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __A =logging.get_logger(__name__) def a ( _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : Dict ): '''simple docstring''' __UpperCAmelCase : List[str] = b.T __UpperCAmelCase : Any = np.sum(np.square(_UpperCAmelCase ) , axis=1 ) __UpperCAmelCase : int = np.sum(np.square(_UpperCAmelCase ) , axis=0 ) __UpperCAmelCase : Optional[int] = np.matmul(_UpperCAmelCase , _UpperCAmelCase ) __UpperCAmelCase : List[str] = aa[:, None] - 2 * ab + ba[None, :] return d def a ( _UpperCAmelCase : Optional[int] , _UpperCAmelCase : Optional[Any] ): '''simple docstring''' __UpperCAmelCase : str = x.reshape(-1 , 3 ) __UpperCAmelCase : Optional[int] = squared_euclidean_distance(_UpperCAmelCase , _UpperCAmelCase ) return np.argmin(_UpperCAmelCase , axis=1 ) class UpperCAmelCase__ ( __UpperCamelCase ): '''simple docstring''' UpperCamelCase = ["""pixel_values"""] def __init__( self : str , a_ : Optional[Union[List[List[int]], np.ndarray]] = None , a_ : bool = True , a_ : Dict[str, int] = None , a_ : PILImageResampling = PILImageResampling.BILINEAR , a_ : bool = True , a_ : bool = True , **a_ : List[str] , ): '''simple docstring''' super().__init__(**a_ ) __UpperCAmelCase : Optional[int] = size if size is not None else {'''height''': 2_56, '''width''': 2_56} __UpperCAmelCase : List[str] = get_size_dict(a_ ) __UpperCAmelCase : str = np.array(a_ ) if clusters is not None else None __UpperCAmelCase : Dict = do_resize __UpperCAmelCase : Tuple = size __UpperCAmelCase : Union[str, Any] = resample __UpperCAmelCase : Tuple = do_normalize __UpperCAmelCase : Optional[int] = do_color_quantize def snake_case__ ( self : Optional[Any] , a_ : np.ndarray , a_ : Dict[str, int] , a_ : PILImageResampling = PILImageResampling.BILINEAR , a_ : Optional[Union[str, ChannelDimension]] = None , **a_ : Dict , ): '''simple docstring''' __UpperCAmelCase : Tuple = get_size_dict(a_ ) if "height" not in size or "width" not in size: raise ValueError(F'Size dictionary must contain both height and width keys. Got {size.keys()}' ) return resize( a_ , size=(size['''height'''], size['''width''']) , resample=a_ , data_format=a_ , **a_ ) def snake_case__ ( self : Tuple , a_ : np.ndarray , a_ : Optional[Union[str, ChannelDimension]] = None , ): '''simple docstring''' __UpperCAmelCase : Dict = rescale(image=a_ , scale=1 / 1_2_7.5 , data_format=a_ ) __UpperCAmelCase : Union[str, Any] = image - 1 return image def snake_case__ ( self : int , a_ : ImageInput , a_ : bool = None , a_ : Dict[str, int] = None , a_ : PILImageResampling = None , a_ : bool = None , a_ : Optional[bool] = None , a_ : Optional[Union[List[List[int]], np.ndarray]] = None , a_ : Optional[Union[str, TensorType]] = None , a_ : Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST , **a_ : Any , ): '''simple docstring''' __UpperCAmelCase : Any = do_resize if do_resize is not None else self.do_resize __UpperCAmelCase : List[str] = size if size is not None else self.size __UpperCAmelCase : Any = get_size_dict(a_ ) __UpperCAmelCase : Optional[int] = resample if resample is not None else self.resample __UpperCAmelCase : List[str] = do_normalize if do_normalize is not None else self.do_normalize __UpperCAmelCase : int = do_color_quantize if do_color_quantize is not None else self.do_color_quantize __UpperCAmelCase : Optional[int] = clusters if clusters is not None else self.clusters __UpperCAmelCase : Any = np.array(a_ ) __UpperCAmelCase : Optional[int] = make_list_of_images(a_ ) if not valid_images(a_ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_color_quantize and clusters is None: raise ValueError('''Clusters must be specified if do_color_quantize is True.''' ) # All transformations expect numpy arrays. __UpperCAmelCase : List[Any] = [to_numpy_array(a_ ) for image in images] if do_resize: __UpperCAmelCase : List[str] = [self.resize(image=a_ , size=a_ , resample=a_ ) for image in images] if do_normalize: __UpperCAmelCase : Dict = [self.normalize(image=a_ ) for image in images] if do_color_quantize: __UpperCAmelCase : int = [to_channel_dimension_format(a_ , ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) __UpperCAmelCase : List[str] = np.array(a_ ) __UpperCAmelCase : Dict = color_quantize(a_ , a_ ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) __UpperCAmelCase : Any = images.shape[0] __UpperCAmelCase : Any = images.reshape(a_ , -1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. __UpperCAmelCase : List[Any] = list(a_ ) else: __UpperCAmelCase : int = [to_channel_dimension_format(a_ , a_ ) for image in images] __UpperCAmelCase : int = {'''input_ids''': images} return BatchFeature(data=a_ , tensor_type=a_ )
226
1
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position UpperCAmelCase__ = "2.13.1" import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse("3.7"): raise ImportWarning( "To use `datasets`, Python>=3.7 is required, and the current version of Python doesn't match this condition." ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( "To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn't match this condition.\n" "If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`." ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip UpperCAmelCase__ = concatenate_datasets UpperCAmelCase__ = DownloadConfig UpperCAmelCase__ = DownloadManager UpperCAmelCase__ = DownloadMode UpperCAmelCase__ = DownloadConfig UpperCAmelCase__ = DownloadMode UpperCAmelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
26
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm UpperCAmelCase__ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex UpperCAmelCase__ = 10 UpperCAmelCase__ = 256 def _a ( a :List[str] ) -> Optional[MinHash]: if len(a ) < MIN_NUM_TOKENS: return None a = MinHash(num_perm=a ) for token in set(a ): min_hash.update(token.encode() ) return min_hash def _a ( a :str ) -> Set[str]: return {t for t in NON_ALPHA.split(a ) if len(t.strip() ) > 0} class lowercase_ : '''simple docstring''' def __init__( self : Any , *, __UpperCAmelCase : float = 0.85 , ) ->Dict: """simple docstring""" a = duplication_jaccard_threshold a = NUM_PERM a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a = defaultdict(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : MinHash ) ->None: """simple docstring""" a = self._index.query(__UpperCAmelCase ) if code_key in self._index.keys: print(F"""Duplicate key {code_key}""" ) return self._index.insert(__UpperCAmelCase , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCAmelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[List[Dict]]: """simple docstring""" a = [] for base, duplicates in self._duplicate_clusters.items(): a = [base] + list(__UpperCAmelCase ) # reformat the cluster to be a list of dict a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCAmelCase ) return duplicate_clusters def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = self.get_duplicate_clusters() with open(__UpperCAmelCase , '''w''' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def _a ( a :List[Any] ) -> List[Any]: a , a = element a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _a ( a :Type[Dataset] ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def _a ( a :Type[Dataset] , a :float ) -> str: a = DuplicationIndex(duplication_jaccard_threshold=a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a ) ) , max_queue_size=100 ) ): di.add(a , a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _a ( a :str , a :str ) -> float: a = get_tokens(a ) a = get_tokens(a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) UpperCAmelCase__ = None def _a ( a :Tuple , a :Tuple ) -> Any: a = [] for elementa in cluster: a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(a , a ) >= jaccard_threshold: elementa["copies"] += 1 break else: a = 1 extremes.append(a ) return extremes def _a ( a :List[Any] , a :Optional[Any] , a :Union[str, Any] ) -> Optional[int]: global _shared_dataset a = dataset a = [] a = partial(_find_cluster_extremes_shared , jaccard_threshold=a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a , a , ) , total=len(a ) , ): extremes_list.append(a ) return extremes_list def _a ( a :Type[Dataset] , a :float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: a = make_duplicate_clusters(a , a ) a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} a = {} a = find_extremes(a , a , a ) for extremes in extremes_clusters: for element in extremes: a = element a = duplicate_indices - set(extreme_dict.keys() ) a = dataset.filter(lambda a , a : idx not in remove_indices , with_indices=a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a = element['''base_index'''] in extreme_dict if element["is_extreme"]: a = extreme_dict[element['''base_index''']]['''copies'''] print(F"""Original dataset size: {len(a )}""" ) print(F"""Number of duplicate clusters: {len(a )}""" ) print(F"""Files in duplicate cluster: {len(a )}""" ) print(F"""Unique files in duplicate cluster: {len(a )}""" ) print(F"""Filtered dataset size: {len(a )}""" ) return ds_filter, duplicate_clusters
26
1
import re import string import numpy as np import datasets SCREAMING_SNAKE_CASE :Optional[int] = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' SCREAMING_SNAKE_CASE :Tuple = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' SCREAMING_SNAKE_CASE :str = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self : List[Any] ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { "predictions": datasets.Value("string" ,id="sequence" ), "references": datasets.Value("string" ,id="sequence" ), } ) ,reference_urls=[] ,) def UpperCamelCase_ ( self : List[Any] ,A : List[Any] ,A : List[Any] ,A : Any=None ,A : List[Any]=False ,A : Any=False ,A : Dict=False ,): if regexes_to_ignore is not None: for s in regexes_to_ignore: __A = np.array([re.sub(A ,"" ,A ) for x in predictions] ) __A = np.array([re.sub(A ,"" ,A ) for x in references] ) else: __A = np.asarray(A ) __A = np.asarray(A ) if ignore_case: __A = np.char.lower(A ) __A = np.char.lower(A ) if ignore_punctuation: __A = string.punctuation.maketrans("" ,"" ,string.punctuation ) __A = np.char.translate(A ,table=A ) __A = np.char.translate(A ,table=A ) if ignore_numbers: __A = string.digits.maketrans("" ,"" ,string.digits ) __A = np.char.translate(A ,table=A ) __A = np.char.translate(A ,table=A ) __A = predictions == references return {"exact_match": np.mean(A ) * 1_00}
15
"""simple docstring""" import os from datetime import datetime as dt from github import Github lowercase__ : int = [ '''good first issue''', '''good second issue''', '''good difficult issue''', '''enhancement''', '''new pipeline/model''', '''new scheduler''', '''wip''', ] def __lowercase ( ): snake_case_ : Optional[Any] = Github(os.environ['''GITHUB_TOKEN'''] ) snake_case_ : Any = g.get_repo('''huggingface/diffusers''' ) snake_case_ : Any = repo.get_issues(state='''open''' ) for issue in open_issues: snake_case_ : str = sorted(issue.get_comments() , key=lambda _a : i.created_at , reverse=_a ) snake_case_ : Dict = comments[0] if len(_a ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Closes the issue after 7 days of inactivity since the Stalebot notification. issue.edit(state='''closed''' ) elif ( "stale" in issue.get_labels() and last_comment is not None and last_comment.user.login != "github-actions[bot]" ): # Opens the issue if someone other than Stalebot commented. issue.edit(state='''open''' ) issue.remove_from_labels('''stale''' ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Post a Stalebot notification after 23 days of inactivity. issue.create_comment( '''This issue has been automatically marked as stale because it has not had ''' '''recent activity. If you think this still needs to be addressed ''' '''please comment on this thread.\n\nPlease note that issues that do not follow the ''' '''[contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) ''' '''are likely to be ignored.''' ) issue.add_to_labels('''stale''' ) if __name__ == "__main__": main()
264
0
"""simple docstring""" import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _a ( _a ): UpperCamelCase = ['''image_processor''', '''tokenizer'''] UpperCamelCase = '''LayoutLMv2ImageProcessor''' UpperCamelCase = ('''LayoutXLMTokenizer''', '''LayoutXLMTokenizerFast''') def __init__( self : str, lowerCAmelCase__ : List[Any]=None, lowerCAmelCase__ : Any=None, **lowerCAmelCase__ : Union[str, Any] ) -> List[str]: '''simple docstring''' if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''', snake_case_, ) _UpperCamelCase : Union[str, Any] = kwargs.pop('''feature_extractor''' ) _UpperCamelCase : Optional[int] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(snake_case_, snake_case_ ) def __call__( self : int, lowerCAmelCase__ : Optional[Any], lowerCAmelCase__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, lowerCAmelCase__ : Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, lowerCAmelCase__ : Union[List[List[int]], List[List[List[int]]]] = None, lowerCAmelCase__ : Optional[Union[List[int], List[List[int]]]] = None, lowerCAmelCase__ : bool = True, lowerCAmelCase__ : Union[bool, str, PaddingStrategy] = False, lowerCAmelCase__ : Union[bool, str, TruncationStrategy] = None, lowerCAmelCase__ : Optional[int] = None, lowerCAmelCase__ : int = 0, lowerCAmelCase__ : Optional[int] = None, lowerCAmelCase__ : Optional[bool] = None, lowerCAmelCase__ : Optional[bool] = None, lowerCAmelCase__ : bool = False, lowerCAmelCase__ : bool = False, lowerCAmelCase__ : bool = False, lowerCAmelCase__ : bool = False, lowerCAmelCase__ : bool = True, lowerCAmelCase__ : Optional[Union[str, TensorType]] = None, **lowerCAmelCase__ : Optional[Any], ) -> Dict: '''simple docstring''' if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor _UpperCamelCase : List[Any] = self.image_processor(images=snake_case_, return_tensors=snake_case_ ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(snake_case_, snake_case_ ): _UpperCamelCase : Optional[Any] = [text] # add batch dimension (as the image processor always adds a batch dimension) _UpperCamelCase : str = features["""words"""] _UpperCamelCase : Optional[int] = self.tokenizer( text=text if text is not None else features['''words'''], text_pair=text_pair if text_pair is not None else None, boxes=boxes if boxes is not None else features['''boxes'''], word_labels=snake_case_, add_special_tokens=snake_case_, padding=snake_case_, truncation=snake_case_, max_length=snake_case_, stride=snake_case_, pad_to_multiple_of=snake_case_, return_token_type_ids=snake_case_, return_attention_mask=snake_case_, return_overflowing_tokens=snake_case_, return_special_tokens_mask=snake_case_, return_offsets_mapping=snake_case_, return_length=snake_case_, verbose=snake_case_, return_tensors=snake_case_, **snake_case_, ) # add pixel values _UpperCamelCase : List[str] = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: _UpperCamelCase : Tuple = self.get_overflowing_images(snake_case_, encoded_inputs['''overflow_to_sample_mapping'''] ) _UpperCamelCase : str = images return encoded_inputs def snake_case ( self : Union[str, Any], lowerCAmelCase__ : Union[str, Any], lowerCAmelCase__ : List[str] ) -> str: '''simple docstring''' _UpperCamelCase : Tuple = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(snake_case_ ) != len(snake_case_ ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(snake_case_ )} and {len(snake_case_ )}""" ) return images_with_overflow def snake_case ( self : List[str], *lowerCAmelCase__ : Any, **lowerCAmelCase__ : Optional[Any] ) -> List[Any]: '''simple docstring''' return self.tokenizer.batch_decode(*snake_case_, **snake_case_ ) def snake_case ( self : Union[str, Any], *lowerCAmelCase__ : Any, **lowerCAmelCase__ : Optional[Any] ) -> Any: '''simple docstring''' return self.tokenizer.decode(*snake_case_, **snake_case_ ) @property def snake_case ( self : int ) -> List[str]: '''simple docstring''' return ["input_ids", "bbox", "attention_mask", "image"] @property def snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', snake_case_, ) return self.image_processor_class @property def snake_case ( self : Optional[int] ) -> int: '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', snake_case_, ) return self.image_processor
371
"""simple docstring""" from __future__ import annotations from random import choice def a_ ( _lowercase ): return choice(_lowercase ) def a_ ( _lowercase , _lowercase ): _UpperCamelCase : Optional[int] = random_pivot(_lowercase ) # partition based on pivot # linear time _UpperCamelCase : Union[str, Any] = [e for e in lst if e < pivot] _UpperCamelCase : int = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(_lowercase ) == k - 1: return pivot # pivot is in elements bigger than k elif len(_lowercase ) < k - 1: return kth_number(_lowercase , k - len(_lowercase ) - 1 ) # pivot is in elements smaller than k else: return kth_number(_lowercase , _lowercase ) if __name__ == "__main__": import doctest doctest.testmod()
128
0
'''simple docstring''' import math def UpperCAmelCase_ (__a : int ): """simple docstring""" 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(__a ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def UpperCAmelCase_ (__a : float = 0.1 ): """simple docstring""" _a : Dict = 3 _a : Optional[Any] = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(__a ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
271
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __lowerCAmelCase = HUGGINGFACE_HUB_CACHE __lowerCAmelCase = """config.json""" __lowerCAmelCase = """diffusion_pytorch_model.bin""" __lowerCAmelCase = """diffusion_flax_model.msgpack""" __lowerCAmelCase = """model.onnx""" __lowerCAmelCase = """diffusion_pytorch_model.safetensors""" __lowerCAmelCase = """weights.pb""" __lowerCAmelCase = """https://huggingface.co""" __lowerCAmelCase = default_cache_path __lowerCAmelCase = """diffusers_modules""" __lowerCAmelCase = os.getenv("""HF_MODULES_CACHE""", os.path.join(hf_cache_home, """modules""")) __lowerCAmelCase = ["""fp16""", """non-ema"""] __lowerCAmelCase = """.self_attn"""
271
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase = logging.get_logger(__name__) __lowercase = { """facebook/s2t-small-librispeech-asr""": ( """https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json""" ), # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text } class _A ( _a ): """simple docstring""" UpperCAmelCase : List[Any] = """speech_to_text""" UpperCAmelCase : Optional[Any] = ["""past_key_values"""] UpperCAmelCase : Dict = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self : Any , __UpperCAmelCase : Optional[int]=10000 , __UpperCAmelCase : str=12 , __UpperCAmelCase : Optional[int]=2048 , __UpperCAmelCase : List[Any]=4 , __UpperCAmelCase : int=6 , __UpperCAmelCase : int=2048 , __UpperCAmelCase : int=4 , __UpperCAmelCase : Tuple=0.0 , __UpperCAmelCase : Union[str, Any]=0.0 , __UpperCAmelCase : str=True , __UpperCAmelCase : str=True , __UpperCAmelCase : str="relu" , __UpperCAmelCase : Union[str, Any]=256 , __UpperCAmelCase : List[str]=0.1 , __UpperCAmelCase : List[Any]=0.0 , __UpperCAmelCase : Optional[Any]=0.0 , __UpperCAmelCase : Union[str, Any]=0.02 , __UpperCAmelCase : Tuple=2 , __UpperCAmelCase : str=True , __UpperCAmelCase : Optional[Any]=1 , __UpperCAmelCase : Dict=0 , __UpperCAmelCase : str=2 , __UpperCAmelCase : List[str]=6000 , __UpperCAmelCase : Union[str, Any]=1024 , __UpperCAmelCase : List[str]=2 , __UpperCAmelCase : List[str]=(5, 5) , __UpperCAmelCase : Any=1024 , __UpperCAmelCase : List[Any]=80 , __UpperCAmelCase : List[Any]=1 , **__UpperCAmelCase : List[str] , ): a : Dict = vocab_size a : Optional[int] = d_model a : Any = encoder_ffn_dim a : Any = encoder_layers a : List[str] = encoder_attention_heads a : str = decoder_ffn_dim a : Any = decoder_layers a : Union[str, Any] = decoder_attention_heads a : Optional[Any] = dropout a : Optional[int] = attention_dropout a : Any = activation_dropout a : Optional[Any] = activation_function a : List[str] = init_std a : Union[str, Any] = encoder_layerdrop a : Any = decoder_layerdrop a : List[Any] = use_cache a : List[Any] = encoder_layers a : int = scale_embedding # scale factor will be sqrt(d_model) if True a : str = max_source_positions a : str = max_target_positions a : Optional[Any] = num_conv_layers a : List[str] = list(__UpperCAmelCase) a : Any = conv_channels a : Union[str, Any] = input_feat_per_channel a : Union[str, Any] = input_channels if len(self.conv_kernel_sizes) != self.num_conv_layers: raise ValueError( "Configuration for convolutional module is incorrect. " "It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` " f'''but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes)}`, ''' f'''`config.num_conv_layers = {self.num_conv_layers}`.''') super().__init__( pad_token_id=__UpperCAmelCase , bos_token_id=__UpperCAmelCase , eos_token_id=__UpperCAmelCase , is_encoder_decoder=__UpperCAmelCase , decoder_start_token_id=__UpperCAmelCase , **__UpperCAmelCase , )
226
"""simple docstring""" import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor __lowercase = logging.get_logger(__name__) class _A ( _a ): """simple docstring""" def __init__( self : List[Any] , *__UpperCAmelCase : int , **__UpperCAmelCase : List[Any]): warnings.warn( "The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use DPTImageProcessor instead." , __UpperCAmelCase , ) super().__init__(*__UpperCAmelCase , **__UpperCAmelCase)
226
1
"""simple docstring""" import math from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''facebook/data2vec-base-960h''': '''https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json''', # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : str ="data2vec-audio" def __init__( self , snake_case__=32 , snake_case__=768 , snake_case__=12 , snake_case__=12 , snake_case__=3_072 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-5 , snake_case__="gelu" , snake_case__=(512, 512, 512, 512, 512, 512, 512) , snake_case__=(5, 2, 2, 2, 2, 2, 2) , snake_case__=(10, 3, 3, 3, 3, 2, 2) , snake_case__=False , snake_case__=16 , snake_case__=19 , snake_case__=5 , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__="sum" , snake_case__=False , snake_case__=False , snake_case__=256 , snake_case__=(512, 512, 512, 512, 1_500) , snake_case__=(5, 3, 3, 1, 1) , snake_case__=(1, 2, 3, 1, 1) , snake_case__=512 , snake_case__=0 , snake_case__=1 , snake_case__=2 , snake_case__=False , snake_case__=3 , snake_case__=2 , snake_case__=3 , snake_case__=None , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) lowerCAmelCase : int = hidden_size lowerCAmelCase : Tuple = feat_extract_activation lowerCAmelCase : Any = list(snake_case__ ) lowerCAmelCase : List[Any] = list(snake_case__ ) lowerCAmelCase : Dict = list(snake_case__ ) lowerCAmelCase : List[Any] = conv_bias lowerCAmelCase : Any = num_conv_pos_embeddings lowerCAmelCase : List[str] = num_conv_pos_embedding_groups lowerCAmelCase : List[str] = conv_pos_kernel_size lowerCAmelCase : Tuple = len(self.conv_dim ) lowerCAmelCase : Dict = num_hidden_layers lowerCAmelCase : List[Any] = intermediate_size lowerCAmelCase : Any = hidden_act lowerCAmelCase : Optional[int] = num_attention_heads lowerCAmelCase : Optional[int] = hidden_dropout lowerCAmelCase : Optional[int] = attention_dropout lowerCAmelCase : Tuple = activation_dropout lowerCAmelCase : Any = feat_proj_dropout lowerCAmelCase : List[str] = final_dropout lowerCAmelCase : Optional[Any] = layerdrop lowerCAmelCase : List[Any] = layer_norm_eps lowerCAmelCase : List[Any] = initializer_range lowerCAmelCase : Optional[Any] = vocab_size lowerCAmelCase : str = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==" " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =" f""" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCAmelCase : int = mask_time_prob lowerCAmelCase : List[Any] = mask_time_length lowerCAmelCase : int = mask_time_min_masks lowerCAmelCase : Any = mask_feature_prob lowerCAmelCase : List[Any] = mask_feature_length lowerCAmelCase : Optional[Any] = mask_feature_min_masks # ctc loss lowerCAmelCase : Dict = ctc_loss_reduction lowerCAmelCase : Tuple = ctc_zero_infinity # adapter lowerCAmelCase : int = add_adapter lowerCAmelCase : Dict = adapter_kernel_size lowerCAmelCase : Tuple = adapter_stride lowerCAmelCase : Tuple = num_adapter_layers lowerCAmelCase : Optional[int] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. lowerCAmelCase : int = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. lowerCAmelCase : List[Any] = list(snake_case__ ) lowerCAmelCase : Optional[int] = list(snake_case__ ) lowerCAmelCase : List[str] = list(snake_case__ ) lowerCAmelCase : Dict = xvector_output_dim @property def lowercase__ ( self ): """simple docstring""" return math.prod(self.conv_stride )
108
import os import pytest from attr import dataclass lowerCamelCase = 'us-east-1' # defaults region @dataclass class A : UpperCamelCase__ : str UpperCamelCase__ : Dict ='arn:aws:iam::558105141721:role/sagemaker_execution_role' UpperCamelCase__ : int ={ 'task_name': 'mnli', 'per_device_train_batch_size': 16, 'per_device_eval_batch_size': 16, 'do_train': True, 'do_eval': True, 'do_predict': True, 'output_dir': '/opt/ml/model', 'overwrite_output_dir': True, 'max_steps': 500, 'save_steps': 5500, } UpperCamelCase__ : Optional[Any] ={**hyperparameters, 'max_steps': 1000} @property def lowerCamelCase ( self : List[Any] ) -> str: """simple docstring""" if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def lowerCamelCase ( self : List[Any] ) -> str: """simple docstring""" return F'''{self.framework}-transfromers-test''' @property def lowerCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" return F'''./tests/sagemaker/scripts/{self.framework}''' @property def lowerCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope='class' ) def a_ ( SCREAMING_SNAKE_CASE__ : Tuple ): '''simple docstring''' _lowerCamelCase : List[Any] =SageMakerTestEnvironment(framework=request.cls.framework )
199
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __SCREAMING_SNAKE_CASE :Union[str, Any] = logging.get_logger(__name__) class A_ ( __a ): _lowerCamelCase : Any = ["""pixel_values"""] def __init__( self : str , snake_case_ : bool = True , snake_case_ : Dict[str, int] = None , snake_case_ : float = None , snake_case_ : PILImageResampling = PILImageResampling.BILINEAR , snake_case_ : bool = True , snake_case_ : Union[int, float] = 1 / 2_5_5 , snake_case_ : bool = True , snake_case_ : Optional[Union[float, List[float]]] = None , snake_case_ : Optional[Union[float, List[float]]] = None , **snake_case_ : int , ): super().__init__(**a__ ) _UpperCAmelCase = size if size is not None else {"shortest_edge": 3_8_4} _UpperCAmelCase = get_size_dict(a__ , default_to_square=a__ ) _UpperCAmelCase = do_resize _UpperCAmelCase = size # Default value set here for backwards compatibility where the value in config is None _UpperCAmelCase = crop_pct if crop_pct is not None else 2_2_4 / 2_5_6 _UpperCAmelCase = resample _UpperCAmelCase = do_rescale _UpperCAmelCase = rescale_factor _UpperCAmelCase = do_normalize _UpperCAmelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _UpperCAmelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowercase ( self : List[str] , snake_case_ : np.ndarray , snake_case_ : Dict[str, int] , snake_case_ : float , snake_case_ : PILImageResampling = PILImageResampling.BICUBIC , snake_case_ : Optional[Union[str, ChannelDimension]] = None , **snake_case_ : List[Any] , ): _UpperCAmelCase = get_size_dict(a__ , default_to_square=a__ ) if "shortest_edge" not in size: raise ValueError(f'Size dictionary must contain \'shortest_edge\' key. Got {size.keys()}' ) _UpperCAmelCase = size["shortest_edge"] if shortest_edge < 3_8_4: # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct _UpperCAmelCase = int(shortest_edge / crop_pct ) _UpperCAmelCase = get_resize_output_image_size(a__ , size=a__ , default_to_square=a__ ) _UpperCAmelCase = resize(image=a__ , size=a__ , resample=a__ , data_format=a__ , **a__ ) # then crop to (shortest_edge, shortest_edge) return center_crop(image=a__ , size=(shortest_edge, shortest_edge) , data_format=a__ , **a__ ) else: # warping (no cropping) when evaluated at 384 or larger return resize( a__ , size=(shortest_edge, shortest_edge) , resample=a__ , data_format=a__ , **a__ ) def lowercase ( self : List[Any] , snake_case_ : np.ndarray , snake_case_ : Union[int, float] , snake_case_ : Optional[Union[str, ChannelDimension]] = None , **snake_case_ : Tuple , ): return rescale(a__ , scale=a__ , data_format=a__ , **a__ ) def lowercase ( self : Optional[int] , snake_case_ : np.ndarray , snake_case_ : Union[float, List[float]] , snake_case_ : Union[float, List[float]] , snake_case_ : Optional[Union[str, ChannelDimension]] = None , **snake_case_ : int , ): return normalize(a__ , mean=a__ , std=a__ , data_format=a__ , **a__ ) def lowercase ( self : List[Any] , snake_case_ : ImageInput , snake_case_ : bool = None , snake_case_ : Dict[str, int] = None , snake_case_ : float = None , snake_case_ : PILImageResampling = None , snake_case_ : bool = None , snake_case_ : float = None , snake_case_ : bool = None , snake_case_ : Optional[Union[float, List[float]]] = None , snake_case_ : Optional[Union[float, List[float]]] = None , snake_case_ : Optional[Union[str, TensorType]] = None , snake_case_ : ChannelDimension = ChannelDimension.FIRST , **snake_case_ : Optional[int] , ): _UpperCAmelCase = do_resize if do_resize is not None else self.do_resize _UpperCAmelCase = crop_pct if crop_pct is not None else self.crop_pct _UpperCAmelCase = resample if resample is not None else self.resample _UpperCAmelCase = do_rescale if do_rescale is not None else self.do_rescale _UpperCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor _UpperCAmelCase = do_normalize if do_normalize is not None else self.do_normalize _UpperCAmelCase = image_mean if image_mean is not None else self.image_mean _UpperCAmelCase = image_std if image_std is not None else self.image_std _UpperCAmelCase = size if size is not None else self.size _UpperCAmelCase = get_size_dict(a__ , default_to_square=a__ ) _UpperCAmelCase = make_list_of_images(a__ ) if not valid_images(a__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_resize and size["shortest_edge"] < 3_8_4 and crop_pct is None: raise ValueError("crop_pct must be specified if size < 384." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # All transformations expect numpy arrays. _UpperCAmelCase = [to_numpy_array(a__ ) for image in images] if do_resize: _UpperCAmelCase = [self.resize(image=a__ , size=a__ , crop_pct=a__ , resample=a__ ) for image in images] if do_rescale: _UpperCAmelCase = [self.rescale(image=a__ , scale=a__ ) for image in images] if do_normalize: _UpperCAmelCase = [self.normalize(image=a__ , mean=a__ , std=a__ ) for image in images] _UpperCAmelCase = [to_channel_dimension_format(a__ , a__ ) for image in images] _UpperCAmelCase = {"pixel_values": images} return BatchFeature(data=a__ , tensor_type=a__ )
360
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class A_ : def __init__( self : List[Any] , snake_case_ : List[Any] , snake_case_ : Optional[Any]=1_3 , snake_case_ : List[Any]=1_0 , snake_case_ : Tuple=3 , snake_case_ : Tuple=2 , snake_case_ : List[str]=2 , snake_case_ : Optional[int]=2 , snake_case_ : Optional[Any]=True , snake_case_ : Union[str, Any]=True , snake_case_ : List[Any]=3_2 , snake_case_ : Optional[Any]=5 , snake_case_ : List[Any]=4 , snake_case_ : int=3_7 , snake_case_ : str="gelu" , snake_case_ : str=0.1 , snake_case_ : List[str]=0.1 , snake_case_ : Optional[Any]=1_0 , snake_case_ : List[str]=0.0_2 , snake_case_ : int=0.9 , snake_case_ : List[Any]=None , ): _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = image_size _UpperCAmelCase = num_channels _UpperCAmelCase = patch_size _UpperCAmelCase = tubelet_size _UpperCAmelCase = num_frames _UpperCAmelCase = is_training _UpperCAmelCase = use_labels _UpperCAmelCase = hidden_size _UpperCAmelCase = num_hidden_layers _UpperCAmelCase = num_attention_heads _UpperCAmelCase = intermediate_size _UpperCAmelCase = hidden_act _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = type_sequence_label_size _UpperCAmelCase = initializer_range _UpperCAmelCase = mask_ratio _UpperCAmelCase = scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame _UpperCAmelCase = (image_size // patch_size) ** 2 _UpperCAmelCase = (num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos _UpperCAmelCase = int(mask_ratio * self.seq_length ) def lowercase ( self : Tuple ): _UpperCAmelCase = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) _UpperCAmelCase = None if self.use_labels: _UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase = self.get_config() return config, pixel_values, labels def lowercase ( self : Optional[int] ): return VideoMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_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 , is_decoder=snake_case_ , initializer_range=self.initializer_range , ) def lowercase ( self : Tuple , snake_case_ : int , snake_case_ : str , snake_case_ : str ): _UpperCAmelCase = VideoMAEModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() _UpperCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase ( self : Union[str, Any] , snake_case_ : List[str] , snake_case_ : Optional[int] , snake_case_ : Tuple ): _UpperCAmelCase = VideoMAEForPreTraining(snake_case_ ) model.to(snake_case_ ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch _UpperCAmelCase = torch.ones((self.num_masks,) ) _UpperCAmelCase = torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) _UpperCAmelCase = mask.expand(self.batch_size , -1 ).bool() _UpperCAmelCase = model(snake_case_ , snake_case_ ) # model only returns predictions for masked patches _UpperCAmelCase = mask.sum().item() _UpperCAmelCase = 3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels) ) def lowercase ( self : Dict ): _UpperCAmelCase = self.prepare_config_and_inputs() _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = config_and_inputs _UpperCAmelCase = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class A_ ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): _lowerCamelCase : Any = ( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) _lowerCamelCase : List[Any] = ( {"""feature-extraction""": VideoMAEModel, """video-classification""": VideoMAEForVideoClassification} if is_torch_available() else {} ) _lowerCamelCase : Union[str, Any] = False _lowerCamelCase : Any = False _lowerCamelCase : str = False _lowerCamelCase : str = False def lowercase ( self : List[str] ): _UpperCAmelCase = VideoMAEModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=snake_case_ , has_text_modality=snake_case_ , hidden_size=3_7 ) def lowercase ( self : Any , snake_case_ : Tuple , snake_case_ : Tuple , snake_case_ : Union[str, Any]=False ): _UpperCAmelCase = copy.deepcopy(snake_case_ ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch _UpperCAmelCase = torch.ones((self.model_tester.num_masks,) ) _UpperCAmelCase = torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) _UpperCAmelCase = mask.expand(self.model_tester.batch_size , -1 ).bool() _UpperCAmelCase = bool_masked_pos.to(snake_case_ ) if return_labels: if model_class in [ *get_values(snake_case_ ), ]: _UpperCAmelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case_ ) return inputs_dict def lowercase ( self : Optional[int] ): self.config_tester.run_common_tests() @unittest.skip(reason="VideoMAE does not use inputs_embeds" ) def lowercase ( self : Union[str, Any] ): pass def lowercase ( self : str ): _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(snake_case_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _UpperCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case_ , nn.Linear ) ) def lowercase ( self : Union[str, Any] ): _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(snake_case_ ) _UpperCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCAmelCase = [*signature.parameters.keys()] _UpperCAmelCase = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case_ ) def lowercase ( self : Any ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def lowercase ( self : Any ): _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*snake_case_ ) @slow def lowercase ( self : List[Any] ): for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase = VideoMAEModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) def lowercase ( self : Tuple ): if not self.has_attentions: pass else: _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = True for model_class in self.all_model_classes: _UpperCAmelCase = self.model_tester.seq_length - self.model_tester.num_masks _UpperCAmelCase = ( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) _UpperCAmelCase = True _UpperCAmelCase = False _UpperCAmelCase = True _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _UpperCAmelCase = outputs.attentions self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _UpperCAmelCase = True _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _UpperCAmelCase = outputs.attentions self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) _UpperCAmelCase = len(snake_case_ ) # Check attention is always last and order is fine _UpperCAmelCase = True _UpperCAmelCase = True _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) self.assertEqual(out_len + 1 , len(snake_case_ ) ) _UpperCAmelCase = outputs.attentions self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def lowercase ( self : Tuple ): def check_hidden_states_output(snake_case_ : List[Any] , snake_case_ : Union[str, Any] , snake_case_ : Dict ): _UpperCAmelCase = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _UpperCAmelCase = outputs.hidden_states _UpperCAmelCase = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(snake_case_ ) , snake_case_ ) _UpperCAmelCase = self.model_tester.seq_length - self.model_tester.num_masks _UpperCAmelCase = num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = True check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _UpperCAmelCase = True check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def lowercase ( self : int ): pass def UpperCAmelCase_ ( ) -> List[Any]: '''simple docstring''' _UpperCAmelCase = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) _UpperCAmelCase = np.load(__lowercase ) return list(__lowercase ) @require_torch @require_vision class A_ ( unittest.TestCase ): @cached_property def lowercase ( self : List[str] ): # logits were tested with a different mean and std, so we use the same here return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def lowercase ( self : Tuple ): _UpperCAmelCase = VideoMAEForVideoClassification.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics" ).to( snake_case_ ) _UpperCAmelCase = self.default_image_processor _UpperCAmelCase = prepare_video() _UpperCAmelCase = image_processor(snake_case_ , return_tensors="pt" ).to(snake_case_ ) # forward pass with torch.no_grad(): _UpperCAmelCase = model(**snake_case_ ) # verify the logits _UpperCAmelCase = torch.Size((1, 4_0_0) ) self.assertEqual(outputs.logits.shape , snake_case_ ) _UpperCAmelCase = torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ).to(snake_case_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case_ , atol=1e-4 ) ) @slow def lowercase ( self : List[Any] ): _UpperCAmelCase = VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short" ).to(snake_case_ ) _UpperCAmelCase = self.default_image_processor _UpperCAmelCase = prepare_video() _UpperCAmelCase = image_processor(snake_case_ , return_tensors="pt" ).to(snake_case_ ) # add boolean mask, indicating which patches to mask _UpperCAmelCase = hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" , filename="bool_masked_pos.pt" ) _UpperCAmelCase = torch.load(snake_case_ ) # forward pass with torch.no_grad(): _UpperCAmelCase = model(**snake_case_ ) # verify the logits _UpperCAmelCase = torch.Size([1, 1_4_0_8, 1_5_3_6] ) _UpperCAmelCase = torch.tensor( [[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] , device=snake_case_ ) self.assertEqual(outputs.logits.shape , snake_case_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , snake_case_ , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) _UpperCAmelCase = torch.tensor([0.5_1_4_2] , device=snake_case_ ) self.assertTrue(torch.allclose(outputs.loss , snake_case_ , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) _UpperCAmelCase = VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short" , norm_pix_loss=snake_case_ ).to( snake_case_ ) with torch.no_grad(): _UpperCAmelCase = model(**snake_case_ ) _UpperCAmelCase = torch.tensor(torch.tensor([0.6_4_6_9] ) , device=snake_case_ ) self.assertTrue(torch.allclose(outputs.loss , snake_case_ , atol=1e-4 ) )
156
0