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
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def a_ ( __lowercase : int , __lowercase : List[str] ) -> Tuple: _snake_case = checkpoint _snake_case = {} _snake_case = vae_state_dict['encoder.conv_in.weight'] _snake_case = vae_state_dict['encoder.conv_in.bias'] _snake_case = vae_state_dict['encoder.conv_out.weight'] _snake_case = vae_state_dict['encoder.conv_out.bias'] _snake_case = vae_state_dict['encoder.norm_out.weight'] _snake_case = vae_state_dict['encoder.norm_out.bias'] _snake_case = vae_state_dict['decoder.conv_in.weight'] _snake_case = vae_state_dict['decoder.conv_in.bias'] _snake_case = vae_state_dict['decoder.conv_out.weight'] _snake_case = vae_state_dict['decoder.conv_out.bias'] _snake_case = vae_state_dict['decoder.norm_out.weight'] _snake_case = vae_state_dict['decoder.norm_out.bias'] _snake_case = vae_state_dict['quant_conv.weight'] _snake_case = vae_state_dict['quant_conv.bias'] _snake_case = vae_state_dict['post_quant_conv.weight'] _snake_case = vae_state_dict['post_quant_conv.bias'] # Retrieves the keys for the encoder down blocks only _snake_case = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'encoder.down' in layer} ) _snake_case = { layer_id: [key for key in vae_state_dict if f'''down.{layer_id}''' in key] for layer_id in range(__lowercase ) } # Retrieves the keys for the decoder up blocks only _snake_case = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'decoder.up' in layer} ) _snake_case = { layer_id: [key for key in vae_state_dict if f'''up.{layer_id}''' in key] for layer_id in range(__lowercase ) } for i in range(__lowercase ): _snake_case = [key for key in down_blocks[i] if f'''down.{i}''' in key and f'''down.{i}.downsample''' not in key] if f'''encoder.down.{i}.downsample.conv.weight''' in vae_state_dict: _snake_case = vae_state_dict.pop( f'''encoder.down.{i}.downsample.conv.weight''' ) _snake_case = vae_state_dict.pop( f'''encoder.down.{i}.downsample.conv.bias''' ) _snake_case = renew_vae_resnet_paths(__lowercase ) _snake_case = {'old': f'''down.{i}.block''', 'new': f'''down_blocks.{i}.resnets'''} assign_to_checkpoint(__lowercase , __lowercase , __lowercase , additional_replacements=[meta_path] , config=__lowercase ) _snake_case = [key for key in vae_state_dict if 'encoder.mid.block' in key] _snake_case = 2 for i in range(1 , num_mid_res_blocks + 1 ): _snake_case = [key for key in mid_resnets if f'''encoder.mid.block_{i}''' in key] _snake_case = renew_vae_resnet_paths(__lowercase ) _snake_case = {'old': f'''mid.block_{i}''', 'new': f'''mid_block.resnets.{i - 1}'''} assign_to_checkpoint(__lowercase , __lowercase , __lowercase , additional_replacements=[meta_path] , config=__lowercase ) _snake_case = [key for key in vae_state_dict if 'encoder.mid.attn' in key] _snake_case = renew_vae_attention_paths(__lowercase ) _snake_case = {'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'} assign_to_checkpoint(__lowercase , __lowercase , __lowercase , additional_replacements=[meta_path] , config=__lowercase ) conv_attn_to_linear(__lowercase ) for i in range(__lowercase ): _snake_case = num_up_blocks - 1 - i _snake_case = [ key for key in up_blocks[block_id] if f'''up.{block_id}''' in key and f'''up.{block_id}.upsample''' not in key ] if f'''decoder.up.{block_id}.upsample.conv.weight''' in vae_state_dict: _snake_case = vae_state_dict[ f'''decoder.up.{block_id}.upsample.conv.weight''' ] _snake_case = vae_state_dict[ f'''decoder.up.{block_id}.upsample.conv.bias''' ] _snake_case = renew_vae_resnet_paths(__lowercase ) _snake_case = {'old': f'''up.{block_id}.block''', 'new': f'''up_blocks.{i}.resnets'''} assign_to_checkpoint(__lowercase , __lowercase , __lowercase , additional_replacements=[meta_path] , config=__lowercase ) _snake_case = [key for key in vae_state_dict if 'decoder.mid.block' in key] _snake_case = 2 for i in range(1 , num_mid_res_blocks + 1 ): _snake_case = [key for key in mid_resnets if f'''decoder.mid.block_{i}''' in key] _snake_case = renew_vae_resnet_paths(__lowercase ) _snake_case = {'old': f'''mid.block_{i}''', 'new': f'''mid_block.resnets.{i - 1}'''} assign_to_checkpoint(__lowercase , __lowercase , __lowercase , additional_replacements=[meta_path] , config=__lowercase ) _snake_case = [key for key in vae_state_dict if 'decoder.mid.attn' in key] _snake_case = renew_vae_attention_paths(__lowercase ) _snake_case = {'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'} assign_to_checkpoint(__lowercase , __lowercase , __lowercase , additional_replacements=[meta_path] , config=__lowercase ) conv_attn_to_linear(__lowercase ) return new_checkpoint def a_ ( __lowercase : str , __lowercase : str , ) -> int: # Only support V1 _snake_case = requests.get( ' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml' ) _snake_case = io.BytesIO(r.content ) _snake_case = OmegaConf.load(__lowercase ) _snake_case = 512 _snake_case = 'cuda' if torch.cuda.is_available() else 'cpu' if checkpoint_path.endswith('safetensors' ): from safetensors import safe_open _snake_case = {} with safe_open(__lowercase , framework='pt' , device='cpu' ) as f: for key in f.keys(): _snake_case = f.get_tensor(__lowercase ) else: _snake_case = torch.load(__lowercase , map_location=__lowercase )['state_dict'] # Convert the VAE model. _snake_case = create_vae_diffusers_config(__lowercase , image_size=__lowercase ) _snake_case = custom_convert_ldm_vae_checkpoint(__lowercase , __lowercase ) _snake_case = AutoencoderKL(**__lowercase ) vae.load_state_dict(__lowercase ) vae.save_pretrained(__lowercase ) if __name__ == "__main__": _lowerCamelCase : Any = argparse.ArgumentParser() parser.add_argument('''--vae_pt_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''') parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''') _lowerCamelCase : int = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
282
from collections.abc import Sequence def a_ ( __lowercase : Sequence[float] , __lowercase : float ) -> float: return sum(c * (x**i) for i, c in enumerate(__lowercase ) ) def a_ ( __lowercase : Sequence[float] , __lowercase : float ) -> float: _snake_case = 0.0 for coeff in reversed(__lowercase ): _snake_case = result * x + coeff return result if __name__ == "__main__": _lowerCamelCase : Optional[Any] = (0.0, 0.0, 5.0, 9.3, 7.0) _lowerCamelCase : Optional[int] = 1_0.0 print(evaluate_poly(poly, x)) print(horner(poly, x))
282
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/resolve/main/config.json''', '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/config.json''', '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json''', '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json''', '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/config.json''', '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json''', } class __magic_name__ (__lowercase ): lowerCamelCase__ = '''bloom''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = { '''num_hidden_layers''': '''n_layer''', '''num_attention_heads''': '''n_head''', } def __init__( self , _a=250880 , _a=64 , _a=2 , _a=8 , _a=1E-5 , _a=0.0_2 , _a=True , _a=1 , _a=2 , _a=False , _a=0.0 , _a=0.0 , _a=1 , _a=False , **_a , ) -> Dict: lowerCAmelCase_ = vocab_size # Backward compatibility with n_embed kwarg lowerCAmelCase_ = kwargs.pop("n_embed" , _a ) lowerCAmelCase_ = hidden_size if n_embed is None else n_embed lowerCAmelCase_ = n_layer lowerCAmelCase_ = n_head lowerCAmelCase_ = layer_norm_epsilon lowerCAmelCase_ = initializer_range lowerCAmelCase_ = use_cache lowerCAmelCase_ = pretraining_tp lowerCAmelCase_ = apply_residual_connection_post_layernorm lowerCAmelCase_ = hidden_dropout lowerCAmelCase_ = attention_dropout lowerCAmelCase_ = bos_token_id lowerCAmelCase_ = eos_token_id lowerCAmelCase_ = slow_but_exact super().__init__(bos_token_id=_a , eos_token_id=_a , **_a ) class __magic_name__ (__lowercase ): lowerCamelCase__ = version.parse('''1.12''' ) def __init__( self , _a , _a = "default" , _a = None , _a = False , ) -> int: super().__init__(_a , task=_a , patching_specs=_a , use_past=_a ) if not getattr(self._config , "pad_token_id" , _a ): # TODO: how to do that better? lowerCAmelCase_ = 0 @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: lowerCAmelCase_ = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(_a , direction="inputs" , inverted_values_shape=_a ) lowerCAmelCase_ = {0: "batch", 1: "past_sequence + sequence"} else: lowerCAmelCase_ = {0: "batch", 1: "sequence"} return common_inputs @property def __a ( self ) -> int: return self._config.n_layer @property def __a ( self ) -> int: return self._config.n_head @property def __a ( self ) -> float: return 1E-3 def __a ( self , _a , _a = -1 , _a = -1 , _a = False , _a = None , ) -> Mapping[str, Any]: lowerCAmelCase_ = super(_a , self ).generate_dummy_inputs( _a , batch_size=_a , seq_length=_a , is_pair=_a , framework=_a ) # We need to order the input in the way they appears in the forward() lowerCAmelCase_ = OrderedDict({"input_ids": common_inputs["input_ids"]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch lowerCAmelCase_ , lowerCAmelCase_ = common_inputs["input_ids"].shape # Not using the same length for past_key_values lowerCAmelCase_ = seqlen + 2 lowerCAmelCase_ = self._config.hidden_size // self.num_attention_heads lowerCAmelCase_ = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) lowerCAmelCase_ = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) lowerCAmelCase_ = [ (torch.zeros(_a ), torch.zeros(_a )) for _ in range(self.num_layers ) ] lowerCAmelCase_ = common_inputs["attention_mask"] if self.use_past: lowerCAmelCase_ = ordered_inputs["attention_mask"].dtype lowerCAmelCase_ = torch.cat( [ordered_inputs["attention_mask"], torch.ones(_a , _a , dtype=_a )] , dim=1 ) return ordered_inputs @property def __a ( self ) -> int: return 13
22
def A(__a: Optional[Any] ): lowerCAmelCase_ = len(__a ) lowerCAmelCase_ = sum(__a ) lowerCAmelCase_ = [[False for x in range(s + 1 )] for y in range(n + 1 )] for i in range(1 , n + 1 ): lowerCAmelCase_ = True for i in range(1 , s + 1 ): lowerCAmelCase_ = False for i in range(1 , n + 1 ): for j in range(1 , s + 1 ): lowerCAmelCase_ = dp[i][j - 1] if arr[i - 1] <= j: lowerCAmelCase_ = 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: lowerCAmelCase_ = s - 2 * j break return diff
22
1
'''simple docstring''' import warnings from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = { "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/config.json", # See all BART models at https://huggingface.co/models?filter=bart } class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' A : int = '''bart''' A : List[str] = ['''past_key_values'''] A : Any = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self, A=50_265, A=1_024, A=12, A=4_096, A=16, A=12, A=4_096, A=16, A=0.0, A=0.0, A="gelu", A=1_024, A=0.1, A=0.0, A=0.0, A=0.02, A=0.0, A=False, A=True, A=3, A=1, A=0, A=2, A=True, A=2, A=2, **A, ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = d_model SCREAMING_SNAKE_CASE : Dict = encoder_ffn_dim SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE : str = encoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : Any = decoder_layers SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = dropout SCREAMING_SNAKE_CASE : List[Any] = attention_dropout SCREAMING_SNAKE_CASE : List[str] = activation_dropout SCREAMING_SNAKE_CASE : Any = activation_function SCREAMING_SNAKE_CASE : Tuple = init_std SCREAMING_SNAKE_CASE : Tuple = encoder_layerdrop SCREAMING_SNAKE_CASE : Tuple = decoder_layerdrop SCREAMING_SNAKE_CASE : Optional[Any] = classifier_dropout SCREAMING_SNAKE_CASE : Optional[int] = use_cache SCREAMING_SNAKE_CASE : Tuple = encoder_layers SCREAMING_SNAKE_CASE : Optional[int] = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( num_labels=A, pad_token_id=A, bos_token_id=A, eos_token_id=A, is_encoder_decoder=A, decoder_start_token_id=A, forced_eos_token_id=A, **A, ) # ensure backward compatibility for BART CNN models if self.forced_bos_token_id is None and kwargs.get('force_bos_token_to_be_generated', A ): SCREAMING_SNAKE_CASE : Dict = self.bos_token_id warnings.warn( F"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. " 'The config can simply be saved and uploaded again to be fixed.' ) class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def UpperCamelCase_ ( self ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = OrderedDict( [ ('input_ids', {0: 'batch', 1: 'encoder_sequence'}), ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}), ] ) if self.use_past: SCREAMING_SNAKE_CASE : str = {0: 'batch'} SCREAMING_SNAKE_CASE : Any = {0: 'batch', 1: 'past_decoder_sequence + sequence'} else: SCREAMING_SNAKE_CASE : int = {0: 'batch', 1: 'decoder_sequence'} SCREAMING_SNAKE_CASE : Dict = {0: 'batch', 1: 'decoder_sequence'} if self.use_past: self.fill_with_past_key_values_(A, direction='inputs' ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE : Optional[Any] = OrderedDict( [ ('input_ids', {0: 'batch', 1: 'encoder_sequence'}), ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}), ] ) if self.use_past: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = self.num_layers for i in range(A ): SCREAMING_SNAKE_CASE : Dict = {0: 'batch', 2: 'past_sequence + sequence'} SCREAMING_SNAKE_CASE : Any = {0: 'batch', 2: 'past_sequence + sequence'} else: SCREAMING_SNAKE_CASE : int = 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 UpperCamelCase_ ( self ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = super().outputs else: SCREAMING_SNAKE_CASE : str = super(A, self ).outputs if self.use_past: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = self.num_layers for i in range(A ): SCREAMING_SNAKE_CASE : Optional[Any] = {0: 'batch', 2: 'past_sequence + sequence'} SCREAMING_SNAKE_CASE : str = {0: 'batch', 2: 'past_sequence + sequence'} return common_outputs def UpperCamelCase_ ( self, A, A = -1, A = -1, A = False, A = None, ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( A, A, A, A, A ) # Generate decoder inputs SCREAMING_SNAKE_CASE : Optional[Any] = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE : Optional[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( A, A, A, A, A ) SCREAMING_SNAKE_CASE : int = {F"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE : List[str] = dict(**A, **A ) if self.use_past: if not is_torch_available(): raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' ) else: import torch SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : str = common_inputs['input_ids'].shape SCREAMING_SNAKE_CASE : int = common_inputs['decoder_input_ids'].shape[1] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = self.num_attention_heads SCREAMING_SNAKE_CASE : List[str] = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[str] = decoder_seq_length + 3 SCREAMING_SNAKE_CASE : Optional[Any] = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE : Any = torch.cat( [common_inputs['decoder_attention_mask'], torch.ones(A, A )], dim=1 ) SCREAMING_SNAKE_CASE : Dict = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[Any] = self.num_layers SCREAMING_SNAKE_CASE : Union[str, Any] = min(A, A ) SCREAMING_SNAKE_CASE : Union[str, Any] = max(A, A ) - min_num_layers SCREAMING_SNAKE_CASE : Dict = 'encoder' if num_encoder_layers > num_decoder_layers else 'decoder' for _ in range(A ): common_inputs["past_key_values"].append( ( torch.zeros(A ), torch.zeros(A ), torch.zeros(A ), torch.zeros(A ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE : Optional[Any] = encoder_shape if remaining_side_name == 'encoder' else decoder_shape for _ in range(A, A ): common_inputs["past_key_values"].append((torch.zeros(A ), torch.zeros(A )) ) return common_inputs def UpperCamelCase_ ( self, A, A = -1, A = -1, A = False, A = None, ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( A, A, A, A, A ) if self.use_past: if not is_torch_available(): raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' ) else: import torch SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = common_inputs['input_ids'].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : Tuple = seqlen + 2 SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = self.num_layers SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : List[str] = self.num_attention_heads SCREAMING_SNAKE_CASE : int = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Any = common_inputs['attention_mask'].dtype SCREAMING_SNAKE_CASE : List[Any] = torch.cat( [common_inputs['attention_mask'], torch.ones(A, A, dtype=A )], dim=1 ) SCREAMING_SNAKE_CASE : int = [ (torch.zeros(A ), torch.zeros(A )) for _ in range(A ) ] return common_inputs def UpperCamelCase_ ( self, A, A = -1, A = -1, A = False, A = None, ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = compute_effective_axis_dimension( A, 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 SCREAMING_SNAKE_CASE : int = tokenizer.num_special_tokens_to_add(A ) SCREAMING_SNAKE_CASE : str = compute_effective_axis_dimension( A, fixed_dimension=OnnxConfig.default_fixed_sequence, num_token_to_add=A ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : List[Any] = [' '.join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Optional[int] = dict(tokenizer(A, return_tensors=A ) ) return common_inputs def UpperCamelCase_ ( self, A, A = -1, A = -1, A = False, A = None, ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : str = self._generate_dummy_inputs_for_default_and_seqaseq_lm( A, batch_size=A, seq_length=A, is_pair=A, framework=A ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE : Optional[int] = self._generate_dummy_inputs_for_causal_lm( A, batch_size=A, seq_length=A, is_pair=A, framework=A ) else: SCREAMING_SNAKE_CASE : Tuple = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( A, batch_size=A, seq_length=A, is_pair=A, framework=A ) return common_inputs def UpperCamelCase_ ( self, A, A, A, A ): '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = super()._flatten_past_key_values_(A, A, A, A ) else: SCREAMING_SNAKE_CASE : int = super(A, self )._flatten_past_key_values_( A, A, A, A )
251
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = { "microsoft/git-base": "https://huggingface.co/microsoft/git-base/resolve/main/config.json", } class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Any = '''git_vision_model''' def __init__( self, A=768, A=3_072, A=12, A=12, A=3, A=224, A=16, A="quick_gelu", A=1E-5, A=0.0, A=0.02, **A, ): '''simple docstring''' super().__init__(**A ) SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_hidden_layers SCREAMING_SNAKE_CASE : str = num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = num_channels SCREAMING_SNAKE_CASE : List[str] = patch_size SCREAMING_SNAKE_CASE : Optional[int] = image_size SCREAMING_SNAKE_CASE : Dict = initializer_range SCREAMING_SNAKE_CASE : List[Any] = attention_dropout SCREAMING_SNAKE_CASE : List[str] = layer_norm_eps SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_act @classmethod def UpperCamelCase_ ( cls, A, **A ): '''simple docstring''' cls._set_token_in_kwargs(A ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = cls.get_config_dict(A, **A ) # get the vision config dict if we are loading from GITConfig if config_dict.get('model_type' ) == "git": SCREAMING_SNAKE_CASE : Optional[Any] = config_dict['vision_config'] if "model_type" in config_dict and hasattr(cls, 'model_type' ) and config_dict["model_type"] != cls.model_type: logger.warning( F"You are using a model of type {config_dict['model_type']} to instantiate a model of type " F"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(A, **A ) class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Optional[Any] = '''git''' def __init__( self, A=None, A=30_522, A=768, A=6, A=12, A=3_072, A="gelu", A=0.1, A=0.1, A=1_024, A=0.02, A=1E-12, A=0, A="absolute", A=True, A=False, A=101, A=102, A=None, **A, ): '''simple docstring''' super().__init__(bos_token_id=A, eos_token_id=A, pad_token_id=A, **A ) if vision_config is None: SCREAMING_SNAKE_CASE : List[str] = {} logger.info('vision_config is None. initializing the GitVisionConfig with default values.' ) SCREAMING_SNAKE_CASE : List[str] = GitVisionConfig(**A ) SCREAMING_SNAKE_CASE : List[str] = vocab_size SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : Any = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : Dict = hidden_act SCREAMING_SNAKE_CASE : List[Any] = intermediate_size SCREAMING_SNAKE_CASE : str = hidden_dropout_prob SCREAMING_SNAKE_CASE : Tuple = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : int = max_position_embeddings SCREAMING_SNAKE_CASE : List[str] = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : str = use_cache SCREAMING_SNAKE_CASE : int = tie_word_embeddings SCREAMING_SNAKE_CASE : Optional[int] = num_image_with_embedding SCREAMING_SNAKE_CASE : List[str] = bos_token_id SCREAMING_SNAKE_CASE : int = eos_token_id def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.vision_config.to_dict() SCREAMING_SNAKE_CASE : int = self.__class__.model_type return output
251
1
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase ( lowerCamelCase_ ): '''simple docstring''' SCREAMING_SNAKE_CASE_ : str = field(default="""audio-classification""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) SCREAMING_SNAKE_CASE_ : ClassVar[Features] = Features({"""audio""": Audio()} ) SCREAMING_SNAKE_CASE_ : ClassVar[Features] = Features({"""labels""": ClassLabel} ) SCREAMING_SNAKE_CASE_ : str = "audio" SCREAMING_SNAKE_CASE_ : str = "labels" def __A ( self , lowerCAmelCase__ ) -> 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] , lowerCAmelCase__ ): 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 ) -> Dict[str, str]: return { self.audio_column: "audio", self.label_column: "labels", }
38
"""simple docstring""" class lowerCAmelCase : '''simple docstring''' def __init__( self , lowerCAmelCase__ ) -> None: SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = [0] * size SCREAMING_SNAKE_CASE = [0] * size @staticmethod def __A ( lowerCAmelCase__ ) -> int: return index | (index + 1) @staticmethod def __A ( lowerCAmelCase__ ) -> int: return (index & (index + 1)) - 1 def __A ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> None: SCREAMING_SNAKE_CASE = value while index < self.size: SCREAMING_SNAKE_CASE = self.get_prev(lowerCAmelCase__ ) + 1 if current_left_border == index: SCREAMING_SNAKE_CASE = value else: SCREAMING_SNAKE_CASE = max(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE = self.get_next(lowerCAmelCase__ ) def __A ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> int: right -= 1 # Because of right is exclusive SCREAMING_SNAKE_CASE = 0 while left <= right: SCREAMING_SNAKE_CASE = self.get_prev(lowerCAmelCase__ ) if left <= current_left: SCREAMING_SNAKE_CASE = max(lowerCAmelCase__ , self.tree[right] ) SCREAMING_SNAKE_CASE = current_left else: SCREAMING_SNAKE_CASE = max(lowerCAmelCase__ , self.arr[right] ) right -= 1 return result if __name__ == "__main__": import doctest doctest.testmod()
38
1
from __future__ import annotations import unittest from transformers import FunnelConfig, is_tf_available from transformers.testing_utils import require_tf from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, ) class snake_case_ : def __init__( self : List[str] , lowercase_ : List[str] , lowercase_ : List[str]=13 , lowercase_ : Optional[int]=7 , lowercase_ : Optional[Any]=True , lowercase_ : str=True , lowercase_ : Optional[int]=True , lowercase_ : List[str]=True , lowercase_ : Any=99 , lowercase_ : Any=[1, 1, 2] , lowercase_ : List[str]=1 , lowercase_ : Union[str, Any]=32 , lowercase_ : Optional[Any]=4 , lowercase_ : Optional[int]=8 , lowercase_ : str=37 , lowercase_ : Tuple="gelu_new" , lowercase_ : Any=0.1 , lowercase_ : Dict=0.1 , lowercase_ : Optional[Any]=0.0 , lowercase_ : int=5_12 , lowercase_ : List[str]=3 , lowercase_ : List[Any]=0.02 , lowercase_ : str=3 , lowercase_ : Dict=4 , lowercase_ : List[str]=None , lowercase_ : Dict=False , ) -> Tuple: lowercase__ : List[Any] = parent lowercase__ : Union[str, Any] = batch_size lowercase__ : int = seq_length lowercase__ : Any = is_training lowercase__ : Dict = use_input_mask lowercase__ : Optional[int] = use_token_type_ids lowercase__ : str = use_labels lowercase__ : int = vocab_size lowercase__ : Dict = block_sizes lowercase__ : str = num_decoder_layers lowercase__ : str = d_model lowercase__ : List[str] = n_head lowercase__ : Union[str, Any] = d_head lowercase__ : List[str] = d_inner lowercase__ : int = hidden_act lowercase__ : Any = hidden_dropout lowercase__ : Optional[Any] = attention_dropout lowercase__ : int = activation_dropout lowercase__ : Tuple = max_position_embeddings lowercase__ : Optional[int] = type_vocab_size lowercase__ : Optional[int] = 2 lowercase__ : int = num_labels lowercase__ : Dict = num_choices lowercase__ : List[Any] = scope lowercase__ : Union[str, Any] = initializer_std # Used in the tests to check the size of the first attention layer lowercase__ : str = n_head # Used in the tests to check the size of the first hidden state lowercase__ : Optional[Any] = self.d_model # Used in the tests to check the number of output hidden states/attentions lowercase__ : List[Any] = sum(self.block_sizes ) + (0 if base else self.num_decoder_layers) # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with # the last hidden state of the first block (which is the first hidden state of the decoder). if not base: lowercase__ : Optional[int] = self.num_hidden_layers + 2 def __UpperCamelCase ( self : int ) -> Tuple: lowercase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ : str = None if self.use_input_mask: lowercase__ : int = random_attention_mask([self.batch_size, self.seq_length] ) lowercase__ : int = None if self.use_token_type_ids: lowercase__ : str = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowercase__ : Union[str, Any] = None lowercase__ : List[str] = None lowercase__ : Tuple = None if self.use_labels: lowercase__ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowercase__ : Dict = ids_tensor([self.batch_size] , self.num_choices ) lowercase__ : int = FunnelConfig( vocab_size=self.vocab_size , block_sizes=self.block_sizes , num_decoder_layers=self.num_decoder_layers , d_model=self.d_model , n_head=self.n_head , d_head=self.d_head , d_inner=self.d_inner , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , activation_dropout=self.activation_dropout , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_std=self.initializer_std , ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, ) def __UpperCamelCase ( self : Optional[int] , lowercase_ : Optional[int] , lowercase_ : Union[str, Any] , lowercase_ : Union[str, Any] , lowercase_ : Union[str, Any] , lowercase_ : Optional[int] , lowercase_ : Union[str, Any] , lowercase_ : Dict , ) -> str: lowercase__ : Optional[int] = TFFunnelModel(config=lowercase_ ) lowercase__ : int = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowercase__ : Tuple = model(lowercase_ ) lowercase__ : Any = [input_ids, input_mask] lowercase__ : Optional[int] = model(lowercase_ ) lowercase__ : Dict = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) lowercase__ : Any = False lowercase__ : List[str] = TFFunnelModel(config=lowercase_ ) lowercase__ : int = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) lowercase__ : List[Any] = False lowercase__ : Union[str, Any] = TFFunnelModel(config=lowercase_ ) lowercase__ : Optional[int] = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.d_model) ) def __UpperCamelCase ( self : Dict , lowercase_ : List[Any] , lowercase_ : Optional[Any] , lowercase_ : List[Any] , lowercase_ : Optional[Any] , lowercase_ : Dict , lowercase_ : Optional[int] , lowercase_ : Any , ) -> List[str]: lowercase__ : Optional[int] = TFFunnelBaseModel(config=lowercase_ ) lowercase__ : Dict = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowercase__ : Optional[int] = model(lowercase_ ) lowercase__ : Any = [input_ids, input_mask] lowercase__ : Dict = model(lowercase_ ) lowercase__ : Any = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 2, self.d_model) ) lowercase__ : Optional[int] = False lowercase__ : Union[str, Any] = TFFunnelBaseModel(config=lowercase_ ) lowercase__ : List[Any] = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 3, self.d_model) ) lowercase__ : Any = False lowercase__ : Any = TFFunnelBaseModel(config=lowercase_ ) lowercase__ : Tuple = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, 2, self.d_model) ) def __UpperCamelCase ( self : Any , lowercase_ : int , lowercase_ : Optional[int] , lowercase_ : Optional[Any] , lowercase_ : Union[str, Any] , lowercase_ : List[Any] , lowercase_ : Optional[int] , lowercase_ : Optional[Any] , ) -> Optional[Any]: lowercase__ : Tuple = TFFunnelForPreTraining(config=lowercase_ ) lowercase__ : List[Any] = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowercase__ : Dict = model(lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length) ) def __UpperCamelCase ( self : int , lowercase_ : List[Any] , lowercase_ : Any , lowercase_ : Optional[Any] , lowercase_ : Tuple , lowercase_ : Union[str, Any] , lowercase_ : Tuple , lowercase_ : Optional[int] , ) -> Union[str, Any]: lowercase__ : Dict = TFFunnelForMaskedLM(config=lowercase_ ) lowercase__ : Any = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowercase__ : Dict = model(lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCamelCase ( self : List[str] , lowercase_ : List[Any] , lowercase_ : List[Any] , lowercase_ : List[str] , lowercase_ : int , lowercase_ : Union[str, Any] , lowercase_ : int , lowercase_ : int , ) -> Dict: lowercase__ : Optional[int] = self.num_labels lowercase__ : List[Any] = TFFunnelForSequenceClassification(config=lowercase_ ) lowercase__ : Any = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowercase__ : Tuple = model(lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self : Optional[Any] , lowercase_ : Any , lowercase_ : Optional[Any] , lowercase_ : Dict , lowercase_ : List[Any] , lowercase_ : Dict , lowercase_ : List[Any] , lowercase_ : str , ) -> Any: lowercase__ : List[str] = self.num_choices lowercase__ : Optional[Any] = TFFunnelForMultipleChoice(config=lowercase_ ) lowercase__ : Dict = tf.tile(tf.expand_dims(lowercase_ , 1 ) , (1, self.num_choices, 1) ) lowercase__ : Any = tf.tile(tf.expand_dims(lowercase_ , 1 ) , (1, self.num_choices, 1) ) lowercase__ : int = tf.tile(tf.expand_dims(lowercase_ , 1 ) , (1, self.num_choices, 1) ) lowercase__ : int = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } lowercase__ : Dict = model(lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __UpperCamelCase ( self : int , lowercase_ : str , lowercase_ : str , lowercase_ : Union[str, Any] , lowercase_ : Optional[int] , lowercase_ : Any , lowercase_ : Dict , lowercase_ : str , ) -> str: lowercase__ : List[Any] = self.num_labels lowercase__ : List[str] = TFFunnelForTokenClassification(config=lowercase_ ) lowercase__ : Any = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowercase__ : Optional[Any] = model(lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCamelCase ( self : Tuple , lowercase_ : List[str] , lowercase_ : Tuple , lowercase_ : str , lowercase_ : List[str] , lowercase_ : List[str] , lowercase_ : str , lowercase_ : Any , ) -> int: lowercase__ : Optional[Any] = TFFunnelForQuestionAnswering(config=lowercase_ ) lowercase__ : Tuple = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowercase__ : int = model(lowercase_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __UpperCamelCase ( self : Dict ) -> Optional[Any]: lowercase__ : List[Any] = self.prepare_config_and_inputs() ( ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ( lowercase__ ) , ) : Optional[int] = config_and_inputs lowercase__ : Tuple = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class snake_case_ ( __A ,__A ,unittest.TestCase ): __A : Union[str, Any] = ( ( TFFunnelModel, TFFunnelForMaskedLM, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForTokenClassification, ) if is_tf_available() else () ) __A : Any = ( { "feature-extraction": (TFFunnelBaseModel, TFFunnelModel), "fill-mask": TFFunnelForMaskedLM, "question-answering": TFFunnelForQuestionAnswering, "text-classification": TFFunnelForSequenceClassification, "token-classification": TFFunnelForTokenClassification, "zero-shot": TFFunnelForSequenceClassification, } if is_tf_available() else {} ) __A : Optional[Any] = False __A : str = False def __UpperCamelCase ( self : Any ) -> Union[str, Any]: lowercase__ : int = TFFunnelModelTester(self ) lowercase__ : List[Any] = ConfigTester(self , config_class=lowercase_ ) def __UpperCamelCase ( self : Dict ) -> Dict: self.config_tester.run_common_tests() def __UpperCamelCase ( self : Optional[int] ) -> Union[str, Any]: lowercase__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowercase_ ) def __UpperCamelCase ( self : Tuple ) -> Any: lowercase__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*lowercase_ ) def __UpperCamelCase ( self : List[Any] ) -> List[str]: lowercase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*lowercase_ ) def __UpperCamelCase ( self : Dict ) -> Dict: lowercase__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowercase_ ) def __UpperCamelCase ( self : Union[str, Any] ) -> List[str]: lowercase__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowercase_ ) @require_tf class snake_case_ ( __A ,unittest.TestCase ): __A : Tuple = ( (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else () ) __A : Optional[Any] = False __A : List[str] = False def __UpperCamelCase ( self : Any ) -> Dict: lowercase__ : Union[str, Any] = TFFunnelModelTester(self , base=lowercase_ ) lowercase__ : Optional[int] = ConfigTester(self , config_class=lowercase_ ) def __UpperCamelCase ( self : str ) -> int: self.config_tester.run_common_tests() def __UpperCamelCase ( self : Optional[int] ) -> Dict: lowercase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_base_model(*lowercase_ ) def __UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: lowercase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowercase_ ) def __UpperCamelCase ( self : int ) -> Optional[int]: lowercase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*lowercase_ )
87
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowerCamelCase_ = '''0.12''' # assumed parallelism: 8 if is_torch_available(): import torch def __magic_name__ ( __a : Union[str, Any] , __a : Any , __a : Union[str, Any]=None ): '''simple docstring''' if rng is None: UpperCamelCase__ = random.Random() UpperCamelCase__ = 1 for dim in shape: total_dims *= dim UpperCamelCase__ = [] for _ in range(__a ): values.append(rng.randint(0 , vocab_size - 1 ) ) UpperCamelCase__ = np.array(__a , dtype=jnp.intaa ).reshape(__a ) return output def __magic_name__ ( __a : Dict , __a : Tuple=None ): '''simple docstring''' UpperCamelCase__ = ids_tensor(__a , vocab_size=2 , rng=__a ) # make sure that at least one token is attended to for each batch UpperCamelCase__ = 1 return attn_mask @require_flax class __A: """simple docstring""" SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = () def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 UpperCamelCase__ = 2 UpperCamelCase__ = inputs["""input_ids"""].shape[-1] // 2 UpperCamelCase__ = inputs["""input_ids"""][:max_batch_size, :sequence_length] UpperCamelCase__ = jnp.ones_like(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens UpperCamelCase__ = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` UpperCamelCase__ = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = False UpperCamelCase__ = max_length UpperCamelCase__ = 0 for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model_class.__name__[4:] # Skip the "Flax" at the beginning UpperCamelCase__ = getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = pt_model_class(SCREAMING_SNAKE_CASE_ ).eval() UpperCamelCase__ = load_flax_weights_in_pytorch_model(SCREAMING_SNAKE_CASE_ , flax_model.params ) UpperCamelCase__ = flax_model.generate(SCREAMING_SNAKE_CASE_ ).sequences UpperCamelCase__ = pt_model.generate(torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=torch.long ) ) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: UpperCamelCase__ = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = False UpperCamelCase__ = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = True UpperCamelCase__ = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = False UpperCamelCase__ = max_length UpperCamelCase__ = 2 for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = False UpperCamelCase__ = max_length UpperCamelCase__ = 2 UpperCamelCase__ = 2 for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = True UpperCamelCase__ = max_length UpperCamelCase__ = 0.8 UpperCamelCase__ = 10 UpperCamelCase__ = 0.3 UpperCamelCase__ = 1 UpperCamelCase__ = 8 UpperCamelCase__ = 9 for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = max_length UpperCamelCase__ = 1 UpperCamelCase__ = 8 UpperCamelCase__ = 9 for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() UpperCamelCase__ = max_length UpperCamelCase__ = 2 UpperCamelCase__ = 1 UpperCamelCase__ = 8 UpperCamelCase__ = 9 for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() # pad attention mask on the left UpperCamelCase__ = attention_mask.at[(0, 0)].set(0 ) UpperCamelCase__ = False UpperCamelCase__ = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() # pad attention mask on the left UpperCamelCase__ = attention_mask.at[(0, 0)].set(0 ) UpperCamelCase__ = True UpperCamelCase__ = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self._get_input_ids_and_config() # pad attention mask on the left UpperCamelCase__ = attention_mask.at[(0, 0)].set(0 ) UpperCamelCase__ = 2 UpperCamelCase__ = max_length for model_class in self.all_generative_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ).sequences self.assertEqual(generation_outputs.shape[-1] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = jit(model.generate ) UpperCamelCase__ = jit_generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) @require_flax class __A( unittest.TestCase ): """simple docstring""" def UpperCAmelCase_ (self ): UpperCamelCase__ = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-bert""" ) UpperCamelCase__ = FlaxAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-bert-flax-only""" ) UpperCamelCase__ = """Hello world""" UpperCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors="""np""" ).input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(SCREAMING_SNAKE_CASE_ , """do_samples""" ): model.generate(SCREAMING_SNAKE_CASE_ , do_samples=SCREAMING_SNAKE_CASE_ ) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(SCREAMING_SNAKE_CASE_ , """foo""" ): UpperCamelCase__ = {"""foo""": """bar"""} model.generate(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
244
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = { 'facebook/nllb-moe-54B': 'https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json', } class _SCREAMING_SNAKE_CASE ( A__ ): UpperCAmelCase_ :str = "nllb-moe" UpperCAmelCase_ :List[Any] = ["past_key_values"] UpperCAmelCase_ :Any = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self , __A=12_8112 , __A=1024 , __A=12 , __A=4096 , __A=16 , __A=12 , __A=4096 , __A=16 , __A=0.0_5 , __A=0.0_5 , __A=True , __A=True , __A="relu" , __A=1024 , __A=0.1 , __A=0.1 , __A=0.0 , __A=0.0_2 , __A=2 , __A=True , __A=False , __A="float32" , __A=False , __A=128 , __A=64 , __A=4 , __A=4 , __A=0.0_0_1 , __A=0.0_0_1 , __A="all" , __A=False , __A=False , __A=1.0 , __A=0.2 , __A=1 , __A=0 , __A=2 , __A=False , **__A , ) -> List[str]: lowerCAmelCase_ :Dict = vocab_size lowerCAmelCase_ :Optional[int] = max_position_embeddings lowerCAmelCase_ :Tuple = d_model lowerCAmelCase_ :Tuple = encoder_ffn_dim lowerCAmelCase_ :Optional[Any] = encoder_layers lowerCAmelCase_ :str = encoder_attention_heads lowerCAmelCase_ :List[str] = decoder_ffn_dim lowerCAmelCase_ :Any = decoder_layers lowerCAmelCase_ :Dict = decoder_attention_heads lowerCAmelCase_ :Any = dropout lowerCAmelCase_ :Dict = attention_dropout lowerCAmelCase_ :int = activation_dropout lowerCAmelCase_ :str = activation_function lowerCAmelCase_ :Tuple = init_std lowerCAmelCase_ :Any = encoder_layerdrop lowerCAmelCase_ :List[Any] = decoder_layerdrop lowerCAmelCase_ :int = use_cache lowerCAmelCase_ :int = encoder_layers lowerCAmelCase_ :Tuple = scale_embedding # scale factor will be sqrt(d_model) if True lowerCAmelCase_ :List[str] = router_z_loss_coef lowerCAmelCase_ :Any = router_aux_loss_coef lowerCAmelCase_ :str = decoder_sparse_step lowerCAmelCase_ :Union[str, Any] = encoder_sparse_step lowerCAmelCase_ :str = num_experts lowerCAmelCase_ :int = expert_capacity lowerCAmelCase_ :Any = router_bias if router_dtype not in ["float32", "float16", "bfloat16"]: raise ValueError(f"""`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}""" ) lowerCAmelCase_ :List[Any] = router_dtype lowerCAmelCase_ :Optional[int] = router_ignore_padding_tokens lowerCAmelCase_ :List[str] = batch_prioritized_routing lowerCAmelCase_ :Optional[int] = second_expert_policy lowerCAmelCase_ :Union[str, Any] = normalize_router_prob_before_dropping lowerCAmelCase_ :Dict = moe_eval_capacity_token_fraction lowerCAmelCase_ :List[Any] = moe_token_dropout lowerCAmelCase_ :int = output_router_logits super().__init__( pad_token_id=__A , bos_token_id=__A , eos_token_id=__A , is_encoder_decoder=__A , decoder_start_token_id=__A , **__A , )
368
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __UpperCAmelCase = {'configuration_deit': ['DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DeiTConfig', 'DeiTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = ['DeiTFeatureExtractor'] __UpperCAmelCase = ['DeiTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ 'DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'DeiTForImageClassification', 'DeiTForImageClassificationWithTeacher', 'DeiTForMaskedImageModeling', 'DeiTModel', 'DeiTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ 'TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFDeiTForImageClassification', 'TFDeiTForImageClassificationWithTeacher', 'TFDeiTForMaskedImageModeling', 'TFDeiTModel', 'TFDeiTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys __UpperCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
1
0
'''simple docstring''' import argparse import copy def lowerCamelCase_ ( SCREAMING_SNAKE_CASE__ : List[Any] ) -> List[str]: UpperCAmelCase_ : List[Any] = {} with open(SCREAMING_SNAKE_CASE__ ) as f: for line in f: if line.split()[0] not in dict_of_neighbours: UpperCAmelCase_ : List[str] = [] _list.append([line.split()[1], line.split()[2]] ) UpperCAmelCase_ : str = _list else: dict_of_neighbours[line.split()[0]].append( [line.split()[1], line.split()[2]] ) if line.split()[1] not in dict_of_neighbours: UpperCAmelCase_ : Optional[Any] = [] _list.append([line.split()[0], line.split()[2]] ) UpperCAmelCase_ : str = _list else: dict_of_neighbours[line.split()[1]].append( [line.split()[0], line.split()[2]] ) return dict_of_neighbours def lowerCamelCase_ ( SCREAMING_SNAKE_CASE__ : Any, SCREAMING_SNAKE_CASE__ : Optional[Any] ) -> Dict: with open(SCREAMING_SNAKE_CASE__ ) as f: UpperCAmelCase_ : Tuple = f.read(1 ) UpperCAmelCase_ : int = start_node UpperCAmelCase_ : Dict = [] UpperCAmelCase_ : Tuple = start_node UpperCAmelCase_ : Optional[Any] = 0 while visiting not in first_solution: UpperCAmelCase_ : Dict = 10000 for k in dict_of_neighbours[visiting]: if int(k[1] ) < int(SCREAMING_SNAKE_CASE__ ) and k[0] not in first_solution: UpperCAmelCase_ : Union[str, Any] = k[1] UpperCAmelCase_ : Any = k[0] first_solution.append(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_ : Dict = distance_of_first_solution + int(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_ : Tuple = best_node first_solution.append(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_ : Tuple = 0 for k in dict_of_neighbours[first_solution[-2]]: if k[0] == start_node: break position += 1 UpperCAmelCase_ : List[str] = ( distance_of_first_solution + int(dict_of_neighbours[first_solution[-2]][position][1] ) - 10000 ) return first_solution, distance_of_first_solution def lowerCamelCase_ ( SCREAMING_SNAKE_CASE__ : List[str], SCREAMING_SNAKE_CASE__ : Any ) -> Any: UpperCAmelCase_ : Union[str, Any] = [] for n in solution[1:-1]: UpperCAmelCase_ : Tuple = solution.index(SCREAMING_SNAKE_CASE__ ) for kn in solution[1:-1]: UpperCAmelCase_ : List[str] = solution.index(SCREAMING_SNAKE_CASE__ ) if n == kn: continue UpperCAmelCase_ : Union[str, Any] = copy.deepcopy(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_ : List[str] = kn UpperCAmelCase_ : Dict = n UpperCAmelCase_ : Tuple = 0 for k in _tmp[:-1]: UpperCAmelCase_ : str = _tmp[_tmp.index(SCREAMING_SNAKE_CASE__ ) + 1] for i in dict_of_neighbours[k]: if i[0] == next_node: UpperCAmelCase_ : List[Any] = distance + int(i[1] ) _tmp.append(SCREAMING_SNAKE_CASE__ ) if _tmp not in neighborhood_of_solution: neighborhood_of_solution.append(_tmp ) UpperCAmelCase_ : Dict = len(neighborhood_of_solution[0] ) - 1 neighborhood_of_solution.sort(key=lambda SCREAMING_SNAKE_CASE__ : x[index_of_last_item_in_the_list] ) return neighborhood_of_solution def lowerCamelCase_ ( SCREAMING_SNAKE_CASE__ : Optional[int], SCREAMING_SNAKE_CASE__ : Dict, SCREAMING_SNAKE_CASE__ : Any, SCREAMING_SNAKE_CASE__ : Optional[int], SCREAMING_SNAKE_CASE__ : Union[str, Any] ) -> Any: UpperCAmelCase_ : Optional[Any] = 1 UpperCAmelCase_ : Any = first_solution UpperCAmelCase_ : Dict = [] UpperCAmelCase_ : str = distance_of_first_solution UpperCAmelCase_ : Union[str, Any] = solution while count <= iters: UpperCAmelCase_ : int = find_neighborhood(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_ : int = 0 UpperCAmelCase_ : List[Any] = neighborhood[index_of_best_solution] UpperCAmelCase_ : Dict = len(SCREAMING_SNAKE_CASE__ ) - 1 UpperCAmelCase_ : Any = False while not found: UpperCAmelCase_ : List[Any] = 0 while i < len(SCREAMING_SNAKE_CASE__ ): if best_solution[i] != solution[i]: UpperCAmelCase_ : Optional[int] = best_solution[i] UpperCAmelCase_ : Any = solution[i] break UpperCAmelCase_ : Dict = i + 1 if [first_exchange_node, second_exchange_node] not in tabu_list and [ second_exchange_node, first_exchange_node, ] not in tabu_list: tabu_list.append([first_exchange_node, second_exchange_node] ) UpperCAmelCase_ : Optional[Any] = True UpperCAmelCase_ : Union[str, Any] = best_solution[:-1] UpperCAmelCase_ : Any = neighborhood[index_of_best_solution][best_cost_index] if cost < best_cost: UpperCAmelCase_ : Tuple = cost UpperCAmelCase_ : int = solution else: UpperCAmelCase_ : Optional[Any] = index_of_best_solution + 1 UpperCAmelCase_ : Any = neighborhood[index_of_best_solution] if len(SCREAMING_SNAKE_CASE__ ) >= size: tabu_list.pop(0 ) UpperCAmelCase_ : Any = count + 1 return best_solution_ever, best_cost def lowerCamelCase_ ( SCREAMING_SNAKE_CASE__ : List[str]=None ) -> str: UpperCAmelCase_ : str = generate_neighbours(args.File ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = generate_first_solution( args.File, SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_ , UpperCAmelCase_ : Any = tabu_search( SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, args.Iterations, args.Size, ) print(F"""Best solution: {best_sol}, with total distance: {best_cost}.""" ) if __name__ == "__main__": snake_case_ : Tuple = argparse.ArgumentParser(description="Tabu Search") parser.add_argument( "-f", "--File", type=str, help="Path to the file containing the data", required=True, ) parser.add_argument( "-i", "--Iterations", type=int, help="How many iterations the algorithm should perform", required=True, ) parser.add_argument( "-s", "--Size", type=int, help="Size of the tabu list", required=True ) # Pass the arguments to main method main(parser.parse_args())
125
# 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__ : List[str] = { 'configuration_vivit': ['VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VivitConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case__ : Tuple = ['VivitImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case__ : Any = [ '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__ : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
117
0
def lowerCAmelCase__ ( a__ , a__ = 0 ) ->list: '''simple docstring''' _UpperCamelCase = length or len(a__ ) _UpperCamelCase = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: _UpperCamelCase , _UpperCamelCase = list_data[i + 1], list_data[i] _UpperCamelCase = True return list_data if not swapped else bubble_sort(a__ , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
63
import argparse import torch from transformers import MobileBertConfig, MobileBertForPreTraining, load_tf_weights_in_mobilebert from transformers.utils import logging logging.set_verbosity_info() def lowerCAmelCase__ ( a__ , a__ , a__ ) ->int: '''simple docstring''' _UpperCamelCase = MobileBertConfig.from_json_file(a__ ) print(f'Building PyTorch model from configuration: {config}' ) _UpperCamelCase = MobileBertForPreTraining(a__ ) # Load weights from tf checkpoint _UpperCamelCase = load_tf_weights_in_mobilebert(a__ , a__ , a__ ) # Save pytorch-model print(f'Save PyTorch model to {pytorch_dump_path}' ) torch.save(model.state_dict() , a__ ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--mobilebert_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained MobileBERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) lowerCamelCase__ = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.mobilebert_config_file, args.pytorch_dump_path)
63
1
"""simple docstring""" import socket def _snake_case ( ): _lowerCamelCase : List[Any] = socket.socket(socket.AF_INET , socket.SOCK_STREAM ) _lowerCamelCase : Union[str, Any] = socket.gethostname() _lowerCamelCase : List[Any] = 12312 sock.connect((host, port) ) sock.send(B'Hello server!' ) with open('Received_file' , 'wb' ) as out_file: print('File opened' ) print('Receiving data...' ) while True: _lowerCamelCase : int = sock.recv(1024 ) if not data: break out_file.write(lowercase__ ) print('Successfully received the file' ) sock.close() print('Connection closed' ) if __name__ == "__main__": main()
96
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tensorflow_text_available, is_torch_available _lowerCamelCase ={ "configuration_ernie": ["ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP", "ErnieConfig", "ErnieOnnxConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase =[ "ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST", "ErnieForCausalLM", "ErnieForMaskedLM", "ErnieForMultipleChoice", "ErnieForNextSentencePrediction", "ErnieForPreTraining", "ErnieForQuestionAnswering", "ErnieForSequenceClassification", "ErnieForTokenClassification", "ErnieModel", "ErniePreTrainedModel", ] if TYPE_CHECKING: from .configuration_ernie import ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieConfig, ErnieOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ernie import ( ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST, ErnieForCausalLM, ErnieForMaskedLM, ErnieForMultipleChoice, ErnieForNextSentencePrediction, ErnieForPreTraining, ErnieForQuestionAnswering, ErnieForSequenceClassification, ErnieForTokenClassification, ErnieModel, ErniePreTrainedModel, ) else: import sys _lowerCamelCase =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
334
0
lowercase_ = [ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] lowercase_ = [ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] lowercase_ = [ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] lowercase_ = [ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] lowercase_ = [ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] lowercase_ = [ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] lowercase_ = [ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] lowercase_ = [ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
269
import sys lowercase_ = ( """73167176531330624919225119674426574742355349194934""" """96983520312774506326239578318016984801869478851843""" """85861560789112949495459501737958331952853208805511""" """12540698747158523863050715693290963295227443043557""" """66896648950445244523161731856403098711121722383113""" """62229893423380308135336276614282806444486645238749""" """30358907296290491560440772390713810515859307960866""" """70172427121883998797908792274921901699720888093776""" """65727333001053367881220235421809751254540594752243""" """52584907711670556013604839586446706324415722155397""" """53697817977846174064955149290862569321978468622482""" """83972241375657056057490261407972968652414535100474""" """82166370484403199890008895243450658541227588666881""" """16427171479924442928230863465674813919123162824586""" """17866458359124566529476545682848912883142607690042""" """24219022671055626321111109370544217506941658960408""" """07198403850962455444362981230987879927244284909188""" """84580156166097919133875499200524063689912560717606""" """05886116467109405077541002256983155200055935729725""" """71636269561882670428252483600823257530420752963450""" ) def __UpperCamelCase (_SCREAMING_SNAKE_CASE = N ) -> int: lowercase__ = -sys.maxsize - 1 for i in range(len(_SCREAMING_SNAKE_CASE ) - 12 ): lowercase__ = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: lowercase__ = product return largest_product if __name__ == "__main__": print(f'''{solution() = }''')
269
1
import random def UpperCAmelCase__ (UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_ = False ): """simple docstring""" snake_case = {i: [] for i in range(__A )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(__A ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(__A ): for j in range(i + 1 ,__A ): if random.random() < probability: graph[i].append(__A ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(__A ) return graph def UpperCAmelCase__ (UpperCamelCase_ ): """simple docstring""" return { i: [j for j in range(__A ) if i != j] for i in range(__A ) } if __name__ == "__main__": import doctest doctest.testmod()
127
'''simple docstring''' from __future__ import annotations def _UpperCamelCase ( __A , __A , __A ) -> dict[str, float]: '''simple docstring''' if (voltage, current, resistance).count(0 ) != 1: raise ValueError("One and only one argument must be 0" ) if resistance < 0: raise ValueError("Resistance cannot be negative" ) if voltage == 0: return {"voltage": float(current * resistance )} elif current == 0: return {"current": voltage / resistance} elif resistance == 0: return {"resistance": voltage / current} else: raise ValueError("Exactly one argument must be 0" ) if __name__ == "__main__": import doctest doctest.testmod()
80
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ = { '''configuration_swinv2''': ['''SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Swinv2Config'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Swinv2ForImageClassification''', '''Swinv2ForMaskedImageModeling''', '''Swinv2Model''', '''Swinv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
369
from copy import deepcopy from typing import Optional, Union import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, is_tf_available, is_torch_available if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf class a_ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' UpperCAmelCase_ = ['image_processor'] UpperCAmelCase_ = 'SamImageProcessor' def __init__( self : Tuple , lowercase__ : Dict): '''simple docstring''' super().__init__(lowercase__) lowerCAmelCase__ = self.image_processor lowerCAmelCase__ = -10 lowerCAmelCase__ = self.image_processor.size['longest_edge'] def __call__( self : List[Any] , lowercase__ : Optional[int]=None , lowercase__ : Any=None , lowercase__ : Tuple=None , lowercase__ : List[str]=None , lowercase__ : Optional[Union[str, TensorType]] = None , **lowercase__ : Dict , ): '''simple docstring''' lowerCAmelCase__ = self.image_processor( lowercase__ , return_tensors=lowercase__ , **lowercase__ , ) # pop arguments that are not used in the foward but used nevertheless lowerCAmelCase__ = encoding_image_processor['original_sizes'] if hasattr(lowercase__ , 'numpy'): # Checks if Torch or TF tensor lowerCAmelCase__ = original_sizes.numpy() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = self._check_and_preprocess_points( input_points=lowercase__ , input_labels=lowercase__ , input_boxes=lowercase__ , ) lowerCAmelCase__ = self._normalize_and_convert( lowercase__ , lowercase__ , input_points=lowercase__ , input_labels=lowercase__ , input_boxes=lowercase__ , return_tensors=lowercase__ , ) return encoding_image_processor def __snake_case ( self : Optional[Any] , lowercase__ : List[str] , lowercase__ : List[str] , lowercase__ : str=None , lowercase__ : Optional[int]=None , lowercase__ : str=None , lowercase__ : Optional[Any]="pt" , ): '''simple docstring''' if input_points is not None: if len(lowercase__) != len(lowercase__): lowerCAmelCase__ = [ self._normalize_coordinates(self.target_size , lowercase__ , original_sizes[0]) for point in input_points ] else: lowerCAmelCase__ = [ self._normalize_coordinates(self.target_size , lowercase__ , lowercase__) for point, original_size in zip(lowercase__ , lowercase__) ] # check that all arrays have the same shape if not all(point.shape == input_points[0].shape for point in input_points): if input_labels is not None: lowerCAmelCase__ , lowerCAmelCase__ = self._pad_points_and_labels(lowercase__ , lowercase__) lowerCAmelCase__ = np.array(lowercase__) if input_labels is not None: lowerCAmelCase__ = np.array(lowercase__) if input_boxes is not None: if len(lowercase__) != len(lowercase__): lowerCAmelCase__ = [ self._normalize_coordinates(self.target_size , lowercase__ , original_sizes[0] , is_bounding_box=lowercase__) for box in input_boxes ] else: lowerCAmelCase__ = [ self._normalize_coordinates(self.target_size , lowercase__ , lowercase__ , is_bounding_box=lowercase__) for box, original_size in zip(lowercase__ , lowercase__) ] lowerCAmelCase__ = np.array(lowercase__) if input_boxes is not None: if return_tensors == "pt": lowerCAmelCase__ = torch.from_numpy(lowercase__) # boxes batch size of 1 by default lowerCAmelCase__ = input_boxes.unsqueeze(1) if len(input_boxes.shape) != 3 else input_boxes elif return_tensors == "tf": lowerCAmelCase__ = tf.convert_to_tensor(lowercase__) # boxes batch size of 1 by default lowerCAmelCase__ = tf.expand_dims(lowercase__ , 1) if len(input_boxes.shape) != 3 else input_boxes encoding_image_processor.update({'input_boxes': input_boxes}) if input_points is not None: if return_tensors == "pt": lowerCAmelCase__ = torch.from_numpy(lowercase__) # point batch size of 1 by default lowerCAmelCase__ = input_points.unsqueeze(1) if len(input_points.shape) != 4 else input_points elif return_tensors == "tf": lowerCAmelCase__ = tf.convert_to_tensor(lowercase__) # point batch size of 1 by default lowerCAmelCase__ = tf.expand_dims(lowercase__ , 1) if len(input_points.shape) != 4 else input_points encoding_image_processor.update({'input_points': input_points}) if input_labels is not None: if return_tensors == "pt": lowerCAmelCase__ = torch.from_numpy(lowercase__) # point batch size of 1 by default lowerCAmelCase__ = input_labels.unsqueeze(1) if len(input_labels.shape) != 3 else input_labels elif return_tensors == "tf": lowerCAmelCase__ = tf.convert_to_tensor(lowercase__) # point batch size of 1 by default lowerCAmelCase__ = tf.expand_dims(lowercase__ , 1) if len(input_labels.shape) != 3 else input_labels encoding_image_processor.update({'input_labels': input_labels}) return encoding_image_processor def __snake_case ( self : str , lowercase__ : Optional[int] , lowercase__ : Optional[Any]): '''simple docstring''' lowerCAmelCase__ = max([point.shape[0] for point in input_points]) lowerCAmelCase__ = [] for i, point in enumerate(lowercase__): if point.shape[0] != expected_nb_points: lowerCAmelCase__ = np.concatenate( [point, np.zeros((expected_nb_points - point.shape[0], 2)) + self.point_pad_value] , axis=0) lowerCAmelCase__ = np.append(input_labels[i] , [self.point_pad_value]) processed_input_points.append(lowercase__) lowerCAmelCase__ = processed_input_points return input_points, input_labels def __snake_case ( self : Optional[Any] , lowercase__ : int , lowercase__ : np.ndarray , lowercase__ : int , lowercase__ : Optional[Any]=False): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ = original_size lowerCAmelCase__ , lowerCAmelCase__ = self.image_processor._get_preprocess_shape(lowercase__ , longest_edge=lowercase__) lowerCAmelCase__ = deepcopy(lowercase__).astype(lowercase__) if is_bounding_box: lowerCAmelCase__ = coords.reshape(-1 , 2 , 2) lowerCAmelCase__ = coords[..., 0] * (new_w / old_w) lowerCAmelCase__ = coords[..., 1] * (new_h / old_h) if is_bounding_box: lowerCAmelCase__ = coords.reshape(-1 , 4) return coords def __snake_case ( self : Dict , lowercase__ : Optional[Any]=None , lowercase__ : Tuple=None , lowercase__ : int=None , ): '''simple docstring''' if input_points is not None: if hasattr(lowercase__ , 'numpy'): # Checks for TF or Torch tensor lowerCAmelCase__ = input_points.numpy().tolist() if not isinstance(lowercase__ , lowercase__) or not isinstance(input_points[0] , lowercase__): raise ValueError('Input points must be a list of list of floating points.') lowerCAmelCase__ = [np.array(lowercase__) for input_point in input_points] else: lowerCAmelCase__ = None if input_labels is not None: if hasattr(lowercase__ , 'numpy'): lowerCAmelCase__ = input_labels.numpy().tolist() if not isinstance(lowercase__ , lowercase__) or not isinstance(input_labels[0] , lowercase__): raise ValueError('Input labels must be a list of list integers.') lowerCAmelCase__ = [np.array(lowercase__) for label in input_labels] else: lowerCAmelCase__ = None if input_boxes is not None: if hasattr(lowercase__ , 'numpy'): lowerCAmelCase__ = input_boxes.numpy().tolist() if ( not isinstance(lowercase__ , lowercase__) or not isinstance(input_boxes[0] , lowercase__) or not isinstance(input_boxes[0][0] , lowercase__) ): raise ValueError('Input boxes must be a list of list of list of floating points.') lowerCAmelCase__ = [np.array(lowercase__).astype(np.floataa) for box in input_boxes] else: lowerCAmelCase__ = None return input_points, input_labels, input_boxes @property def __snake_case ( self : List[Any]): '''simple docstring''' lowerCAmelCase__ = self.image_processor.model_input_names return list(dict.fromkeys(lowercase__)) def __snake_case ( self : int , *lowercase__ : int , **lowercase__ : int): '''simple docstring''' return self.image_processor.post_process_masks(*lowercase__ , **lowercase__)
119
0
from __future__ import annotations def lowercase_ ( _A : list ): """simple docstring""" if not nums: raise ValueError("List is empty" ) return sum(_A ) / len(_A ) if __name__ == "__main__": import doctest doctest.testmod()
184
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=lowercase__) class _lowercase ( lowercase__): """simple docstring""" A__ = field(default="automatic-speech-recognition" , metadata={"include_in_asdict_even_if_is_default": True}) A__ = Features({"audio": Audio()}) A__ = Features({"transcription": Value("string")}) A__ = "audio" A__ = "transcription" def lowerCAmelCase ( self : Any , __lowerCamelCase : int ): '''simple docstring''' if self.audio_column not in features: raise ValueError(f"Column {self.audio_column} is not present in features." ) if not isinstance(features[self.audio_column] , __lowerCamelCase ): raise ValueError(f"Column {self.audio_column} is not an Audio type." ) lowerCamelCase__ : Tuple = copy.deepcopy(self ) lowerCamelCase__ : Tuple = self.input_schema.copy() lowerCamelCase__ : Optional[int] = features[self.audio_column] lowerCamelCase__ : int = input_schema return task_template @property def lowerCAmelCase ( self : int ): '''simple docstring''' return {self.audio_column: "audio", self.transcription_column: "transcription"}
184
1
'''simple docstring''' import argparse import collections import os import re import tempfile import pandas as pd from datasets import Dataset from huggingface_hub import hf_hub_download, upload_folder from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/update_metadata.py _A : List[Any] ="src/transformers" # This is to make sure the transformers module imported is the one in the repo. _A : Tuple =direct_transformers_import(TRANSFORMERS_PATH) # Regexes that match TF/Flax/PT model names. _A : Dict =re.compile(r'''TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') _A : List[str] =re.compile(r'''Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') # Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes. _A : Any =re.compile(r'''(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') # Fill this with tuples (pipeline_tag, model_mapping, auto_model) _A : List[Any] =[ ("pretraining", "MODEL_FOR_PRETRAINING_MAPPING_NAMES", "AutoModelForPreTraining"), ("feature-extraction", "MODEL_MAPPING_NAMES", "AutoModel"), ("audio-classification", "MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES", "AutoModelForAudioClassification"), ("text-generation", "MODEL_FOR_CAUSAL_LM_MAPPING_NAMES", "AutoModelForCausalLM"), ("automatic-speech-recognition", "MODEL_FOR_CTC_MAPPING_NAMES", "AutoModelForCTC"), ("image-classification", "MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES", "AutoModelForImageClassification"), ("image-segmentation", "MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES", "AutoModelForImageSegmentation"), ("fill-mask", "MODEL_FOR_MASKED_LM_MAPPING_NAMES", "AutoModelForMaskedLM"), ("object-detection", "MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES", "AutoModelForObjectDetection"), ( "zero-shot-object-detection", "MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES", "AutoModelForZeroShotObjectDetection", ), ("question-answering", "MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES", "AutoModelForQuestionAnswering"), ("text2text-generation", "MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES", "AutoModelForSeq2SeqLM"), ("text-classification", "MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES", "AutoModelForSequenceClassification"), ("automatic-speech-recognition", "MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES", "AutoModelForSpeechSeq2Seq"), ( "table-question-answering", "MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES", "AutoModelForTableQuestionAnswering", ), ("token-classification", "MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES", "AutoModelForTokenClassification"), ("multiple-choice", "MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES", "AutoModelForMultipleChoice"), ( "next-sentence-prediction", "MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES", "AutoModelForNextSentencePrediction", ), ( "audio-frame-classification", "MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES", "AutoModelForAudioFrameClassification", ), ("audio-xvector", "MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES", "AutoModelForAudioXVector"), ( "document-question-answering", "MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES", "AutoModelForDocumentQuestionAnswering", ), ( "visual-question-answering", "MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING_NAMES", "AutoModelForVisualQuestionAnswering", ), ("image-to-text", "MODEL_FOR_FOR_VISION_2_SEQ_MAPPING_NAMES", "AutoModelForVision2Seq"), ( "zero-shot-image-classification", "MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES", "AutoModelForZeroShotImageClassification", ), ("depth-estimation", "MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES", "AutoModelForDepthEstimation"), ("video-classification", "MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES", "AutoModelForVideoClassification"), ("mask-generation", "MODEL_FOR_MASK_GENERATION_MAPPING_NAMES", "AutoModelForMaskGeneration"), ] def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> Dict: lowerCamelCase__ : str = re.finditer(""".+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)""" , lowerCamelCase_ ) return [m.group(0 ) for m in matches] def SCREAMING_SNAKE_CASE_ () -> Dict: lowerCamelCase__ : Optional[Any] = transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES lowerCamelCase__ : Any = { config.replace("""Config""" , """""" ): model_type for model_type, config in config_maping_names.items() } # Dictionaries flagging if each model prefix has a backend in PT/TF/Flax. lowerCamelCase__ : Union[str, Any] = collections.defaultdict(lowerCamelCase_ ) lowerCamelCase__ : Any = collections.defaultdict(lowerCamelCase_ ) lowerCamelCase__ : int = collections.defaultdict(lowerCamelCase_ ) # Let's lookup through all transformers object (once) and find if models are supported by a given backend. for attr_name in dir(lowerCamelCase_ ): lowerCamelCase__ : Union[str, Any] = None if _re_tf_models.match(lowerCamelCase_ ) is not None: lowerCamelCase__ : Any = tf_models lowerCamelCase__ : int = _re_tf_models.match(lowerCamelCase_ ).groups()[0] elif _re_flax_models.match(lowerCamelCase_ ) is not None: lowerCamelCase__ : List[str] = flax_models lowerCamelCase__ : Tuple = _re_flax_models.match(lowerCamelCase_ ).groups()[0] elif _re_pt_models.match(lowerCamelCase_ ) is not None: lowerCamelCase__ : List[Any] = pt_models lowerCamelCase__ : int = _re_pt_models.match(lowerCamelCase_ ).groups()[0] if lookup_dict is not None: while len(lowerCamelCase_ ) > 0: if attr_name in model_prefix_to_model_type: lowerCamelCase__ : Tuple = True break # Try again after removing the last word in the name lowerCamelCase__ : List[Any] = ''.join(camel_case_split(lowerCamelCase_ )[:-1] ) lowerCamelCase__ : List[Any] = set(list(pt_models.keys() ) + list(tf_models.keys() ) + list(flax_models.keys() ) ) lowerCamelCase__ : Dict = list(lowerCamelCase_ ) all_models.sort() lowerCamelCase__ : Tuple = {'model_type': all_models} lowerCamelCase__ : Dict = [pt_models[t] for t in all_models] lowerCamelCase__ : List[Any] = [tf_models[t] for t in all_models] lowerCamelCase__ : Any = [flax_models[t] for t in all_models] # Now let's use the auto-mapping names to make sure lowerCamelCase__ : str = {} for t in all_models: if t in transformers_module.models.auto.processing_auto.PROCESSOR_MAPPING_NAMES: lowerCamelCase__ : int = 'AutoProcessor' elif t in transformers_module.models.auto.tokenization_auto.TOKENIZER_MAPPING_NAMES: lowerCamelCase__ : Union[str, Any] = 'AutoTokenizer' elif t in transformers_module.models.auto.feature_extraction_auto.FEATURE_EXTRACTOR_MAPPING_NAMES: lowerCamelCase__ : Optional[Any] = 'AutoFeatureExtractor' else: # Default to AutoTokenizer if a model has nothing, for backward compatibility. lowerCamelCase__ : Optional[int] = 'AutoTokenizer' lowerCamelCase__ : Any = [processors[t] for t in all_models] return pd.DataFrame(lowerCamelCase_ ) def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> Any: lowerCamelCase__ : Union[str, Any] = [ transformers_module.models.auto.modeling_auto, transformers_module.models.auto.modeling_tf_auto, transformers_module.models.auto.modeling_flax_auto, ] for pipeline_tag, model_mapping, auto_class in PIPELINE_TAGS_AND_AUTO_MODELS: lowerCamelCase__ : int = [model_mapping, f'''TF_{model_mapping}''', f'''FLAX_{model_mapping}'''] lowerCamelCase__ : List[str] = [auto_class, f'''TF_{auto_class}''', f'''Flax_{auto_class}'''] # Loop through all three frameworks for module, cls, mapping in zip(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): # The type of pipeline may not exist in this framework if not hasattr(lowerCamelCase_ , lowerCamelCase_ ): continue # First extract all model_names lowerCamelCase__ : Union[str, Any] = [] for name in getattr(lowerCamelCase_ , lowerCamelCase_ ).values(): if isinstance(lowerCamelCase_ , lowerCamelCase_ ): model_names.append(lowerCamelCase_ ) else: model_names.extend(list(lowerCamelCase_ ) ) # Add pipeline tag and auto model class for those models table.update({model_name: (pipeline_tag, cls) for model_name in model_names} ) return table def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> List[Any]: lowerCamelCase__ : Tuple = get_frameworks_table() lowerCamelCase__ : Any = Dataset.from_pandas(lowerCamelCase_ ) lowerCamelCase__ : Tuple = hf_hub_download( """huggingface/transformers-metadata""" , """pipeline_tags.json""" , repo_type="""dataset""" , token=lowerCamelCase_ ) lowerCamelCase__ : List[str] = Dataset.from_json(lowerCamelCase_ ) lowerCamelCase__ : Dict = { tags_dataset[i]['model_class']: (tags_dataset[i]['pipeline_tag'], tags_dataset[i]['auto_class']) for i in range(len(lowerCamelCase_ ) ) } lowerCamelCase__ : Any = update_pipeline_and_auto_class_table(lowerCamelCase_ ) # Sort the model classes to avoid some nondeterministic updates to create false update commits. lowerCamelCase__ : int = sorted(table.keys() ) lowerCamelCase__ : Optional[Any] = pd.DataFrame( { """model_class""": model_classes, """pipeline_tag""": [table[m][0] for m in model_classes], """auto_class""": [table[m][1] for m in model_classes], } ) lowerCamelCase__ : int = Dataset.from_pandas(lowerCamelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: frameworks_dataset.to_json(os.path.join(lowerCamelCase_ , """frameworks.json""" ) ) tags_dataset.to_json(os.path.join(lowerCamelCase_ , """pipeline_tags.json""" ) ) if commit_sha is not None: lowerCamelCase__ : List[Any] = ( f'''Update with commit {commit_sha}\n\nSee: ''' f'''https://github.com/huggingface/transformers/commit/{commit_sha}''' ) else: lowerCamelCase__ : Tuple = 'Update' upload_folder( repo_id="""huggingface/transformers-metadata""" , folder_path=lowerCamelCase_ , repo_type="""dataset""" , token=lowerCamelCase_ , commit_message=lowerCamelCase_ , ) def SCREAMING_SNAKE_CASE_ () -> Optional[Any]: lowerCamelCase__ : List[str] = {tag: cls for tag, _, cls in PIPELINE_TAGS_AND_AUTO_MODELS} lowerCamelCase__ : Optional[int] = transformers_module.pipelines.SUPPORTED_TASKS lowerCamelCase__ : str = [] for key in pipeline_tasks: if key not in in_table: lowerCamelCase__ : List[Any] = pipeline_tasks[key]['pt'] if isinstance(lowerCamelCase_ , (list, tuple) ): lowerCamelCase__ : str = model[0] lowerCamelCase__ : Dict = model.__name__ if model not in in_table.values(): missing.append(lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: lowerCamelCase__ : Dict = ', '.join(lowerCamelCase_ ) raise ValueError( """The following pipeline tags are not present in the `PIPELINE_TAGS_AND_AUTO_MODELS` constant inside """ f'''`utils/update_metadata.py`: {msg}. Please add them!''' ) if __name__ == "__main__": _A : Dict =argparse.ArgumentParser() parser.add_argument('''--token''', type=str, help='''The token to use to push to the transformers-metadata dataset.''') parser.add_argument('''--commit_sha''', type=str, help='''The sha of the commit going with this update.''') parser.add_argument('''--check-only''', action='''store_true''', help='''Activate to just check all pipelines are present.''') _A : Optional[int] =parser.parse_args() if args.check_only: check_pipeline_tags() else: update_metadata(args.token, args.commit_sha)
368
'''simple docstring''' from torch import nn def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> Dict: if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(f'''Unsupported activation function: {act_fn}''' )
129
0
import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def lowerCamelCase__ ( snake_case_ : Optional[int] , snake_case_ : Union[str, Any] , snake_case_ : Optional[int] ) -> Union[str, Any]: __snake_case = OmegaConf.load(snake_case_ ) __snake_case = torch.load(snake_case_ , map_location='''cpu''' )['''model'''] __snake_case = list(state_dict.keys() ) # extract state_dict for VQVAE __snake_case = {} __snake_case = '''first_stage_model.''' for key in keys: if key.startswith(snake_case_ ): __snake_case = state_dict[key] # extract state_dict for UNetLDM __snake_case = {} __snake_case = '''model.diffusion_model.''' for key in keys: if key.startswith(snake_case_ ): __snake_case = state_dict[key] __snake_case = config.model.params.first_stage_config.params __snake_case = config.model.params.unet_config.params __snake_case = VQModel(**snake_case_ ).eval() vqvae.load_state_dict(snake_case_ ) __snake_case = UNetLDMModel(**snake_case_ ).eval() unet.load_state_dict(snake_case_ ) __snake_case = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='''scaled_linear''' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=snake_case_ , ) __snake_case = LDMPipeline(snake_case_ , snake_case_ , snake_case_ ) pipeline.save_pretrained(snake_case_ ) if __name__ == "__main__": snake_case_ = argparse.ArgumentParser() parser.add_argument('--checkpoint_path', type=str, required=True) parser.add_argument('--config_path', type=str, required=True) parser.add_argument('--output_path', type=str, required=True) snake_case_ = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
24
'''simple docstring''' 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 _lowerCAmelCase = logging.get_logger(__name__) # General docstring _lowerCAmelCase = '''RegNetConfig''' # Base docstring _lowerCAmelCase = '''facebook/regnet-y-040''' _lowerCAmelCase = [1, 1088, 7, 7] # Image classification docstring _lowerCAmelCase = '''facebook/regnet-y-040''' _lowerCAmelCase = '''tabby, tabby cat''' _lowerCAmelCase = [ '''facebook/regnet-y-040''', # See all regnet models at https://huggingface.co/models?filter=regnet ] class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , _UpperCAmelCase = 3 , _UpperCAmelCase = 1 , _UpperCAmelCase = 1 , _UpperCAmelCase = "relu" , **_UpperCAmelCase , ) -> Optional[int]: super().__init__(**_UpperCAmelCase ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb __UpperCamelCase : List[Any] = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) __UpperCamelCase : Tuple = tf.keras.layers.ConvaD( filters=_UpperCAmelCase , kernel_size=_UpperCAmelCase , strides=_UpperCAmelCase , padding="VALID" , groups=_UpperCAmelCase , use_bias=_UpperCAmelCase , name="convolution" , ) __UpperCamelCase : int = tf.keras.layers.BatchNormalization(epsilon=1E-5 , momentum=0.9 , name="normalization" ) __UpperCamelCase : List[str] = ACTaFN[activation] if activation is not None else tf.identity def a_ (self , _UpperCAmelCase ) -> Dict: __UpperCamelCase : str = self.convolution(self.padding(_UpperCAmelCase ) ) __UpperCamelCase : Dict = self.normalization(_UpperCAmelCase ) __UpperCamelCase : Dict = self.activation(_UpperCAmelCase ) return hidden_state class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , **_UpperCAmelCase ) -> Optional[Any]: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : Any = config.num_channels __UpperCamelCase : str = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name="embedder" , ) def a_ (self , _UpperCAmelCase ) -> Tuple: __UpperCamelCase : Dict = shape_list(_UpperCAmelCase )[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) __UpperCamelCase : Any = tf.transpose(_UpperCAmelCase , perm=(0, 2, 3, 1) ) __UpperCamelCase : List[Any] = self.embedder(_UpperCAmelCase ) return hidden_state class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , _UpperCAmelCase = 2 , **_UpperCAmelCase ) -> Any: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : Any = tf.keras.layers.ConvaD( filters=_UpperCAmelCase , kernel_size=1 , strides=_UpperCAmelCase , use_bias=_UpperCAmelCase , name="convolution" ) __UpperCamelCase : Tuple = tf.keras.layers.BatchNormalization(epsilon=1E-5 , momentum=0.9 , name="normalization" ) def a_ (self , _UpperCAmelCase , _UpperCAmelCase = False ) -> tf.Tensor: return self.normalization(self.convolution(_UpperCAmelCase ) , training=_UpperCAmelCase ) class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , _UpperCAmelCase , **_UpperCAmelCase ) -> Any: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : List[str] = tf.keras.layers.GlobalAveragePoolingaD(keepdims=_UpperCAmelCase , name="pooler" ) __UpperCamelCase : Optional[Any] = [ tf.keras.layers.ConvaD(filters=_UpperCAmelCase , kernel_size=1 , activation="relu" , name="attention.0" ), tf.keras.layers.ConvaD(filters=_UpperCAmelCase , kernel_size=1 , activation="sigmoid" , name="attention.2" ), ] def a_ (self , _UpperCAmelCase ) -> Tuple: # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] __UpperCamelCase : List[str] = self.pooler(_UpperCAmelCase ) for layer_module in self.attention: __UpperCamelCase : str = layer_module(_UpperCAmelCase ) __UpperCamelCase : List[Any] = hidden_state * pooled return hidden_state class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 , **_UpperCAmelCase ) -> int: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : List[Any] = in_channels != out_channels or stride != 1 __UpperCamelCase : List[str] = max(1 , out_channels // config.groups_width ) __UpperCamelCase : List[Any] = ( TFRegNetShortCut(_UpperCAmelCase , stride=_UpperCAmelCase , 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. __UpperCamelCase : Optional[Any] = [ TFRegNetConvLayer(_UpperCAmelCase , kernel_size=1 , activation=config.hidden_act , name="layer.0" ), TFRegNetConvLayer( _UpperCAmelCase , stride=_UpperCAmelCase , groups=_UpperCAmelCase , activation=config.hidden_act , name="layer.1" ), TFRegNetConvLayer(_UpperCAmelCase , kernel_size=1 , activation=_UpperCAmelCase , name="layer.2" ), ] __UpperCamelCase : Dict = ACTaFN[config.hidden_act] def a_ (self , _UpperCAmelCase ) -> Union[str, Any]: __UpperCamelCase : List[Any] = hidden_state for layer_module in self.layers: __UpperCamelCase : Dict = layer_module(_UpperCAmelCase ) __UpperCamelCase : List[Any] = self.shortcut(_UpperCAmelCase ) hidden_state += residual __UpperCamelCase : Tuple = self.activation(_UpperCAmelCase ) return hidden_state class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1 , **_UpperCAmelCase ) -> Any: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : str = in_channels != out_channels or stride != 1 __UpperCamelCase : Optional[int] = max(1 , out_channels // config.groups_width ) __UpperCamelCase : Union[str, Any] = ( TFRegNetShortCut(_UpperCAmelCase , stride=_UpperCAmelCase , name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" , name="shortcut" ) ) __UpperCamelCase : Union[str, Any] = [ TFRegNetConvLayer(_UpperCAmelCase , kernel_size=1 , activation=config.hidden_act , name="layer.0" ), TFRegNetConvLayer( _UpperCAmelCase , stride=_UpperCAmelCase , groups=_UpperCAmelCase , activation=config.hidden_act , name="layer.1" ), TFRegNetSELayer(_UpperCAmelCase , reduced_channels=int(round(in_channels / 4 ) ) , name="layer.2" ), TFRegNetConvLayer(_UpperCAmelCase , kernel_size=1 , activation=_UpperCAmelCase , name="layer.3" ), ] __UpperCamelCase : Union[str, Any] = ACTaFN[config.hidden_act] def a_ (self , _UpperCAmelCase ) -> int: __UpperCamelCase : str = hidden_state for layer_module in self.layers: __UpperCamelCase : Any = layer_module(_UpperCAmelCase ) __UpperCamelCase : Optional[Any] = self.shortcut(_UpperCAmelCase ) hidden_state += residual __UpperCamelCase : Union[str, Any] = self.activation(_UpperCAmelCase ) return hidden_state class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 2 , _UpperCAmelCase = 2 , **_UpperCAmelCase ) -> int: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : List[str] = TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer __UpperCamelCase : Tuple = [ # downsampling is done in the first layer with stride of 2 layer(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , stride=_UpperCAmelCase , name="layers.0" ), *[layer(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , name=f"layers.{i+1}" ) for i in range(depth - 1 )], ] def a_ (self , _UpperCAmelCase ) -> Any: for layer_module in self.layers: __UpperCamelCase : Dict = layer_module(_UpperCAmelCase ) return hidden_state class A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , _UpperCAmelCase , **_UpperCAmelCase ) -> str: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : Dict = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( _UpperCAmelCase , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name="stages.0" , ) ) __UpperCamelCase : Union[str, Any] = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(_UpperCAmelCase , config.depths[1:] ) ): self.stages.append(TFRegNetStage(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , depth=_UpperCAmelCase , name=f"stages.{i+1}" ) ) def a_ (self , _UpperCAmelCase , _UpperCAmelCase = False , _UpperCAmelCase = True ) -> TFBaseModelOutputWithNoAttention: __UpperCamelCase : List[Any] = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __UpperCamelCase : Any = hidden_states + (hidden_state,) __UpperCamelCase : Any = stage_module(_UpperCAmelCase ) if output_hidden_states: __UpperCamelCase : List[Any] = 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=_UpperCAmelCase , hidden_states=_UpperCAmelCase ) @keras_serializable class A ( tf.keras.layers.Layer ): '''simple docstring''' A = RegNetConfig def __init__(self , _UpperCAmelCase , **_UpperCAmelCase ) -> List[Any]: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : Optional[int] = config __UpperCamelCase : List[Any] = TFRegNetEmbeddings(_UpperCAmelCase , name="embedder" ) __UpperCamelCase : Union[str, Any] = TFRegNetEncoder(_UpperCAmelCase , name="encoder" ) __UpperCamelCase : Optional[Any] = tf.keras.layers.GlobalAveragePoolingaD(keepdims=_UpperCAmelCase , name="pooler" ) @unpack_inputs def a_ (self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = False , ) -> TFBaseModelOutputWithPoolingAndNoAttention: __UpperCamelCase : Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __UpperCamelCase : Dict = return_dict if return_dict is not None else self.config.use_return_dict __UpperCamelCase : Union[str, Any] = self.embedder(_UpperCAmelCase , training=_UpperCAmelCase ) __UpperCamelCase : str = self.encoder( _UpperCAmelCase , output_hidden_states=_UpperCAmelCase , return_dict=_UpperCAmelCase , training=_UpperCAmelCase ) __UpperCamelCase : List[str] = encoder_outputs[0] __UpperCamelCase : Tuple = self.pooler(_UpperCAmelCase ) # Change to NCHW output format have uniformity in the modules __UpperCamelCase : List[str] = tf.transpose(_UpperCAmelCase , perm=(0, 3, 1, 2) ) __UpperCamelCase : List[Any] = tf.transpose(_UpperCAmelCase , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: __UpperCamelCase : List[str] = tuple([tf.transpose(_UpperCAmelCase , 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=_UpperCAmelCase , pooler_output=_UpperCAmelCase , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class A ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' A = RegNetConfig A = "regnet" A = "pixel_values" @property def a_ (self ) -> List[Any]: return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_2_4, 2_2_4) , dtype=tf.floataa )} _lowerCAmelCase = 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. ''' _lowerCAmelCase = 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." , SCREAMING_SNAKE_CASE__ , ) class A ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' def __init__(self , _UpperCAmelCase , *_UpperCAmelCase , **_UpperCAmelCase ) -> Tuple: super().__init__(_UpperCAmelCase , *_UpperCAmelCase , **_UpperCAmelCase ) __UpperCamelCase : Optional[Any] = TFRegNetMainLayer(_UpperCAmelCase , name="regnet" ) @unpack_inputs @add_start_docstrings_to_model_forward(_UpperCAmelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_UpperCAmelCase , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def a_ (self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase=False , ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: __UpperCamelCase : List[str] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __UpperCamelCase : Optional[int] = return_dict if return_dict is not None else self.config.use_return_dict __UpperCamelCase : Tuple = self.regnet( pixel_values=_UpperCAmelCase , output_hidden_states=_UpperCAmelCase , return_dict=_UpperCAmelCase , training=_UpperCAmelCase , ) 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( "\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , SCREAMING_SNAKE_CASE__ , ) class A ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): '''simple docstring''' def __init__(self , _UpperCAmelCase , *_UpperCAmelCase , **_UpperCAmelCase ) -> int: super().__init__(_UpperCAmelCase , *_UpperCAmelCase , **_UpperCAmelCase ) __UpperCamelCase : Optional[Any] = config.num_labels __UpperCamelCase : Any = TFRegNetMainLayer(_UpperCAmelCase , name="regnet" ) # classification head __UpperCamelCase : List[str] = [ 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(_UpperCAmelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_UpperCAmelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def a_ (self , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase=False , ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: __UpperCamelCase : Dict = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __UpperCamelCase : str = return_dict if return_dict is not None else self.config.use_return_dict __UpperCamelCase : Dict = self.regnet( _UpperCAmelCase , output_hidden_states=_UpperCAmelCase , return_dict=_UpperCAmelCase , training=_UpperCAmelCase ) __UpperCamelCase : Union[str, Any] = outputs.pooler_output if return_dict else outputs[1] __UpperCamelCase : List[str] = self.classifier[0](_UpperCAmelCase ) __UpperCamelCase : Optional[int] = self.classifier[1](_UpperCAmelCase ) __UpperCamelCase : str = None if labels is None else self.hf_compute_loss(labels=_UpperCAmelCase , logits=_UpperCAmelCase ) if not return_dict: __UpperCamelCase : Union[str, Any] = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=_UpperCAmelCase , logits=_UpperCAmelCase , hidden_states=outputs.hidden_states )
298
0
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 _A : Union[str, Any] = logging.get_logger(__name__) # General docstring _A : Dict = """MobileNetV1Config""" # Base docstring _A : int = """google/mobilenet_v1_1.0_224""" _A : str = [1, 10_24, 7, 7] # Image classification docstring _A : List[Any] = """google/mobilenet_v1_1.0_224""" _A : str = """tabby, tabby cat""" _A : int = [ """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 _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> str: """simple docstring""" lowerCamelCase__ : str = {} if isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowerCamelCase__ : Dict = model.mobilenet_va else: lowerCamelCase__ : Any = model lowerCamelCase__ : Any = 'MobilenetV1/Conv2d_0/' lowerCamelCase__ : List[Any] = backbone.conv_stem.convolution.weight lowerCamelCase__ : int = backbone.conv_stem.normalization.bias lowerCamelCase__ : List[Any] = backbone.conv_stem.normalization.weight lowerCamelCase__ : Tuple = backbone.conv_stem.normalization.running_mean lowerCamelCase__ : Tuple = backbone.conv_stem.normalization.running_var for i in range(13 ): lowerCamelCase__ : List[str] = i + 1 lowerCamelCase__ : int = i * 2 lowerCamelCase__ : Optional[int] = backbone.layer[pt_index] lowerCamelCase__ : List[Any] = f"MobilenetV1/Conv2d_{tf_index}_depthwise/" lowerCamelCase__ : Optional[int] = pointer.convolution.weight lowerCamelCase__ : Any = pointer.normalization.bias lowerCamelCase__ : Union[str, Any] = pointer.normalization.weight lowerCamelCase__ : Dict = pointer.normalization.running_mean lowerCamelCase__ : Optional[Any] = pointer.normalization.running_var lowerCamelCase__ : Union[str, Any] = backbone.layer[pt_index + 1] lowerCamelCase__ : Tuple = f"MobilenetV1/Conv2d_{tf_index}_pointwise/" lowerCamelCase__ : Any = pointer.convolution.weight lowerCamelCase__ : List[Any] = pointer.normalization.bias lowerCamelCase__ : int = pointer.normalization.weight lowerCamelCase__ : str = pointer.normalization.running_mean lowerCamelCase__ : Optional[int] = pointer.normalization.running_var if isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowerCamelCase__ : List[str] = 'MobilenetV1/Logits/Conv2d_1c_1x1/' lowerCamelCase__ : Any = model.classifier.weight lowerCamelCase__ : int = model.classifier.bias return tf_to_pt_map def _a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Union[str, Any]: """simple docstring""" 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 lowerCamelCase__ : List[str] = tf.train.list_variables(_UpperCAmelCase ) lowerCamelCase__ : Tuple = {} for name, shape in init_vars: logger.info(f"Loading TF weight {name} with shape {shape}" ) lowerCamelCase__ : Optional[int] = tf.train.load_variable(_UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = array # Build TF to PyTorch weights loading map lowerCamelCase__ : int = _build_tf_to_pytorch_map(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) 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 lowerCamelCase__ : Optional[Any] = tf_weights[name] if "depthwise_weights" in name: logger.info('''Transposing depthwise''' ) lowerCamelCase__ : Any = np.transpose(_UpperCAmelCase , (2, 3, 0, 1) ) elif "weights" in name: logger.info('''Transposing''' ) if len(pointer.shape ) == 2: # copying into linear layer lowerCamelCase__ : Optional[int] = array.squeeze().transpose() else: lowerCamelCase__ : Tuple = np.transpose(_UpperCAmelCase , (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}" ) lowerCamelCase__ : List[str] = torch.from_numpy(_UpperCAmelCase ) tf_weights.pop(_UpperCAmelCase , _UpperCAmelCase ) tf_weights.pop(name + '''/RMSProp''' , _UpperCAmelCase ) tf_weights.pop(name + '''/RMSProp_1''' , _UpperCAmelCase ) tf_weights.pop(name + '''/ExponentialMovingAverage''' , _UpperCAmelCase ) logger.info(f"Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}" ) return model def _a ( UpperCAmelCase , UpperCAmelCase ) -> torch.Tensor: """simple docstring""" lowerCamelCase__ : Dict = features.shape[-2:] lowerCamelCase__ : str = conv_layer.stride lowerCamelCase__ : Any = conv_layer.kernel_size if in_height % stride_height == 0: lowerCamelCase__ : str = max(kernel_height - stride_height , 0 ) else: lowerCamelCase__ : Dict = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: lowerCamelCase__ : int = max(kernel_width - stride_width , 0 ) else: lowerCamelCase__ : Tuple = max(kernel_width - (in_width % stride_width) , 0 ) lowerCamelCase__ : Any = pad_along_width // 2 lowerCamelCase__ : Optional[Any] = pad_along_width - pad_left lowerCamelCase__ : Any = pad_along_height // 2 lowerCamelCase__ : Optional[int] = pad_along_height - pad_top lowerCamelCase__ : str = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(_UpperCAmelCase , _UpperCAmelCase , '''constant''' , 0.0 ) class __SCREAMING_SNAKE_CASE ( nn.Module ): def __init__( self : Dict , A : MobileNetVaConfig , A : int , A : int , A : int , A : Optional[int] = 1 , A : Optional[int] = 1 , A : bool = False , A : Optional[bool] = True , A : Optional[bool or str] = True , ) ->None: super().__init__() lowerCamelCase__ : Dict = 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." ) lowerCamelCase__ : List[str] = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) lowerCamelCase__ : 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: lowerCamelCase__ : Optional[int] = nn.BatchNormad( num_features=A , eps=config.layer_norm_eps , momentum=0.99_97 , affine=A , track_running_stats=A , ) else: lowerCamelCase__ : Tuple = None if use_activation: if isinstance(A , A ): lowerCamelCase__ : Tuple = ACTaFN[use_activation] elif isinstance(config.hidden_act , A ): lowerCamelCase__ : Any = ACTaFN[config.hidden_act] else: lowerCamelCase__ : Dict = config.hidden_act else: lowerCamelCase__ : Optional[int] = None def __lowerCamelCase ( self : Union[str, Any] , A : torch.Tensor ) ->torch.Tensor: if self.config.tf_padding: lowerCamelCase__ : Dict = apply_tf_padding(A , self.convolution ) lowerCamelCase__ : Union[str, Any] = self.convolution(A ) if self.normalization is not None: lowerCamelCase__ : Union[str, Any] = self.normalization(A ) if self.activation is not None: lowerCamelCase__ : List[str] = self.activation(A ) return features class __SCREAMING_SNAKE_CASE ( __UpperCamelCase ): _UpperCAmelCase : str = MobileNetVaConfig _UpperCAmelCase : List[str] = load_tf_weights_in_mobilenet_va _UpperCAmelCase : str = "mobilenet_v1" _UpperCAmelCase : Dict = "pixel_values" _UpperCAmelCase : Tuple = False def __lowerCamelCase ( self : Optional[Any] , A : Union[nn.Linear, nn.Convad] ) ->None: 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 ) _A : str = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`MobileNetV1Config`]): 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ _A : Union[str, Any] = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileNetV1ImageProcessor.__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 MobileNetV1 model outputting raw hidden-states without any specific head on top." ,__UpperCamelCase ,) class __SCREAMING_SNAKE_CASE ( __UpperCamelCase ): def __init__( self : Dict , A : MobileNetVaConfig , A : bool = True ) ->Dict: super().__init__(A ) lowerCamelCase__ : Optional[Any] = config lowerCamelCase__ : Dict = 3_2 lowerCamelCase__ : List[str] = max(int(depth * config.depth_multiplier ) , config.min_depth ) lowerCamelCase__ : str = MobileNetVaConvLayer( A , in_channels=config.num_channels , out_channels=A , kernel_size=3 , stride=2 , ) lowerCamelCase__ : List[str] = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] lowerCamelCase__ : Any = nn.ModuleList() for i in range(1_3 ): lowerCamelCase__ : Tuple = out_channels if strides[i] == 2 or i == 0: depth *= 2 lowerCamelCase__ : 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 , ) ) lowerCamelCase__ : Optional[int] = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def __lowerCamelCase ( self : str , A : Union[str, Any] ) ->List[str]: 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 __lowerCamelCase ( self : Dict , A : Optional[torch.Tensor] = None , A : Optional[bool] = None , A : Optional[bool] = None , ) ->Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: lowerCamelCase__ : Optional[Any] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowerCamelCase__ : 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''' ) lowerCamelCase__ : Tuple = self.conv_stem(A ) lowerCamelCase__ : Optional[Any] = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): lowerCamelCase__ : Any = layer_module(A ) if output_hidden_states: lowerCamelCase__ : Union[str, Any] = all_hidden_states + (hidden_states,) lowerCamelCase__ : List[Any] = hidden_states if self.pooler is not None: lowerCamelCase__ : Tuple = torch.flatten(self.pooler(A ) , start_dim=1 ) else: lowerCamelCase__ : Any = 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 " ,__UpperCamelCase ,) class __SCREAMING_SNAKE_CASE ( __UpperCamelCase ): def __init__( self : Optional[int] , A : MobileNetVaConfig ) ->None: super().__init__(A ) lowerCamelCase__ : List[str] = config.num_labels lowerCamelCase__ : Dict = MobileNetVaModel(A ) lowerCamelCase__ : Optional[int] = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head lowerCamelCase__ : str = nn.Dropout(config.classifier_dropout_prob , inplace=A ) lowerCamelCase__ : str = 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 __lowerCamelCase ( self : Dict , A : Optional[torch.Tensor] = None , A : Optional[bool] = None , A : Optional[torch.Tensor] = None , A : Optional[bool] = None , ) ->Union[tuple, ImageClassifierOutputWithNoAttention]: lowerCamelCase__ : List[Any] = return_dict if return_dict is not None else self.config.use_return_dict lowerCamelCase__ : List[str] = self.mobilenet_va(A , output_hidden_states=A , return_dict=A ) lowerCamelCase__ : Optional[Any] = outputs.pooler_output if return_dict else outputs[1] lowerCamelCase__ : List[Any] = self.classifier(self.dropout(A ) ) lowerCamelCase__ : Tuple = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: lowerCamelCase__ : Dict = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): lowerCamelCase__ : Tuple = 'single_label_classification' else: lowerCamelCase__ : Dict = 'multi_label_classification' if self.config.problem_type == "regression": lowerCamelCase__ : Union[str, Any] = MSELoss() if self.num_labels == 1: lowerCamelCase__ : int = loss_fct(logits.squeeze() , labels.squeeze() ) else: lowerCamelCase__ : Dict = loss_fct(A , A ) elif self.config.problem_type == "single_label_classification": lowerCamelCase__ : Optional[int] = CrossEntropyLoss() lowerCamelCase__ : List[str] = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": lowerCamelCase__ : Union[str, Any] = BCEWithLogitsLoss() lowerCamelCase__ : int = loss_fct(A , A ) if not return_dict: lowerCamelCase__ : 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 , )
351
from __future__ import annotations _A : List[str] = '#' class __SCREAMING_SNAKE_CASE : def __init__( self : List[Any] ) ->None: lowerCamelCase__ : dict = {} def __lowerCamelCase ( self : Union[str, Any] , A : str ) ->None: lowerCamelCase__ : Any = self._trie for char in text: if char not in trie: lowerCamelCase__ : Any = {} lowerCamelCase__ : Any = trie[char] lowerCamelCase__ : List[str] = True def __lowerCamelCase ( self : List[Any] , A : str ) ->tuple | list: lowerCamelCase__ : Dict = self._trie for char in prefix: if char in trie: lowerCamelCase__ : List[Any] = trie[char] else: return [] return self._elements(A ) def __lowerCamelCase ( self : Dict , A : dict ) ->tuple: lowerCamelCase__ : Optional[Any] = [] for c, v in d.items(): lowerCamelCase__ : Any = [''' '''] if c == END else [(c + s) for s in self._elements(A )] result.extend(A ) return tuple(A ) _A : str = Trie() _A : List[Any] = ('depart', 'detergent', 'daring', 'dog', 'deer', 'deal') for word in words: trie.insert_word(word) def _a ( UpperCAmelCase ) -> tuple: """simple docstring""" lowerCamelCase__ : Optional[int] = trie.find_word(UpperCAmelCase ) return tuple(string + word for word in suffixes ) def _a ( ) -> None: """simple docstring""" print(autocomplete_using_trie('''de''' ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
265
0
'''simple docstring''' import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate __SCREAMING_SNAKE_CASE :Optional[int] = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('''''', '''|''', '''|'''), datarow=DataRow('''''', '''|''', '''|'''), padding=1, with_header_hide=None, ) __SCREAMING_SNAKE_CASE :Any = [] __SCREAMING_SNAKE_CASE :int = [] __SCREAMING_SNAKE_CASE :str = {'''type''': '''section''', '''text''': {'''type''': '''plain_text''', '''text''': '''No failed tests! 🤗''', '''emoji''': True}} __SCREAMING_SNAKE_CASE :Any = [ { '''type''': '''header''', '''text''': { '''type''': '''plain_text''', '''text''': F"🤗 Accelerate nightly {os.environ.get('TEST_TYPE', '')} test results", '''emoji''': True, }, } ] __SCREAMING_SNAKE_CASE :List[str] = 0 for log in Path().glob('''*.log'''): __SCREAMING_SNAKE_CASE :Optional[int] = 0 with open(log, '''r''') as f: for line in f: __SCREAMING_SNAKE_CASE :Tuple = json.loads(line) if line.get('''nodeid''', '''''') != "": __SCREAMING_SNAKE_CASE :Optional[int] = line['''nodeid'''] if line.get('''duration''', None) is not None: __SCREAMING_SNAKE_CASE :List[str] = F"{line['duration']:.4f}" if line.get('''outcome''', '''''') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('''_''')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) __SCREAMING_SNAKE_CASE :int = [] log.unlink() __SCREAMING_SNAKE_CASE :Any = '''''' __SCREAMING_SNAKE_CASE :Union[str, Any] = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" __SCREAMING_SNAKE_CASE :int = [] __SCREAMING_SNAKE_CASE :Any = {} for test in failed_tests: __SCREAMING_SNAKE_CASE :Any = test[0].split('''::''') __SCREAMING_SNAKE_CASE :Optional[Any] = data[0].split('''/''')[-1] if data[0] not in filesafailed: __SCREAMING_SNAKE_CASE :List[Any] = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) __SCREAMING_SNAKE_CASE :Dict = [test[0] for test in failed_table] __SCREAMING_SNAKE_CASE :int = list(set(files)) # Count number of instances in failed_tests __SCREAMING_SNAKE_CASE :List[Any] = [] for file in individual_files: table.append([file, len(filesafailed[file])]) __SCREAMING_SNAKE_CASE :int = tabulate( table, headers=['''Test Location''', '''Num Failed'''], tablefmt=hf_table_format, stralign='''right''', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 3000: __SCREAMING_SNAKE_CASE :Union[str, Any] = '''Too many failed tests, please see the full report in the Action results.''' __SCREAMING_SNAKE_CASE :List[Any] = len(err) + 10 __SCREAMING_SNAKE_CASE :List[str] = message[: 3000 - offset] + F"\n...\n```\n{err}" print(F"### {message}") else: __SCREAMING_SNAKE_CASE :Optional[Any] = '''No failed tests! 🤗''' print(F"## {message}") payload.append(no_error_payload) if os.environ.get('''TEST_TYPE''', '''''') != "": from slack_sdk import WebClient __SCREAMING_SNAKE_CASE :List[Any] = WebClient(token=os.environ['''SLACK_API_TOKEN''']) if message != "No failed tests! 🤗": __SCREAMING_SNAKE_CASE :Optional[Any] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': message, }, } payload.append(md_report) __SCREAMING_SNAKE_CASE :str = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': '''*For more details:*''', }, '''accessory''': { '''type''': '''button''', '''text''': { '''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True, }, '''url''': F"https://github.com/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['GITHUB_RUN_ID']}", }, } payload.append(action_button) __SCREAMING_SNAKE_CASE :List[Any] = { '''type''': '''context''', '''elements''': [ { '''type''': '''plain_text''', '''text''': F"Nightly {os.environ.get('TEST_TYPE')} test results for {date.today()}", } ], } payload.append(date_report) __SCREAMING_SNAKE_CASE :Dict = client.chat_postMessage(channel='''#accelerate-ci-daily''', text=message, blocks=payload) __SCREAMING_SNAKE_CASE :List[Any] = response.data['''ts'''] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name __SCREAMING_SNAKE_CASE :Tuple = '''''' for i, row in enumerate(test_failures): if row[0] != test_class: __SCREAMING_SNAKE_CASE :Any = row[0] else: __SCREAMING_SNAKE_CASE :Union[str, Any] = '''''' __SCREAMING_SNAKE_CASE :Dict = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': F"Test location: {test_location}\n```\n{tabulate(test_failures, headers=['Class', 'Test'], tablefmt=hf_table_format, stralign='right')}\n```", }, } client.chat_postMessage( channel='''#accelerate-ci-daily''', thread_ts=ts, blocks=[payload], )
22
'''simple docstring''' def UpperCAmelCase_ ( __lowercase : str ) -> list: '''simple docstring''' if n_term == "": return [] _UpperCAmelCase = [] for temp in range(int(__lowercase ) ): series.append(f'1/{temp + 1}' if series else "1" ) return series if __name__ == "__main__": __SCREAMING_SNAKE_CASE :str = input('''Enter the last number (nth term) of the Harmonic Series''') print('''Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n''') print(harmonic_series(nth_term))
22
1
'''simple docstring''' def __lowerCamelCase ( __lowerCAmelCase : int ) -> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 snake_case = 1 snake_case = 1 while repunit: snake_case = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def __lowerCamelCase ( __lowerCAmelCase : int = 1_00_00_00 ) -> int: snake_case = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(__lowerCAmelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(F"""{solution() = }""")
3
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class _lowerCAmelCase ( A__ ): """simple docstring""" snake_case_ = "Salesforce/blip-image-captioning-base" snake_case_ = ( "This is a tool that generates a description of an image. It takes an input named `image` which should be the " "image to caption, and returns a text that contains the description in English." ) snake_case_ = "image_captioner" snake_case_ = AutoModelForVisionaSeq snake_case_ = ["image"] snake_case_ = ["text"] def __init__( self : Tuple , *__snake_case : Optional[int] , **__snake_case : Any )-> Optional[Any]: requires_backends(self , ["""vision"""] ) super().__init__(*__snake_case , **__snake_case ) def lowerCAmelCase ( self : str , __snake_case : "Image" )-> int: return self.pre_processor(images=__snake_case , return_tensors="""pt""" ) def lowerCAmelCase ( self : Any , __snake_case : List[str] )-> Union[str, Any]: return self.model.generate(**__snake_case ) def lowerCAmelCase ( self : Union[str, Any] , __snake_case : Any )-> Dict: return self.pre_processor.batch_decode(__snake_case , skip_special_tokens=__snake_case )[0].strip()
3
1
import os import shutil import tempfile import unittest import numpy as np from transformers import AutoTokenizer, BarkProcessor from transformers.testing_utils import require_torch, slow @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def _A ( self : Union[str, Any] ): UpperCamelCase :Union[str, Any] = """ylacombe/bark-small""" UpperCamelCase :List[str] = tempfile.mkdtemp() UpperCamelCase :List[Any] = """en_speaker_1""" UpperCamelCase :int = """This is a test string""" UpperCamelCase :int = """speaker_embeddings_path.json""" UpperCamelCase :Optional[Any] = """speaker_embeddings""" def _A ( self : int , **__lowerCamelCase : Any ): return AutoTokenizer.from_pretrained(self.checkpoint , **__lowerCamelCase ) def _A ( self : Dict ): shutil.rmtree(self.tmpdirname ) def _A ( self : List[Any] ): UpperCamelCase :Optional[int] = self.get_tokenizer() UpperCamelCase :str = BarkProcessor(tokenizer=__lowerCamelCase ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase :Union[str, Any] = BarkProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) @slow def _A ( self : int ): UpperCamelCase :Optional[int] = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) processor.save_pretrained( self.tmpdirname , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , speaker_embeddings_directory=self.speaker_embeddings_directory , ) UpperCamelCase :Union[str, Any] = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) UpperCamelCase :Tuple = BarkProcessor.from_pretrained( self.tmpdirname , self.speaker_embeddings_dict_path , bos_token="""(BOS)""" , eos_token="""(EOS)""" , ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) def _A ( self : Tuple ): UpperCamelCase :Optional[int] = BarkProcessor.from_pretrained( pretrained_processor_name_or_path=self.checkpoint , speaker_embeddings_dict_path=self.speaker_embeddings_dict_path , ) UpperCamelCase :Any = 35 UpperCamelCase :Optional[int] = 2 UpperCamelCase :Tuple = 8 UpperCamelCase :Optional[Any] = { """semantic_prompt""": np.ones(__lowerCamelCase ), """coarse_prompt""": np.ones((nb_codebooks_coarse, seq_len) ), """fine_prompt""": np.ones((nb_codebooks_total, seq_len) ), } # test providing already loaded voice_preset UpperCamelCase :List[Any] = processor(text=self.input_string , voice_preset=__lowerCamelCase ) UpperCamelCase :Union[str, Any] = inputs["""history_prompt"""] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(__lowerCamelCase , np.array([] ) ).tolist() ) # test loading voice preset from npz file UpperCamelCase :List[Any] = os.path.join(self.tmpdirname , """file.npz""" ) np.savez(__lowerCamelCase , **__lowerCamelCase ) UpperCamelCase :Tuple = processor(text=self.input_string , voice_preset=__lowerCamelCase ) UpperCamelCase :Dict = inputs["""history_prompt"""] for key in voice_preset: self.assertListEqual(voice_preset[key].tolist() , processed_voice_preset.get(__lowerCamelCase , np.array([] ) ).tolist() ) # test loading voice preset from the hub UpperCamelCase :List[Any] = processor(text=self.input_string , voice_preset=self.voice_preset ) def _A ( self : List[str] ): UpperCamelCase :Any = self.get_tokenizer() UpperCamelCase :List[str] = BarkProcessor(tokenizer=__lowerCamelCase ) UpperCamelCase :Tuple = processor(text=self.input_string ) UpperCamelCase :Tuple = tokenizer( self.input_string , padding="""max_length""" , max_length=256 , add_special_tokens=__lowerCamelCase , return_attention_mask=__lowerCamelCase , return_token_type_ids=__lowerCamelCase , ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key].squeeze().tolist() )
38
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 UpperCAmelCase_ : Dict = logging.get_logger(__name__) UpperCAmelCase_ : str = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class _SCREAMING_SNAKE_CASE ( _a ): snake_case__ : Optional[int] = """layoutlmv3""" def __init__( self : List[Any] , __lowerCamelCase : Optional[Any]=50_265 , __lowerCamelCase : Dict=768 , __lowerCamelCase : Any=12 , __lowerCamelCase : int=12 , __lowerCamelCase : str=3_072 , __lowerCamelCase : List[Any]="gelu" , __lowerCamelCase : List[str]=0.1 , __lowerCamelCase : Optional[int]=0.1 , __lowerCamelCase : Optional[Any]=512 , __lowerCamelCase : Optional[int]=2 , __lowerCamelCase : Union[str, Any]=0.02 , __lowerCamelCase : Union[str, Any]=1E-5 , __lowerCamelCase : Any=1 , __lowerCamelCase : Optional[int]=0 , __lowerCamelCase : List[Any]=2 , __lowerCamelCase : Dict=1_024 , __lowerCamelCase : List[Any]=128 , __lowerCamelCase : str=128 , __lowerCamelCase : Optional[Any]=True , __lowerCamelCase : str=32 , __lowerCamelCase : List[Any]=128 , __lowerCamelCase : str=64 , __lowerCamelCase : List[str]=256 , __lowerCamelCase : Dict=True , __lowerCamelCase : List[Any]=True , __lowerCamelCase : Tuple=True , __lowerCamelCase : Tuple=224 , __lowerCamelCase : Tuple=3 , __lowerCamelCase : Dict=16 , __lowerCamelCase : Union[str, Any]=None , **__lowerCamelCase : Optional[Any] , ): super().__init__( vocab_size=__lowerCamelCase , hidden_size=__lowerCamelCase , num_hidden_layers=__lowerCamelCase , num_attention_heads=__lowerCamelCase , intermediate_size=__lowerCamelCase , hidden_act=__lowerCamelCase , hidden_dropout_prob=__lowerCamelCase , attention_probs_dropout_prob=__lowerCamelCase , max_position_embeddings=__lowerCamelCase , type_vocab_size=__lowerCamelCase , initializer_range=__lowerCamelCase , layer_norm_eps=__lowerCamelCase , pad_token_id=__lowerCamelCase , bos_token_id=__lowerCamelCase , eos_token_id=__lowerCamelCase , **__lowerCamelCase , ) UpperCamelCase :int = max_ad_position_embeddings UpperCamelCase :Tuple = coordinate_size UpperCamelCase :List[Any] = shape_size UpperCamelCase :Union[str, Any] = has_relative_attention_bias UpperCamelCase :Any = rel_pos_bins UpperCamelCase :Optional[Any] = max_rel_pos UpperCamelCase :str = has_spatial_attention_bias UpperCamelCase :Tuple = rel_ad_pos_bins UpperCamelCase :Optional[int] = max_rel_ad_pos UpperCamelCase :Tuple = text_embed UpperCamelCase :str = visual_embed UpperCamelCase :Optional[Any] = input_size UpperCamelCase :str = num_channels UpperCamelCase :List[Any] = patch_size UpperCamelCase :Optional[Any] = classifier_dropout class _SCREAMING_SNAKE_CASE ( _a ): snake_case__ : int = version.parse("""1.12""" ) @property def _A ( self : Optional[int] ): # The order of inputs is different for question answering and sequence classification if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def _A ( self : str ): return 1E-5 @property def _A ( self : Dict ): return 12 def _A ( self : Dict , __lowerCamelCase : "ProcessorMixin" , __lowerCamelCase : int = -1 , __lowerCamelCase : int = -1 , __lowerCamelCase : bool = False , __lowerCamelCase : Optional["TensorType"] = None , __lowerCamelCase : int = 3 , __lowerCamelCase : int = 40 , __lowerCamelCase : int = 40 , ): setattr(processor.image_processor , """apply_ocr""" , __lowerCamelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase :Optional[Any] = 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 :Optional[int] = processor.tokenizer.num_special_tokens_to_add(__lowerCamelCase ) UpperCamelCase :int = 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 :Any = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes UpperCamelCase :Optional[Any] = [[[48, 84, 73, 128]]] * 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 :List[str] = self._generate_dummy_images(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) UpperCamelCase :Any = dict( processor( __lowerCamelCase , text=__lowerCamelCase , boxes=__lowerCamelCase , return_tensors=__lowerCamelCase , ) ) return inputs
38
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { '''configuration_electra''': ['''ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ElectraConfig''', '''ElectraOnnxConfig'''], '''tokenization_electra''': ['''ElectraTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''ElectraTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ElectraForCausalLM''', '''ElectraForMaskedLM''', '''ElectraForMultipleChoice''', '''ElectraForPreTraining''', '''ElectraForQuestionAnswering''', '''ElectraForSequenceClassification''', '''ElectraForTokenClassification''', '''ElectraModel''', '''ElectraPreTrainedModel''', '''load_tf_weights_in_electra''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFElectraForMaskedLM''', '''TFElectraForMultipleChoice''', '''TFElectraForPreTraining''', '''TFElectraForQuestionAnswering''', '''TFElectraForSequenceClassification''', '''TFElectraForTokenClassification''', '''TFElectraModel''', '''TFElectraPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''FlaxElectraForCausalLM''', '''FlaxElectraForMaskedLM''', '''FlaxElectraForMultipleChoice''', '''FlaxElectraForPreTraining''', '''FlaxElectraForQuestionAnswering''', '''FlaxElectraForSequenceClassification''', '''FlaxElectraForTokenClassification''', '''FlaxElectraModel''', '''FlaxElectraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
363
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__ = { '''configuration_whisper''': ['''WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''WhisperConfig''', '''WhisperOnnxConfig'''], '''feature_extraction_whisper''': ['''WhisperFeatureExtractor'''], '''processing_whisper''': ['''WhisperProcessor'''], '''tokenization_whisper''': ['''WhisperTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''WhisperTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''WhisperForConditionalGeneration''', '''WhisperModel''', '''WhisperPreTrainedModel''', '''WhisperForAudioClassification''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFWhisperForConditionalGeneration''', '''TFWhisperModel''', '''TFWhisperPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''FlaxWhisperForConditionalGeneration''', '''FlaxWhisperModel''', '''FlaxWhisperPreTrainedModel''', '''FlaxWhisperForAudioClassification''', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
52
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCamelCase : Dict = { 'configuration_rag': ['RagConfig'], 'retrieval_rag': ['RagRetriever'], 'tokenization_rag': ['RagTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[Any] = [ 'RagModel', 'RagPreTrainedModel', 'RagSequenceForGeneration', 'RagTokenForGeneration', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : List[Any] = [ 'TFRagModel', 'TFRagPreTrainedModel', 'TFRagSequenceForGeneration', 'TFRagTokenForGeneration', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys lowerCamelCase : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
47
'''simple docstring''' import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class __A ( unittest.TestCase ): def _lowercase (self : Tuple ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def _lowercase (self : str ): UpperCAmelCase_ = 1 UpperCAmelCase_ = 3 UpperCAmelCase_ = (32, 32) UpperCAmelCase_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(__a ) return image @property def _lowercase (self : int ): torch.manual_seed(0 ) UpperCAmelCase_ = UNetaDConditionModel( block_out_channels=(32, 32, 64) , layers_per_block=2 , sample_size=32 , in_channels=7 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , attention_head_dim=8 , use_linear_projection=__a , only_cross_attention=(True, True, False) , num_class_embeds=100 , ) return model @property def _lowercase (self : Any ): torch.manual_seed(0 ) UpperCAmelCase_ = AutoencoderKL( block_out_channels=[32, 32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) return model @property def _lowercase (self : Optional[Any] ): 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=1000 , hidden_act="gelu" , projection_dim=512 , ) return CLIPTextModel(__a ) def _lowercase (self : Any ): UpperCAmelCase_ = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ = self.dummy_cond_unet_upscale UpperCAmelCase_ = DDPMScheduler() UpperCAmelCase_ = DDIMScheduler(prediction_type="v_prediction" ) UpperCAmelCase_ = self.dummy_vae UpperCAmelCase_ = self.dummy_text_encoder UpperCAmelCase_ = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__a ) ).convert("RGB" ).resize((64, 64) ) # make sure here that pndm scheduler skips prk UpperCAmelCase_ = StableDiffusionUpscalePipeline( unet=__a , low_res_scheduler=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , max_noise_level=350 , ) UpperCAmelCase_ = sd_pipe.to(__a ) sd_pipe.set_progress_bar_config(disable=__a ) UpperCAmelCase_ = "A painting of a squirrel eating a burger" UpperCAmelCase_ = torch.Generator(device=__a ).manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , ) UpperCAmelCase_ = output.images UpperCAmelCase_ = torch.Generator(device=__a ).manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , return_dict=__a , )[0] UpperCAmelCase_ = image[0, -3:, -3:, -1] UpperCAmelCase_ = image_from_tuple[0, -3:, -3:, -1] UpperCAmelCase_ = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) UpperCAmelCase_ = np.array([0.31_13, 0.39_10, 0.42_72, 0.48_59, 0.50_61, 0.46_52, 0.53_62, 0.57_15, 0.56_61] ) 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 _lowercase (self : Optional[int] ): UpperCAmelCase_ = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ = self.dummy_cond_unet_upscale UpperCAmelCase_ = DDPMScheduler() UpperCAmelCase_ = DDIMScheduler(prediction_type="v_prediction" ) UpperCAmelCase_ = self.dummy_vae UpperCAmelCase_ = self.dummy_text_encoder UpperCAmelCase_ = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__a ) ).convert("RGB" ).resize((64, 64) ) # make sure here that pndm scheduler skips prk UpperCAmelCase_ = StableDiffusionUpscalePipeline( unet=__a , low_res_scheduler=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , max_noise_level=350 , ) UpperCAmelCase_ = sd_pipe.to(__a ) sd_pipe.set_progress_bar_config(disable=__a ) UpperCAmelCase_ = "A painting of a squirrel eating a burger" UpperCAmelCase_ = sd_pipe( 2 * [prompt] , image=2 * [low_res_image] , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , ) UpperCAmelCase_ = output.images assert image.shape[0] == 2 UpperCAmelCase_ = torch.Generator(device=__a ).manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , num_images_per_prompt=2 , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , ) UpperCAmelCase_ = output.images assert image.shape[0] == 2 @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def _lowercase (self : str ): UpperCAmelCase_ = self.dummy_cond_unet_upscale UpperCAmelCase_ = DDPMScheduler() UpperCAmelCase_ = DDIMScheduler(prediction_type="v_prediction" ) UpperCAmelCase_ = self.dummy_vae UpperCAmelCase_ = self.dummy_text_encoder UpperCAmelCase_ = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__a ) ).convert("RGB" ).resize((64, 64) ) # put models in fp16, except vae as it overflows in fp16 UpperCAmelCase_ = unet.half() UpperCAmelCase_ = text_encoder.half() # make sure here that pndm scheduler skips prk UpperCAmelCase_ = StableDiffusionUpscalePipeline( unet=__a , low_res_scheduler=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , max_noise_level=350 , ) UpperCAmelCase_ = sd_pipe.to(__a ) sd_pipe.set_progress_bar_config(disable=__a ) UpperCAmelCase_ = "A painting of a squirrel eating a burger" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , num_inference_steps=2 , output_type="np" , ).images UpperCAmelCase_ = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) @slow @require_torch_gpu class __A ( unittest.TestCase ): def _lowercase (self : List[str] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase (self : List[Any] ): UpperCAmelCase_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-upscale/low_res_cat.png" ) UpperCAmelCase_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale" "/upsampled_cat.npy" ) UpperCAmelCase_ = "stabilityai/stable-diffusion-x4-upscaler" UpperCAmelCase_ = StableDiffusionUpscalePipeline.from_pretrained(__a ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() UpperCAmelCase_ = "a cat sitting on a park bench" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = pipe( prompt=__a , image=__a , generator=__a , output_type="np" , ) UpperCAmelCase_ = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 1E-3 def _lowercase (self : Tuple ): UpperCAmelCase_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-upscale/low_res_cat.png" ) UpperCAmelCase_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale" "/upsampled_cat_fp16.npy" ) UpperCAmelCase_ = "stabilityai/stable-diffusion-x4-upscaler" UpperCAmelCase_ = StableDiffusionUpscalePipeline.from_pretrained( __a , torch_dtype=torch.floataa , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() UpperCAmelCase_ = "a cat sitting on a park bench" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = pipe( prompt=__a , image=__a , generator=__a , output_type="np" , ) UpperCAmelCase_ = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def _lowercase (self : List[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() UpperCAmelCase_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-upscale/low_res_cat.png" ) UpperCAmelCase_ = "stabilityai/stable-diffusion-x4-upscaler" UpperCAmelCase_ = StableDiffusionUpscalePipeline.from_pretrained( __a , torch_dtype=torch.floataa , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() UpperCAmelCase_ = "a cat sitting on a park bench" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = pipe( prompt=__a , image=__a , generator=__a , num_inference_steps=5 , output_type="np" , ) UpperCAmelCase_ = torch.cuda.max_memory_allocated() # make sure that less than 2.9 GB is allocated assert mem_bytes < 2.9 * 10**9
1
0
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class lowerCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any]=13 , __SCREAMING_SNAKE_CASE : Any=7 , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : List[Any]=99 , __SCREAMING_SNAKE_CASE : Union[str, Any]=32 , __SCREAMING_SNAKE_CASE : Dict=5 , __SCREAMING_SNAKE_CASE : str=4 , __SCREAMING_SNAKE_CASE : Tuple=37 , __SCREAMING_SNAKE_CASE : List[Any]="gelu" , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=512 , __SCREAMING_SNAKE_CASE : Optional[Any]=16 , __SCREAMING_SNAKE_CASE : Optional[Any]=2 , __SCREAMING_SNAKE_CASE : Tuple=0.02 , __SCREAMING_SNAKE_CASE : List[Any]=4 , ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_attention_mask __SCREAMING_SNAKE_CASE = use_token_type_ids __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = type_vocab_size __SCREAMING_SNAKE_CASE = type_sequence_label_size __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = num_choices def UpperCAmelCase__ ( self : Dict ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __SCREAMING_SNAKE_CASE = None if self.use_attention_mask: __SCREAMING_SNAKE_CASE = random_attention_mask([self.batch_size, self.seq_length] ) __SCREAMING_SNAKE_CASE = None if self.use_token_type_ids: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __SCREAMING_SNAKE_CASE = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase__ ( self : List[Any] ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = config_and_inputs __SCREAMING_SNAKE_CASE = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class lowerCAmelCase__ ( a , unittest.TestCase ): """simple docstring""" lowerCAmelCase__ = True lowerCAmelCase__ = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def UpperCAmelCase__ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = FlaxRoFormerModelTester(self ) @slow def UpperCAmelCase__ ( self : int ) -> Any: """simple docstring""" for model_class_name in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class_name.from_pretrained("""junnyu/roformer_chinese_small""" , from_pt=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = model(np.ones((1, 1) ) ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_flax class lowerCAmelCase__ ( unittest.TestCase ): """simple docstring""" @slow def UpperCAmelCase__ ( self : Any ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) __SCREAMING_SNAKE_CASE = jnp.array([[0, 1, 2, 3, 4, 5]] ) __SCREAMING_SNAKE_CASE = model(__SCREAMING_SNAKE_CASE )[0] __SCREAMING_SNAKE_CASE = 50_000 __SCREAMING_SNAKE_CASE = (1, 6, vocab_size) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = jnp.array( [[[-0.1205, -1.0265, 0.2922], [-1.5134, 0.1974, 0.1519], [-5.0135, -3.9003, -0.8404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1E-4 ) )
331
'''simple docstring''' import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration UpperCAmelCase : Any = [ # tf -> hf ('/', '.'), ('layer_', 'layers.'), ('kernel', 'weight'), ('beta', 'bias'), ('gamma', 'weight'), ('pegasus', 'model'), ] UpperCAmelCase : Optional[Any] = [ ('.output.dense', '.fc2'), ('intermediate.LayerNorm', 'final_layer_norm'), ('intermediate.dense', 'fc1'), ] UpperCAmelCase : Optional[int] = ( INIT_COMMON + [ ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.out_proj'), ('attention.self', 'self_attn'), ('attention.encdec.LayerNorm', 'encoder_attn_layer_norm'), ('attention.encdec_output.dense', 'encoder_attn.out_proj'), ('attention.encdec', 'encoder_attn'), ('key', 'k_proj'), ('value', 'v_proj'), ('query', 'q_proj'), ('decoder.LayerNorm', 'decoder.layernorm_embedding'), ] + END_COMMON ) UpperCAmelCase : List[str] = ( INIT_COMMON + [ ('embeddings.word_embeddings', 'shared.weight'), ('embeddings.position_embeddings', 'embed_positions.weight'), ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.output'), ('attention.self', 'self_attn.self'), ('encoder.LayerNorm', 'encoder.layernorm_embedding'), ] + END_COMMON ) UpperCAmelCase : List[Any] = [ 'encdec/key/bias', 'encdec/query/bias', 'encdec/value/bias', 'self/key/bias', 'self/query/bias', 'self/value/bias', 'encdec_output/dense/bias', 'attention/output/dense/bias', ] def a__ ( a__ , a__ ): """simple docstring""" for tf_name, hf_name in patterns: __SCREAMING_SNAKE_CASE = k.replace(a__ , a__ ) return k def a__ ( a__ , a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE = BigBirdPegasusConfig(**a__ ) __SCREAMING_SNAKE_CASE = BigBirdPegasusForConditionalGeneration(a__ ) __SCREAMING_SNAKE_CASE = torch_model.state_dict() __SCREAMING_SNAKE_CASE = {} # separating decoder weights __SCREAMING_SNAKE_CASE = {k: tf_weights[k] for k in tf_weights if k.startswith("""pegasus/decoder""" )} __SCREAMING_SNAKE_CASE = {k: tf_weights[k] for k in tf_weights if not k.startswith("""pegasus/decoder""" )} for k, v in tqdm(decoder_weights.items() , """tf -> hf conversion""" ): __SCREAMING_SNAKE_CASE = [k.endswith(a__ ) for ending in KEYS_TO_IGNORE] if any(a__ ): continue __SCREAMING_SNAKE_CASE = DECODER_PATTERNS __SCREAMING_SNAKE_CASE = rename_state_dict_key(a__ , a__ ) if new_k not in state_dict: raise ValueError(F'could not find new key {new_k} in state dict. (converted from {k})' ) if any(True if i in k else False for i in ["""dense""", """query""", """key""", """value"""] ): __SCREAMING_SNAKE_CASE = v.T __SCREAMING_SNAKE_CASE = torch.from_numpy(a__ ) assert v.shape == state_dict[new_k].shape, F'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}' for k, v in tqdm(remaining_weights.items() , """tf -> hf conversion""" ): __SCREAMING_SNAKE_CASE = [k.endswith(a__ ) for ending in KEYS_TO_IGNORE] if any(a__ ): continue __SCREAMING_SNAKE_CASE = REMAINING_PATTERNS __SCREAMING_SNAKE_CASE = rename_state_dict_key(a__ , a__ ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(F'could not find new key {new_k} in state dict. (converted from {k})' ) if any(True if i in k else False for i in ["""dense""", """query""", """key""", """value"""] ): __SCREAMING_SNAKE_CASE = v.T __SCREAMING_SNAKE_CASE = torch.from_numpy(a__ ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, F'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}' __SCREAMING_SNAKE_CASE = mapping["""model.embed_positions.weight"""] __SCREAMING_SNAKE_CASE = mapping.pop("""model.embed_positions.weight""" ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = torch_model.load_state_dict(a__ , strict=a__ ) __SCREAMING_SNAKE_CASE = [ k for k in missing if k not in [ """final_logits_bias""", """model.encoder.embed_tokens.weight""", """model.decoder.embed_tokens.weight""", """lm_head.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__ ( a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE = tf.train.list_variables(a__ ) __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = ["""global_step"""] for name, shape in tqdm(a__ , desc="""converting tf checkpoint to dict""" ): __SCREAMING_SNAKE_CASE = any(pat in name for pat in ignore_name ) if skip_key: continue __SCREAMING_SNAKE_CASE = tf.train.load_variable(a__ , a__ ) __SCREAMING_SNAKE_CASE = array return tf_weights def a__ ( a__ , a__ , a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE = get_tf_weights_as_numpy(a__ ) __SCREAMING_SNAKE_CASE = convert_bigbird_pegasus(a__ , a__ ) torch_model.save_pretrained(a__ ) if __name__ == "__main__": UpperCAmelCase : Any = argparse.ArgumentParser() 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.') UpperCAmelCase : int = parser.parse_args() UpperCAmelCase : Dict = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
331
1
'''simple docstring''' import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class __SCREAMING_SNAKE_CASE (lowerCamelCase_ , unittest.TestCase ): """simple docstring""" __a =FlaxAutoencoderKL @property def UpperCamelCase__ ( self : List[str] ): _a = 4 _a = 3 _a = (32, 32) _a = jax.random.PRNGKey(0 ) _a = jax.random.uniform(__a , ((batch_size, num_channels) + sizes) ) return {"sample": image, "prng_key": prng_key} def UpperCamelCase__ ( self : str ): _a = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 4, } _a = self.dummy_input return init_dict, inputs_dict
63
'''simple docstring''' from ....utils import logging lowerCAmelCase_ : Union[str, Any] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : Tuple , __a : int , __a : Any=None , __a : Optional[int]=20_48 ): _a = config.__dict__ _a = modal_hidden_size if num_labels: _a = num_labels
63
1
"""simple docstring""" import unittest import numpy as np def lowercase__ ( snake_case_ :np.ndarray , snake_case_ :np.ndarray , snake_case_ :np.ndarray , snake_case_ :np.ndarray | None = None , ): __UpperCAmelCase = np.shape(a_ ) __UpperCAmelCase = np.shape(a_ ) __UpperCAmelCase = np.shape(a_ ) if shape_a[0] != shape_b[0]: __UpperCAmelCase = ( "Expected the same number of rows for A and B. " F'''Instead found A of size {shape_a} and B of size {shape_b}''' ) raise ValueError(a_ ) if shape_b[1] != shape_c[1]: __UpperCAmelCase = ( "Expected the same number of columns for B and C. " F'''Instead found B of size {shape_b} and C of size {shape_c}''' ) raise ValueError(a_ ) __UpperCAmelCase = pseudo_inv if a_inv is None: try: __UpperCAmelCase = np.linalg.inv(a_ ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class _UpperCAmelCase ( unittest.TestCase ): def a ( self : Union[str, Any] ): __UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) __UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) __UpperCAmelCase = np.array([[2, 1], [6, 3]] ) __UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) __UpperCAmelCase = np.block([[a, b], [b.T, c]] ) __UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) __UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) __UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def a ( self : int ): __UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) __UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) __UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def a ( self : Any ): __UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) __UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) __UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
367
"""simple docstring""" import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch _lowercase : Any = True except ImportError: _lowercase : str = False try: from torch.hub import _get_torch_home _lowercase : Any = _get_torch_home() except ImportError: _lowercase : Dict = os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) _lowercase : Tuple = os.path.join(torch_cache_home, 'transformers') _lowercase : int = 'https://cdn.huggingface.co' _lowercase : Union[str, Any] = 'https://s3.amazonaws.com/models.huggingface.co/bert' _lowercase : str = '/'.join(str(Path(__file__).resolve()).split('/')[:-1]) _lowercase : str = os.path.join(PATH, 'config.yaml') _lowercase : int = os.path.join(PATH, 'attributes.txt') _lowercase : List[str] = os.path.join(PATH, 'objects.txt') _lowercase : Optional[int] = os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) _lowercase : int = os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) _lowercase : Dict = os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) _lowercase : Union[str, Any] = 'pytorch_model.bin' _lowercase : List[str] = 'config.yaml' def lowercase__ ( snake_case_ :int=OBJECTS , snake_case_ :Optional[int]=ATTRIBUTES ): __UpperCAmelCase = [] with open(snake_case_ ) as f: for object in f.readlines(): vg_classes.append(object.split(''',''' )[0].lower().strip() ) __UpperCAmelCase = [] with open(snake_case_ ) as f: for object in f.readlines(): vg_attrs.append(object.split(''',''' )[0].lower().strip() ) return vg_classes, vg_attrs def lowercase__ ( snake_case_ :List[Any] ): __UpperCAmelCase = OrderedDict() with open(snake_case_ , '''rb''' ) as f: __UpperCAmelCase = pkl.load(snake_case_ )['''model'''] for k in copy.deepcopy(list(ckp.keys() ) ): __UpperCAmelCase = ckp.pop(snake_case_ ) if isinstance(snake_case_ , np.ndarray ): __UpperCAmelCase = torch.tensor(snake_case_ ) else: assert isinstance(snake_case_ , torch.tensor ), type(snake_case_ ) __UpperCAmelCase = v return r class _UpperCAmelCase : a__ : Tuple = {} def __init__( self : List[str] , _lowercase : dict , _lowercase : str = "root" , _lowercase : Optional[Any]=0 ): __UpperCAmelCase = name __UpperCAmelCase = level __UpperCAmelCase = {} for k, v in dictionary.items(): if v is None: raise ValueError() __UpperCAmelCase = copy.deepcopy(_lowercase ) __UpperCAmelCase = copy.deepcopy(_lowercase ) if isinstance(_lowercase , _lowercase ): __UpperCAmelCase = Config(_lowercase , name=_lowercase , level=level + 1 ) __UpperCAmelCase = v setattr(self , _lowercase , _lowercase ) __UpperCAmelCase = d def __repr__( self : Any ): return str(list((self._pointer.keys()) ) ) def __setattr__( self : Optional[Any] , _lowercase : Optional[Any] , _lowercase : Dict ): __UpperCAmelCase = val __UpperCAmelCase = val __UpperCAmelCase = key.split('''.''' ) __UpperCAmelCase = len(_lowercase ) - 1 __UpperCAmelCase = self._pointer if len(_lowercase ) > 1: for i, l in enumerate(_lowercase ): if hasattr(self , _lowercase ) and isinstance(getattr(self , _lowercase ) , _lowercase ): setattr(getattr(self , _lowercase ) , '''.'''.join(levels[i:] ) , _lowercase ) if l == last_level: __UpperCAmelCase = val else: __UpperCAmelCase = pointer[l] def a ( self : int ): return self._pointer def a ( self : List[str] , _lowercase : Dict , _lowercase : str ): with open(F'''{file_name}''' , '''w''' ) as stream: dump(_lowercase , _lowercase ) def a ( self : int , _lowercase : Dict , _lowercase : Tuple ): with open(F'''{file_name}''' , '''w''' ) as stream: json.dump(_lowercase , _lowercase ) @staticmethod def a ( _lowercase : str ): with open(_lowercase ) as stream: __UpperCAmelCase = load(_lowercase , Loader=_lowercase ) return data def __str__( self : Dict ): __UpperCAmelCase = ''' ''' if self._name != "root": __UpperCAmelCase = F'''{t * (self._level-1)}{self._name}:\n''' else: __UpperCAmelCase = '''''' __UpperCAmelCase = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(_lowercase , _lowercase ): r += F'''{t * (self._level)}{v}\n''' self._level += 1 else: r += F'''{t * (self._level)}{k}: {v} ({type(_lowercase ).__name__})\n''' __UpperCAmelCase = level return r[:-1] @classmethod def a ( cls : str , _lowercase : str , **_lowercase : Any ): __UpperCAmelCase , __UpperCAmelCase = cls.get_config_dict(_lowercase , **_lowercase ) return cls(_lowercase ) @classmethod def a ( cls : Any , _lowercase : str , **_lowercase : str ): __UpperCAmelCase = kwargs.pop('''cache_dir''' , _lowercase ) __UpperCAmelCase = kwargs.pop('''force_download''' , _lowercase ) __UpperCAmelCase = kwargs.pop('''resume_download''' , _lowercase ) __UpperCAmelCase = kwargs.pop('''proxies''' , _lowercase ) __UpperCAmelCase = kwargs.pop('''local_files_only''' , _lowercase ) if os.path.isdir(_lowercase ): __UpperCAmelCase = os.path.join(_lowercase , _lowercase ) elif os.path.isfile(_lowercase ) or is_remote_url(_lowercase ): __UpperCAmelCase = pretrained_model_name_or_path else: __UpperCAmelCase = hf_bucket_url(_lowercase , filename=_lowercase , use_cdn=_lowercase ) try: # Load from URL or cache if already cached __UpperCAmelCase = cached_path( _lowercase , cache_dir=_lowercase , force_download=_lowercase , proxies=_lowercase , resume_download=_lowercase , local_files_only=_lowercase , ) # Load config dict if resolved_config_file is None: raise EnvironmentError __UpperCAmelCase = Config.load_yaml(_lowercase ) except EnvironmentError: __UpperCAmelCase = '''Can\'t load config for''' raise EnvironmentError(_lowercase ) if resolved_config_file == config_file: print('''loading configuration file from path''' ) else: print('''loading configuration file cache''' ) return Config.load_yaml(_lowercase ), kwargs def lowercase__ ( snake_case_ :List[str] ): __UpperCAmelCase = torch.load('''dump.pt''' , map_location=in_tensor.device ) __UpperCAmelCase = in_tensor.numpy() __UpperCAmelCase = out_tensor.numpy()[0] print(na.shape , na[0, 0, :5] ) print(na.shape , na[0, 0, :5] ) assert np.allclose(snake_case_ , snake_case_ , rtol=0.01 , atol=0.1 ), ( F'''{sum([1 for x in np.isclose(snake_case_ , snake_case_ , rtol=0.01 , atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*100:.4f} %''' " element-wise mismatch" ) raise Exception('''tensors are all good''' ) # Hugging face functions below def lowercase__ ( snake_case_ :List[str] ): __UpperCAmelCase = urlparse(snake_case_ ) return parsed.scheme in ("http", "https") def lowercase__ ( snake_case_ :str , snake_case_ :str , snake_case_ :List[str]=True ): __UpperCAmelCase = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX __UpperCAmelCase = '''/''' not in model_id if legacy_format: return F'''{endpoint}/{model_id}-{filename}''' else: return F'''{endpoint}/{model_id}/{filename}''' def lowercase__ ( snake_case_ :str , snake_case_ :Tuple , snake_case_ :List[str]=None , snake_case_ :List[str]=0 , snake_case_ :List[Any]=None , ): __UpperCAmelCase = '''python/{}'''.format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(snake_case_ , snake_case_ ): ua += "; " + "; ".join('''{}/{}'''.format(snake_case_ , snake_case_ ) for k, v in user_agent.items() ) elif isinstance(snake_case_ , snake_case_ ): ua += "; " + user_agent __UpperCAmelCase = {'''user-agent''': ua} if resume_size > 0: __UpperCAmelCase = '''bytes=%d-''' % (resume_size,) __UpperCAmelCase = requests.get(snake_case_ , stream=snake_case_ , proxies=snake_case_ , headers=snake_case_ ) if response.status_code == 416: # Range not satisfiable return __UpperCAmelCase = response.headers.get('''Content-Length''' ) __UpperCAmelCase = resume_size + int(snake_case_ ) if content_length is not None else None __UpperCAmelCase = tqdm( unit='''B''' , unit_scale=snake_case_ , total=snake_case_ , initial=snake_case_ , desc='''Downloading''' , ) for chunk in response.iter_content(chunk_size=1_024 ): if chunk: # filter out keep-alive new chunks progress.update(len(snake_case_ ) ) temp_file.write(snake_case_ ) progress.close() def lowercase__ ( snake_case_ :Optional[int] , snake_case_ :str=None , snake_case_ :Optional[int]=False , snake_case_ :List[Any]=None , snake_case_ :List[Any]=10 , snake_case_ :Optional[int]=False , snake_case_ :List[str]=None , snake_case_ :Union[str, Any]=False , ): if cache_dir is None: __UpperCAmelCase = TRANSFORMERS_CACHE if isinstance(snake_case_ , snake_case_ ): __UpperCAmelCase = str(snake_case_ ) os.makedirs(snake_case_ , exist_ok=snake_case_ ) __UpperCAmelCase = None if not local_files_only: try: __UpperCAmelCase = requests.head(snake_case_ , allow_redirects=snake_case_ , proxies=snake_case_ , timeout=snake_case_ ) if response.status_code == 200: __UpperCAmelCase = response.headers.get('''ETag''' ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass __UpperCAmelCase = url_to_filename(snake_case_ , snake_case_ ) # get cache path to put the file __UpperCAmelCase = os.path.join(snake_case_ , snake_case_ ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(snake_case_ ): return cache_path else: __UpperCAmelCase = [ file for file in fnmatch.filter(os.listdir(snake_case_ ) , filename + '''.*''' ) if not file.endswith('''.json''' ) and not file.endswith('''.lock''' ) ] if len(snake_case_ ) > 0: return os.path.join(snake_case_ , matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( '''Cannot find the requested files in the cached path and outgoing traffic has been''' ''' disabled. To enable model look-ups and downloads online, set \'local_files_only\'''' ''' to False.''' ) return None # From now on, etag is not None. if os.path.exists(snake_case_ ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. __UpperCAmelCase = cache_path + '''.lock''' with FileLock(snake_case_ ): # If the download just completed while the lock was activated. if os.path.exists(snake_case_ ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: __UpperCAmelCase = cache_path + '''.incomplete''' @contextmanager def _resumable_file_manager(): with open(snake_case_ , '''a+b''' ) as f: yield f __UpperCAmelCase = _resumable_file_manager if os.path.exists(snake_case_ ): __UpperCAmelCase = os.stat(snake_case_ ).st_size else: __UpperCAmelCase = 0 else: __UpperCAmelCase = partial(tempfile.NamedTemporaryFile , dir=snake_case_ , delete=snake_case_ ) __UpperCAmelCase = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( '''%s not found in cache or force_download set to True, downloading to %s''' , snake_case_ , temp_file.name , ) http_get( snake_case_ , snake_case_ , proxies=snake_case_ , resume_size=snake_case_ , user_agent=snake_case_ , ) os.replace(temp_file.name , snake_case_ ) __UpperCAmelCase = {'''url''': url, '''etag''': etag} __UpperCAmelCase = cache_path + '''.json''' with open(snake_case_ , '''w''' ) as meta_file: json.dump(snake_case_ , snake_case_ ) return cache_path def lowercase__ ( snake_case_ :int , snake_case_ :str=None ): __UpperCAmelCase = url.encode('''utf-8''' ) __UpperCAmelCase = shaaaa(snake_case_ ) __UpperCAmelCase = url_hash.hexdigest() if etag: __UpperCAmelCase = etag.encode('''utf-8''' ) __UpperCAmelCase = shaaaa(snake_case_ ) filename += "." + etag_hash.hexdigest() if url.endswith('''.h5''' ): filename += ".h5" return filename def lowercase__ ( snake_case_ :Dict , snake_case_ :List[Any]=None , snake_case_ :List[Any]=False , snake_case_ :Optional[int]=None , snake_case_ :List[Any]=False , snake_case_ :Optional[Any]=None , snake_case_ :Any=False , snake_case_ :int=False , snake_case_ :Optional[int]=False , ): if cache_dir is None: __UpperCAmelCase = TRANSFORMERS_CACHE if isinstance(snake_case_ , snake_case_ ): __UpperCAmelCase = str(snake_case_ ) if isinstance(snake_case_ , snake_case_ ): __UpperCAmelCase = str(snake_case_ ) if is_remote_url(snake_case_ ): # URL, so get it from the cache (downloading if necessary) __UpperCAmelCase = get_from_cache( snake_case_ , cache_dir=snake_case_ , force_download=snake_case_ , proxies=snake_case_ , resume_download=snake_case_ , user_agent=snake_case_ , local_files_only=snake_case_ , ) elif os.path.exists(snake_case_ ): # File, and it exists. __UpperCAmelCase = url_or_filename elif urlparse(snake_case_ ).scheme == "": # File, but it doesn't exist. raise EnvironmentError('''file {} not found'''.format(snake_case_ ) ) else: # Something unknown raise ValueError('''unable to parse {} as a URL or as a local path'''.format(snake_case_ ) ) if extract_compressed_file: if not is_zipfile(snake_case_ ) and not tarfile.is_tarfile(snake_case_ ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" __UpperCAmelCase , __UpperCAmelCase = os.path.split(snake_case_ ) __UpperCAmelCase = output_file.replace('''.''' , '''-''' ) + '''-extracted''' __UpperCAmelCase = os.path.join(snake_case_ , snake_case_ ) if os.path.isdir(snake_case_ ) and os.listdir(snake_case_ ) and not force_extract: return output_path_extracted # Prevent parallel extractions __UpperCAmelCase = output_path + '''.lock''' with FileLock(snake_case_ ): shutil.rmtree(snake_case_ , ignore_errors=snake_case_ ) os.makedirs(snake_case_ ) if is_zipfile(snake_case_ ): with ZipFile(snake_case_ , '''r''' ) as zip_file: zip_file.extractall(snake_case_ ) zip_file.close() elif tarfile.is_tarfile(snake_case_ ): __UpperCAmelCase = tarfile.open(snake_case_ ) tar_file.extractall(snake_case_ ) tar_file.close() else: raise EnvironmentError('''Archive format of {} could not be identified'''.format(snake_case_ ) ) return output_path_extracted return output_path def lowercase__ ( snake_case_ :List[Any] , snake_case_ :List[Any]="," ): assert isinstance(snake_case_ , snake_case_ ) if os.path.isfile(snake_case_ ): with open(snake_case_ ) as f: __UpperCAmelCase = eval(f.read() ) else: __UpperCAmelCase = requests.get(snake_case_ ) try: __UpperCAmelCase = requests.json() except Exception: __UpperCAmelCase = req.content.decode() assert data is not None, "could not connect" try: __UpperCAmelCase = eval(snake_case_ ) except Exception: __UpperCAmelCase = data.split('''\n''' ) req.close() return data def lowercase__ ( snake_case_ :Union[str, Any] ): __UpperCAmelCase = requests.get(snake_case_ ) __UpperCAmelCase = np.array(Image.open(BytesIO(response.content ) ) ) return img def lowercase__ ( snake_case_ :List[str] ): __UpperCAmelCase = url.split('''/''' )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(snake_case_ ) with open(snake_case_ , '''rb''' ) as stream: __UpperCAmelCase = pkl.load(snake_case_ ) __UpperCAmelCase = weights.pop('''model''' ) __UpperCAmelCase = {} for k, v in model.items(): __UpperCAmelCase = torch.from_numpy(snake_case_ ) if "running_var" in k: __UpperCAmelCase = torch.tensor([0] ) __UpperCAmelCase = k.replace('''running_var''' , '''num_batches_tracked''' ) __UpperCAmelCase = zero return new def lowercase__ ( ): print(F'''{os.path.abspath(os.path.join(snake_case_ , os.pardir ) )}/demo.ipynb''' ) def lowercase__ ( snake_case_ :Tuple , snake_case_ :Tuple="RGB" ): assert isinstance(snake_case_ , snake_case_ ) if os.path.isfile(snake_case_ ): __UpperCAmelCase = cva.imread(snake_case_ ) else: __UpperCAmelCase = get_image_from_url(snake_case_ ) assert img is not None, F'''could not connect to: {im}''' __UpperCAmelCase = cva.cvtColor(snake_case_ , cva.COLOR_BGR2RGB ) if input_format == "RGB": __UpperCAmelCase = img[:, :, ::-1] return img def lowercase__ ( snake_case_ :Any , snake_case_ :int=1 ): return (images[i : i + batch] for i in range(0 , len(snake_case_ ) , snake_case_ ))
86
0
"""simple docstring""" class A__ : '''simple docstring''' def __init__( self: Any , _SCREAMING_SNAKE_CASE: int) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : Optional[Any] = n __lowerCAmelCase : List[Any] = [None] * self.n __lowerCAmelCase : int = 0 # index of the first element __lowerCAmelCase : Tuple = 0 __lowerCAmelCase : Any = 0 def __len__( self: Dict) -> int: """simple docstring""" return self.size def _SCREAMING_SNAKE_CASE ( self: List[str]) -> bool: """simple docstring""" return self.size == 0 def _SCREAMING_SNAKE_CASE ( self: List[Any]) -> List[str]: """simple docstring""" return False if self.is_empty() else self.array[self.front] def _SCREAMING_SNAKE_CASE ( self: Optional[Any] , _SCREAMING_SNAKE_CASE: List[Any]) -> Tuple: """simple docstring""" if self.size >= self.n: raise Exception("QUEUE IS FULL") __lowerCAmelCase : Optional[Any] = data __lowerCAmelCase : Tuple = (self.rear + 1) % self.n self.size += 1 return self def _SCREAMING_SNAKE_CASE ( self: Any) -> int: """simple docstring""" if self.size == 0: raise Exception("UNDERFLOW") __lowerCAmelCase : Any = self.array[self.front] __lowerCAmelCase : int = None __lowerCAmelCase : Union[str, Any] = (self.front + 1) % self.n self.size -= 1 return temp
269
"""simple docstring""" from __future__ import annotations def _lowercase ( __snake_case ,__snake_case ,__snake_case ,__snake_case ) -> list: __lowerCAmelCase : Dict = [] __lowerCAmelCase , __lowerCAmelCase : Any = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0 ) ) __lowerCAmelCase : int = result + left + right return input_list def _lowercase ( __snake_case ) -> list: if len(__snake_case ) <= 1: return input_list __lowerCAmelCase : int = list(__snake_case ) # iteration for two-way merging __lowerCAmelCase : Optional[int] = 2 while p <= len(__snake_case ): # getting low, high and middle value for merge-sort of single list for i in range(0 ,len(__snake_case ) ,__snake_case ): __lowerCAmelCase : Union[str, Any] = i __lowerCAmelCase : Tuple = i + p - 1 __lowerCAmelCase : Optional[Any] = (low + high + 1) // 2 __lowerCAmelCase : Any = merge(__snake_case ,__snake_case ,__snake_case ,__snake_case ) # final merge of last two parts if p * 2 >= len(__snake_case ): __lowerCAmelCase : Optional[Any] = i __lowerCAmelCase : Union[str, Any] = merge(__snake_case ,0 ,__snake_case ,len(__snake_case ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": __snake_case : Union[str, Any] = input('Enter numbers separated by a comma:\n').strip() if user_input == "": __snake_case : Optional[int] = [] else: __snake_case : int = [int(item.strip()) for item in user_input.split(',')] print(iter_merge_sort(unsorted))
269
1
def lowerCAmelCase_ ( ) -> int: '''simple docstring''' return [ a * b * (1_000 - a - b) for a in range(1, 999 ) for b in range(__A, 999 ) if (a * a + b * b == (1_000 - a - b) ** 2) ][0] if __name__ == "__main__": print(f'''{solution() = }''')
370
from __future__ import annotations import unittest from transformers import RoFormerConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerModel, ) from transformers.models.roformer.modeling_tf_roformer import ( TFRoFormerSelfAttention, TFRoFormerSinusoidalPositionalEmbedding, ) class A : def __init__(self : int , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str]=1_3 , __UpperCAmelCase : Optional[Any]=7 , __UpperCAmelCase : Dict=True , __UpperCAmelCase : int=True , __UpperCAmelCase : Any=True , __UpperCAmelCase : int=True , __UpperCAmelCase : Dict=9_9 , __UpperCAmelCase : Optional[int]=3_2 , __UpperCAmelCase : Dict=2 , __UpperCAmelCase : Any=4 , __UpperCAmelCase : Tuple=3_7 , __UpperCAmelCase : List[Any]="gelu" , __UpperCAmelCase : Dict=0.1 , __UpperCAmelCase : int=0.1 , __UpperCAmelCase : str=5_1_2 , __UpperCAmelCase : Union[str, Any]=1_6 , __UpperCAmelCase : Tuple=2 , __UpperCAmelCase : Tuple=0.02 , __UpperCAmelCase : Optional[int]=3 , __UpperCAmelCase : str=4 , __UpperCAmelCase : Any=None , ) -> Dict: """simple docstring""" UpperCAmelCase__ = parent UpperCAmelCase__ = 1_3 UpperCAmelCase__ = 7 UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = 9_9 UpperCAmelCase__ = 3_2 UpperCAmelCase__ = 2 UpperCAmelCase__ = 4 UpperCAmelCase__ = 3_7 UpperCAmelCase__ = "gelu" UpperCAmelCase__ = 0.1 UpperCAmelCase__ = 0.1 UpperCAmelCase__ = 5_1_2 UpperCAmelCase__ = 1_6 UpperCAmelCase__ = 2 UpperCAmelCase__ = 0.02 UpperCAmelCase__ = 3 UpperCAmelCase__ = 4 UpperCAmelCase__ = None def lowercase_ (self : Union[str, Any] ) -> str: """simple docstring""" UpperCAmelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase__ = None if self.use_input_mask: UpperCAmelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase__ = None if self.use_token_type_ids: UpperCAmelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase__ = None UpperCAmelCase__ = None UpperCAmelCase__ = None if self.use_labels: UpperCAmelCase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase__ = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase__ = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=__UpperCAmelCase , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ (self : List[Any] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Any ) -> List[str]: """simple docstring""" UpperCAmelCase__ = TFRoFormerModel(config=__UpperCAmelCase ) UpperCAmelCase__ = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} UpperCAmelCase__ = [input_ids, input_mask] UpperCAmelCase__ = model(__UpperCAmelCase ) UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase_ (self : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Any , __UpperCAmelCase : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : List[Any] ) -> Any: """simple docstring""" UpperCAmelCase__ = True UpperCAmelCase__ = TFRoFormerForCausalLM(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase )["logits"] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def lowercase_ (self : str , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Any , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Any , __UpperCAmelCase : int ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase__ = TFRoFormerForMaskedLM(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ (self : Optional[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : str ) -> Optional[Any]: """simple docstring""" UpperCAmelCase__ = self.num_labels UpperCAmelCase__ = TFRoFormerForSequenceClassification(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase_ (self : str , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : int , __UpperCAmelCase : Any , __UpperCAmelCase : Tuple , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any] ) -> Any: """simple docstring""" UpperCAmelCase__ = self.num_choices UpperCAmelCase__ = TFRoFormerForMultipleChoice(config=__UpperCAmelCase ) UpperCAmelCase__ = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) UpperCAmelCase__ = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) UpperCAmelCase__ = tf.tile(tf.expand_dims(__UpperCAmelCase , 1 ) , (1, self.num_choices, 1) ) UpperCAmelCase__ = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowercase_ (self : int , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : int , __UpperCAmelCase : int , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : int ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase__ = self.num_labels UpperCAmelCase__ = TFRoFormerForTokenClassification(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ (self : Dict , __UpperCAmelCase : Tuple , __UpperCAmelCase : Any , __UpperCAmelCase : Any , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Dict ) -> List[str]: """simple docstring""" UpperCAmelCase__ = TFRoFormerForQuestionAnswering(config=__UpperCAmelCase ) UpperCAmelCase__ = { "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } UpperCAmelCase__ = model(__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 lowercase_ (self : Dict ) -> Dict: """simple docstring""" UpperCAmelCase__ = self.prepare_config_and_inputs() ( ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ) = config_and_inputs UpperCAmelCase__ = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class A ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): __UpperCAmelCase : str = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) __UpperCAmelCase : List[str] = ( { 'feature-extraction': TFRoFormerModel, 'fill-mask': TFRoFormerForMaskedLM, 'question-answering': TFRoFormerForQuestionAnswering, 'text-classification': TFRoFormerForSequenceClassification, 'text-generation': TFRoFormerForCausalLM, 'token-classification': TFRoFormerForTokenClassification, 'zero-shot': TFRoFormerForSequenceClassification, } if is_tf_available() else {} ) __UpperCAmelCase : Optional[int] = False __UpperCAmelCase : List[Any] = False def lowercase_ (self : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Dict , __UpperCAmelCase : Any , __UpperCAmelCase : Dict , __UpperCAmelCase : Any ) -> int: """simple docstring""" if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def lowercase_ (self : Union[str, Any] ) -> List[Any]: """simple docstring""" UpperCAmelCase__ = TFRoFormerModelTester(self ) UpperCAmelCase__ = ConfigTester(self , config_class=__UpperCAmelCase , hidden_size=3_7 ) def lowercase_ (self : Optional[int] ) -> Any: """simple docstring""" self.config_tester.run_common_tests() def lowercase_ (self : Union[str, Any] ) -> List[Any]: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__UpperCAmelCase ) def lowercase_ (self : Dict ) -> Any: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase ) def lowercase_ (self : Union[str, Any] ) -> str: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*__UpperCAmelCase ) def lowercase_ (self : Optional[Any] ) -> Dict: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase ) def lowercase_ (self : List[Any] ) -> List[str]: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase ) def lowercase_ (self : int ) -> List[Any]: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase ) def lowercase_ (self : List[str] ) -> Tuple: """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase ) @slow def lowercase_ (self : Any ) -> Optional[Any]: """simple docstring""" UpperCAmelCase__ = TFRoFormerModel.from_pretrained("junnyu/roformer_chinese_base" ) self.assertIsNotNone(__UpperCAmelCase ) @require_tf class A ( unittest.TestCase ): @slow def lowercase_ (self : List[Any] ) -> Any: """simple docstring""" UpperCAmelCase__ = TFRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base" ) UpperCAmelCase__ = tf.constant([[0, 1, 2, 3, 4, 5]] ) UpperCAmelCase__ = model(__UpperCAmelCase )[0] # TODO Replace vocab size UpperCAmelCase__ = 5_0_0_0_0 UpperCAmelCase__ = [1, 6, vocab_size] self.assertEqual(output.shape , __UpperCAmelCase ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. UpperCAmelCase__ = tf.constant( [ [ [-0.12053341, -1.0264901, 0.29221946], [-1.5133783, 0.197433, 0.15190607], [-5.0135403, -3.900256, -0.84038764], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __UpperCAmelCase , atol=1E-4 ) @require_tf class A ( unittest.TestCase ): __UpperCAmelCase : Tuple = 1E-4 def lowercase_ (self : List[str] ) -> str: """simple docstring""" UpperCAmelCase__ = tf.constant([[4, 1_0]] ) UpperCAmelCase__ = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) UpperCAmelCase__ = emba(input_ids.shape ) UpperCAmelCase__ = tf.constant( [[0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 1.0000], [0.8415, 0.0464, 0.0022, 0.5403, 0.9989, 1.0000]] ) tf.debugging.assert_near(__UpperCAmelCase , __UpperCAmelCase , atol=self.tolerance ) def lowercase_ (self : List[Any] ) -> int: """simple docstring""" UpperCAmelCase__ = tf.constant( [ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.8415, 0.8219, 0.8020, 0.7819, 0.7617], [0.9093, 0.9364, 0.9581, 0.9749, 0.9870], ] ) UpperCAmelCase__ = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_1_2 , embedding_dim=5_1_2 ) emba([2, 1_6, 5_1_2] ) UpperCAmelCase__ = emba.weight[:3, :5] tf.debugging.assert_near(__UpperCAmelCase , __UpperCAmelCase , atol=self.tolerance ) @require_tf class A ( unittest.TestCase ): __UpperCAmelCase : Any = 1E-4 def lowercase_ (self : Tuple ) -> int: """simple docstring""" UpperCAmelCase__ = tf.reshape(tf.range(2 * 1_2 * 1_6 * 6_4 , dtype=tf.floataa ) , shape=(2, 1_2, 1_6, 6_4) ) / 1_0_0 UpperCAmelCase__ = -tf.reshape(tf.range(2 * 1_2 * 1_6 * 6_4 , dtype=tf.floataa ) , shape=(2, 1_2, 1_6, 6_4) ) / 1_0_0 UpperCAmelCase__ = TFRoFormerSinusoidalPositionalEmbedding(num_positions=3_2 , embedding_dim=6_4 ) UpperCAmelCase__ = embed_positions([2, 1_6, 7_6_8] )[None, None, :, :] UpperCAmelCase__ , UpperCAmelCase__ = TFRoFormerSelfAttention.apply_rotary_position_embeddings( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) UpperCAmelCase__ = tf.constant( [ [0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700], [-0.2012, 0.8897, 0.0263, 0.9401, 0.2074, 0.9463, 0.3481, 0.9343], [-1.7057, 0.6271, -1.2145, 1.3897, -0.6303, 1.7647, -0.1173, 1.8985], [-2.1731, -1.6397, -2.7358, 0.2854, -2.1840, 1.7183, -1.3018, 2.4871], [0.2717, -3.6173, -2.9206, -2.1988, -3.6638, 0.3858, -2.9155, 2.2980], [3.9859, -2.1580, -0.7984, -4.4904, -4.1181, -2.0252, -4.4782, 1.1253], ] ) UpperCAmelCase__ = tf.constant( [ [0.0000, -0.0100, -0.0200, -0.0300, -0.0400, -0.0500, -0.0600, -0.0700], [0.2012, -0.8897, -0.0263, -0.9401, -0.2074, -0.9463, -0.3481, -0.9343], [1.7057, -0.6271, 1.2145, -1.3897, 0.6303, -1.7647, 0.1173, -1.8985], [2.1731, 1.6397, 2.7358, -0.2854, 2.1840, -1.7183, 1.3018, -2.4871], [-0.2717, 3.6173, 2.9206, 2.1988, 3.6638, -0.3858, 2.9155, -2.2980], [-3.9859, 2.1580, 0.7984, 4.4904, 4.1181, 2.0252, 4.4782, -1.1253], ] ) tf.debugging.assert_near(query_layer[0, 0, :6, :8] , __UpperCAmelCase , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , __UpperCAmelCase , atol=self.tolerance )
143
0
"""simple docstring""" from __future__ import annotations from typing import Any class __A : '''simple docstring''' def __init__( self : Optional[Any] ,_snake_case : int ,_snake_case : int ,_snake_case : float = 0 ) -> None: """simple docstring""" lowercase__ , lowercase__ : int = row, column lowercase__ : Any = [[default_value for c in range(_snake_case )] for r in range(_snake_case )] def __str__( self : Dict ) -> str: """simple docstring""" lowercase__ : List[str] = f"""Matrix consist of {self.row} rows and {self.column} columns\n""" # Make string identifier lowercase__ : Optional[int] = 0 for row_vector in self.array: for obj in row_vector: lowercase__ : str = max(_snake_case ,len(str(_snake_case ) ) ) lowercase__ : Tuple = f"""%{max_element_length}s""" # Make string and return def single_line(_snake_case : list[float] ) -> str: nonlocal string_format_identifier lowercase__ : Dict = '''[''' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(_snake_case ) for row_vector in self.array ) return s def __repr__( self : Optional[Any] ) -> str: """simple docstring""" return str(self ) def UpperCAmelCase ( self : Tuple ,_snake_case : tuple[int, int] ) -> bool: """simple docstring""" if not (isinstance(_snake_case ,(list, tuple) ) and len(_snake_case ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : Optional[int] ,_snake_case : tuple[int, int] ) -> Any: """simple docstring""" assert self.validate_indicies(_snake_case ) return self.array[loc[0]][loc[1]] def __setitem__( self : Union[str, Any] ,_snake_case : tuple[int, int] ,_snake_case : float ) -> None: """simple docstring""" assert self.validate_indicies(_snake_case ) lowercase__ : Any = value def __add__( self : int ,_snake_case : Matrix ) -> Matrix: """simple docstring""" assert isinstance(_snake_case ,_snake_case ) assert self.row == another.row and self.column == another.column # Add lowercase__ : List[Any] = Matrix(self.row ,self.column ) for r in range(self.row ): for c in range(self.column ): lowercase__ : Any = self[r, c] + another[r, c] return result def __neg__( self : Dict ) -> Matrix: """simple docstring""" lowercase__ : Dict = Matrix(self.row ,self.column ) for r in range(self.row ): for c in range(self.column ): lowercase__ : List[str] = -self[r, c] return result def __sub__( self : int ,_snake_case : Matrix ) -> Matrix: """simple docstring""" return self + (-another) def __mul__( self : int ,_snake_case : int | float | Matrix ) -> Matrix: """simple docstring""" if isinstance(_snake_case ,(int, float) ): # Scalar multiplication lowercase__ : str = Matrix(self.row ,self.column ) for r in range(self.row ): for c in range(self.column ): lowercase__ : Tuple = self[r, c] * another return result elif isinstance(_snake_case ,_snake_case ): # Matrix multiplication assert self.column == another.row lowercase__ : Any = Matrix(self.row ,another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: lowercase__ : Union[str, Any] = f"""Unsupported type given for another ({type(_snake_case )})""" raise TypeError(_snake_case ) def UpperCAmelCase ( self : Dict ) -> Matrix: """simple docstring""" lowercase__ : int = Matrix(self.column ,self.row ) for r in range(self.row ): for c in range(self.column ): lowercase__ : Dict = self[r, c] return result def UpperCAmelCase ( self : int ,_snake_case : Matrix ,_snake_case : Matrix ) -> Any: """simple docstring""" assert isinstance(_snake_case ,_snake_case ) and isinstance(_snake_case ,_snake_case ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate lowercase__ : Dict = v.transpose() lowercase__ : Union[str, Any] = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def __UpperCAmelCase ( ) -> None: # a^(-1) lowercase__ : Tuple = Matrix(3 , 3 , 0 ) for i in range(3 ): lowercase__ : List[Any] = 1 print(f"""a^(-1) is {ainv}""" ) # u, v lowercase__ : Tuple = Matrix(3 , 1 , 0 ) lowercase__ , lowercase__ , lowercase__ : Dict = 1, 2, -3 lowercase__ : List[Any] = Matrix(3 , 1 , 0 ) lowercase__ , lowercase__ , lowercase__ : str = 4, -2, 5 print(f"""u is {u}""" ) print(f"""v is {v}""" ) print(f"""uv^T is {u * v.transpose()}""" ) # Sherman Morrison print(f"""(a + uv^T)^(-1) is {ainv.sherman_morrison(__lowerCamelCase , __lowerCamelCase )}""" ) def __UpperCAmelCase ( ) -> None: import doctest doctest.testmod() testa()
16
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def UpperCamelCase ( snake_case__ : Dict , snake_case__ : Any=0.999 , snake_case__ : List[Any]="cosine" , ) -> Optional[int]: if alpha_transform_type == "cosine": def alpha_bar_fn(snake_case__ : int ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(snake_case__ : Optional[Any] ): return math.exp(t * -12.0 ) else: raise ValueError(F"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) UpperCamelCase : List[Any] = [] for i in range(snake_case__ ): UpperCamelCase : Optional[Any] = i / num_diffusion_timesteps UpperCamelCase : Union[str, Any] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(snake_case__ ) / alpha_bar_fn(snake_case__ ) , snake_case__ ) ) return torch.tensor(snake_case__ , dtype=torch.floataa ) class lowerCAmelCase_ ( a__ , a__ ): UpperCAmelCase__ : List[Any] = [e.name for e in KarrasDiffusionSchedulers] UpperCAmelCase__ : List[str] = 2 @register_to_config def __init__( self, SCREAMING_SNAKE_CASE_ = 1000, SCREAMING_SNAKE_CASE_ = 0.0_00_85, SCREAMING_SNAKE_CASE_ = 0.0_12, SCREAMING_SNAKE_CASE_ = "linear", SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = "epsilon", SCREAMING_SNAKE_CASE_ = "linspace", SCREAMING_SNAKE_CASE_ = 0, ) -> List[str]: if trained_betas is not None: UpperCamelCase : Union[str, Any] = torch.tensor(SCREAMING_SNAKE_CASE_, dtype=torch.floataa ) elif beta_schedule == "linear": UpperCamelCase : List[Any] = torch.linspace(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. UpperCamelCase : int = ( torch.linspace(beta_start**0.5, beta_end**0.5, SCREAMING_SNAKE_CASE_, dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule UpperCamelCase : Optional[int] = betas_for_alpha_bar(SCREAMING_SNAKE_CASE_ ) else: raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}""" ) UpperCamelCase : Optional[int] = 1.0 - self.betas UpperCamelCase : int = torch.cumprod(self.alphas, dim=0 ) # set all values self.set_timesteps(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=None ) -> str: if schedule_timesteps is None: UpperCamelCase : Union[str, Any] = self.timesteps UpperCamelCase : Dict = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: UpperCamelCase : Optional[int] = 1 if len(SCREAMING_SNAKE_CASE_ ) > 1 else 0 else: UpperCamelCase : str = timestep.cpu().item() if torch.is_tensor(SCREAMING_SNAKE_CASE_ ) else timestep UpperCamelCase : int = self._index_counter[timestep_int] return indices[pos].item() @property def snake_case_ ( self ) -> List[Any]: # standard deviation of the initial noise distribution if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, ) -> torch.FloatTensor: UpperCamelCase : Optional[int] = self.index_for_timestep(SCREAMING_SNAKE_CASE_ ) if self.state_in_first_order: UpperCamelCase : Dict = self.sigmas[step_index] else: UpperCamelCase : int = self.sigmas_interpol[step_index] UpperCamelCase : Any = sample / ((sigma**2 + 1) ** 0.5) return sample def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, ) -> Optional[int]: UpperCamelCase : Dict = num_inference_steps UpperCamelCase : List[Any] = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": UpperCamelCase : int = np.linspace(0, num_train_timesteps - 1, SCREAMING_SNAKE_CASE_, dtype=SCREAMING_SNAKE_CASE_ )[::-1].copy() elif self.config.timestep_spacing == "leading": UpperCamelCase : List[Any] = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 UpperCamelCase : Optional[Any] = (np.arange(0, SCREAMING_SNAKE_CASE_ ) * step_ratio).round()[::-1].copy().astype(SCREAMING_SNAKE_CASE_ ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": UpperCamelCase : Optional[int] = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 UpperCamelCase : Any = (np.arange(SCREAMING_SNAKE_CASE_, 0, -step_ratio )).round().copy().astype(SCREAMING_SNAKE_CASE_ ) timesteps -= 1 else: raise ValueError( F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""" ) UpperCamelCase : Tuple = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) UpperCamelCase : Optional[int] = torch.from_numpy(np.log(SCREAMING_SNAKE_CASE_ ) ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[int] = np.interp(SCREAMING_SNAKE_CASE_, np.arange(0, len(SCREAMING_SNAKE_CASE_ ) ), SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) UpperCamelCase : Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).to(device=SCREAMING_SNAKE_CASE_ ) # interpolate sigmas UpperCamelCase : Union[str, Any] = sigmas.log().lerp(sigmas.roll(1 ).log(), 0.5 ).exp() UpperCamelCase : Any = torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2 ), sigmas[-1:]] ) UpperCamelCase : Optional[Any] = torch.cat( [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2 ), sigmas_interpol[-1:]] ) if str(SCREAMING_SNAKE_CASE_ ).startswith('mps' ): # mps does not support float64 UpperCamelCase : Any = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_, dtype=torch.floataa ) else: UpperCamelCase : Dict = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) # interpolate timesteps UpperCamelCase : int = self.sigma_to_t(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_, dtype=timesteps.dtype ) UpperCamelCase : List[str] = torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None]), dim=-1 ).flatten() UpperCamelCase : Optional[Any] = torch.cat([timesteps[:1], interleaved_timesteps] ) UpperCamelCase : Optional[Any] = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter UpperCamelCase : Dict = defaultdict(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: # get log sigma UpperCamelCase : List[Any] = sigma.log() # get distribution UpperCamelCase : Optional[int] = log_sigma - self.log_sigmas[:, None] # get sigmas range UpperCamelCase : Optional[int] = dists.ge(0 ).cumsum(dim=0 ).argmax(dim=0 ).clamp(max=self.log_sigmas.shape[0] - 2 ) UpperCamelCase : Tuple = low_idx + 1 UpperCamelCase : List[str] = self.log_sigmas[low_idx] UpperCamelCase : Optional[Any] = self.log_sigmas[high_idx] # interpolate sigmas UpperCamelCase : int = (low - log_sigma) / (low - high) UpperCamelCase : Tuple = w.clamp(0, 1 ) # transform interpolation to time range UpperCamelCase : List[str] = (1 - w) * low_idx + w * high_idx UpperCamelCase : Dict = t.view(sigma.shape ) return t @property def snake_case_ ( self ) -> Optional[int]: return self.sample is None def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = True, ) -> Union[SchedulerOutput, Tuple]: UpperCamelCase : str = self.index_for_timestep(SCREAMING_SNAKE_CASE_ ) # advance index counter by 1 UpperCamelCase : Optional[int] = timestep.cpu().item() if torch.is_tensor(SCREAMING_SNAKE_CASE_ ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: UpperCamelCase : Tuple = self.sigmas[step_index] UpperCamelCase : Dict = self.sigmas_interpol[step_index + 1] UpperCamelCase : Optional[int] = self.sigmas[step_index + 1] else: # 2nd order / KDPM2's method UpperCamelCase : str = self.sigmas[step_index - 1] UpperCamelCase : Dict = self.sigmas_interpol[step_index] UpperCamelCase : Any = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API UpperCamelCase : Dict = 0 UpperCamelCase : Any = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": UpperCamelCase : Any = sigma_hat if self.state_in_first_order else sigma_interpol UpperCamelCase : List[Any] = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": UpperCamelCase : Union[str, Any] = sigma_hat if self.state_in_first_order else sigma_interpol UpperCamelCase : Optional[int] = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": raise NotImplementedError('prediction_type not implemented yet: sample' ) else: raise ValueError( F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""" ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order UpperCamelCase : int = (sample - pred_original_sample) / sigma_hat # 3. delta timestep UpperCamelCase : Any = sigma_interpol - sigma_hat # store for 2nd order step UpperCamelCase : Tuple = sample else: # DPM-Solver-2 # 2. Convert to an ODE derivative for 2nd order UpperCamelCase : Union[str, Any] = (sample - pred_original_sample) / sigma_interpol # 3. delta timestep UpperCamelCase : Dict = sigma_next - sigma_hat UpperCamelCase : Any = self.sample UpperCamelCase : str = None UpperCamelCase : Any = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, ) -> torch.FloatTensor: # Make sure sigmas and timesteps have the same device and dtype as original_samples UpperCamelCase : Optional[Any] = self.sigmas.to(device=original_samples.device, dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(SCREAMING_SNAKE_CASE_ ): # mps does not support float64 UpperCamelCase : List[str] = self.timesteps.to(original_samples.device, dtype=torch.floataa ) UpperCamelCase : str = timesteps.to(original_samples.device, dtype=torch.floataa ) else: UpperCamelCase : Dict = self.timesteps.to(original_samples.device ) UpperCamelCase : int = timesteps.to(original_samples.device ) UpperCamelCase : str = [self.index_for_timestep(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) for t in timesteps] UpperCamelCase : List[Any] = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): UpperCamelCase : int = sigma.unsqueeze(-1 ) UpperCamelCase : Any = original_samples + noise * sigma return noisy_samples def __len__( self ) -> Optional[int]: return self.config.num_train_timesteps
119
0
from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class A_ : '''simple docstring''' def __init__( self: Optional[int] , a: Tuple , a: Optional[Any]=3 , a: Tuple=32 , a: Any=3 , a: Optional[int]=10 , a: Any=[10, 20, 30, 40] , a: Union[str, Any]=[1, 1, 2, 1] , a: Tuple=True , a: str=True , a: Union[str, Any]="relu" , a: Optional[int]=3 , a: Optional[int]=None , ): __lowerCamelCase : List[Any] = parent __lowerCamelCase : Optional[int] = batch_size __lowerCamelCase : Optional[int] = image_size __lowerCamelCase : List[str] = num_channels __lowerCamelCase : Dict = embeddings_size __lowerCamelCase : Optional[int] = hidden_sizes __lowerCamelCase : Optional[Any] = depths __lowerCamelCase : Tuple = is_training __lowerCamelCase : Union[str, Any] = use_labels __lowerCamelCase : int = hidden_act __lowerCamelCase : Dict = num_labels __lowerCamelCase : Optional[Any] = scope __lowerCamelCase : Optional[Any] = len(lowerCamelCase_ ) def _snake_case ( self: str ): __lowerCamelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __lowerCamelCase : Union[str, Any] = None if self.use_labels: __lowerCamelCase : Tuple = ids_tensor([self.batch_size] , self.num_labels ) __lowerCamelCase : Union[str, Any] = self.get_config() return config, pixel_values, labels def _snake_case ( self: Tuple ): return ResNetConfig( 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 , image_size=self.image_size , ) def _snake_case ( self: Optional[int] , a: Optional[int] , a: Dict , a: Optional[int] ): __lowerCamelCase : Any = TFResNetModel(config=lowerCamelCase_ ) __lowerCamelCase : Tuple = model(lowerCamelCase_ ) # 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: List[str] , a: Any , a: List[Any] , a: Any ): __lowerCamelCase : Optional[int] = self.num_labels __lowerCamelCase : Dict = TFResNetForImageClassification(lowerCamelCase_ ) __lowerCamelCase : int = model(lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _snake_case ( self: Dict ): __lowerCamelCase : Optional[Any] = self.prepare_config_and_inputs() __lowerCamelCase : List[str] = config_and_inputs __lowerCamelCase : Optional[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class A_ ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' __snake_case = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () __snake_case = ( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) __snake_case = False __snake_case = False __snake_case = False __snake_case = False __snake_case = False def _snake_case ( self: int ): __lowerCamelCase : int = TFResNetModelTester(self ) __lowerCamelCase : Dict = ConfigTester(self , config_class=lowerCamelCase_ , has_text_modality=lowerCamelCase_ ) def _snake_case ( self: List[Any] ): 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: Any ): return @unittest.skip(reason='ResNet does not use inputs_embeds' ) def _snake_case ( self: Any ): pass @unittest.skip(reason='ResNet does not support input and output embeddings' ) def _snake_case ( self: Optional[int] ): pass def _snake_case ( self: List[Any] ): __lowerCamelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase : Any = model_class(lowerCamelCase_ ) __lowerCamelCase : Union[str, Any] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowerCamelCase : List[Any] = [*signature.parameters.keys()] __lowerCamelCase : Any = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def _snake_case ( self: List[Any] ): __lowerCamelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def _snake_case ( self: str ): def check_hidden_states_output(a: Dict , a: Tuple , a: Any ): __lowerCamelCase : Any = model_class(lowerCamelCase_ ) __lowerCamelCase : List[Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) __lowerCamelCase : Any = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states __lowerCamelCase : Optional[int] = self.model_tester.num_stages self.assertEqual(len(lowerCamelCase_ ) , expected_num_stages + 1 ) # ResNet'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] , ) __lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() __lowerCamelCase : Any = ["""basic""", """bottleneck"""] for model_class in self.all_model_classes: for layer_type in layers_type: __lowerCamelCase : Tuple = layer_type __lowerCamelCase : List[str] = True check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __lowerCamelCase : Dict = True check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def _snake_case ( self: str ): __lowerCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def _snake_case ( self: Union[str, Any] ): for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase : Dict = TFResNetModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def UpperCamelCase__ ( ): __lowerCamelCase : Dict = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class A_ ( unittest.TestCase ): '''simple docstring''' @cached_property def _snake_case ( self: int ): return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _snake_case ( self: Optional[Any] ): __lowerCamelCase : Optional[Any] = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) __lowerCamelCase : Tuple = self.default_image_processor __lowerCamelCase : List[str] = prepare_img() __lowerCamelCase : Union[str, Any] = image_processor(images=lowerCamelCase_ , return_tensors='tf' ) # forward pass __lowerCamelCase : Dict = model(**lowerCamelCase_ ) # verify the logits __lowerCamelCase : str = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) __lowerCamelCase : Union[str, Any] = tf.constant([-11.1069, -9.7_8_7_7, -8.3_7_7_7] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , lowerCamelCase_ , atol=1e-4 ) )
365
from math import pow def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ): if current_sum == needed_sum: # If the sum of the powers is equal to needed_sum, then we have a solution. solutions_count += 1 return current_sum, solutions_count __lowerCamelCase : Optional[Any] = int(pow(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) if current_sum + i_to_n <= needed_sum: # If the sum of the powers is less than needed_sum, then continue adding powers. current_sum += i_to_n __lowerCamelCase , __lowerCamelCase : Optional[Any] = backtrack( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , current_number + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) current_sum -= i_to_n if i_to_n < needed_sum: # If the power of i is less than needed_sum, then try with the next power. __lowerCamelCase , __lowerCamelCase : Dict = backtrack( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , current_number + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return current_sum, solutions_count def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if not (1 <= needed_sum <= 1_000 and 2 <= power <= 10): raise ValueError( 'Invalid input\n' 'needed_sum must be between 1 and 1000, power between 2 and 10.' ) return backtrack(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 1 , 0 , 0 )[1] # Return the solutions_count if __name__ == "__main__": import doctest doctest.testmod()
194
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase_ : List[Any] = logging.get_logger(__name__) lowerCamelCase_ : Union[str, Any] = { """facebook/xglm-564M""": """https://huggingface.co/facebook/xglm-564M/resolve/main/config.json""", # See all XGLM models at https://huggingface.co/models?filter=xglm } class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" __lowerCAmelCase = "xglm" __lowerCAmelCase = ["past_key_values"] __lowerCAmelCase = { "num_attention_heads": "attention_heads", "hidden_size": "d_model", "num_hidden_layers": "num_layers", } def __init__( self , __A=25_6008 , __A=2048 , __A=1024 , __A=4096 , __A=24 , __A=16 , __A="gelu" , __A=0.1 , __A=0.1 , __A=0.0 , __A=0.0 , __A=0.02 , __A=True , __A=True , __A=2 , __A=1 , __A=0 , __A=2 , **__A , ) -> str: a =vocab_size a =max_position_embeddings a =d_model a =ffn_dim a =num_layers a =attention_heads a =activation_function a =dropout a =attention_dropout a =activation_dropout a =layerdrop a =init_std a =scale_embedding # scale factor will be sqrt(d_model) if True a =use_cache super().__init__( pad_token_id=__A , bos_token_id=__A , eos_token_id=__A , decoder_start_token_id=__A , **__A , )
81
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowerCamelCase__ ( lowerCamelCase__): '''simple docstring''' snake_case_ =(DPMSolverSDEScheduler,) snake_case_ =10 def lowerCAmelCase__ (self ,**__lowerCamelCase ) -> List[str]: """simple docstring""" lowerCAmelCase__ : List[str] = { '''num_train_timesteps''': 11_00, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''noise_sampler_seed''': 0, } config.update(**__lowerCamelCase ) return config def lowerCAmelCase__ (self ) -> int: """simple docstring""" for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=__lowerCamelCase ) def lowerCAmelCase__ (self ) -> Optional[Any]: """simple docstring""" for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] ,[0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=__lowerCamelCase ,beta_end=__lowerCamelCase ) def lowerCAmelCase__ (self ) -> Union[str, Any]: """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=__lowerCamelCase ) def lowerCAmelCase__ (self ) -> Optional[Any]: """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__lowerCamelCase ) def lowerCAmelCase__ (self ) -> int: """simple docstring""" lowerCAmelCase__ : List[str] = self.scheduler_classes[0] lowerCAmelCase__ : str = self.get_scheduler_config() lowerCAmelCase__ : Optional[Any] = scheduler_class(**__lowerCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Union[str, Any] = self.dummy_model() lowerCAmelCase__ : Tuple = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Union[str, Any] = sample.to(__lowerCamelCase ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : Dict = scheduler.scale_model_input(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Any = model(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : List[Any] = scheduler.step(__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Optional[int] = output.prev_sample lowerCAmelCase__ : List[Any] = torch.sum(torch.abs(__lowerCamelCase ) ) lowerCAmelCase__ : Dict = torch.mean(torch.abs(__lowerCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47_8210_4492_1875 ) < 1e-2 assert abs(result_mean.item() - 0.2178_7059_6456_5277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_3521_1181_6406 ) < 1e-2 assert abs(result_mean.item() - 0.2_2342_9068_9229_9652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52_3834_2285_1562 ) < 1e-2 assert abs(result_mean.item() - 0.211_6195_7085_1326 ) < 1e-3 def lowerCAmelCase__ (self ) -> Union[str, Any]: """simple docstring""" lowerCAmelCase__ : Dict = self.scheduler_classes[0] lowerCAmelCase__ : Any = self.get_scheduler_config(prediction_type='''v_prediction''' ) lowerCAmelCase__ : List[Any] = scheduler_class(**__lowerCamelCase ) scheduler.set_timesteps(self.num_inference_steps ) lowerCAmelCase__ : Optional[int] = self.dummy_model() lowerCAmelCase__ : Optional[Any] = self.dummy_sample_deter * scheduler.init_noise_sigma lowerCAmelCase__ : Tuple = sample.to(__lowerCamelCase ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase__ : Optional[Any] = scheduler.scale_model_input(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Optional[Any] = model(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Optional[int] = scheduler.step(__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Union[str, Any] = output.prev_sample lowerCAmelCase__ : Any = torch.sum(torch.abs(__lowerCamelCase ) ) lowerCAmelCase__ : Union[str, Any] = torch.mean(torch.abs(__lowerCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77_1492_0043_9453 ) < 1e-2 assert abs(result_mean.item() - 0.1_6226_2890_1481_6284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1_6633_6059_5703 ) < 1e-2 assert abs(result_mean.item() - 0.1_6688_3260_0116_7297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8_4875_4882_8125 ) < 1e-2 assert abs(result_mean.item() - 0.1560_5306_6253_6621 ) < 1e-3 def lowerCAmelCase__ (self ) -> List[Any]: """simple docstring""" lowerCAmelCase__ : Any = self.scheduler_classes[0] lowerCAmelCase__ : Tuple = self.get_scheduler_config() lowerCAmelCase__ : str = scheduler_class(**__lowerCamelCase ) scheduler.set_timesteps(self.num_inference_steps ,device=__lowerCamelCase ) lowerCAmelCase__ : Optional[Any] = self.dummy_model() lowerCAmelCase__ : List[Any] = self.dummy_sample_deter.to(__lowerCamelCase ) * scheduler.init_noise_sigma for t in scheduler.timesteps: lowerCAmelCase__ : List[Any] = scheduler.scale_model_input(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Any = model(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : List[Any] = scheduler.step(__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : List[Any] = output.prev_sample lowerCAmelCase__ : List[str] = torch.sum(torch.abs(__lowerCamelCase ) ) lowerCAmelCase__ : Dict = torch.mean(torch.abs(__lowerCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46_9573_9746_0938 ) < 1e-2 assert abs(result_mean.item() - 0.2_1805_9346_0798_2635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_3536_3769_5312 ) < 1e-2 assert abs(result_mean.item() - 0.2_2342_9083_8241_5771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52_3834_2285_1562 ) < 1e-2 assert abs(result_mean.item() - 0.211_6195_7085_1326 ) < 1e-3 def lowerCAmelCase__ (self ) -> int: """simple docstring""" lowerCAmelCase__ : str = self.scheduler_classes[0] lowerCAmelCase__ : List[Any] = self.get_scheduler_config() lowerCAmelCase__ : Union[str, Any] = scheduler_class(**__lowerCamelCase ,use_karras_sigmas=__lowerCamelCase ) scheduler.set_timesteps(self.num_inference_steps ,device=__lowerCamelCase ) lowerCAmelCase__ : str = self.dummy_model() lowerCAmelCase__ : Union[str, Any] = self.dummy_sample_deter.to(__lowerCamelCase ) * scheduler.init_noise_sigma lowerCAmelCase__ : Union[str, Any] = sample.to(__lowerCamelCase ) for t in scheduler.timesteps: lowerCAmelCase__ : List[Any] = scheduler.scale_model_input(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : str = model(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Tuple = scheduler.step(__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : str = output.prev_sample lowerCAmelCase__ : Tuple = torch.sum(torch.abs(__lowerCamelCase ) ) lowerCAmelCase__ : List[Any] = torch.mean(torch.abs(__lowerCamelCase ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66_9741_3574_2188 ) < 1e-2 assert abs(result_mean.item() - 0.2_3003_8727_3098_1811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63_6535_6445_3125 ) < 1e-2 assert abs(result_mean.item() - 0.2_3003_8727_3098_1811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3_1352_2338_8672 ) < 1e-2 assert abs(result_mean.item() - 0.2_3003_8727_3098_1811 ) < 1e-2
129
0
import argparse import torch from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert from transformers.utils import logging logging.set_verbosity_info() def A__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__) -> Tuple: # Initialise PyTorch model __snake_case: Any = BertConfig.from_json_file(SCREAMING_SNAKE_CASE__) print(F'''Building PyTorch model from configuration: {config}''') __snake_case: List[str] = BertForPreTraining(SCREAMING_SNAKE_CASE__) # Load weights from tf checkpoint load_tf_weights_in_bert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''') torch.save(model.state_dict() , SCREAMING_SNAKE_CASE__) if __name__ == "__main__": __UpperCAmelCase : str = 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( "--bert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained BERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __UpperCAmelCase : Tuple = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
361
from dataclasses import dataclass from typing import Dict, Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .attention_processor import AttentionProcessor, AttnProcessor from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder @dataclass class __snake_case ( __lowerCamelCase ): '''simple docstring''' lowerCAmelCase__ = 42 class __snake_case ( __lowerCamelCase , __lowerCamelCase ): '''simple docstring''' lowerCAmelCase__ = True @register_to_config def __init__( self : Union[str, Any] , A : int = 3 , A : int = 3 , A : Tuple[str] = ("DownEncoderBlock2D",) , A : Tuple[str] = ("UpDecoderBlock2D",) , A : Tuple[int] = (64,) , A : int = 1 , A : str = "silu" , A : int = 4 , A : int = 32 , A : int = 32 , A : float = 0.1_8215 , ): super().__init__() # pass init params to Encoder __snake_case: Any = Encoder( in_channels=A , out_channels=A , down_block_types=A , block_out_channels=A , layers_per_block=A , act_fn=A , norm_num_groups=A , double_z=A , ) # pass init params to Decoder __snake_case: int = Decoder( in_channels=A , out_channels=A , up_block_types=A , block_out_channels=A , layers_per_block=A , norm_num_groups=A , act_fn=A , ) __snake_case: Dict = nn.Convad(2 * latent_channels , 2 * latent_channels , 1 ) __snake_case: int = nn.Convad(A , A , 1 ) __snake_case: List[str] = False __snake_case: Optional[int] = False # only relevant if vae tiling is enabled __snake_case: Any = self.config.sample_size __snake_case: int = ( self.config.sample_size[0] if isinstance(self.config.sample_size , (list, tuple) ) else self.config.sample_size ) __snake_case: Union[str, Any] = int(sample_size / (2 ** (len(self.config.block_out_channels ) - 1)) ) __snake_case: Optional[int] = 0.25 def UpperCAmelCase__ ( self : int , A : List[str] , A : Optional[Any]=False ): if isinstance(A , (Encoder, Decoder) ): __snake_case: str = value def UpperCAmelCase__ ( self : str , A : bool = True ): __snake_case: Union[str, Any] = use_tiling def UpperCAmelCase__ ( self : Optional[int] ): self.enable_tiling(A ) def UpperCAmelCase__ ( self : List[Any] ): __snake_case: List[str] = True def UpperCAmelCase__ ( self : List[str] ): __snake_case: List[str] = False @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def UpperCAmelCase__ ( self : Tuple ): __snake_case: Any = {} def fn_recursive_add_processors(A : str , A : torch.nn.Module , A : Dict[str, AttentionProcessor] ): if hasattr(A , """set_processor""" ): __snake_case: List[Any] = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(f'''{name}.{sub_name}''' , A , A ) return processors for name, module in self.named_children(): fn_recursive_add_processors(A , A , A ) return processors def UpperCAmelCase__ ( self : Optional[int] , A : Union[AttentionProcessor, Dict[str, AttentionProcessor]] ): __snake_case: Any = len(self.attn_processors.keys() ) if isinstance(A , A ) and len(A ) != count: raise ValueError( f'''A dict of processors was passed, but the number of processors {len(A )} does not match the''' f''' number of attention layers: {count}. Please make sure to pass {count} processor classes.''' ) def fn_recursive_attn_processor(A : str , A : torch.nn.Module , A : Optional[Any] ): if hasattr(A , """set_processor""" ): if not isinstance(A , A ): module.set_processor(A ) else: module.set_processor(processor.pop(f'''{name}.processor''' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(f'''{name}.{sub_name}''' , A , A ) for name, module in self.named_children(): fn_recursive_attn_processor(A , A , A ) def UpperCAmelCase__ ( self : List[str] ): self.set_attn_processor(AttnProcessor() ) @apply_forward_hook def UpperCAmelCase__ ( self : Optional[Any] , A : torch.FloatTensor , A : bool = True ): if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size): return self.tiled_encode(A , return_dict=A ) if self.use_slicing and x.shape[0] > 1: __snake_case: List[Any] = [self.encoder(A ) for x_slice in x.split(1 )] __snake_case: Optional[Any] = torch.cat(A ) else: __snake_case: str = self.encoder(A ) __snake_case: Any = self.quant_conv(A ) __snake_case: Tuple = DiagonalGaussianDistribution(A ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=A ) def UpperCAmelCase__ ( self : Tuple , A : torch.FloatTensor , A : bool = True ): if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size): return self.tiled_decode(A , return_dict=A ) __snake_case: Optional[int] = self.post_quant_conv(A ) __snake_case: Union[str, Any] = self.decoder(A ) if not return_dict: return (dec,) return DecoderOutput(sample=A ) @apply_forward_hook def UpperCAmelCase__ ( self : Tuple , A : torch.FloatTensor , A : bool = True ): if self.use_slicing and z.shape[0] > 1: __snake_case: Union[str, Any] = [self._decode(A ).sample for z_slice in z.split(1 )] __snake_case: List[str] = torch.cat(A ) else: __snake_case: int = self._decode(A ).sample if not return_dict: return (decoded,) return DecoderOutput(sample=A ) def UpperCAmelCase__ ( self : Any , A : Tuple , A : int , A : List[Any] ): __snake_case: int = min(a.shape[2] , b.shape[2] , A ) for y in range(A ): __snake_case: Dict = a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent) return b def UpperCAmelCase__ ( self : Union[str, Any] , A : Optional[Any] , A : List[str] , A : List[str] ): __snake_case: Dict = min(a.shape[3] , b.shape[3] , A ) for x in range(A ): __snake_case: Tuple = a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent) return b def UpperCAmelCase__ ( self : int , A : torch.FloatTensor , A : bool = True ): __snake_case: List[str] = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor) ) __snake_case: Dict = int(self.tile_latent_min_size * self.tile_overlap_factor ) __snake_case: Dict = self.tile_latent_min_size - blend_extent # Split the image into 512x512 tiles and encode them separately. __snake_case: Optional[int] = [] for i in range(0 , x.shape[2] , A ): __snake_case: Optional[int] = [] for j in range(0 , x.shape[3] , A ): __snake_case: int = x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size] __snake_case: Tuple = self.encoder(A ) __snake_case: Dict = self.quant_conv(A ) row.append(A ) rows.append(A ) __snake_case: Tuple = [] for i, row in enumerate(A ): __snake_case: str = [] for j, tile in enumerate(A ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: __snake_case: Optional[Any] = self.blend_v(rows[i - 1][j] , A , A ) if j > 0: __snake_case: Tuple = self.blend_h(row[j - 1] , A , A ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(A , dim=3 ) ) __snake_case: Tuple = torch.cat(A , dim=2 ) __snake_case: Optional[int] = DiagonalGaussianDistribution(A ) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=A ) def UpperCAmelCase__ ( self : Union[str, Any] , A : torch.FloatTensor , A : bool = True ): __snake_case: Optional[Any] = int(self.tile_latent_min_size * (1 - self.tile_overlap_factor) ) __snake_case: str = int(self.tile_sample_min_size * self.tile_overlap_factor ) __snake_case: int = self.tile_sample_min_size - blend_extent # Split z into overlapping 64x64 tiles and decode them separately. # The tiles have an overlap to avoid seams between tiles. __snake_case: List[Any] = [] for i in range(0 , z.shape[2] , A ): __snake_case: Optional[Any] = [] for j in range(0 , z.shape[3] , A ): __snake_case: Dict = z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size] __snake_case: Any = self.post_quant_conv(A ) __snake_case: Optional[Any] = self.decoder(A ) row.append(A ) rows.append(A ) __snake_case: Optional[Any] = [] for i, row in enumerate(A ): __snake_case: Optional[Any] = [] for j, tile in enumerate(A ): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: __snake_case: Tuple = self.blend_v(rows[i - 1][j] , A , A ) if j > 0: __snake_case: List[str] = self.blend_h(row[j - 1] , A , A ) result_row.append(tile[:, :, :row_limit, :row_limit] ) result_rows.append(torch.cat(A , dim=3 ) ) __snake_case: Dict = torch.cat(A , dim=2 ) if not return_dict: return (dec,) return DecoderOutput(sample=A ) def UpperCAmelCase__ ( self : List[Any] , A : torch.FloatTensor , A : bool = False , A : bool = True , A : Optional[torch.Generator] = None , ): __snake_case: Optional[Any] = sample __snake_case: Union[str, Any] = self.encode(A ).latent_dist if sample_posterior: __snake_case: Optional[Any] = posterior.sample(generator=A ) else: __snake_case: Dict = posterior.mode() __snake_case: Any = self.decode(A ).sample if not return_dict: return (dec,) return DecoderOutput(sample=A )
293
0
"""simple docstring""" from __future__ import annotations a__ : List[str] = tuple[int, int, int] a__ : Dict = tuple[str, str, str] # used alphabet -------------------------- # from string.ascii_uppercase a__ : str = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' # -------------------------- default selection -------------------------- # rotors -------------------------- a__ : str = '''EGZWVONAHDCLFQMSIPJBYUKXTR''' a__ : List[str] = '''FOBHMDKEXQNRAULPGSJVTYICZW''' a__ : Any = '''ZJXESIUQLHAVRMDOYGTNFWPBKC''' # reflector -------------------------- a__ : Optional[int] = { '''A''': '''N''', '''N''': '''A''', '''B''': '''O''', '''O''': '''B''', '''C''': '''P''', '''P''': '''C''', '''D''': '''Q''', '''Q''': '''D''', '''E''': '''R''', '''R''': '''E''', '''F''': '''S''', '''S''': '''F''', '''G''': '''T''', '''T''': '''G''', '''H''': '''U''', '''U''': '''H''', '''I''': '''V''', '''V''': '''I''', '''J''': '''W''', '''W''': '''J''', '''K''': '''X''', '''X''': '''K''', '''L''': '''Y''', '''Y''': '''L''', '''M''': '''Z''', '''Z''': '''M''', } # -------------------------- extra rotors -------------------------- a__ : Tuple = '''RMDJXFUWGISLHVTCQNKYPBEZOA''' a__ : str = '''SGLCPQWZHKXAREONTFBVIYJUDM''' a__ : Optional[Any] = '''HVSICLTYKQUBXDWAJZOMFGPREN''' a__ : List[Any] = '''RZWQHFMVDBKICJLNTUXAGYPSOE''' a__ : Optional[int] = '''LFKIJODBEGAMQPXVUHYSTCZRWN''' a__ : List[Any] = '''KOAEGVDHXPQZMLFTYWJNBRCIUS''' def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' if (unique_rotsel := len(set(lowerCAmelCase_ ) )) < 3: __SCREAMING_SNAKE_CASE = f"""Please use 3 unique rotors (not {unique_rotsel})""" raise Exception(lowerCAmelCase_ ) # Checks if rotor positions are valid __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rotpos if not 0 < rotorposa <= len(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = f"""First rotor position is not within range of 1..26 ({rotorposa}""" raise ValueError(lowerCAmelCase_ ) if not 0 < rotorposa <= len(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = f"""Second rotor position is not within range of 1..26 ({rotorposa})""" raise ValueError(lowerCAmelCase_ ) if not 0 < rotorposa <= len(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = f"""Third rotor position is not within range of 1..26 ({rotorposa})""" raise ValueError(lowerCAmelCase_ ) # Validates string and returns dict __SCREAMING_SNAKE_CASE = _plugboard(lowerCAmelCase_ ) return rotpos, rotsel, pbdict def UpperCAmelCase__ (lowerCAmelCase_ ): '''simple docstring''' if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = f"""Plugboard setting isn't type string ({type(lowerCAmelCase_ )})""" raise TypeError(lowerCAmelCase_ ) elif len(lowerCAmelCase_ ) % 2 != 0: __SCREAMING_SNAKE_CASE = f"""Odd number of symbols ({len(lowerCAmelCase_ )})""" raise Exception(lowerCAmelCase_ ) elif pbstring == "": return {} pbstring.replace(" " , "" ) # Checks if all characters are unique __SCREAMING_SNAKE_CASE = set() for i in pbstring: if i not in abc: __SCREAMING_SNAKE_CASE = f"""'{i}' not in list of symbols""" raise Exception(lowerCAmelCase_ ) elif i in tmppbl: __SCREAMING_SNAKE_CASE = f"""Duplicate symbol ({i})""" raise Exception(lowerCAmelCase_ ) else: tmppbl.add(lowerCAmelCase_ ) del tmppbl # Created the dictionary __SCREAMING_SNAKE_CASE = {} for j in range(0 , len(lowerCAmelCase_ ) - 1 , 2 ): __SCREAMING_SNAKE_CASE = pbstring[j + 1] __SCREAMING_SNAKE_CASE = pbstring[j] return pb def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = (rotora, rotora, rotora) , lowerCAmelCase_ = "" , ): '''simple docstring''' __SCREAMING_SNAKE_CASE = text.upper() __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = _validator( lowerCAmelCase_ , lowerCAmelCase_ , plugb.upper() ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rotor_position __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = rotor_selection rotorposa -= 1 rotorposa -= 1 rotorposa -= 1 __SCREAMING_SNAKE_CASE = [] # encryption/decryption process -------------------------- for symbol in text: if symbol in abc: # 1st plugboard -------------------------- if symbol in plugboard: __SCREAMING_SNAKE_CASE = plugboard[symbol] # rotor ra -------------------------- __SCREAMING_SNAKE_CASE = abc.index(lowerCAmelCase_ ) + rotorposa __SCREAMING_SNAKE_CASE = rotora[index % len(lowerCAmelCase_ )] # rotor rb -------------------------- __SCREAMING_SNAKE_CASE = abc.index(lowerCAmelCase_ ) + rotorposa __SCREAMING_SNAKE_CASE = rotora[index % len(lowerCAmelCase_ )] # rotor rc -------------------------- __SCREAMING_SNAKE_CASE = abc.index(lowerCAmelCase_ ) + rotorposa __SCREAMING_SNAKE_CASE = rotora[index % len(lowerCAmelCase_ )] # reflector -------------------------- # this is the reason you don't need another machine to decipher __SCREAMING_SNAKE_CASE = reflector[symbol] # 2nd rotors __SCREAMING_SNAKE_CASE = abc[rotora.index(lowerCAmelCase_ ) - rotorposa] __SCREAMING_SNAKE_CASE = abc[rotora.index(lowerCAmelCase_ ) - rotorposa] __SCREAMING_SNAKE_CASE = abc[rotora.index(lowerCAmelCase_ ) - rotorposa] # 2nd plugboard if symbol in plugboard: __SCREAMING_SNAKE_CASE = plugboard[symbol] # moves/resets rotor positions rotorposa += 1 if rotorposa >= len(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = 0 rotorposa += 1 if rotorposa >= len(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = 0 rotorposa += 1 if rotorposa >= len(lowerCAmelCase_ ): __SCREAMING_SNAKE_CASE = 0 # else: # pass # Error could be also raised # raise ValueError( # 'Invalid symbol('+repr(symbol)+')') result.append(lowerCAmelCase_ ) return "".join(lowerCAmelCase_ ) if __name__ == "__main__": a__ : Union[str, Any] = '''This is my Python script that emulates the Enigma machine from WWII.''' a__ : Any = (1, 1, 1) a__ : Optional[int] = '''pictures''' a__ : Union[str, Any] = (rotora, rotora, rotora) a__ : Tuple = enigma(message, rotor_pos, rotor_sel, pb) print('''Encrypted message:''', en) print('''Decrypted message:''', enigma(en, rotor_pos, rotor_sel, pb))
54
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax a : Union[str, Any] = logging.get_logger(__name__) @add_end_docstrings(__magic_name__ ) class UpperCamelCase_ ( __magic_name__ ): def __init__( self , **A ) -> List[str]: super().__init__(**A ) requires_backends(self , """vision""" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self , A , **A ) -> Optional[Any]: return super().__call__(A , **A ) def _lowercase( self , **A ) -> Optional[Any]: UpperCAmelCase : List[Any] = {} if "candidate_labels" in kwargs: UpperCAmelCase : Dict = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: UpperCAmelCase : Optional[Any] = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def _lowercase( self , A , A=None , A="This is a photo of {}." ) -> Optional[Any]: UpperCAmelCase : int = load_image(A ) UpperCAmelCase : List[str] = self.image_processor(images=[image] , return_tensors=self.framework ) UpperCAmelCase : List[str] = candidate_labels UpperCAmelCase : Tuple = [hypothesis_template.format(A ) for x in candidate_labels] UpperCAmelCase : Union[str, Any] = self.tokenizer(A , return_tensors=self.framework , padding=A ) UpperCAmelCase : Union[str, Any] = [text_inputs] return inputs def _lowercase( self , A ) -> Optional[int]: UpperCAmelCase : List[Any] = model_inputs.pop("""candidate_labels""" ) UpperCAmelCase : Optional[Any] = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] , A ): UpperCAmelCase : Optional[Any] = text_inputs[0] else: # Batching case. UpperCAmelCase : Any = text_inputs[0][0] UpperCAmelCase : Dict = self.model(**A , **A ) UpperCAmelCase : List[Any] = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_image, } return model_outputs def _lowercase( self , A ) -> Union[str, Any]: UpperCAmelCase : int = model_outputs.pop("""candidate_labels""" ) UpperCAmelCase : int = model_outputs["""logits"""][0] if self.framework == "pt": UpperCAmelCase : Optional[int] = logits.softmax(dim=-1 ).squeeze(-1 ) UpperCAmelCase : Any = probs.tolist() if not isinstance(A , A ): UpperCAmelCase : Any = [scores] elif self.framework == "tf": UpperCAmelCase : List[str] = stable_softmax(A , axis=-1 ) UpperCAmelCase : Union[str, Any] = probs.numpy().tolist() else: raise ValueError(f'''Unsupported framework: {self.framework}''' ) UpperCAmelCase : Any = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(A , A ) , key=lambda A : -x[0] ) ] return result
265
0
from __future__ import annotations def __lowerCAmelCase ( a__ , a__ = None ) -> list[list[str]]: __a = word_bank or [] # create a table __a = len(a__ ) + 1 __a = [] for _ in range(a__ ): table.append([] ) # seed value __a = [[]] # because empty string has empty combination # iterate through the indices for i in range(a__ ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(a__ )] == word: __a = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(a__ )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(a__ )]: combination.reverse() return table[len(a__ )] if __name__ == "__main__": print(all_construct('jwajalapa', ['jwa', 'j', 'w', 'a', 'la', 'lapa'])) print(all_construct('rajamati', ['s', 'raj', 'amat', 'raja', 'ma', 'i', 't'])) print( all_construct( 'hexagonosaurus', ['h', 'ex', 'hex', 'ag', 'ago', 'ru', 'auru', 'rus', 'go', 'no', 'o', 's'], ) )
33
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) A : Optional[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = ['NllbTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = ['NllbTokenizerFast'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys A : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
33
1
'''simple docstring''' def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' if divisor % 5 == 0 or divisor % 2 == 0: return 0 A : Optional[Any] = 1 A : Any = 1 while repunit: A : Tuple = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def lowerCAmelCase_ ( snake_case__ = 100_0000 ): '''simple docstring''' A : int = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(snake_case__ ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'''{solution() = }''')
3
'''simple docstring''' class A : def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" A : Any = None A : Optional[Any] = None A : Tuple = graph self._normalize_graph(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) A : Dict = len(SCREAMING_SNAKE_CASE ) A : Optional[Any] = None def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" if sources is int: A : Dict = [sources] if sinks is int: A : str = [sinks] if len(SCREAMING_SNAKE_CASE ) == 0 or len(SCREAMING_SNAKE_CASE ) == 0: return A : Optional[int] = sources[0] A : Union[str, Any] = sinks[0] # make fake vertex if there are more # than one source or sink if len(SCREAMING_SNAKE_CASE ) > 1 or len(SCREAMING_SNAKE_CASE ) > 1: A : Optional[int] = 0 for i in sources: max_input_flow += sum(self.graph[i] ) A : Dict = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: A : Dict = max_input_flow A : Tuple = 0 A : Tuple = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: A : Optional[Any] = max_input_flow A : Optional[Any] = size - 1 def __lowerCAmelCase ( self ) -> Any: """simple docstring""" if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> Union[str, Any]: """simple docstring""" A : List[Any] = algorithm(self ) class A : def __init__( self , SCREAMING_SNAKE_CASE ) -> Union[str, Any]: """simple docstring""" A : Union[str, Any] = flow_network A : Optional[Any] = flow_network.verticesCount A : Tuple = flow_network.sourceIndex A : Dict = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that A : str = flow_network.graph A : Optional[Any] = False def __lowerCAmelCase ( self ) -> Optional[int]: """simple docstring""" if not self.executed: self._algorithm() A : Optional[int] = True def __lowerCAmelCase ( self ) -> Any: """simple docstring""" pass class A ( __snake_case ): def __init__( self , SCREAMING_SNAKE_CASE ) -> Dict: """simple docstring""" super().__init__(SCREAMING_SNAKE_CASE ) # use this to save your result A : List[str] = -1 def __lowerCAmelCase ( self ) -> str: """simple docstring""" if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class A ( __snake_case ): def __init__( self , SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" super().__init__(SCREAMING_SNAKE_CASE ) A : Optional[Any] = [[0] * self.verticies_count for i in range(self.verticies_count )] A : Union[str, Any] = [0] * self.verticies_count A : List[Any] = [0] * self.verticies_count def __lowerCAmelCase ( self ) -> Dict: """simple docstring""" A : Tuple = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule A : Optional[Any] = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list A : Union[str, Any] = 0 while i < len(SCREAMING_SNAKE_CASE ): A : str = vertices_list[i] A : List[str] = self.heights[vertex_index] self.process_vertex(SCREAMING_SNAKE_CASE ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(SCREAMING_SNAKE_CASE ) ) A : int = 0 else: i += 1 A : Optional[Any] = sum(self.preflow[self.source_index] ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) self.relabel(SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> Union[str, Any]: """simple docstring""" A : Dict = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def __lowerCAmelCase ( self , SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" A : Dict = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): A : Dict = self.heights[to_index] if min_height is not None: A : Dict = min_height + 1 if __name__ == "__main__": lowercase : Optional[int] = [0] lowercase : List[Any] = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] lowercase : int = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network lowercase : List[str] = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate lowercase : List[str] = flow_network.find_maximum_flow() print(f'''maximum flow is {maximum_flow}''')
3
1
from math import factorial def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : int ) -> int: # If either of the conditions are true, the function is being asked # to calculate a factorial of a negative number, which is not possible if n < k or k < 0: raise ValueError("Please enter positive integers for n and k where n >= k" ) return factorial(_lowerCAmelCase ) // (factorial(_lowerCAmelCase ) * factorial(n - k )) if __name__ == "__main__": print( '''The number of five-card hands possible from a standard''', F'''fifty-two card deck is: {combinations(52, 5)}\n''', ) print( '''If a class of 40 students must be arranged into groups of''', F'''4 for group projects, there are {combinations(40, 4)} ways''', '''to arrange them.\n''', ) print( '''If 10 teams are competing in a Formula One race, there''', F'''are {combinations(10, 3)} ways that first, second and''', '''third place can be awarded.''', )
70
# 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 copy import importlib.metadata import json import os from dataclasses import dataclass from typing import Any, Dict, Union from packaging import version from ..utils import is_torch_available, logging if is_torch_available(): import torch _lowerCAmelCase : str = logging.get_logger(__name__) @dataclass class __magic_name__ : """simple docstring""" def __init__( self :Dict , snake_case :List[str]=False , snake_case :Optional[Any]=False , snake_case :Union[str, Any]=6.0 , snake_case :Union[str, Any]=None , snake_case :Union[str, Any]=False , snake_case :str=False , snake_case :Optional[Any]=None , snake_case :int="fp4" , snake_case :int=False , **snake_case :Optional[Any] , ): '''simple docstring''' A_ : int = load_in_abit A_ : Union[str, Any] = load_in_abit A_ : str = llm_inta_threshold A_ : str = llm_inta_skip_modules A_ : List[Any] = llm_inta_enable_fpaa_cpu_offload A_ : Optional[int] = llm_inta_has_fpaa_weight A_ : Optional[int] = bnb_abit_quant_type A_ : Dict = bnb_abit_use_double_quant if bnb_abit_compute_dtype is None: A_ : List[Any] = torch.floataa elif isinstance(snake_case , snake_case ): A_ : Any = getattr(snake_case , snake_case ) elif isinstance(snake_case , torch.dtype ): A_ : Union[str, Any] = bnb_abit_compute_dtype else: raise ValueError("bnb_4bit_compute_dtype must be a string or a torch.dtype" ) self.post_init() def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' if not isinstance(self.llm_inta_threshold , snake_case ): raise ValueError("llm_int8_threshold must be a float" ) if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules , snake_case ): raise ValueError("llm_int8_skip_modules must be a list of strings" ) if not isinstance(self.llm_inta_enable_fpaa_cpu_offload , snake_case ): raise ValueError("llm_int8_enable_fp32_cpu_offload must be a boolean" ) if not isinstance(self.llm_inta_has_fpaa_weight , snake_case ): raise ValueError("llm_int8_has_fp16_weight must be a boolean" ) if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype , torch.dtype ): raise ValueError("bnb_4bit_compute_dtype must be torch.dtype" ) if not isinstance(self.bnb_abit_quant_type , snake_case ): raise ValueError("bnb_4bit_quant_type must be a string" ) if not isinstance(self.bnb_abit_use_double_quant , snake_case ): raise ValueError("bnb_4bit_use_double_quant must be a boolean" ) if self.load_in_abit and not version.parse(importlib.metadata.version("bitsandbytes" ) ) >= version.parse( "0.39.0" ): raise ValueError( "4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version" ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return self.load_in_abit or self.load_in_abit def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' if self.load_in_abit: return "llm_int8" elif self.load_in_abit and self.bnb_abit_quant_type == "fp4": return "fp4" elif self.load_in_abit and self.bnb_abit_quant_type == "nf4": return "nf4" else: return None @classmethod def SCREAMING_SNAKE_CASE ( cls :List[str] , snake_case :Dict , snake_case :str , **snake_case :Dict ): '''simple docstring''' A_ : str = cls(**snake_case ) A_ : Any = [] for key, value in kwargs.items(): if hasattr(snake_case , snake_case ): setattr(snake_case , snake_case , snake_case ) to_remove.append(snake_case ) for key in to_remove: kwargs.pop(snake_case , snake_case ) if return_unused_kwargs: return config, kwargs else: return config def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :Union[str, os.PathLike] ): '''simple docstring''' with open(snake_case , "w" , encoding="utf-8" ) as writer: A_ : List[Any] = self.to_dict() A_ : int = json.dumps(snake_case , indent=2 , sort_keys=snake_case ) + "\n" writer.write(snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : List[str] = copy.deepcopy(self.__dict__ ) A_ : Optional[int] = str(output["bnb_4bit_compute_dtype"] ).split("." )[1] return output def __repr__( self :List[str] ): '''simple docstring''' return f"{self.__class__.__name__} {self.to_json_string()}" def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :bool = True ): '''simple docstring''' if use_diff is True: A_ : List[str] = self.to_diff_dict() else: A_ : int = self.to_dict() return json.dumps(snake_case , indent=2 , sort_keys=snake_case ) + "\n" def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : List[Any] = self.to_dict() # get the default config dict A_ : Optional[Any] = BitsAndBytesConfig().to_dict() A_ : List[Any] = {} # only serialize values that differ from the default config for key, value in config_dict.items(): if value != default_config_dict[key]: A_ : int = value return serializable_config_dict
70
1
"""simple docstring""" import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer __A : List[str] = logging.get_logger(__name__) __A : Optional[Any] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} __A : Optional[Any] = { '''vocab_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''', }, '''merges_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''', }, } __A : int = { '''allenai/led-base-16384''': 16_384, } class _UpperCAmelCase ( _A ): SCREAMING_SNAKE_CASE_ : Optional[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE_ : List[str] = LEDTokenizer SCREAMING_SNAKE_CASE_ : Optional[Any] = ["input_ids", "attention_mask"] def __init__( self : int , A : Any=None , A : Optional[Any]=None , A : List[Any]=None , A : Optional[int]="replace" , A : Tuple="<s>" , A : List[str]="</s>" , A : Optional[int]="</s>" , A : List[Any]="<s>" , A : Optional[Any]="<unk>" , A : Optional[int]="<pad>" , A : Optional[Any]="<mask>" , A : Any=False , A : Union[str, Any]=True , **A : Union[str, Any] , ) -> int: super().__init__( A , A , tokenizer_file=A , errors=A , bos_token=A , eos_token=A , sep_token=A , cls_token=A , unk_token=A , pad_token=A , mask_token=A , add_prefix_space=A , trim_offsets=A , **A , ) lowercase_ : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , A ) != add_prefix_space: lowercase_ : List[str] = getattr(A , pre_tok_state.pop('''type''' ) ) lowercase_ : int = add_prefix_space lowercase_ : Tuple = pre_tok_class(**A ) lowercase_ : int = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` lowercase_ : List[str] = '''post_processor''' lowercase_ : List[Any] = getattr(self.backend_tokenizer , A , A ) if tokenizer_component_instance: lowercase_ : Tuple = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: lowercase_ : Dict = tuple(state['''sep'''] ) if "cls" in state: lowercase_ : Union[str, Any] = tuple(state['''cls'''] ) lowercase_ : Dict = False if state.get('''add_prefix_space''' , A ) != add_prefix_space: lowercase_ : Union[str, Any] = add_prefix_space lowercase_ : Union[str, Any] = True if state.get('''trim_offsets''' , A ) != trim_offsets: lowercase_ : Union[str, Any] = trim_offsets lowercase_ : str = True if changes_to_apply: lowercase_ : int = getattr(A , state.pop('''type''' ) ) lowercase_ : Tuple = component_class(**A ) setattr(self.backend_tokenizer , A , A ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def A ( self : Optional[Any] ) -> str: if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def A ( self : Optional[int] , A : int ) -> str: lowercase_ : str = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else value lowercase_ : Optional[Any] = value def A ( self : Optional[Any] , *A : str , **A : Optional[Any] ) -> BatchEncoding: lowercase_ : int = kwargs.get('''is_split_into_words''' , A ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*A , **A ) def A ( self : List[Any] , *A : int , **A : List[str] ) -> BatchEncoding: lowercase_ : str = kwargs.get('''is_split_into_words''' , A ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*A , **A ) def A ( self : Any , A : str , A : Optional[str] = None ) -> Tuple[str]: lowercase_ : str = self._tokenizer.model.save(A , name=A ) return tuple(A ) def A ( self : Any , A : str , A : Union[str, Any]=None ) -> Any: lowercase_ : Any = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def A ( self : Union[str, Any] , A : List[int] , A : Optional[List[int]] = None ) -> List[int]: lowercase_ : Optional[Any] = [self.sep_token_id] lowercase_ : Optional[int] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def A ( self : Optional[Any] , A : Union[Dict[str, EncodedInput], BatchEncoding] , A : Optional[int] = None , A : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , A : Optional[int] = None , A : Optional[bool] = None , ) -> dict: lowercase_ : Any = super()._pad( encoded_inputs=A , max_length=A , padding_strategy=A , pad_to_multiple_of=A , return_attention_mask=A , ) # Load from model defaults if return_attention_mask is None: lowercase_ : Optional[Any] = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: lowercase_ : Optional[int] = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. lowercase_ : Tuple = len(encoded_inputs['''global_attention_mask'''] ) != len(A ) if needs_to_be_padded: lowercase_ : Union[str, Any] = len(A ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` lowercase_ : Dict = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": lowercase_ : str = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
33
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline __lowerCamelCase : str = logging.get_logger(__name__) # pylint: disable=invalid-name class A__ ( __snake_case ): def __init__( self , A_ , A_ ): '''simple docstring''' super().__init__() self.register_modules(unet=A_ , scheduler=A_ ) @torch.no_grad() def __call__( self , A_ = 1 , A_ = 100 , A_ = None , A_ = None , A_ = True , ): '''simple docstring''' if audio_length_in_s is None: UpperCamelCase : str = self.unet.config.sample_size / self.unet.config.sample_rate UpperCamelCase : Optional[Any] = audio_length_in_s * self.unet.config.sample_rate UpperCamelCase : Any = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F"""{audio_length_in_s} is too small. Make sure it's bigger or equal to""" F""" {3 * down_scale_factor / self.unet.config.sample_rate}.""" ) UpperCamelCase : Union[str, Any] = int(A_ ) if sample_size % down_scale_factor != 0: UpperCamelCase : List[str] = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F"""{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled""" F""" by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising""" " process." ) UpperCamelCase : Any = int(A_ ) UpperCamelCase : Union[str, Any] = next(iter(self.unet.parameters() ) ).dtype UpperCamelCase : Optional[int] = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(A_ , A_ ) and len(A_ ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(A_ )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) UpperCamelCase : Optional[Any] = randn_tensor(A_ , generator=A_ , device=self.device , dtype=A_ ) # set step values self.scheduler.set_timesteps(A_ , device=audio.device ) UpperCamelCase : Optional[int] = self.scheduler.timesteps.to(A_ ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output UpperCamelCase : Dict = self.unet(A_ , A_ ).sample # 2. compute previous image: x_t -> t_t-1 UpperCamelCase : int = self.scheduler.step(A_ , A_ , A_ ).prev_sample UpperCamelCase : Optional[Any] = audio.clamp(-1 , 1 ).float().cpu().numpy() UpperCamelCase : Dict = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=A_ )
52
0
"""simple docstring""" import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py __magic_name__ = "src/diffusers" __magic_name__ = "." # This is to make sure the diffusers module imported is the one in the repo. __magic_name__ = importlib.util.spec_from_file_location( "diffusers", os.path.join(DIFFUSERS_PATH, "__init__.py"), submodule_search_locations=[DIFFUSERS_PATH], ) __magic_name__ = spec.loader.load_module() def _lowerCAmelCase ( A__: int , A__: str ): '''simple docstring''' return line.startswith(A__ ) or len(A__ ) <= 1 or re.search(r'''^\s*\)(\s*->.*:|:)\s*$''' , A__ ) is not None def _lowerCAmelCase ( A__: List[Any] ): '''simple docstring''' UpperCAmelCase = object_name.split('''.''' ) UpperCAmelCase = 0 # First let's find the module where our object lives. UpperCAmelCase = parts[i] while i < len(A__ ) and not os.path.isfile(os.path.join(A__ , F"""{module}.py""" ) ): i += 1 if i < len(A__ ): UpperCAmelCase = os.path.join(A__ , parts[i] ) if i >= len(A__ ): raise ValueError(F"""`object_name` should begin with the name of a module of diffusers but got {object_name}.""" ) with open(os.path.join(A__ , F"""{module}.py""" ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCAmelCase = f.readlines() # Now let's find the class / func in the code! UpperCAmelCase = '''''' UpperCAmelCase = 0 for name in parts[i + 1 :]: while ( line_index < len(A__ ) and re.search(rF"""^{indent}(class|def)\s+{name}(\(|\:)""" , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(A__ ): raise ValueError(F""" {object_name} does not match any function or class in {module}.""" ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). UpperCAmelCase = line_index while line_index < len(A__ ) and _should_continue(lines[line_index] , A__ ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 UpperCAmelCase = lines[start_index:line_index] return "".join(A__ ) __magic_name__ = re.compile(r"^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)") __magic_name__ = re.compile(r"^\s*(\S+)->(\S+)(\s+.*|$)") __magic_name__ = re.compile(r"<FILL\s+[^>]*>") def _lowerCAmelCase ( A__: Union[str, Any] ): '''simple docstring''' UpperCAmelCase = code.split('''\n''' ) UpperCAmelCase = 0 while idx < len(A__ ) and len(lines[idx] ) == 0: idx += 1 if idx < len(A__ ): return re.search(r'''^(\s*)\S''' , lines[idx] ).groups()[0] return "" def _lowerCAmelCase ( A__: Optional[int] ): '''simple docstring''' UpperCAmelCase = len(get_indent(A__ ) ) > 0 if has_indent: UpperCAmelCase = F"""class Bla:\n{code}""" UpperCAmelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=A__ ) UpperCAmelCase = black.format_str(A__ , mode=A__ ) UpperCAmelCase , UpperCAmelCase = style_docstrings_in_code(A__ ) return result[len('''class Bla:\n''' ) :] if has_indent else result def _lowerCAmelCase ( A__: List[Any] , A__: Dict=False ): '''simple docstring''' with open(A__ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCAmelCase = f.readlines() UpperCAmelCase = [] UpperCAmelCase = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(A__ ): UpperCAmelCase = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = search.groups() UpperCAmelCase = find_code_in_diffusers(A__ ) UpperCAmelCase = get_indent(A__ ) UpperCAmelCase = line_index + 1 if indent == theoretical_indent else line_index + 2 UpperCAmelCase = theoretical_indent UpperCAmelCase = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. UpperCAmelCase = True while line_index < len(A__ ) and should_continue: line_index += 1 if line_index >= len(A__ ): break UpperCAmelCase = lines[line_index] UpperCAmelCase = _should_continue(A__ , A__ ) and re.search(F"""^{indent}# End copy""" , A__ ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 UpperCAmelCase = lines[start_index:line_index] UpperCAmelCase = ''''''.join(A__ ) # Remove any nested `Copied from` comments to avoid circular copies UpperCAmelCase = [line for line in theoretical_code.split('''\n''' ) if _re_copy_warning.search(A__ ) is None] UpperCAmelCase = '''\n'''.join(A__ ) # Before comparing, use the `replace_pattern` on the original code. if len(A__ ) > 0: UpperCAmelCase = replace_pattern.replace('''with''' , '''''' ).split(''',''' ) UpperCAmelCase = [_re_replace_pattern.search(A__ ) for p in patterns] for pattern in patterns: if pattern is None: continue UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = pattern.groups() UpperCAmelCase = re.sub(A__ , A__ , A__ ) if option.strip() == "all-casing": UpperCAmelCase = re.sub(obja.lower() , obja.lower() , A__ ) UpperCAmelCase = re.sub(obja.upper() , obja.upper() , A__ ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line UpperCAmelCase = blackify(lines[start_index - 1] + theoretical_code ) UpperCAmelCase = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: UpperCAmelCase = lines[:start_index] + [theoretical_code] + lines[line_index:] UpperCAmelCase = start_index + 1 if overwrite and len(A__ ) > 0: # Warn the user a file has been modified. print(F"""Detected changes, rewriting {filename}.""" ) with open(A__ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(A__ ) return diffs def _lowerCAmelCase ( A__: bool = False ): '''simple docstring''' UpperCAmelCase = glob.glob(os.path.join(A__ , '''**/*.py''' ) , recursive=A__ ) UpperCAmelCase = [] for filename in all_files: UpperCAmelCase = is_copy_consistent(A__ , A__ ) diffs += [F"""- {filename}: copy does not match {d[0]} at line {d[1]}""" for d in new_diffs] if not overwrite and len(A__ ) > 0: UpperCAmelCase = '''\n'''.join(A__ ) raise Exception( '''Found the following copy inconsistencies:\n''' + diff + '''\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.''' ) if __name__ == "__main__": __magic_name__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") __magic_name__ = parser.parse_args() check_copies(args.fix_and_overwrite)
362
import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging __magic_name__ = logging.get_logger(__name__) def _lowerCAmelCase ( A__: nn.ModuleList , A__: nn.ModuleList , A__: List[int] ): '''simple docstring''' UpperCAmelCase = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(A__ ) == len(A__ ), F"""{len(A__ )} != {len(A__ )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) __magic_name__ = { # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 12: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 11], 4: [0, 4, 8, 11], 6: [0, 2, 4, 7, 9, 11], 9: [0, 1, 2, 4, 5, 7, 9, 10, 11], 12: list(range(12)), }, 16: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 15], 3: [0, 8, 15], 4: [0, 5, 10, 15], 6: [0, 3, 6, 9, 12, 15], 8: [0, 2, 4, 6, 8, 10, 12, 15], 9: [0, 1, 3, 5, 7, 9, 11, 13, 15], 12: [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15], 16: list(range(16)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } __magic_name__ = { # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 12: {1: [11], 2: [5, 11], 3: [3, 7, 11], 6: [1, 3, 5, 8, 10, 11]}, 16: {1: [15], 4: [4, 9, 12, 15], 8: [1, 3, 5, 7, 9, 11, 13, 15]}, } def _lowerCAmelCase ( A__: List[str] , A__: Optional[int] ): '''simple docstring''' try: UpperCAmelCase = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( F"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" F""" {n_student}""" ) return list(range(A__ ) ) def _lowerCAmelCase ( A__: Optional[int] , A__: Tuple ): '''simple docstring''' if n_student > n_teacher: raise ValueError(F"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(A__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def _lowerCAmelCase ( A__: Union[str, PreTrainedModel] , A__: Union[str, Path] = "student" , A__: Union[int, None] = None , A__: Union[int, None] = None , A__: Optional[int]=False , A__: Tuple=None , A__: Any=None , **A__: List[str] , ): '''simple docstring''' UpperCAmelCase = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(A__ , A__ ): AutoTokenizer.from_pretrained(A__ ).save_pretrained(A__ ) # purely for convenience UpperCAmelCase = AutoModelForSeqaSeqLM.from_pretrained(A__ ).eval() else: assert isinstance(A__ , A__ ), F"""teacher must be a model or string got type {type(A__ )}""" UpperCAmelCase = teacher.config.to_diff_dict() try: UpperCAmelCase , UpperCAmelCase = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: UpperCAmelCase = teacher_e if d is None: UpperCAmelCase = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): UpperCAmelCase , UpperCAmelCase = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: UpperCAmelCase , UpperCAmelCase = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: UpperCAmelCase = teacher_e if d is None: UpperCAmelCase = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(A__ ) # Copy weights UpperCAmelCase = teacher.config_class(**A__ ) UpperCAmelCase = AutoModelForSeqaSeqLM.from_config(A__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. UpperCAmelCase = student.load_state_dict(teacher.state_dict() , strict=A__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save UpperCAmelCase , UpperCAmelCase = list(range(A__ ) ), list(range(A__ ) ) logger.info( F"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" F""" {save_path}""" ) student.save_pretrained(A__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: UpperCAmelCase = pick_layers_to_copy(A__ , A__ ) if d_layers_to_copy is None: UpperCAmelCase = pick_layers_to_copy(A__ , A__ ) try: if hasattr( A__ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , A__ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , A__ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , A__ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , A__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , A__ ) copy_layers(teacher.decoder.block , student.decoder.block , A__ ) logger.info( F"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) UpperCAmelCase = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(A__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
152
0
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class _lowerCamelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , _A : Optional[Any] , _A : Optional[Any]=13 , _A : int=7 , _A : List[Any]=True , _A : Tuple=True , _A : Dict=True , _A : List[Any]=True , _A : str=99 , _A : Union[str, Any]=32 , _A : Union[str, Any]=5 , _A : Dict=4 , _A : Optional[int]=37 , _A : Optional[int]="gelu" , _A : List[Any]=0.1 , _A : str=0.1 , _A : str=512 , _A : Any=16 , _A : Dict=2 , _A : Union[str, Any]=0.02 , _A : Dict=4 , ) -> Tuple: __magic_name__ : List[Any] = parent __magic_name__ : Union[str, Any] = batch_size __magic_name__ : List[str] = seq_length __magic_name__ : int = is_training __magic_name__ : Dict = use_attention_mask __magic_name__ : Dict = use_token_type_ids __magic_name__ : Union[str, Any] = use_labels __magic_name__ : Dict = vocab_size __magic_name__ : List[Any] = hidden_size __magic_name__ : Optional[int] = num_hidden_layers __magic_name__ : List[str] = num_attention_heads __magic_name__ : List[str] = intermediate_size __magic_name__ : str = hidden_act __magic_name__ : Any = hidden_dropout_prob __magic_name__ : str = attention_probs_dropout_prob __magic_name__ : Tuple = max_position_embeddings __magic_name__ : Any = type_vocab_size __magic_name__ : int = type_sequence_label_size __magic_name__ : List[Any] = initializer_range __magic_name__ : Optional[Any] = num_choices def __lowerCAmelCase ( self : int ) -> List[str]: __magic_name__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __magic_name__ : Optional[int] = None if self.use_attention_mask: __magic_name__ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) __magic_name__ : Optional[int] = None if self.use_token_type_ids: __magic_name__ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __magic_name__ : str = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __lowerCAmelCase ( self : Dict ) -> List[Any]: __magic_name__ : Optional[int] = self.prepare_config_and_inputs() __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ : List[str] = config_and_inputs __magic_name__ : str = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_flax class _lowerCamelCase ( lowercase__ , unittest.TestCase ): '''simple docstring''' A_ : str = True A_ : int = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def __lowerCAmelCase ( self : Optional[int] ) -> str: __magic_name__ : Dict = FlaxRoFormerModelTester(self ) @slow def __lowerCAmelCase ( self : Union[str, Any] ) -> Optional[Any]: for model_class_name in self.all_model_classes: __magic_name__ : Optional[Any] = model_class_name.from_pretrained('junnyu/roformer_chinese_small' , from_pt=_A ) __magic_name__ : int = model(np.ones((1, 1) ) ) self.assertIsNotNone(_A ) @require_flax class _lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self : Dict ) -> List[Any]: __magic_name__ : List[Any] = FlaxRoFormerForMaskedLM.from_pretrained('junnyu/roformer_chinese_base' ) __magic_name__ : Optional[Any] = jnp.array([[0, 1, 2, 3, 4, 5]] ) __magic_name__ : Union[str, Any] = model(_A )[0] __magic_name__ : Tuple = 50000 __magic_name__ : str = (1, 6, vocab_size) self.assertEqual(output.shape , _A ) __magic_name__ : Dict = jnp.array( [[[-0.1205, -1.0265, 0.2922], [-1.5134, 0.1974, 0.1519], [-5.0135, -3.9003, -0.8404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _A , atol=1E-4 ) )
331
'''simple docstring''' import re import tempfile from pathlib import Path import pytest import yaml from datasets.utils.readme import ReadMe # @pytest.fixture # def example_yaml_structure(): lowerCAmelCase :List[str] = yaml.safe_load( '''\ name: "" allow_empty: false allow_empty_text: true subsections: - name: "Dataset Card for X" # First-level markdown heading allow_empty: false allow_empty_text: true subsections: - name: "Table of Contents" allow_empty: false allow_empty_text: false subsections: null - name: "Dataset Description" allow_empty: false allow_empty_text: false subsections: - name: "Dataset Summary" allow_empty: false allow_empty_text: false subsections: null - name: "Supported Tasks and Leaderboards" allow_empty: true allow_empty_text: true subsections: null - name: Languages allow_empty: false allow_empty_text: true subsections: null ''' ) lowerCAmelCase :List[Any] = { '''name''': '''root''', '''text''': '''''', '''is_empty_text''': True, '''subsections''': [ { '''name''': '''Dataset Card for My Dataset''', '''text''': '''''', '''is_empty_text''': True, '''subsections''': [ {'''name''': '''Table of Contents''', '''text''': '''Some text here.''', '''is_empty_text''': False, '''subsections''': []}, { '''name''': '''Dataset Description''', '''text''': '''Some text here.''', '''is_empty_text''': False, '''subsections''': [ { '''name''': '''Dataset Summary''', '''text''': '''Some text here.''', '''is_empty_text''': False, '''subsections''': [], }, { '''name''': '''Supported Tasks and Leaderboards''', '''text''': '''''', '''is_empty_text''': True, '''subsections''': [], }, {'''name''': '''Languages''', '''text''': '''Language Text''', '''is_empty_text''': False, '''subsections''': []}, ], }, ], } ], } lowerCAmelCase :Union[str, Any] = '''\ --- language: - zh - en --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :List[Any] = '''\ --- language: - zh - en --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. #### Extra Ignored Subsection ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :Tuple = { '''name''': '''root''', '''text''': '''''', '''is_empty_text''': True, '''subsections''': [ { '''name''': '''Dataset Card for My Dataset''', '''text''': '''''', '''is_empty_text''': True, '''subsections''': [ {'''name''': '''Table of Contents''', '''text''': '''Some text here.''', '''is_empty_text''': False, '''subsections''': []}, { '''name''': '''Dataset Description''', '''text''': '''Some text here.''', '''is_empty_text''': False, '''subsections''': [ { '''name''': '''Dataset Summary''', '''text''': '''Some text here.''', '''is_empty_text''': False, '''subsections''': [ { '''name''': '''Extra Ignored Subsection''', '''text''': '''''', '''is_empty_text''': True, '''subsections''': [], } ], }, { '''name''': '''Supported Tasks and Leaderboards''', '''text''': '''''', '''is_empty_text''': True, '''subsections''': [], }, {'''name''': '''Languages''', '''text''': '''Language Text''', '''is_empty_text''': False, '''subsections''': []}, ], }, ], } ], } lowerCAmelCase :Optional[Any] = '''\ --- --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :Optional[int] = ( '''The following issues were found for the README at `{path}`:\n-\tEmpty YAML markers are present in the README.''' ) lowerCAmelCase :Tuple = '''\ # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :Union[str, Any] = ( '''The following issues were found for the README at `{path}`:\n-\tNo YAML markers are present in the README.''' ) lowerCAmelCase :Dict = '''\ --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :Dict = '''The following issues were found for the README at `{path}`:\n-\tOnly the start of YAML tags present in the README.''' lowerCAmelCase :Optional[Any] = '''\ --- language: - zh - en --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :int = '''The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Summary` but it is empty.\n-\tExpected some text in section `Dataset Summary` but it is empty (text in subsections are ignored).''' lowerCAmelCase :int = '''\ --- language: - zh - en --- # Dataset Card for My Dataset ''' lowerCAmelCase :List[str] = '''The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Card for My Dataset` but it is empty.\n-\tSection `Dataset Card for My Dataset` expected the following subsections: `Table of Contents`, `Dataset Description`. Found \'None\'.''' lowerCAmelCase :List[Any] = '''\ --- language: - zh - en --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Languages Language Text ''' lowerCAmelCase :List[str] = '''The following issues were found for the README at `{path}`:\n-\tSection `Dataset Description` is missing subsection: `Supported Tasks and Leaderboards`.''' lowerCAmelCase :int = '''\ --- language: - zh - en --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages ''' lowerCAmelCase :Dict = '''The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Languages` but it is empty.''' lowerCAmelCase :Tuple = '''\ --- language: - zh - en --- ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :Optional[Any] = '''The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README.''' lowerCAmelCase :Any = '''\ --- language: - zh - en --- # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text # Dataset Card My Dataset ''' lowerCAmelCase :Dict = '''The following issues were found for the README at `{path}`:\n-\tThe README has several first-level headings: `Dataset Card for My Dataset`, `Dataset Card My Dataset`. Only one heading is expected. Skipping further validation for this README.''' lowerCAmelCase :Tuple = '''\ --- language: - zh - en --- # Dataset Card My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :str = '''The following issues were found for the README at `{path}`:\n-\tNo first-level heading starting with `Dataset Card for` found in README. Skipping further validation for this README.''' lowerCAmelCase :Any = '''''' lowerCAmelCase :Any = '''The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README.\n-\tNo YAML markers are present in the README.''' lowerCAmelCase :List[Any] = '''\ --- language: - zh - en --- # Dataset Card for My Dataset # Dataset Card for My Dataset ## Table of Contents Some text here. ## Dataset Description Some text here. ### Dataset Summary Some text here. ### Supported Tasks and Leaderboards ### Languages Language Text ''' lowerCAmelCase :str = '''The following issues were found while parsing the README at `{path}`:\n-\tMultiple sections with the same heading `Dataset Card for My Dataset` have been found. Please keep only one of these sections.''' @pytest.mark.parametrize( 'readme_md, expected_dict' , [ (README_CORRECT, CORRECT_DICT), (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL), ] , ) def lowerCamelCase ( lowerCAmelCase : str , lowerCAmelCase : Optional[int] ): """simple docstring""" assert ReadMe.from_string(lowerCAmelCase , lowerCAmelCase ).to_dict() == expected_dict @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML), (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML), (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML), (README_EMPTY, EXPECTED_ERROR_README_EMPTY), (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION), (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL), (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION), (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT), (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL), (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL), (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT), ] , ) def lowerCamelCase ( lowerCAmelCase : Optional[int] , lowerCAmelCase : Dict ): """simple docstring""" with pytest.raises(lowerCAmelCase , match=re.escape(expected_error.format(path='root' ) ) ): __magic_name__ : str = ReadMe.from_string(lowerCAmelCase , lowerCAmelCase ) readme.validate() @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1), ] , ) def lowerCamelCase ( lowerCAmelCase : List[str] , lowerCAmelCase : Optional[int] ): """simple docstring""" with pytest.raises(lowerCAmelCase , match=re.escape(expected_error.format(path='root' ) ) ): ReadMe.from_string(lowerCAmelCase , lowerCAmelCase ) @pytest.mark.parametrize( 'readme_md,' , [ (README_MULTIPLE_SAME_HEADING_1), ] , ) def lowerCamelCase ( lowerCAmelCase : Tuple ): """simple docstring""" ReadMe.from_string(lowerCAmelCase , lowerCAmelCase , suppress_parsing_errors=lowerCAmelCase ) @pytest.mark.parametrize( 'readme_md, expected_dict' , [ (README_CORRECT, CORRECT_DICT), (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL), ] , ) def lowerCamelCase ( lowerCAmelCase : Optional[Any] , lowerCAmelCase : List[Any] ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ : Optional[Any] = Path(lowerCAmelCase ) / 'README.md' with open(lowerCAmelCase , 'w+' ) as readme_file: readme_file.write(lowerCAmelCase ) __magic_name__ : Optional[int] = ReadMe.from_readme(lowerCAmelCase , lowerCAmelCase ).to_dict() assert out["name"] == path assert out["text"] == "" assert out["is_empty_text"] assert out["subsections"] == expected_dict["subsections"] @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML), (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML), (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML), (README_EMPTY, EXPECTED_ERROR_README_EMPTY), (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION), (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL), (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION), (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT), (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL), (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL), (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT), ] , ) def lowerCamelCase ( lowerCAmelCase : Tuple , lowerCAmelCase : List[Any] ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ : Union[str, Any] = Path(lowerCAmelCase ) / 'README.md' with open(lowerCAmelCase , 'w+' ) as readme_file: readme_file.write(lowerCAmelCase ) __magic_name__ : str = expected_error.format(path=lowerCAmelCase ) with pytest.raises(lowerCAmelCase , match=re.escape(lowerCAmelCase ) ): __magic_name__ : int = ReadMe.from_readme(lowerCAmelCase , lowerCAmelCase ) readme.validate() @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1), ] , ) def lowerCamelCase ( lowerCAmelCase : int , lowerCAmelCase : str ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ : Optional[int] = Path(lowerCAmelCase ) / 'README.md' with open(lowerCAmelCase , 'w+' ) as readme_file: readme_file.write(lowerCAmelCase ) __magic_name__ : Any = expected_error.format(path=lowerCAmelCase ) with pytest.raises(lowerCAmelCase , match=re.escape(lowerCAmelCase ) ): ReadMe.from_readme(lowerCAmelCase , lowerCAmelCase ) @pytest.mark.parametrize( 'readme_md,' , [ (README_MULTIPLE_SAME_HEADING_1), ] , ) def lowerCamelCase ( lowerCAmelCase : Union[str, Any] ): """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __magic_name__ : Any = Path(lowerCAmelCase ) / 'README.md' with open(lowerCAmelCase , 'w+' ) as readme_file: readme_file.write(lowerCAmelCase ) ReadMe.from_readme(lowerCAmelCase , lowerCAmelCase , suppress_parsing_errors=lowerCAmelCase )
331
1
import unittest import torch from torch import nn from diffusers.models.activations import get_activation class _lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self : Optional[Any] ): '''simple docstring''' _snake_case : Union[str, Any] = get_activation('swish' ) self.assertIsInstance(UpperCamelCase__ , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' _snake_case : Dict = get_activation('silu' ) self.assertIsInstance(UpperCamelCase__ , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase_ ( self : List[Any] ): '''simple docstring''' _snake_case : Optional[Any] = get_activation('mish' ) self.assertIsInstance(UpperCamelCase__ , nn.Mish ) self.assertEqual(act(torch.tensor(-2_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def UpperCamelCase_ ( self : Optional[Any] ): '''simple docstring''' _snake_case : Tuple = get_activation('gelu' ) self.assertIsInstance(UpperCamelCase__ , nn.GELU ) self.assertEqual(act(torch.tensor(-1_00 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
370
def lowerCamelCase_ ( lowerCAmelCase: int , lowerCAmelCase: int )-> int: while a != 0: _snake_case , _snake_case : Optional[Any] = b % a, a return b def lowerCamelCase_ ( lowerCAmelCase: int , lowerCAmelCase: int )-> int: if gcd(lowerCAmelCase , lowerCAmelCase ) != 1: _snake_case : Any = F"""mod inverse of {a!r} and {m!r} does not exist""" raise ValueError(lowerCAmelCase ) _snake_case , _snake_case , _snake_case : Optional[Any] = 1, 0, a _snake_case , _snake_case , _snake_case : Optional[int] = 0, 1, m while va != 0: _snake_case : Dict = ua // va _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case : List[Any] = (ua - q * va), (ua - q * va), (ua - q * va), va, va, va return ua % m
260
0
import unittest from transformers import SqueezeBertConfig, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, ) class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[int] ,A : Optional[Any] ,A : Optional[int]=13 ,A : List[str]=7 ,A : List[str]=True ,A : str=True ,A : int=False ,A : List[Any]=True ,A : Tuple=99 ,A : Optional[int]=32 ,A : Optional[Any]=5 ,A : List[Any]=4 ,A : Optional[Any]=64 ,A : List[Any]="gelu" ,A : List[Any]=0.1 ,A : Union[str, Any]=0.1 ,A : Union[str, Any]=5_12 ,A : Union[str, Any]=16 ,A : Optional[Any]=2 ,A : str=0.02 ,A : int=3 ,A : List[str]=4 ,A : List[str]=None ,A : Union[str, Any]=2 ,A : str=2 ,A : Dict=2 ,A : Optional[Any]=2 ,A : Any=4 ,A : Optional[Any]=1 ,): __A = parent __A = batch_size __A = seq_length __A = is_training __A = use_input_mask __A = use_token_type_ids __A = use_labels __A = vocab_size __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = intermediate_size __A = hidden_act __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_vocab_size __A = type_sequence_label_size __A = initializer_range __A = num_labels __A = num_choices __A = scope __A = q_groups __A = k_groups __A = v_groups __A = post_attention_groups __A = intermediate_groups __A = output_groups def UpperCamelCase_ ( self : List[str] ): __A = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) __A = None if self.use_input_mask: __A = random_attention_mask([self.batch_size, self.seq_length] ) __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] ,self.num_choices ) __A = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase_ ( self : str ): return SqueezeBertConfig( embedding_size=self.hidden_size ,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 ,attention_probs_dropout_prob=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,q_groups=self.q_groups ,k_groups=self.k_groups ,v_groups=self.v_groups ,post_attention_groups=self.post_attention_groups ,intermediate_groups=self.intermediate_groups ,output_groups=self.output_groups ,) def UpperCamelCase_ ( self : int ,A : List[str] ,A : Any ,A : int ,A : str ,A : int ,A : str ): __A = SqueezeBertModel(config=A ) model.to(A ) model.eval() __A = model(A ,A ) __A = model(A ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase_ ( self : str ,A : Any ,A : Any ,A : Optional[int] ,A : int ,A : int ,A : Union[str, Any] ): __A = SqueezeBertForMaskedLM(config=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.vocab_size) ) def UpperCamelCase_ ( self : List[str] ,A : Optional[int] ,A : List[Any] ,A : Union[str, Any] ,A : Optional[Any] ,A : Dict ,A : int ): __A = SqueezeBertForQuestionAnswering(config=A ) model.to(A ) model.eval() __A = model( A ,attention_mask=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 UpperCamelCase_ ( self : Optional[int] ,A : Optional[Any] ,A : List[Any] ,A : Tuple ,A : Dict ,A : Optional[int] ,A : Dict ): __A = self.num_labels __A = SqueezeBertForSequenceClassification(A ) model.to(A ) model.eval() __A = model(A ,attention_mask=A ,labels=A ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self : Dict ,A : Tuple ,A : int ,A : Any ,A : Union[str, Any] ,A : Union[str, Any] ,A : Any ): __A = self.num_labels __A = SqueezeBertForTokenClassification(config=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 UpperCamelCase_ ( self : Tuple ,A : Optional[Any] ,A : int ,A : Optional[Any] ,A : Tuple ,A : List[Any] ,A : Tuple ): __A = self.num_choices __A = SqueezeBertForMultipleChoice(config=A ) model.to(A ) model.eval() __A = input_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 ,labels=A ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def UpperCamelCase_ ( self : Any ): __A = self.prepare_config_and_inputs() ((__A) , (__A) , (__A) , (__A) , (__A) , (__A)) = config_and_inputs __A = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' snake_case_ = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) snake_case_ = ( { "feature-extraction": SqueezeBertModel, "fill-mask": SqueezeBertForMaskedLM, "question-answering": SqueezeBertForQuestionAnswering, "text-classification": SqueezeBertForSequenceClassification, "token-classification": SqueezeBertForTokenClassification, "zero-shot": SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) snake_case_ = False snake_case_ = True snake_case_ = False def UpperCamelCase_ ( self : str ): __A = SqueezeBertModelTester(self ) __A = ConfigTester(self ,config_class=A ,dim=37 ) def UpperCamelCase_ ( self : Union[str, Any] ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : Dict ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*A ) def UpperCamelCase_ ( self : Optional[Any] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*A ) def UpperCamelCase_ ( self : int ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*A ) def UpperCamelCase_ ( self : Dict ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*A ) def UpperCamelCase_ ( self : Optional[int] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*A ) def UpperCamelCase_ ( self : List[str] ): __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*A ) @slow def UpperCamelCase_ ( self : List[str] ): for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __A = SqueezeBertModel.from_pretrained(A ) self.assertIsNotNone(A ) @require_sentencepiece @require_tokenizers @require_torch class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def UpperCamelCase_ ( self : int ): __A = SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli" ) __A = torch.tensor([[1, 2_94_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 13, 15_88, 2]] ) __A = model(A )[0] __A = torch.Size((1, 3) ) self.assertEqual(output.shape ,A ) __A = torch.tensor([[0.64_01, -0.03_49, -0.60_41]] ) self.assertTrue(torch.allclose(A ,A ,atol=1E-4 ) )
15
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( _lowerCamelCase , unittest.TestCase): A_ : Union[str, Any] = DiTPipeline A_ : Union[str, Any] = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS A_ : List[Any] = PipelineTesterMixin.required_optional_params - { 'latents', 'num_images_per_prompt', 'callback', 'callback_steps', } A_ : Optional[Any] = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS A_ : Tuple = False def __lowerCamelCase ( self ): torch.manual_seed(0 ) __lowerCAmelCase : List[str] = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=_SCREAMING_SNAKE_CASE , activation_fn='gelu-approximate' , num_embeds_ada_norm=10_00 , norm_type='ada_norm_zero' , norm_elementwise_affine=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : str = AutoencoderKL() __lowerCAmelCase : Union[str, Any] = DDIMScheduler() __lowerCAmelCase : Dict = {'transformer': transformer.eval(), 'vae': vae.eval(), 'scheduler': scheduler} return components def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=0 ): if str(_SCREAMING_SNAKE_CASE ).startswith('mps' ): __lowerCAmelCase : List[str] = torch.manual_seed(_SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase : List[str] = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = { 'class_labels': [1], 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowerCamelCase ( self ): __lowerCAmelCase : List[str] = 'cpu' __lowerCAmelCase : Any = self.get_dummy_components() __lowerCAmelCase : Union[str, Any] = self.pipeline_class(**_SCREAMING_SNAKE_CASE ) pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Tuple = self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = pipe(**_SCREAMING_SNAKE_CASE ).images __lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) __lowerCAmelCase : Optional[int] = np.array([0.2946, 0.6601, 0.4329, 0.3296, 0.4144, 0.5319, 0.7273, 0.5013, 0.4457] ) __lowerCAmelCase : List[Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 ) def __lowerCamelCase ( self ): self._test_inference_batch_single_identical(relax_max_difference=_SCREAMING_SNAKE_CASE , expected_max_diff=1E-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 ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @require_torch_gpu @slow class A__ ( unittest.TestCase): def __lowerCamelCase ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = torch.manual_seed(0 ) __lowerCAmelCase : int = DiTPipeline.from_pretrained('facebook/DiT-XL-2-256' ) pipe.to('cuda' ) __lowerCAmelCase : Optional[Any] = ['vase', 'umbrella', 'white shark', 'white wolf'] __lowerCAmelCase : Optional[Any] = pipe.get_label_ids(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=40 , output_type='np' ).images for word, image in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Tuple = load_numpy( f"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1E-2 def __lowerCamelCase ( self ): __lowerCAmelCase : Any = DiTPipeline.from_pretrained('facebook/DiT-XL-2-512' ) __lowerCAmelCase : Union[str, Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to('cuda' ) __lowerCAmelCase : Dict = ['vase', 'umbrella'] __lowerCAmelCase : List[str] = pipe.get_label_ids(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = torch.manual_seed(0 ) __lowerCAmelCase : Optional[Any] = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=25 , output_type='np' ).images for word, image in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' f"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1E-1
86
0
from manim import * class A ( __UpperCAmelCase ): def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" lowerCAmelCase_ = Rectangle(height=0.5, width=0.5 ) lowerCAmelCase_ = Rectangle(height=0.46, width=0.46 ).set_stroke(width=0 ) lowerCAmelCase_ = [mem.copy() for i in range(6 )] lowerCAmelCase_ = [mem.copy() for i in range(6 )] lowerCAmelCase_ = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0 ) lowerCAmelCase_ = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0 ) lowerCAmelCase_ = VGroup(UpperCamelCase__, UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0 ) lowerCAmelCase_ = Text('''CPU''', font_size=24 ) lowerCAmelCase_ = Group(UpperCamelCase__, UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0.5, aligned_edge=UpperCamelCase__ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(UpperCamelCase__ ) lowerCAmelCase_ = [mem.copy() for i in range(1 )] lowerCAmelCase_ = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0 ) lowerCAmelCase_ = Text('''GPU''', font_size=24 ) lowerCAmelCase_ = Group(UpperCamelCase__, UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0.5, aligned_edge=UpperCamelCase__ ) gpu.align_to(UpperCamelCase__, UpperCamelCase__ ) gpu.set_x(gpu.get_x() - 1 ) self.add(UpperCamelCase__ ) lowerCAmelCase_ = [mem.copy() for i in range(6 )] lowerCAmelCase_ = VGroup(*UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0 ) lowerCAmelCase_ = Text('''Model''', font_size=24 ) lowerCAmelCase_ = Group(UpperCamelCase__, UpperCamelCase__ ).arrange(UpperCamelCase__, buff=0.5, aligned_edge=UpperCamelCase__ ) model.move_to([3, -1.0, 0] ) self.play( Create(UpperCamelCase__, run_time=1 ), Create(UpperCamelCase__, run_time=1 ), Create(UpperCamelCase__, run_time=1 ), ) lowerCAmelCase_ = MarkupText( f"First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM.", font_size=24, ) lowerCAmelCase_ = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) lowerCAmelCase_ = MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model", font_size=18, ) key_text.move_to([-5, 2.4, 0] ) step_a.move_to([2, 2, 0] ) self.play(Write(UpperCamelCase__, run_time=2.5 ), Write(UpperCamelCase__ ), Write(UpperCamelCase__ ) ) self.add(UpperCamelCase__ ) lowerCAmelCase_ = [] lowerCAmelCase_ = [] lowerCAmelCase_ = [] for i, rect in enumerate(UpperCamelCase__ ): lowerCAmelCase_ = Rectangle(height=0.46, width=0.46 ).set_stroke(width=0.0 ).set_fill(UpperCamelCase__, opacity=0.7 ) cpu_target.move_to(UpperCamelCase__ ) cpu_target.generate_target() lowerCAmelCase_ = 0.46 / 4 lowerCAmelCase_ = 0.46 / 3 if i == 0: cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ), buff=0.02, direction=UpperCamelCase__ ) cpu_target.target.set_x(cpu_target.target.get_x() + 0.1 ) elif i == 3: cpu_target.target.next_to(cpu_targs[0].target, direction=UpperCamelCase__, buff=0.0 ) else: cpu_target.target.next_to(cpu_targs[i - 1].target, direction=UpperCamelCase__, buff=0.0 ) cpu_targs.append(UpperCamelCase__ ) first_animations.append(rect.animate(run_time=0.5 ).set_stroke(UpperCamelCase__ ) ) second_animations.append(MoveToTarget(UpperCamelCase__, run_time=1.5 ) ) self.play(*UpperCamelCase__ ) self.play(*UpperCamelCase__ ) self.wait()
369
import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging _A = logging.get_logger(__name__) def __UpperCamelCase ( _A , _A , _A , _A=False ): try: import torch # noqa: F401 except ImportError: logger.error( '''Loading a PyTorch model in Flax, 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 if not is_sharded: lowerCAmelCase_ = os.path.abspath(_A ) logger.info(f"Loading PyTorch weights from {pt_path}" ) lowerCAmelCase_ = torch.load(_A , map_location='''cpu''' ) logger.info(f"PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters." ) lowerCAmelCase_ = convert_pytorch_state_dict_to_flax(_A , _A ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files lowerCAmelCase_ = convert_pytorch_sharded_state_dict_to_flax(_A , _A ) return flax_state_dict def __UpperCamelCase ( _A , _A , _A , _A , ): def is_key_or_prefix_key_in_dict(_A ) -> bool: return len(set(_A ) & {key, (model_prefix,) + key} ) > 0 # layer norm lowerCAmelCase_ = pt_tuple_key[:-1] + ('''scale''',) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(_A ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean lowerCAmelCase_ = pt_tuple_key[:-1] + ('''mean''',) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(_A ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var lowerCAmelCase_ = pt_tuple_key[:-1] + ('''var''',) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(_A ): return renamed_pt_tuple_key, pt_tensor # embedding lowerCAmelCase_ = pt_tuple_key[:-1] + ('''embedding''',) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(_A ): return renamed_pt_tuple_key, pt_tensor # conv layer lowerCAmelCase_ = pt_tuple_key[:-1] + ('''kernel''',) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(_A ): lowerCAmelCase_ = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer lowerCAmelCase_ = pt_tuple_key[:-1] + ('''kernel''',) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(_A ): lowerCAmelCase_ = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight lowerCAmelCase_ = pt_tuple_key[:-1] + ('''weight''',) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias lowerCAmelCase_ = pt_tuple_key[:-1] + ('''bias''',) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 lowerCAmelCase_ = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): lowerCAmelCase_ = pt_tuple_key[-2] + '''_g''' elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): lowerCAmelCase_ = pt_tuple_key[-2] + '''_v''' if name is not None: lowerCAmelCase_ = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def __UpperCamelCase ( _A , _A ): # convert pytorch tensor to numpy lowerCAmelCase_ = {k: v.numpy() for k, v in pt_state_dict.items()} lowerCAmelCase_ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: lowerCAmelCase_ = flax_model.params['''params'''] else: lowerCAmelCase_ = flax_model.params lowerCAmelCase_ = flatten_dict(_A ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: lowerCAmelCase_ = flatten_dict(flax_model.params['''batch_stats'''] ) random_flax_state_dict.update(_A ) lowerCAmelCase_ = {} lowerCAmelCase_ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split('''.''' )[0] for k in pt_state_dict.keys()} ) lowerCAmelCase_ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split('''.''' )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): lowerCAmelCase_ = tuple(pt_key.split('''.''' ) ) # remove base model prefix if necessary lowerCAmelCase_ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: lowerCAmelCase_ = pt_tuple_key[1:] # Correctly rename weight parameters lowerCAmelCase_ , lowerCAmelCase_ = rename_key_and_reshape_tensor( _A , _A , _A , _A ) # add model prefix if necessary lowerCAmelCase_ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: lowerCAmelCase_ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " f"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: lowerCAmelCase_ = jnp.asarray(_A ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_A , _A ) continue # also add unexpected weight so that warning is thrown lowerCAmelCase_ = jnp.asarray(_A ) else: # also add unexpected weight so that warning is thrown lowerCAmelCase_ = jnp.asarray(_A ) return unflatten_dict(_A ) def __UpperCamelCase ( _A , _A ): import torch # Load the index lowerCAmelCase_ = {} for shard_file in shard_filenames: # load using msgpack utils lowerCAmelCase_ = torch.load(_A ) lowerCAmelCase_ = {k: v.numpy() for k, v in pt_state_dict.items()} lowerCAmelCase_ = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: lowerCAmelCase_ = flax_model.params['''params'''] lowerCAmelCase_ = flatten_dict(_A ) random_flax_state_dict.update(flatten_dict(flax_model.params['''batch_stats'''] ) ) else: lowerCAmelCase_ = flax_model.params lowerCAmelCase_ = flatten_dict(_A ) lowerCAmelCase_ = (model_prefix not in flax_model_params) and ( model_prefix in {k.split('''.''' )[0] for k in pt_state_dict.keys()} ) lowerCAmelCase_ = (model_prefix in flax_model_params) and ( model_prefix not in {k.split('''.''' )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): lowerCAmelCase_ = tuple(pt_key.split('''.''' ) ) # remove base model prefix if necessary lowerCAmelCase_ = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: lowerCAmelCase_ = pt_tuple_key[1:] # Correctly rename weight parameters lowerCAmelCase_ , lowerCAmelCase_ = rename_key_and_reshape_tensor( _A , _A , _A , _A ) # add model prefix if necessary lowerCAmelCase_ = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: lowerCAmelCase_ = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " f"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: lowerCAmelCase_ = jnp.asarray(_A ) continue if "var" in flax_key[-1]: lowerCAmelCase_ = jnp.asarray(_A ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(_A , _A ) continue # also add unexpected weight so that warning is thrown lowerCAmelCase_ = jnp.asarray(_A ) else: # also add unexpected weight so that warning is thrown lowerCAmelCase_ = jnp.asarray(_A ) return unflatten_dict(_A ) def __UpperCamelCase ( _A , _A ): lowerCAmelCase_ = os.path.abspath(_A ) logger.info(f"Loading Flax weights from {flax_checkpoint_path}" ) # import correct flax class lowerCAmelCase_ = getattr(_A , '''Flax''' + model.__class__.__name__ ) # load flax weight dict with open(_A , '''rb''' ) as state_f: try: lowerCAmelCase_ = from_bytes(_A , state_f.read() ) except UnpicklingError: raise EnvironmentError(f"Unable to convert {flax_checkpoint_path} to Flax deserializable object. " ) return load_flax_weights_in_pytorch_model(_A , _A ) def __UpperCamelCase ( _A , _A ): try: import torch # noqa: F401 except ImportError: logger.error( '''Loading a 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 lowerCAmelCase_ = flatten_dict(jax.tree_util.tree_map(lambda _A : x.dtype == jnp.bfloataa , _A ) ).values() if any(_A ): # convert all weights to fp32 if the 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.''' ) lowerCAmelCase_ = jax.tree_util.tree_map( lambda _A : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , _A ) lowerCAmelCase_ = flatten_dict(_A ) lowerCAmelCase_ = pt_model.state_dict() lowerCAmelCase_ = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split('''.''' )[0] for k in pt_model_dict.keys()} ) lowerCAmelCase_ = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split('''.''' )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys lowerCAmelCase_ = [] lowerCAmelCase_ = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): lowerCAmelCase_ = flax_key_tuple[0] == pt_model.base_model_prefix lowerCAmelCase_ = '''.'''.join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: lowerCAmelCase_ = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: lowerCAmelCase_ = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(_A ) not in pt_model_dict: # conv layer lowerCAmelCase_ = flax_key_tuple[:-1] + ('''weight''',) lowerCAmelCase_ = jnp.transpose(_A , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(_A ) not in pt_model_dict: # linear layer lowerCAmelCase_ = flax_key_tuple[:-1] + ('''weight''',) lowerCAmelCase_ = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: lowerCAmelCase_ = flax_key_tuple[:-1] + ('''weight''',) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: lowerCAmelCase_ = flax_key_tuple[:-1] + ('''running_mean''',) elif "var" in flax_key_tuple[-1]: lowerCAmelCase_ = flax_key_tuple[:-1] + ('''running_var''',) if "batch_stats" in flax_state: lowerCAmelCase_ = '''.'''.join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: lowerCAmelCase_ = '''.'''.join(_A ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. lowerCAmelCase_ = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: lowerCAmelCase_ = key.split('''.''' ) lowerCAmelCase_ = None if key_components[-3::2] == ["parametrizations", "original0"]: lowerCAmelCase_ = key_components[-2] + '''_g''' elif key_components[-3::2] == ["parametrizations", "original1"]: lowerCAmelCase_ = key_components[-2] + '''_v''' if name is not None: lowerCAmelCase_ = key_components[:-3] + [name] lowerCAmelCase_ = '''.'''.join(_A ) lowerCAmelCase_ = key if flax_key in special_pt_names: lowerCAmelCase_ = special_pt_names[flax_key] 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 lowerCAmelCase_ = np.asarray(_A ) if not isinstance(_A , np.ndarray ) else flax_tensor lowerCAmelCase_ = torch.from_numpy(_A ) # remove from missing keys missing_keys.remove(_A ) else: # weight is not expected by PyTorch model unexpected_keys.append(_A ) pt_model.load_state_dict(_A ) # re-transform missing_keys to list lowerCAmelCase_ = list(_A ) if len(_A ) > 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).''' ) else: logger.warning(f"All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n" ) if len(_A ) > 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.''' ) else: logger.warning( f"All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n" '''If your task is similar to the task the model of the checkpoint was trained on, ''' f"you can already use {pt_model.__class__.__name__} for predictions without further training." ) return pt_model
167
0
'''simple docstring''' def lowercase ( __magic_name__ ): '''simple docstring''' UpperCAmelCase : int = [1] UpperCAmelCase : Union[str, Any] = 0, 0, 0 UpperCAmelCase : List[str] = ugly_nums[ia] * 2 UpperCAmelCase : Optional[int] = ugly_nums[ia] * 3 UpperCAmelCase : int = ugly_nums[ia] * 5 for _ in range(1 , A__ ): UpperCAmelCase : Tuple = min(A__ , A__ , A__ ) ugly_nums.append(A__ ) if next_num == next_a: ia += 1 UpperCAmelCase : Optional[Any] = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 UpperCAmelCase : Tuple = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 UpperCAmelCase : List[str] = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(F'{ugly_numbers(2_00) = }')
311
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" ,"""False""" ) ) is not True ,reason="""Skipping test because should only be run when releasing minor transformers version""" ,) @pytest.mark.usefixtures("""sm_env""" ) @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue_model_parallelism.py""", """model_name_or_path""": """roberta-large""", """instance_type""": """ml.p3dn.24xlarge""", """results""": {"""train_runtime""": 1600, """eval_accuracy""": 0.3, """eval_loss""": 1.2}, }, { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """roberta-large""", """instance_type""": """ml.p3dn.24xlarge""", """results""": {"""train_runtime""": 1600, """eval_accuracy""": 0.3, """eval_loss""": 1.2}, }, ] ) class __snake_case ( unittest.TestCase ): def __a ( self ) -> Tuple: '''simple docstring''' if self.framework == "pytorch": subprocess.run( F"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() , encoding='utf-8' , check=__UpperCamelCase , ) assert hasattr(self , 'env' ) def __a ( self , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' snake_case__ : Tuple = { 'enabled': True, 'processes_per_host': 8, } snake_case__ : Any = { 'enabled': True, 'parameters': { 'microbatches': 4, 'placement_strategy': 'spread', 'pipeline': 'interleaved', 'optimize': 'speed', 'partitions': 4, 'ddp': True, }, } snake_case__ : Optional[int] = {'smdistributed': {'modelparallel': smp_options}, 'mpi': mpi_options} snake_case__ : int = 'trainer' if self.script == 'run_glue.py' else 'smtrainer' # creates estimator return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=F"""{self.env.base_job_name}-{instance_count}-smp-{name_extension}""" , instance_count=__UpperCamelCase , instance_type=self.instance_type , debugger_hook_config=__UpperCamelCase , hyperparameters={ **self.env.hyperparameters, 'model_name_or_path': self.model_name_or_path, 'max_steps': 500, } , metric_definitions=self.env.metric_definitions , distribution=__UpperCamelCase , py_version='py36' , ) def __a ( self , __UpperCamelCase ) -> List[Any]: '''simple docstring''' TrainingJobAnalytics(__UpperCamelCase ).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(1,)] ) def __a ( self , __UpperCamelCase ) -> List[Any]: '''simple docstring''' snake_case__ : str = self.create_estimator(__UpperCamelCase ) # run training estimator.fit() # result dataframe snake_case__ : Dict = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis snake_case__ : Any = list(result_metrics_df[result_metrics_df.metric_name == 'eval_accuracy']['value'] ) snake_case__ : List[str] = list(result_metrics_df[result_metrics_df.metric_name == 'eval_loss']['value'] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping snake_case__ : Any = ( Session().describe_training_job(estimator.latest_training_job.name ).get('TrainingTimeInSeconds' , 999999 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['eval_accuracy'] for t in eval_accuracy ) assert all(t <= self.results['eval_loss'] for t in eval_loss ) # dump tests result into json file to share in PR with open(F"""{estimator.latest_training_job.name}.json""" , 'w' ) as outfile: json.dump({'train_time': train_runtime, 'eval_accuracy': eval_accuracy, 'eval_loss': eval_loss} , __UpperCamelCase )
143
0
"""simple docstring""" import baseaa import io import json import os from copy import deepcopy from ..optimizer import AcceleratedOptimizer from ..scheduler import AcceleratedScheduler class _UpperCAmelCase : def __init__( self : Union[str, Any] , _lowercase : List[str] ): if isinstance(_lowercase , _lowercase ): # Don't modify user's data should they want to reuse it (e.g. in tests), because once we # modified it, it will not be accepted here again, since `auto` values would have been overridden __UpperCAmelCase = deepcopy(_lowercase ) elif os.path.exists(_lowercase ): with io.open(_lowercase , '''r''' , encoding='''utf-8''' ) as f: __UpperCAmelCase = json.load(_lowercase ) else: try: __UpperCAmelCase = baseaa.urlsafe_baadecode(_lowercase ).decode('''utf-8''' ) __UpperCAmelCase = json.loads(_lowercase ) except (UnicodeDecodeError, AttributeError, ValueError): raise ValueError( F'''Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}''' ) __UpperCAmelCase = config self.set_stage_and_offload() def a ( self : List[str] ): # zero stage - this is done as early as possible, before model is created, to allow # ``is_deepspeed_zero3_enabled`` query and getting to the early deepspeed config object # during ``zero.Init()`` which needs to know the dtype, and some other hparams. __UpperCAmelCase = self.get_value('''zero_optimization.stage''' , -1 ) # offload __UpperCAmelCase = False if self.is_zeroa() or self.is_zeroa(): __UpperCAmelCase = set(['''cpu''', '''nvme'''] ) __UpperCAmelCase = set( [ self.get_value('''zero_optimization.offload_optimizer.device''' ), self.get_value('''zero_optimization.offload_param.device''' ), ] ) if len(offload_devices & offload_devices_valid ) > 0: __UpperCAmelCase = True def a ( self : Optional[int] , _lowercase : Tuple ): __UpperCAmelCase = self.config # find the config node of interest if it exists __UpperCAmelCase = ds_key_long.split('''.''' ) __UpperCAmelCase = nodes.pop() for node in nodes: __UpperCAmelCase = config.get(_lowercase ) if config is None: return None, ds_key return config, ds_key def a ( self : Optional[Any] , _lowercase : Optional[Any] , _lowercase : Optional[Any]=None ): __UpperCAmelCase , __UpperCAmelCase = self.find_config_node(_lowercase ) if config is None: return default return config.get(_lowercase , _lowercase ) def a ( self : Union[str, Any] , _lowercase : Union[str, Any] , _lowercase : Dict=False ): __UpperCAmelCase = self.config # find the config node of interest if it exists __UpperCAmelCase = ds_key_long.split('''.''' ) for node in nodes: __UpperCAmelCase = config __UpperCAmelCase = config.get(_lowercase ) if config is None: if must_exist: raise ValueError(F'''Can\'t find {ds_key_long} entry in the config: {self.config}''' ) else: return # if found remove it if parent_config is not None: parent_config.pop(_lowercase ) def a ( self : Any , _lowercase : Dict ): __UpperCAmelCase = self.get_value(_lowercase ) return False if value is None else bool(_lowercase ) def a ( self : Optional[Any] , _lowercase : List[str] ): __UpperCAmelCase = self.get_value(_lowercase ) return False if value is None else not bool(_lowercase ) def a ( self : str ): return self._stage == 2 def a ( self : Union[str, Any] ): return self._stage == 3 def a ( self : List[str] ): return self._offload class _UpperCAmelCase : def __init__( self : List[str] , _lowercase : int ): __UpperCAmelCase = engine def a ( self : List[str] , _lowercase : List[str] , **_lowercase : Any ): # runs backpropagation and handles mixed precision self.engine.backward(_lowercase , **_lowercase ) # Deepspeed's `engine.step` performs the following operations: # - gradient accumulation check # - gradient clipping # - optimizer step # - zero grad # - checking overflow # - lr_scheduler step (only if engine.lr_scheduler is not None) self.engine.step() # and this plugin overrides the above calls with no-ops when Accelerate runs under # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple # training loop that works transparently under many training regimes. class _UpperCAmelCase ( _lowerCAmelCase ): def __init__( self : List[Any] , _lowercase : Optional[int] ): super().__init__(_lowercase , device_placement=_lowercase , scaler=_lowercase ) __UpperCAmelCase = hasattr(self.optimizer , '''overflow''' ) def a ( self : Dict , _lowercase : int=None ): pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed def a ( self : List[str] ): pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed @property def a ( self : Tuple ): if self.__has_overflow__: return self.optimizer.overflow return False class _UpperCAmelCase ( _lowerCAmelCase ): def __init__( self : Dict , _lowercase : Any , _lowercase : Dict ): super().__init__(_lowercase , _lowercase ) def a ( self : List[Any] ): pass # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed class _UpperCAmelCase : def __init__( self : Any , _lowercase : List[str] , _lowercase : Optional[int]=0.001 , _lowercase : List[str]=0 , **_lowercase : Optional[int] ): __UpperCAmelCase = params __UpperCAmelCase = lr __UpperCAmelCase = weight_decay __UpperCAmelCase = kwargs class _UpperCAmelCase : def __init__( self : str , _lowercase : Any , _lowercase : List[str]=None , _lowercase : Any=0 , **_lowercase : Tuple ): __UpperCAmelCase = optimizer __UpperCAmelCase = total_num_steps __UpperCAmelCase = warmup_num_steps __UpperCAmelCase = kwargs
86
"""simple docstring""" from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch _lowercase : Optional[int] = logging.get_logger(__name__) @add_end_docstrings( _lowerCAmelCase , r"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class _UpperCAmelCase ( _lowerCAmelCase ): def a ( self : List[Any] , _lowercase : GenericTensor ): if self.framework == "tf": __UpperCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": __UpperCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_lowercase ) else: raise ValueError('''Unsupported framework''' ) return masked_index def a ( self : List[str] , _lowercase : GenericTensor ): __UpperCAmelCase = self.get_masked_index(_lowercase ) __UpperCAmelCase = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , F'''No mask_token ({self.tokenizer.mask_token}) found on the input''' , ) def a ( self : Optional[int] , _lowercase : GenericTensor ): if isinstance(_lowercase , _lowercase ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(_lowercase ) def a ( self : List[str] , _lowercase : Optional[int] , _lowercase : Tuple=None , **_lowercase : Tuple ): if return_tensors is None: __UpperCAmelCase = self.framework __UpperCAmelCase = self.tokenizer(_lowercase , return_tensors=_lowercase ) self.ensure_exactly_one_mask_token(_lowercase ) return model_inputs def a ( self : Optional[int] , _lowercase : Tuple ): __UpperCAmelCase = self.model(**_lowercase ) __UpperCAmelCase = model_inputs['''input_ids'''] return model_outputs def a ( self : Optional[int] , _lowercase : List[str] , _lowercase : Optional[Any]=5 , _lowercase : Dict=None ): # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: __UpperCAmelCase = target_ids.shape[0] __UpperCAmelCase = model_outputs['''input_ids'''][0] __UpperCAmelCase = model_outputs['''logits'''] if self.framework == "tf": __UpperCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] __UpperCAmelCase = outputs.numpy() __UpperCAmelCase = outputs[0, masked_index, :] __UpperCAmelCase = stable_softmax(_lowercase , axis=-1 ) if target_ids is not None: __UpperCAmelCase = tf.gather_nd(tf.squeeze(_lowercase , 0 ) , target_ids.reshape(-1 , 1 ) ) __UpperCAmelCase = tf.expand_dims(_lowercase , 0 ) __UpperCAmelCase = tf.math.top_k(_lowercase , k=_lowercase ) __UpperCAmelCase , __UpperCAmelCase = topk.values.numpy(), topk.indices.numpy() else: __UpperCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=_lowercase ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample __UpperCAmelCase = outputs[0, masked_index, :] __UpperCAmelCase = logits.softmax(dim=-1 ) if target_ids is not None: __UpperCAmelCase = probs[..., target_ids] __UpperCAmelCase , __UpperCAmelCase = probs.topk(_lowercase ) __UpperCAmelCase = [] __UpperCAmelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): __UpperCAmelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place __UpperCAmelCase = input_ids.numpy().copy() if target_ids is not None: __UpperCAmelCase = target_ids[p].tolist() __UpperCAmelCase = p # Filter padding out: __UpperCAmelCase = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back __UpperCAmelCase = self.tokenizer.decode(_lowercase , skip_special_tokens=_lowercase ) __UpperCAmelCase = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(_lowercase ) result.append(_lowercase ) if single_mask: return result[0] return result def a ( self : str , _lowercase : List[Any] , _lowercase : List[Any]=None ): if isinstance(_lowercase , _lowercase ): __UpperCAmelCase = [targets] try: __UpperCAmelCase = self.tokenizer.get_vocab() except Exception: __UpperCAmelCase = {} __UpperCAmelCase = [] for target in targets: __UpperCAmelCase = vocab.get(_lowercase , _lowercase ) if id_ is None: __UpperCAmelCase = self.tokenizer( _lowercase , add_special_tokens=_lowercase , return_attention_mask=_lowercase , return_token_type_ids=_lowercase , max_length=1 , truncation=_lowercase , )['''input_ids'''] if len(_lowercase ) == 0: logger.warning( F'''The specified target token `{target}` does not exist in the model vocabulary. ''' '''We cannot replace it with anything meaningful, ignoring it''' ) continue __UpperCAmelCase = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F'''The specified target token `{target}` does not exist in the model vocabulary. ''' F'''Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`.''' ) target_ids.append(id_ ) __UpperCAmelCase = list(set(_lowercase ) ) if len(_lowercase ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) __UpperCAmelCase = np.array(_lowercase ) return target_ids def a ( self : int , _lowercase : Dict=None , _lowercase : Optional[Any]=None ): __UpperCAmelCase = {} if targets is not None: __UpperCAmelCase = self.get_target_ids(_lowercase , _lowercase ) __UpperCAmelCase = target_ids if top_k is not None: __UpperCAmelCase = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__( self : Union[str, Any] , _lowercase : Optional[Any] , *_lowercase : Union[str, Any] , **_lowercase : int ): __UpperCAmelCase = super().__call__(_lowercase , **_lowercase ) if isinstance(_lowercase , _lowercase ) and len(_lowercase ) == 1: return outputs[0] return outputs
86
1
'''simple docstring''' import numpy class snake_case : """simple docstring""" def __init__( self , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = input_array # Random initial weights are assigned where first argument is the # number of nodes in previous layer and second argument is the # number of nodes in the next layer. # Random initial weights are assigned. # self.input_array.shape[1] is used to represent number of nodes in input layer. # First hidden layer consists of 4 nodes. lowerCamelCase_ = numpy.random.rand( self.input_array.shape[1] , 4 ) # Random initial values for the first hidden layer. # First hidden layer has 4 nodes. # Second hidden layer has 3 nodes. lowerCamelCase_ = numpy.random.rand( 4 , 3 ) # Random initial values for the second hidden layer. # Second hidden layer has 3 nodes. # Output layer has 1 node. lowerCamelCase_ = numpy.random.rand(3 , 1 ) # Real output values provided. lowerCamelCase_ = output_array # Predicted output values by the neural network. # Predicted_output array initially consists of zeroes. lowerCamelCase_ = numpy.zeros(output_array.shape ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = sigmoid( numpy.dot(self.input_array , self.input_layer_and_first_hidden_layer_weights ) ) # layer_between_first_hidden_layer_and_second_hidden_layer is the layer # connecting the first hidden set of nodes with the second hidden set of nodes. lowerCamelCase_ = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , ) ) # layer_between_second_hidden_layer_and_output is the layer connecting # second hidden layer with the output node. lowerCamelCase_ = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , ) ) return self.layer_between_second_hidden_layer_and_output def snake_case ( self ): """simple docstring""" lowerCamelCase_ = numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer.T , 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , ) lowerCamelCase_ = numpy.dot( self.layer_between_input_and_first_hidden_layer.T , numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer ) , ) lowerCamelCase_ = numpy.dot( self.input_array.T , numpy.dot( numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer ) , self.first_hidden_layer_and_second_hidden_layer_weights.T , ) * sigmoid_derivative(self.layer_between_input_and_first_hidden_layer ) , ) self.input_layer_and_first_hidden_layer_weights += ( updated_input_layer_and_first_hidden_layer_weights ) self.first_hidden_layer_and_second_hidden_layer_weights += ( updated_first_hidden_layer_and_second_hidden_layer_weights ) self.second_hidden_layer_and_output_layer_weights += ( updated_second_hidden_layer_and_output_layer_weights ) def snake_case ( self , UpperCamelCase , UpperCamelCase , UpperCamelCase ): """simple docstring""" for iteration in range(1 , iterations + 1 ): lowerCamelCase_ = self.feedforward() self.back_propagation() if give_loss: lowerCamelCase_ = numpy.mean(numpy.square(output - self.feedforward() ) ) print(f'''Iteration {iteration} Loss: {loss}''' ) def snake_case ( self , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = input_arr lowerCamelCase_ = sigmoid( numpy.dot(self.array , self.input_layer_and_first_hidden_layer_weights ) ) lowerCamelCase_ = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , ) ) lowerCamelCase_ = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , ) ) return int(self.layer_between_second_hidden_layer_and_output > 0.6 ) def __snake_case ( UpperCAmelCase_ : numpy.ndarray ): return 1 / (1 + numpy.exp(-value )) def __snake_case ( UpperCAmelCase_ : numpy.ndarray ): return (value) * (1 - (value)) def __snake_case ( ): lowerCamelCase_ = numpy.array( ( [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1], ) , dtype=numpy.floataa , ) # True output values for the given input values. lowerCamelCase_ = numpy.array(([0], [1], [1], [0], [1], [0], [0], [1]) , dtype=numpy.floataa ) # Calling neural network class. lowerCamelCase_ = TwoHiddenLayerNeuralNetwork( input_array=UpperCAmelCase_ , output_array=UpperCAmelCase_ ) # Calling training function. # Set give_loss to True if you want to see loss in every iteration. neural_network.train(output=UpperCAmelCase_ , iterations=10 , give_loss=UpperCAmelCase_ ) return neural_network.predict(numpy.array(([1, 1, 1]) , dtype=numpy.floataa ) ) if __name__ == "__main__": example()
55
"""simple docstring""" from __future__ import annotations def lowerCamelCase__ ( __snake_case, __snake_case ) -> float: """simple docstring""" _UpperCamelCase = sorted(numsa + numsa ) _UpperCamelCase , _UpperCamelCase = divmod(len(__snake_case ), 2 ) if mod == 1: return all_numbers[div] else: return (all_numbers[div] + all_numbers[div - 1]) / 2 if __name__ == "__main__": import doctest doctest.testmod() _a = [float(x) for x in input("""Enter the elements of first array: """).split()] _a = [float(x) for x in input("""Enter the elements of second array: """).split()] print(F"""The median of two arrays is: {median_of_two_arrays(array_a, array_a)}""")
194
0
import argparse import torch from transformers import ( UniSpeechSatConfig, UniSpeechSatForAudioFrameClassification, UniSpeechSatForSequenceClassification, UniSpeechSatForXVector, WavaVecaFeatureExtractor, logging, ) logging.set_verbosity_info() a_ : int = logging.get_logger(__name__) def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = UniSpeechSatForSequenceClassification.from_pretrained(_UpperCAmelCase , config=_UpperCAmelCase) SCREAMING_SNAKE_CASE = downstream_dict['projector.weight'] SCREAMING_SNAKE_CASE = downstream_dict['projector.bias'] SCREAMING_SNAKE_CASE = downstream_dict['model.post_net.linear.weight'] SCREAMING_SNAKE_CASE = downstream_dict['model.post_net.linear.bias'] return model def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = UniSpeechSatForAudioFrameClassification.from_pretrained(_UpperCAmelCase , config=_UpperCAmelCase) SCREAMING_SNAKE_CASE = downstream_dict['model.linear.weight'] SCREAMING_SNAKE_CASE = downstream_dict['model.linear.bias'] return model def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = UniSpeechSatForXVector.from_pretrained(_UpperCAmelCase , config=_UpperCAmelCase) SCREAMING_SNAKE_CASE = downstream_dict['connector.weight'] SCREAMING_SNAKE_CASE = downstream_dict['connector.bias'] for i, kernel_size in enumerate(hf_config.tdnn_kernel): SCREAMING_SNAKE_CASE = downstream_dict[ F'''model.framelevel_feature_extractor.module.{i}.kernel.weight''' ] SCREAMING_SNAKE_CASE = downstream_dict[F'''model.framelevel_feature_extractor.module.{i}.kernel.bias'''] SCREAMING_SNAKE_CASE = downstream_dict['model.utterancelevel_feature_extractor.linear1.weight'] SCREAMING_SNAKE_CASE = downstream_dict['model.utterancelevel_feature_extractor.linear1.bias'] SCREAMING_SNAKE_CASE = downstream_dict['model.utterancelevel_feature_extractor.linear2.weight'] SCREAMING_SNAKE_CASE = downstream_dict['model.utterancelevel_feature_extractor.linear2.bias'] SCREAMING_SNAKE_CASE = downstream_dict['objective.W'] return model @torch.no_grad() def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = torch.load(_UpperCAmelCase , map_location='cpu') SCREAMING_SNAKE_CASE = checkpoint['Downstream'] SCREAMING_SNAKE_CASE = UniSpeechSatConfig.from_pretrained(_UpperCAmelCase) SCREAMING_SNAKE_CASE = WavaVecaFeatureExtractor.from_pretrained( _UpperCAmelCase , return_attention_mask=_UpperCAmelCase , do_normalize=_UpperCAmelCase) SCREAMING_SNAKE_CASE = hf_config.architectures[0] if arch.endswith('ForSequenceClassification'): SCREAMING_SNAKE_CASE = convert_classification(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase) elif arch.endswith('ForAudioFrameClassification'): SCREAMING_SNAKE_CASE = convert_diarization(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase) elif arch.endswith('ForXVector'): SCREAMING_SNAKE_CASE = convert_xvector(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase) else: raise NotImplementedError(F'''S3PRL weights conversion is not supported for {arch}''') if hf_config.use_weighted_layer_sum: SCREAMING_SNAKE_CASE = checkpoint['Featurizer']['weights'] hf_feature_extractor.save_pretrained(_UpperCAmelCase) hf_model.save_pretrained(_UpperCAmelCase) if __name__ == "__main__": a_ : List[Any] = argparse.ArgumentParser() parser.add_argument( '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.' ) parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.') parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.') a_ : Tuple = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
327
import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput a_ : Dict = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _snake_case ( A__ ): def __init__( self , *a , a=None , a=None , a=None , **a) -> List[Any]: super().__init__(*a , **a) SCREAMING_SNAKE_CASE = eval_examples SCREAMING_SNAKE_CASE = post_process_function SCREAMING_SNAKE_CASE = quant_trainer_args SCREAMING_SNAKE_CASE = 128 # default number of calibration samples def SCREAMING_SNAKE_CASE__ ( self , a=None) -> Union[str, Any]: if calib_dataset is None and self.calib_dataset is None: raise ValueError('Trainer: calibration requires an calib_dataset.') SCREAMING_SNAKE_CASE = calib_dataset if calib_dataset is not None else self.calib_dataset SCREAMING_SNAKE_CASE = self._remove_unused_columns(a , description='Calibration') return DataLoader( a , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=a , ) def SCREAMING_SNAKE_CASE__ ( self , a=None) -> Optional[Any]: SCREAMING_SNAKE_CASE = self.train_dataset if calib_dataset is None else calib_dataset SCREAMING_SNAKE_CASE = self.get_calib_dataloader(a) SCREAMING_SNAKE_CASE = self.model quant_trainer.configure_model(a , self.quant_trainer_args , calib=a) model.eval() quant_trainer.enable_calibration(a) logger.info('***** Running calibration *****') logger.info(f''' Num examples = {self.calib_num}''') logger.info(f''' Batch size = {calib_dataloader.batch_size}''') for step, inputs in enumerate(a): # Prediction step SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = self.prediction_step(a , a , prediction_loss_only=a) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(a , self.quant_trainer_args) SCREAMING_SNAKE_CASE = model def SCREAMING_SNAKE_CASE__ ( self , a=None , a=None , a=None , a = "eval") -> str: SCREAMING_SNAKE_CASE = self.eval_dataset if eval_dataset is None else eval_dataset SCREAMING_SNAKE_CASE = self.get_eval_dataloader(a) SCREAMING_SNAKE_CASE = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. SCREAMING_SNAKE_CASE = self.compute_metrics SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: SCREAMING_SNAKE_CASE = eval_loop( a , description='Evaluation' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=a , ) finally: SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: SCREAMING_SNAKE_CASE = self.post_process_function(a , a , output.predictions) SCREAMING_SNAKE_CASE = self.compute_metrics(a) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys()): if not key.startswith(f'''{metric_key_prefix}_'''): SCREAMING_SNAKE_CASE = metrics.pop(a) self.log(a) else: SCREAMING_SNAKE_CASE = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report()) SCREAMING_SNAKE_CASE = self.callback_handler.on_evaluate(self.args , self.state , self.control , a) return metrics def SCREAMING_SNAKE_CASE__ ( self , a , a , a=None , a = "test") -> Optional[Any]: SCREAMING_SNAKE_CASE = self.get_test_dataloader(a) # Temporarily disable metric computation, we will do it in the loop here. SCREAMING_SNAKE_CASE = self.compute_metrics SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: SCREAMING_SNAKE_CASE = eval_loop( a , description='Prediction' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=a , ) finally: SCREAMING_SNAKE_CASE = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output SCREAMING_SNAKE_CASE = self.post_process_function(a , a , output.predictions , 'predict') SCREAMING_SNAKE_CASE = self.compute_metrics(a) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys()): if not key.startswith(f'''{metric_key_prefix}_'''): SCREAMING_SNAKE_CASE = metrics.pop(a) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=a) def SCREAMING_SNAKE_CASE__ ( self , a="./") -> List[Any]: SCREAMING_SNAKE_CASE = self.eval_dataset SCREAMING_SNAKE_CASE = self.get_eval_dataloader(a) SCREAMING_SNAKE_CASE = next(iter(a)) # saving device - to make it consistent SCREAMING_SNAKE_CASE = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # convert to tuple SCREAMING_SNAKE_CASE = tuple(v.to(a) for k, v in batch.items()) logger.info('Converting model to be onnx compatible') from pytorch_quantization.nn import TensorQuantizer SCREAMING_SNAKE_CASE = True SCREAMING_SNAKE_CASE = self.model.to(a) model.eval() model.float() SCREAMING_SNAKE_CASE = model.module if hasattr(a , 'module') else model quant_trainer.configure_model(a , self.quant_trainer_args) SCREAMING_SNAKE_CASE = os.path.join(a , 'model.onnx') logger.info(f'''exporting model to {output_model_file}''') SCREAMING_SNAKE_CASE = {0: 'batch_size', 1: 'seq_len'} torch.onnx.export( a , a , a , export_params=a , opset_version=13 , do_constant_folding=a , input_names=['input_ids', 'attention_mask', 'token_type_ids'] , output_names=['output_start_logits', 'output_end_logits'] , dynamic_axes={ 'input_ids': axes, 'attention_mask': axes, 'token_type_ids': axes, 'output_start_logits': axes, 'output_end_logits': axes, } , verbose=a , ) logger.info('onnx export finished')
327
1
"""simple docstring""" import functools from typing import Any def A_ ( _lowercase, _lowercase ): '''simple docstring''' if not isinstance(_SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE ) or len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError("""the string should be not empty string""" ) if not isinstance(_SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE ) or not all( isinstance(_SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE ) and len(_SCREAMING_SNAKE_CASE ) > 0 for item in words ): raise ValueError("""the words should be a list of non-empty strings""" ) # Build trie snake_case_ :dict[str, Any] = {} snake_case_ :List[Any] = 'WORD_KEEPER' for word in words: snake_case_ :Any = trie for c in word: if c not in trie_node: snake_case_ :Union[str, Any] = {} snake_case_ :Any = trie_node[c] snake_case_ :str = True snake_case_ :Optional[int] = len(_SCREAMING_SNAKE_CASE ) # Dynamic programming method @functools.cache def is_breakable(_lowercase ) -> bool: if index == len_string: return True snake_case_ :Union[str, Any] = trie for i in range(_SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE ): snake_case_ :Dict = trie_node.get(string[i], _SCREAMING_SNAKE_CASE ) if trie_node is None: return False if trie_node.get(_SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
66
"""simple docstring""" import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Dict = 1_0 def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Optional[int] = [1, 2, 3, 4] lowerCAmelCase__ :Tuple = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(__UpperCAmelCase , self.block_size , 0 ) , __UpperCAmelCase ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Optional[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] lowerCAmelCase__ :List[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(__UpperCAmelCase , self.block_size , 0 ) , __UpperCAmelCase ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Dict = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3] lowerCAmelCase__ :List[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(__UpperCAmelCase , self.block_size , 0 ) , __UpperCAmelCase ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Tuple = 'It was the year of Our Lord one thousand seven hundred and\n seventy-five.\n\nSpiritual revelations were conceded to England at that\n favoured period, as at this.' lowerCAmelCase__ , lowerCAmelCase__ :List[Any] = process_story(__UpperCAmelCase ) self.assertEqual(__UpperCAmelCase , [] ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Any = '' lowerCAmelCase__ , lowerCAmelCase__ :Any = process_story(__UpperCAmelCase ) self.assertEqual(__UpperCAmelCase , [] ) self.assertEqual(__UpperCAmelCase , [] ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :List[str] = ( 'It was the year of Our Lord one thousand seven hundred and ' 'seventy-five\n\nSpiritual revelations were conceded to England ' 'at that favoured period, as at this.\n@highlight\n\nIt was the best of times' ) lowerCAmelCase__ , lowerCAmelCase__ :str = process_story(__UpperCAmelCase ) lowerCAmelCase__ :List[str] = [ 'It was the year of Our Lord one thousand seven hundred and seventy-five.', 'Spiritual revelations were conceded to England at that favoured period, as at this.', ] self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) lowerCAmelCase__ :List[str] = ['It was the best of times.'] self.assertEqual(__UpperCAmelCase , __UpperCAmelCase ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Tuple = torch.tensor([1, 2, 3, 4] ) lowerCAmelCase__ :List[str] = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(__UpperCAmelCase , 0 ).numpy() , expected.numpy() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :List[Any] = torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3] ) lowerCAmelCase__ :Optional[int] = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(__UpperCAmelCase , 2_3 ).numpy() , expected.numpy() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :List[Any] = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) lowerCAmelCase__ :Optional[Any] = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(__UpperCAmelCase , 1 ).numpy() , expected.numpy() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Optional[Any] = 1_0_1 lowerCAmelCase__ :str = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]] ) lowerCAmelCase__ :Any = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) lowerCAmelCase__ :List[Any] = compute_token_type_ids(__UpperCAmelCase , __UpperCAmelCase ) np.testing.assert_array_equal(__UpperCAmelCase , __UpperCAmelCase )
293
0
"""simple docstring""" import gzip import hashlib import json import multiprocessing import os import re import shutil import time from pathlib import Path import numpy as np from arguments import PreprocessingArguments from datasets import load_dataset from minhash_deduplication import deduplicate_dataset from transformers import AutoTokenizer, HfArgumentParser __snake_case : Tuple = re.compile(R'\s+') def _lowercase ( __snake_case ) -> Union[str, Any]: return {"hash": hashlib.mda(re.sub(__snake_case ,"" ,example["content"] ).encode("utf-8" ) ).hexdigest()} def _lowercase ( __snake_case ) -> Tuple: __lowerCAmelCase : List[str] = [len(__snake_case ) for line in example["content"].splitlines()] return {"line_mean": np.mean(__snake_case ), "line_max": max(__snake_case )} def _lowercase ( __snake_case ) -> Tuple: __lowerCAmelCase : List[Any] = np.mean([c.isalnum() for c in example["content"]] ) return {"alpha_frac": alpha_frac} def _lowercase ( __snake_case ,__snake_case ) -> Union[str, Any]: if example["hash"] in uniques: uniques.remove(example["hash"] ) return True else: return False def _lowercase ( __snake_case ,__snake_case=5 ) -> Optional[Any]: __lowerCAmelCase : Tuple = ["auto-generated", "autogenerated", "automatically generated"] __lowerCAmelCase : Any = example["content"].splitlines() for _, line in zip(range(__snake_case ) ,__snake_case ): for keyword in keywords: if keyword in line.lower(): return {"autogenerated": True} else: return {"autogenerated": False} def _lowercase ( __snake_case ,__snake_case=5 ,__snake_case=0.05 ) -> Dict: __lowerCAmelCase : List[Any] = ["unit tests", "test file", "configuration file"] __lowerCAmelCase : Optional[int] = example["content"].splitlines() __lowerCAmelCase : str = 0 __lowerCAmelCase : List[Any] = 0 # first test for _, line in zip(range(__snake_case ) ,__snake_case ): for keyword in keywords: if keyword in line.lower(): return {"config_or_test": True} # second test __lowerCAmelCase : int = example["content"].count("\n" ) __lowerCAmelCase : Optional[int] = int(coeff * nlines ) for line in lines: count_config += line.lower().count("config" ) count_test += line.lower().count("test" ) if count_config > threshold or count_test > threshold: return {"config_or_test": True} return {"config_or_test": False} def _lowercase ( __snake_case ) -> List[Any]: __lowerCAmelCase : Optional[int] = ["def ", "class ", "for ", "while "] __lowerCAmelCase : Tuple = example["content"].splitlines() for line in lines: for keyword in keywords: if keyword in line.lower(): return {"has_no_keywords": False} return {"has_no_keywords": True} def _lowercase ( __snake_case ,__snake_case=4 ) -> Union[str, Any]: __lowerCAmelCase : Optional[Any] = example["content"].splitlines() __lowerCAmelCase : List[Any] = 0 for line in lines: counter += line.lower().count("=" ) if counter > minimum: return {"has_few_assignments": False} return {"has_few_assignments": True} def _lowercase ( __snake_case ) -> Any: __lowerCAmelCase : str = tokenizer(example["content"] ,truncation=__snake_case )["input_ids"] __lowerCAmelCase : Optional[int] = len(example["content"] ) / len(__snake_case ) return {"ratio": ratio} def _lowercase ( __snake_case ) -> str: __lowerCAmelCase : Any = {} results.update(get_hash(__snake_case ) ) results.update(line_stats(__snake_case ) ) results.update(alpha_stats(__snake_case ) ) results.update(char_token_ratio(__snake_case ) ) results.update(is_autogenerated(__snake_case ) ) results.update(is_config_or_test(__snake_case ) ) results.update(has_no_keywords(__snake_case ) ) results.update(has_few_assignments(__snake_case ) ) return results def _lowercase ( __snake_case ,__snake_case ,__snake_case ) -> Optional[int]: if not check_uniques(__snake_case ,__snake_case ): return False elif example["autogenerated"]: return False elif example["line_max"] > args.line_max: return False elif example["line_mean"] > args.line_mean: return False elif example["alpha_frac"] < args.alpha_frac: return False elif example["ratio"] < args.min_token_ratio: return False elif example["config_or_test"] and np.random.rand() <= args.filter_proba: return False elif example["has_no_keywords"] and np.random.rand() <= args.filter_proba: return False elif example["has_few_assignments"]: return False else: return True def _lowercase ( __snake_case ) -> Optional[int]: with open(__snake_case ,"rb" ) as f_in: with gzip.open(str(__snake_case ) + ".gz" ,"wb" ,compresslevel=6 ) as f_out: shutil.copyfileobj(__snake_case ,__snake_case ) os.unlink(__snake_case ) # Settings __snake_case : Any = HfArgumentParser(PreprocessingArguments) __snake_case : Optional[Any] = parser.parse_args() if args.num_workers is None: __snake_case : Union[str, Any] = multiprocessing.cpu_count() __snake_case : str = AutoTokenizer.from_pretrained(args.tokenizer_dir) # Load dataset __snake_case : List[Any] = time.time() __snake_case : Optional[int] = load_dataset(args.dataset_name, split='train') print(F"""Time to load dataset: {time.time()-t_start:.2f}""") # Run preprocessing __snake_case : Optional[Any] = time.time() __snake_case : int = ds.map(preprocess, num_proc=args.num_workers) print(F"""Time to preprocess dataset: {time.time()-t_start:.2f}""") # Deduplicate hashes __snake_case : List[Any] = set(ds.unique('hash')) __snake_case : List[Any] = len(uniques) / len(ds) print(F"""Fraction of duplicates: {1-frac:.2%}""") # Deduplicate data and apply heuristics __snake_case : int = time.time() __snake_case : Union[str, Any] = ds.filter(filter, fn_kwargs={'uniques': uniques, 'args': args}) print(F"""Time to filter dataset: {time.time()-t_start:.2f}""") print(F"""Size of filtered dataset: {len(ds_filter)}""") # Deduplicate with minhash and jaccard similarity if args.near_deduplication: __snake_case : Union[str, Any] = time.time() __snake_case , __snake_case : List[Any] = deduplicate_dataset(ds_filter, args.jaccard_threshold) print(F"""Time to deduplicate dataset: {time.time()-t_start:.2f}""") print(F"""Size of deduplicate dataset: {len(ds_filter)}""") # Save data in batches of samples_per_file __snake_case : Optional[int] = Path(args.output_dir) output_dir.mkdir(exist_ok=True) # save duplicate_clusters in the output_dir as artifacts # not sure it is the right place the save it if args.near_deduplication: with open(output_dir / 'duplicate_clusters.json', 'w') as f: json.dump(duplicate_clusters, f) __snake_case : Union[str, Any] = output_dir / 'data' data_dir.mkdir(exist_ok=True) __snake_case : List[Any] = time.time() for file_number, index in enumerate(range(0, len(ds_filter), args.samples_per_file)): __snake_case : Any = str(data_dir / F"""file-{file_number+1:012}.json""") __snake_case : Any = min(len(ds_filter), index + args.samples_per_file) ds_filter.select(list(range(index, end_index))).to_json(file_path) compress_file(file_path) print(F"""Time to save dataset: {time.time()-t_start:.2f}""")
58
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class A__ ( unittest.TestCase ): '''simple docstring''' def __init__( self: str , _SCREAMING_SNAKE_CASE: Any , _SCREAMING_SNAKE_CASE: List[Any]=7 , _SCREAMING_SNAKE_CASE: Optional[Any]=3 , _SCREAMING_SNAKE_CASE: int=10 , _SCREAMING_SNAKE_CASE: Tuple=18 , _SCREAMING_SNAKE_CASE: Union[str, Any]=30 , _SCREAMING_SNAKE_CASE: Any=400 , _SCREAMING_SNAKE_CASE: List[str]=True , _SCREAMING_SNAKE_CASE: Union[str, Any]=None , _SCREAMING_SNAKE_CASE: str=True , _SCREAMING_SNAKE_CASE: Union[str, Any]=[0.5, 0.5, 0.5] , _SCREAMING_SNAKE_CASE: Any=[0.5, 0.5, 0.5] , _SCREAMING_SNAKE_CASE: Dict=None , ) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : Optional[Any] = size if size is not None else {"shortest_edge": 18} __lowerCAmelCase : int = crop_size if crop_size is not None else {"height": 18, "width": 18} __lowerCAmelCase : Tuple = parent __lowerCAmelCase : List[Any] = batch_size __lowerCAmelCase : List[str] = num_channels __lowerCAmelCase : int = num_frames __lowerCAmelCase : Union[str, Any] = image_size __lowerCAmelCase : Tuple = min_resolution __lowerCAmelCase : Tuple = max_resolution __lowerCAmelCase : str = do_resize __lowerCAmelCase : Optional[int] = size __lowerCAmelCase : Optional[int] = do_normalize __lowerCAmelCase : Dict = image_mean __lowerCAmelCase : List[Any] = image_std __lowerCAmelCase : List[Any] = crop_size def _SCREAMING_SNAKE_CASE ( self: int) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class A__ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE = VivitImageProcessor if is_vision_available() else None def _SCREAMING_SNAKE_CASE ( self: int) -> Tuple: """simple docstring""" __lowerCAmelCase : Optional[int] = VivitImageProcessingTester(self) @property def _SCREAMING_SNAKE_CASE ( self: int) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _SCREAMING_SNAKE_CASE ( self: List[Any]) -> Optional[int]: """simple docstring""" __lowerCAmelCase : int = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "image_mean")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "image_std")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_normalize")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_resize")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_center_crop")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "size")) def _SCREAMING_SNAKE_CASE ( self: Any) -> Optional[Any]: """simple docstring""" __lowerCAmelCase : List[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}) __lowerCAmelCase : Optional[int] = 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 _SCREAMING_SNAKE_CASE ( self: int) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict) # create random PIL videos __lowerCAmelCase : Dict = prepare_video_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE) for video in video_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) self.assertIsInstance(video[0] , Image.Image) # Test not batched input __lowerCAmelCase : Any = 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 __lowerCAmelCase : str = image_processing(_SCREAMING_SNAKE_CASE , 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 _SCREAMING_SNAKE_CASE ( self: List[str]) -> int: """simple docstring""" __lowerCAmelCase : Optional[int] = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors __lowerCAmelCase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE , numpify=_SCREAMING_SNAKE_CASE) for video in video_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) self.assertIsInstance(video[0] , np.ndarray) # Test not batched input __lowerCAmelCase : Any = 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 __lowerCAmelCase : List[str] = image_processing(_SCREAMING_SNAKE_CASE , 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 _SCREAMING_SNAKE_CASE ( self: Dict) -> int: """simple docstring""" __lowerCAmelCase : str = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors __lowerCAmelCase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE , torchify=_SCREAMING_SNAKE_CASE) for video in video_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) self.assertIsInstance(video[0] , torch.Tensor) # Test not batched input __lowerCAmelCase : 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 __lowerCAmelCase : Any = image_processing(_SCREAMING_SNAKE_CASE , 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"], ) , )
58
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_videomae import VideoMAEImageProcessor __A : Dict = logging.get_logger(__name__) class _UpperCAmelCase ( _A ): def __init__( self : int , *A : Tuple , **A : int ) -> None: warnings.warn( '''The class VideoMAEFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use VideoMAEImageProcessor instead.''' , A , ) super().__init__(*A , **A )
33
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _UpperCAmelCase : def __init__( self : int , A : Tuple , A : int=3 , A : List[str]=32 , A : Dict=3 , A : Any=10 , A : Dict=[10, 20, 30, 40] , A : Optional[Any]=[1, 1, 2, 1] , A : Union[str, Any]=True , A : Optional[Any]=True , A : Any="relu" , A : Optional[Any]=3 , A : Tuple=None , ) -> Dict: lowercase_ : str = parent lowercase_ : List[Any] = batch_size lowercase_ : Optional[int] = image_size lowercase_ : int = num_channels lowercase_ : int = embeddings_size lowercase_ : str = hidden_sizes lowercase_ : List[str] = depths lowercase_ : Dict = is_training lowercase_ : int = use_labels lowercase_ : Any = hidden_act lowercase_ : List[Any] = num_labels lowercase_ : Tuple = scope lowercase_ : Optional[Any] = len(A ) def A ( self : str ) -> Tuple: lowercase_ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase_ : Union[str, Any] = None if self.use_labels: lowercase_ : List[str] = ids_tensor([self.batch_size] , self.num_labels ) lowercase_ : Optional[int] = self.get_config() return config, pixel_values, labels def A ( self : Dict ) -> int: return ResNetConfig( 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 , image_size=self.image_size , ) def A ( self : str , A : Tuple , A : str , A : str ) -> str: lowercase_ : str = TFResNetModel(config=A ) lowercase_ : Union[str, 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 : Any , A : int , A : List[Any] , A : Optional[Any] ) -> Optional[Any]: lowercase_ : Tuple = self.num_labels lowercase_ : Union[str, Any] = TFResNetForImageClassification(A ) lowercase_ : Tuple = model(A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def A ( self : Union[str, Any] ) -> Tuple: lowercase_ : Tuple = self.prepare_config_and_inputs() lowercase_ , lowercase_ , lowercase_ : Dict = config_and_inputs lowercase_ : int = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class _UpperCAmelCase ( _A , _A , unittest.TestCase ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () SCREAMING_SNAKE_CASE_ : List[Any] = ( {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE_ : Optional[int] = False SCREAMING_SNAKE_CASE_ : Optional[int] = False SCREAMING_SNAKE_CASE_ : str = False SCREAMING_SNAKE_CASE_ : Optional[int] = False SCREAMING_SNAKE_CASE_ : Any = False def A ( self : Union[str, Any] ) -> List[Any]: lowercase_ : int = TFResNetModelTester(self ) lowercase_ : str = ConfigTester(self , config_class=A , has_text_modality=A ) def A ( self : Dict ) -> Optional[Any]: 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 : Dict ) -> List[Any]: return @unittest.skip(reason='''ResNet does not use inputs_embeds''' ) def A ( self : Any ) -> Any: pass @unittest.skip(reason='''ResNet does not support input and output embeddings''' ) def A ( self : List[str] ) -> Optional[Any]: pass def A ( self : str ) -> Tuple: lowercase_ , lowercase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase_ : int = model_class(A ) lowercase_ : str = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase_ : str = [*signature.parameters.keys()] lowercase_ : str = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , A ) def A ( self : List[str] ) -> Tuple: lowercase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def A ( self : List[Any] ) -> List[str]: def check_hidden_states_output(A : Union[str, Any] , A : int , A : List[Any] ): lowercase_ : int = model_class(A ) lowercase_ : Optional[Any] = model(**self._prepare_for_class(A , A ) ) lowercase_ : Union[str, Any] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowercase_ : Any = self.model_tester.num_stages self.assertEqual(len(A ) , expected_num_stages + 1 ) # ResNet'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] , ) lowercase_ , lowercase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowercase_ : Union[str, Any] = ['''basic''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: lowercase_ : List[str] = layer_type lowercase_ : Tuple = True check_hidden_states_output(A , A , A ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowercase_ : Optional[Any] = True check_hidden_states_output(A , A , A ) def A ( self : Optional[int] ) -> Tuple: lowercase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A ) @slow def A ( self : List[str] ) -> Optional[int]: for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase_ : Tuple = TFResNetModel.from_pretrained(A ) self.assertIsNotNone(A ) def lowercase ( ): lowercase_ : List[str] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class _UpperCAmelCase ( unittest.TestCase ): @cached_property def A ( self : Any ) -> Optional[int]: return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def A ( self : Any ) -> Optional[int]: lowercase_ : Optional[int] = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowercase_ : List[Any] = self.default_image_processor lowercase_ : Dict = prepare_img() lowercase_ : List[str] = image_processor(images=A , return_tensors='''tf''' ) # forward pass lowercase_ : Tuple = model(**A ) # verify the logits lowercase_ : Optional[int] = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , A ) lowercase_ : Optional[Any] = tf.constant([-11.1069, -9.7877, -8.3777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , A , atol=1e-4 ) )
33
1
'''simple docstring''' from .glue import GlueDataset, GlueDataTrainingArguments from .language_modeling import ( LineByLineTextDataset, LineByLineWithRefDataset, LineByLineWithSOPTextDataset, TextDataset, TextDatasetForNextSentencePrediction, ) from .squad import SquadDataset, SquadDataTrainingArguments
251
'''simple docstring''' import argparse import torch from torch import nn from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration def A_( A : List[Any]): UpperCamelCase = [ 'encoder.version', 'decoder.version', 'model.encoder.version', 'model.decoder.version', 'decoder.output_projection.weight', '_float_tensor', 'encoder.embed_positions._float_tensor', 'decoder.embed_positions._float_tensor', ] for k in ignore_keys: state_dict.pop(A , A) def A_( A : Any): UpperCamelCase = list(s_dict.keys()) for key in keys: if "transformer_layers" in key: UpperCamelCase = s_dict.pop(A) elif "subsample" in key: UpperCamelCase = s_dict.pop(A) def A_( A : Optional[int]): UpperCamelCase , UpperCamelCase = emb.weight.shape UpperCamelCase = nn.Linear(A , A , bias=A) UpperCamelCase = emb.weight.data return lin_layer def A_( A : Optional[int] , A : List[str]): UpperCamelCase = torch.load(A , map_location='cpu') UpperCamelCase = mam_aaa['args'] UpperCamelCase = mam_aaa['model'] UpperCamelCase = state_dict['decoder.output_projection.weight'] remove_ignore_keys_(A) rename_keys(A) UpperCamelCase = state_dict['decoder.embed_tokens.weight'].shape[0] UpperCamelCase = args.share_decoder_input_output_embed UpperCamelCase = [int(A) for i in args.conv_kernel_sizes.split(',')] UpperCamelCase = SpeechaTextConfig( vocab_size=A , max_source_positions=args.max_source_positions , max_target_positions=args.max_target_positions , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='relu' , num_conv_layers=len(A) , conv_channels=args.conv_channels , conv_kernel_sizes=A , input_feat_per_channel=args.input_feat_per_channel , input_channels=args.input_channels , tie_word_embeddings=A , num_beams=5 , max_length=200 , use_cache=A , decoder_start_token_id=2 , early_stopping=A , ) UpperCamelCase = SpeechaTextForConditionalGeneration(A) UpperCamelCase , UpperCamelCase = model.model.load_state_dict(A , strict=A) if len(A) > 0 and not set(A) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( 'Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,' f''' but all the following weights are missing {missing}''') if tie_embeds: UpperCamelCase = make_linear_from_emb(model.model.decoder.embed_tokens) else: UpperCamelCase = lm_head_weights model.save_pretrained(A) if __name__ == "__main__": lowerCAmelCase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('--fairseq_path', type=str, help='Path to the fairseq model (.pt) file.') parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') lowerCAmelCase : List[str] = parser.parse_args() convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)
251
1
'''simple docstring''' from random import randint from tempfile import TemporaryFile import numpy as np def UpperCamelCase__ ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" _lowerCAmelCase = 0 if start < end: _lowerCAmelCase = randint(lowerCAmelCase , lowerCAmelCase ) _lowerCAmelCase = a[end] _lowerCAmelCase = a[pivot] _lowerCAmelCase = temp _lowerCAmelCase , _lowerCAmelCase = _in_place_partition(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) count += _in_place_quick_sort(lowerCAmelCase , lowerCAmelCase , p - 1 ) count += _in_place_quick_sort(lowerCAmelCase , p + 1 , lowerCAmelCase ) return count def UpperCamelCase__ ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ): """simple docstring""" _lowerCAmelCase = 0 _lowerCAmelCase = randint(lowerCAmelCase , lowerCAmelCase ) _lowerCAmelCase = a[end] _lowerCAmelCase = a[pivot] _lowerCAmelCase = temp _lowerCAmelCase = start - 1 for index in range(lowerCAmelCase , lowerCAmelCase ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value _lowerCAmelCase = new_pivot_index + 1 _lowerCAmelCase = a[new_pivot_index] _lowerCAmelCase = a[index] _lowerCAmelCase = temp _lowerCAmelCase = a[new_pivot_index + 1] _lowerCAmelCase = a[end] _lowerCAmelCase = temp return new_pivot_index + 1, count A__ : str =TemporaryFile() A__ : Any =1_00 # 1000 elements are to be sorted A__ , A__ : Tuple =0, 1 # mean and standard deviation A__ : Union[str, Any] =np.random.normal(mu, sigma, p) np.save(outfile, X) print('''The array is''') print(X) outfile.seek(0) # using the same array A__ : Optional[Any] =np.load(outfile) A__ : Any =len(M) - 1 A__ : str =_in_place_quick_sort(M, 0, r) print( '''No of Comparisons for 100 elements selected from a standard normal distribution''' '''is :''' ) print(z)
70
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL A__ : List[str] =logging.get_logger(__name__) def UpperCamelCase__ ( lowerCAmelCase ): """simple docstring""" if isinstance(lowerCAmelCase , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(lowerCAmelCase , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(lowerCAmelCase ): return [[videos]] raise ValueError(f"Could not make batched video from {videos}" ) class UpperCAmelCase ( snake_case_ ): _lowercase: Any = ['''pixel_values'''] def __init__( self : Tuple , __snake_case : bool = True , __snake_case : Dict[str, int] = None , __snake_case : PILImageResampling = PILImageResampling.BILINEAR , __snake_case : bool = True , __snake_case : Dict[str, int] = None , __snake_case : bool = True , __snake_case : Union[int, float] = 1 / 2_55 , __snake_case : bool = True , __snake_case : bool = True , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : Optional[Union[float, List[float]]] = None , **__snake_case : str , ) -> None: super().__init__(**__snake_case ) _lowerCAmelCase = size if size is not None else {"""shortest_edge""": 2_56} _lowerCAmelCase = get_size_dict(__snake_case , default_to_square=__snake_case ) _lowerCAmelCase = crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} _lowerCAmelCase = get_size_dict(__snake_case , param_name="""crop_size""" ) _lowerCAmelCase = do_resize _lowerCAmelCase = size _lowerCAmelCase = do_center_crop _lowerCAmelCase = crop_size _lowerCAmelCase = resample _lowerCAmelCase = do_rescale _lowerCAmelCase = rescale_factor _lowerCAmelCase = offset _lowerCAmelCase = do_normalize _lowerCAmelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _lowerCAmelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowercase__ ( self : int , __snake_case : np.ndarray , __snake_case : Dict[str, int] , __snake_case : PILImageResampling = PILImageResampling.BILINEAR , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : Optional[Any] , ) -> np.ndarray: _lowerCAmelCase = get_size_dict(__snake_case , default_to_square=__snake_case ) if "shortest_edge" in size: _lowerCAmelCase = get_resize_output_image_size(__snake_case , size["""shortest_edge"""] , default_to_square=__snake_case ) elif "height" in size and "width" in size: _lowerCAmelCase = (size["""height"""], size["""width"""]) else: raise ValueError(f"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}" ) return resize(__snake_case , size=__snake_case , resample=__snake_case , data_format=__snake_case , **__snake_case ) def lowercase__ ( self : Union[str, Any] , __snake_case : np.ndarray , __snake_case : Dict[str, int] , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : List[Any] , ) -> np.ndarray: _lowerCAmelCase = get_size_dict(__snake_case ) if "height" not in size or "width" not in size: raise ValueError(f"Size must have 'height' and 'width' as keys. Got {size.keys()}" ) return center_crop(__snake_case , size=(size["""height"""], size["""width"""]) , data_format=__snake_case , **__snake_case ) def lowercase__ ( self : Union[str, Any] , __snake_case : np.ndarray , __snake_case : Union[int, float] , __snake_case : bool = True , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : Optional[Any] , ) -> Dict: _lowerCAmelCase = image.astype(np.floataa ) if offset: _lowerCAmelCase = image - (scale / 2) return rescale(__snake_case , scale=__snake_case , data_format=__snake_case , **__snake_case ) def lowercase__ ( self : Optional[int] , __snake_case : np.ndarray , __snake_case : Union[float, List[float]] , __snake_case : Union[float, List[float]] , __snake_case : Optional[Union[str, ChannelDimension]] = None , **__snake_case : Tuple , ) -> np.ndarray: return normalize(__snake_case , mean=__snake_case , std=__snake_case , data_format=__snake_case , **__snake_case ) def lowercase__ ( self : List[Any] , __snake_case : ImageInput , __snake_case : bool = None , __snake_case : Dict[str, int] = None , __snake_case : PILImageResampling = None , __snake_case : bool = None , __snake_case : Dict[str, int] = None , __snake_case : bool = None , __snake_case : float = None , __snake_case : bool = None , __snake_case : bool = None , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : Optional[ChannelDimension] = ChannelDimension.FIRST , ) -> np.ndarray: if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) if offset and not do_rescale: raise ValueError("""For offset, do_rescale must also be set to True.""" ) # All transformations expect numpy arrays. _lowerCAmelCase = to_numpy_array(__snake_case ) if do_resize: _lowerCAmelCase = self.resize(image=__snake_case , size=__snake_case , resample=__snake_case ) if do_center_crop: _lowerCAmelCase = self.center_crop(__snake_case , size=__snake_case ) if do_rescale: _lowerCAmelCase = self.rescale(image=__snake_case , scale=__snake_case , offset=__snake_case ) if do_normalize: _lowerCAmelCase = self.normalize(image=__snake_case , mean=__snake_case , std=__snake_case ) _lowerCAmelCase = to_channel_dimension_format(__snake_case , __snake_case ) return image def lowercase__ ( self : List[Any] , __snake_case : ImageInput , __snake_case : bool = None , __snake_case : Dict[str, int] = None , __snake_case : PILImageResampling = None , __snake_case : bool = None , __snake_case : Dict[str, int] = None , __snake_case : bool = None , __snake_case : float = None , __snake_case : bool = None , __snake_case : bool = None , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : Optional[Union[float, List[float]]] = None , __snake_case : Optional[Union[str, TensorType]] = None , __snake_case : ChannelDimension = ChannelDimension.FIRST , **__snake_case : List[str] , ) -> PIL.Image.Image: _lowerCAmelCase = do_resize if do_resize is not None else self.do_resize _lowerCAmelCase = resample if resample is not None else self.resample _lowerCAmelCase = do_center_crop if do_center_crop is not None else self.do_center_crop _lowerCAmelCase = do_rescale if do_rescale is not None else self.do_rescale _lowerCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor _lowerCAmelCase = offset if offset is not None else self.offset _lowerCAmelCase = do_normalize if do_normalize is not None else self.do_normalize _lowerCAmelCase = image_mean if image_mean is not None else self.image_mean _lowerCAmelCase = image_std if image_std is not None else self.image_std _lowerCAmelCase = size if size is not None else self.size _lowerCAmelCase = get_size_dict(__snake_case , default_to_square=__snake_case ) _lowerCAmelCase = crop_size if crop_size is not None else self.crop_size _lowerCAmelCase = get_size_dict(__snake_case , param_name="""crop_size""" ) if not valid_images(__snake_case ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) _lowerCAmelCase = make_batched(__snake_case ) _lowerCAmelCase = [ [ self._preprocess_image( image=__snake_case , do_resize=__snake_case , size=__snake_case , resample=__snake_case , do_center_crop=__snake_case , crop_size=__snake_case , do_rescale=__snake_case , rescale_factor=__snake_case , offset=__snake_case , do_normalize=__snake_case , image_mean=__snake_case , image_std=__snake_case , data_format=__snake_case , ) for img in video ] for video in videos ] _lowerCAmelCase = {"""pixel_values""": videos} return BatchFeature(data=__snake_case , tensor_type=__snake_case )
70
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __A = logging.get_logger(__name__) __A = { "microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json", "microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json", } class UpperCAmelCase (_UpperCAmelCase ): """simple docstring""" _UpperCAmelCase :Tuple = "markuplm" def __init__( self , _UpperCAmelCase=30522 , _UpperCAmelCase=768 , _UpperCAmelCase=12 , _UpperCAmelCase=12 , _UpperCAmelCase=3072 , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.1 , _UpperCAmelCase=512 , _UpperCAmelCase=2 , _UpperCAmelCase=0.02 , _UpperCAmelCase=1e-1_2 , _UpperCAmelCase=0 , _UpperCAmelCase=0 , _UpperCAmelCase=2 , _UpperCAmelCase=256 , _UpperCAmelCase=1024 , _UpperCAmelCase=216 , _UpperCAmelCase=1001 , _UpperCAmelCase=32 , _UpperCAmelCase=50 , _UpperCAmelCase="absolute" , _UpperCAmelCase=True , _UpperCAmelCase=None , **_UpperCAmelCase , ): super().__init__( pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase , ) lowercase__: List[str] = vocab_size lowercase__: Any = hidden_size lowercase__: int = num_hidden_layers lowercase__: Union[str, Any] = num_attention_heads lowercase__: Optional[int] = hidden_act lowercase__: Dict = intermediate_size lowercase__: Dict = hidden_dropout_prob lowercase__: str = attention_probs_dropout_prob lowercase__: Any = max_position_embeddings lowercase__: Tuple = type_vocab_size lowercase__: Optional[int] = initializer_range lowercase__: Dict = layer_norm_eps lowercase__: List[str] = position_embedding_type lowercase__: Dict = use_cache lowercase__: int = classifier_dropout # additional properties lowercase__: str = max_depth lowercase__: Optional[Any] = max_xpath_tag_unit_embeddings lowercase__: List[Any] = max_xpath_subs_unit_embeddings lowercase__: List[str] = tag_pad_id lowercase__: Union[str, Any] = subs_pad_id lowercase__: Optional[int] = xpath_unit_hidden_size
2
"""simple docstring""" import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": __A = argparse.ArgumentParser() parser.add_argument( "--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert." ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( "--original_config_file", default=None, type=str, help="The YAML config file corresponding to the original architecture.", ) parser.add_argument( "--num_in_channels", default=None, type=int, help="The number of input channels. If `None` number of input channels will be automatically inferred.", ) parser.add_argument( "--scheduler_type", default="pndm", type=str, help="Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']", ) parser.add_argument( "--pipeline_type", default=None, type=str, help=( "The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'" ". If `None` pipeline will be automatically inferred." ), ) parser.add_argument( "--image_size", default=None, type=int, help=( "The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2" " Base. Use 768 for Stable Diffusion v2." ), ) parser.add_argument( "--prediction_type", default=None, type=str, help=( "The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable" " Diffusion v2 Base. Use 'v_prediction' for Stable Diffusion v2." ), ) parser.add_argument( "--extract_ema", action="store_true", help=( "Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights" " or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield" " higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning." ), ) parser.add_argument( "--upcast_attention", action="store_true", help=( "Whether the attention computation should always be upcasted. This is necessary when running stable" " diffusion 2.1." ), ) parser.add_argument( "--from_safetensors", action="store_true", help="If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.", ) parser.add_argument( "--to_safetensors", action="store_true", help="Whether to store pipeline in safetensors format or not.", ) parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)") parser.add_argument( "--stable_unclip", type=str, default=None, required=False, help="Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.", ) parser.add_argument( "--stable_unclip_prior", type=str, default=None, required=False, help="Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.", ) parser.add_argument( "--clip_stats_path", type=str, help="Path to the clip stats file. Only required if the stable unclip model's config specifies `model.params.noise_aug_config.params.clip_stats_path`.", required=False, ) parser.add_argument( "--controlnet", action="store_true", default=None, help="Set flag if this is a controlnet checkpoint." ) parser.add_argument("--half", action="store_true", help="Save weights in half precision.") parser.add_argument( "--vae_path", type=str, default=None, required=False, help="Set to a path, hub id to an already converted vae to not convert it again.", ) __A = parser.parse_args() __A = download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
2
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _lowerCAmelCase :int = { 'configuration_mobilevit': ['MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MobileViTConfig', 'MobileViTOnnxConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase :Union[str, Any] = ['MobileViTFeatureExtractor'] _lowerCAmelCase :List[Any] = ['MobileViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase :Union[str, Any] = [ 'MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MobileViTForImageClassification', 'MobileViTForSemanticSegmentation', 'MobileViTModel', 'MobileViTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase :Optional[int] = [ 'TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFMobileViTForImageClassification', 'TFMobileViTForSemanticSegmentation', 'TFMobileViTModel', 'TFMobileViTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilevit import MobileViTFeatureExtractor from .image_processing_mobilevit import MobileViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilevit import ( MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel, MobileViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilevit import ( TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel, TFMobileViTPreTrainedModel, ) else: import sys _lowerCAmelCase :str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
263
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { 'kssteven/ibert-roberta-base': 'https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json', 'kssteven/ibert-roberta-large': 'https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json', 'kssteven/ibert-roberta-large-mnli': ( 'https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json' ), } class __SCREAMING_SNAKE_CASE ( lowerCamelCase ): snake_case_ = """ibert""" def __init__( self : Optional[int] , __lowercase : List[str]=3_05_22 , __lowercase : Tuple=7_68 , __lowercase : str=12 , __lowercase : Optional[int]=12 , __lowercase : Optional[Any]=30_72 , __lowercase : str="gelu" , __lowercase : List[str]=0.1 , __lowercase : List[Any]=0.1 , __lowercase : List[str]=5_12 , __lowercase : str=2 , __lowercase : Tuple=0.02 , __lowercase : Union[str, Any]=1e-12 , __lowercase : List[Any]=1 , __lowercase : List[str]=0 , __lowercase : Optional[Any]=2 , __lowercase : int="absolute" , __lowercase : Tuple=False , __lowercase : int="none" , **__lowercase : Optional[Any] , ) -> List[Any]: super().__init__(pad_token_id=__lowercase , bos_token_id=__lowercase , eos_token_id=__lowercase , **__lowercase ) SCREAMING_SNAKE_CASE__ : Any =vocab_size SCREAMING_SNAKE_CASE__ : Dict =hidden_size SCREAMING_SNAKE_CASE__ : str =num_hidden_layers SCREAMING_SNAKE_CASE__ : Union[str, Any] =num_attention_heads SCREAMING_SNAKE_CASE__ : Tuple =hidden_act SCREAMING_SNAKE_CASE__ : List[str] =intermediate_size SCREAMING_SNAKE_CASE__ : Union[str, Any] =hidden_dropout_prob SCREAMING_SNAKE_CASE__ : int =attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ : Union[str, Any] =max_position_embeddings SCREAMING_SNAKE_CASE__ : Dict =type_vocab_size SCREAMING_SNAKE_CASE__ : Tuple =initializer_range SCREAMING_SNAKE_CASE__ : str =layer_norm_eps SCREAMING_SNAKE_CASE__ : Tuple =position_embedding_type SCREAMING_SNAKE_CASE__ : Any =quant_mode SCREAMING_SNAKE_CASE__ : Optional[int] =force_dequant class __SCREAMING_SNAKE_CASE ( lowerCamelCase ): @property def __magic_name__ ( self : str ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": SCREAMING_SNAKE_CASE__ : str ={0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE__ : Any ={0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
152
0
"""simple docstring""" def UpperCAmelCase__ (snake_case__ : List[Any] ): """simple docstring""" if not head: return True # split the list to two parts _snake_case : Tuple = head.next, head while fast and fast.next: _snake_case : Dict = fast.next.next _snake_case : List[Any] = slow.next _snake_case : Optional[int] = slow.next _snake_case : List[str] = None # Don't forget here! But forget still works! # reverse the second part _snake_case : Optional[Any] = None while second: _snake_case : str = second.next _snake_case : List[str] = node _snake_case : Union[str, Any] = second _snake_case : Tuple = nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False _snake_case : Dict = node.next _snake_case : Union[str, Any] = head.next return True def UpperCAmelCase__ (snake_case__ : Optional[Any] ): """simple docstring""" if not head or not head.next: return True # 1. Get the midpoint (slow) _snake_case : Tuple = head while fast and fast.next: _snake_case : Any = fast.next.next, slow.next # 2. Push the second half into the stack _snake_case : str = [slow.val] while slow.next: _snake_case : int = slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False _snake_case : Tuple = cur.next return True def UpperCAmelCase__ (snake_case__ : int ): """simple docstring""" if not head or not head.next: return True _snake_case : Union[str, Any] = {} _snake_case : int = 0 while head: if head.val in d: d[head.val].append(snake_case__ ) else: _snake_case : List[Any] = [pos] _snake_case : Any = head.next pos += 1 _snake_case : List[Any] = pos - 1 _snake_case : Dict = 0 for v in d.values(): if len(snake_case__ ) % 2 != 0: middle += 1 else: _snake_case : int = 0 for i in range(0 , len(snake_case__ ) ): if v[i] + v[len(snake_case__ ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
361
"""simple docstring""" def UpperCAmelCase__ (snake_case__ : int = 10_00 ): """simple docstring""" _snake_case , _snake_case : List[Any] = 1, 1 _snake_case : str = [] for i in range(1 , n + 1 ): _snake_case : Any = prev_numerator + 2 * prev_denominator _snake_case : Optional[Any] = prev_numerator + prev_denominator if len(str(snake_case__ ) ) > len(str(snake_case__ ) ): result.append(snake_case__ ) _snake_case : int = numerator _snake_case : Any = denominator return len(snake_case__ ) if __name__ == "__main__": print(F'''{solution() = }''')
132
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging A =logging.get_logger(__name__) A ={} class _a ( __a ): __a : List[Any] = """llama""" __a : Optional[int] = ["""past_key_values"""] def __init__( self : Dict , lowercase : int=32_000 , lowercase : List[str]=4_096 , lowercase : Optional[int]=11_008 , lowercase : Optional[Any]=32 , lowercase : Tuple=32 , lowercase : List[str]=None , lowercase : Dict="silu" , lowercase : Tuple=2_048 , lowercase : List[str]=0.02 , lowercase : int=1E-6 , lowercase : Any=True , lowercase : Union[str, Any]=0 , lowercase : int=1 , lowercase : List[str]=2 , lowercase : Optional[int]=1 , lowercase : List[Any]=False , lowercase : Any=None , **lowercase : Optional[int] , ): '''simple docstring''' UpperCAmelCase = vocab_size UpperCAmelCase = max_position_embeddings UpperCAmelCase = hidden_size UpperCAmelCase = intermediate_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads # for backward compatibility if num_key_value_heads is None: UpperCAmelCase = num_attention_heads UpperCAmelCase = num_key_value_heads UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = rms_norm_eps UpperCAmelCase = pretraining_tp UpperCAmelCase = use_cache UpperCAmelCase = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=__UpperCamelCase , bos_token_id=__UpperCamelCase , eos_token_id=__UpperCamelCase , tie_word_embeddings=__UpperCamelCase , **__UpperCamelCase , ) def A ( self : Tuple ): '''simple docstring''' if self.rope_scaling is None: return if not isinstance(self.rope_scaling , __UpperCamelCase ) or len(self.rope_scaling ) != 2: raise ValueError( '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ''' f"got {self.rope_scaling}" ) UpperCAmelCase = self.rope_scaling.get('''type''' , __UpperCamelCase ) UpperCAmelCase = self.rope_scaling.get('''factor''' , __UpperCamelCase ) 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(__UpperCamelCase , __UpperCamelCase ) or rope_scaling_factor <= 1.0: raise ValueError(f"`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}" )
34
"""simple docstring""" from __future__ import annotations import math def lowercase ( _SCREAMING_SNAKE_CASE : 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(_SCREAMING_SNAKE_CASE ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowercase ( _SCREAMING_SNAKE_CASE : int ): '''simple docstring''' _UpperCAmelCase = str(_SCREAMING_SNAKE_CASE ) _UpperCAmelCase = [n] for i in range(1 , len(_SCREAMING_SNAKE_CASE ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def lowercase ( _SCREAMING_SNAKE_CASE : int ): '''simple docstring''' if len(str(_SCREAMING_SNAKE_CASE ) ) > 3: if not is_prime(int(str(_SCREAMING_SNAKE_CASE )[-3:] ) ) or not is_prime(int(str(_SCREAMING_SNAKE_CASE )[:3] ) ): return False return True def lowercase ( _SCREAMING_SNAKE_CASE : int = 11 ): '''simple docstring''' _UpperCAmelCase = [] _UpperCAmelCase = 13 while len(_SCREAMING_SNAKE_CASE ) != count: if validate(_SCREAMING_SNAKE_CASE ): _UpperCAmelCase = list_truncated_nums(_SCREAMING_SNAKE_CASE ) if all(is_prime(_SCREAMING_SNAKE_CASE ) for i in list_nums ): list_truncated_primes.append(_SCREAMING_SNAKE_CASE ) num += 2 return list_truncated_primes def lowercase ( ): '''simple docstring''' return sum(compute_truncated_primes(11 ) ) if __name__ == "__main__": print(f'''{sum(compute_truncated_primes(11)) = }''')
260
0
"""simple docstring""" import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def lowerCamelCase_ ( _lowerCamelCase ): lowerCamelCase__ : Dict = np.inf def set_batch_size(_lowerCamelCase ) -> None: nonlocal batch_size if isinstance(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = min(_lowerCamelCase , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : List[str] = min(_lowerCamelCase , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(_lowerCamelCase , _lowerCamelCase ) and feature.dtype == "binary": lowerCamelCase__ : str = min(_lowerCamelCase , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(_lowerCamelCase , _lowerCamelCase ) return None if batch_size is np.inf else batch_size class a_ ( snake_case_ ): '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_ = None, lowerCamelCase_ = None, lowerCamelCase_ = None, lowerCamelCase_ = False, lowerCamelCase_ = False, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' super().__init__( lowerCamelCase_, split=lowerCamelCase_, features=lowerCamelCase_, cache_dir=lowerCamelCase_, keep_in_memory=lowerCamelCase_, streaming=lowerCamelCase_, num_proc=lowerCamelCase_, **lowerCamelCase_, ) lowerCamelCase__ : str = path_or_paths if isinstance(lowerCamelCase_, lowerCamelCase_ ) else {self.split: path_or_paths} lowerCamelCase__ : Optional[Any] = _PACKAGED_DATASETS_MODULES['parquet'][1] lowerCamelCase__ : List[Any] = Parquet( cache_dir=lowerCamelCase_, data_files=lowerCamelCase_, features=lowerCamelCase_, hash=lowerCamelCase_, **lowerCamelCase_, ) def a__ (self ): '''simple docstring''' if self.streaming: lowerCamelCase__ : Any = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: lowerCamelCase__ : str = None lowerCamelCase__ : str = None lowerCamelCase__ : Any = None lowerCamelCase__ : Union[str, Any] = None self.builder.download_and_prepare( download_config=lowerCamelCase_, download_mode=lowerCamelCase_, verification_mode=lowerCamelCase_, base_path=lowerCamelCase_, num_proc=self.num_proc, ) lowerCamelCase__ : List[str] = self.builder.as_dataset( split=self.split, verification_mode=lowerCamelCase_, in_memory=self.keep_in_memory ) return dataset class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ = None, **lowerCamelCase_, ): '''simple docstring''' lowerCamelCase__ : Optional[int] = dataset lowerCamelCase__ : List[Any] = path_or_buf lowerCamelCase__ : Tuple = batch_size or get_writer_batch_size(dataset.features ) lowerCamelCase__ : str = parquet_writer_kwargs def a__ (self ): '''simple docstring''' lowerCamelCase__ : Tuple = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf, (str, bytes, os.PathLike) ): with open(self.path_or_buf, 'wb+' ) as buffer: lowerCamelCase__ : List[str] = self._write(file_obj=lowerCamelCase_, batch_size=lowerCamelCase_, **self.parquet_writer_kwargs ) else: lowerCamelCase__ : int = self._write(file_obj=self.path_or_buf, batch_size=lowerCamelCase_, **self.parquet_writer_kwargs ) return written def a__ (self, lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Any = 0 lowerCamelCase__ : List[str] = parquet_writer_kwargs.pop('path_or_buf', lowerCamelCase_ ) lowerCamelCase__ : str = self.dataset.features.arrow_schema lowerCamelCase__ : str = pq.ParquetWriter(lowerCamelCase_, schema=lowerCamelCase_, **lowerCamelCase_ ) for offset in logging.tqdm( range(0, len(self.dataset ), lowerCamelCase_ ), unit='ba', disable=not logging.is_progress_bar_enabled(), desc='Creating parquet from Arrow format', ): lowerCamelCase__ : List[Any] = query_table( table=self.dataset._data, key=slice(lowerCamelCase_, offset + batch_size ), indices=self.dataset._indices if self.dataset._indices is not None else None, ) writer.write_table(lowerCamelCase_ ) written += batch.nbytes writer.close() return written
316
"""simple docstring""" print((lambda quine: quine % quine)("print((lambda quine: quine %% quine)(%r))"))
316
1
import enum import warnings from .. import MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING from ..utils import add_end_docstrings, is_tf_available from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf class snake_case__( enum.Enum ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = 0 SCREAMING_SNAKE_CASE__ : Dict = 1 SCREAMING_SNAKE_CASE__ : Tuple = 2 @add_end_docstrings(__UpperCAmelCase ) class snake_case__( __UpperCAmelCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = """ In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision and denounces one of the men as a horse thief. Although his father initially slaps him for making such an accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop, begging for his blessing. <eod> </s> <eos> """ def __init__( self , *__lowercase , **__lowercase ) -> List[Any]: super().__init__(*_lowerCamelCase , **_lowerCamelCase ) self.check_model_type( TF_MODEL_FOR_CAUSAL_LM_MAPPING if self.framework == '''tf''' else MODEL_FOR_CAUSAL_LM_MAPPING ) if "prefix" not in self._preprocess_params: # This is very specific. The logic is quite complex and needs to be done # as a "default". # It also defines both some preprocess_kwargs and generate_kwargs # which is why we cannot put them in their respective methods. lowerCAmelCase_ : List[str] = None if self.model.config.prefix is not None: lowerCAmelCase_ : Union[str, Any] = self.model.config.prefix if prefix is None and self.model.__class__.__name__ in [ "XLNetLMHeadModel", "TransfoXLLMHeadModel", "TFXLNetLMHeadModel", "TFTransfoXLLMHeadModel", ]: # For XLNet and TransformerXL we add an article to the prompt to give more state to the model. lowerCAmelCase_ : List[Any] = self.XL_PREFIX if prefix is not None: # Recalculate some generate_kwargs linked to prefix. lowerCAmelCase_ : List[str] = self._sanitize_parameters(prefix=_lowerCamelCase , **self._forward_params ) lowerCAmelCase_ : Any = {**self._preprocess_params, **preprocess_params} lowerCAmelCase_ : List[str] = {**self._forward_params, **forward_params} def lowercase_ ( self , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , **__lowercase , ) -> Union[str, Any]: lowerCAmelCase_ : int = {} if prefix is not None: lowerCAmelCase_ : str = prefix if prefix: lowerCAmelCase_ : List[Any] = self.tokenizer( _lowerCamelCase , padding=_lowerCamelCase , add_special_tokens=_lowerCamelCase , return_tensors=self.framework ) lowerCAmelCase_ : Optional[int] = prefix_inputs['''input_ids'''].shape[-1] if handle_long_generation is not None: if handle_long_generation not in {"hole"}: raise ValueError( f"""{handle_long_generation} is not a valid value for `handle_long_generation` parameter expected""" ''' [None, \'hole\']''' ) lowerCAmelCase_ : Dict = handle_long_generation preprocess_params.update(_lowerCamelCase ) lowerCAmelCase_ : Optional[Any] = generate_kwargs lowerCAmelCase_ : List[Any] = {} if return_full_text is not None and return_type is None: if return_text is not None: raise ValueError('''`return_text` is mutually exclusive with `return_full_text`''' ) if return_tensors is not None: raise ValueError('''`return_full_text` is mutually exclusive with `return_tensors`''' ) lowerCAmelCase_ : Union[str, Any] = ReturnType.FULL_TEXT if return_full_text else ReturnType.NEW_TEXT if return_tensors is not None and return_type is None: if return_text is not None: raise ValueError('''`return_text` is mutually exclusive with `return_tensors`''' ) lowerCAmelCase_ : Tuple = ReturnType.TENSORS if return_type is not None: lowerCAmelCase_ : Optional[Any] = return_type if clean_up_tokenization_spaces is not None: lowerCAmelCase_ : List[str] = clean_up_tokenization_spaces if stop_sequence is not None: lowerCAmelCase_ : List[Any] = self.tokenizer.encode(_lowerCamelCase , add_special_tokens=_lowerCamelCase ) if len(_lowerCamelCase ) > 1: warnings.warn( '''Stopping on a multiple token sequence is not yet supported on transformers. The first token of''' ''' the stop sequence will be used as the stop sequence string in the interim.''' ) lowerCAmelCase_ : Any = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def lowercase_ ( self , *__lowercase , **__lowercase ) -> Any: if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]: kwargs.update({'''add_space_before_punct_symbol''': True} ) return super()._parse_and_tokenize(*_lowerCamelCase , **_lowerCamelCase ) def __call__( self , __lowercase , **__lowercase ) -> Tuple: return super().__call__(_lowerCamelCase , **_lowerCamelCase ) def lowercase_ ( self , __lowercase , __lowercase="" , __lowercase=None , **__lowercase ) -> Any: lowerCAmelCase_ : Any = self.tokenizer( prefix + prompt_text , padding=_lowerCamelCase , add_special_tokens=_lowerCamelCase , return_tensors=self.framework ) lowerCAmelCase_ : Any = prompt_text if handle_long_generation == "hole": lowerCAmelCase_ : int = inputs['''input_ids'''].shape[-1] if "max_new_tokens" in generate_kwargs: lowerCAmelCase_ : Optional[Any] = generate_kwargs['''max_new_tokens'''] else: lowerCAmelCase_ : str = generate_kwargs.get('''max_length''' , self.model.config.max_length ) - cur_len if new_tokens < 0: raise ValueError('''We cannot infer how many new tokens are expected''' ) if cur_len + new_tokens > self.tokenizer.model_max_length: lowerCAmelCase_ : List[Any] = self.tokenizer.model_max_length - new_tokens if keep_length <= 0: raise ValueError( '''We cannot use `hole` to handle this generation the number of desired tokens exceeds the''' ''' models max length''' ) lowerCAmelCase_ : Any = inputs['''input_ids'''][:, -keep_length:] if "attention_mask" in inputs: lowerCAmelCase_ : Optional[Any] = inputs['''attention_mask'''][:, -keep_length:] return inputs def lowercase_ ( self , __lowercase , **__lowercase ) -> Any: lowerCAmelCase_ : List[Any] = model_inputs['''input_ids'''] lowerCAmelCase_ : List[Any] = model_inputs.get('''attention_mask''' , _lowerCamelCase ) # Allow empty prompts if input_ids.shape[1] == 0: lowerCAmelCase_ : Optional[Any] = None lowerCAmelCase_ : Optional[Any] = None lowerCAmelCase_ : Dict = 1 else: lowerCAmelCase_ : int = input_ids.shape[0] lowerCAmelCase_ : str = model_inputs.pop('''prompt_text''' ) # If there is a prefix, we may need to adjust the generation length. Do so without permanently modifying # generate_kwargs, as some of the parameterization may come from the initialization of the pipeline. lowerCAmelCase_ : List[Any] = generate_kwargs.pop('''prefix_length''' , 0 ) if prefix_length > 0: lowerCAmelCase_ : Optional[Any] = '''max_new_tokens''' in generate_kwargs or ( '''generation_config''' in generate_kwargs and generate_kwargs['''generation_config'''].max_new_tokens is not None ) if not has_max_new_tokens: lowerCAmelCase_ : int = generate_kwargs.get('''max_length''' ) or self.model.config.max_length generate_kwargs["max_length"] += prefix_length lowerCAmelCase_ : List[Any] = '''min_new_tokens''' in generate_kwargs or ( '''generation_config''' in generate_kwargs and generate_kwargs['''generation_config'''].min_new_tokens is not None ) if not has_min_new_tokens and "min_length" in generate_kwargs: generate_kwargs["min_length"] += prefix_length # BS x SL lowerCAmelCase_ : int = self.model.generate(input_ids=_lowerCamelCase , attention_mask=_lowerCamelCase , **_lowerCamelCase ) lowerCAmelCase_ : List[str] = generated_sequence.shape[0] if self.framework == "pt": lowerCAmelCase_ : Optional[Any] = generated_sequence.reshape(_lowerCamelCase , out_b // in_b , *generated_sequence.shape[1:] ) elif self.framework == "tf": lowerCAmelCase_ : Union[str, Any] = tf.reshape(_lowerCamelCase , (in_b, out_b // in_b, *generated_sequence.shape[1:]) ) return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text} def lowercase_ ( self , __lowercase , __lowercase=ReturnType.FULL_TEXT , __lowercase=True ) -> List[str]: lowerCAmelCase_ : List[Any] = model_outputs['''generated_sequence'''][0] lowerCAmelCase_ : int = model_outputs['''input_ids'''] lowerCAmelCase_ : Union[str, Any] = model_outputs['''prompt_text'''] lowerCAmelCase_ : List[Any] = generated_sequence.numpy().tolist() lowerCAmelCase_ : str = [] for sequence in generated_sequence: if return_type == ReturnType.TENSORS: lowerCAmelCase_ : Optional[Any] = {'''generated_token_ids''': sequence} elif return_type in {ReturnType.NEW_TEXT, ReturnType.FULL_TEXT}: # Decode text lowerCAmelCase_ : List[str] = self.tokenizer.decode( _lowerCamelCase , skip_special_tokens=_lowerCamelCase , clean_up_tokenization_spaces=_lowerCamelCase , ) # Remove PADDING prompt of the sequence if XLNet or Transfo-XL model is used if input_ids is None: lowerCAmelCase_ : str = 0 else: lowerCAmelCase_ : Optional[int] = len( self.tokenizer.decode( input_ids[0] , skip_special_tokens=_lowerCamelCase , clean_up_tokenization_spaces=_lowerCamelCase , ) ) if return_type == ReturnType.FULL_TEXT: lowerCAmelCase_ : Tuple = prompt_text + text[prompt_length:] else: lowerCAmelCase_ : Any = text[prompt_length:] lowerCAmelCase_ : Any = {'''generated_text''': all_text} records.append(_lowerCamelCase ) return records
262
"""simple docstring""" import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : Dict = logging.get_logger(__name__) _lowerCamelCase : Optional[Any] = ['model.decoder.embed_positions.weights'] def lowercase_ ( _UpperCAmelCase ): """simple docstring""" if "emb" in name: A_ : Tuple = name.replace('''emb''' , '''model.decoder.embed_tokens''' ) if "transformer" in name: A_ : Optional[int] = name.replace('''transformer''' , '''model.decoder''' ) if "cross_attention" in name: A_ : Optional[Any] = name.replace('''cross_attention''' , '''encoder_attn''' ) if "linear1" in name: A_ : int = name.replace('''linear1''' , '''fc1''' ) if "linear2" in name: A_ : Optional[int] = name.replace('''linear2''' , '''fc2''' ) if "norm1" in name: A_ : Any = name.replace('''norm1''' , '''self_attn_layer_norm''' ) if "norm_cross" in name: A_ : Any = name.replace('''norm_cross''' , '''encoder_attn_layer_norm''' ) if "norm2" in name: A_ : Dict = name.replace('''norm2''' , '''final_layer_norm''' ) if "out_norm" in name: A_ : Tuple = name.replace('''out_norm''' , '''model.decoder.layer_norm''' ) if "linears" in name: A_ : Union[str, Any] = name.replace('''linears''' , '''lm_heads''' ) if "condition_provider.conditioners.description.output_proj" in name: A_ : Tuple = name.replace('''condition_provider.conditioners.description.output_proj''' , '''enc_to_dec_proj''' ) return name def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase ): """simple docstring""" A_ : List[Any] = list(state_dict.keys() ) A_ : List[Any] = {} for key in keys: A_ : List[str] = state_dict.pop(_UpperCAmelCase ) A_ : Tuple = rename_keys(_UpperCAmelCase ) if "in_proj_weight" in key: # split fused qkv proj A_ : Any = val[:hidden_size, :] A_ : Optional[int] = val[hidden_size : 2 * hidden_size, :] A_ : Union[str, Any] = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: A_ : List[str] = val else: A_ : int = val return state_dict, enc_dec_proj_state_dict def lowercase_ ( _UpperCAmelCase ): """simple docstring""" if checkpoint == "small": # default config values A_ : Optional[Any] = 1024 A_ : Tuple = 24 A_ : int = 16 elif checkpoint == "medium": A_ : Any = 1536 A_ : Union[str, Any] = 48 A_ : List[Any] = 24 elif checkpoint == "large": A_ : Optional[int] = 2048 A_ : Optional[int] = 48 A_ : Tuple = 32 else: raise ValueError(f"""Checkpoint should be one of `['small', 'medium', 'large']`, got {checkpoint}.""" ) A_ : Tuple = MusicgenDecoderConfig( hidden_size=_UpperCAmelCase , ffn_dim=hidden_size * 4 , num_hidden_layers=_UpperCAmelCase , num_attention_heads=_UpperCAmelCase , ) return config @torch.no_grad() def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="cpu" ): """simple docstring""" A_ : Any = MusicGen.get_pretrained(_UpperCAmelCase , device=_UpperCAmelCase ) A_ : str = decoder_config_from_checkpoint(_UpperCAmelCase ) A_ : Optional[int] = fairseq_model.lm.state_dict() A_ , A_ : str = rename_state_dict( _UpperCAmelCase , hidden_size=decoder_config.hidden_size ) A_ : List[str] = TaEncoderModel.from_pretrained('''t5-base''' ) A_ : Tuple = EncodecModel.from_pretrained('''facebook/encodec_32khz''' ) A_ : Union[str, Any] = MusicgenForCausalLM(_UpperCAmelCase ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection A_ , A_ : Tuple = decoder.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) for key in missing_keys.copy(): if key.startswith(('''text_encoder''', '''audio_encoder''') ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(_UpperCAmelCase ) if len(_UpperCAmelCase ) > 0: raise ValueError(f"""Missing key(s) in state_dict: {missing_keys}""" ) if len(_UpperCAmelCase ) > 0: raise ValueError(f"""Unexpected key(s) in state_dict: {unexpected_keys}""" ) # init the composite model A_ : Tuple = MusicgenForConditionalGeneration(text_encoder=_UpperCAmelCase , audio_encoder=_UpperCAmelCase , decoder=_UpperCAmelCase ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(_UpperCAmelCase ) # check we can do a forward pass A_ : List[str] = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) A_ : Union[str, Any] = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): A_ : Tuple = model(input_ids=_UpperCAmelCase , decoder_input_ids=_UpperCAmelCase ).logits if logits.shape != (8, 1, 2048): raise ValueError('''Incorrect shape for logits''' ) # now construct the processor A_ : str = AutoTokenizer.from_pretrained('''t5-base''' ) A_ : int = AutoFeatureExtractor.from_pretrained('''facebook/encodec_32khz''' , padding_side='''left''' ) A_ : Optional[int] = MusicgenProcessor(feature_extractor=_UpperCAmelCase , tokenizer=_UpperCAmelCase ) # set the appropriate bos/pad token ids A_ : Tuple = 2048 A_ : Union[str, Any] = 2048 # set other default generation config params A_ : Union[str, Any] = int(30 * audio_encoder.config.frame_rate ) A_ : List[str] = True A_ : List[str] = 3.0 if pytorch_dump_folder is not None: Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) logger.info(f"""Saving model {checkpoint} to {pytorch_dump_folder}""" ) model.save_pretrained(_UpperCAmelCase ) processor.save_pretrained(_UpperCAmelCase ) if repo_id: logger.info(f"""Pushing model {checkpoint} to {repo_id}""" ) model.push_to_hub(_UpperCAmelCase ) processor.push_to_hub(_UpperCAmelCase ) if __name__ == "__main__": _lowerCamelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint', default='small', type=str, help='Checkpoint size of the MusicGen model you\'d like to convert. Can be one of: `[\'small\', \'medium\', \'large\']`.', ) parser.add_argument( '--pytorch_dump_folder', required=True, default=None, type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', default=None, type=str, help='Where to upload the converted model on the 🤗 hub.' ) parser.add_argument( '--device', default='cpu', type=str, help='Torch device to run the conversion, either cpu or cuda.' ) _lowerCamelCase : Optional[Any] = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
167
0
"""simple docstring""" # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( '''stable diffusion controlnet''', '''0.22.0''', '''Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.''', standard_warn=False, stacklevel=3, )
309
"""simple docstring""" import os from collections.abc import Iterator def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : str = "." ): for dir_path, dir_names, filenames in os.walk(_UpperCAmelCase ): lowerCAmelCase = [d for d in dir_names if d != 'scripts' and d[0] not in '._'] for filename in filenames: if filename == "__init__.py": continue if os.path.splitext(_UpperCAmelCase )[1] in (".py", ".ipynb"): yield os.path.join(_UpperCAmelCase , _UpperCAmelCase ).lstrip('./' ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : int ): return F'{i * " "}*' if i else "\n##" def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : str , _UpperCAmelCase : str ): lowerCAmelCase = old_path.split(os.sep ) for i, new_part in enumerate(new_path.split(os.sep ) ): if (i + 1 > len(_UpperCAmelCase ) or old_parts[i] != new_part) and new_part: print(F'{md_prefix(_UpperCAmelCase )} {new_part.replace("_" , " " ).title()}' ) return new_path def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : str = "." ): lowerCAmelCase = '' for filepath in sorted(good_file_paths(_UpperCAmelCase ) ): lowerCAmelCase ,lowerCAmelCase = os.path.split(_UpperCAmelCase ) if filepath != old_path: lowerCAmelCase = print_path(_UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase = (filepath.count(os.sep ) + 1) if filepath else 0 lowerCAmelCase = F'{filepath}/{filename}'.replace(' ' , '%20' ) lowerCAmelCase = os.path.splitext(filename.replace('_' , ' ' ).title() )[0] print(F'{md_prefix(_UpperCAmelCase )} [{filename}]({url})' ) if __name__ == "__main__": print_directory_md('''.''')
309
1
"""simple docstring""" from abc import ABC, abstractmethod from typing import List, Optional class A__ ( _lowerCamelCase): def __init__( self ): # test for the above condition self.test() def __lowerCamelCase ( self ): __lowerCAmelCase : Optional[int] = 0 __lowerCAmelCase : List[Any] = False while not completed: if counter == 1: self.reset() __lowerCAmelCase : str = self.advance() if not self.does_advance(_SCREAMING_SNAKE_CASE ): raise Exception( 'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.' ) __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase : int = self.update(_SCREAMING_SNAKE_CASE ) counter += 1 if counter > 1_00_00: raise Exception('update() does not fulfill the constraint.' ) if self.remaining() != 0: raise Exception('Custom Constraint is not defined correctly.' ) @abstractmethod def __lowerCamelCase ( self ): raise NotImplementedError( f"{self.__class__} is an abstract class. Only classes inheriting this class can be called." ) @abstractmethod def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): raise NotImplementedError( f"{self.__class__} is an abstract class. Only classes inheriting this class can be called." ) @abstractmethod def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): raise NotImplementedError( f"{self.__class__} is an abstract class. Only classes inheriting this class can be called." ) @abstractmethod def __lowerCamelCase ( self ): raise NotImplementedError( f"{self.__class__} is an abstract class. Only classes inheriting this class can be called." ) @abstractmethod def __lowerCamelCase ( self ): raise NotImplementedError( f"{self.__class__} is an abstract class. Only classes inheriting this class can be called." ) @abstractmethod def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE=False ): raise NotImplementedError( f"{self.__class__} is an abstract class. Only classes inheriting this class can be called." ) class A__ ( _lowerCamelCase): def __init__( self , _SCREAMING_SNAKE_CASE ): super(_SCREAMING_SNAKE_CASE , self ).__init__() if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError(f"`token_ids` has to be a non-empty list, but is {token_ids}." ) if any((not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or token_id < 0) for token_id in token_ids ): raise ValueError(f"Each list in `token_ids` has to be a list of positive integers, but is {token_ids}." ) __lowerCAmelCase : List[Any] = token_ids __lowerCAmelCase : List[Any] = len(self.token_ids ) __lowerCAmelCase : Optional[Any] = -1 # the index of the currently fulfilled step __lowerCAmelCase : Optional[int] = False def __lowerCamelCase ( self ): if self.completed: return None return self.token_ids[self.fulfilled_idx + 1] def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(_SCREAMING_SNAKE_CASE )}" ) if self.completed: return False return token_id == self.token_ids[self.fulfilled_idx + 1] def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(_SCREAMING_SNAKE_CASE )}" ) __lowerCAmelCase : str = False __lowerCAmelCase : Union[str, Any] = False __lowerCAmelCase : Tuple = False if self.does_advance(_SCREAMING_SNAKE_CASE ): self.fulfilled_idx += 1 __lowerCAmelCase : Tuple = True if self.fulfilled_idx == (self.seqlen - 1): __lowerCAmelCase : Dict = True __lowerCAmelCase : Tuple = completed else: # failed to make progress. __lowerCAmelCase : Any = True self.reset() return stepped, completed, reset def __lowerCamelCase ( self ): __lowerCAmelCase : Union[str, Any] = False __lowerCAmelCase : Tuple = 0 def __lowerCamelCase ( self ): return self.seqlen - (self.fulfilled_idx + 1) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE=False ): __lowerCAmelCase : Optional[Any] = PhrasalConstraint(self.token_ids ) if stateful: __lowerCAmelCase : int = self.seqlen __lowerCAmelCase : List[Any] = self.fulfilled_idx __lowerCAmelCase : List[Any] = self.completed return new_constraint class A__ : def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=True ): __lowerCAmelCase : str = max([len(_SCREAMING_SNAKE_CASE ) for one in nested_token_ids] ) __lowerCAmelCase : str = {} for token_ids in nested_token_ids: __lowerCAmelCase : List[Any] = root for tidx, token_id in enumerate(_SCREAMING_SNAKE_CASE ): if token_id not in level: __lowerCAmelCase : Union[str, Any] = {} __lowerCAmelCase : Dict = level[token_id] if no_subsets and self.has_subsets(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError( 'Each list in `nested_token_ids` can\'t be a complete subset of another list, but is' f" {nested_token_ids}." ) __lowerCAmelCase : List[str] = root def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Optional[int] = self.trie for current_token in current_seq: __lowerCAmelCase : int = start[current_token] __lowerCAmelCase : Any = list(start.keys() ) return next_tokens def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Tuple = self.next_tokens(_SCREAMING_SNAKE_CASE ) return len(_SCREAMING_SNAKE_CASE ) == 0 def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Optional[Any] = list(root.values() ) if len(_SCREAMING_SNAKE_CASE ) == 0: return 1 else: return sum([self.count_leaves(_SCREAMING_SNAKE_CASE ) for nn in next_nodes] ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : List[Any] = self.count_leaves(_SCREAMING_SNAKE_CASE ) return len(_SCREAMING_SNAKE_CASE ) != leaf_count class A__ ( _lowerCamelCase): def __init__( self , _SCREAMING_SNAKE_CASE ): super(_SCREAMING_SNAKE_CASE , self ).__init__() if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or len(_SCREAMING_SNAKE_CASE ) == 0: raise ValueError(f"`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}." ) if any(not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for token_ids in nested_token_ids ): raise ValueError(f"`nested_token_ids` has to be a list of lists, but is {nested_token_ids}." ) if any( any((not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or token_id < 0) for token_id in token_ids ) for token_ids in nested_token_ids ): raise ValueError( f"Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}." ) __lowerCAmelCase : int = DisjunctiveTrie(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Union[str, Any] = nested_token_ids __lowerCAmelCase : Any = self.trie.max_height __lowerCAmelCase : Union[str, Any] = [] __lowerCAmelCase : Tuple = False def __lowerCamelCase ( self ): __lowerCAmelCase : Tuple = self.trie.next_tokens(self.current_seq ) if len(_SCREAMING_SNAKE_CASE ) == 0: return None else: return token_list def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(_SCREAMING_SNAKE_CASE )}" ) __lowerCAmelCase : List[str] = self.trie.next_tokens(self.current_seq ) return token_id in next_tokens def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(_SCREAMING_SNAKE_CASE )}" ) __lowerCAmelCase : Tuple = False __lowerCAmelCase : List[str] = False __lowerCAmelCase : List[str] = False if self.does_advance(_SCREAMING_SNAKE_CASE ): self.current_seq.append(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : str = True else: __lowerCAmelCase : Optional[int] = True self.reset() __lowerCAmelCase : Dict = self.trie.reached_leaf(self.current_seq ) __lowerCAmelCase : Union[str, Any] = completed return stepped, completed, reset def __lowerCamelCase ( self ): __lowerCAmelCase : List[Any] = False __lowerCAmelCase : List[str] = [] def __lowerCamelCase ( self ): if self.completed: # since this can be completed without reaching max height return 0 else: return self.seqlen - len(self.current_seq ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE=False ): __lowerCAmelCase : Any = DisjunctiveConstraint(self.token_ids ) if stateful: __lowerCAmelCase : Optional[Any] = self.seqlen __lowerCAmelCase : int = self.current_seq __lowerCAmelCase : Union[str, Any] = self.completed return new_constraint class A__ : def __init__( self , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : List[str] = constraints # max # of steps required to fulfill a given constraint __lowerCAmelCase : Dict = max([c.seqlen for c in constraints] ) __lowerCAmelCase : Optional[Any] = len(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : int = False self.init_state() def __lowerCamelCase ( self ): __lowerCAmelCase : Tuple = [] __lowerCAmelCase : Tuple = None __lowerCAmelCase : Any = [constraint.copy(stateful=_SCREAMING_SNAKE_CASE ) for constraint in self.constraints] def __lowerCamelCase ( self ): __lowerCAmelCase : Any = 0 if self.inprogress_constraint: # extra points for having a constraint mid-fulfilled add += self.max_seqlen - self.inprogress_constraint.remaining() return (len(self.complete_constraints ) * self.max_seqlen) + add def __lowerCamelCase ( self ): __lowerCAmelCase : int = [] if self.inprogress_constraint is None: for constraint in self.pending_constraints: # "pending" == "unfulfilled yet" __lowerCAmelCase : Union[str, Any] = constraint.advance() if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): token_list.append(_SCREAMING_SNAKE_CASE ) elif isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): token_list.extend(_SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase : int = self.inprogress_constraint.advance() if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): token_list.append(_SCREAMING_SNAKE_CASE ) elif isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): token_list.extend(_SCREAMING_SNAKE_CASE ) if len(_SCREAMING_SNAKE_CASE ) == 0: return None else: return token_list def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): self.init_state() if token_ids is not None: for token in token_ids: # completes or steps **one** constraint __lowerCAmelCase , __lowerCAmelCase : Optional[int] = self.add(_SCREAMING_SNAKE_CASE ) # the entire list of constraints are fulfilled if self.completed: break def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE ): if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError(f"`token_id` should be an `int`, but is `{token_id}`." ) __lowerCAmelCase , __lowerCAmelCase : List[str] = False, False if self.completed: __lowerCAmelCase : Dict = True __lowerCAmelCase : Optional[int] = False return complete, stepped if self.inprogress_constraint is not None: # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current # job, simply update the state __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase : Tuple = self.inprogress_constraint.update(_SCREAMING_SNAKE_CASE ) if reset: # 1. If the next token breaks the progress, then we must restart. # e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books". # But that doesn't mean we self.init_state(), since we only reset the state for this particular # constraint, not the full list of constraints. self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_SCREAMING_SNAKE_CASE ) ) __lowerCAmelCase : List[str] = None if complete: # 2. If the next token completes the constraint, move it to completed list, set # inprogress to None. If there are no pending constraints either, then this full list of constraints # is complete. self.complete_constraints.append(self.inprogress_constraint ) __lowerCAmelCase : Dict = None if len(self.pending_constraints ) == 0: # we're done! __lowerCAmelCase : Optional[int] = True else: # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list # of constraints? for cidx, pending_constraint in enumerate(self.pending_constraints ): if pending_constraint.does_advance(_SCREAMING_SNAKE_CASE ): __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase : Any = pending_constraint.update(_SCREAMING_SNAKE_CASE ) if not stepped: raise Exception( '`constraint.update(token_id)` is not yielding incremental progress, ' 'even though `constraint.does_advance(token_id)` is true.' ) if complete: self.complete_constraints.append(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = None if not complete and stepped: __lowerCAmelCase : Any = pending_constraint if complete or stepped: # If we made any progress at all, then it's at least not a "pending constraint". __lowerCAmelCase : str = ( self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :] ) if len(self.pending_constraints ) == 0 and self.inprogress_constraint is None: # If there's no longer any pending after this and no inprogress either, then we must be # complete. __lowerCAmelCase : Optional[Any] = True break # prevent accidentally stepping through multiple constraints with just one token. return complete, stepped def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE=True ): __lowerCAmelCase : Union[str, Any] = ConstraintListState(self.constraints ) # we actually never though self.constraints objects # throughout this process. So it's at initialization state. if stateful: __lowerCAmelCase : Tuple = [ constraint.copy(stateful=_SCREAMING_SNAKE_CASE ) for constraint in self.complete_constraints ] if self.inprogress_constraint is not None: __lowerCAmelCase : List[Any] = self.inprogress_constraint.copy(stateful=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = [constraint.copy() for constraint in self.pending_constraints] return new_state
86
"""simple docstring""" import argparse import torch # Step 1. clone https://github.com/microsoft/unilm # Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd # Step 3. cd unilm # Step 4. ln -s $(realpath wavlm/modules.py) ./ # create simlink # import classes from unilm.wavlm.WavLM import WavLM as WavLMOrig from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig from transformers import WavLMConfig, WavLMModel, logging logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { """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.grep_linear""": """encoder.layers.*.attention.gru_rel_pos_linear""", """self_attn.relative_attention_bias""": """encoder.layers.*.attention.rel_attn_embed""", """self_attn.grep_a""": """encoder.layers.*.attention.gru_rel_pos_const""", """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""", } lowerCamelCase__ = [ """ctc_proj""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): for attribute in key.split('.' ): __lowerCAmelCase : str = getattr(_UpperCamelCase , _UpperCamelCase ) if weight_type is not None: __lowerCAmelCase : Tuple = getattr(_UpperCamelCase , _UpperCamelCase ).shape else: __lowerCAmelCase : Dict = 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 : Union[str, Any] = value elif weight_type == "weight_g": __lowerCAmelCase : List[Any] = value elif weight_type == "weight_v": __lowerCAmelCase : Any = value elif weight_type == "bias": __lowerCAmelCase : List[str] = value else: __lowerCAmelCase : List[Any] = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase : Any = [] __lowerCAmelCase : Optional[int] = fairseq_model.state_dict() __lowerCAmelCase : Union[str, Any] = hf_model.feature_extractor for name, value in fairseq_dict.items(): __lowerCAmelCase : Union[str, Any] = False if "conv_layers" in name: load_conv_layer( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , hf_model.config.feat_extract_norm == 'group' , ) __lowerCAmelCase : str = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: __lowerCAmelCase : int = True if "*" in mapped_key: __lowerCAmelCase : List[str] = name.split(_UpperCamelCase )[0].split('.' )[-2] __lowerCAmelCase : Optional[Any] = mapped_key.replace('*' , _UpperCamelCase ) if "weight_g" in name: __lowerCAmelCase : Union[str, Any] = 'weight_g' elif "weight_v" in name: __lowerCAmelCase : int = 'weight_v' elif "bias" in name and "relative_attention_bias" not in name: __lowerCAmelCase : Optional[Any] = 'bias' elif "weight" in name: # TODO: don't match quantizer.weight_proj __lowerCAmelCase : List[str] = 'weight' else: __lowerCAmelCase : Optional[Any] = 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 , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): __lowerCAmelCase : List[Any] = full_name.split('conv_layers.' )[-1] __lowerCAmelCase : Any = name.split('.' ) __lowerCAmelCase : List[Any] = int(items[0] ) __lowerCAmelCase : Tuple = 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 : Tuple = 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 : int = 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 : Optional[Any] = 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 : Any = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_UpperCamelCase ) @torch.no_grad() def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase , _UpperCamelCase=None ): # load the pre-trained checkpoints __lowerCAmelCase : Any = torch.load(_UpperCamelCase ) __lowerCAmelCase : List[str] = WavLMConfigOrig(checkpoint['cfg'] ) __lowerCAmelCase : Optional[Any] = WavLMOrig(_UpperCamelCase ) model.load_state_dict(checkpoint['model'] ) model.eval() if config_path is not None: __lowerCAmelCase : Dict = WavLMConfig.from_pretrained(_UpperCamelCase ) else: __lowerCAmelCase : List[str] = WavLMConfig() __lowerCAmelCase : List[str] = WavLMModel(_UpperCamelCase ) recursively_load_weights(_UpperCamelCase , _UpperCamelCase ) hf_wavlm.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": lowerCamelCase__ = 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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") lowerCamelCase__ = parser.parse_args() convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
86
1
"""simple docstring""" from __future__ import annotations from sys import maxsize from typing import Generic, TypeVar lowerCAmelCase__ = TypeVar('''T''') def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" return (position - 1) // 2 def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" return (2 * position) + 1 def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" return (2 * position) + 2 class _lowerCamelCase ( Generic[T] ): def __init__(self ) -> None: UpperCamelCase = [] UpperCamelCase = {} UpperCamelCase = 0 def __len__(self ) -> int: return self.elements def __repr__(self ) -> str: return str(self.heap ) def snake_case_ (self ) -> bool: # Check if the priority queue is empty return self.elements == 0 def snake_case_ (self , __a , __a ) -> None: # Add an element with given priority to the queue self.heap.append((elem, weight) ) UpperCamelCase = self.elements self.elements += 1 self._bubble_up(__a ) def snake_case_ (self ) -> T: # Remove and return the element with lowest weight (highest priority) if self.elements > 1: self._swap_nodes(0 , self.elements - 1 ) UpperCamelCase , UpperCamelCase = self.heap.pop() del self.position_map[elem] self.elements -= 1 if self.elements > 0: UpperCamelCase , UpperCamelCase = self.heap[0] self._bubble_down(__a ) return elem def snake_case_ (self , __a , __a ) -> None: # Update the weight of the given key UpperCamelCase = self.position_map[elem] UpperCamelCase = (elem, weight) if position > 0: UpperCamelCase = get_parent_position(__a ) UpperCamelCase , UpperCamelCase = self.heap[parent_position] if parent_weight > weight: self._bubble_up(__a ) else: self._bubble_down(__a ) else: self._bubble_down(__a ) def snake_case_ (self , __a ) -> None: # Place a node at the proper position (upward movement) [to be used internally # only] UpperCamelCase = self.position_map[elem] if curr_pos == 0: return None UpperCamelCase = get_parent_position(__a ) UpperCamelCase , UpperCamelCase = self.heap[curr_pos] UpperCamelCase , UpperCamelCase = self.heap[parent_position] if parent_weight > weight: self._swap_nodes(__a , __a ) return self._bubble_up(__a ) return None def snake_case_ (self , __a ) -> None: # Place a node at the proper position (downward movement) [to be used # internally only] UpperCamelCase = self.position_map[elem] UpperCamelCase , UpperCamelCase = self.heap[curr_pos] UpperCamelCase = get_child_left_position(__a ) UpperCamelCase = get_child_right_position(__a ) if child_left_position < self.elements and child_right_position < self.elements: UpperCamelCase , UpperCamelCase = self.heap[child_left_position] UpperCamelCase , UpperCamelCase = self.heap[child_right_position] if child_right_weight < child_left_weight and child_right_weight < weight: self._swap_nodes(__a , __a ) return self._bubble_down(__a ) if child_left_position < self.elements: UpperCamelCase , UpperCamelCase = self.heap[child_left_position] if child_left_weight < weight: self._swap_nodes(__a , __a ) return self._bubble_down(__a ) else: return None if child_right_position < self.elements: UpperCamelCase , UpperCamelCase = self.heap[child_right_position] if child_right_weight < weight: self._swap_nodes(__a , __a ) return self._bubble_down(__a ) return None def snake_case_ (self , __a , __a ) -> None: # Swap the nodes at the given positions UpperCamelCase = self.heap[nodea_pos][0] UpperCamelCase = self.heap[nodea_pos][0] UpperCamelCase , UpperCamelCase = ( self.heap[nodea_pos], self.heap[nodea_pos], ) UpperCamelCase = nodea_pos UpperCamelCase = nodea_pos class _lowerCamelCase ( Generic[T] ): def __init__(self ) -> None: UpperCamelCase = {} UpperCamelCase = 0 def __repr__(self ) -> str: return str(self.connections ) def __len__(self ) -> int: return self.nodes def snake_case_ (self , __a ) -> None: # Add a node in the graph if it is not in the graph if node not in self.connections: UpperCamelCase = {} self.nodes += 1 def snake_case_ (self , __a , __a , __a ) -> None: # Add an edge between 2 nodes in the graph self.add_node(__a ) self.add_node(__a ) UpperCamelCase = weight UpperCamelCase = weight def a__ ( _SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase = {node: maxsize for node in graph.connections} UpperCamelCase = {node: None for node in graph.connections} UpperCamelCase = MinPriorityQueue() for node, weight in dist.items(): priority_queue.push(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if priority_queue.is_empty(): return dist, parent # initialization UpperCamelCase = priority_queue.extract_min() UpperCamelCase = 0 for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: UpperCamelCase = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(_SCREAMING_SNAKE_CASE , dist[neighbour] ) UpperCamelCase = node # running prim's algorithm while not priority_queue.is_empty(): UpperCamelCase = priority_queue.extract_min() for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: UpperCamelCase = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(_SCREAMING_SNAKE_CASE , dist[neighbour] ) UpperCamelCase = node return dist, parent
244
"""simple docstring""" from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record lowerCAmelCase__ = '''\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } ''' lowerCAmelCase__ = '''\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. ''' lowerCAmelCase__ = ''' Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for \'record\': list of question-answer dictionaries with the following keys: - \'idx\': index of the question as specified by the dataset - \'prediction_text\': the predicted answer text - for \'multirc\': list of question-answer dictionaries with the following keys: - \'idx\': index of the question-answer pair as specified by the dataset - \'prediction\': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for \'record\': list of question-answers dictionaries with the following keys: - \'idx\': index of the question as specified by the dataset - \'answers\': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for \'record\': - \'exact_match\': Exact match between answer and gold answer - \'f1\': F1 score - for \'multirc\': - \'exact_match\': Exact match between answer and gold answer - \'f1_m\': Per-question macro-F1 score - \'f1_a\': Average F1 score over all answers - for \'axb\': \'matthews_correlation\': Matthew Correlation - for \'cb\': - \'accuracy\': Accuracy - \'f1\': F1 score - for all others: - \'accuracy\': Accuracy Examples: >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\') >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}] >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 1.0, \'f1\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\') >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0} >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'matthews_correlation\': 1.0} ''' def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" return float((preds == labels).mean() ) def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="binary" ): """simple docstring""" UpperCamelCase = simple_accuracy(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase = float(fa_score(y_true=_SCREAMING_SNAKE_CASE , y_pred=_SCREAMING_SNAKE_CASE , average=_SCREAMING_SNAKE_CASE ) ) return { "accuracy": acc, "f1": fa, } def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = {} for id_pred, label in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCamelCase = F"{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}" UpperCamelCase = id_pred["prediction"] if question_id in question_map: question_map[question_id].append((pred, label) ) else: UpperCamelCase = [(pred, label)] UpperCamelCase , UpperCamelCase = [], [] for question, preds_labels in question_map.items(): UpperCamelCase , UpperCamelCase = zip(*_SCREAMING_SNAKE_CASE ) UpperCamelCase = fa_score(y_true=_SCREAMING_SNAKE_CASE , y_pred=_SCREAMING_SNAKE_CASE , average="macro" ) fas.append(_SCREAMING_SNAKE_CASE ) UpperCamelCase = int(sum(pred == label for pred, label in preds_labels ) == len(_SCREAMING_SNAKE_CASE ) ) ems.append(_SCREAMING_SNAKE_CASE ) UpperCamelCase = float(sum(_SCREAMING_SNAKE_CASE ) / len(_SCREAMING_SNAKE_CASE ) ) UpperCamelCase = sum(_SCREAMING_SNAKE_CASE ) / len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = float(fa_score(y_true=_SCREAMING_SNAKE_CASE , y_pred=[id_pred["prediction"] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _lowerCamelCase ( datasets.Metric ): def snake_case_ (self ) -> Dict: if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format="numpy" if not self.config_name == "record" and not self.config_name == "multirc" else None , ) def snake_case_ (self ) -> Tuple: if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "prediction_text": datasets.Value("string" ), }, "references": { "idx": { "passage": datasets.Value("int64" ), "query": datasets.Value("int64" ), }, "answers": datasets.Sequence(datasets.Value("string" ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value("int64" ), "paragraph": datasets.Value("int64" ), "question": datasets.Value("int64" ), }, "prediction": datasets.Value("int64" ), }, "references": datasets.Value("int64" ), } else: return { "predictions": datasets.Value("int64" ), "references": datasets.Value("int64" ), } def snake_case_ (self , __a , __a ) -> str: if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(__a , __a )} elif self.config_name == "cb": return acc_and_fa(__a , __a , fa_avg="macro" ) elif self.config_name == "record": UpperCamelCase = [ { "qas": [ {"id": ref["idx"]["query"], "answers": [{"text": ans} for ans in ref["answers"]]} for ref in references ] } ] UpperCamelCase = {pred["idx"]["query"]: pred["prediction_text"] for pred in predictions} return evaluate_record(__a , __a )[0] elif self.config_name == "multirc": return evaluate_multirc(__a , __a ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(__a , __a )} else: raise KeyError( "You should supply a configuration name selected in " "[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]" )
244
1
import argparse import torch from transformers import ( UniSpeechSatConfig, UniSpeechSatForAudioFrameClassification, UniSpeechSatForSequenceClassification, UniSpeechSatForXVector, WavaVecaFeatureExtractor, logging, ) logging.set_verbosity_info() _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): snake_case_ : Any = UniSpeechSatForSequenceClassification.from_pretrained(__a , config=__a ) snake_case_ : Tuple = downstream_dict['projector.weight'] snake_case_ : Dict = downstream_dict['projector.bias'] snake_case_ : Optional[int] = downstream_dict['model.post_net.linear.weight'] snake_case_ : Any = downstream_dict['model.post_net.linear.bias'] return model def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): snake_case_ : Union[str, Any] = UniSpeechSatForAudioFrameClassification.from_pretrained(__a , config=__a ) snake_case_ : List[Any] = downstream_dict['model.linear.weight'] snake_case_ : str = downstream_dict['model.linear.bias'] return model def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): snake_case_ : Union[str, Any] = UniSpeechSatForXVector.from_pretrained(__a , config=__a ) snake_case_ : Union[str, Any] = downstream_dict['connector.weight'] snake_case_ : Optional[Any] = downstream_dict['connector.bias'] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): snake_case_ : int = downstream_dict[ f"""model.framelevel_feature_extractor.module.{i}.kernel.weight""" ] snake_case_ : Any = downstream_dict[f"""model.framelevel_feature_extractor.module.{i}.kernel.bias"""] snake_case_ : List[str] = downstream_dict['model.utterancelevel_feature_extractor.linear1.weight'] snake_case_ : List[Any] = downstream_dict['model.utterancelevel_feature_extractor.linear1.bias'] snake_case_ : Any = downstream_dict['model.utterancelevel_feature_extractor.linear2.weight'] snake_case_ : Any = downstream_dict['model.utterancelevel_feature_extractor.linear2.bias'] snake_case_ : int = downstream_dict['objective.W'] return model @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( __a , __a , __a , __a ): snake_case_ : Dict = torch.load(__a , map_location='cpu' ) snake_case_ : Any = checkpoint['Downstream'] snake_case_ : List[Any] = UniSpeechSatConfig.from_pretrained(__a ) snake_case_ : Union[str, Any] = WavaVecaFeatureExtractor.from_pretrained( __a , return_attention_mask=__a , do_normalize=__a ) snake_case_ : List[str] = hf_config.architectures[0] if arch.endswith('ForSequenceClassification' ): snake_case_ : List[Any] = convert_classification(__a , __a , __a ) elif arch.endswith('ForAudioFrameClassification' ): snake_case_ : List[str] = convert_diarization(__a , __a , __a ) elif arch.endswith('ForXVector' ): snake_case_ : Tuple = convert_xvector(__a , __a , __a ) else: raise NotImplementedError(f"""S3PRL weights conversion is not supported for {arch}""" ) if hf_config.use_weighted_layer_sum: snake_case_ : Tuple = checkpoint['Featurizer']['weights'] hf_feature_extractor.save_pretrained(__a ) hf_model.save_pretrained(__a ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument( """--base_model_name""", default=None, type=str, help="""Name of the huggingface pretrained base model.""" ) parser.add_argument("""--config_path""", default=None, type=str, help="""Path to the huggingface classifier config.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to the s3prl checkpoint.""") parser.add_argument("""--model_dump_path""", default=None, type=str, help="""Path to the final converted model.""") _SCREAMING_SNAKE_CASE = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
327
import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) _SCREAMING_SNAKE_CASE = logging.getLogger(__name__) @dataclass class SCREAMING_SNAKE_CASE_ : __magic_name__: str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) __magic_name__: Optional[str] = field( default=snake_case_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) __magic_name__: Optional[str] = field( default=snake_case_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) __magic_name__: Optional[str] = field( default=snake_case_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) __magic_name__: bool = field(default=snake_case_ , metadata={"help": "Whether tp freeze the encoder."} ) __magic_name__: bool = field(default=snake_case_ , metadata={"help": "Whether to freeze the embeddings."} ) @dataclass class SCREAMING_SNAKE_CASE_ : __magic_name__: str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) __magic_name__: Optional[str] = field( default="summarization" , metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"} , ) __magic_name__: Optional[int] = field( default=1024 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __magic_name__: Optional[int] = field( default=128 , metadata={ "help": ( "The maximum total sequence length for target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __magic_name__: Optional[int] = field( default=142 , metadata={ "help": ( "The maximum total sequence length for validation target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded. " "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used " "during ``evaluate`` and ``predict``." ) } , ) __magic_name__: Optional[int] = field( default=142 , metadata={ "help": ( "The maximum total sequence length for test target text after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) __magic_name__: Optional[int] = field(default=-1 , metadata={"help": "# training examples. -1 means use all."} ) __magic_name__: Optional[int] = field(default=-1 , metadata={"help": "# validation examples. -1 means use all."} ) __magic_name__: Optional[int] = field(default=-1 , metadata={"help": "# test examples. -1 means use all."} ) __magic_name__: Optional[str] = field(default=snake_case_ , metadata={"help": "Source language id for translation."} ) __magic_name__: Optional[str] = field(default=snake_case_ , metadata={"help": "Target language id for translation."} ) __magic_name__: Optional[int] = field(default=snake_case_ , metadata={"help": "# num_beams to use for evaluation."} ) __magic_name__: bool = field( default=snake_case_ , metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."} , ) def SCREAMING_SNAKE_CASE__ ( __a , __a , __a ): logger.info(f"""***** {split} metrics *****""" ) for key in sorted(metrics.keys() ): logger.info(f""" {key} = {metrics[key]}""" ) save_json(__a , os.path.join(__a , f"""{split}_results.json""" ) ) def SCREAMING_SNAKE_CASE__ ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case_ : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. snake_case_ ,snake_case_ ,snake_case_ : List[Any] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: snake_case_ ,snake_case_ ,snake_case_ : List[str] = parser.parse_args_into_dataclasses() check_output_dir(__a ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info('Training/evaluation parameters %s' , __a ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case_ : Tuple = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case_ : Any = ('encoder_layerdrop', 'decoder_layerdrop', 'dropout', 'attention_dropout') for p in extra_model_params: if getattr(__a , __a , __a ): assert hasattr(__a , __a ), f"""({config.__class__.__name__}) doesn't have a `{p}` attribute""" setattr(__a , __a , getattr(__a , __a ) ) snake_case_ : Tuple = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case_ : Any = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf='.ckpt' in model_args.model_name_or_path , config=__a , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(__a , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: snake_case_ : Any = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(__a , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(__a , __a ): snake_case_ : int = tokenizer.lang_code_to_id[data_args.tgt_lang] else: snake_case_ : int = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(__a ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) snake_case_ : List[Any] = SeqaSeqDataset # Get datasets snake_case_ : List[Any] = ( dataset_class( __a , type_path='train' , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_train else None ) snake_case_ : List[str] = ( dataset_class( __a , type_path='val' , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) snake_case_ : List[Any] = ( dataset_class( __a , type_path='test' , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '' , ) if training_args.do_predict else None ) # Initialize our Trainer snake_case_ : Any = ( build_compute_metrics_fn(data_args.task , __a ) if training_args.predict_with_generate else None ) snake_case_ : List[str] = SeqaSeqTrainer( model=__a , args=__a , data_args=__a , train_dataset=__a , eval_dataset=__a , data_collator=SeqaSeqDataCollator( __a , __a , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=__a , tokenizer=__a , ) snake_case_ : Optional[int] = {} # Training if training_args.do_train: logger.info('*** Train ***' ) snake_case_ : Any = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) snake_case_ : Tuple = train_result.metrics snake_case_ : List[str] = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics('train' , __a , training_args.output_dir ) all_metrics.update(__a ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , 'trainer_state.json' ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case_ : List[Any] = trainer.evaluate(metric_key_prefix='val' ) snake_case_ : str = data_args.n_val snake_case_ : Union[str, Any] = round(metrics['val_loss'] , 4 ) if trainer.is_world_process_zero(): handle_metrics('val' , __a , training_args.output_dir ) all_metrics.update(__a ) if training_args.do_predict: logger.info('*** Predict ***' ) snake_case_ : Dict = trainer.predict(test_dataset=__a , metric_key_prefix='test' ) snake_case_ : Union[str, Any] = test_output.metrics snake_case_ : int = data_args.n_test if trainer.is_world_process_zero(): snake_case_ : List[str] = round(metrics['test_loss'] , 4 ) handle_metrics('test' , __a , training_args.output_dir ) all_metrics.update(__a ) if training_args.predict_with_generate: snake_case_ : Any = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=__a , clean_up_tokenization_spaces=__a ) snake_case_ : Any = lmap(str.strip , __a ) write_txt_file(__a , os.path.join(training_args.output_dir , 'test_generations.txt' ) ) if trainer.is_world_process_zero(): save_json(__a , os.path.join(training_args.output_dir , 'all_results.json' ) ) return all_metrics def SCREAMING_SNAKE_CASE__ ( __a ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
327
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCAmelCase : List[Any] = {'''configuration_sew''': ['''SEW_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SEWConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase : Tuple = [ '''SEW_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SEWForCTC''', '''SEWForSequenceClassification''', '''SEWModel''', '''SEWPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys _lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
340
"""simple docstring""" import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def lowerCamelCase_( _lowerCamelCase ) -> Tuple: '''simple docstring''' _lowerCamelCase : Optional[int] = {} _lowerCamelCase : Optional[int] = tokenizer(example["content"] , truncation=_lowerCamelCase )["input_ids"] _lowerCamelCase : Dict = len(example["content"] ) / len(output["input_ids"] ) return output _lowerCAmelCase : Tuple = HfArgumentParser(PretokenizationArguments) _lowerCAmelCase : Optional[int] = parser.parse_args() if args.num_workers is None: _lowerCAmelCase : Any = multiprocessing.cpu_count() _lowerCAmelCase : List[str] = AutoTokenizer.from_pretrained(args.tokenizer_dir) _lowerCAmelCase : Union[str, Any] = time.time() _lowerCAmelCase : Optional[int] = load_dataset(args.dataset_name, split='''train''') print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') _lowerCAmelCase : Any = time.time() _lowerCAmelCase : Dict = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ '''repo_name''', '''path''', '''copies''', '''size''', '''content''', '''license''', '''hash''', '''line_mean''', '''line_max''', '''alpha_frac''', '''autogenerated''', ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') _lowerCAmelCase : str = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
340
1
'''simple docstring''' import operator as op lowercase_ = """scaler.pt""" lowercase_ = """pytorch_model""" lowercase_ = """random_states""" lowercase_ = """optimizer""" lowercase_ = """scheduler""" lowercase_ = """pytorch_model.bin""" lowercase_ = """pytorch_model.bin.index.json""" lowercase_ = """model.safetensors""" lowercase_ = """model.safetensors.index.json""" lowercase_ = """1.10.2""" lowercase_ = """py38""" lowercase_ = """4.17.0""" lowercase_ = ["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""] lowercase_ = ["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""] lowercase_ = ["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""] lowercase_ = ["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""] lowercase_ = ["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""] lowercase_ = """2.0.1""" lowercase_ = ["""pdsh""", """standard""", """openmpi""", """mvapich"""] lowercase_ = ["""default""", """reduce-overhead""", """max-autotune"""] lowercase_ = {""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 lowercase_ = [ """nnodes""", """nproc_per_node""", """rdzv_backend""", """rdzv_endpoint""", """rdzv_id""", """rdzv_conf""", """standalone""", """max_restarts""", """monitor_interval""", """start_method""", """role""", """module""", """m""", """no_python""", """run_path""", """log_dir""", """r""", """redirects""", """t""", """tee""", """node_rank""", """master_addr""", """master_port""", ] lowercase_ = ["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""] lowercase_ = ["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]
58
'''simple docstring''' import collections.abc from typing import Optional, Tuple, Union import torch import torch.utils.checkpoint from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithNoAttention, 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_poolformer import PoolFormerConfig lowercase_ = logging.get_logger(__name__) # General docstring lowercase_ = """PoolFormerConfig""" # Base docstring lowercase_ = """sail/poolformer_s12""" lowercase_ = [1, 512, 7, 7] # Image classification docstring lowercase_ = """sail/poolformer_s12""" lowercase_ = """tabby, tabby cat""" lowercase_ = [ """sail/poolformer_s12""", # See all PoolFormer models at https://huggingface.co/models?filter=poolformer ] def lowerCamelCase ( __lowerCamelCase : List[Any] , __lowerCamelCase : float = 0.0 , __lowerCamelCase : bool = False ) ->int: if drop_prob == 0.0 or not training: return input _SCREAMING_SNAKE_CASE = 1 - drop_prob _SCREAMING_SNAKE_CASE = (input.shape[0],) + (1,) * (input.ndim - 1) # work with diff dim tensors, not just 2D ConvNets _SCREAMING_SNAKE_CASE = keep_prob + torch.rand(__lowerCamelCase , dtype=input.dtype , device=input.device ) random_tensor.floor_() # binarize _SCREAMING_SNAKE_CASE = input.div(__lowerCamelCase ) * random_tensor return output class a_ ( nn.Module ): '''simple docstring''' def __init__( self , A = None ) -> None: super().__init__() _SCREAMING_SNAKE_CASE = drop_prob def snake_case_( self , A ) -> torch.Tensor: return drop_path(A , self.drop_prob , self.training ) def snake_case_( self ) -> str: return "p={}".format(self.drop_prob ) class a_ ( nn.Module ): '''simple docstring''' def __init__( self , A , A , A , A , A , A=None ) -> Union[str, Any]: super().__init__() _SCREAMING_SNAKE_CASE = patch_size if isinstance(A , collections.abc.Iterable ) else (patch_size, patch_size) _SCREAMING_SNAKE_CASE = stride if isinstance(A , collections.abc.Iterable ) else (stride, stride) _SCREAMING_SNAKE_CASE = padding if isinstance(A , collections.abc.Iterable ) else (padding, padding) _SCREAMING_SNAKE_CASE = nn.Convad(A , A , kernel_size=A , stride=A , padding=A ) _SCREAMING_SNAKE_CASE = norm_layer(A ) if norm_layer else nn.Identity() def snake_case_( self , A ) -> Optional[Any]: _SCREAMING_SNAKE_CASE = self.projection(A ) _SCREAMING_SNAKE_CASE = self.norm(A ) return embeddings class a_ ( nn.GroupNorm ): '''simple docstring''' def __init__( self , A , **A ) -> Union[str, Any]: super().__init__(1 , A , **A ) class a_ ( nn.Module ): '''simple docstring''' def __init__( self , A ) -> Union[str, Any]: super().__init__() _SCREAMING_SNAKE_CASE = nn.AvgPoolad(A , stride=1 , padding=pool_size // 2 , count_include_pad=A ) def snake_case_( self , A ) -> Union[str, Any]: return self.pool(A ) - hidden_states class a_ ( nn.Module ): '''simple docstring''' def __init__( self , A , A , A , A ) -> List[Any]: super().__init__() _SCREAMING_SNAKE_CASE = nn.Convad(A , A , 1 ) _SCREAMING_SNAKE_CASE = nn.Convad(A , A , 1 ) _SCREAMING_SNAKE_CASE = PoolFormerDropPath(A ) if isinstance(config.hidden_act , A ): _SCREAMING_SNAKE_CASE = ACTaFN[config.hidden_act] else: _SCREAMING_SNAKE_CASE = config.hidden_act def snake_case_( self , A ) -> Optional[int]: _SCREAMING_SNAKE_CASE = self.conva(A ) _SCREAMING_SNAKE_CASE = self.act_fn(A ) _SCREAMING_SNAKE_CASE = self.drop(A ) _SCREAMING_SNAKE_CASE = self.conva(A ) _SCREAMING_SNAKE_CASE = self.drop(A ) return hidden_states class a_ ( nn.Module ): '''simple docstring''' def __init__( self , A , A , A , A , A , A ) -> Union[str, Any]: super().__init__() _SCREAMING_SNAKE_CASE = PoolFormerPooling(A ) _SCREAMING_SNAKE_CASE = PoolFormerOutput(A , A , A , A ) _SCREAMING_SNAKE_CASE = PoolFormerGroupNorm(A ) _SCREAMING_SNAKE_CASE = PoolFormerGroupNorm(A ) # Useful for training neural nets _SCREAMING_SNAKE_CASE = PoolFormerDropPath(A ) if drop_path > 0.0 else nn.Identity() _SCREAMING_SNAKE_CASE = config.use_layer_scale if config.use_layer_scale: _SCREAMING_SNAKE_CASE = nn.Parameter( config.layer_scale_init_value * torch.ones((A) ) , requires_grad=A ) _SCREAMING_SNAKE_CASE = nn.Parameter( config.layer_scale_init_value * torch.ones((A) ) , requires_grad=A ) def snake_case_( self , A ) -> Optional[Any]: if self.use_layer_scale: _SCREAMING_SNAKE_CASE = self.pooling(self.before_norm(A ) ) _SCREAMING_SNAKE_CASE = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * pooling_output # First residual connection _SCREAMING_SNAKE_CASE = hidden_states + self.drop_path(A ) _SCREAMING_SNAKE_CASE = () _SCREAMING_SNAKE_CASE = self.output(self.after_norm(A ) ) _SCREAMING_SNAKE_CASE = self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * layer_output # Second residual connection _SCREAMING_SNAKE_CASE = hidden_states + self.drop_path(A ) _SCREAMING_SNAKE_CASE = (output,) + outputs return outputs else: _SCREAMING_SNAKE_CASE = self.drop_path(self.pooling(self.before_norm(A ) ) ) # First residual connection _SCREAMING_SNAKE_CASE = pooling_output + hidden_states _SCREAMING_SNAKE_CASE = () # Second residual connection inside the PoolFormerOutput block _SCREAMING_SNAKE_CASE = self.drop_path(self.output(self.after_norm(A ) ) ) _SCREAMING_SNAKE_CASE = hidden_states + layer_output _SCREAMING_SNAKE_CASE = (output,) + outputs return outputs class a_ ( nn.Module ): '''simple docstring''' def __init__( self , A ) -> Any: super().__init__() _SCREAMING_SNAKE_CASE = config # stochastic depth decay rule _SCREAMING_SNAKE_CASE = [x.item() for x in torch.linspace(0 , config.drop_path_rate , sum(config.depths ) )] # patch embeddings _SCREAMING_SNAKE_CASE = [] for i in range(config.num_encoder_blocks ): embeddings.append( PoolFormerEmbeddings( patch_size=config.patch_sizes[i] , stride=config.strides[i] , padding=config.padding[i] , num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1] , hidden_size=config.hidden_sizes[i] , ) ) _SCREAMING_SNAKE_CASE = nn.ModuleList(A ) # Transformer blocks _SCREAMING_SNAKE_CASE = [] _SCREAMING_SNAKE_CASE = 0 for i in range(config.num_encoder_blocks ): # each block consists of layers _SCREAMING_SNAKE_CASE = [] if i != 0: cur += config.depths[i - 1] for j in range(config.depths[i] ): layers.append( PoolFormerLayer( A , num_channels=config.hidden_sizes[i] , pool_size=config.pool_size , hidden_size=config.hidden_sizes[i] , intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio ) , drop_path=dpr[cur + j] , ) ) blocks.append(nn.ModuleList(A ) ) _SCREAMING_SNAKE_CASE = nn.ModuleList(A ) def snake_case_( self , A , A=False , A=True ) -> List[Any]: _SCREAMING_SNAKE_CASE = () if output_hidden_states else None _SCREAMING_SNAKE_CASE = pixel_values for idx, layers in enumerate(zip(self.patch_embeddings , self.block ) ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = layers # Get patch embeddings from hidden_states _SCREAMING_SNAKE_CASE = embedding_layer(A ) # Send the embeddings through the blocks for _, blk in enumerate(A ): _SCREAMING_SNAKE_CASE = blk(A ) _SCREAMING_SNAKE_CASE = layer_outputs[0] if output_hidden_states: _SCREAMING_SNAKE_CASE = all_hidden_states + (hidden_states,) if not return_dict: return tuple(v for v in [hidden_states, all_hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=A , hidden_states=A ) class a_ ( snake_case_ ): '''simple docstring''' UpperCamelCase = PoolFormerConfig UpperCamelCase = '''poolformer''' UpperCamelCase = '''pixel_values''' UpperCamelCase = True def snake_case_( self , A ) -> int: 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.LayerNorm ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) def snake_case_( self , A , A=False ) -> Dict: if isinstance(A , A ): _SCREAMING_SNAKE_CASE = value lowercase_ = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`PoolFormerConfig`]): 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowercase_ = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`PoolFormerImageProcessor.__call__`] for details. """ @add_start_docstrings( '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.''' , snake_case_ , ) class a_ ( snake_case_ ): '''simple docstring''' def __init__( self , A ) -> int: super().__init__(A ) _SCREAMING_SNAKE_CASE = config _SCREAMING_SNAKE_CASE = PoolFormerEncoder(A ) # Initialize weights and apply final processing self.post_init() def snake_case_( self ) -> Any: return self.embeddings.patch_embeddings @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 = None , A = None , A = None , ) -> Union[Tuple, BaseModelOutputWithNoAttention]: _SCREAMING_SNAKE_CASE = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _SCREAMING_SNAKE_CASE = 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""" ) _SCREAMING_SNAKE_CASE = self.encoder( A , output_hidden_states=A , return_dict=A , ) _SCREAMING_SNAKE_CASE = encoder_outputs[0] if not return_dict: return (sequence_output, None) + encoder_outputs[1:] return BaseModelOutputWithNoAttention( last_hidden_state=A , hidden_states=encoder_outputs.hidden_states , ) class a_ ( nn.Module ): '''simple docstring''' def __init__( self , A ) -> Dict: super().__init__() _SCREAMING_SNAKE_CASE = nn.Linear(config.hidden_size , config.hidden_size ) def snake_case_( self , A ) -> str: _SCREAMING_SNAKE_CASE = self.dense(A ) return output @add_start_docstrings( ''' PoolFormer Model transformer with an image classification head on top ''' , snake_case_ , ) class a_ ( snake_case_ ): '''simple docstring''' def __init__( self , A ) -> Optional[Any]: super().__init__(A ) _SCREAMING_SNAKE_CASE = config.num_labels _SCREAMING_SNAKE_CASE = PoolFormerModel(A ) # Final norm _SCREAMING_SNAKE_CASE = PoolFormerGroupNorm(config.hidden_sizes[-1] ) # Classifier head _SCREAMING_SNAKE_CASE = ( nn.Linear(config.hidden_sizes[-1] , 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 snake_case_( self , A = None , A = None , A = None , A = None , ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: _SCREAMING_SNAKE_CASE = return_dict if return_dict is not None else self.config.use_return_dict _SCREAMING_SNAKE_CASE = self.poolformer( A , output_hidden_states=A , return_dict=A , ) _SCREAMING_SNAKE_CASE = outputs[0] _SCREAMING_SNAKE_CASE = self.classifier(self.norm(A ).mean([-2, -1] ) ) _SCREAMING_SNAKE_CASE = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: _SCREAMING_SNAKE_CASE = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): _SCREAMING_SNAKE_CASE = """single_label_classification""" else: _SCREAMING_SNAKE_CASE = """multi_label_classification""" if self.config.problem_type == "regression": _SCREAMING_SNAKE_CASE = MSELoss() if self.num_labels == 1: _SCREAMING_SNAKE_CASE = loss_fct(logits.squeeze() , labels.squeeze() ) else: _SCREAMING_SNAKE_CASE = loss_fct(A , A ) elif self.config.problem_type == "single_label_classification": _SCREAMING_SNAKE_CASE = CrossEntropyLoss() _SCREAMING_SNAKE_CASE = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": _SCREAMING_SNAKE_CASE = BCEWithLogitsLoss() _SCREAMING_SNAKE_CASE = loss_fct(A , A ) if not return_dict: _SCREAMING_SNAKE_CASE = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=A , logits=A , hidden_states=outputs.hidden_states )
58
1
"""simple docstring""" import unittest import numpy as np from datasets import load_dataset 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 BeitImageProcessor class snake_case ( unittest.TestCase ): def __init__( self : Union[str, Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Union[str, Any]=7 , UpperCamelCase__ : str=3 , UpperCamelCase__ : Optional[int]=1_8 , UpperCamelCase__ : Union[str, Any]=3_0 , UpperCamelCase__ : str=4_0_0 , UpperCamelCase__ : str=True , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : Tuple=None , UpperCamelCase__ : Any=True , UpperCamelCase__ : Any=[0.5, 0.5, 0.5] , UpperCamelCase__ : Union[str, Any]=[0.5, 0.5, 0.5] , UpperCamelCase__ : List[Any]=False , )-> Dict: '''simple docstring''' __lowerCAmelCase: int = size if size is not None else {'height': 2_0, 'width': 2_0} __lowerCAmelCase: int = crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8} __lowerCAmelCase: Union[str, Any] = parent __lowerCAmelCase: Any = batch_size __lowerCAmelCase: List[str] = num_channels __lowerCAmelCase: List[str] = image_size __lowerCAmelCase: Optional[Any] = min_resolution __lowerCAmelCase: Optional[int] = max_resolution __lowerCAmelCase: Union[str, Any] = do_resize __lowerCAmelCase: Any = size __lowerCAmelCase: int = do_center_crop __lowerCAmelCase: int = crop_size __lowerCAmelCase: Union[str, Any] = do_normalize __lowerCAmelCase: Optional[int] = image_mean __lowerCAmelCase: int = image_std __lowerCAmelCase: Tuple = do_reduce_labels def lowercase_ ( self : Any)-> Optional[int]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_reduce_labels": self.do_reduce_labels, } def a__ ( ) -> str: __lowerCAmelCase: Union[str, Any] = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) __lowerCAmelCase: Union[str, Any] = Image.open(dataset[0]["file"] ) __lowerCAmelCase: Any = Image.open(dataset[1]["file"] ) return image, map def a__ ( ) -> List[str]: __lowerCAmelCase: int = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) __lowerCAmelCase: str = Image.open(ds[0]["file"] ) __lowerCAmelCase: List[str] = Image.open(ds[1]["file"] ) __lowerCAmelCase: int = Image.open(ds[2]["file"] ) __lowerCAmelCase: Dict = Image.open(ds[3]["file"] ) return [imagea, imagea], [mapa, mapa] @require_torch @require_vision class snake_case ( SCREAMING_SNAKE_CASE_, unittest.TestCase ): SCREAMING_SNAKE_CASE_ : int = BeitImageProcessor if is_vision_available() else None def lowercase_ ( self : Dict)-> str: '''simple docstring''' __lowerCAmelCase: str = BeitImageProcessingTester(self) @property def lowercase_ ( self : Tuple)-> str: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def lowercase_ ( self : str)-> List[Any]: '''simple docstring''' __lowerCAmelCase: Optional[int] = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(snake_case__ , "do_resize")) self.assertTrue(hasattr(snake_case__ , "size")) self.assertTrue(hasattr(snake_case__ , "do_center_crop")) self.assertTrue(hasattr(snake_case__ , "center_crop")) self.assertTrue(hasattr(snake_case__ , "do_normalize")) self.assertTrue(hasattr(snake_case__ , "image_mean")) self.assertTrue(hasattr(snake_case__ , "image_std")) def lowercase_ ( self : str)-> str: '''simple docstring''' __lowerCAmelCase: List[Any] = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {"height": 2_0, "width": 2_0}) self.assertEqual(image_processor.crop_size , {"height": 1_8, "width": 1_8}) self.assertEqual(image_processor.do_reduce_labels , snake_case__) __lowerCAmelCase: List[str] = self.image_processing_class.from_dict( self.image_processor_dict , size=4_2 , crop_size=8_4 , reduce_labels=snake_case__) self.assertEqual(image_processor.size , {"height": 4_2, "width": 4_2}) self.assertEqual(image_processor.crop_size , {"height": 8_4, "width": 8_4}) self.assertEqual(image_processor.do_reduce_labels , snake_case__) def lowercase_ ( self : Union[str, Any])-> List[Any]: '''simple docstring''' pass def lowercase_ ( self : Any)-> Tuple: '''simple docstring''' __lowerCAmelCase: Optional[int] = self.image_processing_class(**self.image_processor_dict) # create random PIL images __lowerCAmelCase: int = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case__) for image in image_inputs: self.assertIsInstance(snake_case__ , Image.Image) # Test not batched input __lowerCAmelCase: Tuple = image_processing(image_inputs[0] , return_tensors="pt").pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched __lowerCAmelCase: Tuple = image_processing(snake_case__ , return_tensors="pt").pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def lowercase_ ( self : List[Any])-> int: '''simple docstring''' __lowerCAmelCase: List[str] = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors __lowerCAmelCase: Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case__ , numpify=snake_case__) for image in image_inputs: self.assertIsInstance(snake_case__ , np.ndarray) # Test not batched input __lowerCAmelCase: str = image_processing(image_inputs[0] , return_tensors="pt").pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched __lowerCAmelCase: Union[str, Any] = image_processing(snake_case__ , return_tensors="pt").pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def lowercase_ ( self : int)-> Union[str, Any]: '''simple docstring''' __lowerCAmelCase: Tuple = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors __lowerCAmelCase: str = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case__ , torchify=snake_case__) for image in image_inputs: self.assertIsInstance(snake_case__ , torch.Tensor) # Test not batched input __lowerCAmelCase: Union[str, Any] = image_processing(image_inputs[0] , return_tensors="pt").pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched __lowerCAmelCase: Union[str, Any] = image_processing(snake_case__ , return_tensors="pt").pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def lowercase_ ( self : int)-> int: '''simple docstring''' __lowerCAmelCase: List[Any] = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors __lowerCAmelCase: Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=snake_case__ , torchify=snake_case__) __lowerCAmelCase: List[Any] = [] for image in image_inputs: self.assertIsInstance(snake_case__ , torch.Tensor) maps.append(torch.zeros(image.shape[-2:]).long()) # Test not batched input __lowerCAmelCase: List[Any] = image_processing(image_inputs[0] , maps[0] , return_tensors="pt") self.assertEqual( encoding["pixel_values"].shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual( encoding["labels"].shape , ( 1, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual(encoding["labels"].dtype , torch.long) self.assertTrue(encoding["labels"].min().item() >= 0) self.assertTrue(encoding["labels"].max().item() <= 2_5_5) # Test batched __lowerCAmelCase: List[Any] = image_processing(snake_case__ , snake_case__ , return_tensors="pt") self.assertEqual( encoding["pixel_values"].shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual( encoding["labels"].shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual(encoding["labels"].dtype , torch.long) self.assertTrue(encoding["labels"].min().item() >= 0) self.assertTrue(encoding["labels"].max().item() <= 2_5_5) # Test not batched input (PIL images) __lowerCAmelCase: List[Any] = prepare_semantic_single_inputs() __lowerCAmelCase: Any = image_processing(snake_case__ , snake_case__ , return_tensors="pt") self.assertEqual( encoding["pixel_values"].shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual( encoding["labels"].shape , ( 1, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual(encoding["labels"].dtype , torch.long) self.assertTrue(encoding["labels"].min().item() >= 0) self.assertTrue(encoding["labels"].max().item() <= 2_5_5) # Test batched input (PIL images) __lowerCAmelCase: Optional[int] = prepare_semantic_batch_inputs() __lowerCAmelCase: Dict = image_processing(snake_case__ , snake_case__ , return_tensors="pt") self.assertEqual( encoding["pixel_values"].shape , ( 2, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual( encoding["labels"].shape , ( 2, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) self.assertEqual(encoding["labels"].dtype , torch.long) self.assertTrue(encoding["labels"].min().item() >= 0) self.assertTrue(encoding["labels"].max().item() <= 2_5_5) def lowercase_ ( self : int)-> List[Any]: '''simple docstring''' __lowerCAmelCase: str = self.image_processing_class(**self.image_processor_dict) # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150 __lowerCAmelCase: Optional[int] = prepare_semantic_single_inputs() __lowerCAmelCase: int = image_processing(snake_case__ , snake_case__ , return_tensors="pt") self.assertTrue(encoding["labels"].min().item() >= 0) self.assertTrue(encoding["labels"].max().item() <= 1_5_0) __lowerCAmelCase: Union[str, Any] = True __lowerCAmelCase: Any = image_processing(snake_case__ , snake_case__ , return_tensors="pt") self.assertTrue(encoding["labels"].min().item() >= 0) self.assertTrue(encoding["labels"].max().item() <= 2_5_5)
353
"""simple docstring""" import collections import importlib.util import os import re from pathlib import Path __A = "src/transformers" # Matches is_xxx_available() __A = re.compile(r"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} __A = re.compile(r"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __A = re.compile(r"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available __A = re.compile(r"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") __A = re.compile(r"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __A = re.compile(r"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", __A = re.compile("^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], __A = re.compile("^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo __A = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: __A = re.compile(r"^\s*try:") # Catches a line with else: __A = re.compile(r"^\s*else:") def a__ ( __SCREAMING_SNAKE_CASE ) -> Any: if _re_test_backend.search(__SCREAMING_SNAKE_CASE ) is None: return None __lowerCAmelCase: Union[str, Any] = [b[0] for b in _re_backend.findall(__SCREAMING_SNAKE_CASE )] backends.sort() return "_and_".join(__SCREAMING_SNAKE_CASE ) def a__ ( __SCREAMING_SNAKE_CASE ) -> int: with open(__SCREAMING_SNAKE_CASE , "r" , encoding="utf-8" , newline="\n" ) as f: __lowerCAmelCase: Optional[int] = f.readlines() __lowerCAmelCase: Dict = 0 while line_index < len(__SCREAMING_SNAKE_CASE ) and not lines[line_index].startswith("_import_structure = {" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(__SCREAMING_SNAKE_CASE ): return None # First grab the objects without a specific backend in _import_structure __lowerCAmelCase: Optional[Any] = [] while not lines[line_index].startswith("if TYPE_CHECKING" ) and find_backend(lines[line_index] ) is None: __lowerCAmelCase: Optional[int] = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(__SCREAMING_SNAKE_CASE ): __lowerCAmelCase: List[Any] = _re_one_line_import_struct.search(__SCREAMING_SNAKE_CASE ).groups()[0] __lowerCAmelCase: List[Any] = re.findall("\[([^\]]+)\]" , __SCREAMING_SNAKE_CASE ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(", " )] ) line_index += 1 continue __lowerCAmelCase: str = _re_import_struct_key_value.search(__SCREAMING_SNAKE_CASE ) if single_line_import_search is not None: __lowerCAmelCase: str = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(", " ) if len(__SCREAMING_SNAKE_CASE ) > 0] objects.extend(__SCREAMING_SNAKE_CASE ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) line_index += 1 __lowerCAmelCase: Tuple = {"none": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("if TYPE_CHECKING" ): # If the line is an if not is_backend_available, we grab all objects associated. __lowerCAmelCase: Optional[int] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __lowerCAmelCase: Optional[int] = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __lowerCAmelCase: Optional[int] = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 4 ): __lowerCAmelCase: Optional[Any] = lines[line_index] if _re_import_struct_add_one.search(__SCREAMING_SNAKE_CASE ) is not None: objects.append(_re_import_struct_add_one.search(__SCREAMING_SNAKE_CASE ).groups()[0] ) elif _re_import_struct_add_many.search(__SCREAMING_SNAKE_CASE ) is not None: __lowerCAmelCase: Union[str, Any] = _re_import_struct_add_many.search(__SCREAMING_SNAKE_CASE ).groups()[0].split(", " ) __lowerCAmelCase: int = [obj[1:-1] for obj in imports if len(__SCREAMING_SNAKE_CASE ) > 0] objects.extend(__SCREAMING_SNAKE_CASE ) elif _re_between_brackets.search(__SCREAMING_SNAKE_CASE ) is not None: __lowerCAmelCase: Tuple = _re_between_brackets.search(__SCREAMING_SNAKE_CASE ).groups()[0].split(", " ) __lowerCAmelCase: Tuple = [obj[1:-1] for obj in imports if len(__SCREAMING_SNAKE_CASE ) > 0] objects.extend(__SCREAMING_SNAKE_CASE ) elif _re_quote_object.search(__SCREAMING_SNAKE_CASE ) is not None: objects.append(_re_quote_object.search(__SCREAMING_SNAKE_CASE ).groups()[0] ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) elif line.startswith(" " * 1_2 + "\"" ): objects.append(line[1_3:-3] ) line_index += 1 __lowerCAmelCase: Union[str, Any] = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend __lowerCAmelCase: str = [] while ( line_index < len(__SCREAMING_SNAKE_CASE ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("else" ) ): __lowerCAmelCase: List[Any] = lines[line_index] __lowerCAmelCase: Tuple = _re_import.search(__SCREAMING_SNAKE_CASE ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 __lowerCAmelCase: Any = {"none": objects} # Let's continue with backend-specific objects while line_index < len(__SCREAMING_SNAKE_CASE ): # If the line is an if is_backend_available, we grab all objects associated. __lowerCAmelCase: Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __lowerCAmelCase: Any = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __lowerCAmelCase: List[Any] = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 8 ): __lowerCAmelCase: Optional[int] = lines[line_index] __lowerCAmelCase: Any = _re_import.search(__SCREAMING_SNAKE_CASE ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 1_2 ): objects.append(line[1_2:-2] ) line_index += 1 __lowerCAmelCase: Union[str, Any] = objects else: line_index += 1 return import_dict_objects, type_hint_objects def a__ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) -> Tuple: def find_duplicates(__SCREAMING_SNAKE_CASE ): return [k for k, v in collections.Counter(__SCREAMING_SNAKE_CASE ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] __lowerCAmelCase: Optional[int] = [] for key in import_dict_objects.keys(): __lowerCAmelCase: Union[str, Any] = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"Duplicate _import_structure definitions for: {duplicate_imports}" ) __lowerCAmelCase: Optional[Any] = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F"Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}" ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): __lowerCAmelCase: Union[str, Any] = "base imports" if key == "none" else F"{key} backend" errors.append(F"Differences for {name}:" ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F" {a} in TYPE_HINT but not in _import_structure." ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F" {a} in _import_structure but not in TYPE_HINT." ) return errors def a__ ( ) -> Tuple: __lowerCAmelCase: Optional[Any] = [] for root, _, files in os.walk(__SCREAMING_SNAKE_CASE ): if "__init__.py" in files: __lowerCAmelCase: List[str] = os.path.join(__SCREAMING_SNAKE_CASE , "__init__.py" ) __lowerCAmelCase: int = parse_init(__SCREAMING_SNAKE_CASE ) if objects is not None: __lowerCAmelCase: Optional[Any] = analyze_results(*__SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ) > 0: __lowerCAmelCase: Union[str, Any] = F"Problem in {fname}, both halves do not define the same objects.\n{errors[0]}" failures.append("\n".join(__SCREAMING_SNAKE_CASE ) ) if len(__SCREAMING_SNAKE_CASE ) > 0: raise ValueError("\n\n".join(__SCREAMING_SNAKE_CASE ) ) def a__ ( ) -> Any: __lowerCAmelCase: Optional[int] = [] for path, directories, files in os.walk(__SCREAMING_SNAKE_CASE ): for folder in directories: # Ignore private modules if folder.startswith("_" ): directories.remove(__SCREAMING_SNAKE_CASE ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(__SCREAMING_SNAKE_CASE ) / folder).glob("*.py" ) ) ) == 0: continue __lowerCAmelCase: Optional[int] = str((Path(__SCREAMING_SNAKE_CASE ) / folder).relative_to(__SCREAMING_SNAKE_CASE ) ) __lowerCAmelCase: Tuple = short_path.replace(os.path.sep , "." ) submodules.append(__SCREAMING_SNAKE_CASE ) for fname in files: if fname == "__init__.py": continue __lowerCAmelCase: Dict = str((Path(__SCREAMING_SNAKE_CASE ) / fname).relative_to(__SCREAMING_SNAKE_CASE ) ) __lowerCAmelCase: Dict = short_path.replace(".py" , "" ).replace(os.path.sep , "." ) if len(submodule.split("." ) ) == 1: submodules.append(__SCREAMING_SNAKE_CASE ) return submodules __A = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", ] def a__ ( ) -> Optional[int]: # This is to make sure the transformers module imported is the one in the repo. __lowerCAmelCase: Optional[Any] = importlib.util.spec_from_file_location( "transformers" , os.path.join(__SCREAMING_SNAKE_CASE , "__init__.py" ) , submodule_search_locations=[PATH_TO_TRANSFORMERS] , ) __lowerCAmelCase: Optional[int] = spec.loader.load_module() __lowerCAmelCase: str = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys() ] if len(__SCREAMING_SNAKE_CASE ) > 0: __lowerCAmelCase: Optional[int] = "\n".join(F"- {module}" for module in module_not_registered ) raise ValueError( "The following submodules are not properly registered in the main init of Transformers:\n" F"{list_of_modules}\n" "Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value." ) if __name__ == "__main__": check_all_inits() check_submodules()
108
0
'''simple docstring''' def lowercase__( __UpperCamelCase: int = 10 ,__UpperCamelCase: int = 22 ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = range(1 ,__UpperCamelCase ) SCREAMING_SNAKE_CASE : str = range(1 ,__UpperCamelCase ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(F"""{solution(1_0, 2_2) = }""")
251
'''simple docstring''' from __future__ import annotations from scipy.special import comb # type: ignore class _a : '''simple docstring''' def __init__( self, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = list_of_points # Degree determines the flexibility of the curve. # Degree = 1 will produce a straight line. SCREAMING_SNAKE_CASE : Optional[int] = len(A ) - 1 def UpperCamelCase_ ( self, A ): '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." SCREAMING_SNAKE_CASE : list[float] = [] for i in range(len(self.list_of_points ) ): # basis function for each i output_values.append( comb(self.degree, A ) * ((1 - t) ** (self.degree - i)) * (t**i) ) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(A ), 5 ) == 1 return output_values def UpperCamelCase_ ( self, A ): '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." SCREAMING_SNAKE_CASE : str = self.basis_function(A ) SCREAMING_SNAKE_CASE : str = 0.0 SCREAMING_SNAKE_CASE : List[Any] = 0.0 for i in range(len(self.list_of_points ) ): # For all points, sum up the product of i-th basis function and i-th point. x += basis_function[i] * self.list_of_points[i][0] y += basis_function[i] * self.list_of_points[i][1] return (x, y) def UpperCamelCase_ ( self, A = 0.01 ): '''simple docstring''' from matplotlib import pyplot as plt # type: ignore SCREAMING_SNAKE_CASE : list[float] = [] # x coordinates of points to plot SCREAMING_SNAKE_CASE : list[float] = [] # y coordinates of points to plot SCREAMING_SNAKE_CASE : List[str] = 0.0 while t <= 1: SCREAMING_SNAKE_CASE : Optional[int] = self.bezier_curve_function(A ) to_plot_x.append(value[0] ) to_plot_y.append(value[1] ) t += step_size SCREAMING_SNAKE_CASE : List[Any] = [i[0] for i in self.list_of_points] SCREAMING_SNAKE_CASE : Union[str, Any] = [i[1] for i in self.list_of_points] plt.plot( A, A, color='blue', label='Curve of Degree ' + str(self.degree ), ) plt.scatter(A, A, color='red', label='Control Points' ) plt.legend() plt.show() if __name__ == "__main__": import doctest doctest.testmod() BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
251
1
from typing import Optional, Tuple, Union import flax import flax.linen as nn import jax import jax.numpy as jnp from flax.core.frozen_dict import FrozenDict from ..configuration_utils import ConfigMixin, flax_register_to_config from ..utils import BaseOutput from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps from .modeling_flax_utils import FlaxModelMixin from .unet_ad_blocks_flax import ( FlaxCrossAttnDownBlockaD, FlaxDownBlockaD, FlaxUNetMidBlockaDCrossAttn, ) @flax.struct.dataclass class __snake_case (_a ): lowerCAmelCase__ = 42 lowerCAmelCase__ = 42 class __snake_case (nn.Module ): lowerCAmelCase__ = 42 lowerCAmelCase__ = (1_6, 3_2, 9_6, 2_5_6) lowerCAmelCase__ = jnp.floataa def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: '''simple docstring''' _lowerCAmelCase : Dict = nn.Conv( self.block_out_channels[0] , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) _lowerCAmelCase : Any = [] for i in range(len(self.block_out_channels ) - 1 ): _lowerCAmelCase : Any = self.block_out_channels[i] _lowerCAmelCase : Optional[int] = self.block_out_channels[i + 1] _lowerCAmelCase : int = nn.Conv( _UpperCAmelCase , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(_UpperCAmelCase ) _lowerCAmelCase : Dict = nn.Conv( _UpperCAmelCase , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(_UpperCAmelCase ) _lowerCAmelCase : int = blocks _lowerCAmelCase : Optional[int] = nn.Conv( self.conditioning_embedding_channels , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self : Dict , _UpperCAmelCase : str ) -> Optional[Any]: '''simple docstring''' _lowerCAmelCase : str = self.conv_in(_UpperCAmelCase ) _lowerCAmelCase : int = nn.silu(_UpperCAmelCase ) for block in self.blocks: _lowerCAmelCase : Any = block(_UpperCAmelCase ) _lowerCAmelCase : Union[str, Any] = nn.silu(_UpperCAmelCase ) _lowerCAmelCase : List[str] = self.conv_out(_UpperCAmelCase ) return embedding @flax_register_to_config class __snake_case (nn.Module , _a , _a ): lowerCAmelCase__ = 3_2 lowerCAmelCase__ = 4 lowerCAmelCase__ = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) lowerCAmelCase__ = False lowerCAmelCase__ = (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0) lowerCAmelCase__ = 2 lowerCAmelCase__ = 8 lowerCAmelCase__ = None lowerCAmelCase__ = 1_2_8_0 lowerCAmelCase__ = 0.0 lowerCAmelCase__ = False lowerCAmelCase__ = jnp.floataa lowerCAmelCase__ = True lowerCAmelCase__ = 0 lowerCAmelCase__ = "rgb" lowerCAmelCase__ = (1_6, 3_2, 9_6, 2_5_6) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , _UpperCAmelCase : jax.random.KeyArray ) -> FrozenDict: '''simple docstring''' _lowerCAmelCase : int = (1, self.in_channels, self.sample_size, self.sample_size) _lowerCAmelCase : Union[str, Any] = jnp.zeros(_UpperCAmelCase , dtype=jnp.floataa ) _lowerCAmelCase : Any = jnp.ones((1,) , dtype=jnp.intaa ) _lowerCAmelCase : Optional[Any] = jnp.zeros((1, 1, self.cross_attention_dim) , dtype=jnp.floataa ) _lowerCAmelCase : Any = (1, 3, self.sample_size * 8, self.sample_size * 8) _lowerCAmelCase : List[str] = jnp.zeros(_UpperCAmelCase , dtype=jnp.floataa ) _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = jax.random.split(_UpperCAmelCase ) _lowerCAmelCase : Any = {"""params""": params_rng, """dropout""": dropout_rng} return self.init(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase )["params"] def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> str: '''simple docstring''' _lowerCAmelCase : Tuple = self.block_out_channels _lowerCAmelCase : Tuple = block_out_channels[0] * 4 # If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. _lowerCAmelCase : List[str] = self.num_attention_heads or self.attention_head_dim # input _lowerCAmelCase : Optional[Any] = nn.Conv( block_out_channels[0] , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) # time _lowerCAmelCase : str = FlaxTimesteps( block_out_channels[0] , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.config.freq_shift ) _lowerCAmelCase : int = FlaxTimestepEmbedding(_UpperCAmelCase , dtype=self.dtype ) _lowerCAmelCase : Tuple = FlaxControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0] , block_out_channels=self.conditioning_embedding_out_channels , ) _lowerCAmelCase : Any = self.only_cross_attention if isinstance(_UpperCAmelCase , _UpperCAmelCase ): _lowerCAmelCase : Tuple = (only_cross_attention,) * len(self.down_block_types ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ): _lowerCAmelCase : Any = (num_attention_heads,) * len(self.down_block_types ) # down _lowerCAmelCase : Dict = [] _lowerCAmelCase : Union[str, Any] = [] _lowerCAmelCase : List[str] = block_out_channels[0] _lowerCAmelCase : int = nn.Conv( _UpperCAmelCase , kernel_size=(1, 1) , padding="""VALID""" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_UpperCAmelCase ) for i, down_block_type in enumerate(self.down_block_types ): _lowerCAmelCase : List[Any] = output_channel _lowerCAmelCase : Union[str, Any] = block_out_channels[i] _lowerCAmelCase : Tuple = i == len(_UpperCAmelCase ) - 1 if down_block_type == "CrossAttnDownBlock2D": _lowerCAmelCase : Optional[Any] = FlaxCrossAttnDownBlockaD( in_channels=_UpperCAmelCase , out_channels=_UpperCAmelCase , dropout=self.dropout , num_layers=self.layers_per_block , num_attention_heads=num_attention_heads[i] , add_downsample=not is_final_block , use_linear_projection=self.use_linear_projection , only_cross_attention=only_cross_attention[i] , dtype=self.dtype , ) else: _lowerCAmelCase : Optional[int] = FlaxDownBlockaD( in_channels=_UpperCAmelCase , out_channels=_UpperCAmelCase , dropout=self.dropout , num_layers=self.layers_per_block , add_downsample=not is_final_block , dtype=self.dtype , ) down_blocks.append(_UpperCAmelCase ) for _ in range(self.layers_per_block ): _lowerCAmelCase : Any = nn.Conv( _UpperCAmelCase , kernel_size=(1, 1) , padding="""VALID""" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_UpperCAmelCase ) if not is_final_block: _lowerCAmelCase : Any = nn.Conv( _UpperCAmelCase , kernel_size=(1, 1) , padding="""VALID""" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(_UpperCAmelCase ) _lowerCAmelCase : Dict = down_blocks _lowerCAmelCase : Optional[Any] = controlnet_down_blocks # mid _lowerCAmelCase : List[str] = block_out_channels[-1] _lowerCAmelCase : Tuple = FlaxUNetMidBlockaDCrossAttn( in_channels=_UpperCAmelCase , dropout=self.dropout , num_attention_heads=num_attention_heads[-1] , use_linear_projection=self.use_linear_projection , dtype=self.dtype , ) _lowerCAmelCase : int = nn.Conv( _UpperCAmelCase , kernel_size=(1, 1) , padding="""VALID""" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self : Optional[Any] , _UpperCAmelCase : int , _UpperCAmelCase : Tuple , _UpperCAmelCase : Tuple , _UpperCAmelCase : int , _UpperCAmelCase : float = 1.0 , _UpperCAmelCase : bool = True , _UpperCAmelCase : bool = False , ) -> Union[FlaxControlNetOutput, Tuple]: '''simple docstring''' _lowerCAmelCase : List[str] = self.controlnet_conditioning_channel_order if channel_order == "bgr": _lowerCAmelCase : Any = jnp.flip(_UpperCAmelCase , axis=1 ) # 1. time if not isinstance(_UpperCAmelCase , jnp.ndarray ): _lowerCAmelCase : List[Any] = jnp.array([timesteps] , dtype=jnp.intaa ) elif isinstance(_UpperCAmelCase , jnp.ndarray ) and len(timesteps.shape ) == 0: _lowerCAmelCase : Tuple = timesteps.astype(dtype=jnp.floataa ) _lowerCAmelCase : Tuple = jnp.expand_dims(_UpperCAmelCase , 0 ) _lowerCAmelCase : int = self.time_proj(_UpperCAmelCase ) _lowerCAmelCase : int = self.time_embedding(_UpperCAmelCase ) # 2. pre-process _lowerCAmelCase : List[Any] = jnp.transpose(_UpperCAmelCase , (0, 2, 3, 1) ) _lowerCAmelCase : Tuple = self.conv_in(_UpperCAmelCase ) _lowerCAmelCase : List[str] = jnp.transpose(_UpperCAmelCase , (0, 2, 3, 1) ) _lowerCAmelCase : List[Any] = self.controlnet_cond_embedding(_UpperCAmelCase ) sample += controlnet_cond # 3. down _lowerCAmelCase : Optional[Any] = (sample,) for down_block in self.down_blocks: if isinstance(_UpperCAmelCase , _UpperCAmelCase ): _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = down_block(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , deterministic=not train ) else: _lowerCAmelCase , _lowerCAmelCase : List[Any] = down_block(_UpperCAmelCase , _UpperCAmelCase , deterministic=not train ) down_block_res_samples += res_samples # 4. mid _lowerCAmelCase : str = self.mid_block(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , deterministic=not train ) # 5. contronet blocks _lowerCAmelCase : Dict = () for down_block_res_sample, controlnet_block in zip(_UpperCAmelCase , self.controlnet_down_blocks ): _lowerCAmelCase : int = controlnet_block(_UpperCAmelCase ) controlnet_down_block_res_samples += (down_block_res_sample,) _lowerCAmelCase : List[Any] = controlnet_down_block_res_samples _lowerCAmelCase : int = self.controlnet_mid_block(_UpperCAmelCase ) # 6. scaling _lowerCAmelCase : str = [sample * conditioning_scale for sample in down_block_res_samples] mid_block_res_sample *= conditioning_scale if not return_dict: return (down_block_res_samples, mid_block_res_sample) return FlaxControlNetOutput( down_block_res_samples=_UpperCAmelCase , mid_block_res_sample=_UpperCAmelCase )
159
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _lowerCamelCase : List[Any] = logging.get_logger(__name__) _lowerCamelCase : Union[str, Any] = { "kssteven/ibert-roberta-base": "https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json", "kssteven/ibert-roberta-large": "https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json", "kssteven/ibert-roberta-large-mnli": ( "https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json" ), } class __snake_case (_a ): lowerCAmelCase__ = "ibert" def __init__( self : int , _UpperCAmelCase : Optional[int]=3_0522 , _UpperCAmelCase : Union[str, Any]=768 , _UpperCAmelCase : str=12 , _UpperCAmelCase : List[str]=12 , _UpperCAmelCase : Any=3072 , _UpperCAmelCase : Optional[Any]="gelu" , _UpperCAmelCase : Optional[Any]=0.1 , _UpperCAmelCase : Any=0.1 , _UpperCAmelCase : Dict=512 , _UpperCAmelCase : Any=2 , _UpperCAmelCase : Tuple=0.02 , _UpperCAmelCase : str=1E-12 , _UpperCAmelCase : str=1 , _UpperCAmelCase : Any=0 , _UpperCAmelCase : Union[str, Any]=2 , _UpperCAmelCase : List[str]="absolute" , _UpperCAmelCase : Optional[int]=False , _UpperCAmelCase : Any="none" , **_UpperCAmelCase : Optional[int] , ) -> Optional[int]: '''simple docstring''' super().__init__(pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase ) _lowerCAmelCase : str = vocab_size _lowerCAmelCase : Any = hidden_size _lowerCAmelCase : str = num_hidden_layers _lowerCAmelCase : List[Any] = num_attention_heads _lowerCAmelCase : Optional[int] = hidden_act _lowerCAmelCase : Optional[Any] = intermediate_size _lowerCAmelCase : int = hidden_dropout_prob _lowerCAmelCase : Tuple = attention_probs_dropout_prob _lowerCAmelCase : Optional[Any] = max_position_embeddings _lowerCAmelCase : Union[str, Any] = type_vocab_size _lowerCAmelCase : Dict = initializer_range _lowerCAmelCase : Any = layer_norm_eps _lowerCAmelCase : str = position_embedding_type _lowerCAmelCase : int = quant_mode _lowerCAmelCase : str = force_dequant class __snake_case (_a ): @property def SCREAMING_SNAKE_CASE ( self : str ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": _lowerCAmelCase : Optional[int] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _lowerCAmelCase : Optional[Any] = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
159
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase : Tuple = logging.get_logger(__name__) lowerCamelCase : Any = { 'microsoft/markuplm-base': 'https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json', 'microsoft/markuplm-large': 'https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json', } class __lowerCAmelCase (lowercase_ ): '''simple docstring''' lowerCAmelCase__ : Optional[int] = """markuplm""" def __init__(self : Optional[Any] , UpperCamelCase : str=30522 , UpperCamelCase : Tuple=768 , UpperCamelCase : List[str]=12 , UpperCamelCase : List[Any]=12 , UpperCamelCase : List[str]=3072 , UpperCamelCase : Optional[int]="gelu" , UpperCamelCase : Dict=0.1 , UpperCamelCase : str=0.1 , UpperCamelCase : Tuple=512 , UpperCamelCase : Union[str, Any]=2 , UpperCamelCase : int=0.02 , UpperCamelCase : str=1E-12 , UpperCamelCase : List[Any]=0 , UpperCamelCase : Dict=0 , UpperCamelCase : Union[str, Any]=2 , UpperCamelCase : Any=256 , UpperCamelCase : Dict=1024 , UpperCamelCase : int=216 , UpperCamelCase : Optional[int]=1001 , UpperCamelCase : Dict=32 , UpperCamelCase : Optional[int]=50 , UpperCamelCase : str="absolute" , UpperCamelCase : Any=True , UpperCamelCase : Any=None , **UpperCamelCase : Tuple , ): '''simple docstring''' super().__init__( pad_token_id=UpperCamelCase , bos_token_id=UpperCamelCase , eos_token_id=UpperCamelCase , **UpperCamelCase , ) lowercase__ = vocab_size lowercase__ = hidden_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__ = position_embedding_type lowercase__ = use_cache lowercase__ = classifier_dropout # additional properties lowercase__ = max_depth lowercase__ = max_xpath_tag_unit_embeddings lowercase__ = max_xpath_subs_unit_embeddings lowercase__ = tag_pad_id lowercase__ = subs_pad_id lowercase__ = xpath_unit_hidden_size
2
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCamelCase : str = { 'configuration_rag': ['RagConfig'], 'retrieval_rag': ['RagRetriever'], 'tokenization_rag': ['RagTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Dict = [ 'RagModel', 'RagPreTrainedModel', 'RagSequenceForGeneration', 'RagTokenForGeneration', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[int] = [ 'TFRagModel', 'TFRagPreTrainedModel', 'TFRagSequenceForGeneration', 'TFRagTokenForGeneration', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys lowerCamelCase : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
2
1
"""simple docstring""" __A = 0 # The first color of the flag. __A = 1 # The second color of the flag. __A = 2 # The third color of the flag. __A = (red, white, blue) def a__ ( __SCREAMING_SNAKE_CASE ) -> list: if not sequence: return [] if len(__SCREAMING_SNAKE_CASE ) == 1: return list(__SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Any = 0 __lowerCAmelCase: int = len(__SCREAMING_SNAKE_CASE ) - 1 __lowerCAmelCase: List[str] = 0 while mid <= high: if sequence[mid] == colors[0]: __lowerCAmelCase , __lowerCAmelCase: Optional[Any] = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: __lowerCAmelCase , __lowerCAmelCase: Optional[int] = sequence[high], sequence[mid] high -= 1 else: __lowerCAmelCase: List[str] = F"The elements inside the sequence must contains only {colors} values" raise ValueError(__SCREAMING_SNAKE_CASE ) return sequence if __name__ == "__main__": import doctest doctest.testmod() __A = input("Enter numbers separated by commas:\n").strip() __A = [int(item.strip()) for item in user_input.split(",")] print(F'''{dutch_national_flag_sort(unsorted)}''')
108
"""simple docstring""" import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer __A = logging.get_logger(__name__) __A = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __A = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } __A = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } __A = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } __A = { "facebook/dpr-ctx_encoder-single-nq-base": 512, "facebook/dpr-ctx_encoder-multiset-base": 512, } __A = { "facebook/dpr-question_encoder-single-nq-base": 512, "facebook/dpr-question_encoder-multiset-base": 512, } __A = { "facebook/dpr-reader-single-nq-base": 512, "facebook/dpr-reader-multiset-base": 512, } __A = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } __A = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } __A = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class snake_case ( __snake_case ): SCREAMING_SNAKE_CASE_ : List[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ : List[Any] = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ : Any = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE_ : int = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE_ : Union[str, Any] = DPRContextEncoderTokenizer class snake_case ( __snake_case ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ : Optional[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE_ : Optional[int] = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE_ : Tuple = DPRQuestionEncoderTokenizer __A = collections.namedtuple( "DPRSpanPrediction", ["span_score", "relevance_score", "doc_id", "start_index", "end_index", "text"] ) __A = collections.namedtuple("DPRReaderOutput", ["start_logits", "end_logits", "relevance_logits"]) __A = r"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Return:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(__snake_case ) class snake_case : def __call__( self : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Optional[str] = None , UpperCamelCase__ : Optional[str] = None , UpperCamelCase__ : Union[bool, str] = False , UpperCamelCase__ : Union[bool, str] = False , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Optional[Union[str, TensorType]] = None , UpperCamelCase__ : Optional[bool] = None , **UpperCamelCase__ : Dict , )-> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ , return_tensors=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , **UpperCamelCase__ , ) elif titles is None or texts is None: __lowerCAmelCase: Tuple = titles if texts is None else texts return super().__call__( UpperCamelCase__ , UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ , return_tensors=UpperCamelCase__ , return_attention_mask=UpperCamelCase__ , **UpperCamelCase__ , ) __lowerCAmelCase: Dict = titles if not isinstance(UpperCamelCase__ , UpperCamelCase__) else [titles] __lowerCAmelCase: Optional[Any] = texts if not isinstance(UpperCamelCase__ , UpperCamelCase__) else [texts] __lowerCAmelCase: Union[str, Any] = len(UpperCamelCase__) __lowerCAmelCase: List[str] = questions if not isinstance(UpperCamelCase__ , UpperCamelCase__) else [questions] * n_passages assert len(UpperCamelCase__) == len( UpperCamelCase__), f"There should be as many titles than texts but got {len(UpperCamelCase__)} titles and {len(UpperCamelCase__)} texts." __lowerCAmelCase: Tuple = super().__call__(UpperCamelCase__ , UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__)["input_ids"] __lowerCAmelCase: Union[str, Any] = super().__call__(UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__)["input_ids"] __lowerCAmelCase: Optional[int] = { "input_ids": [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(UpperCamelCase__ , UpperCamelCase__) ] } if return_attention_mask is not False: __lowerCAmelCase: Tuple = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id) for input_id in input_ids]) __lowerCAmelCase: List[Any] = attention_mask return self.pad(UpperCamelCase__ , padding=UpperCamelCase__ , max_length=UpperCamelCase__ , return_tensors=UpperCamelCase__) def lowercase_ ( self : List[Any] , UpperCamelCase__ : BatchEncoding , UpperCamelCase__ : DPRReaderOutput , UpperCamelCase__ : int = 1_6 , UpperCamelCase__ : int = 6_4 , UpperCamelCase__ : int = 4 , )-> List[DPRSpanPrediction]: '''simple docstring''' __lowerCAmelCase: List[Any] = reader_input["input_ids"] __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase: List[Any] = reader_output[:3] __lowerCAmelCase: Optional[int] = len(UpperCamelCase__) __lowerCAmelCase: Union[str, Any] = sorted(range(UpperCamelCase__) , reverse=UpperCamelCase__ , key=relevance_logits.__getitem__) __lowerCAmelCase: List[DPRReaderOutput] = [] for doc_id in sorted_docs: __lowerCAmelCase: Any = list(input_ids[doc_id]) # assuming question & title information is at the beginning of the sequence __lowerCAmelCase: Dict = sequence_ids.index(self.sep_token_id , 2) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: __lowerCAmelCase: str = sequence_ids.index(self.pad_token_id) else: __lowerCAmelCase: Union[str, Any] = len(UpperCamelCase__) __lowerCAmelCase: Tuple = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=UpperCamelCase__ , top_spans=UpperCamelCase__ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=UpperCamelCase__ , start_index=UpperCamelCase__ , end_index=UpperCamelCase__ , text=self.decode(sequence_ids[start_index : end_index + 1]) , )) if len(UpperCamelCase__) >= num_spans: break return nbest_spans_predictions[:num_spans] def lowercase_ ( self : int , UpperCamelCase__ : List[int] , UpperCamelCase__ : List[int] , UpperCamelCase__ : int , UpperCamelCase__ : int , )-> List[DPRSpanPrediction]: '''simple docstring''' __lowerCAmelCase: Tuple = [] for start_index, start_score in enumerate(UpperCamelCase__): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length]): scores.append(((start_index, start_index + answer_length), start_score + end_score)) __lowerCAmelCase: Tuple = sorted(UpperCamelCase__ , key=lambda UpperCamelCase__: x[1] , reverse=UpperCamelCase__) __lowerCAmelCase: Tuple = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"Wrong span indices: [{start_index}:{end_index}]" __lowerCAmelCase: Any = end_index - start_index + 1 assert length <= max_answer_length, f"Span is too long: {length} > {max_answer_length}" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals): continue chosen_span_intervals.append((start_index, end_index)) if len(UpperCamelCase__) == top_spans: break return chosen_span_intervals @add_end_docstrings(__snake_case ) class snake_case ( __snake_case, __snake_case ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ : str = READER_PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ : List[str] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE_ : int = READER_PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE_ : Optional[Any] = ["""input_ids""", """attention_mask"""] SCREAMING_SNAKE_CASE_ : Optional[int] = DPRReaderTokenizer
108
1
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 = DUMMY_UNKNOWN_IDENTIFIER # An actual model hosted on huggingface.co __snake_case = "main" # Default branch name __snake_case = "f2c752cfc5c0ab6f4bdec59acea69eefbee381c2" # One particular commit (not the top of `main`) __snake_case = "aaaaaaa" # This commit does not exist, so we should 404. __snake_case = "d9e9f15bc825e4b2c9249e9578f884bbcb5e3684" # Sha-1 of config.json on the top of `main`, for checking purposes __snake_case = "4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3" @contextlib.contextmanager def _A ( ): print('''Welcome!''' ) yield print('''Bye!''' ) @contextlib.contextmanager def _A ( ): print('''Bonjour!''' ) yield print('''Au revoir!''' ) class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase ( self ) -> str: assert transformers.__spec__ is not None assert importlib.util.find_spec('''transformers''' ) is not None class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @unittest.mock.patch('''sys.stdout''' , new_callable=io.StringIO ) def UpperCAmelCase ( self , SCREAMING_SNAKE_CASE_ ) -> Tuple: 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 UpperCAmelCase ( self , SCREAMING_SNAKE_CASE_ ) -> 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 UpperCAmelCase ( self , SCREAMING_SNAKE_CASE_ ) -> Any: 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 UpperCAmelCase ( self ) -> Dict: self.assertEqual(find_labels(_a ) , ['''labels'''] ) self.assertEqual(find_labels(_a ) , ['''labels''', '''next_sentence_label'''] ) self.assertEqual(find_labels(_a ) , ['''start_positions''', '''end_positions'''] ) class UpperCAmelCase_ ( UpperCamelCase_ ): """simple docstring""" pass self.assertEqual(find_labels(_a ) , ['''labels'''] ) @require_tf def UpperCAmelCase ( self ) -> str: self.assertEqual(find_labels(_a ) , ['''labels'''] ) self.assertEqual(find_labels(_a ) , ['''labels''', '''next_sentence_label'''] ) self.assertEqual(find_labels(_a ) , ['''start_positions''', '''end_positions'''] ) class UpperCAmelCase_ ( UpperCamelCase_ ): """simple docstring""" pass self.assertEqual(find_labels(_a ) , ['''labels'''] ) @require_flax def UpperCAmelCase ( self ) -> Union[str, Any]: self.assertEqual(find_labels(_a ) , [] ) self.assertEqual(find_labels(_a ) , [] ) self.assertEqual(find_labels(_a ) , [] ) class UpperCAmelCase_ ( UpperCamelCase_ ): """simple docstring""" pass self.assertEqual(find_labels(_a ) , [] )
259
"""simple docstring""" def _lowercase ( ) -> int: return [ a * b * (1000 - a - b) for a in range(1 , 999 ) for b in range(__lowerCAmelCase , 999 ) if (a * a + b * b == (1000 - a - b) ** 2) ][0] if __name__ == "__main__": print(f'{solution() = }')
132
0
import argparse import torch from transformers import ( WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForAudioFrameClassification, WavaVecaForSequenceClassification, WavaVecaForXVector, logging, ) logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def _A ( lowerCAmelCase_ : str , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Optional[Any] ): """simple docstring""" lowerCAmelCase__ = WavaVecaForSequenceClassification.from_pretrained(lowerCAmelCase_ , config=lowerCAmelCase_ ) lowerCAmelCase__ = downstream_dict["projector.weight"] lowerCAmelCase__ = downstream_dict["projector.bias"] lowerCAmelCase__ = downstream_dict["model.post_net.linear.weight"] lowerCAmelCase__ = downstream_dict["model.post_net.linear.bias"] return model def _A ( lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Any ): """simple docstring""" lowerCAmelCase__ = WavaVecaForAudioFrameClassification.from_pretrained(lowerCAmelCase_ , config=lowerCAmelCase_ ) lowerCAmelCase__ = downstream_dict["model.linear.weight"] lowerCAmelCase__ = downstream_dict["model.linear.bias"] return model def _A ( lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : Any , lowerCAmelCase_ : Union[str, Any] ): """simple docstring""" lowerCAmelCase__ = WavaVecaForXVector.from_pretrained(lowerCAmelCase_ , config=lowerCAmelCase_ ) lowerCAmelCase__ = downstream_dict["connector.weight"] lowerCAmelCase__ = downstream_dict["connector.bias"] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): lowerCAmelCase__ = downstream_dict[ F'model.framelevel_feature_extractor.module.{i}.kernel.weight' ] lowerCAmelCase__ = downstream_dict[F'model.framelevel_feature_extractor.module.{i}.kernel.bias'] lowerCAmelCase__ = downstream_dict["model.utterancelevel_feature_extractor.linear1.weight"] lowerCAmelCase__ = downstream_dict["model.utterancelevel_feature_extractor.linear1.bias"] lowerCAmelCase__ = downstream_dict["model.utterancelevel_feature_extractor.linear2.weight"] lowerCAmelCase__ = downstream_dict["model.utterancelevel_feature_extractor.linear2.bias"] lowerCAmelCase__ = downstream_dict["objective.W"] return model @torch.no_grad() def _A ( lowerCAmelCase_ : str , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : Any , lowerCAmelCase_ : List[str] ): """simple docstring""" lowerCAmelCase__ = torch.load(lowerCAmelCase_ , map_location="cpu" ) lowerCAmelCase__ = checkpoint["Downstream"] lowerCAmelCase__ = WavaVecaConfig.from_pretrained(lowerCAmelCase_ ) lowerCAmelCase__ = WavaVecaFeatureExtractor.from_pretrained( lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , do_normalize=lowerCAmelCase_ ) lowerCAmelCase__ = hf_config.architectures[0] if arch.endswith("ForSequenceClassification" ): lowerCAmelCase__ = convert_classification(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) elif arch.endswith("ForAudioFrameClassification" ): lowerCAmelCase__ = convert_diarization(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) elif arch.endswith("ForXVector" ): lowerCAmelCase__ = convert_xvector(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) else: raise NotImplementedError(F'S3PRL weights conversion is not supported for {arch}' ) if hf_config.use_weighted_layer_sum: lowerCAmelCase__ = checkpoint["Featurizer"]["weights"] hf_feature_extractor.save_pretrained(lowerCAmelCase_ ) hf_model.save_pretrained(lowerCAmelCase_ ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.' ) parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.') parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.') UpperCamelCase = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
221
import string def _A ( lowerCAmelCase_ : str ): """simple docstring""" for key in range(len(string.ascii_uppercase ) ): lowerCAmelCase__ = "" for symbol in message: if symbol in string.ascii_uppercase: lowerCAmelCase__ = string.ascii_uppercase.find(lowerCAmelCase_ ) lowerCAmelCase__ = num - key if num < 0: lowerCAmelCase__ = num + len(string.ascii_uppercase ) lowerCAmelCase__ = translated + string.ascii_uppercase[num] else: lowerCAmelCase__ = translated + symbol print(F'Decryption using Key #{key}: {translated}' ) def _A ( ): """simple docstring""" lowerCAmelCase__ = input("Encrypted message: " ) lowerCAmelCase__ = message.upper() decrypt(lowerCAmelCase_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
221
1
"""simple docstring""" import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def A ( snake_case :Features ) -> Optional[int]: __UpperCamelCase = np.inf def set_batch_size(snake_case :FeatureType ) -> None: nonlocal batch_size if isinstance(snake_case , snake_case ): __UpperCamelCase = min(snake_case , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(snake_case , snake_case ): __UpperCamelCase = min(snake_case , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(snake_case , snake_case ) and feature.dtype == "binary": __UpperCamelCase = min(snake_case , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(snake_case , snake_case ) return None if batch_size is np.inf else batch_size class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): def __init__( self , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = False , __UpperCAmelCase = False , __UpperCAmelCase = None , **__UpperCAmelCase , ): '''simple docstring''' super().__init__( __UpperCAmelCase , split=__UpperCAmelCase , features=__UpperCAmelCase , cache_dir=__UpperCAmelCase , keep_in_memory=__UpperCAmelCase , streaming=__UpperCAmelCase , num_proc=__UpperCAmelCase , **__UpperCAmelCase , ) __UpperCamelCase = path_or_paths if isinstance(__UpperCAmelCase , __UpperCAmelCase ) else {self.split: path_or_paths} __UpperCamelCase = _PACKAGED_DATASETS_MODULES['parquet'][1] __UpperCamelCase = Parquet( cache_dir=__UpperCAmelCase , data_files=__UpperCAmelCase , features=__UpperCAmelCase , hash=__UpperCAmelCase , **__UpperCAmelCase , ) def UpperCAmelCase ( self ): '''simple docstring''' if self.streaming: __UpperCamelCase = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = None self.builder.download_and_prepare( download_config=__UpperCAmelCase , download_mode=__UpperCAmelCase , verification_mode=__UpperCAmelCase , base_path=__UpperCAmelCase , num_proc=self.num_proc , ) __UpperCamelCase = self.builder.as_dataset( split=self.split , verification_mode=__UpperCAmelCase , in_memory=self.keep_in_memory ) return dataset class __lowerCAmelCase : def __init__( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , **__UpperCAmelCase , ): '''simple docstring''' __UpperCamelCase = dataset __UpperCamelCase = path_or_buf __UpperCamelCase = batch_size or get_writer_batch_size(dataset.features ) __UpperCamelCase = parquet_writer_kwargs def UpperCAmelCase ( self ): '''simple docstring''' __UpperCamelCase = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with open(self.path_or_buf , 'wb+' ) as buffer: __UpperCamelCase = self._write(file_obj=__UpperCAmelCase , batch_size=__UpperCAmelCase , **self.parquet_writer_kwargs ) else: __UpperCamelCase = self._write(file_obj=self.path_or_buf , batch_size=__UpperCAmelCase , **self.parquet_writer_kwargs ) return written def UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , **__UpperCAmelCase ): '''simple docstring''' __UpperCamelCase = 0 __UpperCamelCase = parquet_writer_kwargs.pop('path_or_buf' , __UpperCAmelCase ) __UpperCamelCase = self.dataset.features.arrow_schema __UpperCamelCase = pq.ParquetWriter(__UpperCAmelCase , schema=__UpperCAmelCase , **__UpperCAmelCase ) for offset in logging.tqdm( range(0 , len(self.dataset ) , __UpperCAmelCase ) , unit='ba' , disable=not logging.is_progress_bar_enabled() , desc='Creating parquet from Arrow format' , ): __UpperCamelCase = query_table( table=self.dataset._data , key=slice(__UpperCAmelCase , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(__UpperCAmelCase ) written += batch.nbytes writer.close() return written
316
"""simple docstring""" def A ( snake_case :int = 1_0 , snake_case :int = 2_2 ) -> int: __UpperCamelCase = range(1 , snake_case ) __UpperCamelCase = range(1 , snake_case ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(f'''{solution(1_0, 2_2) = }''')
316
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _A = { """configuration_swinv2""": ["""SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Swinv2Config"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ """SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST""", """Swinv2ForImageClassification""", """Swinv2ForMaskedImageModeling""", """Swinv2Model""", """Swinv2PreTrainedModel""", ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys _A = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
166
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { """transfo-xl-wt103""": """https://huggingface.co/transfo-xl-wt103/resolve/main/config.json""", } class lowerCamelCase ( lowerCAmelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE = 'transfo-xl' SCREAMING_SNAKE_CASE = ['mems'] SCREAMING_SNAKE_CASE = { 'n_token': 'vocab_size', 'hidden_size': 'd_model', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__(self , _lowerCamelCase=267735 , _lowerCamelCase=[20000, 40000, 200000] , _lowerCamelCase=1024 , _lowerCamelCase=1024 , _lowerCamelCase=16 , _lowerCamelCase=64 , _lowerCamelCase=4096 , _lowerCamelCase=4 , _lowerCamelCase=False , _lowerCamelCase=18 , _lowerCamelCase=1600 , _lowerCamelCase=1000 , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=0 , _lowerCamelCase=-1 , _lowerCamelCase=True , _lowerCamelCase=0.1 , _lowerCamelCase=0.0 , _lowerCamelCase=True , _lowerCamelCase="normal" , _lowerCamelCase=0.01 , _lowerCamelCase=0.01 , _lowerCamelCase=0.02 , _lowerCamelCase=1e-5 , _lowerCamelCase=0 , **_lowerCamelCase , ): """simple docstring""" UpperCAmelCase__ : Any = vocab_size UpperCAmelCase__ : Dict = [] self.cutoffs.extend(_lowerCamelCase ) if proj_share_all_but_first: UpperCAmelCase__ : Optional[int] = [False] + [True] * len(self.cutoffs ) else: UpperCAmelCase__ : List[Any] = [False] + [False] * len(self.cutoffs ) UpperCAmelCase__ : Dict = d_model UpperCAmelCase__ : Dict = d_embed UpperCAmelCase__ : List[Any] = d_head UpperCAmelCase__ : List[str] = d_inner UpperCAmelCase__ : Any = div_val UpperCAmelCase__ : str = pre_lnorm UpperCAmelCase__ : int = n_layer UpperCAmelCase__ : Optional[Any] = n_head UpperCAmelCase__ : Tuple = mem_len UpperCAmelCase__ : Dict = same_length UpperCAmelCase__ : Union[str, Any] = attn_type UpperCAmelCase__ : Optional[int] = clamp_len UpperCAmelCase__ : str = sample_softmax UpperCAmelCase__ : Any = adaptive UpperCAmelCase__ : List[Any] = dropout UpperCAmelCase__ : List[Any] = dropatt UpperCAmelCase__ : Tuple = untie_r UpperCAmelCase__ : str = init UpperCAmelCase__ : Optional[int] = init_range UpperCAmelCase__ : Tuple = proj_init_std UpperCAmelCase__ : str = init_std UpperCAmelCase__ : List[str] = layer_norm_epsilon super().__init__(eos_token_id=_lowerCamelCase , **_lowerCamelCase ) @property def _a (self ): """simple docstring""" logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" ) return -1 @max_position_embeddings.setter def _a (self , _lowerCamelCase ): """simple docstring""" raise NotImplementedError( F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
166
1
'''simple docstring''' # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( """stable diffusion controlnet""", """0.22.0""", """Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.""", standard_warn=False, stacklevel=3, )
309
'''simple docstring''' import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def _UpperCAmelCase ( ) -> Tuple: _lowerCAmelCase : List[Any] = """https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png""" _lowerCAmelCase : int = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert("""RGB""" ) return image def _UpperCAmelCase ( _lowerCamelCase : Any ) -> Dict: _lowerCAmelCase : str = [] # fmt: off # vision encoder rename_keys.append(("""visual_encoder.cls_token""", """vision_model.embeddings.class_embedding""") ) rename_keys.append(("""visual_encoder.pos_embed""", """vision_model.embeddings.position_embedding""") ) rename_keys.append(("""visual_encoder.patch_embed.proj.weight""", """vision_model.embeddings.patch_embedding.weight""") ) rename_keys.append(("""visual_encoder.patch_embed.proj.bias""", """vision_model.embeddings.patch_embedding.bias""") ) rename_keys.append(("""ln_vision.weight""", """vision_model.post_layernorm.weight""") ) rename_keys.append(("""ln_vision.bias""", """vision_model.post_layernorm.bias""") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'visual_encoder.blocks.{i}.norm1.weight', f'vision_model.encoder.layers.{i}.layer_norm1.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.norm1.bias', f'vision_model.encoder.layers.{i}.layer_norm1.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.norm2.weight', f'vision_model.encoder.layers.{i}.layer_norm2.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.norm2.bias', f'vision_model.encoder.layers.{i}.layer_norm2.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.attn.qkv.weight', f'vision_model.encoder.layers.{i}.self_attn.qkv.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.attn.proj.weight', f'vision_model.encoder.layers.{i}.self_attn.projection.weight',) ) rename_keys.append((f'visual_encoder.blocks.{i}.attn.proj.bias', f'vision_model.encoder.layers.{i}.self_attn.projection.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc1.weight', f'vision_model.encoder.layers.{i}.mlp.fc1.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc1.bias', f'vision_model.encoder.layers.{i}.mlp.fc1.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc2.weight', f'vision_model.encoder.layers.{i}.mlp.fc2.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc2.bias', f'vision_model.encoder.layers.{i}.mlp.fc2.bias') ) # QFormer rename_keys.append(("""Qformer.bert.embeddings.LayerNorm.weight""", """qformer.layernorm.weight""") ) rename_keys.append(("""Qformer.bert.embeddings.LayerNorm.bias""", """qformer.layernorm.bias""") ) # fmt: on return rename_keys def _UpperCAmelCase ( _lowerCamelCase : List[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : List[Any] ) -> Optional[Any]: _lowerCAmelCase : str = dct.pop(_lowerCamelCase ) _lowerCAmelCase : str = val def _UpperCAmelCase ( _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Tuple ) -> Tuple: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases _lowerCAmelCase : Tuple = state_dict.pop(f'visual_encoder.blocks.{i}.attn.q_bias' ) _lowerCAmelCase : Optional[Any] = state_dict.pop(f'visual_encoder.blocks.{i}.attn.v_bias' ) # next, set bias in the state dict _lowerCAmelCase : int = torch.cat((q_bias, torch.zeros_like(_lowerCamelCase , requires_grad=_lowerCamelCase ), v_bias) ) _lowerCAmelCase : str = qkv_bias def _UpperCAmelCase ( _lowerCamelCase : Optional[Any] , _lowerCamelCase : Union[str, Any] ) -> List[Any]: _lowerCAmelCase : str = 3_64 if """coco""" in model_name else 2_24 _lowerCAmelCase : str = BlipaVisionConfig(image_size=_lowerCamelCase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: _lowerCAmelCase : int = OPTConfig.from_pretrained("""facebook/opt-2.7b""" , eos_token_id=_lowerCamelCase ).to_dict() elif "opt-6.7b" in model_name: _lowerCAmelCase : Union[str, Any] = OPTConfig.from_pretrained("""facebook/opt-6.7b""" , eos_token_id=_lowerCamelCase ).to_dict() elif "t5-xl" in model_name: _lowerCAmelCase : Optional[int] = TaConfig.from_pretrained("""google/flan-t5-xl""" , dense_act_fn="""gelu""" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: _lowerCAmelCase : str = TaConfig.from_pretrained("""google/flan-t5-xxl""" , dense_act_fn="""gelu""" , bos_token_id=1 ).to_dict() _lowerCAmelCase : Dict = BlipaConfig(vision_config=_lowerCamelCase , text_config=_lowerCamelCase ) return config, image_size @torch.no_grad() def _UpperCAmelCase ( _lowerCamelCase : Tuple , _lowerCamelCase : List[Any]=None , _lowerCamelCase : int=False ) -> List[str]: _lowerCAmelCase : int = ( AutoTokenizer.from_pretrained("""facebook/opt-2.7b""" ) if """opt""" in model_name else AutoTokenizer.from_pretrained("""google/flan-t5-xl""" ) ) _lowerCAmelCase : List[Any] = tokenizer("""\n""" , add_special_tokens=_lowerCamelCase ).input_ids[0] _lowerCAmelCase , _lowerCAmelCase : List[str] = get_blipa_config(_lowerCamelCase , eos_token_id=_lowerCamelCase ) _lowerCAmelCase : Optional[int] = BlipaForConditionalGeneration(_lowerCamelCase ).eval() _lowerCAmelCase : Union[str, Any] = { """blip2-opt-2.7b""": ("""blip2_opt""", """pretrain_opt2.7b"""), """blip2-opt-6.7b""": ("""blip2_opt""", """pretrain_opt6.7b"""), """blip2-opt-2.7b-coco""": ("""blip2_opt""", """caption_coco_opt2.7b"""), """blip2-opt-6.7b-coco""": ("""blip2_opt""", """caption_coco_opt6.7b"""), """blip2-flan-t5-xl""": ("""blip2_t5""", """pretrain_flant5xl"""), """blip2-flan-t5-xl-coco""": ("""blip2_t5""", """caption_coco_flant5xl"""), """blip2-flan-t5-xxl""": ("""blip2_t5""", """pretrain_flant5xxl"""), } _lowerCAmelCase , _lowerCAmelCase : List[str] = model_name_to_original[model_name] # load original model print("""Loading original model...""" ) _lowerCAmelCase : Dict = """cuda""" if torch.cuda.is_available() else """cpu""" _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Optional[int] = load_model_and_preprocess( name=_lowerCamelCase , model_type=_lowerCamelCase , is_eval=_lowerCamelCase , device=_lowerCamelCase ) original_model.eval() print("""Done!""" ) # update state dict keys _lowerCAmelCase : List[Any] = original_model.state_dict() _lowerCAmelCase : Optional[int] = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): _lowerCAmelCase : Tuple = state_dict.pop(_lowerCamelCase ) if key.startswith("""Qformer.bert""" ): _lowerCAmelCase : List[Any] = key.replace("""Qformer.bert""" , """qformer""" ) if "attention.self" in key: _lowerCAmelCase : Optional[int] = key.replace("""self""" , """attention""" ) if "opt_proj" in key: _lowerCAmelCase : Dict = key.replace("""opt_proj""" , """language_projection""" ) if "t5_proj" in key: _lowerCAmelCase : Tuple = key.replace("""t5_proj""" , """language_projection""" ) if key.startswith("""opt""" ): _lowerCAmelCase : List[Any] = key.replace("""opt""" , """language""" ) if key.startswith("""t5""" ): _lowerCAmelCase : int = key.replace("""t5""" , """language""" ) _lowerCAmelCase : Tuple = val # read in qv biases read_in_q_v_bias(_lowerCamelCase , _lowerCamelCase ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = hf_model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) assert len(_lowerCamelCase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] _lowerCAmelCase : Union[str, Any] = load_demo_image() _lowerCAmelCase : Optional[int] = vis_processors["""eval"""](_lowerCamelCase ).unsqueeze(0 ).to(_lowerCamelCase ) _lowerCAmelCase : List[str] = tokenizer(["""\n"""] , return_tensors="""pt""" ).input_ids.to(_lowerCamelCase ) # create processor _lowerCAmelCase : Optional[int] = BlipImageProcessor( size={"""height""": image_size, """width""": image_size} , image_mean=_lowerCamelCase , image_std=_lowerCamelCase ) _lowerCAmelCase : Tuple = BlipaProcessor(image_processor=_lowerCamelCase , tokenizer=_lowerCamelCase ) _lowerCAmelCase : Any = processor(images=_lowerCamelCase , return_tensors="""pt""" ).pixel_values.to(_lowerCamelCase ) # make sure processor creates exact same pixel values assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) original_model.to(_lowerCamelCase ) hf_model.to(_lowerCamelCase ) with torch.no_grad(): if "opt" in model_name: _lowerCAmelCase : Optional[Any] = original_model({"""image""": original_pixel_values, """text_input""": [""""""]} ).logits _lowerCAmelCase : Optional[Any] = hf_model(_lowerCamelCase , _lowerCamelCase ).logits else: _lowerCAmelCase : List[Any] = original_model( {"""image""": original_pixel_values, """text_input""": ["""\n"""], """text_output""": ["""\n"""]} ).logits _lowerCAmelCase : Tuple = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) _lowerCAmelCase : Dict = hf_model(_lowerCamelCase , _lowerCamelCase , labels=_lowerCamelCase ).logits assert original_logits.shape == logits.shape print("""First values of original logits:""" , original_logits[0, :3, :3] ) print("""First values of HF logits:""" , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": _lowerCAmelCase : Any = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=_lowerCamelCase ) assert torch.allclose(logits[0, :3, :3] , _lowerCamelCase , atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": _lowerCAmelCase : List[Any] = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=_lowerCamelCase ) else: # cast to same type _lowerCAmelCase : Union[str, Any] = logits.dtype assert torch.allclose(original_logits.to(_lowerCamelCase ) , _lowerCamelCase , atol=1e-2 ) print("""Looks ok!""" ) print("""Generating a caption...""" ) _lowerCAmelCase : Optional[int] = """""" _lowerCAmelCase : Union[str, Any] = tokenizer(_lowerCamelCase , return_tensors="""pt""" ).input_ids.to(_lowerCamelCase ) _lowerCAmelCase : List[Any] = original_model.generate({"""image""": original_pixel_values} ) _lowerCAmelCase : Dict = hf_model.generate( _lowerCamelCase , _lowerCamelCase , do_sample=_lowerCamelCase , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print("""Original generation:""" , _lowerCamelCase ) _lowerCAmelCase : int = input_ids.shape[1] _lowerCAmelCase : str = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_lowerCamelCase ) _lowerCAmelCase : List[str] = [text.strip() for text in output_text] print("""HF generation:""" , _lowerCamelCase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_lowerCamelCase ) hf_model.save_pretrained(_lowerCamelCase ) if push_to_hub: processor.push_to_hub(f'nielsr/{model_name}' ) hf_model.push_to_hub(f'nielsr/{model_name}' ) if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() UpperCamelCase_ = [ """blip2-opt-2.7b""", """blip2-opt-6.7b""", """blip2-opt-2.7b-coco""", """blip2-opt-6.7b-coco""", """blip2-flan-t5-xl""", """blip2-flan-t5-xl-coco""", """blip2-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""blip2-opt-2.7b""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) UpperCamelCase_ = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
309
1
"""simple docstring""" def UpperCamelCase ( lowerCAmelCase__ ): '''simple docstring''' if upper_limit < 0: raise ValueError('''Limit for the Catalan sequence must be ≥ 0''' ) lowercase = [0] * (upper_limit + 1) # Base case: C(0) = C(1) = 1 lowercase = 1 if upper_limit > 0: lowercase = 1 # Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i for i in range(2 , upper_limit + 1 ): for j in range(lowerCAmelCase__ ): catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1] return catalan_list if __name__ == "__main__": print("\n********* Catalan Numbers Using Dynamic Programming ************\n") print("\n*** Enter -1 at any time to quit ***") print("\nEnter the upper limit (≥ 0) for the Catalan number sequence: ", end="") try: while True: lowercase__ :Union[str, Any] = int(input().strip()) if N < 0: print("\n********* Goodbye!! ************") break else: print(F'The Catalan numbers from 0 through {N} are:') print(catalan_numbers(N)) print("Try another upper limit for the sequence: ", end="") except (NameError, ValueError): print("\n********* Invalid input, goodbye! ************\n") import doctest doctest.testmod()
357
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, is_vision_available, ) lowercase__ :str = {"configuration_vit": ["VIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTConfig", "ViTOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ :str = ["ViTFeatureExtractor"] lowercase__ :int = ["ViTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ :int = [ "VIT_PRETRAINED_MODEL_ARCHIVE_LIST", "ViTForImageClassification", "ViTForMaskedImageModeling", "ViTModel", "ViTPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ :Any = [ "TFViTForImageClassification", "TFViTModel", "TFViTPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ :Optional[int] = [ "FlaxViTForImageClassification", "FlaxViTModel", "FlaxViTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_vit import ViTFeatureExtractor from .image_processing_vit import ViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit import ( VIT_PRETRAINED_MODEL_ARCHIVE_LIST, ViTForImageClassification, ViTForMaskedImageModeling, ViTModel, ViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel else: import sys lowercase__ :Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
97
0
lowerCamelCase_ = frozenset( [ '''prompt''', '''height''', '''width''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', '''cross_attention_kwargs''', ] ) lowerCamelCase_ = frozenset(['''prompt''', '''negative_prompt''']) lowerCamelCase_ = frozenset([]) lowerCamelCase_ = frozenset(['''image''']) lowerCamelCase_ = frozenset( [ '''image''', '''height''', '''width''', '''guidance_scale''', ] ) lowerCamelCase_ = frozenset(['''image''']) lowerCamelCase_ = frozenset( [ '''prompt''', '''image''', '''height''', '''width''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', ] ) lowerCamelCase_ = frozenset(['''prompt''', '''image''', '''negative_prompt''']) lowerCamelCase_ = frozenset( [ # Text guided image variation with an image mask '''prompt''', '''image''', '''mask_image''', '''height''', '''width''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', ] ) lowerCamelCase_ = frozenset(['''prompt''', '''image''', '''mask_image''', '''negative_prompt''']) lowerCamelCase_ = frozenset( [ # image variation with an image mask '''image''', '''mask_image''', '''height''', '''width''', '''guidance_scale''', ] ) lowerCamelCase_ = frozenset(['''image''', '''mask_image''']) lowerCamelCase_ = frozenset( [ '''example_image''', '''image''', '''mask_image''', '''height''', '''width''', '''guidance_scale''', ] ) lowerCamelCase_ = frozenset(['''example_image''', '''image''', '''mask_image''']) lowerCamelCase_ = frozenset(['''class_labels''']) lowerCamelCase_ = frozenset(['''class_labels''']) lowerCamelCase_ = frozenset(['''batch_size''']) lowerCamelCase_ = frozenset([]) lowerCamelCase_ = frozenset(['''batch_size''']) lowerCamelCase_ = frozenset([]) lowerCamelCase_ = frozenset( [ '''prompt''', '''audio_length_in_s''', '''guidance_scale''', '''negative_prompt''', '''prompt_embeds''', '''negative_prompt_embeds''', '''cross_attention_kwargs''', ] ) lowerCamelCase_ = frozenset(['''prompt''', '''negative_prompt''']) lowerCamelCase_ = frozenset(['''input_tokens''']) lowerCamelCase_ = frozenset(['''input_tokens'''])
244
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''', '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''', '''uclanlp/visualbert-vqa-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''', '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''', '''uclanlp/visualbert-vcr-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json''' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class __A( __lowerCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE__ = """visual_bert""" def __init__(self , SCREAMING_SNAKE_CASE_=3_05_22 , SCREAMING_SNAKE_CASE_=7_68 , SCREAMING_SNAKE_CASE_=5_12 , SCREAMING_SNAKE_CASE_=12 , SCREAMING_SNAKE_CASE_=12 , SCREAMING_SNAKE_CASE_=30_72 , SCREAMING_SNAKE_CASE_="gelu" , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=5_12 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=0.02 , SCREAMING_SNAKE_CASE_=1E-12 , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=1 , SCREAMING_SNAKE_CASE_=0 , SCREAMING_SNAKE_CASE_=2 , **SCREAMING_SNAKE_CASE_ , ): super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = vocab_size UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = hidden_size UpperCamelCase__ = visual_embedding_dim 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__ = type_vocab_size UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = bypass_transformer UpperCamelCase__ = special_visual_initialize
244
1
from ....utils import logging _snake_case = logging.get_logger(__name__) class UpperCAmelCase_ ( a): def __init__( self, __a, __a=None, __a=2048): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = config.__dict__ _lowerCAmelCase : List[Any] = modal_hidden_size if num_labels: _lowerCAmelCase : Optional[int] = num_labels
300
import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, Pipeline, ZeroShotClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. _snake_case = {"LayoutLMv2Config", "LayoutLMv3Config"} @is_pipeline_test class UpperCAmelCase_ ( unittest.TestCase): lowerCamelCase__ = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING lowerCamelCase__ = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: lowerCamelCase__ = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: lowerCamelCase__ = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } def snake_case__ ( self, __a, __a, __a): '''simple docstring''' _lowerCAmelCase : str = ZeroShotClassificationPipeline( model=__a, tokenizer=__a, candidate_labels=["polics", "health"]) return classifier, ["Who are you voting for in 2020?", "My stomach hurts."] def snake_case__ ( self, __a, __a): '''simple docstring''' _lowerCAmelCase : List[Any] = classifier("Who are you voting for in 2020?", candidate_labels="politics") self.assertEqual(__a, {"sequence": ANY(__a), "labels": [ANY(__a)], "scores": [ANY(__a)]}) # No kwarg _lowerCAmelCase : int = classifier("Who are you voting for in 2020?", ["politics"]) self.assertEqual(__a, {"sequence": ANY(__a), "labels": [ANY(__a)], "scores": [ANY(__a)]}) _lowerCAmelCase : Tuple = classifier("Who are you voting for in 2020?", candidate_labels=["politics"]) self.assertEqual(__a, {"sequence": ANY(__a), "labels": [ANY(__a)], "scores": [ANY(__a)]}) _lowerCAmelCase : List[Any] = classifier("Who are you voting for in 2020?", candidate_labels="politics, public health") self.assertEqual( __a, {"sequence": ANY(__a), "labels": [ANY(__a), ANY(__a)], "scores": [ANY(__a), ANY(__a)]}) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"])), 1.0) _lowerCAmelCase : List[str] = classifier("Who are you voting for in 2020?", candidate_labels=["politics", "public health"]) self.assertEqual( __a, {"sequence": ANY(__a), "labels": [ANY(__a), ANY(__a)], "scores": [ANY(__a), ANY(__a)]}) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"])), 1.0) _lowerCAmelCase : List[Any] = classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template="This text is about {}") self.assertEqual(__a, {"sequence": ANY(__a), "labels": [ANY(__a)], "scores": [ANY(__a)]}) # https://github.com/huggingface/transformers/issues/13846 _lowerCAmelCase : Optional[int] = classifier(["I am happy"], ["positive", "negative"]) self.assertEqual( __a, [ {"sequence": ANY(__a), "labels": [ANY(__a), ANY(__a)], "scores": [ANY(__a), ANY(__a)]} for i in range(1) ], ) _lowerCAmelCase : Any = classifier(["I am happy", "I am sad"], ["positive", "negative"]) self.assertEqual( __a, [ {"sequence": ANY(__a), "labels": [ANY(__a), ANY(__a)], "scores": [ANY(__a), ANY(__a)]} for i in range(2) ], ) with self.assertRaises(__a): classifier("", candidate_labels="politics") with self.assertRaises(__a): classifier(__a, candidate_labels="politics") with self.assertRaises(__a): classifier("Who are you voting for in 2020?", candidate_labels="") with self.assertRaises(__a): classifier("Who are you voting for in 2020?", candidate_labels=__a) with self.assertRaises(__a): classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template="Not formatting template", ) with self.assertRaises(__a): classifier( "Who are you voting for in 2020?", candidate_labels="politics", hypothesis_template=__a, ) self.run_entailment_id(__a) def snake_case__ ( self, __a): '''simple docstring''' _lowerCAmelCase : Tuple = zero_shot_classifier.model.config _lowerCAmelCase : Optional[Any] = config.labelaid _lowerCAmelCase : Union[str, Any] = zero_shot_classifier.entailment_id _lowerCAmelCase : Any = {"LABEL_0": 0, "LABEL_1": 1, "LABEL_2": 2} self.assertEqual(zero_shot_classifier.entailment_id, -1) _lowerCAmelCase : Optional[int] = {"entailment": 0, "neutral": 1, "contradiction": 2} self.assertEqual(zero_shot_classifier.entailment_id, 0) _lowerCAmelCase : Optional[int] = {"ENTAIL": 0, "NON-ENTAIL": 1} self.assertEqual(zero_shot_classifier.entailment_id, 0) _lowerCAmelCase : Optional[Any] = {"ENTAIL": 2, "NEUTRAL": 1, "CONTR": 0} self.assertEqual(zero_shot_classifier.entailment_id, 2) _lowerCAmelCase : List[str] = original_labelaid self.assertEqual(__a, zero_shot_classifier.entailment_id) @require_torch def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Tuple = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="pt", ) # There was a regression in 4.10 for this # Adding a test so we don't make the mistake again. # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499 zero_shot_classifier( "Who are you voting for in 2020?" * 100, candidate_labels=["politics", "public health", "science"]) @require_torch def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : int = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="pt", ) _lowerCAmelCase : List[Any] = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"]) self.assertEqual( nested_simplify(__a), { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], }, ) @require_tf def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : List[str] = pipeline( "zero-shot-classification", model="sshleifer/tiny-distilbert-base-cased-distilled-squad", framework="tf", ) _lowerCAmelCase : Union[str, Any] = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"]) self.assertEqual( nested_simplify(__a), { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], }, ) @slow @require_torch def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : Any = pipeline("zero-shot-classification", model="roberta-large-mnli", framework="pt") _lowerCAmelCase : Optional[Any] = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"]) self.assertEqual( nested_simplify(__a), { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], }, ) _lowerCAmelCase : Union[str, Any] = zero_shot_classifier( "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks" " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder" " through an attention mechanism. We propose a new simple network architecture, the Transformer, based" " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two" " machine translation tasks show these models to be superior in quality while being more parallelizable" " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014" " English-to-German translation task, improving over the existing best results, including ensembles by" " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new" " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small" " fraction of the training costs of the best models from the literature. We show that the Transformer" " generalizes well to other tasks by applying it successfully to English constituency parsing both with" " large and limited training data.", candidate_labels=["machine learning", "statistics", "translation", "vision"], multi_label=__a, ) self.assertEqual( nested_simplify(__a), { "sequence": ( "The dominant sequence transduction models are based on complex recurrent or convolutional neural" " networks in an encoder-decoder configuration. The best performing models also connect the" " encoder and decoder through an attention mechanism. We propose a new simple network" " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence" " and convolutions entirely. Experiments on two machine translation tasks show these models to be" " superior in quality while being more parallelizable and requiring significantly less time to" " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task," " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014" " English-to-French translation task, our model establishes a new single-model state-of-the-art" " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training" " costs of the best models from the literature. We show that the Transformer generalizes well to" " other tasks by applying it successfully to English constituency parsing both with large and" " limited training data." ), "labels": ["translation", "machine learning", "vision", "statistics"], "scores": [0.817, 0.713, 0.018, 0.018], }, ) @slow @require_tf def snake_case__ ( self): '''simple docstring''' _lowerCAmelCase : List[Any] = pipeline("zero-shot-classification", model="roberta-large-mnli", framework="tf") _lowerCAmelCase : Dict = zero_shot_classifier( "Who are you voting for in 2020?", candidate_labels=["politics", "public health", "science"]) self.assertEqual( nested_simplify(__a), { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], }, ) _lowerCAmelCase : str = zero_shot_classifier( "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks" " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder" " through an attention mechanism. We propose a new simple network architecture, the Transformer, based" " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two" " machine translation tasks show these models to be superior in quality while being more parallelizable" " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014" " English-to-German translation task, improving over the existing best results, including ensembles by" " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new" " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small" " fraction of the training costs of the best models from the literature. We show that the Transformer" " generalizes well to other tasks by applying it successfully to English constituency parsing both with" " large and limited training data.", candidate_labels=["machine learning", "statistics", "translation", "vision"], multi_label=__a, ) self.assertEqual( nested_simplify(__a), { "sequence": ( "The dominant sequence transduction models are based on complex recurrent or convolutional neural" " networks in an encoder-decoder configuration. The best performing models also connect the" " encoder and decoder through an attention mechanism. We propose a new simple network" " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence" " and convolutions entirely. Experiments on two machine translation tasks show these models to be" " superior in quality while being more parallelizable and requiring significantly less time to" " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task," " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014" " English-to-French translation task, our model establishes a new single-model state-of-the-art" " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training" " costs of the best models from the literature. We show that the Transformer generalizes well to" " other tasks by applying it successfully to English constituency parsing both with large and" " limited training data." ), "labels": ["translation", "machine learning", "vision", "statistics"], "scores": [0.817, 0.713, 0.018, 0.018], }, )
300
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a_ = {'''configuration_sew''': ['''SEW_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SEWConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ '''SEW_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SEWForCTC''', '''SEWForSequenceClassification''', '''SEWModel''', '''SEWPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys a_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
340
from collections import defaultdict def _a ( UpperCamelCase_ : int ) -> int: """simple docstring""" lowerCAmelCase__ = 1 lowerCAmelCase__ = True for v in tree[start]: if v not in visited: ret += dfs(UpperCamelCase_ ) if ret % 2 == 0: cuts.append(UpperCamelCase_ ) return ret def _a ( ) -> Optional[Any]: """simple docstring""" dfs(1 ) if __name__ == "__main__": a_, a_ = 10, 9 a_ = defaultdict(list) a_ = {} a_ = [] a_ = 0 a_ = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
340
1
"""simple docstring""" import argparse from transformers import ( TapasConfig, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasTokenizer, load_tf_weights_in_tapas, ) from transformers.utils import logging logging.set_verbosity_info() def A ( snake_case :int , snake_case :Optional[Any] , snake_case :Optional[Any] , snake_case :Optional[Any] , snake_case :Tuple ) -> Optional[Any]: # Initialise PyTorch model. # If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of # TapasConfig to False. # initialize configuration from json file __UpperCamelCase = TapasConfig.from_json_file(lowerCAmelCase__ ) # set absolute/relative position embeddings parameter __UpperCamelCase = reset_position_index_per_cell # set remaining parameters of TapasConfig as well as the model based on the task if task == "SQA": __UpperCamelCase = TapasForQuestionAnswering(config=lowerCAmelCase__ ) elif task == "WTQ": # run_task_main.py hparams __UpperCamelCase = 4 __UpperCamelCase = True # hparam_utils.py hparams __UpperCamelCase = 0.664_694 __UpperCamelCase = 0.207_951 __UpperCamelCase = 0.121_194 __UpperCamelCase = True __UpperCamelCase = True __UpperCamelCase = False __UpperCamelCase = 0.0_352_513 __UpperCamelCase = TapasForQuestionAnswering(config=lowerCAmelCase__ ) elif task == "WIKISQL_SUPERVISED": # run_task_main.py hparams __UpperCamelCase = 4 __UpperCamelCase = False # hparam_utils.py hparams __UpperCamelCase = 36.4_519 __UpperCamelCase = 0.903_421 __UpperCamelCase = 2_2_2.0_8_8 __UpperCamelCase = True __UpperCamelCase = True __UpperCamelCase = True __UpperCamelCase = 0.763_141 __UpperCamelCase = TapasForQuestionAnswering(config=lowerCAmelCase__ ) elif task == "TABFACT": __UpperCamelCase = TapasForSequenceClassification(config=lowerCAmelCase__ ) elif task == "MLM": __UpperCamelCase = TapasForMaskedLM(config=lowerCAmelCase__ ) elif task == "INTERMEDIATE_PRETRAINING": __UpperCamelCase = TapasModel(config=lowerCAmelCase__ ) else: raise ValueError(f'Task {task} not supported.' ) print(f'Building PyTorch model from configuration: {config}' ) # Load weights from tf checkpoint load_tf_weights_in_tapas(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save pytorch-model (weights and configuration) print(f'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(lowerCAmelCase__ ) # Save tokenizer files print(f'Save tokenizer files to {pytorch_dump_path}' ) __UpperCamelCase = TapasTokenizer(vocab_file=tf_checkpoint_path[:-1_0] + 'vocab.txt' , model_max_length=5_1_2 ) tokenizer.save_pretrained(lowerCAmelCase__ ) print('Used relative position embeddings:' , model.config.reset_position_index_per_cell ) if __name__ == "__main__": UpperCamelCase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="SQA", type=str, help="Model task for which to convert a checkpoint. Defaults to SQA." ) parser.add_argument( "--reset_position_index_per_cell", default=False, action="store_true", help="Whether to use relative position embeddings or not. Defaults to True.", ) parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--tapas_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained TAPAS model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) UpperCamelCase : Tuple = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.task, args.reset_position_index_per_cell, args.tf_checkpoint_path, args.tapas_config_file, args.pytorch_dump_path, )
352
"""simple docstring""" import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def A ( snake_case :Dict , snake_case :Optional[Any] , snake_case :List[Any] ) -> Optional[Any]: __UpperCamelCase = OmegaConf.load(snake_case ) __UpperCamelCase = torch.load(snake_case , map_location='cpu' )['model'] __UpperCamelCase = list(state_dict.keys() ) # extract state_dict for VQVAE __UpperCamelCase = {} __UpperCamelCase = 'first_stage_model.' for key in keys: if key.startswith(snake_case ): __UpperCamelCase = state_dict[key] # extract state_dict for UNetLDM __UpperCamelCase = {} __UpperCamelCase = 'model.diffusion_model.' for key in keys: if key.startswith(snake_case ): __UpperCamelCase = state_dict[key] __UpperCamelCase = config.model.params.first_stage_config.params __UpperCamelCase = config.model.params.unet_config.params __UpperCamelCase = VQModel(**snake_case ).eval() vqvae.load_state_dict(snake_case ) __UpperCamelCase = UNetLDMModel(**snake_case ).eval() unet.load_state_dict(snake_case ) __UpperCamelCase = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='scaled_linear' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=snake_case , ) __UpperCamelCase = LDMPipeline(snake_case , snake_case , snake_case ) pipeline.save_pretrained(snake_case ) if __name__ == "__main__": UpperCamelCase : Dict = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", type=str, required=True) parser.add_argument("--config_path", type=str, required=True) parser.add_argument("--output_path", type=str, required=True) UpperCamelCase : Optional[int] = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
263
0
'''simple docstring''' def UpperCAmelCase_ ( ) -> list[list[int]]: '''simple docstring''' return [list(range(1000 - i , -1000 - i , -1 ) ) for i in range(1000 )] __SCREAMING_SNAKE_CASE :str = generate_large_matrix() __SCREAMING_SNAKE_CASE :Any = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def UpperCAmelCase_ ( __lowercase : list[list[int]] ) -> None: '''simple docstring''' assert all(row == sorted(__lowercase , reverse=__lowercase ) for row in grid ) assert all(list(__lowercase ) == sorted(__lowercase , reverse=__lowercase ) for col in zip(*__lowercase ) ) def UpperCAmelCase_ ( __lowercase : list[int] ) -> int: '''simple docstring''' _UpperCAmelCase = 0 _UpperCAmelCase = len(__lowercase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: _UpperCAmelCase = (left + right) // 2 _UpperCAmelCase = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: _UpperCAmelCase = mid + 1 else: _UpperCAmelCase = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__lowercase ) def UpperCAmelCase_ ( __lowercase : list[list[int]] ) -> int: '''simple docstring''' _UpperCAmelCase = 0 _UpperCAmelCase = len(grid[0] ) for i in range(len(__lowercase ) ): _UpperCAmelCase = find_negative_index(grid[i][:bound] ) total += bound return (len(__lowercase ) * len(grid[0] )) - total def UpperCAmelCase_ ( __lowercase : list[list[int]] ) -> int: '''simple docstring''' return len([number for row in grid for number in row if number < 0] ) def UpperCAmelCase_ ( __lowercase : list[list[int]] ) -> int: '''simple docstring''' _UpperCAmelCase = 0 for row in grid: for i, number in enumerate(__lowercase ): if number < 0: total += len(__lowercase ) - i break return total def UpperCAmelCase_ ( ) -> None: '''simple docstring''' from timeit import timeit print("Running benchmarks" ) _UpperCAmelCase = ( "from __main__ import count_negatives_binary_search, " "count_negatives_brute_force, count_negatives_brute_force_with_break, grid" ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): _UpperCAmelCase = timeit(f'{func}(grid=grid)' , setup=__lowercase , number=500 ) print(f'{func}() took {time:0.4f} seconds' ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
22
"""simple docstring""" lowerCAmelCase__ = [ '''Audio''', '''Array2D''', '''Array3D''', '''Array4D''', '''Array5D''', '''ClassLabel''', '''Features''', '''Sequence''', '''Value''', '''Image''', '''Translation''', '''TranslationVariableLanguages''', ] from .audio import Audio from .features import ArrayaD, ArrayaD, ArrayaD, ArrayaD, ClassLabel, Features, Sequence, Value from .image import Image from .translation import Translation, TranslationVariableLanguages
108
0
'''simple docstring''' from ...utils import logging from ..ta.modeling_tf_ta import TFTaEncoderModel, TFTaForConditionalGeneration, TFTaModel from .configuration_mta import MTaConfig __snake_case : int = logging.get_logger(__name__) __snake_case : List[Any] = 'T5Config' class lowerCamelCase ( lowercase_ ): '''simple docstring''' __snake_case = 'mt5' __snake_case = MTaConfig class lowerCamelCase ( lowercase_ ): '''simple docstring''' __snake_case = 'mt5' __snake_case = MTaConfig class lowerCamelCase ( lowercase_ ): '''simple docstring''' __snake_case = 'mt5' __snake_case = MTaConfig
136
'''simple docstring''' import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __snake_case : List[str] = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece @require_tokenizers class lowerCamelCase ( lowercase_ , unittest.TestCase ): '''simple docstring''' __snake_case = XLMRobertaTokenizer __snake_case = XLMRobertaTokenizerFast __snake_case = True __snake_case = True def lowercase__ ( self : Any ) -> Union[str, Any]: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing A__ : Any =XLMRobertaTokenizer(lowerCAmelCase_ , keep_accents=lowerCAmelCase_ ) tokenizer.save_pretrained(self.tmpdirname ) def lowercase__ ( self : Optional[Any] ) -> Tuple: '''simple docstring''' A__ : Union[str, Any] ="""<pad>""" A__ : Any =1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase_ ) , lowerCAmelCase_ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase_ ) , lowerCAmelCase_ ) def lowercase__ ( self : List[Any] ) -> Any: '''simple docstring''' A__ : int =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(lowerCAmelCase_ ) , 10_02 ) def lowercase__ ( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 10_02 ) def lowercase__ ( self : Tuple ) -> Any: '''simple docstring''' A__ : List[Any] =XLMRobertaTokenizer(lowerCAmelCase_ , keep_accents=lowerCAmelCase_ ) A__ : Tuple =tokenizer.tokenize("""This is a test""" ) self.assertListEqual(lowerCAmelCase_ , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase_ ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) A__ : Optional[int] =tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( lowerCAmelCase_ , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) A__ : Optional[int] =tokenizer.convert_tokens_to_ids(lowerCAmelCase_ ) self.assertListEqual( lowerCAmelCase_ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) A__ : Union[str, Any] =tokenizer.convert_ids_to_tokens(lowerCAmelCase_ ) self.assertListEqual( lowerCAmelCase_ , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) def lowercase__ ( self : List[Any] ) -> Optional[int]: '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return A__ : Dict =(self.rust_tokenizer_class, """hf-internal-testing/tiny-xlm-roberta""", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): A__ : List[str] =self.rust_tokenizer_class.from_pretrained(lowerCAmelCase_ , **lowerCAmelCase_ ) A__ : Union[str, Any] =self.tokenizer_class.from_pretrained(lowerCAmelCase_ , **lowerCAmelCase_ ) A__ : Optional[Any] =tempfile.mkdtemp() A__ : Union[str, Any] =tokenizer_r.save_pretrained(lowerCAmelCase_ ) A__ : Union[str, Any] =tokenizer_p.save_pretrained(lowerCAmelCase_ ) # 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 ) ) A__ : List[str] =tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f ) self.assertSequenceEqual(lowerCAmelCase_ , lowerCAmelCase_ ) # Checks everything loads correctly in the same way A__ : Any =tokenizer_r.from_pretrained(lowerCAmelCase_ ) A__ : Union[str, Any] =tokenizer_p.from_pretrained(lowerCAmelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCAmelCase_ , lowerCAmelCase_ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(lowerCAmelCase_ ) # Save tokenizer rust, legacy_format=True A__ : List[str] =tempfile.mkdtemp() A__ : List[str] =tokenizer_r.save_pretrained(lowerCAmelCase_ , legacy_format=lowerCAmelCase_ ) A__ : List[Any] =tokenizer_p.save_pretrained(lowerCAmelCase_ ) # Checks it save with the same files self.assertSequenceEqual(lowerCAmelCase_ , lowerCAmelCase_ ) # Checks everything loads correctly in the same way A__ : str =tokenizer_r.from_pretrained(lowerCAmelCase_ ) A__ : List[Any] =tokenizer_p.from_pretrained(lowerCAmelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCAmelCase_ , lowerCAmelCase_ ) ) shutil.rmtree(lowerCAmelCase_ ) # Save tokenizer rust, legacy_format=False A__ : List[str] =tempfile.mkdtemp() A__ : Dict =tokenizer_r.save_pretrained(lowerCAmelCase_ , legacy_format=lowerCAmelCase_ ) A__ : List[Any] =tokenizer_p.save_pretrained(lowerCAmelCase_ ) # 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 A__ : Optional[int] =tokenizer_r.from_pretrained(lowerCAmelCase_ ) A__ : str =tokenizer_p.from_pretrained(lowerCAmelCase_ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCAmelCase_ , lowerCAmelCase_ ) ) shutil.rmtree(lowerCAmelCase_ ) @cached_property def lowercase__ ( self : Optional[Any] ) -> List[str]: '''simple docstring''' return XLMRobertaTokenizer.from_pretrained("""xlm-roberta-base""" ) def lowercase__ ( self : Tuple ) -> Tuple: '''simple docstring''' with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCAmelCase_ , f.name ) A__ : Dict =XLMRobertaTokenizer(f.name , keep_accents=lowerCAmelCase_ ) A__ : Optional[Any] =pickle.dumps(lowerCAmelCase_ ) pickle.loads(lowerCAmelCase_ ) def lowercase__ ( self : Optional[Any] ) -> List[Any]: '''simple docstring''' if not self.test_rust_tokenizer: return A__ : Any =self.get_tokenizer() A__ : Any =self.get_rust_tokenizer() A__ : Optional[Any] ="""I was born in 92000, and this is falsé.""" A__ : List[str] =tokenizer.tokenize(lowerCAmelCase_ ) A__ : int =rust_tokenizer.tokenize(lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) A__ : str =tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ ) A__ : Dict =rust_tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) A__ : Union[str, Any] =self.get_rust_tokenizer() A__ : Union[str, Any] =tokenizer.encode(lowerCAmelCase_ ) A__ : Optional[Any] =rust_tokenizer.encode(lowerCAmelCase_ ) self.assertListEqual(lowerCAmelCase_ , lowerCAmelCase_ ) @slow def lowercase__ ( self : Dict ) -> int: '''simple docstring''' A__ : Optional[Any] ="""Hello World!""" A__ : Optional[Any] =[0, 3_53_78, 66_61, 38, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(lowerCAmelCase_ , self.big_tokenizer.encode(lowerCAmelCase_ ) ) @slow def lowercase__ ( self : List[Any] ) -> int: '''simple docstring''' A__ : List[Any] =( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) A__ : Optional[Any] =[ 0, 32_93, 83, 10, 45_52, 49_89, 79_86, 6_78, 10, 59_15, 1_11, 17_94_59, 12_48_50, 4, 60_44, 2_37, 12, 6, 5, 6, 4, 67_80, 7_05, 15, 13_88, 44, 3_78, 1_01_14, 7_11, 1_52, 20, 6, 5, 2_23_76, 6_42, 12_21, 1_51_90, 3_41_53, 4_50, 56_08, 9_59, 11_19, 5_77_02, 1_36, 1_86, 47, 10_98, 2_93_67, 47, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 60_44, 2_37, 62_84, 5_09_01, 5_28, 31, 90, 34, 9_27, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(lowerCAmelCase_ , self.big_tokenizer.encode(lowerCAmelCase_ ) ) @slow def lowercase__ ( self : int ) -> Optional[Any]: '''simple docstring''' # fmt: off A__ : List[Any] ={"""input_ids""": [[0, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [0, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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=lowerCAmelCase_ , model_name="""xlm-roberta-base""" , revision="""d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3""" , )
136
1
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, ) SCREAMING_SNAKE_CASE :List[str] = { '''configuration_clip''': [ '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPConfig''', '''CLIPOnnxConfig''', '''CLIPTextConfig''', '''CLIPVisionConfig''', ], '''processing_clip''': ['''CLIPProcessor'''], '''tokenization_clip''': ['''CLIPTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :Union[str, Any] = ['''CLIPTokenizerFast'''] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :Tuple = ['''CLIPFeatureExtractor'''] SCREAMING_SNAKE_CASE :int = ['''CLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :Optional[Any] = [ '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPModel''', '''CLIPPreTrainedModel''', '''CLIPTextModel''', '''CLIPTextModelWithProjection''', '''CLIPVisionModel''', '''CLIPVisionModelWithProjection''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :List[Any] = [ '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFCLIPModel''', '''TFCLIPPreTrainedModel''', '''TFCLIPTextModel''', '''TFCLIPVisionModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE :int = [ '''FlaxCLIPModel''', '''FlaxCLIPPreTrainedModel''', '''FlaxCLIPTextModel''', '''FlaxCLIPTextPreTrainedModel''', '''FlaxCLIPVisionModel''', '''FlaxCLIPVisionPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_clip import ( CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPOnnxConfig, CLIPTextConfig, CLIPVisionConfig, ) from .processing_clip import CLIPProcessor from .tokenization_clip import CLIPTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_clip_fast import CLIPTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_clip import CLIPFeatureExtractor from .image_processing_clip import CLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clip import ( CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPModel, CLIPPreTrainedModel, CLIPTextModel, CLIPTextModelWithProjection, CLIPVisionModel, CLIPVisionModelWithProjection, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_clip import ( TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFCLIPModel, TFCLIPPreTrainedModel, TFCLIPTextModel, TFCLIPVisionModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_clip import ( FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPTextPreTrainedModel, FlaxCLIPVisionModel, FlaxCLIPVisionPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE :int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
159
from typing import Union import fire import torch from tqdm import tqdm def _lowerCAmelCase ( lowerCAmelCase_ :str , lowerCAmelCase_ :str = "cpu" , lowerCAmelCase_ :Union[str, None] = None )->None: '''simple docstring''' snake_case_ = torch.load(lowerCAmelCase_ , map_location=lowerCAmelCase_ ) for k, v in tqdm(state_dict.items() ): if not isinstance(lowerCAmelCase_ , torch.Tensor ): raise TypeError("FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin" ) snake_case_ = v.half() if save_path is None: # overwrite src_path snake_case_ = src_path torch.save(lowerCAmelCase_ , lowerCAmelCase_ ) if __name__ == "__main__": fire.Fire(convert)
159
1
import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) 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 __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = KandinskyVaaImgaImgPipeline __UpperCamelCase = ['''image_embeds''', '''negative_image_embeds''', '''image'''] __UpperCamelCase = [ '''image_embeds''', '''negative_image_embeds''', '''image''', ] __UpperCamelCase = [ '''generator''', '''height''', '''width''', '''strength''', '''guidance_scale''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] __UpperCamelCase = False @property def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return 32 @property def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' return 32 @property def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' return self.time_input_dim @property def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' return self.time_input_dim * 4 @property def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' return 100 @property def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' torch.manual_seed(0 ) A_ : Tuple = { "in_channels": 4, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } A_ : Union[str, Any] = UNetaDConditionModel(**snake_case ) return model @property def SCREAMING_SNAKE_CASE ( self :str ): '''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 SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' torch.manual_seed(0 ) A_ : Dict = VQModel(**self.dummy_movq_kwargs ) return model def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : int = self.dummy_unet A_ : Any = self.dummy_movq A_ : int = { "num_train_timesteps": 1_000, "beta_schedule": "linear", "beta_start": 0.00085, "beta_end": 0.012, "clip_sample": False, "set_alpha_to_one": False, "steps_offset": 0, "prediction_type": "epsilon", "thresholding": False, } A_ : List[str] = DDIMScheduler(**snake_case ) A_ : Tuple = { "unet": unet, "scheduler": scheduler, "movq": movq, } return components def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Union[str, Any] , snake_case :int=0 ): '''simple docstring''' A_ : Union[str, Any] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(snake_case ) ).to(snake_case ) A_ : str = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( snake_case ) # create init_image A_ : Optional[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(snake_case ) ).to(snake_case ) A_ : List[str] = image.cpu().permute(0 , 2 , 3 , 1 )[0] A_ : List[str] = Image.fromarray(np.uinta(snake_case ) ).convert("RGB" ).resize((256, 256) ) if str(snake_case ).startswith("mps" ): A_ : List[Any] = torch.manual_seed(snake_case ) else: A_ : Optional[int] = torch.Generator(device=snake_case ).manual_seed(snake_case ) A_ : Optional[int] = { "image": init_image, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 64, "width": 64, "num_inference_steps": 10, "guidance_scale": 7.0, "strength": 0.2, "output_type": "np", } return inputs def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : Union[str, Any] = "cpu" A_ : Optional[Any] = self.get_dummy_components() A_ : Tuple = self.pipeline_class(**snake_case ) A_ : Any = pipe.to(snake_case ) pipe.set_progress_bar_config(disable=snake_case ) A_ : Tuple = pipe(**self.get_dummy_inputs(snake_case ) ) A_ : str = output.images A_ : Dict = pipe( **self.get_dummy_inputs(snake_case ) , return_dict=snake_case , )[0] A_ : str = image[0, -3:, -3:, -1] A_ : List[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) A_ : List[str] = np.array( [0.6199778, 0.63984406, 0.46145785, 0.62944984, 0.5622215, 0.47306132, 0.47441456, 0.4607606, 0.48719263] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/kandinskyv22_img2img_frog.npy" ) A_ : int = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) A_ : Optional[int] = "A red cartoon frog, 4k" A_ : Dict = KandinskyVaaPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-prior" , torch_dtype=torch.floataa ) pipe_prior.to(snake_case ) A_ : Optional[int] = KandinskyVaaImgaImgPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-decoder" , torch_dtype=torch.floataa ) A_ : Dict = pipeline.to(snake_case ) pipeline.set_progress_bar_config(disable=snake_case ) A_ : Any = torch.Generator(device="cpu" ).manual_seed(0 ) A_ , A_ : Optional[int] = pipe_prior( snake_case , generator=snake_case , num_inference_steps=5 , negative_prompt="" , ).to_tuple() A_ : int = pipeline( image=snake_case , image_embeds=snake_case , negative_image_embeds=snake_case , generator=snake_case , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type="np" , ) A_ : Optional[int] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(snake_case , snake_case )
70
import argparse import torch from transformers import ( SpeechTaConfig, SpeechTaFeatureExtractor, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaProcessor, SpeechTaTokenizer, logging, ) from transformers.tokenization_utils import AddedToken logging.set_verbosity_info() _lowerCAmelCase : Tuple = logging.get_logger('''transformers.models.speecht5''') _lowerCAmelCase : int = { '''speech_encoder_prenet.layer_norm''': '''speecht5.encoder.prenet.feature_projection.layer_norm''', '''speech_encoder_prenet.post_extract_proj''': '''speecht5.encoder.prenet.feature_projection.projection''', '''speech_encoder_prenet.pos_conv.0''': '''speecht5.encoder.prenet.pos_conv_embed.conv''', '''speech_encoder_prenet.mask_emb''': '''speecht5.encoder.prenet.masked_spec_embed''', } _lowerCAmelCase : str = { '''text_encoder_prenet.encoder_prenet.0''': '''speecht5.encoder.prenet.embed_tokens''', '''text_encoder_prenet.encoder_prenet.1.alpha''': '''speecht5.encoder.prenet.encode_positions.alpha''', } _lowerCAmelCase : int = { '''speech_decoder_prenet.decoder_prenet.0.0.prenet.0.0''': '''speecht5.decoder.prenet.layers.0''', '''speech_decoder_prenet.decoder_prenet.0.0.prenet.1.0''': '''speecht5.decoder.prenet.layers.1''', '''speech_decoder_prenet.decoder_prenet.0.1''': '''speecht5.decoder.prenet.final_layer''', '''speech_decoder_prenet.decoder_prenet.1.alpha''': '''speecht5.decoder.prenet.encode_positions.alpha''', '''speech_decoder_prenet.spkembs_layer.0''': '''speecht5.decoder.prenet.speaker_embeds_layer''', } _lowerCAmelCase : Union[str, Any] = { '''speech_decoder_postnet.feat_out''': '''speech_decoder_postnet.feat_out''', '''speech_decoder_postnet.prob_out''': '''speech_decoder_postnet.prob_out''', '''speech_decoder_postnet.postnet.postnet.0.0''': '''speech_decoder_postnet.layers.0.conv''', '''speech_decoder_postnet.postnet.postnet.0.1''': '''speech_decoder_postnet.layers.0.batch_norm''', '''speech_decoder_postnet.postnet.postnet.1.0''': '''speech_decoder_postnet.layers.1.conv''', '''speech_decoder_postnet.postnet.postnet.1.1''': '''speech_decoder_postnet.layers.1.batch_norm''', '''speech_decoder_postnet.postnet.postnet.2.0''': '''speech_decoder_postnet.layers.2.conv''', '''speech_decoder_postnet.postnet.postnet.2.1''': '''speech_decoder_postnet.layers.2.batch_norm''', '''speech_decoder_postnet.postnet.postnet.3.0''': '''speech_decoder_postnet.layers.3.conv''', '''speech_decoder_postnet.postnet.postnet.3.1''': '''speech_decoder_postnet.layers.3.batch_norm''', '''speech_decoder_postnet.postnet.postnet.4.0''': '''speech_decoder_postnet.layers.4.conv''', '''speech_decoder_postnet.postnet.postnet.4.1''': '''speech_decoder_postnet.layers.4.batch_norm''', } _lowerCAmelCase : Union[str, Any] = { '''text_decoder_prenet.embed_tokens''': '''speecht5.decoder.prenet.embed_tokens''', } _lowerCAmelCase : int = { '''text_decoder_postnet.output_projection''': '''text_decoder_postnet.lm_head''', } _lowerCAmelCase : Any = { '''encoder.layers.*.self_attn.k_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.k_proj''', '''encoder.layers.*.self_attn.v_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.v_proj''', '''encoder.layers.*.self_attn.q_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.q_proj''', '''encoder.layers.*.self_attn.out_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.out_proj''', '''encoder.layers.*.self_attn_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.layer_norm''', '''encoder.layers.*.fc1''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.intermediate_dense''', '''encoder.layers.*.fc2''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.output_dense''', '''encoder.layers.*.final_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''speecht5.encoder.wrapped_encoder.layer_norm''', '''encoder.pos_emb.pe_k''': '''speecht5.encoder.wrapped_encoder.embed_positions.pe_k''', } _lowerCAmelCase : List[str] = { '''decoder.layers.*.self_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.k_proj''', '''decoder.layers.*.self_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.v_proj''', '''decoder.layers.*.self_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.q_proj''', '''decoder.layers.*.self_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.out_proj''', '''decoder.layers.*.self_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn_layer_norm''', '''decoder.layers.*.encoder_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.k_proj''', '''decoder.layers.*.encoder_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.v_proj''', '''decoder.layers.*.encoder_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.q_proj''', '''decoder.layers.*.encoder_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.out_proj''', '''decoder.layers.*.encoder_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn_layer_norm''', '''decoder.layers.*.fc1''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.intermediate_dense''', '''decoder.layers.*.fc2''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.output_dense''', '''decoder.layers.*.final_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.final_layer_norm''', } _lowerCAmelCase : Optional[Any] = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_TEXT_DECODER_PRENET, **MAPPING_TEXT_DECODER_POSTNET, } _lowerCAmelCase : Dict = { **MAPPING_TEXT_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } _lowerCAmelCase : Union[str, Any] = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } _lowerCAmelCase : Optional[Any] = [] _lowerCAmelCase : Tuple = [ '''encoder.version''', '''encoder.layers.*.norm_k.weight''', '''encoder.layers.*.norm_k.bias''', '''decoder.version''', '''decoder.layers.*.norm_k.weight''', '''decoder.layers.*.norm_k.bias''', '''decoder.pos_emb.pe_k''', '''speech_encoder_prenet.embed_positions._float_tensor''', '''text_decoder_prenet.embed_positions._float_tensor''', ] _lowerCAmelCase : Tuple = IGNORE_KEYS + [ '''encoder.proj''', '''text_encoder_prenet.*''', '''speech_decoder_prenet.*''', '''speech_decoder_postnet.*''', ] _lowerCAmelCase : int = IGNORE_KEYS + [ '''encoder.proj''', '''speech_encoder_prenet.*''', '''text_decoder_prenet.*''', '''text_decoder_postnet.*''', ] _lowerCAmelCase : Optional[int] = IGNORE_KEYS + [ '''encoder.proj''', '''text_encoder_prenet.*''', '''text_decoder_prenet.*''', '''text_decoder_postnet.*''', ] def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Any , _lowerCAmelCase : List[str] ) -> Optional[Any]: for attribute in key.split("." ): A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ) if weight_type is not None: A_ : Tuple = getattr(_lowerCAmelCase , _lowerCAmelCase ).shape else: A_ : List[Any] = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": A_ : Dict = value elif weight_type == "weight_g": A_ : int = value elif weight_type == "weight_v": A_ : str = value elif weight_type == "bias": A_ : int = value elif weight_type == "running_mean": A_ : str = value elif weight_type == "running_var": A_ : Any = value elif weight_type == "num_batches_tracked": A_ : str = value else: A_ : int = value logger.info(f"{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}." ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> Union[str, Any]: for key in ignore_keys: if key.endswith(".*" ): if name.startswith(key[:-1] ): return True elif ".*." in key: A_ , A_ : Tuple = key.split(".*." ) if prefix in name and suffix in name: return True elif key in name: return True return False def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Tuple , _lowerCAmelCase : Optional[Any] ) -> Union[str, Any]: A_ : Tuple = [] if task == "s2t": A_ : Union[str, Any] = hf_model.speechta.encoder.prenet.feature_encoder A_ : str = MAPPING_S2T A_ : Union[str, Any] = IGNORE_KEYS_S2T elif task == "t2s": A_ : Optional[int] = None A_ : Dict = MAPPING_T2S A_ : Any = IGNORE_KEYS_T2S elif task == "s2s": A_ : Optional[int] = hf_model.speechta.encoder.prenet.feature_encoder A_ : Dict = MAPPING_S2S A_ : List[str] = IGNORE_KEYS_S2S else: raise ValueError(f"Unsupported task: {task}" ) for name, value in fairseq_dict.items(): if should_ignore(_lowerCAmelCase , _lowerCAmelCase ): logger.info(f"{name} was ignored" ) continue A_ : List[Any] = False if "conv_layers" in name: load_conv_layer( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , hf_model.config.feat_extract_norm == "group" , ) A_ : Tuple = True else: for key, mapped_key in MAPPING.items(): # mapped_key = "speecht5." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if "*" in key: A_ , A_ : Optional[Any] = key.split(".*." ) if prefix in name and suffix in name: A_ : int = suffix # if key in name or key.split("w2v_model.")[-1] == name.split(".")[0]: if key in name: A_ : str = True if "*" in mapped_key: A_ : List[str] = name.split(_lowerCAmelCase )[0].split("." )[-2] A_ : Optional[int] = mapped_key.replace("*" , _lowerCAmelCase ) if "weight_g" in name: A_ : Union[str, Any] = "weight_g" elif "weight_v" in name: A_ : List[Any] = "weight_v" elif "bias" in name: A_ : Tuple = "bias" elif "weight" in name: A_ : List[Any] = "weight" elif "running_mean" in name: A_ : Union[str, Any] = "running_mean" elif "running_var" in name: A_ : Union[str, Any] = "running_var" elif "num_batches_tracked" in name: A_ : List[Any] = "num_batches_tracked" else: A_ : Optional[Any] = 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 __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Optional[int] ) -> List[Any]: A_ : int = full_name.split("conv_layers." )[-1] A_ : Optional[Any] = name.split("." ) A_ : List[Any] = int(items[0] ) A_ : int = 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_ : Optional[int] = 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_ : Optional[Any] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found." ) A_ : Tuple = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found." ) A_ : Union[str, Any] = 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 __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : str=None , _lowerCAmelCase : List[str]=None , _lowerCAmelCase : int=None , ) -> Optional[Any]: if config_path is not None: A_ : Dict = SpeechTaConfig.from_pretrained(_lowerCAmelCase ) else: A_ : Optional[int] = SpeechTaConfig() if task == "s2t": A_ : Optional[Any] = config.max_text_positions A_ : Optional[int] = SpeechTaForSpeechToText(_lowerCAmelCase ) elif task == "t2s": A_ : str = 1876 A_ : List[str] = 600 A_ : List[str] = config.max_speech_positions A_ : Tuple = SpeechTaForTextToSpeech(_lowerCAmelCase ) elif task == "s2s": A_ : Optional[int] = 1876 A_ : int = config.max_speech_positions A_ : Union[str, Any] = SpeechTaForSpeechToSpeech(_lowerCAmelCase ) else: raise ValueError(f"Unknown task name: {task}" ) if vocab_path: A_ : int = SpeechTaTokenizer(_lowerCAmelCase , model_max_length=config.max_text_positions ) # Mask token behaves like a normal word, i.e. include the space before it A_ : str = AddedToken("<mask>" , lstrip=_lowerCAmelCase , rstrip=_lowerCAmelCase ) A_ : int = mask_token tokenizer.add_special_tokens({"mask_token": mask_token} ) tokenizer.add_tokens(["<ctc_blank>"] ) A_ : int = SpeechTaFeatureExtractor() A_ : Optional[Any] = SpeechTaProcessor(tokenizer=_lowerCAmelCase , feature_extractor=_lowerCAmelCase ) processor.save_pretrained(_lowerCAmelCase ) A_ : Union[str, Any] = torch.load(_lowerCAmelCase ) recursively_load_weights(fairseq_checkpoint["model"] , _lowerCAmelCase , _lowerCAmelCase ) model.save_pretrained(_lowerCAmelCase ) if repo_id: print("Pushing to the hub..." ) processor.push_to_hub(_lowerCAmelCase ) model.push_to_hub(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Tuple = argparse.ArgumentParser() parser.add_argument( '''--task''', default='''s2t''', type=str, help='''Type of the SpeechT5 model you\'d like to convert. Should be one of \'s2t\', \'t2s\', \'s2s\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--vocab_path''', default=None, type=str, help='''Path to SentencePiece model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) _lowerCAmelCase : Tuple = parser.parse_args() convert_speechta_checkpoint( args.task, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.vocab_path, args.push_to_hub, )
70
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 ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = 1 lowerCAmelCase : Optional[int] = 3 lowerCAmelCase : List[Any] = (32, 32) lowerCAmelCase : str = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(snake_case__ ) return image @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCAmelCase : Tuple = 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 lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCAmelCase : int = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) return model @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCAmelCase : str = 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 , ) return CLIPTextModel(snake_case__ ) @property def lowercase__ ( self ): """simple docstring""" def extract(*snake_case__ , **snake_case__ ): class SCREAMING_SNAKE_CASE__ : """simple docstring""" def __init__( self ): """simple docstring""" lowerCAmelCase : int = torch.ones([0] ) def lowercase__ ( self , snake_case__ ): """simple docstring""" self.pixel_values.to(snake_case__ ) return self return Out() return extract def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : Dict = self.dummy_cond_unet lowerCAmelCase : List[str] = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="scaled_linear" , clip_sample=snake_case__ , set_alpha_to_one=snake_case__ , ) lowerCAmelCase : Union[str, Any] = self.dummy_vae lowerCAmelCase : List[Any] = self.dummy_text_encoder lowerCAmelCase : Optional[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) # make sure here that pndm scheduler skips prk lowerCAmelCase : Any = StableDiffusionPipeline( unet=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , safety_checker=snake_case__ , feature_extractor=self.dummy_extractor , ) lowerCAmelCase : List[str] = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : Union[str, Any] = "A painting of a squirrel eating a burger" lowerCAmelCase : str = torch.Generator(device=snake_case__ ).manual_seed(0 ) lowerCAmelCase : int = sd_pipe([prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" ) lowerCAmelCase : str = output.images lowerCAmelCase : List[str] = torch.Generator(device=snake_case__ ).manual_seed(0 ) lowerCAmelCase : Optional[int] = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , return_dict=snake_case__ , )[0] lowerCAmelCase : Any = image[0, -3:, -3:, -1] lowerCAmelCase : List[str] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : Any = np.array([0.5756, 0.6118, 0.5005, 0.5041, 0.5471, 0.4726, 0.4976, 0.4865, 0.4864] ) 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[int] = "cpu" # ensure determinism for the device-dependent torch.Generator lowerCAmelCase : Optional[Any] = self.dummy_cond_unet lowerCAmelCase : Any = PNDMScheduler(skip_prk_steps=snake_case__ ) lowerCAmelCase : Union[str, Any] = self.dummy_vae lowerCAmelCase : str = self.dummy_text_encoder lowerCAmelCase : Any = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) # make sure here that pndm scheduler skips prk lowerCAmelCase : Union[str, Any] = StableDiffusionPipeline( unet=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , safety_checker=snake_case__ , feature_extractor=self.dummy_extractor , ) lowerCAmelCase : Optional[int] = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : Union[str, Any] = "A painting of a squirrel eating a burger" lowerCAmelCase : List[str] = torch.Generator(device=snake_case__ ).manual_seed(0 ) lowerCAmelCase : str = sd_pipe([prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" ) lowerCAmelCase : Union[str, Any] = output.images lowerCAmelCase : str = torch.Generator(device=snake_case__ ).manual_seed(0 ) lowerCAmelCase : Tuple = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , return_dict=snake_case__ , )[0] lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] lowerCAmelCase : Optional[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase : List[Any] = np.array([0.5125, 0.5716, 0.4828, 0.5060, 0.5650, 0.4768, 0.5185, 0.4895, 0.4993] ) 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : str = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-lms-pipe" , safety_checker=snake_case__ ) assert isinstance(snake_case__ , snake_case__ ) assert isinstance(pipe.scheduler , snake_case__ ) assert pipe.safety_checker is None lowerCAmelCase : Dict = 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(snake_case__ ) lowerCAmelCase : Dict = StableDiffusionPipeline.from_pretrained(snake_case__ ) # sanity check that the pipeline still works assert pipe.safety_checker is None lowerCAmelCase : str = 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 lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = self.dummy_cond_unet lowerCAmelCase : Dict = PNDMScheduler(skip_prk_steps=snake_case__ ) lowerCAmelCase : str = self.dummy_vae lowerCAmelCase : List[Any] = self.dummy_text_encoder lowerCAmelCase : Union[str, Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) # put models in fp16 lowerCAmelCase : str = unet.half() lowerCAmelCase : Union[str, Any] = vae.half() lowerCAmelCase : Any = bert.half() # make sure here that pndm scheduler skips prk lowerCAmelCase : str = StableDiffusionPipeline( unet=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , safety_checker=snake_case__ , feature_extractor=self.dummy_extractor , ) lowerCAmelCase : Optional[Any] = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : List[Any] = "A painting of a squirrel eating a burger" lowerCAmelCase : 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 ): """simple docstring""" def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5" , safety_checker=snake_case__ ) lowerCAmelCase : Optional[Any] = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) lowerCAmelCase : Tuple = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : Dict = ( "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 " ) lowerCAmelCase : Any = 4_003_660_346 lowerCAmelCase : List[str] = 7 # without safety guidance (sld_guidance_scale = 0) lowerCAmelCase : Dict = torch.manual_seed(snake_case__ ) lowerCAmelCase : str = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type="np" , width=512 , height=512 , sld_guidance_scale=0 , ) lowerCAmelCase : Optional[int] = output.images lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] lowerCAmelCase : str = [0.2278, 0.2231, 0.2249, 0.2333, 0.2303, 0.1885, 0.2273, 0.2144, 0.2176] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 # without safety guidance (strong configuration) lowerCAmelCase : int = torch.manual_seed(snake_case__ ) lowerCAmelCase : Optional[int] = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type="np" , width=512 , height=512 , sld_guidance_scale=2_000 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) lowerCAmelCase : Optional[Any] = output.images lowerCAmelCase : Tuple = image[0, -3:, -3:, -1] lowerCAmelCase : Tuple = [0.2383, 0.2276, 0.236, 0.2192, 0.2186, 0.2053, 0.1971, 0.1901, 0.1719] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5" , safety_checker=snake_case__ ) lowerCAmelCase : Optional[Any] = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) lowerCAmelCase : Optional[Any] = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : str = "padme amidala taking a bath artwork, safe for work, no nudity" lowerCAmelCase : Tuple = 2_734_971_755 lowerCAmelCase : Dict = 7 lowerCAmelCase : str = torch.manual_seed(snake_case__ ) lowerCAmelCase : List[str] = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type="np" , width=512 , height=512 , sld_guidance_scale=0 , ) lowerCAmelCase : List[Any] = output.images lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] lowerCAmelCase : Any = [0.3502, 0.3622, 0.3396, 0.3642, 0.3478, 0.3318, 0.35, 0.3348, 0.3297] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 lowerCAmelCase : List[str] = torch.manual_seed(snake_case__ ) lowerCAmelCase : Union[str, Any] = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type="np" , width=512 , height=512 , sld_guidance_scale=2_000 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) lowerCAmelCase : List[Any] = output.images lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] lowerCAmelCase : Optional[Any] = [0.5531, 0.5206, 0.4895, 0.5156, 0.5182, 0.4751, 0.4802, 0.4803, 0.4443] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[Any] = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5" ) lowerCAmelCase : Union[str, Any] = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) lowerCAmelCase : Optional[int] = ( "the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c." " leyendecker" ) lowerCAmelCase : Dict = 1_044_355_234 lowerCAmelCase : Any = 12 lowerCAmelCase : Tuple = torch.manual_seed(snake_case__ ) lowerCAmelCase : Optional[Any] = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type="np" , width=512 , height=512 , sld_guidance_scale=0 , ) lowerCAmelCase : int = output.images lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] lowerCAmelCase : int = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-7 lowerCAmelCase : Optional[int] = torch.manual_seed(snake_case__ ) lowerCAmelCase : Optional[Any] = sd_pipe( [prompt] , generator=snake_case__ , guidance_scale=snake_case__ , num_inference_steps=50 , output_type="np" , width=512 , height=512 , sld_guidance_scale=2_000 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) lowerCAmelCase : Optional[int] = output.images lowerCAmelCase : Union[str, Any] = image[0, -3:, -3:, -1] lowerCAmelCase : int = np.array([0.5818, 0.6285, 0.6835, 0.6019, 0.625, 0.6754, 0.6096, 0.6334, 0.6561] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
108
"""simple docstring""" import argparse import glob import logging import os from argparse import Namespace from importlib import import_module import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from seqeval.metrics import accuracy_score, fa_score, precision_score, recall_score from torch.nn import CrossEntropyLoss from torch.utils.data import DataLoader, TensorDataset from utils_ner import TokenClassificationTask lowerCAmelCase__ = logging.getLogger(__name__) class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Tuple ="token-classification" def __init__( self , snake_case__ ): """simple docstring""" if type(snake_case__ ) == dict: lowerCAmelCase : List[str] = Namespace(**snake_case__ ) lowerCAmelCase : Optional[Any] = import_module("tasks" ) try: lowerCAmelCase : Dict = getattr(snake_case__ , hparams.task_type ) lowerCAmelCase : TokenClassificationTask = token_classification_task_clazz() except AttributeError: raise ValueError( f"""Task {hparams.task_type} needs to be defined as a TokenClassificationTask subclass in {module}. """ f"""Available tasks classes are: {TokenClassificationTask.__subclasses__()}""" ) lowerCAmelCase : Dict = self.token_classification_task.get_labels(hparams.labels ) lowerCAmelCase : str = CrossEntropyLoss().ignore_index super().__init__(snake_case__ , len(self.labels ) , self.mode ) def lowercase__ ( self , **snake_case__ ): """simple docstring""" return self.model(**snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" lowerCAmelCase : List[str] = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type != "distilbert": lowerCAmelCase : Optional[int] = ( batch[2] if self.config.model_type in ["bert", "xlnet"] else None ) # XLM and RoBERTa don"t use token_type_ids lowerCAmelCase : str = self(**snake_case__ ) lowerCAmelCase : Tuple = outputs[0] # tensorboard_logs = {"loss": loss, "rate": self.lr_scheduler.get_last_lr()[-1]} return {"loss": loss} def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.hparams for mode in ["train", "dev", "test"]: lowerCAmelCase : Optional[int] = self._feature_file(snake_case__ ) if os.path.exists(snake_case__ ) and not args.overwrite_cache: logger.info("Loading features from cached file %s" , snake_case__ ) lowerCAmelCase : Dict = torch.load(snake_case__ ) else: logger.info("Creating features from dataset file at %s" , args.data_dir ) lowerCAmelCase : Optional[int] = self.token_classification_task.read_examples_from_file(args.data_dir , snake_case__ ) lowerCAmelCase : Optional[int] = self.token_classification_task.convert_examples_to_features( snake_case__ , self.labels , args.max_seq_length , self.tokenizer , cls_token_at_end=bool(self.config.model_type in ["xlnet"] ) , cls_token=self.tokenizer.cls_token , cls_token_segment_id=2 if self.config.model_type in ["xlnet"] else 0 , sep_token=self.tokenizer.sep_token , sep_token_extra=snake_case__ , pad_on_left=bool(self.config.model_type in ["xlnet"] ) , pad_token=self.tokenizer.pad_token_id , pad_token_segment_id=self.tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info("Saving features into cached file %s" , snake_case__ ) torch.save(snake_case__ , snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = False ): """simple docstring""" lowerCAmelCase : Dict = self._feature_file(snake_case__ ) logger.info("Loading features from cached file %s" , snake_case__ ) lowerCAmelCase : Optional[Any] = torch.load(snake_case__ ) lowerCAmelCase : Optional[int] = torch.tensor([f.input_ids for f in features] , dtype=torch.long ) lowerCAmelCase : int = torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) if features[0].token_type_ids is not None: lowerCAmelCase : Tuple = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) else: lowerCAmelCase : Optional[Any] = torch.tensor([0 for f in features] , dtype=torch.long ) # HACK(we will not use this anymore soon) lowerCAmelCase : Optional[int] = torch.tensor([f.label_ids for f in features] , dtype=torch.long ) return DataLoader( TensorDataset(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) , batch_size=snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ ): """simple docstring""" """Compute validation""" "" lowerCAmelCase : str = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]} if self.config.model_type != "distilbert": lowerCAmelCase : List[Any] = ( batch[2] if self.config.model_type in ["bert", "xlnet"] else None ) # XLM and RoBERTa don"t use token_type_ids lowerCAmelCase : Any = self(**snake_case__ ) lowerCAmelCase , lowerCAmelCase : List[str] = outputs[:2] lowerCAmelCase : Dict = logits.detach().cpu().numpy() lowerCAmelCase : Optional[Any] = inputs["labels"].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase : List[str] = torch.stack([x["val_loss"] for x in outputs] ).mean() lowerCAmelCase : Optional[Any] = np.concatenate([x["pred"] for x in outputs] , axis=0 ) lowerCAmelCase : List[Any] = np.argmax(snake_case__ , axis=2 ) lowerCAmelCase : Optional[int] = np.concatenate([x["target"] for x in outputs] , axis=0 ) lowerCAmelCase : str = dict(enumerate(self.labels ) ) lowerCAmelCase : Optional[int] = [[] for _ in range(out_label_ids.shape[0] )] lowerCAmelCase : Dict = [[] for _ in range(out_label_ids.shape[0] )] for i in range(out_label_ids.shape[0] ): for j in range(out_label_ids.shape[1] ): if out_label_ids[i, j] != self.pad_token_label_id: out_label_list[i].append(label_map[out_label_ids[i][j]] ) preds_list[i].append(label_map[preds[i][j]] ) lowerCAmelCase : Dict = { "val_loss": val_loss_mean, "accuracy_score": accuracy_score(snake_case__ , snake_case__ ), "precision": precision_score(snake_case__ , snake_case__ ), "recall": recall_score(snake_case__ , snake_case__ ), "f1": fa_score(snake_case__ , snake_case__ ), } lowerCAmelCase : int = dict(results.items() ) lowerCAmelCase : Tuple = results return ret, preds_list, out_label_list def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : Dict = self._eval_end(snake_case__ ) lowerCAmelCase : int = ret["log"] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def lowercase__ ( self , snake_case__ ): """simple docstring""" lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : int = self._eval_end(snake_case__ ) # Converting to the dict required by pl # https://github.com/PyTorchLightning/pytorch-lightning/blob/master/\ # pytorch_lightning/trainer/logging.py#L139 lowerCAmelCase : str = ret["log"] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def lowercase__ ( snake_case__ , snake_case__ ): """simple docstring""" BaseTransformer.add_model_specific_args(snake_case__ , snake_case__ ) parser.add_argument( "--task_type" , default="NER" , type=snake_case__ , help="Task type to fine tune in training (e.g. NER, POS, etc)" ) parser.add_argument( "--max_seq_length" , default=128 , type=snake_case__ , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--labels" , default="" , type=snake_case__ , help="Path to a file containing all labels. If not specified, CoNLL-2003 labels are used." , ) parser.add_argument( "--gpus" , default=0 , type=snake_case__ , help="The number of GPUs allocated for this, it is by default 0 meaning none" , ) parser.add_argument( "--overwrite_cache" , action="store_true" , help="Overwrite the cached training and evaluation sets" ) return parser if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() add_generic_args(parser, os.getcwd()) lowerCAmelCase__ = NERTransformer.add_model_specific_args(parser, os.getcwd()) lowerCAmelCase__ = parser.parse_args() lowerCAmelCase__ = NERTransformer(args) lowerCAmelCase__ = generic_train(model, args) if args.do_predict: # See https://github.com/huggingface/transformers/issues/3159 # pl use this default format to create a checkpoint: # https://github.com/PyTorchLightning/pytorch-lightning/blob/master\ # /pytorch_lightning/callbacks/model_checkpoint.py#L322 lowerCAmelCase__ = sorted(glob.glob(os.path.join(args.output_dir, '''checkpoint-epoch=*.ckpt'''), recursive=True)) lowerCAmelCase__ = model.load_from_checkpoint(checkpoints[-1]) trainer.test(model)
108
1
from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) _lowerCamelCase : List[str] = { """vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_config_file""": """tokenizer_config.json""", } _lowerCamelCase : str = { """vocab_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json""" }, """merges_file""": { """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt""" }, """tokenizer_config_file""": { """facebook/blenderbot_small-90M""": ( """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json""" ) }, } _lowerCamelCase : Optional[int] = { """facebook/blenderbot_small-90M""": 512, } class UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = BlenderbotSmallTokenizer def __init__( self : Optional[Any] , UpperCAmelCase__ : Dict=None , UpperCAmelCase__ : Optional[Any]=None , UpperCAmelCase__ : int="<|endoftext|>" , UpperCAmelCase__ : int="<|endoftext|>" , UpperCAmelCase__ : str="<|endoftext|>" , UpperCAmelCase__ : Any=False , UpperCAmelCase__ : Tuple=True , **UpperCAmelCase__ : List[Any] , ) ->int: '''simple docstring''' super().__init__( ByteLevelBPETokenizer( vocab=UpperCAmelCase__ , merges=UpperCAmelCase__ , add_prefix_space=UpperCAmelCase__ , trim_offsets=UpperCAmelCase__ , ) , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , **UpperCAmelCase__ , ) A__ = add_prefix_space def SCREAMING_SNAKE_CASE ( self : Optional[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any]=None) ->Tuple: '''simple docstring''' A__ = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self : Dict , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : 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 + sep + token_ids_a + sep) * [0]
360
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class UpperCamelCase_ : '''simple docstring''' def __init__( self : str , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Any=14 , UpperCAmelCase__ : Any=7 , UpperCAmelCase__ : Dict=True , UpperCAmelCase__ : int=True , UpperCAmelCase__ : Tuple=False , UpperCAmelCase__ : Optional[int]=True , UpperCAmelCase__ : Tuple=99 , UpperCAmelCase__ : Optional[Any]=32 , UpperCAmelCase__ : Tuple=4 , UpperCAmelCase__ : Any=4 , UpperCAmelCase__ : int=4 , UpperCAmelCase__ : Optional[Any]=37 , UpperCAmelCase__ : Union[str, Any]="gelu" , UpperCAmelCase__ : Optional[int]=0.1 , UpperCAmelCase__ : int=0.1 , UpperCAmelCase__ : Optional[Any]=512 , UpperCAmelCase__ : Union[str, Any]=0.02 , ) ->Any: '''simple docstring''' A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = rotary_dim A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = initializer_range A__ = None A__ = vocab_size - 1 A__ = vocab_size - 1 A__ = vocab_size - 1 def SCREAMING_SNAKE_CASE ( self : List[str]) ->List[str]: '''simple docstring''' A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length]) A__ = GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=UpperCAmelCase__ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def SCREAMING_SNAKE_CASE ( self : Any) ->Optional[int]: '''simple docstring''' A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE ( self : Any , UpperCAmelCase__ : int , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : List[Any]) ->Optional[int]: '''simple docstring''' A__ = 20 A__ = model_class_name(UpperCAmelCase__) A__ = model.init_cache(input_ids.shape[0] , UpperCAmelCase__) A__ = jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''') A__ = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1)[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1)) A__ = model( input_ids[:, :-1] , attention_mask=UpperCAmelCase__ , past_key_values=UpperCAmelCase__ , position_ids=UpperCAmelCase__ , ) A__ = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''') A__ = model( input_ids[:, -1:] , attention_mask=UpperCAmelCase__ , past_key_values=outputs_cache.past_key_values , position_ids=UpperCAmelCase__ , ) A__ = model(UpperCAmelCase__) A__ = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]))) self.parent.assertTrue(diff < 1e-3 , msg=f"""Max diff is {diff}""") def SCREAMING_SNAKE_CASE ( self : Tuple , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : int) ->Any: '''simple docstring''' A__ = 20 A__ = model_class_name(UpperCAmelCase__) A__ = jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]))] , axis=-1 , ) A__ = model.init_cache(input_ids.shape[0] , UpperCAmelCase__) A__ = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1)[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1)) A__ = model( input_ids[:, :-1] , attention_mask=UpperCAmelCase__ , past_key_values=UpperCAmelCase__ , position_ids=UpperCAmelCase__ , ) A__ = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''') A__ = model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=UpperCAmelCase__ , position_ids=UpperCAmelCase__ , ) A__ = model(UpperCAmelCase__ , attention_mask=UpperCAmelCase__) A__ = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]))) self.parent.assertTrue(diff < 1e-3 , msg=f"""Max diff is {diff}""") @require_flax class UpperCamelCase_ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' UpperCAmelCase__ = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () UpperCAmelCase__ = (FlaxGPTJForCausalLM,) if is_flax_available() else () def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->List[Any]: '''simple docstring''' A__ = FlaxGPTJModelTester(self) def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->Any: '''simple docstring''' for model_class_name in self.all_model_classes: A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : Optional[int]) ->Any: '''simple docstring''' for model_class_name in self.all_model_classes: A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__) @tooslow def SCREAMING_SNAKE_CASE ( self : Optional[Any]) ->Dict: '''simple docstring''' A__ = GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''') A__ = tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=UpperCAmelCase__ , truncation=UpperCAmelCase__) A__ = FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''') A__ = False A__ = model.config.eos_token_id A__ = jax.jit(model.generate) A__ = jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id).sequences A__ = tokenizer.batch_decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__) A__ = [ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(UpperCAmelCase__ , UpperCAmelCase__) @is_pt_flax_cross_test def SCREAMING_SNAKE_CASE ( self : Optional[int]) ->int: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__): # prepare inputs A__ = self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__) A__ = {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class A__ = model_class.__name__[4:] # Skip the "Flax" at the beginning A__ = getattr(UpperCAmelCase__ , UpperCAmelCase__) A__ , A__ = pt_inputs['''input_ids'''].shape A__ = np.random.randint(0 , seq_length - 1 , size=(batch_size,)) for batch_idx, start_index in enumerate(UpperCAmelCase__): A__ = 0 A__ = 1 A__ = 0 A__ = 1 A__ = pt_model_class(UpperCAmelCase__).eval() A__ = model_class(UpperCAmelCase__ , dtype=jnp.floataa) A__ = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , UpperCAmelCase__) A__ = fx_state with torch.no_grad(): A__ = pt_model(**UpperCAmelCase__).to_tuple() A__ = fx_model(**UpperCAmelCase__).to_tuple() self.assertEqual(len(UpperCAmelCase__) , len(UpperCAmelCase__) , '''Output lengths differ between Flax and PyTorch''') for fx_output, pt_output in zip(UpperCAmelCase__ , UpperCAmelCase__): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(UpperCAmelCase__) A__ = model_class.from_pretrained(UpperCAmelCase__ , from_pt=UpperCAmelCase__) A__ = fx_model_loaded(**UpperCAmelCase__).to_tuple() self.assertEqual( len(UpperCAmelCase__) , len(UpperCAmelCase__) , '''Output lengths differ between Flax and PyTorch''') for fx_output_loaded, pt_output in zip(UpperCAmelCase__ , UpperCAmelCase__): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2) @is_pt_flax_cross_test def SCREAMING_SNAKE_CASE ( self : Any) ->Optional[int]: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__): # prepare inputs A__ = self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__) A__ = {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class A__ = model_class.__name__[4:] # Skip the "Flax" at the beginning A__ = getattr(UpperCAmelCase__ , UpperCAmelCase__) A__ = pt_model_class(UpperCAmelCase__).eval() A__ = model_class(UpperCAmelCase__ , dtype=jnp.floataa) A__ = load_flax_weights_in_pytorch_model(UpperCAmelCase__ , fx_model.params) A__ , A__ = pt_inputs['''input_ids'''].shape A__ = np.random.randint(0 , seq_length - 1 , size=(batch_size,)) for batch_idx, start_index in enumerate(UpperCAmelCase__): A__ = 0 A__ = 1 A__ = 0 A__ = 1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): A__ = pt_model(**UpperCAmelCase__).to_tuple() A__ = fx_model(**UpperCAmelCase__).to_tuple() self.assertEqual(len(UpperCAmelCase__) , len(UpperCAmelCase__) , '''Output lengths differ between Flax and PyTorch''') for fx_output, pt_output in zip(UpperCAmelCase__ , UpperCAmelCase__): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(UpperCAmelCase__) A__ = pt_model_class.from_pretrained(UpperCAmelCase__ , from_flax=UpperCAmelCase__) with torch.no_grad(): A__ = pt_model_loaded(**UpperCAmelCase__).to_tuple() self.assertEqual( len(UpperCAmelCase__) , len(UpperCAmelCase__) , '''Output lengths differ between Flax and PyTorch''') for fx_output, pt_output in zip(UpperCAmelCase__ , UpperCAmelCase__): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2) @tooslow def SCREAMING_SNAKE_CASE ( self : Tuple) ->Optional[Any]: '''simple docstring''' for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''') A__ = model(np.ones((1, 1))) self.assertIsNotNone(UpperCAmelCase__)
231
0
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCamelCase = logging.get_logger(__name__) __lowerCamelCase = { "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 UpperCamelCase__( __A ): lowerCAmelCase__ : str = 'sew-d' def __init__( self ,__UpperCAmelCase=32 ,__UpperCAmelCase=7_68 ,__UpperCAmelCase=12 ,__UpperCAmelCase=12 ,__UpperCAmelCase=30_72 ,__UpperCAmelCase=2 ,__UpperCAmelCase=5_12 ,__UpperCAmelCase=2_56 ,__UpperCAmelCase=True ,__UpperCAmelCase=True ,__UpperCAmelCase=("p2c", "c2p") ,__UpperCAmelCase="layer_norm" ,__UpperCAmelCase="gelu_python" ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=0.0 ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=0.0_2 ,__UpperCAmelCase=1e-7 ,__UpperCAmelCase=1e-5 ,__UpperCAmelCase="group" ,__UpperCAmelCase="gelu" ,__UpperCAmelCase=(64, 1_28, 1_28, 1_28, 1_28, 2_56, 2_56, 2_56, 2_56, 5_12, 5_12, 5_12, 5_12) ,__UpperCAmelCase=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,__UpperCAmelCase=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,__UpperCAmelCase=False ,__UpperCAmelCase=1_28 ,__UpperCAmelCase=16 ,__UpperCAmelCase=True ,__UpperCAmelCase=0.0_5 ,__UpperCAmelCase=10 ,__UpperCAmelCase=2 ,__UpperCAmelCase=0.0 ,__UpperCAmelCase=10 ,__UpperCAmelCase=0 ,__UpperCAmelCase="mean" ,__UpperCAmelCase=False ,__UpperCAmelCase=False ,__UpperCAmelCase=2_56 ,__UpperCAmelCase=0 ,__UpperCAmelCase=1 ,__UpperCAmelCase=2 ,**__UpperCAmelCase ,) -> Optional[int]: super().__init__(**__UpperCAmelCase ,pad_token_id=__UpperCAmelCase ,bos_token_id=__UpperCAmelCase ,eos_token_id=__UpperCAmelCase ) A__ = hidden_size A__ = feat_extract_norm A__ = feat_extract_activation A__ = list(__UpperCAmelCase ) A__ = list(__UpperCAmelCase ) A__ = list(__UpperCAmelCase ) A__ = conv_bias A__ = num_conv_pos_embeddings A__ = num_conv_pos_embedding_groups A__ = len(self.conv_dim ) A__ = num_hidden_layers A__ = intermediate_size A__ = squeeze_factor A__ = max_position_embeddings A__ = position_buckets A__ = share_att_key A__ = relative_attention A__ = norm_rel_ebd A__ = list(__UpperCAmelCase ) A__ = hidden_act A__ = num_attention_heads A__ = hidden_dropout A__ = attention_dropout A__ = activation_dropout A__ = feat_proj_dropout A__ = final_dropout A__ = layer_norm_eps A__ = feature_layer_norm_eps A__ = initializer_range A__ = 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 A__ = apply_spec_augment A__ = mask_time_prob A__ = mask_time_length A__ = mask_time_min_masks A__ = mask_feature_prob A__ = mask_feature_length A__ = mask_feature_min_masks # ctc loss A__ = ctc_loss_reduction A__ = ctc_zero_infinity # sequence classification A__ = use_weighted_layer_sum A__ = classifier_proj_size @property def snake_case__ ( self ) -> str: return functools.reduce(operator.mul ,self.conv_stride ,1 )
221
"""simple docstring""" import logging import os from typing import Dict, List, Optional, Union import torch import torch.nn as nn from accelerate.utils.imports import ( is_abit_bnb_available, is_abit_bnb_available, is_bnb_available, ) from ..big_modeling import dispatch_model, init_empty_weights from .dataclasses import BnbQuantizationConfig from .modeling import ( find_tied_parameters, get_balanced_memory, infer_auto_device_map, load_checkpoint_in_model, offload_weight, set_module_tensor_to_device, ) if is_bnb_available(): import bitsandbytes as bnb from copy import deepcopy __lowerCamelCase = logging.getLogger(__name__) def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = False , ): """simple docstring""" A__ = bnb_quantization_config.load_in_abit A__ = bnb_quantization_config.load_in_abit if load_in_abit and not is_abit_bnb_available(): raise ImportError( 'You have a version of `bitsandbytes` that is not compatible with 8bit quantization,' ' make sure you have the latest version of `bitsandbytes` installed.' ) if load_in_abit and not is_abit_bnb_available(): raise ValueError( 'You have a version of `bitsandbytes` that is not compatible with 4bit quantization,' 'make sure you have the latest version of `bitsandbytes` installed.' ) A__ = [] # custom device map if isinstance(UpperCamelCase__ , UpperCamelCase__ ) and len(device_map.keys() ) > 1: A__ = [key for key, value in device_map.items() if value in ['disk', 'cpu']] # We keep some modules such as the lm_head in their original dtype for numerical stability reasons if bnb_quantization_config.skip_modules is None: A__ = get_keys_to_not_convert(UpperCamelCase__ ) # add cpu modules to skip modules only for 4-bit modules if load_in_abit: bnb_quantization_config.skip_modules.extend(UpperCamelCase__ ) A__ = bnb_quantization_config.skip_modules # We add the modules we want to keep in full precision if bnb_quantization_config.keep_in_fpaa_modules is None: A__ = [] A__ = bnb_quantization_config.keep_in_fpaa_modules modules_to_not_convert.extend(UpperCamelCase__ ) # compatibility with peft A__ = load_in_abit A__ = load_in_abit A__ = get_parameter_device(UpperCamelCase__ ) if model_device.type != "meta": # quantization of an already loaded model logger.warning( 'It is not recommended to quantize a loaded model. ' 'The model should be instantiated under the `init_empty_weights` context manager.' ) A__ = replace_with_bnb_layers(UpperCamelCase__ , UpperCamelCase__ , modules_to_not_convert=UpperCamelCase__ ) # convert param to the right dtype A__ = bnb_quantization_config.torch_dtype for name, param in model.state_dict().items(): if any(module_to_keep_in_fpaa in name for module_to_keep_in_fpaa in keep_in_fpaa_modules ): param.to(torch.floataa ) if param.dtype != torch.floataa: A__ = name.replace('.weight' , '' ).replace('.bias' , '' ) A__ = getattr(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if param is not None: param.to(torch.floataa ) elif torch.is_floating_point(UpperCamelCase__ ): param.to(UpperCamelCase__ ) if model_device.type == "cuda": # move everything to cpu in the first place because we can't do quantization if the weights are already on cuda model.cuda(torch.cuda.current_device() ) torch.cuda.empty_cache() elif torch.cuda.is_available(): model.to(torch.cuda.current_device() ) else: raise RuntimeError('No GPU found. A GPU is needed for quantization.' ) logger.info( F'''The model device type is {model_device.type}. However, cuda is needed for quantization.''' 'We move the model to cuda.' ) return model elif weights_location is None: raise RuntimeError( F'''`weights_location` needs to be the folder path containing the weights of the model, but we found {weights_location} ''' ) else: with init_empty_weights(): A__ = replace_with_bnb_layers( UpperCamelCase__ , UpperCamelCase__ , modules_to_not_convert=UpperCamelCase__ ) A__ = get_quantized_model_device_map( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , max_memory=UpperCamelCase__ , no_split_module_classes=UpperCamelCase__ , ) if offload_state_dict is None and device_map is not None and "disk" in device_map.values(): A__ = True A__ = any(x in list(device_map.values() ) for x in ['cpu', 'disk'] ) load_checkpoint_in_model( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , dtype=bnb_quantization_config.torch_dtype , offload_folder=UpperCamelCase__ , offload_state_dict=UpperCamelCase__ , keep_in_fpaa_modules=bnb_quantization_config.keep_in_fpaa_modules , offload_abit_bnb=load_in_abit and offload , ) return dispatch_model(UpperCamelCase__ , device_map=UpperCamelCase__ , offload_dir=UpperCamelCase__ ) def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None ): """simple docstring""" if device_map is None: if torch.cuda.is_available(): A__ = {'': torch.cuda.current_device()} else: raise RuntimeError('No GPU found. A GPU is needed for quantization.' ) logger.info('The device_map was not initialized.' 'Setting device_map to `{\'\':torch.cuda.current_device()}`.' ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ): if device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]: raise ValueError( 'If passing a string for `device_map`, please choose \'auto\', \'balanced\', \'balanced_low_0\' or ' '\'sequential\'.' ) A__ = {} special_dtypes.update( { name: bnb_quantization_config.torch_dtype for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.skip_modules ) } ) special_dtypes.update( { name: torch.floataa for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.keep_in_fpaa_modules ) } ) A__ = {} A__ = special_dtypes A__ = no_split_module_classes A__ = bnb_quantization_config.target_dtype # get max_memory for each device. if device_map != "sequential": A__ = get_balanced_memory( UpperCamelCase__ , low_zero=(device_map == 'balanced_low_0') , max_memory=UpperCamelCase__ , **UpperCamelCase__ , ) A__ = max_memory A__ = infer_auto_device_map(UpperCamelCase__ , **UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ): # check if don't have any quantized module on the cpu A__ = bnb_quantization_config.skip_modules + bnb_quantization_config.keep_in_fpaa_modules A__ = { key: device_map[key] for key in device_map.keys() if key not in modules_not_to_convert } for device in ["cpu", "disk"]: if device in device_map_without_some_modules.values(): if bnb_quantization_config.load_in_abit: raise ValueError( '\n Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit\n the quantized model. If you want to dispatch the model on the CPU or the disk while keeping\n these modules in `torch_dtype`, you need to pass a custom `device_map` to\n `load_and_quantize_model`. Check\n https://huggingface.co/docs/accelerate/main/en/usage_guides/quantization#offload-modules-to-cpu-and-disk\n for more details.\n ' ) else: logger.info( 'Some modules are are offloaded to the CPU or the disk. Note that these modules will be converted to 8-bit' ) del device_map_without_some_modules return device_map def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=None ): """simple docstring""" if modules_to_not_convert is None: A__ = [] A__ , A__ = _replace_with_bnb_layers( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) if not has_been_replaced: logger.warning( 'You are loading your model in 8bit or 4bit but no linear modules were found in your model.' ' this can happen for some architectures such as gpt2 that uses Conv1D instead of Linear layers.' ' Please double check your model architecture, or submit an issue on github if you think this is' ' a bug.' ) return model def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=None , ): """simple docstring""" A__ = False for name, module in model.named_children(): if current_key_name is None: A__ = [] current_key_name.append(UpperCamelCase__ ) if isinstance(UpperCamelCase__ , nn.Linear ) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` A__ = '.'.join(UpperCamelCase__ ) A__ = True for key in modules_to_not_convert: if ( (key in current_key_name_str) and (key + "." in current_key_name_str) ) or key == current_key_name_str: A__ = False break if proceed: # Load bnb module with empty weight and replace ``nn.Linear` module if bnb_quantization_config.load_in_abit: A__ = bnb.nn.LinearabitLt( module.in_features , module.out_features , module.bias is not None , has_fpaa_weights=UpperCamelCase__ , threshold=bnb_quantization_config.llm_inta_threshold , ) elif bnb_quantization_config.load_in_abit: A__ = bnb.nn.Linearabit( module.in_features , module.out_features , module.bias is not None , bnb_quantization_config.bnb_abit_compute_dtype , compress_statistics=bnb_quantization_config.bnb_abit_use_double_quant , quant_type=bnb_quantization_config.bnb_abit_quant_type , ) else: raise ValueError('load_in_8bit and load_in_4bit can\'t be both False' ) A__ = module.weight.data if module.bias is not None: A__ = module.bias.data bnb_module.requires_grad_(UpperCamelCase__ ) setattr(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ = True if len(list(module.children() ) ) > 0: A__ , A__ = _replace_with_bnb_layers( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A__ = has_been_replaced | _has_been_replaced # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def UpperCAmelCase ( UpperCamelCase__ ): """simple docstring""" with init_empty_weights(): A__ = deepcopy(UpperCamelCase__ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` A__ = find_tied_parameters(UpperCamelCase__ ) # For compatibility with Accelerate < 0.18 if isinstance(UpperCamelCase__ , UpperCamelCase__ ): A__ = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: A__ = sum(UpperCamelCase__ , [] ) A__ = len(UpperCamelCase__ ) > 0 # Check if it is a base model A__ = False if hasattr(UpperCamelCase__ , 'base_model_prefix' ): A__ = not hasattr(UpperCamelCase__ , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head A__ = list(model.named_children() ) A__ = [list_modules[-1][0]] # add last module together with tied weights A__ = set(UpperCamelCase__ ) - set(UpperCamelCase__ ) A__ = list(set(UpperCamelCase__ ) ) + list(UpperCamelCase__ ) # remove ".weight" from the keys A__ = ['.weight', '.bias'] A__ = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: A__ = name.replace(UpperCamelCase__ , '' ) filtered_module_names.append(UpperCamelCase__ ) return filtered_module_names def UpperCAmelCase ( UpperCamelCase__ ): """simple docstring""" for m in model.modules(): if isinstance(UpperCamelCase__ , bnb.nn.Linearabit ): return True return False def UpperCAmelCase ( UpperCamelCase__ ): """simple docstring""" return next(parameter.parameters() ).device def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" if fpaa_statistics is None: set_module_tensor_to_device(UpperCamelCase__ , UpperCamelCase__ , 0 , dtype=UpperCamelCase__ , value=UpperCamelCase__ ) A__ = param_name A__ = model if "." in tensor_name: A__ = tensor_name.split('.' ) for split in splits[:-1]: A__ = getattr(UpperCamelCase__ , UpperCamelCase__ ) if new_module is None: raise ValueError(F'''{module} has no attribute {split}.''' ) A__ = new_module A__ = splits[-1] # offload weights A__ = False offload_weight(module._parameters[tensor_name] , UpperCamelCase__ , UpperCamelCase__ , index=UpperCamelCase__ ) if hasattr(module._parameters[tensor_name] , 'SCB' ): offload_weight( module._parameters[tensor_name].SCB , param_name.replace('weight' , 'SCB' ) , UpperCamelCase__ , index=UpperCamelCase__ , ) else: offload_weight(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , index=UpperCamelCase__ ) offload_weight(UpperCamelCase__ , param_name.replace('weight' , 'SCB' ) , UpperCamelCase__ , index=UpperCamelCase__ ) set_module_tensor_to_device(UpperCamelCase__ , UpperCamelCase__ , 'meta' , dtype=UpperCamelCase__ , value=torch.empty(*param.size() ) )
221
1
import operator as op lowercase : List[Any] = 'scaler.pt' lowercase : List[str] = 'pytorch_model' lowercase : List[str] = 'random_states' lowercase : str = 'optimizer' lowercase : Any = 'scheduler' lowercase : Optional[Any] = 'pytorch_model.bin' lowercase : Dict = 'pytorch_model.bin.index.json' lowercase : int = 'model.safetensors' lowercase : int = 'model.safetensors.index.json' lowercase : Optional[int] = '1.10.2' lowercase : Any = 'py38' lowercase : int = '4.17.0' lowercase : Optional[int] = ['ml.p3.16xlarge', 'ml.p3dn.24xlarge', 'ml.p4dn.24xlarge'] lowercase : Optional[int] = ['FULL_SHARD', 'SHARD_GRAD_OP', 'NO_SHARD', 'HYBRID_SHARD', 'HYBRID_SHARD_ZERO2'] lowercase : Optional[Any] = ['TRANSFORMER_BASED_WRAP', 'SIZE_BASED_WRAP', 'NO_WRAP'] lowercase : Any = ['BACKWARD_PRE', 'BACKWARD_POST', 'NO_PREFETCH'] lowercase : Optional[int] = ['FULL_STATE_DICT', 'LOCAL_STATE_DICT', 'SHARDED_STATE_DICT'] lowercase : Optional[Any] = '2.0.1' lowercase : Any = ['pdsh', 'standard', 'openmpi', 'mvapich'] lowercase : Optional[int] = ['default', 'reduce-overhead', 'max-autotune'] lowercase : List[Any] = {'>': op.gt, '>=': op.ge, '==': op.eq, '!=': op.ne, '<=': op.le, '<': op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 lowercase : Optional[int] = [ 'nnodes', 'nproc_per_node', 'rdzv_backend', 'rdzv_endpoint', 'rdzv_id', 'rdzv_conf', 'standalone', 'max_restarts', 'monitor_interval', 'start_method', 'role', 'module', 'm', 'no_python', 'run_path', 'log_dir', 'r', 'redirects', 't', 'tee', 'node_rank', 'master_addr', 'master_port', ] lowercase : Optional[Any] = ['DEEPSPEED', 'MULTI_GPU', 'FSDP', 'MEGATRON_LM'] lowercase : int = ['DEEPSPEED', 'MULTI_XPU', 'FSDP']
151
import copy from dataclasses import dataclass from pathlib import Path from typing import Dict, Optional, Union @dataclass class lowerCamelCase__ : '''simple docstring''' _A = None _A = False _A = False _A = False _A = None _A = None _A = False _A = False _A = False _A = True _A = None _A = 1 _A = None _A = False _A = None _A = None def _lowerCamelCase ( self :List[Any] ) -> "DownloadConfig": return self.__class__(**{k: copy.deepcopy(a ) for k, v in self.__dict__.items()} )
151
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class _UpperCamelCase ( A , unittest.TestCase ): '''simple docstring''' lowerCAmelCase__ = ShapEPipeline lowerCAmelCase__ = ["""prompt"""] lowerCAmelCase__ = ["""prompt"""] lowerCAmelCase__ = [ """num_images_per_prompt""", """num_inference_steps""", """generator""", """latents""", """guidance_scale""", """frame_size""", """output_type""", """return_dict""", ] lowerCAmelCase__ = False @property def __lowerCamelCase ( self : List[Any]): '''simple docstring''' return 3_2 @property def __lowerCamelCase ( self : List[str]): '''simple docstring''' return 3_2 @property def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' return self.time_input_dim * 4 @property def __lowerCamelCase ( self : List[str]): '''simple docstring''' return 8 @property def __lowerCamelCase ( self : int): '''simple docstring''' __lowercase =CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip') return tokenizer @property def __lowerCamelCase ( self : str): '''simple docstring''' torch.manual_seed(0) __lowercase =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) return CLIPTextModelWithProjection(_lowerCAmelCase) @property def __lowerCamelCase ( self : Any): '''simple docstring''' torch.manual_seed(0) __lowercase ={ 'num_attention_heads': 2, 'attention_head_dim': 1_6, 'embedding_dim': self.time_input_dim, 'num_embeddings': 3_2, 'embedding_proj_dim': self.text_embedder_hidden_size, 'time_embed_dim': self.time_embed_dim, 'num_layers': 1, 'clip_embed_dim': self.time_input_dim * 2, 'additional_embeddings': 0, 'time_embed_act_fn': 'gelu', 'norm_in_type': 'layer', 'encoder_hid_proj_type': None, 'added_emb_type': None, } __lowercase =PriorTransformer(**_lowerCAmelCase) return model @property def __lowerCamelCase ( self : List[Any]): '''simple docstring''' torch.manual_seed(0) __lowercase ={ 'param_shapes': ( (self.renderer_dim, 9_3), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), 'd_latent': self.time_input_dim, 'd_hidden': self.renderer_dim, 'n_output': 1_2, 'background': ( 0.1, 0.1, 0.1, ), } __lowercase =ShapERenderer(**_lowerCAmelCase) return model def __lowerCamelCase ( self : str): '''simple docstring''' __lowercase =self.dummy_prior __lowercase =self.dummy_text_encoder __lowercase =self.dummy_tokenizer __lowercase =self.dummy_renderer __lowercase =HeunDiscreteScheduler( beta_schedule='exp' , num_train_timesteps=1_0_2_4 , prediction_type='sample' , use_karras_sigmas=_lowerCAmelCase , clip_sample=_lowerCAmelCase , clip_sample_range=1.0 , ) __lowercase ={ 'prior': prior, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'renderer': renderer, 'scheduler': scheduler, } return components def __lowerCamelCase ( self : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any]=0): '''simple docstring''' if str(_lowerCAmelCase).startswith('mps'): __lowercase =torch.manual_seed(_lowerCAmelCase) else: __lowercase =torch.Generator(device=_lowerCAmelCase).manual_seed(_lowerCAmelCase) __lowercase ={ 'prompt': 'horse', 'generator': generator, 'num_inference_steps': 1, 'frame_size': 3_2, 'output_type': 'np', } return inputs def __lowerCamelCase ( self : List[str]): '''simple docstring''' __lowercase ='cpu' __lowercase =self.get_dummy_components() __lowercase =self.pipeline_class(**_lowerCAmelCase) __lowercase =pipe.to(_lowerCAmelCase) pipe.set_progress_bar_config(disable=_lowerCAmelCase) __lowercase =pipe(**self.get_dummy_inputs(_lowerCAmelCase)) __lowercase =output.images[0] __lowercase =image[0, -3:, -3:, -1] assert image.shape == (2_0, 3_2, 3_2, 3) __lowercase =np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2 def __lowerCamelCase ( self : List[str]): '''simple docstring''' self._test_inference_batch_consistent(batch_sizes=[1, 2]) def __lowerCamelCase ( self : Tuple): '''simple docstring''' __lowercase =torch_device == 'cpu' __lowercase =True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=_lowerCAmelCase , relax_max_difference=_lowerCAmelCase , ) def __lowerCamelCase ( self : Optional[Any]): '''simple docstring''' __lowercase =self.get_dummy_components() __lowercase =self.pipeline_class(**_lowerCAmelCase) __lowercase =pipe.to(_lowerCAmelCase) pipe.set_progress_bar_config(disable=_lowerCAmelCase) __lowercase =1 __lowercase =2 __lowercase =self.get_dummy_inputs(_lowerCAmelCase) for key in inputs.keys(): if key in self.batch_params: __lowercase =batch_size * [inputs[key]] __lowercase =pipe(**_lowerCAmelCase , num_images_per_prompt=_lowerCAmelCase)[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' def __lowerCamelCase ( self : List[str]): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCamelCase ( self : Optional[Any]): '''simple docstring''' __lowercase =load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/shap_e/test_shap_e_np_out.npy') __lowercase =ShapEPipeline.from_pretrained('openai/shap-e') __lowercase =pipe.to(_lowerCAmelCase) pipe.set_progress_bar_config(disable=_lowerCAmelCase) __lowercase =torch.Generator(device=_lowerCAmelCase).manual_seed(0) __lowercase =pipe( 'a shark' , generator=_lowerCAmelCase , guidance_scale=15.0 , num_inference_steps=6_4 , frame_size=6_4 , output_type='np' , ).images[0] assert images.shape == (2_0, 6_4, 6_4, 3) assert_mean_pixel_difference(_lowerCAmelCase , _lowerCAmelCase)
166
'''simple docstring''' import os import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers.models.realm.configuration_realm import RealmConfig from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer class _UpperCamelCase ( A ): '''simple docstring''' def __lowerCamelCase ( self : int): '''simple docstring''' __lowercase =tempfile.mkdtemp() __lowercase =5 # Realm tok __lowercase =[ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'test', 'question', 'this', 'is', 'the', 'first', 'second', 'third', 'fourth', 'fifth', 'record', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] __lowercase =os.path.join(self.tmpdirname , 'realm_tokenizer') os.makedirs(_lowerCAmelCase , exist_ok=_lowerCAmelCase) __lowercase =os.path.join(_lowerCAmelCase , VOCAB_FILES_NAMES['vocab_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens])) __lowercase =os.path.join(self.tmpdirname , 'realm_block_records') os.makedirs(_lowerCAmelCase , exist_ok=_lowerCAmelCase) def __lowerCamelCase ( self : Dict): '''simple docstring''' return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'realm_tokenizer')) def __lowerCamelCase ( self : List[Any]): '''simple docstring''' shutil.rmtree(self.tmpdirname) def __lowerCamelCase ( self : List[str]): '''simple docstring''' __lowercase =RealmConfig(num_block_records=self.num_block_records) return config def __lowerCamelCase ( self : Dict): '''simple docstring''' __lowercase =Dataset.from_dict( { 'id': ['0', '1'], 'question': ['foo', 'bar'], 'answers': [['Foo', 'Bar'], ['Bar']], }) return dataset def __lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __lowercase =np.array( [ B'This is the first record', B'This is the second record', B'This is the third record', B'This is the fourth record', B'This is the fifth record', B'This is a longer longer longer record', ] , dtype=_lowerCAmelCase , ) return block_records def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' __lowercase =RealmRetriever( block_records=self.get_dummy_block_records() , tokenizer=self.get_tokenizer() , ) return retriever def __lowerCamelCase ( self : int): '''simple docstring''' __lowercase =self.get_config() __lowercase =self.get_dummy_retriever() __lowercase =retriever.tokenizer __lowercase =np.array([0, 3] , dtype='long') __lowercase =tokenizer(['Test question']).input_ids __lowercase =tokenizer( ['the fourth'] , add_special_tokens=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ).input_ids __lowercase =config.reader_seq_len __lowercase , __lowercase , __lowercase , __lowercase =retriever( _lowerCAmelCase , _lowerCAmelCase , answer_ids=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np') self.assertEqual(len(_lowerCAmelCase) , 2) self.assertEqual(len(_lowerCAmelCase) , 2) self.assertEqual(len(_lowerCAmelCase) , 2) self.assertEqual(concat_inputs.input_ids.shape , (2, 1_0)) self.assertEqual(concat_inputs.attention_mask.shape , (2, 1_0)) self.assertEqual(concat_inputs.token_type_ids.shape , (2, 1_0)) self.assertEqual(concat_inputs.special_tokens_mask.shape , (2, 1_0)) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]) , ['[CLS]', 'test', 'question', '[SEP]', 'this', 'is', 'the', 'first', 'record', '[SEP]'] , ) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]) , ['[CLS]', 'test', 'question', '[SEP]', 'this', 'is', 'the', 'fourth', 'record', '[SEP]'] , ) def __lowerCamelCase ( self : Dict): '''simple docstring''' __lowercase =self.get_config() __lowercase =self.get_dummy_retriever() __lowercase =retriever.tokenizer __lowercase =np.array([0, 3, 5] , dtype='long') __lowercase =tokenizer(['Test question']).input_ids __lowercase =tokenizer( ['the fourth', 'longer longer'] , add_special_tokens=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ).input_ids __lowercase =config.reader_seq_len __lowercase , __lowercase , __lowercase , __lowercase =retriever( _lowerCAmelCase , _lowerCAmelCase , answer_ids=_lowerCAmelCase , max_length=_lowerCAmelCase , return_tensors='np') self.assertEqual([False, True, True] , _lowerCAmelCase) self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]] , _lowerCAmelCase) self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]] , _lowerCAmelCase) def __lowerCamelCase ( self : Tuple): '''simple docstring''' __lowercase =self.get_dummy_retriever() retriever.save_pretrained(os.path.join(self.tmpdirname , 'realm_block_records')) # Test local path __lowercase =retriever.from_pretrained(os.path.join(self.tmpdirname , 'realm_block_records')) self.assertEqual(retriever.block_records[0] , B'This is the first record') # Test mocked remote path with patch('transformers.models.realm.retrieval_realm.hf_hub_download') as mock_hf_hub_download: __lowercase =os.path.join( os.path.join(self.tmpdirname , 'realm_block_records') , _REALM_BLOCK_RECORDS_FILENAME) __lowercase =RealmRetriever.from_pretrained('google/realm-cc-news-pretrained-openqa') self.assertEqual(retriever.block_records[0] , B'This is the first record')
166
1
import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def UpperCamelCase ( __lowercase : Optional[Any] ,__lowercase : Dict ,__lowercase : Any ): '''simple docstring''' A_ : Tuple = LxmertConfig.from_json_file(a_ ) print(f'''Building PyTorch model from configuration: {config}''' ) A_ : Union[str, Any] = LxmertForPreTraining(a_ ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(a_ ,a_ ,a_ ) # Save pytorch-model print(f'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() ,a_ ) if __name__ == "__main__": _UpperCAmelCase = 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.""" ) _UpperCAmelCase = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
367
import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @property def lowerCAmelCase_ ( self ): """simple docstring""" torch.manual_seed(0 ) A_ : int = 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') , ) return model @property def lowerCAmelCase_ ( self ): """simple docstring""" torch.manual_seed(0 ) A_ : Optional[Any] = VQModel( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=3 , ) return model @property def lowerCAmelCase_ ( self ): """simple docstring""" torch.manual_seed(0 ) A_ : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) return CLIPTextModel(lowercase ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : Optional[int] = self.dummy_uncond_unet A_ : List[Any] = DDIMScheduler() A_ : Any = self.dummy_vq_model A_ : int = LDMPipeline(unet=lowercase , vqvae=lowercase , scheduler=lowercase ) ldm.to(lowercase ) ldm.set_progress_bar_config(disable=lowercase ) A_ : Any = torch.manual_seed(0 ) A_ : Dict = ldm(generator=lowercase , num_inference_steps=2 , output_type='numpy' ).images A_ : Any = torch.manual_seed(0 ) A_ : List[str] = ldm(generator=lowercase , num_inference_steps=2 , output_type='numpy' , return_dict=lowercase )[0] A_ : Union[str, Any] = image[0, -3:, -3:, -1] A_ : int = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) A_ : List[str] = np.array([0.8512, 0.818, 0.6411, 0.6808, 0.4465, 0.5618, 0.46, 0.6231, 0.5172] ) A_ : str = 1E-2 if torch_device != 'mps' else 3E-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < tolerance @slow @require_torch class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self ): """simple docstring""" A_ : List[Any] = LDMPipeline.from_pretrained('CompVis/ldm-celebahq-256' ) ldm.to(lowercase ) ldm.set_progress_bar_config(disable=lowercase ) A_ : Any = torch.manual_seed(0 ) A_ : List[str] = ldm(generator=lowercase , num_inference_steps=5 , output_type='numpy' ).images A_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 2_5_6, 2_5_6, 3) A_ : Tuple = np.array([0.4399, 0.4_4975, 0.4_6825, 0.474, 0.4359, 0.4581, 0.4_5095, 0.4341, 0.4447] ) A_ : Dict = 1E-2 if torch_device != 'mps' else 3E-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance
192
0
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets A__: Optional[int] = ''' Compute the Matthews correlation coefficient (MCC) The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary and multiclass classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction. The statistic is also known as the phi coefficient. [source: Wikipedia] ''' A__: List[Any] = ''' Args: predictions (list of int): Predicted labels, as returned by a model. references (list of int): Ground truth labels. sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`. Returns: matthews_correlation (dict containing float): Matthews correlation. Examples: Example 1, a basic example with only predictions and references as inputs: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3]) >>> print(round(results[\'matthews_correlation\'], 2)) 0.54 Example 2, the same example as above, but also including sample weights: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 3, 1, 1, 1, 2]) >>> print(round(results[\'matthews_correlation\'], 2)) 0.1 Example 3, the same example as above, but with sample weights that cause a negative correlation: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 1, 0, 0, 0, 1]) >>> print(round(results[\'matthews_correlation\'], 2)) -0.25 ''' A__: str = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A__ ( datasets.Metric ): def __UpperCAmelCase ( self :List[str] ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def __UpperCAmelCase ( self :Dict , SCREAMING_SNAKE_CASE :str , SCREAMING_SNAKE_CASE :List[Any] , SCREAMING_SNAKE_CASE :Tuple=None ) -> Tuple: '''simple docstring''' return { "matthews_correlation": float(matthews_corrcoef(UpperCamelCase_ , UpperCamelCase_ , sample_weight=UpperCamelCase_ ) ), }
276
'''simple docstring''' import re from filelock import FileLock try: import nltk __snake_case = True except (ImportError, ModuleNotFoundError): __snake_case = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def a ( __a ) -> str: '''simple docstring''' re.sub('''<n>''' , '''''' , __a ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(__a ) )
97
0
'''simple docstring''' import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging __snake_case = logging.get_logger(__name__) def a ( __a ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ :Tuple = R'''\w+[.]\d+''' UpperCamelCase__ :Dict = re.findall(__a , __a ) for pat in pats: UpperCamelCase__ :Dict = key.replace(__a , '''_'''.join(pat.split('''.''' ) ) ) return key def a ( __a , __a , __a ) -> Dict: '''simple docstring''' UpperCamelCase__ :Optional[int] = pt_tuple_key[:-1] + ('''scale''',) if ( any('''norm''' in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): UpperCamelCase__ :int = pt_tuple_key[:-1] + ('''scale''',) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: UpperCamelCase__ :Optional[int] = pt_tuple_key[:-1] + ('''scale''',) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: UpperCamelCase__ :str = pt_tuple_key[:-1] + ('''embedding''',) return renamed_pt_tuple_key, pt_tensor # conv layer UpperCamelCase__ :Any = pt_tuple_key[:-1] + ('''kernel''',) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: UpperCamelCase__ :int = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer UpperCamelCase__ :Union[str, Any] = pt_tuple_key[:-1] + ('''kernel''',) if pt_tuple_key[-1] == "weight": UpperCamelCase__ :int = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight UpperCamelCase__ :Tuple = pt_tuple_key[:-1] + ('''weight''',) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias UpperCamelCase__ :List[Any] = pt_tuple_key[:-1] + ('''bias''',) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def a ( __a , __a , __a=42 ) -> str: '''simple docstring''' UpperCamelCase__ :List[str] = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params UpperCamelCase__ :Tuple = flax_model.init_weights(PRNGKey(__a ) ) UpperCamelCase__ :Optional[int] = flatten_dict(__a ) UpperCamelCase__ :List[Any] = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCamelCase__ :Any = rename_key(__a ) UpperCamelCase__ :Tuple = tuple(renamed_pt_key.split('''.''' ) ) # Correctly rename weight parameters UpperCamelCase__ , UpperCamelCase__ :int = rename_key_and_reshape_tensor(__a , __a , __a ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( f'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape ''' f'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.''' ) # also add unexpected weight so that warning is thrown UpperCamelCase__ :Any = jnp.asarray(__a ) return unflatten_dict(__a )
219
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class lowercase ( A__ , A__ , A__ , unittest.TestCase ): """simple docstring""" _a = AltDiffusionPipeline _a = TEXT_TO_IMAGE_PARAMS _a = TEXT_TO_IMAGE_BATCH_PARAMS _a = TEXT_TO_IMAGE_IMAGE_PARAMS _a = TEXT_TO_IMAGE_IMAGE_PARAMS def lowerCAmelCase__ ( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCamelCase__ :List[str] = 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 , ) UpperCamelCase__ :int = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=UpperCamelCase_ , set_alpha_to_one=UpperCamelCase_ , ) torch.manual_seed(0 ) UpperCamelCase__ :List[str] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) UpperCamelCase__ :Union[str, Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5002 , ) UpperCamelCase__ :Optional[Any] = CLIPTextModel(UpperCamelCase_ ) UpperCamelCase__ :int = XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' ) UpperCamelCase__ :Dict = 77 UpperCamelCase__ :Dict = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def lowerCAmelCase__ ( self , UpperCamelCase_ , UpperCamelCase_=0 ): '''simple docstring''' if str(UpperCamelCase_ ).startswith('''mps''' ): UpperCamelCase__ :Dict = torch.manual_seed(UpperCamelCase_ ) else: UpperCamelCase__ :List[str] = torch.Generator(device=UpperCamelCase_ ).manual_seed(UpperCamelCase_ ) UpperCamelCase__ :Optional[int] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def lowerCAmelCase__ ( self ): '''simple docstring''' super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def lowerCAmelCase__ ( self ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :str = '''cpu''' # ensure determinism for the device-dependent torch.Generator UpperCamelCase__ :Tuple = self.get_dummy_components() torch.manual_seed(0 ) UpperCamelCase__ :Dict = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder UpperCamelCase__ :Union[str, Any] = RobertaSeriesModelWithTransformation(UpperCamelCase_ ) UpperCamelCase__ :Union[str, Any] = text_encoder UpperCamelCase__ :Any = AltDiffusionPipeline(**UpperCamelCase_ ) UpperCamelCase__ :List[Any] = alt_pipe.to(UpperCamelCase_ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase_ ) UpperCamelCase__ :int = self.get_dummy_inputs(UpperCamelCase_ ) UpperCamelCase__ :Dict = '''A photo of an astronaut''' UpperCamelCase__ :Union[str, Any] = alt_pipe(**UpperCamelCase_ ) UpperCamelCase__ :Any = output.images UpperCamelCase__ :List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCamelCase__ :List[Any] = np.array( [0.5748162, 0.60447145, 0.48821217, 0.50100636, 0.5431185, 0.45763683, 0.49657696, 0.48132733, 0.47573093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :str = '''cpu''' # ensure determinism for the device-dependent torch.Generator UpperCamelCase__ :int = self.get_dummy_components() UpperCamelCase__ :Union[str, Any] = PNDMScheduler(skip_prk_steps=UpperCamelCase_ ) torch.manual_seed(0 ) UpperCamelCase__ :Dict = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder UpperCamelCase__ :str = RobertaSeriesModelWithTransformation(UpperCamelCase_ ) UpperCamelCase__ :Union[str, Any] = text_encoder UpperCamelCase__ :Union[str, Any] = AltDiffusionPipeline(**UpperCamelCase_ ) UpperCamelCase__ :Optional[int] = alt_pipe.to(UpperCamelCase_ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase_ ) UpperCamelCase__ :List[str] = self.get_dummy_inputs(UpperCamelCase_ ) UpperCamelCase__ :Optional[Any] = alt_pipe(**UpperCamelCase_ ) UpperCamelCase__ :str = output.images UpperCamelCase__ :int = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCamelCase__ :List[Any] = np.array( [0.51605093, 0.5707241, 0.47365507, 0.50578886, 0.5633877, 0.4642503, 0.5182081, 0.48763484, 0.49084237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class lowercase ( unittest.TestCase ): """simple docstring""" def lowerCAmelCase__ ( self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Optional[Any] = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , safety_checker=UpperCamelCase_ ) UpperCamelCase__ :str = alt_pipe.to(UpperCamelCase_ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase_ ) UpperCamelCase__ :int = '''A painting of a squirrel eating a burger''' UpperCamelCase__ :int = torch.manual_seed(0 ) UpperCamelCase__ :Optional[Any] = alt_pipe([prompt] , generator=UpperCamelCase_ , guidance_scale=6.0 , num_inference_steps=20 , output_type='''np''' ) UpperCamelCase__ :Union[str, Any] = output.images UpperCamelCase__ :Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCamelCase__ :Union[str, Any] = np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowerCAmelCase__ ( self ): '''simple docstring''' UpperCamelCase__ :Any = DDIMScheduler.from_pretrained('''BAAI/AltDiffusion''' , subfolder='''scheduler''' ) UpperCamelCase__ :List[str] = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , scheduler=UpperCamelCase_ , safety_checker=UpperCamelCase_ ) UpperCamelCase__ :Dict = alt_pipe.to(UpperCamelCase_ ) alt_pipe.set_progress_bar_config(disable=UpperCamelCase_ ) UpperCamelCase__ :List[Any] = '''A painting of a squirrel eating a burger''' UpperCamelCase__ :str = torch.manual_seed(0 ) UpperCamelCase__ :Optional[int] = alt_pipe([prompt] , generator=UpperCamelCase_ , num_inference_steps=2 , output_type='''numpy''' ) UpperCamelCase__ :Any = output.images UpperCamelCase__ :int = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCamelCase__ :int = np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
219
1
'''simple docstring''' from math import ceil def a__ ( a__ = 10_01 ): """simple docstring""" __SCREAMING_SNAKE_CASE = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): __SCREAMING_SNAKE_CASE = 2 * i + 1 __SCREAMING_SNAKE_CASE = 2 * i __SCREAMING_SNAKE_CASE = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: UpperCAmelCase : Optional[int] = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number')
267
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase : Union[str, Any] = {'configuration_sew': ['SEW_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SEWConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Union[str, Any] = [ 'SEW_PRETRAINED_MODEL_ARCHIVE_LIST', 'SEWForCTC', 'SEWForSequenceClassification', 'SEWModel', 'SEWPreTrainedModel', ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys UpperCAmelCase : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
267
1
import argparse import torch from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def UpperCamelCase_ ( A__ : List[str] , A__ : List[str] , A__ : List[Any] ): '''simple docstring''' if openai_config_file == "": lowerCAmelCase_ : int = OpenAIGPTConfig() else: lowerCAmelCase_ : Tuple = OpenAIGPTConfig.from_json_file(A__ ) lowerCAmelCase_ : Optional[Any] = OpenAIGPTModel(A__ ) # Load weights from numpy load_tf_weights_in_openai_gpt(A__ , A__ , A__ ) # Save pytorch-model lowerCAmelCase_ : List[Any] = pytorch_dump_folder_path + """/""" + WEIGHTS_NAME lowerCAmelCase_ : int = pytorch_dump_folder_path + """/""" + CONFIG_NAME print(f'Save PyTorch model to {pytorch_weights_dump_path}' ) torch.save(model.state_dict() , A__ ) print(f'Save configuration file to {pytorch_config_dump_path}' ) with open(A__ , """w""" , encoding="""utf-8""" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __A : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--openai_checkpoint_folder_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--openai_config_file", default="", type=str, help=( "An optional config json file corresponding to the pre-trained OpenAI model. \n" "This specifies the model architecture." ), ) __A : Any = parser.parse_args() convert_openai_checkpoint_to_pytorch( args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path )
352
'''simple docstring''' from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging __A : Dict = logging.get_logger(__name__) __A : int = { "snap-research/efficientformer-l1-300": ( "https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json" ), } class __snake_case ( _SCREAMING_SNAKE_CASE): """simple docstring""" lowercase = 'efficientformer' def __init__( self : Any , lowerCamelCase : List[int] = [3, 2, 6, 4] , lowerCamelCase : List[int] = [48, 96, 2_24, 4_48] , lowerCamelCase : List[bool] = [True, True, True, True] , lowerCamelCase : int = 4_48 , lowerCamelCase : int = 32 , lowerCamelCase : int = 4 , lowerCamelCase : int = 7 , lowerCamelCase : int = 5 , lowerCamelCase : int = 8 , lowerCamelCase : int = 4 , lowerCamelCase : float = 0.0 , lowerCamelCase : int = 16 , lowerCamelCase : int = 3 , lowerCamelCase : int = 3 , lowerCamelCase : int = 3 , lowerCamelCase : int = 2 , lowerCamelCase : int = 1 , lowerCamelCase : float = 0.0 , lowerCamelCase : int = 1 , lowerCamelCase : bool = True , lowerCamelCase : bool = True , lowerCamelCase : float = 1E-5 , lowerCamelCase : str = "gelu" , lowerCamelCase : float = 0.02 , lowerCamelCase : float = 1E-12 , lowerCamelCase : int = 2_24 , lowerCamelCase : float = 1E-05 , **lowerCamelCase : int , ) -> None: super().__init__(**lowerCamelCase ) lowerCAmelCase_ : List[str] = hidden_act lowerCAmelCase_ : List[Any] = hidden_dropout_prob lowerCAmelCase_ : Any = hidden_sizes lowerCAmelCase_ : List[Any] = num_hidden_layers lowerCAmelCase_ : Union[str, Any] = num_attention_heads lowerCAmelCase_ : Tuple = initializer_range lowerCAmelCase_ : Union[str, Any] = layer_norm_eps lowerCAmelCase_ : int = patch_size lowerCAmelCase_ : List[str] = num_channels lowerCAmelCase_ : Dict = depths lowerCAmelCase_ : int = mlp_expansion_ratio lowerCAmelCase_ : Optional[Any] = downsamples lowerCAmelCase_ : Union[str, Any] = dim lowerCAmelCase_ : Union[str, Any] = key_dim lowerCAmelCase_ : str = attention_ratio lowerCAmelCase_ : Tuple = resolution lowerCAmelCase_ : Optional[Any] = pool_size lowerCAmelCase_ : str = downsample_patch_size lowerCAmelCase_ : Dict = downsample_stride lowerCAmelCase_ : str = downsample_pad lowerCAmelCase_ : str = drop_path_rate lowerCAmelCase_ : List[Any] = num_metaad_blocks lowerCAmelCase_ : Tuple = distillation lowerCAmelCase_ : Optional[Any] = use_layer_scale lowerCAmelCase_ : Dict = layer_scale_init_value lowerCAmelCase_ : Optional[Any] = image_size lowerCAmelCase_ : Optional[Any] = batch_norm_eps
89
0
import numpy as np from sklearn.datasets import fetch_california_housing from sklearn.metrics import mean_absolute_error, mean_squared_error from sklearn.model_selection import train_test_split from xgboost import XGBRegressor def A_ ( A__ ) -> tuple: return (data["data"], data["target"]) def A_ ( A__ , A__ , A__ ) -> np.ndarray: a__ : int = XGBRegressor(verbosity=0 , random_state=42 ) xgb.fit(A__ , A__ ) # Predict target for test data a__ : Union[str, Any] = xgb.predict(A__ ) a__ : Optional[Any] = predictions.reshape(len(A__ ) , 1 ) return predictions def A_ ( ) -> None: a__ : List[str] = fetch_california_housing() a__ , a__ : int = data_handling(A__ ) a__ , a__ , a__ , a__ : Optional[int] = train_test_split( A__ , A__ , test_size=0.25 , random_state=1 ) a__ : Optional[Any] = xgboost(A__ , A__ , A__ ) # Error printing print(F'Mean Absolute Error : {mean_absolute_error(A__ , A__ )}' ) print(F'Mean Square Error : {mean_squared_error(A__ , A__ )}' ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
99
"""simple docstring""" import argparse import json import os import torch from transformers import LukeConfig, LukeModel, LukeTokenizer, RobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowerCamelCase_ (UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : int , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] ): # Load configuration defined in the metadata file with open(UpperCamelCase__ ) as metadata_file: _UpperCAmelCase : Dict = json.load(UpperCamelCase__ ) _UpperCAmelCase : List[Any] = LukeConfig(use_entity_aware_attention=UpperCamelCase__ , **metadata['''model_config'''] ) # Load in the weights from the checkpoint_path _UpperCAmelCase : List[Any] = torch.load(UpperCamelCase__ , map_location='''cpu''' ) # Load the entity vocab file _UpperCAmelCase : Optional[int] = load_entity_vocab(UpperCamelCase__ ) _UpperCAmelCase : Optional[int] = RobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks _UpperCAmelCase : int = AddedToken('''<ent>''' , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) _UpperCAmelCase : Optional[Any] = AddedToken('''<ent2>''' , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) 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(UpperCamelCase__ ) with open(os.path.join(UpperCamelCase__ , LukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) , '''w''' ) as f: json.dump(UpperCamelCase__ , UpperCamelCase__ ) _UpperCAmelCase : Any = LukeTokenizer.from_pretrained(UpperCamelCase__ ) # Initialize the embeddings of the special tokens _UpperCAmelCase : str = state_dict['''embeddings.word_embeddings.weight'''] _UpperCAmelCase : Dict = word_emb[tokenizer.convert_tokens_to_ids(['''@'''] )[0]].unsqueeze(0 ) _UpperCAmelCase : Union[str, Any] = word_emb[tokenizer.convert_tokens_to_ids(['''#'''] )[0]].unsqueeze(0 ) _UpperCAmelCase : Tuple = torch.cat([word_emb, ent_emb, enta_emb] ) # 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"]: _UpperCAmelCase : List[Any] = F'encoder.layer.{layer_index}.attention.self.' _UpperCAmelCase : Optional[Any] = state_dict[prefix + matrix_name] _UpperCAmelCase : Tuple = state_dict[prefix + matrix_name] _UpperCAmelCase : str = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks _UpperCAmelCase : Any = state_dict['''entity_embeddings.entity_embeddings.weight'''] _UpperCAmelCase : Dict = entity_emb[entity_vocab['''[MASK]''']] _UpperCAmelCase : Optional[int] = LukeModel(config=UpperCamelCase__ ).eval() _UpperCAmelCase , _UpperCAmelCase : int = model.load_state_dict(UpperCamelCase__ , strict=UpperCamelCase__ ) if not (len(UpperCamelCase__ ) == 1 and missing_keys[0] == "embeddings.position_ids"): raise ValueError(F'Missing keys {", ".join(UpperCamelCase__ )}. Expected only missing embeddings.position_ids' ) if not (all(key.startswith('''entity_predictions''' ) or key.startswith('''lm_head''' ) for key in unexpected_keys )): raise ValueError( '''Unexpected keys''' F' {", ".join([key for key in unexpected_keys if not (key.startswith("entity_predictions" ) or key.startswith("lm_head" ))] )}' ) # Check outputs _UpperCAmelCase : Optional[int] = LukeTokenizer.from_pretrained(UpperCamelCase__ , task='''entity_classification''' ) _UpperCAmelCase : List[str] = ( '''Top seed Ana Ivanovic said on Thursday she could hardly believe her luck as a fortuitous netcord helped the''' ''' new world number one avoid a humiliating second- round exit at Wimbledon .''' ) _UpperCAmelCase : Dict = (39, 42) _UpperCAmelCase : Any = tokenizer(UpperCamelCase__ , entity_spans=[span] , add_prefix_space=UpperCamelCase__ , return_tensors='''pt''' ) _UpperCAmelCase : List[Any] = model(**UpperCamelCase__ ) # Verify word hidden states if model_size == "large": _UpperCAmelCase : str = torch.Size((1, 42, 1024) ) _UpperCAmelCase : Union[str, Any] = torch.tensor( [[0.0133, 0.0865, 0.0095], [0.3093, -0.2576, -0.7418], [-0.1720, -0.2117, -0.2869]] ) else: # base _UpperCAmelCase : Optional[Any] = torch.Size((1, 42, 768) ) _UpperCAmelCase : str = torch.tensor([[0.0037, 0.1368, -0.0091], [0.1099, 0.3329, -0.1095], [0.0765, 0.5335, 0.1179]] ) 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] , UpperCamelCase__ , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": _UpperCAmelCase : int = torch.Size((1, 1, 1024) ) _UpperCAmelCase : str = torch.tensor([[0.0466, -0.0106, -0.0179]] ) else: # base _UpperCAmelCase : List[str] = torch.Size((1, 1, 768) ) _UpperCAmelCase : List[Any] = torch.tensor([[0.1457, 0.1044, 0.0174]] ) 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] , UpperCamelCase__ , atol=1E-4 ): raise ValueError # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(UpperCamelCase__ ) ) model.save_pretrained(UpperCamelCase__ ) def lowerCamelCase_ (UpperCamelCase__ : Union[str, Any] ): _UpperCAmelCase : Any = {} with open(UpperCamelCase__ , '''r''' , encoding='''utf-8''' ) as f: for index, line in enumerate(UpperCamelCase__ ): _UpperCAmelCase , _UpperCAmelCase : Any = line.rstrip().split('''\t''' ) _UpperCAmelCase : Tuple = index return entity_vocab if __name__ == "__main__": _lowerCAmelCase :List[Any] = 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 :Any = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
263
0
from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_outputs import ( BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import logging from .configuration_regnet import RegNetConfig lowercase : Optional[int] = logging.get_logger(__name__) # General docstring lowercase : Union[str, Any] = """RegNetConfig""" # Base docstring lowercase : List[Any] = """facebook/regnet-y-040""" lowercase : Union[str, Any] = [1, 1088, 7, 7] # Image classification docstring lowercase : int = """facebook/regnet-y-040""" lowercase : List[str] = """tabby, tabby cat""" lowercase : Dict = [ """facebook/regnet-y-040""", # See all regnet models at https://huggingface.co/models?filter=regnet ] class __snake_case ( nn.Module ): def __init__( self ,snake_case ,snake_case ,snake_case = 3 ,snake_case = 1 ,snake_case = 1 ,snake_case = "relu" ,): '''simple docstring''' super().__init__() lowercase : List[Any] = nn.Convad( snake_case ,snake_case ,kernel_size=snake_case ,stride=snake_case ,padding=kernel_size // 2 ,groups=snake_case ,bias=snake_case ,) lowercase : List[str] = nn.BatchNormad(snake_case ) lowercase : Union[str, Any] = ACTaFN[activation] if activation is not None else nn.Identity() def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : str = self.convolution(snake_case ) lowercase : List[Any] = self.normalization(snake_case ) lowercase : str = self.activation(snake_case ) return hidden_state class __snake_case ( nn.Module ): def __init__( self ,snake_case ): '''simple docstring''' super().__init__() lowercase : str = RegNetConvLayer( config.num_channels ,config.embedding_size ,kernel_size=3 ,stride=2 ,activation=config.hidden_act ) lowercase : Dict = config.num_channels def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : int = pixel_values.shape[1] if 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.""" ) lowercase : int = self.embedder(snake_case ) return hidden_state class __snake_case ( nn.Module ): def __init__( self ,snake_case ,snake_case ,snake_case = 2 ): '''simple docstring''' super().__init__() lowercase : List[str] = nn.Convad(snake_case ,snake_case ,kernel_size=1 ,stride=snake_case ,bias=snake_case ) lowercase : Dict = nn.BatchNormad(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : int = self.convolution(snake_case ) lowercase : Dict = self.normalization(snake_case ) return hidden_state class __snake_case ( nn.Module ): def __init__( self ,snake_case ,snake_case ): '''simple docstring''' super().__init__() lowercase : int = nn.AdaptiveAvgPoolad((1, 1) ) lowercase : int = nn.Sequential( nn.Convad(snake_case ,snake_case ,kernel_size=1 ) ,nn.ReLU() ,nn.Convad(snake_case ,snake_case ,kernel_size=1 ) ,nn.Sigmoid() ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[str] = self.pooler(snake_case ) lowercase : List[str] = self.attention(snake_case ) lowercase : Any = hidden_state * attention return hidden_state class __snake_case ( nn.Module ): def __init__( self ,snake_case ,snake_case ,snake_case ,snake_case = 1 ): '''simple docstring''' super().__init__() lowercase : str = in_channels != out_channels or stride != 1 lowercase : Dict = max(1 ,out_channels // config.groups_width ) lowercase : Union[str, Any] = ( RegNetShortCut(snake_case ,snake_case ,stride=snake_case ) if should_apply_shortcut else nn.Identity() ) lowercase : str = nn.Sequential( RegNetConvLayer(snake_case ,snake_case ,kernel_size=1 ,activation=config.hidden_act ) ,RegNetConvLayer(snake_case ,snake_case ,stride=snake_case ,groups=snake_case ,activation=config.hidden_act ) ,RegNetConvLayer(snake_case ,snake_case ,kernel_size=1 ,activation=snake_case ) ,) lowercase : Tuple = ACTaFN[config.hidden_act] def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : List[Any] = hidden_state lowercase : Dict = self.layer(snake_case ) lowercase : int = self.shortcut(snake_case ) hidden_state += residual lowercase : List[str] = self.activation(snake_case ) return hidden_state class __snake_case ( nn.Module ): def __init__( self ,snake_case ,snake_case ,snake_case ,snake_case = 1 ): '''simple docstring''' super().__init__() lowercase : List[Any] = in_channels != out_channels or stride != 1 lowercase : List[str] = max(1 ,out_channels // config.groups_width ) lowercase : Union[str, Any] = ( RegNetShortCut(snake_case ,snake_case ,stride=snake_case ) if should_apply_shortcut else nn.Identity() ) lowercase : Dict = nn.Sequential( RegNetConvLayer(snake_case ,snake_case ,kernel_size=1 ,activation=config.hidden_act ) ,RegNetConvLayer(snake_case ,snake_case ,stride=snake_case ,groups=snake_case ,activation=config.hidden_act ) ,RegNetSELayer(snake_case ,reduced_channels=int(round(in_channels / 4 ) ) ) ,RegNetConvLayer(snake_case ,snake_case ,kernel_size=1 ,activation=snake_case ) ,) lowercase : int = ACTaFN[config.hidden_act] def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = hidden_state lowercase : Optional[int] = self.layer(snake_case ) lowercase : Tuple = self.shortcut(snake_case ) hidden_state += residual lowercase : Tuple = self.activation(snake_case ) return hidden_state class __snake_case ( nn.Module ): def __init__( self ,snake_case ,snake_case ,snake_case ,snake_case = 2 ,snake_case = 2 ,): '''simple docstring''' super().__init__() lowercase : int = RegNetXLayer if config.layer_type == """x""" else RegNetYLayer lowercase : Optional[int] = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer( snake_case ,snake_case ,snake_case ,stride=snake_case ,) ,*[layer(snake_case ,snake_case ,snake_case ) for _ in range(depth - 1 )] ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Optional[Any] = self.layers(snake_case ) return hidden_state class __snake_case ( nn.Module ): def __init__( self ,snake_case ): '''simple docstring''' super().__init__() lowercase : Tuple = nn.ModuleList([] ) # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( RegNetStage( snake_case ,config.embedding_size ,config.hidden_sizes[0] ,stride=2 if config.downsample_in_first_stage else 1 ,depth=config.depths[0] ,) ) lowercase : List[str] = zip(config.hidden_sizes ,config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(snake_case ,config.depths[1:] ): self.stages.append(RegNetStage(snake_case ,snake_case ,snake_case ,depth=snake_case ) ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = False ,snake_case = True ): '''simple docstring''' lowercase : Dict = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: lowercase : Any = hidden_states + (hidden_state,) lowercase : Tuple = stage_module(snake_case ) if output_hidden_states: lowercase : int = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=snake_case ,hidden_states=snake_case ) class __snake_case ( lowerCAmelCase ): _a : List[str]= RegNetConfig _a : Optional[int]= "regnet" _a : Optional[int]= "pixel_values" _a : Dict= True def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if isinstance(snake_case ,nn.Convad ): nn.init.kaiming_normal_(module.weight ,mode="""fan_out""" ,nonlinearity="""relu""" ) elif isinstance(snake_case ,(nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight ,1 ) nn.init.constant_(module.bias ,0 ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=False ): '''simple docstring''' if isinstance(snake_case ,snake_case ): lowercase : Tuple = value lowercase : Union[str, Any] = R""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowercase : Dict = R""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__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 [`~file_utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( "The bare RegNet model outputting raw features without any specific head on top." , lowerCAmelCase , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet class __snake_case ( lowerCAmelCase ): def __init__( self ,snake_case ): '''simple docstring''' super().__init__(snake_case ) lowercase : List[str] = config lowercase : List[str] = RegNetEmbeddings(snake_case ) lowercase : Optional[int] = RegNetEncoder(snake_case ) lowercase : int = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(snake_case ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC ,output_type=snake_case ,config_class=_CONFIG_FOR_DOC ,modality="""vision""" ,expected_output=_EXPECTED_OUTPUT_SHAPE ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case = None ,snake_case = None ): '''simple docstring''' lowercase : Tuple = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase : str = return_dict if return_dict is not None else self.config.use_return_dict lowercase : Tuple = self.embedder(snake_case ) lowercase : Dict = self.encoder( snake_case ,output_hidden_states=snake_case ,return_dict=snake_case ) lowercase : Union[str, Any] = encoder_outputs[0] lowercase : Union[str, Any] = self.pooler(snake_case ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=snake_case ,pooler_output=snake_case ,hidden_states=encoder_outputs.hidden_states ,) @add_start_docstrings( "\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , lowerCAmelCase , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet class __snake_case ( lowerCAmelCase ): def __init__( self ,snake_case ): '''simple docstring''' super().__init__(snake_case ) lowercase : int = config.num_labels lowercase : Optional[int] = RegNetModel(snake_case ) # classification head lowercase : str = nn.Sequential( nn.Flatten() ,nn.Linear(config.hidden_sizes[-1] ,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(snake_case ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT ,output_type=snake_case ,config_class=_CONFIG_FOR_DOC ,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case = None ,snake_case = None ,snake_case = None ,snake_case = None ,): '''simple docstring''' lowercase : int = return_dict if return_dict is not None else self.config.use_return_dict lowercase : Optional[int] = self.regnet(snake_case ,output_hidden_states=snake_case ,return_dict=snake_case ) lowercase : Optional[Any] = outputs.pooler_output if return_dict else outputs[1] lowercase : List[Any] = self.classifier(snake_case ) lowercase : List[Any] = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: lowercase : str = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): lowercase : Optional[Any] = """single_label_classification""" else: lowercase : Optional[Any] = """multi_label_classification""" if self.config.problem_type == "regression": lowercase : Dict = MSELoss() if self.num_labels == 1: lowercase : List[Any] = loss_fct(logits.squeeze() ,labels.squeeze() ) else: lowercase : int = loss_fct(snake_case ,snake_case ) elif self.config.problem_type == "single_label_classification": lowercase : List[Any] = CrossEntropyLoss() lowercase : Tuple = loss_fct(logits.view(-1 ,self.num_labels ) ,labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": lowercase : int = BCEWithLogitsLoss() lowercase : List[Any] = loss_fct(snake_case ,snake_case ) if not return_dict: lowercase : Dict = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=snake_case ,logits=snake_case ,hidden_states=outputs.hidden_states )
285
from __future__ import annotations import numpy as np def _snake_case( SCREAMING_SNAKE_CASE__ ) -> tuple[np.ndarray, np.ndarray]: lowercase , lowercase : Dict = np.shape(SCREAMING_SNAKE_CASE__ ) if rows != columns: lowercase : str = ( """'table' has to be of square shaped array but got a """ f"{rows}x{columns} array:\n{table}" ) raise ValueError(SCREAMING_SNAKE_CASE__ ) lowercase : Any = np.zeros((rows, columns) ) lowercase : int = np.zeros((rows, columns) ) for i in range(SCREAMING_SNAKE_CASE__ ): for j in range(SCREAMING_SNAKE_CASE__ ): lowercase : Optional[int] = sum(lower[i][k] * upper[k][j] for k in range(SCREAMING_SNAKE_CASE__ ) ) if upper[j][j] == 0: raise ArithmeticError("""No LU decomposition exists""" ) lowercase : str = (table[i][j] - total) / upper[j][j] lowercase : Optional[Any] = 1 for j in range(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : Any = sum(lower[i][k] * upper[k][j] for k in range(SCREAMING_SNAKE_CASE__ ) ) lowercase : Tuple = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
285
1
"""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 : Dict = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ): lowercase__ = ["input_features", "attention_mask"] def __init__( self : List[Any] , lowerCAmelCase_ : int=8_0 , lowerCAmelCase_ : Tuple=1_6_0_0_0 , lowerCAmelCase_ : List[str]=8_0 , lowerCAmelCase_ : str=0.0 , lowerCAmelCase_ : Optional[Any]=True , lowerCAmelCase_ : Optional[Any]=True , lowerCAmelCase_ : List[str]=True , **lowerCAmelCase_ : List[str] , ): """simple docstring""" super().__init__(feature_size=lowerCAmelCase_ , sampling_rate=lowerCAmelCase_ , padding_value=lowerCAmelCase_ , **lowerCAmelCase_) lowercase_ = num_mel_bins lowercase_ = do_ceptral_normalize lowercase_ = normalize_means lowercase_ = normalize_vars lowercase_ = True def _UpperCAmelCase ( self : Union[str, Any] , lowerCAmelCase_ : np.ndarray , ): """simple docstring""" lowercase_ = waveform * (2**1_5) # Kaldi compliance: 16-bit signed integers lowercase_ = torch.from_numpy(lowerCAmelCase_).unsqueeze(0) lowercase_ = ta_kaldi.fbank(lowerCAmelCase_ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate) return features.numpy() @staticmethod def _UpperCAmelCase ( lowerCAmelCase_ : np.ndarray , lowerCAmelCase_ : int , lowerCAmelCase_ : Optional[bool] = True , lowerCAmelCase_ : Optional[bool] = True , lowerCAmelCase_ : float = 0.0 , ): """simple docstring""" if normalize_means: lowercase_ = x[:input_length].mean(axis=0) lowercase_ = np.subtract(lowerCAmelCase_ , lowerCAmelCase_) if normalize_vars: lowercase_ = x[:input_length].std(axis=0) lowercase_ = np.divide(lowerCAmelCase_ , lowerCAmelCase_) if input_length < x.shape[0]: lowercase_ = padding_value # make sure array is in float32 lowercase_ = x.astype(np.floataa) return x def _UpperCAmelCase ( self : int , lowerCAmelCase_ : List[np.ndarray] , lowerCAmelCase_ : Optional[np.ndarray] = None): """simple docstring""" lowercase_ = 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 : str , 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_ : Tuple , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''') else: logger.warning( """It is strongly recommended to pass the `sampling_rate` argument to this function. """ """Failing to do so can result in silent errors that might be hard to debug.""") lowercase_ = 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}''') lowercase_ = is_batched_numpy or ( isinstance(lowerCAmelCase_ , (list, tuple)) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list))) ) if is_batched: lowercase_ = [np.asarray(lowerCAmelCase_ , dtype=np.floataa) for speech in raw_speech] elif not is_batched and not isinstance(lowerCAmelCase_ , np.ndarray): lowercase_ = np.asarray(lowerCAmelCase_ , dtype=np.floataa) elif isinstance(lowerCAmelCase_ , np.ndarray) and raw_speech.dtype is np.dtype(np.floataa): lowercase_ = raw_speech.astype(np.floataa) # always return batch if not is_batched: lowercase_ = [raw_speech] # extract fbank features lowercase_ = [self._extract_fbank_features(lowerCAmelCase_) for waveform in raw_speech] # convert into correct format for padding lowercase_ = BatchFeature({"""input_features""": features}) lowercase_ = 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 lowercase_ = padded_inputs.get("""input_features""") if isinstance(input_features[0] , lowerCAmelCase_): lowercase_ = [np.asarray(lowerCAmelCase_ , dtype=np.floataa) for feature in input_features] lowercase_ = padded_inputs.get("""attention_mask""") if attention_mask is not None: lowercase_ = [np.asarray(lowerCAmelCase_ , dtype=np.intaa) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: lowercase_ = ( np.array(lowerCAmelCase_ , dtype=np.intaa) if self._get_padding_strategies(lowerCAmelCase_ , max_length=lowerCAmelCase_) is not PaddingStrategy.DO_NOT_PAD else None ) lowercase_ = self.normalize( padded_inputs["""input_features"""] , attention_mask=lowerCAmelCase_) if return_tensors is not None: lowercase_ = padded_inputs.convert_to_tensors(lowerCAmelCase_) return padded_inputs
136
"""simple docstring""" import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class SCREAMING_SNAKE_CASE__ ( __UpperCAmelCase ): lowercase__ = "Speech2TextFeatureExtractor" lowercase__ = "Speech2TextTokenizer" def __init__( self : Optional[Any] , lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Tuple): """simple docstring""" super().__init__(lowerCAmelCase_ , lowerCAmelCase_) lowercase_ = self.feature_extractor lowercase_ = False def __call__( self : Dict , *lowerCAmelCase_ : List[str] , **lowerCAmelCase_ : List[str]): """simple docstring""" if self._in_target_context_manager: return self.current_processor(*lowerCAmelCase_ , **lowerCAmelCase_) if "raw_speech" in kwargs: warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.""") lowercase_ = kwargs.pop("""raw_speech""") else: lowercase_ = kwargs.pop("""audio""" , lowerCAmelCase_) lowercase_ = kwargs.pop("""sampling_rate""" , lowerCAmelCase_) lowercase_ = kwargs.pop("""text""" , lowerCAmelCase_) if len(lowerCAmelCase_) > 0: lowercase_ = args[0] lowercase_ = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""") if audio is not None: lowercase_ = self.feature_extractor(lowerCAmelCase_ , *lowerCAmelCase_ , sampling_rate=lowerCAmelCase_ , **lowerCAmelCase_) if text is not None: lowercase_ = self.tokenizer(lowerCAmelCase_ , **lowerCAmelCase_) if text is None: return inputs elif audio is None: return encodings else: lowercase_ = encodings["""input_ids"""] return inputs def _UpperCAmelCase ( self : List[str] , *lowerCAmelCase_ : Dict , **lowerCAmelCase_ : Optional[Any]): """simple docstring""" return self.tokenizer.batch_decode(*lowerCAmelCase_ , **lowerCAmelCase_) def _UpperCAmelCase ( self : Optional[int] , *lowerCAmelCase_ : int , **lowerCAmelCase_ : str): """simple docstring""" return self.tokenizer.decode(*lowerCAmelCase_ , **lowerCAmelCase_) @contextmanager def _UpperCAmelCase ( self : Optional[int]): """simple docstring""" warnings.warn( """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """ """labels by using the argument `text` of the regular `__call__` method (either in the same call as """ """your audio inputs, or in a separate call.""") lowercase_ = True lowercase_ = self.tokenizer yield lowercase_ = self.feature_extractor lowercase_ = False
136
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase_ = { '''configuration_biogpt''': ['''BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BioGptConfig'''], '''tokenization_biogpt''': ['''BioGptTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ '''BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BioGptForCausalLM''', '''BioGptForTokenClassification''', '''BioGptForSequenceClassification''', '''BioGptModel''', '''BioGptPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
59
import os def lowerCamelCase_ ( _a : str = "input.txt" ): '''simple docstring''' with open(os.path.join(os.path.dirname(_a ) , _a ) ) as input_file: UpperCAmelCase_ : Dict = [ [int(_a ) for element in line.split(""",""" )] for line in input_file.readlines() ] UpperCAmelCase_ : Any = len(_a ) UpperCAmelCase_ : Tuple = len(matrix[0] ) UpperCAmelCase_ : Optional[int] = [[-1 for _ in range(_a )] for _ in range(_a )] for i in range(_a ): UpperCAmelCase_ : Optional[Any] = matrix[i][0] for j in range(1 , _a ): for i in range(_a ): UpperCAmelCase_ : str = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 , _a ): UpperCAmelCase_ : Optional[int] = min( minimal_path_sums[i][j] , minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2 , -1 , -1 ): UpperCAmelCase_ : Union[str, Any] = 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() = }")
59
1
'''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 UpperCAmelCase ( unittest.TestCase ): @slow def lowercase__ ( self : int ) -> Tuple: _lowerCAmelCase = TFCamembertModel.from_pretrained("""jplu/tf-camembert-base""" ) _lowerCAmelCase = tf.convert_to_tensor( [[5, 1_21, 11, 6_60, 16, 7_30, 2_55_43, 1_10, 83, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" _lowerCAmelCase = model(__snake_case )["""last_hidden_state"""] _lowerCAmelCase = tf.TensorShape((1, 10, 7_68) ) self.assertEqual(output.shape , __snake_case ) # compare the actual values for a slice. _lowerCAmelCase = tf.convert_to_tensor( [[[-0.02_54, 0.02_35, 0.10_27], [0.06_06, -0.18_11, -0.04_18], [-0.15_61, -0.11_27, 0.26_87]]] , 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 ) )
70
'''simple docstring''' import inspect import unittest from transformers import SegformerConfig, is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_MAPPING, SegformerForImageClassification, SegformerForSemanticSegmentation, SegformerModel, ) from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import SegformerImageProcessor class UpperCAmelCase ( snake_case_ ): def lowercase__ ( self : List[Any] ) -> Union[str, Any]: _lowerCAmelCase = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__snake_case , """hidden_sizes""" ) ) self.parent.assertTrue(hasattr(__snake_case , """num_attention_heads""" ) ) self.parent.assertTrue(hasattr(__snake_case , """num_encoder_blocks""" ) ) class UpperCAmelCase : def __init__( self : Optional[int] , __snake_case : str , __snake_case : Dict=13 , __snake_case : str=64 , __snake_case : Dict=3 , __snake_case : Dict=4 , __snake_case : Tuple=[2, 2, 2, 2] , __snake_case : int=[8, 4, 2, 1] , __snake_case : List[str]=[16, 32, 64, 1_28] , __snake_case : Optional[Any]=[1, 4, 8, 16] , __snake_case : Dict=[1, 2, 4, 8] , __snake_case : Optional[Any]=True , __snake_case : List[str]=True , __snake_case : int="gelu" , __snake_case : Optional[Any]=0.1 , __snake_case : Any=0.1 , __snake_case : Tuple=0.02 , __snake_case : Union[str, Any]=3 , __snake_case : Tuple=None , ) -> List[str]: _lowerCAmelCase = parent _lowerCAmelCase = batch_size _lowerCAmelCase = image_size _lowerCAmelCase = num_channels _lowerCAmelCase = num_encoder_blocks _lowerCAmelCase = sr_ratios _lowerCAmelCase = depths _lowerCAmelCase = hidden_sizes _lowerCAmelCase = downsampling_rates _lowerCAmelCase = num_attention_heads _lowerCAmelCase = is_training _lowerCAmelCase = use_labels _lowerCAmelCase = hidden_act _lowerCAmelCase = hidden_dropout_prob _lowerCAmelCase = attention_probs_dropout_prob _lowerCAmelCase = initializer_range _lowerCAmelCase = num_labels _lowerCAmelCase = scope def lowercase__ ( self : int ) -> Union[str, Any]: _lowerCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _lowerCAmelCase = None if self.use_labels: _lowerCAmelCase = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _lowerCAmelCase = self.get_config() return config, pixel_values, labels def lowercase__ ( self : List[Any] ) -> List[str]: return SegformerConfig( image_size=self.image_size , num_channels=self.num_channels , num_encoder_blocks=self.num_encoder_blocks , depths=self.depths , hidden_sizes=self.hidden_sizes , num_attention_heads=self.num_attention_heads , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def lowercase__ ( self : Tuple , __snake_case : Optional[Any] , __snake_case : Union[str, Any] , __snake_case : Optional[int] ) -> Tuple: _lowerCAmelCase = SegformerModel(config=__snake_case ) model.to(__snake_case ) model.eval() _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = _lowerCAmelCase = self.image_size // (self.downsampling_rates[-1] * 2) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) ) def lowercase__ ( self : List[str] , __snake_case : List[Any] , __snake_case : Optional[Any] , __snake_case : Optional[int] ) -> List[str]: _lowerCAmelCase = self.num_labels _lowerCAmelCase = SegformerForSemanticSegmentation(__snake_case ) model.to(__snake_case ) model.eval() _lowerCAmelCase = model(__snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) _lowerCAmelCase = model(__snake_case , labels=__snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) ) self.parent.assertGreater(result.loss , 0.0 ) def lowercase__ ( self : str , __snake_case : Union[str, Any] , __snake_case : Optional[int] , __snake_case : Dict ) -> List[str]: _lowerCAmelCase = 1 _lowerCAmelCase = SegformerForSemanticSegmentation(config=__snake_case ) model.to(__snake_case ) model.eval() _lowerCAmelCase = torch.randint(0 , 1 , (self.batch_size, self.image_size, self.image_size) ).to(__snake_case ) _lowerCAmelCase = model(__snake_case , labels=__snake_case ) self.parent.assertGreater(result.loss , 0.0 ) def lowercase__ ( self : Optional[int] ) -> int: _lowerCAmelCase = self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = config_and_inputs _lowerCAmelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase ( snake_case_ , snake_case_ , unittest.TestCase ): _lowercase: Any = ( ( SegformerModel, SegformerForSemanticSegmentation, SegformerForImageClassification, ) if is_torch_available() else () ) _lowercase: Tuple = ( { '''feature-extraction''': SegformerModel, '''image-classification''': SegformerForImageClassification, '''image-segmentation''': SegformerForSemanticSegmentation, } if is_torch_available() else {} ) _lowercase: Tuple = True _lowercase: Union[str, Any] = False _lowercase: Dict = False _lowercase: Optional[Any] = False def lowercase__ ( self : Tuple ) -> Any: _lowerCAmelCase = SegformerModelTester(self ) _lowerCAmelCase = SegformerConfigTester(self , config_class=__snake_case ) def lowercase__ ( self : Optional[Any] ) -> Dict: self.config_tester.run_common_tests() def lowercase__ ( self : int ) -> Union[str, Any]: _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__snake_case ) def lowercase__ ( self : Dict ) -> int: _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_binary_image_segmentation(*__snake_case ) def lowercase__ ( self : Dict ) -> Dict: _lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_segmentation(*__snake_case ) @unittest.skip("""SegFormer does not use inputs_embeds""" ) def lowercase__ ( self : int ) -> Union[str, Any]: pass @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods""" ) def lowercase__ ( self : Optional[int] ) -> int: pass def lowercase__ ( self : Union[str, Any] ) -> Optional[Any]: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase = model_class(__snake_case ) _lowerCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCAmelCase = [*signature.parameters.keys()] _lowerCAmelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __snake_case ) def lowercase__ ( self : Tuple ) -> Tuple: _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase = True for model_class in self.all_model_classes: _lowerCAmelCase = True _lowerCAmelCase = False _lowerCAmelCase = True _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) _lowerCAmelCase = outputs.attentions _lowerCAmelCase = sum(self.model_tester.depths ) self.assertEqual(len(__snake_case ) , __snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _lowerCAmelCase = True _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) _lowerCAmelCase = outputs.attentions self.assertEqual(len(__snake_case ) , __snake_case ) # verify the first attentions (first block, first layer) _lowerCAmelCase = (self.model_tester.image_size // 4) ** 2 _lowerCAmelCase = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) # verify the last attentions (last block, last layer) _lowerCAmelCase = (self.model_tester.image_size // 32) ** 2 _lowerCAmelCase = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2 self.assertListEqual( list(attentions[-1].shape[-3:] ) , [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len] , ) _lowerCAmelCase = len(__snake_case ) # Check attention is always last and order is fine _lowerCAmelCase = True _lowerCAmelCase = True _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) self.assertEqual(out_len + 1 , len(__snake_case ) ) _lowerCAmelCase = outputs.attentions self.assertEqual(len(__snake_case ) , __snake_case ) # verify the first attentions (first block, first layer) _lowerCAmelCase = (self.model_tester.image_size // 4) ** 2 _lowerCAmelCase = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2 self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len] , ) def lowercase__ ( self : int ) -> List[str]: def check_hidden_states_output(__snake_case : str , __snake_case : Tuple , __snake_case : Optional[int] ): _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.eval() with torch.no_grad(): _lowerCAmelCase = model(**self._prepare_for_class(__snake_case , __snake_case ) ) _lowerCAmelCase = outputs.hidden_states _lowerCAmelCase = self.model_tester.num_encoder_blocks self.assertEqual(len(__snake_case ) , __snake_case ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.hidden_sizes[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase = True check_hidden_states_output(__snake_case , __snake_case , __snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _lowerCAmelCase = True check_hidden_states_output(__snake_case , __snake_case , __snake_case ) def lowercase__ ( self : Optional[Any] ) -> Any: if not self.model_tester.is_training: return _lowerCAmelCase , _lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase = True for model_class in self.all_model_classes: if model_class in get_values(__snake_case ): continue _lowerCAmelCase = model_class(__snake_case ) model.to(__snake_case ) model.train() _lowerCAmelCase = self._prepare_for_class(__snake_case , __snake_case , return_labels=__snake_case ) _lowerCAmelCase = model(**__snake_case ).loss loss.backward() @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def lowercase__ ( self : Tuple ) -> Dict: pass @slow def lowercase__ ( self : str ) -> Optional[int]: for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase = SegformerModel.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) def UpperCamelCase__ ( ): """simple docstring""" _lowerCAmelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch class UpperCAmelCase ( unittest.TestCase ): @slow def lowercase__ ( self : Union[str, Any] ) -> Any: # only resize + normalize _lowerCAmelCase = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=__snake_case , align=__snake_case , do_random_crop=__snake_case ) _lowerCAmelCase = SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512""" ).to( __snake_case ) _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=__snake_case , return_tensors="""pt""" ) _lowerCAmelCase = encoded_inputs.pixel_values.to(__snake_case ) with torch.no_grad(): _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , __snake_case ) _lowerCAmelCase = torch.tensor( [ [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]], [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]], [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]], ] ).to(__snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , __snake_case , atol=1E-4 ) ) @slow def lowercase__ ( self : Optional[Any] ) -> Any: # only resize + normalize _lowerCAmelCase = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=__snake_case , align=__snake_case , do_random_crop=__snake_case ) _lowerCAmelCase = SegformerForSemanticSegmentation.from_pretrained( """nvidia/segformer-b1-finetuned-cityscapes-1024-1024""" ).to(__snake_case ) _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=__snake_case , return_tensors="""pt""" ) _lowerCAmelCase = encoded_inputs.pixel_values.to(__snake_case ) with torch.no_grad(): _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = torch.Size((1, model.config.num_labels, 1_28, 1_28) ) self.assertEqual(outputs.logits.shape , __snake_case ) _lowerCAmelCase = torch.tensor( [ [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]], [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]], [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]], ] ).to(__snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] , __snake_case , atol=1E-1 ) ) @slow def lowercase__ ( self : Any ) -> str: # only resize + normalize _lowerCAmelCase = SegformerImageProcessor( image_scale=(5_12, 5_12) , keep_ratio=__snake_case , align=__snake_case , do_random_crop=__snake_case ) _lowerCAmelCase = SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512""" ).to( __snake_case ) _lowerCAmelCase = prepare_img() _lowerCAmelCase = image_processor(images=__snake_case , return_tensors="""pt""" ) _lowerCAmelCase = encoded_inputs.pixel_values.to(__snake_case ) with torch.no_grad(): _lowerCAmelCase = model(__snake_case ) _lowerCAmelCase = outputs.logits.detach().cpu() _lowerCAmelCase = image_processor.post_process_semantic_segmentation(outputs=__snake_case , target_sizes=[(5_00, 3_00)] ) _lowerCAmelCase = torch.Size((5_00, 3_00) ) self.assertEqual(segmentation[0].shape , __snake_case ) _lowerCAmelCase = image_processor.post_process_semantic_segmentation(outputs=__snake_case ) _lowerCAmelCase = torch.Size((1_28, 1_28) ) self.assertEqual(segmentation[0].shape , __snake_case )
70
1
'''simple docstring''' A : Optional[Any] = 256 # Modulus to hash a string A : Tuple = 1000003 def lowerCAmelCase__ ( lowerCamelCase : str ,lowerCamelCase : str ): _A : Dict = len(lowerCamelCase ) _A : Any = len(lowerCamelCase ) if p_len > t_len: return False _A : Union[str, Any] = 0 _A : Any = 0 _A : List[str] = 1 # Calculating the hash of pattern and substring of text for i in range(lowerCamelCase ): _A : Any = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus _A : List[str] = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue _A : Optional[Any] = (modulus_power * alphabet_size) % modulus for i in range(0 ,t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash _A : Any = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def lowerCAmelCase__ ( ): _A : Optional[Any] = '''abc1abc12''' _A : List[str] = '''alskfjaldsabc1abc1abc12k23adsfabcabc''' _A : Tuple = '''alskfjaldsk23adsfabcabc''' assert rabin_karp(lowerCamelCase ,lowerCamelCase ) and not rabin_karp(lowerCamelCase ,lowerCamelCase ) # Test 2) _A : int = '''ABABX''' _A : Dict = '''ABABZABABYABABX''' assert rabin_karp(lowerCamelCase ,lowerCamelCase ) # Test 3) _A : int = '''AAAB''' _A : List[Any] = '''ABAAAAAB''' assert rabin_karp(lowerCamelCase ,lowerCamelCase ) # Test 4) _A : Optional[int] = '''abcdabcy''' _A : List[str] = '''abcxabcdabxabcdabcdabcy''' assert rabin_karp(lowerCamelCase ,lowerCamelCase ) # Test 5) _A : int = '''Lü''' _A : int = '''Lüsai''' assert rabin_karp(lowerCamelCase ,lowerCamelCase ) _A : str = '''Lue''' assert not rabin_karp(lowerCamelCase ,lowerCamelCase ) print('Success.' ) if __name__ == "__main__": test_rabin_karp()
361
'''simple docstring''' A : Dict = '''Alexander Joslin''' import operator as op from .stack import Stack def lowerCAmelCase__ ( lowerCamelCase : str ): _A : Dict = {'*': op.mul, '/': op.truediv, '+': op.add, '-': op.sub} _A : Stack[int] = Stack() _A : Stack[str] = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(lowerCamelCase ) ) elif i in operators: # RULE 2 operator_stack.push(lowerCamelCase ) elif i == ")": # RULE 4 _A : List[str] = operator_stack.peek() operator_stack.pop() _A : Dict = operand_stack.peek() operand_stack.pop() _A : Optional[int] = operand_stack.peek() operand_stack.pop() _A : Dict = operators[opr](lowerCamelCase ,lowerCamelCase ) operand_stack.push(lowerCamelCase ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": A : Dict = '''(5 + ((4 * 2) * (2 + 3)))''' # answer = 45 print(f"""{equation} = {dijkstras_two_stack_algorithm(equation)}""")
227
0
def SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase, _UpperCAmelCase ) -> List[Any]: '''simple docstring''' while second != 0: lowerCAmelCase : Optional[Any] = first & second first ^= second lowerCAmelCase : List[Any] = c << 1 return first if __name__ == "__main__": import doctest doctest.testmod() __A : Any = int(input('''Enter the first number: ''').strip()) __A : Optional[Any] = int(input('''Enter the second number: ''').strip()) print(F'{add(first, second) = }')
138
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _A = { "configuration_distilbert": [ "DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DistilBertConfig", "DistilBertOnnxConfig", ], "tokenization_distilbert": ["DistilBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = ["DistilBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ "DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "DistilBertForMaskedLM", "DistilBertForMultipleChoice", "DistilBertForQuestionAnswering", "DistilBertForSequenceClassification", "DistilBertForTokenClassification", "DistilBertModel", "DistilBertPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ "TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDistilBertForMaskedLM", "TFDistilBertForMultipleChoice", "TFDistilBertForQuestionAnswering", "TFDistilBertForSequenceClassification", "TFDistilBertForTokenClassification", "TFDistilBertMainLayer", "TFDistilBertModel", "TFDistilBertPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ "FlaxDistilBertForMaskedLM", "FlaxDistilBertForMultipleChoice", "FlaxDistilBertForQuestionAnswering", "FlaxDistilBertForSequenceClassification", "FlaxDistilBertForTokenClassification", "FlaxDistilBertModel", "FlaxDistilBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_distilbert import ( DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DistilBertConfig, DistilBertOnnxConfig, ) from .tokenization_distilbert import DistilBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_distilbert_fast import DistilBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_distilbert import ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, DistilBertPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_distilbert import ( TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDistilBertForMaskedLM, TFDistilBertForMultipleChoice, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertMainLayer, TFDistilBertModel, TFDistilBertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_distilbert import ( FlaxDistilBertForMaskedLM, FlaxDistilBertForMultipleChoice, FlaxDistilBertForQuestionAnswering, FlaxDistilBertForSequenceClassification, FlaxDistilBertForTokenClassification, FlaxDistilBertModel, FlaxDistilBertPreTrainedModel, ) else: import sys _A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
231
0
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging __snake_case = logging.get_logger(__name__) __snake_case = {"""vocab_file""": """spiece.model"""} __snake_case = { """vocab_file""": { """TsinghuaAI/CPM-Generate""": """https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model""", } } class lowercase__ ( _UpperCAmelCase ): def __init__( self : Any , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : str=True , UpperCAmelCase_ : int=False , UpperCAmelCase_ : List[str]="<s>" , UpperCAmelCase_ : Dict="</s>" , UpperCAmelCase_ : Any="<unk>" , UpperCAmelCase_ : Tuple="<sep>" , UpperCAmelCase_ : Tuple="<pad>" , UpperCAmelCase_ : str="<cls>" , UpperCAmelCase_ : int="<mask>" , UpperCAmelCase_ : int=["<eop>", "<eod>"] , UpperCAmelCase_ : Optional[Dict[str, Any]] = None , **UpperCAmelCase_ : Dict , ): SCREAMING_SNAKE_CASE__ = AddedToken(UpperCAmelCase_ , lstrip=UpperCAmelCase_ , rstrip=UpperCAmelCase_ ) if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) else mask_token SCREAMING_SNAKE_CASE__ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=UpperCAmelCase_ , remove_space=UpperCAmelCase_ , keep_accents=UpperCAmelCase_ , bos_token=UpperCAmelCase_ , eos_token=UpperCAmelCase_ , unk_token=UpperCAmelCase_ , sep_token=UpperCAmelCase_ , pad_token=UpperCAmelCase_ , cls_token=UpperCAmelCase_ , mask_token=UpperCAmelCase_ , additional_special_tokens=UpperCAmelCase_ , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase_ , ) SCREAMING_SNAKE_CASE__ = 3 SCREAMING_SNAKE_CASE__ = do_lower_case SCREAMING_SNAKE_CASE__ = remove_space SCREAMING_SNAKE_CASE__ = keep_accents SCREAMING_SNAKE_CASE__ = vocab_file SCREAMING_SNAKE_CASE__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCAmelCase_ ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( 'You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ' 'See https://pypi.org/project/jieba/ for installation.' ) SCREAMING_SNAKE_CASE__ = jieba SCREAMING_SNAKE_CASE__ = str.maketrans(' \n' , '\u2582\u2583' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def A_ ( self : Any ): return len(self.sp_model ) def A_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE__ = {self.convert_ids_to_tokens(UpperCAmelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : List[str] ): SCREAMING_SNAKE_CASE__ = self.__dict__.copy() SCREAMING_SNAKE_CASE__ = None return state def __setstate__( self : Optional[int] , UpperCAmelCase_ : Any ): SCREAMING_SNAKE_CASE__ = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def A_ ( self : Any , UpperCAmelCase_ : Any ): if self.remove_space: SCREAMING_SNAKE_CASE__ = ' '.join(inputs.strip().split() ) else: SCREAMING_SNAKE_CASE__ = inputs SCREAMING_SNAKE_CASE__ = outputs.replace('``' , '"' ).replace('\'\'' , '"' ) if not self.keep_accents: SCREAMING_SNAKE_CASE__ = unicodedata.normalize('NFKD' , UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = ''.join([c for c in outputs if not unicodedata.combining(UpperCAmelCase_ )] ) if self.do_lower_case: SCREAMING_SNAKE_CASE__ = outputs.lower() return outputs def A_ ( self : Optional[int] , UpperCAmelCase_ : str ): SCREAMING_SNAKE_CASE__ = self.preprocess_text(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = self.sp_model.encode(UpperCAmelCase_ , out_type=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = [] for piece in pieces: if len(UpperCAmelCase_ ) > 1 and piece[-1] == str(',' ) and piece[-2].isdigit(): SCREAMING_SNAKE_CASE__ = self.sp_model.EncodeAsPieces(piece[:-1].replace(UpperCAmelCase_ , '' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: SCREAMING_SNAKE_CASE__ = cur_pieces[1:] else: SCREAMING_SNAKE_CASE__ = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(UpperCAmelCase_ ) else: new_pieces.append(UpperCAmelCase_ ) return new_pieces def A_ ( self : List[str] , UpperCAmelCase_ : Any ): return self.sp_model.PieceToId(UpperCAmelCase_ ) def A_ ( self : str , UpperCAmelCase_ : Optional[int] ): return self.sp_model.IdToPiece(UpperCAmelCase_ ) def A_ ( self : str , UpperCAmelCase_ : Tuple ): SCREAMING_SNAKE_CASE__ = ''.join(UpperCAmelCase_ ).replace(UpperCAmelCase_ , ' ' ).strip() return out_string def A_ ( self : Optional[Any] , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE__ = [self.sep_token_id] SCREAMING_SNAKE_CASE__ = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def A_ ( self : List[Any] , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None , UpperCAmelCase_ : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCAmelCase_ , token_ids_a=UpperCAmelCase_ , already_has_special_tokens=UpperCAmelCase_ ) if token_ids_a is not None: return ([0] * len(UpperCAmelCase_ )) + [1] + ([0] * len(UpperCAmelCase_ )) + [1, 1] return ([0] * len(UpperCAmelCase_ )) + [1, 1] def A_ ( self : str , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None ): SCREAMING_SNAKE_CASE__ = [self.sep_token_id] SCREAMING_SNAKE_CASE__ = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def A_ ( self : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[str] = None ): if not os.path.isdir(UpperCAmelCase_ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return SCREAMING_SNAKE_CASE__ = os.path.join( UpperCAmelCase_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase_ ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase_ , 'wb' ) as fi: SCREAMING_SNAKE_CASE__ = self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase_ ) return (out_vocab_file,) def A_ ( self : Union[str, Any] , *UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : str ): SCREAMING_SNAKE_CASE__ = super()._decode(*UpperCAmelCase_ , **UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = text.replace(' ' , '' ).replace('\u2582' , ' ' ).replace('\u2583' , '\n' ) return text
360
import json import os import sys import tempfile import unittest from pathlib import Path from shutil import copyfile from huggingface_hub import HfFolder, Repository, create_repo, delete_repo from requests.exceptions import HTTPError import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, PROCESSOR_MAPPING, TOKENIZER_MAPPING, AutoConfig, AutoFeatureExtractor, AutoProcessor, AutoTokenizer, BertTokenizer, ProcessorMixin, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaProcessor, ) from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 from test_module.custom_processing import CustomProcessor # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 __snake_case = get_tests_dir("""fixtures/dummy_feature_extractor_config.json""") __snake_case = get_tests_dir("""fixtures/vocab.json""") __snake_case = get_tests_dir("""fixtures""") class lowercase__ ( unittest.TestCase ): A__ : List[Any] =["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """bla""", """blou"""] def A_ ( self : Tuple ): SCREAMING_SNAKE_CASE__ = 0 def A_ ( self : str ): SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained('facebook/wav2vec2-base-960h' ) self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_ ) def A_ ( self : List[Any] ): with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE__ = WavaVecaConfig() SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained('facebook/wav2vec2-base-960h' ) # save in new folder model_config.save_pretrained(UpperCAmelCase_ ) processor.save_pretrained(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained(UpperCAmelCase_ ) self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_ ) def A_ ( self : Union[str, Any] ): with tempfile.TemporaryDirectory() as tmpdirname: # copy relevant files copyfile(UpperCAmelCase_ , os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) ) copyfile(UpperCAmelCase_ , os.path.join(UpperCAmelCase_ , 'vocab.json' ) ) SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained(UpperCAmelCase_ ) self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_ ) def A_ ( self : Dict ): with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE__ = WavaVecaFeatureExtractor() SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('facebook/wav2vec2-base-960h' ) SCREAMING_SNAKE_CASE__ = WavaVecaProcessor(UpperCAmelCase_ , UpperCAmelCase_ ) # save in new folder processor.save_pretrained(UpperCAmelCase_ ) # drop `processor_class` in tokenizer with open(os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) , 'r' ) as f: SCREAMING_SNAKE_CASE__ = json.load(UpperCAmelCase_ ) config_dict.pop('processor_class' ) with open(os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) , 'w' ) as f: f.write(json.dumps(UpperCAmelCase_ ) ) SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained(UpperCAmelCase_ ) self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_ ) def A_ ( self : List[str] ): with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE__ = WavaVecaFeatureExtractor() SCREAMING_SNAKE_CASE__ = AutoTokenizer.from_pretrained('facebook/wav2vec2-base-960h' ) SCREAMING_SNAKE_CASE__ = WavaVecaProcessor(UpperCAmelCase_ , UpperCAmelCase_ ) # save in new folder processor.save_pretrained(UpperCAmelCase_ ) # drop `processor_class` in feature extractor with open(os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) , 'r' ) as f: SCREAMING_SNAKE_CASE__ = json.load(UpperCAmelCase_ ) config_dict.pop('processor_class' ) with open(os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) , 'w' ) as f: f.write(json.dumps(UpperCAmelCase_ ) ) SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained(UpperCAmelCase_ ) self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_ ) def A_ ( self : Union[str, Any] ): with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE__ = WavaVecaConfig(processor_class='Wav2Vec2Processor' ) model_config.save_pretrained(UpperCAmelCase_ ) # copy relevant files copyfile(UpperCAmelCase_ , os.path.join(UpperCAmelCase_ , 'vocab.json' ) ) # create emtpy sample processor with open(os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) , 'w' ) as f: f.write('{}' ) SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained(UpperCAmelCase_ ) self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_ ) def A_ ( self : Optional[Any] ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(UpperCAmelCase_ ): SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained('hf-internal-testing/test_dynamic_processor' ) # If remote code is disabled, we can't load this config. with self.assertRaises(UpperCAmelCase_ ): SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained( 'hf-internal-testing/test_dynamic_processor' , trust_remote_code=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained('hf-internal-testing/test_dynamic_processor' , trust_remote_code=UpperCAmelCase_ ) self.assertTrue(processor.special_attribute_present ) self.assertEqual(processor.__class__.__name__ , 'NewProcessor' ) SCREAMING_SNAKE_CASE__ = processor.feature_extractor self.assertTrue(feature_extractor.special_attribute_present ) self.assertEqual(feature_extractor.__class__.__name__ , 'NewFeatureExtractor' ) SCREAMING_SNAKE_CASE__ = processor.tokenizer self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast' ) # Test we can also load the slow version SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained( 'hf-internal-testing/test_dynamic_processor' , trust_remote_code=UpperCAmelCase_ , use_fast=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = new_processor.tokenizer self.assertTrue(new_tokenizer.special_attribute_present ) self.assertEqual(new_tokenizer.__class__.__name__ , 'NewTokenizer' ) else: self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) def A_ ( self : Union[str, Any] ): try: AutoConfig.register('custom' , UpperCAmelCase_ ) AutoFeatureExtractor.register(UpperCAmelCase_ , UpperCAmelCase_ ) AutoTokenizer.register(UpperCAmelCase_ , slow_tokenizer_class=UpperCAmelCase_ ) AutoProcessor.register(UpperCAmelCase_ , UpperCAmelCase_ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(UpperCAmelCase_ ): AutoProcessor.register(UpperCAmelCase_ , UpperCAmelCase_ ) # Now that the config is registered, it can be used as any other config with the auto-API SCREAMING_SNAKE_CASE__ = CustomFeatureExtractor.from_pretrained(UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE__ = os.path.join(UpperCAmelCase_ , 'vocab.txt' ) with open(UpperCAmelCase_ , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in self.vocab_tokens] ) ) SCREAMING_SNAKE_CASE__ = CustomTokenizer(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = CustomProcessor(UpperCAmelCase_ , UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained(UpperCAmelCase_ ) self.assertIsInstance(UpperCAmelCase_ , UpperCAmelCase_ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def A_ ( self : Tuple ): class lowercase__ ( _UpperCAmelCase ): A__ : Optional[int] =False class lowercase__ ( _UpperCAmelCase ): A__ : Optional[int] =False class lowercase__ ( _UpperCAmelCase ): A__ : Dict ="""AutoFeatureExtractor""" A__ : Optional[int] ="""AutoTokenizer""" A__ : str =False try: AutoConfig.register('custom' , UpperCAmelCase_ ) AutoFeatureExtractor.register(UpperCAmelCase_ , UpperCAmelCase_ ) AutoTokenizer.register(UpperCAmelCase_ , slow_tokenizer_class=UpperCAmelCase_ ) AutoProcessor.register(UpperCAmelCase_ , UpperCAmelCase_ ) # If remote code is not set, the default is to use local classes. SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained('hf-internal-testing/test_dynamic_processor' ) self.assertEqual(processor.__class__.__name__ , 'NewProcessor' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote code is disabled, we load the local ones. SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained( 'hf-internal-testing/test_dynamic_processor' , trust_remote_code=UpperCAmelCase_ ) self.assertEqual(processor.__class__.__name__ , 'NewProcessor' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub. SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained( 'hf-internal-testing/test_dynamic_processor' , trust_remote_code=UpperCAmelCase_ ) self.assertEqual(processor.__class__.__name__ , 'NewProcessor' ) self.assertTrue(processor.special_attribute_present ) self.assertTrue(processor.feature_extractor.special_attribute_present ) self.assertTrue(processor.tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def A_ ( self : List[str] ): SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(processor.__class__.__name__ , 'BertTokenizerFast' ) def A_ ( self : str ): SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained('hf-internal-testing/tiny-random-convnext' ) self.assertEqual(processor.__class__.__name__ , 'ConvNextImageProcessor' ) @is_staging_test class lowercase__ ( unittest.TestCase ): A__ : List[Any] =["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """bla""", """blou"""] @classmethod def A_ ( cls : str ): SCREAMING_SNAKE_CASE__ = TOKEN HfFolder.save_token(UpperCAmelCase_ ) @classmethod def A_ ( cls : List[Any] ): try: delete_repo(token=cls._token , repo_id='test-processor' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-processor-org' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='test-dynamic-processor' ) except HTTPError: pass def A_ ( self : List[str] ): SCREAMING_SNAKE_CASE__ = WavaVecaProcessor.from_pretrained(UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCAmelCase_ , 'test-processor' ) , push_to_hub=UpperCAmelCase_ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE__ = WavaVecaProcessor.from_pretrained(F'{USER}/test-processor' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCAmelCase_ , getattr(new_processor.feature_extractor , UpperCAmelCase_ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def A_ ( self : Dict ): SCREAMING_SNAKE_CASE__ = WavaVecaProcessor.from_pretrained(UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(UpperCAmelCase_ , 'test-processor-org' ) , push_to_hub=UpperCAmelCase_ , use_auth_token=self._token , organization='valid_org' , ) SCREAMING_SNAKE_CASE__ = WavaVecaProcessor.from_pretrained('valid_org/test-processor-org' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(UpperCAmelCase_ , getattr(new_processor.feature_extractor , UpperCAmelCase_ ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def A_ ( self : Optional[Any] ): CustomFeatureExtractor.register_for_auto_class() CustomTokenizer.register_for_auto_class() CustomProcessor.register_for_auto_class() SCREAMING_SNAKE_CASE__ = CustomFeatureExtractor.from_pretrained(UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: SCREAMING_SNAKE_CASE__ = os.path.join(UpperCAmelCase_ , 'vocab.txt' ) with open(UpperCAmelCase_ , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in self.vocab_tokens] ) ) SCREAMING_SNAKE_CASE__ = CustomTokenizer(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = CustomProcessor(UpperCAmelCase_ , UpperCAmelCase_ ) with tempfile.TemporaryDirectory() as tmp_dir: create_repo(F'{USER}/test-dynamic-processor' , token=self._token ) SCREAMING_SNAKE_CASE__ = Repository(UpperCAmelCase_ , clone_from=F'{USER}/test-dynamic-processor' , token=self._token ) processor.save_pretrained(UpperCAmelCase_ ) # This has added the proper auto_map field to the feature extractor config self.assertDictEqual( processor.feature_extractor.auto_map , { 'AutoFeatureExtractor': 'custom_feature_extraction.CustomFeatureExtractor', 'AutoProcessor': 'custom_processing.CustomProcessor', } , ) # This has added the proper auto_map field to the tokenizer config with open(os.path.join(UpperCAmelCase_ , 'tokenizer_config.json' ) ) as f: SCREAMING_SNAKE_CASE__ = json.load(UpperCAmelCase_ ) self.assertDictEqual( tokenizer_config['auto_map'] , { 'AutoTokenizer': ['custom_tokenization.CustomTokenizer', None], 'AutoProcessor': 'custom_processing.CustomProcessor', } , ) # The code has been copied from fixtures self.assertTrue(os.path.isfile(os.path.join(UpperCAmelCase_ , 'custom_feature_extraction.py' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCAmelCase_ , 'custom_tokenization.py' ) ) ) self.assertTrue(os.path.isfile(os.path.join(UpperCAmelCase_ , 'custom_processing.py' ) ) ) repo.push_to_hub() SCREAMING_SNAKE_CASE__ = AutoProcessor.from_pretrained(F'{USER}/test-dynamic-processor' , trust_remote_code=UpperCAmelCase_ ) # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module self.assertEqual(new_processor.__class__.__name__ , 'CustomProcessor' )
169
0
'''simple docstring''' from __future__ import annotations def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ ): if nth_term == "": return [""] UpperCAmelCase : str = int(UpperCAmelCase_ ) UpperCAmelCase : Dict = int(UpperCAmelCase_ ) UpperCAmelCase : list[str] = [] for temp in range(int(UpperCAmelCase_ ) ): series.append(F"""1 / {pow(temp + 1 , int(UpperCAmelCase_ ) )}""" if series else '1' ) return series if __name__ == "__main__": import doctest doctest.testmod() lowercase__ = int(input("Enter the last number (nth term) of the P-Series")) lowercase__ = int(input("Enter the power for P-Series")) print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") print(p_series(nth_term, power))
151
'''simple docstring''' def UpperCamelCase( UpperCAmelCase_ ): UpperCAmelCase : Optional[Any] = set() # edges = list of graph's edges UpperCAmelCase : str = get_edges(UpperCAmelCase_ ) # While there are still elements in edges list, take an arbitrary edge # (from_node, to_node) and add his extremity to chosen_vertices and then # remove all arcs adjacent to the from_node and to_node while edges: UpperCAmelCase , UpperCAmelCase : Union[str, Any] = edges.pop() chosen_vertices.add(UpperCAmelCase_ ) chosen_vertices.add(UpperCAmelCase_ ) for edge in edges.copy(): if from_node in edge or to_node in edge: edges.discard(UpperCAmelCase_ ) return chosen_vertices def UpperCamelCase( UpperCAmelCase_ ): UpperCAmelCase : List[str] = set() for from_node, to_nodes in graph.items(): for to_node in to_nodes: edges.add((from_node, to_node) ) return edges if __name__ == "__main__": import doctest doctest.testmod() # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")
151
1
import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase ( A_ ,unittest.TestCase ): A__ : int = ProphetNetTokenizer A__ : List[Any] = False def _SCREAMING_SNAKE_CASE (self : Any ) -> str: '''simple docstring''' super().setUp() snake_case : Dict = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] snake_case : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) def _SCREAMING_SNAKE_CASE (self : Tuple , snake_case__ : str ) -> Optional[Any]: '''simple docstring''' snake_case : int = "UNwant\u00E9d,running" snake_case : Dict = "unwanted, running" return input_text, output_text def _SCREAMING_SNAKE_CASE (self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = self.tokenizer_class(self.vocab_file ) snake_case : int = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(snake_case__ , ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) , [9, 6, 7, 12, 10, 11] ) def _SCREAMING_SNAKE_CASE (self : Tuple ) -> Optional[Any]: '''simple docstring''' snake_case : str = BasicTokenizer() self.assertListEqual(tokenizer.tokenize("ah\u535A\u63A8zz" ) , ["ah", "\u535A", "\u63A8", "zz"] ) def _SCREAMING_SNAKE_CASE (self : Any ) -> int: '''simple docstring''' snake_case : Optional[Any] = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? " ) , ["hello", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ) , ["hello"] ) def _SCREAMING_SNAKE_CASE (self : Tuple ) -> List[Any]: '''simple docstring''' snake_case : Any = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ) , ["hällo", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ) , ["h\u00E9llo"] ) def _SCREAMING_SNAKE_CASE (self : Tuple ) -> Optional[int]: '''simple docstring''' snake_case : Tuple = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ) , ["hallo", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ) , ["hello"] ) def _SCREAMING_SNAKE_CASE (self : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Any = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ) , ["hallo", "!", "how", "are", "you", "?"] ) self.assertListEqual(tokenizer.tokenize("H\u00E9llo" ) , ["hello"] ) def _SCREAMING_SNAKE_CASE (self : List[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? " ) , ["HeLLo", "!", "how", "Are", "yoU", "?"] ) def _SCREAMING_SNAKE_CASE (self : Any ) -> Dict: '''simple docstring''' snake_case : List[Any] = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ) , ["HäLLo", "!", "how", "Are", "yoU", "?"] ) def _SCREAMING_SNAKE_CASE (self : Optional[int] ) -> List[str]: '''simple docstring''' snake_case : Any = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(" \tHäLLo!how \n Are yoU? " ) , ["HaLLo", "!", "how", "Are", "yoU", "?"] ) def _SCREAMING_SNAKE_CASE (self : Optional[int] ) -> int: '''simple docstring''' snake_case : Tuple = BasicTokenizer(do_lower_case=snake_case__ , never_split=["[UNK]"] ) self.assertListEqual( tokenizer.tokenize(" \tHeLLo!how \n Are yoU? [UNK]" ) , ["HeLLo", "!", "how", "Are", "yoU", "?", "[UNK]"] ) def _SCREAMING_SNAKE_CASE (self : Optional[Any] ) -> Any: '''simple docstring''' snake_case : Tuple = ["[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing"] snake_case : List[str] = {} for i, token in enumerate(snake_case__ ): snake_case : Optional[int] = i snake_case : Tuple = WordpieceTokenizer(vocab=snake_case__ , unk_token="[UNK]" ) self.assertListEqual(tokenizer.tokenize("" ) , [] ) self.assertListEqual(tokenizer.tokenize("unwanted running" ) , ["un", "##want", "##ed", "runn", "##ing"] ) self.assertListEqual(tokenizer.tokenize("unwantedX running" ) , ["[UNK]", "runn", "##ing"] ) @require_torch def _SCREAMING_SNAKE_CASE (self : int ) -> Optional[Any]: '''simple docstring''' snake_case : Union[str, Any] = self.tokenizer_class.from_pretrained("microsoft/prophetnet-large-uncased" ) snake_case : Optional[Any] = ["A long paragraph for summarization.", "Another paragraph for summarization."] snake_case : Union[str, Any] = [10_37, 21_46, 2_04_23, 20_05, 76_80, 78_49, 39_89, 10_12, 1_02] snake_case : Optional[Any] = tokenizer(snake_case__ , padding=snake_case__ , return_tensors="pt" ) self.assertIsInstance(snake_case__ , snake_case__ ) snake_case : Any = list(batch.input_ids.numpy()[0] ) self.assertListEqual(snake_case__ , snake_case__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) def _SCREAMING_SNAKE_CASE (self : Tuple ) -> str: '''simple docstring''' self.assertTrue(_is_whitespace(" " ) ) self.assertTrue(_is_whitespace("\t" ) ) self.assertTrue(_is_whitespace("\r" ) ) self.assertTrue(_is_whitespace("\n" ) ) self.assertTrue(_is_whitespace("\u00A0" ) ) self.assertFalse(_is_whitespace("A" ) ) self.assertFalse(_is_whitespace("-" ) ) def _SCREAMING_SNAKE_CASE (self : Union[str, Any] ) -> Tuple: '''simple docstring''' self.assertTrue(_is_control("\u0005" ) ) self.assertFalse(_is_control("A" ) ) self.assertFalse(_is_control(" " ) ) self.assertFalse(_is_control("\t" ) ) self.assertFalse(_is_control("\r" ) ) def _SCREAMING_SNAKE_CASE (self : List[Any] ) -> Tuple: '''simple docstring''' self.assertTrue(_is_punctuation("-" ) ) self.assertTrue(_is_punctuation("$" ) ) self.assertTrue(_is_punctuation("`" ) ) self.assertTrue(_is_punctuation("." ) ) self.assertFalse(_is_punctuation("A" ) ) self.assertFalse(_is_punctuation(" " ) ) @slow def _SCREAMING_SNAKE_CASE (self : List[Any] ) -> Dict: '''simple docstring''' snake_case : Tuple = self.tokenizer_class.from_pretrained("microsoft/prophetnet-large-uncased" ) snake_case : Tuple = tokenizer.encode("sequence builders" , add_special_tokens=snake_case__ ) snake_case : List[Any] = tokenizer.encode("multi-sequence build" , add_special_tokens=snake_case__ ) snake_case : int = tokenizer.build_inputs_with_special_tokens(snake_case__ ) snake_case : Optional[Any] = tokenizer.build_inputs_with_special_tokens(snake_case__ , snake_case__ ) assert encoded_sentence == text + [1_02] assert encoded_pair == text + [1_02] + text_a + [1_02]
10
import dataclasses import re import string from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple import numpy as np from . import residue_constants __lowerCamelCase = Mapping[str, np.ndarray] __lowerCamelCase = Mapping[str, Any] # Is a nested dict. __lowerCamelCase = 0.01 @dataclasses.dataclass(frozen=A_ ) class UpperCAmelCase : A__ : np.ndarray # [num_res, num_atom_type, 3] # Amino-acid type for each residue represented as an integer between 0 and # 20, where 20 is 'X'. A__ : np.ndarray # [num_res] # Binary float mask to indicate presence of a particular atom. 1.0 if an atom # is present and 0.0 if not. This should be used for loss masking. A__ : np.ndarray # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. A__ : np.ndarray # [num_res] # B-factors, or temperature factors, of each residue (in sq. angstroms units), # representing the displacement of the residue from its ground truth mean # value. A__ : np.ndarray # [num_res, num_atom_type] # Chain indices for multi-chain predictions A__ : Optional[np.ndarray] = None # Optional remark about the protein. Included as a comment in output PDB # files A__ : Optional[str] = None # Templates used to generate this protein (prediction-only) A__ : Optional[Sequence[str]] = None # Chain corresponding to each parent A__ : Optional[Sequence[int]] = None def UpperCamelCase ( __lowerCamelCase : str ): snake_case : Dict = r"(\[[A-Z]+\]\n)" snake_case : List[str] = [tag.strip() for tag in re.split(__lowerCamelCase , __lowerCamelCase ) if len(__lowerCamelCase ) > 0] snake_case : Iterator[Tuple[str, List[str]]] = zip(tags[0::2] , [l.split("\n" ) for l in tags[1::2]] ) snake_case : List[str] = ["N", "CA", "C"] snake_case : str = None snake_case : str = None snake_case : Tuple = None for g in groups: if "[PRIMARY]" == g[0]: snake_case : Tuple = g[1][0].strip() for i in range(len(__lowerCamelCase ) ): if seq[i] not in residue_constants.restypes: snake_case : Optional[Any] = "X" # FIXME: strings are immutable snake_case : Optional[int] = np.array( [residue_constants.restype_order.get(__lowerCamelCase , residue_constants.restype_num ) for res_symbol in seq] ) elif "[TERTIARY]" == g[0]: snake_case : List[List[float]] = [] for axis in range(3 ): tertiary.append(list(map(__lowerCamelCase , g[1][axis].split() ) ) ) snake_case : Union[str, Any] = np.array(__lowerCamelCase ) snake_case : str = np.zeros((len(tertiary[0] ) // 3, residue_constants.atom_type_num, 3) ).astype(np.floataa ) for i, atom in enumerate(__lowerCamelCase ): snake_case : Dict = np.transpose(tertiary_np[:, i::3] ) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: snake_case : int = np.array(list(map({"-": 0, "+": 1}.get , g[1][0].strip() ) ) ) snake_case : List[str] = np.zeros( ( len(__lowerCamelCase ), residue_constants.atom_type_num, ) ).astype(np.floataa ) for i, atom in enumerate(__lowerCamelCase ): snake_case : Any = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=__lowerCamelCase , atom_mask=__lowerCamelCase , aatype=__lowerCamelCase , residue_index=np.arange(len(__lowerCamelCase ) ) , b_factors=__lowerCamelCase , ) def UpperCamelCase ( __lowerCamelCase : Protein , __lowerCamelCase : int = 0 ): snake_case : List[str] = [] snake_case : str = prot.remark if remark is not None: pdb_headers.append(f"""REMARK {remark}""" ) snake_case : Union[str, Any] = prot.parents snake_case : Dict = prot.parents_chain_index if parents is not None and parents_chain_index is not None: snake_case : Tuple = [p for i, p in zip(__lowerCamelCase , __lowerCamelCase ) if i == chain_id] if parents is None or len(__lowerCamelCase ) == 0: snake_case : int = ["N/A"] pdb_headers.append(f"""PARENT {' '.join(__lowerCamelCase )}""" ) return pdb_headers def UpperCamelCase ( __lowerCamelCase : Protein , __lowerCamelCase : str ): snake_case : List[str] = [] snake_case : Any = pdb_str.split("\n" ) snake_case : int = prot.remark if remark is not None: out_pdb_lines.append(f"""REMARK {remark}""" ) snake_case : List[List[str]] if prot.parents is not None and len(prot.parents ) > 0: snake_case : Optional[Any] = [] if prot.parents_chain_index is not None: snake_case : Dict[str, List[str]] = {} for p, i in zip(prot.parents , prot.parents_chain_index ): parent_dict.setdefault(str(__lowerCamelCase ) , [] ) parent_dict[str(__lowerCamelCase )].append(__lowerCamelCase ) snake_case : List[str] = max([int(__lowerCamelCase ) for chain_idx in parent_dict] ) for i in range(max_idx + 1 ): snake_case : Optional[Any] = parent_dict.get(str(__lowerCamelCase ) , ["N/A"] ) parents_per_chain.append(__lowerCamelCase ) else: parents_per_chain.append(list(prot.parents ) ) else: snake_case : Optional[Any] = [["N/A"]] def make_parent_line(__lowerCamelCase : Sequence[str] ) -> str: return f"""PARENT {' '.join(__lowerCamelCase )}""" out_pdb_lines.append(make_parent_line(parents_per_chain[0] ) ) snake_case : List[Any] = 0 for i, l in enumerate(__lowerCamelCase ): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(__lowerCamelCase ) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(__lowerCamelCase ): snake_case : int = parents_per_chain[chain_counter] else: snake_case : Any = ["N/A"] out_pdb_lines.append(make_parent_line(__lowerCamelCase ) ) return "\n".join(__lowerCamelCase ) def UpperCamelCase ( __lowerCamelCase : Protein ): snake_case : str = residue_constants.restypes + ["X"] def res_atoa(__lowerCamelCase : int ) -> str: return residue_constants.restype_atoa.get(restypes[r] , "UNK" ) snake_case : List[Any] = residue_constants.atom_types snake_case : List[str] = [] snake_case : Any = prot.atom_mask snake_case : Any = prot.aatype snake_case : Dict = prot.atom_positions snake_case : List[str] = prot.residue_index.astype(np.intaa ) snake_case : Dict = prot.b_factors snake_case : Tuple = prot.chain_index if np.any(aatype > residue_constants.restype_num ): raise ValueError("Invalid aatypes." ) snake_case : Any = get_pdb_headers(__lowerCamelCase ) if len(__lowerCamelCase ) > 0: pdb_lines.extend(__lowerCamelCase ) snake_case : Dict = aatype.shape[0] snake_case : Tuple = 1 snake_case : Any = 0 snake_case : Union[str, Any] = string.ascii_uppercase snake_case : int = None # Add all atom sites. for i in range(__lowerCamelCase ): snake_case : List[Any] = res_atoa(aatype[i] ) for atom_name, pos, mask, b_factor in zip(__lowerCamelCase , atom_positions[i] , atom_mask[i] , b_factors[i] ): if mask < 0.5: continue snake_case : Any = "ATOM" snake_case : str = atom_name if len(__lowerCamelCase ) == 4 else f""" {atom_name}""" snake_case : Optional[Any] = "" snake_case : Dict = "" snake_case : Optional[Any] = 1.00 snake_case : str = atom_name[0] # Protein supports only C, N, O, S, this works. snake_case : Dict = "" snake_case : Any = "A" if chain_index is not None: snake_case : str = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! snake_case : List[str] = ( f"""{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}""" f"""{res_name_a:>3} {chain_tag:>1}""" f"""{residue_index[i]:>4}{insertion_code:>1} """ f"""{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}""" f"""{occupancy:>6.2f}{b_factor:>6.2f} """ f"""{element:>2}{charge:>2}""" ) pdb_lines.append(__lowerCamelCase ) atom_index += 1 snake_case : Optional[int] = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: snake_case : Any = True snake_case : Tuple = chain_index[i + 1] if should_terminate: # Close the chain. snake_case : Optional[Any] = "TER" snake_case : Optional[int] = ( f"""{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i] ):>3} {chain_tag:>1}{residue_index[i]:>4}""" ) pdb_lines.append(__lowerCamelCase ) atom_index += 1 if i != n - 1: # "prev" is a misnomer here. This happens at the beginning of # each new chain. pdb_lines.extend(get_pdb_headers(__lowerCamelCase , __lowerCamelCase ) ) pdb_lines.append("END" ) pdb_lines.append("" ) return "\n".join(__lowerCamelCase ) def UpperCamelCase ( __lowerCamelCase : Protein ): return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def UpperCamelCase ( __lowerCamelCase : FeatureDict , __lowerCamelCase : ModelOutput , __lowerCamelCase : Optional[np.ndarray] = None , __lowerCamelCase : Optional[np.ndarray] = None , __lowerCamelCase : Optional[str] = None , __lowerCamelCase : Optional[Sequence[str]] = None , __lowerCamelCase : Optional[Sequence[int]] = None , ): return Protein( aatype=features["aatype"] , atom_positions=result["final_atom_positions"] , atom_mask=result["final_atom_mask"] , residue_index=features["residue_index"] + 1 , b_factors=b_factors if b_factors is not None else np.zeros_like(result["final_atom_mask"] ) , chain_index=__lowerCamelCase , remark=__lowerCamelCase , parents=__lowerCamelCase , parents_chain_index=__lowerCamelCase , )
10
1