code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging SCREAMING_SNAKE_CASE_: Any =logging.get_logger(__name__) SCREAMING_SNAKE_CASE_: List[Any] ={ 'facebook/blenderbot_small-90M': 'https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json', # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class __A ( __snake_case ): a__ : Tuple = "blenderbot-small" a__ : List[str] = ["past_key_values"] a__ : Any = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__(self : str , __a : List[Any]=50265 , __a : Any=512 , __a : Any=8 , __a : str=2048 , __a : Any=16 , __a : List[Any]=8 , __a : Union[str, Any]=2048 , __a : List[Any]=16 , __a : Tuple=0.0 , __a : Dict=0.0 , __a : Union[str, Any]=True , __a : Any=True , __a : Tuple="gelu" , __a : Any=512 , __a : Optional[int]=0.1 , __a : Any=0.0 , __a : Dict=0.0 , __a : List[Any]=0.02 , __a : str=1 , __a : str=False , __a : int=0 , __a : Any=1 , __a : int=2 , __a : Dict=2 , **__a : Optional[Any] , ): UpperCAmelCase_ = vocab_size UpperCAmelCase_ = max_position_embeddings UpperCAmelCase_ = d_model UpperCAmelCase_ = encoder_ffn_dim UpperCAmelCase_ = encoder_layers UpperCAmelCase_ = encoder_attention_heads UpperCAmelCase_ = decoder_ffn_dim UpperCAmelCase_ = decoder_layers UpperCAmelCase_ = decoder_attention_heads UpperCAmelCase_ = dropout UpperCAmelCase_ = attention_dropout UpperCAmelCase_ = activation_dropout UpperCAmelCase_ = activation_function UpperCAmelCase_ = init_std UpperCAmelCase_ = encoder_layerdrop UpperCAmelCase_ = decoder_layerdrop UpperCAmelCase_ = use_cache UpperCAmelCase_ = encoder_layers UpperCAmelCase_ = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , forced_eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) class __A ( __snake_case ): @property def _lowercase (self : int ): if self.task in ["default", "seq2seq-lm"]: UpperCAmelCase_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: UpperCAmelCase_ = {0: """batch"""} UpperCAmelCase_ = {0: """batch""", 1: """past_decoder_sequence + sequence"""} else: UpperCAmelCase_ = {0: """batch""", 1: """decoder_sequence"""} UpperCAmelCase_ = {0: """batch""", 1: """decoder_sequence"""} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. UpperCAmelCase_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: UpperCAmelCase_ = self.num_layers for i in range(lowerCamelCase_ ): UpperCAmelCase_ = {0: """batch""", 2: """past_sequence + sequence"""} UpperCAmelCase_ = {0: """batch""", 2: """past_sequence + sequence"""} else: UpperCAmelCase_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}), ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}), ] ) return common_inputs @property def _lowercase (self : Tuple ): if self.task in ["default", "seq2seq-lm"]: UpperCAmelCase_ = super().outputs else: UpperCAmelCase_ = super(lowerCamelCase_ , self ).outputs if self.use_past: UpperCAmelCase_ = self.num_layers for i in range(lowerCamelCase_ ): UpperCAmelCase_ = {0: """batch""", 2: """past_sequence + sequence"""} UpperCAmelCase_ = {0: """batch""", 2: """past_sequence + sequence"""} return common_outputs def _lowercase (self : List[Any] , __a : PreTrainedTokenizer , __a : int = -1 , __a : int = -1 , __a : bool = False , __a : Optional[TensorType] = None , ): UpperCAmelCase_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Generate decoder inputs UpperCAmelCase_ = seq_length if not self.use_past else 1 UpperCAmelCase_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) UpperCAmelCase_ = {f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()} UpperCAmelCase_ = dict(**lowerCamelCase_ , **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch UpperCAmelCase_ = common_inputs["""input_ids"""].shape UpperCAmelCase_ = common_inputs["""decoder_input_ids"""].shape[1] UpperCAmelCase_ = self.num_attention_heads UpperCAmelCase_ = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) UpperCAmelCase_ = decoder_seq_length + 3 UpperCAmelCase_ = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) UpperCAmelCase_ = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(lowerCamelCase_ , lowerCamelCase_ )] , dim=1 ) UpperCAmelCase_ = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered UpperCAmelCase_ = self.num_layers UpperCAmelCase_ = min(lowerCamelCase_ , lowerCamelCase_ ) UpperCAmelCase_ = max(lowerCamelCase_ , lowerCamelCase_ ) - min_num_layers UpperCAmelCase_ = """encoder""" if num_encoder_layers > num_decoder_layers else """decoder""" for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. UpperCAmelCase_ = encoder_shape if remaining_side_name == """encoder""" else decoder_shape for _ in range(lowerCamelCase_ , lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def _lowercase (self : Optional[int] , __a : PreTrainedTokenizer , __a : int = -1 , __a : int = -1 , __a : bool = False , __a : Optional[TensorType] = None , ): UpperCAmelCase_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch UpperCAmelCase_ = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values UpperCAmelCase_ = seqlen + 2 UpperCAmelCase_ = self.num_layers UpperCAmelCase_ = self.num_attention_heads UpperCAmelCase_ = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) UpperCAmelCase_ = common_inputs["""attention_mask"""].dtype UpperCAmelCase_ = torch.cat( [common_inputs["attention_mask"], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) UpperCAmelCase_ = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def _lowercase (self : str , __a : PreTrainedTokenizer , __a : int = -1 , __a : int = -1 , __a : bool = False , __a : Optional[TensorType] = None , ): # Copied from OnnxConfig.generate_dummy_inputs # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity. # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCAmelCase_ = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCAmelCase_ = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) UpperCAmelCase_ = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence UpperCAmelCase_ = [""" """.join([tokenizer.unk_token] ) * seq_length] * batch_size UpperCAmelCase_ = dict(tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return common_inputs def _lowercase (self : Optional[Any] , __a : PreTrainedTokenizer , __a : int = -1 , __a : int = -1 , __a : bool = False , __a : Optional[TensorType] = None , ): if self.task in ["default", "seq2seq-lm"]: UpperCAmelCase_ = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) elif self.task == "causal-lm": UpperCAmelCase_ = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) else: UpperCAmelCase_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) return common_inputs def _lowercase (self : Any , __a : List[Any] , __a : Optional[int] , __a : Any , __a : List[Any] ): if self.task in ["default", "seq2seq-lm"]: UpperCAmelCase_ = super()._flatten_past_key_values_(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: UpperCAmelCase_ = super(lowerCamelCase_ , self )._flatten_past_key_values_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ )
1
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 _snake_case : '''simple docstring''' def __init__( self: Tuple ,lowerCamelCase_: List[str] ,lowerCamelCase_: int=13 ,lowerCamelCase_: int=32 ,lowerCamelCase_: Optional[int]=2 ,lowerCamelCase_: Any=3 ,lowerCamelCase_: str=16 ,lowerCamelCase_: Optional[Any]=[1, 2, 1] ,lowerCamelCase_: Tuple=[2, 2, 4] ,lowerCamelCase_: int=2 ,lowerCamelCase_: List[Any]=2.0 ,lowerCamelCase_: str=True ,lowerCamelCase_: Optional[int]=0.0 ,lowerCamelCase_: List[Any]=0.0 ,lowerCamelCase_: List[str]=0.1 ,lowerCamelCase_: Tuple="gelu" ,lowerCamelCase_: Union[str, Any]=False ,lowerCamelCase_: Union[str, Any]=True ,lowerCamelCase_: Optional[int]=0.0_2 ,lowerCamelCase_: int=1e-5 ,lowerCamelCase_: Optional[int]=True ,lowerCamelCase_: Union[str, Any]=None ,lowerCamelCase_: Union[str, Any]=True ,lowerCamelCase_: Optional[int]=10 ,lowerCamelCase_: Tuple=8 ,) -> List[Any]: UpperCAmelCase_ : List[str] = parent UpperCAmelCase_ : int = batch_size UpperCAmelCase_ : int = image_size UpperCAmelCase_ : Union[str, Any] = patch_size UpperCAmelCase_ : Optional[Any] = num_channels UpperCAmelCase_ : int = embed_dim UpperCAmelCase_ : Union[str, Any] = depths UpperCAmelCase_ : List[str] = num_heads UpperCAmelCase_ : int = window_size UpperCAmelCase_ : List[str] = mlp_ratio UpperCAmelCase_ : Tuple = qkv_bias UpperCAmelCase_ : Tuple = hidden_dropout_prob UpperCAmelCase_ : str = attention_probs_dropout_prob UpperCAmelCase_ : Tuple = drop_path_rate UpperCAmelCase_ : List[str] = hidden_act UpperCAmelCase_ : int = use_absolute_embeddings UpperCAmelCase_ : Any = patch_norm UpperCAmelCase_ : Optional[int] = layer_norm_eps UpperCAmelCase_ : Tuple = initializer_range UpperCAmelCase_ : Optional[Any] = is_training UpperCAmelCase_ : Dict = scope UpperCAmelCase_ : int = use_labels UpperCAmelCase_ : Optional[Any] = type_sequence_label_size UpperCAmelCase_ : List[str] = encoder_stride def A__ ( self: Any ) -> int: UpperCAmelCase_ : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase_ : List[Any] = None if self.use_labels: UpperCAmelCase_ : Optional[int] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) UpperCAmelCase_ : str = self.get_config() return config, pixel_values, labels def A__ ( self: List[Any] ) -> 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 A__ ( self: Dict ,lowerCamelCase_: Tuple ,lowerCamelCase_: Union[str, Any] ,lowerCamelCase_: List[str] ) -> str: UpperCAmelCase_ : str = SwinvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() UpperCAmelCase_ : Optional[Any] = model(lowerCamelCase_ ) UpperCAmelCase_ : List[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) UpperCAmelCase_ : List[Any] = 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 A__ ( self: List[Any] ,lowerCamelCase_: List[Any] ,lowerCamelCase_: int ,lowerCamelCase_: int ) -> int: UpperCAmelCase_ : Any = SwinvaForMaskedImageModeling(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() UpperCAmelCase_ : Union[str, Any] = model(lowerCamelCase_ ) self.parent.assertEqual( result.logits.shape ,(self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images UpperCAmelCase_ : str = 1 UpperCAmelCase_ : Optional[Any] = SwinvaForMaskedImageModeling(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() UpperCAmelCase_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase_ : int = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, 1, self.image_size, self.image_size) ) def A__ ( self: int ,lowerCamelCase_: int ,lowerCamelCase_: List[Any] ,lowerCamelCase_: Optional[Any] ) -> int: UpperCAmelCase_ : Union[str, Any] = self.type_sequence_label_size UpperCAmelCase_ : int = SwinvaForImageClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() UpperCAmelCase_ : Optional[int] = model(lowerCamelCase_ ,labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def A__ ( self: str ) -> Union[str, Any]: UpperCAmelCase_ : Optional[Any] = self.prepare_config_and_inputs() UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = config_and_inputs UpperCAmelCase_ : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class _snake_case ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' A__ : Tuple = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) A__ : Optional[Any] = ( {"feature-extraction": SwinvaModel, "image-classification": SwinvaForImageClassification} if is_torch_available() else {} ) A__ : List[Any] = False A__ : Tuple = False A__ : int = False A__ : Union[str, Any] = False def A__ ( self: List[str] ) -> Optional[Any]: UpperCAmelCase_ : Any = SwinvaModelTester(self ) UpperCAmelCase_ : str = ConfigTester(self ,config_class=lowerCamelCase_ ,embed_dim=37 ) def A__ ( self: Optional[int] ) -> 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 A__ ( self: Any ) -> Dict: UpperCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0.""" ) def A__ ( self: int ) -> Dict: pass @unittest.skip(reason="""Swinv2 does not use inputs_embeds""" ) def A__ ( self: Tuple ) -> List[str]: pass def A__ ( self: str ) -> List[Any]: UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ : int = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) UpperCAmelCase_ : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_ ,nn.Linear ) ) def A__ ( self: Optional[Any] ) -> Optional[int]: UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_ : Dict = model_class(lowerCamelCase_ ) UpperCAmelCase_ : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase_ : int = [*signature.parameters.keys()] UpperCAmelCase_ : Tuple = ["""pixel_values"""] self.assertListEqual(arg_names[:1] ,lowerCamelCase_ ) def A__ ( self: Union[str, Any] ) -> Optional[Any]: UpperCAmelCase_ , UpperCAmelCase_ : int = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ : Any = True for model_class in self.all_model_classes: UpperCAmelCase_ : Optional[Any] = True UpperCAmelCase_ : Union[str, Any] = False UpperCAmelCase_ : str = True UpperCAmelCase_ : List[Any] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): UpperCAmelCase_ : Optional[int] = model(**self._prepare_for_class(lowerCamelCase_ ,lowerCamelCase_ ) ) UpperCAmelCase_ : Optional[Any] = outputs.attentions UpperCAmelCase_ : List[str] = len(self.model_tester.depths ) self.assertEqual(len(lowerCamelCase_ ) ,lowerCamelCase_ ) # check that output_attentions also work using config del inputs_dict["output_attentions"] UpperCAmelCase_ : str = True UpperCAmelCase_ : Optional[Any] = config.window_size**2 UpperCAmelCase_ : Optional[int] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**self._prepare_for_class(lowerCamelCase_ ,lowerCamelCase_ ) ) UpperCAmelCase_ : List[Any] = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) ,lowerCamelCase_ ) self.assertListEqual( list(attentions[0].shape[-3:] ) ,[self.model_tester.num_heads[0], window_size_squared, window_size_squared] ,) UpperCAmelCase_ : Optional[Any] = len(lowerCamelCase_ ) # Check attention is always last and order is fine UpperCAmelCase_ : Tuple = True UpperCAmelCase_ : List[Any] = True UpperCAmelCase_ : Tuple = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): UpperCAmelCase_ : Union[str, Any] = model(**self._prepare_for_class(lowerCamelCase_ ,lowerCamelCase_ ) ) if hasattr(self.model_tester ,"""num_hidden_states_types""" ): UpperCAmelCase_ : List[Any] = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states UpperCAmelCase_ : List[str] = 2 self.assertEqual(out_len + added_hidden_states ,len(lowerCamelCase_ ) ) UpperCAmelCase_ : Any = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) ,lowerCamelCase_ ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) ,[self.model_tester.num_heads[0], window_size_squared, window_size_squared] ,) def A__ ( self: List[str] ,lowerCamelCase_: Dict ,lowerCamelCase_: Tuple ,lowerCamelCase_: Optional[Any] ,lowerCamelCase_: Optional[int] ) -> List[Any]: UpperCAmelCase_ : str = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): UpperCAmelCase_ : int = model(**self._prepare_for_class(lowerCamelCase_ ,lowerCamelCase_ ) ) UpperCAmelCase_ : List[str] = outputs.hidden_states UpperCAmelCase_ : Optional[Any] = getattr( self.model_tester ,"""expected_num_hidden_layers""" ,len(self.model_tester.depths ) + 1 ) self.assertEqual(len(lowerCamelCase_ ) ,lowerCamelCase_ ) # Swinv2 has a different seq_length UpperCAmelCase_ : Optional[Any] = ( config.patch_size if isinstance(config.patch_size ,collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) UpperCAmelCase_ : 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] ,) UpperCAmelCase_ : Optional[int] = outputs.reshaped_hidden_states self.assertEqual(len(lowerCamelCase_ ) ,lowerCamelCase_ ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = reshaped_hidden_states[0].shape UpperCAmelCase_ : Optional[Any] = ( reshaped_hidden_states[0].view(lowerCamelCase_ ,lowerCamelCase_ ,height * width ).permute(0 ,2 ,1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) ,[num_patches, self.model_tester.embed_dim] ,) def A__ ( self: Any ) -> int: UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ : Dict = ( self.model_tester.image_size if isinstance(self.model_tester.image_size ,collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: UpperCAmelCase_ : Any = True self.check_hidden_states_output(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase_ : str = True self.check_hidden_states_output(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ) def A__ ( self: List[str] ) -> Dict: UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ : Union[str, Any] = 3 UpperCAmelCase_ : Optional[int] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size ,collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) UpperCAmelCase_ : List[str] = ( config.patch_size if isinstance(config.patch_size ,collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) UpperCAmelCase_ : List[Any] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) UpperCAmelCase_ : Optional[Any] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: UpperCAmelCase_ : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,(padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase_ : List[str] = True self.check_hidden_states_output(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,(padded_height, padded_width) ) def A__ ( self: Optional[int] ) -> str: UpperCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def A__ ( self: Union[str, Any] ) -> Dict: UpperCAmelCase_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def A__ ( self: str ) -> Tuple: for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_ : Dict = SwinvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def A__ ( self: Any ) -> int: UpperCAmelCase_ , UpperCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase_ : List[str] = _config_zero_init(lowerCamelCase_ ) for model_class in self.all_model_classes: UpperCAmelCase_ : int = model_class(config=lowerCamelCase_ ) 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 _snake_case ( unittest.TestCase ): '''simple docstring''' @cached_property def A__ ( self: Dict ) -> Optional[Any]: return ( AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ) if is_vision_available() else None ) @slow def A__ ( self: str ) -> List[Any]: UpperCAmelCase_ : Tuple = SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ).to( lowerCamelCase_ ) UpperCAmelCase_ : Any = self.default_image_processor UpperCAmelCase_ : List[str] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) UpperCAmelCase_ : Optional[int] = image_processor(images=lowerCamelCase_ ,return_tensors="""pt""" ).to(lowerCamelCase_ ) # forward pass with torch.no_grad(): UpperCAmelCase_ : Optional[Any] = model(**lowerCamelCase_ ) # verify the logits UpperCAmelCase_ : Dict = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape ,lowerCamelCase_ ) UpperCAmelCase_ : Any = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,lowerCamelCase_ ,atol=1e-4 ) )
345
0
'''simple docstring''' import math def _A (lowerCAmelCase__ :int ) -> bool: '''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(lowerCAmelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _A (lowerCAmelCase__ :float = 0.1 ) -> int: '''simple docstring''' _a = 3 _a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowerCAmelCase__ ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
365
'''simple docstring''' import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def _A () -> Optional[Any]: '''simple docstring''' with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(lowerCAmelCase__ ): requests.request('GET' , 'https://huggingface.co' ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request('GET' , 'https://huggingface.co' , timeout=1.0 ) @pytest.mark.integration def _A () -> Any: '''simple docstring''' with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request('GET' , 'https://huggingface.co' ) def _A () -> Dict: '''simple docstring''' with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(lowerCAmelCase__ ): http_head('https://huggingface.co' )
104
0
from collections.abc import Sequence from queue import Queue class A_ : def __init__(self :Dict , _UpperCamelCase :Dict , _UpperCamelCase :int , _UpperCamelCase :Tuple , _UpperCamelCase :int=None , _UpperCamelCase :Optional[Any]=None )-> Dict: __A = start __A = end __A = val __A = (start + end) // 2 __A = left __A = right def __repr__(self :Any )-> Any: return f"""SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})""" class A_ : def __init__(self :str , _UpperCamelCase :Sequence , _UpperCamelCase :Optional[int] )-> Tuple: __A = collection __A = function if self.collection: __A = self._build_tree(0 , len(_UpperCamelCase ) - 1 ) def _lowerCAmelCase (self :List[Any] , _UpperCamelCase :List[Any] , _UpperCamelCase :Dict )-> List[Any]: self._update_tree(self.root , _UpperCamelCase , _UpperCamelCase ) def _lowerCAmelCase (self :Optional[int] , _UpperCamelCase :List[str] , _UpperCamelCase :int )-> List[str]: return self._query_range(self.root , _UpperCamelCase , _UpperCamelCase ) def _lowerCAmelCase (self :Optional[int] , _UpperCamelCase :str , _UpperCamelCase :Tuple )-> Optional[int]: if start == end: return SegmentTreeNode(_UpperCamelCase , _UpperCamelCase , self.collection[start] ) __A = (start + end) // 2 __A = self._build_tree(_UpperCamelCase , _UpperCamelCase ) __A = self._build_tree(mid + 1 , _UpperCamelCase ) return SegmentTreeNode(_UpperCamelCase , _UpperCamelCase , self.fn(left.val , right.val ) , _UpperCamelCase , _UpperCamelCase ) def _lowerCAmelCase (self :Any , _UpperCamelCase :Optional[Any] , _UpperCamelCase :Optional[int] , _UpperCamelCase :List[Any] )-> List[Any]: if node.start == i and node.end == i: __A = val return if i <= node.mid: self._update_tree(node.left , _UpperCamelCase , _UpperCamelCase ) else: self._update_tree(node.right , _UpperCamelCase , _UpperCamelCase ) __A = self.fn(node.left.val , node.right.val ) def _lowerCAmelCase (self :Union[str, Any] , _UpperCamelCase :List[str] , _UpperCamelCase :int , _UpperCamelCase :Any )-> Optional[Any]: if node.start == i and node.end == j: return node.val if i <= node.mid: if j <= node.mid: # range in left child tree return self._query_range(node.left , _UpperCamelCase , _UpperCamelCase ) else: # range in left child tree and right child tree return self.fn( self._query_range(node.left , _UpperCamelCase , node.mid ) , self._query_range(node.right , node.mid + 1 , _UpperCamelCase ) , ) else: # range in right child tree return self._query_range(node.right , _UpperCamelCase , _UpperCamelCase ) def _lowerCAmelCase (self :Dict )-> Any: if self.root is not None: __A = Queue() queue.put(self.root ) while not queue.empty(): __A = queue.get() yield node if node.left is not None: queue.put(node.left ) if node.right is not None: queue.put(node.right ) if __name__ == "__main__": import operator for fn in [operator.add, max, min]: print('*' * 50) snake_case__ : Tuple = SegmentTree([2, 1, 5, 3, 4], fn) for node in arr.traverse(): print(node) print() arr.update(1, 5) for node in arr.traverse(): print(node) print() print(arr.query_range(3, 4)) # 7 print(arr.query_range(2, 2)) # 5 print(arr.query_range(1, 3)) # 13 print()
117
import argparse import fairseq import torch from transformers import UniSpeechSatConfig, UniSpeechSatForCTC, UniSpeechSatForPreTraining, logging logging.set_verbosity_info() snake_case__ : Dict = logging.get_logger(__name__) snake_case__ : Optional[Any] = { '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', 'encoder.layer_norm_for_extract': 'layer_norm_for_extract', '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', 'label_embs_concat': 'label_embeddings_concat', 'mask_emb': 'masked_spec_embed', 'spk_proj': 'speaker_proj', } snake_case__ : Optional[int] = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'label_embeddings_concat', 'speaker_proj', 'layer_norm_for_extract', ] def _a ( lowerCamelCase: List[Any] , lowerCamelCase: Any , lowerCamelCase: Union[str, Any] , lowerCamelCase: Any , lowerCamelCase: int ) -> List[str]: '''simple docstring''' for attribute in key.split('''.''' ): __A = getattr(lowerCamelCase , lowerCamelCase ) if weight_type is not None: __A = getattr(lowerCamelCase , lowerCamelCase ).shape else: __A = 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": __A = value elif weight_type == "weight_g": __A = value elif weight_type == "weight_v": __A = value elif weight_type == "bias": __A = value else: __A = value logger.info(F"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.""" ) def _a ( lowerCamelCase: List[str] , lowerCamelCase: Optional[int] ) -> Tuple: '''simple docstring''' __A = [] __A = fairseq_model.state_dict() __A = hf_model.unispeech_sat.feature_extractor for name, value in fairseq_dict.items(): __A = False if "conv_layers" in name: load_conv_layer( lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , hf_model.config.feat_extract_norm == '''group''' , ) __A = True else: for key, mapped_key in MAPPING.items(): __A = '''unispeech_sat.''' + 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]: if "layer_norm_for_extract" in name and (".".join(name.split('''.''' )[:-1] ) != key): # special case since naming is very similar continue __A = True if "*" in mapped_key: __A = name.split(lowerCamelCase )[0].split('''.''' )[-2] __A = mapped_key.replace('''*''' , lowerCamelCase ) if "weight_g" in name: __A = '''weight_g''' elif "weight_v" in name: __A = '''weight_v''' elif "bias" in name: __A = '''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj __A = '''weight''' else: __A = None set_recursively(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase ) continue if not is_used: unused_weights.append(lowerCamelCase ) logger.warning(F"""Unused weights: {unused_weights}""" ) def _a ( lowerCamelCase: int , lowerCamelCase: Any , lowerCamelCase: int , lowerCamelCase: int , lowerCamelCase: List[str] ) -> Union[str, Any]: '''simple docstring''' __A = full_name.split('''conv_layers.''' )[-1] __A = name.split('''.''' ) __A = int(items[0] ) __A = 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.""" ) __A = 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.""" ) __A = 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[layer_id].layer_norm.bias.data.shape} was found.""" ) __A = 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[layer_id].layer_norm.weight.data.shape} was found.""" ) __A = 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: Tuple , lowerCamelCase: int , lowerCamelCase: Optional[Any]=None , lowerCamelCase: Optional[Any]=None , lowerCamelCase: Optional[int]=True ) -> List[Any]: '''simple docstring''' if config_path is not None: __A = UniSpeechSatConfig.from_pretrained(lowerCamelCase ) else: __A = UniSpeechSatConfig() __A = '''''' if is_finetuned: __A = UniSpeechSatForCTC(lowerCamelCase ) else: __A = UniSpeechSatForPreTraining(lowerCamelCase ) __A , __A , __A = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) __A = model[0].eval() recursively_load_weights(lowerCamelCase , lowerCamelCase ) hf_wavavec.save_pretrained(lowerCamelCase ) if __name__ == "__main__": snake_case__ : Tuple = 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' ) snake_case__ : Any = parser.parse_args() convert_unispeech_sat_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
117
1
from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging __a :Optional[int] = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Optional[Any] ): """simple docstring""" try: with open(__UpperCamelCase ,"rb" ) as flax_state_f: A_ = from_bytes(__UpperCamelCase ,flax_state_f.read() ) except UnpicklingError as e: try: with open(__UpperCamelCase ) as f: if f.read().startswith("version" ): raise OSError( "You seem to have cloned a repository without having git-lfs installed. Please" " install git-lfs and run `git lfs install` followed by `git lfs pull` in the" " folder you cloned." ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(f'''Unable to convert {model_file} to Flax deserializable object. ''' ) return load_flax_weights_in_pytorch_model(__UpperCamelCase ,__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Optional[int] ,__UpperCamelCase : Tuple ): """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( "Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise # check if we have bf16 weights A_ = flatten_dict(jax.tree_util.tree_map(lambda __UpperCamelCase : x.dtype == jnp.bfloataa ,__UpperCamelCase ) ).values() if any(__UpperCamelCase ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( "Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` " "before loading those in PyTorch model." ) A_ = jax.tree_util.tree_map( lambda __UpperCamelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params ,__UpperCamelCase ) A_ = "" A_ = flatten_dict(__UpperCamelCase ,sep="." ) A_ = pt_model.state_dict() # keep track of unexpected & missing keys A_ = [] A_ = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): A_ = flax_key_tuple.split("." ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: A_ = flax_key_tuple_array[:-1] + ["weight"] A_ = jnp.transpose(__UpperCamelCase ,(3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": A_ = flax_key_tuple_array[:-1] + ["weight"] A_ = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": A_ = flax_key_tuple_array[:-1] + ["weight"] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(__UpperCamelCase ): A_ = ( flax_key_tuple_string.replace("_0" ,".0" ) .replace("_1" ,".1" ) .replace("_2" ,".2" ) .replace("_3" ,".3" ) .replace("_4" ,".4" ) .replace("_5" ,".5" ) .replace("_6" ,".6" ) .replace("_7" ,".7" ) .replace("_8" ,".8" ) .replace("_9" ,".9" ) ) A_ = ".".join(__UpperCamelCase ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( f'''Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ''' f'''to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.''' ) else: # add weight to pytorch dict A_ = np.asarray(__UpperCamelCase ) if not isinstance(__UpperCamelCase ,np.ndarray ) else flax_tensor A_ = torch.from_numpy(__UpperCamelCase ) # remove from missing keys missing_keys.remove(__UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(__UpperCamelCase ) pt_model.load_state_dict(__UpperCamelCase ) # re-transform missing_keys to list A_ = list(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: logger.warning( "Some weights of the Flax model were not used when initializing the PyTorch model" f''' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing''' f''' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture''' " (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This" f''' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect''' " to be exactly identical (e.g. initializing a BertForSequenceClassification model from a" " FlaxBertForSequenceClassification model)." ) if len(__UpperCamelCase ) > 0: logger.warning( f'''Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly''' f''' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to''' " use it for predictions and inference." ) return pt_model
329
import argparse import torch from huggingface_hub import hf_hub_download from transformers import AutoTokenizer, RobertaPreLayerNormConfig, RobertaPreLayerNormForMaskedLM from transformers.utils import logging logging.set_verbosity_info() __a :str = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : str ): """simple docstring""" A_ = RobertaPreLayerNormConfig.from_pretrained( __UpperCamelCase ,architectures=["RobertaPreLayerNormForMaskedLM"] ) # convert state_dict A_ = torch.load(hf_hub_download(repo_id=__UpperCamelCase ,filename="pytorch_model.bin" ) ) A_ = {} for tensor_key, tensor_value in original_state_dict.items(): # The transformer implementation gives the model a unique name, rather than overwiriting 'roberta' if tensor_key.startswith("roberta." ): A_ = "roberta_prelayernorm." + tensor_key[len("roberta." ) :] # The original implementation contains weights which are not used, remove them from the state_dict if tensor_key.endswith(".self.LayerNorm.weight" ) or tensor_key.endswith(".self.LayerNorm.bias" ): continue A_ = tensor_value A_ = RobertaPreLayerNormForMaskedLM.from_pretrained( pretrained_model_name_or_path=__UpperCamelCase ,config=__UpperCamelCase ,state_dict=__UpperCamelCase ) model.save_pretrained(__UpperCamelCase ) # convert tokenizer A_ = AutoTokenizer.from_pretrained(__UpperCamelCase ) tokenizer.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": __a :Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint-repo', default=None, type=str, required=True, help='Path the official PyTorch dump, e.g. \'andreasmadsen/efficient_mlm_m0.40\'.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __a :Any = parser.parse_args() convert_roberta_prelayernorm_checkpoint_to_pytorch(args.checkpoint_repo, args.pytorch_dump_folder_path)
329
1
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase : List[str] = {"configuration_van": ["VAN_PRETRAINED_CONFIG_ARCHIVE_MAP", "VanConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : List[str] = [ "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 UpperCAmelCase : str = _LazyModule(__name__, globals()["__file__"], _import_structure)
136
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase : List[str] = logging.get_logger(__name__) UpperCAmelCase : Union[str, Any] = { "EleutherAI/gpt-neox-20b": "https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json", # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox } class SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ): lowercase__ = "gpt_neox" def __init__( self : Union[str, Any] , lowerCAmelCase_ : str=5_0_4_3_2 , lowerCAmelCase_ : List[Any]=6_1_4_4 , lowerCAmelCase_ : str=4_4 , lowerCAmelCase_ : Tuple=6_4 , lowerCAmelCase_ : Optional[int]=2_4_5_7_6 , lowerCAmelCase_ : List[Any]="gelu" , lowerCAmelCase_ : Any=0.25 , lowerCAmelCase_ : int=1_0_0_0_0 , lowerCAmelCase_ : str=0.0 , lowerCAmelCase_ : Dict=0.0 , lowerCAmelCase_ : Optional[Any]=0.1 , lowerCAmelCase_ : Union[str, Any]=2_0_4_8 , lowerCAmelCase_ : Optional[int]=0.02 , lowerCAmelCase_ : List[Any]=1E-5 , lowerCAmelCase_ : Optional[Any]=True , lowerCAmelCase_ : List[Any]=0 , lowerCAmelCase_ : Optional[Any]=2 , lowerCAmelCase_ : int=False , lowerCAmelCase_ : Tuple=True , lowerCAmelCase_ : int=None , **lowerCAmelCase_ : str , ): """simple docstring""" super().__init__(bos_token_id=lowerCAmelCase_ , eos_token_id=lowerCAmelCase_ , **lowerCAmelCase_) lowercase_ = vocab_size lowercase_ = max_position_embeddings lowercase_ = hidden_size lowercase_ = num_hidden_layers lowercase_ = num_attention_heads lowercase_ = intermediate_size lowercase_ = hidden_act lowercase_ = rotary_pct lowercase_ = rotary_emb_base lowercase_ = attention_dropout lowercase_ = hidden_dropout lowercase_ = classifier_dropout lowercase_ = initializer_range lowercase_ = layer_norm_eps lowercase_ = use_cache lowercase_ = tie_word_embeddings lowercase_ = use_parallel_residual lowercase_ = rope_scaling self._rope_scaling_validation() if self.hidden_size % self.num_attention_heads != 0: raise ValueError( """The hidden size is not divisble by the number of attention heads! Make sure to update them!""") def _UpperCAmelCase ( self : Optional[Any]): """simple docstring""" if self.rope_scaling is None: return if not isinstance(self.rope_scaling , lowerCAmelCase_) or len(self.rope_scaling) != 2: raise ValueError( """`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, """ F'''got {self.rope_scaling}''') lowercase_ = self.rope_scaling.get("""type""" , lowerCAmelCase_) lowercase_ = self.rope_scaling.get("""factor""" , lowerCAmelCase_) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( F'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''') if rope_scaling_factor is None or not isinstance(lowerCAmelCase_ , lowerCAmelCase_) or rope_scaling_factor <= 1.0: raise ValueError(F'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''')
136
1
'''simple docstring''' from collections.abc import Iterable from typing import Any class _SCREAMING_SNAKE_CASE : def __init__( self : Optional[int] , a__ : List[Any] = None ): __magic_name__ = value __magic_name__ = None # Added in order to delete a node easier __magic_name__ = None __magic_name__ = None def __repr__( self : Optional[Any] ): from pprint import pformat if self.left is None and self.right is None: return str(self.value ) return pformat({F'''{self.value}''': (self.left, self.right)} , indent=1 ) class _SCREAMING_SNAKE_CASE : def __init__( self : Any , a__ : List[str] = None ): __magic_name__ = root def __str__( self : List[str] ): return str(self.root ) def snake_case__ ( self : Optional[int] , a__ : Optional[Any] , a__ : Any ): if new_children is not None: # reset its kids __magic_name__ = node.parent if node.parent is not None: # reset its parent if self.is_right(lowercase_ ): # If it is the right children __magic_name__ = new_children else: __magic_name__ = new_children else: __magic_name__ = new_children def snake_case__ ( self : Tuple , a__ : List[Any] ): if node.parent and node.parent.right: return node == node.parent.right return False def snake_case__ ( self : Optional[Any] ): return self.root is None def snake_case__ ( self : Optional[int] , a__ : Union[str, Any] ): __magic_name__ = Node(lowercase_ ) # create a new Node if self.empty(): # if Tree is empty __magic_name__ = new_node # set its root else: # Tree is not empty __magic_name__ = self.root # from root if parent_node is None: return while True: # While we don't get to a leaf if value < parent_node.value: # We go left if parent_node.left is None: __magic_name__ = new_node # We insert the new node in a leaf break else: __magic_name__ = parent_node.left else: if parent_node.right is None: __magic_name__ = new_node break else: __magic_name__ = parent_node.right __magic_name__ = parent_node def snake_case__ ( self : List[str] , *a__ : Union[str, Any] ): for value in values: self.__insert(lowercase_ ) def snake_case__ ( self : List[str] , a__ : Union[str, Any] ): if self.empty(): raise IndexError('''Warning: Tree is empty! please use another.''' ) else: __magic_name__ = self.root # use lazy evaluation here to avoid NoneType Attribute error while node is not None and node.value is not value: __magic_name__ = node.left if value < node.value else node.right return node def snake_case__ ( self : List[Any] , a__ : Union[str, Any] = None ): if node is None: if self.root is None: return None __magic_name__ = self.root if not self.empty(): while node.right is not None: __magic_name__ = node.right return node def snake_case__ ( self : List[Any] , a__ : List[Any] = None ): if node is None: __magic_name__ = self.root if self.root is None: return None if not self.empty(): __magic_name__ = self.root while node.left is not None: __magic_name__ = node.left return node def snake_case__ ( self : List[str] , a__ : str ): __magic_name__ = self.search(lowercase_ ) # Look for the node with that label if node is not None: if node.left is None and node.right is None: # If it has no children self.__reassign_nodes(lowercase_ , lowercase_ ) elif node.left is None: # Has only right children self.__reassign_nodes(lowercase_ , node.right ) elif node.right is None: # Has only left children self.__reassign_nodes(lowercase_ , node.left ) else: __magic_name__ = self.get_max( node.left ) # Gets the max value of the left branch self.remove(tmp_node.value ) # type: ignore __magic_name__ = ( tmp_node.value # type: ignore ) # Assigns the value to the node to delete and keep tree structure def snake_case__ ( self : List[str] , a__ : Optional[Any] ): if node is not None: yield node # Preorder Traversal yield from self.preorder_traverse(node.left ) yield from self.preorder_traverse(node.right ) def snake_case__ ( self : Tuple , a__ : Tuple=None ): if traversal_function is None: return self.preorder_traverse(self.root ) else: return traversal_function(self.root ) def snake_case__ ( self : Dict , a__ : List[Any] , a__ : Dict ): if node: self.inorder(lowercase_ , node.left ) arr.append(node.value ) self.inorder(lowercase_ , node.right ) def snake_case__ ( self : List[str] , a__ : Optional[int] , a__ : int ): __magic_name__ = [] self.inorder(lowercase_ , lowercase_ ) # append all values to list using inorder traversal return arr[k - 1] def UpperCamelCase ( a ) -> list[Node]: '''simple docstring''' __magic_name__ = [] if curr_node is not None: __magic_name__ = postorder(curr_node.left ) + postorder(curr_node.right ) + [curr_node] return node_list def UpperCamelCase ( ) -> None: '''simple docstring''' __magic_name__ = (8, 3, 6, 1, 10, 14, 13, 4, 7) __magic_name__ = BinarySearchTree() for i in testlist: t.insert(a_ ) # Prints all the elements of the list in order traversal print(a_ ) if t.search(6 ) is not None: print('''The value 6 exists''' ) else: print('''The value 6 doesn\'t exist''' ) if t.search(-1 ) is not None: print('''The value -1 exists''' ) else: print('''The value -1 doesn\'t exist''' ) if not t.empty(): print('''Max Value: ''' , t.get_max().value ) # type: ignore print('''Min Value: ''' , t.get_min().value ) # type: ignore for i in testlist: t.remove(a_ ) print(a_ ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
364
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCAmelCase = { "configuration_clap": [ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapAudioConfig", "ClapConfig", "ClapTextConfig", ], "processing_clap": ["ClapProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = [ "CLAP_PRETRAINED_MODEL_ARCHIVE_LIST", "ClapModel", "ClapPreTrainedModel", "ClapTextModel", "ClapTextModelWithProjection", "ClapAudioModel", "ClapAudioModelWithProjection", ] _lowerCAmelCase = ["ClapFeatureExtractor"] if TYPE_CHECKING: from .configuration_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioConfig, ClapConfig, ClapTextConfig, ) from .processing_clap import ClapProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clap import ClapFeatureExtractor from .modeling_clap import ( CLAP_PRETRAINED_MODEL_ARCHIVE_LIST, ClapAudioModel, ClapAudioModelWithProjection, ClapModel, ClapPreTrainedModel, ClapTextModel, ClapTextModelWithProjection, ) else: import sys _lowerCAmelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
98
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase__ : Dict = { '''configuration_convbert''': ['''CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ConvBertConfig''', '''ConvBertOnnxConfig'''], '''tokenization_convbert''': ['''ConvBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Optional[Any] = ['''ConvBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : List[Any] = [ '''CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ConvBertForMaskedLM''', '''ConvBertForMultipleChoice''', '''ConvBertForQuestionAnswering''', '''ConvBertForSequenceClassification''', '''ConvBertForTokenClassification''', '''ConvBertLayer''', '''ConvBertModel''', '''ConvBertPreTrainedModel''', '''load_tf_weights_in_convbert''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Tuple = [ '''TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFConvBertForMaskedLM''', '''TFConvBertForMultipleChoice''', '''TFConvBertForQuestionAnswering''', '''TFConvBertForSequenceClassification''', '''TFConvBertForTokenClassification''', '''TFConvBertLayer''', '''TFConvBertModel''', '''TFConvBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys lowercase__ : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
'''simple docstring''' import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __lowerCAmelCase = None __lowerCAmelCase = '''<''' if sys.byteorder == '''little''' else '''>''' # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __lowerCAmelCase = [ np.dtype('''|b1'''), np.dtype('''|u1'''), np.dtype('''<u2'''), np.dtype('''>u2'''), np.dtype('''<i2'''), np.dtype('''>i2'''), np.dtype('''<u4'''), np.dtype('''>u4'''), np.dtype('''<i4'''), np.dtype('''>i4'''), np.dtype('''<f4'''), np.dtype('''>f4'''), np.dtype('''<f8'''), np.dtype('''>f8'''), ] @dataclass class __magic_name__ : lowerCAmelCase : bool = True lowerCAmelCase : Optional[str] = None # Automatically constructed lowerCAmelCase : ClassVar[str] = "PIL.Image.Image" lowerCAmelCase : ClassVar[Any] = pa.struct({'bytes': pa.binary(), 'path': pa.string()} ) lowerCAmelCase : str = field(default='Image' , init=_UpperCamelCase , repr=_UpperCamelCase ) def __call__( self : Union[str, Any] ): return self.pa_type def __lowercase ( self : Any ,_UpperCAmelCase : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) if isinstance(_UpperCAmelCase ,_UpperCAmelCase ): _a : Optional[Any] = np.array(_UpperCAmelCase ) if isinstance(_UpperCAmelCase ,_UpperCAmelCase ): return {"path": value, "bytes": None} elif isinstance(_UpperCAmelCase ,_UpperCAmelCase ): return {"path": None, "bytes": value} elif isinstance(_UpperCAmelCase ,np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(_UpperCAmelCase ) elif isinstance(_UpperCAmelCase ,PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(_UpperCAmelCase ) elif value.get('path' ) is not None and os.path.isfile(value['path'] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get('path' )} elif value.get('bytes' ) is not None or value.get('path' ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get('bytes' ), "path": value.get('path' )} else: raise ValueError( F"""An image sample should have one of 'path' or 'bytes' but they are missing or None in {value}.""" ) def __lowercase ( self : Optional[Any] ,_UpperCAmelCase : dict ,_UpperCAmelCase : Optional[int]=None ): if not self.decode: raise RuntimeError('Decoding is disabled for this feature. Please use Image(decode=True) instead.' ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support decoding images, please install \'Pillow\'.' ) if token_per_repo_id is None: _a : Dict = {} _a , _a : str = value['path'], value['bytes'] if bytes_ is None: if path is None: raise ValueError(F"""An image should have one of 'path' or 'bytes' but both are None in {value}.""" ) else: if is_local_path(_UpperCAmelCase ): _a : Any = PIL.Image.open(_UpperCAmelCase ) else: _a : List[Any] = path.split('::' )[-1] try: _a : str = string_to_dict(_UpperCAmelCase ,config.HUB_DATASETS_URL )['repo_id'] _a : Optional[Any] = token_per_repo_id.get(_UpperCAmelCase ) except ValueError: _a : int = None with xopen(_UpperCAmelCase ,'rb' ,use_auth_token=_UpperCAmelCase ) as f: _a : Tuple = BytesIO(f.read() ) _a : Union[str, Any] = PIL.Image.open(bytes_ ) else: _a : Optional[int] = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def __lowercase ( self : int ): from .features import Value return ( self if self.decode else { "bytes": Value('binary' ), "path": Value('string' ), } ) def __lowercase ( self : str ,_UpperCAmelCase : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): if pa.types.is_string(storage.type ): _a : Union[str, Any] = pa.array([None] * len(_UpperCAmelCase ) ,type=pa.binary() ) _a : Union[str, Any] = pa.StructArray.from_arrays([bytes_array, storage] ,['bytes', 'path'] ,mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): _a : List[str] = pa.array([None] * len(_UpperCAmelCase ) ,type=pa.string() ) _a : Any = pa.StructArray.from_arrays([storage, path_array] ,['bytes', 'path'] ,mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index('bytes' ) >= 0: _a : Union[str, Any] = storage.field('bytes' ) else: _a : Tuple = pa.array([None] * len(_UpperCAmelCase ) ,type=pa.binary() ) if storage.type.get_field_index('path' ) >= 0: _a : Union[str, Any] = storage.field('path' ) else: _a : Dict = pa.array([None] * len(_UpperCAmelCase ) ,type=pa.string() ) _a : Optional[Any] = pa.StructArray.from_arrays([bytes_array, path_array] ,['bytes', 'path'] ,mask=storage.is_null() ) elif pa.types.is_list(storage.type ): _a : List[str] = pa.array( [encode_np_array(np.array(_UpperCAmelCase ) )['bytes'] if arr is not None else None for arr in storage.to_pylist()] ,type=pa.binary() ,) _a : int = pa.array([None] * len(_UpperCAmelCase ) ,type=pa.string() ) _a : Optional[Any] = pa.StructArray.from_arrays( [bytes_array, path_array] ,['bytes', 'path'] ,mask=bytes_array.is_null() ) return array_cast(_UpperCAmelCase ,self.pa_type ) def __lowercase ( self : Dict ,_UpperCAmelCase : pa.StructArray ): @no_op_if_value_is_null def path_to_bytes(_UpperCAmelCase : Tuple ): with xopen(_UpperCAmelCase ,'rb' ) as f: _a : int = f.read() return bytes_ _a : Any = pa.array( [ (path_to_bytes(x['path'] ) if x['bytes'] is None else x['bytes']) if x is not None else None for x in storage.to_pylist() ] ,type=pa.binary() ,) _a : Optional[Any] = pa.array( [os.path.basename(_UpperCAmelCase ) if path is not None else None for path in storage.field('path' ).to_pylist()] ,type=pa.string() ,) _a : Dict = pa.StructArray.from_arrays([bytes_array, path_array] ,['bytes', 'path'] ,mask=bytes_array.is_null() ) return array_cast(_UpperCAmelCase ,self.pa_type ) def __lowerCamelCase ( ) -> List[str]: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() _a : Dict = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def __lowerCamelCase ( lowerCAmelCase_ ) -> bytes: _a : Optional[int] = BytesIO() if image.format in list_image_compression_formats(): _a : Optional[Any] = image.format else: _a : str = 'PNG' if image.mode in ['1', 'L', 'LA', 'RGB', 'RGBA'] else 'TIFF' image.save(lowerCAmelCase_ , format=lowerCAmelCase_ ) return buffer.getvalue() def __lowerCamelCase ( lowerCAmelCase_ ) -> dict: if hasattr(lowerCAmelCase_ , 'filename' ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(lowerCAmelCase_ )} def __lowerCamelCase ( lowerCAmelCase_ ) -> dict: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) _a : List[Any] = array.dtype _a : Optional[int] = dtype.byteorder if dtype.byteorder != '=' else _NATIVE_BYTEORDER _a : Union[str, Any] = dtype.kind _a : Union[str, Any] = dtype.itemsize _a : List[Any] = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: _a : Optional[int] = np.dtype('|u1' ) if dtype_kind not in ["u", "i"]: raise TypeError( f"""Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.""" ) if dtype is not dest_dtype: warnings.warn(f"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'""" ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: _a : Union[str, Any] = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: _a : str = dtype_byteorder + dtype_kind + str(lowerCAmelCase_ ) _a : List[Any] = np.dtype(lowerCAmelCase_ ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(f"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'""" ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( f"""Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}""" ) _a : Union[str, Any] = PIL.Image.fromarray(array.astype(lowerCAmelCase_ ) ) return {"path": None, "bytes": image_to_bytes(lowerCAmelCase_ )} def __lowerCamelCase ( lowerCAmelCase_ ) -> List[dict]: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) if objs: _a , _a : Optional[Any] = first_non_null_value(lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(lowerCAmelCase_ , np.ndarray ): _a : List[str] = no_op_if_value_is_null(lowerCAmelCase_ ) return [obj_to_image_dict_func(lowerCAmelCase_ ) for obj in objs] elif isinstance(lowerCAmelCase_ , PIL.Image.Image ): _a : List[str] = no_op_if_value_is_null(lowerCAmelCase_ ) return [obj_to_image_dict_func(lowerCAmelCase_ ) for obj in objs] else: return objs else: return objs
89
0
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_OBJECT_DETECTION_MAPPING, AutoFeatureExtractor, AutoModelForObjectDetection, ObjectDetectionPipeline, is_vision_available, pipeline, ) from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_pytesseract, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __UpperCAmelCase : '''simple docstring''' @staticmethod def __A ( *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: pass @is_pipeline_test @require_vision @require_timm @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Optional[Any] = MODEL_FOR_OBJECT_DETECTION_MAPPING def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: A_ = ObjectDetectionPipeline(model=lowercase_ , image_processor=lowercase_ ) return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = object_detector('''./tests/fixtures/tests_samples/COCO/000000039769.png''' , threshold=0.0 ) self.assertGreater(len(lowercase_ ) , 0 ) for detected_object in outputs: self.assertEqual( lowercase_ , { '''score''': ANY(lowercase_ ), '''label''': ANY(lowercase_ ), '''box''': {'''xmin''': ANY(lowercase_ ), '''ymin''': ANY(lowercase_ ), '''xmax''': ANY(lowercase_ ), '''ymax''': ANY(lowercase_ )}, } , ) import datasets A_ = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''' ) A_ = [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ] A_ = object_detector(lowercase_ , threshold=0.0 ) self.assertEqual(len(lowercase_ ) , len(lowercase_ ) ) for outputs in batch_outputs: self.assertGreater(len(lowercase_ ) , 0 ) for detected_object in outputs: self.assertEqual( lowercase_ , { '''score''': ANY(lowercase_ ), '''label''': ANY(lowercase_ ), '''box''': {'''xmin''': ANY(lowercase_ ), '''ymin''': ANY(lowercase_ ), '''xmax''': ANY(lowercase_ ), '''ymax''': ANY(lowercase_ )}, } , ) @require_tf @unittest.skip('''Object detection not implemented in TF''' ) def __A ( self ) -> Optional[Any]: pass @require_torch def __A ( self ) -> Any: A_ = '''hf-internal-testing/tiny-detr-mobilenetsv3''' A_ = AutoModelForObjectDetection.from_pretrained(lowercase_ ) A_ = AutoFeatureExtractor.from_pretrained(lowercase_ ) A_ = ObjectDetectionPipeline(model=lowercase_ , feature_extractor=lowercase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=0.0 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ] , ) A_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] , threshold=0.0 , ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ], [ {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ], ] , ) @require_torch @slow def __A ( self ) -> Optional[Any]: A_ = '''facebook/detr-resnet-50''' A_ = AutoModelForObjectDetection.from_pretrained(lowercase_ ) A_ = AutoFeatureExtractor.from_pretrained(lowercase_ ) A_ = ObjectDetectionPipeline(model=lowercase_ , feature_extractor=lowercase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) A_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], ] , ) @require_torch @slow def __A ( self ) -> Dict: A_ = '''facebook/detr-resnet-50''' A_ = pipeline('''object-detection''' , model=lowercase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) A_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], ] , ) @require_torch @slow def __A ( self ) -> List[Any]: A_ = 0.9_985 A_ = '''facebook/detr-resnet-50''' A_ = pipeline('''object-detection''' , model=lowercase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=lowercase_ ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) @require_torch @require_pytesseract @slow def __A ( self ) -> Tuple: A_ = '''Narsil/layoutlmv3-finetuned-funsd''' A_ = 0.9_993 A_ = pipeline('''object-detection''' , model=lowercase_ , threshold=lowercase_ ) A_ = object_detector( '''https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png''' ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {'''score''': 0.9_993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}}, {'''score''': 0.9_993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}}, ] , )
368
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets __snake_case : Any = '\\n@inproceedings{lin-2004-rouge,\n title = "{ROUGE}: A Package for Automatic Evaluation of Summaries",\n author = "Lin, Chin-Yew",\n booktitle = "Text Summarization Branches Out",\n month = jul,\n year = "2004",\n address = "Barcelona, Spain",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W04-1013",\n pages = "74--81",\n}\n' __snake_case : Dict = '\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n' __snake_case : Optional[int] = '\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `"rouge{n}"` (e.g. `"rouge1"`, `"rouge2"`) where: {n} is the n-gram based scoring,\n `"rougeL"`: Longest common subsequence based scoring.\n `"rougeLSum"`: rougeLsum splits text using `"\n"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric(\'rouge\')\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n [\'rouge1\', \'rouge2\', \'rougeL\', \'rougeLsum\']\n >>> print(results["rouge1"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results["rouge1"].mid.fmeasure)\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> List[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/google-research/google-research/tree/master/rouge'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/ROUGE_(metric)''', '''https://github.com/google-research/google-research/tree/master/rouge''', ] , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False ) -> Optional[int]: if rouge_types is None: A_ = ['''rouge1''', '''rouge2''', '''rougeL''', '''rougeLsum'''] A_ = rouge_scorer.RougeScorer(rouge_types=_SCREAMING_SNAKE_CASE , use_stemmer=_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = scoring.BootstrapAggregator() else: A_ = [] for ref, pred in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ = scorer.score(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if use_aggregator: aggregator.add_scores(_SCREAMING_SNAKE_CASE ) else: scores.append(_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = aggregator.aggregate() else: A_ = {} for key in scores[0]: A_ = [score[key] for score in scores] return result
18
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ = logging.get_logger(__name__) UpperCamelCase__ = { """abeja/gpt-neox-japanese-2.7b""": """https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json""", } class a__ ( _a ): _a : List[str] = '''gpt_neox_japanese''' def __init__( self , _A=3_2_0_0_0 , _A=2_5_6_0 , _A=3_2 , _A=3_2 , _A=4 , _A="gelu" , _A=1.00 , _A=1_0_0_0_0 , _A=2_0_4_8 , _A=0.02 , _A=1E-5 , _A=True , _A=3_1_9_9_6 , _A=3_1_9_9_9 , _A=0.1 , _A=0.0 , **_A , ): """simple docstring""" super().__init__(bos_token_id=SCREAMING_SNAKE_CASE__ , eos_token_id=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = vocab_size __lowerCAmelCase = max_position_embeddings __lowerCAmelCase = hidden_size __lowerCAmelCase = num_hidden_layers __lowerCAmelCase = num_attention_heads __lowerCAmelCase = intermediate_multiple_size __lowerCAmelCase = hidden_act __lowerCAmelCase = rotary_pct __lowerCAmelCase = rotary_emb_base __lowerCAmelCase = initializer_range __lowerCAmelCase = layer_norm_eps __lowerCAmelCase = use_cache __lowerCAmelCase = attention_dropout __lowerCAmelCase = hidden_dropout
92
'''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_ = logging.get_logger(__name__) # pylint: disable=invalid-name class _snake_case ( _a ): def __init__( self : List[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: SCREAMING_SNAKE_CASE:Union[str, Any] = ( 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__ ) SCREAMING_SNAKE_CASE:Tuple = dict(scheduler.config ) SCREAMING_SNAKE_CASE:Union[str, Any] = 1 SCREAMING_SNAKE_CASE:Dict = FrozenDict(SCREAMING_SNAKE_CASE__ ) if hasattr(scheduler.config ,"skip_prk_steps" ) and scheduler.config.skip_prk_steps is False: SCREAMING_SNAKE_CASE:List[Any] = ( 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__ ) SCREAMING_SNAKE_CASE:Tuple = dict(scheduler.config ) SCREAMING_SNAKE_CASE:int = True SCREAMING_SNAKE_CASE:Optional[int] = 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 __UpperCamelCase ( 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 SCREAMING_SNAKE_CASE:Optional[Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(SCREAMING_SNAKE_CASE__ ) def __UpperCamelCase ( self : str ): self.enable_attention_slicing(SCREAMING_SNAKE_CASE__ ) def __UpperCamelCase ( self : List[str] ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) SCREAMING_SNAKE_CASE:str = 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 __UpperCamelCase ( self : 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 : Dict ,SCREAMING_SNAKE_CASE__ : Union[str, List[str]] ,SCREAMING_SNAKE_CASE__ : Union[torch.FloatTensor, PIL.Image.Image] ,SCREAMING_SNAKE_CASE__ : str ,SCREAMING_SNAKE_CASE__ : int = 512 ,SCREAMING_SNAKE_CASE__ : int = 512 ,SCREAMING_SNAKE_CASE__ : int = 50 ,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__ : Dict ,): SCREAMING_SNAKE_CASE:str = self.segmentation_processor( text=[text] ,images=[image] ,padding="max_length" ,return_tensors="pt" ).to(self.device ) SCREAMING_SNAKE_CASE:Union[str, Any] = self.segmentation_model(**SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE:Optional[int] = torch.sigmoid(outputs.logits ).cpu().detach().unsqueeze(-1 ).numpy() SCREAMING_SNAKE_CASE:Optional[Any] = self.numpy_to_pil(SCREAMING_SNAKE_CASE__ )[0].resize(image.size ) # Run inpainting pipeline with the generated mask SCREAMING_SNAKE_CASE:Any = 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__ ,)
139
0
"""simple docstring""" from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class _lowerCAmelCase ( nn.Module ): """simple docstring""" def __init__( self : Union[str, Any], UpperCAmelCase__ : str = 1_6, UpperCAmelCase__ : str = 8_8, UpperCAmelCase__ : Union[str, Any] = None, UpperCAmelCase__ : Dict = 1, UpperCAmelCase__ : Optional[Any] = 0.0, UpperCAmelCase__ : Dict = 3_2, UpperCAmelCase__ : str = None, UpperCAmelCase__ : List[str] = False, UpperCAmelCase__ : Union[str, Any] = None, UpperCAmelCase__ : Union[str, Any] = None, UpperCAmelCase__ : Any = "geglu", UpperCAmelCase__ : List[str] = None, ): super().__init__() __lowercase = nn.ModuleList( [ TransformeraDModel( num_attention_heads=__lowercase, attention_head_dim=__lowercase, in_channels=__lowercase, num_layers=__lowercase, dropout=__lowercase, norm_num_groups=__lowercase, cross_attention_dim=__lowercase, attention_bias=__lowercase, sample_size=__lowercase, num_vector_embeds=__lowercase, activation_fn=__lowercase, num_embeds_ada_norm=__lowercase, ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference __lowercase = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` __lowercase = [7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` __lowercase = [1, 0] def _lowercase ( self : Any, UpperCAmelCase__ : List[Any], UpperCAmelCase__ : Optional[Any], UpperCAmelCase__ : List[str]=None, UpperCAmelCase__ : List[Any]=None, UpperCAmelCase__ : List[Any]=None, UpperCAmelCase__ : Tuple = True, ): __lowercase = hidden_states __lowercase = [] __lowercase = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens __lowercase = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] __lowercase = self.transformer_index_for_condition[i] __lowercase = self.transformers[transformer_index]( __lowercase, encoder_hidden_states=__lowercase, timestep=__lowercase, cross_attention_kwargs=__lowercase, return_dict=__lowercase, )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] __lowercase = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) __lowercase = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=__lowercase )
359
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : List[str], UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : Any, UpperCAmelCase__ : Dict ): __lowercase = dataset __lowercase = process __lowercase = params def __len__( self : str ): return len(self.dataset ) def __getitem__( self : List[Any], UpperCAmelCase__ : int ): __lowercase = self.dataset[i] __lowercase = self.process(UpperCAmelCase__, **self.params ) return processed class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : Optional[Any], UpperCAmelCase__ : Dict, UpperCAmelCase__ : Any, UpperCAmelCase__ : Union[str, Any], UpperCAmelCase__ : List[Any]=None ): __lowercase = loader __lowercase = infer __lowercase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether __lowercase = None __lowercase = loader_batch_size # Internal bookkeeping __lowercase = None __lowercase = None def __len__( self : str ): return len(self.loader ) def __iter__( self : List[str] ): __lowercase = iter(self.loader ) return self def _lowercase ( self : Union[str, Any] ): if isinstance(self._loader_batch_data, torch.Tensor ): # Batch data is simple tensor, just fetch the slice __lowercase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) __lowercase = {} for k, element in self._loader_batch_data.items(): if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): # Convert ModelOutput to tuple first __lowercase = element.to_tuple() if isinstance(element[0], torch.Tensor ): __lowercase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0], np.ndarray ): __lowercase = tuple(np.expand_dims(el[self._loader_batch_index], 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(UpperCAmelCase__, UpperCAmelCase__ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0], torch.Tensor ): __lowercase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0], np.ndarray ): __lowercase = tuple(np.expand_dims(el[self._loader_batch_index], 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around __lowercase = None elif isinstance(element[self._loader_batch_index], torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers __lowercase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index], np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers __lowercase = np.expand_dims(element[self._loader_batch_index], 0 ) else: # This is typically a list, so no need to `unsqueeze`. __lowercase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 __lowercase = self._loader_batch_data.__class__(UpperCAmelCase__ ) self._loader_batch_index += 1 return result def _lowercase ( self : Tuple ): if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch __lowercase = next(self.iterator ) __lowercase = self.infer(UpperCAmelCase__, **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(UpperCAmelCase__, torch.Tensor ): __lowercase = processed else: __lowercase = list(processed.keys() )[0] __lowercase = processed[key] if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase = len(UpperCAmelCase__ ) else: __lowercase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. __lowercase = observed_batch_size # Setting internal index to unwrap the batch __lowercase = processed __lowercase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : Union[str, Any], UpperCAmelCase__ : Any, UpperCAmelCase__ : List[str], UpperCAmelCase__ : int, UpperCAmelCase__ : str=None ): super().__init__(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) def __iter__( self : str ): __lowercase = iter(self.loader ) __lowercase = None return self def _lowercase ( self : int ): if self.subiterator is None: __lowercase = self.infer(next(self.iterator ), **self.params ) try: # Try to return next item __lowercase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators __lowercase = self.infer(next(self.iterator ), **self.params ) __lowercase = next(self.subiterator ) return processed class _lowerCAmelCase ( lowercase ): """simple docstring""" def __iter__( self : int ): __lowercase = iter(self.loader ) return self def _lowercase ( self : List[str] ): # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. __lowercase = False __lowercase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: __lowercase = self.loader_batch_item() __lowercase = item.pop("is_last" ) accumulator.append(UpperCAmelCase__ ) if is_last: return accumulator while not is_last: __lowercase = self.infer(next(self.iterator ), **self.params ) if self.loader_batch_size is not None: if isinstance(UpperCAmelCase__, torch.Tensor ): __lowercase = processed else: __lowercase = list(processed.keys() )[0] __lowercase = processed[key] if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase = len(UpperCAmelCase__ ) else: __lowercase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. __lowercase = observed_batch_size __lowercase = processed __lowercase = 0 while self._loader_batch_index < self.loader_batch_size: __lowercase = self.loader_batch_item() __lowercase = item.pop("is_last" ) accumulator.append(UpperCAmelCase__ ) if is_last: return accumulator else: __lowercase = processed __lowercase = item.pop("is_last" ) accumulator.append(UpperCAmelCase__ ) return accumulator class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : List[Any], UpperCAmelCase__ : Dataset, UpperCAmelCase__ : str ): __lowercase = dataset __lowercase = key def __len__( self : Optional[Any] ): return len(self.dataset ) def __getitem__( self : Union[str, Any], UpperCAmelCase__ : Any ): return self.dataset[i][self.key] class _lowerCAmelCase ( lowercase ): """simple docstring""" def __init__( self : str, UpperCAmelCase__ : Dataset, UpperCAmelCase__ : str, UpperCAmelCase__ : str ): __lowercase = dataset __lowercase = keya __lowercase = keya def __len__( self : Optional[int] ): return len(self.dataset ) def __getitem__( self : Dict, UpperCAmelCase__ : Tuple ): return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
144
0
import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __magic_name__ ( A : List[str], A : str, A : int, A : Union[str, Any] ): '''simple docstring''' a = multiprocessing.Manager() a = manager.list() a = multiprocessing.Process(target=__lowerCAmelCase, args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("timed out" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __magic_name__ ( A : List[str], A : Union[str, Any], A : List[Any] ): '''simple docstring''' with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil a = shutil.rmtree a = os.rmdir a = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: a = {} with swallow_io(): with time_limit(__lowerCAmelCase ): exec(__lowerCAmelCase, __lowerCAmelCase ) result.append("passed" ) except TimeoutException: result.append("timed out" ) except BaseException as e: result.append(F"""failed: {e}""" ) # Needed for cleaning up. a = rmtree a = rmdir a = chdir @contextlib.contextmanager def __magic_name__ ( A : Optional[Any] ): '''simple docstring''' def signal_handler(A : int, A : Optional[Any] ): raise TimeoutException("Timed out!" ) signal.setitimer(signal.ITIMER_REAL, __lowerCAmelCase ) signal.signal(signal.SIGALRM, __lowerCAmelCase ) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0 ) @contextlib.contextmanager def __magic_name__ ( ): '''simple docstring''' a = WriteOnlyStringIO() with contextlib.redirect_stdout(__lowerCAmelCase ): with contextlib.redirect_stderr(__lowerCAmelCase ): with redirect_stdin(__lowerCAmelCase ): yield @contextlib.contextmanager def __magic_name__ ( ): '''simple docstring''' with tempfile.TemporaryDirectory() as dirname: with chdir(__lowerCAmelCase ): yield dirname class snake_case__ (__UpperCAmelCase ): """simple docstring""" pass class snake_case__ (io.StringIO ): """simple docstring""" def __UpperCAmelCase ( self : int , *__lowerCamelCase : int , **__lowerCamelCase : Union[str, Any] ) -> Optional[Any]: raise OSError def __UpperCAmelCase ( self : int , *__lowerCamelCase : Optional[Any] , **__lowerCamelCase : Dict ) -> str: raise OSError def __UpperCAmelCase ( self : Optional[int] , *__lowerCamelCase : Dict , **__lowerCamelCase : int ) -> Any: raise OSError def __UpperCAmelCase ( self : Tuple , *__lowerCamelCase : Optional[int] , **__lowerCamelCase : Optional[Any] ) -> int: return False class snake_case__ (contextlib._RedirectStream ): # type: ignore """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = """stdin""" @contextlib.contextmanager def __magic_name__ ( A : Union[str, Any] ): '''simple docstring''' if root == ".": yield return a = os.getcwd() os.chdir(__lowerCAmelCase ) try: yield except BaseException as exc: raise exc finally: os.chdir(__lowerCAmelCase ) def __magic_name__ ( A : Optional[Any]=None ): '''simple docstring''' if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins a = None a = None import os a = "1" a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None a = None import shutil a = None a = None a = None import subprocess a = None # type: ignore a = None import sys a = None a = None a = None a = None a = None
107
"""simple docstring""" import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder UpperCAmelCase : Union[str, Any] = "__DUMMY_TRANSFORMERS_USER__" UpperCAmelCase : Dict = "Dummy User" UpperCAmelCase : Optional[int] = "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" UpperCAmelCase : Tuple = "https://hub-ci.huggingface.co" UpperCAmelCase : Optional[Any] = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" UpperCAmelCase : Tuple = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" UpperCAmelCase : int = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> Dict: '''simple docstring''' monkeypatch.setattr( """huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE""" , __lowerCAmelCase ) @pytest.fixture def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> Union[str, Any]: '''simple docstring''' monkeypatch.setattr("""datasets.config.HF_ENDPOINT""" , __lowerCAmelCase ) monkeypatch.setattr("""datasets.config.HUB_DATASETS_URL""" , __lowerCAmelCase ) @pytest.fixture def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> int: '''simple docstring''' monkeypatch.setattr("""huggingface_hub.hf_api.HfFolder.path_token""" , __lowerCAmelCase ) @pytest.fixture def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase ) -> Optional[Any]: '''simple docstring''' HfFolder.save_token(__lowerCAmelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="""session""" ) def _SCREAMING_SNAKE_CASE () -> Dict: '''simple docstring''' return HfApi(endpoint=__lowerCAmelCase ) @pytest.fixture(scope="""session""" ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> List[str]: '''simple docstring''' lowercase_ = 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 _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> Optional[Any]: '''simple docstring''' def _cleanup_repo(__lowerCAmelCase ): hf_api.delete_repo(__lowerCAmelCase , token=__lowerCAmelCase , repo_type="""dataset""" ) return _cleanup_repo @pytest.fixture def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> Optional[Any]: '''simple docstring''' @contextmanager def _temporary_repo(__lowerCAmelCase ): try: yield repo_id finally: cleanup_repo(__lowerCAmelCase ) return _temporary_repo @pytest.fixture(scope="""session""" ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> List[Any]: '''simple docstring''' lowercase_ = F'''repo_txt_data-{int(time.time() * 10E3 )}''' lowercase_ = 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 _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Any: '''simple docstring''' return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="""session""" ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Union[str, Any]: '''simple docstring''' lowercase_ = F'''repo_zipped_txt_data-{int(time.time() * 10E3 )}''' lowercase_ = 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 _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Optional[Any]: '''simple docstring''' return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="""session""" ) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> str: '''simple docstring''' lowercase_ = F'''repo_zipped_img_data-{int(time.time() * 10E3 )}''' lowercase_ = 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 _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Dict: '''simple docstring''' return hf_private_dataset_repo_zipped_img_data_
136
0
import itertools from dataclasses import dataclass from typing import Optional import pandas as pd import pyarrow as pa import datasets from datasets.table import table_cast @dataclass class a ( datasets.BuilderConfig ): '''simple docstring''' lowerCAmelCase : Optional[datasets.Features] = None class a ( datasets.ArrowBasedBuilder ): '''simple docstring''' lowerCAmelCase : Optional[int] = PandasConfig def lowerCamelCase_ ( self : int ): return datasets.DatasetInfo(features=self.config.features ) def lowerCamelCase_ ( self : List[str] , __snake_case : List[str] ): if not self.config.data_files: raise ValueError(F'At least one data file must be specified, but got data_files={self.config.data_files}' ) UpperCAmelCase_ = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__snake_case , (str, list, tuple) ): UpperCAmelCase_ = data_files if isinstance(__snake_case , __snake_case ): UpperCAmelCase_ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCAmelCase_ = [dl_manager.iter_files(__snake_case ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] UpperCAmelCase_ = [] for split_name, files in data_files.items(): if isinstance(__snake_case , __snake_case ): UpperCAmelCase_ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCAmelCase_ = [dl_manager.iter_files(__snake_case ) for file in files] splits.append(datasets.SplitGenerator(name=__snake_case , gen_kwargs={'''files''': files} ) ) return splits def lowerCamelCase_ ( self : Dict , __snake_case : pa.Table ): if self.config.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example UpperCAmelCase_ = table_cast(__snake_case , self.config.features.arrow_schema ) return pa_table def lowerCamelCase_ ( self : Union[str, Any] , __snake_case : List[Any] ): for i, file in enumerate(itertools.chain.from_iterable(__snake_case ) ): with open(__snake_case , '''rb''' ) as f: UpperCAmelCase_ = pa.Table.from_pandas(pd.read_pickle(__snake_case ) ) yield i, self._cast_table(__snake_case )
352
# This model implementation is heavily inspired by https://github.com/haofanwang/ControlNet-for-Diffusers/ 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, ControlNetModel, DDIMScheduler, StableDiffusionControlNetImgaImgPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, ) enable_full_determinism() class a ( _A , _A , _A , unittest.TestCase ): '''simple docstring''' lowerCAmelCase : List[Any] = StableDiffusionControlNetImgaImgPipeline lowerCAmelCase : List[str] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'} lowerCAmelCase : Union[str, Any] = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowerCAmelCase : Optional[Any] = IMAGE_TO_IMAGE_IMAGE_PARAMS.union({'control_image'} ) lowerCAmelCase : List[Any] = IMAGE_TO_IMAGE_IMAGE_PARAMS def lowerCamelCase_ ( self : Dict ): torch.manual_seed(0 ) UpperCAmelCase_ = 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 , ) torch.manual_seed(0 ) UpperCAmelCase_ = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) torch.manual_seed(0 ) UpperCAmelCase_ = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__snake_case , set_alpha_to_one=__snake_case , ) torch.manual_seed(0 ) UpperCAmelCase_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) UpperCAmelCase_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) UpperCAmelCase_ = CLIPTextModel(__snake_case ) UpperCAmelCase_ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) UpperCAmelCase_ = { '''unet''': unet, '''controlnet''': controlnet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def lowerCamelCase_ ( self : Union[str, Any] , __snake_case : Optional[int] , __snake_case : Any=0 ): if str(__snake_case ).startswith('''mps''' ): UpperCAmelCase_ = torch.manual_seed(__snake_case ) else: UpperCAmelCase_ = torch.Generator(device=__snake_case ).manual_seed(__snake_case ) UpperCAmelCase_ = 2 UpperCAmelCase_ = randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=__snake_case , device=torch.device(__snake_case ) , ) UpperCAmelCase_ = floats_tensor(control_image.shape , rng=random.Random(__snake_case ) ).to(__snake_case ) UpperCAmelCase_ = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__snake_case ) ).convert('''RGB''' ).resize((64, 64) ) UpperCAmelCase_ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', '''image''': image, '''control_image''': control_image, } return inputs def lowerCamelCase_ ( self : Union[str, Any] ): return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def lowerCamelCase_ ( self : Optional[Any] ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 ) def lowerCamelCase_ ( self : Dict ): self._test_inference_batch_single_identical(expected_max_diff=2E-3 ) class a ( _A , _A , unittest.TestCase ): '''simple docstring''' lowerCAmelCase : Dict = StableDiffusionControlNetImgaImgPipeline lowerCAmelCase : List[Any] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'} lowerCAmelCase : Any = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowerCAmelCase : Optional[int] = frozenset([] ) # TO_DO: add image_params once refactored VaeImageProcessor.preprocess def lowerCamelCase_ ( self : Optional[Any] ): torch.manual_seed(0 ) UpperCAmelCase_ = 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 , ) torch.manual_seed(0 ) def init_weights(__snake_case : Tuple ): if isinstance(__snake_case , torch.nn.Convad ): torch.nn.init.normal(m.weight ) m.bias.data.fill_(1.0 ) UpperCAmelCase_ = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) controlneta.controlnet_down_blocks.apply(__snake_case ) torch.manual_seed(0 ) UpperCAmelCase_ = ControlNetModel( block_out_channels=(32, 64) , layers_per_block=2 , in_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , cross_attention_dim=32 , conditioning_embedding_out_channels=(16, 32) , ) controlneta.controlnet_down_blocks.apply(__snake_case ) torch.manual_seed(0 ) UpperCAmelCase_ = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__snake_case , set_alpha_to_one=__snake_case , ) torch.manual_seed(0 ) UpperCAmelCase_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) UpperCAmelCase_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) UpperCAmelCase_ = CLIPTextModel(__snake_case ) UpperCAmelCase_ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) UpperCAmelCase_ = MultiControlNetModel([controlneta, controlneta] ) UpperCAmelCase_ = { '''unet''': unet, '''controlnet''': controlnet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def lowerCamelCase_ ( self : Optional[Any] , __snake_case : Any , __snake_case : Optional[Any]=0 ): if str(__snake_case ).startswith('''mps''' ): UpperCAmelCase_ = torch.manual_seed(__snake_case ) else: UpperCAmelCase_ = torch.Generator(device=__snake_case ).manual_seed(__snake_case ) UpperCAmelCase_ = 2 UpperCAmelCase_ = [ randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=__snake_case , device=torch.device(__snake_case ) , ), randn_tensor( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor) , generator=__snake_case , device=torch.device(__snake_case ) , ), ] UpperCAmelCase_ = floats_tensor(control_image[0].shape , rng=random.Random(__snake_case ) ).to(__snake_case ) UpperCAmelCase_ = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__snake_case ) ).convert('''RGB''' ).resize((64, 64) ) UpperCAmelCase_ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', '''image''': image, '''control_image''': control_image, } return inputs def lowerCamelCase_ ( self : List[Any] ): UpperCAmelCase_ = self.get_dummy_components() UpperCAmelCase_ = self.pipeline_class(**__snake_case ) pipe.to(__snake_case ) UpperCAmelCase_ = 10.0 UpperCAmelCase_ = 4 UpperCAmelCase_ = self.get_dummy_inputs(__snake_case ) UpperCAmelCase_ = steps UpperCAmelCase_ = scale UpperCAmelCase_ = pipe(**__snake_case )[0] UpperCAmelCase_ = self.get_dummy_inputs(__snake_case ) UpperCAmelCase_ = steps UpperCAmelCase_ = scale UpperCAmelCase_ = pipe(**__snake_case , control_guidance_start=0.1 , control_guidance_end=0.2 )[0] UpperCAmelCase_ = self.get_dummy_inputs(__snake_case ) UpperCAmelCase_ = steps UpperCAmelCase_ = scale UpperCAmelCase_ = pipe(**__snake_case , control_guidance_start=[0.1, 0.3] , control_guidance_end=[0.2, 0.7] )[0] UpperCAmelCase_ = self.get_dummy_inputs(__snake_case ) UpperCAmelCase_ = steps UpperCAmelCase_ = scale UpperCAmelCase_ = pipe(**__snake_case , control_guidance_start=0.4 , control_guidance_end=[0.5, 0.8] )[0] # make sure that all outputs are different assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 assert np.sum(np.abs(output_a - output_a ) ) > 1E-3 def lowerCamelCase_ ( self : Optional[int] ): return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3 ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def lowerCamelCase_ ( self : List[Any] ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3 ) def lowerCamelCase_ ( self : List[Any] ): self._test_inference_batch_single_identical(expected_max_diff=2E-3 ) def lowerCamelCase_ ( self : Optional[Any] ): UpperCAmelCase_ = self.get_dummy_components() UpperCAmelCase_ = self.pipeline_class(**__snake_case ) pipe.to(__snake_case ) pipe.set_progress_bar_config(disable=__snake_case ) with tempfile.TemporaryDirectory() as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(__snake_case ) except NotImplementedError: pass @slow @require_torch_gpu class a ( unittest.TestCase ): '''simple docstring''' def lowerCamelCase_ ( self : Optional[Any] ): super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCamelCase_ ( self : Union[str, Any] ): UpperCAmelCase_ = ControlNetModel.from_pretrained('''lllyasviel/sd-controlnet-canny''' ) UpperCAmelCase_ = StableDiffusionControlNetImgaImgPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , safety_checker=__snake_case , controlnet=__snake_case ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=__snake_case ) UpperCAmelCase_ = torch.Generator(device='''cpu''' ).manual_seed(0 ) UpperCAmelCase_ = '''evil space-punk bird''' UpperCAmelCase_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png''' ).resize((5_12, 5_12) ) UpperCAmelCase_ = load_image( '''https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png''' ).resize((5_12, 5_12) ) UpperCAmelCase_ = pipe( __snake_case , __snake_case , control_image=__snake_case , generator=__snake_case , output_type='''np''' , num_inference_steps=50 , strength=0.6 , ) UpperCAmelCase_ = output.images[0] assert image.shape == (5_12, 5_12, 3) UpperCAmelCase_ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/img2img.npy''' ) assert np.abs(expected_image - image ).max() < 9E-2
177
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, is_vision_available, ) __A ={ 'configuration_owlvit': [ 'OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'OwlViTConfig', 'OwlViTOnnxConfig', 'OwlViTTextConfig', 'OwlViTVisionConfig', ], 'processing_owlvit': ['OwlViTProcessor'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A =['OwlViTFeatureExtractor'] __A =['OwlViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A =[ 'OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'OwlViTModel', 'OwlViTPreTrainedModel', 'OwlViTTextModel', 'OwlViTVisionModel', 'OwlViTForObjectDetection', ] if TYPE_CHECKING: from .configuration_owlvit import ( OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, OwlViTConfig, OwlViTOnnxConfig, OwlViTTextConfig, OwlViTVisionConfig, ) from .processing_owlvit import OwlViTProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_owlvit import OwlViTFeatureExtractor from .image_processing_owlvit import OwlViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_owlvit import ( OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST, OwlViTForObjectDetection, OwlViTModel, OwlViTPreTrainedModel, OwlViTTextModel, OwlViTVisionModel, ) else: import sys __A =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
163
'''simple docstring''' import logging import os import sys from pathlib import Path from unittest.mock import patch from parameterized import parameterized from run_eval import run_generate from run_eval_search import run_search from transformers.testing_utils import CaptureStdout, TestCasePlus, slow from utils import ROUGE_KEYS logging.basicConfig(level=logging.DEBUG) __A =logging.getLogger() def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase__ : Union[str, Any] = """\n""".join(UpperCamelCase__ ) Path(UpperCamelCase__ ).open("""w""" ).writelines(UpperCamelCase__ ) __A ='patrickvonplaten/t5-tiny-random' __A ='sshleifer/bart-tiny-random' __A ='sshleifer/tiny-mbart' __A =logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) logging.disable(logging.CRITICAL) # remove noisy download output from tracebacks class _snake_case ( a__ ): def snake_case__ ( self , _lowerCamelCase): UpperCAmelCase__ : Any = Path(self.get_auto_remove_tmp_dir()) / """utest_input.source""" UpperCAmelCase__ : Dict = input_file_name.parent / """utest_output.txt""" assert not output_file_name.exists() UpperCAmelCase__ : Any = [""" New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County."""] _dump_articles(_lowerCamelCase , _lowerCamelCase) UpperCAmelCase__ : Optional[Any] = str(Path(self.get_auto_remove_tmp_dir()) / """scores.json""") UpperCAmelCase__ : int = """translation_en_to_de""" if model == T5_TINY else """summarization""" UpperCAmelCase__ : Union[str, Any] = f''' run_eval_search.py {model} {input_file_name} {output_file_name} --score_path {score_path} --task {task} --num_beams 2 --length_penalty 2.0 '''.split() with patch.object(_lowerCamelCase , """argv""" , _lowerCamelCase): run_generate() assert Path(_lowerCamelCase).exists() # os.remove(Path(output_file_name)) def snake_case__ ( self): self.run_eval_tester(_lowerCamelCase) @parameterized.expand([BART_TINY, MBART_TINY]) @slow def snake_case__ ( self , _lowerCamelCase): self.run_eval_tester(_lowerCamelCase) @parameterized.expand([T5_TINY, MBART_TINY]) @slow def snake_case__ ( self , _lowerCamelCase): UpperCAmelCase__ : Optional[Any] = Path(self.get_auto_remove_tmp_dir()) / """utest_input.source""" UpperCAmelCase__ : List[str] = input_file_name.parent / """utest_output.txt""" assert not output_file_name.exists() UpperCAmelCase__ : int = { """en""": ["""Machine learning is great, isn't it?""", """I like to eat bananas""", """Tomorrow is another great day!"""], """de""": [ """Maschinelles Lernen ist großartig, oder?""", """Ich esse gerne Bananen""", """Morgen ist wieder ein toller Tag!""", ], } UpperCAmelCase__ : int = Path(self.get_auto_remove_tmp_dir()) UpperCAmelCase__ : Any = str(tmp_dir / """scores.json""") UpperCAmelCase__ : List[str] = str(tmp_dir / """val.target""") _dump_articles(_lowerCamelCase , text["""en"""]) _dump_articles(_lowerCamelCase , text["""de"""]) UpperCAmelCase__ : int = """translation_en_to_de""" if model == T5_TINY else """summarization""" UpperCAmelCase__ : List[Any] = f''' run_eval_search.py {model} {str(_lowerCamelCase)} {str(_lowerCamelCase)} --score_path {score_path} --reference_path {reference_path} --task {task} '''.split() testargs.extend(["""--search""", """num_beams=1:2 length_penalty=0.9:1.0"""]) with patch.object(_lowerCamelCase , """argv""" , _lowerCamelCase): with CaptureStdout() as cs: run_search() UpperCAmelCase__ : Optional[Any] = [""" num_beams | length_penalty""", model, """Best score args"""] UpperCAmelCase__ : Any = ["""Info"""] if "translation" in task: expected_strings.append("""bleu""") else: expected_strings.extend(_lowerCamelCase) for w in expected_strings: assert w in cs.out for w in un_expected_strings: assert w not in cs.out assert Path(_lowerCamelCase).exists() os.remove(Path(_lowerCamelCase))
163
1
'''simple docstring''' from __future__ import annotations def __UpperCamelCase ( _UpperCAmelCase ): __UpperCAmelCase : Dict = 0.00 __UpperCAmelCase : Dict = 0 for resistor in resistors: if resistor <= 0: __UpperCAmelCase : Optional[Any] = F"Resistor at index {index} has a negative or zero value!" raise ValueError(_UpperCAmelCase ) first_sum += 1 / float(_UpperCAmelCase ) index += 1 return 1 / first_sum def __UpperCamelCase ( _UpperCAmelCase ): __UpperCAmelCase : int = 0.00 __UpperCAmelCase : Optional[Any] = 0 for resistor in resistors: sum_r += resistor if resistor < 0: __UpperCAmelCase : int = F"Resistor at index {index} has a negative value!" raise ValueError(_UpperCAmelCase ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
366
'''simple docstring''' import warnings from ...utils import logging from .image_processing_imagegpt import ImageGPTImageProcessor lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( snake_case__ ): """simple docstring""" def __init__( self : Any , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : Union[str, Any] ): """simple docstring""" warnings.warn( "The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use ImageGPTImageProcessor instead." , UpperCAmelCase_ , ) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_ )
37
0
import importlib import inspect import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py _a = '''src/transformers''' # This is to make sure the transformers module imported is the one in the repo. _a = importlib.util.spec_from_file_location( '''transformers''', os.path.join(PATH_TO_TRANSFORMERS, '''__init__.py'''), submodule_search_locations=[PATH_TO_TRANSFORMERS], ) _a = spec.loader.load_module() _a = transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` _a = re.compile('''\[(.+?)\]\((https://huggingface\.co/.+?)\)''') _a = { '''CLIPConfigMixin''', '''DecisionTransformerConfigMixin''', '''EncoderDecoderConfigMixin''', '''RagConfigMixin''', '''SpeechEncoderDecoderConfigMixin''', '''VisionEncoderDecoderConfigMixin''', '''VisionTextDualEncoderConfigMixin''', } def __A ( )-> List[str]: """simple docstring""" _UpperCAmelCase = [] for config_class in list(CONFIG_MAPPING.values() ): _UpperCAmelCase = False # source code of `config_class` _UpperCAmelCase = inspect.getsource(__lowerCAmelCase ) _UpperCAmelCase = _re_checkpoint.findall(__lowerCAmelCase ) for checkpoint in checkpoints: # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` _UpperCAmelCase , _UpperCAmelCase = checkpoint # verify the checkpoint name corresponds to the checkpoint link _UpperCAmelCase = F"""https://huggingface.co/{ckpt_name}""" if ckpt_link == ckpt_link_from_name: _UpperCAmelCase = True break _UpperCAmelCase = config_class.__name__ if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(__lowerCAmelCase ) if len(__lowerCAmelCase ) > 0: _UpperCAmelCase = '\n'.join(sorted(__lowerCAmelCase ) ) raise ValueError(F"""The following configurations don't contain any valid checkpoint:\n{message}""" ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
39
import unittest from transformers import AlbertTokenizer, AlbertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _a = get_tests_dir('''fixtures/spiece.model''') @require_sentencepiece @require_tokenizers class __lowerCamelCase ( snake_case__ , unittest.TestCase): """simple docstring""" UpperCamelCase__ = AlbertTokenizer UpperCamelCase__ = AlbertTokenizerFast UpperCamelCase__ = True UpperCamelCase__ = True UpperCamelCase__ = True def UpperCamelCase ( self ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase = AlbertTokenizer(UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = 'this is a test' _UpperCAmelCase = 'this is a test' return input_text, output_text def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = '<pad>' _UpperCAmelCase = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase ) , UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase ) , UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<pad>' ) self.assertEqual(vocab_keys[1] , '<unk>' ) self.assertEqual(vocab_keys[-1] , '▁eloquent' ) self.assertEqual(len(UpperCAmelCase ) , 3_0000 ) def UpperCamelCase ( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 3_0000 ) def UpperCamelCase ( self ): """simple docstring""" if not self.test_rust_tokenizer: return _UpperCAmelCase = self.get_tokenizer() _UpperCAmelCase = self.get_rust_tokenizer() _UpperCAmelCase = 'I was born in 92000, and this is falsé.' _UpperCAmelCase = tokenizer.tokenize(UpperCAmelCase ) _UpperCAmelCase = rust_tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) _UpperCAmelCase = rust_tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self.get_rust_tokenizer() _UpperCAmelCase = tokenizer.encode(UpperCAmelCase ) _UpperCAmelCase = rust_tokenizer.encode(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = AlbertTokenizer(UpperCAmelCase , keep_accents=UpperCAmelCase ) _UpperCAmelCase = tokenizer.tokenize('This is a test' ) self.assertListEqual(UpperCAmelCase , ['▁this', '▁is', '▁a', '▁test'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [48, 25, 21, 1289] ) _UpperCAmelCase = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( UpperCAmelCase , ['▁i', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', 'é', '.'] ) _UpperCAmelCase = tokenizer.convert_tokens_to_ids(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , [31, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9] ) _UpperCAmelCase = tokenizer.convert_ids_to_tokens(UpperCAmelCase ) self.assertListEqual( UpperCAmelCase , ['▁i', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '.'] , ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = AlbertTokenizer(UpperCAmelCase ) _UpperCAmelCase = tokenizer.encode('sequence builders' ) _UpperCAmelCase = tokenizer.encode('multi-sequence build' ) _UpperCAmelCase = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase ) _UpperCAmelCase = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase , UpperCAmelCase ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ] @slow def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = {'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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'input_ids': [[2, 2_1970, 13, 5, 6092, 167, 28, 7103, 2153, 673, 8, 7028, 1_2051, 18, 17, 7103, 2153, 673, 8, 3515, 1_8684, 8, 4461, 6, 1927, 297, 8, 1_2060, 2607, 18, 13, 5, 4461, 15, 1_0538, 38, 8, 135, 15, 822, 58, 15, 993, 1_0363, 15, 1460, 8005, 4461, 15, 993, 255, 2328, 9, 9, 9, 6, 26, 1112, 816, 3260, 13, 5, 103, 2377, 6, 17, 1112, 816, 2782, 13, 5, 103, 1_0641, 6, 29, 84, 2512, 2430, 782, 1_8684, 2761, 19, 808, 2430, 2556, 17, 855, 1480, 9477, 4091, 128, 1_1712, 15, 7103, 2153, 673, 17, 2_4883, 9990, 9, 3], [2, 1_1502, 25, 1006, 20, 782, 8, 1_1809, 855, 1732, 1_9393, 1_8667, 37, 367, 2_1018, 69, 1854, 34, 1_1860, 1_9124, 27, 156, 225, 17, 193, 4141, 19, 65, 9124, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 2231, 886, 2385, 1_7659, 84, 14, 1_6792, 1952, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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=UpperCAmelCase , model_name='albert-base-v2' , revision='6b6560eaf5ff2e250b00c50f380c5389a9c2d82e' , )
39
1
'''simple docstring''' from typing import Optional from urllib.parse import quote import huggingface_hub as hfh from packaging import version def SCREAMING_SNAKE_CASE__( _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : Optional[str] = None ) -> str: '''simple docstring''' if version.parse(hfh.__version__ ).release < version.parse("0.11.0" ).release: # old versions of hfh don't url-encode the file path UpperCamelCase__ = quote(_UpperCamelCase ) return hfh.hf_hub_url(_UpperCamelCase , _UpperCamelCase , repo_type="dataset" , revision=_UpperCamelCase )
356
'''simple docstring''' import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __lowercase: Optional[List[str]] = None __lowercase: List[Any] = "<" if sys.byteorder == "little" else ">" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __lowercase: Tuple = [ np.dtype("|b1"), np.dtype("|u1"), np.dtype("<u2"), np.dtype(">u2"), np.dtype("<i2"), np.dtype(">i2"), np.dtype("<u4"), np.dtype(">u4"), np.dtype("<i4"), np.dtype(">i4"), np.dtype("<f4"), np.dtype(">f4"), np.dtype("<f8"), np.dtype(">f8"), ] @dataclass class UpperCAmelCase : _lowerCamelCase : bool = True _lowerCamelCase : Optional[str] = None # Automatically constructed _lowerCamelCase : ClassVar[str] = "PIL.Image.Image" _lowerCamelCase : ClassVar[Any] = pa.struct({'bytes': pa.binary(), 'path': pa.string()}) _lowerCamelCase : str = field(default='Image' , init=SCREAMING_SNAKE_CASE__ , repr=SCREAMING_SNAKE_CASE__) def __call__( self : Union[str, Any] ): """simple docstring""" return self.pa_type def lowercase_ ( self : Optional[Any], a_ : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(a_, a_ ): UpperCamelCase__ = np.array(a_ ) if isinstance(a_, a_ ): return {"path": value, "bytes": None} elif isinstance(a_, a_ ): return {"path": None, "bytes": value} elif isinstance(a_, np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(a_ ) elif isinstance(a_, PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(a_ ) elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.' ) def lowercase_ ( self : Dict, a_ : dict, a_ : Dict=None ): """simple docstring""" if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead." ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support decoding images, please install 'Pillow'." ) if token_per_repo_id is None: UpperCamelCase__ = {} UpperCamelCase__ , UpperCamelCase__ = value["path"], value["bytes"] if bytes_ is None: if path is None: raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.' ) else: if is_local_path(a_ ): UpperCamelCase__ = PIL.Image.open(a_ ) else: UpperCamelCase__ = path.split("::" )[-1] try: UpperCamelCase__ = string_to_dict(a_, config.HUB_DATASETS_URL )["repo_id"] UpperCamelCase__ = token_per_repo_id.get(a_ ) except ValueError: UpperCamelCase__ = None with xopen(a_, "rb", use_auth_token=a_ ) as f: UpperCamelCase__ = BytesIO(f.read() ) UpperCamelCase__ = PIL.Image.open(bytes_ ) else: UpperCamelCase__ = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def lowercase_ ( self : List[str] ): """simple docstring""" from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def lowercase_ ( self : List[Any], a_ : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): """simple docstring""" if pa.types.is_string(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, storage], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([storage, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: UpperCamelCase__ = storage.field("bytes" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: UpperCamelCase__ = storage.field("path" ) else: UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCamelCase__ = pa.array( [encode_np_array(np.array(a_ ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()], type=pa.binary(), ) UpperCamelCase__ = pa.array([None] * len(a_ ), type=pa.string() ) UpperCamelCase__ = pa.StructArray.from_arrays( [bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def lowercase_ ( self : str, a_ : pa.StructArray ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(a_ : Dict ): with xopen(a_, "rb" ) as f: UpperCamelCase__ = f.read() return bytes_ UpperCamelCase__ = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ], type=pa.binary(), ) UpperCamelCase__ = pa.array( [os.path.basename(a_ ) if path is not None else None for path in storage.field("path" ).to_pylist()], type=pa.string(), ) UpperCamelCase__ = pa.StructArray.from_arrays([bytes_array, path_array], ["bytes", "path"], mask=bytes_array.is_null() ) return array_cast(a_, self.pa_type ) def SCREAMING_SNAKE_CASE__( ) -> List[str]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() UpperCamelCase__ = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> bytes: '''simple docstring''' UpperCamelCase__ = BytesIO() if image.format in list_image_compression_formats(): UpperCamelCase__ = image.format else: UpperCamelCase__ = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(_UpperCamelCase , format=_UpperCamelCase ) return buffer.getvalue() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : "PIL.Image.Image" ) -> dict: '''simple docstring''' if hasattr(_UpperCamelCase , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : np.ndarray ) -> dict: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) UpperCamelCase__ = array.dtype UpperCamelCase__ = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER UpperCamelCase__ = dtype.kind UpperCamelCase__ = dtype.itemsize UpperCamelCase__ = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCamelCase__ = np.dtype("|u1" ) if dtype_kind not in ["u", "i"]: raise TypeError( F'Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.' ) if dtype is not dest_dtype: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: UpperCamelCase__ = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: UpperCamelCase__ = dtype_byteorder + dtype_kind + str(_UpperCamelCase ) UpperCamelCase__ = np.dtype(_UpperCamelCase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F'Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}' ) UpperCamelCase__ = PIL.Image.fromarray(array.astype(_UpperCamelCase ) ) return {"path": None, "bytes": image_to_bytes(_UpperCamelCase )} def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ) -> List[dict]: '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: UpperCamelCase__ , UpperCamelCase__ = first_non_null_value(_UpperCamelCase ) if isinstance(_UpperCamelCase , _UpperCamelCase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_UpperCamelCase , np.ndarray ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] elif isinstance(_UpperCamelCase , PIL.Image.Image ): UpperCamelCase__ = no_op_if_value_is_null(_UpperCamelCase ) return [obj_to_image_dict_func(_UpperCamelCase ) for obj in objs] else: return objs else: return objs
31
0
'''simple docstring''' import os # Precomputes a list of the 100 first triangular numbers lowerCAmelCase :Union[str, Any] = [int(0.5 * n * (n + 1)) for n in range(1, 1_0_1)] def lowerCamelCase ( ): """simple docstring""" __magic_name__ : Optional[Any] = os.path.dirname(os.path.realpath(lowerCAmelCase ) ) __magic_name__ : Union[str, Any] = os.path.join(lowerCAmelCase , 'words.txt' ) __magic_name__ : List[str] = '' with open(lowerCAmelCase ) as f: __magic_name__ : Dict = f.readline() __magic_name__ : Dict = [word.strip('"' ) for word in words.strip('\r\n' ).split(',' )] __magic_name__ : Any = [ word for word in [sum(ord(lowerCAmelCase ) - 64 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(lowerCAmelCase ) if __name__ == "__main__": print(solution())
331
'''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 _lowerCamelCase ( lowercase__ ): '''simple docstring''' def __init__( self : List[Any] , _A : str , _A : str=13 , _A : Union[str, Any]=7 , _A : Tuple=True , _A : Dict=True , _A : List[str]=True , _A : Optional[int]=True , _A : Dict=99 , _A : Optional[Any]=32 , _A : Optional[int]=5 , _A : str=4 , _A : str=37 , _A : Tuple="gelu" , _A : Any=0.1 , _A : Dict=0.1 , _A : str=512 , _A : Tuple=16 , _A : str=2 , _A : int=0.02 , _A : int=False , _A : List[str]=True , _A : List[Any]="None" , _A : List[str]=3 , _A : Optional[Any]=4 , _A : Dict=None , ) -> Dict: __magic_name__ : Union[str, Any] = parent __magic_name__ : Any = batch_size __magic_name__ : Optional[int] = seq_length __magic_name__ : List[str] = is_training __magic_name__ : Optional[Any] = use_input_mask __magic_name__ : Dict = use_token_type_ids __magic_name__ : str = use_labels __magic_name__ : int = vocab_size __magic_name__ : List[Any] = hidden_size __magic_name__ : Dict = num_hidden_layers __magic_name__ : Dict = num_attention_heads __magic_name__ : Tuple = intermediate_size __magic_name__ : Any = hidden_act __magic_name__ : Union[str, Any] = hidden_dropout_prob __magic_name__ : Union[str, Any] = attention_probs_dropout_prob __magic_name__ : List[Any] = max_position_embeddings __magic_name__ : Any = type_vocab_size __magic_name__ : Union[str, Any] = type_sequence_label_size __magic_name__ : Union[str, Any] = initializer_range __magic_name__ : str = num_labels __magic_name__ : Tuple = num_choices __magic_name__ : Any = relative_attention __magic_name__ : str = position_biased_input __magic_name__ : str = pos_att_type __magic_name__ : Union[str, Any] = scope def __lowerCAmelCase ( self : Optional[int] ) -> Union[str, Any]: __magic_name__ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __magic_name__ : List[Any] = None if self.use_input_mask: __magic_name__ : List[str] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) __magic_name__ : int = None if self.use_token_type_ids: __magic_name__ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __magic_name__ : List[str] = None __magic_name__ : Tuple = None __magic_name__ : Union[str, Any] = None if self.use_labels: __magic_name__ : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __magic_name__ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __magic_name__ : Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) __magic_name__ : Any = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self : Tuple ) -> Optional[Any]: 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 __lowerCAmelCase ( self : str ) -> Optional[Any]: __magic_name__ : List[Any] = self.get_config() __magic_name__ : Union[str, Any] = 300 return config def __lowerCAmelCase ( self : int , _A : Dict ) -> Tuple: self.parent.assertListEqual(list(result.loss.size() ) , [] ) def __lowerCAmelCase ( self : Any , _A : Optional[int] , _A : Optional[Any] , _A : Optional[int] , _A : Optional[int] , _A : Any , _A : str , _A : List[Any] ) -> List[Any]: __magic_name__ : Dict = DebertaModel(config=_A ) model.to(_A ) model.eval() __magic_name__ : Optional[Any] = model(_A , attention_mask=_A , token_type_ids=_A )[0] __magic_name__ : Optional[int] = model(_A , token_type_ids=_A )[0] __magic_name__ : List[str] = model(_A )[0] self.parent.assertListEqual(list(sequence_output.size() ) , [self.batch_size, self.seq_length, self.hidden_size] ) def __lowerCAmelCase ( self : Any , _A : Union[str, Any] , _A : Optional[Any] , _A : Dict , _A : Optional[Any] , _A : Dict , _A : Optional[Any] , _A : Optional[int] ) -> Dict: __magic_name__ : List[str] = DebertaForMaskedLM(config=_A ) model.to(_A ) model.eval() __magic_name__ : List[str] = 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 __lowerCAmelCase ( self : str , _A : Union[str, Any] , _A : List[str] , _A : Optional[int] , _A : Optional[int] , _A : str , _A : Union[str, Any] , _A : Any ) -> Union[str, Any]: __magic_name__ : Optional[int] = self.num_labels __magic_name__ : Optional[Any] = DebertaForSequenceClassification(_A ) model.to(_A ) model.eval() __magic_name__ : 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 __lowerCAmelCase ( self : Tuple , _A : str , _A : str , _A : int , _A : str , _A : int , _A : Optional[int] , _A : List[str] ) -> Optional[int]: __magic_name__ : str = self.num_labels __magic_name__ : int = DebertaForTokenClassification(config=_A ) model.to(_A ) model.eval() __magic_name__ : List[str] = 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 __lowerCAmelCase ( self : Optional[Any] , _A : str , _A : Tuple , _A : Optional[int] , _A : Any , _A : Optional[int] , _A : Dict , _A : Union[str, Any] ) -> List[Any]: __magic_name__ : int = DebertaForQuestionAnswering(config=_A ) model.to(_A ) model.eval() __magic_name__ : Optional[int] = 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 __lowerCAmelCase ( self : Optional[int] ) -> List[Any]: __magic_name__ : Union[str, Any] = self.prepare_config_and_inputs() ( ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ) : int = config_and_inputs __magic_name__ : Optional[Any] = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class _lowerCamelCase ( lowercase__ , lowercase__ , unittest.TestCase ): '''simple docstring''' A_ : List[Any] = ( ( DebertaModel, DebertaForMaskedLM, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaForQuestionAnswering, ) if is_torch_available() else () ) A_ : Tuple = ( { """feature-extraction""": DebertaModel, """fill-mask""": DebertaForMaskedLM, """question-answering""": DebertaForQuestionAnswering, """text-classification""": DebertaForSequenceClassification, """token-classification""": DebertaForTokenClassification, """zero-shot""": DebertaForSequenceClassification, } if is_torch_available() else {} ) A_ : Union[str, Any] = True A_ : Any = False A_ : Dict = False A_ : str = False A_ : Dict = False def __lowerCAmelCase ( self : List[str] ) -> Optional[Any]: __magic_name__ : List[str] = DebertaModelTester(self ) __magic_name__ : Tuple = ConfigTester(self , config_class=_A , hidden_size=37 ) def __lowerCAmelCase ( self : List[str] ) -> Tuple: self.config_tester.run_common_tests() def __lowerCAmelCase ( self : Optional[Any] ) -> Optional[int]: __magic_name__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*_A ) def __lowerCAmelCase ( self : Union[str, Any] ) -> Optional[int]: __magic_name__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*_A ) def __lowerCAmelCase ( self : Any ) -> str: __magic_name__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*_A ) def __lowerCAmelCase ( self : Any ) -> Tuple: __magic_name__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*_A ) def __lowerCAmelCase ( self : str ) -> List[Any]: __magic_name__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*_A ) @slow def __lowerCAmelCase ( self : str ) -> Optional[Any]: for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __magic_name__ : int = DebertaModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @require_torch @require_sentencepiece @require_tokenizers class _lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @unittest.skip(reason='Model not available yet' ) def __lowerCAmelCase ( self : List[Any] ) -> Optional[int]: pass @slow def __lowerCAmelCase ( self : Dict ) -> Tuple: __magic_name__ : int = DebertaModel.from_pretrained('microsoft/deberta-base' ) __magic_name__ : List[Any] = torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]] ) __magic_name__ : Union[str, Any] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): __magic_name__ : Optional[int] = model(_A , attention_mask=_A )[0] # compare the actual values for a slice. __magic_name__ : Tuple = torch.tensor( [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _A , atol=1E-4 ) , F'{output[:, 1:4, 1:4]}' )
331
1
'''simple docstring''' from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class lowerCAmelCase_ : '''simple docstring''' lowerCAmelCase_ : List[Any] = XGLMConfig lowerCAmelCase_ : Any = {} lowerCAmelCase_ : Dict = """gelu""" def __init__( self : Optional[int] , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : str=14 , _UpperCAmelCase : List[Any]=7 , _UpperCAmelCase : Dict=True , _UpperCAmelCase : Union[str, Any]=True , _UpperCAmelCase : Tuple=True , _UpperCAmelCase : Optional[int]=99 , _UpperCAmelCase : List[Any]=32 , _UpperCAmelCase : Dict=2 , _UpperCAmelCase : List[str]=4 , _UpperCAmelCase : Optional[Any]=37 , _UpperCAmelCase : List[Any]="gelu" , _UpperCAmelCase : List[Any]=0.1 , _UpperCAmelCase : Dict=0.1 , _UpperCAmelCase : Dict=5_12 , _UpperCAmelCase : Union[str, Any]=0.02 , ): """simple docstring""" UpperCAmelCase__ = parent UpperCAmelCase__ = batch_size UpperCAmelCase__ = seq_length UpperCAmelCase__ = is_training UpperCAmelCase__ = use_input_mask UpperCAmelCase__ = use_labels UpperCAmelCase__ = vocab_size UpperCAmelCase__ = d_model UpperCAmelCase__ = num_hidden_layers UpperCAmelCase__ = num_attention_heads UpperCAmelCase__ = ffn_dim UpperCAmelCase__ = activation_function UpperCAmelCase__ = activation_dropout UpperCAmelCase__ = attention_dropout UpperCAmelCase__ = max_position_embeddings UpperCAmelCase__ = initializer_range UpperCAmelCase__ = None UpperCAmelCase__ = 0 UpperCAmelCase__ = 2 UpperCAmelCase__ = 1 def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return XGLMConfig.from_pretrained("""facebook/xglm-564M""" ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" UpperCAmelCase__ = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) UpperCAmelCase__ = None if self.use_input_mask: UpperCAmelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase__ = self.get_config() UpperCAmelCase__ = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=_UpperCAmelCase , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=_UpperCAmelCase , ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" UpperCAmelCase__ = self.prepare_config_and_inputs() ( ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ) = config_and_inputs UpperCAmelCase__ = { """input_ids""": input_ids, """head_mask""": head_mask, } return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): '''simple docstring''' lowerCAmelCase_ : Dict = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () lowerCAmelCase_ : Any = (TFXGLMForCausalLM,) if is_tf_available() else () lowerCAmelCase_ : List[str] = ( {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {} ) lowerCAmelCase_ : Union[str, Any] = False lowerCAmelCase_ : Tuple = False lowerCAmelCase_ : List[Any] = False def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" UpperCAmelCase__ = TFXGLMModelTester(self ) UpperCAmelCase__ = ConfigTester(self , config_class=_UpperCAmelCase , n_embd=37 ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): """simple docstring""" self.config_tester.run_common_tests() @slow def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase__ = TFXGLMModel.from_pretrained(_UpperCAmelCase ) self.assertIsNotNone(_UpperCAmelCase ) @unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor.""" ) def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , _UpperCAmelCase : str=True ): """simple docstring""" UpperCAmelCase__ = TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M""" ) UpperCAmelCase__ = tf.convert_to_tensor([[2, 2_68, 98_65]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off UpperCAmelCase__ = [2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81] # fmt: on UpperCAmelCase__ = model.generate(_UpperCAmelCase , do_sample=_UpperCAmelCase , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , _UpperCAmelCase ) @slow def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" UpperCAmelCase__ = XGLMTokenizer.from_pretrained("""facebook/xglm-564M""" ) UpperCAmelCase__ = TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M""" ) tf.random.set_seed(0 ) UpperCAmelCase__ = tokenizer("""Today is a nice day and""" , return_tensors="""tf""" ) UpperCAmelCase__ = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(""":/CPU:0""" ): UpperCAmelCase__ = model.generate(_UpperCAmelCase , do_sample=_UpperCAmelCase , seed=[7, 0] ) UpperCAmelCase__ = tokenizer.decode(output_ids[0] , skip_special_tokens=_UpperCAmelCase ) UpperCAmelCase__ = ( """Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due""" ) self.assertEqual(_UpperCAmelCase , _UpperCAmelCase ) @slow def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" UpperCAmelCase__ = TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M""" ) UpperCAmelCase__ = XGLMTokenizer.from_pretrained("""facebook/xglm-564M""" ) UpperCAmelCase__ = """left""" # use different length sentences to test batching UpperCAmelCase__ = [ """This is an extremelly long sentence that only exists to test the ability of the model to cope with """ """left-padding, such as in batched generation. The output for the sequence below should be the same """ """regardless of whether left padding is applied or not. When""", """Hello, my dog is a little""", ] UpperCAmelCase__ = tokenizer(_UpperCAmelCase , return_tensors="""tf""" , padding=_UpperCAmelCase ) UpperCAmelCase__ = inputs["""input_ids"""] UpperCAmelCase__ = model.generate(input_ids=_UpperCAmelCase , attention_mask=inputs["""attention_mask"""] , max_new_tokens=12 ) UpperCAmelCase__ = tokenizer(sentences[0] , return_tensors="""tf""" ).input_ids UpperCAmelCase__ = model.generate(input_ids=_UpperCAmelCase , max_new_tokens=12 ) UpperCAmelCase__ = tokenizer(sentences[1] , return_tensors="""tf""" ).input_ids UpperCAmelCase__ = model.generate(input_ids=_UpperCAmelCase , max_new_tokens=12 ) UpperCAmelCase__ = tokenizer.batch_decode(_UpperCAmelCase , skip_special_tokens=_UpperCAmelCase ) UpperCAmelCase__ = tokenizer.decode(output_non_padded[0] , skip_special_tokens=_UpperCAmelCase ) UpperCAmelCase__ = tokenizer.decode(output_padded[0] , skip_special_tokens=_UpperCAmelCase ) UpperCAmelCase__ = [ """This is an extremelly long sentence that only exists to test the ability of the model to cope with """ """left-padding, such as in batched generation. The output for the sequence below should be the same """ """regardless of whether left padding is applied or not. When left padding is applied, the sequence will be """ """a single""", """Hello, my dog is a little bit of a shy one, but he is very friendly""", ] self.assertListEqual(_UpperCAmelCase , _UpperCAmelCase ) self.assertListEqual(_UpperCAmelCase , [non_padded_sentence, padded_sentence] )
367
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : List[str] = """yolos""" def __init__( self : str , _UpperCAmelCase : int=7_68 , _UpperCAmelCase : Dict=12 , _UpperCAmelCase : Union[str, Any]=12 , _UpperCAmelCase : Tuple=30_72 , _UpperCAmelCase : int="gelu" , _UpperCAmelCase : Optional[Any]=0.0 , _UpperCAmelCase : List[str]=0.0 , _UpperCAmelCase : Dict=0.02 , _UpperCAmelCase : Optional[Any]=1E-12 , _UpperCAmelCase : Tuple=[5_12, 8_64] , _UpperCAmelCase : str=16 , _UpperCAmelCase : Optional[int]=3 , _UpperCAmelCase : Tuple=True , _UpperCAmelCase : int=1_00 , _UpperCAmelCase : List[str]=True , _UpperCAmelCase : Dict=False , _UpperCAmelCase : Dict=1 , _UpperCAmelCase : Union[str, Any]=5 , _UpperCAmelCase : Dict=2 , _UpperCAmelCase : int=5 , _UpperCAmelCase : str=2 , _UpperCAmelCase : Tuple=0.1 , **_UpperCAmelCase : List[Any] , ): """simple docstring""" super().__init__(**_UpperCAmelCase ) 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__ = initializer_range UpperCAmelCase__ = layer_norm_eps UpperCAmelCase__ = image_size UpperCAmelCase__ = patch_size UpperCAmelCase__ = num_channels UpperCAmelCase__ = qkv_bias UpperCAmelCase__ = num_detection_tokens UpperCAmelCase__ = use_mid_position_embeddings UpperCAmelCase__ = auxiliary_loss # Hungarian matcher UpperCAmelCase__ = class_cost UpperCAmelCase__ = bbox_cost UpperCAmelCase__ = giou_cost # Loss coefficients UpperCAmelCase__ = bbox_loss_coefficient UpperCAmelCase__ = giou_loss_coefficient UpperCAmelCase__ = eos_coefficient class lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : List[str] = version.parse("""1.11""" ) @property def SCREAMING_SNAKE_CASE__ ( self : int ): """simple docstring""" return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def SCREAMING_SNAKE_CASE__ ( self : List[str] ): """simple docstring""" return 1E-4 @property def SCREAMING_SNAKE_CASE__ ( self : str ): """simple docstring""" return 12
61
0
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig snake_case_ = logging.get_logger(__name__) # General docstring snake_case_ = '''RegNetConfig''' # Base docstring snake_case_ = '''facebook/regnet-y-040''' snake_case_ = [1, 1_088, 7, 7] # Image classification docstring snake_case_ = '''facebook/regnet-y-040''' snake_case_ = '''tabby, tabby cat''' snake_case_ = [ '''facebook/regnet-y-040''', # See all regnet models at https://huggingface.co/models?filter=regnet ] class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , a = 3 , a = 1 , a = 1 , a = "relu" , **a , ): super().__init__(**a) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb lowercase__ : Optional[int] = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2) lowercase__ : Union[str, Any] = tf.keras.layers.ConvaD( filters=a , kernel_size=a , strides=a , padding='VALID' , groups=a , use_bias=a , name='convolution' , ) lowercase__ : Optional[Any] = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization') lowercase__ : List[Any] = ACTaFN[activation] if activation is not None else tf.identity def snake_case_ ( self , a): lowercase__ : Optional[Any] = self.convolution(self.padding(a)) lowercase__ : Dict = self.normalization(a) lowercase__ : Optional[Any] = self.activation(a) return hidden_state class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , **a): super().__init__(**a) lowercase__ : Union[str, Any] = config.num_channels lowercase__ : Dict = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='embedder' , ) def snake_case_ ( self , a): lowercase__ : str = shape_list(a)[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.') # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) lowercase__ : Optional[Any] = tf.transpose(a , perm=(0, 2, 3, 1)) lowercase__ : int = self.embedder(a) return hidden_state class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , a = 2 , **a): super().__init__(**a) lowercase__ : List[Any] = tf.keras.layers.ConvaD( filters=a , kernel_size=1 , strides=a , use_bias=a , name='convolution') lowercase__ : Optional[int] = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization') def snake_case_ ( self , a , a = False): return self.normalization(self.convolution(a) , training=a) class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , a , **a): super().__init__(**a) lowercase__ : Tuple = tf.keras.layers.GlobalAveragePoolingaD(keepdims=a , name='pooler') lowercase__ : List[str] = [ tf.keras.layers.ConvaD(filters=a , kernel_size=1 , activation='relu' , name='attention.0'), tf.keras.layers.ConvaD(filters=a , kernel_size=1 , activation='sigmoid' , name='attention.2'), ] def snake_case_ ( self , a): # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] lowercase__ : Union[str, Any] = self.pooler(a) for layer_module in self.attention: lowercase__ : int = layer_module(a) lowercase__ : Dict = hidden_state * pooled return hidden_state class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , a , a , a = 1 , **a): super().__init__(**a) lowercase__ : List[Any] = in_channels != out_channels or stride != 1 lowercase__ : Union[str, Any] = max(1 , out_channels // config.groups_width) lowercase__ : int = ( TFRegNetShortCut(a , stride=a , name='shortcut') if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut') ) # `self.layers` instead of `self.layer` because that is a reserved argument. lowercase__ : int = [ TFRegNetConvLayer(a , kernel_size=1 , activation=config.hidden_act , name='layer.0'), TFRegNetConvLayer( a , stride=a , groups=a , activation=config.hidden_act , name='layer.1'), TFRegNetConvLayer(a , kernel_size=1 , activation=a , name='layer.2'), ] lowercase__ : List[Any] = ACTaFN[config.hidden_act] def snake_case_ ( self , a): lowercase__ : Any = hidden_state for layer_module in self.layers: lowercase__ : Optional[Any] = layer_module(a) lowercase__ : List[str] = self.shortcut(a) hidden_state += residual lowercase__ : List[Any] = self.activation(a) return hidden_state class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , a , a , a = 1 , **a): super().__init__(**a) lowercase__ : Dict = in_channels != out_channels or stride != 1 lowercase__ : str = max(1 , out_channels // config.groups_width) lowercase__ : List[Any] = ( TFRegNetShortCut(a , stride=a , name='shortcut') if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut') ) lowercase__ : Optional[Any] = [ TFRegNetConvLayer(a , kernel_size=1 , activation=config.hidden_act , name='layer.0'), TFRegNetConvLayer( a , stride=a , groups=a , activation=config.hidden_act , name='layer.1'), TFRegNetSELayer(a , reduced_channels=int(round(in_channels / 4)) , name='layer.2'), TFRegNetConvLayer(a , kernel_size=1 , activation=a , name='layer.3'), ] lowercase__ : Union[str, Any] = ACTaFN[config.hidden_act] def snake_case_ ( self , a): lowercase__ : Optional[Any] = hidden_state for layer_module in self.layers: lowercase__ : Optional[int] = layer_module(a) lowercase__ : Tuple = self.shortcut(a) hidden_state += residual lowercase__ : Optional[int] = self.activation(a) return hidden_state class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , a , a , a = 2 , a = 2 , **a): super().__init__(**a) lowercase__ : Any = TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer lowercase__ : Tuple = [ # downsampling is done in the first layer with stride of 2 layer(a , a , a , stride=a , name='layers.0'), *[layer(a , a , a , name=f"""layers.{i+1}""") for i in range(depth - 1)], ] def snake_case_ ( self , a): for layer_module in self.layers: lowercase__ : Union[str, Any] = layer_module(a) return hidden_state class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): def __init__( self , a , **a): super().__init__(**a) lowercase__ : str = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( a , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='stages.0' , )) lowercase__ : Union[str, Any] = zip(config.hidden_sizes , config.hidden_sizes[1:]) for i, ((in_channels, out_channels), depth) in enumerate(zip(a , config.depths[1:])): self.stages.append(TFRegNetStage(a , a , a , depth=a , name=f"""stages.{i+1}""")) def snake_case_ ( self , a , a = False , a = True): lowercase__ : Tuple = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: lowercase__ : int = hidden_states + (hidden_state,) lowercase__ : str = stage_module(a) if output_hidden_states: lowercase__ : Dict = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None) return TFBaseModelOutputWithNoAttention(last_hidden_state=a , hidden_states=a) @keras_serializable class SCREAMING_SNAKE_CASE__ (tf.keras.layers.Layer ): __lowerCamelCase : List[str] = RegNetConfig def __init__( self , a , **a): super().__init__(**a) lowercase__ : str = config lowercase__ : List[str] = TFRegNetEmbeddings(a , name='embedder') lowercase__ : List[str] = TFRegNetEncoder(a , name='encoder') lowercase__ : List[str] = tf.keras.layers.GlobalAveragePoolingaD(keepdims=a , name='pooler') @unpack_inputs def snake_case_ ( self , a , a = None , a = None , a = False , ): lowercase__ : Tuple = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase__ : Optional[Any] = return_dict if return_dict is not None else self.config.use_return_dict lowercase__ : str = self.embedder(a , training=a) lowercase__ : Optional[Any] = self.encoder( a , output_hidden_states=a , return_dict=a , training=a) lowercase__ : Optional[Any] = encoder_outputs[0] lowercase__ : str = self.pooler(a) # Change to NCHW output format have uniformity in the modules lowercase__ : Optional[Any] = tf.transpose(a , perm=(0, 3, 1, 2)) lowercase__ : str = tf.transpose(a , perm=(0, 3, 1, 2)) # Change the other hidden state outputs to NCHW as well if output_hidden_states: lowercase__ : List[str] = tuple([tf.transpose(a , perm=(0, 3, 1, 2)) for h in encoder_outputs[1]]) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=a , pooler_output=a , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class SCREAMING_SNAKE_CASE__ (__snake_case ): __lowerCamelCase : Dict = RegNetConfig __lowerCamelCase : str = """regnet""" __lowerCamelCase : Any = """pixel_values""" @property def snake_case_ ( self): return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa)} snake_case_ = r''' Parameters: This model is a Tensorflow [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and behavior. config ([`RegNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights. ''' snake_case_ = r''' Args: pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConveNextImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. ''' @add_start_docstrings( """The bare RegNet model outputting raw features without any specific head on top.""" , __snake_case , ) class SCREAMING_SNAKE_CASE__ (__snake_case ): def __init__( self , a , *a , **a): super().__init__(a , *a , **a) lowercase__ : Any = TFRegNetMainLayer(a , name='regnet') @unpack_inputs @add_start_docstrings_to_model_forward(a) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=a , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def snake_case_ ( self , a , a = None , a = None , a=False , ): lowercase__ : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase__ : Dict = return_dict if return_dict is not None else self.config.use_return_dict lowercase__ : List[Any] = self.regnet( pixel_values=a , output_hidden_states=a , return_dict=a , training=a , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( """ RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , __snake_case , ) class SCREAMING_SNAKE_CASE__ (__snake_case , __snake_case ): def __init__( self , a , *a , **a): super().__init__(a , *a , **a) lowercase__ : List[str] = config.num_labels lowercase__ : Union[str, Any] = TFRegNetMainLayer(a , name='regnet') # classification head lowercase__ : Optional[Any] = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='classifier.1') if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(a) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=a , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def snake_case_ ( self , a = None , a = None , a = None , a = None , a=False , ): lowercase__ : Union[str, Any] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase__ : Optional[int] = return_dict if return_dict is not None else self.config.use_return_dict lowercase__ : Optional[Any] = self.regnet( a , output_hidden_states=a , return_dict=a , training=a) lowercase__ : int = outputs.pooler_output if return_dict else outputs[1] lowercase__ : Dict = self.classifier[0](a) lowercase__ : List[Any] = self.classifier[1](a) lowercase__ : str = None if labels is None else self.hf_compute_loss(labels=a , logits=a) if not return_dict: lowercase__ : Dict = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=a , logits=a , hidden_states=outputs.hidden_states)
214
from __future__ import annotations from collections.abc import Callable from typing import Generic, TypeVar snake_case_ = TypeVar('''T''') snake_case_ = TypeVar('''U''') class SCREAMING_SNAKE_CASE__ (Generic[T, U] ): def __init__( self , a , a): lowercase__ : List[Any] = key lowercase__ : List[Any] = val lowercase__ : DoubleLinkedListNode[T, U] | None = None lowercase__ : DoubleLinkedListNode[T, U] | None = None def __repr__( self): return ( f"""Node: key: {self.key}, val: {self.val}, """ f"""has next: {bool(self.next)}, has prev: {bool(self.prev)}""" ) class SCREAMING_SNAKE_CASE__ (Generic[T, U] ): def __init__( self): lowercase__ : DoubleLinkedListNode[T, U] = DoubleLinkedListNode(a , a) lowercase__ : DoubleLinkedListNode[T, U] = DoubleLinkedListNode(a , a) lowercase__ , lowercase__ : Union[str, Any] = self.rear, self.head def __repr__( self): lowercase__ : Any = ['DoubleLinkedList'] lowercase__ : List[str] = self.head while node.next is not None: rep.append(str(a)) lowercase__ : Tuple = node.next rep.append(str(self.rear)) return ",\n ".join(a) def snake_case_ ( self , a): lowercase__ : Optional[Any] = self.rear.prev # All nodes other than self.head are guaranteed to have non-None previous assert previous is not None lowercase__ : Dict = node lowercase__ : int = previous lowercase__ : Union[str, Any] = node lowercase__ : Optional[int] = self.rear def snake_case_ ( self , a): if node.prev is None or node.next is None: return None lowercase__ : Union[str, Any] = node.next lowercase__ : Tuple = node.prev lowercase__ : Union[str, Any] = None lowercase__ : List[Any] = None return node class SCREAMING_SNAKE_CASE__ (Generic[T, U] ): __lowerCamelCase : dict[Callable[[T], U], LRUCache[T, U]] = {} def __init__( self , a): lowercase__ : DoubleLinkedList[T, U] = DoubleLinkedList() lowercase__ : Optional[Any] = capacity lowercase__ : Union[str, Any] = 0 lowercase__ : Tuple = 0 lowercase__ : int = 0 lowercase__ : dict[T, DoubleLinkedListNode[T, U]] = {} def __repr__( self): return ( f"""CacheInfo(hits={self.hits}, misses={self.miss}, """ f"""capacity={self.capacity}, current size={self.num_keys})""" ) def __contains__( self , a): return key in self.cache def snake_case_ ( self , a): # Note: pythonic interface would throw KeyError rather than return None if key in self.cache: self.hits += 1 lowercase__ : DoubleLinkedListNode[T, U] = self.cache[key] lowercase__ : str = self.list.remove(self.cache[key]) assert node == value_node # node is guaranteed not None because it is in self.cache assert node is not None self.list.add(a) return node.val self.miss += 1 return None def snake_case_ ( self , a , a): if key not in self.cache: if self.num_keys >= self.capacity: # delete first node (oldest) when over capacity lowercase__ : Optional[int] = self.list.head.next # guaranteed to have a non-None first node when num_keys > 0 # explain to type checker via assertions assert first_node is not None assert first_node.key is not None assert ( self.list.remove(a) is not None ) # node guaranteed to be in list assert node.key is not None del self.cache[first_node.key] self.num_keys -= 1 lowercase__ : Optional[Any] = DoubleLinkedListNode(a , a) self.list.add(self.cache[key]) self.num_keys += 1 else: # bump node to the end of the list, update value lowercase__ : Any = self.list.remove(self.cache[key]) assert node is not None # node guaranteed to be in list lowercase__ : Union[str, Any] = value self.list.add(a) @classmethod def snake_case_ ( cls , a = 128): def cache_decorator_inner(a) -> Callable[..., U]: def cache_decorator_wrapper(*a) -> U: if func not in cls.decorator_function_to_instance_map: lowercase__ : Dict = LRUCache(a) lowercase__ : str = cls.decorator_function_to_instance_map[func].get(args[0]) if result is None: lowercase__ : str = func(*a) cls.decorator_function_to_instance_map[func].put(args[0] , a) return result def cache_info() -> LRUCache[T, U]: return cls.decorator_function_to_instance_map[func] setattr(a , 'cache_info' , a) # noqa: B010 return cache_decorator_wrapper return cache_decorator_inner if __name__ == "__main__": import doctest doctest.testmod()
214
1
import json import os import re import sys import urllib.request import requests from bsa import BeautifulSoup _lowerCamelCase : Optional[Any] = { '''User-Agent''': '''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36''' ''' (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582''' } def a_ ( __lowercase : str = "dhaka" , __lowercase : Union[str, Any] = 5 ) -> int: _snake_case = min(__snake_case , 50 ) # Prevent abuse! _snake_case = { 'q': query, 'tbm': 'isch', 'hl': 'en', 'ijn': '0', } _snake_case = requests.get('https://www.google.com/search' , params=__snake_case , headers=__snake_case ) _snake_case = BeautifulSoup(html.text , 'html.parser' ) _snake_case = ''.join( re.findall(r'AF_initDataCallback\(([^<]+)\);' , str(soup.select('script' ) ) ) ) _snake_case = json.dumps(__snake_case ) _snake_case = json.loads(__snake_case ) _snake_case = re.findall( r'\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\",' , __snake_case , ) if not matched_google_image_data: return 0 _snake_case = re.sub( r'\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]' , '' , str(__snake_case ) , ) _snake_case = re.findall( r'(?:\'|,),\[\"(https:|http.*?)\",\d+,\d+\]' , __snake_case , ) for index, fixed_full_res_image in enumerate(__snake_case ): if index >= max_images: return index _snake_case = bytes(__snake_case , 'ascii' ).decode( 'unicode-escape' ) _snake_case = bytes(__snake_case , 'ascii' ).decode( 'unicode-escape' ) _snake_case = urllib.request.build_opener() _snake_case = [ ( 'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' ' (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582', ) ] urllib.request.install_opener(__snake_case ) _snake_case = f'''query_{query.replace(' ' , '_' )}''' if not os.path.exists(__snake_case ): os.makedirs(__snake_case ) urllib.request.urlretrieve( # noqa: S310 __snake_case , f'''{path_name}/original_size_img_{index}.jpg''' ) return index if __name__ == "__main__": try: _lowerCamelCase : Union[str, Any] = download_images_from_google_query(sys.argv[1]) print(F'{image_count} images were downloaded to disk.') except IndexError: print('''Please provide a search term.''') raise
362
import logging import os import sys from pathlib import Path from unittest.mock import patch from parameterized import parameterized from run_eval import run_generate from run_eval_search import run_search from transformers.testing_utils import CaptureStdout, TestCasePlus, slow from utils import ROUGE_KEYS logging.basicConfig(level=logging.DEBUG) _lowerCamelCase : Union[str, Any] = logging.getLogger() def a_ ( __lowercase : Path , __lowercase : list ) -> Tuple: _snake_case = '\n'.join(__lowercase ) Path(__lowercase ).open('w' ).writelines(__lowercase ) _lowerCamelCase : Any = '''patrickvonplaten/t5-tiny-random''' _lowerCamelCase : List[Any] = '''sshleifer/bart-tiny-random''' _lowerCamelCase : List[Any] = '''sshleifer/tiny-mbart''' _lowerCamelCase : Union[str, Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) logging.disable(logging.CRITICAL) # remove noisy download output from tracebacks class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase ): '''simple docstring''' def A ( self : Optional[int] , lowercase : int ): '''simple docstring''' _snake_case = Path(self.get_auto_remove_tmp_dir() ) / 'utest_input.source' _snake_case = input_file_name.parent / 'utest_output.txt' assert not output_file_name.exists() _snake_case = [' New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County.'] _dump_articles(lowercase , lowercase ) _snake_case = str(Path(self.get_auto_remove_tmp_dir() ) / 'scores.json' ) _snake_case = 'translation_en_to_de' if model == T5_TINY else 'summarization' _snake_case = f''' run_eval_search.py {model} {input_file_name} {output_file_name} --score_path {score_path} --task {task} --num_beams 2 --length_penalty 2.0 '''.split() with patch.object(lowercase , 'argv' , lowercase ): run_generate() assert Path(lowercase ).exists() # os.remove(Path(output_file_name)) def A ( self : List[Any] ): '''simple docstring''' self.run_eval_tester(lowercase ) @parameterized.expand([BART_TINY, MBART_TINY] ) @slow def A ( self : Any , lowercase : int ): '''simple docstring''' self.run_eval_tester(lowercase ) @parameterized.expand([T5_TINY, MBART_TINY] ) @slow def A ( self : Any , lowercase : str ): '''simple docstring''' _snake_case = Path(self.get_auto_remove_tmp_dir() ) / 'utest_input.source' _snake_case = input_file_name.parent / 'utest_output.txt' assert not output_file_name.exists() _snake_case = { 'en': ['Machine learning is great, isn\'t it?', 'I like to eat bananas', 'Tomorrow is another great day!'], 'de': [ 'Maschinelles Lernen ist großartig, oder?', 'Ich esse gerne Bananen', 'Morgen ist wieder ein toller Tag!', ], } _snake_case = Path(self.get_auto_remove_tmp_dir() ) _snake_case = str(tmp_dir / 'scores.json' ) _snake_case = str(tmp_dir / 'val.target' ) _dump_articles(lowercase , text['en'] ) _dump_articles(lowercase , text['de'] ) _snake_case = 'translation_en_to_de' if model == T5_TINY else 'summarization' _snake_case = f''' run_eval_search.py {model} {str(lowercase )} {str(lowercase )} --score_path {score_path} --reference_path {reference_path} --task {task} '''.split() testargs.extend(['--search', 'num_beams=1:2 length_penalty=0.9:1.0'] ) with patch.object(lowercase , 'argv' , lowercase ): with CaptureStdout() as cs: run_search() _snake_case = [' num_beams | length_penalty', model, 'Best score args'] _snake_case = ['Info'] if "translation" in task: expected_strings.append('bleu' ) else: expected_strings.extend(lowercase ) for w in expected_strings: assert w in cs.out for w in un_expected_strings: assert w not in cs.out assert Path(lowercase ).exists() os.remove(Path(lowercase ) )
130
0
def __lowercase ( _SCREAMING_SNAKE_CASE ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = len(_A ) SCREAMING_SNAKE_CASE = sum(_A ) SCREAMING_SNAKE_CASE = [[False for x in range(s + 1 )] for y in range(n + 1 )] for i in range(1 , n + 1 ): SCREAMING_SNAKE_CASE = True for i in range(1 , s + 1 ): SCREAMING_SNAKE_CASE = False for i in range(1 , n + 1 ): for j in range(1 , s + 1 ): SCREAMING_SNAKE_CASE = dp[i][j - 1] if arr[i - 1] <= j: SCREAMING_SNAKE_CASE = dp[i][j] or dp[i - 1][j - arr[i - 1]] for j in range(int(s / 2 ) , -1 , -1 ): if dp[n][j] is True: SCREAMING_SNAKE_CASE = s - 2 * j break return diff
296
'''simple docstring''' def lowerCamelCase__ ( _A , _A ): while second != 0: a : Union[str, Any] = first & second first ^= second a : Tuple = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase: Optional[int] = int(input('Enter the first number: ').strip()) lowerCAmelCase: Union[str, Any] = int(input('Enter the second number: ').strip()) print(F"{add(first, second) = }")
297
0
import argparse import json import os from collections import OrderedDict import numpy as np import tensorflow as tf import torch def __lowerCamelCase ( __a :Optional[Any] ) -> int: """simple docstring""" A__ = os.path.join(args.tf_model_dir , """parameters.json""" ) A__ = json.loads(open(lowerCamelCase__ ).read() ) if not params: raise ValueError( F'It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file.' ) if not args.output.endswith(""".pt""" ): A__ = args.output + """.pt""" A__ = OrderedDict() with tf.device("""/CPU:0""" ): A__ = tf.train.load_checkpoint(args.tf_model_dir ) A__ = reader.get_variable_to_shape_map() for key_name in shapes.keys(): A__ = reader.get_tensor(lowerCamelCase__ ).astype(np.floataa ) if key_name.endswith("""/adam_m""" ) or key_name.endswith("""/adam_v""" ): continue if key_name.startswith("""pasts/""" ): if key_name.startswith("""pasts/mlp""" ): A__ = int(key_name[9] ) elif key_name.startswith("""pasts/out""" ): A__ = 8 A__ = """model.sqout.%d.weight""" % (player * 2) # enter to nn.Sequencial with Tanh, so 2 at a time A__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ = torch.tensor(lowerCamelCase__ ) elif key_name.startswith("""model/moe""" ): A__ = int(key_name[9:].split("""/""" )[0] ) if key_name.endswith("""/switch_gating/kernel""" ): A__ = """model.blocks.%d.feed_forward.mlp.router.classifier.weight""" % player A__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/softmlp/kernel""" ): A__ = """model.blocks.%d.feed_forward.soft_bypass_mlp.weight""" % player A__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/wo/kernel""" ) or key_name.endswith("""/wi/kernel""" ): A__ = key_name[-9:-7] for i in range(1_6 ): A__ = """model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight""" % (player, i, nlayer) A__ = ( vnp[i].transpose([1, 0] ).copy() ) # In Mesh-Tensorflow, it is one array, so it is divided A__ = torch.tensor(lowerCamelCase__ ) elif key_name.startswith("""model/mlp""" ): A__ = int(key_name[9:].split("""/""" )[0] ) if key_name.endswith("""/p1/kernel""" ): A__ = """model.blocks.%d.feed_forward.mlp.wi.weight""" % player A__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/p1/bias""" ): A__ = """model.blocks.%d.feed_forward.mlp.wi.bias""" % player A__ = vnp.copy() # same because it is one dimensional A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/p2/kernel""" ): A__ = """model.blocks.%d.feed_forward.mlp.wo.weight""" % player A__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/p2/bias""" ): A__ = """model.blocks.%d.feed_forward.mlp.wo.bias""" % player A__ = vnp.copy() # same because it is one dimensional A__ = torch.tensor(lowerCamelCase__ ) elif key_name.startswith("""model/ln""" ): A__ = int(key_name[8:].split("""/""" )[0] ) if key_name.endswith("""/b""" ): A__ = """model.blocks.%d.feed_forward.norm.bias""" % player A__ = vnp.copy() # same because it is one dimensional A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/g""" ): A__ = """model.blocks.%d.feed_forward.norm.weight""" % player A__ = vnp.copy() # same because it is one dimensional A__ = torch.tensor(lowerCamelCase__ ) elif key_name.startswith("""model/att""" ): A__ = int(key_name[9:].split("""/""" )[0] ) if key_name.endswith("""/qkv/kernel""" ): A__ = vnp.copy() # Compute same dimension as Mesh-tensorflow using einsum A__ = state[:, 0, :, :] A__ = state[:, 1, :, :] A__ = state[:, 2, :, :] A__ = ( state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix A__ = ( state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix A__ = ( state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix A__ = """model.blocks.%d.self_attn.self_attn.q_proj.weight""" % player A__ = torch.tensor(lowerCamelCase__ ) A__ = """model.blocks.%d.self_attn.self_attn.k_proj.weight""" % player A__ = torch.tensor(lowerCamelCase__ ) A__ = """model.blocks.%d.self_attn.self_attn.v_proj.weight""" % player A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/o/kernel""" ): A__ = """model.blocks.%d.self_attn.self_attn.out_proj.weight""" % player A__ = ( vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]] ).transpose([1, 0] ).copy() ) # Mesh-Tensorflow is a diagonal matrix A__ = torch.tensor(lowerCamelCase__ ) elif key_name.startswith("""model/an""" ): A__ = int(key_name[8:].split("""/""" )[0] ) if key_name.endswith("""/b""" ): A__ = """model.blocks.%d.self_attn.norm.bias""" % player A__ = vnp.copy() # same because it is one dimensional A__ = torch.tensor(lowerCamelCase__ ) elif key_name.endswith("""/g""" ): A__ = """model.blocks.%d.self_attn.norm.weight""" % player A__ = vnp.copy() # same because it is one dimensional A__ = torch.tensor(lowerCamelCase__ ) elif ( key_name.startswith("""model/wte""" ) or key_name.startswith("""model/wpe""" ) or key_name.startswith("""model/ete""" ) ): A__ = {"""wte""": """embed_tokens""", """wpe""": """position_embeddings""", """ete""": """extra_position_embeddings"""}[ key_name[-3:] ] A__ = """model.%s.weight""" % nlayer A__ = vnp.copy() # same in embedded A__ = torch.tensor(lowerCamelCase__ ) if key_name.startswith("""model/wte""" ): A__ = """lm_head.weight""" A__ = vnp.copy() # same in embedded A__ = torch.tensor(lowerCamelCase__ ) elif key_name.startswith("""model/wob""" ): A__ = """final_logits_bias""" A__ = vnp.copy() # same in embedded A__ = state.reshape((1, -1) ) A__ = torch.tensor(lowerCamelCase__ ) elif key_name == "model/dense/kernel": A__ = """model.last_project.weight""" A__ = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix A__ = torch.tensor(lowerCamelCase__ ) elif key_name == "model/dense_1/bias": A__ = """model.last_project.bias""" A__ = vnp.copy() # same because it is one dimensional A__ = torch.tensor(lowerCamelCase__ ) torch.save(lowerCamelCase__ , args.output ) if __name__ == "__main__": lowerCamelCase__ : Tuple = argparse.ArgumentParser( description='''model converter.''', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('''--tf_model_dir''', metavar='''PATH''', type=str, required=True, help='''import model''') parser.add_argument('''--output''', metavar='''PATH''', type=str, required=True, help='''output model''') lowerCamelCase__ : Optional[Any] = parser.parse_args() convert_tf_gptsan_to_pt(args)
350
import math def __lowerCamelCase ( ) -> None: """simple docstring""" A__ = input("""Enter message: """ ) A__ = int(input(F'Enter key [2-{len(__a ) - 1}]: ' ) ) A__ = input("""Encryption/Decryption [e/d]: """ ) if mode.lower().startswith("""e""" ): A__ = encrypt_message(__a , __a ) elif mode.lower().startswith("""d""" ): A__ = decrypt_message(__a , __a ) # Append pipe symbol (vertical bar) to identify spaces at the end. print(F'Output:\n{text + "|"}' ) def __lowerCamelCase ( __a :int , __a :str ) -> str: """simple docstring""" A__ = [""""""] * key for col in range(__a ): A__ = col while pointer < len(__a ): cipher_text[col] += message[pointer] pointer += key return "".join(__a ) def __lowerCamelCase ( __a :int , __a :str ) -> str: """simple docstring""" A__ = math.ceil(len(__a ) / key ) A__ = key A__ = (num_cols * num_rows) - len(__a ) A__ = [""""""] * num_cols A__ = 0 A__ = 0 for symbol in message: plain_text[col] += symbol col += 1 if ( (col == num_cols) or (col == num_cols - 1) and (row >= num_rows - num_shaded_boxes) ): A__ = 0 row += 1 return "".join(__a ) if __name__ == "__main__": import doctest doctest.testmod() main()
276
0
import cva import numpy as np class UpperCAmelCase__ : """simple docstring""" def __init__( self , A_ , A_ ) -> Optional[Any]: if k in (0.04, 0.06): __UpperCamelCase =k __UpperCamelCase =window_size else: raise ValueError('invalid k value' ) def __str__( self ) -> str: return str(self.k ) def _a ( self , A_ ) -> tuple[cva.Mat, list[list[int]]]: __UpperCamelCase =cva.imread(A_ , 0 ) __UpperCamelCase , __UpperCamelCase =img.shape __UpperCamelCase =[] __UpperCamelCase =img.copy() __UpperCamelCase =cva.cvtColor(A_ , cva.COLOR_GRAY2RGB ) __UpperCamelCase , __UpperCamelCase =np.gradient(A_ ) __UpperCamelCase =dx**2 __UpperCamelCase =dy**2 __UpperCamelCase =dx * dy __UpperCamelCase =0.04 __UpperCamelCase =self.window_size // 2 for y in range(A_ , h - offset ): for x in range(A_ , w - offset ): __UpperCamelCase =ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __UpperCamelCase =iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __UpperCamelCase =ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __UpperCamelCase =(wxx * wyy) - (wxy**2) __UpperCamelCase =wxx + wyy __UpperCamelCase =det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 255 ) return color_img, corner_list if __name__ == "__main__": _A = HarrisCorner(0.04, 3) _A , _A = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
62
import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def _snake_case ( lowerCAmelCase : int , lowerCAmelCase : str , lowerCAmelCase : str , lowerCAmelCase : Path , lowerCAmelCase : str = None , lowerCAmelCase : str = None , lowerCAmelCase : str = None , ): """simple docstring""" if config_name_or_path is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = "facebook/rag-token-base" if model_type == "rag_token" else "facebook/rag-sequence-base" if generator_tokenizer_name_or_path is None: SCREAMING_SNAKE_CASE_ : Dict = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = question_encoder_name_or_path SCREAMING_SNAKE_CASE_ : Union[str, Any] = RagTokenForGeneration if model_type == "rag_token" else RagSequenceForGeneration # Save model. SCREAMING_SNAKE_CASE_ : List[Any] = RagConfig.from_pretrained(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Tuple = AutoConfig.from_pretrained(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : int = AutoConfig.from_pretrained(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = gen_config SCREAMING_SNAKE_CASE_ : Optional[Any] = question_encoder_config SCREAMING_SNAKE_CASE_ : Dict = model_class.from_pretrained_question_encoder_generator( lowerCAmelCase , lowerCAmelCase , config=lowerCAmelCase ) rag_model.save_pretrained(lowerCAmelCase ) # Sanity check. model_class.from_pretrained(lowerCAmelCase ) # Save tokenizers. SCREAMING_SNAKE_CASE_ : Optional[Any] = AutoTokenizer.from_pretrained(lowerCAmelCase ) gen_tokenizer.save_pretrained(dest_dir / "generator_tokenizer/" ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = AutoTokenizer.from_pretrained(lowerCAmelCase ) question_encoder_tokenizer.save_pretrained(dest_dir / "question_encoder_tokenizer/" ) if __name__ == "__main__": __lowerCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument( '''--model_type''', choices=['''rag_sequence''', '''rag_token'''], required=True, type=str, help='''RAG model type: rag_sequence, rag_token''', ) parser.add_argument('''--dest''', type=str, required=True, help='''Path to the output checkpoint directory.''') parser.add_argument('''--generator_name_or_path''', type=str, required=True, help='''Generator model identifier''') parser.add_argument( '''--question_encoder_name_or_path''', type=str, required=True, help='''Question encoder model identifier''' ) parser.add_argument( '''--generator_tokenizer_name_or_path''', type=str, help='''Generator tokenizer identifier, if not specified, resolves to ``generator_name_or_path``''', ) parser.add_argument( '''--question_encoder_tokenizer_name_or_path''', type=str, help='''Question encoder tokenizer identifier, if not specified, resolves to ``question_encoder_name_or_path``''', ) parser.add_argument( '''--config_name_or_path''', type=str, help=( '''Identifier of the model config to use, if not provided, resolves to a base config for a given''' ''' ``model_type``''' ), ) __lowerCamelCase : str = parser.parse_args() __lowerCamelCase : int = Path(args.dest) dest_dir.mkdir(exist_ok=True) consolidate( args.model_type, args.generator_name_or_path, args.question_encoder_name_or_path, dest_dir, args.config_name_or_path, args.generator_tokenizer_name_or_path, args.question_encoder_tokenizer_name_or_path, )
18
0
import os from pathlib import Path def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Union[str, Any] , __magic_name__ : str , __magic_name__ : List[str] , __magic_name__ : Optional[Any] ) -> Optional[Any]: """simple docstring""" UpperCamelCase :List[str] = { "en": "Machine learning is great, isn't it?", "ru": "Машинное обучение - это здорово, не так ли?", "de": "Maschinelles Lernen ist großartig, nicht wahr?", } # BLUE scores as follows: # "pair": [fairseq, transformers] UpperCamelCase :List[str] = { "wmt16-en-de-dist-12-1": [28.3, 27.52], "wmt16-en-de-dist-6-1": [27.4, 27.11], "wmt16-en-de-12-1": [26.9, 25.75], } UpperCamelCase :Any = f"""{src_lang}-{tgt_lang}""" UpperCamelCase :Optional[Any] = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt16 - allenai license: apache-2.0 datasets: - wmt16 metrics: - bleu --- # FSMT ## Model description This is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}. For more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369). All 3 models are available: * [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1) * [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1) * [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"allenai/{model_name}\" 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 ## Training data Pretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369). ## Eval results Here are the BLEU scores: model | fairseq | transformers -------|---------|---------- {model_name} | {scores[model_name][0]} | {scores[model_name][1]} The score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs. 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=5 mkdir -p $DATA_DIR sacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $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 ``` ## Data Sources - [training, etc.](http://www.statmt.org/wmt16/) - [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372) ### BibTeX entry and citation info ``` @misc{{kasai2020deep, title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}}, author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}}, year={{2020}}, eprint={{2006.10369}}, archivePrefix={{arXiv}}, primaryClass={{cs.CL}} }} ``` """ model_card_dir.mkdir(parents=_lowerCamelCase , exist_ok=_lowerCamelCase ) UpperCamelCase :Optional[Any] = os.path.join(_lowerCamelCase , """README.md""" ) print(f"""Generating {path}""" ) with open(_lowerCamelCase , """w""" , encoding="""utf-8""" ) as f: f.write(_lowerCamelCase ) # make sure we are under the root of the project UpperCAmelCase_ : Tuple = Path(__file__).resolve().parent.parent.parent UpperCAmelCase_ : int = repo_dir / '''model_cards''' for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]: UpperCAmelCase_ : Optional[int] = model_cards_dir / '''allenai''' / model_name write_model_card(model_card_dir, src_lang='''en''', tgt_lang='''de''', model_name=model_name)
369
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DeformableDetrImageProcessor class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self : Union[str, Any] , __lowerCamelCase : int , __lowerCamelCase : Any=7 , __lowerCamelCase : Tuple=3 , __lowerCamelCase : Optional[Any]=30 , __lowerCamelCase : Union[str, Any]=400 , __lowerCamelCase : Optional[int]=True , __lowerCamelCase : Tuple=None , __lowerCamelCase : int=True , __lowerCamelCase : Dict=[0.5, 0.5, 0.5] , __lowerCamelCase : int=[0.5, 0.5, 0.5] , __lowerCamelCase : Optional[int]=True , __lowerCamelCase : str=1 / 255 , __lowerCamelCase : str=True , ): # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p UpperCamelCase :List[Any] = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 1_333} UpperCamelCase :Tuple = parent UpperCamelCase :int = batch_size UpperCamelCase :str = num_channels UpperCamelCase :Dict = min_resolution UpperCamelCase :Any = max_resolution UpperCamelCase :int = do_resize UpperCamelCase :str = size UpperCamelCase :Dict = do_normalize UpperCamelCase :Tuple = image_mean UpperCamelCase :Optional[int] = image_std UpperCamelCase :Tuple = do_rescale UpperCamelCase :Optional[Any] = rescale_factor UpperCamelCase :List[Any] = do_pad def _A ( self : List[Any] ): return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def _A ( self : Dict , __lowerCamelCase : Any , __lowerCamelCase : Optional[int]=False ): if not batched: UpperCamelCase :Optional[Any] = image_inputs[0] if isinstance(__lowerCamelCase , Image.Image ): UpperCamelCase , UpperCamelCase :Union[str, Any] = image.size else: UpperCamelCase , UpperCamelCase :Optional[int] = image.shape[1], image.shape[2] if w < h: UpperCamelCase :int = int(self.size["""shortest_edge"""] * h / w ) UpperCamelCase :Tuple = self.size["""shortest_edge"""] elif w > h: UpperCamelCase :List[Any] = self.size["""shortest_edge"""] UpperCamelCase :str = int(self.size["""shortest_edge"""] * w / h ) else: UpperCamelCase :List[Any] = self.size["""shortest_edge"""] UpperCamelCase :str = self.size["""shortest_edge"""] else: UpperCamelCase :List[Any] = [] for image in image_inputs: UpperCamelCase , UpperCamelCase :int = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) UpperCamelCase :int = max(__lowerCamelCase , key=lambda __lowerCamelCase : item[0] )[0] UpperCamelCase :Tuple = max(__lowerCamelCase , key=lambda __lowerCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class _SCREAMING_SNAKE_CASE ( _a , unittest.TestCase ): snake_case__ : Optional[int] = DeformableDetrImageProcessor if is_vision_available() else None def _A ( self : Optional[Any] ): UpperCamelCase :str = DeformableDetrImageProcessingTester(self ) @property def _A ( self : Optional[Any] ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Dict ): UpperCamelCase :int = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__lowerCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """image_std""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """do_rescale""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """do_pad""" ) ) self.assertTrue(hasattr(__lowerCamelCase , """size""" ) ) def _A ( self : str ): UpperCamelCase :Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 1_333} ) self.assertEqual(image_processor.do_pad , __lowerCamelCase ) UpperCamelCase :int = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=__lowerCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , __lowerCamelCase ) def _A ( self : List[Any] ): pass def _A ( self : Dict ): # Initialize image_processing UpperCamelCase :List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase :List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__lowerCamelCase ) for image in image_inputs: self.assertIsInstance(__lowerCamelCase , Image.Image ) # Test not batched input UpperCamelCase :Dict = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values UpperCamelCase , UpperCamelCase :Optional[int] = self.image_processor_tester.get_expected_values(__lowerCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase , UpperCamelCase :str = self.image_processor_tester.get_expected_values(__lowerCamelCase , batched=__lowerCamelCase ) UpperCamelCase :int = image_processing(__lowerCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _A ( self : Tuple ): # Initialize image_processing UpperCamelCase :Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase :Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__lowerCamelCase , numpify=__lowerCamelCase ) for image in image_inputs: self.assertIsInstance(__lowerCamelCase , np.ndarray ) # Test not batched input UpperCamelCase :Union[str, Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values UpperCamelCase , UpperCamelCase :Any = self.image_processor_tester.get_expected_values(__lowerCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase :Dict = image_processing(__lowerCamelCase , return_tensors="""pt""" ).pixel_values UpperCamelCase , UpperCamelCase :Optional[Any] = self.image_processor_tester.get_expected_values(__lowerCamelCase , batched=__lowerCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _A ( self : Any ): # Initialize image_processing UpperCamelCase :Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase :List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__lowerCamelCase , torchify=__lowerCamelCase ) for image in image_inputs: self.assertIsInstance(__lowerCamelCase , torch.Tensor ) # Test not batched input UpperCamelCase :Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values UpperCamelCase , UpperCamelCase :List[str] = self.image_processor_tester.get_expected_values(__lowerCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase :Union[str, Any] = image_processing(__lowerCamelCase , return_tensors="""pt""" ).pixel_values UpperCamelCase , UpperCamelCase :List[str] = self.image_processor_tester.get_expected_values(__lowerCamelCase , batched=__lowerCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def _A ( self : Optional[Any] ): # prepare image and target UpperCamelCase :int = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: UpperCamelCase :str = json.loads(f.read() ) UpperCamelCase :List[Any] = {"""image_id""": 39_769, """annotations""": target} # encode them UpperCamelCase :Optional[int] = DeformableDetrImageProcessor() UpperCamelCase :Dict = image_processing(images=__lowerCamelCase , annotations=__lowerCamelCase , return_tensors="""pt""" ) # verify pixel values UpperCamelCase :Union[str, Any] = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["""pixel_values"""].shape , __lowerCamelCase ) UpperCamelCase :Optional[Any] = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , __lowerCamelCase , atol=1E-4 ) ) # verify area UpperCamelCase :str = torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , __lowerCamelCase ) ) # verify boxes UpperCamelCase :List[Any] = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , __lowerCamelCase ) UpperCamelCase :List[str] = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , __lowerCamelCase , atol=1E-3 ) ) # verify image_id UpperCamelCase :Tuple = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , __lowerCamelCase ) ) # verify is_crowd UpperCamelCase :List[Any] = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , __lowerCamelCase ) ) # verify class_labels UpperCamelCase :Union[str, Any] = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , __lowerCamelCase ) ) # verify orig_size UpperCamelCase :Dict = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , __lowerCamelCase ) ) # verify size UpperCamelCase :int = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , __lowerCamelCase ) ) @slow def _A ( self : str ): # prepare image, target and masks_path UpperCamelCase :Any = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: UpperCamelCase :Any = json.loads(f.read() ) UpperCamelCase :int = {"""file_name""": """000000039769.png""", """image_id""": 39_769, """segments_info""": target} UpperCamelCase :Any = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them UpperCamelCase :Tuple = DeformableDetrImageProcessor(format="""coco_panoptic""" ) UpperCamelCase :Dict = image_processing(images=__lowerCamelCase , annotations=__lowerCamelCase , masks_path=__lowerCamelCase , return_tensors="""pt""" ) # verify pixel values UpperCamelCase :Optional[int] = torch.Size([1, 3, 800, 1_066] ) self.assertEqual(encoding["""pixel_values"""].shape , __lowerCamelCase ) UpperCamelCase :Optional[int] = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , __lowerCamelCase , atol=1E-4 ) ) # verify area UpperCamelCase :List[str] = torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , __lowerCamelCase ) ) # verify boxes UpperCamelCase :List[str] = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , __lowerCamelCase ) UpperCamelCase :List[Any] = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , __lowerCamelCase , atol=1E-3 ) ) # verify image_id UpperCamelCase :str = torch.tensor([39_769] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , __lowerCamelCase ) ) # verify is_crowd UpperCamelCase :Tuple = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , __lowerCamelCase ) ) # verify class_labels UpperCamelCase :List[Any] = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , __lowerCamelCase ) ) # verify masks UpperCamelCase :Union[str, Any] = 822_873 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , __lowerCamelCase ) # verify orig_size UpperCamelCase :Tuple = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , __lowerCamelCase ) ) # verify size UpperCamelCase :str = torch.tensor([800, 1_066] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , __lowerCamelCase ) )
62
0
'''simple docstring''' import fire from utils import calculate_rouge, save_json def a__ ( lowercase : Optional[int], lowercase : List[str], lowercase : List[Any]=None, **lowercase : int ) -> Optional[Any]: """simple docstring""" _UpperCamelCase = [x.strip() for x in open(lowercase ).readlines()] _UpperCamelCase = [x.strip() for x in open(lowercase ).readlines()][: len(lowercase )] _UpperCamelCase = calculate_rouge(lowercase, lowercase, **lowercase ) if save_path is not None: save_json(lowercase, lowercase, indent=lowercase ) return metrics # these print nicely if __name__ == "__main__": fire.Fire(calculate_rouge_path)
324
'''simple docstring''' import os import numpy import onnx def a__ ( lowercase : List[str], lowercase : str ) -> List[Any]: """simple docstring""" _UpperCamelCase = a.name _UpperCamelCase = b.name _UpperCamelCase = '''''' _UpperCamelCase = '''''' _UpperCamelCase = a == b _UpperCamelCase = name_a _UpperCamelCase = name_b return res def a__ ( lowercase : List[str], lowercase : List[Any], lowercase : Tuple ) -> int: """simple docstring""" for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(lowercase, lowercase ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g, lowercase, lowercase ) _graph_replace_input_with(node_proto.attribute[1].g, lowercase, lowercase ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g, lowercase, lowercase ) def a__ ( lowercase : Any, lowercase : Union[str, Any], lowercase : Dict ) -> Tuple: """simple docstring""" for n in graph_proto.node: _node_replace_input_with(lowercase, lowercase, lowercase ) def a__ ( lowercase : Optional[int], lowercase : Union[str, Any], lowercase : Optional[int] ) -> Tuple: """simple docstring""" _UpperCamelCase = list(model.graph.initializer ) _UpperCamelCase = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i _UpperCamelCase = inits[i].name _UpperCamelCase = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph, lowercase, lowercase ) def a__ ( lowercase : Dict ) -> Dict: """simple docstring""" _UpperCamelCase = os.path.dirname(lowercase ) _UpperCamelCase = os.path.basename(lowercase ) _UpperCamelCase = onnx.load(os.path.join(lowercase, lowercase ) ) _UpperCamelCase = list(model.graph.initializer ) _UpperCamelCase = set() _UpperCamelCase = {} _UpperCamelCase = [] _UpperCamelCase = 0 for i in range(len(lowercase ) ): if i in dup_set: continue for j in range(i + 1, len(lowercase ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i], inits[j] ): dup_set.add(lowercase ) dup_set.add(lowercase ) _UpperCamelCase = inits[j].data_type _UpperCamelCase = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print('''unexpected data type: ''', lowercase ) total_reduced_size += mem_size _UpperCamelCase = inits[i].name _UpperCamelCase = inits[j].name if name_i in dup_map: dup_map[name_i].append(lowercase ) else: _UpperCamelCase = [name_j] ind_to_replace.append((j, i) ) print('''total reduced size: ''', total_reduced_size / 1024 / 1024 / 1024, '''GB''' ) _UpperCamelCase = sorted(lowercase ) _remove_dup_initializers_from_model(lowercase, lowercase, lowercase ) _UpperCamelCase = '''optimized_''' + model_file_name _UpperCamelCase = os.path.join(lowercase, lowercase ) onnx.save(lowercase, lowercase ) return new_model
324
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCamelCase = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''MLukeTokenizer'''] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
334
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a__ : int = { "configuration_xlm_roberta_xl": [ "XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMRobertaXLConfig", "XLMRobertaXLOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : List[Any] = [ "XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST", "XLMRobertaXLForCausalLM", "XLMRobertaXLForMaskedLM", "XLMRobertaXLForMultipleChoice", "XLMRobertaXLForQuestionAnswering", "XLMRobertaXLForSequenceClassification", "XLMRobertaXLForTokenClassification", "XLMRobertaXLModel", "XLMRobertaXLPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys a__ : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure)
161
'''simple docstring''' import unittest from transformers import XLMConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMWithLMHeadModel, ) from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase__ : def __init__( self :List[str] , _A :Tuple , _A :Optional[int]=13 , _A :List[Any]=7 , _A :Tuple=True , _A :Optional[Any]=True , _A :int=True , _A :Union[str, Any]=True , _A :Union[str, Any]=True , _A :Union[str, Any]=False , _A :int=False , _A :Any=False , _A :Tuple=2 , _A :Tuple=99 , _A :Union[str, Any]=0 , _A :Union[str, Any]=32 , _A :str=5 , _A :Optional[Any]=4 , _A :List[str]=0.1 , _A :List[Any]=0.1 , _A :Optional[Any]=512 , _A :Dict=2 , _A :Any=0.02 , _A :int=2 , _A :Dict=4 , _A :Optional[int]="last" , _A :str=True , _A :List[str]=None , _A :Optional[int]=0 , ) -> int: '''simple docstring''' __A = parent __A = batch_size __A = seq_length __A = is_training __A = use_input_lengths __A = use_token_type_ids __A = use_labels __A = gelu_activation __A = sinusoidal_embeddings __A = causal __A = asm __A = n_langs __A = vocab_size __A = n_special __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_sequence_label_size __A = initializer_range __A = num_labels __A = num_choices __A = summary_type __A = use_proj __A = scope __A = bos_token_id def lowercase_ ( self :int ) -> Tuple: '''simple docstring''' __A = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __A = random_attention_mask([self.batch_size, self.seq_length] ) __A = None if self.use_input_lengths: __A = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length __A = None if self.use_token_type_ids: __A = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) __A = None __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.seq_length] , self.num_labels ) __A = ids_tensor([self.batch_size] , 2 ).float() __A = ids_tensor([self.batch_size] , self.num_choices ) __A = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def lowercase_ ( self :Union[str, Any] ) -> List[Any]: '''simple docstring''' return XLMConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , num_labels=self.num_labels , bos_token_id=self.bos_token_id , ) def lowercase_ ( self :str , _A :Optional[int] , _A :Dict , _A :Union[str, Any] , _A :List[Any] , _A :str , _A :Union[str, Any] , _A :Optional[Any] , _A :List[str] , _A :Dict , ) -> Any: '''simple docstring''' __A = XLMModel(config=_A ) model.to(_A ) model.eval() __A = model(_A , lengths=_A , langs=_A ) __A = model(_A , langs=_A ) __A = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase_ ( self :int , _A :List[Any] , _A :List[str] , _A :List[Any] , _A :int , _A :Optional[int] , _A :Optional[Any] , _A :Dict , _A :List[Any] , _A :List[Any] , ) -> List[Any]: '''simple docstring''' __A = XLMWithLMHeadModel(_A ) model.to(_A ) model.eval() __A = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ ( self :Union[str, Any] , _A :str , _A :List[str] , _A :Union[str, Any] , _A :str , _A :Any , _A :Dict , _A :Any , _A :Union[str, Any] , _A :Optional[Any] , ) -> int: '''simple docstring''' __A = XLMForQuestionAnsweringSimple(_A ) model.to(_A ) model.eval() __A = model(_A ) __A = model(_A , start_positions=_A , end_positions=_A ) __A = outputs self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowercase_ ( self :Union[str, Any] , _A :Any , _A :Union[str, Any] , _A :str , _A :Dict , _A :Optional[Any] , _A :Union[str, Any] , _A :List[str] , _A :str , _A :Optional[Any] , ) -> int: '''simple docstring''' __A = XLMForQuestionAnswering(_A ) model.to(_A ) model.eval() __A = model(_A ) __A = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , p_mask=_A , ) __A = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , ) ((__A) , ) = result_with_labels.to_tuple() __A = model(_A , start_positions=_A , end_positions=_A ) ((__A) , ) = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def lowercase_ ( self :Optional[int] , _A :Optional[Any] , _A :Optional[int] , _A :List[Any] , _A :int , _A :Tuple , _A :Union[str, Any] , _A :List[Any] , _A :List[str] , _A :Dict , ) -> str: '''simple docstring''' __A = XLMForSequenceClassification(_A ) model.to(_A ) model.eval() __A = model(_A ) __A = model(_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowercase_ ( self :Optional[int] , _A :str , _A :List[str] , _A :Union[str, Any] , _A :Dict , _A :int , _A :Dict , _A :Union[str, Any] , _A :int , _A :Optional[Any] , ) -> List[str]: '''simple docstring''' __A = self.num_labels __A = XLMForTokenClassification(_A ) model.to(_A ) model.eval() __A = model(_A , attention_mask=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ ( self :List[str] , _A :Optional[Any] , _A :List[str] , _A :List[Any] , _A :Union[str, Any] , _A :Any , _A :List[str] , _A :Optional[Any] , _A :Any , _A :Tuple , ) -> List[Any]: '''simple docstring''' __A = self.num_choices __A = XLMForMultipleChoice(config=_A ) model.to(_A ) model.eval() __A = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __A = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __A = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __A = model( _A , attention_mask=_A , token_type_ids=_A , labels=_A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowercase_ ( self :Union[str, Any] ) -> Dict: '''simple docstring''' __A = self.prepare_config_and_inputs() ( ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ( __A ) , ) = config_and_inputs __A = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths} return config, inputs_dict @require_torch class UpperCamelCase__ ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase): UpperCAmelCase__ : Optional[int] = ( ( XLMModel, XLMWithLMHeadModel, XLMForQuestionAnswering, XLMForSequenceClassification, XLMForQuestionAnsweringSimple, XLMForTokenClassification, XLMForMultipleChoice, ) if is_torch_available() else () ) UpperCAmelCase__ : Dict = ( (XLMWithLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable UpperCAmelCase__ : List[Any] = ( { 'feature-extraction': XLMModel, 'fill-mask': XLMWithLMHeadModel, 'question-answering': XLMForQuestionAnsweringSimple, 'text-classification': XLMForSequenceClassification, 'text-generation': XLMWithLMHeadModel, 'token-classification': XLMForTokenClassification, 'zero-shot': XLMForSequenceClassification, } if is_torch_available() else {} ) def lowercase_ ( self :int , _A :int , _A :Optional[Any] , _A :Dict , _A :List[Any] , _A :str ) -> str: '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def lowercase_ ( self :int , _A :Optional[Any] , _A :Dict , _A :Optional[int]=False ) -> List[Any]: '''simple docstring''' __A = super()._prepare_for_class(_A , _A , return_labels=_A ) if return_labels: if model_class.__name__ == "XLMForQuestionAnswering": __A = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) __A = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) return inputs_dict def lowercase_ ( self :Optional[int] ) -> Any: '''simple docstring''' __A = XLMModelTester(self ) __A = ConfigTester(self , config_class=_A , emb_dim=37 ) def lowercase_ ( self :Dict ) -> List[Any]: '''simple docstring''' self.config_tester.run_common_tests() def lowercase_ ( self :List[Any] ) -> Any: '''simple docstring''' __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_model(*_A ) def lowercase_ ( self :str ) -> List[Any]: '''simple docstring''' __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_lm_head(*_A ) def lowercase_ ( self :Any ) -> Tuple: '''simple docstring''' __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_simple_qa(*_A ) def lowercase_ ( self :str ) -> str: '''simple docstring''' __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_qa(*_A ) def lowercase_ ( self :List[Any] ) -> Optional[int]: '''simple docstring''' __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_sequence_classif(*_A ) def lowercase_ ( self :List[str] ) -> Optional[Any]: '''simple docstring''' __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_token_classif(*_A ) def lowercase_ ( self :Any ) -> Union[str, Any]: '''simple docstring''' __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_xlm_for_multiple_choice(*_A ) def lowercase_ ( self :Any , _A :str , _A :str , _A :int , _A :Optional[int] , _A :Any , _A :List[Any]=False , _A :Dict=1 ) -> Optional[int]: '''simple docstring''' self.assertIsInstance(_A , _A ) self.assertListEqual( [isinstance(_A , _A ) for iter_attentions in attentions] , [True] * len(_A ) ) self.assertEqual(len(_A ) , (max_length - min_length) * num_beam_groups ) for idx, iter_attentions in enumerate(_A ): # adds PAD dummy token __A = min_length + idx + 1 __A = min_length + idx + 1 __A = ( batch_size * num_beam_groups, config.num_attention_heads, tgt_len, src_len, ) # check attn size self.assertListEqual( [layer_attention.shape for layer_attention in iter_attentions] , [expected_shape] * len(_A ) ) def lowercase_ ( self :Optional[Any] , _A :str , _A :List[Any] , _A :str , _A :str , _A :int , _A :Union[str, Any]=False , _A :Optional[Any]=1 ) -> Dict: '''simple docstring''' self.assertIsInstance(_A , _A ) self.assertListEqual( [isinstance(_A , _A ) for iter_hidden_states in hidden_states] , [True] * len(_A ) , ) self.assertEqual(len(_A ) , (max_length - min_length) * num_beam_groups ) for idx, iter_hidden_states in enumerate(_A ): # adds PAD dummy token __A = min_length + idx + 1 __A = (batch_size * num_beam_groups, seq_len, config.hidden_size) # check hidden size self.assertListEqual( [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states] , [expected_shape] * len(_A ) , ) pass @slow def lowercase_ ( self :int ) -> Tuple: '''simple docstring''' for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __A = XLMModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @require_torch class UpperCamelCase__ ( unittest.TestCase): @slow def lowercase_ ( self :int ) -> str: '''simple docstring''' __A = XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048' ) model.to(_A ) __A = torch.tensor([[14, 447]] , dtype=torch.long , device=_A ) # the president __A = [ 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, 14, 447, ] # the president the president the president the president the president the president the president the president the president the president # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference __A = model.generate(_A , do_sample=_A ) self.assertListEqual(output_ids[0].cpu().numpy().tolist() , _A )
161
1
"""simple docstring""" import os import string import sys __A : Optional[int] = 1 << 8 __A : List[Any] = { 'tab': ord('\t'), 'newline': ord('\r'), 'esc': 27, 'up': 65 + ARROW_KEY_FLAG, 'down': 66 + ARROW_KEY_FLAG, 'right': 67 + ARROW_KEY_FLAG, 'left': 68 + ARROW_KEY_FLAG, 'mod_int': 91, 'undefined': sys.maxsize, 'interrupt': 3, 'insert': 50, 'delete': 51, 'pg_up': 53, 'pg_down': 54, } __A : Any = KEYMAP['up'] __A : str = KEYMAP['left'] if sys.platform == "win32": __A : Union[str, Any] = [] __A : Union[str, Any] = { b'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG, b'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG, b'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG, b'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG, b'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG, b'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG, b'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG, b'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG, } for i in range(10): __A : str = ord(str(i)) def __SCREAMING_SNAKE_CASE ( ): """simple docstring""" if os.name == "nt": import msvcrt A = "mbcs" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(lowercase__ ) == 0: # Read the keystroke A = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): A = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: A = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["mod_int"] ) ) WIN_CH_BUFFER.append(lowercase__ ) if ord(lowercase__ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(126 ) ) A = chr(KEYMAP["esc"] ) except KeyError: A = cha[1] else: A = ch.decode(lowercase__ ) else: A = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty A = sys.stdin.fileno() A = termios.tcgetattr(lowercase__ ) try: tty.setraw(lowercase__ ) A = sys.stdin.read(1 ) finally: termios.tcsetattr(lowercase__ , termios.TCSADRAIN , lowercase__ ) return ch def __SCREAMING_SNAKE_CASE ( ): """simple docstring""" A = get_raw_chars() if ord(lowercase__ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(lowercase__ ) == KEYMAP["esc"]: A = get_raw_chars() if ord(lowercase__ ) == KEYMAP["mod_int"]: A = get_raw_chars() if ord(lowercase__ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(lowercase__ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(lowercase__ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
57
"""simple docstring""" import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( UniSpeechConfig, UniSpeechForCTC, UniSpeechForPreTraining, WavaVecaFeatureExtractor, WavaVecaPhonemeCTCTokenizer, WavaVecaProcessor, logging, ) logging.set_verbosity_info() __A : int = logging.get_logger(__name__) __A : Optional[Any] = { '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', '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': 'ctc_proj', 'mask_emb': 'masked_spec_embed', } __A : str = [ 'ctc_proj', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def __SCREAMING_SNAKE_CASE ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ): """simple docstring""" for attribute in key.split("." ): if is_finetuned: if attribute in ["quantizer", "project_q", "project_hid"]: # those layers are only relevant for pretraining and should be dropped return if attribute == "ctc_proj": # we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models A = "lm_head" A = getattr(lowercase__ , lowercase__ ) if weight_type is not None: A = getattr(lowercase__ , lowercase__ ).shape else: A = hf_pointer.shape assert hf_shape == value.shape, ( 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": A = value elif weight_type == "weight_g": A = value elif weight_type == "weight_v": A = value elif weight_type == "bias": A = value else: A = value logger.info(F"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def __SCREAMING_SNAKE_CASE ( lowercase__ , lowercase__ , lowercase__ ): """simple docstring""" A = [] A = fairseq_model.state_dict() A = hf_model.unispeech.feature_extractor for name, value in fairseq_dict.items(): A = False if "conv_layers" in name: load_conv_layer( lowercase__ , lowercase__ , lowercase__ , lowercase__ , hf_model.config.feat_extract_norm == "group" , ) A = True else: for key, mapped_key in MAPPING.items(): A = "unispeech." + 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]: A = True if "*" in mapped_key: A = name.split(lowercase__ )[0].split("." )[-2] A = mapped_key.replace("*" , lowercase__ ) if "weight_g" in name: A = "weight_g" elif "weight_v" in name: A = "weight_v" elif "bias" in name: A = "bias" elif "weight" in name: # TODO: don't match quantizer.weight_proj A = "weight" else: A = None set_recursively(lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ) continue if not is_used: unused_weights.append(lowercase__ ) logger.warning(F"""Unused weights: {unused_weights}""" ) def __SCREAMING_SNAKE_CASE ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ): """simple docstring""" A = full_name.split("conv_layers." )[-1] A = name.split("." ) A = int(items[0] ) A = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) A = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) A = 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: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) A = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) A = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(lowercase__ ) @torch.no_grad() def __SCREAMING_SNAKE_CASE ( lowercase__ , lowercase__ , lowercase__=None , lowercase__=None , lowercase__=True ): """simple docstring""" if config_path is not None: A = UniSpeechConfig.from_pretrained(lowercase__ ) else: A = UniSpeechConfig() if is_finetuned: if dict_path: A = Dictionary.load_from_json(lowercase__ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq A = target_dict.pad_index A = target_dict.bos_index A = target_dict.eos_index A = len(target_dict.symbols ) A = os.path.join(lowercase__ , "vocab.json" ) if not os.path.isdir(lowercase__ ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(lowercase__ ) ) return os.makedirs(lowercase__ , exist_ok=lowercase__ ) A = target_dict.indices # fairseq has the <pad> and <s> switched A = 42 A = 43 with open(lowercase__ , "w" , encoding="utf-8" ) as vocab_handle: json.dump(lowercase__ , lowercase__ ) A = WavaVecaPhonemeCTCTokenizer( lowercase__ , 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=lowercase__ , ) A = True if config.feat_extract_norm == "layer" else False A = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16_000 , padding_value=0 , do_normalize=lowercase__ , return_attention_mask=lowercase__ , ) A = WavaVecaProcessor(feature_extractor=lowercase__ , tokenizer=lowercase__ ) processor.save_pretrained(lowercase__ ) A = UniSpeechForCTC(lowercase__ ) else: A = UniSpeechForPreTraining(lowercase__ ) if is_finetuned: A , A , A = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] ), "w2v_path": checkpoint_path} ) else: A , A , A = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) A = model[0].eval() recursively_load_weights(lowercase__ , lowercase__ , lowercase__ ) hf_unispeech.save_pretrained(lowercase__ ) if __name__ == "__main__": __A : List[str] = 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' ) __A : int = parser.parse_args() convert_unispeech_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
57
1
'''simple docstring''' from pathlib import Path import fire def UpperCAmelCase_ ( __lowerCamelCase : str ,__lowerCamelCase : str ,__lowerCamelCase : int ): lowercase_ :List[Any] = Path(SCREAMING_SNAKE_CASE__ ) lowercase_ :str = Path(SCREAMING_SNAKE_CASE__ ) dest_dir.mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) for path in src_dir.iterdir(): lowercase_ :Optional[Any] = [x.rstrip() for x in list(path.open().readlines() )][:n] lowercase_ :Tuple = dest_dir.joinpath(path.name ) print(SCREAMING_SNAKE_CASE__ ) dest_path.open("w" ).write("\n".join(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": fire.Fire(minify)
223
'''simple docstring''' from __future__ import annotations from collections.abc import MutableSequence class __a : def __init__( self : int , __magic_name__ : int , __magic_name__ : MutableSequence[float] ) -> None: """simple docstring""" if len(__magic_name__ ) != degree + 1: raise ValueError( '''The number of coefficients should be equal to the degree + 1.''' ) UpperCAmelCase_ : list[float] = list(__magic_name__ ) UpperCAmelCase_ : List[str] = degree def __add__( self : List[str] , __magic_name__ : Polynomial ) -> Polynomial: """simple docstring""" if self.degree > polynomial_a.degree: UpperCAmelCase_ : Dict = self.coefficients[:] for i in range(polynomial_a.degree + 1 ): coefficients[i] += polynomial_a.coefficients[i] return Polynomial(self.degree , __magic_name__ ) else: UpperCAmelCase_ : List[str] = polynomial_a.coefficients[:] for i in range(self.degree + 1 ): coefficients[i] += self.coefficients[i] return Polynomial(polynomial_a.degree , __magic_name__ ) def __sub__( self : Dict , __magic_name__ : Polynomial ) -> Polynomial: """simple docstring""" return self + polynomial_a * Polynomial(0 , [-1] ) def __neg__( self : List[Any] ) -> Polynomial: """simple docstring""" return Polynomial(self.degree , [-c for c in self.coefficients] ) def __mul__( self : str , __magic_name__ : Polynomial ) -> Polynomial: """simple docstring""" UpperCAmelCase_ : list[float] = [0] * (self.degree + polynomial_a.degree + 1) for i in range(self.degree + 1 ): for j in range(polynomial_a.degree + 1 ): coefficients[i + j] += ( self.coefficients[i] * polynomial_a.coefficients[j] ) return Polynomial(self.degree + polynomial_a.degree , __magic_name__ ) def UpperCAmelCase__ ( self : Optional[Any] , __magic_name__ : int | float ) -> int | float: """simple docstring""" UpperCAmelCase_ : int | float = 0 for i in range(self.degree + 1 ): result += self.coefficients[i] * (substitution**i) return result def __str__( self : Optional[int] ) -> str: """simple docstring""" UpperCAmelCase_ : Optional[Any] = '''''' for i in range(self.degree , -1 , -1 ): if self.coefficients[i] == 0: continue elif self.coefficients[i] > 0: if polynomial: polynomial += " + " else: polynomial += " - " if i == 0: polynomial += str(abs(self.coefficients[i] ) ) elif i == 1: polynomial += str(abs(self.coefficients[i] ) ) + "x" else: polynomial += str(abs(self.coefficients[i] ) ) + "x^" + str(__magic_name__ ) return polynomial def __repr__( self : List[Any] ) -> str: """simple docstring""" return self.__str__() def UpperCAmelCase__ ( self : List[str] ) -> Polynomial: """simple docstring""" UpperCAmelCase_ : list[float] = [0] * self.degree for i in range(self.degree ): UpperCAmelCase_ : List[str] = self.coefficients[i + 1] * (i + 1) return Polynomial(self.degree - 1 , __magic_name__ ) def UpperCAmelCase__ ( self : Union[str, Any] , __magic_name__ : int | float = 0 ) -> Polynomial: """simple docstring""" UpperCAmelCase_ : list[float] = [0] * (self.degree + 2) UpperCAmelCase_ : Union[str, Any] = constant for i in range(self.degree + 1 ): UpperCAmelCase_ : Optional[Any] = self.coefficients[i] / (i + 1) return Polynomial(self.degree + 1 , __magic_name__ ) def __eq__( self : Any , __magic_name__ : object ) -> bool: """simple docstring""" if not isinstance(__magic_name__ , __magic_name__ ): return False if self.degree != polynomial_a.degree: return False for i in range(self.degree + 1 ): if self.coefficients[i] != polynomial_a.coefficients[i]: return False return True def __ne__( self : List[Any] , __magic_name__ : object ) -> bool: """simple docstring""" return not self.__eq__(__magic_name__ )
125
0
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __lowerCAmelCase ( __magic_name__ , unittest.TestCase ): """simple docstring""" snake_case_ = KandinskyInpaintPipeline snake_case_ = ['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''', '''mask_image'''] snake_case_ = [ '''prompt''', '''negative_prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''', '''mask_image''', ] snake_case_ = [ '''generator''', '''height''', '''width''', '''latents''', '''guidance_scale''', '''negative_prompt''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] snake_case_ = False @property def lowercase_ ( self ) -> Tuple: '''simple docstring''' return 32 @property def lowercase_ ( self ) -> List[Any]: '''simple docstring''' return 32 @property def lowercase_ ( self ) -> Tuple: '''simple docstring''' return self.time_input_dim @property def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' return self.time_input_dim * 4 @property def lowercase_ ( self ) -> List[Any]: '''simple docstring''' return 100 @property def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase = XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base' ) return tokenizer @property def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' torch.manual_seed(0 ) __lowerCamelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_005 , ) __lowerCamelCase = MultilingualCLIP(lowerCamelCase__ ) __lowerCamelCase = text_encoder.eval() return text_encoder @property def lowercase_ ( self ) -> str: '''simple docstring''' torch.manual_seed(0 ) __lowerCamelCase = { 'in_channels': 9, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'text_image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'text_image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } __lowerCamelCase = UNetaDConditionModel(**lowerCamelCase__ ) return model @property def lowercase_ ( self ) -> Union[str, Any]: '''simple docstring''' return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def lowercase_ ( self ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) __lowerCamelCase = VQModel(**self.dummy_movq_kwargs ) return model def lowercase_ ( self ) -> List[Any]: '''simple docstring''' __lowerCamelCase = self.dummy_text_encoder __lowerCamelCase = self.dummy_tokenizer __lowerCamelCase = self.dummy_unet __lowerCamelCase = self.dummy_movq __lowerCamelCase = DDIMScheduler( num_train_timesteps=1_000 , beta_schedule='linear' , beta_start=0.0_00_85 , beta_end=0.0_12 , clip_sample=lowerCamelCase__ , set_alpha_to_one=lowerCamelCase__ , steps_offset=1 , prediction_type='epsilon' , thresholding=lowerCamelCase__ , ) __lowerCamelCase = { 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__=0 ) -> str: '''simple docstring''' __lowerCamelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __lowerCamelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowerCamelCase__ ) # create init_image __lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(lowerCamelCase__ ) ).to(lowerCamelCase__ ) __lowerCamelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] __lowerCamelCase = Image.fromarray(np.uinta(lowerCamelCase__ ) ).convert('RGB' ).resize((256, 256) ) # create mask __lowerCamelCase = np.ones((64, 64) , dtype=np.floataa ) __lowerCamelCase = 0 if str(lowerCamelCase__ ).startswith('mps' ): __lowerCamelCase = torch.manual_seed(lowerCamelCase__ ) else: __lowerCamelCase = torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) __lowerCamelCase = { 'prompt': 'horse', 'image': init_image, 'mask_image': mask, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 2, 'guidance_scale': 4.0, 'output_type': 'np', } return inputs def lowercase_ ( self ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = 'cpu' __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = self.pipeline_class(**lowerCamelCase__ ) __lowerCamelCase = pipe.to(lowerCamelCase__ ) pipe.set_progress_bar_config(disable=lowerCamelCase__ ) __lowerCamelCase = pipe(**self.get_dummy_inputs(lowerCamelCase__ ) ) __lowerCamelCase = output.images __lowerCamelCase = pipe( **self.get_dummy_inputs(lowerCamelCase__ ) , return_dict=lowerCamelCase__ , )[0] __lowerCamelCase = image[0, -3:, -3:, -1] __lowerCamelCase = image_from_tuple[0, -3:, -3:, -1] print(f"""image.shape {image.shape}""" ) assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array( [0.8_32_69_19, 0.73_79_04_67, 0.20_91_85_81, 0.9_30_96_12, 0.5_51_17_91, 0.43_71_33_28, 0.5_51_33_21, 0.49_92_29_34, 0.59_49_77_86] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy' ) __lowerCamelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) __lowerCamelCase = np.ones((768, 768) , dtype=np.floataa ) __lowerCamelCase = 0 __lowerCamelCase = 'a hat' __lowerCamelCase = KandinskyPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-prior' , torch_dtype=torch.floataa ) pipe_prior.to(lowerCamelCase__ ) __lowerCamelCase = KandinskyInpaintPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-inpaint' , torch_dtype=torch.floataa ) __lowerCamelCase = pipeline.to(lowerCamelCase__ ) pipeline.set_progress_bar_config(disable=lowerCamelCase__ ) __lowerCamelCase = torch.Generator(device='cpu' ).manual_seed(0 ) __lowerCamelCase , __lowerCamelCase = pipe_prior( lowerCamelCase__ , generator=lowerCamelCase__ , num_inference_steps=5 , negative_prompt='' , ).to_tuple() __lowerCamelCase = pipeline( lowerCamelCase__ , image=lowerCamelCase__ , mask_image=lowerCamelCase__ , image_embeds=lowerCamelCase__ , negative_image_embeds=lowerCamelCase__ , generator=lowerCamelCase__ , num_inference_steps=100 , height=768 , width=768 , output_type='np' , ) __lowerCamelCase = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(lowerCamelCase__ , lowerCamelCase__ )
348
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __A = logging.get_logger(__name__) __A = { "asapp/sew-d-tiny-100k": "https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __lowerCAmelCase ( __magic_name__ ): """simple docstring""" snake_case_ = '''sew-d''' def __init__( self , lowerCamelCase__=32 , lowerCamelCase__=768 , lowerCamelCase__=12 , lowerCamelCase__=12 , lowerCamelCase__=3_072 , lowerCamelCase__=2 , lowerCamelCase__=512 , lowerCamelCase__=256 , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=("p2c", "c2p") , lowerCamelCase__="layer_norm" , lowerCamelCase__="gelu_python" , lowerCamelCase__=0.1 , lowerCamelCase__=0.1 , lowerCamelCase__=0.1 , lowerCamelCase__=0.0 , lowerCamelCase__=0.1 , lowerCamelCase__=0.02 , lowerCamelCase__=1e-7 , lowerCamelCase__=1e-5 , lowerCamelCase__="group" , lowerCamelCase__="gelu" , lowerCamelCase__=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , lowerCamelCase__=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , lowerCamelCase__=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , lowerCamelCase__=False , lowerCamelCase__=128 , lowerCamelCase__=16 , lowerCamelCase__=True , lowerCamelCase__=0.05 , lowerCamelCase__=10 , lowerCamelCase__=2 , lowerCamelCase__=0.0 , lowerCamelCase__=10 , lowerCamelCase__=0 , lowerCamelCase__="mean" , lowerCamelCase__=False , lowerCamelCase__=False , lowerCamelCase__=256 , lowerCamelCase__=0 , lowerCamelCase__=1 , lowerCamelCase__=2 , **lowerCamelCase__ , ) -> Any: '''simple docstring''' super().__init__(**lowerCamelCase__ , pad_token_id=lowerCamelCase__ , bos_token_id=lowerCamelCase__ , eos_token_id=lowerCamelCase__ ) __lowerCamelCase = hidden_size __lowerCamelCase = feat_extract_norm __lowerCamelCase = feat_extract_activation __lowerCamelCase = list(lowerCamelCase__ ) __lowerCamelCase = list(lowerCamelCase__ ) __lowerCamelCase = list(lowerCamelCase__ ) __lowerCamelCase = conv_bias __lowerCamelCase = num_conv_pos_embeddings __lowerCamelCase = num_conv_pos_embedding_groups __lowerCamelCase = len(self.conv_dim ) __lowerCamelCase = num_hidden_layers __lowerCamelCase = intermediate_size __lowerCamelCase = squeeze_factor __lowerCamelCase = max_position_embeddings __lowerCamelCase = position_buckets __lowerCamelCase = share_att_key __lowerCamelCase = relative_attention __lowerCamelCase = norm_rel_ebd __lowerCamelCase = list(lowerCamelCase__ ) __lowerCamelCase = hidden_act __lowerCamelCase = num_attention_heads __lowerCamelCase = hidden_dropout __lowerCamelCase = attention_dropout __lowerCamelCase = activation_dropout __lowerCamelCase = feat_proj_dropout __lowerCamelCase = final_dropout __lowerCamelCase = layer_norm_eps __lowerCamelCase = feature_layer_norm_eps __lowerCamelCase = initializer_range __lowerCamelCase = 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 __lowerCamelCase = apply_spec_augment __lowerCamelCase = mask_time_prob __lowerCamelCase = mask_time_length __lowerCamelCase = mask_time_min_masks __lowerCamelCase = mask_feature_prob __lowerCamelCase = mask_feature_length __lowerCamelCase = mask_feature_min_masks # ctc loss __lowerCamelCase = ctc_loss_reduction __lowerCamelCase = ctc_zero_infinity # sequence classification __lowerCamelCase = use_weighted_layer_sum __lowerCamelCase = classifier_proj_size @property def lowercase_ ( self ) -> Any: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
348
1
from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class SCREAMING_SNAKE_CASE_ ( snake_case_ ): __magic_name__: List[str] = "" __magic_name__: Optional[Any] = "hf-legacy" # "hf://"" is reserved for hffs def __init__( self : List[Any] , _A : Optional[DatasetInfo] = None , _A : Optional[str] = None , **_A : List[str] , ) -> int: """simple docstring""" super().__init__(self , **_A ) snake_case_ : Tuple = repo_info snake_case_ : Optional[int] = token snake_case_ : str = None def UpperCAmelCase_ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" if self.dir_cache is None: snake_case_ : str = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes snake_case_ : Optional[int] = { 'name': hf_file.rfilename, 'size': None, 'type': 'file', } self.dir_cache.update( { str(_A ): {'name': str(_A ), 'size': None, 'type': 'directory'} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def UpperCAmelCase_ ( self : Union[str, Any] , _A : str , _A : str = "rb" , **_A : Union[str, Any] , ) -> List[str]: """simple docstring""" if not isinstance(self.repo_info , _A ): raise NotImplementedError(F"""Open is only implemented for dataset repositories, but got {self.repo_info}""" ) snake_case_ : Optional[int] = hf_hub_url(self.repo_info.id , _A , revision=self.repo_info.sha ) return fsspec.open( _A , mode=_A , headers=get_authentication_headers_for_url(_A , use_auth_token=self.token ) , client_kwargs={'trust_env': True} , ).open() def UpperCAmelCase_ ( self : Optional[int] , _A : str , **_A : int ) -> List[Any]: """simple docstring""" self._get_dirs() snake_case_ : Optional[Any] = self._strip_protocol(_A ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(_A ) def UpperCAmelCase_ ( self : Tuple , _A : Tuple , _A : str=False , **_A : List[Any] ) -> List[str]: """simple docstring""" self._get_dirs() snake_case_ : List[Any] = PurePosixPath(path.strip('/' ) ) snake_case_ : Optional[int] = {} for p, f in self.dir_cache.items(): snake_case_ : Optional[int] = PurePosixPath(p.strip('/' ) ) snake_case_ : str = p.parent if root == path: snake_case_ : List[str] = f snake_case_ : List[Any] = list(paths.values() ) if detail: return out else: return sorted(f['name'] for f in out )
327
import unittest from typing import Dict, List, Optional, Union 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 BridgeTowerImageProcessor class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): def __init__( self : List[str] , _A : List[Any] , _A : bool = True , _A : Dict[str, int] = None , _A : int = 32 , _A : bool = True , _A : Union[int, float] = 1 / 255 , _A : bool = True , _A : bool = True , _A : Optional[Union[float, List[float]]] = [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3] , _A : Optional[Union[float, List[float]]] = [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1] , _A : bool = True , _A : Tuple=7 , _A : Tuple=30 , _A : int=400 , _A : Tuple=3 , ) -> Optional[int]: """simple docstring""" snake_case_ : str = parent snake_case_ : str = do_resize snake_case_ : str = size if size is not None else {'shortest_edge': 288} snake_case_ : Any = size_divisor snake_case_ : Any = do_rescale snake_case_ : Union[str, Any] = rescale_factor snake_case_ : str = do_normalize snake_case_ : int = do_center_crop snake_case_ : str = image_mean snake_case_ : int = image_std snake_case_ : Any = do_pad snake_case_ : Optional[int] = batch_size snake_case_ : List[str] = num_channels snake_case_ : Any = min_resolution snake_case_ : str = max_resolution def UpperCAmelCase_ ( self : Tuple ) -> Optional[int]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "size_divisor": self.size_divisor, } def UpperCAmelCase_ ( self : Dict , _A : str , _A : Union[str, Any]=False ) -> int: """simple docstring""" if not batched: snake_case_ : Optional[int] = self.size['shortest_edge'] snake_case_ : List[Any] = image_inputs[0] if isinstance(_A , Image.Image ): snake_case_ ,snake_case_ : Optional[Any] = image.size else: snake_case_ ,snake_case_ : str = image.shape[1], image.shape[2] snake_case_ : Dict = size / min(_A , _A ) if h < w: snake_case_ ,snake_case_ : str = size, scale * w else: snake_case_ ,snake_case_ : Tuple = scale * h, size snake_case_ : Dict = int((1333 / 800) * size ) if max(_A , _A ) > max_size: snake_case_ : Union[str, Any] = max_size / max(_A , _A ) snake_case_ : Any = newh * scale snake_case_ : Union[str, Any] = neww * scale snake_case_ ,snake_case_ : Any = int(newh + 0.5 ), int(neww + 0.5 ) snake_case_ ,snake_case_ : int = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: snake_case_ : Optional[int] = [] for image in image_inputs: snake_case_ ,snake_case_ : Optional[int] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) snake_case_ : str = max(_A , key=lambda _A : item[0] )[0] snake_case_ : List[str] = max(_A , key=lambda _A : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class SCREAMING_SNAKE_CASE_ ( snake_case_ , unittest.TestCase ): __magic_name__: List[Any] = BridgeTowerImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self : List[Any] ) -> Tuple: """simple docstring""" snake_case_ : int = BridgeTowerImageProcessingTester(self ) @property def UpperCAmelCase_ ( self : int ) -> Any: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" snake_case_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_A , 'image_mean' ) ) self.assertTrue(hasattr(_A , 'image_std' ) ) self.assertTrue(hasattr(_A , 'do_normalize' ) ) self.assertTrue(hasattr(_A , 'do_resize' ) ) self.assertTrue(hasattr(_A , 'size' ) ) self.assertTrue(hasattr(_A , 'size_divisor' ) ) def UpperCAmelCase_ ( self : Optional[int] ) -> List[str]: """simple docstring""" pass def UpperCAmelCase_ ( self : Tuple ) -> Tuple: """simple docstring""" snake_case_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_A ) for image in image_inputs: self.assertIsInstance(_A , Image.Image ) # Test not batched input snake_case_ : Dict = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Optional[Any] = self.image_processor_tester.get_expected_values(_A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case_ : List[str] = image_processing(_A , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Optional[Any] = self.image_processor_tester.get_expected_values(_A , batched=_A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Any: """simple docstring""" snake_case_ : int = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=_A , numpify=_A ) for image in image_inputs: self.assertIsInstance(_A , np.ndarray ) # Test not batched input snake_case_ : Union[str, Any] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Tuple = self.image_processor_tester.get_expected_values(_A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case_ : Any = image_processing(_A , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Any = self.image_processor_tester.get_expected_values(_A , batched=_A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self : Dict ) -> List[str]: """simple docstring""" snake_case_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_A , torchify=_A ) for image in image_inputs: self.assertIsInstance(_A , torch.Tensor ) # Test not batched input snake_case_ : Optional[int] = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Optional[Any] = self.image_processor_tester.get_expected_values(_A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case_ : str = image_processing(_A , return_tensors='pt' ).pixel_values snake_case_ ,snake_case_ : Tuple = self.image_processor_tester.get_expected_values(_A , batched=_A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
327
1
'''simple docstring''' import unittest from datasets import load_dataset from transformers import BloomTokenizerFast from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class snake_case__ ( __UpperCamelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase = None lowerCamelCase = BloomTokenizerFast lowerCamelCase = BloomTokenizerFast lowerCamelCase = True lowerCamelCase = False lowerCamelCase = "tokenizer_file" lowerCamelCase = {"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "pad_token": "<pad>"} def lowerCAmelCase ( self : str ) -> Any: """simple docstring""" super().setUp() snake_case : List[Any] = BloomTokenizerFast.from_pretrained('''bigscience/tokenizer''' ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCAmelCase ( self : int , **UpperCamelCase__ : List[Any] ) -> Any: """simple docstring""" kwargs.update(self.special_tokens_map ) return BloomTokenizerFast.from_pretrained(self.tmpdirname , **_lowerCAmelCase ) def lowerCAmelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" snake_case : List[str] = self.get_rust_tokenizer() snake_case : int = ["""The quick brown fox</s>""", """jumps over the lazy dog</s>"""] snake_case : Optional[int] = [[2175, 2_3714, 7_3173, 14_4252, 2], [77, 13_2619, 3478, 368, 10_9586, 3_5433, 2]] snake_case : Optional[int] = tokenizer.batch_encode_plus(_lowerCAmelCase )["""input_ids"""] self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) snake_case : Optional[Any] = tokenizer.batch_decode(_lowerCAmelCase ) self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase ( self : Dict , UpperCamelCase__ : Dict=6 ) -> List[str]: """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): snake_case : List[str] = self.rust_tokenizer_class.from_pretrained(_lowerCAmelCase , **_lowerCAmelCase ) # tokenizer_r.pad_token = None # Hotfixing padding = None # Simple input snake_case : int = """This is a simple input""" snake_case : List[str] = ["""This is a simple input 1""", """This is a simple input 2"""] snake_case : Optional[int] = ("""This is a simple input""", """This is a pair""") snake_case : Optional[Any] = [ ("""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 try: tokenizer_r.encode(_lowerCAmelCase , max_length=_lowerCAmelCase ) tokenizer_r.encode_plus(_lowerCAmelCase , max_length=_lowerCAmelCase ) tokenizer_r.batch_encode_plus(_lowerCAmelCase , max_length=_lowerCAmelCase ) tokenizer_r.encode(_lowerCAmelCase , max_length=_lowerCAmelCase ) tokenizer_r.batch_encode_plus(_lowerCAmelCase , max_length=_lowerCAmelCase ) except ValueError: self.fail('''Bloom Tokenizer should be able to deal with padding''' ) snake_case : List[Any] = None # Hotfixing padding = None 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 lowerCAmelCase ( self : List[str] ) -> int: """simple docstring""" snake_case : str = self.get_rust_tokenizer() snake_case : str = load_dataset('''xnli''' , '''all_languages''' , split='''test''' , streaming=_lowerCAmelCase ) snake_case : List[str] = next(iter(_lowerCAmelCase ) )["""premise"""] # pick up one data snake_case : Tuple = list(sample_data.values() ) snake_case : Any = list(map(tokenizer.encode , _lowerCAmelCase ) ) snake_case : Optional[int] = [tokenizer.decode(_lowerCAmelCase , clean_up_tokenization_spaces=_lowerCAmelCase ) for x in output_tokens] self.assertListEqual(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase ( self : Dict ) -> str: """simple docstring""" self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map ) , 1 ) self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values() )[0] ) , 1 )
370
'''simple docstring''' import unittest from transformers import AlbertConfig, is_torch_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, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, ) from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST class snake_case__ : """simple docstring""" def __init__( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int=13 , UpperCamelCase__ : Optional[int]=7 , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : Any=True , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : str=99 , UpperCamelCase__ : str=16 , UpperCamelCase__ : Dict=36 , UpperCamelCase__ : List[str]=6 , UpperCamelCase__ : Any=6 , UpperCamelCase__ : Any=6 , UpperCamelCase__ : Union[str, Any]=37 , UpperCamelCase__ : str="gelu" , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : Optional[int]=0.1 , UpperCamelCase__ : Optional[int]=512 , UpperCamelCase__ : List[str]=16 , UpperCamelCase__ : Tuple=2 , UpperCamelCase__ : Dict=0.02 , UpperCamelCase__ : Tuple=3 , UpperCamelCase__ : Union[str, Any]=4 , UpperCamelCase__ : Optional[int]=None , ) -> Optional[Any]: """simple docstring""" snake_case : Optional[Any] = parent snake_case : str = batch_size snake_case : Optional[Any] = seq_length snake_case : Optional[int] = is_training snake_case : Optional[int] = use_input_mask snake_case : List[Any] = use_token_type_ids snake_case : Tuple = use_labels snake_case : Optional[Any] = vocab_size snake_case : List[Any] = embedding_size snake_case : Any = hidden_size snake_case : Any = num_hidden_layers snake_case : Union[str, Any] = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : Any = intermediate_size snake_case : List[Any] = hidden_act snake_case : List[str] = hidden_dropout_prob snake_case : Union[str, Any] = attention_probs_dropout_prob snake_case : Dict = max_position_embeddings snake_case : Union[str, Any] = type_vocab_size snake_case : List[Any] = type_sequence_label_size snake_case : List[Any] = initializer_range snake_case : Union[str, Any] = num_labels snake_case : Optional[Any] = num_choices snake_case : Optional[int] = scope def lowerCAmelCase ( self : Any ) -> int: """simple docstring""" snake_case : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case : List[str] = None if self.use_input_mask: snake_case : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case : Optional[int] = None if self.use_token_type_ids: snake_case : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case : Union[str, Any] = None snake_case : List[Any] = None snake_case : int = None if self.use_labels: snake_case : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case : Tuple = ids_tensor([self.batch_size] , self.num_choices ) snake_case : List[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase ( self : int ) -> Tuple: """simple docstring""" return AlbertConfig( 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 , num_hidden_groups=self.num_hidden_groups , ) def lowerCAmelCase ( self : Union[str, Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : str , UpperCamelCase__ : Optional[Any] ) -> Optional[Any]: """simple docstring""" snake_case : Optional[int] = AlbertModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() snake_case : List[str] = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ ) snake_case : str = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ ) snake_case : int = model(UpperCamelCase__ ) 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 lowerCAmelCase ( self : Union[str, Any] , UpperCamelCase__ : str , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] ) -> Any: """simple docstring""" snake_case : Optional[Any] = AlbertForPreTraining(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() snake_case : List[Any] = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ , sentence_order_label=UpperCamelCase__ , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.sop_logits.shape , (self.batch_size, config.num_labels) ) def lowerCAmelCase ( self : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] ) -> Optional[Any]: """simple docstring""" snake_case : str = AlbertForMaskedLM(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() snake_case : Dict = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase ( self : Dict , UpperCamelCase__ : int , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : Dict ) -> Any: """simple docstring""" snake_case : int = AlbertForQuestionAnswering(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() snake_case : str = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , start_positions=UpperCamelCase__ , end_positions=UpperCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase ( self : Tuple , UpperCamelCase__ : List[str] , UpperCamelCase__ : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : str ) -> Union[str, Any]: """simple docstring""" snake_case : Optional[int] = self.num_labels snake_case : List[str] = AlbertForSequenceClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() snake_case : Dict = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase ( self : List[str] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : str ) -> Tuple: """simple docstring""" snake_case : Tuple = self.num_labels snake_case : int = AlbertForTokenClassification(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() snake_case : str = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase ( self : Any , UpperCamelCase__ : List[str] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : int , UpperCamelCase__ : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : int , UpperCamelCase__ : str ) -> Tuple: """simple docstring""" snake_case : int = self.num_choices snake_case : List[Any] = AlbertForMultipleChoice(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() snake_case : Tuple = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case : Union[str, Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case : Tuple = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case : Union[str, Any] = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase ( self : List[str] ) -> Any: """simple docstring""" snake_case : Optional[int] = self.prepare_config_and_inputs() ( ( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) ,( snake_case ) , ) : List[str] = config_and_inputs snake_case : Dict = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class snake_case__ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" lowerCamelCase = ( ( AlbertModel, AlbertForPreTraining, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertForQuestionAnswering, ) if is_torch_available() else () ) lowerCamelCase = ( { """feature-extraction""": AlbertModel, """fill-mask""": AlbertForMaskedLM, """question-answering""": AlbertForQuestionAnswering, """text-classification""": AlbertForSequenceClassification, """token-classification""": AlbertForTokenClassification, """zero-shot""": AlbertForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase = True def lowerCAmelCase ( self : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : Dict , UpperCamelCase__ : int=False ) -> Optional[Any]: """simple docstring""" snake_case : Any = super()._prepare_for_class(UpperCamelCase__ , UpperCamelCase__ , return_labels=UpperCamelCase__ ) if return_labels: if model_class in get_values(UpperCamelCase__ ): snake_case : Optional[int] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=UpperCamelCase__ ) snake_case : List[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCamelCase__ ) return inputs_dict def lowerCAmelCase ( self : Optional[Any] ) -> List[Any]: """simple docstring""" snake_case : Any = AlbertModelTester(self ) snake_case : Any = ConfigTester(self , config_class=UpperCamelCase__ , hidden_size=37 ) def lowerCAmelCase ( self : List[Any] ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def lowerCAmelCase ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" snake_case : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def lowerCAmelCase ( self : List[Any] ) -> Optional[Any]: """simple docstring""" snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*UpperCamelCase__ ) def lowerCAmelCase ( self : Any ) -> Optional[Any]: """simple docstring""" snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__ ) def lowerCAmelCase ( self : Tuple ) -> str: """simple docstring""" snake_case : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__ ) def lowerCAmelCase ( self : Any ) -> Tuple: """simple docstring""" snake_case : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__ ) def lowerCAmelCase ( self : Optional[int] ) -> Any: """simple docstring""" snake_case : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__ ) def lowerCAmelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" snake_case : Union[str, Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: snake_case : int = type self.model_tester.create_and_check_model(*UpperCamelCase__ ) @slow def lowerCAmelCase ( self : Tuple ) -> str: """simple docstring""" for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case : List[Any] = AlbertModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @require_torch class snake_case__ ( unittest.TestCase ): """simple docstring""" @slow def lowerCAmelCase ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" snake_case : Tuple = AlbertModel.from_pretrained('''albert-base-v2''' ) snake_case : Dict = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) snake_case : Union[str, Any] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): snake_case : Any = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ )[0] snake_case : int = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , UpperCamelCase__ ) snake_case : Any = torch.tensor( [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , UpperCamelCase__ , atol=1e-4 ) )
83
0
'''simple docstring''' import argparse import os import re lowercase__ = "src/diffusers" # Pattern that looks at the indentation in a line. lowercase__ = re.compile(r"^(\s*)\S") # Pattern that matches `"key":" and puts `key` in group 0. lowercase__ = re.compile(r"^\s*\"([^\"]+)\":") # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. lowercase__ = re.compile(r"^\s*_import_structure\[\"([^\"]+)\"\]") # Pattern that matches `"key",` and puts `key` in group 0. lowercase__ = re.compile(r"^\s*\"([^\"]+)\",\s*$") # Pattern that matches any `[stuff]` and puts `stuff` in group 0. lowercase__ = re.compile(r"\[([^\]]+)\]") def UpperCamelCase( UpperCAmelCase_ ): UpperCAmelCase : Dict = _re_indent.search(UpperCAmelCase_ ) return "" if search is None else search.groups()[0] def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_="" , UpperCAmelCase_=None , UpperCAmelCase_=None ): UpperCAmelCase : Tuple = 0 UpperCAmelCase : Any = code.split('\n' ) if start_prompt is not None: while not lines[index].startswith(UpperCAmelCase_ ): index += 1 UpperCAmelCase : Any = ['\n'.join(lines[:index] )] else: UpperCAmelCase : Union[str, Any] = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). UpperCAmelCase : Optional[int] = [lines[index]] index += 1 while index < len(UpperCAmelCase_ ) and (end_prompt is None or not lines[index].startswith(UpperCAmelCase_ )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(UpperCAmelCase_ ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + ' ' ): current_block.append(lines[index] ) blocks.append('\n'.join(UpperCAmelCase_ ) ) if index < len(UpperCAmelCase_ ) - 1: UpperCAmelCase : Tuple = [lines[index + 1]] index += 1 else: UpperCAmelCase : Any = [] else: blocks.append('\n'.join(UpperCAmelCase_ ) ) UpperCAmelCase : List[Any] = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(UpperCAmelCase_ ) > 0: blocks.append('\n'.join(UpperCAmelCase_ ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(UpperCAmelCase_ ): blocks.append('\n'.join(lines[index:] ) ) return blocks def UpperCamelCase( UpperCAmelCase_ ): def _inner(UpperCAmelCase_ ): return key(UpperCAmelCase_ ).lower().replace('_' , '' ) return _inner def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_=None ): # If no key is provided, we use a noop. def noop(UpperCAmelCase_ ): return x if key is None: UpperCAmelCase : List[Any] = noop # Constants are all uppercase, they go first. UpperCAmelCase : List[Any] = [obj for obj in objects if key(UpperCAmelCase_ ).isupper()] # Classes are not all uppercase but start with a capital, they go second. UpperCAmelCase : int = [obj for obj in objects if key(UpperCAmelCase_ )[0].isupper() and not key(UpperCAmelCase_ ).isupper()] # Functions begin with a lowercase, they go last. UpperCAmelCase : Tuple = [obj for obj in objects if not key(UpperCAmelCase_ )[0].isupper()] UpperCAmelCase : List[str] = ignore_underscore(UpperCAmelCase_ ) return sorted(UpperCAmelCase_ , key=UpperCAmelCase_ ) + sorted(UpperCAmelCase_ , key=UpperCAmelCase_ ) + sorted(UpperCAmelCase_ , key=UpperCAmelCase_ ) def UpperCamelCase( UpperCAmelCase_ ): # This inner function sort imports between [ ]. def _replace(UpperCAmelCase_ ): UpperCAmelCase : Union[str, Any] = match.groups()[0] if "," not in imports: return F"""[{imports}]""" UpperCAmelCase : Optional[Any] = [part.strip().replace('"' , '' ) for part in imports.split(',' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCAmelCase : Any = keys[:-1] return "[" + ", ".join([F"""\"{k}\"""" for k in sort_objects(UpperCAmelCase_ )] ) + "]" UpperCAmelCase : List[Any] = import_statement.split('\n' ) if len(UpperCAmelCase_ ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. UpperCAmelCase : Optional[int] = 2 if lines[1].strip() == '[' else 1 UpperCAmelCase : List[Any] = [(i, _re_strip_line.search(UpperCAmelCase_ ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] UpperCAmelCase : int = sort_objects(UpperCAmelCase_ , key=lambda UpperCAmelCase_ : x[1] ) UpperCAmelCase : Any = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(UpperCAmelCase_ ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: UpperCAmelCase : List[Any] = _re_bracket_content.sub(_replace , lines[1] ) else: UpperCAmelCase : Dict = [part.strip().replace('"' , '' ) for part in lines[1].split(',' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCAmelCase : str = keys[:-1] UpperCAmelCase : Optional[int] = get_indent(lines[1] ) + ', '.join([F"""\"{k}\"""" for k in sort_objects(UpperCAmelCase_ )] ) return "\n".join(UpperCAmelCase_ ) else: # Finally we have to deal with imports fitting on one line UpperCAmelCase : Optional[Any] = _re_bracket_content.sub(_replace , UpperCAmelCase_ ) return import_statement def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_=True ): with open(UpperCAmelCase_ , 'r' ) as f: UpperCAmelCase : str = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 UpperCAmelCase : Optional[Any] = split_code_in_indented_blocks( UpperCAmelCase_ , start_prompt='_import_structure = {' , end_prompt='if TYPE_CHECKING:' ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(UpperCAmelCase_ ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. UpperCAmelCase : List[Any] = main_blocks[block_idx] UpperCAmelCase : Dict = block.split('\n' ) # Get to the start of the imports. UpperCAmelCase : Any = 0 while line_idx < len(UpperCAmelCase_ ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: UpperCAmelCase : List[Any] = len(UpperCAmelCase_ ) else: line_idx += 1 if line_idx >= len(UpperCAmelCase_ ): continue # Ignore beginning and last line: they don't contain anything. UpperCAmelCase : List[str] = '\n'.join(block_lines[line_idx:-1] ) UpperCAmelCase : List[str] = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. UpperCAmelCase : str = split_code_in_indented_blocks(UpperCAmelCase_ , indent_level=UpperCAmelCase_ ) # We have two categories of import key: list or _import_structure[key].append/extend UpperCAmelCase : Dict = _re_direct_key if '_import_structure' in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. UpperCAmelCase : Optional[Any] = [(pattern.search(UpperCAmelCase_ ).groups()[0] if pattern.search(UpperCAmelCase_ ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. UpperCAmelCase : Optional[Any] = [(i, key) for i, key in enumerate(UpperCAmelCase_ ) if key is not None] UpperCAmelCase : Tuple = [x[0] for x in sorted(UpperCAmelCase_ , key=lambda UpperCAmelCase_ : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. UpperCAmelCase : int = 0 UpperCAmelCase : Dict = [] for i in range(len(UpperCAmelCase_ ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: UpperCAmelCase : int = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(UpperCAmelCase_ ) count += 1 # And we put our main block back together with its first and last line. UpperCAmelCase : Union[str, Any] = '\n'.join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(UpperCAmelCase_ ): if check_only: return True else: print(F"""Overwriting {file}.""" ) with open(UpperCAmelCase_ , 'w' ) as f: f.write('\n'.join(UpperCAmelCase_ ) ) def UpperCamelCase( UpperCAmelCase_=True ): UpperCAmelCase : str = [] for root, _, files in os.walk(UpperCAmelCase_ ): if "__init__.py" in files: UpperCAmelCase : Any = sort_imports(os.path.join(UpperCAmelCase_ , '__init__.py' ) , check_only=UpperCAmelCase_ ) if result: UpperCAmelCase : Any = [os.path.join(UpperCAmelCase_ , '__init__.py' )] if len(UpperCAmelCase_ ) > 0: raise ValueError(F"""Would overwrite {len(UpperCAmelCase_ )} files, run `make style`.""" ) if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() parser.add_argument("--check_only", action="store_true", help="Whether to only check or fix style.") lowercase__ = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
151
'''simple docstring''' from jiwer import compute_measures import datasets lowercase__ = "\\n@inproceedings{inproceedings,\n author = {Morris, Andrew and Maier, Viktoria and Green, Phil},\n year = {2004},\n month = {01},\n pages = {},\n title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}\n}\n" lowercase__ = "\\nWord error rate (WER) is a common metric of the performance of an automatic speech recognition system.\n\nThe general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort.\n\nThis problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate.\n\nWord error rate can then be computed as:\n\nWER = (S + D + I) / N = (S + D + I) / (S + D + C)\n\nwhere\n\nS is the number of substitutions,\nD is the number of deletions,\nI is the number of insertions,\nC is the number of correct words,\nN is the number of words in the reference (N=S+D+C).\n\nThis value indicates the average number of errors per reference word. The lower the value, the better the\nperformance of the ASR system with a WER of 0 being a perfect score.\n" lowercase__ = "\nCompute WER score of transcribed segments against references.\n\nArgs:\n references: List of references for each speech input.\n predictions: List of transcriptions to score.\n concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively.\n\nReturns:\n (float): the word error rate\n\nExamples:\n\n >>> predictions = [\"this is the prediction\", \"there is an other sample\"]\n >>> references = [\"this is the reference\", \"there is another one\"]\n >>> wer = datasets.load_metric(\"wer\")\n >>> wer_score = wer.compute(predictions=predictions, references=references)\n >>> print(wer_score)\n 0.5\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A_ ( datasets.Metric ): '''simple docstring''' def UpperCAmelCase_ ( self : Any ) -> Union[str, Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Value('string' , id='sequence' ), } ) , codebase_urls=['https://github.com/jitsi/jiwer/'] , reference_urls=[ 'https://en.wikipedia.org/wiki/Word_error_rate', ] , ) def UpperCAmelCase_ ( self : List[str] , lowercase_ : int=None , lowercase_ : List[Any]=None , lowercase_ : Union[str, Any]=False ) -> Any: if concatenate_texts: return compute_measures(lowercase_ , lowercase_ )["wer"] else: UpperCAmelCase : Optional[Any] = 0 UpperCAmelCase : List[Any] = 0 for prediction, reference in zip(lowercase_ , lowercase_ ): UpperCAmelCase : str = compute_measures(lowercase_ , lowercase_ ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
151
1
# Lint as: python3 import dataclasses import re from dataclasses import dataclass from functools import total_ordering from typing import Optional, Union lowerCamelCase_ = re.compile(r"""^(?P<major>\d+)""" r"""\.(?P<minor>\d+)""" r"""\.(?P<patch>\d+)$""") @total_ordering @dataclass class a_ : '''simple docstring''' __a: str __a: Optional[str] = None __a: Optional[Union[str, int]] = None __a: Optional[Union[str, int]] = None __a: Optional[Union[str, int]] = None def _lowercase ( self ) -> int: '''simple docstring''' lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = _str_to_version_tuple(self.version_str ) def __repr__( self ) -> Any: '''simple docstring''' return f'''{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}''' @property def _lowercase ( self ) -> int: '''simple docstring''' return self.major, self.minor, self.patch def _lowercase ( self , lowercase_ ) -> List[str]: '''simple docstring''' if isinstance(lowercase_ , lowercase_ ): return Version(lowercase_ ) elif isinstance(lowercase_ , lowercase_ ): return other raise TypeError(f'''{other} (type {type(lowercase_ )}) cannot be compared to version.''' ) def __eq__( self , lowercase_ ) -> List[Any]: '''simple docstring''' try: lowerCAmelCase_ = self._validate_operand(lowercase_ ) except (TypeError, ValueError): return False else: return self.tuple == other.tuple def __lt__( self , lowercase_ ) -> Dict: '''simple docstring''' lowerCAmelCase_ = self._validate_operand(lowercase_ ) return self.tuple < other.tuple def __hash__( self ) -> Optional[int]: '''simple docstring''' return hash(_version_tuple_to_str(self.tuple ) ) @classmethod def _lowercase ( cls , lowercase_ ) -> str: '''simple docstring''' lowerCAmelCase_ = {f.name for f in dataclasses.fields(cls )} return cls(**{k: v for k, v in dic.items() if k in field_names} ) def _lowercase ( self ) -> str: '''simple docstring''' return self.version_str def lowerCamelCase ( a_ ) -> List[Any]: lowerCAmelCase_ = _VERSION_REG.match(a_ ) if not res: raise ValueError(F'''Invalid version \'{version_str}\'. Format should be x.y.z with {{x,y,z}} being digits.''' ) return tuple(int(a_ ) for v in [res.group('major' ), res.group('minor' ), res.group('patch' )] ) def lowerCamelCase ( a_ ) -> Union[str, Any]: return ".".join(str(a_ ) for v in version_tuple )
14
# Copyright 2021 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 packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) lowerCamelCase_ = """pytorch_model.bin""" lowerCamelCase_ = """pytorch_model.bin.index.json""" lowerCamelCase_ = """adapter_config.json""" lowerCamelCase_ = """adapter_model.bin""" lowerCamelCase_ = """adapter_model.safetensors""" lowerCamelCase_ = """tf_model.h5""" lowerCamelCase_ = """tf_model.h5.index.json""" lowerCamelCase_ = """model.ckpt""" lowerCamelCase_ = """flax_model.msgpack""" lowerCamelCase_ = """flax_model.msgpack.index.json""" lowerCamelCase_ = """model.safetensors""" lowerCamelCase_ = """model.safetensors.index.json""" lowerCamelCase_ = """config.json""" lowerCamelCase_ = """preprocessor_config.json""" lowerCamelCase_ = FEATURE_EXTRACTOR_NAME lowerCamelCase_ = """generation_config.json""" lowerCamelCase_ = """modelcard.json""" lowerCamelCase_ = """▁""" lowerCamelCase_ = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility lowerCamelCase_ = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. lowerCamelCase_ = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] lowerCamelCase_ = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def lowerCamelCase ( a_ ) -> Dict: if version.parse(a_ ) < version.parse(a_ ): if "dev" in min_version: lowerCAmelCase_ = ( 'This example requires a source install from HuggingFace Transformers (see ' '`https://huggingface.co/docs/transformers/installation#install-from-source`),' ) else: lowerCAmelCase_ = F'''This example requires a minimum version of {min_version},''' error_message += F''' but the version found is {__version__}.\n''' raise ImportError( error_message + 'Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other ' 'versions of HuggingFace Transformers.' )
14
1
import argparse import json from collections import OrderedDict from functools import partial from pathlib import Path import timm import torch from huggingface_hub import hf_hub_download from transformers import LevitConfig, LevitForImageClassificationWithTeacher, LevitImageProcessor from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase : Dict = logging.get_logger() def __lowerCamelCase ( lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Tuple , lowerCamelCase__ : List[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : Optional[int] = True ): '''simple docstring''' print(f'Converting {name}...' ) with torch.no_grad(): if hidden_sizes == 128: if name[-1] == "S": lowerCamelCase = timm.create_model("""levit_128s""" , pretrained=lowerCamelCase__ ) else: lowerCamelCase = timm.create_model("""levit_128""" , pretrained=lowerCamelCase__ ) if hidden_sizes == 192: lowerCamelCase = timm.create_model("""levit_192""" , pretrained=lowerCamelCase__ ) if hidden_sizes == 256: lowerCamelCase = timm.create_model("""levit_256""" , pretrained=lowerCamelCase__ ) if hidden_sizes == 384: lowerCamelCase = timm.create_model("""levit_384""" , pretrained=lowerCamelCase__ ) from_model.eval() lowerCamelCase = LevitForImageClassificationWithTeacher(lowerCamelCase__ ).eval() lowerCamelCase = OrderedDict() lowerCamelCase = from_model.state_dict() lowerCamelCase = list(from_model.state_dict().keys() ) lowerCamelCase = list(our_model.state_dict().keys() ) print(len(lowerCamelCase__ ) , len(lowerCamelCase__ ) ) for i in range(len(lowerCamelCase__ ) ): lowerCamelCase = weights[og_keys[i]] our_model.load_state_dict(lowerCamelCase__ ) lowerCamelCase = torch.randn((2, 3, 224, 224) ) lowerCamelCase = from_model(lowerCamelCase__ ) lowerCamelCase = our_model(lowerCamelCase__ ).logits assert torch.allclose(lowerCamelCase__ , lowerCamelCase__ ), "The model logits don't match the original one." lowerCamelCase = name print(lowerCamelCase__ ) if push_to_hub: our_model.save_pretrained(save_directory / checkpoint_name ) lowerCamelCase = LevitImageProcessor() image_processor.save_pretrained(save_directory / checkpoint_name ) print(f'Pushed {checkpoint_name}' ) def __lowerCamelCase ( lowerCamelCase__ : Tuple , lowerCamelCase__ : Tuple = None , lowerCamelCase__ : Any = True ): '''simple docstring''' lowerCamelCase = "imagenet-1k-id2label.json" lowerCamelCase = 1000 lowerCamelCase = (1, num_labels) lowerCamelCase = "huggingface/label-files" lowerCamelCase = num_labels lowerCamelCase = json.load(open(hf_hub_download(lowerCamelCase__ , lowerCamelCase__ , repo_type="""dataset""" ) , """r""" ) ) lowerCamelCase = {int(lowerCamelCase__ ): v for k, v in idalabel.items()} lowerCamelCase = idalabel lowerCamelCase = {v: k for k, v in idalabel.items()} lowerCamelCase = partial(lowerCamelCase__ , num_labels=lowerCamelCase__ , idalabel=lowerCamelCase__ , labelaid=lowerCamelCase__ ) lowerCamelCase = { "levit-128S": 128, "levit-128": 128, "levit-192": 192, "levit-256": 256, "levit-384": 384, } lowerCamelCase = { "levit-128S": ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 6, 8] , depths=[2, 3, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), "levit-128": ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 8, 12] , depths=[4, 4, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), "levit-192": ImageNetPreTrainedConfig( hidden_sizes=[192, 288, 384] , num_attention_heads=[3, 5, 6] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), "levit-256": ImageNetPreTrainedConfig( hidden_sizes=[256, 384, 512] , num_attention_heads=[4, 6, 8] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), "levit-384": ImageNetPreTrainedConfig( hidden_sizes=[384, 512, 768] , num_attention_heads=[6, 9, 12] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0.1 , ), } if model_name: convert_weight_and_push( names_to_hidden_sizes[model_name] , lowerCamelCase__ , names_to_config[model_name] , lowerCamelCase__ , lowerCamelCase__ ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(names_to_hidden_sizes[model_name] , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) return config, expected_shape if __name__ == "__main__": UpperCAmelCase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default=None, type=str, help="The name of the model you wish to convert, it must be one of the supported Levit* architecture,", ) parser.add_argument( "--pytorch_dump_folder_path", default="levit-dump-folder/", type=Path, required=False, help="Path to the output PyTorch model directory.", ) parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub") parser.add_argument( "--no-push_to_hub", dest="push_to_hub", action="store_false", help="Do not push model and image processor to the hub", ) UpperCAmelCase : List[str] = parser.parse_args() UpperCAmelCase : str = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
252
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } lowerCAmelCase__ = { '''b0''': { '''hidden_dim''': 1_2_8_0, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 2_2_4, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 1_2_8_0, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 2_4_0, '''dropout_rate''': 0.2, '''dw_padding''': [1_6], }, '''b2''': { '''hidden_dim''': 1_4_0_8, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 2_6_0, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 1_6], }, '''b3''': { '''hidden_dim''': 1_5_3_6, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 3_0_0, '''dropout_rate''': 0.3, '''dw_padding''': [5, 1_8], }, '''b4''': { '''hidden_dim''': 1_7_9_2, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 3_8_0, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 2_0_4_8, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 4_5_6, '''dropout_rate''': 0.4, '''dw_padding''': [1_3, 2_7], }, '''b6''': { '''hidden_dim''': 2_3_0_4, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 5_2_8, '''dropout_rate''': 0.5, '''dw_padding''': [3_1], }, '''b7''': { '''hidden_dim''': 2_5_6_0, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 6_0_0, '''dropout_rate''': 0.5, '''dw_padding''': [1_8], }, } def __lowerCamelCase ( lowerCamelCase__ ): """simple docstring""" lowercase__ : Dict = EfficientNetConfig() lowercase__ : int = CONFIG_MAP[model_name]["hidden_dim"] lowercase__ : Any = CONFIG_MAP[model_name]["width_coef"] lowercase__ : Optional[Any] = CONFIG_MAP[model_name]["depth_coef"] lowercase__ : List[str] = CONFIG_MAP[model_name]["image_size"] lowercase__ : List[Any] = CONFIG_MAP[model_name]["dropout_rate"] lowercase__ : Optional[int] = CONFIG_MAP[model_name]["dw_padding"] lowercase__ : Optional[int] = "huggingface/label-files" lowercase__ : Any = "imagenet-1k-id2label.json" lowercase__ : List[Any] = 1_000 lowercase__ : List[Any] = json.load(open(hf_hub_download(lowerCamelCase__ , lowerCamelCase__ , repo_type="dataset" ) , "r" ) ) lowercase__ : List[str] = {int(lowerCamelCase__ ): v for k, v in idalabel.items()} lowercase__ : List[str] = idalabel lowercase__ : Dict = {v: k for k, v in idalabel.items()} return config def __lowerCamelCase ( ): """simple docstring""" lowercase__ : Tuple = "http://images.cocodataset.org/val2017/000000039769.jpg" lowercase__ : str = Image.open(requests.get(lowerCamelCase__ , stream=lowerCamelCase__ ).raw ) return im def __lowerCamelCase ( lowerCamelCase__ ): """simple docstring""" lowercase__ : Tuple = CONFIG_MAP[model_name]["image_size"] lowercase__ : Optional[int] = EfficientNetImageProcessor( size={"height": size, "width": size} , image_mean=[0.485, 0.456, 0.406] , image_std=[0.47853944, 0.4732864, 0.47434163] , do_center_crop=lowerCamelCase__ , ) return preprocessor def __lowerCamelCase ( lowerCamelCase__ ): """simple docstring""" lowercase__ : Tuple = [v.split("_" )[0].split("block" )[1] for v in original_param_names if v.startswith("block" )] lowercase__ : Any = sorted(set(lowerCamelCase__ ) ) lowercase__ : List[Any] = len(lowerCamelCase__ ) lowercase__ : Dict = {b: str(lowerCamelCase__ ) for b, i in zip(lowerCamelCase__ , range(lowerCamelCase__ ) )} lowercase__ : List[str] = [] rename_keys.append(("stem_conv/kernel:0", "embeddings.convolution.weight") ) rename_keys.append(("stem_bn/gamma:0", "embeddings.batchnorm.weight") ) rename_keys.append(("stem_bn/beta:0", "embeddings.batchnorm.bias") ) rename_keys.append(("stem_bn/moving_mean:0", "embeddings.batchnorm.running_mean") ) rename_keys.append(("stem_bn/moving_variance:0", "embeddings.batchnorm.running_var") ) for b in block_names: lowercase__ : Tuple = block_name_mapping[b] rename_keys.append((F"""block{b}_expand_conv/kernel:0""", F"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""") ) rename_keys.append((F"""block{b}_expand_bn/gamma:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""") ) rename_keys.append((F"""block{b}_expand_bn/beta:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""") ) rename_keys.append( (F"""block{b}_expand_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""") ) rename_keys.append( (F"""block{b}_dwconv/depthwise_kernel:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""") ) rename_keys.append((F"""block{b}_bn/gamma:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""") ) rename_keys.append((F"""block{b}_bn/beta:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""") ) rename_keys.append( (F"""block{b}_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""") ) rename_keys.append( (F"""block{b}_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""") ) rename_keys.append((F"""block{b}_se_reduce/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""") ) rename_keys.append((F"""block{b}_se_reduce/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""") ) rename_keys.append((F"""block{b}_se_expand/kernel:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""") ) rename_keys.append((F"""block{b}_se_expand/bias:0""", F"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""") ) rename_keys.append( (F"""block{b}_project_conv/kernel:0""", F"""encoder.blocks.{hf_b}.projection.project_conv.weight""") ) rename_keys.append((F"""block{b}_project_bn/gamma:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.weight""") ) rename_keys.append((F"""block{b}_project_bn/beta:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.bias""") ) rename_keys.append( (F"""block{b}_project_bn/moving_mean:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""") ) rename_keys.append( (F"""block{b}_project_bn/moving_variance:0""", F"""encoder.blocks.{hf_b}.projection.project_bn.running_var""") ) rename_keys.append(("top_conv/kernel:0", "encoder.top_conv.weight") ) rename_keys.append(("top_bn/gamma:0", "encoder.top_bn.weight") ) rename_keys.append(("top_bn/beta:0", "encoder.top_bn.bias") ) rename_keys.append(("top_bn/moving_mean:0", "encoder.top_bn.running_mean") ) rename_keys.append(("top_bn/moving_variance:0", "encoder.top_bn.running_var") ) lowercase__ : List[str] = {} for item in rename_keys: if item[0] in original_param_names: lowercase__ : Tuple = "efficientnet." + item[1] lowercase__ : Union[str, Any] = "classifier.weight" lowercase__ : List[Any] = "classifier.bias" return key_mapping def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" for key, value in tf_params.items(): if "normalization" in key: continue lowercase__ : List[str] = key_mapping[key] if "_conv" in key and "kernel" in key: lowercase__ : int = torch.from_numpy(lowerCamelCase__ ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: lowercase__ : Any = torch.from_numpy(lowerCamelCase__ ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: lowercase__ : List[Any] = torch.from_numpy(np.transpose(lowerCamelCase__ ) ) else: lowercase__ : Tuple = torch.from_numpy(lowerCamelCase__ ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(lowerCamelCase__ ) @torch.no_grad() def __lowerCamelCase ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" lowercase__ : Optional[int] = model_classes[model_name]( include_top=lowerCamelCase__ , weights="imagenet" , input_tensor=lowerCamelCase__ , input_shape=lowerCamelCase__ , pooling=lowerCamelCase__ , classes=1_000 , classifier_activation="softmax" , ) lowercase__ : int = original_model.trainable_variables lowercase__ : str = original_model.non_trainable_variables lowercase__ : Optional[Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: lowercase__ : Tuple = param.numpy() lowercase__ : Optional[Any] = list(tf_params.keys() ) # Load HuggingFace model lowercase__ : int = get_efficientnet_config(lowerCamelCase__ ) lowercase__ : int = EfficientNetForImageClassification(lowerCamelCase__ ).eval() lowercase__ : Tuple = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print("Converting parameters..." ) lowercase__ : str = rename_keys(lowerCamelCase__ ) replace_params(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # Initialize preprocessor and preprocess input image lowercase__ : Any = convert_image_processor(lowerCamelCase__ ) lowercase__ : int = preprocessor(images=prepare_img() , return_tensors="pt" ) # HF model inference hf_model.eval() with torch.no_grad(): lowercase__ : Optional[int] = hf_model(**lowerCamelCase__ ) lowercase__ : List[Any] = outputs.logits.detach().numpy() # Original model inference lowercase__ : Optional[int] = False lowercase__ : Any = CONFIG_MAP[model_name]["image_size"] lowercase__ : List[str] = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) lowercase__ : Union[str, Any] = image.img_to_array(lowerCamelCase__ ) lowercase__ : List[Any] = np.expand_dims(lowerCamelCase__ , axis=0 ) lowercase__ : List[Any] = original_model.predict(lowerCamelCase__ ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(lowerCamelCase__ , lowerCamelCase__ , atol=1e-3 ), "The predicted logits are not the same." print("Model outputs match!" ) if save_model: # Create folder to save model if not os.path.isdir(lowerCamelCase__ ): os.mkdir(lowerCamelCase__ ) # Save converted model and image processor hf_model.save_pretrained(lowerCamelCase__ ) preprocessor.save_pretrained(lowerCamelCase__ ) if push_to_hub: # Push model and image processor to hub print(F"""Pushing converted {model_name} to the hub...""" ) lowercase__ : List[str] = F"""efficientnet-{model_name}""" preprocessor.push_to_hub(lowerCamelCase__ ) hf_model.push_to_hub(lowerCamelCase__ ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') lowerCAmelCase__ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
130
0
from . import __version__ # Backward compatibility imports, to make sure all those objects can be found in file_utils from .utils import ( CLOUDFRONT_DISTRIB_PREFIX, CONFIG_NAME, DISABLE_TELEMETRY, DUMMY_INPUTS, DUMMY_MASK, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, FEATURE_EXTRACTOR_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MODEL_CARD_NAME, MULTIPLE_CHOICE_DUMMY_INPUTS, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, SENTENCEPIECE_UNDERLINE, SPIECE_UNDERLINE, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME, TORCH_FX_REQUIRED_VERSION, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, USE_JAX, USE_TF, USE_TORCH, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ContextManagers, DummyObject, EntryNotFoundError, ExplicitEnum, ModelOutput, PaddingStrategy, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, TensorType, _LazyModule, add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, cached_property, copy_func, default_cache_path, define_sagemaker_information, get_cached_models, get_file_from_repo, get_full_repo_name, get_torch_version, has_file, http_user_agent, is_apex_available, is_bsa_available, is_coloredlogs_available, is_datasets_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_librosa_available, is_offline_mode, is_onnx_available, is_pandas_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytorch_quantization_available, is_rjieba_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_tensor, is_tensorflow_probability_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_training_run_on_sagemaker, is_vision_available, replace_return_docstrings, requires_backends, to_numpy, to_py_obj, torch_only_method, )
250
from __future__ import annotations from math import pi, sqrt def _a ( lowerCamelCase: float , lowerCamelCase: float ) -> tuple: '''simple docstring''' if inductance <= 0: raise ValueError('''Inductance cannot be 0 or negative''' ) elif capacitance <= 0: raise ValueError('''Capacitance cannot be 0 or negative''' ) else: return ( "Resonant frequency", float(1 / (2 * pi * (sqrt(inductance * capacitance ))) ), ) if __name__ == "__main__": import doctest doctest.testmod()
250
1
"""simple docstring""" from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( "pipelines_utils", "0.22.0", "Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.", standard_warn=False, stacklevel=3, )
213
"""simple docstring""" def UpperCAmelCase__ (lowerCAmelCase_ = 100_0000 ): '''simple docstring''' __SCREAMING_SNAKE_CASE = set(range(3 , lowerCAmelCase_ , 2 ) ) primes.add(2 ) for p in range(3 , lowerCAmelCase_ , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , lowerCAmelCase_ , lowerCAmelCase_ ) ) ) __SCREAMING_SNAKE_CASE = [float(lowerCAmelCase_ ) for n in range(limit + 1 )] for p in primes: for n in range(lowerCAmelCase_ , limit + 1 , lowerCAmelCase_ ): phi[n] *= 1 - 1 / p return int(sum(phi[2:] ) ) if __name__ == "__main__": print(F"{solution() = }")
54
0
import math def snake_case_ ( snake_case ) -> bool: 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(snake_case ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def snake_case_ ( snake_case = 1_00_01 ) -> int: try: lowercase__: Optional[Any] = int(snake_case ) except (TypeError, ValueError): raise TypeError('Parameter nth must be int or castable to int.' ) from None if nth <= 0: raise ValueError('Parameter nth must be greater than or equal to one.' ) lowercase__: list[int] = [] lowercase__: List[Any] = 2 while len(snake_case ) < nth: if is_prime(snake_case ): primes.append(snake_case ) num += 1 else: num += 1 return primes[len(snake_case ) - 1] if __name__ == "__main__": print(F'''{solution() = }''')
288
import argparse import os import jax as jnp import numpy as onp import torch import torch.nn as nn from music_spectrogram_diffusion import inference from tax import checkpoints from diffusers import DDPMScheduler, OnnxRuntimeModel, SpectrogramDiffusionPipeline from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder __lowerCAmelCase = '''base_with_context''' def snake_case_ ( snake_case , snake_case ) -> int: lowercase__: Tuple = nn.Parameter(torch.FloatTensor(weights['token_embedder']['embedding'] ) ) lowercase__: Optional[int] = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=snake_case ) for lyr_num, lyr in enumerate(model.encoders ): lowercase__: List[str] = weights[f'layers_{lyr_num}'] lowercase__: List[Any] = nn.Parameter( torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale'] ) ) lowercase__: Any = ly_weight['attention'] lowercase__: int = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: int = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: List[Any] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) lowercase__: Optional[Any] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale'] ) ) return model def snake_case_ ( snake_case , snake_case ) -> List[str]: lowercase__: str = nn.Parameter(torch.FloatTensor(weights['input_proj']['kernel'].T ) ) lowercase__: Dict = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=snake_case ) for lyr_num, lyr in enumerate(model.encoders ): lowercase__: str = weights[f'layers_{lyr_num}'] lowercase__: Optional[Any] = ly_weight['attention'] lowercase__: List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: int = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: Union[str, Any] = nn.Parameter( torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale'] ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) lowercase__: Tuple = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) lowercase__: Optional[int] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale'] ) ) return model def snake_case_ ( snake_case , snake_case ) -> Any: lowercase__: int = nn.Parameter(torch.FloatTensor(weights['time_emb_dense0']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(weights['time_emb_dense1']['kernel'].T ) ) lowercase__: int = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=snake_case ) lowercase__: Dict = nn.Parameter( torch.FloatTensor(weights['continuous_inputs_projection']['kernel'].T ) ) for lyr_num, lyr in enumerate(model.decoders ): lowercase__: Optional[Any] = weights[f'layers_{lyr_num}'] lowercase__: Any = nn.Parameter( torch.FloatTensor(ly_weight['pre_self_attention_layer_norm']['scale'] ) ) lowercase__: int = nn.Parameter( torch.FloatTensor(ly_weight['FiLMLayer_0']['DenseGeneral_0']['kernel'].T ) ) lowercase__: List[str] = ly_weight['self_attention'] lowercase__: Optional[int] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: Tuple = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Union[str, Any] = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: int = ly_weight['MultiHeadDotProductAttention_0'] lowercase__: List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: str = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: int = nn.Parameter( torch.FloatTensor(ly_weight['pre_cross_attention_layer_norm']['scale'] ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) lowercase__: Union[str, Any] = nn.Parameter( torch.FloatTensor(ly_weight['FiLMLayer_1']['DenseGeneral_0']['kernel'].T ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) lowercase__: int = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) lowercase__: str = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) lowercase__: Optional[Any] = nn.Parameter(torch.FloatTensor(weights['decoder_norm']['scale'] ) ) lowercase__: Union[str, Any] = nn.Parameter(torch.FloatTensor(weights['spec_out_dense']['kernel'].T ) ) return model def snake_case_ ( snake_case ) -> Any: lowercase__: int = checkpoints.load_tax_checkpoint(args.checkpoint_path ) lowercase__: Tuple = jnp.tree_util.tree_map(onp.array , snake_case ) lowercase__: List[str] = [ 'from __gin__ import dynamic_registration', 'from music_spectrogram_diffusion.models.diffusion import diffusion_utils', 'diffusion_utils.ClassifierFreeGuidanceConfig.eval_condition_weight = 2.0', 'diffusion_utils.DiffusionConfig.classifier_free_guidance = @diffusion_utils.ClassifierFreeGuidanceConfig()', ] lowercase__: List[Any] = os.path.join(args.checkpoint_path , '..' , 'config.gin' ) lowercase__: Optional[Any] = inference.parse_training_gin_file(snake_case , snake_case ) lowercase__: str = inference.InferenceModel(args.checkpoint_path , snake_case ) lowercase__: Dict = DDPMScheduler(beta_schedule='squaredcos_cap_v2' , variance_type='fixed_large' ) lowercase__: List[Any] = SpectrogramNotesEncoder( max_length=synth_model.sequence_length['inputs'] , vocab_size=synth_model.model.module.config.vocab_size , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='gated-gelu' , ) lowercase__: Dict = SpectrogramContEncoder( input_dims=synth_model.audio_codec.n_dims , targets_context_length=synth_model.sequence_length['targets_context'] , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='gated-gelu' , ) lowercase__: Optional[Any] = TaFilmDecoder( input_dims=synth_model.audio_codec.n_dims , targets_length=synth_model.sequence_length['targets_context'] , max_decoder_noise_time=synth_model.model.module.config.max_decoder_noise_time , d_model=synth_model.model.module.config.emb_dim , num_layers=synth_model.model.module.config.num_decoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , dropout_rate=synth_model.model.module.config.dropout_rate , ) lowercase__: Dict = load_notes_encoder(ta_checkpoint['target']['token_encoder'] , snake_case ) lowercase__: int = load_continuous_encoder(ta_checkpoint['target']['continuous_encoder'] , snake_case ) lowercase__: Optional[int] = load_decoder(ta_checkpoint['target']['decoder'] , snake_case ) lowercase__: int = OnnxRuntimeModel.from_pretrained('kashif/soundstream_mel_decoder' ) lowercase__: List[Any] = SpectrogramDiffusionPipeline( notes_encoder=snake_case , continuous_encoder=snake_case , decoder=snake_case , scheduler=snake_case , melgan=snake_case , ) if args.save: pipe.save_pretrained(args.output_path ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('''--output_path''', default=None, type=str, required=True, help='''Path to the converted model.''') parser.add_argument( '''--save''', default=True, type=bool, required=False, help='''Whether to save the converted model or not.''' ) parser.add_argument( '''--checkpoint_path''', default=F'''{MODEL}/checkpoint_500000''', type=str, required=False, help='''Path to the original jax model checkpoint.''', ) __lowerCAmelCase = parser.parse_args() main(args)
288
1
'''simple docstring''' from collections.abc import Generator def A__ ( ): _UpperCamelCase , _UpperCamelCase : Tuple = 0, 1 while True: _UpperCamelCase , _UpperCamelCase : Union[str, Any] = b, a + b yield b def A__ ( UpperCAmelCase_ = 1_0_0_0 ): _UpperCamelCase : List[Any] = 1 _UpperCamelCase : str = fibonacci_generator() while len(str(next(UpperCAmelCase_ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
83
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case : Tuple = logging.get_logger(__name__) _snake_case : Optional[Any] = { "facebook/data2vec-vision-base-ft": ( "https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json" ), } class a (_lowerCAmelCase ): """simple docstring""" __UpperCAmelCase : Any = "data2vec-vision" def __init__( self : Optional[int] , lowerCamelCase : int=768 , lowerCamelCase : Optional[Any]=12 , lowerCamelCase : Optional[int]=12 , lowerCamelCase : Union[str, Any]=3072 , lowerCamelCase : Tuple="gelu" , lowerCamelCase : List[Any]=0.0 , lowerCamelCase : Tuple=0.0 , lowerCamelCase : List[Any]=0.02 , lowerCamelCase : int=1E-12 , lowerCamelCase : Optional[int]=224 , lowerCamelCase : List[str]=16 , lowerCamelCase : str=3 , lowerCamelCase : Any=False , lowerCamelCase : Tuple=False , lowerCamelCase : List[str]=False , lowerCamelCase : Optional[int]=False , lowerCamelCase : List[Any]=0.1 , lowerCamelCase : Optional[Any]=0.1 , lowerCamelCase : Dict=True , lowerCamelCase : Tuple=[3, 5, 7, 11] , lowerCamelCase : Union[str, Any]=[1, 2, 3, 6] , lowerCamelCase : List[str]=True , lowerCamelCase : int=0.4 , lowerCamelCase : Optional[int]=256 , lowerCamelCase : Tuple=1 , lowerCamelCase : Tuple=False , lowerCamelCase : Any=255 , **lowerCamelCase : str , ) -> Optional[int]: super().__init__(**lowerCamelCase ) __snake_case : Dict = hidden_size __snake_case : str = num_hidden_layers __snake_case : str = num_attention_heads __snake_case : Tuple = intermediate_size __snake_case : int = hidden_act __snake_case : Optional[int] = hidden_dropout_prob __snake_case : Any = attention_probs_dropout_prob __snake_case : Union[str, Any] = initializer_range __snake_case : Optional[Any] = layer_norm_eps __snake_case : Tuple = image_size __snake_case : Tuple = patch_size __snake_case : Optional[Any] = num_channels __snake_case : Optional[Any] = use_mask_token __snake_case : Dict = use_absolute_position_embeddings __snake_case : Optional[Any] = use_relative_position_bias __snake_case : Any = use_shared_relative_position_bias __snake_case : Union[str, Any] = layer_scale_init_value __snake_case : List[Any] = drop_path_rate __snake_case : Any = use_mean_pooling # decode head attributes (semantic segmentation) __snake_case : Optional[int] = out_indices __snake_case : List[str] = pool_scales # auxiliary head attributes (semantic segmentation) __snake_case : int = use_auxiliary_head __snake_case : Optional[Any] = auxiliary_loss_weight __snake_case : Optional[int] = auxiliary_channels __snake_case : str = auxiliary_num_convs __snake_case : Any = auxiliary_concat_input __snake_case : Optional[Any] = semantic_loss_ignore_index class a (_lowerCAmelCase ): """simple docstring""" __UpperCAmelCase : Union[str, Any] = version.parse("1.11" ) @property def __snake_case ( self : int ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __snake_case ( self : List[Any] ) -> float: return 1E-4
123
0
def _a ( lowerCamelCase: int = 10_00 ) -> int: '''simple docstring''' __A = 2**power __A = str(__lowerCAmelCase ) __A = list(__lowerCAmelCase ) __A = 0 for i in list_num: sum_of_num += int(__lowerCAmelCase ) return sum_of_num if __name__ == "__main__": snake_case__ : str = int(input('Enter the power of 2: ').strip()) print('2 ^ ', power, ' = ', 2**power) snake_case__ : Optional[int] = solution(power) print('Sum of the digits is: ', result)
353
import contextlib import importlib import io import unittest import transformers # Try to import everything from transformers to ensure every object can be loaded. from transformers import * # noqa F406 from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available if is_torch_available(): from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification if is_tf_available(): from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification if is_flax_available(): from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification snake_case__ : Dict = DUMMY_UNKNOWN_IDENTIFIER # An actual model hosted on huggingface.co snake_case__ : Any = 'main' # Default branch name snake_case__ : Union[str, Any] = 'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2' # One particular commit (not the top of `main`) snake_case__ : Optional[int] = 'aaaaaaa' # This commit does not exist, so we should 404. snake_case__ : int = 'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684' # Sha-1 of config.json on the top of `main`, for checking purposes snake_case__ : Any = '4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3' @contextlib.contextmanager def _a ( ) -> Tuple: '''simple docstring''' print('''Welcome!''' ) yield print('''Bye!''' ) @contextlib.contextmanager def _a ( ) -> Optional[int]: '''simple docstring''' print('''Bonjour!''' ) yield print('''Au revoir!''' ) class A_ ( unittest.TestCase ): def _lowerCAmelCase (self :Any )-> Optional[Any]: # If the spec is missing, importlib would not be able to import the module dynamically. assert transformers.__spec__ is not None assert importlib.util.find_spec('''transformers''' ) is not None class A_ ( unittest.TestCase ): @unittest.mock.patch('''sys.stdout''' , new_callable=io.StringIO ) def _lowerCAmelCase (self :str , _UpperCamelCase :str )-> Optional[int]: with ContextManagers([] ): print('''Transformers are awesome!''' ) # The print statement adds a new line at the end of the output self.assertEqual(mock_stdout.getvalue() , '''Transformers are awesome!\n''' ) @unittest.mock.patch('''sys.stdout''' , new_callable=io.StringIO ) def _lowerCAmelCase (self :Optional[int] , _UpperCamelCase :List[Any] )-> Union[str, Any]: with ContextManagers([context_en()] ): print('''Transformers are awesome!''' ) # The output should be wrapped with an English welcome and goodbye self.assertEqual(mock_stdout.getvalue() , '''Welcome!\nTransformers are awesome!\nBye!\n''' ) @unittest.mock.patch('''sys.stdout''' , new_callable=io.StringIO ) def _lowerCAmelCase (self :int , _UpperCamelCase :Union[str, Any] )-> int: with ContextManagers([context_fr(), context_en()] ): print('''Transformers are awesome!''' ) # The output should be wrapped with an English and French welcome and goodbye self.assertEqual(mock_stdout.getvalue() , '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n''' ) @require_torch def _lowerCAmelCase (self :int )-> str: self.assertEqual(find_labels(_UpperCamelCase ) , ['''labels'''] ) self.assertEqual(find_labels(_UpperCamelCase ) , ['''labels''', '''next_sentence_label'''] ) self.assertEqual(find_labels(_UpperCamelCase ) , ['''start_positions''', '''end_positions'''] ) class A_ ( _lowerCamelCase ): pass self.assertEqual(find_labels(_UpperCamelCase ) , ['''labels'''] ) @require_tf def _lowerCAmelCase (self :Any )-> str: self.assertEqual(find_labels(_UpperCamelCase ) , ['''labels'''] ) self.assertEqual(find_labels(_UpperCamelCase ) , ['''labels''', '''next_sentence_label'''] ) self.assertEqual(find_labels(_UpperCamelCase ) , ['''start_positions''', '''end_positions'''] ) class A_ ( _lowerCamelCase ): pass self.assertEqual(find_labels(_UpperCamelCase ) , ['''labels'''] ) @require_flax def _lowerCAmelCase (self :Optional[int] )-> Dict: # Flax models don't have labels self.assertEqual(find_labels(_UpperCamelCase ) , [] ) self.assertEqual(find_labels(_UpperCamelCase ) , [] ) self.assertEqual(find_labels(_UpperCamelCase ) , [] ) class A_ ( _lowerCamelCase ): pass self.assertEqual(find_labels(_UpperCamelCase ) , [] )
250
0
"""simple docstring""" from random import randint, random def _snake_case ( lowercase__ , lowercase__ , lowercase__ , lowercase__ = False , lowercase__ = False , lowercase__ = 5 , ): _lowerCamelCase : Tuple = [[-1] * number_of_cells] # Create a highway without any car _lowerCamelCase : Any = 0 _lowerCamelCase : str = max(lowercase__ , 0 ) while i < number_of_cells: _lowerCamelCase : Optional[int] = ( randint(0 , lowercase__ ) if random_speed else initial_speed ) # Place the cars i += ( randint(1 , max_speed * 2 ) if random_frequency else frequency ) # Arbitrary number, may need tuning return highway def _snake_case ( lowercase__ , lowercase__ ): _lowerCamelCase : Optional[int] = 0 _lowerCamelCase : Optional[int] = highway_now[car_index + 1 :] for cell in range(len(lowercase__ ) ): # May need a better name for this if cells[cell] != -1: # If the cell is not empty then return distance # we have the distance we wanted distance += 1 # Here if the car is near the end of the highway return distance + get_distance(lowercase__ , -1 ) def _snake_case ( lowercase__ , lowercase__ , lowercase__ ): _lowerCamelCase : int = len(lowercase__ ) # Beforce calculations, the highway is empty _lowerCamelCase : Any = [-1] * number_of_cells for car_index in range(lowercase__ ): if highway_now[car_index] != -1: # Add 1 to the current speed of the car and cap the speed _lowerCamelCase : int = min(highway_now[car_index] + 1 , lowercase__ ) # Number of empty cell before the next car _lowerCamelCase : int = get_distance(lowercase__ , lowercase__ ) - 1 # We can't have the car causing an accident _lowerCamelCase : Union[str, Any] = min(next_highway[car_index] , lowercase__ ) if random() < probability: # Randomly, a driver will slow down _lowerCamelCase : Optional[int] = max(next_highway[car_index] - 1 , 0 ) return next_highway def _snake_case ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): _lowerCamelCase : Tuple = len(highway[0] ) for i in range(lowercase__ ): _lowerCamelCase : Dict = update(highway[i] , lowercase__ , lowercase__ ) _lowerCamelCase : Any = [-1] * number_of_cells for car_index in range(lowercase__ ): _lowerCamelCase : List[str] = next_speeds_calculated[car_index] if speed != -1: # Change the position based on the speed (with % to create the loop) _lowerCamelCase : str = (car_index + speed) % number_of_cells # Commit the change of position _lowerCamelCase : Optional[Any] = speed highway.append(lowercase__ ) return highway if __name__ == "__main__": import doctest doctest.testmod()
96
"""simple docstring""" from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase__ ( lowercase ): '''simple docstring''' lowerCamelCase__ = """new-model""" if is_tf_available(): class lowerCAmelCase__ ( lowercase ): '''simple docstring''' lowerCamelCase__ = NewModelConfig @require_tf class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' @slow def A_ ( self ): _lowerCamelCase : List[str] = 'bert-base-cased' _lowerCamelCase : Union[str, Any] = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : Union[str, Any] = TFAutoModel.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow def A_ ( self ): _lowerCamelCase : List[str] = 'bert-base-cased' _lowerCamelCase : int = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : int = TFAutoModelForPreTraining.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow def A_ ( self ): for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCamelCase : Dict = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : int = TFAutoModelForCausalLM.from_pretrained(lowercase ) _lowerCamelCase, _lowerCamelCase : str = TFAutoModelForCausalLM.from_pretrained(lowercase , output_loading_info=lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow def A_ ( self ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCamelCase : List[Any] = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : str = TFAutoModelWithLMHead.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow def A_ ( self ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCamelCase : Tuple = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : Optional[int] = TFAutoModelForMaskedLM.from_pretrained(lowercase ) _lowerCamelCase, _lowerCamelCase : Tuple = TFAutoModelForMaskedLM.from_pretrained(lowercase , output_loading_info=lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow def A_ ( self ): for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCamelCase : Optional[int] = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : Tuple = TFAutoModelForSeqaSeqLM.from_pretrained(lowercase ) _lowerCamelCase, _lowerCamelCase : Tuple = TFAutoModelForSeqaSeqLM.from_pretrained(lowercase , output_loading_info=lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow def A_ ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: _lowerCamelCase : str = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : Union[str, Any] = TFAutoModelForSequenceClassification.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow def A_ ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: _lowerCamelCase : Optional[Any] = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : List[str] = TFAutoModelForQuestionAnswering.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) @slow @require_tensorflow_probability def A_ ( self ): for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: _lowerCamelCase : Dict = AutoConfig.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : Dict = TFAutoModelForTableQuestionAnswering.from_pretrained(lowercase ) _lowerCamelCase, _lowerCamelCase : List[Any] = TFAutoModelForTableQuestionAnswering.from_pretrained( lowercase , output_loading_info=lowercase ) self.assertIsNotNone(lowercase ) self.assertIsInstance(lowercase , lowercase ) def A_ ( self ): _lowerCamelCase : int = TFAutoModelWithLMHead.from_pretrained(lowercase ) self.assertIsInstance(lowercase , lowercase ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowercase ) , 14410 ) def A_ ( self ): _lowerCamelCase : Any = TFAutoModelWithLMHead.from_pretrained(lowercase ) self.assertIsInstance(lowercase , lowercase ) self.assertEqual(model.num_parameters() , 14410 ) self.assertEqual(model.num_parameters(only_trainable=lowercase ) , 14410 ) def A_ ( self ): # For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel _lowerCamelCase : List[str] = TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' ) self.assertIsInstance(lowercase , lowercase ) _lowerCamelCase : Optional[int] = copy.deepcopy(model.config ) _lowerCamelCase : Dict = ['FunnelBaseModel'] _lowerCamelCase : List[Any] = TFAutoModel.from_config(lowercase ) self.assertIsInstance(lowercase , lowercase ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowercase ) _lowerCamelCase : Tuple = TFAutoModel.from_pretrained(lowercase ) self.assertIsInstance(lowercase , lowercase ) def A_ ( self ): try: AutoConfig.register('new-model' , lowercase ) _lowerCamelCase : Tuple = [ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowercase ): auto_class.register(lowercase , lowercase ) auto_class.register(lowercase , lowercase ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowercase ): auto_class.register(lowercase , lowercase ) # Now that the config is registered, it can be used as any other config with the auto-API _lowerCamelCase : Optional[Any] = BertModelTester(self ).get_config() _lowerCamelCase : Dict = NewModelConfig(**tiny_config.to_dict() ) _lowerCamelCase : int = auto_class.from_config(lowercase ) self.assertIsInstance(lowercase , lowercase ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowercase ) _lowerCamelCase : List[Any] = auto_class.from_pretrained(lowercase ) self.assertIsInstance(lowercase , lowercase ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def A_ ( self ): with self.assertRaisesRegex( lowercase , 'bert-base is not a local folder and is not a valid model identifier' ): _lowerCamelCase : Union[str, Any] = TFAutoModel.from_pretrained('bert-base' ) def A_ ( self ): with self.assertRaisesRegex( lowercase , r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): _lowerCamelCase : str = TFAutoModel.from_pretrained(lowercase , revision='aaaaaa' ) def A_ ( self ): with self.assertRaisesRegex( lowercase , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin' , ): _lowerCamelCase : Tuple = TFAutoModel.from_pretrained('hf-internal-testing/config-no-model' ) def A_ ( self ): with self.assertRaisesRegex(lowercase , 'Use `from_pt=True` to load this model' ): _lowerCamelCase : Tuple = TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only' ) def A_ ( self ): # Make sure we have cached the model. _lowerCamelCase : Optional[int] = TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: _lowerCamelCase : Optional[int] = TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint _lowerCamelCase : int = TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) with RequestCounter() as counter: _lowerCamelCase : List[Any] = TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
96
1
"""simple docstring""" import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging lowerCAmelCase_ : Dict = logging.get_logger(__name__) def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase ): '''simple docstring''' UpperCAmelCase = set() UpperCAmelCase = [] def parse_line(lowerCAmelCase ): for line in fp: if isinstance(lowerCAmelCase , lowerCAmelCase ): UpperCAmelCase = line.decode("""UTF-8""" ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(""" """ ): # process a single warning and move it to `selected_warnings`. if len(lowerCAmelCase ) > 0: UpperCAmelCase = """\n""".join(lowerCAmelCase ) # Only keep the warnings specified in `targets` if any(F''': {x}: ''' in warning for x in targets ): selected_warnings.add(lowerCAmelCase ) buffer.clear() continue else: UpperCAmelCase = line.strip() buffer.append(lowerCAmelCase ) if from_gh: for filename in os.listdir(lowerCAmelCase ): UpperCAmelCase = os.path.join(lowerCAmelCase , lowerCAmelCase ) if not os.path.isdir(lowerCAmelCase ): # read the file if filename != "warnings.txt": continue with open(lowerCAmelCase ) as fp: parse_line(lowerCAmelCase ) else: try: with zipfile.ZipFile(lowerCAmelCase ) as z: for filename in z.namelist(): if not os.path.isdir(lowerCAmelCase ): # read the file if filename != "warnings.txt": continue with z.open(lowerCAmelCase ) as fp: parse_line(lowerCAmelCase ) except Exception: logger.warning( F'''{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.''' ) return selected_warnings def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase ): '''simple docstring''' UpperCAmelCase = set() UpperCAmelCase = [os.path.join(lowerCAmelCase , lowerCAmelCase ) for p in os.listdir(lowerCAmelCase ) if (p.endswith(""".zip""" ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(lowerCAmelCase , lowerCAmelCase ) ) return selected_warnings if __name__ == "__main__": def _lowerCAmelCase ( lowerCAmelCase ): '''simple docstring''' return values.split(""",""" ) lowerCAmelCase_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('''--workflow_run_id''', type=str, required=True, help='''A GitHub Actions workflow run id.''') parser.add_argument( '''--output_dir''', type=str, required=True, help='''Where to store the downloaded artifacts and other result files.''', ) parser.add_argument('''--token''', default=None, type=str, help='''A token that has actions:read permission.''') # optional parameters parser.add_argument( '''--targets''', default='''DeprecationWarning,UserWarning,FutureWarning''', type=list_str, help='''Comma-separated list of target warning(s) which we want to extract.''', ) parser.add_argument( '''--from_gh''', action='''store_true''', help='''If running from a GitHub action workflow and collecting warnings from its artifacts.''', ) lowerCAmelCase_ : Union[str, Any] = parser.parse_args() lowerCAmelCase_ : Optional[Any] = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links lowerCAmelCase_ : Dict = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, '''artifacts.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print('''=''' * 8_0) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts lowerCAmelCase_ : Dict = extract_warnings(args.output_dir, args.targets) lowerCAmelCase_ : List[str] = sorted(selected_warnings) with open(os.path.join(args.output_dir, '''selected_warnings.json'''), '''w''', encoding='''UTF-8''') as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
248
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType lowerCAmelCase_ : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase_ : List[str] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class UpperCamelCase_ ( a_ ): _A : List[Any] = 'layoutlmv3' def __init__( self , snake_case__=5_02_65 , snake_case__=7_68 , snake_case__=12 , snake_case__=12 , snake_case__=30_72 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=5_12 , snake_case__=2 , snake_case__=0.02 , snake_case__=1e-5 , snake_case__=1 , snake_case__=0 , snake_case__=2 , snake_case__=10_24 , snake_case__=1_28 , snake_case__=1_28 , snake_case__=True , snake_case__=32 , snake_case__=1_28 , snake_case__=64 , snake_case__=2_56 , snake_case__=True , snake_case__=True , snake_case__=True , snake_case__=2_24 , snake_case__=3 , snake_case__=16 , snake_case__=None , **snake_case__ , ) -> Tuple: """simple docstring""" super().__init__( vocab_size=snake_case__ , hidden_size=snake_case__ , num_hidden_layers=snake_case__ , num_attention_heads=snake_case__ , intermediate_size=snake_case__ , hidden_act=snake_case__ , hidden_dropout_prob=snake_case__ , attention_probs_dropout_prob=snake_case__ , max_position_embeddings=snake_case__ , type_vocab_size=snake_case__ , initializer_range=snake_case__ , layer_norm_eps=snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ , **snake_case__ , ) UpperCAmelCase = max_ad_position_embeddings UpperCAmelCase = coordinate_size UpperCAmelCase = shape_size UpperCAmelCase = has_relative_attention_bias UpperCAmelCase = rel_pos_bins UpperCAmelCase = max_rel_pos UpperCAmelCase = has_spatial_attention_bias UpperCAmelCase = rel_ad_pos_bins UpperCAmelCase = max_rel_ad_pos UpperCAmelCase = text_embed UpperCAmelCase = visual_embed UpperCAmelCase = input_size UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = classifier_dropout class UpperCamelCase_ ( a_ ): _A : str = version.parse('1.12' ) @property def UpperCamelCase_ ( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def UpperCamelCase_ ( self ) -> float: """simple docstring""" return 1e-5 @property def UpperCamelCase_ ( self ) -> int: """simple docstring""" return 12 def UpperCamelCase_ ( self , snake_case__ , snake_case__ = -1 , snake_case__ = -1 , snake_case__ = False , snake_case__ = None , snake_case__ = 3 , snake_case__ = 40 , snake_case__ = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , snake_case__ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCAmelCase = compute_effective_axis_dimension( snake_case__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCAmelCase = processor.tokenizer.num_special_tokens_to_add(snake_case__ ) UpperCAmelCase = compute_effective_axis_dimension( snake_case__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=snake_case__ ) # Generate dummy inputs according to compute batch and sequence UpperCAmelCase = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes UpperCAmelCase = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) UpperCAmelCase = self._generate_dummy_images(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) UpperCAmelCase = dict( processor( snake_case__ , text=snake_case__ , boxes=snake_case__ , return_tensors=snake_case__ , ) ) return inputs
248
1
'''simple docstring''' import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : str ) -> Dict: _a : Dict =SwinConfig(image_size=192 ) if "base" in model_name: _a : List[Any] =6 _a : Dict =128 _a : Optional[Any] =(2, 2, 18, 2) _a : Any =(4, 8, 16, 32) elif "large" in model_name: _a : Dict =12 _a : Dict =192 _a : str =(2, 2, 18, 2) _a : Any =(6, 12, 24, 48) else: raise ValueError("""Model not supported, only supports base and large variants""" ) _a : Optional[int] =window_size _a : Union[str, Any] =embed_dim _a : Dict =depths _a : str =num_heads return config def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : Dict ) -> List[Any]: if "encoder.mask_token" in name: _a : List[str] =name.replace("""encoder.mask_token""" ,"""embeddings.mask_token""" ) if "encoder.patch_embed.proj" in name: _a : str =name.replace("""encoder.patch_embed.proj""" ,"""embeddings.patch_embeddings.projection""" ) if "encoder.patch_embed.norm" in name: _a : Union[str, Any] =name.replace("""encoder.patch_embed.norm""" ,"""embeddings.norm""" ) if "attn.proj" in name: _a : Dict =name.replace("""attn.proj""" ,"""attention.output.dense""" ) if "attn" in name: _a : str =name.replace("""attn""" ,"""attention.self""" ) if "norm1" in name: _a : List[Any] =name.replace("""norm1""" ,"""layernorm_before""" ) if "norm2" in name: _a : Any =name.replace("""norm2""" ,"""layernorm_after""" ) if "mlp.fc1" in name: _a : Optional[int] =name.replace("""mlp.fc1""" ,"""intermediate.dense""" ) if "mlp.fc2" in name: _a : Dict =name.replace("""mlp.fc2""" ,"""output.dense""" ) if name == "encoder.norm.weight": _a : Any ="""layernorm.weight""" if name == "encoder.norm.bias": _a : Optional[Any] ="""layernorm.bias""" if "decoder" in name: pass else: _a : List[Any] ="""swin.""" + name return name def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : int ,_UpperCAmelCase : Union[str, Any] ) -> Dict: for key in orig_state_dict.copy().keys(): _a : Tuple =orig_state_dict.pop(_UpperCAmelCase ) if "attn_mask" in key: pass elif "qkv" in key: _a : Tuple =key.split(""".""" ) _a : Optional[int] =int(key_split[2] ) _a : List[Any] =int(key_split[4] ) _a : Union[str, Any] =model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: _a : List[Any] =val[:dim, :] _a : str =val[ dim : dim * 2, : ] _a : int =val[-dim:, :] else: _a : Optional[Any] =val[ :dim ] _a : Optional[Any] =val[ dim : dim * 2 ] _a : List[Any] =val[ -dim: ] else: _a : Optional[int] =val return orig_state_dict def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : str ,_UpperCAmelCase : Union[str, Any] ,_UpperCAmelCase : int ,_UpperCAmelCase : Dict ) -> List[Any]: _a : List[Any] =torch.load(_UpperCAmelCase ,map_location="""cpu""" )["""model"""] _a : Any =get_swin_config(_UpperCAmelCase ) _a : Dict =SwinForMaskedImageModeling(_UpperCAmelCase ) model.eval() _a : Union[str, Any] =convert_state_dict(_UpperCAmelCase ,_UpperCAmelCase ) model.load_state_dict(_UpperCAmelCase ) _a : Optional[int] ="""http://images.cocodataset.org/val2017/000000039769.jpg""" _a : Any =ViTImageProcessor(size={"""height""": 192, """width""": 192} ) _a : Any =Image.open(requests.get(_UpperCAmelCase ,stream=_UpperCAmelCase ).raw ) _a : Optional[Any] =image_processor(images=_UpperCAmelCase ,return_tensors="""pt""" ) with torch.no_grad(): _a : Optional[int] =model(**_UpperCAmelCase ).logits print(outputs.keys() ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_UpperCAmelCase ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(_UpperCAmelCase ) if push_to_hub: print(F"Pushing model and image processor for {model_name} to hub" ) model.push_to_hub(F"microsoft/{model_name}" ) image_processor.push_to_hub(F"microsoft/{model_name}" ) if __name__ == "__main__": A__: Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''swin-base-simmim-window6-192''', type=str, choices=['''swin-base-simmim-window6-192''', '''swin-large-simmim-window12-192'''], help='''Name of the Swin SimMIM model you\'d like to convert.''', ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth''', type=str, help='''Path to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the 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.''' ) A__: Optional[int] = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
276
'''simple docstring''' class A__ : def __init__( self :List[str] ) -> List[Any]: '''simple docstring''' _a : Tuple =0 _a : Any =0 _a : int ={} def __UpperCAmelCase ( self :Any , SCREAMING_SNAKE_CASE :List[str] ) -> Optional[int]: '''simple docstring''' if vertex not in self.adjacency: _a : Dict ={} self.num_vertices += 1 def __UpperCAmelCase ( self :Optional[Any] , SCREAMING_SNAKE_CASE :str , SCREAMING_SNAKE_CASE :str , SCREAMING_SNAKE_CASE :Any ) -> List[str]: '''simple docstring''' self.add_vertex(SCREAMING_SNAKE_CASE ) self.add_vertex(SCREAMING_SNAKE_CASE ) if head == tail: return _a : Any =weight _a : Tuple =weight def __UpperCAmelCase ( self :Dict ) -> Optional[int]: '''simple docstring''' _a : Union[str, Any] =self.get_edges() for edge in edges: _a , _a , _a : List[str] =edge edges.remove((tail, head, weight) ) for i in range(len(SCREAMING_SNAKE_CASE ) ): _a : str =list(edges[i] ) edges.sort(key=lambda SCREAMING_SNAKE_CASE : e[2] ) for i in range(len(SCREAMING_SNAKE_CASE ) - 1 ): if edges[i][2] >= edges[i + 1][2]: _a : Union[str, Any] =edges[i][2] + 1 for edge in edges: _a , _a , _a : Tuple =edge _a : Tuple =weight _a : List[Any] =weight def __str__( self :int ) -> str: '''simple docstring''' _a : int ="""""" for tail in self.adjacency: for head in self.adjacency[tail]: _a : str =self.adjacency[head][tail] string += f"{head} -> {tail} == {weight}\n" return string.rstrip("""\n""" ) def __UpperCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' _a : Union[str, Any] =[] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __UpperCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' return self.adjacency.keys() @staticmethod def __UpperCAmelCase ( SCREAMING_SNAKE_CASE :Dict=None , SCREAMING_SNAKE_CASE :List[Any]=None ) -> Optional[int]: '''simple docstring''' _a : str =Graph() if vertices is None: _a : Union[str, Any] =[] if edges is None: _a : List[Any] =[] for vertex in vertices: g.add_vertex(SCREAMING_SNAKE_CASE ) for edge in edges: g.add_edge(*SCREAMING_SNAKE_CASE ) return g class A__ : def __init__( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' _a : Optional[int] ={} _a : List[str] ={} def __len__( self :List[Any] ) -> List[Any]: '''simple docstring''' return len(self.parent ) def __UpperCAmelCase ( self :Tuple , SCREAMING_SNAKE_CASE :Tuple ) -> Dict: '''simple docstring''' if item in self.parent: return self.find(SCREAMING_SNAKE_CASE ) _a : Optional[Any] =item _a : List[str] =0 return item def __UpperCAmelCase ( self :int , SCREAMING_SNAKE_CASE :Dict ) -> List[str]: '''simple docstring''' if item not in self.parent: return self.make_set(SCREAMING_SNAKE_CASE ) if item != self.parent[item]: _a : str =self.find(self.parent[item] ) return self.parent[item] def __UpperCAmelCase ( self :Optional[int] , SCREAMING_SNAKE_CASE :Tuple , SCREAMING_SNAKE_CASE :List[Any] ) -> Optional[Any]: '''simple docstring''' _a : Optional[int] =self.find(SCREAMING_SNAKE_CASE ) _a : Dict =self.find(SCREAMING_SNAKE_CASE ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _a : Any =roota return roota if self.rank[roota] < self.rank[roota]: _a : List[str] =roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _a : List[Any] =roota return roota return None @staticmethod def __UpperCAmelCase ( SCREAMING_SNAKE_CASE :Dict ) -> Union[str, Any]: '''simple docstring''' _a : Any =graph.num_vertices _a : Union[str, Any] =Graph.UnionFind() _a : Optional[int] =[] while num_components > 1: _a : str ={} for vertex in graph.get_vertices(): _a : List[str] =-1 _a : Any =graph.get_edges() for edge in edges: _a , _a , _a : Tuple =edge edges.remove((tail, head, weight) ) for edge in edges: _a , _a , _a : Any =edge _a : Any =union_find.find(SCREAMING_SNAKE_CASE ) _a : List[Any] =union_find.find(SCREAMING_SNAKE_CASE ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _a : Optional[int] =[head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _a : List[Any] =[head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _a , _a , _a : Optional[Any] =cheap_edge[vertex] if union_find.find(SCREAMING_SNAKE_CASE ) != union_find.find(SCREAMING_SNAKE_CASE ): union_find.union(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) mst_edges.append(cheap_edge[vertex] ) _a : str =num_components - 1 _a : str =Graph.build(edges=SCREAMING_SNAKE_CASE ) return mst
276
1
def __lowerCamelCase (UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Dict ): SCREAMING_SNAKE_CASE = [1] for i in range(2 , UpperCAmelCase__ ): factorials.append(factorials[-1] * i ) assert 0 <= k < factorials[-1] * n, "k out of bounds" SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = list(range(UpperCAmelCase__ ) ) # Find permutation while factorials: SCREAMING_SNAKE_CASE = factorials.pop() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = divmod(UpperCAmelCase__ , UpperCAmelCase__ ) permutation.append(elements[number] ) elements.remove(elements[number] ) permutation.append(elements[0] ) return permutation if __name__ == "__main__": import doctest doctest.testmod()
206
# Author: OMKAR PATHAK, Nwachukwu Chidiebere # Use a Python dictionary to construct the graph. from __future__ import annotations from pprint import pformat from typing import Generic, TypeVar _lowerCamelCase : Union[str, Any] = TypeVar('''T''') class lowercase ( Generic[T] ): def __init__( self : int , _UpperCamelCase : bool = True ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE = {} # dictionary of lists SCREAMING_SNAKE_CASE = directed def __snake_case( self : int , _UpperCamelCase : T , _UpperCamelCase : T ) -> GraphAdjacencyList[T]: '''simple docstring''' if not self.directed: # For undirected graphs # if both source vertex and destination vertex are both present in the # adjacency list, add destination vertex to source vertex list of adjacent # vertices and add source vertex to destination vertex list of adjacent # vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(_UpperCamelCase ) self.adj_list[destination_vertex].append(_UpperCamelCase ) # if only source vertex is present in adjacency list, add destination vertex # to source vertex list of adjacent vertices, then create a new vertex with # destination vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(_UpperCamelCase ) SCREAMING_SNAKE_CASE = [source_vertex] # if only destination vertex is present in adjacency list, add source vertex # to destination vertex list of adjacent vertices, then create a new vertex # with source vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif destination_vertex in self.adj_list: self.adj_list[destination_vertex].append(_UpperCamelCase ) SCREAMING_SNAKE_CASE = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and assign a list # containing the destination vertex as it's first adjacent vertex also # create a new vertex with destination vertex as key and assign a list # containing the source vertex as it's first adjacent vertex. else: SCREAMING_SNAKE_CASE = [destination_vertex] SCREAMING_SNAKE_CASE = [source_vertex] else: # For directed graphs # if both source vertex and destination vertex are present in adjacency # list, add destination vertex to source vertex list of adjacent vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(_UpperCamelCase ) # if only source vertex is present in adjacency list, add destination # vertex to source vertex list of adjacent vertices and create a new vertex # with destination vertex as key, which has no adjacent vertex elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(_UpperCamelCase ) SCREAMING_SNAKE_CASE = [] # if only destination vertex is present in adjacency list, create a new # vertex with source vertex as key and assign a list containing destination # vertex as first adjacent vertex elif destination_vertex in self.adj_list: SCREAMING_SNAKE_CASE = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and a list containing # destination vertex as it's first adjacent vertex. Then create a new vertex # with destination vertex as key, which has no adjacent vertex else: SCREAMING_SNAKE_CASE = [destination_vertex] SCREAMING_SNAKE_CASE = [] return self def __repr__( self : Union[str, Any] ) -> str: '''simple docstring''' return pformat(self.adj_list )
206
1
"""simple docstring""" def a__ ( SCREAMING_SNAKE_CASE : list ): '''simple docstring''' if len(SCREAMING_SNAKE_CASE ) < 2: return collection def circle_sort_util(SCREAMING_SNAKE_CASE : list , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ) -> bool: lowerCAmelCase : List[str] = False if low == high: return swapped lowerCAmelCase : List[str] = low lowerCAmelCase : Dict = high while left < right: if collection[left] > collection[right]: lowerCAmelCase , lowerCAmelCase : int = ( collection[right], collection[left], ) lowerCAmelCase : Tuple = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCAmelCase , lowerCAmelCase : int = ( collection[right + 1], collection[left], ) lowerCAmelCase : Optional[int] = True lowerCAmelCase : List[Any] = low + int((high - low) / 2 ) lowerCAmelCase : Union[str, Any] = circle_sort_util(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Dict = circle_sort_util(SCREAMING_SNAKE_CASE , mid + 1 , SCREAMING_SNAKE_CASE ) return swapped or left_swap or right_swap lowerCAmelCase : Dict = True while is_not_sorted is True: lowerCAmelCase : List[Any] = circle_sort_util(SCREAMING_SNAKE_CASE , 0 , len(SCREAMING_SNAKE_CASE ) - 1 ) return collection if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(circle_sort(unsorted))
108
'''simple docstring''' from __future__ import annotations import requests def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : str ) -> dict: _a : Any =F"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty" return requests.get(_UpperCAmelCase ).json() def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : int = 10 ) -> list[dict]: _a : Union[str, Any] ="""https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty""" _a : int =requests.get(_UpperCAmelCase ).json()[:max_stories] return [get_hackernews_story(_UpperCAmelCase ) for story_id in story_ids] def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : int = 10 ) -> str: _a : Union[str, Any] =hackernews_top_stories(_UpperCAmelCase ) return "\n".join("""* [{title}]({url})""".format(**_UpperCAmelCase ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
276
0
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging _UpperCAmelCase : Optional[int] = logging.get_logger(__name__) class a__ ( __A ): """simple docstring""" __UpperCamelCase : List[str] = ['input_features', 'attention_mask'] def __init__(self , __lowercase=80 , __lowercase=1_60_00 , __lowercase=80 , __lowercase=0.0 , __lowercase=True , __lowercase=True , __lowercase=True , **__lowercase , ): super().__init__(feature_size=__lowercase , sampling_rate=__lowercase , padding_value=__lowercase , **__lowercase ) __lowerCAmelCase = num_mel_bins __lowerCAmelCase = do_ceptral_normalize __lowerCAmelCase = normalize_means __lowerCAmelCase = normalize_vars __lowerCAmelCase = True def _snake_case (self , __lowercase , ): __lowerCAmelCase = waveform * (2**15) # Kaldi compliance: 16-bit signed integers __lowerCAmelCase = torch.from_numpy(__lowercase ).unsqueeze(0 ) __lowerCAmelCase = ta_kaldi.fbank(__lowercase , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _snake_case (__lowercase , __lowercase , __lowercase = True , __lowercase = True , __lowercase = 0.0 , ): # make sure we normalize float32 arrays if normalize_means: __lowerCAmelCase = x[:input_length].mean(axis=0 ) __lowerCAmelCase = np.subtract(__lowercase , __lowercase ) if normalize_vars: __lowerCAmelCase = x[:input_length].std(axis=0 ) __lowerCAmelCase = np.divide(__lowercase , __lowercase ) if input_length < x.shape[0]: __lowerCAmelCase = padding_value # make sure array is in float32 __lowerCAmelCase = x.astype(np.floataa ) return x def _snake_case (self , __lowercase , __lowercase = None ): __lowerCAmelCase = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(__lowercase , __lowercase , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(__lowercase , __lowercase ) ] def __call__(self , __lowercase , __lowercase = False , __lowercase = None , __lowercase = False , __lowercase = None , __lowercase = None , __lowercase = None , __lowercase = None , **__lowercase , ): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" F""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" F""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) __lowerCAmelCase = isinstance(__lowercase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"""Only mono-channel audio is supported for input to {self}""" ) __lowerCAmelCase = is_batched_numpy or ( isinstance(__lowercase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: __lowerCAmelCase = [np.asarray(__lowercase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(__lowercase , np.ndarray ): __lowerCAmelCase = np.asarray(__lowercase , dtype=np.floataa ) elif isinstance(__lowercase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): __lowerCAmelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: __lowerCAmelCase = [raw_speech] # extract fbank features __lowerCAmelCase = [self._extract_fbank_features(__lowercase ) for waveform in raw_speech] # convert into correct format for padding __lowerCAmelCase = BatchFeature({'''input_features''': features} ) __lowerCAmelCase = self.pad( __lowercase , padding=__lowercase , max_length=__lowercase , truncation=__lowercase , pad_to_multiple_of=__lowercase , return_attention_mask=__lowercase , **__lowercase , ) # make sure list is in array format __lowerCAmelCase = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , __lowercase ): __lowerCAmelCase = [np.asarray(__lowercase , dtype=np.floataa ) for feature in input_features] __lowerCAmelCase = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: __lowerCAmelCase = [np.asarray(__lowercase , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: __lowerCAmelCase = ( np.array(__lowercase , dtype=np.intaa ) if self._get_padding_strategies(__lowercase , max_length=__lowercase ) is not PaddingStrategy.DO_NOT_PAD else None ) __lowerCAmelCase = self.normalize( padded_inputs['''input_features'''] , attention_mask=__lowercase ) if return_tensors is not None: __lowerCAmelCase = padded_inputs.convert_to_tensors(__lowercase ) return padded_inputs
9
'''simple docstring''' 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 __magic_name__( lowerCamelCase, lowerCamelCase): __lowerCAmelCase = old_name if "patch_embed" in old_name: __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = old_name.split('''.''') if layer == "0": __lowerCAmelCase = old_name.replace('''0''', '''convolution1''') elif layer == "1": __lowerCAmelCase = old_name.replace('''1''', '''batchnorm_before''') elif layer == "3": __lowerCAmelCase = old_name.replace('''3''', '''convolution2''') else: __lowerCAmelCase = old_name.replace('''4''', '''batchnorm_after''') if "network" in old_name and re.search(r'''\d\.\d''', lowerCamelCase): __lowerCAmelCase = r'''\b\d{2}\b''' if bool(re.search(lowerCamelCase, lowerCamelCase)): __lowerCAmelCase = re.search(r'''\d\.\d\d.''', lowerCamelCase).group() else: __lowerCAmelCase = re.search(r'''\d\.\d.''', lowerCamelCase).group() if int(match[0]) < 6: __lowerCAmelCase = old_name.replace(lowerCamelCase, '''''') __lowerCAmelCase = trimmed_name.replace('''network''', match[0] + '''.meta4D_layers.blocks.''' + match[2:-1]) __lowerCAmelCase = '''intermediate_stages.''' + trimmed_name else: __lowerCAmelCase = old_name.replace(lowerCamelCase, '''''') if int(match[2]) < num_meta4D_last_stage: __lowerCAmelCase = trimmed_name.replace('''network''', '''meta4D_layers.blocks.''' + match[2]) else: __lowerCAmelCase = str(int(match[2]) - num_meta4D_last_stage) __lowerCAmelCase = trimmed_name.replace('''network''', '''meta3D_layers.blocks.''' + layer_index) if "norm1" in old_name: __lowerCAmelCase = trimmed_name.replace('''norm1''', '''layernorm1''') elif "norm2" in old_name: __lowerCAmelCase = trimmed_name.replace('''norm2''', '''layernorm2''') elif "fc1" in old_name: __lowerCAmelCase = trimmed_name.replace('''fc1''', '''linear_in''') elif "fc2" in old_name: __lowerCAmelCase = trimmed_name.replace('''fc2''', '''linear_out''') __lowerCAmelCase = '''last_stage.''' + trimmed_name elif "network" in old_name and re.search(r'''.\d.''', lowerCamelCase): __lowerCAmelCase = old_name.replace('''network''', '''intermediate_stages''') if "fc" in new_name: __lowerCAmelCase = new_name.replace('''fc''', '''convolution''') elif ("norm1" in new_name) and ("layernorm1" not in new_name): __lowerCAmelCase = new_name.replace('''norm1''', '''batchnorm_before''') elif ("norm2" in new_name) and ("layernorm2" not in new_name): __lowerCAmelCase = new_name.replace('''norm2''', '''batchnorm_after''') if "proj" in new_name: __lowerCAmelCase = new_name.replace('''proj''', '''projection''') if "dist_head" in new_name: __lowerCAmelCase = new_name.replace('''dist_head''', '''distillation_classifier''') elif "head" in new_name: __lowerCAmelCase = new_name.replace('''head''', '''classifier''') elif "patch_embed" in new_name: __lowerCAmelCase = '''efficientformer.''' + new_name elif new_name == "norm.weight" or new_name == "norm.bias": __lowerCAmelCase = new_name.replace('''norm''', '''layernorm''') __lowerCAmelCase = '''efficientformer.''' + new_name else: __lowerCAmelCase = '''efficientformer.encoder.''' + new_name return new_name def __magic_name__( lowerCamelCase, lowerCamelCase): for key in checkpoint.copy().keys(): __lowerCAmelCase = checkpoint.pop(lowerCamelCase) __lowerCAmelCase = val return checkpoint def __magic_name__( ): __lowerCAmelCase = '''http://images.cocodataset.org/val2017/000000039769.jpg''' __lowerCAmelCase = Image.open(requests.get(lowerCamelCase, stream=lowerCamelCase).raw) return image def __magic_name__( lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase): __lowerCAmelCase = torch.load(lowerCamelCase, map_location='''cpu''')['''model'''] __lowerCAmelCase = EfficientFormerConfig.from_json_file(lowerCamelCase) __lowerCAmelCase = EfficientFormerForImageClassificationWithTeacher(lowerCamelCase) __lowerCAmelCase = '''_'''.join(checkpoint_path.split('''/''')[-1].split('''.''')[0].split('''_''')[:-1]) __lowerCAmelCase = config.depths[-1] - config.num_metaad_blocks + 1 __lowerCAmelCase = convert_torch_checkpoint(lowerCamelCase, lowerCamelCase) model.load_state_dict(lowerCamelCase) model.eval() __lowerCAmelCase = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } # prepare image __lowerCAmelCase = prepare_img() __lowerCAmelCase = 2_5_6 __lowerCAmelCase = 2_2_4 __lowerCAmelCase = EfficientFormerImageProcessor( size={'''shortest_edge''': image_size}, crop_size={'''height''': crop_size, '''width''': crop_size}, resample=pillow_resamplings['''bicubic'''], ) __lowerCAmelCase = processor(images=lowerCamelCase, return_tensors='''pt''').pixel_values # original processing pipeline __lowerCAmelCase = Compose( [ Resize(lowerCamelCase, interpolation=pillow_resamplings['''bicubic''']), CenterCrop(lowerCamelCase), ToTensor(), Normalize(lowerCamelCase, lowerCamelCase), ]) __lowerCAmelCase = image_transforms(lowerCamelCase).unsqueeze(0) assert torch.allclose(lowerCamelCase, lowerCamelCase) __lowerCAmelCase = model(lowerCamelCase) __lowerCAmelCase = outputs.logits __lowerCAmelCase = (1, 1_0_0_0) if "l1" in model_name: __lowerCAmelCase = torch.Tensor( [-0.13_12, 0.43_53, -1.04_99, -0.51_24, 0.41_83, -0.67_93, -1.37_77, -0.08_93, -0.73_58, -2.43_28]) assert torch.allclose(logits[0, :1_0], lowerCamelCase, atol=1E-3) assert logits.shape == expected_shape elif "l3" in model_name: __lowerCAmelCase = torch.Tensor( [-1.31_50, -1.54_56, -1.25_56, -0.84_96, -0.71_27, -0.78_97, -0.97_28, -0.30_52, 0.37_51, -0.31_27]) assert torch.allclose(logits[0, :1_0], lowerCamelCase, atol=1E-3) assert logits.shape == expected_shape elif "l7" in model_name: __lowerCAmelCase = torch.Tensor( [-1.02_83, -1.41_31, -0.56_44, -1.31_15, -0.57_85, -1.20_49, -0.75_28, 0.19_92, -0.38_22, -0.08_78]) 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(lowerCamelCase).mkdir(exist_ok=lowerCamelCase) model.save_pretrained(lowerCamelCase) print(F"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""") processor.save_pretrained(lowerCamelCase) 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=lowerCamelCase, ) processor.push_to_hub( repo_id=F"""Bearnardd/{pytorch_dump_path}""", commit_message='''Add image processor''', use_temp_dir=lowerCamelCase, ) if __name__ == "__main__": _UpperCAmelCase : List[Any] = 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) _UpperCAmelCase : List[str] = 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, )
9
1
import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModel, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel from diffusers.pipelines.pipeline_utils import DiffusionPipeline from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( enable_full_determinism, floats_tensor, load_image, load_numpy, require_torch_gpu, skip_mps, slow, torch_device, ) from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class __UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCAmelCase_ = StableUnCLIPImgaImgPipeline lowerCAmelCase_ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS lowerCAmelCase_ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowerCAmelCase_ = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowerCAmelCase_ = frozenset([] ) def UpperCAmelCase__ ( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = 32 __SCREAMING_SNAKE_CASE : List[Any] = embedder_hidden_size # image encoding components __SCREAMING_SNAKE_CASE : str = CLIPImageProcessor(crop_size=32 , size=32 ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : List[str] = CLIPVisionModelWithProjection( CLIPVisionConfig( hidden_size=_A , projection_dim=_A , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) ) # regular denoising components torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Optional[int] = StableUnCLIPImageNormalizer(embedding_dim=_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Tuple = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Any = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : List[str] = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type='''projection''' , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=_A , layers_per_block=1 , upcast_attention=_A , use_linear_projection=_A , ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : int = DDIMScheduler( beta_schedule='''scaled_linear''' , beta_start=0.0_00_85 , beta_end=0.0_12 , prediction_type='''v_prediction''' , set_alpha_to_one=_A , steps_offset=1 , ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Any = AutoencoderKL() __SCREAMING_SNAKE_CASE : int = { # image encoding components '''feature_extractor''': feature_extractor, '''image_encoder''': image_encoder.eval(), # image noising components '''image_normalizer''': image_normalizer.eval(), '''image_noising_scheduler''': image_noising_scheduler, # regular denoising components '''tokenizer''': tokenizer, '''text_encoder''': text_encoder.eval(), '''unet''': unet.eval(), '''scheduler''': scheduler, '''vae''': vae.eval(), } return components def UpperCAmelCase__ ( self : str , _A : List[Any] , _A : int=0 , _A : Union[str, Any]=True ): """simple docstring""" if str(_A ).startswith('''mps''' ): __SCREAMING_SNAKE_CASE : Union[str, Any] = torch.manual_seed(_A ) else: __SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device=_A ).manual_seed(_A ) __SCREAMING_SNAKE_CASE : List[str] = floats_tensor((1, 3, 32, 32) , rng=random.Random(_A ) ).to(_A ) if pil_image: __SCREAMING_SNAKE_CASE : Optional[int] = input_image * 0.5 + 0.5 __SCREAMING_SNAKE_CASE : str = input_image.clamp(0 , 1 ) __SCREAMING_SNAKE_CASE : List[str] = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() __SCREAMING_SNAKE_CASE : Any = DiffusionPipeline.numpy_to_pil(_A )[0] return { "prompt": "An anime racoon running a marathon", "image": input_image, "generator": generator, "num_inference_steps": 2, "output_type": "np", } @skip_mps def UpperCAmelCase__ ( self : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_dummy_components() __SCREAMING_SNAKE_CASE : int = StableUnCLIPImgaImgPipeline(**_A ) __SCREAMING_SNAKE_CASE : Optional[Any] = sd_pipe.to(_A ) sd_pipe.set_progress_bar_config(disable=_A ) __SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_dummy_inputs(_A ) inputs.update({'''image_embeds''': None} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = sd_pipe(**_A ).images __SCREAMING_SNAKE_CASE : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __SCREAMING_SNAKE_CASE : List[str] = np.array([0.38_72, 0.72_24, 0.56_01, 0.47_41, 0.68_72, 0.58_14, 0.46_36, 0.38_67, 0.50_78] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = torch_device in ['''cpu''', '''mps'''] self._test_attention_slicing_forward_pass(test_max_difference=_A ) def UpperCAmelCase__ ( self : Optional[int] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = torch_device in ['''cpu''', '''mps'''] self._test_inference_batch_single_identical(test_max_difference=_A ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def UpperCAmelCase__ ( self : int ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(test_max_difference=_A ) @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self : Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase__ ( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) __SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy''' ) __SCREAMING_SNAKE_CASE : Any = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-l-img2img''' , torch_dtype=torch.floataa ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE : Optional[int] = torch.Generator(device='''cpu''' ).manual_seed(0 ) __SCREAMING_SNAKE_CASE : Any = pipe(_A , '''anime turle''' , generator=_A , output_type='''np''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_A , _A ) def UpperCAmelCase__ ( self : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[str] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) __SCREAMING_SNAKE_CASE : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy''' ) __SCREAMING_SNAKE_CASE : Any = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' , torch_dtype=torch.floataa ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) __SCREAMING_SNAKE_CASE : Tuple = pipe(_A , '''anime turle''' , generator=_A , output_type='''np''' ) __SCREAMING_SNAKE_CASE : Any = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_A , _A ) def UpperCAmelCase__ ( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __SCREAMING_SNAKE_CASE : Dict = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' , torch_dtype=torch.floataa ) __SCREAMING_SNAKE_CASE : Optional[int] = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE : Tuple = pipe( _A , '''anime turtle''' , num_inference_steps=2 , output_type='''np''' , ) __SCREAMING_SNAKE_CASE : Any = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
303
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem lowercase_ = importlib.util.find_spec("""s3fs""") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 lowercase_ = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(f'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def a__ ( snake_case ): """simple docstring""" if "://" in dataset_path: __SCREAMING_SNAKE_CASE : Any = dataset_path.split('''://''' )[1] return dataset_path def a__ ( snake_case ): """simple docstring""" if fs is not None and fs.protocol != "file": return True else: return False def a__ ( snake_case , snake_case , snake_case ): """simple docstring""" __SCREAMING_SNAKE_CASE : Any = not is_remote_filesystem(snake_case ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(snake_case ) , fs._strip_protocol(snake_case ) ) else: fs.mv(snake_case , snake_case , recursive=snake_case ) def a__ ( ): """simple docstring""" if hasattr(fsspec.asyn , '''reset_lock''' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: __SCREAMING_SNAKE_CASE : int = None __SCREAMING_SNAKE_CASE : Union[str, Any] = None __SCREAMING_SNAKE_CASE : Union[str, Any] = threading.Lock()
303
1
import os def lowercase_ ( _lowerCamelCase : str = "input.txt"): with open(os.path.join(os.path.dirname(_lowerCamelCase) , _lowerCamelCase)) as input_file: lowercase__ : Tuple = [ [int(_lowerCamelCase) for element in line.split(",")] for line in input_file.readlines() ] lowercase__ : Optional[Any] = len(_lowerCamelCase) lowercase__ : int = len(matrix[0]) lowercase__ : Dict = [[-1 for _ in range(_lowerCamelCase)] for _ in range(_lowerCamelCase)] for i in range(_lowerCamelCase): lowercase__ : int = matrix[i][0] for j in range(1 , _lowerCamelCase): for i in range(_lowerCamelCase): lowercase__ : Optional[int] = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 , _lowerCamelCase): lowercase__ : Tuple = min( minimal_path_sums[i][j] , minimal_path_sums[i - 1][j] + matrix[i][j]) for i in range(rows - 2 , -1 , -1): lowercase__ : str = min( minimal_path_sums[i][j] , minimal_path_sums[i + 1][j] + matrix[i][j]) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums) if __name__ == "__main__": print(f"{solution() = }")
333
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''facebook/vit-mae-base''': '''https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json''', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class snake_case_ ( __A ): __A : List[str] = "vit_mae" def __init__( self : List[Any] , lowercase_ : List[Any]=7_68 , lowercase_ : Tuple=12 , lowercase_ : Tuple=12 , lowercase_ : Optional[Any]=30_72 , lowercase_ : str="gelu" , lowercase_ : Tuple=0.0 , lowercase_ : int=0.0 , lowercase_ : Dict=0.02 , lowercase_ : int=1E-12 , lowercase_ : Tuple=2_24 , lowercase_ : Any=16 , lowercase_ : Dict=3 , lowercase_ : List[Any]=True , lowercase_ : Dict=16 , lowercase_ : List[str]=5_12 , lowercase_ : Tuple=8 , lowercase_ : Any=20_48 , lowercase_ : int=0.75 , lowercase_ : Tuple=False , **lowercase_ : Optional[int] , ) -> Optional[Any]: super().__init__(**lowercase_ ) lowercase__ : List[str] = hidden_size lowercase__ : str = num_hidden_layers lowercase__ : Optional[int] = num_attention_heads lowercase__ : List[Any] = intermediate_size lowercase__ : str = hidden_act lowercase__ : List[str] = hidden_dropout_prob lowercase__ : Optional[Any] = attention_probs_dropout_prob lowercase__ : Any = initializer_range lowercase__ : Optional[Any] = layer_norm_eps lowercase__ : Optional[Any] = image_size lowercase__ : Optional[int] = patch_size lowercase__ : Any = num_channels lowercase__ : str = qkv_bias lowercase__ : Optional[Any] = decoder_num_attention_heads lowercase__ : Any = decoder_hidden_size lowercase__ : Any = decoder_num_hidden_layers lowercase__ : Union[str, Any] = decoder_intermediate_size lowercase__ : int = mask_ratio lowercase__ : Tuple = norm_pix_loss
333
1
# Lint as: python3 import dataclasses import re from dataclasses import dataclass from functools import total_ordering from typing import Optional, Union _lowerCamelCase : Tuple = re.compile(r"""^(?P<major>\d+)""" r"""\.(?P<minor>\d+)""" r"""\.(?P<patch>\d+)$""") @total_ordering @dataclass class UpperCamelCase_ : '''simple docstring''' UpperCAmelCase__ = 42 UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = None def SCREAMING_SNAKE_CASE ( self : Any) ->Any: '''simple docstring''' A__ , A__ , A__ = _str_to_version_tuple(self.version_str) def __repr__( self : Dict) ->List[str]: '''simple docstring''' return f"""{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}""" @property def SCREAMING_SNAKE_CASE ( self : Optional[int]) ->List[str]: '''simple docstring''' return self.major, self.minor, self.patch def SCREAMING_SNAKE_CASE ( self : Optional[int] , UpperCAmelCase__ : Union[str, Any]) ->List[Any]: '''simple docstring''' if isinstance(UpperCAmelCase__ , UpperCAmelCase__): return Version(UpperCAmelCase__) elif isinstance(UpperCAmelCase__ , UpperCAmelCase__): return other raise TypeError(f"""{other} (type {type(UpperCAmelCase__)}) cannot be compared to version.""") def __eq__( self : Tuple , UpperCAmelCase__ : int) ->Union[str, Any]: '''simple docstring''' try: A__ = self._validate_operand(UpperCAmelCase__) except (TypeError, ValueError): return False else: return self.tuple == other.tuple def __lt__( self : List[Any] , UpperCAmelCase__ : Union[str, Any]) ->List[str]: '''simple docstring''' A__ = self._validate_operand(UpperCAmelCase__) return self.tuple < other.tuple def __hash__( self : int) ->Any: '''simple docstring''' return hash(_version_tuple_to_str(self.tuple)) @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[int] , UpperCAmelCase__ : int) ->List[str]: '''simple docstring''' A__ = {f.name for f in dataclasses.fields(cls)} return cls(**{k: v for k, v in dic.items() if k in field_names}) def SCREAMING_SNAKE_CASE ( self : Dict) ->str: '''simple docstring''' return self.version_str def SCREAMING_SNAKE_CASE ( lowercase_ ) -> Union[str, Any]: """simple docstring""" A__ = _VERSION_REG.match(lowercase_ ) if not res: raise ValueError(f"""Invalid version '{version_str}'. Format should be x.y.z with {{x,y,z}} being digits.""" ) return tuple(int(lowercase_ ) for v in [res.group('''major''' ), res.group('''minor''' ), res.group('''patch''' )] ) def SCREAMING_SNAKE_CASE ( lowercase_ ) -> Union[str, Any]: """simple docstring""" return ".".join(str(lowercase_ ) for v in version_tuple )
14
from __future__ import annotations import typing from collections.abc import Iterable import numpy as np _lowerCamelCase : str = typing.Union[Iterable[float], Iterable[int], np.ndarray] # noqa: UP007 _lowerCamelCase : Tuple = typing.Union[np.floataa, int, float] # noqa: UP007 def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ ) -> VectorOut: """simple docstring""" return np.sqrt(np.sum((np.asarray(lowercase_ ) - np.asarray(lowercase_ )) ** 2 ) ) def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ ) -> VectorOut: """simple docstring""" return sum((va - va) ** 2 for va, va in zip(lowercase_ , lowercase_ ) ) ** (1 / 2) if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( ) -> None: """simple docstring""" from timeit import timeit print('''Without Numpy''' ) print( timeit( '''euclidean_distance_no_np([1, 2, 3], [4, 5, 6])''' , number=10_000 , globals=globals() , ) ) print('''With Numpy''' ) print( timeit( '''euclidean_distance([1, 2, 3], [4, 5, 6])''' , number=10_000 , globals=globals() , ) ) benchmark()
14
1
import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def A (__A : List[Any] , __A : Optional[Any] , __A : Optional[Any] , __A : Dict=None , __A : Optional[Any]=None , __A : Optional[int]=None , __A : Optional[Any]=None , __A : Any=None , ) -> Dict: """simple docstring""" if attention_mask is None: UpperCAmelCase_ = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: UpperCAmelCase_ = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: UpperCAmelCase_ = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=__A ) if decoder_head_mask is None: UpperCAmelCase_ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__A ) if cross_attn_head_mask is None: UpperCAmelCase_ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__A ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class __snake_case : def __init__( self : List[str] , _snake_case : Union[str, Any] , _snake_case : int=13 , _snake_case : Dict=7 , _snake_case : Tuple=True , _snake_case : List[Any]=False , _snake_case : Tuple=99 , _snake_case : Optional[Any]=16 , _snake_case : Optional[int]=2 , _snake_case : List[Any]=4 , _snake_case : Any=4 , _snake_case : Dict="relu" , _snake_case : Optional[Any]=0.1 , _snake_case : int=0.1 , _snake_case : Tuple=0.0 , _snake_case : int=0.0 , _snake_case : Any=20 , _snake_case : Union[str, Any]=2 , _snake_case : str=1 , _snake_case : Union[str, Any]=0 , ): """simple docstring""" UpperCAmelCase_ = parent UpperCAmelCase_ = batch_size UpperCAmelCase_ = seq_length UpperCAmelCase_ = is_training 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_ = encoder_layerdrop UpperCAmelCase_ = decoder_layerdrop UpperCAmelCase_ = max_position_embeddings UpperCAmelCase_ = eos_token_id UpperCAmelCase_ = pad_token_id UpperCAmelCase_ = bos_token_id def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) UpperCAmelCase_ = self.eos_token_id # Eos Token UpperCAmelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input UpperCAmelCase_ = input_ids.clamp(self.pad_token_id + 1) UpperCAmelCase_ = decoder_input_ids.clamp(self.pad_token_id + 1) UpperCAmelCase_ = self.get_config() UpperCAmelCase_ = prepare_mam_aaa_inputs_dict(_snake_case , _snake_case , _snake_case) return config, inputs_dict def lowerCamelCase ( self : int): """simple docstring""" return MaMaaaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , encoder_layerdrop=self.encoder_layerdrop , decoder_layerdrop=self.decoder_layerdrop , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , ) def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.prepare_config_and_inputs() return config, inputs_dict def lowerCamelCase ( self : str , _snake_case : List[Any] , _snake_case : Union[str, Any]): """simple docstring""" UpperCAmelCase_ = MaMaaaModel(config=_snake_case).get_decoder().to(_snake_case).eval() UpperCAmelCase_ = inputs_dict['''input_ids'''] UpperCAmelCase_ = inputs_dict['''attention_mask'''] UpperCAmelCase_ = inputs_dict['''head_mask'''] # first forward pass UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case , head_mask=_snake_case , use_cache=_snake_case) UpperCAmelCase_ , UpperCAmelCase_ = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids UpperCAmelCase_ = ids_tensor((self.batch_size, 3) , config.vocab_size) UpperCAmelCase_ = ids_tensor((self.batch_size, 3) , 2) # append to next input_ids and UpperCAmelCase_ = torch.cat([input_ids, next_tokens] , dim=-1) UpperCAmelCase_ = torch.cat([attention_mask, next_attn_mask] , dim=-1) UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case)['''last_hidden_state'''] UpperCAmelCase_ = model(_snake_case , attention_mask=_snake_case , past_key_values=_snake_case)[ '''last_hidden_state''' ] # select random slice UpperCAmelCase_ = ids_tensor((1,) , output_from_past.shape[-1]).item() UpperCAmelCase_ = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCAmelCase_ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_snake_case , _snake_case , atol=1e-2)) def lowerCamelCase ( self : Optional[int] , _snake_case : str , _snake_case : List[Any]): """simple docstring""" UpperCAmelCase_ = MaMaaaModel(config=_snake_case).to(_snake_case).eval() UpperCAmelCase_ = model(**_snake_case) UpperCAmelCase_ = outputs.encoder_last_hidden_state UpperCAmelCase_ = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: UpperCAmelCase_ = model.get_encoder() encoder.save_pretrained(_snake_case) UpperCAmelCase_ = MaMaaaEncoder.from_pretrained(_snake_case).to(_snake_case) UpperCAmelCase_ = encoder(inputs_dict['''input_ids'''] , attention_mask=inputs_dict['''attention_mask'''])[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3) with tempfile.TemporaryDirectory() as tmpdirname: UpperCAmelCase_ = model.get_decoder() decoder.save_pretrained(_snake_case) UpperCAmelCase_ = MaMaaaDecoder.from_pretrained(_snake_case).to(_snake_case) UpperCAmelCase_ = decoder( input_ids=inputs_dict['''decoder_input_ids'''] , attention_mask=inputs_dict['''decoder_attention_mask'''] , encoder_hidden_states=_snake_case , encoder_attention_mask=inputs_dict['''attention_mask'''] , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3) @require_torch class __snake_case ( a , a , a , unittest.TestCase ): UpperCAmelCase__ : List[str] = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) UpperCAmelCase__ : List[str] = (MaMaaaForConditionalGeneration,) if is_torch_available() else () UpperCAmelCase__ : Union[str, Any] = ( { '''conversational''': MaMaaaForConditionalGeneration, '''feature-extraction''': MaMaaaModel, '''summarization''': MaMaaaForConditionalGeneration, '''text2text-generation''': MaMaaaForConditionalGeneration, '''translation''': MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) UpperCAmelCase__ : Optional[int] = True UpperCAmelCase__ : Any = True UpperCAmelCase__ : List[str] = False UpperCAmelCase__ : Optional[int] = False def lowerCamelCase ( self : Union[str, Any] , _snake_case : List[Any] , _snake_case : Dict , _snake_case : Any , _snake_case : List[Any] , _snake_case : Tuple): """simple docstring""" if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = MaMaaaModelTester(self) UpperCAmelCase_ = ConfigTester(self , config_class=_snake_case) def lowerCamelCase ( self : List[Any]): """simple docstring""" self.config_tester.run_common_tests() def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: UpperCAmelCase_ = model_class(_snake_case) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_snake_case) UpperCAmelCase_ , UpperCAmelCase_ = model_class.from_pretrained(_snake_case , output_loading_info=_snake_case) self.assertEqual(info['''missing_keys'''] , []) def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*_snake_case) def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*_snake_case) def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): UpperCAmelCase_ = model_class(_snake_case) model.to(_snake_case) model.eval() UpperCAmelCase_ = copy.deepcopy(self._prepare_for_class(_snake_case , _snake_case)) if not self.is_encoder_decoder: UpperCAmelCase_ = inputs['''input_ids'''] del inputs["input_ids"] else: UpperCAmelCase_ = inputs['''input_ids'''] UpperCAmelCase_ = inputs.get('''decoder_input_ids''' , _snake_case) del inputs["input_ids"] inputs.pop('''decoder_input_ids''' , _snake_case) UpperCAmelCase_ = model.get_input_embeddings() if not self.is_encoder_decoder: UpperCAmelCase_ = wte(_snake_case) else: UpperCAmelCase_ = wte(_snake_case) UpperCAmelCase_ = wte(_snake_case) with torch.no_grad(): model(**_snake_case)[0] def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.model_tester.prepare_config_and_inputs() UpperCAmelCase_ = input_dict['''input_ids'''] UpperCAmelCase_ = input_ids.ne(1).to(_snake_case) UpperCAmelCase_ = MaMaaaForConditionalGeneration(_snake_case).eval().to(_snake_case) if torch_device == "cuda": model.half() model.generate(_snake_case , attention_mask=_snake_case) model.generate(num_beams=4 , do_sample=_snake_case , early_stopping=_snake_case , num_return_sequences=3) def A (__A : List[str] ) -> Any: """simple docstring""" return torch.tensor(__A , dtype=torch.long , device=__A ) snake_case_ : Tuple = 1e-4 @require_torch @require_sentencepiece @require_tokenizers @slow class __snake_case ( unittest.TestCase ): @cached_property def lowerCamelCase ( self : Tuple): """simple docstring""" return MaMaaaTokenizer.from_pretrained('''facebook/m2m100_418M''') def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = MaMaaaModel.from_pretrained('''facebook/m2m100_418M''').to(_snake_case) UpperCAmelCase_ = _long_tensor([[128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38, 2]]) UpperCAmelCase_ = _long_tensor([[2, 128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38]]) UpperCAmelCase_ = prepare_mam_aaa_inputs_dict(model.config , _snake_case , _snake_case) with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case)[0] UpperCAmelCase_ = torch.Size((1, 11, 1024)) self.assertEqual(output.shape , _snake_case) # change to expected output here UpperCAmelCase_ = torch.tensor( [[-0.7_7_8_0, -0.1_6_7_6, 0.1_0_3_8], [-6.7_5_5_6, -1.3_9_9_2, 0.0_5_6_7], [-7.5_3_8_3, -0.5_9_2_0, -0.2_7_7_9]] , device=_snake_case) self.assertTrue(torch.allclose(output[:, :3, :3] , _snake_case , atol=_snake_case)) def lowerCamelCase ( self : int): """simple docstring""" UpperCAmelCase_ = MaMaaaForConditionalGeneration.from_pretrained('''facebook/m2m100_418M''').to(_snake_case) # change to intended input UpperCAmelCase_ = _long_tensor([[128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38, 2]]) UpperCAmelCase_ = _long_tensor([[2, 128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38]]) UpperCAmelCase_ = prepare_mam_aaa_inputs_dict(model.config , _snake_case , _snake_case) with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case)[0] UpperCAmelCase_ = torch.Size((1, 11, model.config.vocab_size)) self.assertEqual(output.shape , _snake_case) # change to expected output here UpperCAmelCase_ = torch.tensor( [[-1.0_4_4_8, -1.0_4_1_1, 3.7_9_9_2], [-3.2_1_9_1, -3.2_3_8_6, -1.3_4_5_1], [-3.6_2_1_0, -3.5_9_9_3, 0.4_9_2_5]] , device=_snake_case) self.assertTrue(torch.allclose(output[:, :3, :3] , _snake_case , atol=_snake_case)) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = MaMaaaForConditionalGeneration.from_pretrained('''facebook/m2m100_418M''').to(_snake_case) UpperCAmelCase_ = MaMaaaTokenizer.from_pretrained('''facebook/m2m100_418M''' , src_lang='''fr''' , tgt_lang='''en''') UpperCAmelCase_ = [ '''L\'affaire NSA souligne l\'absence totale de débat sur le renseignement''', '''Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.''', '''Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent''' ''' Fabius convoque l\'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de''' ''' l\'ampleur de la surveillance américaine sur l\'ensemble des communications en France.''', ] # The below article tests that we don't add any hypotheses outside of the top n_beams UpperCAmelCase_ = tokenizer(_snake_case , padding=_snake_case , return_tensors='''pt''') UpperCAmelCase_ = model.generate( input_ids=dct['''input_ids'''].to(_snake_case) , attention_mask=dct['''attention_mask'''].to(_snake_case) , num_beams=5 , forced_bos_token_id=tokenizer.get_lang_id('''en''') , ) UpperCAmelCase_ = [ '''The NSA case highlights the total absence of intelligence debate''', '''I think there are two levels of response from the French government.''', '''When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.''' ''' Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all''' ''' communications in France.''', ] UpperCAmelCase_ = tokenizer.batch_decode( hypotheses_batch.tolist() , clean_up_tokenization_spaces=_snake_case , skip_special_tokens=_snake_case) assert generated == expected_en
350
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets snake_case_ : Dict = "\\n@inproceedings{snover-etal-2006-study,\n title = \"A Study of Translation Edit Rate with Targeted Human Annotation\",\n author = \"Snover, Matthew and\n Dorr, Bonnie and\n Schwartz, Rich and\n Micciulla, Linnea and\n Makhoul, John\",\n booktitle = \"Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers\",\n month = aug # \" 8-12\",\n year = \"2006\",\n address = \"Cambridge, Massachusetts, USA\",\n publisher = \"Association for Machine Translation in the Americas\",\n url = \"https://aclanthology.org/2006.amta-papers.25\",\n pages = \"223--231\",\n}\n@inproceedings{post-2018-call,\n title = \"A Call for Clarity in Reporting {BLEU} Scores\",\n author = \"Post, Matt\",\n booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\",\n month = oct,\n year = \"2018\",\n address = \"Belgium, Brussels\",\n publisher = \"Association for Computational Linguistics\",\n url = \"https://www.aclweb.org/anthology/W18-6319\",\n pages = \"186--191\",\n}\n" snake_case_ : List[str] = "\\nTER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a\nhypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu\n(https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found\nhere: https://github.com/jhclark/tercom.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu's required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information.\n" snake_case_ : List[Any] = "\nProduces TER scores alongside the number of edits and reference length.\n\nArgs:\n predictions (list of str): The system stream (a sequence of segments).\n references (list of list of str): A list of one or more reference streams (each a sequence of segments).\n normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.\n ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.\n support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters,\n as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana.\n Only applies if `normalized = True`. Defaults to `False`.\n case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`.\n\nReturns:\n 'score' (float): TER score (num_edits / sum_ref_lengths * 100)\n 'num_edits' (int): The cumulative number of edits\n 'ref_length' (float): The cumulative average reference length\n\nExamples:\n Example 1:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\",\n ... \"What did the TER metric user say to the developer?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"],\n ... [\"Your jokes are...\", \"...TERrible\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 150.0, 'num_edits': 15, 'ref_length': 10.0}\n\n Example 2:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 62.5, 'num_edits': 5, 'ref_length': 8.0}\n\n Example 3:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... normalized=True,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 57.14285714285714, 'num_edits': 6, 'ref_length': 10.5}\n\n Example 4:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... ignore_punct=True,\n ... case_sensitive=False)\n >>> print(results)\n {'score': 0.0, 'num_edits': 0, 'ref_length': 8.0}\n\n Example 5:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\",\n ... \"What did the TER metric user say to the developer?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"],\n ... [\"Your jokes are...\", \"...TERrible\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... ignore_punct=True,\n ... case_sensitive=False)\n >>> print(results)\n {'score': 100.0, 'num_edits': 10, 'ref_length': 10.0}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def lowerCamelCase ( self : Tuple): """simple docstring""" if version.parse(scb.__version__) < version.parse('''1.4.12'''): raise ImportWarning( '''To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n''' '''You can install it with `pip install "sacrebleu>=1.4.12"`.''') return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''http://www.cs.umd.edu/~snover/tercom/''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence'''), '''references''': datasets.Sequence(datasets.Value('''string''' , id='''sequence''') , id='''references'''), }) , codebase_urls=['''https://github.com/mjpost/sacreBLEU#ter'''] , reference_urls=[ '''https://github.com/jhclark/tercom''', ] , ) def lowerCamelCase ( self : Union[str, Any] , _snake_case : Optional[int] , _snake_case : List[Any] , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , _snake_case : bool = False , ): """simple docstring""" UpperCAmelCase_ = len(references[0]) if any(len(_snake_case) != references_per_prediction for refs in references): raise ValueError('''Sacrebleu requires the same number of references for each prediction''') UpperCAmelCase_ = [[refs[i] for refs in references] for i in range(_snake_case)] UpperCAmelCase_ = TER( normalized=_snake_case , no_punct=_snake_case , asian_support=_snake_case , case_sensitive=_snake_case , ) UpperCAmelCase_ = sb_ter.corpus_score(_snake_case , _snake_case) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
7
0
'''simple docstring''' from __future__ import annotations 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__ : int ): '''simple docstring''' lowerCAmelCase_ : List[str] = str(A__ ) lowerCAmelCase_ : Union[str, Any] = [n] for i in range(1 , len(A__ ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def UpperCamelCase_ ( A__ : int ): '''simple docstring''' if len(str(A__ ) ) > 3: if not is_prime(int(str(A__ )[-3:] ) ) or not is_prime(int(str(A__ )[:3] ) ): return False return True def UpperCamelCase_ ( A__ : int = 11 ): '''simple docstring''' lowerCAmelCase_ : list[int] = [] lowerCAmelCase_ : Union[str, Any] = 13 while len(A__ ) != count: if validate(A__ ): lowerCAmelCase_ : Union[str, Any] = list_truncated_nums(A__ ) if all(is_prime(A__ ) for i in list_nums ): list_truncated_primes.append(A__ ) num += 2 return list_truncated_primes def UpperCamelCase_ ( ): '''simple docstring''' return sum(compute_truncated_primes(11 ) ) if __name__ == "__main__": print(F'''{sum(compute_truncated_primes(11)) = }''')
120
'''simple docstring''' def UpperCamelCase_ ( A__ : int ): '''simple docstring''' lowerCAmelCase_ : Union[str, Any] = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def UpperCamelCase_ ( A__ : int = 50_00 ): '''simple docstring''' lowerCAmelCase_ : Optional[int] = [(i * (3 * i - 1)) // 2 for i in range(1 , A__ )] for i, pentagonal_i in enumerate(A__ ): for j in range(A__ , len(A__ ) ): lowerCAmelCase_ : int = pentagonal_nums[j] lowerCAmelCase_ : Union[str, Any] = pentagonal_i + pentagonal_j lowerCAmelCase_ : List[Any] = pentagonal_j - pentagonal_i if is_pentagonal(A__ ) and is_pentagonal(A__ ): return b return -1 if __name__ == "__main__": print(F'''{solution() = }''')
120
1
from collections import defaultdict def lowerCamelCase_ ( _a , _a ): """simple docstring""" lowerCAmelCase__ : str = first_str.lower().strip() lowerCAmelCase__ : Union[str, Any] = second_str.lower().strip() # Remove whitespace lowerCAmelCase__ : List[str] = first_str.replace(''' ''' , '''''' ) lowerCAmelCase__ : Union[str, Any] = second_str.replace(''' ''' , '''''' ) # Strings of different lengths are not anagrams if len(_a ) != len(_a ): return False # Default values for count should be 0 lowerCAmelCase__ : defaultdict[str, int] = defaultdict(_a ) # For each character in input strings, # increment count in the corresponding for i in range(len(_a ) ): count[first_str[i]] += 1 count[second_str[i]] -= 1 return all(_count == 0 for _count in count.values() ) if __name__ == "__main__": from doctest import testmod testmod() lowerCamelCase = input('''Enter the first string ''').strip() lowerCamelCase = input('''Enter the second string ''').strip() lowerCamelCase = check_anagrams(input_a, input_b) print(f'''{input_a} and {input_b} are {"" if status else "not "}anagrams.''')
211
import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def lowerCamelCase_ ( _a ): """simple docstring""" def wrapper(*_a , **_a ): lowerCAmelCase__ : List[str] = timeit.default_timer() lowerCAmelCase__ : List[Any] = func(*_a , **_a ) lowerCAmelCase__ : Any = timeit.default_timer() - starttime return delta lowerCAmelCase__ : Any = func.__name__ return wrapper def lowerCamelCase_ ( _a , _a=100 , _a=None ): """simple docstring""" lowerCAmelCase__ : str = [] lowerCAmelCase__ : str = seq_shapes or {} for i in range(_a ): lowerCAmelCase__ : List[str] = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(_a , _ArrayXD ): lowerCAmelCase__ : List[str] = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(_a , datasets.Value ): if v.dtype == "string": lowerCAmelCase__ : Dict = '''The small grey turtle was surprisingly fast when challenged.''' else: lowerCAmelCase__ : Any = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(_a , datasets.Sequence ): while isinstance(_a , datasets.Sequence ): lowerCAmelCase__ : Optional[int] = v.feature lowerCAmelCase__ : str = seq_shapes[k] lowerCAmelCase__ : Any = np.random.rand(*_a ).astype(v.dtype ) lowerCAmelCase__ : int = data dummy_data.append((i, example) ) return dummy_data def lowerCamelCase_ ( _a , _a , _a=100 , _a=None ): """simple docstring""" lowerCAmelCase__ : Optional[Any] = generate_examples(_a , num_examples=_a , seq_shapes=_a ) with ArrowWriter(features=_a , path=_a ) as writer: for key, record in dummy_data: lowerCAmelCase__ : Optional[int] = features.encode_example(_a ) writer.write(_a ) lowerCAmelCase__ , lowerCAmelCase__ : List[str] = writer.finalize() if not num_final_examples == num_examples: raise ValueError( f'Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.' ) lowerCAmelCase__ : List[Any] = datasets.Dataset.from_file(filename=_a , info=datasets.DatasetInfo(features=_a ) ) return dataset
211
1
'''simple docstring''' import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def _A ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ): """simple docstring""" with open(_lowerCAmelCase ) as metadata_file: __lowercase =json.load(_lowerCAmelCase ) __lowercase =LukeConfig(use_entity_aware_attention=_lowerCAmelCase , **metadata['model_config'] ) # Load in the weights from the checkpoint_path __lowercase =torch.load(_lowerCAmelCase , map_location='cpu' )['module'] # Load the entity vocab file __lowercase =load_original_entity_vocab(_lowerCAmelCase ) # add an entry for [MASK2] __lowercase =max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 __lowercase =XLMRobertaTokenizer.from_pretrained(metadata['model_config']['bert_model_name'] ) # Add special tokens to the token vocabulary for downstream tasks __lowercase =AddedToken('<ent>' , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase ) __lowercase =AddedToken('<ent2>' , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase ) tokenizer.add_special_tokens({'additional_special_tokens': [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(f"""Saving tokenizer to {pytorch_dump_folder_path}""" ) tokenizer.save_pretrained(_lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , 'tokenizer_config.json' ) , 'r' ) as f: __lowercase =json.load(_lowerCAmelCase ) __lowercase ='MLukeTokenizer' with open(os.path.join(_lowerCAmelCase , 'tokenizer_config.json' ) , 'w' ) as f: json.dump(_lowerCAmelCase , _lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , MLukeTokenizer.vocab_files_names['entity_vocab_file'] ) , 'w' ) as f: json.dump(_lowerCAmelCase , _lowerCAmelCase ) __lowercase =MLukeTokenizer.from_pretrained(_lowerCAmelCase ) # Initialize the embeddings of the special tokens __lowercase =tokenizer.convert_tokens_to_ids(['@'] )[0] __lowercase =tokenizer.convert_tokens_to_ids(['#'] )[0] __lowercase =state_dict['embeddings.word_embeddings.weight'] __lowercase =word_emb[ent_init_index].unsqueeze(0 ) __lowercase =word_emb[enta_init_index].unsqueeze(0 ) __lowercase =torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: __lowercase =state_dict[bias_name] __lowercase =decoder_bias[ent_init_index].unsqueeze(0 ) __lowercase =decoder_bias[enta_init_index].unsqueeze(0 ) __lowercase =torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: __lowercase =f"""encoder.layer.{layer_index}.attention.self.""" __lowercase =state_dict[prefix + matrix_name] __lowercase =state_dict[prefix + matrix_name] __lowercase =state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks __lowercase =state_dict['entity_embeddings.entity_embeddings.weight'] __lowercase =entity_emb[entity_vocab['[MASK]']].unsqueeze(0 ) __lowercase =torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' __lowercase =state_dict['entity_predictions.bias'] __lowercase =entity_prediction_bias[entity_vocab['[MASK]']].unsqueeze(0 ) __lowercase =torch.cat([entity_prediction_bias, entity_mask_bias] ) __lowercase =LukeForMaskedLM(config=_lowerCAmelCase ).eval() state_dict.pop('entity_predictions.decoder.weight' ) state_dict.pop('lm_head.decoder.weight' ) state_dict.pop('lm_head.decoder.bias' ) __lowercase =OrderedDict() for key, value in state_dict.items(): if not (key.startswith('lm_head' ) or key.startswith('entity_predictions' )): __lowercase =state_dict[key] else: __lowercase =state_dict[key] __lowercase , __lowercase =model.load_state_dict(_lowerCAmelCase , strict=_lowerCAmelCase ) if set(_lowerCAmelCase ) != {"luke.embeddings.position_ids"}: raise ValueError(f"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(_lowerCAmelCase ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(f"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs __lowercase =MLukeTokenizer.from_pretrained(_lowerCAmelCase , task='entity_classification' ) __lowercase ='ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).' __lowercase =(0, 9) __lowercase =tokenizer(_lowerCAmelCase , entity_spans=[span] , return_tensors='pt' ) __lowercase =model(**_lowerCAmelCase ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base __lowercase =torch.Size((1, 33, 768) ) __lowercase =torch.tensor([[0.08_92, 0.05_96, -0.28_19], [0.01_34, 0.11_99, 0.05_73], [-0.01_69, 0.09_27, 0.06_44]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , _lowerCAmelCase , atol=1e-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base __lowercase =torch.Size((1, 1, 768) ) __lowercase =torch.tensor([[-0.14_82, 0.06_09, 0.03_22]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( f"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" f""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , _lowerCAmelCase , atol=1e-4 ): raise ValueError # Verify masked word/entity prediction __lowercase =MLukeTokenizer.from_pretrained(_lowerCAmelCase ) __lowercase ='Tokyo is the capital of <mask>.' __lowercase =(24, 30) __lowercase =tokenizer(_lowerCAmelCase , entity_spans=[span] , return_tensors='pt' ) __lowercase =model(**_lowerCAmelCase ) __lowercase =encoding['input_ids'][0].tolist() __lowercase =input_ids.index(tokenizer.convert_tokens_to_ids('<mask>' ) ) __lowercase =outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(_lowerCAmelCase ) __lowercase =outputs.entity_logits[0][0].argmax().item() __lowercase =[ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('en:' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('Saving PyTorch model to {}'.format(_lowerCAmelCase ) ) model.save_pretrained(_lowerCAmelCase ) def _A ( _lowerCAmelCase ): """simple docstring""" __lowercase =['[MASK]', '[PAD]', '[UNK]'] __lowercase =[json.loads(_lowerCAmelCase ) for line in open(_lowerCAmelCase )] __lowercase ={} for entry in data: __lowercase =entry['id'] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: __lowercase =entity_id break __lowercase =f"""{language}:{entity_name}""" __lowercase =entity_id return new_mapping if __name__ == "__main__": lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument("""--checkpoint_path""", type=str, help="""Path to a pytorch_model.bin file.""") parser.add_argument( """--metadata_path""", default=None, type=str, help="""Path to a metadata.json file, defining the configuration.""" ) parser.add_argument( """--entity_vocab_path""", default=None, type=str, help="""Path to an entity_vocab.tsv file, containing the entity vocabulary.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to where to dump the output PyTorch model.""" ) parser.add_argument( """--model_size""", default="""base""", type=str, choices=["""base""", """large"""], help="""Size of the model to be converted.""" ) lowerCamelCase = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
166
'''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 lowerCamelCase = logging.get_logger(__name__) lowerCamelCase = """▁""" lowerCamelCase = {"""vocab_file""": """spiece.model"""} lowerCamelCase = { """vocab_file""": { """google/reformer-crime-and-punishment""": ( """https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model""" ) } } lowerCamelCase = { """google/reformer-crime-and-punishment""": 52_4288, } class _UpperCamelCase ( A ): '''simple docstring''' lowerCAmelCase__ = VOCAB_FILES_NAMES lowerCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase__ = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[Any] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any]="</s>" , _lowerCAmelCase : Any="<unk>" , _lowerCAmelCase : int=[] , _lowerCAmelCase : Optional[Dict[str, Any]] = None , **_lowerCAmelCase : List[Any] , ): '''simple docstring''' __lowercase ={} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=_lowerCAmelCase , unk_token=_lowerCAmelCase , additional_special_tokens=_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 __lowerCamelCase ( self : int): '''simple docstring''' return self.sp_model.get_piece_size() def __lowerCamelCase ( self : Optional[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 : Any): '''simple docstring''' __lowercase =self.__dict__.copy() __lowercase =None return state def __setstate__( self : Optional[int] , _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 __lowerCamelCase ( self : List[str] , _lowerCAmelCase : str): '''simple docstring''' return self.sp_model.encode(_lowerCAmelCase , out_type=_lowerCAmelCase) def __lowerCamelCase ( self : Optional[Any] , _lowerCAmelCase : List[Any]): '''simple docstring''' return self.sp_model.piece_to_id(_lowerCAmelCase) def __lowerCamelCase ( self : List[Any] , _lowerCAmelCase : Optional[Any]): '''simple docstring''' if index < self.sp_model.get_piece_size(): __lowercase =self.sp_model.IdToPiece(_lowerCAmelCase) return token def __lowerCamelCase ( self : Any , _lowerCAmelCase : Optional[int]): '''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 __lowerCamelCase ( self : int , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None): '''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,)
166
1
"""simple docstring""" from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_torch_available(): import torch if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm UpperCamelCase_ = logging.get_logger(__name__) @dataclass class snake_case ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = [ """no_inference""", """no_cuda""", """no_tpu""", """no_speed""", """no_memory""", """no_env_print""", """no_multi_process""", ] def __init__( self , **__UpperCAmelCase) ->Dict: for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: a_ = deprecated_arg[3:] setattr(self , __UpperCAmelCase , not kwargs.pop(__UpperCAmelCase)) logger.warning( F'''{deprecated_arg} is depreciated. Please use --no_{positive_arg} or''' F''' {positive_arg}={kwargs[positive_arg]}''') a_ = kwargs.pop("torchscript" , self.torchscript) a_ = kwargs.pop("torch_xla_tpu_print_metrics" , self.torch_xla_tpu_print_metrics) a_ = kwargs.pop("fp16_opt_level" , self.fpaa_opt_level) super().__init__(**__UpperCAmelCase) a_ : bool = field(default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """Trace the models using torchscript"""} ) a_ : bool = field(default=SCREAMING_SNAKE_CASE_ , metadata={"""help""": """Print Xla/PyTorch tpu metrics"""} ) a_ : str = field( default="""O1""" , metadata={ """help""": ( """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']. """ """See details at https://nvidia.github.io/apex/amp.html""" ) } , ) @cached_property def UpperCAmelCase__ ( self) ->Tuple["torch.device", int]: requires_backends(self , ["torch"]) logger.info("PyTorch: setting up devices") if not self.cuda: a_ = torch.device("cpu") a_ = 0 elif is_torch_tpu_available(): a_ = xm.xla_device() a_ = 0 else: a_ = torch.device("cuda" if torch.cuda.is_available() else "cpu") a_ = torch.cuda.device_count() return device, n_gpu @property def UpperCAmelCase__ ( self) ->Any: return is_torch_tpu_available() and self.tpu @property def UpperCAmelCase__ ( self) ->int: requires_backends(self , ["torch"]) # TODO(PVP): currently only single GPU is supported return torch.cuda.current_device() @property def UpperCAmelCase__ ( self) ->"torch.device": requires_backends(self , ["torch"]) return self._setup_devices[0] @property def UpperCAmelCase__ ( self) ->Optional[int]: requires_backends(self , ["torch"]) return self._setup_devices[1] @property def UpperCAmelCase__ ( self) ->Optional[Any]: return self.n_gpu > 0
303
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase_ = { 'configuration_lilt': ['LILT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LiltConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ 'LILT_PRETRAINED_MODEL_ARCHIVE_LIST', 'LiltForQuestionAnswering', 'LiltForSequenceClassification', 'LiltForTokenClassification', 'LiltModel', 'LiltPreTrainedModel', ] if TYPE_CHECKING: from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_lilt import ( LILT_PRETRAINED_MODEL_ARCHIVE_LIST, LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, LiltPreTrainedModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
303
1
"""simple docstring""" import gc import random import tempfile import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline from diffusers.utils import floats_tensor, nightly, torch_device from diffusers.utils.testing_utils import require_torch_gpu class _SCREAMING_SNAKE_CASE( unittest.TestCase ): def _UpperCamelCase ( self ) -> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def _UpperCamelCase ( self ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE :Optional[int] = 1 __SCREAMING_SNAKE_CASE :str = 3 __SCREAMING_SNAKE_CASE :str = (32, 32) __SCREAMING_SNAKE_CASE :int = floats_tensor((batch_size, num_channels) + sizes ,rng=random.Random(0 ) ).to(SCREAMING_SNAKE_CASE__ ) return image @property def _UpperCamelCase ( self ) -> List[str]: """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :int = UNetaDConditionModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') ,up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') ,cross_attention_dim=32 ,) return model @property def _UpperCamelCase ( self ) -> int: """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :Optional[int] = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=3 ,out_channels=3 ,down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] ,up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] ,latent_channels=4 ,) return model @property def _UpperCamelCase ( self ) -> Tuple: """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE :Tuple = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=32 ,intermediate_size=37 ,layer_norm_eps=1E-05 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=10_00 ,) return CLIPTextModel(SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self ) -> Optional[int]: """simple docstring""" def extract(*SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__ ): class _SCREAMING_SNAKE_CASE: def __init__( self ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE :str = torch.ones([0] ) def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: """simple docstring""" self.pixel_values.to(SCREAMING_SNAKE_CASE__ ) return self return Out() return extract def _UpperCamelCase ( self ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :List[Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE :Tuple = self.dummy_cond_unet __SCREAMING_SNAKE_CASE :List[str] = DDIMScheduler( beta_start=0.0_0_0_8_5 ,beta_end=0.0_1_2 ,beta_schedule='''scaled_linear''' ,clip_sample=SCREAMING_SNAKE_CASE__ ,set_alpha_to_one=SCREAMING_SNAKE_CASE__ ,) __SCREAMING_SNAKE_CASE :Optional[Any] = self.dummy_vae __SCREAMING_SNAKE_CASE :List[str] = self.dummy_text_encoder __SCREAMING_SNAKE_CASE :Tuple = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # make sure here that pndm scheduler skips prk __SCREAMING_SNAKE_CASE :List[Any] = StableDiffusionPipeline( unet=SCREAMING_SNAKE_CASE__ ,scheduler=SCREAMING_SNAKE_CASE__ ,vae=SCREAMING_SNAKE_CASE__ ,text_encoder=SCREAMING_SNAKE_CASE__ ,tokenizer=SCREAMING_SNAKE_CASE__ ,safety_checker=SCREAMING_SNAKE_CASE__ ,feature_extractor=self.dummy_extractor ,) __SCREAMING_SNAKE_CASE :Optional[Any] = sd_pipe.to(SCREAMING_SNAKE_CASE__ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :str = '''A painting of a squirrel eating a burger''' __SCREAMING_SNAKE_CASE :str = torch.Generator(device=SCREAMING_SNAKE_CASE__ ).manual_seed(0 ) __SCREAMING_SNAKE_CASE :Optional[Any] = sd_pipe([prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type='''np''' ) __SCREAMING_SNAKE_CASE :Dict = output.images __SCREAMING_SNAKE_CASE :Union[str, Any] = torch.Generator(device=SCREAMING_SNAKE_CASE__ ).manual_seed(0 ) __SCREAMING_SNAKE_CASE :Optional[int] = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type='''np''' ,return_dict=SCREAMING_SNAKE_CASE__ ,)[0] __SCREAMING_SNAKE_CASE :Any = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :List[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __SCREAMING_SNAKE_CASE :Optional[int] = np.array([0.5_7_5_6, 0.6_1_1_8, 0.5_0_0_5, 0.5_0_4_1, 0.5_4_7_1, 0.4_7_2_6, 0.4_9_7_6, 0.4_8_6_5, 0.4_8_6_4] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _UpperCamelCase ( self ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE :List[str] = '''cpu''' # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE :int = self.dummy_cond_unet __SCREAMING_SNAKE_CASE :Union[str, Any] = PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :List[Any] = self.dummy_vae __SCREAMING_SNAKE_CASE :Optional[Any] = self.dummy_text_encoder __SCREAMING_SNAKE_CASE :Union[str, Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # make sure here that pndm scheduler skips prk __SCREAMING_SNAKE_CASE :Any = StableDiffusionPipeline( unet=SCREAMING_SNAKE_CASE__ ,scheduler=SCREAMING_SNAKE_CASE__ ,vae=SCREAMING_SNAKE_CASE__ ,text_encoder=SCREAMING_SNAKE_CASE__ ,tokenizer=SCREAMING_SNAKE_CASE__ ,safety_checker=SCREAMING_SNAKE_CASE__ ,feature_extractor=self.dummy_extractor ,) __SCREAMING_SNAKE_CASE :str = sd_pipe.to(SCREAMING_SNAKE_CASE__ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Union[str, Any] = '''A painting of a squirrel eating a burger''' __SCREAMING_SNAKE_CASE :Optional[Any] = torch.Generator(device=SCREAMING_SNAKE_CASE__ ).manual_seed(0 ) __SCREAMING_SNAKE_CASE :Tuple = sd_pipe([prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type='''np''' ) __SCREAMING_SNAKE_CASE :Optional[Any] = output.images __SCREAMING_SNAKE_CASE :str = torch.Generator(device=SCREAMING_SNAKE_CASE__ ).manual_seed(0 ) __SCREAMING_SNAKE_CASE :Optional[Any] = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type='''np''' ,return_dict=SCREAMING_SNAKE_CASE__ ,)[0] __SCREAMING_SNAKE_CASE :Optional[int] = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :Optional[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __SCREAMING_SNAKE_CASE :int = np.array([0.5_1_2_5, 0.5_7_1_6, 0.4_8_2_8, 0.5_0_6_0, 0.5_6_5_0, 0.4_7_6_8, 0.5_1_8_5, 0.4_8_9_5, 0.4_9_9_3] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _UpperCamelCase ( self ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :Dict = StableDiffusionPipeline.from_pretrained( '''hf-internal-testing/tiny-stable-diffusion-lms-pipe''' ,safety_checker=SCREAMING_SNAKE_CASE__ ) assert isinstance(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) assert isinstance(pipe.scheduler ,SCREAMING_SNAKE_CASE__ ) assert pipe.safety_checker is None __SCREAMING_SNAKE_CASE :Tuple = pipe('''example prompt''' ,num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :List[str] = StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__ ) # sanity check that the pipeline still works assert pipe.safety_checker is None __SCREAMING_SNAKE_CASE :Optional[int] = pipe('''example prompt''' ,num_inference_steps=2 ).images[0] assert image is not None @unittest.skipIf(torch_device != '''cuda''' ,'''This test requires a GPU''' ) def _UpperCamelCase ( self ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :Any = self.dummy_cond_unet __SCREAMING_SNAKE_CASE :List[str] = PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Dict = self.dummy_vae __SCREAMING_SNAKE_CASE :List[Any] = self.dummy_text_encoder __SCREAMING_SNAKE_CASE :Any = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # put models in fp16 __SCREAMING_SNAKE_CASE :Optional[int] = unet.half() __SCREAMING_SNAKE_CASE :Dict = vae.half() __SCREAMING_SNAKE_CASE :Optional[Any] = bert.half() # make sure here that pndm scheduler skips prk __SCREAMING_SNAKE_CASE :List[Any] = StableDiffusionPipeline( unet=SCREAMING_SNAKE_CASE__ ,scheduler=SCREAMING_SNAKE_CASE__ ,vae=SCREAMING_SNAKE_CASE__ ,text_encoder=SCREAMING_SNAKE_CASE__ ,tokenizer=SCREAMING_SNAKE_CASE__ ,safety_checker=SCREAMING_SNAKE_CASE__ ,feature_extractor=self.dummy_extractor ,) __SCREAMING_SNAKE_CASE :int = sd_pipe.to(SCREAMING_SNAKE_CASE__ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :str = '''A painting of a squirrel eating a burger''' __SCREAMING_SNAKE_CASE :Any = sd_pipe([prompt] ,num_inference_steps=2 ,output_type='''np''' ).images assert image.shape == (1, 64, 64, 3) @nightly @require_torch_gpu class _SCREAMING_SNAKE_CASE( unittest.TestCase ): def _UpperCamelCase ( self ) -> Tuple: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self ) -> Any: """simple docstring""" __SCREAMING_SNAKE_CASE :Tuple = StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' ,safety_checker=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Optional[Any] = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) __SCREAMING_SNAKE_CASE :List[str] = sd_pipe.to(SCREAMING_SNAKE_CASE__ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :str = ( '''portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle''' ''' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with''' ''' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and''' ''' children from bahnhof zoo, detailed ''' ) __SCREAMING_SNAKE_CASE :Union[str, Any] = 40_03_66_03_46 __SCREAMING_SNAKE_CASE :Any = 7 # without safety guidance (sld_guidance_scale = 0) __SCREAMING_SNAKE_CASE :List[Any] = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :List[Any] = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=SCREAMING_SNAKE_CASE__ ,num_inference_steps=50 ,output_type='''np''' ,width=5_12 ,height=5_12 ,sld_guidance_scale=0 ,) __SCREAMING_SNAKE_CASE :str = output.images __SCREAMING_SNAKE_CASE :Any = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :Optional[Any] = [0.2_2_7_8, 0.2_2_3_1, 0.2_2_4_9, 0.2_3_3_3, 0.2_3_0_3, 0.1_8_8_5, 0.2_2_7_3, 0.2_1_4_4, 0.2_1_7_6] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 # without safety guidance (strong configuration) __SCREAMING_SNAKE_CASE :List[Any] = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :List[Any] = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=SCREAMING_SNAKE_CASE__ ,num_inference_steps=50 ,output_type='''np''' ,width=5_12 ,height=5_12 ,sld_guidance_scale=20_00 ,sld_warmup_steps=7 ,sld_threshold=0.0_2_5 ,sld_momentum_scale=0.5 ,sld_mom_beta=0.7 ,) __SCREAMING_SNAKE_CASE :Union[str, Any] = output.images __SCREAMING_SNAKE_CASE :Optional[Any] = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :List[str] = [0.2_3_8_3, 0.2_2_7_6, 0.2_3_6, 0.2_1_9_2, 0.2_1_8_6, 0.2_0_5_3, 0.1_9_7_1, 0.1_9_0_1, 0.1_7_1_9] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _UpperCamelCase ( self ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE :int = StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' ,safety_checker=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Dict = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) __SCREAMING_SNAKE_CASE :Dict = sd_pipe.to(SCREAMING_SNAKE_CASE__ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :List[Any] = '''padme amidala taking a bath artwork, safe for work, no nudity''' __SCREAMING_SNAKE_CASE :List[str] = 27_34_97_17_55 __SCREAMING_SNAKE_CASE :Union[str, Any] = 7 __SCREAMING_SNAKE_CASE :int = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :str = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=SCREAMING_SNAKE_CASE__ ,num_inference_steps=50 ,output_type='''np''' ,width=5_12 ,height=5_12 ,sld_guidance_scale=0 ,) __SCREAMING_SNAKE_CASE :Dict = output.images __SCREAMING_SNAKE_CASE :str = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :Dict = [0.3_5_0_2, 0.3_6_2_2, 0.3_3_9_6, 0.3_6_4_2, 0.3_4_7_8, 0.3_3_1_8, 0.3_5, 0.3_3_4_8, 0.3_2_9_7] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 __SCREAMING_SNAKE_CASE :Dict = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Dict = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=SCREAMING_SNAKE_CASE__ ,num_inference_steps=50 ,output_type='''np''' ,width=5_12 ,height=5_12 ,sld_guidance_scale=20_00 ,sld_warmup_steps=7 ,sld_threshold=0.0_2_5 ,sld_momentum_scale=0.5 ,sld_mom_beta=0.7 ,) __SCREAMING_SNAKE_CASE :Any = output.images __SCREAMING_SNAKE_CASE :Union[str, Any] = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :Tuple = [0.5_5_3_1, 0.5_2_0_6, 0.4_8_9_5, 0.5_1_5_6, 0.5_1_8_2, 0.4_7_5_1, 0.4_8_0_2, 0.4_8_0_3, 0.4_4_4_3] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _UpperCamelCase ( self ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE :int = StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' ) __SCREAMING_SNAKE_CASE :Tuple = sd_pipe.to(SCREAMING_SNAKE_CASE__ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Optional[int] = ( '''the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.''' ''' leyendecker''' ) __SCREAMING_SNAKE_CASE :Dict = 10_44_35_52_34 __SCREAMING_SNAKE_CASE :List[Any] = 12 __SCREAMING_SNAKE_CASE :Optional[int] = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Dict = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=SCREAMING_SNAKE_CASE__ ,num_inference_steps=50 ,output_type='''np''' ,width=5_12 ,height=5_12 ,sld_guidance_scale=0 ,) __SCREAMING_SNAKE_CASE :Optional[int] = output.images __SCREAMING_SNAKE_CASE :str = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :List[str] = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ) assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-7 __SCREAMING_SNAKE_CASE :Tuple = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :int = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE__ ,guidance_scale=SCREAMING_SNAKE_CASE__ ,num_inference_steps=50 ,output_type='''np''' ,width=5_12 ,height=5_12 ,sld_guidance_scale=20_00 ,sld_warmup_steps=7 ,sld_threshold=0.0_2_5 ,sld_momentum_scale=0.5 ,sld_mom_beta=0.7 ,) __SCREAMING_SNAKE_CASE :Union[str, Any] = output.images __SCREAMING_SNAKE_CASE :Dict = image[0, -3:, -3:, -1] __SCREAMING_SNAKE_CASE :Optional[Any] = np.array([0.5_8_1_8, 0.6_2_8_5, 0.6_8_3_5, 0.6_0_1_9, 0.6_2_5, 0.6_7_5_4, 0.6_0_9_6, 0.6_3_3_4, 0.6_5_6_1] ) assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
191
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { "google/vivit-b-16x2-kinetics400": ( "https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json" ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class _SCREAMING_SNAKE_CASE( A ): SCREAMING_SNAKE_CASE_ : List[str] = '''vivit''' def __init__( self ,SCREAMING_SNAKE_CASE__=2_24 ,SCREAMING_SNAKE_CASE__=32 ,SCREAMING_SNAKE_CASE__=[2, 16, 16] ,SCREAMING_SNAKE_CASE__=3 ,SCREAMING_SNAKE_CASE__=7_68 ,SCREAMING_SNAKE_CASE__=12 ,SCREAMING_SNAKE_CASE__=12 ,SCREAMING_SNAKE_CASE__=30_72 ,SCREAMING_SNAKE_CASE__="gelu_fast" ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=0.0_2 ,SCREAMING_SNAKE_CASE__=1E-06 ,SCREAMING_SNAKE_CASE__=True ,**SCREAMING_SNAKE_CASE__ ,) -> Any: """simple docstring""" __SCREAMING_SNAKE_CASE :int = hidden_size __SCREAMING_SNAKE_CASE :List[Any] = num_hidden_layers __SCREAMING_SNAKE_CASE :Union[str, Any] = num_attention_heads __SCREAMING_SNAKE_CASE :Union[str, Any] = intermediate_size __SCREAMING_SNAKE_CASE :Any = hidden_act __SCREAMING_SNAKE_CASE :Optional[Any] = hidden_dropout_prob __SCREAMING_SNAKE_CASE :str = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE :Any = initializer_range __SCREAMING_SNAKE_CASE :Optional[int] = layer_norm_eps __SCREAMING_SNAKE_CASE :Optional[int] = image_size __SCREAMING_SNAKE_CASE :List[str] = num_frames __SCREAMING_SNAKE_CASE :Any = tubelet_size __SCREAMING_SNAKE_CASE :str = num_channels __SCREAMING_SNAKE_CASE :Any = qkv_bias super().__init__(**SCREAMING_SNAKE_CASE__ )
191
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor snake_case = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( lowerCAmelCase ): '''simple docstring''' def __init__( self : List[str] , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Tuple ): warnings.warn( "The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use FlavaImageProcessor instead." , UpperCAmelCase_ , ) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_ )
366
class SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : Union[str, Any] , UpperCAmelCase_ : list ): SCREAMING_SNAKE_CASE : Union[str, Any] = set_counts SCREAMING_SNAKE_CASE : Any = max(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Any = len(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[str] = [1] * num_sets SCREAMING_SNAKE_CASE : List[str] = list(range(UpperCAmelCase_ ) ) def _A ( self : Union[str, Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : int ): SCREAMING_SNAKE_CASE : List[Any] = self.get_parent(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : List[str] = self.get_parent(UpperCAmelCase_ ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] SCREAMING_SNAKE_CASE : Dict = 0 SCREAMING_SNAKE_CASE : Union[str, Any] = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 SCREAMING_SNAKE_CASE : List[str] = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] SCREAMING_SNAKE_CASE : Optional[int] = 0 SCREAMING_SNAKE_CASE : Tuple = src_parent SCREAMING_SNAKE_CASE : Optional[int] = self.set_counts[src_parent] SCREAMING_SNAKE_CASE : Optional[Any] = max(self.max_set , UpperCAmelCase_ ) return True def _A ( self : Tuple , UpperCAmelCase_ : int ): if self.parents[disj_set] == disj_set: return disj_set SCREAMING_SNAKE_CASE : Tuple = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
319
0
"""simple docstring""" def snake_case_ ( A_ : str ): '''simple docstring''' return [ txt[:a] + txt[a].upper() + txt[a + 1 :] for a in range(len(A_ ) ) if txt[a].isalpha() ] if __name__ == "__main__": __import__('''doctest''').testmod()
72
'''simple docstring''' import shutil import tempfile import unittest from transformers import ( SPIECE_UNDERLINE, AddedToken, BatchEncoding, NllbTokenizer, NllbTokenizerFast, is_torch_available, ) from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin __SCREAMING_SNAKE_CASE : Union[str, Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right __SCREAMING_SNAKE_CASE : Optional[int] = 256_047 __SCREAMING_SNAKE_CASE : Optional[int] = 256_145 @require_sentencepiece @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: int = NllbTokenizer __UpperCamelCase: Tuple = NllbTokenizerFast __UpperCamelCase: Union[str, Any] = True __UpperCamelCase: Dict = True __UpperCamelCase: Optional[Any] = {} def _A ( self : Union[str, Any] ): super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) tokenizer.save_pretrained(self.tmpdirname ) def _A ( self : Dict ): _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) _UpperCAmelCase : Optional[Any] = tokenizer.tokenize("This is a test" ) self.assertListEqual(A , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(A ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _UpperCAmelCase : List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( A , [ 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", "é", ".", ] , ) _UpperCAmelCase : Optional[Any] = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual( A , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) _UpperCAmelCase : Union[str, Any] = tokenizer.convert_ids_to_tokens(A ) self.assertListEqual( A , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) def _A ( self : List[Any] ): _UpperCAmelCase : Any = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-nllb", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : str = self.tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Dict = tokenizer_r.save_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) _UpperCAmelCase : Optional[int] = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : List[str] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=True _UpperCAmelCase : Optional[Any] = tempfile.mkdtemp() _UpperCAmelCase : str = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : str = tokenizer_p.save_pretrained(A ) # Checks it save with the same files self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : Optional[int] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=False _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Optional[int] = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Optional[int] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) @require_torch def _A ( self : Tuple ): if not self.test_seqaseq: return _UpperCAmelCase : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): # Longer text that will definitely require truncation. _UpperCAmelCase : Optional[Any] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for" " Syria is that 'there is no military solution' to the nearly five-year conflict and more weapons" " will only worsen the violence and misery for millions of people.", ] _UpperCAmelCase : Optional[Any] = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al" " Rusiei pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi" " că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] try: _UpperCAmelCase : Optional[int] = tokenizer.prepare_seqaseq_batch( src_texts=A , tgt_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="ron_Latn" , ) except NotImplementedError: return self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 10 ) # max_target_length will default to max_length if not specified _UpperCAmelCase : Tuple = tokenizer.prepare_seqaseq_batch( A , tgt_texts=A , max_length=3 , return_tensors="pt" ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 3 ) _UpperCAmelCase : Union[str, Any] = tokenizer.prepare_seqaseq_batch( src_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" ) self.assertEqual(batch_encoder_only.input_ids.shape[1] , 3 ) self.assertEqual(batch_encoder_only.attention_mask.shape[1] , 3 ) self.assertNotIn("decoder_input_ids" , A ) @unittest.skip("Unfortunately way too slow to build a BPE with SentencePiece." ) def _A ( self : List[Any] ): pass def _A ( self : Union[str, Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Any = [AddedToken("<special>" , lstrip=A )] _UpperCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Dict = tokenizer_r.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_r.encode("<special>" , add_special_tokens=A )[0] self.assertTrue(special_token_id in r_output ) if self.test_slow_tokenizer: _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A , ) _UpperCAmelCase : Optional[int] = self.tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Union[str, Any] = tokenizer_p.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_cr.encode("Hey this is a <special> token" ) self.assertEqual(A , A ) self.assertEqual(A , A ) self.assertTrue(special_token_id in p_output ) self.assertTrue(special_token_id in cr_output ) @require_torch @require_sentencepiece @require_tokenizers class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = "facebook/nllb-200-distilled-600M" __UpperCamelCase: Optional[int] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] __UpperCamelCase: str = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] __UpperCamelCase: str = [ 2_5_6_0_4_7, 1_6_2_9_7, 1_3_4_4_0_8, 8_1_6_5, 2_4_8_0_6_6, 1_4_7_3_4, 9_5_0, 1_1_3_5, 1_0_5_7_2_1, 3_5_7_3, 8_3, 2_7_3_5_2, 1_0_8, 4_9_4_8_6, 2, ] @classmethod def _A ( cls : int ): _UpperCAmelCase : NllbTokenizer = NllbTokenizer.from_pretrained( cls.checkpoint_name , src_lang="eng_Latn" , tgt_lang="ron_Latn" ) _UpperCAmelCase : Union[str, Any] = 1 return cls def _A ( self : Any ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Arab"] , 256001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Latn"] , 256002 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["fra_Latn"] , 256057 ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Optional[int] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , A ) def _A ( self : Tuple ): self.assertIn(A , self.tokenizer.all_special_ids ) # fmt: off _UpperCAmelCase : List[Any] = [RO_CODE, 4254, 98068, 112923, 39072, 3909, 713, 102767, 26, 17314, 35642, 14683, 33118, 2022, 66987, 2, 256047] # fmt: on _UpperCAmelCase : Tuple = self.tokenizer.decode(A , skip_special_tokens=A ) _UpperCAmelCase : Optional[Any] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=A ) self.assertEqual(A , A ) self.assertNotIn(self.tokenizer.eos_token , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : List[Any] = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , A ) _UpperCAmelCase : Dict = 10 _UpperCAmelCase : Tuple = self.tokenizer(A , max_length=A , truncation=A ).input_ids[0] self.assertEqual(ids[-1] , 2 ) self.assertEqual(ids[0] , A ) self.assertEqual(len(A ) , A ) def _A ( self : Dict ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [256203, 3] ) def _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = tempfile.mkdtemp() _UpperCAmelCase : str = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(A ) _UpperCAmelCase : Tuple = NllbTokenizer.from_pretrained(A ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , A ) @require_torch def _A ( self : Dict ): _UpperCAmelCase : List[str] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=A , truncation=A , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) _UpperCAmelCase : Tuple = shift_tokens_right( batch["labels"] , self.tokenizer.pad_token_id , self.tokenizer.lang_code_to_id["ron_Latn"] ) self.assertIsInstance(A , A ) self.assertEqual((2, 15) , batch.input_ids.shape ) self.assertEqual((2, 15) , batch.attention_mask.shape ) _UpperCAmelCase : Dict = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , A ) self.assertEqual(A , batch.decoder_input_ids[0, 0] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def _A ( self : str ): _UpperCAmelCase : Optional[Any] = self.tokenizer(self.src_text , padding=A , truncation=A , max_length=3 , return_tensors="pt" ) _UpperCAmelCase : Dict = self.tokenizer( text_target=self.tgt_text , padding=A , truncation=A , max_length=10 , return_tensors="pt" ) _UpperCAmelCase : List[Any] = targets["input_ids"] _UpperCAmelCase : Union[str, Any] = shift_tokens_right( A , self.tokenizer.pad_token_id , decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang] , ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _A ( self : List[Any] ): _UpperCAmelCase : str = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( nested_simplify(A ) , { # A, test, EOS, en_XX "input_ids": [[256047, 70, 7356, 2]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 256057, } , ) @require_torch def _A ( self : Any ): _UpperCAmelCase : Dict = True _UpperCAmelCase : Any = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2, 256047] ) _UpperCAmelCase : Optional[int] = False _UpperCAmelCase : str = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [256047, 16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2] )
31
0
'''simple docstring''' import inspect import os import torch from transformers import AutoModel from transformers.testing_utils import mockenv_context from transformers.trainer_utils import set_seed import accelerate from accelerate.accelerator import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils.testing import ( AccelerateTestCase, TempDirTestCase, execute_subprocess_async, require_cuda, require_fsdp, require_multi_gpu, slow, ) from accelerate.utils.constants import ( FSDP_AUTO_WRAP_POLICY, FSDP_BACKWARD_PREFETCH, FSDP_SHARDING_STRATEGY, FSDP_STATE_DICT_TYPE, ) from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin from accelerate.utils.other import patch_environment set_seed(42) _lowercase = """bert-base-cased""" _lowercase = """fp16""" _lowercase = """bf16""" _lowercase = [FPaa, BFaa] @require_fsdp @require_cuda class UpperCAmelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def _lowercase ( self ): """simple docstring""" super().setUp() _lowerCAmelCase = dict( ACCELERATE_USE_FSDP="""true""" , MASTER_ADDR="""localhost""" , MASTER_PORT="""10999""" , RANK="""0""" , LOCAL_RANK="""0""" , WORLD_SIZE="""1""" , ) def _lowercase ( self ): """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy for i, strategy in enumerate(_lowercase ): _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = F'{i + 1}' _lowerCAmelCase = strategy with mockenv_context(**_lowercase ): _lowerCAmelCase = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.sharding_strategy , ShardingStrategy(i + 1 ) ) def _lowercase ( self ): """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch for i, prefetch_policy in enumerate(_lowercase ): _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = prefetch_policy with mockenv_context(**_lowercase ): _lowerCAmelCase = FullyShardedDataParallelPlugin() if prefetch_policy == "NO_PREFETCH": self.assertIsNone(fsdp_plugin.backward_prefetch ) else: self.assertEqual(fsdp_plugin.backward_prefetch , BackwardPrefetch(i + 1 ) ) def _lowercase ( self ): """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType for i, state_dict_type in enumerate(_lowercase ): _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = state_dict_type with mockenv_context(**_lowercase ): _lowerCAmelCase = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.state_dict_type , StateDictType(i + 1 ) ) if state_dict_type == "FULL_STATE_DICT": self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu ) self.assertTrue(fsdp_plugin.state_dict_config.ranka_only ) def _lowercase ( self ): """simple docstring""" _lowerCAmelCase = AutoModel.from_pretrained(_lowercase ) for policy in FSDP_AUTO_WRAP_POLICY: _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = policy if policy == "TRANSFORMER_BASED_WRAP": _lowerCAmelCase = """BertLayer""" elif policy == "SIZE_BASED_WRAP": _lowerCAmelCase = """2000""" with mockenv_context(**_lowercase ): _lowerCAmelCase = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(_lowercase ) if policy == "NO_WRAP": self.assertIsNone(fsdp_plugin.auto_wrap_policy ) else: self.assertIsNotNone(fsdp_plugin.auto_wrap_policy ) _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = """TRANSFORMER_BASED_WRAP""" _lowerCAmelCase = """T5Layer""" with mockenv_context(**_lowercase ): _lowerCAmelCase = FullyShardedDataParallelPlugin() with self.assertRaises(_lowercase ) as cm: fsdp_plugin.set_auto_wrap_policy(_lowercase ) self.assertTrue("""Could not find the transformer layer class to wrap in the model.""" in str(cm.exception ) ) _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = """SIZE_BASED_WRAP""" _lowerCAmelCase = """0""" with mockenv_context(**_lowercase ): _lowerCAmelCase = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(_lowercase ) self.assertIsNone(fsdp_plugin.auto_wrap_policy ) def _lowercase ( self ): """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler for mp_dtype in dtypes: _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = mp_dtype with mockenv_context(**_lowercase ): _lowerCAmelCase = Accelerator() if mp_dtype == "fp16": _lowerCAmelCase = torch.floataa elif mp_dtype == "bf16": _lowerCAmelCase = torch.bfloataa _lowerCAmelCase = MixedPrecision(param_dtype=_lowercase , reduce_dtype=_lowercase , buffer_dtype=_lowercase ) self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy , _lowercase ) if mp_dtype == FPaa: self.assertTrue(isinstance(accelerator.scaler , _lowercase ) ) elif mp_dtype == BFaa: self.assertIsNone(accelerator.scaler ) AcceleratorState._reset_state(_lowercase ) def _lowercase ( self ): """simple docstring""" from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload for flag in [True, False]: _lowerCAmelCase = self.dist_env.copy() _lowerCAmelCase = str(_lowercase ).lower() with mockenv_context(**_lowercase ): _lowerCAmelCase = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.cpu_offload , CPUOffload(offload_params=_lowercase ) ) @require_fsdp @require_multi_gpu @slow class UpperCAmelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def _lowercase ( self ): """simple docstring""" super().setUp() _lowerCAmelCase = 0.82 _lowerCAmelCase = [ """fsdp_shard_grad_op_transformer_based_wrap""", """fsdp_full_shard_transformer_based_wrap""", ] _lowerCAmelCase = { """multi_gpu_fp16""": 3_200, """fsdp_shard_grad_op_transformer_based_wrap_fp16""": 2_000, """fsdp_full_shard_transformer_based_wrap_fp16""": 1_900, # Disabling below test as it overwhelms the RAM memory usage # on CI self-hosted runner leading to tests getting killed. # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500, # fp16 was leading to indefinite hang } _lowerCAmelCase = 160 _lowerCAmelCase = 160 _lowerCAmelCase = inspect.getfile(accelerate.test_utils ) _lowerCAmelCase = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["""scripts""", """external_deps"""] ) def _lowercase ( self ): """simple docstring""" _lowerCAmelCase = os.path.join(self.test_scripts_folder , """test_performance.py""" ) _lowerCAmelCase = ["""accelerate""", """launch""", """--num_processes=2""", """--num_machines=1""", """--machine_rank=0""", """--use_fsdp"""] for config in self.performance_configs: _lowerCAmelCase = cmd.copy() for i, strategy in enumerate(_lowercase ): if strategy.lower() in config: cmd_config.append(F'--fsdp_sharding_strategy={i+1}' ) break if "fp32" in config: cmd_config.append("""--mixed_precision=no""" ) else: cmd_config.append("""--mixed_precision=fp16""" ) if "cpu_offload" in config: cmd_config.append("""--fsdp_offload_params=True""" ) for policy in FSDP_AUTO_WRAP_POLICY: if policy.lower() in config: cmd_config.append(F'--fsdp_auto_wrap_policy={policy}' ) break if policy == "TRANSFORMER_BASED_WRAP": cmd_config.append("""--fsdp_transformer_layer_cls_to_wrap=BertLayer""" ) elif policy == "SIZE_BASED_WRAP": cmd_config.append("""--fsdp_min_num_params=2000""" ) cmd_config.extend( [ self.test_file_path, F'--output_dir={self.tmpdir}', F'--performance_lower_bound={self.performance_lower_bound}', ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_lowercase , env=os.environ.copy() ) def _lowercase ( self ): """simple docstring""" _lowerCAmelCase = os.path.join(self.test_scripts_folder , """test_checkpointing.py""" ) _lowerCAmelCase = [ """accelerate""", """launch""", """--num_processes=2""", """--num_machines=1""", """--machine_rank=0""", """--use_fsdp""", """--mixed_precision=fp16""", """--fsdp_transformer_layer_cls_to_wrap=BertLayer""", ] for i, strategy in enumerate(_lowercase ): _lowerCAmelCase = cmd.copy() cmd_config.append(F'--fsdp_sharding_strategy={i+1}' ) if strategy != "FULL_SHARD": continue _lowerCAmelCase = len(_lowercase ) for state_dict_type in FSDP_STATE_DICT_TYPE: _lowerCAmelCase = cmd_config[:state_dict_config_index] cmd_config.append(F'--fsdp_state_dict_type={state_dict_type}' ) cmd_config.extend( [ self.test_file_path, F'--output_dir={self.tmpdir}', """--partial_train_epoch=1""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_lowercase , env=os.environ.copy() ) _lowerCAmelCase = cmd_config[:-1] _lowerCAmelCase = os.path.join(self.tmpdir , """epoch_0""" ) cmd_config.extend( [ F'--resume_from_checkpoint={resume_from_checkpoint}', ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_lowercase , env=os.environ.copy() ) def _lowercase ( self ): """simple docstring""" _lowerCAmelCase = os.path.join(self.test_scripts_folder , """test_peak_memory_usage.py""" ) _lowerCAmelCase = [ """accelerate""", """launch""", """--num_processes=2""", """--num_machines=1""", """--machine_rank=0""", ] for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items(): _lowerCAmelCase = cmd.copy() if "fp16" in spec: cmd_config.extend(["""--mixed_precision=fp16"""] ) else: cmd_config.extend(["""--mixed_precision=no"""] ) if "multi_gpu" in spec: continue else: cmd_config.extend(["""--use_fsdp"""] ) for i, strategy in enumerate(_lowercase ): if strategy.lower() in spec: cmd_config.append(F'--fsdp_sharding_strategy={i+1}' ) break if "cpu_offload" in spec: cmd_config.append("""--fsdp_offload_params=True""" ) for policy in FSDP_AUTO_WRAP_POLICY: if policy.lower() in spec: cmd_config.append(F'--fsdp_auto_wrap_policy={policy}' ) break if policy == "TRANSFORMER_BASED_WRAP": cmd_config.append("""--fsdp_transformer_layer_cls_to_wrap=BertLayer""" ) elif policy == "SIZE_BASED_WRAP": cmd_config.append("""--fsdp_min_num_params=2000""" ) cmd_config.extend( [ self.test_file_path, F'--output_dir={self.tmpdir}', F'--peak_memory_upper_bound={peak_mem_upper_bound}', F'--n_train={self.n_train}', F'--n_val={self.n_val}', ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_lowercase , env=os.environ.copy() )
369
'''simple docstring''' import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets _lowercase = datasets.logging.get_logger(__name__) _lowercase = """\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = \"Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric\", author = \"Moosavi, Nafise Sadat and Strube, Michael\", booktitle = \"Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)\", month = aug, year = \"2016\", address = \"Berlin, Germany\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/P16-1060\", doi = \"10.18653/v1/P16-1060\", pages = \"632--642\", } """ _lowercase = """\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the \"([pos] [word])\" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a \"-\" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section \"*_conll File Format\"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. """ _lowercase = """ Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting 'keep_singletons=False', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the 'NP_only' option, the scorer will only evaluate the resolution of NPs. min_span: By setting 'min_span', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: 'mentions': mentions 'muc': MUC metric [Vilain et al, 1995] 'bcub': B-cubed [Bagga and Baldwin, 1998] 'ceafe': CEAFe [Luo et al., 2005] 'lea': LEA [Moosavi and Strube, 2016] 'conll_score': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric('coval') >>> words = ['bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -', ... 'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)', ... 'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)', ... 'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -', ... 'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -', ... 'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {'mentions/recall': 1.0,[...] 'conll_score': 100.0} """ def A (__lowerCamelCase :str , __lowerCamelCase :Optional[Any] , __lowerCamelCase :Union[str, Any]=False , __lowerCamelCase :List[Any]=False , __lowerCamelCase :str=True , __lowerCamelCase :str=False , __lowerCamelCase :str="dummy_doc" ): _lowerCAmelCase = {doc: key_lines} _lowerCAmelCase = {doc: sys_lines} _lowerCAmelCase = {} _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase , _lowerCAmelCase = reader.get_doc_mentions(__lowerCamelCase , key_doc_lines[doc] , __lowerCamelCase ) key_singletons_num += singletons_num if NP_only or min_span: _lowerCAmelCase = reader.set_annotated_parse_trees(__lowerCamelCase , key_doc_lines[doc] , __lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase , _lowerCAmelCase = reader.get_doc_mentions(__lowerCamelCase , sys_doc_lines[doc] , __lowerCamelCase ) sys_singletons_num += singletons_num if NP_only or min_span: _lowerCAmelCase = reader.set_annotated_parse_trees(__lowerCamelCase , key_doc_lines[doc] , __lowerCamelCase , __lowerCamelCase ) if remove_nested: _lowerCAmelCase , _lowerCAmelCase = reader.remove_nested_coref_mentions(__lowerCamelCase , __lowerCamelCase ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters _lowerCAmelCase , _lowerCAmelCase = reader.remove_nested_coref_mentions(__lowerCamelCase , __lowerCamelCase ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters _lowerCAmelCase = reader.get_mention_assignments(__lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = reader.get_mention_assignments(__lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( """Number of removed nested coreferring mentions in the key """ f'annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}' ) logger.info( """Number of resulting singleton clusters in the key """ f'annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}' ) if not keep_singletons: logger.info( f'{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system ' """files, respectively""" ) return doc_coref_infos def A (__lowerCamelCase :List[str] , __lowerCamelCase :str , __lowerCamelCase :str , __lowerCamelCase :int , __lowerCamelCase :int , __lowerCamelCase :Optional[Any] , __lowerCamelCase :Optional[Any] ): _lowerCAmelCase = get_coref_infos(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = {} _lowerCAmelCase = 0 _lowerCAmelCase = 0 for name, metric in metrics: _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = evaluator.evaluate_documents(__lowerCamelCase , __lowerCamelCase , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({f'{name}/recall': recall, f'{name}/precision': precision, f'{name}/f1': fa} ) logger.info( name.ljust(10 ) , f'Recall: {recall * 100:.2f}' , f' Precision: {precision * 100:.2f}' , f' F1: {fa * 100:.2f}' , ) if conll_subparts_num == 3: _lowerCAmelCase = (conll / 3) * 100 logger.info(f'CoNLL score: {conll:.2f}' ) output_scores.update({"""conll_score""": conll} ) return output_scores def A (__lowerCamelCase :List[str] ): _lowerCAmelCase = False for line in key_lines: if not line.startswith("""#""" ): if len(line.split() ) > 6: _lowerCAmelCase = line.split()[5] if not parse_col == "-": _lowerCAmelCase = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): '''simple docstring''' def _lowercase ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""string""" ) ), """references""": datasets.Sequence(datasets.Value("""string""" ) ), } ) , codebase_urls=["""https://github.com/ns-moosavi/coval"""] , reference_urls=[ """https://github.com/ns-moosavi/coval""", """https://www.aclweb.org/anthology/P16-1060""", """http://www.conll.cemantix.org/2012/data.html""", ] , ) def _lowercase ( self , _lowercase , _lowercase , _lowercase=True , _lowercase=False , _lowercase=False , _lowercase=False ): """simple docstring""" _lowerCAmelCase = [ ("""mentions""", evaluator.mentions), ("""muc""", evaluator.muc), ("""bcub""", evaluator.b_cubed), ("""ceafe""", evaluator.ceafe), ("""lea""", evaluator.lea), ] if min_span: _lowerCAmelCase = util.check_gold_parse_annotation(_lowercase ) if not has_gold_parse: raise NotImplementedError("""References should have gold parse annotation to use 'min_span'.""" ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" _lowerCAmelCase = evaluate( key_lines=_lowercase , sys_lines=_lowercase , metrics=_lowercase , NP_only=_lowercase , remove_nested=_lowercase , keep_singletons=_lowercase , min_span=_lowercase , ) return score
229
0
"""simple docstring""" import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import TimesformerConfig 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, TimesformerForVideoClassification, TimesformerModel, ) from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class lowerCAmelCase__ : '''simple docstring''' def __init__( self , lowercase , lowercase=13 , lowercase=10 , lowercase=3 , lowercase=2 , lowercase=2 , lowercase=True , lowercase=True , lowercase=32 , lowercase=5 , lowercase=4 , lowercase=37 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=10 , lowercase=0.02 , lowercase="divided_space_time" , lowercase=None , ): _lowerCamelCase : Tuple = parent _lowerCamelCase : Any = batch_size _lowerCamelCase : Tuple = image_size _lowerCamelCase : str = num_channels _lowerCamelCase : Optional[Any] = patch_size _lowerCamelCase : Any = num_frames _lowerCamelCase : List[Any] = is_training _lowerCamelCase : List[Any] = use_labels _lowerCamelCase : List[Any] = hidden_size _lowerCamelCase : Optional[Any] = num_hidden_layers _lowerCamelCase : List[Any] = num_attention_heads _lowerCamelCase : List[str] = intermediate_size _lowerCamelCase : Any = hidden_act _lowerCamelCase : int = hidden_dropout_prob _lowerCamelCase : Any = attention_probs_dropout_prob _lowerCamelCase : List[str] = attention_type _lowerCamelCase : int = initializer_range _lowerCamelCase : Any = scope _lowerCamelCase : int = num_labels # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token _lowerCamelCase : Optional[Any] = (image_size // patch_size) ** 2 _lowerCamelCase : Dict = (num_frames) * self.num_patches_per_frame + 1 def A_ ( self ): _lowerCamelCase : str = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) _lowerCamelCase : Union[str, Any] = None if self.use_labels: _lowerCamelCase : Dict = ids_tensor([self.batch_size] , self.num_labels ) _lowerCamelCase : int = self.get_config() return config, pixel_values, labels def A_ ( self ): _lowerCamelCase : str = TimesformerConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , attention_type=self.attention_type , ) _lowerCamelCase : List[str] = self.num_labels return config def A_ ( self , lowercase , lowercase , lowercase ): _lowerCamelCase : Union[str, Any] = TimesformerModel(config=lowercase ) model.to(lowercase ) model.eval() _lowerCamelCase : Any = model(lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A_ ( self , lowercase , lowercase , lowercase ): _lowerCamelCase : Optional[Any] = TimesformerForVideoClassification(lowercase ) model.to(lowercase ) model.eval() _lowerCamelCase : List[Any] = model(lowercase ) # verify the logits shape _lowerCamelCase : List[Any] = torch.Size((self.batch_size, self.num_labels) ) self.parent.assertEqual(result.logits.shape , lowercase ) def A_ ( self ): _lowerCamelCase : Optional[int] = self.prepare_config_and_inputs() _lowerCamelCase, _lowerCamelCase, _lowerCamelCase : List[str] = config_and_inputs _lowerCamelCase : Dict = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class lowerCAmelCase__ ( lowercase, lowercase, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else () lowerCamelCase__ = ( {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification} if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def A_ ( self ): _lowerCamelCase : Union[str, Any] = TimesformerModelTester(self ) _lowerCamelCase : List[Any] = ConfigTester( self , config_class=lowercase , has_text_modality=lowercase , hidden_size=37 ) def A_ ( self , lowercase , lowercase , lowercase=False ): _lowerCamelCase : Tuple = copy.deepcopy(lowercase ) if return_labels: if model_class in get_values(lowercase ): _lowerCamelCase : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowercase ) return inputs_dict def A_ ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='TimeSformer does not use inputs_embeds' ) def A_ ( self ): pass def A_ ( self ): _lowerCamelCase, _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCamelCase : Union[str, Any] = model_class(lowercase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _lowerCamelCase : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowercase , nn.Linear ) ) def A_ ( self ): _lowerCamelCase, _lowerCamelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCamelCase : Optional[int] = model_class(lowercase ) _lowerCamelCase : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCamelCase : Dict = [*signature.parameters.keys()] _lowerCamelCase : Optional[int] = ['pixel_values'] self.assertListEqual(arg_names[:1] , lowercase ) def A_ ( self ): _lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase ) def A_ ( self ): _lowerCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_video_classification(*lowercase ) @slow def A_ ( self ): for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCamelCase : str = TimesformerModel.from_pretrained(lowercase ) self.assertIsNotNone(lowercase ) def A_ ( self ): if not self.has_attentions: pass else: _lowerCamelCase, _lowerCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() _lowerCamelCase : Union[str, Any] = True for model_class in self.all_model_classes: _lowerCamelCase : List[Any] = self.model_tester.seq_length _lowerCamelCase : List[str] = self.model_tester.num_frames _lowerCamelCase : Dict = True _lowerCamelCase : List[Any] = False _lowerCamelCase : Optional[Any] = True _lowerCamelCase : Optional[int] = model_class(lowercase ) model.to(lowercase ) model.eval() with torch.no_grad(): _lowerCamelCase : List[Any] = model(**self._prepare_for_class(lowercase , lowercase ) ) _lowerCamelCase : Optional[int] = outputs.attentions self.assertEqual(len(lowercase ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _lowerCamelCase : int = True _lowerCamelCase : Optional[int] = model_class(lowercase ) model.to(lowercase ) model.eval() with torch.no_grad(): _lowerCamelCase : List[Any] = model(**self._prepare_for_class(lowercase , lowercase ) ) _lowerCamelCase : List[Any] = outputs.attentions self.assertEqual(len(lowercase ) , self.model_tester.num_hidden_layers ) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) _lowerCamelCase : Any = len(lowercase ) # Check attention is always last and order is fine _lowerCamelCase : Optional[Any] = True _lowerCamelCase : Union[str, Any] = True _lowerCamelCase : Union[str, Any] = model_class(lowercase ) model.to(lowercase ) model.eval() with torch.no_grad(): _lowerCamelCase : str = model(**self._prepare_for_class(lowercase , lowercase ) ) self.assertEqual(out_len + 1 , len(lowercase ) ) _lowerCamelCase : Optional[Any] = outputs.attentions self.assertEqual(len(lowercase ) , self.model_tester.num_hidden_layers ) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) def A_ ( self ): def check_hidden_states_output(lowercase , lowercase , lowercase ): _lowerCamelCase : Any = model_class(lowercase ) model.to(lowercase ) model.eval() with torch.no_grad(): _lowerCamelCase : List[Any] = model(**self._prepare_for_class(lowercase , lowercase ) ) _lowerCamelCase : int = outputs.hidden_states _lowerCamelCase : str = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(lowercase ) , lowercase ) _lowerCamelCase : Optional[Any] = self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) _lowerCamelCase, _lowerCamelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCamelCase : Tuple = True check_hidden_states_output(lowercase , lowercase , lowercase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _lowerCamelCase : Dict = True check_hidden_states_output(lowercase , lowercase , lowercase ) def _snake_case ( ): _lowerCamelCase : str = hf_hub_download( repo_id='hf-internal-testing/spaghetti-video' , filename='eating_spaghetti.npy' , repo_type='dataset' ) _lowerCamelCase : Optional[Any] = np.load(lowercase__ ) return list(lowercase__ ) @require_torch @require_vision class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' @cached_property def A_ ( self ): # 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 A_ ( self ): _lowerCamelCase : List[Any] = TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400' ).to( lowercase ) _lowerCamelCase : Any = self.default_image_processor _lowerCamelCase : Union[str, Any] = prepare_video() _lowerCamelCase : Any = image_processor(video[:8] , return_tensors='pt' ).to(lowercase ) # forward pass with torch.no_grad(): _lowerCamelCase : Any = model(**lowercase ) # verify the logits _lowerCamelCase : Dict = torch.Size((1, 400) ) self.assertEqual(outputs.logits.shape , lowercase ) _lowerCamelCase : Optional[Any] = torch.tensor([-0.30_16, -0.77_13, -0.42_05] ).to(lowercase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowercase , atol=1E-4 ) )
96
'''simple docstring''' import unittest from transformers import load_tool from transformers.utils import is_torch_available if is_torch_available(): import torch from transformers.testing_utils import require_torch from .test_tools_common import ToolTesterMixin @require_torch class __magic_name__ ( unittest.TestCase, _UpperCAmelCase): def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] ): lowercase_ : List[str] = load_tool("""text-to-speech""" ) self.tool.setup() def SCREAMING_SNAKE_CASE_ ( self : List[Any] ): # SpeechT5 isn't deterministic torch.manual_seed(0 ) lowercase_ : List[str] = self.tool("""hey""" ) lowercase_ : Optional[int] = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.0_00_59_66_66_88_32_11_58_29, -0.0_00_36_57_64_01_90_79_50_64, -0.00_01_34_39_50_27_99_88_34_85] ) , ) ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] ): # SpeechT5 isn't deterministic torch.manual_seed(0 ) lowercase_ : Union[str, Any] = self.tool("""hey""" ) lowercase_ : int = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.0_00_59_66_66_88_32_11_58_29, -0.0_00_36_57_64_01_90_79_50_64, -0.00_01_34_39_50_27_99_88_34_85] ) , ) )
239
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) _UpperCAmelCase : List[str] = { """configuration_convbert""": ["""CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ConvBertConfig""", """ConvBertOnnxConfig"""], """tokenization_convbert""": ["""ConvBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase : Optional[Any] = ["""ConvBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase : List[str] = [ """CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """ConvBertForMaskedLM""", """ConvBertForMultipleChoice""", """ConvBertForQuestionAnswering""", """ConvBertForSequenceClassification""", """ConvBertForTokenClassification""", """ConvBertLayer""", """ConvBertModel""", """ConvBertPreTrainedModel""", """load_tf_weights_in_convbert""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase : Tuple = [ """TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFConvBertForMaskedLM""", """TFConvBertForMultipleChoice""", """TFConvBertForQuestionAnswering""", """TFConvBertForSequenceClassification""", """TFConvBertForTokenClassification""", """TFConvBertLayer""", """TFConvBertModel""", """TFConvBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys _UpperCAmelCase : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
353
import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowercase ( lowercase_ , unittest.TestCase ): __SCREAMING_SNAKE_CASE : List[str] = BarthezTokenizer __SCREAMING_SNAKE_CASE : str = BarthezTokenizerFast __SCREAMING_SNAKE_CASE : Optional[Any] = True __SCREAMING_SNAKE_CASE : str = True def a ( self ): super().setUp() snake_case_ = BarthezTokenizerFast.from_pretrained('moussaKam/mbarthez' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=snake_case ) snake_case_ = tokenizer def a ( self ): snake_case_ = '<pad>' snake_case_ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case ) , snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case ) , snake_case ) def a ( self ): snake_case_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , '<mask>' ) self.assertEqual(len(snake_case ) , 10_1122 ) def a ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 10_1122 ) @require_torch def a ( self ): snake_case_ = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] snake_case_ = [0, 57, 3018, 7_0307, 91, 2] snake_case_ = self.tokenizer( snake_case , max_length=len(snake_case ) , padding=snake_case , truncation=snake_case , return_tensors='pt' ) self.assertIsInstance(snake_case , snake_case ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) snake_case_ = batch.input_ids.tolist()[0] self.assertListEqual(snake_case , snake_case ) def a ( self ): if not self.test_rust_tokenizer: return snake_case_ = self.get_tokenizer() snake_case_ = self.get_rust_tokenizer() snake_case_ = 'I was born in 92000, and this is falsé.' snake_case_ = tokenizer.tokenize(snake_case ) snake_case_ = rust_tokenizer.tokenize(snake_case ) self.assertListEqual(snake_case , snake_case ) snake_case_ = tokenizer.encode(snake_case , add_special_tokens=snake_case ) snake_case_ = rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) self.assertListEqual(snake_case , snake_case ) snake_case_ = self.get_rust_tokenizer() snake_case_ = tokenizer.encode(snake_case ) snake_case_ = rust_tokenizer.encode(snake_case ) self.assertListEqual(snake_case , snake_case ) @slow def a ( self ): # fmt: off snake_case_ = {'input_ids': [[0, 490, 1_4328, 4507, 354, 47, 4_3669, 95, 25, 7_8117, 2_0215, 1_9779, 190, 22, 400, 4, 3_5343, 8_0310, 603, 86, 2_4937, 105, 3_3438, 9_4762, 196, 3_9642, 7, 15, 1_5933, 173, 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], [0, 1_0534, 87, 25, 66, 3358, 196, 5_5289, 8, 8_2961, 81, 2204, 7_5203, 7, 15, 763, 1_2956, 216, 178, 1_4328, 9595, 1377, 6_9693, 7, 448, 7_1021, 196, 1_8106, 1437, 1_3974, 108, 9083, 4, 4_9315, 7, 39, 86, 1326, 2793, 4_6333, 4, 448, 196, 7_4588, 7, 4_9315, 7, 39, 21, 822, 3_8470, 74, 21, 6_6723, 6_2480, 8, 2_2050, 5, 2]], '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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. snake_case_ = [ 'Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ' 'utilisé principalement dans le domaine du traitement automatique des langues (TAL).', 'À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ' 'pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ' 'telles que la traduction et la synthèse de texte.', ] self.tokenizer_integration_test_util( expected_encoding=snake_case , model_name='moussaKam/mbarthez' , revision='c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6' , sequences=snake_case , )
200
0
from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __lowerCAmelCase : List[Any] =logging.get_logger(__name__) class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = ['''input_features''', '''attention_mask'''] def __init__( self :int , lowerCAmelCase__ :List[Any]=80 , lowerCAmelCase__ :Union[str, Any]=16_000 , lowerCAmelCase__ :List[str]=80 , lowerCAmelCase__ :str=0.0 , lowerCAmelCase__ :Union[str, Any]=True , lowerCAmelCase__ :List[Any]=True , lowerCAmelCase__ :Union[str, Any]=True , **lowerCAmelCase__ :Tuple , ) -> List[str]: super().__init__(feature_size=lowerCAmelCase__ , sampling_rate=lowerCAmelCase__ , padding_value=lowerCAmelCase__ , **lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = num_mel_bins __SCREAMING_SNAKE_CASE : List[str] = do_ceptral_normalize __SCREAMING_SNAKE_CASE : Any = normalize_means __SCREAMING_SNAKE_CASE : Dict = normalize_vars __SCREAMING_SNAKE_CASE : Any = True def __magic_name__( self :str , lowerCAmelCase__ :np.ndarray , ) -> np.ndarray: __SCREAMING_SNAKE_CASE : Any = waveform * (2**15) # Kaldi compliance: 16-bit signed integers __SCREAMING_SNAKE_CASE : str = torch.from_numpy(lowerCAmelCase__ ).unsqueeze(0 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = ta_kaldi.fbank(lowerCAmelCase__ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def __magic_name__( lowerCAmelCase__ :np.ndarray , lowerCAmelCase__ :int , lowerCAmelCase__ :Optional[bool] = True , lowerCAmelCase__ :Optional[bool] = True , lowerCAmelCase__ :float = 0.0 , ) -> np.ndarray: # make sure we normalize float32 arrays if normalize_means: __SCREAMING_SNAKE_CASE : int = x[:input_length].mean(axis=0 ) __SCREAMING_SNAKE_CASE : List[str] = np.subtract(lowerCAmelCase__ , lowerCAmelCase__ ) if normalize_vars: __SCREAMING_SNAKE_CASE : Optional[int] = x[:input_length].std(axis=0 ) __SCREAMING_SNAKE_CASE : Union[str, Any] = np.divide(lowerCAmelCase__ , lowerCAmelCase__ ) if input_length < x.shape[0]: __SCREAMING_SNAKE_CASE : Tuple = padding_value # make sure array is in float32 __SCREAMING_SNAKE_CASE : Union[str, Any] = x.astype(np.floataa ) return x def __magic_name__( self :str , lowerCAmelCase__ :List[np.ndarray] , lowerCAmelCase__ :Optional[np.ndarray] = None ) -> List[np.ndarray]: __SCREAMING_SNAKE_CASE : Optional[Any] = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(lowerCAmelCase__ , lowerCAmelCase__ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] def __call__( self :int , lowerCAmelCase__ :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , lowerCAmelCase__ :Union[bool, str, PaddingStrategy] = False , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :bool = False , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :Optional[Union[str, TensorType]] = None , lowerCAmelCase__ :Optional[int] = None , lowerCAmelCase__ :Optional[bool] = None , **lowerCAmelCase__ :int , ) -> 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 `raw_speech` input was sampled with''' f''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) __SCREAMING_SNAKE_CASE : Tuple = isinstance(lowerCAmelCase__ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'''Only mono-channel audio is supported for input to {self}''' ) __SCREAMING_SNAKE_CASE : List[str] = is_batched_numpy or ( isinstance(lowerCAmelCase__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: __SCREAMING_SNAKE_CASE : List[Any] = [np.asarray(lowerCAmelCase__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(lowerCAmelCase__ , np.ndarray ): __SCREAMING_SNAKE_CASE : Optional[Any] = np.asarray(lowerCAmelCase__ , dtype=np.floataa ) elif isinstance(lowerCAmelCase__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): __SCREAMING_SNAKE_CASE : List[str] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: __SCREAMING_SNAKE_CASE : Optional[Any] = [raw_speech] # extract fbank features __SCREAMING_SNAKE_CASE : Dict = [self._extract_fbank_features(lowerCAmelCase__ ) for waveform in raw_speech] # convert into correct format for padding __SCREAMING_SNAKE_CASE : Tuple = BatchFeature({'''input_features''': features} ) __SCREAMING_SNAKE_CASE : Tuple = self.pad( lowerCAmelCase__ , padding=lowerCAmelCase__ , max_length=lowerCAmelCase__ , truncation=lowerCAmelCase__ , pad_to_multiple_of=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , **lowerCAmelCase__ , ) # make sure list is in array format __SCREAMING_SNAKE_CASE : str = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : int = [np.asarray(lowerCAmelCase__ , dtype=np.floataa ) for feature in input_features] __SCREAMING_SNAKE_CASE : Dict = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: __SCREAMING_SNAKE_CASE : Tuple = [np.asarray(lowerCAmelCase__ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: __SCREAMING_SNAKE_CASE : Tuple = ( np.array(lowerCAmelCase__ , dtype=np.intaa ) if self._get_padding_strategies(lowerCAmelCase__ , max_length=lowerCAmelCase__ ) is not PaddingStrategy.DO_NOT_PAD else None ) __SCREAMING_SNAKE_CASE : List[str] = self.normalize( padded_inputs['''input_features'''] , attention_mask=lowerCAmelCase__ ) if return_tensors is not None: __SCREAMING_SNAKE_CASE : Dict = padded_inputs.convert_to_tensors(lowerCAmelCase__ ) return padded_inputs
9
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_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class _lowercase ( unittest.TestCase ): '''simple docstring''' def __init__( self :Any , lowerCAmelCase__ :Union[str, Any] , lowerCAmelCase__ :Tuple=7 , lowerCAmelCase__ :List[Any]=3 , lowerCAmelCase__ :Any=10 , lowerCAmelCase__ :Optional[int]=18 , lowerCAmelCase__ :Dict=30 , lowerCAmelCase__ :Tuple=400 , lowerCAmelCase__ :List[Any]=True , lowerCAmelCase__ :Tuple=None , lowerCAmelCase__ :str=True , lowerCAmelCase__ :List[str]=[0.5, 0.5, 0.5] , lowerCAmelCase__ :List[str]=[0.5, 0.5, 0.5] , lowerCAmelCase__ :Optional[Any]=None , ) -> Optional[Any]: __SCREAMING_SNAKE_CASE : Dict = size if size is not None else {'''shortest_edge''': 18} __SCREAMING_SNAKE_CASE : Optional[int] = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} __SCREAMING_SNAKE_CASE : Tuple = parent __SCREAMING_SNAKE_CASE : List[Any] = batch_size __SCREAMING_SNAKE_CASE : List[str] = num_channels __SCREAMING_SNAKE_CASE : Union[str, Any] = num_frames __SCREAMING_SNAKE_CASE : Tuple = image_size __SCREAMING_SNAKE_CASE : Optional[Any] = min_resolution __SCREAMING_SNAKE_CASE : Any = max_resolution __SCREAMING_SNAKE_CASE : List[Any] = do_resize __SCREAMING_SNAKE_CASE : Optional[Any] = size __SCREAMING_SNAKE_CASE : Optional[int] = do_normalize __SCREAMING_SNAKE_CASE : List[Any] = image_mean __SCREAMING_SNAKE_CASE : List[str] = image_std __SCREAMING_SNAKE_CASE : str = crop_size def __magic_name__( self :Tuple ) -> Any: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class _lowercase ( A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = VivitImageProcessor if is_vision_available() else None def __magic_name__( self :List[str] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : str = VivitImageProcessingTester(self ) @property def __magic_name__( self :int ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def __magic_name__( self :List[str] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_center_crop''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size''' ) ) def __magic_name__( self :Optional[Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) __SCREAMING_SNAKE_CASE : Tuple = 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 __magic_name__( self :List[Any] ) -> Union[str, Any]: # Initialize image_processing __SCREAMING_SNAKE_CASE : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos __SCREAMING_SNAKE_CASE : List[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE : List[str] = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Optional[Any] = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __magic_name__( self :str ) -> int: # Initialize image_processing __SCREAMING_SNAKE_CASE : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE : List[str] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE : Optional[int] = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Any = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __magic_name__( self :Any ) -> List[str]: # Initialize image_processing __SCREAMING_SNAKE_CASE : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for video in video_inputs: self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE : Tuple = image_processing(video_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched __SCREAMING_SNAKE_CASE : Optional[int] = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
9
1
"""simple docstring""" # tests directory-specific settings - this file is run automatically # by pytest before any tests are run import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. snake_case__ : List[str] = abspath(join(dirname(__file__), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def _snake_case ( _snake_case : List[Any] ): config.addinivalue_line( '''markers''' , '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested''' ) config.addinivalue_line( '''markers''' , '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested''' ) config.addinivalue_line('''markers''' , '''is_pipeline_test: mark test to run only when pipelines are tested''' ) config.addinivalue_line('''markers''' , '''is_staging_test: mark test to run only in the staging environment''' ) config.addinivalue_line('''markers''' , '''accelerate_tests: mark test that require accelerate''' ) config.addinivalue_line('''markers''' , '''tool_tests: mark the tool tests that are run on their specific schedule''' ) def _snake_case ( _snake_case : Union[str, Any] ): from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(_snake_case ) def _snake_case ( _snake_case : Optional[int] ): from transformers.testing_utils import pytest_terminal_summary_main lowerCAmelCase : Optional[int] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(_snake_case , id=_snake_case ) def _snake_case ( _snake_case : str , _snake_case : Any ): # If no tests are collected, pytest exists with code 5, which makes the CI fail. if exitstatus == 5: lowerCAmelCase : Tuple = 0 # Doctest custom flag to ignore output. snake_case__ : List[Any] = doctest.register_optionflag('''IGNORE_RESULT''') snake_case__ : Dict = doctest.OutputChecker class snake_case_( a__ ): def lowerCamelCase__ ( self : int , UpperCamelCase_ : Optional[Any] , UpperCamelCase_ : Optional[Any] , UpperCamelCase_ : Optional[Any] ): if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) snake_case__ : Any = CustomOutputChecker snake_case__ : Any = HfDoctestModule snake_case__ : List[Any] = HfDocTestParser
314
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case__ : Any = logging.get_logger(__name__) snake_case__ : Any = { '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class snake_case_( a__ ): __UpperCamelCase = '''vit_msn''' def __init__( self : Dict , UpperCamelCase_ : str=7_6_8 , UpperCamelCase_ : List[Any]=1_2 , UpperCamelCase_ : Optional[Any]=1_2 , UpperCamelCase_ : str=3_0_7_2 , UpperCamelCase_ : List[Any]="gelu" , UpperCamelCase_ : List[Any]=0.0 , UpperCamelCase_ : Any=0.0 , UpperCamelCase_ : List[str]=0.02 , UpperCamelCase_ : List[Any]=1E-06 , UpperCamelCase_ : Tuple=2_2_4 , UpperCamelCase_ : Union[str, Any]=1_6 , UpperCamelCase_ : List[Any]=3 , UpperCamelCase_ : Any=True , **UpperCamelCase_ : Union[str, Any] , ): super().__init__(**UpperCamelCase_ ) lowerCAmelCase : Any = hidden_size lowerCAmelCase : Tuple = num_hidden_layers lowerCAmelCase : List[Any] = num_attention_heads lowerCAmelCase : Any = intermediate_size lowerCAmelCase : Dict = hidden_act lowerCAmelCase : int = hidden_dropout_prob lowerCAmelCase : List[str] = attention_probs_dropout_prob lowerCAmelCase : Tuple = initializer_range lowerCAmelCase : Union[str, Any] = layer_norm_eps lowerCAmelCase : Tuple = image_size lowerCAmelCase : List[str] = patch_size lowerCAmelCase : int = num_channels lowerCAmelCase : Optional[int] = qkv_bias
314
1
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(lowerCamelCase_ , lowerCamelCase_ ) def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : List[str] = emb.weight.shape __SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Linear(lowerCamelCase_ , lowerCamelCase_ , bias=lowerCamelCase_ ) __SCREAMING_SNAKE_CASE : str = emb.weight.data return lin_layer def _UpperCamelCase ( lowercase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = torch.load(lowerCamelCase_ , map_location='''cpu''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = Namespace(**checkpoint['''cfg''']['''model'''] ) __SCREAMING_SNAKE_CASE : Union[str, Any] = checkpoint["""model"""] remove_ignore_keys_(lowerCamelCase_ ) __SCREAMING_SNAKE_CASE : Optional[int] = state_dict["""decoder.embed_tokens.weight"""].shape[0] __SCREAMING_SNAKE_CASE : Optional[Any] = {key.replace('''decoder''' , '''model''' ): val for key, val in state_dict.items()} __SCREAMING_SNAKE_CASE : List[str] = XGLMConfig( vocab_size=lowerCamelCase_ , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='''gelu''' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) __SCREAMING_SNAKE_CASE : Tuple = XGLMForCausalLM(lowerCamelCase_ ) __SCREAMING_SNAKE_CASE : List[Any] = model.load_state_dict(lowerCamelCase_ , strict=lowerCamelCase_ ) print(lowerCamelCase_ ) __SCREAMING_SNAKE_CASE : List[Any] = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": __lowerCAmelCase : List[str] =argparse.ArgumentParser() # Required parameters parser.add_argument('fairseq_path', type=str, help='path to a model.pt on local filesystem.') parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') __lowerCAmelCase : Optional[Any] =parser.parse_args() __lowerCAmelCase : List[Any] =convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
9
'''simple docstring''' from typing import Optional import numpy as np import torch from torch import nn from transformers import GPTaConfig, GPTaLMHeadModel from transformers.modeling_utils import ModuleUtilsMixin from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class UpperCamelCase__ ( lowercase_ , lowercase_ , lowercase_ ): """simple docstring""" SCREAMING_SNAKE_CASE__ = [R'''h\.\d+\.attn\.bias''', R'''h\.\d+\.attn\.masked_bias'''] @register_to_config def __init__( self : List[Any] , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : Optional[int] = None , lowerCamelCase_ : int = 5_02_57 , lowerCamelCase_ : int = 10_24 , lowerCamelCase_ : int = 7_68 , lowerCamelCase_ : int = 12 , lowerCamelCase_ : int = 12 , lowerCamelCase_ : Optional[int] = None , lowerCamelCase_ : str = "gelu_new" , lowerCamelCase_ : float = 0.1 , lowerCamelCase_ : float = 0.1 , lowerCamelCase_ : float = 0.1 , lowerCamelCase_ : float = 1e-5 , lowerCamelCase_ : float = 0.02 , lowerCamelCase_ : bool = True , lowerCamelCase_ : bool = True , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Optional[int] = prefix_length if prefix_inner_dim != n_embd and prefix_hidden_dim is None: raise ValueError( f'''`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and''' f''' `n_embd`: {n_embd} are not equal.''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = prefix_inner_dim SCREAMING_SNAKE_CASE : List[str] = prefix_hidden_dim SCREAMING_SNAKE_CASE : Tuple = ( nn.Linear(self.prefix_inner_dim , self.prefix_hidden_dim ) if self.prefix_hidden_dim is not None else nn.Identity() ) SCREAMING_SNAKE_CASE : str = ( nn.Linear(self.prefix_hidden_dim , lowerCamelCase_ ) if self.prefix_hidden_dim is not None else nn.Identity() ) SCREAMING_SNAKE_CASE : Any = GPTaConfig( vocab_size=lowerCamelCase_ , n_positions=lowerCamelCase_ , n_embd=lowerCamelCase_ , n_layer=lowerCamelCase_ , n_head=lowerCamelCase_ , n_inner=lowerCamelCase_ , activation_function=lowerCamelCase_ , resid_pdrop=lowerCamelCase_ , embd_pdrop=lowerCamelCase_ , attn_pdrop=lowerCamelCase_ , layer_norm_epsilon=lowerCamelCase_ , initializer_range=lowerCamelCase_ , scale_attn_weights=lowerCamelCase_ , use_cache=lowerCamelCase_ , scale_attn_by_inverse_layer_idx=lowerCamelCase_ , reorder_and_upcast_attn=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = GPTaLMHeadModel(lowerCamelCase_ ) def lowerCamelCase_ ( self : List[Any] , lowerCamelCase_ : torch.Tensor , lowerCamelCase_ : torch.Tensor , lowerCamelCase_ : Optional[torch.Tensor] = None , lowerCamelCase_ : Optional[torch.Tensor] = None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.transformer.transformer.wte(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.encode_prefix(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = self.decode_prefix(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = torch.cat((prefix_embeds, embedding_text) , dim=1 ) if labels is not None: SCREAMING_SNAKE_CASE : List[Any] = self.get_dummy_token(input_ids.shape[0] , input_ids.device ) SCREAMING_SNAKE_CASE : Dict = torch.cat((dummy_token, input_ids) , dim=1 ) SCREAMING_SNAKE_CASE : str = self.transformer(inputs_embeds=lowerCamelCase_ , labels=lowerCamelCase_ , attention_mask=lowerCamelCase_ ) if self.prefix_hidden_dim is not None: return out, hidden else: return out def lowerCamelCase_ ( self : Dict , lowerCamelCase_ : int , lowerCamelCase_ : torch.device ): '''simple docstring''' return torch.zeros(lowerCamelCase_ , self.prefix_length , dtype=torch.intaa , device=lowerCamelCase_ ) def lowerCamelCase_ ( self : str , lowerCamelCase_ : Optional[Any] ): '''simple docstring''' return self.encode_prefix(lowerCamelCase_ ) @torch.no_grad() def lowerCamelCase_ ( self : List[Any] , lowerCamelCase_ : int , lowerCamelCase_ : Optional[int] , lowerCamelCase_ : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = torch.split(lowerCamelCase_ , 1 , dim=0 ) SCREAMING_SNAKE_CASE : Dict = [] SCREAMING_SNAKE_CASE : Tuple = [] for feature in features: SCREAMING_SNAKE_CASE : Optional[int] = self.decode_prefix(feature.to(lowerCamelCase_ ) ) # back to the clip feature # Only support beam search for now SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = self.generate_beam( input_embeds=lowerCamelCase_ , device=lowerCamelCase_ , eos_token_id=lowerCamelCase_ ) generated_tokens.append(output_tokens[0] ) generated_seq_lengths.append(seq_lengths[0] ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.stack(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = torch.stack(lowerCamelCase_ ) return generated_tokens, generated_seq_lengths @torch.no_grad() def lowerCamelCase_ ( self : str , lowerCamelCase_ : List[Any]=None , lowerCamelCase_ : Dict=None , lowerCamelCase_ : int=None , lowerCamelCase_ : int = 5 , lowerCamelCase_ : int = 67 , lowerCamelCase_ : float = 1.0 , lowerCamelCase_ : Optional[int] = None , ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = eos_token_id SCREAMING_SNAKE_CASE : int = None SCREAMING_SNAKE_CASE : List[Any] = None SCREAMING_SNAKE_CASE : Union[str, Any] = torch.ones(lowerCamelCase_ , device=lowerCamelCase_ , dtype=torch.int ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.zeros(lowerCamelCase_ , device=lowerCamelCase_ , dtype=torch.bool ) if input_embeds is not None: SCREAMING_SNAKE_CASE : Dict = input_embeds else: SCREAMING_SNAKE_CASE : Dict = self.transformer.transformer.wte(lowerCamelCase_ ) for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Optional[int] = self.transformer(inputs_embeds=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = outputs.logits SCREAMING_SNAKE_CASE : Optional[int] = logits[:, -1, :] / (temperature if temperature > 0 else 1.0) SCREAMING_SNAKE_CASE : Any = logits.softmax(-1 ).log() if scores is None: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = logits.topk(lowerCamelCase_ , -1 ) SCREAMING_SNAKE_CASE : Optional[Any] = generated.expand(lowerCamelCase_ , *generated.shape[1:] ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = next_tokens.permute(1 , 0 ), scores.squeeze(0 ) if tokens is None: SCREAMING_SNAKE_CASE : List[Any] = next_tokens else: SCREAMING_SNAKE_CASE : Dict = tokens.expand(lowerCamelCase_ , *tokens.shape[1:] ) SCREAMING_SNAKE_CASE : str = torch.cat((tokens, next_tokens) , dim=1 ) else: SCREAMING_SNAKE_CASE : Tuple = -float(np.inf ) SCREAMING_SNAKE_CASE : Optional[int] = 0 SCREAMING_SNAKE_CASE : Dict = scores[:, None] + logits seq_lengths[~is_stopped] += 1 SCREAMING_SNAKE_CASE : List[str] = scores_sum / seq_lengths[:, None] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = scores_sum_average.view(-1 ).topk(lowerCamelCase_ , -1 ) SCREAMING_SNAKE_CASE : str = next_tokens // scores_sum.shape[1] SCREAMING_SNAKE_CASE : Tuple = seq_lengths[next_tokens_source] SCREAMING_SNAKE_CASE : int = next_tokens % scores_sum.shape[1] SCREAMING_SNAKE_CASE : Dict = next_tokens.unsqueeze(1 ) SCREAMING_SNAKE_CASE : Dict = tokens[next_tokens_source] SCREAMING_SNAKE_CASE : Any = torch.cat((tokens, next_tokens) , dim=1 ) SCREAMING_SNAKE_CASE : List[str] = generated[next_tokens_source] SCREAMING_SNAKE_CASE : Optional[Any] = scores_sum_average * seq_lengths SCREAMING_SNAKE_CASE : Any = is_stopped[next_tokens_source] SCREAMING_SNAKE_CASE : Dict = self.transformer.transformer.wte(next_tokens.squeeze() ).view(generated.shape[0] , 1 , -1 ) SCREAMING_SNAKE_CASE : str = torch.cat((generated, next_token_embed) , dim=1 ) SCREAMING_SNAKE_CASE : Dict = is_stopped + next_tokens.eq(lowerCamelCase_ ).squeeze() if is_stopped.all(): break SCREAMING_SNAKE_CASE : int = scores / seq_lengths SCREAMING_SNAKE_CASE : Dict = scores.argsort(descending=lowerCamelCase_ ) # tokens tensors are already padded to max_seq_length SCREAMING_SNAKE_CASE : Union[str, Any] = [tokens[i] for i in order] SCREAMING_SNAKE_CASE : Dict = torch.stack(lowerCamelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([seq_lengths[i] for i in order] , dtype=seq_lengths.dtype ) return output_texts, seq_lengths
323
0
"""simple docstring""" import logging import os from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union from filelock import FileLock from transformers import PreTrainedTokenizer, is_tf_available, is_torch_available a = logging.getLogger(__name__) @dataclass class lowercase_ : '''simple docstring''' UpperCAmelCase : str UpperCAmelCase : List[str] UpperCAmelCase : Optional[List[str]] @dataclass class lowercase_ : '''simple docstring''' UpperCAmelCase : List[int] UpperCAmelCase : List[int] UpperCAmelCase : Optional[List[int]] = None UpperCAmelCase : Optional[List[int]] = None class lowercase_ ( __lowerCAmelCase ): '''simple docstring''' UpperCAmelCase : Any = '''train''' UpperCAmelCase : Tuple = '''dev''' UpperCAmelCase : int = '''test''' class lowercase_ : '''simple docstring''' @staticmethod def lowerCAmelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : Union[Split, str] ): raise NotImplementedError @staticmethod def lowerCAmelCase_ ( _UpperCAmelCase : str ): raise NotImplementedError @staticmethod def lowerCAmelCase_ ( _UpperCAmelCase : List[InputExample] , _UpperCAmelCase : List[str] , _UpperCAmelCase : int , _UpperCAmelCase : PreTrainedTokenizer , _UpperCAmelCase : List[str]=False , _UpperCAmelCase : List[str]="[CLS]" , _UpperCAmelCase : List[Any]=1 , _UpperCAmelCase : Tuple="[SEP]" , _UpperCAmelCase : List[str]=False , _UpperCAmelCase : Optional[Any]=False , _UpperCAmelCase : str=0 , _UpperCAmelCase : Optional[int]=0 , _UpperCAmelCase : Any=-100 , _UpperCAmelCase : Union[str, Any]=0 , _UpperCAmelCase : List[Any]=True , ): _A = {label: i for i, label in enumerate(_UpperCAmelCase )} _A = [] for ex_index, example in enumerate(_UpperCAmelCase ): if ex_index % 10_000 == 0: logger.info('Writing example %d of %d' , _UpperCAmelCase , len(_UpperCAmelCase ) ) _A = [] _A = [] for word, label in zip(example.words , example.labels ): _A = tokenizer.tokenize(_UpperCAmelCase ) # bert-base-multilingual-cased sometimes output "nothing ([]) when calling tokenize with just a space. if len(_UpperCAmelCase ) > 0: tokens.extend(_UpperCAmelCase ) # Use the real label id for the first token of the word, and padding ids for the remaining tokens label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(_UpperCAmelCase ) - 1) ) # Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa. _A = tokenizer.num_special_tokens_to_add() if len(_UpperCAmelCase ) > max_seq_length - special_tokens_count: _A = tokens[: (max_seq_length - special_tokens_count)] _A = label_ids[: (max_seq_length - special_tokens_count)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens += [sep_token] label_ids += [pad_token_label_id] if sep_token_extra: # roberta uses an extra separator b/w pairs of sentences tokens += [sep_token] label_ids += [pad_token_label_id] _A = [sequence_a_segment_id] * len(_UpperCAmelCase ) if cls_token_at_end: tokens += [cls_token] label_ids += [pad_token_label_id] segment_ids += [cls_token_segment_id] else: _A = [cls_token] + tokens _A = [pad_token_label_id] + label_ids _A = [cls_token_segment_id] + segment_ids _A = tokenizer.convert_tokens_to_ids(_UpperCAmelCase ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. _A = [1 if mask_padding_with_zero else 0] * len(_UpperCAmelCase ) # Zero-pad up to the sequence length. _A = max_seq_length - len(_UpperCAmelCase ) if pad_on_left: _A = ([pad_token] * padding_length) + input_ids _A = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask _A = ([pad_token_segment_id] * padding_length) + segment_ids _A = ([pad_token_label_id] * padding_length) + label_ids else: input_ids += [pad_token] * padding_length input_mask += [0 if mask_padding_with_zero else 1] * padding_length segment_ids += [pad_token_segment_id] * padding_length label_ids += [pad_token_label_id] * padding_length assert len(_UpperCAmelCase ) == max_seq_length assert len(_UpperCAmelCase ) == max_seq_length assert len(_UpperCAmelCase ) == max_seq_length assert len(_UpperCAmelCase ) == max_seq_length if ex_index < 5: logger.info('*** Example ***' ) logger.info('guid: %s' , example.guid ) logger.info('tokens: %s' , ' '.join([str(_UpperCAmelCase ) for x in tokens] ) ) logger.info('input_ids: %s' , ' '.join([str(_UpperCAmelCase ) for x in input_ids] ) ) logger.info('input_mask: %s' , ' '.join([str(_UpperCAmelCase ) for x in input_mask] ) ) logger.info('segment_ids: %s' , ' '.join([str(_UpperCAmelCase ) for x in segment_ids] ) ) logger.info('label_ids: %s' , ' '.join([str(_UpperCAmelCase ) for x in label_ids] ) ) if "token_type_ids" not in tokenizer.model_input_names: _A = None features.append( InputFeatures( input_ids=_UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , label_ids=_UpperCAmelCase ) ) return features if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset class lowercase_ ( __lowerCAmelCase ): '''simple docstring''' UpperCAmelCase : List[InputFeatures] UpperCAmelCase : int = nn.CrossEntropyLoss().ignore_index def __init__( self : int , _UpperCAmelCase : TokenClassificationTask , _UpperCAmelCase : str , _UpperCAmelCase : PreTrainedTokenizer , _UpperCAmelCase : List[str] , _UpperCAmelCase : str , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : Optional[Any]=False , _UpperCAmelCase : Split = Split.train , ): # Load data features from cache or dataset file _A = os.path.join( _UpperCAmelCase , 'cached_{}_{}_{}'.format(mode.value , tokenizer.__class__.__name__ , str(_UpperCAmelCase ) ) , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. _A = cached_features_file + '.lock' with FileLock(_UpperCAmelCase ): if os.path.exists(_UpperCAmelCase ) and not overwrite_cache: logger.info(F'''Loading features from cached file {cached_features_file}''' ) _A = torch.load(_UpperCAmelCase ) else: logger.info(F'''Creating features from dataset file at {data_dir}''' ) _A = token_classification_task.read_examples_from_file(_UpperCAmelCase , _UpperCAmelCase ) # TODO clean up all this to leverage built-in features of tokenizers _A = token_classification_task.convert_examples_to_features( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , cls_token_at_end=bool(model_type in ['xlnet'] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['xlnet'] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=_UpperCAmelCase , pad_on_left=bool(tokenizer.padding_side == 'left' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info(F'''Saving features into cached file {cached_features_file}''' ) torch.save(self.features , _UpperCAmelCase ) def __len__( self : Dict ): return len(self.features ) def __getitem__( self : int , _UpperCAmelCase : Union[str, Any] ): return self.features[i] if is_tf_available(): import tensorflow as tf class lowercase_ : '''simple docstring''' UpperCAmelCase : List[InputFeatures] UpperCAmelCase : int = -100 def __init__( self : int , _UpperCAmelCase : TokenClassificationTask , _UpperCAmelCase : str , _UpperCAmelCase : PreTrainedTokenizer , _UpperCAmelCase : List[str] , _UpperCAmelCase : str , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : Optional[Any]=False , _UpperCAmelCase : Split = Split.train , ): _A = token_classification_task.read_examples_from_file(_UpperCAmelCase , _UpperCAmelCase ) # TODO clean up all this to leverage built-in features of tokenizers _A = token_classification_task.convert_examples_to_features( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , cls_token_at_end=bool(model_type in ['xlnet'] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['xlnet'] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=_UpperCAmelCase , pad_on_left=bool(tokenizer.padding_side == 'left' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) def gen(): for ex in self.features: if ex.token_type_ids is None: yield ( {"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label_ids, ) else: yield ( { "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label_ids, ) if "token_type_ids" not in tokenizer.model_input_names: _A = tf.data.Dataset.from_generator( _UpperCAmelCase , ({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa) , ( {'input_ids': tf.TensorShape([None] ), 'attention_mask': tf.TensorShape([None] )}, tf.TensorShape([None] ), ) , ) else: _A = tf.data.Dataset.from_generator( _UpperCAmelCase , ({'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa}, tf.intaa) , ( { 'input_ids': tf.TensorShape([None] ), 'attention_mask': tf.TensorShape([None] ), 'token_type_ids': tf.TensorShape([None] ), }, tf.TensorShape([None] ), ) , ) def lowerCAmelCase_ ( self : Dict ): _A = self.dataset.apply(tf.data.experimental.assert_cardinality(len(self.features ) ) ) return self.dataset def __len__( self : Tuple ): return len(self.features ) def __getitem__( self : Dict , _UpperCAmelCase : Optional[int] ): return self.features[i]
271
"""simple docstring""" import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class lowercase_ ( __lowerCAmelCase ): '''simple docstring''' UpperCAmelCase : Union[str, Any] = (IPNDMScheduler,) UpperCAmelCase : Optional[Any] = (('''num_inference_steps''', 50),) def lowerCAmelCase_ ( self : Union[str, Any] , **_UpperCAmelCase : List[Any] ): _A = {'num_train_timesteps': 1_000} config.update(**_UpperCAmelCase ) return config def lowerCAmelCase_ ( self : Tuple , _UpperCAmelCase : Optional[int]=0 , **_UpperCAmelCase : Union[str, Any] ): _A = dict(self.forward_default_kwargs ) _A = kwargs.pop('num_inference_steps' , _UpperCAmelCase ) _A = self.dummy_sample _A = 0.1 * sample _A = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: _A = self.get_scheduler_config(**_UpperCAmelCase ) _A = scheduler_class(**_UpperCAmelCase ) scheduler.set_timesteps(_UpperCAmelCase ) # copy over dummy past residuals _A = dummy_past_residuals[:] if time_step is None: _A = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_UpperCAmelCase ) _A = scheduler_class.from_pretrained(_UpperCAmelCase ) new_scheduler.set_timesteps(_UpperCAmelCase ) # copy over dummy past residuals _A = dummy_past_residuals[:] _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample _A = new_scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample _A = new_scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def lowerCAmelCase_ ( self : str ): pass def lowerCAmelCase_ ( self : Optional[Any] , _UpperCAmelCase : Any=0 , **_UpperCAmelCase : Any ): _A = dict(self.forward_default_kwargs ) _A = kwargs.pop('num_inference_steps' , _UpperCAmelCase ) _A = self.dummy_sample _A = 0.1 * sample _A = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: _A = self.get_scheduler_config() _A = scheduler_class(**_UpperCAmelCase ) scheduler.set_timesteps(_UpperCAmelCase ) # copy over dummy past residuals (must be after setting timesteps) _A = dummy_past_residuals[:] if time_step is None: _A = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_UpperCAmelCase ) _A = scheduler_class.from_pretrained(_UpperCAmelCase ) # copy over dummy past residuals new_scheduler.set_timesteps(_UpperCAmelCase ) # copy over dummy past residual (must be after setting timesteps) _A = dummy_past_residuals[:] _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample _A = new_scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample _A = new_scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def lowerCAmelCase_ ( self : List[str] , **_UpperCAmelCase : Optional[int] ): _A = self.scheduler_classes[0] _A = self.get_scheduler_config(**_UpperCAmelCase ) _A = scheduler_class(**_UpperCAmelCase ) _A = 10 _A = self.dummy_model() _A = self.dummy_sample_deter scheduler.set_timesteps(_UpperCAmelCase ) for i, t in enumerate(scheduler.timesteps ): _A = model(_UpperCAmelCase , _UpperCAmelCase ) _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ).prev_sample for i, t in enumerate(scheduler.timesteps ): _A = model(_UpperCAmelCase , _UpperCAmelCase ) _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ).prev_sample return sample def lowerCAmelCase_ ( self : Union[str, Any] ): _A = dict(self.forward_default_kwargs ) _A = kwargs.pop('num_inference_steps' , _UpperCAmelCase ) for scheduler_class in self.scheduler_classes: _A = self.get_scheduler_config() _A = scheduler_class(**_UpperCAmelCase ) _A = self.dummy_sample _A = 0.1 * sample if num_inference_steps is not None and hasattr(_UpperCAmelCase , 'set_timesteps' ): scheduler.set_timesteps(_UpperCAmelCase ) elif num_inference_steps is not None and not hasattr(_UpperCAmelCase , 'set_timesteps' ): _A = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) _A = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] _A = dummy_past_residuals[:] _A = scheduler.timesteps[5] _A = scheduler.timesteps[6] _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample _A = scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def lowerCAmelCase_ ( self : Tuple ): for timesteps in [100, 1_000]: self.check_over_configs(num_train_timesteps=_UpperCAmelCase , time_step=_UpperCAmelCase ) def lowerCAmelCase_ ( self : List[Any] ): for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=_UpperCAmelCase , time_step=_UpperCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] ): _A = self.full_loop() _A = torch.mean(torch.abs(_UpperCAmelCase ) ) assert abs(result_mean.item() - 2_540_529 ) < 10
271
1
"""simple docstring""" from timeit import timeit def __lowerCamelCase ( a_ : int ) -> int: if number < 0: raise ValueError('''the value of input must not be negative''' ) __SCREAMING_SNAKE_CASE :List[str] = 0 while number: number &= number - 1 result += 1 return result def __lowerCamelCase ( a_ : int ) -> int: if number < 0: raise ValueError('''the value of input must not be negative''' ) __SCREAMING_SNAKE_CASE :str = 0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def __lowerCamelCase ( ) -> None: def do_benchmark(a_ : int ) -> None: __SCREAMING_SNAKE_CASE :Dict = '''import __main__ as z''' print(f'''Benchmark when {number = }:''' ) print(f'''{get_set_bits_count_using_modulo_operator(SCREAMING_SNAKE_CASE__ ) = }''' ) __SCREAMING_SNAKE_CASE :List[Any] = timeit('''z.get_set_bits_count_using_modulo_operator(25)''' , setup=SCREAMING_SNAKE_CASE__ ) print(f'''timeit() runs in {timing} seconds''' ) print(f'''{get_set_bits_count_using_brian_kernighans_algorithm(SCREAMING_SNAKE_CASE__ ) = }''' ) __SCREAMING_SNAKE_CASE :str = timeit( '''z.get_set_bits_count_using_brian_kernighans_algorithm(25)''' , setup=SCREAMING_SNAKE_CASE__ , ) print(f'''timeit() runs in {timing} seconds''' ) for number in (25, 37, 58, 0): do_benchmark(SCREAMING_SNAKE_CASE__ ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
191
import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available from . import BaseDiffusersCLICommand def _snake_case( SCREAMING_SNAKE_CASE__ : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' return EnvironmentCommand() class A ( _UpperCAmelCase ): """simple docstring""" @staticmethod def snake_case__ ( lowercase_ : ArgumentParser )-> Dict: '''simple docstring''' A__ = parser.add_parser('env' ) download_parser.set_defaults(func=lowercase_ ) def snake_case__ ( self : List[Any] )-> List[str]: '''simple docstring''' A__ = huggingface_hub.__version__ A__ = 'not installed' A__ = 'NA' if is_torch_available(): import torch A__ = torch.__version__ A__ = torch.cuda.is_available() A__ = 'not installed' if is_transformers_available(): import transformers A__ = transformers.__version__ A__ = 'not installed' if is_accelerate_available(): import accelerate A__ = accelerate.__version__ A__ = 'not installed' if is_xformers_available(): import xformers A__ = xformers.__version__ A__ = { '`diffusers` version': version, 'Platform': platform.platform(), 'Python version': platform.python_version(), 'PyTorch version (GPU?)': F'{pt_version} ({pt_cuda_available})', 'Huggingface_hub version': hub_version, 'Transformers version': transformers_version, 'Accelerate version': accelerate_version, 'xFormers version': xformers_version, 'Using GPU in script?': '<fill in>', 'Using distributed or parallel set-up in script?': '<fill in>', } print('\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n' ) print(self.format_dict(lowercase_ ) ) return info @staticmethod def snake_case__ ( lowercase_ : int )-> Optional[Any]: '''simple docstring''' return "\n".join([F'- {prop}: {val}' for prop, val in d.items()] ) + "\n"
7
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. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class lowercase ( a ): lowercase__ : Optional[Any] = ( """This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.""" """It takes two arguments named `image` which should be the original image, and `label` which should be a text """ """describing the elements what should be identified in the segmentation mask. The tool returns the mask.""" ) lowercase__ : Optional[int] = """CIDAS/clipseg-rd64-refined""" lowercase__ : Tuple = """image_segmenter""" lowercase__ : Optional[Any] = CLIPSegForImageSegmentation lowercase__ : int = ["""image""", """text"""] lowercase__ : List[str] = ["""image"""] def __init__( self : str , *_UpperCamelCase : str , **_UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["vision"] ) super().__init__(*_UpperCamelCase , **_UpperCamelCase ) def __snake_case( self : int , _UpperCamelCase : "Image" , _UpperCamelCase : str ) -> Optional[int]: '''simple docstring''' return self.pre_processor(text=[label] , images=[image] , padding=_UpperCamelCase , return_tensors="pt" ) def __snake_case( self : Union[str, Any] , _UpperCamelCase : str ) -> Union[str, Any]: '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE = self.model(**_UpperCamelCase ).logits return logits def __snake_case( self : Any , _UpperCamelCase : Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = outputs.cpu().detach().numpy() SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
353
from random import randint, random def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : int = 5 , ): SCREAMING_SNAKE_CASE = [[-1] * number_of_cells] # Create a highway without any car SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = max(UpperCAmelCase__ , 0 ) while i < number_of_cells: SCREAMING_SNAKE_CASE = ( randint(0 , UpperCAmelCase__ ) if random_speed else initial_speed ) # Place the cars i += ( randint(1 , max_speed * 2 ) if random_frequency else frequency ) # Arbitrary number, may need tuning return highway def __lowerCamelCase (UpperCAmelCase__ : list , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = highway_now[car_index + 1 :] for cell in range(len(UpperCAmelCase__ ) ): # May need a better name for this if cells[cell] != -1: # If the cell is not empty then return distance # we have the distance we wanted distance += 1 # Here if the car is near the end of the highway return distance + get_distance(UpperCAmelCase__ , -1 ) def __lowerCamelCase (UpperCAmelCase__ : list , UpperCAmelCase__ : float , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = len(UpperCAmelCase__ ) # Beforce calculations, the highway is empty SCREAMING_SNAKE_CASE = [-1] * number_of_cells for car_index in range(UpperCAmelCase__ ): if highway_now[car_index] != -1: # Add 1 to the current speed of the car and cap the speed SCREAMING_SNAKE_CASE = min(highway_now[car_index] + 1 , UpperCAmelCase__ ) # Number of empty cell before the next car SCREAMING_SNAKE_CASE = get_distance(UpperCAmelCase__ , UpperCAmelCase__ ) - 1 # We can't have the car causing an accident SCREAMING_SNAKE_CASE = min(next_highway[car_index] , UpperCAmelCase__ ) if random() < probability: # Randomly, a driver will slow down SCREAMING_SNAKE_CASE = max(next_highway[car_index] - 1 , 0 ) return next_highway def __lowerCamelCase (UpperCAmelCase__ : list , UpperCAmelCase__ : int , UpperCAmelCase__ : float , UpperCAmelCase__ : int ): SCREAMING_SNAKE_CASE = len(highway[0] ) for i in range(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = update(highway[i] , UpperCAmelCase__ , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = [-1] * number_of_cells for car_index in range(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = next_speeds_calculated[car_index] if speed != -1: # Change the position based on the speed (with % to create the loop) SCREAMING_SNAKE_CASE = (car_index + speed) % number_of_cells # Commit the change of position SCREAMING_SNAKE_CASE = speed highway.append(UpperCAmelCase__ ) return highway if __name__ == "__main__": import doctest doctest.testmod()
206
0
import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() __A = logging.get_logger(__name__) __A = { "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", "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", } __A = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def lowerCamelCase_ ( UpperCamelCase__ : Any , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] ) -> Tuple: """simple docstring""" for attribute in key.split('.' ): __lowerCamelCase = getattr(UpperCamelCase__ , UpperCamelCase__ ) if weight_type is not None: __lowerCamelCase = getattr(UpperCamelCase__ , UpperCamelCase__ ).shape else: __lowerCamelCase = hf_pointer.shape assert hf_shape == value.shape, ( 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 else: __lowerCamelCase = value logger.info(F"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.""" ) def lowerCamelCase_ ( UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple ) -> Optional[Any]: """simple docstring""" __lowerCamelCase = [] __lowerCamelCase = fairseq_model.state_dict() __lowerCamelCase = hf_model.feature_extractor __lowerCamelCase = hf_model.adapter for name, value in fairseq_dict.items(): __lowerCamelCase = False if "conv_layers" in name: load_conv_layer( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , hf_model.config.feat_extract_norm == 'group' , ) __lowerCamelCase = True elif any(x in name for x in ['adaptor', 'w2v_encoder.proj.', 'w2v_proj_ln.'] ): load_adapter(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) __lowerCamelCase = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: __lowerCamelCase = True if "*" in mapped_key: __lowerCamelCase = name.split(UpperCamelCase__ )[0].split('.' )[-2] __lowerCamelCase = mapped_key.replace('*' , UpperCamelCase__ ) 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: __lowerCamelCase = 'weight' else: __lowerCamelCase = None set_recursively(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) continue if not is_used: unused_weights.append(UpperCamelCase__ ) logger.warning(F"""Unused weights: {unused_weights}""" ) def lowerCamelCase_ ( UpperCamelCase__ : Dict , UpperCamelCase__ : Any , UpperCamelCase__ : str , UpperCamelCase__ : Tuple , UpperCamelCase__ : Tuple ) -> 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: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( 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: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( 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: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"""{full_name} has size {value.shape}, but {feature_extractor[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: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor[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(UpperCamelCase__ ) def lowerCamelCase_ ( UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : int ) -> Union[str, Any]: """simple docstring""" __lowerCamelCase = full_name.split('adaptor.' )[-1] __lowerCamelCase = name.split('.' ) if items[1].isdigit(): __lowerCamelCase = int(items[1] ) else: __lowerCamelCase = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found.""" __lowerCamelCase = value logger.info(F"""Adapter proj layer norm bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found.""" __lowerCamelCase = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found.""" __lowerCamelCase = value logger.info(F"""Adapter proj layer bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found.""" __lowerCamelCase = value logger.info(F"""Adapter proj layer weight was initialized from {full_name}.""" ) elif isinstance(UpperCamelCase__ , UpperCamelCase__ ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found.""" __lowerCamelCase = value logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found.""" __lowerCamelCase = value logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) else: unused_weights.append(UpperCamelCase__ ) def lowerCamelCase_ ( UpperCamelCase__ : Tuple ) -> Tuple: """simple docstring""" __lowerCamelCase , __lowerCamelCase = emb.weight.shape __lowerCamelCase = nn.Linear(UpperCamelCase__ , UpperCamelCase__ , bias=UpperCamelCase__ ) __lowerCamelCase = emb.weight.data return lin_layer @torch.no_grad() def lowerCamelCase_ ( UpperCamelCase__ : List[str] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[str] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[Any] , ) -> str: """simple docstring""" __lowerCamelCase = WavaVecaConfig.from_pretrained( UpperCamelCase__ , add_adapter=UpperCamelCase__ , adapter_stride=UpperCamelCase__ , adapter_kernel_size=UpperCamelCase__ , use_auth_token=UpperCamelCase__ , output_hidden_size=UpperCamelCase__ , ) __lowerCamelCase = MBartConfig.from_pretrained(UpperCamelCase__ ) # load model __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ 'config_yaml': config_yaml_path, 'data': '/'.join(dict_path.split('/' )[:-1] ), 'w2v_path': checkpoint_path, 'load_pretrained_decoder_from': None, } , ) __lowerCamelCase = model[0].eval() # load feature extractor __lowerCamelCase = WavaVecaFeatureExtractor.from_pretrained(UpperCamelCase__ , use_auth_token=UpperCamelCase__ ) # set weights for wav2vec2 encoder __lowerCamelCase = WavaVecaModel(UpperCamelCase__ ) recursively_load_weights_wavaveca(model.encoder , UpperCamelCase__ ) # load decoder weights __lowerCamelCase = MBartForCausalLM(UpperCamelCase__ ) __lowerCamelCase , __lowerCamelCase = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=UpperCamelCase__ ) logger.warning(F"""The following keys are missing when loading the decoder weights: {missing_keys}""" ) logger.warning(F"""The following keys are unexpected when loading the decoder weights: {unexpected_keys}""" ) __lowerCamelCase = SpeechEncoderDecoderModel(encoder=UpperCamelCase__ , decoder=UpperCamelCase__ ) __lowerCamelCase = False __lowerCamelCase = MBartaaTokenizer(UpperCamelCase__ ) tokenizer.save_pretrained(UpperCamelCase__ ) __lowerCamelCase = hf_wavavec.config.to_dict() __lowerCamelCase = tokenizer.pad_token_id __lowerCamelCase = tokenizer.bos_token_id __lowerCamelCase = tokenizer.eos_token_id __lowerCamelCase = 'mbart50' __lowerCamelCase = 'wav2vec2' __lowerCamelCase = tokenizer.eos_token_id __lowerCamelCase = 25_0004 __lowerCamelCase = tokenizer.eos_token_id __lowerCamelCase = SpeechEncoderDecoderConfig.from_dict(UpperCamelCase__ ) hf_wavavec.save_pretrained(UpperCamelCase__ ) feature_extractor.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": __A = 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_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-xls-r-1b", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/mbart-large-50-one-to-many-mmt", type=str, help="Path to hf decoder checkpoint config", ) parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers") parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers") parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers") parser.add_argument("--encoder_output_dim", default=10_24, type=int, help="encoder output dim") parser.add_argument("--start_token_id", default=25_00_04, type=int, help="`decoder_start_token_id` of model config") __A = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
90
from math import sqrt def lowerCamelCase_ ( UpperCamelCase__ : int ) -> bool: """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(sqrt(UpperCamelCase__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCamelCase_ ( UpperCamelCase__ : int = 1_0001 ) -> int: """simple docstring""" __lowerCamelCase = 0 __lowerCamelCase = 1 while count != nth and number < 3: number += 1 if is_prime(UpperCamelCase__ ): count += 1 while count != nth: number += 2 if is_prime(UpperCamelCase__ ): count += 1 return number if __name__ == "__main__": print(f'''{solution() = }''')
90
1
def A_ ( A__ ) -> tuple[int, int]: try: a__ : Union[str, Any] = float(A__ ) except ValueError: raise ValueError('Please enter a valid number' ) a__ : str = decimal - int(A__ ) if fractional_part == 0: return int(A__ ), 1 else: a__ : Any = len(str(A__ ).split('.' )[1] ) a__ : Any = int(decimal * (10**number_of_frac_digits) ) a__ : List[Any] = 10**number_of_frac_digits a__ , a__ : str = denominator, numerator while True: a__ : str = dividend % divisor if remainder == 0: break a__ , a__ : List[str] = divisor, remainder a__ , a__ : Dict = numerator / divisor, denominator / divisor return int(A__ ), int(A__ ) if __name__ == "__main__": print(F"""{decimal_to_fraction(2) = }""") print(F"""{decimal_to_fraction(89.0) = }""") print(F"""{decimal_to_fraction('67') = }""") print(F"""{decimal_to_fraction('45.0') = }""") print(F"""{decimal_to_fraction(1.5) = }""") print(F"""{decimal_to_fraction('6.25') = }""") print(F"""{decimal_to_fraction('78td') = }""")
225
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation def A_ ( A__ ) -> str: a__ : Any = 384 if "tiny" in model_name: a__ : List[Any] = [3, 3, 9, 3] a__ : Optional[Any] = [96, 192, 384, 768] if "small" in model_name: a__ : Union[str, Any] = [3, 3, 27, 3] a__ : List[Any] = [96, 192, 384, 768] if "base" in model_name: a__ : int = [3, 3, 27, 3] a__ : List[str] = [128, 256, 512, 1024] a__ : Optional[int] = 512 if "large" in model_name: a__ : Optional[int] = [3, 3, 27, 3] a__ : Any = [192, 384, 768, 1536] a__ : int = 768 if "xlarge" in model_name: a__ : str = [3, 3, 27, 3] a__ : int = [256, 512, 1024, 2048] a__ : List[str] = 1024 # set label information a__ : int = 150 a__ : List[Any] = 'huggingface/label-files' a__ : str = 'ade20k-id2label.json' a__ : Optional[int] = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) a__ : List[str] = {int(A__ ): v for k, v in idalabel.items()} a__ : Union[str, Any] = {v: k for k, v in idalabel.items()} a__ : List[Any] = ConvNextConfig( depths=A__ , hidden_sizes=A__ , out_features=['stage1', 'stage2', 'stage3', 'stage4'] ) a__ : Optional[int] = UperNetConfig( backbone_config=A__ , auxiliary_in_channels=A__ , num_labels=A__ , idalabel=A__ , labelaid=A__ , ) return config def A_ ( A__ ) -> Tuple: a__ : Optional[int] = [] # fmt: off # stem rename_keys.append(('backbone.downsample_layers.0.0.weight', 'backbone.embeddings.patch_embeddings.weight') ) rename_keys.append(('backbone.downsample_layers.0.0.bias', 'backbone.embeddings.patch_embeddings.bias') ) rename_keys.append(('backbone.downsample_layers.0.1.weight', 'backbone.embeddings.layernorm.weight') ) rename_keys.append(('backbone.downsample_layers.0.1.bias', 'backbone.embeddings.layernorm.bias') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F'backbone.stages.{i}.{j}.gamma', F'backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter') ) rename_keys.append((F'backbone.stages.{i}.{j}.depthwise_conv.weight', F'backbone.encoder.stages.{i}.layers.{j}.dwconv.weight') ) rename_keys.append((F'backbone.stages.{i}.{j}.depthwise_conv.bias', F'backbone.encoder.stages.{i}.layers.{j}.dwconv.bias') ) rename_keys.append((F'backbone.stages.{i}.{j}.norm.weight', F'backbone.encoder.stages.{i}.layers.{j}.layernorm.weight') ) rename_keys.append((F'backbone.stages.{i}.{j}.norm.bias', F'backbone.encoder.stages.{i}.layers.{j}.layernorm.bias') ) rename_keys.append((F'backbone.stages.{i}.{j}.pointwise_conv1.weight', F'backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight') ) rename_keys.append((F'backbone.stages.{i}.{j}.pointwise_conv1.bias', F'backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias') ) rename_keys.append((F'backbone.stages.{i}.{j}.pointwise_conv2.weight', F'backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight') ) rename_keys.append((F'backbone.stages.{i}.{j}.pointwise_conv2.bias', F'backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias') ) if i > 0: rename_keys.append((F'backbone.downsample_layers.{i}.0.weight', F'backbone.encoder.stages.{i}.downsampling_layer.0.weight') ) rename_keys.append((F'backbone.downsample_layers.{i}.0.bias', F'backbone.encoder.stages.{i}.downsampling_layer.0.bias') ) rename_keys.append((F'backbone.downsample_layers.{i}.1.weight', F'backbone.encoder.stages.{i}.downsampling_layer.1.weight') ) rename_keys.append((F'backbone.downsample_layers.{i}.1.bias', F'backbone.encoder.stages.{i}.downsampling_layer.1.bias') ) rename_keys.append((F'backbone.norm{i}.weight', F'backbone.hidden_states_norms.stage{i+1}.weight') ) rename_keys.append((F'backbone.norm{i}.bias', F'backbone.hidden_states_norms.stage{i+1}.bias') ) # decode head rename_keys.extend( [ ('decode_head.conv_seg.weight', 'decode_head.classifier.weight'), ('decode_head.conv_seg.bias', 'decode_head.classifier.bias'), ('auxiliary_head.conv_seg.weight', 'auxiliary_head.classifier.weight'), ('auxiliary_head.conv_seg.bias', 'auxiliary_head.classifier.bias'), ] ) # fmt: on return rename_keys def A_ ( A__ , A__ , A__ ) -> str: a__ : List[str] = dct.pop(A__ ) a__ : int = val def A_ ( A__ , A__ , A__ ) -> str: a__ : Tuple = { 'upernet-convnext-tiny': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth', 'upernet-convnext-small': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth', 'upernet-convnext-base': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth', 'upernet-convnext-large': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth', 'upernet-convnext-xlarge': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth', } a__ : Dict = model_name_to_url[model_name] a__ : Optional[int] = torch.hub.load_state_dict_from_url(A__ , map_location='cpu' )['state_dict'] a__ : List[Any] = get_upernet_config(A__ ) a__ : Dict = UperNetForSemanticSegmentation(A__ ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): a__ : Dict = state_dict.pop(A__ ) if "bn" in key: a__ : Optional[int] = key.replace('bn' , 'batch_norm' ) a__ : List[Any] = val # rename keys a__ : Union[str, Any] = create_rename_keys(A__ ) for src, dest in rename_keys: rename_key(A__ , A__ , A__ ) model.load_state_dict(A__ ) # verify on image a__ : str = 'https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg' a__ : int = Image.open(requests.get(A__ , stream=A__ ).raw ).convert('RGB' ) a__ : Union[str, Any] = SegformerImageProcessor() a__ : Union[str, Any] = processor(A__ , return_tensors='pt' ).pixel_values with torch.no_grad(): a__ : Optional[Any] = model(A__ ) if model_name == "upernet-convnext-tiny": a__ : Union[str, Any] = torch.tensor( [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]] ) elif model_name == "upernet-convnext-small": a__ : int = torch.tensor( [[-8.82_36, -8.82_36, -8.67_71], [-8.82_36, -8.82_36, -8.67_71], [-8.76_38, -8.76_38, -8.62_40]] ) elif model_name == "upernet-convnext-base": a__ : int = torch.tensor( [[-8.85_58, -8.85_58, -8.69_05], [-8.85_58, -8.85_58, -8.69_05], [-8.76_69, -8.76_69, -8.60_21]] ) elif model_name == "upernet-convnext-large": a__ : Optional[Any] = torch.tensor( [[-8.66_60, -8.66_60, -8.62_10], [-8.66_60, -8.66_60, -8.62_10], [-8.63_10, -8.63_10, -8.59_64]] ) elif model_name == "upernet-convnext-xlarge": a__ : Optional[int] = torch.tensor( [[-8.49_80, -8.49_80, -8.39_77], [-8.49_80, -8.49_80, -8.39_77], [-8.43_79, -8.43_79, -8.34_12]] ) print('Logits:' , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , A__ , atol=1E-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(F'Saving model {model_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(A__ ) print(F'Saving processor to {pytorch_dump_folder_path}' ) processor.save_pretrained(A__ ) if push_to_hub: print(F'Pushing model and processor for {model_name} to hub' ) model.push_to_hub(F'openmmlab/{model_name}' ) processor.push_to_hub(F'openmmlab/{model_name}' ) if __name__ == "__main__": lowercase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""upernet-convnext-tiny""", type=str, choices=[F"""upernet-convnext-{size}""" for size in ["""tiny""", """small""", """base""", """large""", """xlarge"""]], help="""Name of the ConvNext UperNet model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) lowercase : str = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
225
1
"""simple docstring""" import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __a = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ["memory_attention", "encoder_attn"], ["attention", "attn"], ["/", "."], [".LayerNorm.gamma", "_layer_norm.weight"], [".LayerNorm.beta", "_layer_norm.bias"], ["r.layer_", "r.layers."], ["output_proj", "out_proj"], ["ffn.dense_1.", "fc2."], ["ffn.dense.", "fc1."], ["ffn_layer_norm", "final_layer_norm"], ["kernel", "weight"], ["encoder_layer_norm.", "encoder.layer_norm."], ["decoder_layer_norm.", "decoder.layer_norm."], ["embeddings.weights", "shared.weight"], ] def A_ ( _lowercase ): '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case_ :Any = k.replace(_lowercase, _lowercase ) return k def A_ ( _lowercase, _lowercase ): '''simple docstring''' snake_case_ :int = DEFAULTS.copy() cfg_kwargs.update(_lowercase ) snake_case_ :Union[str, Any] = PegasusConfig(**_lowercase ) snake_case_ :List[Any] = PegasusForConditionalGeneration(_lowercase ) snake_case_ :Optional[Any] = torch_model.model.state_dict() snake_case_ :Tuple = {} for k, v in tf_weights.items(): snake_case_ :Optional[Any] = rename_state_dict_key(_lowercase ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case_ :int = v.T snake_case_ :Dict = torch.tensor(_lowercase, dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case_ :Optional[Any] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case_ :Optional[Any] = mapping["""shared.weight"""] snake_case_ :List[str] = mapping["""shared.weight"""] snake_case_ :List[Any] = {k: torch.zeros_like(_lowercase ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**_lowercase ) snake_case_, snake_case_ :Optional[int] = torch_model.model.load_state_dict(_lowercase, strict=_lowercase ) snake_case_ :str = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def A_ ( _lowercase="./ckpt/aeslc/model.ckpt-32000" ): '''simple docstring''' snake_case_ :List[str] = tf.train.list_variables(_lowercase ) snake_case_ :int = {} snake_case_ :Any = ["""Adafactor""", """global_step"""] for name, shape in tqdm(_lowercase, desc="""converting tf checkpoint to dict""" ): snake_case_ :List[Any] = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case_ :Optional[Any] = tf.train.load_variable(_lowercase, _lowercase ) snake_case_ :List[str] = array return tf_weights def A_ ( _lowercase, _lowercase ): '''simple docstring''' snake_case_ :str = Path(_lowercase ).parent.name snake_case_ :int = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case_ :Optional[int] = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""", model_max_length=_lowercase ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(_lowercase ) # convert model snake_case_ :List[str] = get_tf_weights_as_numpy(_lowercase ) snake_case_ :Optional[int] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case_ :str = task_specific_params snake_case_ :Dict = convert_pegasus(_lowercase, _lowercase ) torch_model.save_pretrained(_lowercase ) snake_case_ :Optional[int] = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(_lowercase, Path(_lowercase ) / """pytorch_model.bin""" ) if __name__ == "__main__": __a = argparse.ArgumentParser() # Required parameters parser.add_argument("tf_ckpt_path", type=str, help="passed to tf.train.list_variables") parser.add_argument("save_dir", default=None, type=str, help="Path to the output PyTorch model.") __a = parser.parse_args() if args.save_dir is None: __a = Path(args.tf_ckpt_path).parent.name __a = os.path.join("pegasus", dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
66
"""simple docstring""" import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class lowerCamelCase : '''simple docstring''' def __init__( self: Dict , snake_case: Optional[Any] , snake_case: Tuple=13 , snake_case: Any=32 , snake_case: Union[str, Any]=2 , snake_case: Tuple=3 , snake_case: Union[str, Any]=16 , snake_case: Union[str, Any]=[1, 2, 1] , snake_case: Optional[Any]=[2, 2, 4] , snake_case: str=2 , snake_case: List[str]=2.0 , snake_case: Optional[int]=True , snake_case: Union[str, Any]=0.0 , snake_case: Optional[int]=0.0 , snake_case: Optional[Any]=0.1 , snake_case: List[str]="gelu" , snake_case: Any=False , snake_case: Optional[Any]=True , snake_case: Optional[int]=0.0_2 , snake_case: Any=1E-5 , snake_case: Optional[int]=True , snake_case: int=None , snake_case: Any=True , snake_case: str=10 , snake_case: Optional[Any]=8 , snake_case: Union[str, Any]=["stage1", "stage2", "stage3"] , snake_case: Tuple=[1, 2, 3] , ) -> Dict: snake_case_ :Dict = parent snake_case_ :List[Any] = batch_size snake_case_ :Dict = image_size snake_case_ :Dict = patch_size snake_case_ :Tuple = num_channels snake_case_ :List[Any] = embed_dim snake_case_ :List[str] = depths snake_case_ :str = num_heads snake_case_ :Tuple = window_size snake_case_ :Tuple = mlp_ratio snake_case_ :int = qkv_bias snake_case_ :Tuple = hidden_dropout_prob snake_case_ :Optional[Any] = attention_probs_dropout_prob snake_case_ :Dict = drop_path_rate snake_case_ :Any = hidden_act snake_case_ :Any = use_absolute_embeddings snake_case_ :int = patch_norm snake_case_ :List[Any] = layer_norm_eps snake_case_ :Tuple = initializer_range snake_case_ :str = is_training snake_case_ :int = scope snake_case_ :Tuple = use_labels snake_case_ :Tuple = type_sequence_label_size snake_case_ :str = encoder_stride snake_case_ :List[Any] = out_features snake_case_ :str = out_indices def lowerCAmelCase_ ( self: Tuple ) -> Dict: snake_case_ :Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ :str = None if self.use_labels: snake_case_ :Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ :Union[str, Any] = self.get_config() return config, pixel_values, labels def lowerCAmelCase_ ( self: int ) -> Optional[Any]: return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def lowerCAmelCase_ ( self: List[Any] , snake_case: str , snake_case: int , snake_case: List[str] ) -> Any: snake_case_ :Dict = MaskFormerSwinModel(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Tuple = model(snake_case ) snake_case_ :Dict = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) snake_case_ :Any = 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: Optional[Any] , snake_case: int , snake_case: List[str] , snake_case: Tuple ) -> Union[str, Any]: snake_case_ :Any = MaskFormerSwinBackbone(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Optional[Any] = model(snake_case ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [13, 16, 16, 16] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , [16, 32, 64] ) # verify ValueError with self.parent.assertRaises(snake_case ): snake_case_ :Optional[Any] = ["""stem"""] snake_case_ :str = MaskFormerSwinBackbone(config=snake_case ) def lowerCAmelCase_ ( self: List[str] ) -> Optional[Any]: snake_case_ :Optional[int] = self.prepare_config_and_inputs() snake_case_, snake_case_, snake_case_ :str = config_and_inputs snake_case_ :Tuple = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): '''simple docstring''' _A : Union[str, Any] = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) _A : str = {"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {} _A : List[str] = False _A : Any = False _A : Dict = False _A : List[Any] = False _A : Optional[int] = False def lowerCAmelCase_ ( self: Dict ) -> Any: snake_case_ :str = MaskFormerSwinModelTester(self ) snake_case_ :Optional[Any] = ConfigTester(self , config_class=snake_case , embed_dim=37 ) @require_torch_multi_gpu @unittest.skip( reason=( """`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with""" """ `nn.DataParallel`""" ) ) def lowerCAmelCase_ ( self: List[str] ) -> Optional[int]: pass def lowerCAmelCase_ ( self: Union[str, 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 lowerCAmelCase_ ( self: Any ) -> Tuple: return def lowerCAmelCase_ ( self: Any ) -> Any: snake_case_ :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> int: snake_case_ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*snake_case ) @unittest.skip("""Swin does not use inputs_embeds""" ) def lowerCAmelCase_ ( self: str ) -> List[str]: pass @unittest.skip("""Swin does not support feedforward chunking""" ) def lowerCAmelCase_ ( self: int ) -> Optional[int]: pass def lowerCAmelCase_ ( self: List[str] ) -> List[Any]: snake_case_, snake_case_ :List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ :str = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) snake_case_ :Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def lowerCAmelCase_ ( self: Tuple ) -> Dict: snake_case_, snake_case_ :int = 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_ :str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ :str = [*signature.parameters.keys()] snake_case_ :str = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , snake_case ) @unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions""" ) def lowerCAmelCase_ ( self: List[Any] ) -> List[Any]: pass @unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone""" ) def lowerCAmelCase_ ( self: Dict ) -> List[Any]: pass def lowerCAmelCase_ ( self: Union[str, Any] , snake_case: Union[str, Any] , snake_case: int , snake_case: Any , snake_case: List[str] ) -> str: snake_case_ :List[str] = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :List[Any] = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :Any = outputs.hidden_states snake_case_ :Optional[int] = getattr( self.model_tester , """expected_num_hidden_layers""" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(snake_case ) , snake_case ) # Swin has a different seq_length snake_case_ :str = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :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] , ) def lowerCAmelCase_ ( self: List[Any] ) -> Optional[int]: snake_case_, snake_case_ :Any = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :List[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_ :Tuple = 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[Any] = True self.check_hidden_states_output(snake_case , snake_case , snake_case , snake_case ) def lowerCAmelCase_ ( self: Optional[Any] ) -> Tuple: snake_case_, snake_case_ :int = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :List[Any] = 3 snake_case_ :List[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_ :Any = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :Tuple = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) snake_case_ :List[str] = 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_ :Any = True self.check_hidden_states_output(snake_case , snake_case , snake_case , (padded_height, padded_width) ) @unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints""" ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> List[str]: pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def lowerCAmelCase_ ( self: List[str] ) -> str: pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def lowerCAmelCase_ ( self: str ) -> List[Any]: pass def lowerCAmelCase_ ( self: Union[str, Any] ) -> Optional[Any]: snake_case_, snake_case_ :Dict = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(snake_case: str ): snake_case_ :Optional[int] = 0 return t def check_equivalence(snake_case: List[Any] , snake_case: Union[str, Any] , snake_case: int , snake_case: Tuple={} ): with torch.no_grad(): snake_case_ :List[Any] = model(**snake_case , return_dict=snake_case , **snake_case ) snake_case_ :Any = model(**snake_case , return_dict=snake_case , **snake_case ).to_tuple() def recursive_check(snake_case: List[Any] , snake_case: int ): if isinstance(snake_case , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(snake_case , snake_case ): recursive_check(snake_case , snake_case ) elif isinstance(snake_case , snake_case ): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values() ): recursive_check(snake_case , snake_case ) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(snake_case ) , set_nan_tensor_to_zero(snake_case ) , atol=1E-5 ) , msg=( """Tuple and dict output are not equal. Difference:""" f""" {torch.max(torch.abs(tuple_object - dict_object ) )}. Tuple has `nan`:""" f""" {torch.isnan(snake_case ).any()} and `inf`: {torch.isinf(snake_case )}. Dict has""" f""" `nan`: {torch.isnan(snake_case ).any()} and `inf`: {torch.isinf(snake_case )}.""" ) , ) recursive_check(snake_case , snake_case ) for model_class in self.all_model_classes: snake_case_ :int = model_class(snake_case ) model.to(snake_case ) model.eval() snake_case_ :Any = self._prepare_for_class(snake_case , snake_case ) snake_case_ :List[Any] = self._prepare_for_class(snake_case , snake_case ) check_equivalence(snake_case , snake_case , snake_case ) snake_case_ :Tuple = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) snake_case_ :Dict = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) check_equivalence(snake_case , snake_case , snake_case ) snake_case_ :Tuple = self._prepare_for_class(snake_case , snake_case ) snake_case_ :Any = self._prepare_for_class(snake_case , snake_case ) check_equivalence(snake_case , snake_case , snake_case , {"""output_hidden_states""": True} ) snake_case_ :Dict = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) snake_case_ :List[str] = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) check_equivalence(snake_case , snake_case , snake_case , {"""output_hidden_states""": True} ) @require_torch class lowerCamelCase ( unittest.TestCase , _lowerCAmelCase ): '''simple docstring''' _A : int = (MaskFormerSwinBackbone,) if is_torch_available() else () _A : Tuple = MaskFormerSwinConfig def lowerCAmelCase_ ( self: List[str] ) -> Optional[int]: snake_case_ :Optional[Any] = MaskFormerSwinModelTester(self ) def lowerCAmelCase_ ( self: int ) -> Optional[int]: snake_case_, snake_case_ :Any = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Tuple = inputs_dict["""pixel_values"""].shape[0] for backbone_class in self.all_model_classes: snake_case_ :List[str] = backbone_class(snake_case ) backbone.to(snake_case ) backbone.eval() snake_case_ :List[Any] = backbone(**snake_case ) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , snake_case ) self.assertTrue(len(outputs.feature_maps ) == len(backbone.channels ) ) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels ): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels) ) self.assertIsNone(outputs.hidden_states ) self.assertIsNone(outputs.attentions ) # Test output_hidden_states=True snake_case_ :Union[str, Any] = backbone(**snake_case , output_hidden_states=snake_case ) self.assertIsNotNone(outputs.hidden_states ) self.assertTrue(len(outputs.hidden_states ) , len(backbone.stage_names ) ) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels ): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) snake_case_, snake_case_, snake_case_ :List[Any] = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels) ) # Test output_attentions=True if self.has_attentions: snake_case_ :List[Any] = backbone(**snake_case , output_attentions=snake_case ) self.assertIsNotNone(outputs.attentions )
66
1
"""simple docstring""" from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer _A = logging.get_logger(__name__) # pylint: disable=invalid-name _A = '\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")\n\n >>> repo = "openai/shap-e-img2img"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = "https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png"\n >>> image = load_image(image_url).convert("RGB")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], "corgi_3d.gif")\n ```\n' @dataclass class _lowercase ( __UpperCAmelCase ): lowercase_ = 42 class _lowercase ( __UpperCAmelCase ): def __init__( self , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , ) -> Optional[int]: super().__init__() self.register_modules( prior=UpperCAmelCase_ , image_encoder=UpperCAmelCase_ , image_processor=UpperCAmelCase_ , scheduler=UpperCAmelCase_ , renderer=UpperCAmelCase_ , ) def _UpperCamelCase ( self , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) -> Optional[int]: if latents is None: lowerCamelCase : Any = randn_tensor(UpperCAmelCase_ , generator=UpperCAmelCase_ , device=UpperCAmelCase_ , dtype=UpperCAmelCase_ ) else: if latents.shape != shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}""" ) lowerCamelCase : str = latents.to(UpperCAmelCase_ ) lowerCamelCase : str = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self , UpperCAmelCase_=0 ) -> Optional[int]: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) lowerCamelCase : List[Any] = torch.device(F"""cuda:{gpu_id}""" ) lowerCamelCase : Any = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(UpperCAmelCase_ , UpperCAmelCase_ ) @property def _UpperCamelCase ( self ) -> Dict: if self.device != torch.device('meta' ) or not hasattr(self.image_encoder , '_hf_hook' ): return self.device for module in self.image_encoder.modules(): if ( hasattr(UpperCAmelCase_ , '_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 def _UpperCamelCase ( self , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , ) -> List[Any]: if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase : Union[str, Any] = torch.cat(UpperCAmelCase_ , axis=0 ) if image[0].ndim == 4 else torch.stack(UpperCAmelCase_ , axis=0 ) if not isinstance(UpperCAmelCase_ , torch.Tensor ): lowerCamelCase : Optional[Any] = self.image_processor(UpperCAmelCase_ , return_tensors='pt' ).pixel_values[0].unsqueeze(0 ) lowerCamelCase : Optional[int] = image.to(dtype=self.image_encoder.dtype , device=UpperCAmelCase_ ) lowerCamelCase : Optional[int] = self.image_encoder(UpperCAmelCase_ )['last_hidden_state'] lowerCamelCase : Optional[int] = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase : List[Any] = image_embeds.repeat_interleave(UpperCAmelCase_ , dim=0 ) if do_classifier_free_guidance: lowerCamelCase : Tuple = torch.zeros_like(UpperCAmelCase_ ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes lowerCamelCase : Optional[int] = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(UpperCAmelCase_ ) def __call__( self , UpperCAmelCase_ , UpperCAmelCase_ = 1 , UpperCAmelCase_ = 25 , UpperCAmelCase_ = None , UpperCAmelCase_ = None , UpperCAmelCase_ = 4.0 , UpperCAmelCase_ = 64 , UpperCAmelCase_ = "pil" , UpperCAmelCase_ = True , ) -> Optional[int]: if isinstance(UpperCAmelCase_ , PIL.Image.Image ): lowerCamelCase : Tuple = 1 elif isinstance(UpperCAmelCase_ , torch.Tensor ): lowerCamelCase : Optional[Any] = image.shape[0] elif isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase : Any = len(UpperCAmelCase_ ) else: raise ValueError( F"""`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(UpperCAmelCase_ )}""" ) lowerCamelCase : Optional[Any] = self._execution_device lowerCamelCase : str = batch_size * num_images_per_prompt lowerCamelCase : Tuple = guidance_scale > 1.0 lowerCamelCase : List[str] = self._encode_image(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # prior self.scheduler.set_timesteps(UpperCAmelCase_ , device=UpperCAmelCase_ ) lowerCamelCase : List[str] = self.scheduler.timesteps lowerCamelCase : Optional[int] = self.prior.config.num_embeddings lowerCamelCase : Dict = self.prior.config.embedding_dim lowerCamelCase : Union[str, Any] = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim lowerCamelCase : Union[str, Any] = latents.reshape(latents.shape[0] , UpperCAmelCase_ , UpperCAmelCase_ ) for i, t in enumerate(self.progress_bar(UpperCAmelCase_ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase : List[str] = self.scheduler.scale_model_input(UpperCAmelCase_ , UpperCAmelCase_ ) lowerCamelCase : Tuple = self.prior( UpperCAmelCase_ , timestep=UpperCAmelCase_ , proj_embedding=UpperCAmelCase_ , ).predicted_image_embedding # remove the variance lowerCamelCase , lowerCamelCase : Union[str, Any] = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase , lowerCamelCase : Optional[Any] = noise_pred.chunk(2 ) lowerCamelCase : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase : str = self.scheduler.step( UpperCAmelCase_ , timestep=UpperCAmelCase_ , sample=UpperCAmelCase_ , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=UpperCAmelCase_ ) lowerCamelCase : Dict = [] for i, latent in enumerate(UpperCAmelCase_ ): print() lowerCamelCase : Dict = self.renderer.decode( latent[None, :] , UpperCAmelCase_ , size=UpperCAmelCase_ , ray_batch_size=4096 , n_coarse_samples=64 , n_fine_samples=128 , ) images.append(UpperCAmelCase_ ) lowerCamelCase : Tuple = torch.stack(UpperCAmelCase_ ) if output_type not in ["np", "pil"]: raise ValueError(F"""Only the output types `pil` and `np` are supported not output_type={output_type}""" ) lowerCamelCase : List[Any] = images.cpu().numpy() if output_type == "pil": lowerCamelCase : Dict = [self.numpy_to_pil(UpperCAmelCase_ ) for image in images] # Offload last model to CPU if hasattr(self , 'final_offload_hook' ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=UpperCAmelCase_ )
205
"""simple docstring""" import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import numpy as np import pytest from datasets.arrow_dataset import Dataset from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex from .utils import require_elasticsearch, require_faiss _A = pytest.mark.integration @require_faiss class _lowercase ( __UpperCAmelCase ): def _UpperCamelCase ( self ) -> Union[str, Any]: lowerCamelCase : Any = Dataset.from_dict({'filename': ['my_name-train' + '_' + str(UpperCAmelCase_ ) for x in np.arange(30 ).tolist()]} ) return dset def _UpperCamelCase ( self ) -> List[Any]: import faiss lowerCamelCase : Dataset = self._create_dummy_dataset() lowerCamelCase : Optional[int] = dset.map( lambda UpperCAmelCase_ , UpperCAmelCase_ : {"vecs": i * np.ones(5 , dtype=np.floataa )} , with_indices=UpperCAmelCase_ , keep_in_memory=UpperCAmelCase_ ) lowerCamelCase : Dict = dset.add_faiss_index('vecs' , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT ) lowerCamelCase , lowerCamelCase : List[str] = dset.get_nearest_examples('vecs' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) dset.drop_index('vecs' ) def _UpperCamelCase ( self ) -> Tuple: import faiss lowerCamelCase : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='vecs' , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT , ) lowerCamelCase , lowerCamelCase : str = dset.get_nearest_examples('vecs' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) def _UpperCamelCase ( self ) -> int: import faiss lowerCamelCase : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='vecs' , metric_type=faiss.METRIC_INNER_PRODUCT , ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=UpperCAmelCase_ ) as tmp_file: dset.save_faiss_index('vecs' , tmp_file.name ) dset.load_faiss_index('vecs2' , tmp_file.name ) os.unlink(tmp_file.name ) lowerCamelCase , lowerCamelCase : List[str] = dset.get_nearest_examples('vecs2' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) def _UpperCamelCase ( self ) -> Any: lowerCamelCase : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='vecs' ) dset.drop_index('vecs' ) self.assertRaises(UpperCAmelCase_ , partial(dset.get_nearest_examples , 'vecs2' , np.ones(5 , dtype=np.floataa ) ) ) def _UpperCamelCase ( self ) -> Union[str, Any]: from elasticsearch import Elasticsearch lowerCamelCase : Dataset = self._create_dummy_dataset() with patch('elasticsearch.Elasticsearch.search' ) as mocked_search, patch( 'elasticsearch.client.IndicesClient.create' ) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk' ) as mocked_bulk: lowerCamelCase : Tuple = {'acknowledged': True} mocked_bulk.return_value([(True, None)] * 30 ) lowerCamelCase : int = {'hits': {'hits': [{'_score': 1, '_id': 29}]}} lowerCamelCase : Optional[Any] = Elasticsearch() dset.add_elasticsearch_index('filename' , es_client=UpperCAmelCase_ ) lowerCamelCase , lowerCamelCase : str = dset.get_nearest_examples('filename' , 'my_name-train_29' ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) @require_faiss class _lowercase ( __UpperCAmelCase ): def _UpperCamelCase ( self ) -> Union[str, Any]: import faiss lowerCamelCase : str = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) # add vectors index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsNotNone(index.faiss_index ) self.assertEqual(index.faiss_index.ntotal , 5 ) index.add_vectors(np.zeros((5, 5) , dtype=np.floataa ) ) self.assertEqual(index.faiss_index.ntotal , 10 ) # single query lowerCamelCase : Optional[int] = np.zeros(5 , dtype=np.floataa ) lowerCamelCase : List[str] = 1 lowerCamelCase , lowerCamelCase : int = index.search(UpperCAmelCase_ ) self.assertRaises(UpperCAmelCase_ , index.search , query.reshape(-1 , 1 ) ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) # batched queries lowerCamelCase : Tuple = np.eye(5 , dtype=np.floataa )[::-1] lowerCamelCase , lowerCamelCase : List[str] = index.search_batch(UpperCAmelCase_ ) self.assertRaises(UpperCAmelCase_ , index.search_batch , queries[0] ) lowerCamelCase : List[str] = [scores[0] for scores in total_scores] lowerCamelCase : List[str] = [indices[0] for indices in total_indices] self.assertGreater(np.min(UpperCAmelCase_ ) , 0 ) self.assertListEqual([4, 3, 2, 1, 0] , UpperCAmelCase_ ) def _UpperCamelCase ( self ) -> Dict: import faiss lowerCamelCase : List[Any] = FaissIndex(string_factory='Flat' ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) lowerCamelCase : int = FaissIndex(string_factory='LSH' ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexLSH ) with self.assertRaises(UpperCAmelCase_ ): lowerCamelCase : str = FaissIndex(string_factory='Flat' , custom_index=faiss.IndexFlat(5 ) ) def _UpperCamelCase ( self ) -> Any: import faiss lowerCamelCase : Any = faiss.IndexFlat(5 ) lowerCamelCase : Any = FaissIndex(custom_index=UpperCAmelCase_ ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) def _UpperCamelCase ( self ) -> Any: import faiss lowerCamelCase : Any = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=UpperCAmelCase_ ) as tmp_file: index.save(tmp_file.name ) lowerCamelCase : List[str] = FaissIndex.load(tmp_file.name ) os.unlink(tmp_file.name ) lowerCamelCase : Dict = np.zeros(5 , dtype=np.floataa ) lowerCamelCase : Optional[Any] = 1 lowerCamelCase , lowerCamelCase : str = index.search(UpperCAmelCase_ ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) @require_faiss def UpperCAmelCase ( a_ ): '''simple docstring''' import faiss lowerCamelCase : int = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5, dtype=np.floataa ) ) lowerCamelCase : Union[str, Any] = 'index.faiss' lowerCamelCase : List[Any] = F"""mock://{index_name}""" index.save(a_, storage_options=mockfs.storage_options ) lowerCamelCase : Optional[int] = FaissIndex.load(a_, storage_options=mockfs.storage_options ) lowerCamelCase : str = np.zeros(5, dtype=np.floataa ) lowerCamelCase : str = 1 lowerCamelCase , lowerCamelCase : int = index.search(a_ ) assert scores[0] > 0 assert indices[0] == 1 @require_elasticsearch class _lowercase ( __UpperCAmelCase ): def _UpperCamelCase ( self ) -> int: from elasticsearch import Elasticsearch with patch('elasticsearch.Elasticsearch.search' ) as mocked_search, patch( 'elasticsearch.client.IndicesClient.create' ) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk' ) as mocked_bulk: lowerCamelCase : Union[str, Any] = Elasticsearch() lowerCamelCase : Optional[Any] = {'acknowledged': True} lowerCamelCase : str = ElasticSearchIndex(es_client=UpperCAmelCase_ ) mocked_bulk.return_value([(True, None)] * 3 ) index.add_documents(['foo', 'bar', 'foobar'] ) # single query lowerCamelCase : Tuple = 'foo' lowerCamelCase : List[str] = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} lowerCamelCase , lowerCamelCase : Any = index.search(UpperCAmelCase_ ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # single query with timeout lowerCamelCase : Dict = 'foo' lowerCamelCase : Optional[Any] = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} lowerCamelCase , lowerCamelCase : Optional[Any] = index.search(UpperCAmelCase_ , request_timeout=30 ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # batched queries lowerCamelCase : str = ['foo', 'bar', 'foobar'] lowerCamelCase : Union[str, Any] = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} lowerCamelCase , lowerCamelCase : Optional[int] = index.search_batch(UpperCAmelCase_ ) lowerCamelCase : Dict = [scores[0] for scores in total_scores] lowerCamelCase : Optional[int] = [indices[0] for indices in total_indices] self.assertGreater(np.min(UpperCAmelCase_ ) , 0 ) self.assertListEqual([1, 1, 1] , UpperCAmelCase_ ) # batched queries with timeout lowerCamelCase : List[str] = ['foo', 'bar', 'foobar'] lowerCamelCase : str = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} lowerCamelCase , lowerCamelCase : Dict = index.search_batch(UpperCAmelCase_ , request_timeout=30 ) lowerCamelCase : Dict = [scores[0] for scores in total_scores] lowerCamelCase : int = [indices[0] for indices in total_indices] self.assertGreater(np.min(UpperCAmelCase_ ) , 0 ) self.assertListEqual([1, 1, 1] , UpperCAmelCase_ )
205
1
'''simple docstring''' import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DeformableDetrImageProcessor class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def __init__( self : Tuple , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any=7 , lowerCAmelCase__ : Optional[Any]=3 , lowerCAmelCase__ : Optional[Any]=30 , lowerCAmelCase__ : Dict=400 , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : str=None , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : List[str]=[0.5, 0.5, 0.5] , lowerCAmelCase__ : int=[0.5, 0.5, 0.5] , lowerCAmelCase__ : List[str]=True , lowerCAmelCase__ : Union[str, Any]=1 / 255 , lowerCAmelCase__ : Tuple=True , ) -> Optional[int]: '''simple docstring''' _UpperCamelCase = size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333} _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = num_channels _UpperCamelCase = min_resolution _UpperCamelCase = max_resolution _UpperCamelCase = do_resize _UpperCamelCase = size _UpperCamelCase = do_normalize _UpperCamelCase = image_mean _UpperCamelCase = image_std _UpperCamelCase = do_rescale _UpperCamelCase = rescale_factor _UpperCamelCase = do_pad def snake_case__ ( self : Optional[int] ) -> Dict: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def snake_case__ ( self : List[str] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=False ) -> str: '''simple docstring''' if not batched: _UpperCamelCase = image_inputs[0] if isinstance(lowerCAmelCase__ , Image.Image ): _UpperCamelCase , _UpperCamelCase = image.size else: _UpperCamelCase , _UpperCamelCase = image.shape[1], image.shape[2] if w < h: _UpperCamelCase = int(self.size['''shortest_edge'''] * h / w ) _UpperCamelCase = self.size['''shortest_edge'''] elif w > h: _UpperCamelCase = self.size['''shortest_edge'''] _UpperCamelCase = int(self.size['''shortest_edge'''] * w / h ) else: _UpperCamelCase = self.size['''shortest_edge'''] _UpperCamelCase = self.size['''shortest_edge'''] else: _UpperCamelCase = [] for image in image_inputs: _UpperCamelCase , _UpperCamelCase = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) _UpperCamelCase = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[0] )[0] _UpperCamelCase = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class __lowerCAmelCase ( __magic_name__ , unittest.TestCase ): """simple docstring""" _snake_case : Union[str, Any] = DeformableDetrImageProcessor if is_vision_available() else None def snake_case__ ( self : int ) -> Optional[Any]: '''simple docstring''' _UpperCamelCase = DeformableDetrImageProcessingTester(self ) @property def snake_case__ ( self : Optional[int] ) -> Dict: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def snake_case__ ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_mean''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''image_std''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_normalize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_resize''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_rescale''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''do_pad''' ) ) self.assertTrue(hasattr(lowerCAmelCase__ , '''size''' ) ) def snake_case__ ( self : List[Any] ) -> int: '''simple docstring''' _UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18, '''longest_edge''': 1333} ) self.assertEqual(image_processor.do_pad , lowerCAmelCase__ ) _UpperCamelCase = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=lowerCAmelCase__ ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42, '''longest_edge''': 84} ) self.assertEqual(image_processor.do_pad , lowerCAmelCase__ ) def snake_case__ ( self : Tuple ) -> Any: '''simple docstring''' pass def snake_case__ ( self : int ) -> Any: '''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=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , Image.Image ) # Test not batched input _UpperCamelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values _UpperCamelCase , _UpperCamelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _UpperCamelCase , _UpperCamelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) _UpperCamelCase = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def snake_case__ ( self : str ) -> List[str]: '''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=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , np.ndarray ) # Test not batched input _UpperCamelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values _UpperCamelCase , _UpperCamelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _UpperCamelCase = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values _UpperCamelCase , _UpperCamelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def snake_case__ ( self : Union[str, Any] ) -> 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=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , torch.Tensor ) # Test not batched input _UpperCamelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values _UpperCamelCase , _UpperCamelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _UpperCamelCase = image_processing(lowerCAmelCase__ , return_tensors='''pt''' ).pixel_values _UpperCamelCase , _UpperCamelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def snake_case__ ( self : int ) -> Tuple: '''simple docstring''' _UpperCamelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt''' , '''r''' ) as f: _UpperCamelCase = json.loads(f.read() ) _UpperCamelCase = {'''image_id''': 39769, '''annotations''': target} # encode them _UpperCamelCase = DeformableDetrImageProcessor() _UpperCamelCase = image_processing(images=lowerCAmelCase__ , annotations=lowerCAmelCase__ , return_tensors='''pt''' ) # verify pixel values _UpperCamelCase = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['''pixel_values'''].shape , lowerCAmelCase__ ) _UpperCamelCase = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , lowerCAmelCase__ , atol=1e-4 ) ) # verify area _UpperCamelCase = torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , lowerCAmelCase__ ) ) # verify boxes _UpperCamelCase = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , lowerCAmelCase__ ) _UpperCamelCase = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , lowerCAmelCase__ , atol=1e-3 ) ) # verify image_id _UpperCamelCase = torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , lowerCAmelCase__ ) ) # verify is_crowd _UpperCamelCase = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , lowerCAmelCase__ ) ) # verify class_labels _UpperCamelCase = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , lowerCAmelCase__ ) ) # verify orig_size _UpperCamelCase = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , lowerCAmelCase__ ) ) # verify size _UpperCamelCase = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , lowerCAmelCase__ ) ) @slow def snake_case__ ( self : Optional[Any] ) -> List[str]: '''simple docstring''' _UpperCamelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt''' , '''r''' ) as f: _UpperCamelCase = json.loads(f.read() ) _UpperCamelCase = {'''file_name''': '''000000039769.png''', '''image_id''': 39769, '''segments_info''': target} _UpperCamelCase = pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic''' ) # encode them _UpperCamelCase = DeformableDetrImageProcessor(format='''coco_panoptic''' ) _UpperCamelCase = image_processing(images=lowerCAmelCase__ , annotations=lowerCAmelCase__ , masks_path=lowerCAmelCase__ , return_tensors='''pt''' ) # verify pixel values _UpperCamelCase = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['''pixel_values'''].shape , lowerCAmelCase__ ) _UpperCamelCase = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , lowerCAmelCase__ , atol=1e-4 ) ) # verify area _UpperCamelCase = torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , lowerCAmelCase__ ) ) # verify boxes _UpperCamelCase = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , lowerCAmelCase__ ) _UpperCamelCase = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , lowerCAmelCase__ , atol=1e-3 ) ) # verify image_id _UpperCamelCase = torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , lowerCAmelCase__ ) ) # verify is_crowd _UpperCamelCase = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , lowerCAmelCase__ ) ) # verify class_labels _UpperCamelCase = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , lowerCAmelCase__ ) ) # verify masks _UpperCamelCase = 822873 self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item() , lowerCAmelCase__ ) # verify orig_size _UpperCamelCase = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , lowerCAmelCase__ ) ) # verify size _UpperCamelCase = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , lowerCAmelCase__ ) )
324
'''simple docstring''' from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase__ : Tuple = { 'configuration_mctct': ['MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MCTCTConfig'], 'feature_extraction_mctct': ['MCTCTFeatureExtractor'], 'processing_mctct': ['MCTCTProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Tuple = [ 'MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MCTCTForCTC', 'MCTCTModel', 'MCTCTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowercase__ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
324
1
_a = 8.31_4462 # Unit - J mol-1 K-1 def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase ): if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def __a ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase ): if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
359
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : str = torch.nn.Linear(10 , 10 ) UpperCAmelCase_ : List[str] = torch.optim.SGD(model.parameters() , 0.1 ) UpperCAmelCase_ : Optional[Any] = Accelerator() UpperCAmelCase_ : Tuple = accelerator.prepare(lowercase_ ) try: pickle.loads(pickle.dumps(lowercase_ ) ) except Exception as e: self.fail(F"""Accelerated optimizer pickling failed with {e}""" ) AcceleratorState._reset_state()
23
0
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 ) __UpperCamelCase : str = logging.getLogger(__name__) def a_ ( _A , _A ) -> List[Any]: """simple docstring""" snake_case__ = np.argmax(_A , axis=1 ) return np.sum(outputs == labels ) def a_ ( _A ) -> int: """simple docstring""" with open(_A , encoding='utf_8' ) as f: snake_case__ = csv.reader(_A ) snake_case__ = [] next(_A ) # skip the first line for line in tqdm(_A ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def a_ ( _A , _A , _A , _A , _A , _A ) -> List[Any]: """simple docstring""" snake_case__ = [] for dataset in encoded_datasets: snake_case__ = len(_A ) snake_case__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) snake_case__ = np.zeros((n_batch, 2) , dtype=np.intaa ) snake_case__ = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) snake_case__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(_A ): snake_case__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] snake_case__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] snake_case__ = with_conta snake_case__ = with_conta snake_case__ = len(_A ) - 1 snake_case__ = len(_A ) - 1 snake_case__ = with_conta snake_case__ = with_conta snake_case__ = mc_label snake_case__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(_A ) for t in all_inputs ) ) return tensor_datasets def a_ ( ) -> Optional[int]: """simple docstring""" snake_case__ = argparse.ArgumentParser() parser.add_argument('--model_name' , type=_A , 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=_A , type=_A , required=_A , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=_A , default='' ) parser.add_argument('--eval_dataset' , type=_A , default='' ) parser.add_argument('--seed' , type=_A , default=42 ) parser.add_argument('--num_train_epochs' , type=_A , default=3 ) parser.add_argument('--train_batch_size' , type=_A , default=8 ) parser.add_argument('--eval_batch_size' , type=_A , default=16 ) parser.add_argument('--adam_epsilon' , default=1e-8 , type=_A , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=_A , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=_A , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=_A , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=_A , default=6.25e-5 ) parser.add_argument('--warmup_steps' , default=0 , type=_A , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=_A , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=_A , default=0.01 ) parser.add_argument('--lm_coef' , type=_A , default=0.9 ) parser.add_argument('--n_valid' , type=_A , default=374 ) parser.add_argument('--server_ip' , type=_A , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=_A , default='' , help='Can be used for distant debugging.' ) snake_case__ = parser.parse_args() print(_A ) 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=_A ) 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 ) snake_case__ = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) snake_case__ = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(_A , _A ) ) 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 snake_case__ = ['_start_', '_delimiter_', '_classify_'] snake_case__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(_A ) snake_case__ = tokenizer.convert_tokens_to_ids(_A ) snake_case__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(_A ) ) model.to(_A ) # Load and encode the datasets def tokenize_and_encode(_A ): if isinstance(_A , _A ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(_A ) ) elif isinstance(_A , _A ): return obj return [tokenize_and_encode(_A ) for o in obj] logger.info('Encoding dataset...' ) snake_case__ = load_rocstories_dataset(args.train_dataset ) snake_case__ = load_rocstories_dataset(args.eval_dataset ) snake_case__ = (train_dataset, eval_dataset) snake_case__ = tokenize_and_encode(_A ) # Compute the max input length for the Transformer snake_case__ = model.config.n_positions // 2 - 2 snake_case__ = 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 ) snake_case__ = min(_A , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders snake_case__ = pre_process_datasets(_A , _A , _A , *_A ) snake_case__ , snake_case__ = tensor_datasets[0], tensor_datasets[1] snake_case__ = TensorDataset(*_A ) snake_case__ = RandomSampler(_A ) snake_case__ = DataLoader(_A , sampler=_A , batch_size=args.train_batch_size ) snake_case__ = TensorDataset(*_A ) snake_case__ = SequentialSampler(_A ) snake_case__ = DataLoader(_A , sampler=_A , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: snake_case__ = args.max_steps snake_case__ = args.max_steps // (len(_A ) // args.gradient_accumulation_steps) + 1 else: snake_case__ = len(_A ) // args.gradient_accumulation_steps * args.num_train_epochs snake_case__ = list(model.named_parameters() ) snake_case__ = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] snake_case__ = [ { '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}, ] snake_case__ = AdamW(_A , lr=args.learning_rate , eps=args.adam_epsilon ) snake_case__ = get_linear_schedule_with_warmup( _A , num_warmup_steps=args.warmup_steps , num_training_steps=_A ) if args.do_train: snake_case__ , snake_case__ , snake_case__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): snake_case__ = 0 snake_case__ = 0 snake_case__ = tqdm(_A , desc='Training' ) for step, batch in enumerate(_A ): snake_case__ = tuple(t.to(_A ) for t in batch ) snake_case__ , snake_case__ , snake_case__ , snake_case__ = batch snake_case__ = model(_A , mc_token_ids=_A , lm_labels=_A , mc_labels=_A ) snake_case__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() snake_case__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 snake_case__ = 'Training loss: {:.2e} lr: {:.2e}'.format(_A , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer snake_case__ = model.module if hasattr(_A , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` snake_case__ = os.path.join(args.output_dir , _A ) snake_case__ = os.path.join(args.output_dir , _A ) torch.save(model_to_save.state_dict() , _A ) model_to_save.config.to_json_file(_A ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned snake_case__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) snake_case__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(_A ) if args.do_eval: model.eval() snake_case__ , snake_case__ = 0, 0 snake_case__ , snake_case__ = 0, 0 for batch in tqdm(_A , desc='Evaluating' ): snake_case__ = tuple(t.to(_A ) for t in batch ) snake_case__ , snake_case__ , snake_case__ , snake_case__ = batch with torch.no_grad(): snake_case__ , snake_case__ , snake_case__ , snake_case__ = model( _A , mc_token_ids=_A , lm_labels=_A , mc_labels=_A ) snake_case__ = mc_logits.detach().cpu().numpy() snake_case__ = mc_labels.to('cpu' ).numpy() snake_case__ = accuracy(_A , _A ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 snake_case__ = eval_loss / nb_eval_steps snake_case__ = eval_accuracy / nb_eval_examples snake_case__ = tr_loss / nb_tr_steps if args.do_train else None snake_case__ = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} snake_case__ = os.path.join(args.output_dir , 'eval_results.txt' ) with open(_A , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , _A , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
307
import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class __SCREAMING_SNAKE_CASE( ctypes.Structure ): # _fields is a specific attr expected by ctypes _UpperCAmelCase = [("size", ctypes.c_int), ("visible", ctypes.c_byte)] def a_ ( ) -> Any: """simple docstring""" if os.name == "nt": snake_case__ = CursorInfo() snake_case__ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(_A , ctypes.byref(_A ) ) snake_case__ = False ctypes.windll.kernelaa.SetConsoleCursorInfo(_A , ctypes.byref(_A ) ) elif os.name == "posix": sys.stdout.write('\033[?25l' ) sys.stdout.flush() def a_ ( ) -> Tuple: """simple docstring""" if os.name == "nt": snake_case__ = CursorInfo() snake_case__ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(_A , ctypes.byref(_A ) ) snake_case__ = True ctypes.windll.kernelaa.SetConsoleCursorInfo(_A , ctypes.byref(_A ) ) elif os.name == "posix": sys.stdout.write('\033[?25h' ) sys.stdout.flush() @contextmanager def a_ ( ) -> str: """simple docstring""" try: hide_cursor() yield finally: show_cursor()
307
1
import itertools import string from collections.abc import Generator, Iterable def _lowercase ( _UpperCAmelCase , _UpperCAmelCase ) -> Generator[tuple[str, ...], None, None]: lowerCamelCase =iter(__lowerCAmelCase ) while True: lowerCamelCase =tuple(itertools.islice(__lowerCAmelCase , __lowerCAmelCase ) ) if not chunk: return yield chunk def _lowercase ( _UpperCAmelCase ) -> str: lowerCamelCase =''''''.join([c.upper() for c in dirty if c in string.ascii_letters] ) lowerCamelCase ='''''' if len(__lowerCAmelCase ) < 2: return dirty for i in range(len(__lowerCAmelCase ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(__lowerCAmelCase ) & 1: clean += "X" return clean def _lowercase ( _UpperCAmelCase ) -> list[str]: lowerCamelCase ='''ABCDEFGHIKLMNOPQRSTUVWXYZ''' # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler lowerCamelCase =[] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(__lowerCAmelCase ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(__lowerCAmelCase ) return table def _lowercase ( _UpperCAmelCase , _UpperCAmelCase ) -> str: lowerCamelCase =generate_table(__lowerCAmelCase ) lowerCamelCase =prepare_input(__lowerCAmelCase ) lowerCamelCase ='''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(__lowerCAmelCase , 2 ): lowerCamelCase =divmod(table.index(__lowerCAmelCase ) , 5 ) lowerCamelCase =divmod(table.index(__lowerCAmelCase ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def _lowercase ( _UpperCAmelCase , _UpperCAmelCase ) -> str: lowerCamelCase =generate_table(__lowerCAmelCase ) lowerCamelCase ='''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(__lowerCAmelCase , 2 ): lowerCamelCase =divmod(table.index(__lowerCAmelCase ) , 5 ) lowerCamelCase =divmod(table.index(__lowerCAmelCase ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
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
from __future__ import annotations from math import pow, sqrt def __lowerCamelCase ( UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float ): """simple docstring""" if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError('''One and only one argument must be 0''' ) if resistance == 0: return {"resistance": sqrt(pow(UpperCAmelCase_ , 2 ) - pow(UpperCAmelCase_ , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(UpperCAmelCase_ , 2 ) - pow(UpperCAmelCase_ , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(UpperCAmelCase_ , 2 ) + pow(UpperCAmelCase_ , 2 ) )} else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
94
"""simple docstring""" import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed lowerCAmelCase = """true""" def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : int=8_2 , snake_case_ : Optional[Any]=1_6 ) ->Dict: set_seed(4_2 ) lowerCamelCase__ : List[Any] =RegressionModel() lowerCamelCase__ : List[Any] =deepcopy(snake_case_ ) lowerCamelCase__ : List[str] =RegressionDataset(length=snake_case_ ) lowerCamelCase__ : Any =DataLoader(snake_case_ , batch_size=snake_case_ ) model.to(accelerator.device ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =accelerator.prepare(snake_case_ , snake_case_ ) return model, ddp_model, dataloader def lowerCAmelCase_ ( snake_case_ : Accelerator , snake_case_ : str=False ) ->List[str]: lowerCamelCase__ : int =AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) lowerCamelCase__ : List[Any] =load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(snake_case_ : Optional[Any] ): lowerCamelCase__ : Optional[int] =tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=snake_case_ , max_length=snake_case_ ) return outputs with accelerator.main_process_first(): lowerCamelCase__ : Tuple =dataset.map( snake_case_ , batched=snake_case_ , remove_columns=['idx', 'sentence1', 'sentence2'] , ) lowerCamelCase__ : List[Any] =tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(snake_case_ : Union[str, Any] ): if use_longest: return tokenizer.pad(snake_case_ , padding='longest' , return_tensors='pt' ) return tokenizer.pad(snake_case_ , padding='max_length' , max_length=1_2_8 , return_tensors='pt' ) return DataLoader(snake_case_ , shuffle=snake_case_ , collate_fn=snake_case_ , batch_size=1_6 ) def lowerCAmelCase_ ( snake_case_ : List[str] , snake_case_ : Tuple ) ->Any: lowerCamelCase__ : Optional[int] =Accelerator(dispatch_batches=snake_case_ , split_batches=snake_case_ ) lowerCamelCase__ : List[Any] =get_dataloader(snake_case_ , not dispatch_batches ) lowerCamelCase__ : Union[str, Any] =AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Dict =accelerator.prepare(snake_case_ , snake_case_ ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def lowerCAmelCase_ ( snake_case_ : Tuple , snake_case_ : List[Any] , snake_case_ : List[str] ) ->Dict: lowerCamelCase__ : Optional[Any] =[] for batch in dataloader: lowerCamelCase__ , lowerCamelCase__ : int =batch.values() with torch.no_grad(): lowerCamelCase__ : Optional[Any] =model(snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =[], [] for logit, targ in logits_and_targets: logits.append(snake_case_ ) targs.append(snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =torch.cat(snake_case_ ), torch.cat(snake_case_ ) return logits, targs def lowerCAmelCase_ ( snake_case_ : Accelerator , snake_case_ : Optional[int]=8_2 , snake_case_ : Any=False , snake_case_ : List[Any]=False , snake_case_ : Optional[int]=1_6 ) ->List[str]: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict =get_basic_setup(snake_case_ , snake_case_ , snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Any =generate_predictions(snake_case_ , snake_case_ , snake_case_ ) assert ( len(snake_case_ ) == num_samples ), f"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(snake_case_ )}""" def lowerCAmelCase_ ( snake_case_ : bool = False , snake_case_ : bool = False ) ->str: lowerCamelCase__ : Dict =evaluate.load('glue' , 'mrpc' ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =get_mrpc_setup(snake_case_ , snake_case_ ) # First do baseline lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict =setup['no'] model.to(snake_case_ ) model.eval() for batch in dataloader: batch.to(snake_case_ ) with torch.inference_mode(): lowerCamelCase__ : Any =model(**snake_case_ ) lowerCamelCase__ : List[str] =outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=snake_case_ , references=batch['labels'] ) lowerCamelCase__ : Optional[Any] =metric.compute() # Then do distributed lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[int] =setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): lowerCamelCase__ : List[Any] =model(**snake_case_ ) lowerCamelCase__ : str =outputs.logits.argmax(dim=-1 ) lowerCamelCase__ : int =batch['labels'] lowerCamelCase__ , lowerCamelCase__ : List[Any] =accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=snake_case_ , references=snake_case_ ) lowerCamelCase__ : List[str] =metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), f"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def lowerCAmelCase_ ( ) ->str: lowerCamelCase__ : List[str] =Accelerator(split_batches=snake_case_ , dispatch_batches=snake_case_ ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(snake_case_ , snake_case_ ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: lowerCamelCase__ : Dict =Accelerator(split_batches=snake_case_ , dispatch_batches=snake_case_ ) if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(snake_case_ , 9_9 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) lowerCamelCase__ : List[Any] =Accelerator() test_torch_metrics(snake_case_ , 5_1_2 ) accelerator.state._reset_state() def lowerCAmelCase_ ( snake_case_ : List[Any] ) ->Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
126
0
"""simple docstring""" from decimal import Decimal, getcontext from math import ceil, factorial def lowercase__(A ) ->int: """simple docstring""" if not isinstance(__a , __a ): raise TypeError("Undefined for non-integers" ) elif precision < 1: raise ValueError("Undefined for non-natural numbers" ) lowercase__ : Dict= precision lowercase__ : str= ceil(precision / 14 ) lowercase__ : str= 426_880 * Decimal(10_005 ).sqrt() lowercase__ : Tuple= 1 lowercase__ : int= 13_591_409 lowercase__ : Tuple= Decimal(__a ) for k in range(1 , __a ): lowercase__ : List[Any]= factorial(6 * k ) // (factorial(3 * k ) * factorial(__a ) ** 3) linear_term += 545_140_134 exponential_term *= -262_537_412_640_768_000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": a : str = 50 print(F"""The first {n} digits of pi is: {pi(n)}""")
350
"""simple docstring""" from __future__ import annotations from collections.abc import Iterator from typing import Any class __UpperCAmelCase: """simple docstring""" def __init__( self , snake_case__ ): '''simple docstring''' lowercase__ : Any= data lowercase__ : Node | None= None class __UpperCAmelCase: """simple docstring""" def __init__( self ): '''simple docstring''' lowercase__ : List[str]= None lowercase__ : int= None def __iter__( self ): '''simple docstring''' lowercase__ : Any= self.head while self.head: yield node.data lowercase__ : str= node.next if node == self.head: break def __len__( self ): '''simple docstring''' return sum(1 for _ in self ) def __repr__( self ): '''simple docstring''' return "->".join(str(snake_case__ ) for item in iter(self ) ) def UpperCAmelCase_ ( self , snake_case__ ): '''simple docstring''' self.insert_nth(len(self ) , snake_case__ ) def UpperCAmelCase_ ( self , snake_case__ ): '''simple docstring''' self.insert_nth(0 , snake_case__ ) def UpperCAmelCase_ ( self , snake_case__ , snake_case__ ): '''simple docstring''' if index < 0 or index > len(self ): raise IndexError("list index out of range." ) lowercase__ : Optional[Any]= Node(snake_case__ ) if self.head is None: lowercase__ : Tuple= new_node # first node points itself lowercase__ : str= new_node elif index == 0: # insert at head lowercase__ : Dict= self.head lowercase__ : int= new_node else: lowercase__ : Dict= self.head for _ in range(index - 1 ): lowercase__ : Any= temp.next lowercase__ : str= temp.next lowercase__ : Dict= new_node if index == len(self ) - 1: # insert at tail lowercase__ : int= new_node def UpperCAmelCase_ ( self ): '''simple docstring''' return self.delete_nth(0 ) def UpperCAmelCase_ ( self ): '''simple docstring''' return self.delete_nth(len(self ) - 1 ) def UpperCAmelCase_ ( self , snake_case__ = 0 ): '''simple docstring''' if not 0 <= index < len(self ): raise IndexError("list index out of range." ) lowercase__ : int= self.head if self.head == self.tail: # just one node lowercase__ : Union[str, Any]= None elif index == 0: # delete head node lowercase__ : Tuple= self.tail.next.next lowercase__ : Union[str, Any]= self.head.next else: lowercase__ : Union[str, Any]= self.head for _ in range(index - 1 ): lowercase__ : Dict= temp.next lowercase__ : List[str]= temp.next lowercase__ : Optional[Any]= temp.next.next if index == len(self ) - 1: # delete at tail lowercase__ : Optional[Any]= temp return delete_node.data def UpperCAmelCase_ ( self ): '''simple docstring''' return len(self ) == 0 def lowercase__() ->None: """simple docstring""" lowercase__ : Dict= CircularLinkedList() assert len(A ) == 0 assert circular_linked_list.is_empty() is True assert str(A ) == "" try: circular_linked_list.delete_front() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_tail() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_nth(-1 ) raise AssertionError except IndexError: assert True try: circular_linked_list.delete_nth(0 ) raise AssertionError except IndexError: assert True assert circular_linked_list.is_empty() is True for i in range(5 ): assert len(A ) == i circular_linked_list.insert_nth(A , i + 1 ) assert str(A ) == "->".join(str(A ) for i in range(1 , 6 ) ) circular_linked_list.insert_tail(6 ) assert str(A ) == "->".join(str(A ) for i in range(1 , 7 ) ) circular_linked_list.insert_head(0 ) assert str(A ) == "->".join(str(A ) for i in range(0 , 7 ) ) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 assert str(A ) == "->".join(str(A ) for i in range(1 , 6 ) ) assert circular_linked_list.delete_nth(2 ) == 3 circular_linked_list.insert_nth(2 , 3 ) assert str(A ) == "->".join(str(A ) for i in range(1 , 6 ) ) assert circular_linked_list.is_empty() is False if __name__ == "__main__": import doctest doctest.testmod()
150
0
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class lowercase ( unittest.TestCase): def a_ ( self : Union[str, Any] ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def a_ ( self : Optional[Any] ): """simple docstring""" A_ : int = StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) A_ : List[str] = sd_pipe.to(__SCREAMING_SNAKE_CASE ) sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) sd_pipe.set_scheduler('''sample_euler''' ) A_ : Dict = '''A painting of a squirrel eating a burger''' A_ : Tuple = torch.manual_seed(0 ) A_ : int = sd_pipe([prompt] , generator=__SCREAMING_SNAKE_CASE , guidance_scale=9.0 , num_inference_steps=20 , output_type='''np''' ) A_ : Optional[Any] = output.images A_ : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A_ : int = np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def a_ ( self : Tuple ): """simple docstring""" A_ : List[Any] = StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) A_ : Tuple = sd_pipe.to(__SCREAMING_SNAKE_CASE ) sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) sd_pipe.set_scheduler('''sample_euler''' ) A_ : Tuple = '''A painting of a squirrel eating a burger''' A_ : Union[str, Any] = torch.manual_seed(0 ) A_ : Tuple = sd_pipe([prompt] , generator=__SCREAMING_SNAKE_CASE , guidance_scale=9.0 , num_inference_steps=20 , output_type='''np''' ) A_ : Optional[int] = output.images A_ : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A_ : Dict = np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1 def a_ ( self : str ): """simple docstring""" A_ : List[Any] = StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) A_ : Any = sd_pipe.to(__SCREAMING_SNAKE_CASE ) sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) sd_pipe.set_scheduler('''sample_dpmpp_2m''' ) A_ : Tuple = '''A painting of a squirrel eating a burger''' A_ : Optional[int] = torch.manual_seed(0 ) A_ : Dict = sd_pipe( [prompt] , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=15 , output_type='''np''' , use_karras_sigmas=__SCREAMING_SNAKE_CASE , ) A_ : Optional[int] = output.images A_ : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) A_ : Union[str, Any] = np.array( [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
167
"""simple docstring""" import json from typing import Iterator, List, Union from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers from tokenizers.implementations.base_tokenizer import BaseTokenizer from tokenizers.models import Unigram from tokenizers.processors import TemplateProcessing class a ( __snake_case ): def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : str = "▁" , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Union[str, AddedToken] = "<unk>" , __SCREAMING_SNAKE_CASE : Union[str, AddedToken] = "</s>" , __SCREAMING_SNAKE_CASE : Union[str, AddedToken] = "<pad>" , ) -> Optional[int]: lowerCamelCase_ = { 'pad': {'id': 0, 'token': pad_token}, 'eos': {'id': 1, 'token': eos_token}, 'unk': {'id': 2, 'token': unk_token}, } lowerCamelCase_ = [None] * len(self.special_tokens ) for token_dict in self.special_tokens.values(): lowerCamelCase_ = token_dict['token'] lowerCamelCase_ = Tokenizer(Unigram() ) lowerCamelCase_ = normalizers.Sequence( [ normalizers.Nmt(), normalizers.NFKC(), normalizers.Replace(Regex(' {2,}' ) , ' ' ), normalizers.Lowercase(), ] ) lowerCamelCase_ = pre_tokenizers.Sequence( [ pre_tokenizers.Metaspace(replacement=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ), pre_tokenizers.Digits(individual_digits=__SCREAMING_SNAKE_CASE ), pre_tokenizers.Punctuation(), ] ) lowerCamelCase_ = decoders.Metaspace(replacement=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = TemplateProcessing( single=F'''$A {self.special_tokens["eos"]["token"]}''' , special_tokens=[(self.special_tokens['eos']['token'], self.special_tokens['eos']['id'])] , ) lowerCamelCase_ = { 'model': 'SentencePieceUnigram', 'replacement': replacement, 'add_prefix_space': add_prefix_space, } super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : Union[str, List[str]] , __SCREAMING_SNAKE_CASE : int = 8000 , __SCREAMING_SNAKE_CASE : bool = True , ) -> Tuple: lowerCamelCase_ = trainers.UnigramTrainer( vocab_size=__SCREAMING_SNAKE_CASE , special_tokens=self.special_tokens_list , show_progress=__SCREAMING_SNAKE_CASE , ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCamelCase_ = [files] self._tokenizer.train(__SCREAMING_SNAKE_CASE , trainer=__SCREAMING_SNAKE_CASE ) self.add_unk_id() def UpperCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : Union[Iterator[str], Iterator[Iterator[str]]] , __SCREAMING_SNAKE_CASE : int = 8000 , __SCREAMING_SNAKE_CASE : bool = True , ) -> str: lowerCamelCase_ = trainers.UnigramTrainer( vocab_size=__SCREAMING_SNAKE_CASE , special_tokens=self.special_tokens_list , show_progress=__SCREAMING_SNAKE_CASE , ) self._tokenizer.train_from_iterator(__SCREAMING_SNAKE_CASE , trainer=__SCREAMING_SNAKE_CASE ) self.add_unk_id() def UpperCamelCase ( self : List[Any] ) -> Dict: lowerCamelCase_ = json.loads(self._tokenizer.to_str() ) lowerCamelCase_ = self.special_tokens['unk']['id'] lowerCamelCase_ = Tokenizer.from_str(json.dumps(__SCREAMING_SNAKE_CASE ) )
183
0
import os import sys lowerCamelCase_ : Tuple = os.path.join(os.path.dirname(__file__), """src""") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowerCamelCase_ : Dict = [ """torch""", """numpy""", """tokenizers""", """filelock""", """requests""", """tqdm""", """regex""", """sentencepiece""", """sacremoses""", """importlib_metadata""", """huggingface_hub""", ] @add_start_docstrings(AutoConfig.__doc__ ) def lowerCAmelCase( *__lowerCamelCase , **__lowerCamelCase ): return AutoConfig.from_pretrained(*__lowerCamelCase , **__lowerCamelCase ) @add_start_docstrings(AutoTokenizer.__doc__ ) def lowerCAmelCase( *__lowerCamelCase , **__lowerCamelCase ): return AutoTokenizer.from_pretrained(*__lowerCamelCase , **__lowerCamelCase ) @add_start_docstrings(AutoModel.__doc__ ) def lowerCAmelCase( *__lowerCamelCase , **__lowerCamelCase ): return AutoModel.from_pretrained(*__lowerCamelCase , **__lowerCamelCase ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def lowerCAmelCase( *__lowerCamelCase , **__lowerCamelCase ): return AutoModelForCausalLM.from_pretrained(*__lowerCamelCase , **__lowerCamelCase ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def lowerCAmelCase( *__lowerCamelCase , **__lowerCamelCase ): return AutoModelForMaskedLM.from_pretrained(*__lowerCamelCase , **__lowerCamelCase ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def lowerCAmelCase( *__lowerCamelCase , **__lowerCamelCase ): return AutoModelForSequenceClassification.from_pretrained(*__lowerCamelCase , **__lowerCamelCase ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def lowerCAmelCase( *__lowerCamelCase , **__lowerCamelCase ): return AutoModelForQuestionAnswering.from_pretrained(*__lowerCamelCase , **__lowerCamelCase )
197
from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from transformers.modeling_outputs import BaseModelOutput from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING lowerCamelCase_ : Dict = logging.get_logger(__name__) @add_end_docstrings(__snake_case ) class a__ ( __snake_case ): def __init__( self , **UpperCAmelCase ) -> List[str]: super().__init__(**UpperCAmelCase ) if self.framework == "tf": raise ValueError(f'''The {self.__class__} is only available in PyTorch.''' ) requires_backends(self , 'vision' ) self.check_model_type(UpperCAmelCase ) def __call__( self , UpperCAmelCase , UpperCAmelCase = None , **UpperCAmelCase , ) -> Tuple: if "text_queries" in kwargs: __a = kwargs.pop('text_queries' ) if isinstance(UpperCAmelCase , (str, Image.Image) ): __a = {'image': image, 'candidate_labels': candidate_labels} else: __a = image __a = super().__call__(UpperCAmelCase , **UpperCAmelCase ) return results def __SCREAMING_SNAKE_CASE ( self , **UpperCAmelCase ) -> List[str]: __a = {} if "threshold" in kwargs: __a = kwargs['threshold'] if "top_k" in kwargs: __a = kwargs['top_k'] return {}, {}, postprocess_params def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> Union[str, Any]: __a = load_image(inputs['image'] ) __a = inputs['candidate_labels'] if isinstance(UpperCAmelCase , UpperCAmelCase ): __a = candidate_labels.split(',' ) __a = torch.tensor([[image.height, image.width]] , dtype=torch.intaa ) for i, candidate_label in enumerate(UpperCAmelCase ): __a = self.tokenizer(UpperCAmelCase , return_tensors=self.framework ) __a = self.image_processor(UpperCAmelCase , return_tensors=self.framework ) yield { "is_last": i == len(UpperCAmelCase ) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> str: __a = model_inputs.pop('target_size' ) __a = model_inputs.pop('candidate_label' ) __a = model_inputs.pop('is_last' ) __a = self.model(**UpperCAmelCase ) __a = {'target_size': target_size, 'candidate_label': candidate_label, 'is_last': is_last, **outputs} return model_outputs def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase , UpperCAmelCase=0.1 , UpperCAmelCase=None ) -> Tuple: __a = [] for model_output in model_outputs: __a = model_output['candidate_label'] __a = BaseModelOutput(UpperCAmelCase ) __a = self.image_processor.post_process_object_detection( outputs=UpperCAmelCase , threshold=UpperCAmelCase , target_sizes=model_output['target_size'] )[0] for index in outputs["scores"].nonzero(): __a = outputs['scores'][index].item() __a = self._get_bounding_box(outputs['boxes'][index][0] ) __a = {'score': score, 'label': label, 'box': box} results.append(UpperCAmelCase ) __a = sorted(UpperCAmelCase , key=lambda UpperCAmelCase : x["score"] , reverse=UpperCAmelCase ) if top_k: __a = results[:top_k] return results def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> Dict[str, int]: if self.framework != "pt": raise ValueError('The ZeroShotObjectDetectionPipeline is only available in PyTorch.' ) __a , __a , __a , __a = box.int().tolist() __a = { 'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax, } return bbox
197
1
from sklearn.metrics import mean_squared_error import datasets _lowerCAmelCase : str = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" _lowerCAmelCase : List[Any] = "\\nMean Squared Error(MSE) is the average of the square of difference between the predicted\nand actual values.\n" _lowerCAmelCase : Optional[Any] = "\nArgs:\n predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Estimated target values.\n references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Ground truth (correct) target values.\n sample_weight: array-like of shape (n_samples,), default=None\n Sample weights.\n multioutput: {\"raw_values\", \"uniform_average\"} or array-like of shape (n_outputs,), default=\"uniform_average\"\n Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n \"raw_values\" : Returns a full set of errors in case of multioutput input.\n\n \"uniform_average\" : Errors of all outputs are averaged with uniform weight.\n\n squared : bool, default=True\n If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.\n\nReturns:\n mse : mean squared error.\nExamples:\n\n >>> mse_metric = datasets.load_metric(\"mse\")\n >>> predictions = [2.5, 0.0, 2, 8]\n >>> references = [3, -0.5, 2, 7]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'mse': 0.375}\n >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)\n >>> print(rmse_result)\n {'mse': 0.6123724356957945}\n\n If you're using multi-dimensional lists, then set the config as follows :\n\n >>> mse_metric = datasets.load_metric(\"mse\", \"multilist\")\n >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n >>> references = [[0, 2], [-1, 2], [8, -5]]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'mse': 0.7083333333333334}\n >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput='raw_values')\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {'mse': array([0.41666667, 1. ])}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _UpperCamelCase ( datasets.Metric ): def UpperCAmelCase_ ( self :Union[str, Any] ) -> Any: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html" ] , ) def UpperCAmelCase_ ( self :Union[str, Any] ) -> int: if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value("float" ) ), "references": datasets.Sequence(datasets.Value("float" ) ), } else: return { "predictions": datasets.Value("float" ), "references": datasets.Value("float" ), } def UpperCAmelCase_ ( self :List[str] , lowerCamelCase :Union[str, Any] , lowerCamelCase :str , lowerCamelCase :Tuple=None , lowerCamelCase :str="uniform_average" , lowerCamelCase :int=True ) -> int: UpperCAmelCase__ = mean_squared_error( lowerCamelCase , lowerCamelCase , sample_weight=lowerCamelCase , multioutput=lowerCamelCase , squared=lowerCamelCase ) return {"mse": mse}
169
def lowerCAmelCase ( _lowerCAmelCase : list[int] ): """simple docstring""" if not numbers: return 0 if not isinstance(_lowerCAmelCase , (list, tuple) ) or not all( isinstance(_lowerCAmelCase , _lowerCAmelCase ) for number in numbers ): raise ValueError("numbers must be an iterable of integers" ) UpperCAmelCase__ = UpperCAmelCase__ = UpperCAmelCase__ = numbers[0] for i in range(1 , len(_lowerCAmelCase ) ): # update the maximum and minimum subarray products UpperCAmelCase__ = numbers[i] if number < 0: UpperCAmelCase__ , UpperCAmelCase__ = min_till_now, max_till_now UpperCAmelCase__ = max(_lowerCAmelCase , max_till_now * number ) UpperCAmelCase__ = min(_lowerCAmelCase , min_till_now * number ) # update the maximum product found till now UpperCAmelCase__ = max(_lowerCAmelCase , _lowerCAmelCase ) return max_prod
169
1
import enum import shutil import sys SCREAMING_SNAKE_CASE : str = shutil.get_terminal_size() SCREAMING_SNAKE_CASE : int = {"UP": "A", "DOWN": "B", "RIGHT": "C", "LEFT": "D"} class UpperCamelCase ( enum.Enum ): '''simple docstring''' lowercase : Dict =0 lowercase : List[Any] =1 def UpperCamelCase ( _a , _a="" ) -> str: '''simple docstring''' sys.stdout.write(str(_a ) + end ) sys.stdout.flush() def UpperCamelCase ( _a , _a , _a="" ) -> List[str]: '''simple docstring''' forceWrite(f"\u001b[{color}m{content}\u001b[0m" , _a ) def UpperCamelCase ( ) -> Optional[int]: '''simple docstring''' forceWrite('''\r''' ) def UpperCamelCase ( _a , _a ) -> Dict: '''simple docstring''' forceWrite(f"\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}" ) def UpperCamelCase ( ) -> int: '''simple docstring''' forceWrite(''' ''' * TERMINAL_WIDTH ) reset_cursor() def UpperCamelCase ( ) -> Union[str, Any]: '''simple docstring''' reset_cursor() forceWrite('''-''' * TERMINAL_WIDTH )
350
from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class UpperCamelCase : '''simple docstring''' lowercase : Any =PegasusConfig lowercase : Any ={} lowercase : int ="""gelu""" def __init__( self , UpperCamelCase_ , UpperCamelCase_=13 , UpperCamelCase_=7 , UpperCamelCase_=True , UpperCamelCase_=False , UpperCamelCase_=99 , UpperCamelCase_=32 , UpperCamelCase_=2 , UpperCamelCase_=4 , UpperCamelCase_=37 , UpperCamelCase_=0.1 , UpperCamelCase_=0.1 , UpperCamelCase_=40 , UpperCamelCase_=2 , UpperCamelCase_=1 , UpperCamelCase_=0 , ): lowercase_ :Union[str, Any] = parent lowercase_ :Tuple = batch_size lowercase_ :Optional[Any] = seq_length lowercase_ :Any = is_training lowercase_ :Optional[int] = use_labels lowercase_ :Optional[int] = vocab_size lowercase_ :Optional[Any] = hidden_size lowercase_ :List[Any] = num_hidden_layers lowercase_ :Tuple = num_attention_heads lowercase_ :Optional[Any] = intermediate_size lowercase_ :List[Any] = hidden_dropout_prob lowercase_ :Optional[Any] = attention_probs_dropout_prob lowercase_ :Any = max_position_embeddings lowercase_ :Any = eos_token_id lowercase_ :List[str] = pad_token_id lowercase_ :Optional[Any] = bos_token_id def UpperCamelCase ( self ): lowercase_ :Any = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) lowercase_ :Tuple = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) lowercase_ :int = tf.concat([input_ids, eos_tensor] , axis=1 ) lowercase_ :List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase_ :List[Any] = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) lowercase_ :Optional[int] = prepare_pegasus_inputs_dict(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) return config, inputs_dict def UpperCamelCase ( self , UpperCamelCase_ , UpperCamelCase_ ): lowercase_ :Optional[Any] = TFPegasusModel(config=UpperCamelCase_ ).get_decoder() lowercase_ :Tuple = inputs_dict['''input_ids'''] lowercase_ :Optional[int] = input_ids[:1, :] lowercase_ :Tuple = inputs_dict['''attention_mask'''][:1, :] lowercase_ :List[str] = inputs_dict['''head_mask'''] lowercase_ :int = 1 # first forward pass lowercase_ :Tuple = model(UpperCamelCase_ , attention_mask=UpperCamelCase_ , head_mask=UpperCamelCase_ , use_cache=UpperCamelCase_ ) lowercase_ , lowercase_ :Dict = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids lowercase_ :List[str] = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowercase_ :Optional[Any] = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and lowercase_ :Optional[int] = tf.concat([input_ids, next_tokens] , axis=-1 ) lowercase_ :Optional[int] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) lowercase_ :Optional[Any] = model(UpperCamelCase_ , attention_mask=UpperCamelCase_ )[0] lowercase_ :str = model(UpperCamelCase_ , attention_mask=UpperCamelCase_ , past_key_values=UpperCamelCase_ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice lowercase_ :Optional[Any] = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) lowercase_ :Optional[int] = output_from_no_past[:, -3:, random_slice_idx] lowercase_ :Any = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(UpperCamelCase_ , UpperCamelCase_ , rtol=1E-3 ) def UpperCamelCase ( _a , _a , _a , _a=None , _a=None , _a=None , _a=None , _a=None , ) -> Optional[int]: '''simple docstring''' if attention_mask is None: lowercase_ :Dict = tf.cast(tf.math.not_equal(_a , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: lowercase_ :Dict = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: lowercase_ :List[str] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: lowercase_ :Optional[Any] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: lowercase_ :Any = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class UpperCamelCase ( lowercase__ , lowercase__ , unittest.TestCase ): '''simple docstring''' lowercase : Tuple =(TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () lowercase : List[str] =(TFPegasusForConditionalGeneration,) if is_tf_available() else () lowercase : str =( { """conversational""": TFPegasusForConditionalGeneration, """feature-extraction""": TFPegasusModel, """summarization""": TFPegasusForConditionalGeneration, """text2text-generation""": TFPegasusForConditionalGeneration, """translation""": TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) lowercase : Optional[int] =True lowercase : List[str] =False lowercase : Union[str, Any] =False def UpperCamelCase ( self ): lowercase_ :Dict = TFPegasusModelTester(self ) lowercase_ :str = ConfigTester(self , config_class=UpperCamelCase_ ) def UpperCamelCase ( self ): self.config_tester.run_common_tests() def UpperCamelCase ( self ): lowercase_ :Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase_ ) @require_sentencepiece @require_tokenizers @require_tf class UpperCamelCase ( unittest.TestCase ): '''simple docstring''' lowercase : Tuple =[ """ PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.""", """ The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning 'Oh I think you're nominated'\", said Dappy.\"And I was like 'Oh yeah, which one?' And now we've got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it's best of us to come down and mingle with everyone and say hello to the cameras. And now we find we've got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn't be too disappointed if they didn't win this time around.\"At the end of the day we're grateful to be where we are in our careers.\"If it don't happen then it don't happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers' All These Things That I've Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year's Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" """, ] lowercase : Optional[int] =[ """California's largest electricity provider has cut power to hundreds of thousands of customers in an effort to""" """ reduce the risk of wildfires.""", """N-Dubz have revealed they\'re \"grateful\" to have been nominated for four Mobo Awards.""", ] # differs slightly from pytorch, likely due to numerical differences in linear layers lowercase : Optional[Any] ="""google/pegasus-xsum""" @cached_property def UpperCamelCase ( self ): return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCamelCase ( self ): lowercase_ :Union[str, Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def UpperCamelCase ( self , **UpperCamelCase_ ): lowercase_ :Any = self.translate_src_text(**UpperCamelCase_ ) assert self.expected_text == generated_words def UpperCamelCase ( self , **UpperCamelCase_ ): lowercase_ :Dict = self.tokenizer(self.src_text , **UpperCamelCase_ , padding=UpperCamelCase_ , return_tensors='''tf''' ) lowercase_ :int = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=UpperCamelCase_ , ) lowercase_ :int = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=UpperCamelCase_ ) return generated_words @slow def UpperCamelCase ( self ): self._assert_generated_batch_equal_expected()
252
0
import argparse import os from accelerate.test_utils import execute_subprocess_async def SCREAMING_SNAKE_CASE ( _UpperCAmelCase=None ) -> List[Any]: if subparsers is not None: lowerCamelCase__ : str = subparsers.add_parser('test' ) else: lowerCamelCase__ : 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 SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> Any: lowerCamelCase__ : List[Any] = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['test_utils', 'scripts', 'test_script.py'] ) if args.config_file is None: lowerCamelCase__ : str = script_name else: lowerCamelCase__ : List[Any] = F"""--config_file={args.config_file} {script_name}""" lowerCamelCase__ : Optional[Any] = ['accelerate-launch'] + test_args.split() lowerCamelCase__ : Union[str, Any] = 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 SCREAMING_SNAKE_CASE ( ) -> int: lowerCamelCase__ : Any = test_command_parser() lowerCamelCase__ : Union[str, Any] = parser.parse_args() test_command(__A ) if __name__ == "__main__": main()
50
'''simple docstring''' from ....utils import logging a__ : Optional[Any] = logging.get_logger(__name__) class lowercase_ ( a__ ): def __init__( self , a , a=None , a=20_48 ): UpperCamelCase__ = config.__dict__ UpperCamelCase__ = modal_hidden_size if num_labels: UpperCamelCase__ = num_labels
80
0
'''simple docstring''' import unittest import torch from torch import nn from accelerate.test_utils import require_cuda from accelerate.utils.memory import find_executable_batch_size, release_memory def a_ ( ): raise RuntimeError('CUDA out of memory.' ) class UpperCAmelCase_ ( nn.Module ): def __init__( self : Union[str, Any] ) -> Optional[int]: super().__init__() lowerCAmelCase = nn.Linear(3 , 4 ) lowerCAmelCase = nn.BatchNormad(4 ) lowerCAmelCase = nn.Linear(4 , 5 ) def __UpperCAmelCase ( self : List[str] , UpperCAmelCase__ : Optional[int] ) -> Tuple: return self.lineara(self.batchnorm(self.lineara(UpperCAmelCase__ ) ) ) class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : Optional[Any] ) -> int: lowerCAmelCase = [] @find_executable_batch_size(starting_batch_size=1_2_8 ) def mock_training_loop_function(UpperCAmelCase__ : Any ): nonlocal batch_sizes batch_sizes.append(UpperCAmelCase__ ) if batch_size != 8: raise_fake_out_of_memory() mock_training_loop_function() self.assertListEqual(UpperCAmelCase__ , [1_2_8, 6_4, 3_2, 1_6, 8] ) def __UpperCAmelCase ( self : Dict ) -> Optional[Any]: lowerCAmelCase = [] @find_executable_batch_size(starting_batch_size=1_2_8 ) def mock_training_loop_function(UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Any ): nonlocal batch_sizes batch_sizes.append(UpperCAmelCase__ ) if batch_size != 8: raise_fake_out_of_memory() return batch_size, arga lowerCAmelCase , lowerCAmelCase = mock_training_loop_function('hello' ) self.assertListEqual(UpperCAmelCase__ , [1_2_8, 6_4, 3_2, 1_6, 8] ) self.assertListEqual([bs, arga] , [8, 'hello'] ) def __UpperCAmelCase ( self : List[str] ) -> Tuple: @find_executable_batch_size(starting_batch_size=0 ) def mock_training_loop_function(UpperCAmelCase__ : Optional[Any] ): pass with self.assertRaises(UpperCAmelCase__ ) as cm: mock_training_loop_function() self.assertIn('No executable batch size found, reached zero.' , cm.exception.args[0] ) def __UpperCAmelCase ( self : Optional[int] ) -> Optional[Any]: @find_executable_batch_size(starting_batch_size=1_6 ) def mock_training_loop_function(UpperCAmelCase__ : Union[str, Any] ): if batch_size > 0: raise_fake_out_of_memory() pass with self.assertRaises(UpperCAmelCase__ ) as cm: mock_training_loop_function() self.assertIn('No executable batch size found, reached zero.' , cm.exception.args[0] ) def __UpperCAmelCase ( self : Any ) -> str: @find_executable_batch_size(starting_batch_size=1_2_8 ) def mock_training_loop_function(UpperCAmelCase__ : Dict , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[str] ): if batch_size != 8: raise raise_fake_out_of_memory() with self.assertRaises(UpperCAmelCase__ ) as cm: mock_training_loop_function(1_2_8 , 'hello' , 'world' ) self.assertIn('Batch size was passed into `f`' , cm.exception.args[0] ) self.assertIn('`f(arg1=\'hello\', arg2=\'world\')' , cm.exception.args[0] ) def __UpperCAmelCase ( self : int ) -> List[str]: @find_executable_batch_size(starting_batch_size=1_6 ) def mock_training_loop_function(UpperCAmelCase__ : Optional[int] ): raise ValueError('Oops, we had an error!' ) with self.assertRaises(UpperCAmelCase__ ) as cm: mock_training_loop_function() self.assertIn('Oops, we had an error!' , cm.exception.args[0] ) @require_cuda def __UpperCAmelCase ( self : Tuple ) -> Dict: lowerCAmelCase = torch.cuda.memory_allocated() lowerCAmelCase = ModelForTest() model.cuda() self.assertGreater(torch.cuda.memory_allocated() , UpperCAmelCase__ ) lowerCAmelCase = release_memory(UpperCAmelCase__ ) self.assertEqual(torch.cuda.memory_allocated() , UpperCAmelCase__ )
354
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __snake_case ={ """configuration_swiftformer""": [ """SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SwiftFormerConfig""", """SwiftFormerOnnxConfig""", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =[ """SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """SwiftFormerForImageClassification""", """SwiftFormerModel""", """SwiftFormerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_swiftformer import ( SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SwiftFormerConfig, SwiftFormerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swiftformer import ( SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, SwiftFormerForImageClassification, SwiftFormerModel, SwiftFormerPreTrainedModel, ) else: import sys __snake_case =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
55
0
"""simple docstring""" lowerCAmelCase_ = { 0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f', } def __UpperCAmelCase ( __lowerCamelCase ) -> str: assert type(snake_case__ ) in (int, float) and decimal == int(snake_case__ ) lowercase__ : Dict = int(snake_case__ ) lowercase__ : Optional[Any] = '' lowercase__ : Optional[Any] = False if decimal < 0: lowercase__ : List[Any] = True decimal *= -1 while decimal > 0: lowercase__ : Dict = divmod(snake_case__ , 16 ) lowercase__ : Dict = values[remainder] + hexadecimal lowercase__ : str = '0x' + hexadecimal if negative: lowercase__ : List[Any] = '-' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
16
import inspect import unittest from transformers import ConvNextConfig 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_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowerCAmelCase_ : def __init__( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=13, SCREAMING_SNAKE_CASE_=32, SCREAMING_SNAKE_CASE_=3, SCREAMING_SNAKE_CASE_=4, SCREAMING_SNAKE_CASE_=[10, 20, 30, 40], SCREAMING_SNAKE_CASE_=[2, 2, 3, 2], SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=37, SCREAMING_SNAKE_CASE_="gelu", SCREAMING_SNAKE_CASE_=10, SCREAMING_SNAKE_CASE_=0.02, SCREAMING_SNAKE_CASE_=["stage2", "stage3", "stage4"], SCREAMING_SNAKE_CASE_=[2, 3, 4], SCREAMING_SNAKE_CASE_=None, ) -> Optional[int]: UpperCamelCase : Dict = parent UpperCamelCase : Optional[Any] = batch_size UpperCamelCase : Union[str, Any] = image_size UpperCamelCase : Union[str, Any] = num_channels UpperCamelCase : List[Any] = num_stages UpperCamelCase : Any = hidden_sizes UpperCamelCase : Optional[int] = depths UpperCamelCase : Optional[int] = is_training UpperCamelCase : List[str] = use_labels UpperCamelCase : Dict = intermediate_size UpperCamelCase : List[Any] = hidden_act UpperCamelCase : Union[str, Any] = num_labels UpperCamelCase : str = initializer_range UpperCamelCase : List[str] = out_features UpperCamelCase : List[str] = out_indices UpperCamelCase : str = scope def snake_case_ ( self ) -> Optional[Any]: UpperCamelCase : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase : Any = None if self.use_labels: UpperCamelCase : List[Any] = ids_tensor([self.batch_size], self.num_labels ) UpperCamelCase : List[Any] = self.get_config() return config, pixel_values, labels def snake_case_ ( self ) -> Any: return ConvNextConfig( num_channels=self.num_channels, hidden_sizes=self.hidden_sizes, depths=self.depths, num_stages=self.num_stages, hidden_act=self.hidden_act, is_decoder=SCREAMING_SNAKE_CASE_, initializer_range=self.initializer_range, out_features=self.out_features, out_indices=self.out_indices, num_labels=self.num_labels, ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Tuple: UpperCamelCase : Dict = ConvNextModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() UpperCamelCase : str = model(SCREAMING_SNAKE_CASE_ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape, (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32), ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCamelCase : Optional[int] = ConvNextForImageClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() UpperCamelCase : Dict = model(SCREAMING_SNAKE_CASE_, labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels) ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Dict: UpperCamelCase : int = ConvNextBackbone(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() UpperCamelCase : Any = model(SCREAMING_SNAKE_CASE_ ) # verify hidden states self.parent.assertEqual(len(result.feature_maps ), len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ), [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ), len(config.out_features ) ) self.parent.assertListEqual(model.channels, config.hidden_sizes[1:] ) # verify backbone works with out_features=None UpperCamelCase : Any = None UpperCamelCase : List[str] = ConvNextBackbone(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() UpperCamelCase : Tuple = model(SCREAMING_SNAKE_CASE_ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ), 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ), [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ), 1 ) self.parent.assertListEqual(model.channels, [config.hidden_sizes[-1]] ) def snake_case_ ( self ) -> int: UpperCamelCase : Optional[int] = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase : Optional[Any] = config_and_inputs UpperCamelCase : str = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class lowerCAmelCase_ ( a__ , a__ , unittest.TestCase ): UpperCAmelCase__ : List[Any] = ( ( ConvNextModel, ConvNextForImageClassification, ConvNextBackbone, ) if is_torch_available() else () ) UpperCAmelCase__ : Union[str, Any] = ( {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Dict = True UpperCAmelCase__ : List[str] = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : List[Any] = False UpperCAmelCase__ : Tuple = False def snake_case_ ( self ) -> Optional[Any]: UpperCamelCase : Dict = ConvNextModelTester(self ) UpperCamelCase : int = ConfigTester(self, config_class=SCREAMING_SNAKE_CASE_, has_text_modality=SCREAMING_SNAKE_CASE_, hidden_size=37 ) def snake_case_ ( self ) -> List[str]: self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def snake_case_ ( self ) -> Union[str, Any]: return @unittest.skip(reason='ConvNext does not use inputs_embeds' ) def snake_case_ ( self ) -> List[str]: pass @unittest.skip(reason='ConvNext does not support input and output embeddings' ) def snake_case_ ( self ) -> str: pass @unittest.skip(reason='ConvNext does not use feedforward chunking' ) def snake_case_ ( self ) -> str: pass def snake_case_ ( self ) -> Any: UpperCamelCase , UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Optional[Any] = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase : List[Any] = [*signature.parameters.keys()] UpperCamelCase : Union[str, Any] = ['pixel_values'] self.assertListEqual(arg_names[:1], SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ) -> List[str]: UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ) -> Dict: UpperCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ) -> Dict: def check_hidden_states_output(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ): UpperCamelCase : List[Any] = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() with torch.no_grad(): UpperCamelCase : List[Any] = model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : List[str] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states UpperCamelCase : int = self.model_tester.num_stages self.assertEqual(len(SCREAMING_SNAKE_CASE_ ), expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ), [self.model_tester.image_size // 4, self.model_tester.image_size // 4], ) UpperCamelCase , UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Optional[int] = True check_hidden_states_output(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCamelCase : str = True check_hidden_states_output(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ) -> List[str]: UpperCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_ ) @slow def snake_case_ ( self ) -> Tuple: for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : Union[str, Any] = ConvNextModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def UpperCamelCase ( ) -> int: UpperCamelCase : Optional[int] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class lowerCAmelCase_ ( unittest.TestCase ): @cached_property def snake_case_ ( self ) -> Optional[int]: return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224' ) if is_vision_available() else None @slow def snake_case_ ( self ) -> str: UpperCamelCase : Dict = ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224' ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = self.default_image_processor UpperCamelCase : Any = prepare_img() UpperCamelCase : List[str] = image_processor(images=SCREAMING_SNAKE_CASE_, return_tensors='pt' ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): UpperCamelCase : Optional[Any] = model(**SCREAMING_SNAKE_CASE_ ) # verify the logits UpperCamelCase : List[Any] = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape, SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = torch.tensor([-0.02_60, -0.47_39, 0.19_11] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3], SCREAMING_SNAKE_CASE_, atol=1e-4 ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase , a__ ): UpperCAmelCase__ : Tuple = (ConvNextBackbone,) if is_torch_available() else () UpperCAmelCase__ : List[str] = ConvNextConfig UpperCAmelCase__ : Tuple = False def snake_case_ ( self ) -> int: UpperCamelCase : List[Any] = ConvNextModelTester(self )
119
0
import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class lowercase__ ( unittest.TestCase ): def A_ ( self : Optional[int] ): debug_launcher(test_script.main ) def A_ ( self : int ): debug_launcher(test_ops.main )
365
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=_UpperCAmelCase ) class lowercase__ ( _UpperCAmelCase ): A__ : str =field(default="""audio-classification""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) A__ : ClassVar[Features] =Features({"""audio""": Audio()} ) A__ : ClassVar[Features] =Features({"""labels""": ClassLabel} ) A__ : str ="audio" A__ : str ="labels" def A_ ( self : List[Any] , UpperCAmelCase_ : Optional[Any] ): 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] , UpperCAmelCase_ ): raise ValueError(F'Column {self.label_column} is not a ClassLabel.' ) SCREAMING_SNAKE_CASE__ = copy.deepcopy(self ) SCREAMING_SNAKE_CASE__ = self.label_schema.copy() SCREAMING_SNAKE_CASE__ = features[self.label_column] SCREAMING_SNAKE_CASE__ = label_schema return task_template @property def A_ ( self : Union[str, Any] ): return { self.audio_column: "audio", self.label_column: "labels", }
169
0
import json import os from typing import Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { 'vocab_file': 'vocab.json', 'tokenizer_config_file': 'tokenizer_config.json', 'merges_file': 'merges.txt', } lowerCAmelCase__ = { 'vocab_file': { 'facebook/s2t-wav2vec2-large-en-de': ( 'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/vocab.json' ), }, 'tokenizer_config_file': { 'facebook/s2t-wav2vec2-large-en-de': ( 'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/tokenizer_config.json' ), }, 'merges_file': { 'facebook/s2t-wav2vec2-large-en-de': ( 'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/merges.txt' ), }, } lowerCAmelCase__ = '</w>' lowerCAmelCase__ = '@@ ' def _UpperCAmelCase (UpperCamelCase__ : Optional[Any] ): _A : Optional[int] = set() _A : Optional[Any] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _A : List[Any] = char return pairs # Speech2Text2 has no max input length lowerCAmelCase__ = {'facebook/s2t-wav2vec2-large-en-de': 10_24} class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE = ["input_ids", "attention_mask"] def __init__( self , __lowerCamelCase , __lowerCamelCase="<s>" , __lowerCamelCase="<pad>" , __lowerCamelCase="</s>" , __lowerCamelCase="<unk>" , __lowerCamelCase=False , __lowerCamelCase=None , **__lowerCamelCase , ) -> Optional[Any]: super().__init__( unk_token=__lowerCamelCase , bos_token=__lowerCamelCase , eos_token=__lowerCamelCase , pad_token=__lowerCamelCase , do_lower_case=__lowerCamelCase , **__lowerCamelCase , ) _A : Dict = do_lower_case with open(__lowerCamelCase , encoding="utf-8") as vocab_handle: _A : Optional[int] = json.load(__lowerCamelCase) _A : Optional[Any] = {v: k for k, v in self.encoder.items()} if merges_file is None: logger.info(F"No merges files provided. {self.__class__.__name__} can only be used for decoding.") _A : Optional[Any] = None _A : Tuple = None else: with open(__lowerCamelCase , encoding="utf-8") as merges_handle: _A : Optional[int] = merges_handle.read().split("\n")[:-1] _A : Union[str, Any] = [tuple(merge.split()[:2]) for merge in merges] _A : Optional[int] = dict(zip(__lowerCamelCase , range(len(__lowerCamelCase)))) _A : List[Any] = {} @property def _lowerCamelCase ( self) -> int: return len(self.decoder) def _lowerCamelCase ( self) -> Dict: return dict(self.encoder , **self.added_tokens_encoder) def _lowerCamelCase ( self , __lowerCamelCase) -> Dict: _A : Tuple = tuple(token[:-1]) + (token[-1] + BPE_TOKEN_MERGES,) if token in self.cache: return self.cache[token] _A : int = get_pairs(__lowerCamelCase) if not pairs: return token while True: _A : Any = min(__lowerCamelCase , key=lambda __lowerCamelCase: self.bpe_ranks.get(__lowerCamelCase , float("inf"))) if bigram not in self.bpe_ranks: break _A , _A : Optional[int] = bigram _A : int = [] _A : str = 0 while i < len(__lowerCamelCase): try: _A : str = word.index(__lowerCamelCase , __lowerCamelCase) except ValueError: new_word.extend(word[i:]) break else: new_word.extend(word[i:j]) _A : str = j if word[i] == first and i < len(__lowerCamelCase) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 _A : List[str] = tuple(__lowerCamelCase) _A : List[str] = new_word if len(__lowerCamelCase) == 1: break else: _A : List[Any] = get_pairs(__lowerCamelCase) _A : Tuple = " ".join(__lowerCamelCase) if word == "\n " + BPE_TOKEN_MERGES: _A : List[str] = "\n" + BPE_TOKEN_MERGES if word.endswith(__lowerCamelCase): _A : int = word.replace(__lowerCamelCase , "") _A : int = word.replace(" " , __lowerCamelCase) _A : Union[str, Any] = word return word def _lowerCamelCase ( self , __lowerCamelCase) -> Optional[Any]: if self.bpe_ranks is None: raise ValueError( "This tokenizer was instantiated without a `merges.txt` file, so" " that it can only be used for decoding, not for encoding." "Make sure to provide `merges.txt` file at instantiation to enable " "encoding.") if self.do_lower_case: _A : List[Any] = text.lower() _A : Optional[int] = text.split() _A : List[str] = [] for token in text: if token: split_tokens.extend(list(self.bpe(__lowerCamelCase).split(" "))) return split_tokens def _lowerCamelCase ( self , __lowerCamelCase) -> int: return self.encoder.get(__lowerCamelCase , self.encoder.get(self.unk_token)) def _lowerCamelCase ( self , __lowerCamelCase) -> str: _A : List[str] = self.decoder.get(__lowerCamelCase , self.unk_token) return result def _lowerCamelCase ( self , __lowerCamelCase) -> str: _A : str = " ".join(__lowerCamelCase) # make sure @@ tokens are concatenated _A : int = "".join(string.split(__lowerCamelCase)) return string def _lowerCamelCase ( self , __lowerCamelCase , __lowerCamelCase = None) -> Tuple[str]: if not os.path.isdir(__lowerCamelCase): logger.error(F"Vocabulary path ({save_directory}) should be a directory") return _A : Any = os.path.join( __lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) _A : Any = os.path.join( __lowerCamelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]) with open(__lowerCamelCase , "w" , encoding="utf-8") as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=__lowerCamelCase , ensure_ascii=__lowerCamelCase) + "\n") _A : Union[str, Any] = 0 if self.bpe_ranks is None: return (vocab_file,) with open(__lowerCamelCase , "w" , encoding="utf-8") as writer: for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda __lowerCamelCase: kv[1]): if index != token_index: logger.warning( F"Saving vocabulary to {merges_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!") _A : Optional[int] = token_index writer.write(" ".join(__lowerCamelCase) + "\n") index += 1 return (vocab_file, merges_file)
11
"""simple docstring""" import argparse import os import re import packaging.version A : Any = "examples/" A : Optional[Any] = { "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"), "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"), "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","), "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"), } A : Optional[int] = { "init": "src/transformers/__init__.py", "setup": "setup.py", } A : List[Any] = "README.md" def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' with open(_UpperCamelCase , "r" , encoding="utf-8" , newline="\n" ) as f: __lowerCAmelCase = f.read() __lowerCAmelCase , __lowerCAmelCase = REPLACE_PATTERNS[pattern] __lowerCAmelCase = replace.replace("VERSION" , _UpperCamelCase ) __lowerCAmelCase = re_pattern.sub(_UpperCamelCase , _UpperCamelCase ) with open(_UpperCamelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(_UpperCamelCase ) def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' for folder, directories, fnames in os.walk(_UpperCamelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("research_projects" ) if "legacy" in directories: directories.remove("legacy" ) for fname in fnames: if fname.endswith(".py" ): update_version_in_file(os.path.join(_UpperCamelCase , _UpperCamelCase ) , _UpperCamelCase , pattern="examples" ) def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase=False ): '''simple docstring''' for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if not patch: update_version_in_examples(_UpperCamelCase ) def _lowerCamelCase ( ): '''simple docstring''' __lowerCAmelCase = "🤗 Transformers currently provides the following architectures" __lowerCAmelCase = "1. Want to contribute a new model?" with open(_UpperCamelCase , "r" , encoding="utf-8" , newline="\n" ) as f: __lowerCAmelCase = f.readlines() # Find the start of the list. __lowerCAmelCase = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __lowerCAmelCase = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): __lowerCAmelCase = lines[index].replace( "https://huggingface.co/docs/transformers/main/model_doc" , "https://huggingface.co/docs/transformers/model_doc" , ) index += 1 with open(_UpperCamelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(_UpperCamelCase ) def _lowerCamelCase ( ): '''simple docstring''' with open(REPLACE_FILES["init"] , "r" ) as f: __lowerCAmelCase = f.read() __lowerCAmelCase = REPLACE_PATTERNS["init"][0].search(_UpperCamelCase ).groups()[0] return packaging.version.parse(_UpperCamelCase ) def _lowerCamelCase ( _UpperCamelCase=False ): '''simple docstring''' __lowerCAmelCase = get_version() if patch and default_version.is_devrelease: raise ValueError("Can't create a patch version from the dev branch, checkout a released version!" ) if default_version.is_devrelease: __lowerCAmelCase = default_version.base_version elif patch: __lowerCAmelCase = f"{default_version.major}.{default_version.minor}.{default_version.micro + 1}" else: __lowerCAmelCase = f"{default_version.major}.{default_version.minor + 1}.0" # Now let's ask nicely if that's the right one. __lowerCAmelCase = input(f"Which version are you releasing? [{default_version}]" ) if len(_UpperCamelCase ) == 0: __lowerCAmelCase = default_version print(f"Updating version to {version}." ) global_version_update(_UpperCamelCase , patch=_UpperCamelCase ) if not patch: print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() def _lowerCamelCase ( ): '''simple docstring''' __lowerCAmelCase = get_version() __lowerCAmelCase = f"{current_version.major}.{current_version.minor + 1}.0.dev0" __lowerCAmelCase = current_version.base_version # Check with the user we got that right. __lowerCAmelCase = input(f"Which version are we developing now? [{dev_version}]" ) if len(_UpperCamelCase ) == 0: __lowerCAmelCase = dev_version print(f"Updating version to {version}." ) global_version_update(_UpperCamelCase ) print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() if __name__ == "__main__": A : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") A : Dict = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
57
0
"""simple docstring""" import torch from diffusers import DiffusionPipeline class lowercase( __a ): '''simple docstring''' def __init__( self: Dict, a_: Tuple, a_: List[Any] ): '''simple docstring''' super().__init__() self.register_modules(unet=a_, scheduler=a_ ) def __call__( self: Optional[Any] ): '''simple docstring''' _snake_case : List[str] = torch.randn( (1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size), ) _snake_case : int = 1 _snake_case : Dict = self.unet(a_, a_ ).sample _snake_case : List[Any] = self.scheduler.step(a_, a_, a_ ).prev_sample _snake_case : str = scheduler_output - scheduler_output + torch.ones_like(a_ ) return result
132
"""simple docstring""" import unicodedata from dataclasses import dataclass from typing import Optional, Union import numpy as np from transformers.data.data_collator import DataCollatorMixin from transformers.file_utils import PaddingStrategy from transformers.tokenization_utils_base import PreTrainedTokenizerBase def UpperCAmelCase__ (snake_case__ : Optional[int] , snake_case__ : Any , snake_case__ : List[str] , snake_case__ : int ): """simple docstring""" if isinstance(snake_case__ , snake_case__ ): _snake_case : List[Any] = np.full((len(snake_case__ ), sequence_length, 2) , snake_case__ ) else: _snake_case : Any = np.full((len(snake_case__ ), sequence_length) , snake_case__ ) for i, tensor in enumerate(snake_case__ ): if padding_side == "right": if isinstance(snake_case__ , snake_case__ ): _snake_case : Dict = tensor[:sequence_length] else: _snake_case : List[Any] = tensor[:sequence_length] else: if isinstance(snake_case__ , snake_case__ ): _snake_case : str = tensor[:sequence_length] else: _snake_case : Tuple = tensor[:sequence_length] return out_tensor.tolist() def UpperCAmelCase__ (snake_case__ : Optional[int] ): """simple docstring""" _snake_case : str = ord(snake_case__ ) if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 1_23 and cp <= 1_26): return True _snake_case : Union[str, Any] = unicodedata.category(snake_case__ ) if cat.startswith("""P""" ): return True return False @dataclass class lowercase( __a ): '''simple docstring''' lowercase__ = 42 lowercase__ = True lowercase__ = None lowercase__ = None lowercase__ = -1_00 lowercase__ = "pt" def UpperCamelCase_ ( self: Any, a_: Union[str, Any] ): '''simple docstring''' import torch _snake_case : Optional[Any] = """label""" if """label""" in features[0].keys() else """labels""" _snake_case : str = [feature[label_name] for feature in features] if label_name in features[0].keys() else None _snake_case : Any = self.tokenizer.pad( a_, padding=self.padding, max_length=self.max_length, pad_to_multiple_of=self.pad_to_multiple_of, return_tensors="""pt""" if labels is None else None, ) if labels is None: return batch _snake_case : Optional[int] = torch.tensor(batch["""entity_ids"""] ).shape[1] _snake_case : Any = self.tokenizer.padding_side if padding_side == "right": _snake_case : Optional[int] = [ list(a_ ) + [self.label_pad_token_id] * (sequence_length - len(a_ )) for label in labels ] else: _snake_case : Union[str, Any] = [ [self.label_pad_token_id] * (sequence_length - len(a_ )) + list(a_ ) for label in labels ] _snake_case : List[Any] = [feature["""ner_tags"""] for feature in features] _snake_case : str = padding_tensor(a_, -1, a_, a_ ) _snake_case : Any = [feature["""original_entity_spans"""] for feature in features] _snake_case : int = padding_tensor(a_, (-1, -1), a_, a_ ) _snake_case : str = {k: torch.tensor(a_, dtype=torch.intaa ) for k, v in batch.items()} return batch
132
1
import math from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->UnCLIP class lowercase ( A__ ): lowercase_ : Tuple =42 lowercase_ : Optional[int] =None def UpperCamelCase ( lowerCAmelCase__ , lowerCAmelCase__=0.9_99 , lowerCAmelCase__="cosine" , ): '''simple docstring''' if alpha_transform_type == "cosine": def alpha_bar_fn(lowerCAmelCase__ ): return math.cos((t + 0.0_08) / 1.0_08 * 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}' ) lowercase = [] for i in range(_lowerCAmelCase ): lowercase = i / num_diffusion_timesteps lowercase = (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 lowercase ( A__ , A__ ): @register_to_config def __init__( self ,A__ = 1_0_0_0 ,A__ = "fixed_small_log" ,A__ = True ,A__ = 1.0 ,A__ = "epsilon" ,A__ = "squaredcos_cap_v2" ,): if beta_schedule != "squaredcos_cap_v2": raise ValueError('''UnCLIPScheduler only supports `beta_schedule`: \'squaredcos_cap_v2\'''') lowercase = betas_for_alpha_bar(__snake_case) lowercase = 1.0 - self.betas lowercase = torch.cumprod(self.alphas ,dim=0) lowercase = torch.tensor(1.0) # standard deviation of the initial noise distribution lowercase = 1.0 # setable values lowercase = None lowercase = torch.from_numpy(np.arange(0 ,__snake_case)[::-1].copy()) lowercase = variance_type def A__ ( self ,A__ ,A__ = None): return sample def A__ ( self ,A__ ,A__ = None): lowercase = num_inference_steps lowercase = (self.config.num_train_timesteps - 1) / (self.num_inference_steps - 1) lowercase = (np.arange(0 ,__snake_case) * step_ratio).round()[::-1].copy().astype(np.intaa) lowercase = torch.from_numpy(__snake_case).to(__snake_case) def A__ ( self ,A__ ,A__=None ,A__=None ,A__=None): if prev_timestep is None: lowercase = t - 1 lowercase = self.alphas_cumprod[t] lowercase = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one lowercase = 1 - alpha_prod_t lowercase = 1 - alpha_prod_t_prev if prev_timestep == t - 1: lowercase = self.betas[t] else: lowercase = 1 - alpha_prod_t / alpha_prod_t_prev # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowercase = beta_prod_t_prev / beta_prod_t * beta if variance_type is None: lowercase = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small_log": lowercase = torch.log(torch.clamp(__snake_case ,min=1E-20)) lowercase = torch.exp(0.5 * variance) elif variance_type == "learned_range": # NOTE difference with DDPM scheduler lowercase = variance.log() lowercase = beta.log() lowercase = (predicted_variance + 1) / 2 lowercase = frac * max_log + (1 - frac) * min_log return variance def A__ ( self ,A__ ,A__ ,A__ ,A__ = None ,A__=None ,A__ = True ,): lowercase = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type == "learned_range": lowercase = torch.split(__snake_case ,sample.shape[1] ,dim=1) else: lowercase = None # 1. compute alphas, betas if prev_timestep is None: lowercase = t - 1 lowercase = self.alphas_cumprod[t] lowercase = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one lowercase = 1 - alpha_prod_t lowercase = 1 - alpha_prod_t_prev if prev_timestep == t - 1: lowercase = self.betas[t] lowercase = self.alphas[t] else: lowercase = 1 - alpha_prod_t / alpha_prod_t_prev lowercase = 1 - beta # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowercase = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowercase = model_output else: raise ValueError( f'prediction_type given as {self.config.prediction_type} must be one of `epsilon` or `sample`' ''' for the UnCLIPScheduler.''') # 3. Clip "predicted x_0" if self.config.clip_sample: lowercase = torch.clamp( __snake_case ,-self.config.clip_sample_range ,self.config.clip_sample_range) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowercase = (alpha_prod_t_prev ** 0.5 * beta) / beta_prod_t lowercase = alpha ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowercase = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise lowercase = 0 if t > 0: lowercase = randn_tensor( model_output.shape ,dtype=model_output.dtype ,generator=__snake_case ,device=model_output.device) lowercase = self._get_variance( __snake_case ,predicted_variance=__snake_case ,prev_timestep=__snake_case ,) if self.variance_type == "fixed_small_log": lowercase = variance elif self.variance_type == "learned_range": lowercase = (0.5 * variance).exp() else: raise ValueError( f'variance_type given as {self.variance_type} must be one of `fixed_small_log` or `learned_range`' ''' for the UnCLIPScheduler.''') lowercase = variance * variance_noise lowercase = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return UnCLIPSchedulerOutput(prev_sample=__snake_case ,pred_original_sample=__snake_case) def A__ ( self ,A__ ,A__ ,A__ ,): # Make sure alphas_cumprod and timestep have same device and dtype as original_samples lowercase = self.alphas_cumprod.to(device=original_samples.device ,dtype=original_samples.dtype) lowercase = timesteps.to(original_samples.device) lowercase = alphas_cumprod[timesteps] ** 0.5 lowercase = sqrt_alpha_prod.flatten() while len(sqrt_alpha_prod.shape) < len(original_samples.shape): lowercase = sqrt_alpha_prod.unsqueeze(-1) lowercase = (1 - alphas_cumprod[timesteps]) ** 0.5 lowercase = sqrt_one_minus_alpha_prod.flatten() while len(sqrt_one_minus_alpha_prod.shape) < len(original_samples.shape): lowercase = sqrt_one_minus_alpha_prod.unsqueeze(-1) lowercase = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples
101
'''simple docstring''' import unittest from transformers import MraConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class SCREAMING_SNAKE_CASE: """simple docstring""" def __init__( self : Optional[int] , __snake_case : str , __snake_case : Union[str, Any]=2 , __snake_case : Optional[int]=8 , __snake_case : Any=True , __snake_case : Union[str, Any]=True , __snake_case : Dict=True , __snake_case : int=True , __snake_case : List[Any]=99 , __snake_case : str=16 , __snake_case : Tuple=5 , __snake_case : Tuple=2 , __snake_case : str=36 , __snake_case : Dict="gelu" , __snake_case : str=0.0 , __snake_case : Optional[int]=0.0 , __snake_case : Optional[int]=512 , __snake_case : Optional[Any]=16 , __snake_case : int=2 , __snake_case : int=0.02 , __snake_case : str=3 , __snake_case : Dict=4 , __snake_case : str=None , ) -> Optional[int]: UpperCAmelCase : Optional[int] = parent UpperCAmelCase : Tuple = batch_size UpperCAmelCase : List[str] = seq_length UpperCAmelCase : List[Any] = is_training UpperCAmelCase : int = use_input_mask UpperCAmelCase : Any = use_token_type_ids UpperCAmelCase : str = use_labels UpperCAmelCase : Union[str, Any] = vocab_size UpperCAmelCase : List[str] = hidden_size UpperCAmelCase : Optional[Any] = num_hidden_layers UpperCAmelCase : Union[str, Any] = num_attention_heads UpperCAmelCase : Optional[Any] = intermediate_size UpperCAmelCase : Union[str, Any] = hidden_act UpperCAmelCase : int = hidden_dropout_prob UpperCAmelCase : Optional[int] = attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] = max_position_embeddings UpperCAmelCase : str = type_vocab_size UpperCAmelCase : List[str] = type_sequence_label_size UpperCAmelCase : Tuple = initializer_range UpperCAmelCase : Optional[Any] = num_labels UpperCAmelCase : Optional[int] = num_choices UpperCAmelCase : Any = scope def A ( self : Tuple ) -> Union[str, Any]: UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[int] = None if self.use_input_mask: UpperCAmelCase : Dict = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : Dict = None if self.use_token_type_ids: UpperCAmelCase : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase : str = None UpperCAmelCase : Tuple = None UpperCAmelCase : int = None if self.use_labels: UpperCAmelCase : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase : str = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase : List[str] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def A ( self : int ) -> Tuple: return MraConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__snake_case , initializer_range=self.initializer_range , ) def A ( self : Optional[Any] ) -> Any: UpperCAmelCase : Optional[Any] = self.get_config() UpperCAmelCase : int = 300 return config def A ( self : Optional[Any] ) -> Any: ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) : Tuple = self.prepare_config_and_inputs() UpperCAmelCase : Dict = True UpperCAmelCase : Tuple = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def A ( self : Union[str, Any] , __snake_case : Union[str, Any] , __snake_case : int , __snake_case : Optional[int] , __snake_case : int , __snake_case : Dict , __snake_case : Tuple , __snake_case : Optional[Any] ) -> List[str]: UpperCAmelCase : int = MraModel(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : Tuple = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case ) UpperCAmelCase : Optional[int] = model(__snake_case , token_type_ids=__snake_case ) UpperCAmelCase : Dict = model(__snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self : Tuple , __snake_case : Optional[Any] , __snake_case : List[str] , __snake_case : List[str] , __snake_case : int , __snake_case : Union[str, Any] , __snake_case : Optional[Any] , __snake_case : Any , __snake_case : List[Any] , __snake_case : Optional[Any] , ) -> Tuple: UpperCAmelCase : str = True UpperCAmelCase : Tuple = MraModel(__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : Optional[int] = model( __snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , encoder_hidden_states=__snake_case , encoder_attention_mask=__snake_case , ) UpperCAmelCase : Optional[Any] = model( __snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , encoder_hidden_states=__snake_case , ) UpperCAmelCase : str = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self : Tuple , __snake_case : str , __snake_case : Optional[Any] , __snake_case : List[str] , __snake_case : Tuple , __snake_case : Optional[Any] , __snake_case : List[str] , __snake_case : int ) -> Any: UpperCAmelCase : Dict = MraForMaskedLM(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : Optional[int] = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A ( self : Tuple , __snake_case : Tuple , __snake_case : Dict , __snake_case : Dict , __snake_case : Any , __snake_case : int , __snake_case : Optional[Any] , __snake_case : Tuple ) -> Optional[int]: UpperCAmelCase : List[str] = MraForQuestionAnswering(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : List[Any] = model( __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 A ( self : str , __snake_case : Optional[int] , __snake_case : List[str] , __snake_case : str , __snake_case : int , __snake_case : Optional[Any] , __snake_case : Union[str, Any] , __snake_case : List[Any] ) -> int: UpperCAmelCase : int = self.num_labels UpperCAmelCase : Union[str, Any] = MraForSequenceClassification(__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : List[str] = model(__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 A ( self : str , __snake_case : Dict , __snake_case : Optional[Any] , __snake_case : Dict , __snake_case : Union[str, Any] , __snake_case : Union[str, Any] , __snake_case : Union[str, Any] , __snake_case : Dict ) -> int: UpperCAmelCase : Tuple = self.num_labels UpperCAmelCase : List[str] = MraForTokenClassification(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : str = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def A ( self : str , __snake_case : int , __snake_case : Any , __snake_case : Tuple , __snake_case : Optional[int] , __snake_case : List[str] , __snake_case : str , __snake_case : Union[str, Any] ) -> Optional[Any]: UpperCAmelCase : Tuple = self.num_choices UpperCAmelCase : int = MraForMultipleChoice(config=__snake_case ) model.to(__snake_case ) model.eval() UpperCAmelCase : str = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase : List[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase : List[str] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase : List[str] = model( __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_choices) ) def A ( self : str ) -> Dict: UpperCAmelCase : Any = self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) : List[str] = config_and_inputs UpperCAmelCase : Any = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE( A__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = () def A ( self : int ) -> Union[str, Any]: UpperCAmelCase : List[str] = MraModelTester(self ) UpperCAmelCase : Optional[int] = ConfigTester(self , config_class=__snake_case , hidden_size=37 ) def A ( self : Optional[Any] ) -> str: self.config_tester.run_common_tests() def A ( self : Tuple ) -> Optional[Any]: UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__snake_case ) def A ( self : List[Any] ) -> Optional[Any]: UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCAmelCase : List[Any] = type self.model_tester.create_and_check_model(*__snake_case ) def A ( self : Tuple ) -> Dict: UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__snake_case ) def A ( self : Tuple ) -> List[str]: UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__snake_case ) def A ( self : int ) -> Dict: UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__snake_case ) def A ( self : Dict ) -> Optional[int]: UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__snake_case ) def A ( self : Any ) -> Optional[int]: UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__snake_case ) @slow def A ( self : Dict ) -> Any: for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase : str = MraModel.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) @unittest.skip(reason='''MRA does not output attentions''' ) def A ( self : str ) -> Optional[Any]: return @require_torch class SCREAMING_SNAKE_CASE( unittest.TestCase ): """simple docstring""" @slow def A ( self : Tuple ) -> List[Any]: UpperCAmelCase : int = MraModel.from_pretrained('''uw-madison/mra-base-512-4''' ) UpperCAmelCase : Optional[Any] = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): UpperCAmelCase : List[Any] = model(__snake_case )[0] UpperCAmelCase : Optional[Any] = torch.Size((1, 256, 768) ) self.assertEqual(output.shape , __snake_case ) UpperCAmelCase : Any = torch.tensor( [[[-0.01_40, 0.08_30, -0.03_81], [0.15_46, 0.14_02, 0.02_20], [0.11_62, 0.08_51, 0.01_65]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __snake_case , atol=1E-4 ) ) @slow def A ( self : Optional[Any] ) -> Any: UpperCAmelCase : Optional[int] = MraForMaskedLM.from_pretrained('''uw-madison/mra-base-512-4''' ) UpperCAmelCase : Dict = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): UpperCAmelCase : List[Any] = model(__snake_case )[0] UpperCAmelCase : int = 50265 UpperCAmelCase : int = torch.Size((1, 256, vocab_size) ) self.assertEqual(output.shape , __snake_case ) UpperCAmelCase : Union[str, Any] = torch.tensor( [[[9.25_95, -3.60_38, 11.88_19], [9.38_69, -3.26_93, 11.09_56], [11.85_24, -3.49_38, 13.12_10]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __snake_case , atol=1E-4 ) ) @slow def A ( self : str ) -> List[Any]: UpperCAmelCase : List[Any] = MraForMaskedLM.from_pretrained('''uw-madison/mra-base-4096-8-d3''' ) UpperCAmelCase : List[Any] = torch.arange(4096 ).unsqueeze(0 ) with torch.no_grad(): UpperCAmelCase : Tuple = model(__snake_case )[0] UpperCAmelCase : Optional[int] = 50265 UpperCAmelCase : Tuple = torch.Size((1, 4096, vocab_size) ) self.assertEqual(output.shape , __snake_case ) UpperCAmelCase : Optional[int] = torch.tensor( [[[5.47_89, -2.35_64, 7.50_64], [7.90_67, -1.33_69, 9.96_68], [9.07_12, -1.81_06, 7.03_80]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __snake_case , atol=1E-4 ) )
23
0
import inspect import unittest from transformers import RegNetConfig from transformers.file_utils import cached_property, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import RegNetForImageClassification, RegNetModel from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _a : '''simple docstring''' def __init__( self , A__ , A__=3 , A__=32 , A__=3 , A__=10 , A__=[10, 20, 30, 40] , A__=[1, 1, 2, 1] , A__=True , A__=True , A__="relu" , A__=3 , A__=None , ): A__ : Optional[int] = parent A__ : int = batch_size A__ : Dict = image_size A__ : Dict = num_channels A__ : Any = embeddings_size A__ : Optional[Any] = hidden_sizes A__ : List[str] = depths A__ : List[Any] = is_training A__ : Optional[Any] = use_labels A__ : int = hidden_act A__ : int = num_labels A__ : Tuple = scope A__ : Optional[Any] = len(A__ ) def __A ( self ): A__ : List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A__ : Optional[Any] = None if self.use_labels: A__ : Union[str, Any] = ids_tensor([self.batch_size] , self.num_labels ) A__ : Optional[Any] = self.get_config() return config, pixel_values, labels def __A ( self ): return RegNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , ) def __A ( self , A__ , A__ , A__ ): A__ : str = RegNetModel(config=A__ ) model.to(A__ ) model.eval() A__ : Any = model(A__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def __A ( self , A__ , A__ , A__ ): A__ : int = self.num_labels A__ : int = RegNetForImageClassification(A__ ) model.to(A__ ) model.eval() A__ : Tuple = model(A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self ): A__ : Any = self.prepare_config_and_inputs() A__ , A__ , A__ : int = config_and_inputs A__ : Any = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class _a (__magic_name__ , __magic_name__ , unittest.TestCase ): '''simple docstring''' UpperCAmelCase__: Tuple = (RegNetModel, RegNetForImageClassification) if is_torch_available() else () UpperCAmelCase__: List[Any] = ( {'''feature-extraction''': RegNetModel, '''image-classification''': RegNetForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__: Any = False UpperCAmelCase__: Tuple = False UpperCAmelCase__: Optional[Any] = False UpperCAmelCase__: str = False def __A ( self ): A__ : int = RegNetModelTester(self ) A__ : List[str] = ConfigTester(self , config_class=A__ , has_text_modality=A__ ) def __A ( self ): 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 ): return @unittest.skip(reason="""RegNet does not use inputs_embeds""" ) def __A ( self ): pass @unittest.skip(reason="""RegNet does not support input and output embeddings""" ) def __A ( self ): pass def __A ( self ): A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[Any] = model_class(A__ ) A__ : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ : Tuple = [*signature.parameters.keys()] A__ : List[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , A__ ) def __A ( self ): A__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def __A ( self ): A__ , A__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ : List[Any] = model_class(config=A__ ) for name, module in model.named_modules(): if isinstance(A__ , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) def __A ( self ): def check_hidden_states_output(A__ , A__ , A__ ): A__ : int = model_class(A__ ) model.to(A__ ) model.eval() with torch.no_grad(): A__ : Tuple = model(**self._prepare_for_class(A__ , A__ ) ) A__ : Any = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states A__ : Optional[Any] = self.model_tester.num_stages self.assertEqual(len(A__ ) , expected_num_stages + 1 ) # RegNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 2, self.model_tester.image_size // 2] , ) A__ , A__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() A__ : List[str] = ["""basic""", """bottleneck"""] for model_class in self.all_model_classes: for layer_type in layers_type: A__ : str = layer_type A__ : Dict = True check_hidden_states_output(A__ , A__ , A__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ : Union[str, Any] = True check_hidden_states_output(A__ , A__ , A__ ) def __A ( self ): A__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A__ ) @slow def __A ( self ): for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ : Tuple = RegNetModel.from_pretrained(A__ ) self.assertIsNotNone(A__ ) def UpperCamelCase () -> Optional[Any]: A__ : int = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class _a (unittest.TestCase ): '''simple docstring''' @cached_property def __A ( self ): return ( AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __A ( self ): A__ : Optional[Any] = RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(A__ ) A__ : Union[str, Any] = self.default_image_processor A__ : List[str] = prepare_img() A__ : Tuple = image_processor(images=A__ , return_tensors="""pt""" ).to(A__ ) # forward pass with torch.no_grad(): A__ : Tuple = model(**A__ ) # verify the logits A__ : Optional[Any] = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , A__ ) A__ : Optional[int] = torch.tensor([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6] ).to(A__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , A__ , atol=1e-4 ) )
141
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) A_ : Optional[int] = {'configuration_fnet': ['FNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FNetConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Optional[Any] = ['FNetTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Tuple = ['FNetTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : List[Any] = [ 'FNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'FNetForMaskedLM', 'FNetForMultipleChoice', 'FNetForNextSentencePrediction', 'FNetForPreTraining', 'FNetForQuestionAnswering', 'FNetForSequenceClassification', 'FNetForTokenClassification', 'FNetLayer', 'FNetModel', 'FNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys A_ : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
141
1
"""simple docstring""" from ..utils import DummyObject, requires_backends class _UpperCAmelCase ( metaclass=UpperCAmelCase_): _lowerCAmelCase : Dict = ['torch', 'scipy'] def __init__( self : List[Any] , *lowercase_ : List[str] , **lowercase_ : List[Any] ): requires_backends(self , ['''torch''', '''scipy'''] ) @classmethod def _snake_case ( cls : Union[str, Any] , *lowercase_ : Any , **lowercase_ : Any ): requires_backends(cls , ['''torch''', '''scipy'''] ) @classmethod def _snake_case ( cls : List[Any] , *lowercase_ : Optional[Any] , **lowercase_ : Optional[Any] ): requires_backends(cls , ['''torch''', '''scipy'''] )
264
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 lowerCAmelCase_ ( __A, __A=False ) -> Any: '''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(__A ) 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 UpperCamelCase__ = parse_flag_from_env('RUN_SLOW', default=False) UpperCamelCase__ = parse_flag_from_env('RUN_REMOTE', default=False) UpperCamelCase__ = parse_flag_from_env('RUN_LOCAL', default=True) UpperCamelCase__ = parse_flag_from_env('RUN_PACKAGED', default=True) # Compression UpperCamelCase__ = pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='test requires lz4') UpperCamelCase__ = pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='test requires py7zr') UpperCamelCase__ = pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='test requires zstandard') # Audio UpperCamelCase__ = 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 UpperCamelCase__ = 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 UpperCamelCase__ = pytest.mark.skipif( config.DILL_VERSION <= version.parse('0.3.2'), reason='test requires dill>0.3.2 for cloudpickle compatibility', ) # Windows UpperCamelCase__ = pytest.mark.skipif( sys.platform == 'win32', reason='test should not be run on Windows', ) def lowerCAmelCase_ ( __A ) -> Any: '''simple docstring''' try: import faiss # noqa except ImportError: UpperCAmelCase__ = unittest.skip("test requires faiss" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> Optional[Any]: '''simple docstring''' try: import regex # noqa except ImportError: UpperCAmelCase__ = unittest.skip("test requires regex" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> List[str]: '''simple docstring''' try: import elasticsearch # noqa except ImportError: UpperCAmelCase__ = unittest.skip("test requires elasticsearch" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> List[Any]: '''simple docstring''' try: import sqlalchemy # noqa except ImportError: UpperCAmelCase__ = unittest.skip("test requires sqlalchemy" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> List[str]: '''simple docstring''' if not config.TORCH_AVAILABLE: UpperCAmelCase__ = unittest.skip("test requires PyTorch" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> Union[str, Any]: '''simple docstring''' if not config.TF_AVAILABLE: UpperCAmelCase__ = unittest.skip("test requires TensorFlow" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> Any: '''simple docstring''' if not config.JAX_AVAILABLE: UpperCAmelCase__ = unittest.skip("test requires JAX" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> int: '''simple docstring''' if not config.PIL_AVAILABLE: UpperCAmelCase__ = unittest.skip("test requires Pillow" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> Tuple: '''simple docstring''' try: import transformers # noqa F401 except ImportError: return unittest.skip("test requires transformers" )(__A ) else: return test_case def lowerCAmelCase_ ( __A ) -> Dict: '''simple docstring''' try: import tiktoken # noqa F401 except ImportError: return unittest.skip("test requires tiktoken" )(__A ) else: return test_case def lowerCAmelCase_ ( __A ) -> Optional[Any]: '''simple docstring''' try: import spacy # noqa F401 except ImportError: return unittest.skip("test requires spacy" )(__A ) else: return test_case def lowerCAmelCase_ ( __A ) -> Optional[int]: '''simple docstring''' def _require_spacy_model(__A ): try: import spacy # noqa F401 spacy.load(__A ) except ImportError: return unittest.skip("test requires spacy" )(__A ) except OSError: return unittest.skip("test requires spacy model '{}'".format(__A ) )(__A ) else: return test_case return _require_spacy_model def lowerCAmelCase_ ( __A ) -> Optional[Any]: '''simple docstring''' try: import pyspark # noqa F401 except ImportError: return unittest.skip("test requires pyspark" )(__A ) else: return test_case def lowerCAmelCase_ ( __A ) -> Tuple: '''simple docstring''' try: import joblibspark # noqa F401 except ImportError: return unittest.skip("test requires joblibspark" )(__A ) else: return test_case def lowerCAmelCase_ ( __A ) -> Optional[int]: '''simple docstring''' if not _run_slow_tests or _run_slow_tests == 0: UpperCAmelCase__ = unittest.skip("test is slow" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> List[Any]: '''simple docstring''' if not _run_local_tests or _run_local_tests == 0: UpperCAmelCase__ = unittest.skip("test is local" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> Optional[Any]: '''simple docstring''' if not _run_packaged_tests or _run_packaged_tests == 0: UpperCAmelCase__ = unittest.skip("test is packaged" )(__A ) return test_case def lowerCAmelCase_ ( __A ) -> Any: '''simple docstring''' if not _run_remote_tests or _run_remote_tests == 0: UpperCAmelCase__ = unittest.skip("test requires remote" )(__A ) return test_case def lowerCAmelCase_ ( *__A ) -> Optional[int]: '''simple docstring''' def decorate(cls ): for name, fn in cls.__dict__.items(): if callable(__A ) and name.startswith("test" ): for decorator in decorators: UpperCAmelCase__ = decorator(__A ) setattr(cls, __A, __A ) return cls return decorate class A ( UpperCAmelCase_ ): pass class A ( UpperCAmelCase_ ): __UpperCAmelCase : Union[str, Any] = 0 __UpperCAmelCase : str = 1 __UpperCAmelCase : int = 2 @contextmanager def lowerCAmelCase_ ( __A=OfflineSimulationMode.CONNECTION_FAILS, __A=1e-16 ) -> List[str]: '''simple docstring''' UpperCAmelCase__ = requests.Session().request def timeout_request(__A, __A, __A, **__A ): # 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(__A, __A, **__A ) 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(__A, __A, **__A ): raise requests.ConnectionError("Offline mode is enabled.", request=__A ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch("requests.Session.send", __A ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch("requests.Session.request", __A ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch("datasets.config.HF_DATASETS_OFFLINE", __A ): yield else: raise ValueError("Please use a value from the OfflineSimulationMode enum." ) @contextmanager def lowerCAmelCase_ ( *__A, **__A ) -> str: '''simple docstring''' UpperCAmelCase__ = str(Path().resolve() ) with tempfile.TemporaryDirectory(*__A, **__A ) as tmp_dir: try: os.chdir(__A ) yield finally: os.chdir(__A ) @contextmanager def lowerCAmelCase_ ( ) -> 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 lowerCAmelCase_ ( ) -> List[str]: '''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 lowerCAmelCase_ ( __A, __A ) -> List[str]: '''simple docstring''' return deepcopy(__A ).integers(0, 100, 10 ).tolist() == deepcopy(__A ).integers(0, 100, 10 ).tolist() def lowerCAmelCase_ ( __A ) -> Optional[int]: '''simple docstring''' import decorator from requests.exceptions import HTTPError def _wrapper(__A, *__A, **__A ): try: return func(*__A, **__A ) except HTTPError as err: if str(__A ).startswith("500" ) or str(__A ).startswith("502" ): pytest.xfail(str(__A ) ) raise err return decorator.decorator(_wrapper, __A ) class A : def __init__(self : Optional[Any] , __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : List[str] ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase__ = returncode UpperCAmelCase__ = stdout UpperCAmelCase__ = stderr async def lowerCAmelCase_ ( __A, __A ) -> Optional[int]: '''simple docstring''' while True: UpperCAmelCase__ = await stream.readline() if line: callback(__A ) else: break async def lowerCAmelCase_ ( __A, __A=None, __A=None, __A=None, __A=False, __A=False ) -> _RunOutput: '''simple docstring''' if echo: print("\nRunning: ", " ".join(__A ) ) UpperCAmelCase__ = await asyncio.create_subprocess_exec( cmd[0], *cmd[1:], stdin=__A, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=__A, ) # 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(__A, __A, __A, __A="" ): UpperCAmelCase__ = line.decode("utf-8" ).rstrip() sink.append(__A ) if not quiet: print(__A, __A, file=__A ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout, lambda __A : tee(__A, __A, sys.stdout, label="stdout:" ) ), _read_stream(p.stderr, lambda __A : tee(__A, __A, sys.stderr, label="stderr:" ) ), ], timeout=__A, ) return _RunOutput(await p.wait(), __A, __A ) def lowerCAmelCase_ ( __A, __A=None, __A=None, __A=180, __A=False, __A=True ) -> _RunOutput: '''simple docstring''' UpperCAmelCase__ = asyncio.get_event_loop() UpperCAmelCase__ = loop.run_until_complete( _stream_subprocess(__A, env=__A, stdin=__A, timeout=__A, quiet=__A, echo=__A ) ) UpperCAmelCase__ = " ".join(__A ) 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 lowerCAmelCase_ ( ) -> Tuple: '''simple docstring''' UpperCAmelCase__ = os.environ.get("PYTEST_XDIST_WORKER", "gw0" ) UpperCAmelCase__ = re.sub(r"^gw", "", __A, 0, re.M ) return int(__A ) def lowerCAmelCase_ ( ) -> List[Any]: '''simple docstring''' UpperCAmelCase__ = 29_500 UpperCAmelCase__ = pytest_xdist_worker_id() return port + uniq_delta
65
0
'''simple docstring''' # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _snake_case : Any = { 'configuration_vivit': ['VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VivitConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Any = ['VivitImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Optional[int] = [ 'VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'VivitModel', 'VivitPreTrainedModel', 'VivitForVideoClassification', ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys _snake_case : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
179
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bert import BertTokenizer _snake_case : Any = logging.get_logger(__name__) _snake_case : Any = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} _snake_case : List[str] = { 'vocab_file': { 'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt', 'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt', 'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt', 'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt', 'bert-base-multilingual-uncased': ( 'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt' ), 'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt', 'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt', 'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt', 'bert-large-uncased-whole-word-masking': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt' ), 'bert-large-cased-whole-word-masking': ( 'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt' ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt' ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt' ), 'bert-base-cased-finetuned-mrpc': ( 'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt' ), 'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt', 'bert-base-german-dbmdz-uncased': ( 'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt' ), 'TurkuNLP/bert-base-finnish-cased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt' ), 'TurkuNLP/bert-base-finnish-uncased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt' ), 'wietsedv/bert-base-dutch-cased': ( 'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json', 'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json', 'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json', 'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json', 'bert-base-multilingual-uncased': ( 'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json' ), 'bert-base-multilingual-cased': ( 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json' ), 'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json', 'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json', 'bert-large-uncased-whole-word-masking': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json' ), 'bert-large-cased-whole-word-masking': ( 'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json' ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json' ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( 'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json' ), 'bert-base-cased-finetuned-mrpc': ( 'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json' ), 'bert-base-german-dbmdz-cased': ( 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json' ), 'bert-base-german-dbmdz-uncased': ( 'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json' ), 'TurkuNLP/bert-base-finnish-cased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json' ), 'TurkuNLP/bert-base-finnish-uncased-v1': ( 'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json' ), 'wietsedv/bert-base-dutch-cased': ( 'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json' ), }, } _snake_case : int = { 'bert-base-uncased': 512, 'bert-large-uncased': 512, 'bert-base-cased': 512, 'bert-large-cased': 512, 'bert-base-multilingual-uncased': 512, 'bert-base-multilingual-cased': 512, 'bert-base-chinese': 512, 'bert-base-german-cased': 512, 'bert-large-uncased-whole-word-masking': 512, 'bert-large-cased-whole-word-masking': 512, 'bert-large-uncased-whole-word-masking-finetuned-squad': 512, 'bert-large-cased-whole-word-masking-finetuned-squad': 512, 'bert-base-cased-finetuned-mrpc': 512, 'bert-base-german-dbmdz-cased': 512, 'bert-base-german-dbmdz-uncased': 512, 'TurkuNLP/bert-base-finnish-cased-v1': 512, 'TurkuNLP/bert-base-finnish-uncased-v1': 512, 'wietsedv/bert-base-dutch-cased': 512, } _snake_case : int = { 'bert-base-uncased': {'do_lower_case': True}, 'bert-large-uncased': {'do_lower_case': True}, 'bert-base-cased': {'do_lower_case': False}, 'bert-large-cased': {'do_lower_case': False}, 'bert-base-multilingual-uncased': {'do_lower_case': True}, 'bert-base-multilingual-cased': {'do_lower_case': False}, 'bert-base-chinese': {'do_lower_case': False}, 'bert-base-german-cased': {'do_lower_case': False}, 'bert-large-uncased-whole-word-masking': {'do_lower_case': True}, 'bert-large-cased-whole-word-masking': {'do_lower_case': False}, 'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True}, 'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False}, 'bert-base-cased-finetuned-mrpc': {'do_lower_case': False}, 'bert-base-german-dbmdz-cased': {'do_lower_case': False}, 'bert-base-german-dbmdz-uncased': {'do_lower_case': True}, 'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False}, 'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True}, 'wietsedv/bert-base-dutch-cased': {'do_lower_case': False}, } class A ( _a ): lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_INIT_CONFIGURATION lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = BertTokenizer def __init__( self : str , lowerCAmelCase_ : Union[str, Any]=None , lowerCAmelCase_ : str=None , lowerCAmelCase_ : List[Any]=True , lowerCAmelCase_ : Any="[UNK]" , lowerCAmelCase_ : Union[str, Any]="[SEP]" , lowerCAmelCase_ : Tuple="[PAD]" , lowerCAmelCase_ : Tuple="[CLS]" , lowerCAmelCase_ : Optional[int]="[MASK]" , lowerCAmelCase_ : Dict=True , lowerCAmelCase_ : Union[str, Any]=None , **lowerCAmelCase_ : Union[str, Any] , ) -> Union[str, Any]: """simple docstring""" super().__init__( lowerCAmelCase_ , tokenizer_file=lowerCAmelCase_ , do_lower_case=lowerCAmelCase_ , unk_token=lowerCAmelCase_ , sep_token=lowerCAmelCase_ , pad_token=lowerCAmelCase_ , cls_token=lowerCAmelCase_ , mask_token=lowerCAmelCase_ , tokenize_chinese_chars=lowerCAmelCase_ , strip_accents=lowerCAmelCase_ , **lowerCAmelCase_ , ) _a = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , lowerCAmelCase_ ) != do_lower_case or normalizer_state.get('''strip_accents''' , lowerCAmelCase_ ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , lowerCAmelCase_ ) != tokenize_chinese_chars ): _a = getattr(lowerCAmelCase_ , normalizer_state.pop('''type''' ) ) _a = do_lower_case _a = strip_accents _a = tokenize_chinese_chars _a = normalizer_class(**lowerCAmelCase_ ) _a = do_lower_case def __lowerCAmelCase ( self : str , lowerCAmelCase_ : Dict , lowerCAmelCase_ : int=None ) -> List[str]: """simple docstring""" _a = [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 : Any , lowerCAmelCase_ : List[int] , lowerCAmelCase_ : Optional[List[int]] = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowerCAmelCase ( self : List[Any] , lowerCAmelCase_ : str , lowerCAmelCase_ : Optional[str] = None ) -> Tuple[str]: """simple docstring""" _a = self._tokenizer.model.save(lowerCAmelCase_ , name=lowerCAmelCase_ ) return tuple(lowerCAmelCase_ )
179
1
'''simple docstring''' def lowercase__ ( __lowercase : int , __lowercase : int ) -> int: """simple docstring""" while b: __UpperCamelCase , __UpperCamelCase = b, a % b return a def lowercase__ ( __lowercase : int , __lowercase : int ) -> int: """simple docstring""" return a if b == 0 else euclidean_gcd_recursive(__lowercase , a % b ) def lowercase__ ( ) -> Union[str, Any]: """simple docstring""" print(F'''euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}''' ) print(F'''euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}''' ) print(F'''euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}''' ) print(F'''euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}''' ) print(F'''euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}''' ) print(F'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}''' ) print(F'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}''' ) print(F'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}''' ) print(F'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}''' ) print(F'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}''' ) if __name__ == "__main__": main()
53
"""simple docstring""" 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, ) __snake_case = """hf-internal-testing/tiny-random-bert""" __snake_case = os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""") __snake_case = """9b8c223d42b2188cb49d29af482996f9d0f3e5a6""" class _lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self ) -> str: '''simple docstring''' snake_case : int = cached_file(UpperCamelCase__ , UpperCamelCase__ ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(UpperCamelCase__ ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) ) ) with open(os.path.join(UpperCamelCase__ , "refs" , "main" ) ) as f: snake_case : Dict = f.read() self.assertEqual(UpperCamelCase__ , os.path.join(UpperCamelCase__ , "snapshots" , UpperCamelCase__ , UpperCamelCase__ ) ) self.assertTrue(os.path.isfile(UpperCamelCase__ ) ) # File is cached at the same place the second time. snake_case : List[str] = cached_file(UpperCamelCase__ , UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) # Using a specific revision to test the full commit hash. snake_case : Any = cached_file(UpperCamelCase__ , UpperCamelCase__ , revision="9b8c223" ) self.assertEqual(UpperCamelCase__ , os.path.join(UpperCamelCase__ , "snapshots" , UpperCamelCase__ , UpperCamelCase__ ) ) def lowerCamelCase ( self ) -> Any: '''simple docstring''' with self.assertRaisesRegex(UpperCamelCase__ , "is not a valid model identifier" ): snake_case : Optional[Any] = cached_file("tiny-random-bert" , UpperCamelCase__ ) with self.assertRaisesRegex(UpperCamelCase__ , "is not a valid git identifier" ): snake_case : Optional[Any] = cached_file(UpperCamelCase__ , UpperCamelCase__ , revision="aaaa" ) with self.assertRaisesRegex(UpperCamelCase__ , "does not appear to have a file named" ): snake_case : List[Any] = cached_file(UpperCamelCase__ , "conf" ) def lowerCamelCase ( self ) -> Tuple: '''simple docstring''' with self.assertRaisesRegex(UpperCamelCase__ , "does not appear to have a file named" ): snake_case : Tuple = cached_file(UpperCamelCase__ , "conf" ) with open(os.path.join(UpperCamelCase__ , "refs" , "main" ) ) as f: snake_case : Any = f.read() self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase__ , ".no_exist" , UpperCamelCase__ , "conf" ) ) ) snake_case : Optional[Any] = cached_file(UpperCamelCase__ , "conf" , _raise_exceptions_for_missing_entries=UpperCamelCase__ ) self.assertIsNone(UpperCamelCase__ ) snake_case : Any = cached_file(UpperCamelCase__ , "conf" , local_files_only=UpperCamelCase__ , _raise_exceptions_for_missing_entries=UpperCamelCase__ ) self.assertIsNone(UpperCamelCase__ ) snake_case : Any = mock.Mock() snake_case : List[Any] = 500 snake_case : int = {} snake_case : Optional[int] = HTTPError snake_case : Tuple = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch("requests.Session.request" , return_value=UpperCamelCase__ ) as mock_head: snake_case : Tuple = cached_file(UpperCamelCase__ , "conf" , _raise_exceptions_for_connection_errors=UpperCamelCase__ ) self.assertIsNone(UpperCamelCase__ ) # This check we did call the fake head request mock_head.assert_called() def lowerCamelCase ( self ) -> Any: '''simple docstring''' self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only" , UpperCamelCase__ ) ) self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only" , UpperCamelCase__ ) ) self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only" , UpperCamelCase__ ) ) def lowerCamelCase ( self ) -> str: '''simple docstring''' self.assertIsNone(get_file_from_repo("bert-base-cased" , "ahah.txt" ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(UpperCamelCase__ , "is not a valid model identifier" ): get_file_from_repo("bert-base-case" , UpperCamelCase__ ) # The function raises if the revision does not exist. with self.assertRaisesRegex(UpperCamelCase__ , "is not a valid git identifier" ): get_file_from_repo("bert-base-cased" , UpperCamelCase__ , revision="ahaha" ) snake_case : int = get_file_from_repo("bert-base-cased" , UpperCamelCase__ ) # The name is the cached name which is not very easy to test, so instead we load the content. snake_case : str = json.loads(open(UpperCamelCase__ , "r" ).read() ) self.assertEqual(config["hidden_size"] , 768 ) def lowerCamelCase ( self ) -> Optional[Any]: '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: snake_case : int = Path(UpperCamelCase__ ) / "a.txt" filename.touch() self.assertEqual(get_file_from_repo(UpperCamelCase__ , "a.txt" ) , str(UpperCamelCase__ ) ) self.assertIsNone(get_file_from_repo(UpperCamelCase__ , "b.txt" ) )
203
0
import numpy as np class lowerCamelCase : '''simple docstring''' def __init__( self ) -> str: UpperCAmelCase_ : str = (0, 0) UpperCAmelCase_ : List[Any] = None UpperCAmelCase_ : List[Any] = 0 UpperCAmelCase_ : List[Any] = 0 UpperCAmelCase_ : Optional[Any] = 0 def __eq__( self , _UpperCamelCase ) -> Optional[int]: return self.position == cell.position def __UpperCAmelCase ( self ) -> List[Any]: print(self.position ) class lowerCamelCase : '''simple docstring''' def __init__( self , _UpperCamelCase=(5, 5) ) -> int: UpperCAmelCase_ : Tuple = np.zeros(_UpperCamelCase ) UpperCAmelCase_ : Union[str, Any] = world_size[0] UpperCAmelCase_ : List[Any] = world_size[1] def __UpperCAmelCase ( self ) -> Dict: print(self.w ) def __UpperCAmelCase ( self , _UpperCamelCase ) -> str: UpperCAmelCase_ : Dict = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] UpperCAmelCase_ : Any = cell.position[0] UpperCAmelCase_ : Union[str, Any] = cell.position[1] UpperCAmelCase_ : int = [] for n in neughbour_cord: UpperCAmelCase_ : List[str] = current_x + n[0] UpperCAmelCase_ : int = current_y + n[1] if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: UpperCAmelCase_ : Union[str, Any] = Cell() UpperCAmelCase_ : Union[str, Any] = (x, y) UpperCAmelCase_ : Any = cell neighbours.append(_UpperCamelCase ) return neighbours def lowercase__ ( __snake_case : List[Any] , __snake_case : List[Any] , __snake_case : List[str] ): '''simple docstring''' UpperCAmelCase_ : Dict = [] UpperCAmelCase_ : List[Any] = [] _open.append(__snake_case ) while _open: UpperCAmelCase_ : Dict = np.argmin([n.f for n in _open] ) UpperCAmelCase_ : Union[str, Any] = _open[min_f] _closed.append(_open.pop(__snake_case ) ) if current == goal: break for n in world.get_neigbours(__snake_case ): for c in _closed: if c == n: continue UpperCAmelCase_ : Tuple = current.g + 1 UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = n.position UpperCAmelCase_ , UpperCAmelCase_ : str = goal.position UpperCAmelCase_ : Tuple = (ya - ya) ** 2 + (xa - xa) ** 2 UpperCAmelCase_ : List[str] = n.h + n.g for c in _open: if c == n and c.f < n.f: continue _open.append(__snake_case ) UpperCAmelCase_ : List[Any] = [] while current.parent is not None: path.append(current.position ) UpperCAmelCase_ : Union[str, Any] = current.parent path.append(current.position ) return path[::-1] if __name__ == "__main__": __UpperCAmelCase = Gridworld() # Start position and goal __UpperCAmelCase = Cell() __UpperCAmelCase = (0, 0) __UpperCAmelCase = Cell() __UpperCAmelCase = (4, 4) print(F'path from {start.position} to {goal.position}') __UpperCAmelCase = astar(world, start, goal) # Just for visual reasons. for i in s: __UpperCAmelCase = 1 print(world.w)
145
import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap __UpperCAmelCase = 'Usage of script: script_name <size_of_canvas:int>' __UpperCAmelCase = [0] * 100 + [1] * 10 random.shuffle(choice) def lowercase__ ( __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : Any = [[False for i in range(__snake_case )] for j in range(__snake_case )] return canvas def lowercase__ ( __snake_case : list[list[bool]] ): '''simple docstring''' for i, row in enumerate(__snake_case ): for j, _ in enumerate(__snake_case ): UpperCAmelCase_ : Tuple = bool(random.getrandbits(1 ) ) def lowercase__ ( __snake_case : list[list[bool]] ): '''simple docstring''' UpperCAmelCase_ : List[Any] = np.array(__snake_case ) UpperCAmelCase_ : Any = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(__snake_case ): for c, pt in enumerate(__snake_case ): UpperCAmelCase_ : Optional[int] = __judge_point( __snake_case , current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) UpperCAmelCase_ : List[Any] = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. UpperCAmelCase_ : list[list[bool]] = current_canvas.tolist() return return_canvas def lowercase__ ( __snake_case : bool , __snake_case : list[list[bool]] ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = 0 UpperCAmelCase_ : List[Any] = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. UpperCAmelCase_ : List[Any] = pt if pt: if alive < 2: UpperCAmelCase_ : str = False elif alive == 2 or alive == 3: UpperCAmelCase_ : int = True elif alive > 3: UpperCAmelCase_ : List[Any] = False else: if alive == 3: UpperCAmelCase_ : int = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) __UpperCAmelCase = int(sys.argv[1]) # main working structure of this module. __UpperCAmelCase = create_canvas(canvas_size) seed(c) __UpperCAmelCase , __UpperCAmelCase = plt.subplots() fig.show() __UpperCAmelCase = ListedColormap(['w', 'k']) try: while True: __UpperCAmelCase = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
145
1
from ..utils import DummyObject, requires_backends class snake_case_ ( metaclass=__A ): __A : Tuple = ["speech"] def __init__( self : List[str] , *lowercase_ : Optional[Any] , **lowercase_ : str ) -> List[str]: requires_backends(self , ["speech"] ) class snake_case_ ( metaclass=__A ): __A : Any = ["speech"] def __init__( self : Optional[Any] , *lowercase_ : Optional[Any] , **lowercase_ : int ) -> Any: requires_backends(self , ["speech"] )
87
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class _a : """simple docstring""" @property def __A ( self : Union[str, Any] ): return self.get_dummy_input() @property def __A ( self : int ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def __A ( self : Union[str, Any] , UpperCAmelCase : List[Any]=True , UpperCAmelCase : str=False , UpperCAmelCase : Tuple=False , UpperCAmelCase : Optional[Any]=False , ): A_ = 4 A_ = 32 A_ = (32, 32) A_ = torch.manual_seed(0 ) A_ = torch.device(UpperCAmelCase ) A_ = (batch_size, num_channels) + sizes A_ = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ) A_ = {"hidden_states": hidden_states} if include_temb: A_ = 128 A_ = randn_tensor((batch_size, temb_channels) , generator=UpperCAmelCase , device=UpperCAmelCase ) if include_res_hidden_states_tuple: A_ = torch.manual_seed(1 ) A_ = (randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ),) if include_encoder_hidden_states: A_ = floats_tensor((batch_size, 32, 32) ).to(UpperCAmelCase ) if include_skip_sample: A_ = randn_tensor(((batch_size, 3) + sizes) , generator=UpperCAmelCase , device=UpperCAmelCase ) return dummy_input def __A ( self : Optional[int] ): A_ = { "in_channels": 32, "out_channels": 32, "temb_channels": 128, } if self.block_type == "up": A_ = 32 if self.block_type == "mid": init_dict.pop("out_channels" ) A_ = self.dummy_input return init_dict, inputs_dict def __A ( self : List[str] , UpperCAmelCase : Optional[Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) unet_block.to(UpperCAmelCase ) unet_block.eval() with torch.no_grad(): A_ = unet_block(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] self.assertEqual(output.shape , self.output_shape ) A_ = output[0, -1, -3:, -3:] A_ = torch.tensor(UpperCAmelCase ).to(UpperCAmelCase ) assert torch_all_close(output_slice.flatten() , UpperCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == "mps" , "Training is not supported in mps" ) def __A ( self : Union[str, Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) model.to(UpperCAmelCase ) model.train() A_ = model(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] A_ = torch.device(UpperCAmelCase ) A_ = randn_tensor(output.shape , device=UpperCAmelCase ) A_ = torch.nn.functional.mse_loss(UpperCAmelCase , UpperCAmelCase ) loss.backward()
312
0
"""simple docstring""" import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _lowerCAmelCase ( lowercase ,unittest.TestCase ): """simple docstring""" __UpperCAmelCase : List[str] = DDIMPipeline __UpperCAmelCase : Any = UNCONDITIONAL_IMAGE_GENERATION_PARAMS __UpperCAmelCase : str = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } __UpperCAmelCase : Dict = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS __UpperCAmelCase : Dict = False def _lowercase ( self : Optional[int] ): torch.manual_seed(0 ) __lowercase = UNetaDModel( block_out_channels=(3_2, 6_4), layers_per_block=2, sample_size=3_2, in_channels=3, out_channels=3, down_block_types=("DownBlock2D", "AttnDownBlock2D"), up_block_types=("AttnUpBlock2D", "UpBlock2D"), ) __lowercase = DDIMScheduler() __lowercase = {"unet": unet, "scheduler": scheduler} return components def _lowercase ( self : Optional[Any], UpperCAmelCase__ : Optional[Any], UpperCAmelCase__ : List[str]=0 ): if str(UpperCAmelCase__ ).startswith("mps" ): __lowercase = torch.manual_seed(UpperCAmelCase__ ) else: __lowercase = torch.Generator(device=UpperCAmelCase__ ).manual_seed(UpperCAmelCase__ ) __lowercase = { "batch_size": 1, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def _lowercase ( self : Any ): __lowercase = "cpu" __lowercase = self.get_dummy_components() __lowercase = self.pipeline_class(**UpperCAmelCase__ ) pipe.to(UpperCAmelCase__ ) pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) __lowercase = self.get_dummy_inputs(UpperCAmelCase__ ) __lowercase = pipe(**UpperCAmelCase__ ).images __lowercase = image[0, -3:, -3:, -1] self.assertEqual(image.shape, (1, 3_2, 3_2, 3) ) __lowercase = np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) __lowercase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(UpperCAmelCase__, 1E-3 ) def _lowercase ( self : int ): super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def _lowercase ( self : List[str] ): super().test_save_load_local(expected_max_difference=3E-3 ) def _lowercase ( self : List[str] ): super().test_save_load_optional_components(expected_max_difference=3E-3 ) def _lowercase ( self : List[str] ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _lowercase ( self : str ): __lowercase = "google/ddpm-cifar10-32" __lowercase = UNetaDModel.from_pretrained(UpperCAmelCase__ ) __lowercase = DDIMScheduler() __lowercase = DDIMPipeline(unet=UpperCAmelCase__, scheduler=UpperCAmelCase__ ) ddim.to(UpperCAmelCase__ ) ddim.set_progress_bar_config(disable=UpperCAmelCase__ ) __lowercase = torch.manual_seed(0 ) __lowercase = ddim(generator=UpperCAmelCase__, eta=0.0, output_type="numpy" ).images __lowercase = image[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) __lowercase = np.array([0.1_723, 0.1_617, 0.1_600, 0.1_626, 0.1_497, 0.1_513, 0.1_505, 0.1_442, 0.1_453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def _lowercase ( self : int ): __lowercase = "google/ddpm-ema-bedroom-256" __lowercase = UNetaDModel.from_pretrained(UpperCAmelCase__ ) __lowercase = DDIMScheduler.from_pretrained(UpperCAmelCase__ ) __lowercase = DDIMPipeline(unet=UpperCAmelCase__, scheduler=UpperCAmelCase__ ) ddpm.to(UpperCAmelCase__ ) ddpm.set_progress_bar_config(disable=UpperCAmelCase__ ) __lowercase = torch.manual_seed(0 ) __lowercase = ddpm(generator=UpperCAmelCase__, output_type="numpy" ).images __lowercase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) __lowercase = np.array([0.0_060, 0.0_201, 0.0_344, 0.0_024, 0.0_018, 0.0_002, 0.0_022, 0.0_000, 0.0_069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
144
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def _lowercase ( self : Optional[Any] ): __lowercase = TFCamembertModel.from_pretrained("jplu/tf-camembert-base" ) __lowercase = tf.convert_to_tensor( [[5, 1_2_1, 1_1, 6_6_0, 1_6, 7_3_0, 2_5_5_4_3, 1_1_0, 8_3, 6]], dtype=tf.intaa, ) # J'aime le camembert !" __lowercase = model(UpperCAmelCase__ )["last_hidden_state"] __lowercase = tf.TensorShape((1, 1_0, 7_6_8) ) self.assertEqual(output.shape, UpperCAmelCase__ ) # compare the actual values for a slice. __lowercase = tf.convert_to_tensor( [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]], dtype=tf.floataa, ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1E-4 ) )
144
1
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 ( __UpperCAmelCase ,__UpperCAmelCase ,unittest.TestCase ): UpperCamelCase__ : str = StableDiffusionDiffEditPipeline UpperCamelCase__ : List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'''height''', '''width''', '''image'''} | {'''image_latents'''} UpperCamelCase__ : List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {'''image'''} | {'''image_latents'''} UpperCamelCase__ : List[str] = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCamelCase__ : Any = frozenset([] ) def _lowerCamelCase ( self : List[Any]): '''simple docstring''' 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=__SCREAMING_SNAKE_CASE , ) __a = DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , clip_sample=__SCREAMING_SNAKE_CASE , set_alpha_to_one=__SCREAMING_SNAKE_CASE , ) __a = DDIMInverseScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , clip_sample=__SCREAMING_SNAKE_CASE , set_alpha_to_zero=__SCREAMING_SNAKE_CASE , ) 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-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , hidden_act='''gelu''' , projection_dim=512 , ) __a = CLIPTextModel(__SCREAMING_SNAKE_CASE) __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 _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[Any]=0): '''simple docstring''' __a = floats_tensor((1, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE)).to(__SCREAMING_SNAKE_CASE) __a = floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE)).to(__SCREAMING_SNAKE_CASE) if str(__SCREAMING_SNAKE_CASE).startswith('''mps'''): __a = torch.manual_seed(__SCREAMING_SNAKE_CASE) else: __a = torch.Generator(device=__SCREAMING_SNAKE_CASE).manual_seed(__SCREAMING_SNAKE_CASE) __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 _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : List[str]=0): '''simple docstring''' __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE)).to(__SCREAMING_SNAKE_CASE) __a = image.cpu().permute(0 , 2 , 3 , 1)[0] __a = Image.fromarray(np.uinta(__SCREAMING_SNAKE_CASE)).convert('''RGB''') if str(__SCREAMING_SNAKE_CASE).startswith('''mps'''): __a = torch.manual_seed(__SCREAMING_SNAKE_CASE) else: __a = torch.Generator(device=__SCREAMING_SNAKE_CASE).manual_seed(__SCREAMING_SNAKE_CASE) __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 _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[Any]=0): '''simple docstring''' __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE)).to(__SCREAMING_SNAKE_CASE) __a = image.cpu().permute(0 , 2 , 3 , 1)[0] __a = Image.fromarray(np.uinta(__SCREAMING_SNAKE_CASE)).convert('''RGB''') if str(__SCREAMING_SNAKE_CASE).startswith('''mps'''): __a = torch.manual_seed(__SCREAMING_SNAKE_CASE) else: __a = torch.Generator(device=__SCREAMING_SNAKE_CASE).manual_seed(__SCREAMING_SNAKE_CASE) __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 _lowerCamelCase ( self : Optional[int]): '''simple docstring''' if not hasattr(self.pipeline_class , '''_optional_components'''): return __a = self.get_dummy_components() __a = self.pipeline_class(**__SCREAMING_SNAKE_CASE) pipe.to(__SCREAMING_SNAKE_CASE) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components}) __a = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE) __a = pipe(**__SCREAMING_SNAKE_CASE)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(__SCREAMING_SNAKE_CASE) __a = self.pipeline_class.from_pretrained(__SCREAMING_SNAKE_CASE) pipe_loaded.to(__SCREAMING_SNAKE_CASE) pipe_loaded.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE) for optional_component in pipe._optional_components: self.assertTrue( getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) is None , F'`{optional_component}` did not stay set to None after loading.' , ) __a = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE) __a = pipe_loaded(**__SCREAMING_SNAKE_CASE)[0] __a = np.abs(output - output_loaded).max() self.assertLess(__SCREAMING_SNAKE_CASE , 1E-4) def _lowerCamelCase ( self : Dict): '''simple docstring''' __a = '''cpu''' __a = self.get_dummy_components() __a = self.pipeline_class(**__SCREAMING_SNAKE_CASE) pipe.to(__SCREAMING_SNAKE_CASE) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE) __a = self.get_dummy_mask_inputs(__SCREAMING_SNAKE_CASE) __a = pipe.generate_mask(**__SCREAMING_SNAKE_CASE) __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(__SCREAMING_SNAKE_CASE , 1E-3) self.assertEqual(mask[0, -3, -4] , 0) def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a = '''cpu''' __a = self.get_dummy_components() __a = self.pipeline_class(**__SCREAMING_SNAKE_CASE) pipe.to(__SCREAMING_SNAKE_CASE) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE) __a = self.get_dummy_inversion_inputs(__SCREAMING_SNAKE_CASE) __a = pipe.invert(**__SCREAMING_SNAKE_CASE).images __a = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3)) __a = np.array( [0.51_50, 0.51_34, 0.50_43, 0.53_76, 0.46_94, 0.5_10_50, 0.50_15, 0.44_07, 0.47_99] , ) __a = np.abs(image_slice.flatten() - expected_slice).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1E-3) def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=5E-3) def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a = '''cpu''' __a = self.get_dummy_components() __a = {'''beta_start''': 0.0_00_85, '''beta_end''': 0.0_12, '''beta_schedule''': '''scaled_linear'''} __a = DPMSolverMultistepScheduler(**__SCREAMING_SNAKE_CASE) __a = DPMSolverMultistepInverseScheduler(**__SCREAMING_SNAKE_CASE) __a = self.pipeline_class(**__SCREAMING_SNAKE_CASE) pipe.to(__SCREAMING_SNAKE_CASE) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE) __a = self.get_dummy_inversion_inputs(__SCREAMING_SNAKE_CASE) __a = pipe.invert(**__SCREAMING_SNAKE_CASE).images __a = image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3)) __a = np.array( [0.51_50, 0.51_34, 0.50_43, 0.53_76, 0.46_94, 0.5_10_50, 0.50_15, 0.44_07, 0.47_99] , ) __a = np.abs(image_slice.flatten() - expected_slice).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1E-3) @require_torch_gpu @slow class _A ( unittest.TestCase ): def _lowerCamelCase ( self : List[str]): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def _lowerCamelCase ( cls : str): '''simple docstring''' __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 _lowerCamelCase ( self : Dict): '''simple docstring''' __a = torch.manual_seed(0) __a = StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=__SCREAMING_SNAKE_CASE , 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=__SCREAMING_SNAKE_CASE) __a = '''a bowl of fruit''' __a = '''a bowl of pears''' __a = pipe.generate_mask( image=self.raw_image , source_prompt=__SCREAMING_SNAKE_CASE , target_prompt=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , ) __a = pipe.invert( prompt=__SCREAMING_SNAKE_CASE , image=self.raw_image , inpaint_strength=0.7 , generator=__SCREAMING_SNAKE_CASE).latents __a = pipe( prompt=__SCREAMING_SNAKE_CASE , mask_image=__SCREAMING_SNAKE_CASE , image_latents=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , negative_prompt=__SCREAMING_SNAKE_CASE , 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 _lowerCamelCase ( self : Any): '''simple docstring''' __a = torch.manual_seed(0) __a = StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=__SCREAMING_SNAKE_CASE , 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=__SCREAMING_SNAKE_CASE) __a = '''a bowl of fruit''' __a = '''a bowl of pears''' __a = pipe.generate_mask( image=self.raw_image , source_prompt=__SCREAMING_SNAKE_CASE , target_prompt=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , ) __a = pipe.invert( prompt=__SCREAMING_SNAKE_CASE , image=self.raw_image , inpaint_strength=0.7 , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=25 , ).latents __a = pipe( prompt=__SCREAMING_SNAKE_CASE , mask_image=__SCREAMING_SNAKE_CASE , image_latents=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , negative_prompt=__SCREAMING_SNAKE_CASE , 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
49
import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): # Initialise PyTorch model __a = LxmertConfig.from_json_file(_UpperCAmelCase ) print(f'Building PyTorch model from configuration: {config}' ) __a = LxmertForPreTraining(_UpperCAmelCase ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Save pytorch-model print(f'Save PyTorch model to {pytorch_dump_path}' ) torch.save(model.state_dict() , _UpperCAmelCase ) if __name__ == "__main__": __snake_case :List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __snake_case :Optional[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
49
1
'''simple docstring''' import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __UpperCamelCase ( lowerCAmelCase_ , unittest.TestCase ): A_ = CodeGenTokenizer A_ = CodeGenTokenizerFast A_ = True A_ = {"add_prefix_space": True} A_ = False def __UpperCAmelCase ( self ): '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __a : Tuple = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', '<|endoftext|>', ] __a : Union[str, Any] = dict(zip(__a , range(len(__a ) ) ) ) __a : Tuple = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] __a : Dict = {'unk_token': '<unk>'} __a : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) __a : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(__a ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(__a ) ) def __UpperCAmelCase ( self , **__a ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **__a ) def __UpperCAmelCase ( self , **__a ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **__a ) def __UpperCAmelCase ( self , __a ): '''simple docstring''' __a : Tuple = 'lower newer' __a : Tuple = 'lower newer' return input_text, output_text def __UpperCAmelCase ( self ): '''simple docstring''' __a : List[Any] = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) __a : str = 'lower newer' __a : Tuple = ['\u0120low', 'er', '\u0120', 'n', 'e', 'w', 'er'] __a : Dict = tokenizer.tokenize(__a , add_prefix_space=__a ) self.assertListEqual(__a , __a ) __a : List[str] = tokens + [tokenizer.unk_token] __a : Any = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__a ) , __a ) def __UpperCAmelCase ( self ): '''simple docstring''' if not self.test_rust_tokenizer: return __a : List[Any] = self.get_tokenizer() __a : List[str] = self.get_rust_tokenizer(add_prefix_space=__a ) __a : Any = 'lower newer' # Testing tokenization __a : Dict = tokenizer.tokenize(__a , add_prefix_space=__a ) __a : Dict = rust_tokenizer.tokenize(__a ) self.assertListEqual(__a , __a ) # Testing conversion to ids without special tokens __a : int = tokenizer.encode(__a , add_special_tokens=__a , add_prefix_space=__a ) __a : Tuple = rust_tokenizer.encode(__a , add_special_tokens=__a ) self.assertListEqual(__a , __a ) # Testing conversion to ids with special tokens __a : Tuple = self.get_rust_tokenizer(add_prefix_space=__a ) __a : Union[str, Any] = tokenizer.encode(__a , add_prefix_space=__a ) __a : int = rust_tokenizer.encode(__a ) self.assertListEqual(__a , __a ) # Testing the unknown token __a : Any = tokens + [rust_tokenizer.unk_token] __a : Tuple = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(__a ) , __a ) def __UpperCAmelCase ( self , *__a , **__a ): '''simple docstring''' pass def __UpperCAmelCase ( self , __a=15 ): '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): __a : Optional[int] = self.rust_tokenizer_class.from_pretrained(__a , **__a ) # Simple input __a : List[Any] = 'This is a simple input' __a : Tuple = ['This is a simple input 1', 'This is a simple input 2'] __a : Tuple = ('This is a simple input', 'This is a pair') __a : str = [ ('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(__a , tokenizer_r.encode , __a , max_length=__a , padding='max_length' ) # Simple input self.assertRaises(__a , tokenizer_r.encode_plus , __a , max_length=__a , padding='max_length' ) # Simple input self.assertRaises( __a , tokenizer_r.batch_encode_plus , __a , max_length=__a , padding='max_length' , ) # Pair input self.assertRaises(__a , tokenizer_r.encode , __a , max_length=__a , padding='max_length' ) # Pair input self.assertRaises(__a , tokenizer_r.encode_plus , __a , max_length=__a , padding='max_length' ) # Pair input self.assertRaises( __a , tokenizer_r.batch_encode_plus , __a , max_length=__a , padding='max_length' , ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : List[Any] = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token='<pad>' ) # Simple input __a : str = 'This is a simple input' __a : Any = ['This is a simple input looooooooong', 'This is a simple input'] __a : Optional[int] = ('This is a simple input', 'This is a pair') __a : Optional[Any] = [ ('This is a simple input loooooong', 'This is a simple input'), ('This is a simple pair loooooong', 'This is a simple pair'), ] __a : int = tokenizer.pad_token_id __a : List[Any] = tokenizer(__a , padding='max_length' , max_length=30 , return_tensors='np' ) __a : Union[str, Any] = tokenizer(__a , padding=__a , truncate=__a , return_tensors='np' ) __a : Optional[Any] = tokenizer(*__a , padding='max_length' , max_length=60 , return_tensors='np' ) __a : List[Any] = tokenizer(__a , padding=__a , truncate=__a , return_tensors='np' ) # s # test single string max_length padding self.assertEqual(out_s['input_ids'].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s['input_ids'] ) self.assertTrue(0 in out_s['attention_mask'] ) # s2 # test automatic padding self.assertEqual(out_sa['input_ids'].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa['input_ids'][0] ) self.assertFalse(0 in out_sa['attention_mask'][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa['input_ids'][1] ) self.assertTrue(0 in out_sa['attention_mask'][1] ) # p # test single pair max_length padding self.assertEqual(out_p['input_ids'].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p['input_ids'] ) self.assertTrue(0 in out_p['attention_mask'] ) # p2 # test automatic padding pair self.assertEqual(out_pa['input_ids'].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa['input_ids'][0] ) self.assertFalse(0 in out_pa['attention_mask'][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa['input_ids'][1] ) self.assertTrue(0 in out_pa['attention_mask'][1] ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : Optional[int] = '$$$' __a : List[str] = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=__a , add_bos_token=__a ) __a : Union[str, Any] = 'This is a simple input' __a : List[Any] = ['This is a simple input 1', 'This is a simple input 2'] __a : List[Any] = tokenizer.bos_token_id __a : List[str] = tokenizer(__a ) __a : Optional[Any] = tokenizer(__a ) self.assertEqual(out_s.input_ids[0] , __a ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) __a : Any = tokenizer.decode(out_s.input_ids ) __a : Union[str, Any] = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , __a ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def __UpperCAmelCase ( self ): '''simple docstring''' __a : Any = CodeGenTokenizer.from_pretrained('Salesforce/codegen-350M-mono' ) __a : Optional[int] = '\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#' __a : Tuple = '\nif len_a > len_b: result = a\nelse: result = b' __a : Optional[int] = tokenizer.encode(__a ) __a : Union[str, Any] = ['^#', re.escape('<|endoftext|>' ), '^\'\'\'', '^"""', '\n\n\n'] __a : Tuple = tokenizer.decode(__a , truncate_before_pattern=__a ) self.assertEqual(__a , __a ) def __UpperCAmelCase ( self ): '''simple docstring''' pass
294
'''simple docstring''' from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig __lowercase : str = logging.get_logger(__name__) # General docstring __lowercase : List[str] = 'MobileNetV1Config' # Base docstring __lowercase : Tuple = 'google/mobilenet_v1_1.0_224' __lowercase : List[Any] = [1, 10_24, 7, 7] # Image classification docstring __lowercase : int = 'google/mobilenet_v1_1.0_224' __lowercase : Any = 'tabby, tabby cat' __lowercase : Dict = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Optional[Any]=None ): __a : Dict = {} if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __a : Optional[Any] = model.mobilenet_va else: __a : List[Any] = model __a : Dict = 'MobilenetV1/Conv2d_0/' __a : Dict = backbone.conv_stem.convolution.weight __a : Optional[Any] = backbone.conv_stem.normalization.bias __a : int = backbone.conv_stem.normalization.weight __a : int = backbone.conv_stem.normalization.running_mean __a : Tuple = backbone.conv_stem.normalization.running_var for i in range(13 ): __a : int = i + 1 __a : Dict = i * 2 __a : Dict = backbone.layer[pt_index] __a : Dict = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/""" __a : Union[str, Any] = pointer.convolution.weight __a : Optional[Any] = pointer.normalization.bias __a : Union[str, Any] = pointer.normalization.weight __a : List[Any] = pointer.normalization.running_mean __a : Tuple = pointer.normalization.running_var __a : List[str] = backbone.layer[pt_index + 1] __a : Optional[Any] = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/""" __a : Optional[int] = pointer.convolution.weight __a : List[str] = pointer.normalization.bias __a : Dict = pointer.normalization.weight __a : Dict = pointer.normalization.running_mean __a : Optional[int] = pointer.normalization.running_var if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __a : Any = 'MobilenetV1/Logits/Conv2d_1c_1x1/' __a : Optional[int] = model.classifier.weight __a : List[Any] = model.classifier.bias return tf_to_pt_map def lowerCamelCase (_SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Dict ): try: import numpy as np import tensorflow as tf except ImportError: logger.error( 'Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see ' 'https://www.tensorflow.org/install/ for installation instructions.' ) raise # Load weights from TF model __a : Union[str, Any] = tf.train.list_variables(_SCREAMING_SNAKE_CASE ) __a : Optional[int] = {} for name, shape in init_vars: logger.info(F"""Loading TF weight {name} with shape {shape}""" ) __a : List[str] = tf.train.load_variable(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __a : Optional[Any] = array # Build TF to PyTorch weights loading map __a : Optional[int] = _build_tf_to_pytorch_map(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for name, pointer in tf_to_pt_map.items(): logger.info(F"""Importing {name}""" ) if name not in tf_weights: logger.info(F"""{name} not in tf pre-trained weights, skipping""" ) continue __a : Union[str, Any] = tf_weights[name] if "depthwise_weights" in name: logger.info('Transposing depthwise' ) __a : Optional[Any] = np.transpose(_SCREAMING_SNAKE_CASE , (2, 3, 0, 1) ) elif "weights" in name: logger.info('Transposing' ) if len(pointer.shape ) == 2: # copying into linear layer __a : Union[str, Any] = array.squeeze().transpose() else: __a : Dict = np.transpose(_SCREAMING_SNAKE_CASE , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched""" ) logger.info(F"""Initialize PyTorch weight {name} {array.shape}""" ) __a : List[str] = torch.from_numpy(_SCREAMING_SNAKE_CASE ) tf_weights.pop(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) tf_weights.pop(name + '/RMSProp' , _SCREAMING_SNAKE_CASE ) tf_weights.pop(name + '/RMSProp_1' , _SCREAMING_SNAKE_CASE ) tf_weights.pop(name + '/ExponentialMovingAverage' , _SCREAMING_SNAKE_CASE ) logger.info(F"""Weights not copied to PyTorch model: {", ".join(tf_weights.keys() )}""" ) return model def lowerCamelCase (_SCREAMING_SNAKE_CASE : torch.Tensor , _SCREAMING_SNAKE_CASE : nn.Convad ): __a , __a : Any = features.shape[-2:] __a , __a : int = conv_layer.stride __a , __a : Any = conv_layer.kernel_size if in_height % stride_height == 0: __a : int = max(kernel_height - stride_height , 0 ) else: __a : int = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: __a : Any = max(kernel_width - stride_width , 0 ) else: __a : str = max(kernel_width - (in_width % stride_width) , 0 ) __a : int = pad_along_width // 2 __a : Dict = pad_along_width - pad_left __a : List[str] = pad_along_height // 2 __a : Union[str, Any] = pad_along_height - pad_top __a : str = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , 'constant' , 0.0 ) class __UpperCamelCase ( nn.Module ): def __init__( self , __a , __a , __a , __a , __a = 1 , __a = 1 , __a = False , __a = True , __a = True , ): '''simple docstring''' super().__init__() __a : Optional[int] = config if in_channels % groups != 0: raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""" ) if out_channels % groups != 0: raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""" ) __a : Dict = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) __a : Union[str, Any] = nn.Convad( in_channels=__a , out_channels=__a , kernel_size=__a , stride=__a , padding=__a , groups=__a , bias=__a , padding_mode='zeros' , ) if use_normalization: __a : List[str] = nn.BatchNormad( num_features=__a , eps=config.layer_norm_eps , momentum=0.9997 , affine=__a , track_running_stats=__a , ) else: __a : Tuple = None if use_activation: if isinstance(__a , __a ): __a : Tuple = ACTaFN[use_activation] elif isinstance(config.hidden_act , __a ): __a : Union[str, Any] = ACTaFN[config.hidden_act] else: __a : Dict = config.hidden_act else: __a : List[Any] = None def __UpperCAmelCase ( self , __a ): '''simple docstring''' if self.config.tf_padding: __a : Union[str, Any] = apply_tf_padding(__a , self.convolution ) __a : Union[str, Any] = self.convolution(__a ) if self.normalization is not None: __a : str = self.normalization(__a ) if self.activation is not None: __a : Optional[int] = self.activation(__a ) return features class __UpperCamelCase ( lowerCAmelCase_ ): A_ = MobileNetVaConfig A_ = load_tf_weights_in_mobilenet_va A_ = "mobilenet_v1" A_ = "pixel_values" A_ = False def __UpperCAmelCase ( self , __a ): '''simple docstring''' if isinstance(__a , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(__a , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) __lowercase : Any = R'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' __lowercase : Optional[int] = R'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top." , lowerCAmelCase_ , ) class __UpperCamelCase ( lowerCAmelCase_ ): def __init__( self , __a , __a = True ): '''simple docstring''' super().__init__(__a ) __a : Optional[int] = config __a : str = 32 __a : Dict = max(int(depth * config.depth_multiplier ) , config.min_depth ) __a : Union[str, Any] = MobileNetVaConvLayer( __a , in_channels=config.num_channels , out_channels=__a , kernel_size=3 , stride=2 , ) __a : Tuple = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] __a : Any = nn.ModuleList() for i in range(13 ): __a : Union[str, Any] = out_channels if strides[i] == 2 or i == 0: depth *= 2 __a : List[Any] = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( __a , in_channels=__a , out_channels=__a , kernel_size=3 , stride=strides[i] , groups=__a , ) ) self.layer.append( MobileNetVaConvLayer( __a , in_channels=__a , out_channels=__a , kernel_size=1 , ) ) __a : Optional[int] = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def __UpperCAmelCase ( self , __a ): '''simple docstring''' raise NotImplementedError @add_start_docstrings_to_model_forward(__a ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=__a , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def __UpperCAmelCase ( self , __a = None , __a = None , __a = None , ): '''simple docstring''' __a : Dict = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __a : int = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError('You have to specify pixel_values' ) __a : Union[str, Any] = self.conv_stem(__a ) __a : Any = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): __a : List[str] = layer_module(__a ) if output_hidden_states: __a : List[Any] = all_hidden_states + (hidden_states,) __a : str = hidden_states if self.pooler is not None: __a : Union[str, Any] = torch.flatten(self.pooler(__a ) , start_dim=1 ) else: __a : int = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=__a , pooler_output=__a , hidden_states=__a , ) @add_start_docstrings( "\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , lowerCAmelCase_ , ) class __UpperCamelCase ( lowerCAmelCase_ ): def __init__( self , __a ): '''simple docstring''' super().__init__(__a ) __a : Tuple = config.num_labels __a : Tuple = MobileNetVaModel(__a ) __a : Optional[int] = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head __a : Any = nn.Dropout(config.classifier_dropout_prob , inplace=__a ) __a : Any = nn.Linear(__a , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(__a ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=__a , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def __UpperCAmelCase ( self , __a = None , __a = None , __a = None , __a = None , ): '''simple docstring''' __a : Union[str, Any] = return_dict if return_dict is not None else self.config.use_return_dict __a : Dict = self.mobilenet_va(__a , output_hidden_states=__a , return_dict=__a ) __a : List[str] = outputs.pooler_output if return_dict else outputs[1] __a : int = self.classifier(self.dropout(__a ) ) __a : Tuple = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: __a : str = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): __a : int = 'single_label_classification' else: __a : Optional[Any] = 'multi_label_classification' if self.config.problem_type == "regression": __a : Optional[Any] = MSELoss() if self.num_labels == 1: __a : List[Any] = loss_fct(logits.squeeze() , labels.squeeze() ) else: __a : Any = loss_fct(__a , __a ) elif self.config.problem_type == "single_label_classification": __a : List[str] = CrossEntropyLoss() __a : str = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": __a : Tuple = BCEWithLogitsLoss() __a : Optional[int] = loss_fct(__a , __a ) if not return_dict: __a : List[Any] = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=__a , logits=__a , hidden_states=outputs.hidden_states , )
294
1
"""simple docstring""" import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 A : int = { "return_dict": False, "output_hidden_states": True, "output_attentions": True, "torchscript": True, "torch_dtype": "float16", "use_bfloat16": True, "tf_legacy_loss": True, "pruned_heads": {"a": 1}, "tie_word_embeddings": False, "is_decoder": True, "cross_attention_hidden_size": 1_2_8, "add_cross_attention": True, "tie_encoder_decoder": True, "max_length": 5_0, "min_length": 3, "do_sample": True, "early_stopping": True, "num_beams": 3, "num_beam_groups": 3, "diversity_penalty": 0.5, "temperature": 2.0, "top_k": 1_0, "top_p": 0.7, "typical_p": 0.2, "repetition_penalty": 0.8, "length_penalty": 0.8, "no_repeat_ngram_size": 5, "encoder_no_repeat_ngram_size": 5, "bad_words_ids": [1, 2, 3], "num_return_sequences": 3, "chunk_size_feed_forward": 5, "output_scores": True, "return_dict_in_generate": True, "forced_bos_token_id": 2, "forced_eos_token_id": 3, "remove_invalid_values": True, "architectures": ["BertModel"], "finetuning_task": "translation", "id2label": {0: "label"}, "label2id": {"label": "0"}, "tokenizer_class": "BertTokenizerFast", "prefix": "prefix", "bos_token_id": 6, "pad_token_id": 7, "eos_token_id": 8, "sep_token_id": 9, "decoder_start_token_id": 1_0, "exponential_decay_length_penalty": (5, 1.01), "suppress_tokens": [0, 1], "begin_suppress_tokens": 2, "task_specific_params": {"translation": "some_params"}, "problem_type": "regression", } @is_staging_test class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def snake_case ( cls ): __lowerCAmelCase = TOKEN HfFolder.save_token(__a ) @classmethod def snake_case ( cls ): try: delete_repo(token=cls._token , repo_id="test-config" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-config-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-config" ) except HTTPError: pass def snake_case ( self ): __lowerCAmelCase = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) config.push_to_hub("test-config" , use_auth_token=self._token ) __lowerCAmelCase = BertConfig.from_pretrained(f"{USER}/test-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__a , getattr(__a , __a ) ) # Reset repo delete_repo(token=self._token , repo_id="test-config" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(__a , repo_id="test-config" , push_to_hub=__a , use_auth_token=self._token ) __lowerCAmelCase = BertConfig.from_pretrained(f"{USER}/test-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__a , getattr(__a , __a ) ) def snake_case ( self ): __lowerCAmelCase = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) config.push_to_hub("valid_org/test-config-org" , use_auth_token=self._token ) __lowerCAmelCase = BertConfig.from_pretrained("valid_org/test-config-org" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__a , getattr(__a , __a ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-config-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( __a , repo_id="valid_org/test-config-org" , push_to_hub=__a , use_auth_token=self._token ) __lowerCAmelCase = BertConfig.from_pretrained("valid_org/test-config-org" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__a , getattr(__a , __a ) ) def snake_case ( self ): CustomConfig.register_for_auto_class() __lowerCAmelCase = CustomConfig(attribute=42 ) config.push_to_hub("test-dynamic-config" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map , {"AutoConfig": "custom_configuration.CustomConfig"} ) __lowerCAmelCase = AutoConfig.from_pretrained(f"{USER}/test-dynamic-config" , trust_remote_code=__a ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ , "CustomConfig" ) self.assertEqual(new_config.attribute , 42 ) class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' def snake_case ( self ): __lowerCAmelCase = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated __lowerCAmelCase = c.n_embd + 1 # int __lowerCAmelCase = c.resid_pdrop + 1.0 # float __lowerCAmelCase = not c.scale_attn_weights # bool __lowerCAmelCase = c.summary_type + "foo" # str c.update_from_string( f"n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}" ) self.assertEqual(__a , c.n_embd , "mismatch for key: n_embd" ) self.assertEqual(__a , c.resid_pdrop , "mismatch for key: resid_pdrop" ) self.assertEqual(__a , c.scale_attn_weights , "mismatch for key: scale_attn_weights" ) self.assertEqual(__a , c.summary_type , "mismatch for key: summary_type" ) def snake_case ( self ): __lowerCAmelCase = PretrainedConfig() __lowerCAmelCase = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( __a , ["is_encoder_decoder", "_name_or_path", "_commit_hash", "transformers_version"] ) __lowerCAmelCase = [key for key, value in config_common_kwargs.items() if value == getattr(__a , __a )] if len(__a ) > 0: raise ValueError( "The following keys are set with the default values in" " `test_configuration_common.config_common_kwargs` pick another value for them:" f" {', '.join(__a )}." ) def snake_case ( self ): with self.assertRaises(__a ): # config is in subfolder, the following should not work without specifying the subfolder __lowerCAmelCase = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder" ) __lowerCAmelCase = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder" , subfolder="bert" ) self.assertIsNotNone(__a ) def snake_case ( self ): # A mock response for an HTTP head request to emulate server down __lowerCAmelCase = mock.Mock() __lowerCAmelCase = 5_00 __lowerCAmelCase = {} __lowerCAmelCase = HTTPError __lowerCAmelCase = {} # Download this model to make sure it's in the cache. __lowerCAmelCase = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=__a ) as mock_head: __lowerCAmelCase = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert" ) # This check we did call the fake head request mock_head.assert_called() def snake_case ( self ): # This test is for deprecated behavior and can be removed in v5 __lowerCAmelCase = BertConfig.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json" ) def snake_case ( self ): __lowerCAmelCase = AutoConfig.from_pretrained("bert-base-cased" ) __lowerCAmelCase = ["config.4.0.0.json"] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(__a ) __lowerCAmelCase = 2 json.dump(configuration.to_dict() , open(os.path.join(__a , "config.4.0.0.json" ) , "w" ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 __lowerCAmelCase = AutoConfig.from_pretrained(__a ) self.assertEqual(new_configuration.hidden_size , 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 __lowerCAmelCase = ["config.42.0.0.json"] __lowerCAmelCase = 7_68 configuration.save_pretrained(__a ) shutil.move(os.path.join(__a , "config.4.0.0.json" ) , os.path.join(__a , "config.42.0.0.json" ) ) __lowerCAmelCase = AutoConfig.from_pretrained(__a ) self.assertEqual(new_configuration.hidden_size , 7_68 ) def snake_case ( self ): # This repo has two configuration files, one for v4.0.0 and above with a different hidden size. __lowerCAmelCase = "hf-internal-testing/test-two-configs" import transformers as new_transformers __lowerCAmelCase = "v4.0.0" __lowerCAmelCase , __lowerCAmelCase = new_transformers.models.auto.AutoConfig.from_pretrained( __a , return_unused_kwargs=__a ) self.assertEqual(new_configuration.hidden_size , 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(__a , {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers __lowerCAmelCase = "v3.0.0" __lowerCAmelCase = old_transformers.models.auto.AutoConfig.from_pretrained(__a ) self.assertEqual(old_configuration.hidden_size , 7_68 )
57
"""simple docstring""" def _lowerCamelCase ( _UpperCamelCase = 6008_5147_5143 ): '''simple docstring''' try: __lowerCAmelCase = int(_UpperCamelCase ) 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." ) __lowerCAmelCase = 2 __lowerCAmelCase = 0 if n == 2: return 2 while n > 2: while n % i != 0: i += 1 __lowerCAmelCase = i while n % i == 0: __lowerCAmelCase = n // i i += 1 return int(_UpperCamelCase ) if __name__ == "__main__": print(f'''{solution() = }''')
57
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A : Tuple = logging.get_logger(__name__) A : Dict = { 'studio-ousia/luke-base': 'https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json', 'studio-ousia/luke-large': 'https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json', } class A ( UpperCAmelCase__ ): '''simple docstring''' A__ = '''luke''' def __init__(self : str , _UpperCAmelCase : Any=5_0267 , _UpperCAmelCase : str=50_0000 , _UpperCAmelCase : Any=768 , _UpperCAmelCase : Optional[Any]=256 , _UpperCAmelCase : Union[str, Any]=12 , _UpperCAmelCase : Optional[Any]=12 , _UpperCAmelCase : str=3072 , _UpperCAmelCase : List[str]="gelu" , _UpperCAmelCase : Optional[int]=0.1 , _UpperCAmelCase : Tuple=0.1 , _UpperCAmelCase : Optional[int]=512 , _UpperCAmelCase : str=2 , _UpperCAmelCase : Tuple=0.02 , _UpperCAmelCase : str=1E-1_2 , _UpperCAmelCase : Optional[int]=True , _UpperCAmelCase : Optional[int]=None , _UpperCAmelCase : Tuple=1 , _UpperCAmelCase : Tuple=0 , _UpperCAmelCase : str=2 , **_UpperCAmelCase : Dict , ) -> Tuple: """simple docstring""" super().__init__(pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase ) lowercase__ = vocab_size lowercase__ = entity_vocab_size lowercase__ = hidden_size lowercase__ = entity_emb_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = hidden_act lowercase__ = intermediate_size lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = max_position_embeddings lowercase__ = type_vocab_size lowercase__ = initializer_range lowercase__ = layer_norm_eps lowercase__ = use_entity_aware_attention lowercase__ = classifier_dropout
146
from __future__ import annotations from collections import deque class A : '''simple docstring''' def __init__(self : Any , _UpperCAmelCase : list[str] ) -> Optional[int]: """simple docstring""" lowercase__ = [] self.adlist.append( {"""value""": """""", """next_states""": [], """fail_state""": 0, """output""": []} ) for keyword in keywords: self.add_keyword(_UpperCAmelCase ) self.set_fail_transitions() def lowerCamelCase__ (self : List[str] , _UpperCAmelCase : int , _UpperCAmelCase : str ) -> int | None: """simple docstring""" for state in self.adlist[current_state]["next_states"]: if char == self.adlist[state]["value"]: return state return None def lowerCamelCase__ (self : List[Any] , _UpperCAmelCase : str ) -> None: """simple docstring""" lowercase__ = 0 for character in keyword: lowercase__ = self.find_next_state(_UpperCAmelCase , _UpperCAmelCase ) if next_state is None: self.adlist.append( { """value""": character, """next_states""": [], """fail_state""": 0, """output""": [], } ) self.adlist[current_state]["next_states"].append(len(self.adlist ) - 1 ) lowercase__ = len(self.adlist ) - 1 else: lowercase__ = next_state self.adlist[current_state]["output"].append(_UpperCAmelCase ) def lowerCamelCase__ (self : Any ) -> None: """simple docstring""" lowercase__ = deque() for node in self.adlist[0]["next_states"]: q.append(_UpperCAmelCase ) lowercase__ = 0 while q: lowercase__ = q.popleft() for child in self.adlist[r]["next_states"]: q.append(_UpperCAmelCase ) lowercase__ = self.adlist[r]["""fail_state"""] while ( self.find_next_state(_UpperCAmelCase , self.adlist[child]["""value"""] ) is None and state != 0 ): lowercase__ = self.adlist[state]["""fail_state"""] lowercase__ = self.find_next_state( _UpperCAmelCase , self.adlist[child]["""value"""] ) if self.adlist[child]["fail_state"] is None: lowercase__ = 0 lowercase__ = ( self.adlist[child]["""output"""] + self.adlist[self.adlist[child]["""fail_state"""]]["""output"""] ) def lowerCamelCase__ (self : List[str] , _UpperCAmelCase : str ) -> dict[str, list[int]]: """simple docstring""" lowercase__ = {} # returns a dict with keywords and list of its occurrences lowercase__ = 0 for i in range(len(_UpperCAmelCase ) ): while ( self.find_next_state(_UpperCAmelCase , string[i] ) is None and current_state != 0 ): lowercase__ = self.adlist[current_state]["""fail_state"""] lowercase__ = self.find_next_state(_UpperCAmelCase , string[i] ) if next_state is None: lowercase__ = 0 else: lowercase__ = next_state for key in self.adlist[current_state]["output"]: if key not in result: lowercase__ = [] result[key].append(i - len(_UpperCAmelCase ) + 1 ) return result if __name__ == "__main__": import doctest doctest.testmod()
146
1
from typing import Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images from ...utils import TensorType, logging UpperCAmelCase__ = logging.get_logger(__name__) class __lowerCAmelCase ( A ): UpperCamelCase = ['''pixel_values'''] def __init__( self : int , A : bool = True , A : Union[int, float] = 1 / 2_55 , A : bool = True , A : int = 8 , **A : int , ) -> None: """simple docstring""" super().__init__(**A) _UpperCAmelCase = do_rescale _UpperCAmelCase = rescale_factor _UpperCAmelCase = do_pad _UpperCAmelCase = pad_size def _lowerCamelCase ( self : Any , A : np.ndarray , A : float , A : Optional[Union[str, ChannelDimension]] = None , **A : List[str]) -> np.ndarray: """simple docstring""" return rescale(A , scale=A , data_format=A , **A) def _lowerCamelCase ( self : Any , A : np.ndarray , A : int , A : Optional[Union[str, ChannelDimension]] = None) -> Dict: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = get_image_size(A) _UpperCAmelCase = (old_height // size + 1) * size - old_height _UpperCAmelCase = (old_width // size + 1) * size - old_width return pad(A , ((0, pad_height), (0, pad_width)) , mode='symmetric' , data_format=A) def _lowerCamelCase ( self : Optional[int] , A : ImageInput , A : Optional[bool] = None , A : Optional[float] = None , A : Optional[bool] = None , A : Optional[int] = None , A : Optional[Union[str, TensorType]] = None , A : Union[str, ChannelDimension] = ChannelDimension.FIRST , **A : List[Any] , ) -> Tuple: """simple docstring""" _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_pad if do_pad is not None else self.do_pad _UpperCAmelCase = pad_size if pad_size is not None else self.pad_size _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_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') # All transformations expect numpy arrays. _UpperCAmelCase = [to_numpy_array(A) for image in images] if do_rescale: _UpperCAmelCase = [self.rescale(image=A , scale=A) for image in images] if do_pad: _UpperCAmelCase = [self.pad(A , size=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)
339
import requests from bsa import BeautifulSoup def A ( _UpperCAmelCase : str , _UpperCAmelCase : dict ) -> str: '''simple docstring''' _UpperCAmelCase = BeautifulSoup(requests.get(_UpperCAmelCase , params=_UpperCAmelCase ).content , 'html.parser' ) _UpperCAmelCase = soup.find('div' , attrs={'class': 'gs_ri'} ) _UpperCAmelCase = div.find('div' , attrs={'class': 'gs_fl'} ).find_all('a' ) return anchors[2].get_text() if __name__ == "__main__": UpperCAmelCase__ = { "title": ( "Precisely geometry controlled microsupercapacitors for ultrahigh areal " "capacitance, volumetric capacitance, and energy density" ), "journal": "Chem. Mater.", "volume": 30, "pages": "3979-3990", "year": 2018, "hl": "en", } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
339
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase : Dict = { """configuration_mega""": ["""MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MegaConfig""", """MegaOnnxConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[int] = [ """MEGA_PRETRAINED_MODEL_ARCHIVE_LIST""", """MegaForCausalLM""", """MegaForMaskedLM""", """MegaForMultipleChoice""", """MegaForQuestionAnswering""", """MegaForSequenceClassification""", """MegaForTokenClassification""", """MegaModel""", """MegaPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mega import ( MEGA_PRETRAINED_MODEL_ARCHIVE_LIST, MegaForCausalLM, MegaForMaskedLM, MegaForMultipleChoice, MegaForQuestionAnswering, MegaForSequenceClassification, MegaForTokenClassification, MegaModel, MegaPreTrainedModel, ) else: import sys UpperCAmelCase : Optional[int] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
355
from typing import Optional from .. import Features, NamedSplit from ..packaged_modules.text.text import Text from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class __lowerCAmelCase ( UpperCamelCase__): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = False , lowerCAmelCase__ = False , lowerCAmelCase__ = None , **lowerCAmelCase__ , ) -> int: '''simple docstring''' super().__init__( lowerCAmelCase__ , split=lowerCAmelCase__ , features=lowerCAmelCase__ , cache_dir=lowerCAmelCase__ , keep_in_memory=lowerCAmelCase__ , streaming=lowerCAmelCase__ , num_proc=lowerCAmelCase__ , **lowerCAmelCase__ , ) a__ : Tuple =path_or_paths if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else {self.split: path_or_paths} a__ : List[str] =Text( cache_dir=lowerCAmelCase__ , data_files=lowerCAmelCase__ , features=lowerCAmelCase__ , **lowerCAmelCase__ , ) def _lowercase ( self ) -> List[Any]: '''simple docstring''' if self.streaming: a__ : str =self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: a__ : Dict =None a__ : Optional[Any] =None a__ : Union[str, Any] =None a__ : Tuple =None self.builder.download_and_prepare( download_config=lowerCAmelCase__ , download_mode=lowerCAmelCase__ , verification_mode=lowerCAmelCase__ , base_path=lowerCAmelCase__ , num_proc=self.num_proc , ) a__ : Tuple =self.builder.as_dataset( split=self.split , verification_mode=lowerCAmelCase__ , in_memory=self.keep_in_memory ) return dataset
148
0