code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def __UpperCAmelCase ( __a : Namespace ) -> Dict: """simple docstring""" return ConvertCommand( args.model_type ,args.tf_checkpoint ,args.pytorch_dump_output ,args.config ,args.finetuning_task_name ) a__ = ''' transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions. ''' class UpperCAmelCase_ ( __lowercase ): """simple docstring""" @staticmethod def __lowercase ( _a ) -> Optional[Any]: _a : Union[str, Any] = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=_a , required=_a , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=_a , required=_a , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=_a , required=_a , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=_a , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=_a , default=_a , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=_a ) def __init__( self , _a , _a , _a , _a , _a , *_a , ) -> Tuple: _a : str = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(F"""Loading model {model_type}""" ) _a : int = model_type _a : Optional[Any] = tf_checkpoint _a : Union[str, Any] = pytorch_dump_output _a : Tuple = config _a : str = finetuning_task_name def __lowercase ( self ) -> str: if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(_a ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) if "ckpt" in self._tf_checkpoint.lower(): _a : Union[str, Any] = self._tf_checkpoint _a : str = '''''' else: _a : int = self._tf_checkpoint _a : str = '''''' convert_transfo_xl_checkpoint_to_pytorch( _a , self._config , self._pytorch_dump_output , _a ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(_a ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
14
import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : Dict = '''ZinengTang/tvlt-base''' _a : List[str] = tempfile.mkdtemp() def __lowercase ( self , **_a ) -> int: return TvltImageProcessor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self , **_a ) -> List[Any]: return TvltFeatureExtractor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self ) -> Optional[int]: shutil.rmtree(self.tmpdirname ) def __lowercase ( self ) -> Dict: _a : Union[str, Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Optional[int] = TvltProcessor(image_processor=_a , feature_extractor=_a ) processor.save_pretrained(self.tmpdirname ) _a : Any = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor , _a ) self.assertIsInstance(processor.image_processor , _a ) def __lowercase ( self ) -> Any: _a : Optional[Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : Union[str, Any] = np.ones([1_2_0_0_0] ) _a : Dict = feature_extractor(_a , return_tensors='''np''' ) _a : Tuple = processor(audio=_a , return_tensors='''np''' ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> int: _a : Optional[Any] = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Optional[Any] = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[Any] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = image_processor(_a , return_tensors='''np''' ) _a : Optional[int] = processor(images=_a , return_tensors='''np''' ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> Union[str, Any]: _a : int = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Any = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[str] = np.ones([1_2_0_0_0] ) _a : Optional[int] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = processor(audio=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __lowercase ( self ) -> Union[str, Any]: _a : str = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' , )
14
1
import json import logging import os import sys from time import time from unittest.mock import patch from transformers.testing_utils import TestCasePlus, require_torch_tpu logging.basicConfig(level=logging.DEBUG) a__ = logging.getLogger() def __UpperCAmelCase ( __a : List[str] ) -> Tuple: """simple docstring""" _a : List[str] = {} _a : int = os.path.join(__a ,'''all_results.json''' ) if os.path.exists(__a ): with open(__a ,'''r''' ) as f: _a : Any = json.load(__a ) else: raise ValueError(F"""can't find {path}""" ) return results a__ = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) @require_torch_tpu class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __lowercase ( self ) -> Optional[Any]: import xla_spawn _a : Union[str, Any] = self.get_auto_remove_tmp_dir() _a : List[Any] = F""" ./examples/pytorch/text-classification/run_glue.py --num_cores=8 ./examples/pytorch/text-classification/run_glue.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --overwrite_output_dir --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --do_train --do_eval --debug tpu_metrics_debug --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --max_steps=10 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(_a , '''argv''' , _a ): _a : List[str] = time() xla_spawn.main() _a : Tuple = time() _a : Tuple = get_results(_a ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.75 ) # Assert that the script takes less than 500 seconds to make sure it doesn't hang. self.assertLess(end - start , 5_0_0 ) def __lowercase ( self ) -> Dict: import xla_spawn _a : str = ''' ./tests/test_trainer_tpu.py --num_cores=8 ./tests/test_trainer_tpu.py '''.split() with patch.object(_a , '''argv''' , _a ): xla_spawn.main()
14
def __UpperCAmelCase ( __a : str ) -> list: """simple docstring""" if n_term == "": return [] _a : list = [] for temp in range(int(__a ) ): series.append(F"""1/{temp + 1}""" if series else '''1''' ) return series if __name__ == "__main__": a__ = 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))
14
1
def __UpperCAmelCase ( __a : list[int] ,__a : list[int] ) -> None: """simple docstring""" _a : List[Any] = len(__a ) print('''The following activities are selected:''' ) # The first activity is always selected _a : List[Any] = 0 print(__a ,end=''',''' ) # Consider rest of the activities for j in range(__a ): # If this activity has start time greater than # or equal to the finish time of previously # selected activity, then select it if start[j] >= finish[i]: print(__a ,end=''',''' ) _a : Optional[int] = j if __name__ == "__main__": import doctest doctest.testmod() a__ = [1, 3, 0, 5, 8, 5] a__ = [2, 4, 6, 7, 9, 9] print_max_activities(start, finish)
14
import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Optional[int] ) -> Dict: """simple docstring""" return params[F"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :] def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[int] ,__a : int ,__a : List[str]="attention" ) -> List[str]: """simple docstring""" _a : str = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :] ) _a : Tuple = k_tmp.reshape(k_tmp.shape[0] ,k_tmp.shape[1] * k_tmp.shape[2] ) _a : Any = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :] ) _a : Dict = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] ,o_tmp.shape[2] ) _a : Union[str, Any] = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :] ) _a : Any = q_tmp.reshape(q_tmp.shape[0] ,q_tmp.shape[1] * q_tmp.shape[2] ) _a : Tuple = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :] ) _a : int = v_tmp.reshape(v_tmp.shape[0] ,v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Union[str, Any] ,__a : List[Any] ,__a : Any=False ) -> Any: """simple docstring""" if split_mlp_wi: _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :] _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :] _a : List[str] = (wi_a, wi_a) else: _a : List[str] = params[F"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :] _a : Optional[int] = params[F"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :] return wi, wo def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Union[str, Any] ,__a : str ) -> List[str]: """simple docstring""" return params[F"""{prefix}/{prefix}/{layer_name}/scale"""][:, i] def __UpperCAmelCase ( __a : dict ,*, __a : int ,__a : bool ,__a : bool = False ) -> Any: """simple docstring""" _a : Dict = traverse_util.flatten_dict(variables['''target'''] ) _a : Any = {'''/'''.join(__a ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi _a : Optional[int] = '''encoder/encoder/mlp/wi_0/kernel''' in old print('''Split MLP:''' ,__a ) _a : Tuple = collections.OrderedDict() # Shared embeddings. _a : Any = old['''token_embedder/embedding'''] # Encoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Optional[Any] = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_attention_layer_norm''' ) _a , _a , _a , _a : List[str] = tax_attention_lookup(__a ,__a ,'''encoder''' ,'''attention''' ) _a : List[str] = layer_norm _a : Optional[Any] = k.T _a : str = o.T _a : List[Any] = q.T _a : Tuple = v.T # Block i, layer 1 (MLP). _a : str = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Any = tax_mlp_lookup(__a ,__a ,'''encoder''' ,__a ) _a : str = layer_norm if split_mlp_wi: _a : List[Any] = wi[0].T _a : Any = wi[1].T else: _a : Any = wi.T _a : Optional[Any] = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Dict = tax_relpos_bias_lookup( __a ,__a ,'''encoder''' ).T _a : List[str] = old['''encoder/encoder_norm/scale'''] if not scalable_attention: _a : List[Any] = tax_relpos_bias_lookup( __a ,0 ,'''encoder''' ).T _a : Optional[Any] = tax_relpos_bias_lookup( __a ,0 ,'''decoder''' ).T if not is_encoder_only: # Decoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Union[str, Any] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_self_attention_layer_norm''' ) _a , _a , _a , _a : Optional[Any] = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''self_attention''' ) _a : Optional[Any] = layer_norm _a : Dict = k.T _a : str = o.T _a : str = q.T _a : List[str] = v.T # Block i, layer 1 (Cross Attention). _a : Any = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_cross_attention_layer_norm''' ) _a , _a , _a , _a : str = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''encoder_decoder_attention''' ) _a : Optional[Any] = layer_norm _a : Optional[int] = k.T _a : Dict = o.T _a : str = q.T _a : int = v.T # Block i, layer 2 (MLP). _a : Optional[int] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Tuple = tax_mlp_lookup(__a ,__a ,'''decoder''' ,__a ) _a : Optional[Any] = layer_norm if split_mlp_wi: _a : List[str] = wi[0].T _a : List[Any] = wi[1].T else: _a : Dict = wi.T _a : str = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Tuple = tax_relpos_bias_lookup(__a ,__a ,'''decoder''' ).T _a : Tuple = old['''decoder/decoder_norm/scale'''] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: _a : Any = old['''decoder/logits_dense/kernel'''].T return new def __UpperCAmelCase ( __a : Dict ,__a : bool ) -> Tuple: """simple docstring""" _a : Tuple = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: _a : Any = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: _a : Optional[int] = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''' ) _a : str = state_dict['''shared.weight'''] return state_dict def __UpperCAmelCase ( __a : List[str] ,__a : Union[str, Any] ,__a : Dict ,__a : Union[str, Any] ,__a : List[Any] ) -> int: """simple docstring""" _a : List[str] = checkpoints.load_tax_checkpoint(__a ) _a : str = convert_tax_to_pytorch( __a ,num_layers=config.num_layers ,is_encoder_only=__a ,scalable_attention=__a ) _a : str = make_state_dict(__a ,__a ) model.load_state_dict(__a ,strict=__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : Any ,__a : Union[str, Any] ,__a : bool = False ,__a : bool = False ,) -> Optional[Any]: """simple docstring""" _a : List[str] = MTaConfig.from_json_file(__a ) print(F"""Building PyTorch model from configuration: {config}""" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: _a : Any = UMTaEncoderModel(__a ) else: _a : Tuple = UMTaForConditionalGeneration(__a ) # Load weights from tf checkpoint load_tax_weights_in_ta(__a ,__a ,__a ,__a ,__a ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(__a ) # Verify that we can load the checkpoint. model.from_pretrained(__a ) print('''Done''' ) if __name__ == "__main__": a__ = argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''') # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained T5 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.''' ) parser.add_argument( '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False ) parser.add_argument( '''--scalable_attention''', action='''store_true''', help='''Whether the model uses scaled attention (umt5 model)''', default=False, ) a__ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
14
1
import os import tempfile import unittest import uuid from pathlib import Path from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available if is_torch_available(): import torch if is_soundfile_availble(): import soundfile as sf if is_vision_available(): from PIL import Image def __UpperCAmelCase ( __a : Dict="" ) -> str: """simple docstring""" _a : int = tempfile.mkdtemp() return os.path.join(__a ,str(uuid.uuida() ) + suffix ) @require_soundfile @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> Tuple: _a : List[Any] = torch.rand(1_2 , dtype=torch.floataa ) - 0.5 _a : Tuple = AgentAudio(_a ) _a : List[Any] = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(_a , agent_type.to_raw() , atol=1e-4 ) ) del agent_type # Ensure the path remains even after the object deletion self.assertTrue(os.path.exists(_a ) ) # Ensure that the file contains the same value as the original tensor _a , _a : Dict = sf.read(_a ) self.assertTrue(torch.allclose(_a , torch.tensor(_a ) , atol=1e-4 ) ) def __lowercase ( self ) -> Optional[int]: _a : Dict = torch.rand(1_2 , dtype=torch.floataa ) - 0.5 _a : str = get_new_path(suffix='''.wav''' ) sf.write(_a , _a , 1_6_0_0_0 ) _a : int = AgentAudio(_a ) self.assertTrue(torch.allclose(_a , agent_type.to_raw() , atol=1e-4 ) ) self.assertEqual(agent_type.to_string() , _a ) @require_vision @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> Optional[int]: _a : str = torch.randint(0 , 2_5_6 , (6_4, 6_4, 3) ) _a : List[str] = AgentImage(_a ) _a : Union[str, Any] = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(_a , agent_type._tensor , atol=1e-4 ) ) self.assertIsInstance(agent_type.to_raw() , Image.Image ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(_a ) ) def __lowercase ( self ) -> Tuple: _a : Tuple = Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' _a : str = Image.open(_a ) _a : str = AgentImage(_a ) self.assertTrue(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(_a ) ) def __lowercase ( self ) -> List[Any]: _a : Optional[Any] = Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' _a : Union[str, Any] = Image.open(_a ) _a : Tuple = AgentImage(_a ) self.assertFalse(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(_a ) ) class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : List[Any] = '''Hey!''' _a : Dict = AgentText(_a ) self.assertEqual(_a , agent_type.to_string() ) self.assertEqual(_a , agent_type.to_raw() ) self.assertEqual(_a , _a )
14
import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap a__ = '''Usage of script: script_name <size_of_canvas:int>''' a__ = [0] * 100 + [1] * 10 random.shuffle(choice) def __UpperCAmelCase ( __a : int ) -> list[list[bool]]: """simple docstring""" _a : int = [[False for i in range(__a )] for j in range(__a )] return canvas def __UpperCAmelCase ( __a : list[list[bool]] ) -> None: """simple docstring""" for i, row in enumerate(__a ): for j, _ in enumerate(__a ): _a : Optional[int] = bool(random.getrandbits(1 ) ) def __UpperCAmelCase ( __a : list[list[bool]] ) -> list[list[bool]]: """simple docstring""" _a : Any = np.array(__a ) _a : Optional[int] = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(__a ): for c, pt in enumerate(__a ): _a : Tuple = __judge_point( __a ,current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) _a : List[str] = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. _a : list[list[bool]] = current_canvas.tolist() return return_canvas def __UpperCAmelCase ( __a : bool ,__a : list[list[bool]] ) -> bool: """simple docstring""" _a : Optional[Any] = 0 _a : str = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. _a : Optional[int] = pt if pt: if alive < 2: _a : Dict = False elif alive == 2 or alive == 3: _a : Optional[Any] = True elif alive > 3: _a : str = False else: if alive == 3: _a : int = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) a__ = int(sys.argv[1]) # main working structure of this module. a__ = create_canvas(canvas_size) seed(c) a__ , a__ = plt.subplots() fig.show() a__ = ListedColormap(['''w''', '''k''']) try: while True: a__ = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
14
1
def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" _a : Optional[int] = [int(__a ) for i in ip_va_address.split('''.''' ) if i.isdigit()] return len(__a ) == 4 and all(0 <= int(__a ) <= 254 for octet in octets ) if __name__ == "__main__": a__ = input().strip() a__ = '''valid''' if is_ip_va_address_valid(ip) else '''invalid''' print(f'''{ip} is a {valid_or_invalid} IP v4 address.''')
14
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''', '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''', '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''', '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''', '''funnel-transformer/intermediate''': ( '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json''' ), '''funnel-transformer/intermediate-base''': ( '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json''' ), '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''', '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''', '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''', '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''', } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = "funnel" UpperCAmelCase__ : Tuple = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self , _a=3_0_5_2_2 , _a=[4, 4, 4] , _a=None , _a=2 , _a=7_6_8 , _a=1_2 , _a=6_4 , _a=3_0_7_2 , _a="gelu_new" , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=None , _a=1e-9 , _a="mean" , _a="relative_shift" , _a=True , _a=True , _a=True , **_a , ) -> List[Any]: _a : Optional[int] = vocab_size _a : Dict = block_sizes _a : Optional[int] = [1] * len(_a ) if block_repeats is None else block_repeats assert len(_a ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." _a : int = num_decoder_layers _a : List[str] = d_model _a : Optional[Any] = n_head _a : Tuple = d_head _a : Dict = d_inner _a : List[str] = hidden_act _a : int = hidden_dropout _a : Union[str, Any] = attention_dropout _a : Tuple = activation_dropout _a : Optional[Any] = initializer_range _a : Dict = initializer_std _a : Union[str, Any] = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported.""" _a : Any = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported.""" _a : Optional[Any] = attention_type _a : int = separate_cls _a : Tuple = truncate_seq _a : List[Any] = pool_q_only super().__init__(**_a ) @property def __lowercase ( self ) -> Tuple: return sum(self.block_sizes ) @num_hidden_layers.setter def __lowercase ( self , _a ) -> List[str]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.''' ) @property def __lowercase ( self ) -> Optional[int]: return len(self.block_sizes ) @num_blocks.setter def __lowercase ( self , _a ) -> Dict: raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.''' )
14
1
import numpy as np import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel from ...utils import logging a__ = logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : List[Any] = CLIPConfig UpperCAmelCase__ : List[Any] = ["CLIPEncoderLayer"] def __init__( self , _a ) -> List[Any]: super().__init__(_a ) _a : List[str] = CLIPVisionModelWithProjection(config.vision_config ) _a : Any = nn.Linear(config.vision_config.projection_dim , 1 ) _a : Any = nn.Linear(config.vision_config.projection_dim , 1 ) @torch.no_grad() def __lowercase ( self , _a , _a , _a=0.5 , _a=0.5 ) -> Any: _a : Dict = self.vision_model(_a )[0] _a : Union[str, Any] = self.p_head(_a ) _a : str = nsfw_detected.flatten() _a : Optional[int] = nsfw_detected > p_threshold _a : Dict = nsfw_detected.tolist() if any(_a ): logger.warning( '''Potential NSFW content was detected in one or more images. A black image will be returned instead.''' ''' Try again with a different prompt and/or seed.''' ) for idx, nsfw_detected_ in enumerate(_a ): if nsfw_detected_: _a : Union[str, Any] = np.zeros(images[idx].shape ) _a : str = self.w_head(_a ) _a : Dict = watermark_detected.flatten() _a : Optional[Any] = watermark_detected > w_threshold _a : str = watermark_detected.tolist() if any(_a ): logger.warning( '''Potential watermarked content was detected in one or more images. A black image will be returned instead.''' ''' Try again with a different prompt and/or seed.''' ) for idx, watermark_detected_ in enumerate(_a ): if watermark_detected_: _a : Dict = np.zeros(images[idx].shape ) return images, nsfw_detected, watermark_detected
14
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : int = "mobilenet_v1" def __init__( self , _a=3 , _a=2_2_4 , _a=1.0 , _a=8 , _a="relu6" , _a=True , _a=0.999 , _a=0.02 , _a=0.001 , **_a , ) -> List[Any]: super().__init__(**_a ) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''' ) _a : Tuple = num_channels _a : str = image_size _a : Tuple = depth_multiplier _a : Any = min_depth _a : int = hidden_act _a : Optional[Any] = tf_padding _a : str = classifier_dropout_prob _a : Optional[int] = initializer_range _a : Any = layer_norm_eps class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = version.parse("1.11" ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([('''pixel_values''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})] ) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> float: return 1e-4
14
1
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import SegformerImageProcessor, SwinConfig, UperNetConfig, UperNetForSemanticSegmentation def __UpperCAmelCase ( __a : List[str] ) -> Tuple: """simple docstring""" _a : Dict = 384 _a : List[Any] = 7 if "tiny" in model_name: _a : Union[str, Any] = 96 _a : Any = (2, 2, 6, 2) _a : Union[str, Any] = (3, 6, 12, 24) elif "small" in model_name: _a : Optional[int] = 96 _a : Tuple = (2, 2, 18, 2) _a : Optional[int] = (3, 6, 12, 24) elif "base" in model_name: _a : Optional[Any] = 128 _a : Any = (2, 2, 18, 2) _a : Optional[Any] = (4, 8, 16, 32) _a : Dict = 12 _a : Union[str, Any] = 512 elif "large" in model_name: _a : Any = 192 _a : Any = (2, 2, 18, 2) _a : Optional[int] = (6, 12, 24, 48) _a : Optional[Any] = 12 _a : Tuple = 768 # set label information _a : str = 150 _a : int = '''huggingface/label-files''' _a : Tuple = '''ade20k-id2label.json''' _a : List[Any] = json.load(open(hf_hub_download(__a ,__a ,repo_type='''dataset''' ) ,'''r''' ) ) _a : List[Any] = {int(__a ): v for k, v in idalabel.items()} _a : List[Any] = {v: k for k, v in idalabel.items()} _a : str = SwinConfig( embed_dim=__a ,depths=__a ,num_heads=__a ,window_size=__a ,out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ,) _a : Tuple = UperNetConfig( backbone_config=__a ,auxiliary_in_channels=__a ,num_labels=__a ,idalabel=__a ,labelaid=__a ,) return config def __UpperCAmelCase ( __a : int ) -> Optional[Any]: """simple docstring""" _a : Union[str, Any] = [] # fmt: off # stem rename_keys.append(('''backbone.patch_embed.projection.weight''', '''backbone.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.projection.bias''', '''backbone.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''backbone.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''backbone.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm1.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm1.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_bias_table""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_index""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm2.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.norm2.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.1.weight""", F"""backbone.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.stages.{i}.blocks.{j}.ffn.layers.1.bias""", F"""backbone.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.stages.{i}.downsample.reduction.weight""", F"""backbone.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.stages.{i}.downsample.norm.weight""", F"""backbone.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.stages.{i}.downsample.norm.bias""", F"""backbone.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""backbone.hidden_states_norms.stage{i+1}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""backbone.hidden_states_norms.stage{i+1}.bias""") ) # decode head rename_keys.extend( [ ('''decode_head.conv_seg.weight''', '''decode_head.classifier.weight'''), ('''decode_head.conv_seg.bias''', '''decode_head.classifier.bias'''), ('''auxiliary_head.conv_seg.weight''', '''auxiliary_head.classifier.weight'''), ('''auxiliary_head.conv_seg.bias''', '''auxiliary_head.classifier.bias'''), ] ) # fmt: on return rename_keys def __UpperCAmelCase ( __a : int ,__a : int ,__a : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" _a : str = dct.pop(__a ) _a : Dict = val def __UpperCAmelCase ( __a : Dict ,__a : str ) -> Dict: """simple docstring""" _a : List[Any] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _a : int = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _a : Optional[int] = state_dict.pop(F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.weight""" ) _a : Dict = state_dict.pop(F"""backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _a : int = in_proj_weight[:dim, :] _a : Any = in_proj_bias[: dim] _a : Tuple = in_proj_weight[ dim : dim * 2, : ] _a : Any = in_proj_bias[ dim : dim * 2 ] _a : List[Any] = in_proj_weight[ -dim :, : ] _a : Any = in_proj_bias[-dim :] # fmt: on def __UpperCAmelCase ( __a : str ) -> str: """simple docstring""" _a , _a : List[str] = x.shape _a : Any = x.reshape(__a ,4 ,in_channel // 4 ) _a : int = x[:, [0, 2, 1, 3], :].transpose(1 ,2 ).reshape(__a ,__a ) return x def __UpperCAmelCase ( __a : Tuple ) -> Optional[Any]: """simple docstring""" _a , _a : Optional[int] = x.shape _a : List[str] = x.reshape(__a ,in_channel // 4 ,4 ) _a : Dict = x[:, :, [0, 2, 1, 3]].transpose(1 ,2 ).reshape(__a ,__a ) return x def __UpperCAmelCase ( __a : List[Any] ) -> Dict: """simple docstring""" _a : int = x.shape[0] _a : int = x.reshape(4 ,in_channel // 4 ) _a : int = x[[0, 2, 1, 3], :].transpose(0 ,1 ).reshape(__a ) return x def __UpperCAmelCase ( __a : List[Any] ) -> List[Any]: """simple docstring""" _a : str = x.shape[0] _a : Union[str, Any] = x.reshape(in_channel // 4 ,4 ) _a : List[str] = x[:, [0, 2, 1, 3]].transpose(0 ,1 ).reshape(__a ) return x def __UpperCAmelCase ( __a : Dict ,__a : Optional[Any] ,__a : Any ) -> str: """simple docstring""" _a : Dict = { '''upernet-swin-tiny''': '''https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210531_112542-e380ad3e.pth''', '''upernet-swin-small''': '''https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210526_192015-ee2fff1c.pth''', '''upernet-swin-base''': '''https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K_20210531_125459-429057bf.pth''', '''upernet-swin-large''': '''https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k_20220318_091743-9ba68901.pth''', } _a : Any = model_name_to_url[model_name] _a : Union[str, Any] = torch.hub.load_state_dict_from_url(__a ,map_location='''cpu''' ,file_name=__a )[ '''state_dict''' ] for name, param in state_dict.items(): print(__a ,param.shape ) _a : Union[str, Any] = get_upernet_config(__a ) _a : Optional[int] = UperNetForSemanticSegmentation(__a ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): _a : Any = state_dict.pop(__a ) if "bn" in key: _a : Optional[int] = key.replace('''bn''' ,'''batch_norm''' ) _a : Any = val # rename keys _a : str = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a ,__a ,__a ) read_in_q_k_v(__a ,config.backbone_config ) # fix downsample parameters for key, value in state_dict.items(): if "downsample" in key: if "reduction" in key: _a : List[str] = reverse_correct_unfold_reduction_order(__a ) if "norm" in key: _a : Optional[Any] = reverse_correct_unfold_norm_order(__a ) model.load_state_dict(__a ) # verify on image _a : str = '''https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg''' _a : List[str] = Image.open(requests.get(__a ,stream=__a ).raw ).convert('''RGB''' ) _a : Union[str, Any] = SegformerImageProcessor() _a : List[Any] = processor(__a ,return_tensors='''pt''' ).pixel_values with torch.no_grad(): _a : Dict = model(__a ) _a : str = outputs.logits print(logits.shape ) print('''First values of logits:''' ,logits[0, 0, :3, :3] ) # assert values if model_name == "upernet-swin-tiny": _a : Any = torch.tensor( [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]] ) elif model_name == "upernet-swin-small": _a : Any = torch.tensor( [[-7.19_21, -7.19_21, -6.95_32], [-7.19_21, -7.19_21, -6.95_32], [-7.09_08, -7.09_08, -6.85_34]] ) elif model_name == "upernet-swin-base": _a : int = torch.tensor( [[-6.58_51, -6.58_51, -6.43_30], [-6.58_51, -6.58_51, -6.43_30], [-6.47_63, -6.47_63, -6.32_54]] ) elif model_name == "upernet-swin-large": _a : Any = torch.tensor( [[-7.52_97, -7.52_97, -7.38_02], [-7.52_97, -7.52_97, -7.38_02], [-7.40_44, -7.40_44, -7.25_86]] ) print('''Logits:''' ,outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] ,__a ,atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__a ) print(F"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(__a ) if push_to_hub: print(F"""Pushing model and processor for {model_name} to hub""" ) model.push_to_hub(F"""openmmlab/{model_name}""" ) processor.push_to_hub(F"""openmmlab/{model_name}""" ) if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''upernet-swin-tiny''', type=str, choices=[f'''upernet-swin-{size}''' for size in ['''tiny''', '''small''', '''base''', '''large''']], help='''Name of the Swin + UperNet model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) a__ = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
14
a__ = '''Input must be a string of 8 numbers plus letter''' a__ = '''TRWAGMYFPDXBNJZSQVHLCKE''' def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" if not isinstance(__a ,__a ): _a : List[str] = F"""Expected string as input, found {type(__a ).__name__}""" raise TypeError(__a ) _a : List[Any] = spanish_id.replace('''-''' ,'''''' ).upper() if len(__a ) != 9: raise ValueError(__a ) try: _a : Any = int(spanish_id_clean[0:8] ) _a : str = spanish_id_clean[8] except ValueError as ex: raise ValueError(__a ) from ex if letter.isdigit(): raise ValueError(__a ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
14
1
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter a__ = logging.get_logger(__name__) a__ = {} a__ = {} a__ = {} def __UpperCAmelCase ( __a : type ,__a : Optional[str] ,__a : Optional[List[str]] = None ,) -> Any: """simple docstring""" _a : Any = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( F"""Overwriting format type '{format_type}' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})""" ) _a : Optional[Any] = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( F"""Overwriting format type alias '{alias}' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})""" ) _a : Optional[int] = format_type def __UpperCAmelCase ( __a : Exception ,__a : Optional[str] ,__a : Optional[List[str]] = None ) -> str: """simple docstring""" _a : List[str] = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): _a : int = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['''python''']) _register_formatter(ArrowFormatter, '''arrow''', aliases=['''pa''', '''pyarrow''']) _register_formatter(NumpyFormatter, '''numpy''', aliases=['''np''']) _register_formatter(PandasFormatter, '''pandas''', aliases=['''pd''']) _register_formatter(CustomFormatter, '''custom''') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, '''torch''', aliases=['''pt''', '''pytorch''']) else: a__ = ValueError('''PyTorch needs to be installed to be able to return PyTorch tensors.''') _register_unavailable_formatter(_torch_error, '''torch''', aliases=['''pt''', '''pytorch''']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, '''tensorflow''', aliases=['''tf''']) else: a__ = ValueError('''Tensorflow needs to be installed to be able to return Tensorflow tensors.''') _register_unavailable_formatter(_tf_error, '''tensorflow''', aliases=['''tf''']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, '''jax''', aliases=[]) else: a__ = ValueError('''JAX needs to be installed to be able to return JAX arrays.''') _register_unavailable_formatter(_jax_error, '''jax''', aliases=[]) def __UpperCAmelCase ( __a : Optional[str] ) -> Optional[str]: """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __UpperCAmelCase ( __a : Optional[str] ,**__a : Optional[Any] ) -> Formatter: """simple docstring""" _a : str = get_format_type_from_alias(__a ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__a ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( F"""Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got '{format_type}'""" )
14
from random import randint from tempfile import TemporaryFile import numpy as np def __UpperCAmelCase ( __a : Optional[Any] ,__a : int ,__a : Any ) -> int: """simple docstring""" _a : int = 0 if start < end: _a : Tuple = randint(__a ,__a ) _a : Tuple = a[end] _a : List[str] = a[pivot] _a : Any = temp _a , _a : Optional[int] = _in_place_partition(__a ,__a ,__a ) count += _in_place_quick_sort(__a ,__a ,p - 1 ) count += _in_place_quick_sort(__a ,p + 1 ,__a ) return count def __UpperCAmelCase ( __a : List[Any] ,__a : Tuple ,__a : Dict ) -> Dict: """simple docstring""" _a : Dict = 0 _a : Tuple = randint(__a ,__a ) _a : List[Any] = a[end] _a : str = a[pivot] _a : str = temp _a : Dict = start - 1 for index in range(__a ,__a ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value _a : int = new_pivot_index + 1 _a : Any = a[new_pivot_index] _a : Optional[int] = a[index] _a : str = temp _a : Union[str, Any] = a[new_pivot_index + 1] _a : Tuple = a[end] _a : Any = temp return new_pivot_index + 1, count a__ = TemporaryFile() a__ = 100 # 1000 elements are to be sorted a__ , a__ = 0, 1 # mean and standard deviation a__ = np.random.normal(mu, sigma, p) np.save(outfile, X) print('''The array is''') print(X) outfile.seek(0) # using the same array a__ = np.load(outfile) a__ = len(M) - 1 a__ = _in_place_quick_sort(M, 0, r) print( '''No of Comparisons for 100 elements selected from a standard normal distribution''' '''is :''' ) print(z)
14
1
import datasets a__ = '''\ @InProceedings{conneau2018xnli, author = "Conneau, Alexis and Rinott, Ruty and Lample, Guillaume and Williams, Adina and Bowman, Samuel R. and Schwenk, Holger and Stoyanov, Veselin", title = "XNLI: Evaluating Cross-lingual Sentence Representations", booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing", year = "2018", publisher = "Association for Computational Linguistics", location = "Brussels, Belgium", } ''' a__ = '''\ XNLI is a subset of a few thousand examples from MNLI which has been translated into a 14 different languages (some low-ish resource). As with MNLI, the goal is to predict textual entailment (does sentence A imply/contradict/neither sentence B) and is a classification task (given two sentences, predict one of three labels). ''' a__ = ''' Computes XNLI score which is just simple accuracy. Args: predictions: Predicted labels. references: Ground truth labels. Returns: \'accuracy\': accuracy Examples: >>> predictions = [0, 1] >>> references = [0, 1] >>> xnli_metric = datasets.load_metric("xnli") >>> results = xnli_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} ''' def __UpperCAmelCase ( __a : Optional[int] ,__a : int ) -> Optional[int]: """simple docstring""" return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), '''references''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32''' ), } ) , codebase_urls=[] , reference_urls=[] , format='''numpy''' , ) def __lowercase ( self , _a , _a ) -> Optional[Any]: return {"accuracy": simple_accuracy(_a , _a )}
14
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = MgpstrTokenizer UpperCAmelCase__ : int = False UpperCAmelCase__ : Union[str, Any] = {} UpperCAmelCase__ : List[Any] = False def __lowercase ( self ) -> Any: super().setUp() # fmt: off _a : Tuple = ['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _a : Optional[int] = dict(zip(_a , range(len(_a ) ) ) ) _a : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) def __lowercase ( self , **_a ) -> Dict: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __lowercase ( self , _a ) -> Tuple: _a : List[str] = '''tester''' _a : Optional[Any] = '''tester''' return input_text, output_text @unittest.skip('''MGP-STR always lower cases letters.''' ) def __lowercase ( self ) -> Any: pass def __lowercase ( self ) -> Any: _a : Union[str, Any] = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a : int = '''[SPECIAL_TOKEN]''' tokenizer.add_special_tokens({'''cls_token''': special_token} ) _a : Tuple = tokenizer.encode([special_token] , add_special_tokens=_a ) self.assertEqual(len(_a ) , 1 ) _a : Tuple = tokenizer.decode(_a , skip_special_tokens=_a ) self.assertTrue(special_token not in decoded ) def __lowercase ( self ) -> Tuple: _a : List[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a , _a : int = self.get_input_output_texts(_a ) _a : List[str] = tokenizer.tokenize(_a ) _a : Optional[int] = tokenizer.convert_tokens_to_ids(_a ) _a : Tuple = tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Optional[int] = tokenizer.convert_ids_to_tokens(_a ) self.assertNotEqual(len(_a ) , 0 ) _a : int = tokenizer.decode(_a ) self.assertIsInstance(_a , _a ) self.assertEqual(text_a.replace(''' ''' , '''''' ) , _a ) @unittest.skip('''MGP-STR tokenizer only handles one sequence.''' ) def __lowercase ( self ) -> List[str]: pass @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer''' ) def __lowercase ( self ) -> Optional[Any]: pass
14
1
import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline a__ = { '''n_samples''': 64, '''horizon''': 32, '''num_inference_steps''': 20, '''n_guide_steps''': 2, # can set to 0 for faster sampling, does not use value network '''scale_grad_by_std''': True, '''scale''': 0.1, '''eta''': 0.0, '''t_grad_cutoff''': 2, '''device''': '''cpu''', } if __name__ == "__main__": a__ = '''hopper-medium-v2''' a__ = gym.make(env_name) a__ = ValueGuidedRLPipeline.from_pretrained( '''bglick13/hopper-medium-v2-value-function-hor32''', env=env, ) env.seed(0) a__ = env.reset() a__ = 0 a__ = 0 a__ = 1000 a__ = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy a__ = pipeline(obs, planning_horizon=32) # execute action in environment a__ , a__ , a__ , a__ = env.step(denorm_actions) a__ = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( f'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' f''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) a__ = next_observation except KeyboardInterrupt: pass print(f'''Total reward: {total_reward}''')
14
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> List[Any]: _a : int = 0 def __lowercase ( self ) -> List[str]: _a : Dict = AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32''' ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : Optional[Any] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: # Ensure we can load the image processor from the feature extractor config with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' _a : Any = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Dict = CLIPConfig() # Create a dummy config file with image_proceesor_type _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : List[str] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally _a : Tuple = AutoImageProcessor.from_pretrained(_a ).to_dict() config_dict.pop('''image_processor_type''' ) _a : Tuple = CLIPImageProcessor(**_a ) # save in new folder model_config.save_pretrained(_a ) config.save_pretrained(_a ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) # make sure private variable is not incorrectly saved _a : Optional[int] = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Any: with self.assertRaisesRegex( _a , '''clip-base is not a local folder and is not a valid model identifier''' ): _a : Dict = AutoImageProcessor.from_pretrained('''clip-base''' ) def __lowercase ( self ) -> List[Any]: with self.assertRaisesRegex( _a , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): _a : List[str] = AutoImageProcessor.from_pretrained(_a , revision='''aaaaaa''' ) def __lowercase ( self ) -> Dict: with self.assertRaisesRegex( _a , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): _a : Optional[int] = AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowercase ( self ) -> Union[str, Any]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_a ): _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_a ): _a : Optional[Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) _a : Union[str, Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a , trust_remote_code=_a ) self.assertEqual(reloaded_image_processor.__class__.__name__ , '''NewImageProcessor''' ) def __lowercase ( self ) -> Dict: try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_a ): AutoImageProcessor.register(_a , _a ) with tempfile.TemporaryDirectory() as tmpdirname: _a : int = Path(_a ) / '''preprocessor_config.json''' _a : int = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : int = CustomImageProcessor.from_pretrained(_a ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def __lowercase ( self ) -> Union[str, Any]: class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = True try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # If remote code is not set, the default is to use local _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. _a : int = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub _a : Dict = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(not hasattr(_a , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
14
1
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / '''utils''')) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 a__ = get_tests_dir('''fixtures''') class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> Tuple: # A mock response for an HTTP head request to emulate server down _a : Optional[Any] = mock.Mock() _a : List[Any] = 5_0_0 _a : Optional[Any] = {} _a : List[Any] = HTTPError _a : Optional[Any] = {} # Download this model to make sure it's in the cache. _a : Dict = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''' , return_value=_a ) as mock_head: _a : Tuple = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # This check we did call the fake head request mock_head.assert_called() def __lowercase ( self ) -> Tuple: # This test is for deprecated behavior and can be removed in v5 _a : int = WavaVecaFeatureExtractor.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json''' ) @is_staging_test class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @classmethod def __lowercase ( cls ) -> Optional[Any]: _a : str = TOKEN HfFolder.save_token(_a ) @classmethod def __lowercase ( cls ) -> Tuple: try: delete_repo(token=cls._token , repo_id='''test-feature-extractor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-feature-extractor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-feature-extractor''' ) except HTTPError: pass def __lowercase ( self ) -> str: _a : int = WavaVecaFeatureExtractor.from_pretrained(_a ) feature_extractor.push_to_hub('''test-feature-extractor''' , use_auth_token=self._token ) _a : Optional[Any] = WavaVecaFeatureExtractor.from_pretrained(F"""{USER}/test-feature-extractor""" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( _a , repo_id='''test-feature-extractor''' , push_to_hub=_a , use_auth_token=self._token ) _a : Tuple = WavaVecaFeatureExtractor.from_pretrained(F"""{USER}/test-feature-extractor""" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) def __lowercase ( self ) -> Dict: _a : str = WavaVecaFeatureExtractor.from_pretrained(_a ) feature_extractor.push_to_hub('''valid_org/test-feature-extractor''' , use_auth_token=self._token ) _a : Dict = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( _a , repo_id='''valid_org/test-feature-extractor-org''' , push_to_hub=_a , use_auth_token=self._token ) _a : Optional[Any] = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor-org''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) def __lowercase ( self ) -> Any: CustomFeatureExtractor.register_for_auto_class() _a : Dict = CustomFeatureExtractor.from_pretrained(_a ) feature_extractor.push_to_hub('''test-dynamic-feature-extractor''' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {'''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor'''} , ) _a : Tuple = AutoFeatureExtractor.from_pretrained( F"""{USER}/test-dynamic-feature-extractor""" , trust_remote_code=_a ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , '''CustomFeatureExtractor''' )
14
from __future__ import annotations from dataclasses import dataclass @dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : float UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase__ : TreeNode | None = None def __UpperCAmelCase ( __a : TreeNode | None ) -> bool: """simple docstring""" def is_valid_tree(__a : TreeNode | None ) -> bool: if node is None: return True if not isinstance(__a ,__a ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(__a ): raise ValueError( '''Each node should be type of TreeNode and data should be float.''' ) def is_binary_search_tree_recursive_check( __a : TreeNode | None ,__a : float ,__a : float ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left ,__a ,node.data ) and is_binary_search_tree_recursive_check( node.right ,node.data ,__a ) ) return is_binary_search_tree_recursive_check(__a ,-float('''inf''' ) ,float('''inf''' ) ) if __name__ == "__main__": import doctest doctest.testmod()
14
1
a__ = '''0.21.0''' from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
14
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake a__ = numpy.array([0, 0]) a__ = numpy.array([0.5, 0.8660254]) a__ = numpy.array([1, 0]) a__ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def __UpperCAmelCase ( __a : list[numpy.ndarray] ,__a : int ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = initial_vectors for _ in range(__a ): _a : int = iteration_step(__a ) return vectors def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = [] for i, start_vector in enumerate(vectors[:-1] ): _a : str = vectors[i + 1] new_vectors.append(__a ) _a : Optional[int] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 ,60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def __UpperCAmelCase ( __a : numpy.ndarray ,__a : float ) -> numpy.ndarray: """simple docstring""" _a : Tuple = numpy.radians(__a ) _a , _a : List[Any] = numpy.cos(__a ), numpy.sin(__a ) _a : Dict = numpy.array(((c, -s), (s, c)) ) return numpy.dot(__a ,__a ) def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> None: """simple docstring""" _a : str = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() _a , _a : Optional[int] = zip(*__a ) plt.plot(__a ,__a ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() a__ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
14
1
from manim import * class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __lowercase ( self ) -> str: _a : Union[str, Any] = Rectangle(height=0.5 , width=0.5 ) _a : List[str] = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) _a : List[str] = [mem.copy() for i in range(6 )] _a : Optional[int] = [mem.copy() for i in range(6 )] _a : int = VGroup(*_a ).arrange(_a , buff=0 ) _a : List[Any] = VGroup(*_a ).arrange(_a , buff=0 ) _a : Any = VGroup(_a , _a ).arrange(_a , buff=0 ) _a : Tuple = Text('''CPU''' , font_size=2_4 ) _a : Union[str, Any] = Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) cpu.move_to([-2.5, -0.5, 0] ) self.add(_a ) _a : int = [mem.copy() for i in range(4 )] _a : List[Any] = VGroup(*_a ).arrange(_a , buff=0 ) _a : Any = Text('''GPU''' , font_size=2_4 ) _a : int = Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) gpu.move_to([-1, -1, 0] ) self.add(_a ) _a : Optional[int] = [mem.copy() for i in range(6 )] _a : Optional[Any] = VGroup(*_a ).arrange(_a , buff=0 ) _a : Any = Text('''Model''' , font_size=2_4 ) _a : str = Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) model.move_to([3, -1.0, 0] ) self.add(_a ) _a : List[Any] = [] for i, rect in enumerate(_a ): rect.set_stroke(_a ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) _a : Tuple = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(_a , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=_a ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=_a , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=_a , buff=0.0 ) self.add(_a ) cpu_targs.append(_a ) _a : Tuple = [mem.copy() for i in range(6 )] _a : List[Any] = VGroup(*_a ).arrange(_a , buff=0 ) _a : Dict = Text('''Loaded Checkpoint''' , font_size=2_4 ) _a : str = Group(_a , _a ).arrange(_a , aligned_edge=_a , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) _a : Tuple = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) _a : List[Any] = MarkupText( F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model""" , font_size=1_8 , ) key_text.move_to([-5, 2.4, 0] ) self.add(_a , _a ) _a : List[str] = MarkupText( F"""<span fgcolor='{BLUE}'>●</span> Checkpoint""" , font_size=1_8 , ) blue_text.next_to(_a , DOWN * 2.4 , aligned_edge=key_text.get_left() ) _a : List[str] = MarkupText( F"""Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>.""" , font_size=2_4 , ) step_a.move_to([2, 2, 0] ) self.play(Write(_a ) , Write(_a ) ) self.play(Write(_a , run_time=1 ) , Create(_a , run_time=1 ) ) _a : Optional[Any] = [] _a : List[Any] = [] for i, rect in enumerate(_a ): _a : List[str] = fill.copy().set_fill(_a , opacity=0.7 ) target.move_to(_a ) first_animations.append(GrowFromCenter(_a , run_time=1 ) ) _a : List[Any] = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(_a , run_time=1.5 ) ) self.play(*_a ) self.play(*_a ) self.wait()
14
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def __UpperCAmelCase ( __a : Tuple ,__a : Dict ,__a : List[str] ,__a : Optional[Any] ,__a : Tuple ) -> Dict: """simple docstring""" with open(__a ) as metadata_file: _a : Optional[Any] = json.load(__a ) _a : List[Any] = LukeConfig(use_entity_aware_attention=__a ,**metadata['''model_config'''] ) # Load in the weights from the checkpoint_path _a : Optional[Any] = torch.load(__a ,map_location='''cpu''' )['''module'''] # Load the entity vocab file _a : Any = load_original_entity_vocab(__a ) # add an entry for [MASK2] _a : Union[str, Any] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 _a : Dict = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks _a : Optional[int] = AddedToken('''<ent>''' ,lstrip=__a ,rstrip=__a ) _a : Tuple = AddedToken('''<ent2>''' ,lstrip=__a ,rstrip=__a ) 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(__a ) with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''r''' ) as f: _a : List[str] = json.load(__a ) _a : Tuple = '''MLukeTokenizer''' with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''w''' ) as f: json.dump(__a ,__a ) with open(os.path.join(__a ,MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) ,'''w''' ) as f: json.dump(__a ,__a ) _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) # Initialize the embeddings of the special tokens _a : str = tokenizer.convert_tokens_to_ids(['''@'''] )[0] _a : Tuple = tokenizer.convert_tokens_to_ids(['''#'''] )[0] _a : Any = state_dict['''embeddings.word_embeddings.weight'''] _a : Optional[int] = word_emb[ent_init_index].unsqueeze(0 ) _a : Any = word_emb[enta_init_index].unsqueeze(0 ) _a : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: _a : Tuple = state_dict[bias_name] _a : Optional[Any] = decoder_bias[ent_init_index].unsqueeze(0 ) _a : Optional[int] = decoder_bias[enta_init_index].unsqueeze(0 ) _a : Dict = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: _a : Tuple = F"""encoder.layer.{layer_index}.attention.self.""" _a : List[Any] = state_dict[prefix + matrix_name] _a : Dict = state_dict[prefix + matrix_name] _a : List[Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks _a : Union[str, Any] = state_dict['''entity_embeddings.entity_embeddings.weight'''] _a : Optional[int] = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' _a : int = state_dict['''entity_predictions.bias'''] _a : int = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Optional[Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) _a : Optional[int] = LukeForMaskedLM(config=__a ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) _a : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): _a : Optional[int] = state_dict[key] else: _a : Tuple = state_dict[key] _a , _a : int = model.load_state_dict(__a ,strict=__a ) if set(__a ) != {"luke.embeddings.position_ids"}: raise ValueError(F"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(__a ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ,task='''entity_classification''' ) _a : int = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' _a : List[Any] = (0, 9) _a : Tuple = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : int = model(**__a ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base _a : List[str] = torch.Size((1, 33, 768) ) _a : Union[str, Any] = torch.tensor([[0.08_92, 0.05_96, -0.28_19], [0.01_34, 0.11_99, 0.05_73], [-0.01_69, 0.09_27, 0.06_44]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base _a : str = torch.Size((1, 1, 768) ) _a : List[Any] = torch.tensor([[-0.14_82, 0.06_09, 0.03_22]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" F""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify masked word/entity prediction _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) _a : Dict = '''Tokyo is the capital of <mask>.''' _a : List[str] = (24, 30) _a : Optional[int] = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : Optional[Any] = model(**__a ) _a : Any = encoding['''input_ids'''][0].tolist() _a : Optional[Any] = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) _a : Any = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(__a ) _a : Any = outputs.entity_logits[0][0].argmax().item() _a : Optional[Any] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(__a ) ) model.save_pretrained(__a ) def __UpperCAmelCase ( __a : List[Any] ) -> int: """simple docstring""" _a : Union[str, Any] = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] _a : int = [json.loads(__a ) for line in open(__a )] _a : List[Any] = {} for entry in data: _a : int = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: _a : List[Any] = entity_id break _a : Dict = F"""{language}:{entity_name}""" _a : int = entity_id return new_mapping if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) a__ = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
14
1
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=__lowercase ) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) UpperCAmelCase__ : ClassVar[Features] = Features({"image": Image()} ) UpperCAmelCase__ : ClassVar[Features] = Features({"labels": ClassLabel} ) UpperCAmelCase__ : str = "image" UpperCAmelCase__ : str = "labels" def __lowercase ( self , _a ) -> 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] , _a ): raise ValueError(F"""Column {self.label_column} is not a ClassLabel.""" ) _a : Dict = copy.deepcopy(self ) _a : List[Any] = self.label_schema.copy() _a : Any = features[self.label_column] _a : int = label_schema return task_template @property def __lowercase ( self ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
14
from scipy.stats import spearmanr import datasets a__ = ''' The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. ''' a__ = ''' Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {\'spearmanr\': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results[\'spearmanr\']) -0.7 >>> print(round(results[\'spearmanr_pvalue\'], 2)) 0.19 ''' a__ = R'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'''] , ) def __lowercase ( self , _a , _a , _a=False ) -> str: _a : int = spearmanr(_a , _a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
14
1
import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging a__ = ( '''https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py''' ) a__ = logging.get_logger(__name__) # pylint: disable=invalid-name def __UpperCAmelCase ( ) -> Union[str, Any]: """simple docstring""" _a : Any = '''https://pypi.org/pypi/diffusers/json''' _a : Optional[int] = json.loads(request.urlopen(__a ).read() )['''releases'''].keys() return sorted(__a ,key=lambda __a : version.Version(__a ) ) def __UpperCAmelCase ( ) -> List[str]: """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(__a ) os.makedirs(__a ,exist_ok=__a ) _a : int = Path(__a ) / '''__init__.py''' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( __a : Union[str, os.PathLike] ) -> Optional[Any]: """simple docstring""" init_hf_modules() _a : List[Any] = Path(__a ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(__a ,exist_ok=__a ) _a : Any = dynamic_module_path / '''__init__.py''' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( __a : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" with open(__a ,'''r''' ,encoding='''utf-8''' ) as f: _a : Optional[Any] = f.read() # Imports of the form `import .xxx` _a : List[str] = re.findall('''^\s*import\s+\.(\S+)\s*$''' ,__a ,flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('''^\s*from\s+\.(\S+)\s+import''' ,__a ,flags=re.MULTILINE ) # Unique-ify return list(set(__a ) ) def __UpperCAmelCase ( __a : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" _a : Optional[int] = False _a : str = [module_file] _a : Dict = [] # Let's recurse through all relative imports while not no_change: _a : Tuple = [] for f in files_to_check: new_imports.extend(get_relative_imports(__a ) ) _a : Any = Path(__a ).parent _a : int = [str(module_path / m ) for m in new_imports] _a : str = [f for f in new_import_files if f not in all_relative_imports] _a : int = [F"""{f}.py""" for f in new_import_files] _a : int = len(__a ) == 0 all_relative_imports.extend(__a ) return all_relative_imports def __UpperCAmelCase ( __a : str ) -> Any: """simple docstring""" with open(__a ,'''r''' ,encoding='''utf-8''' ) as f: _a : List[str] = f.read() # Imports of the form `import xxx` _a : List[str] = re.findall('''^\s*import\s+(\S+)\s*$''' ,__a ,flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('''^\s*from\s+(\S+)\s+import''' ,__a ,flags=re.MULTILINE ) # Only keep the top-level module _a : int = [imp.split('''.''' )[0] for imp in imports if not imp.startswith('''.''' )] # Unique-ify and test we got them all _a : List[str] = list(set(__a ) ) _a : Tuple = [] for imp in imports: try: importlib.import_module(__a ) except ImportError: missing_packages.append(__a ) if len(__a ) > 0: raise ImportError( '''This modeling file requires the following packages that were not found in your environment: ''' F"""{', '.join(__a )}. Run `pip install {' '.join(__a )}`""" ) return get_relative_imports(__a ) def __UpperCAmelCase ( __a : Optional[int] ,__a : Optional[Any] ) -> Optional[int]: """simple docstring""" _a : Any = module_path.replace(os.path.sep ,'''.''' ) _a : Any = importlib.import_module(__a ) if class_name is None: return find_pipeline_class(__a ) return getattr(__a ,__a ) def __UpperCAmelCase ( __a : Any ) -> List[Any]: """simple docstring""" from ..pipelines import DiffusionPipeline _a : Tuple = dict(inspect.getmembers(__a ,inspect.isclass ) ) _a : Optional[int] = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls ,__a ) and cls.__module__.split('''.''' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F"""Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:""" F""" {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in""" F""" {loaded_module}.""" ) _a : Optional[int] = cls return pipeline_class def __UpperCAmelCase ( __a : Union[str, os.PathLike] ,__a : str ,__a : Optional[Union[str, os.PathLike]] = None ,__a : bool = False ,__a : bool = False ,__a : Optional[Dict[str, str]] = None ,__a : Optional[Union[bool, str]] = None ,__a : Optional[str] = None ,__a : bool = False ,) -> Optional[Any]: """simple docstring""" _a : List[Any] = str(__a ) _a : Tuple = os.path.join(__a ,__a ) if os.path.isfile(__a ): _a : List[Any] = module_file_or_url _a : str = '''local''' elif pretrained_model_name_or_path.count('''/''' ) == 0: _a : int = get_diffusers_versions() # cut ".dev0" _a : Optional[Any] = '''v''' + '''.'''.join(__version__.split('''.''' )[:3] ) # retrieve github version that matches if revision is None: _a : Union[str, Any] = latest_version if latest_version[1:] in available_versions else '''main''' logger.info(F"""Defaulting to latest_version: {revision}.""" ) elif revision in available_versions: _a : Dict = F"""v{revision}""" elif revision == "main": _a : Dict = revision else: raise ValueError( F"""`custom_revision`: {revision} does not exist. Please make sure to choose one of""" F""" {', '.join(available_versions + ['main'] )}.""" ) # community pipeline on GitHub _a : Union[str, Any] = COMMUNITY_PIPELINES_URL.format(revision=__a ,pipeline=__a ) try: _a : int = cached_download( __a ,cache_dir=__a ,force_download=__a ,proxies=__a ,resume_download=__a ,local_files_only=__a ,use_auth_token=__a ,) _a : Dict = '''git''' _a : str = pretrained_model_name_or_path + '''.py''' except EnvironmentError: logger.error(F"""Could not locate the {module_file} inside {pretrained_model_name_or_path}.""" ) raise else: try: # Load from URL or cache if already cached _a : Optional[Any] = hf_hub_download( __a ,__a ,cache_dir=__a ,force_download=__a ,proxies=__a ,resume_download=__a ,local_files_only=__a ,use_auth_token=__a ,) _a : Union[str, Any] = os.path.join('''local''' ,'''--'''.join(pretrained_model_name_or_path.split('''/''' ) ) ) except EnvironmentError: logger.error(F"""Could not locate the {module_file} inside {pretrained_model_name_or_path}.""" ) raise # Check we have all the requirements in our environment _a : Dict = check_imports(__a ) # Now we move the module inside our cached dynamic modules. _a : int = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(__a ) _a : Tuple = Path(__a ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(__a ,submodule_path / module_file ) for module_needed in modules_needed: _a : Any = F"""{module_needed}.py""" shutil.copy(os.path.join(__a ,__a ) ,submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(__a ,__a ): _a : List[str] = use_auth_token elif use_auth_token is True: _a : Optional[int] = HfFolder.get_token() else: _a : str = None _a : List[Any] = model_info(__a ,revision=__a ,token=__a ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. _a : List[str] = submodule_path / commit_hash _a : Tuple = full_submodule + os.path.sep + commit_hash create_dynamic_module(__a ) if not (submodule_path / module_file).exists(): shutil.copy(__a ,submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( __a ,F"""{module_needed}.py""" ,cache_dir=__a ,force_download=__a ,resume_download=__a ,proxies=__a ,use_auth_token=__a ,revision=__a ,local_files_only=__a ,) return os.path.join(__a ,__a ) def __UpperCAmelCase ( __a : Union[str, os.PathLike] ,__a : str ,__a : Optional[str] = None ,__a : Optional[Union[str, os.PathLike]] = None ,__a : bool = False ,__a : bool = False ,__a : Optional[Dict[str, str]] = None ,__a : Optional[Union[bool, str]] = None ,__a : Optional[str] = None ,__a : bool = False ,**__a : Tuple ,) -> int: """simple docstring""" _a : Any = get_cached_module_file( __a ,__a ,cache_dir=__a ,force_download=__a ,resume_download=__a ,proxies=__a ,use_auth_token=__a ,revision=__a ,local_files_only=__a ,) return get_class_in_module(__a ,final_module.replace('''.py''' ,'''''' ) )
14
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def __UpperCAmelCase ( __a : bytes ,__a : int ) -> np.array: """simple docstring""" _a : int = F"""{sampling_rate}""" _a : str = '''1''' _a : Optional[int] = '''f32le''' _a : Optional[Any] = [ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(__a ,stdin=subprocess.PIPE ,stdout=subprocess.PIPE ) as ffmpeg_process: _a : Any = ffmpeg_process.communicate(__a ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error _a : Optional[Any] = output_stream[0] _a : Optional[int] = np.frombuffer(__a ,np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def __UpperCAmelCase ( __a : int ,__a : float ,__a : str = "f32le" ,) -> str: """simple docstring""" _a : Dict = F"""{sampling_rate}""" _a : Optional[Any] = '''1''' if format_for_conversion == "s16le": _a : Dict = 2 elif format_for_conversion == "f32le": _a : Optional[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) _a : Dict = platform.system() if system == "Linux": _a : Dict = '''alsa''' _a : Union[str, Any] = '''default''' elif system == "Darwin": _a : Union[str, Any] = '''avfoundation''' _a : List[str] = ''':0''' elif system == "Windows": _a : Optional[int] = '''dshow''' _a : str = '''default''' _a : Tuple = [ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] _a : Any = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample _a : str = _ffmpeg_stream(__a ,__a ) for item in iterator: yield item def __UpperCAmelCase ( __a : int ,__a : float ,__a : Optional[int] = None ,__a : Optional[Union[Tuple[float, float], float]] = None ,__a : str = "f32le" ,) -> Optional[int]: """simple docstring""" if stream_chunk_s is not None: _a : Tuple = stream_chunk_s else: _a : Tuple = chunk_length_s _a : Tuple = ffmpeg_microphone(__a ,__a ,format_for_conversion=__a ) if format_for_conversion == "s16le": _a : Any = np.intaa _a : Optional[int] = 2 elif format_for_conversion == "f32le": _a : Dict = np.floataa _a : List[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) if stride_length_s is None: _a : List[Any] = chunk_length_s / 6 _a : Optional[int] = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__a ,(int, float) ): _a : Optional[Any] = [stride_length_s, stride_length_s] _a : Optional[Any] = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample _a : str = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample _a : Optional[Any] = datetime.datetime.now() _a : Tuple = datetime.timedelta(seconds=__a ) for item in chunk_bytes_iter(__a ,__a ,stride=(stride_left, stride_right) ,stream=__a ): # Put everything back in numpy scale _a : Dict = np.frombuffer(item['''raw'''] ,dtype=__a ) _a : Dict = ( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) _a : str = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def __UpperCAmelCase ( __a : Optional[int] ,__a : int ,__a : Tuple[int, int] ,__a : bool = False ) -> Optional[int]: """simple docstring""" _a : Any = b'''''' _a , _a : List[str] = stride if stride_left + stride_right >= chunk_len: raise ValueError( F"""Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}""" ) _a : List[str] = 0 for raw in iterator: acc += raw if stream and len(__a ) < chunk_len: _a : Dict = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__a ) >= chunk_len: # We are flushing the accumulator _a : List[str] = (_stride_left, stride_right) _a : List[Any] = {'''raw''': acc[:chunk_len], '''stride''': stride} if stream: _a : List[Any] = False yield item _a : Optional[Any] = stride_left _a : Optional[Any] = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__a ) > stride_left: _a : Optional[Any] = {'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: _a : Dict = False yield item def __UpperCAmelCase ( __a : int ,__a : int ) -> Tuple: """simple docstring""" _a : Dict = 2**24 # 16Mo try: with subprocess.Popen(__a ,stdout=subprocess.PIPE ,bufsize=__a ) as ffmpeg_process: while True: _a : int = ffmpeg_process.stdout.read(__a ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
14
1
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class UpperCAmelCase_ : """simple docstring""" def __lowercase ( self ) -> Tuple: torch.manual_seed(0 ) _a : int = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) _a : Tuple = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) _a : List[str] = UNetaDConditionModel( sample_size=3_2 , layers_per_block=1 , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=3 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) _a : Union[str, Any] = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0001 , beta_end=0.02 , thresholding=_a , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) _a : List[Any] = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def __lowercase ( self ) -> int: torch.manual_seed(0 ) _a : str = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) _a : Any = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) _a : str = UNetaDConditionModel( sample_size=3_2 , layers_per_block=[1, 2] , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=6 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , class_embed_type='''timestep''' , mid_block_scale_factor=1.414 , time_embedding_act_fn='''gelu''' , time_embedding_dim=3_2 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) _a : List[Any] = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0001 , beta_end=0.02 , thresholding=_a , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) _a : Dict = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.0001 , beta_end=0.02 , ) torch.manual_seed(0 ) _a : int = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def __lowercase ( self ) -> List[Any]: _a : Optional[int] = self.get_dummy_components() _a : Optional[Any] = self.pipeline_class(**_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _a : List[str] = self.get_dummy_inputs(_a ) _a : Any = inputs['''prompt'''] _a : Any = inputs['''generator'''] _a : Tuple = inputs['''num_inference_steps'''] _a : List[Any] = inputs['''output_type'''] if "image" in inputs: _a : Optional[int] = inputs['''image'''] else: _a : List[Any] = None if "mask_image" in inputs: _a : List[str] = inputs['''mask_image'''] else: _a : str = None if "original_image" in inputs: _a : Tuple = inputs['''original_image'''] else: _a : List[str] = None _a , _a : int = pipe.encode_prompt(_a ) # inputs with prompt converted to embeddings _a : Dict = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: _a : List[Any] = image if mask_image is not None: _a : Tuple = mask_image if original_image is not None: _a : Tuple = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(_a , _a , _a ) _a : Union[str, Any] = pipe(**_a )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_a ) _a : List[Any] = self.pipeline_class.from_pretrained(_a ) pipe_loaded.to(_a ) pipe_loaded.set_progress_bar_config(disable=_a ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(_a , _a ) is None , F"""`{optional_component}` did not stay set to None after loading.""" , ) _a : Dict = self.get_dummy_inputs(_a ) _a : str = inputs['''generator'''] _a : Optional[Any] = inputs['''num_inference_steps'''] _a : List[str] = inputs['''output_type'''] # inputs with prompt converted to embeddings _a : int = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: _a : List[Any] = image if mask_image is not None: _a : Optional[int] = mask_image if original_image is not None: _a : str = original_image _a : List[str] = pipe_loaded(**_a )[0] _a : Dict = np.abs(to_np(_a ) - to_np(_a ) ).max() self.assertLess(_a , 1e-4 ) def __lowercase ( self ) -> int: _a : List[Any] = self.get_dummy_components() _a : int = self.pipeline_class(**_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _a : Optional[Any] = self.get_dummy_inputs(_a ) _a : Dict = pipe(**_a )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_a ) _a : int = self.pipeline_class.from_pretrained(_a ) pipe_loaded.to(_a ) pipe_loaded.set_progress_bar_config(disable=_a ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests _a : List[str] = self.get_dummy_inputs(_a ) _a : Optional[Any] = pipe_loaded(**_a )[0] _a : Union[str, Any] = np.abs(to_np(_a ) - to_np(_a ) ).max() self.assertLess(_a , 1e-4 )
14
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = KandinskyInpaintPipeline UpperCAmelCase__ : Optional[int] = ["prompt", "image_embeds", "negative_image_embeds", "image", "mask_image"] UpperCAmelCase__ : Optional[Any] = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", "mask_image", ] UpperCAmelCase__ : Optional[int] = [ "generator", "height", "width", "latents", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase__ : Any = False @property def __lowercase ( self ) -> Optional[int]: return 3_2 @property def __lowercase ( self ) -> int: return 3_2 @property def __lowercase ( self ) -> List[str]: return self.time_input_dim @property def __lowercase ( self ) -> List[str]: return self.time_input_dim * 4 @property def __lowercase ( self ) -> Optional[Any]: return 1_0_0 @property def __lowercase ( self ) -> Optional[Any]: _a : Any = XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[Any] = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) _a : Optional[int] = MultilingualCLIP(_a ) _a : Tuple = text_encoder.eval() return text_encoder @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[str] = { '''in_channels''': 9, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _a : Dict = UNetaDConditionModel(**_a ) return model @property def __lowercase ( self ) -> Optional[int]: return { "block_out_channels": [3_2, 6_4], "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": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowercase ( self ) -> Tuple: torch.manual_seed(0 ) _a : List[Any] = VQModel(**self.dummy_movq_kwargs ) return model def __lowercase ( self ) -> Any: _a : List[Any] = self.dummy_text_encoder _a : Optional[Any] = self.dummy_tokenizer _a : Optional[Any] = self.dummy_unet _a : Union[str, Any] = self.dummy_movq _a : Tuple = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''linear''' , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=_a , set_alpha_to_one=_a , steps_offset=1 , prediction_type='''epsilon''' , thresholding=_a , ) _a : str = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __lowercase ( self , _a , _a=0 ) -> int: _a : Union[str, Any] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _a : List[str] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _a : Tuple = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(_a ) ).to(_a ) _a : Dict = image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : Optional[int] = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) # create mask _a : Union[str, Any] = np.ones((6_4, 6_4) , dtype=np.floataa ) _a : List[str] = 0 if str(_a ).startswith('''mps''' ): _a : Tuple = torch.manual_seed(_a ) else: _a : Any = torch.Generator(device=_a ).manual_seed(_a ) _a : Any = { '''prompt''': '''horse''', '''image''': init_image, '''mask_image''': mask, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 2, '''guidance_scale''': 4.0, '''output_type''': '''np''', } return inputs def __lowercase ( self ) -> Optional[Any]: _a : Optional[Any] = '''cpu''' _a : List[Any] = self.get_dummy_components() _a : Tuple = self.pipeline_class(**_a ) _a : int = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _a : Any = pipe(**self.get_dummy_inputs(_a ) ) _a : str = output.images _a : Tuple = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _a : Union[str, Any] = image[0, -3:, -3:, -1] _a : Tuple = image_from_tuple[0, -3:, -3:, -1] print(F"""image.shape {image.shape}""" ) assert image.shape == (1, 6_4, 6_4, 3) _a : str = np.array( [0.832_6919, 0.7379_0467, 0.2091_8581, 0.930_9612, 0.551_1791, 0.4371_3328, 0.551_3321, 0.4992_2934, 0.5949_7786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def __lowercase ( self ) -> Dict: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowercase ( self ) -> Union[str, Any]: _a : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy''' ) _a : str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _a : Tuple = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) _a : Any = 0 _a : Optional[Any] = '''a hat''' _a : Optional[Any] = KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _a : Tuple = KandinskyInpaintPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-inpaint''' , torch_dtype=torch.floataa ) _a : Union[str, Any] = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _a : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) _a , _a : Dict = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _a : Optional[int] = pipeline( _a , image=_a , mask_image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type='''np''' , ) _a : Optional[int] = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(_a , _a )
14
1
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/config.json''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/config.json''', } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = "xlnet" UpperCAmelCase__ : Dict = ["mems"] UpperCAmelCase__ : str = { "n_token": "vocab_size", # Backward compatibility "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self , _a=3_2_0_0_0 , _a=1_0_2_4 , _a=2_4 , _a=1_6 , _a=4_0_9_6 , _a="gelu" , _a=True , _a="bi" , _a=0.02 , _a=1e-1_2 , _a=0.1 , _a=5_1_2 , _a=None , _a=True , _a=False , _a=False , _a=-1 , _a=False , _a="last" , _a=True , _a="tanh" , _a=0.1 , _a=5 , _a=5 , _a=5 , _a=1 , _a=2 , **_a , ) -> int: _a : Any = vocab_size _a : Any = d_model _a : Any = n_layer _a : Optional[Any] = n_head if d_model % n_head != 0: raise ValueError(F"""'d_model % n_head' ({d_model % n_head}) should be equal to 0""" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( F"""`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})""" ) _a : Tuple = d_model // n_head _a : int = ff_activation _a : str = d_inner _a : int = untie_r _a : List[str] = attn_type _a : Union[str, Any] = initializer_range _a : Union[str, Any] = layer_norm_eps _a : Tuple = dropout _a : Optional[int] = mem_len _a : Union[str, Any] = reuse_len _a : Dict = bi_data _a : List[str] = clamp_len _a : Tuple = same_length _a : List[Any] = summary_type _a : Tuple = summary_use_proj _a : Dict = summary_activation _a : Dict = summary_last_dropout _a : int = start_n_top _a : int = end_n_top _a : Any = bos_token_id _a : Optional[Any] = pad_token_id _a : int = eos_token_id if "use_cache" in kwargs: warnings.warn( '''The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`''' ''' instead.''' , _a , ) _a : Optional[Any] = kwargs['''use_cache'''] _a : List[str] = use_mems_eval _a : List[Any] = use_mems_train super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) @property def __lowercase ( self ) -> List[str]: 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 __lowercase ( self , _a ) -> Tuple: # Message copied from Transformer-XL documentation raise NotImplementedError( F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
14
import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_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.''' ) parser.add_argument( '''--original_config_file''', type=str, required=True, 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( '''--image_size''', default=512, 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( '''--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.)''') def __UpperCAmelCase ( __a : Any ) -> List[Any]: """simple docstring""" if string == "True": return True elif string == "False": return False else: raise ValueError(F"""could not parse string as bool {string}""" ) parser.add_argument( '''--use_linear_projection''', help='''Override for use linear projection''', required=False, type=parse_bool ) parser.add_argument('''--cross_attention_dim''', help='''Override for cross attention_dim''', required=False, type=int) a__ = parser.parse_args() a__ = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
14
1
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, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class UpperCAmelCase_ ( __lowercase , __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = StableDiffusionInpaintPipeline UpperCAmelCase__ : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS UpperCAmelCase__ : int = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS UpperCAmelCase__ : Dict = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess UpperCAmelCase__ : Optional[int] = frozenset([] ) def __lowercase ( self ) -> Tuple: torch.manual_seed(0 ) _a : Tuple = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=9 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=3_2 , attention_head_dim=(2, 4) , use_linear_projection=_a , ) _a : List[Any] = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) _a : Tuple = AutoencoderKL( 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=4 , sample_size=1_2_8 , ) torch.manual_seed(0 ) _a : List[str] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , hidden_act='''gelu''' , projection_dim=5_1_2 , ) _a : str = CLIPTextModel(_a ) _a : str = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) _a : Dict = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __lowercase ( self , _a , _a=0 ) -> List[Any]: # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched _a : Union[str, Any] = floats_tensor((1, 3, 3_2, 3_2) , rng=random.Random(_a ) ).to(_a ) _a : str = image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : Tuple = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((6_4, 6_4) ) _a : Dict = Image.fromarray(np.uinta(image + 4 ) ).convert('''RGB''' ).resize((6_4, 6_4) ) if str(_a ).startswith('''mps''' ): _a : Optional[int] = torch.manual_seed(_a ) else: _a : Dict = torch.Generator(device=_a ).manual_seed(_a ) _a : Optional[int] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': init_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __lowercase ( self ) -> List[str]: _a : int = '''cpu''' # ensure determinism for the device-dependent torch.Generator _a : List[str] = self.get_dummy_components() _a : List[str] = StableDiffusionInpaintPipeline(**_a ) _a : Tuple = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) _a : Union[str, Any] = self.get_dummy_inputs(_a ) _a : List[str] = sd_pipe(**_a ).images _a : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) _a : str = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __lowercase ( self ) -> Any: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowercase ( self ) -> List[Any]: _a : int = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _a : Dict = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _a : Any = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench.npy''' ) _a : Optional[int] = '''stabilityai/stable-diffusion-2-inpainting''' _a : Union[str, Any] = StableDiffusionInpaintPipeline.from_pretrained(_a , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() _a : int = '''Face of a yellow cat, high resolution, sitting on a park bench''' _a : List[Any] = torch.manual_seed(0 ) _a : Any = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) _a : Union[str, Any] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 9e-3 def __lowercase ( self ) -> Dict: _a : List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _a : Optional[int] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _a : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint''' '''/yellow_cat_sitting_on_a_park_bench_fp16.npy''' ) _a : Tuple = '''stabilityai/stable-diffusion-2-inpainting''' _a : Optional[int] = StableDiffusionInpaintPipeline.from_pretrained( _a , torch_dtype=torch.floataa , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() _a : List[Any] = '''Face of a yellow cat, high resolution, sitting on a park bench''' _a : Dict = torch.manual_seed(0 ) _a : Any = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , output_type='''np''' , ) _a : List[str] = output.images[0] assert image.shape == (5_1_2, 5_1_2, 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() _a : Any = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _a : List[str] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _a : List[Any] = '''stabilityai/stable-diffusion-2-inpainting''' _a : Optional[int] = PNDMScheduler.from_pretrained(_a , subfolder='''scheduler''' ) _a : Tuple = StableDiffusionInpaintPipeline.from_pretrained( _a , safety_checker=_a , scheduler=_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() _a : Tuple = '''Face of a yellow cat, high resolution, sitting on a park bench''' _a : Tuple = torch.manual_seed(0 ) _a : Dict = pipe( prompt=_a , image=_a , mask_image=_a , generator=_a , num_inference_steps=2 , output_type='''np''' , ) _a : List[str] = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 1_0**9
14
class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a , _a ) -> List[str]: _a : List[Any] = name _a : List[str] = value _a : List[str] = weight def __repr__( self ) -> Optional[int]: return F"""{self.__class__.__name__}({self.name}, {self.value}, {self.weight})""" def __lowercase ( self ) -> List[Any]: return self.value def __lowercase ( self ) -> int: return self.name def __lowercase ( self ) -> Optional[int]: return self.weight def __lowercase ( self ) -> Optional[Any]: return self.value / self.weight def __UpperCAmelCase ( __a : Optional[int] ,__a : Tuple ,__a : List[str] ) -> List[str]: """simple docstring""" _a : Optional[int] = [] for i in range(len(__a ) ): menu.append(Things(name[i] ,value[i] ,weight[i] ) ) return menu def __UpperCAmelCase ( __a : int ,__a : Union[str, Any] ,__a : int ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = sorted(__a ,key=__a ,reverse=__a ) _a : Any = [] _a , _a : Optional[int] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def __UpperCAmelCase ( ) -> int: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
14
1
import os from math import logaa def __UpperCAmelCase ( __a : str = "base_exp.txt" ) -> int: """simple docstring""" _a : float = 0 _a : Dict = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(__a ) ,__a ) ) ): _a , _a : Optional[Any] = list(map(__a ,line.split(''',''' ) ) ) if x * logaa(__a ) > largest: _a : List[str] = x * logaa(__a ) _a : Any = i + 1 return result if __name__ == "__main__": print(solution())
14
import copy import inspect import unittest from transformers import PretrainedConfig, SwiftFormerConfig from transformers.testing_utils import ( require_torch, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwiftFormerForImageClassification, SwiftFormerModel from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=1_3 , _a=3 , _a=True , _a=True , _a=0.1 , _a=0.1 , _a=2_2_4 , _a=1_0_0_0 , _a=[3, 3, 6, 4] , _a=[4_8, 5_6, 1_1_2, 2_2_0] , ) -> Tuple: _a : Dict = parent _a : Optional[int] = batch_size _a : Optional[Any] = num_channels _a : Union[str, Any] = is_training _a : Tuple = use_labels _a : Dict = hidden_dropout_prob _a : List[Any] = attention_probs_dropout_prob _a : Dict = num_labels _a : List[str] = image_size _a : Dict = layer_depths _a : str = embed_dims def __lowercase ( self ) -> Optional[Any]: _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : int = None if self.use_labels: _a : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) _a : Dict = self.get_config() return config, pixel_values, labels def __lowercase ( self ) -> int: return SwiftFormerConfig( depths=self.layer_depths , embed_dims=self.embed_dims , mlp_ratio=4 , downsamples=[True, True, True, True] , hidden_act='''gelu''' , num_labels=self.num_labels , down_patch_size=3 , down_stride=2 , down_pad=1 , drop_rate=0.0 , drop_path_rate=0.0 , use_layer_scale=_a , layer_scale_init_value=1e-5 , ) def __lowercase ( self , _a , _a , _a ) -> str: _a : List[Any] = SwiftFormerModel(config=_a ) model.to(_a ) model.eval() _a : Optional[int] = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dims[-1], 7, 7) ) def __lowercase ( self , _a , _a , _a ) -> Optional[Any]: _a : List[str] = self.num_labels _a : Optional[int] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : List[str] = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) _a : Union[str, Any] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : Optional[Any] = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowercase ( self ) -> Tuple: ((_a) , (_a) , (_a)) : Optional[int] = self.prepare_config_and_inputs() _a : List[Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else () UpperCAmelCase__ : Optional[int] = ( {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Optional[Any] = False UpperCAmelCase__ : str = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : str = False def __lowercase ( self ) -> Optional[int]: _a : Union[str, Any] = SwiftFormerModelTester(self ) _a : int = ConfigTester( self , config_class=_a , has_text_modality=_a , hidden_size=3_7 , num_attention_heads=1_2 , num_hidden_layers=1_2 , ) def __lowercase ( self ) -> int: self.config_tester.run_common_tests() @unittest.skip(reason='''SwiftFormer does not use inputs_embeds''' ) def __lowercase ( self ) -> Union[str, Any]: pass def __lowercase ( self ) -> Dict: _a , _a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Any = model_class(_a ) _a : int = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def __lowercase ( self ) -> str: _a , _a : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Optional[int] = model_class(_a ) _a : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Tuple = [*signature.parameters.keys()] _a : List[str] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowercase ( self ) -> int: _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self ) -> Optional[int]: _a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self ) -> Optional[Any]: for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Any = SwiftFormerModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason='''SwiftFormer does not output attentions''' ) def __lowercase ( self ) -> List[Any]: pass def __lowercase ( self ) -> int: def check_hidden_states_output(_a , _a , _a ): _a : Optional[int] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Union[str, Any] = model(**self._prepare_for_class(_a , _a ) ) _a : Optional[Any] = outputs.hidden_states _a : Union[str, Any] = 8 self.assertEqual(len(_a ) , _a ) # TODO # SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width) # with the width and height being successively divided by 2, after every 2 blocks for i in range(len(_a ) ): self.assertEqual( hidden_states[i].shape , torch.Size( [ self.model_tester.batch_size, self.model_tester.embed_dims[i // 2], (self.model_tester.image_size // 4) // 2 ** (i // 2), (self.model_tester.image_size // 4) // 2 ** (i // 2), ] ) , ) _a , _a : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : str = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : List[str] = True check_hidden_states_output(_a , _a , _a ) def __lowercase ( self ) -> str: def _config_zero_init(_a ): _a : List[Any] = copy.deepcopy(_a ) for key in configs_no_init.__dict__.keys(): if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key: setattr(_a , _a , 1e-1_0 ) if isinstance(getattr(_a , _a , _a ) , _a ): _a : int = _config_zero_init(getattr(_a , _a ) ) setattr(_a , _a , _a ) return configs_no_init _a , _a : Any = self.model_tester.prepare_config_and_inputs_for_common() _a : Dict = _config_zero_init(_a ) for model_class in self.all_model_classes: _a : Dict = model_class(config=_a ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9) / 1e9).round().item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __lowercase ( self ) -> Optional[Any]: pass def __UpperCAmelCase ( ) -> Optional[Any]: """simple docstring""" _a : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self ) -> str: return ViTImageProcessor.from_pretrained('''MBZUAI/swiftformer-xs''' ) if is_vision_available() else None @slow def __lowercase ( self ) -> Dict: _a : Any = SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs''' ).to(_a ) _a : Any = self.default_image_processor _a : Any = prepare_img() _a : Any = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _a : Optional[Any] = model(**_a ) # verify the logits _a : List[str] = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _a ) _a : int = torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
14
1
from __future__ import annotations import math def __UpperCAmelCase ( __a : int ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 ,int(math.sqrt(__a ) + 1 ) ,6 ): if number % i == 0 or number % (i + 2) == 0: return False return True a__ = [num for num in range(3, 100001, 2) if not is_prime(num)] def __UpperCAmelCase ( __a : int ) -> list[int]: """simple docstring""" if not isinstance(__a ,__a ): raise ValueError('''n must be an integer''' ) if n <= 0: raise ValueError('''n must be >= 0''' ) _a : Dict = [] for num in range(len(__a ) ): _a : Optional[Any] = 0 while 2 * i * i <= odd_composites[num]: _a : Optional[int] = odd_composites[num] - 2 * i * i if is_prime(__a ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(__a ) == n: return list_nums return [] def __UpperCAmelCase ( ) -> int: """simple docstring""" return compute_nums(1 )[0] if __name__ == "__main__": print(f'''{solution() = }''')
14
import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() a__ = logging.get_logger(__name__) def __UpperCAmelCase ( __a : str ) -> List[Any]: """simple docstring""" _a : Tuple = SwinConfig.from_pretrained( '''microsoft/swin-tiny-patch4-window7-224''' ,out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) _a : Dict = MaskFormerConfig(backbone_config=__a ) _a : Optional[Any] = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok _a : Optional[Any] = 847 _a : List[Any] = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok _a : Union[str, Any] = 150 _a : Any = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok _a : int = 171 _a : List[str] = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO _a : Dict = 133 _a : Optional[Any] = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok _a : List[Any] = 19 _a : Optional[Any] = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok _a : List[Any] = 65 _a : Dict = '''mapillary-vistas-id2label.json''' _a : Optional[int] = json.load(open(hf_hub_download(__a ,__a ,repo_type='''dataset''' ) ,'''r''' ) ) _a : Tuple = {int(__a ): v for k, v in idalabel.items()} return config def __UpperCAmelCase ( __a : Optional[Any] ) -> Tuple: """simple docstring""" _a : Optional[Any] = [] # stem # fmt: off rename_keys.append(('''backbone.patch_embed.proj.weight''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.proj.bias''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''model.pixel_level_module.encoder.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''model.pixel_level_module.encoder.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.layers.{i}.downsample.reduction.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(('''sem_seg_head.layer_4.weight''', '''model.pixel_level_module.decoder.fpn.stem.0.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.weight''', '''model.pixel_level_module.decoder.fpn.stem.1.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.bias''', '''model.pixel_level_module.decoder.fpn.stem.1.bias''') ) for source_index, target_index in zip(range(3 ,0 ,-1 ) ,range(0 ,3 ) ): rename_keys.append((F"""sem_seg_head.adapter_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(('''sem_seg_head.mask_features.weight''', '''model.pixel_level_module.decoder.mask_projection.weight''') ) rename_keys.append(('''sem_seg_head.mask_features.bias''', '''model.pixel_level_module.decoder.mask_projection.bias''') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.weight''', '''model.transformer_module.decoder.layernorm.weight''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.bias''', '''model.transformer_module.decoder.layernorm.bias''') ) # heads on top rename_keys.append(('''sem_seg_head.predictor.query_embed.weight''', '''model.transformer_module.queries_embedder.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.weight''', '''model.transformer_module.input_projection.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.bias''', '''model.transformer_module.input_projection.bias''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.weight''', '''class_predictor.weight''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.bias''', '''class_predictor.bias''') ) for i in range(3 ): rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", F"""mask_embedder.{i}.0.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", F"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ,__a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _a : str = dct.pop(__a ) _a : str = val def __UpperCAmelCase ( __a : List[Any] ,__a : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _a : Optional[Any] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) _a : Optional[int] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[int] = in_proj_weight[:dim, :] _a : List[Any] = in_proj_bias[: dim] _a : Optional[int] = in_proj_weight[ dim : dim * 2, : ] _a : Tuple = in_proj_bias[ dim : dim * 2 ] _a : int = in_proj_weight[ -dim :, : ] _a : Optional[int] = in_proj_bias[-dim :] # fmt: on def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ) -> List[Any]: """simple docstring""" _a : Optional[int] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : Union[str, Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Union[str, Any] = in_proj_weight[: hidden_size, :] _a : List[Any] = in_proj_bias[:config.hidden_size] _a : Dict = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Any = in_proj_bias[hidden_size : hidden_size * 2] _a : Tuple = in_proj_weight[-hidden_size :, :] _a : List[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) _a : List[str] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[Any] = in_proj_weight[: hidden_size, :] _a : Any = in_proj_bias[:config.hidden_size] _a : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Optional[Any] = in_proj_bias[hidden_size : hidden_size * 2] _a : List[str] = in_proj_weight[-hidden_size :, :] _a : int = in_proj_bias[-hidden_size :] # fmt: on def __UpperCAmelCase ( ) -> torch.Tensor: """simple docstring""" _a : str = '''http://images.cocodataset.org/val2017/000000039769.jpg''' _a : Dict = Image.open(requests.get(__a ,stream=__a ).raw ) return im @torch.no_grad() def __UpperCAmelCase ( __a : str ,__a : str ,__a : str ,__a : bool = False ) -> Union[str, Any]: """simple docstring""" _a : Optional[Any] = get_maskformer_config(__a ) # load original state_dict with open(__a ,'''rb''' ) as f: _a : str = pickle.load(__a ) _a : Union[str, Any] = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys _a : Any = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a ,__a ,__a ) read_in_swin_q_k_v(__a ,config.backbone_config ) read_in_decoder_q_k_v(__a ,__a ) # update to torch tensors for key, value in state_dict.items(): _a : Optional[int] = torch.from_numpy(__a ) # load 🤗 model _a : Dict = MaskFormerForInstanceSegmentation(__a ) model.eval() for name, param in model.named_parameters(): print(__a ,param.shape ) _a , _a : Tuple = model.load_state_dict(__a ,strict=__a ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(__a ) == 0, F"""Unexpected keys: {unexpected_keys}""" # verify results _a : Union[str, Any] = prepare_img() if "vistas" in model_name: _a : int = 65 elif "cityscapes" in model_name: _a : Tuple = 65_535 else: _a : str = 255 _a : Dict = True if '''ade''' in model_name else False _a : Optional[Any] = MaskFormerImageProcessor(ignore_index=__a ,reduce_labels=__a ) _a : Optional[Any] = image_processor(__a ,return_tensors='''pt''' ) _a : int = model(**__a ) print('''Logits:''' ,outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": _a : Union[str, Any] = torch.tensor( [[3.63_53, -4.47_70, -2.60_65], [0.50_81, -4.23_94, -3.53_43], [2.19_09, -5.03_53, -1.93_23]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] ,__a ,atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(__a ).mkdir(exist_ok=__a ) model.save_pretrained(__a ) image_processor.save_pretrained(__a ) if push_to_hub: print('''Pushing model and image processor to the hub...''' ) model.push_to_hub(F"""nielsr/{model_name}""" ) image_processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''maskformer-swin-tiny-ade''', type=str, help=('''Name of the MaskFormer model you\'d like to convert''',), ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl''', type=str, help='''Path to the original state dict (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) a__ = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
14
1
import argparse import logging import os import sys import numpy as np import onnxruntime import torch from bart_onnx.generation_onnx import BARTBeamSearchGenerator from bart_onnx.reduce_onnx_size import remove_dup_initializers import transformers from transformers import BartForConditionalGeneration, BartTokenizer logging.basicConfig( format='''%(asctime)s | %(levelname)s | %(name)s | [%(filename)s:%(lineno)d] %(message)s''', datefmt='''%Y-%m-%d %H:%M:%S''', level=os.environ.get('''LOGLEVEL''', '''INFO''').upper(), stream=sys.stdout, ) a__ = logging.getLogger(__name__) a__ = {'''facebook/bart-base''': BartForConditionalGeneration} a__ = {'''facebook/bart-base''': BartTokenizer} def __UpperCAmelCase ( ) -> str: """simple docstring""" _a : List[Any] = argparse.ArgumentParser(description='''Export Bart model + Beam Search to ONNX graph.''' ) parser.add_argument( '''--validation_file''' ,type=__a ,default=__a ,help='''A csv or a json file containing the validation data.''' ) parser.add_argument( '''--max_length''' ,type=__a ,default=5 ,help='''The maximum total input sequence length after tokenization.''' ,) parser.add_argument( '''--num_beams''' ,type=__a ,default=__a ,help=( '''Number of beams to use for evaluation. This argument will be ''' '''passed to ``model.generate``, which is used during ``evaluate`` and ``predict``.''' ) ,) parser.add_argument( '''--model_name_or_path''' ,type=__a ,help='''Path to pretrained model or model identifier from huggingface.co/models.''' ,required=__a ,) parser.add_argument( '''--config_name''' ,type=__a ,default=__a ,help='''Pretrained config name or path if not the same as model_name''' ,) parser.add_argument( '''--device''' ,type=__a ,default='''cpu''' ,help='''Device where the model will be run''' ,) parser.add_argument('''--output_file_path''' ,type=__a ,default=__a ,help='''Where to store the final ONNX file.''' ) _a : Optional[Any] = parser.parse_args() return args def __UpperCAmelCase ( __a : Any ,__a : Dict="cpu" ) -> List[Any]: """simple docstring""" _a : int = model_dict[model_name].from_pretrained(__a ).to(__a ) _a : int = tokenizer_dict[model_name].from_pretrained(__a ) if model_name in ["facebook/bart-base"]: _a : Dict = 0 _a : Union[str, Any] = None _a : Optional[int] = 0 return huggingface_model, tokenizer def __UpperCAmelCase ( __a : str ,__a : Dict ,__a : Union[str, Any] ,__a : Union[str, Any] ,__a : List[Any] ) -> int: """simple docstring""" model.eval() _a : Optional[int] = None _a : str = torch.jit.script(BARTBeamSearchGenerator(__a ) ) with torch.no_grad(): _a : List[Any] = '''My friends are cool but they eat too many carbs.''' _a : Union[str, Any] = tokenizer([ARTICLE_TO_SUMMARIZE] ,max_length=1_024 ,return_tensors='''pt''' ).to(model.device ) _a : List[str] = model.generate( inputs['''input_ids'''] ,attention_mask=inputs['''attention_mask'''] ,num_beams=__a ,max_length=__a ,early_stopping=__a ,decoder_start_token_id=model.config.decoder_start_token_id ,) torch.onnx.export( __a ,( inputs['''input_ids'''], inputs['''attention_mask'''], num_beams, max_length, model.config.decoder_start_token_id, ) ,__a ,opset_version=14 ,input_names=['''input_ids''', '''attention_mask''', '''num_beams''', '''max_length''', '''decoder_start_token_id'''] ,output_names=['''output_ids'''] ,dynamic_axes={ '''input_ids''': {0: '''batch''', 1: '''seq'''}, '''output_ids''': {0: '''batch''', 1: '''seq_out'''}, } ,example_outputs=__a ,) logger.info('''Model exported to {}'''.format(__a ) ) _a : int = remove_dup_initializers(os.path.abspath(__a ) ) logger.info('''Deduplicated and optimized model written to {}'''.format(__a ) ) _a : Tuple = onnxruntime.InferenceSession(__a ) _a : List[Any] = ort_sess.run( __a ,{ '''input_ids''': inputs['''input_ids'''].cpu().numpy(), '''attention_mask''': inputs['''attention_mask'''].cpu().numpy(), '''num_beams''': np.array(__a ), '''max_length''': np.array(__a ), '''decoder_start_token_id''': np.array(model.config.decoder_start_token_id ), } ,) np.testing.assert_allclose(summary_ids.cpu().numpy() ,ort_out[0] ,rtol=1E-3 ,atol=1E-3 ) logger.info('''Model outputs from torch and ONNX Runtime are similar.''' ) logger.info('''Success.''' ) def __UpperCAmelCase ( ) -> Optional[Any]: """simple docstring""" _a : Optional[int] = parse_args() _a : str = 5 _a : Optional[Any] = 4 # Make one log on every process with the configuration for debugging. logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' ,datefmt='''%m/%d/%Y %H:%M:%S''' ,level=logging.INFO ,) logger.setLevel(logging.INFO ) transformers.utils.logging.set_verbosity_error() _a : Optional[Any] = torch.device(args.device ) _a , _a : List[str] = load_model_tokenizer(args.model_name_or_path ,__a ) if model.config.decoder_start_token_id is None: raise ValueError('''Make sure that `config.decoder_start_token_id` is correctly defined''' ) model.to(__a ) if args.max_length: _a : Tuple = args.max_length if args.num_beams: _a : Dict = args.num_beams if args.output_file_path: _a : Optional[Any] = args.output_file_path else: _a : Dict = '''BART.onnx''' logger.info('''Exporting model to ONNX''' ) export_and_validate_model(__a ,__a ,__a ,__a ,__a ) if __name__ == "__main__": main()
14
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin a__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = XLMProphetNetTokenizer UpperCAmelCase__ : Optional[int] = False UpperCAmelCase__ : List[Any] = True def __lowercase ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _a : List[Any] = XLMProphetNetTokenizer(_a , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def __lowercase ( self ) -> Any: _a : Tuple = '''[PAD]''' _a : int = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def __lowercase ( self ) -> str: _a : Any = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''[PAD]''' ) self.assertEqual(vocab_keys[1] , '''[CLS]''' ) self.assertEqual(vocab_keys[-1] , '''j''' ) self.assertEqual(len(_a ) , 1_0_1_2 ) def __lowercase ( self ) -> Union[str, Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1_0_1_2 ) def __lowercase ( self ) -> str: _a : Tuple = XLMProphetNetTokenizer(_a , keep_accents=_a ) _a : Union[str, Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) _a : Optional[int] = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _a : List[Any] = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, -9, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, -9, 4] ] , ) _a : List[str] = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''[UNK]''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''[UNK]''', '''.''', ] , ) @cached_property def __lowercase ( self ) -> List[str]: return XLMProphetNetTokenizer.from_pretrained('''microsoft/xprophetnet-large-wiki100-cased''' ) @slow def __lowercase ( self ) -> Tuple: _a : str = '''Hello World!''' _a : Tuple = [3_5_3_8_9, 6_6_7_2, 4_9, 2] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @slow def __lowercase ( self ) -> str: # fmt: off _a : str = {'''input_ids''': [[1_1_0_7_3, 8_2_7_8_3, 1_8, 2_6, 8_2_7_8_3, 5_4_9, 5_1_5_4_0, 2_4_8, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 2_1_5_1_8_6, 1_3_2_5, 1_4_7, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 5_6_3_7_0, 5_3, 1_2_2_0_2_0, 2_0, 1_6_4_7_7, 2_7, 8_7_3_5_5, 4_5_4_8, 2_0, 4_7_2_8, 7_8_3_9_2, 1_7, 1_5_9_9_6_9, 1_8, 2_6, 2_4_4_9_1, 6_2_9, 1_5, 5_3_8, 2_2_7_0_4, 5_4_3_9, 1_5, 2_7_8_8, 2_4_4_9_1, 9_8_8_5, 1_5, 4_3_5_3_4, 6_0_5, 1_5, 8_1_4, 1_8_4_0_3, 3_3_2_0_0, 2_9, 1_5, 4_3_5_3_4, 2_4_4_5_8, 1_2_4_1_0, 1_1_1, 2_4_9_6_6, 8_3_6_6_9, 9_6_3_7, 1_4_4_0_6_8, 2_6, 8_5_0, 2_2_3_4_6, 2_7, 1_4_7, 2_4_9_6_6, 8_3_6_6_9, 8_3_4_9_0, 2_6, 3_9_1_1_3, 7_3_5, 2_7, 6_8_9, 6_5_6, 2_8_0_0, 1_3_3_9, 4_6_0_0, 5_3, 1_2_2_0_2_0, 1_1_5_7_8_5, 3_4, 8_1_6, 1_3_3_9, 4_6_8_8_7, 1_8, 1_4_7, 5_3_9_0_5, 1_9_5_1, 4_2_2_3_8, 4_1_1_7_0, 1_7_7_3_2, 8_3_4, 4_3_6, 1_5, 2_7_5_2_3, 9_8_7_3_3, 2_1_7, 1_4_7, 5_5_4_2, 4_9_8_1, 9_3_0, 1_7_3_4_7, 1_6, 2], [2_0_0_9_1, 6_2_9, 9_4, 8_2_7_8_6, 5_8, 4_9_0, 2_0, 1_5_2_8, 8_4, 5_3_9_0_5, 3_4_4, 8_0_5_9_2, 1_1_0_1_2_8, 1_8_8_2_2, 5_2_6_7, 1_3_0_6, 6_2, 1_5_2_5_3_7, 3_0_8, 7_9_9_7, 4_0_1, 1_2_4_4_2_7, 5_4_9, 3_5_4_4_2, 2_2_5, 1_0_9, 1_5_0_5_5, 2_5_7_4_8, 1_4_7, 7_1_1_9, 4_3_7_1_2, 3_4, 7_6_7, 1_3_5_3_6_6, 1_8, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5_9_2, 6_3_7_8_4, 1_1_9_4_6_6, 1_7, 1_4_7_8_0_8, 8_8_2_1_4, 1_8, 6_5_6, 8_1, 3_2, 3_2_9_6, 1_0_2_8_0, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a , model_name='''microsoft/xprophetnet-large-wiki100-cased''' , revision='''1acad1643ddd54a44df6a1b797ada8373685d90e''' , )
14
1
import pytest import datasets # Import fixture modules as plugins a__ = ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec'''] def __UpperCAmelCase ( __a : Dict ,__a : Dict ) -> List[str]: """simple docstring""" for item in items: if any(marker in item.keywords for marker in ['''integration''', '''unit'''] ): continue item.add_marker(pytest.mark.unit ) def __UpperCAmelCase ( __a : Tuple ) -> Optional[Any]: """simple docstring""" config.addinivalue_line('''markers''' ,'''torchaudio_latest: mark test to run with torchaudio>=0.12''' ) @pytest.fixture(autouse=__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : Dict ) -> str: """simple docstring""" _a : Any = tmp_path_factory.getbasetemp() / '''cache''' _a : Dict = test_hf_cache_home / '''datasets''' _a : Optional[int] = test_hf_cache_home / '''metrics''' _a : List[str] = test_hf_cache_home / '''modules''' monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE''' ,str(__a ) ) monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE''' ,str(__a ) ) monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE''' ,str(__a ) ) _a : List[Any] = test_hf_datasets_cache / '''downloads''' monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH''' ,str(__a ) ) _a : List[str] = test_hf_datasets_cache / '''downloads''' / '''extracted''' monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH''' ,str(__a ) ) @pytest.fixture(autouse=__a ,scope='''session''' ) def __UpperCAmelCase ( ) -> List[str]: """simple docstring""" datasets.disable_progress_bar() @pytest.fixture(autouse=__a ) def __UpperCAmelCase ( __a : List[Any] ) -> Dict: """simple docstring""" monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS''' ,__a ) @pytest.fixture def __UpperCAmelCase ( __a : Optional[int] ) -> int: """simple docstring""" monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING''' ,__a )
14
import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Any = LxmertTokenizer UpperCAmelCase__ : Optional[Any] = LxmertTokenizerFast UpperCAmelCase__ : Any = True UpperCAmelCase__ : Dict = True def __lowercase ( self ) -> Union[str, Any]: super().setUp() _a : int = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _a : Any = 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 __lowercase ( self , _a ) -> List[str]: _a : Tuple = '''UNwant\u00E9d,running''' _a : str = '''unwanted, running''' return input_text, output_text def __lowercase ( self ) -> List[Any]: _a : str = self.tokenizer_class(self.vocab_file ) _a : str = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [7, 4, 5, 1_0, 8, 9] ) def __lowercase ( self ) -> List[Any]: if not self.test_rust_tokenizer: return _a : Optional[Any] = self.get_tokenizer() _a : str = self.get_rust_tokenizer() _a : Optional[Any] = '''I was born in 92000, and this is falsé.''' _a : Optional[Any] = tokenizer.tokenize(_a ) _a : List[Any] = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _a : List[Any] = tokenizer.encode(_a , add_special_tokens=_a ) _a : Any = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Dict = self.get_rust_tokenizer() _a : Optional[int] = tokenizer.encode(_a ) _a : Dict = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a )
14
1
def __UpperCAmelCase ( __a : int = 10**9 ) -> int: """simple docstring""" _a : Tuple = 1 _a : Tuple = 2 _a : List[str] = 0 _a : Union[str, Any] = 0 _a : Optional[int] = 0 while perimeter <= max_perimeter: perimeters_sum += perimeter prev_value += 2 * value value += prev_value _a : Tuple = 2 * value + 2 if i % 2 == 0 else 2 * value - 2 i += 1 return perimeters_sum if __name__ == "__main__": print(f'''{solution() = }''')
14
import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : Dict = '''ZinengTang/tvlt-base''' _a : List[str] = tempfile.mkdtemp() def __lowercase ( self , **_a ) -> int: return TvltImageProcessor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self , **_a ) -> List[Any]: return TvltFeatureExtractor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self ) -> Optional[int]: shutil.rmtree(self.tmpdirname ) def __lowercase ( self ) -> Dict: _a : Union[str, Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Optional[int] = TvltProcessor(image_processor=_a , feature_extractor=_a ) processor.save_pretrained(self.tmpdirname ) _a : Any = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor , _a ) self.assertIsInstance(processor.image_processor , _a ) def __lowercase ( self ) -> Any: _a : Optional[Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : Union[str, Any] = np.ones([1_2_0_0_0] ) _a : Dict = feature_extractor(_a , return_tensors='''np''' ) _a : Tuple = processor(audio=_a , return_tensors='''np''' ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> int: _a : Optional[Any] = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Optional[Any] = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[Any] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = image_processor(_a , return_tensors='''np''' ) _a : Optional[int] = processor(images=_a , return_tensors='''np''' ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> Union[str, Any]: _a : int = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Any = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[str] = np.ones([1_2_0_0_0] ) _a : Optional[int] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = processor(audio=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __lowercase ( self ) -> Union[str, Any]: _a : str = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' , )
14
1
from __future__ import annotations from fractions import Fraction def __UpperCAmelCase ( __a : int ,__a : int ) -> bool: """simple docstring""" return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __UpperCAmelCase ( __a : int ) -> list[str]: """simple docstring""" _a : Dict = [] _a : Tuple = 11 _a : int = int('''1''' + '''0''' * digit_len ) for num in range(__a ,__a ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__a ,__a ): solutions.append(F"""{num}/{den}""" ) den += 1 num += 1 _a : int = 10 return solutions def __UpperCAmelCase ( __a : int = 2 ) -> int: """simple docstring""" _a : Union[str, Any] = 1.0 for fraction in fraction_list(__a ): _a : Any = Fraction(__a ) result *= frac.denominator / frac.numerator return int(__a ) if __name__ == "__main__": print(solution())
14
def __UpperCAmelCase ( __a : str ) -> list: """simple docstring""" if n_term == "": return [] _a : list = [] for temp in range(int(__a ) ): series.append(F"""1/{temp + 1}""" if series else '''1''' ) return series if __name__ == "__main__": a__ = 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))
14
1
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 UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __lowercase ( self ) -> Optional[Any]: _a : str = 1 _a : Union[str, Any] = 3 _a : Optional[int] = (3_2, 3_2) _a : int = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_a ) return image @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : str = UNetaDConditionModel( block_out_channels=(3_2, 3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=7 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=3_2 , attention_head_dim=8 , use_linear_projection=_a , only_cross_attention=(True, True, False) , num_class_embeds=1_0_0 , ) return model @property def __lowercase ( self ) -> Any: torch.manual_seed(0 ) _a : Optional[Any] = AutoencoderKL( block_out_channels=[3_2, 3_2, 6_4] , 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 ) -> str: torch.manual_seed(0 ) _a : List[str] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , hidden_act='''gelu''' , projection_dim=5_1_2 , ) return CLIPTextModel(_a ) def __lowercase ( self ) -> str: _a : List[Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator _a : List[str] = self.dummy_cond_unet_upscale _a : int = DDPMScheduler() _a : int = DDIMScheduler(prediction_type='''v_prediction''' ) _a : List[Any] = self.dummy_vae _a : Tuple = self.dummy_text_encoder _a : Optional[int] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) _a : Tuple = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : int = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((6_4, 6_4) ) # make sure here that pndm scheduler skips prk _a : int = StableDiffusionUpscalePipeline( unet=_a , low_res_scheduler=_a , scheduler=_a , vae=_a , text_encoder=_a , tokenizer=_a , max_noise_level=3_5_0 , ) _a : str = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) _a : Union[str, Any] = '''A painting of a squirrel eating a burger''' _a : Any = torch.Generator(device=_a ).manual_seed(0 ) _a : Dict = sd_pipe( [prompt] , image=_a , generator=_a , guidance_scale=6.0 , noise_level=2_0 , num_inference_steps=2 , output_type='''np''' , ) _a : Optional[int] = output.images _a : Optional[int] = torch.Generator(device=_a ).manual_seed(0 ) _a : Tuple = sd_pipe( [prompt] , image=_a , generator=_a , guidance_scale=6.0 , noise_level=2_0 , num_inference_steps=2 , output_type='''np''' , return_dict=_a , )[0] _a : Union[str, Any] = image[0, -3:, -3:, -1] _a : List[Any] = image_from_tuple[0, -3:, -3:, -1] _a : str = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) _a : Optional[int] = np.array([0.3113, 0.3910, 0.4272, 0.4859, 0.5061, 0.4652, 0.5362, 0.5715, 0.5661] ) 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 ) -> List[Any]: _a : str = '''cpu''' # ensure determinism for the device-dependent torch.Generator _a : Any = self.dummy_cond_unet_upscale _a : Dict = DDPMScheduler() _a : Any = DDIMScheduler(prediction_type='''v_prediction''' ) _a : Optional[int] = self.dummy_vae _a : Tuple = self.dummy_text_encoder _a : Union[str, Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) _a : Union[str, Any] = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : List[Any] = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((6_4, 6_4) ) # make sure here that pndm scheduler skips prk _a : Any = StableDiffusionUpscalePipeline( unet=_a , low_res_scheduler=_a , scheduler=_a , vae=_a , text_encoder=_a , tokenizer=_a , max_noise_level=3_5_0 , ) _a : Tuple = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) _a : Dict = '''A painting of a squirrel eating a burger''' _a : List[Any] = sd_pipe( 2 * [prompt] , image=2 * [low_res_image] , guidance_scale=6.0 , noise_level=2_0 , num_inference_steps=2 , output_type='''np''' , ) _a : Dict = output.images assert image.shape[0] == 2 _a : Dict = torch.Generator(device=_a ).manual_seed(0 ) _a : int = sd_pipe( [prompt] , image=_a , generator=_a , num_images_per_prompt=2 , guidance_scale=6.0 , noise_level=2_0 , num_inference_steps=2 , output_type='''np''' , ) _a : Optional[Any] = output.images assert image.shape[0] == 2 @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def __lowercase ( self ) -> Union[str, Any]: _a : Union[str, Any] = self.dummy_cond_unet_upscale _a : List[Any] = DDPMScheduler() _a : Dict = DDIMScheduler(prediction_type='''v_prediction''' ) _a : Dict = self.dummy_vae _a : int = self.dummy_text_encoder _a : int = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) _a : Any = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : Optional[Any] = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((6_4, 6_4) ) # put models in fp16, except vae as it overflows in fp16 _a : Dict = unet.half() _a : Tuple = text_encoder.half() # make sure here that pndm scheduler skips prk _a : List[Any] = StableDiffusionUpscalePipeline( unet=_a , low_res_scheduler=_a , scheduler=_a , vae=_a , text_encoder=_a , tokenizer=_a , max_noise_level=3_5_0 , ) _a : Union[str, Any] = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) _a : Tuple = '''A painting of a squirrel eating a burger''' _a : str = torch.manual_seed(0 ) _a : List[str] = sd_pipe( [prompt] , image=_a , generator=_a , num_inference_steps=2 , output_type='''np''' , ).images _a : int = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> Dict: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowercase ( self ) -> int: _a : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-upscale/low_res_cat.png''' ) _a : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale''' '''/upsampled_cat.npy''' ) _a : List[Any] = '''stabilityai/stable-diffusion-x4-upscaler''' _a : Dict = StableDiffusionUpscalePipeline.from_pretrained(_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() _a : Optional[Any] = '''a cat sitting on a park bench''' _a : Dict = torch.manual_seed(0 ) _a : Tuple = pipe( prompt=_a , image=_a , generator=_a , output_type='''np''' , ) _a : Dict = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 1e-3 def __lowercase ( self ) -> List[str]: _a : Dict = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-upscale/low_res_cat.png''' ) _a : Optional[int] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale''' '''/upsampled_cat_fp16.npy''' ) _a : Optional[int] = '''stabilityai/stable-diffusion-x4-upscaler''' _a : Union[str, Any] = StableDiffusionUpscalePipeline.from_pretrained( _a , torch_dtype=torch.floataa , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() _a : Optional[int] = '''a cat sitting on a park bench''' _a : Optional[Any] = torch.manual_seed(0 ) _a : str = pipe( prompt=_a , image=_a , generator=_a , output_type='''np''' , ) _a : str = output.images[0] assert image.shape == (5_1_2, 5_1_2, 3) assert np.abs(expected_image - image ).max() < 5e-1 def __lowercase ( self ) -> Optional[Any]: torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() _a : List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-upscale/low_res_cat.png''' ) _a : Dict = '''stabilityai/stable-diffusion-x4-upscaler''' _a : str = 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() _a : str = '''a cat sitting on a park bench''' _a : Optional[int] = torch.manual_seed(0 ) _a : Optional[Any] = pipe( prompt=_a , image=_a , generator=_a , num_inference_steps=5 , output_type='''np''' , ) _a : str = torch.cuda.max_memory_allocated() # make sure that less than 2.9 GB is allocated assert mem_bytes < 2.9 * 1_0**9
14
import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Optional[int] ) -> Dict: """simple docstring""" return params[F"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :] def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[int] ,__a : int ,__a : List[str]="attention" ) -> List[str]: """simple docstring""" _a : str = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :] ) _a : Tuple = k_tmp.reshape(k_tmp.shape[0] ,k_tmp.shape[1] * k_tmp.shape[2] ) _a : Any = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :] ) _a : Dict = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] ,o_tmp.shape[2] ) _a : Union[str, Any] = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :] ) _a : Any = q_tmp.reshape(q_tmp.shape[0] ,q_tmp.shape[1] * q_tmp.shape[2] ) _a : Tuple = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :] ) _a : int = v_tmp.reshape(v_tmp.shape[0] ,v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Union[str, Any] ,__a : List[Any] ,__a : Any=False ) -> Any: """simple docstring""" if split_mlp_wi: _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :] _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :] _a : List[str] = (wi_a, wi_a) else: _a : List[str] = params[F"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :] _a : Optional[int] = params[F"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :] return wi, wo def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Union[str, Any] ,__a : str ) -> List[str]: """simple docstring""" return params[F"""{prefix}/{prefix}/{layer_name}/scale"""][:, i] def __UpperCAmelCase ( __a : dict ,*, __a : int ,__a : bool ,__a : bool = False ) -> Any: """simple docstring""" _a : Dict = traverse_util.flatten_dict(variables['''target'''] ) _a : Any = {'''/'''.join(__a ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi _a : Optional[int] = '''encoder/encoder/mlp/wi_0/kernel''' in old print('''Split MLP:''' ,__a ) _a : Tuple = collections.OrderedDict() # Shared embeddings. _a : Any = old['''token_embedder/embedding'''] # Encoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Optional[Any] = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_attention_layer_norm''' ) _a , _a , _a , _a : List[str] = tax_attention_lookup(__a ,__a ,'''encoder''' ,'''attention''' ) _a : List[str] = layer_norm _a : Optional[Any] = k.T _a : str = o.T _a : List[Any] = q.T _a : Tuple = v.T # Block i, layer 1 (MLP). _a : str = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Any = tax_mlp_lookup(__a ,__a ,'''encoder''' ,__a ) _a : str = layer_norm if split_mlp_wi: _a : List[Any] = wi[0].T _a : Any = wi[1].T else: _a : Any = wi.T _a : Optional[Any] = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Dict = tax_relpos_bias_lookup( __a ,__a ,'''encoder''' ).T _a : List[str] = old['''encoder/encoder_norm/scale'''] if not scalable_attention: _a : List[Any] = tax_relpos_bias_lookup( __a ,0 ,'''encoder''' ).T _a : Optional[Any] = tax_relpos_bias_lookup( __a ,0 ,'''decoder''' ).T if not is_encoder_only: # Decoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Union[str, Any] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_self_attention_layer_norm''' ) _a , _a , _a , _a : Optional[Any] = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''self_attention''' ) _a : Optional[Any] = layer_norm _a : Dict = k.T _a : str = o.T _a : str = q.T _a : List[str] = v.T # Block i, layer 1 (Cross Attention). _a : Any = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_cross_attention_layer_norm''' ) _a , _a , _a , _a : str = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''encoder_decoder_attention''' ) _a : Optional[Any] = layer_norm _a : Optional[int] = k.T _a : Dict = o.T _a : str = q.T _a : int = v.T # Block i, layer 2 (MLP). _a : Optional[int] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Tuple = tax_mlp_lookup(__a ,__a ,'''decoder''' ,__a ) _a : Optional[Any] = layer_norm if split_mlp_wi: _a : List[str] = wi[0].T _a : List[Any] = wi[1].T else: _a : Dict = wi.T _a : str = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Tuple = tax_relpos_bias_lookup(__a ,__a ,'''decoder''' ).T _a : Tuple = old['''decoder/decoder_norm/scale'''] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: _a : Any = old['''decoder/logits_dense/kernel'''].T return new def __UpperCAmelCase ( __a : Dict ,__a : bool ) -> Tuple: """simple docstring""" _a : Tuple = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: _a : Any = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: _a : Optional[int] = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''' ) _a : str = state_dict['''shared.weight'''] return state_dict def __UpperCAmelCase ( __a : List[str] ,__a : Union[str, Any] ,__a : Dict ,__a : Union[str, Any] ,__a : List[Any] ) -> int: """simple docstring""" _a : List[str] = checkpoints.load_tax_checkpoint(__a ) _a : str = convert_tax_to_pytorch( __a ,num_layers=config.num_layers ,is_encoder_only=__a ,scalable_attention=__a ) _a : str = make_state_dict(__a ,__a ) model.load_state_dict(__a ,strict=__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : Any ,__a : Union[str, Any] ,__a : bool = False ,__a : bool = False ,) -> Optional[Any]: """simple docstring""" _a : List[str] = MTaConfig.from_json_file(__a ) print(F"""Building PyTorch model from configuration: {config}""" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: _a : Any = UMTaEncoderModel(__a ) else: _a : Tuple = UMTaForConditionalGeneration(__a ) # Load weights from tf checkpoint load_tax_weights_in_ta(__a ,__a ,__a ,__a ,__a ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(__a ) # Verify that we can load the checkpoint. model.from_pretrained(__a ) print('''Done''' ) if __name__ == "__main__": a__ = argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''') # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained T5 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.''' ) parser.add_argument( '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False ) parser.add_argument( '''--scalable_attention''', action='''store_true''', help='''Whether the model uses scaled attention (umt5 model)''', default=False, ) a__ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
14
1
# We ignore warnings about stepping the scheduler since we step it ourselves during gradient accumulation import warnings from .state import AcceleratorState, GradientState warnings.filterwarnings('''ignore''', category=UserWarning, module='''torch.optim.lr_scheduler''') class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a , _a = True , _a = False ) -> Optional[Any]: _a : Tuple = scheduler _a : List[Any] = optimizers if isinstance(_a , (list, tuple) ) else [optimizers] _a : Any = split_batches _a : Dict = step_with_optimizer _a : Tuple = GradientState() def __lowercase ( self , *_a , **_a ) -> int: if not self.step_with_optimizer: # No link between scheduler and optimizer -> just step self.scheduler.step(*_a , **_a ) return # Otherwise, first make sure the optimizer was stepped. if not self.gradient_state.sync_gradients: if self.gradient_state.adjust_scheduler: self.scheduler._step_count += 1 return for opt in self.optimizers: if opt.step_was_skipped: return if self.split_batches: # Split batches -> the training dataloader batch size is not changed so one step per training step self.scheduler.step(*_a , **_a ) else: # Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do # num_processes steps per training step _a : Tuple = AcceleratorState().num_processes for _ in range(_a ): # Special case when using OneCycle and `drop_last` was not used if hasattr(self.scheduler , '''total_steps''' ): if self.scheduler._step_count <= self.scheduler.total_steps: self.scheduler.step(*_a , **_a ) else: self.scheduler.step(*_a , **_a ) def __lowercase ( self ) -> int: return self.scheduler.get_last_lr() def __lowercase ( self ) -> Optional[int]: return self.scheduler.state_dict() def __lowercase ( self , _a ) -> Dict: self.scheduler.load_state_dict(_a ) def __lowercase ( self ) -> str: return self.scheduler.get_lr() def __lowercase ( self , *_a , **_a ) -> List[str]: return self.scheduler.print_lr(*_a , **_a )
14
import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap a__ = '''Usage of script: script_name <size_of_canvas:int>''' a__ = [0] * 100 + [1] * 10 random.shuffle(choice) def __UpperCAmelCase ( __a : int ) -> list[list[bool]]: """simple docstring""" _a : int = [[False for i in range(__a )] for j in range(__a )] return canvas def __UpperCAmelCase ( __a : list[list[bool]] ) -> None: """simple docstring""" for i, row in enumerate(__a ): for j, _ in enumerate(__a ): _a : Optional[int] = bool(random.getrandbits(1 ) ) def __UpperCAmelCase ( __a : list[list[bool]] ) -> list[list[bool]]: """simple docstring""" _a : Any = np.array(__a ) _a : Optional[int] = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(__a ): for c, pt in enumerate(__a ): _a : Tuple = __judge_point( __a ,current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) _a : List[str] = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. _a : list[list[bool]] = current_canvas.tolist() return return_canvas def __UpperCAmelCase ( __a : bool ,__a : list[list[bool]] ) -> bool: """simple docstring""" _a : Optional[Any] = 0 _a : str = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. _a : Optional[int] = pt if pt: if alive < 2: _a : Dict = False elif alive == 2 or alive == 3: _a : Optional[Any] = True elif alive > 3: _a : str = False else: if alive == 3: _a : int = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) a__ = int(sys.argv[1]) # main working structure of this module. a__ = create_canvas(canvas_size) seed(c) a__ , a__ = plt.subplots() fig.show() a__ = ListedColormap(['''w''', '''k''']) try: while True: a__ = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
14
1
from __future__ import annotations a__ = { '''A''': ['''B''', '''C''', '''E'''], '''B''': ['''A''', '''D''', '''E'''], '''C''': ['''A''', '''F''', '''G'''], '''D''': ['''B'''], '''E''': ['''A''', '''B''', '''D'''], '''F''': ['''C'''], '''G''': ['''C'''], } class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a ) -> None: _a : List[str] = graph # mapping node to its parent in resulting breadth first tree _a : dict[str, str | None] = {} _a : List[Any] = source_vertex def __lowercase ( self ) -> None: _a : Optional[int] = {self.source_vertex} _a : int = None _a : str = [self.source_vertex] # first in first out queue while queue: _a : Dict = queue.pop(0 ) for adjacent_vertex in self.graph[vertex]: if adjacent_vertex not in visited: visited.add(_a ) _a : Dict = vertex queue.append(_a ) def __lowercase ( self , _a ) -> str: if target_vertex == self.source_vertex: return self.source_vertex _a : Tuple = self.parent.get(_a ) if target_vertex_parent is None: _a : Dict = ( F"""No path from vertex: {self.source_vertex} to vertex: {target_vertex}""" ) raise ValueError(_a ) return self.shortest_path(_a ) + F"""->{target_vertex}""" if __name__ == "__main__": a__ = Graph(graph, '''G''') g.breath_first_search() print(g.shortest_path('''D''')) print(g.shortest_path('''G''')) print(g.shortest_path('''Foo'''))
14
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''', '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''', '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''', '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''', '''funnel-transformer/intermediate''': ( '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json''' ), '''funnel-transformer/intermediate-base''': ( '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json''' ), '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''', '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''', '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''', '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''', } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = "funnel" UpperCAmelCase__ : Tuple = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self , _a=3_0_5_2_2 , _a=[4, 4, 4] , _a=None , _a=2 , _a=7_6_8 , _a=1_2 , _a=6_4 , _a=3_0_7_2 , _a="gelu_new" , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=None , _a=1e-9 , _a="mean" , _a="relative_shift" , _a=True , _a=True , _a=True , **_a , ) -> List[Any]: _a : Optional[int] = vocab_size _a : Dict = block_sizes _a : Optional[int] = [1] * len(_a ) if block_repeats is None else block_repeats assert len(_a ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." _a : int = num_decoder_layers _a : List[str] = d_model _a : Optional[Any] = n_head _a : Tuple = d_head _a : Dict = d_inner _a : List[str] = hidden_act _a : int = hidden_dropout _a : Union[str, Any] = attention_dropout _a : Tuple = activation_dropout _a : Optional[Any] = initializer_range _a : Dict = initializer_std _a : Union[str, Any] = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported.""" _a : Any = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported.""" _a : Optional[Any] = attention_type _a : int = separate_cls _a : Tuple = truncate_seq _a : List[Any] = pool_q_only super().__init__(**_a ) @property def __lowercase ( self ) -> Tuple: return sum(self.block_sizes ) @num_hidden_layers.setter def __lowercase ( self , _a ) -> List[str]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.''' ) @property def __lowercase ( self ) -> Optional[int]: return len(self.block_sizes ) @num_blocks.setter def __lowercase ( self , _a ) -> Dict: raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.''' )
14
1
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": a__ = input('''Enter image url: ''').strip() print(f'''Downloading image from {url} ...''') a__ = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image a__ = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] a__ = requests.get(image_url).content a__ = f'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(f'''Done. Image saved to disk as {file_name}.''')
14
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : int = "mobilenet_v1" def __init__( self , _a=3 , _a=2_2_4 , _a=1.0 , _a=8 , _a="relu6" , _a=True , _a=0.999 , _a=0.02 , _a=0.001 , **_a , ) -> List[Any]: super().__init__(**_a ) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''' ) _a : Tuple = num_channels _a : str = image_size _a : Tuple = depth_multiplier _a : Any = min_depth _a : int = hidden_act _a : Optional[Any] = tf_padding _a : str = classifier_dropout_prob _a : Optional[int] = initializer_range _a : Any = layer_norm_eps class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = version.parse("1.11" ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([('''pixel_values''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})] ) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> float: return 1e-4
14
1
import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin a__ = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_torch_available(): from transformers.models.plbart.modeling_plbart import shift_tokens_right a__ = 50003 a__ = 50002 @require_sentencepiece @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Dict = PLBartTokenizer UpperCAmelCase__ : str = None UpperCAmelCase__ : Tuple = False def __lowercase ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _a : int = PLBartTokenizer(_a , language_codes='''base''' , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def __lowercase ( self ) -> Tuple: _a : str = PLBartTokenizer(_a , language_codes='''base''' , keep_accents=_a ) _a : Optional[Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) _a : int = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _a : Optional[int] = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4] ] , ) _a : int = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) _a : Any = tokenizer.vocab_size _a : Any = [tokenizer.convert_ids_to_tokens(_a ) for x in range(end - 4 , _a )] self.assertListEqual(_a , ['''__java__''', '''__python__''', '''__en_XX__''', '''<mask>'''] ) _a : List[Any] = '''java.lang.Exception, python.lang.Exception, javascript, php, ruby, go''' _a : str = tokenizer(_a ).input_ids self.assertEqual( tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) , _a , ) def __lowercase ( self ) -> Dict: _a : int = PLBartTokenizer(_a , language_codes='''multi''' , keep_accents=_a ) _a : Optional[int] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) _a : Dict = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _a : Tuple = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4] ] , ) _a : List[Any] = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) _a : Optional[Any] = tokenizer.vocab_size _a : Dict = [tokenizer.convert_ids_to_tokens(_a ) for x in range(end - 7 , _a )] self.assertListEqual( _a , ['''__java__''', '''__python__''', '''__en_XX__''', '''__javascript__''', '''__php__''', '''__ruby__''', '''__go__'''] ) _a : Optional[Any] = '''java.lang.Exception, python.lang.Exception, javascript, php, ruby, go''' _a : str = tokenizer(_a ).input_ids self.assertEqual( tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) , _a , ) @require_torch @require_sentencepiece @require_tokenizers class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = "uclanlp/plbart-python-en_XX" UpperCAmelCase__ : int = [ "def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])", "def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])", ] UpperCAmelCase__ : Optional[int] = [ "Returns the maximum value of a b c.", "Sums the values of a b c.", ] UpperCAmelCase__ : Optional[int] = [ 134, 5452, 33460, 33441, 33463, 33465, 33463, 33449, 988, 20, 33456, 19, 33456, 771, 39, 4258, 889, 3318, 33441, 33463, 33465, 33463, 33449, 2471, 2, PYTHON_CODE, ] @classmethod def __lowercase ( cls ) -> Any: _a : PLBartTokenizer = PLBartTokenizer.from_pretrained( cls.checkpoint_name , language_codes='''base''' , src_lang='''python''' , tgt_lang='''en_XX''' ) _a : Optional[int] = 1 return cls def __lowercase ( self ) -> int: self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__java__'''] , 5_0_0_0_1 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__python__'''] , 5_0_0_0_2 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__en_XX__'''] , 5_0_0_0_3 ) def __lowercase ( self ) -> Dict: _a : Tuple = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , _a ) def __lowercase ( self ) -> List[Any]: self.assertIn(_a , self.tokenizer.all_special_ids ) _a : Union[str, Any] = [EN_CODE, 9_0_3_7, 3_3_4_4_2, 5_7, 7_5_2, 1_5_3, 1_4, 5_6, 1_8, 9, 2] _a : Tuple = self.tokenizer.decode(_a , skip_special_tokens=_a ) _a : List[Any] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_a ) self.assertEqual(_a , _a ) self.assertNotIn(self.tokenizer.eos_token , _a ) def __lowercase ( self ) -> Optional[Any]: _a : Dict = ['''def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])''' * 2_0] self.assertIsInstance(src_text[0] , _a ) _a : Optional[Any] = 1_0 _a : List[str] = self.tokenizer(_a , max_length=_a , truncation=_a ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , _a ) self.assertEqual(len(_a ) , _a ) def __lowercase ( self ) -> Optional[int]: self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''__java__'''] ) , [5_0_0_0_4, 5_0_0_0_1] ) def __lowercase ( self ) -> Dict: _a : int = tempfile.mkdtemp() _a : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(_a ) _a : Tuple = PLBartTokenizer.from_pretrained(_a ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , _a ) @require_torch def __lowercase ( self ) -> List[str]: _a : Tuple = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=_a , return_tensors='''pt''' ) _a : Tuple = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 self.assertEqual(batch.input_ids[1][-2:].tolist() , [2, PYTHON_CODE] ) self.assertEqual(batch.decoder_input_ids[1][0] , _a ) self.assertEqual(batch.decoder_input_ids[1][-1] , 2 ) self.assertEqual(batch.labels[1][-2:].tolist() , [2, EN_CODE] ) @require_torch def __lowercase ( self ) -> int: _a : Tuple = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=_a , truncation=_a , max_length=len(self.expected_src_tokens ) , return_tensors='''pt''' , ) _a : str = shift_tokens_right(batch['''labels'''] , self.tokenizer.pad_token_id ) self.assertIsInstance(_a , _a ) self.assertEqual((2, 2_6) , batch.input_ids.shape ) self.assertEqual((2, 2_6) , batch.attention_mask.shape ) _a : Optional[int] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , _a ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, PYTHON_CODE] ) def __lowercase ( self ) -> Optional[Any]: _a : List[str] = self.tokenizer(self.src_text , padding=_a , truncation=_a , max_length=3 , return_tensors='''pt''' ) _a : str = self.tokenizer( text_target=self.tgt_text , padding=_a , truncation=_a , max_length=1_0 , return_tensors='''pt''' ) _a : Optional[Any] = targets['''input_ids'''] _a : str = shift_tokens_right(_a , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 1_0 ) @require_torch def __lowercase ( self ) -> List[Any]: _a : Optional[Any] = self.tokenizer._build_translation_inputs( '''A test''' , return_tensors='''pt''' , src_lang='''en_XX''' , tgt_lang='''java''' ) self.assertEqual( nested_simplify(_a ) , { # A, test, EOS, en_XX '''input_ids''': [[1_5_0, 2_4_2, 2, 5_0_0_0_3]], '''attention_mask''': [[1, 1, 1, 1]], # java '''forced_bos_token_id''': 5_0_0_0_1, } , )
14
a__ = '''Input must be a string of 8 numbers plus letter''' a__ = '''TRWAGMYFPDXBNJZSQVHLCKE''' def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" if not isinstance(__a ,__a ): _a : List[str] = F"""Expected string as input, found {type(__a ).__name__}""" raise TypeError(__a ) _a : List[Any] = spanish_id.replace('''-''' ,'''''' ).upper() if len(__a ) != 9: raise ValueError(__a ) try: _a : Any = int(spanish_id_clean[0:8] ) _a : str = spanish_id_clean[8] except ValueError as ex: raise ValueError(__a ) from ex if letter.isdigit(): raise ValueError(__a ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
14
1
class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=None , _a=None ) -> List[str]: _a : List[Any] = data _a : List[str] = previous _a : Union[str, Any] = next_node def __str__( self ) -> str: return F"""{self.data}""" def __lowercase ( self ) -> int: return self.data def __lowercase ( self ) -> Union[str, Any]: return self.next def __lowercase ( self ) -> str: return self.previous class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a ) -> str: _a : int = head def __iter__( self ) -> List[Any]: return self def __lowercase ( self ) -> Optional[int]: if not self.current: raise StopIteration else: _a : Optional[int] = self.current.get_data() _a : Dict = self.current.get_next() return value class UpperCAmelCase_ : """simple docstring""" def __init__( self ) -> str: _a : Tuple = None # First node in list _a : List[str] = None # Last node in list def __str__( self ) -> List[str]: _a : Optional[int] = self.head _a : int = [] while current is not None: nodes.append(current.get_data() ) _a : Optional[Any] = current.get_next() return " ".join(str(_a ) for node in nodes ) def __contains__( self , _a ) -> Union[str, Any]: _a : Optional[Any] = self.head while current: if current.get_data() == value: return True _a : Optional[Any] = current.get_next() return False def __iter__( self ) -> Dict: return LinkedListIterator(self.head ) def __lowercase ( self ) -> Any: if self.head: return self.head.get_data() return None def __lowercase ( self ) -> Tuple: if self.tail: return self.tail.get_data() return None def __lowercase ( self , _a ) -> None: if self.head is None: _a : str = node _a : Tuple = node else: self.insert_before_node(self.head , _a ) def __lowercase ( self , _a ) -> None: if self.head is None: self.set_head(_a ) else: self.insert_after_node(self.tail , _a ) def __lowercase ( self , _a ) -> None: _a : str = Node(_a ) if self.head is None: self.set_head(_a ) else: self.set_tail(_a ) def __lowercase ( self , _a , _a ) -> None: _a : List[str] = node _a : str = node.previous if node.get_previous() is None: _a : Optional[int] = node_to_insert else: _a : List[str] = node_to_insert _a : List[str] = node_to_insert def __lowercase ( self , _a , _a ) -> None: _a : Union[str, Any] = node _a : List[str] = node.next if node.get_next() is None: _a : Any = node_to_insert else: _a : List[str] = node_to_insert _a : Union[str, Any] = node_to_insert def __lowercase ( self , _a , _a ) -> None: _a : List[Any] = 1 _a : Any = Node(_a ) _a : Any = self.head while node: if current_position == position: self.insert_before_node(_a , _a ) return current_position += 1 _a : Tuple = node.next self.insert_after_node(self.tail , _a ) def __lowercase ( self , _a ) -> Node: _a : Optional[Any] = self.head while node: if node.get_data() == item: return node _a : Any = node.get_next() raise Exception('''Node not found''' ) def __lowercase ( self , _a ) -> Union[str, Any]: if (node := self.get_node(_a )) is not None: if node == self.head: _a : Optional[Any] = self.head.get_next() if node == self.tail: _a : Any = self.tail.get_previous() self.remove_node_pointers(_a ) @staticmethod def __lowercase ( _a ) -> None: if node.get_next(): _a : Optional[int] = node.previous if node.get_previous(): _a : List[Any] = node.next _a : Optional[int] = None _a : Tuple = None def __lowercase ( self ) -> str: return self.head is None def __UpperCAmelCase ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
14
from random import randint from tempfile import TemporaryFile import numpy as np def __UpperCAmelCase ( __a : Optional[Any] ,__a : int ,__a : Any ) -> int: """simple docstring""" _a : int = 0 if start < end: _a : Tuple = randint(__a ,__a ) _a : Tuple = a[end] _a : List[str] = a[pivot] _a : Any = temp _a , _a : Optional[int] = _in_place_partition(__a ,__a ,__a ) count += _in_place_quick_sort(__a ,__a ,p - 1 ) count += _in_place_quick_sort(__a ,p + 1 ,__a ) return count def __UpperCAmelCase ( __a : List[Any] ,__a : Tuple ,__a : Dict ) -> Dict: """simple docstring""" _a : Dict = 0 _a : Tuple = randint(__a ,__a ) _a : List[Any] = a[end] _a : str = a[pivot] _a : str = temp _a : Dict = start - 1 for index in range(__a ,__a ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value _a : int = new_pivot_index + 1 _a : Any = a[new_pivot_index] _a : Optional[int] = a[index] _a : str = temp _a : Union[str, Any] = a[new_pivot_index + 1] _a : Tuple = a[end] _a : Any = temp return new_pivot_index + 1, count a__ = TemporaryFile() a__ = 100 # 1000 elements are to be sorted a__ , a__ = 0, 1 # mean and standard deviation a__ = np.random.normal(mu, sigma, p) np.save(outfile, X) print('''The array is''') print(X) outfile.seek(0) # using the same array a__ = np.load(outfile) a__ = len(M) - 1 a__ = _in_place_quick_sort(M, 0, r) print( '''No of Comparisons for 100 elements selected from a standard normal distribution''' '''is :''' ) print(z)
14
1
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor a__ = logging.getLogger(__name__) a__ = 50 # max width of layer names a__ = 70 # max width of quantizer names def __UpperCAmelCase ( __a : Optional[Any] ) -> Dict: """simple docstring""" _a : List[str] = parser.add_argument_group('''quant_trainer arguments''' ) group.add_argument('''--wprec''' ,type=__a ,default=8 ,help='''weight precision''' ) group.add_argument('''--aprec''' ,type=__a ,default=8 ,help='''activation precision''' ) group.add_argument('''--quant-per-tensor''' ,action='''store_true''' ,help='''per tensor weight scaling''' ) group.add_argument('''--quant-disable''' ,action='''store_true''' ,help='''disable all quantizers''' ) group.add_argument('''--quant-disable-embeddings''' ,action='''store_true''' ,help='''disable all embeddings quantizers''' ) group.add_argument('''--quant-disable-keyword''' ,type=__a ,nargs='''+''' ,help='''disable quantizers by keyword''' ) group.add_argument('''--quant-disable-layer-module''' ,type=__a ,help='''disable quantizers by keyword under layer.''' ) group.add_argument('''--quant-enable-layer-module''' ,type=__a ,help='''enable quantizers by keyword under layer''' ) group.add_argument('''--calibrator''' ,default='''max''' ,help='''which quantization range calibrator to use''' ) group.add_argument('''--percentile''' ,default=__a ,type=__a ,help='''percentile for PercentileCalibrator''' ) group.add_argument('''--fuse-qkv''' ,action='''store_true''' ,help='''use the same scale factor for qkv''' ) group.add_argument('''--clip-gelu''' ,metavar='''N''' ,type=__a ,help='''clip gelu output maximum value to N''' ) group.add_argument( '''--recalibrate-weights''' ,action='''store_true''' ,help=( '''recalibrate weight amaxes by taking the max of the weights.''' ''' amaxes will be computed with the current quantization granularity (axis).''' ) ,) def __UpperCAmelCase ( __a : Union[str, Any] ) -> str: """simple docstring""" if args.calibrator == "max": _a : int = '''max''' elif args.calibrator == "percentile": if args.percentile is None: raise ValueError('''Specify --percentile when using percentile calibrator''' ) _a : List[str] = '''histogram''' elif args.calibrator == "mse": _a : Dict = '''histogram''' else: raise ValueError(F"""Invalid calibrator {args.calibrator}""" ) _a : Any = QuantDescriptor(num_bits=args.aprec ,calib_method=__a ) _a : Dict = QuantDescriptor(num_bits=args.wprec ,axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__a ) quant_nn.QuantLinear.set_default_quant_desc_weight(__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : Union[str, Any] ,__a : List[str]=False ,__a : Tuple=False ) -> str: """simple docstring""" logger.info('''Configuring Model for Quantization''' ) logger.info(F"""using quantization package {pytorch_quantization.__file__}""" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__a ,['''embeddings'''] ,which='''weight''' ,_disabled=__a ) if args.quant_disable: set_quantizer_by_name(__a ,[''''''] ,_disabled=__a ) if args.quant_disable_keyword: set_quantizer_by_name(__a ,args.quant_disable_keyword ,_disabled=__a ) if args.quant_disable_layer_module: set_quantizer_by_name(__a ,[R'''layer.\d+.''' + args.quant_disable_layer_module] ,_disabled=__a ) if args.quant_enable_layer_module: set_quantizer_by_name(__a ,[R'''layer.\d+.''' + args.quant_enable_layer_module] ,_disabled=__a ) if args.recalibrate_weights: recalibrate_weights(__a ) if args.fuse_qkv: fuse_qkv(__a ,__a ) if args.clip_gelu: clip_gelu(__a ,args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__a ) def __UpperCAmelCase ( __a : str ) -> Dict: """simple docstring""" logger.info('''Enabling Calibration''' ) for name, module in model.named_modules(): if name.endswith('''_quantizer''' ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"""{name:80}: {module}""" ) def __UpperCAmelCase ( __a : int ,__a : Union[str, Any] ) -> Optional[Any]: """simple docstring""" logger.info('''Loading calibrated amax''' ) for name, module in model.named_modules(): if name.endswith('''_quantizer''' ): if module._calibrator is not None: if isinstance(module._calibrator ,calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax('''percentile''' ,percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__a ) def __UpperCAmelCase ( __a : Any ,__a : int ) -> Tuple: """simple docstring""" def fusea(__a : Optional[Any] ,__a : List[Any] ,__a : int ): for mod in [qq, qk, qv]: if not hasattr(__a ,'''_amax''' ): print(''' WARNING: NO AMAX BUFFER''' ) return _a : int = qq._amax.detach().item() _a : int = qk._amax.detach().item() _a : Optional[int] = qv._amax.detach().item() _a : Any = max(__a ,__a ,__a ) qq._amax.fill_(__a ) qk._amax.fill_(__a ) qv._amax.fill_(__a ) logger.info(F""" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}""" ) for name, mod in model.named_modules(): if name.endswith('''.attention.self''' ): logger.info(F"""FUSE_QKV: {name:{name_width}}""" ) fusea(mod.matmul_q_input_quantizer ,mod.matmul_k_input_quantizer ,mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer ,mod.key._weight_quantizer ,mod.value._weight_quantizer ) def __UpperCAmelCase ( __a : Any ,__a : Any ) -> Any: """simple docstring""" for name, mod in model.named_modules(): if name.endswith('''.output.dense''' ) and not name.endswith('''attention.output.dense''' ): _a : Dict = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__a ) _a : List[str] = mod._input_quantizer._amax.data.detach().item() logger.info(F"""CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}""" ) def __UpperCAmelCase ( __a : List[str] ) -> str: """simple docstring""" for name, mod in model.named_modules(): if hasattr(__a ,'''_weight_quantizer''' ) and mod._weight_quantizer.axis is not None: _a : Optional[int] = mod.weight.shape[0] _a : List[str] = mod._weight_quantizer._amax.detach() _a : Optional[int] = torch.ones(__a ,dtype=amax.dtype ,device=amax.device ) * amax print(F"""expanding {name} {amax} -> {mod._weight_quantizer._amax}""" ) def __UpperCAmelCase ( __a : Optional[Any] ) -> Dict: """simple docstring""" for name, mod in model.named_modules(): if hasattr(__a ,'''_weight_quantizer''' ): if not hasattr(mod.weight_quantizer ,'''_amax''' ): print('''RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER''' ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) _a : Dict = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) _a : Tuple = set(range(len(mod.weight.size() ) ) ) - axis_set _a : str = pytorch_quantization.utils.reduce_amax(mod.weight ,axis=__a ,keepdims=__a ).detach() logger.info(F"""RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}""" ) _a : int = amax def __UpperCAmelCase ( __a : Union[str, Any] ,__a : int=25 ,__a : str=180 ,__a : Union[str, Any]=None ) -> str: """simple docstring""" if ignore is None: _a : Optional[int] = [] elif not isinstance(__a ,__a ): _a : Tuple = [ignore] _a : Optional[Any] = 0 for name, mod in model.named_modules(): if not hasattr(__a ,'''weight''' ): continue _a : Any = max(__a ,len(__a ) ) for name, mod in model.named_modules(): _a : Tuple = getattr(__a ,'''_input_quantizer''' ,__a ) _a : int = getattr(__a ,'''_weight_quantizer''' ,__a ) if not hasattr(__a ,'''weight''' ): continue if type(__a ) in ignore: continue if [True for s in ignore if type(__a ) is str and s in name]: continue _a : List[str] = F"""Act:{input_q.extra_repr()}""" _a : int = F"""Wgt:{weight_q.extra_repr()}""" _a : int = F"""{name:{name_width}} {act_str} {wgt_str}""" if len(__a ) <= line_width: logger.info(__a ) else: logger.info(F"""{name:{name_width}} {act_str}""" ) logger.info(F"""{' ':{name_width}} {wgt_str}""" ) def __UpperCAmelCase ( __a : str ) -> Optional[Any]: """simple docstring""" _a : Optional[Any] = 0 for name, mod in model.named_modules(): if isinstance(__a ,pytorch_quantization.nn.TensorQuantizer ): print(F"""{name:80} {mod}""" ) count += 1 print(F"""{count} TensorQuantizers found in model""" ) def __UpperCAmelCase ( __a : Any ,__a : Union[str, Any] ,__a : Tuple ,__a : Dict ,__a : List[Any] ) -> int: """simple docstring""" _a : int = getattr(__a ,__a ,__a ) if quantizer_mod is not None: assert hasattr(__a ,__a ) setattr(__a ,__a ,__a ) else: logger.warning(F"""{name} has no {quantizer}""" ) def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Optional[Any] ,__a : Any="both" ,**__a : Optional[Any] ) -> str: """simple docstring""" _a : List[str] = F"""Warning: changing {which} quantizers of {name:{qname_width}}""" for k, v in kwargs.items(): s += F""" {k}={v}""" if which in ["input", "both"]: set_quantizer(__a ,__a ,'''_input_quantizer''' ,__a ,__a ) if which in ["weight", "both"]: set_quantizer(__a ,__a ,'''_weight_quantizer''' ,__a ,__a ) logger.info(__a ) def __UpperCAmelCase ( __a : str ,__a : Dict ,**__a : Union[str, Any] ) -> Any: """simple docstring""" for name, mod in model.named_modules(): if hasattr(__a ,'''_input_quantizer''' ) or hasattr(__a ,'''_weight_quantizer''' ): for n in names: if re.search(__a ,__a ): set_quantizers(__a ,__a ,**__a ) elif name.endswith('''_quantizer''' ): for n in names: if re.search(__a ,__a ): _a : List[str] = F"""Warning: changing {name:{name_width}}""" for k, v in kwargs.items(): s += F""" {k}={v}""" setattr(__a ,__a ,__a ) logger.info(__a )
14
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = MgpstrTokenizer UpperCAmelCase__ : int = False UpperCAmelCase__ : Union[str, Any] = {} UpperCAmelCase__ : List[Any] = False def __lowercase ( self ) -> Any: super().setUp() # fmt: off _a : Tuple = ['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _a : Optional[int] = dict(zip(_a , range(len(_a ) ) ) ) _a : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) def __lowercase ( self , **_a ) -> Dict: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __lowercase ( self , _a ) -> Tuple: _a : List[str] = '''tester''' _a : Optional[Any] = '''tester''' return input_text, output_text @unittest.skip('''MGP-STR always lower cases letters.''' ) def __lowercase ( self ) -> Any: pass def __lowercase ( self ) -> Any: _a : Union[str, Any] = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a : int = '''[SPECIAL_TOKEN]''' tokenizer.add_special_tokens({'''cls_token''': special_token} ) _a : Tuple = tokenizer.encode([special_token] , add_special_tokens=_a ) self.assertEqual(len(_a ) , 1 ) _a : Tuple = tokenizer.decode(_a , skip_special_tokens=_a ) self.assertTrue(special_token not in decoded ) def __lowercase ( self ) -> Tuple: _a : List[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a , _a : int = self.get_input_output_texts(_a ) _a : List[str] = tokenizer.tokenize(_a ) _a : Optional[int] = tokenizer.convert_tokens_to_ids(_a ) _a : Tuple = tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Optional[int] = tokenizer.convert_ids_to_tokens(_a ) self.assertNotEqual(len(_a ) , 0 ) _a : int = tokenizer.decode(_a ) self.assertIsInstance(_a , _a ) self.assertEqual(text_a.replace(''' ''' , '''''' ) , _a ) @unittest.skip('''MGP-STR tokenizer only handles one sequence.''' ) def __lowercase ( self ) -> List[str]: pass @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer''' ) def __lowercase ( self ) -> Optional[Any]: pass
14
1
import copy import inspect import unittest from transformers import PretrainedConfig, SwiftFormerConfig from transformers.testing_utils import ( require_torch, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwiftFormerForImageClassification, SwiftFormerModel from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=1_3 , _a=3 , _a=True , _a=True , _a=0.1 , _a=0.1 , _a=2_2_4 , _a=1_0_0_0 , _a=[3, 3, 6, 4] , _a=[4_8, 5_6, 1_1_2, 2_2_0] , ) -> Tuple: _a : Dict = parent _a : Optional[int] = batch_size _a : Optional[Any] = num_channels _a : Union[str, Any] = is_training _a : Tuple = use_labels _a : Dict = hidden_dropout_prob _a : List[Any] = attention_probs_dropout_prob _a : Dict = num_labels _a : List[str] = image_size _a : Dict = layer_depths _a : str = embed_dims def __lowercase ( self ) -> Optional[Any]: _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : int = None if self.use_labels: _a : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) _a : Dict = self.get_config() return config, pixel_values, labels def __lowercase ( self ) -> int: return SwiftFormerConfig( depths=self.layer_depths , embed_dims=self.embed_dims , mlp_ratio=4 , downsamples=[True, True, True, True] , hidden_act='''gelu''' , num_labels=self.num_labels , down_patch_size=3 , down_stride=2 , down_pad=1 , drop_rate=0.0 , drop_path_rate=0.0 , use_layer_scale=_a , layer_scale_init_value=1e-5 , ) def __lowercase ( self , _a , _a , _a ) -> str: _a : List[Any] = SwiftFormerModel(config=_a ) model.to(_a ) model.eval() _a : Optional[int] = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dims[-1], 7, 7) ) def __lowercase ( self , _a , _a , _a ) -> Optional[Any]: _a : List[str] = self.num_labels _a : Optional[int] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : List[str] = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) _a : Union[str, Any] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : Optional[Any] = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowercase ( self ) -> Tuple: ((_a) , (_a) , (_a)) : Optional[int] = self.prepare_config_and_inputs() _a : List[Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else () UpperCAmelCase__ : Optional[int] = ( {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Optional[Any] = False UpperCAmelCase__ : str = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : str = False def __lowercase ( self ) -> Optional[int]: _a : Union[str, Any] = SwiftFormerModelTester(self ) _a : int = ConfigTester( self , config_class=_a , has_text_modality=_a , hidden_size=3_7 , num_attention_heads=1_2 , num_hidden_layers=1_2 , ) def __lowercase ( self ) -> int: self.config_tester.run_common_tests() @unittest.skip(reason='''SwiftFormer does not use inputs_embeds''' ) def __lowercase ( self ) -> Union[str, Any]: pass def __lowercase ( self ) -> Dict: _a , _a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Any = model_class(_a ) _a : int = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def __lowercase ( self ) -> str: _a , _a : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Optional[int] = model_class(_a ) _a : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Tuple = [*signature.parameters.keys()] _a : List[str] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowercase ( self ) -> int: _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self ) -> Optional[int]: _a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self ) -> Optional[Any]: for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Any = SwiftFormerModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason='''SwiftFormer does not output attentions''' ) def __lowercase ( self ) -> List[Any]: pass def __lowercase ( self ) -> int: def check_hidden_states_output(_a , _a , _a ): _a : Optional[int] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Union[str, Any] = model(**self._prepare_for_class(_a , _a ) ) _a : Optional[Any] = outputs.hidden_states _a : Union[str, Any] = 8 self.assertEqual(len(_a ) , _a ) # TODO # SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width) # with the width and height being successively divided by 2, after every 2 blocks for i in range(len(_a ) ): self.assertEqual( hidden_states[i].shape , torch.Size( [ self.model_tester.batch_size, self.model_tester.embed_dims[i // 2], (self.model_tester.image_size // 4) // 2 ** (i // 2), (self.model_tester.image_size // 4) // 2 ** (i // 2), ] ) , ) _a , _a : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : str = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : List[str] = True check_hidden_states_output(_a , _a , _a ) def __lowercase ( self ) -> str: def _config_zero_init(_a ): _a : List[Any] = copy.deepcopy(_a ) for key in configs_no_init.__dict__.keys(): if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key: setattr(_a , _a , 1e-1_0 ) if isinstance(getattr(_a , _a , _a ) , _a ): _a : int = _config_zero_init(getattr(_a , _a ) ) setattr(_a , _a , _a ) return configs_no_init _a , _a : Any = self.model_tester.prepare_config_and_inputs_for_common() _a : Dict = _config_zero_init(_a ) for model_class in self.all_model_classes: _a : Dict = model_class(config=_a ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9) / 1e9).round().item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __lowercase ( self ) -> Optional[Any]: pass def __UpperCAmelCase ( ) -> Optional[Any]: """simple docstring""" _a : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self ) -> str: return ViTImageProcessor.from_pretrained('''MBZUAI/swiftformer-xs''' ) if is_vision_available() else None @slow def __lowercase ( self ) -> Dict: _a : Any = SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs''' ).to(_a ) _a : Any = self.default_image_processor _a : Any = prepare_img() _a : Any = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _a : Optional[Any] = model(**_a ) # verify the logits _a : List[str] = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _a ) _a : int = torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
14
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> List[Any]: _a : int = 0 def __lowercase ( self ) -> List[str]: _a : Dict = AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32''' ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : Optional[Any] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: # Ensure we can load the image processor from the feature extractor config with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' _a : Any = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Dict = CLIPConfig() # Create a dummy config file with image_proceesor_type _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : List[str] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally _a : Tuple = AutoImageProcessor.from_pretrained(_a ).to_dict() config_dict.pop('''image_processor_type''' ) _a : Tuple = CLIPImageProcessor(**_a ) # save in new folder model_config.save_pretrained(_a ) config.save_pretrained(_a ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) # make sure private variable is not incorrectly saved _a : Optional[int] = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Any: with self.assertRaisesRegex( _a , '''clip-base is not a local folder and is not a valid model identifier''' ): _a : Dict = AutoImageProcessor.from_pretrained('''clip-base''' ) def __lowercase ( self ) -> List[Any]: with self.assertRaisesRegex( _a , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): _a : List[str] = AutoImageProcessor.from_pretrained(_a , revision='''aaaaaa''' ) def __lowercase ( self ) -> Dict: with self.assertRaisesRegex( _a , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): _a : Optional[int] = AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowercase ( self ) -> Union[str, Any]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_a ): _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_a ): _a : Optional[Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) _a : Union[str, Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a , trust_remote_code=_a ) self.assertEqual(reloaded_image_processor.__class__.__name__ , '''NewImageProcessor''' ) def __lowercase ( self ) -> Dict: try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_a ): AutoImageProcessor.register(_a , _a ) with tempfile.TemporaryDirectory() as tmpdirname: _a : int = Path(_a ) / '''preprocessor_config.json''' _a : int = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : int = CustomImageProcessor.from_pretrained(_a ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def __lowercase ( self ) -> Union[str, Any]: class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = True try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # If remote code is not set, the default is to use local _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. _a : int = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub _a : Dict = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(not hasattr(_a , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
14
1
from scipy.stats import spearmanr import datasets a__ = ''' The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. ''' a__ = ''' Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {\'spearmanr\': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results[\'spearmanr\']) -0.7 >>> print(round(results[\'spearmanr_pvalue\'], 2)) 0.19 ''' a__ = R'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'''] , ) def __lowercase ( self , _a , _a , _a=False ) -> str: _a : int = spearmanr(_a , _a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
14
from __future__ import annotations from dataclasses import dataclass @dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : float UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase__ : TreeNode | None = None def __UpperCAmelCase ( __a : TreeNode | None ) -> bool: """simple docstring""" def is_valid_tree(__a : TreeNode | None ) -> bool: if node is None: return True if not isinstance(__a ,__a ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(__a ): raise ValueError( '''Each node should be type of TreeNode and data should be float.''' ) def is_binary_search_tree_recursive_check( __a : TreeNode | None ,__a : float ,__a : float ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left ,__a ,node.data ) and is_binary_search_tree_recursive_check( node.right ,node.data ,__a ) ) return is_binary_search_tree_recursive_check(__a ,-float('''inf''' ) ,float('''inf''' ) ) if __name__ == "__main__": import doctest doctest.testmod()
14
1
import os import random import sys from . import cryptomath_module as cryptoMath # noqa: N812 from . import rabin_miller as rabinMiller # noqa: N812 def __UpperCAmelCase ( ) -> None: """simple docstring""" print('''Making key files...''' ) make_key_files('''rsa''' ,1_024 ) print('''Key files generation successful.''' ) def __UpperCAmelCase ( __a : int ) -> tuple[tuple[int, int], tuple[int, int]]: """simple docstring""" print('''Generating prime p...''' ) _a : Union[str, Any] = rabinMiller.generate_large_prime(__a ) print('''Generating prime q...''' ) _a : Optional[Any] = rabinMiller.generate_large_prime(__a ) _a : Union[str, Any] = p * q print('''Generating e that is relatively prime to (p - 1) * (q - 1)...''' ) while True: _a : Optional[int] = random.randrange(2 ** (key_size - 1) ,2 ** (key_size) ) if cryptoMath.gcd(__a ,(p - 1) * (q - 1) ) == 1: break print('''Calculating d that is mod inverse of e...''' ) _a : Optional[int] = cryptoMath.find_mod_inverse(__a ,(p - 1) * (q - 1) ) _a : Optional[Any] = (n, e) _a : int = (n, d) return (public_key, private_key) def __UpperCAmelCase ( __a : str ,__a : int ) -> None: """simple docstring""" if os.path.exists(F"""{name}_pubkey.txt""" ) or os.path.exists(F"""{name}_privkey.txt""" ): print('''\nWARNING:''' ) print( F"""\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n""" '''Use a different name or delete these files and re-run this program.''' ) sys.exit() _a , _a : List[str] = generate_key(__a ) print(F"""\nWriting public key to file {name}_pubkey.txt...""" ) with open(F"""{name}_pubkey.txt""" ,'''w''' ) as out_file: out_file.write(F"""{key_size},{public_key[0]},{public_key[1]}""" ) print(F"""Writing private key to file {name}_privkey.txt...""" ) with open(F"""{name}_privkey.txt""" ,'''w''' ) as out_file: out_file.write(F"""{key_size},{private_key[0]},{private_key[1]}""" ) if __name__ == "__main__": main()
14
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake a__ = numpy.array([0, 0]) a__ = numpy.array([0.5, 0.8660254]) a__ = numpy.array([1, 0]) a__ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def __UpperCAmelCase ( __a : list[numpy.ndarray] ,__a : int ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = initial_vectors for _ in range(__a ): _a : int = iteration_step(__a ) return vectors def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = [] for i, start_vector in enumerate(vectors[:-1] ): _a : str = vectors[i + 1] new_vectors.append(__a ) _a : Optional[int] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 ,60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def __UpperCAmelCase ( __a : numpy.ndarray ,__a : float ) -> numpy.ndarray: """simple docstring""" _a : Tuple = numpy.radians(__a ) _a , _a : List[Any] = numpy.cos(__a ), numpy.sin(__a ) _a : Dict = numpy.array(((c, -s), (s, c)) ) return numpy.dot(__a ,__a ) def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> None: """simple docstring""" _a : str = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() _a , _a : Optional[int] = zip(*__a ) plt.plot(__a ,__a ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() a__ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
14
1
import argparse import json import os import tensorstore as ts import torch from flax import serialization from flax.traverse_util import flatten_dict, unflatten_dict from tensorflow.io import gfile from transformers.modeling_utils import dtype_byte_size from transformers.models.switch_transformers.convert_switch_transformers_original_flax_checkpoint_to_pytorch import ( rename_keys, ) from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME from transformers.utils.hub import convert_file_size_to_int def __UpperCAmelCase ( __a : List[str] ,__a : Dict ) -> List[str]: """simple docstring""" if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 3: # expert layer _a : Tuple = flax_key_tuple[:-1] + ('''weight''',) _a : Union[str, Any] = torch.permute(__a ,(0, 2, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(__a ): # linear layer _a : Optional[Any] = flax_key_tuple[:-1] + ('''weight''',) _a : Any = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: _a : Tuple = flax_key_tuple[:-1] + ('''weight''',) return flax_key_tuple, flax_tensor def __UpperCAmelCase ( __a : int ,__a : List[str] ,__a : List[str] ) -> Optional[int]: """simple docstring""" if "metadata" in layer: _a : Optional[Any] = layer.split('''metadata''' ) _a : Optional[int] = ''''''.join(split_layer[0] )[:-1] _a : Dict = [tuple(('''metadata''' + split_layer[1]).split('''/''' ) )] elif "kvstore" in layer: _a : List[Any] = layer.split('''kvstore''' ) _a : List[str] = ''''''.join(split_layer[0] )[:-1] _a : str = [tuple(('''kvstore''' + split_layer[1]).split('''/''' ) )] else: _a : Tuple = layer.split('''/''' ) _a : Any = '''/'''.join(split_layer[:-1] ) _a : Optional[Any] = (split_layer[-1],) if "kvstore/path" in layer: _a : Tuple = F"""{switch_checkpoint_path}/{checkpoint_info[layer]}""" elif "kvstore/driver" in layer: _a : str = '''file''' else: _a : Optional[Any] = checkpoint_info[layer] return curr_real_layer_name, split_layer, content def __UpperCAmelCase ( __a : Optional[Any] ,__a : int ) -> Optional[Any]: """simple docstring""" _a : int = rename_keys(__a ) _a : Any = {} for k, v in current_block.items(): _a : Dict = v _a : int = new_current_block torch.save(__a ,__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : int ,__a : str ,__a : int ,__a : str = WEIGHTS_NAME ) -> Any: """simple docstring""" _a : Optional[Any] = convert_file_size_to_int(__a ) _a : List[str] = [] _a : List[Any] = {} _a : int = 0 _a : List[Any] = 0 os.makedirs(__a ,exist_ok=__a ) with gfile.GFile(switch_checkpoint_path + '''/checkpoint''' ,'''rb''' ) as fp: _a : List[Any] = serialization.msgpack_restore(fp.read() )['''optimizer''']['''target'''] _a : Union[str, Any] = flatten_dict(__a ,sep='''/''' ) _a : Optional[int] = {} for layer in checkpoint_info.keys(): _a , _a , _a : int = get_key_and_tensorstore_dict( __a ,__a ,__a ) if curr_real_layer_name in all_layers: _a : Any = content else: _a : Dict = {split_layer[-1]: content} for key in all_layers.keys(): # open tensorstore file _a : Optional[Any] = ts.open(unflatten_dict(all_layers[key] ) ).result().read().result() _a : Optional[Any] = torch.tensor(__a ) _a : List[str] = raw_weights.numel() * dtype_byte_size(raw_weights.dtype ) # use the renaming pattern from the small conversion scripts _a , _a : str = rename_base_flax_keys(tuple(key.split('''/''' ) ) ,__a ) _a : int = '''/'''.join(__a ) # If this weight is going to tip up over the maximal size, we split. if current_block_size + weight_size > max_shard_size: _a : Union[str, Any] = os.path.join( __a ,weights_name.replace('''.bin''' ,F"""-{len(__a )+1:05d}-of-???.bin""" ) ) rename_and_save_block(__a ,__a ) sharded_state_dicts.append(current_block.keys() ) del current_block _a : List[Any] = {} _a : Optional[int] = 0 _a : Dict = raw_weights.to(getattr(__a ,__a ) ) current_block_size += weight_size total_size += weight_size # Add the last block _a : Dict = os.path.join(__a ,weights_name.replace('''.bin''' ,F"""-{len(__a )+1:05d}-of-???.bin""" ) ) rename_and_save_block(__a ,__a ) sharded_state_dicts.append(current_block.keys() ) # If we only have one shard, we return it if len(__a ) == 1: return {weights_name: sharded_state_dicts[0]}, None # Otherwise, let's build the index _a : int = {} _a : Any = {} for idx, shard in enumerate(__a ): _a : int = weights_name.replace( '''.bin''' ,F"""-{idx+1:05d}-of-{len(__a ):05d}.bin""" ) # len(sharded_state_dicts):05d} _a : Optional[Any] = os.path.join(__a ,weights_name.replace('''.bin''' ,F"""-{idx+1:05d}-of-???.bin""" ) ) os.rename(__a ,os.path.join(__a ,__a ) ) _a : Optional[Any] = shard for key in shard: _a : str = shard_file # Add the metadata _a : str = {'''total_size''': total_size} _a : str = {'''metadata''': metadata, '''weight_map''': weight_map} with open(os.path.join(__a ,__a ) ,'''w''' ,encoding='''utf-8''' ) as f: _a : Optional[int] = json.dumps(__a ,indent=2 ,sort_keys=__a ) + '''\n''' f.write(__a ) return metadata, index if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--switch_t5x_checkpoint_path''', default='''/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128/checkpoint_634600''', type=str, required=False, help='''Path to a directory containing a folder per layer. Follows the original Google format.''', ) parser.add_argument('''--max_shard_size''', default='''10GB''', required=False, help='''Max shard size''') parser.add_argument('''--dtype''', default='''bfloat16''', type=str, required=False, help='''dtype of the saved model''') parser.add_argument( '''--pytorch_dump_folder_path''', default='''/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128-converted''', type=str, required=False, help='''Path to the output pytorch model.''', ) a__ = parser.parse_args() shard_on_the_fly( args.switch_tax_checkpoint_path, args.pytorch_dump_folder_path, args.max_shard_size, args.dtype, ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration, TaTokenizer _a : Optional[Any] = SwitchTransformersConfig.from_pretrained('''google/switch-base-8''' ) config.save_pretrained('''/home/arthur_huggingface_co/transformers/switch_converted''' ) _a : Tuple = SwitchTransformersForConditionalGeneration.from_pretrained( '''/home/arthur_huggingface_co/transformers/switch_converted''' ,device_map='''auto''' ) _a : Any = TaTokenizer.from_pretrained('''t5-small''' ) _a : Optional[int] = '''A <extra_id_0> walks into a bar a orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''' _a : str = tokenizer(__a ,return_tensors='''pt''' ).input_ids _a : Any = model.generate(__a ,decoder_start_token_id=0 ) print(tokenizer.decode(out[0] ) )
14
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def __UpperCAmelCase ( __a : Tuple ,__a : Dict ,__a : List[str] ,__a : Optional[Any] ,__a : Tuple ) -> Dict: """simple docstring""" with open(__a ) as metadata_file: _a : Optional[Any] = json.load(__a ) _a : List[Any] = LukeConfig(use_entity_aware_attention=__a ,**metadata['''model_config'''] ) # Load in the weights from the checkpoint_path _a : Optional[Any] = torch.load(__a ,map_location='''cpu''' )['''module'''] # Load the entity vocab file _a : Any = load_original_entity_vocab(__a ) # add an entry for [MASK2] _a : Union[str, Any] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 _a : Dict = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks _a : Optional[int] = AddedToken('''<ent>''' ,lstrip=__a ,rstrip=__a ) _a : Tuple = AddedToken('''<ent2>''' ,lstrip=__a ,rstrip=__a ) 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(__a ) with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''r''' ) as f: _a : List[str] = json.load(__a ) _a : Tuple = '''MLukeTokenizer''' with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''w''' ) as f: json.dump(__a ,__a ) with open(os.path.join(__a ,MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) ,'''w''' ) as f: json.dump(__a ,__a ) _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) # Initialize the embeddings of the special tokens _a : str = tokenizer.convert_tokens_to_ids(['''@'''] )[0] _a : Tuple = tokenizer.convert_tokens_to_ids(['''#'''] )[0] _a : Any = state_dict['''embeddings.word_embeddings.weight'''] _a : Optional[int] = word_emb[ent_init_index].unsqueeze(0 ) _a : Any = word_emb[enta_init_index].unsqueeze(0 ) _a : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: _a : Tuple = state_dict[bias_name] _a : Optional[Any] = decoder_bias[ent_init_index].unsqueeze(0 ) _a : Optional[int] = decoder_bias[enta_init_index].unsqueeze(0 ) _a : Dict = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: _a : Tuple = F"""encoder.layer.{layer_index}.attention.self.""" _a : List[Any] = state_dict[prefix + matrix_name] _a : Dict = state_dict[prefix + matrix_name] _a : List[Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks _a : Union[str, Any] = state_dict['''entity_embeddings.entity_embeddings.weight'''] _a : Optional[int] = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' _a : int = state_dict['''entity_predictions.bias'''] _a : int = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Optional[Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) _a : Optional[int] = LukeForMaskedLM(config=__a ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) _a : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): _a : Optional[int] = state_dict[key] else: _a : Tuple = state_dict[key] _a , _a : int = model.load_state_dict(__a ,strict=__a ) if set(__a ) != {"luke.embeddings.position_ids"}: raise ValueError(F"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(__a ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ,task='''entity_classification''' ) _a : int = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' _a : List[Any] = (0, 9) _a : Tuple = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : int = model(**__a ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base _a : List[str] = torch.Size((1, 33, 768) ) _a : Union[str, Any] = torch.tensor([[0.08_92, 0.05_96, -0.28_19], [0.01_34, 0.11_99, 0.05_73], [-0.01_69, 0.09_27, 0.06_44]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base _a : str = torch.Size((1, 1, 768) ) _a : List[Any] = torch.tensor([[-0.14_82, 0.06_09, 0.03_22]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" F""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify masked word/entity prediction _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) _a : Dict = '''Tokyo is the capital of <mask>.''' _a : List[str] = (24, 30) _a : Optional[int] = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : Optional[Any] = model(**__a ) _a : Any = encoding['''input_ids'''][0].tolist() _a : Optional[Any] = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) _a : Any = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(__a ) _a : Any = outputs.entity_logits[0][0].argmax().item() _a : Optional[Any] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(__a ) ) model.save_pretrained(__a ) def __UpperCAmelCase ( __a : List[Any] ) -> int: """simple docstring""" _a : Union[str, Any] = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] _a : int = [json.loads(__a ) for line in open(__a )] _a : List[Any] = {} for entry in data: _a : int = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: _a : List[Any] = entity_id break _a : Dict = F"""{language}:{entity_name}""" _a : int = entity_id return new_mapping if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) a__ = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
14
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a__ = { '''configuration_clipseg''': [ '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPSegConfig''', '''CLIPSegTextConfig''', '''CLIPSegVisionConfig''', ], '''processing_clipseg''': ['''CLIPSegProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = [ '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPSegModel''', '''CLIPSegPreTrainedModel''', '''CLIPSegTextModel''', '''CLIPSegVisionModel''', '''CLIPSegForImageSegmentation''', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys a__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
14
from scipy.stats import spearmanr import datasets a__ = ''' The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. ''' a__ = ''' Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {\'spearmanr\': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results[\'spearmanr\']) -0.7 >>> print(round(results[\'spearmanr_pvalue\'], 2)) 0.19 ''' a__ = R'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'''] , ) def __lowercase ( self , _a , _a , _a=False ) -> str: _a : int = spearmanr(_a , _a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
14
1
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 a__ = logging.get_logger(__name__) a__ = {'''vocab_file''': '''spiece.model'''} a__ = { '''vocab_file''': { '''TsinghuaAI/CPM-Generate''': '''https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model''', } } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self , _a , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , _a = None , **_a , ) -> None: _a : Optional[Any] = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token _a : Optional[int] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , sp_model_kwargs=self.sp_model_kwargs , **_a , ) _a : List[str] = 3 _a : str = do_lower_case _a : Optional[int] = remove_space _a : Optional[Any] = keep_accents _a : Optional[Any] = vocab_file _a : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_a ) 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.''' ) _a : Optional[Any] = jieba _a : Optional[int] = str.maketrans(''' \n''' , '''\u2582\u2583''' ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def __lowercase ( self ) -> int: return len(self.sp_model ) def __lowercase ( self ) -> Optional[int]: _a : List[str] = {self.convert_ids_to_tokens(_a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> Optional[int]: _a : Any = self.__dict__.copy() _a : Optional[Any] = None return state def __setstate__( self , _a ) -> Union[str, Any]: _a : Union[str, Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _a : Dict = {} _a : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowercase ( self , _a ) -> Any: if self.remove_space: _a : Optional[Any] = ''' '''.join(inputs.strip().split() ) else: _a : Optional[int] = inputs _a : Optional[int] = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: _a : Union[str, Any] = unicodedata.normalize('''NFKD''' , _a ) _a : List[Any] = ''''''.join([c for c in outputs if not unicodedata.combining(_a )] ) if self.do_lower_case: _a : Tuple = outputs.lower() return outputs def __lowercase ( self , _a ) -> List[str]: _a : Optional[Any] = self.preprocess_text(_a ) _a : Optional[int] = self.sp_model.encode(_a , out_type=_a ) _a : str = [] for piece in pieces: if len(_a ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): _a : Tuple = self.sp_model.EncodeAsPieces(piece[:-1].replace(_a , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: _a : List[Any] = cur_pieces[1:] else: _a : Dict = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_a ) else: new_pieces.append(_a ) return new_pieces def __lowercase ( self , _a ) -> Dict: return self.sp_model.PieceToId(_a ) def __lowercase ( self , _a ) -> str: return self.sp_model.IdToPiece(_a ) def __lowercase ( self , _a ) -> List[Any]: _a : Union[str, Any] = ''''''.join(_a ).replace(_a , ''' ''' ).strip() return out_string def __lowercase ( self , _a , _a = None ) -> List[int]: _a : str = [self.sep_token_id] _a : Any = [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 __lowercase ( self , _a , _a = None , _a = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_a , token_ids_a=_a , already_has_special_tokens=_a ) if token_ids_a is not None: return ([0] * len(_a )) + [1] + ([0] * len(_a )) + [1, 1] return ([0] * len(_a )) + [1, 1] def __lowercase ( self , _a , _a = None ) -> List[int]: _a : Tuple = [self.sep_token_id] _a : Tuple = [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 __lowercase ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _a : int = os.path.join( _a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _a ) elif not os.path.isfile(self.vocab_file ): with open(_a , '''wb''' ) as fi: _a : Dict = self.sp_model.serialized_model_proto() fi.write(_a ) return (out_vocab_file,) def __lowercase ( self , *_a , **_a ) -> Tuple: _a : Any = super()._decode(*_a , **_a ) _a : Union[str, Any] = text.replace(''' ''' , '''''' ).replace('''\u2582''' , ''' ''' ).replace('''\u2583''' , '''\n''' ) return text
14
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def __UpperCAmelCase ( __a : bytes ,__a : int ) -> np.array: """simple docstring""" _a : int = F"""{sampling_rate}""" _a : str = '''1''' _a : Optional[int] = '''f32le''' _a : Optional[Any] = [ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(__a ,stdin=subprocess.PIPE ,stdout=subprocess.PIPE ) as ffmpeg_process: _a : Any = ffmpeg_process.communicate(__a ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error _a : Optional[Any] = output_stream[0] _a : Optional[int] = np.frombuffer(__a ,np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def __UpperCAmelCase ( __a : int ,__a : float ,__a : str = "f32le" ,) -> str: """simple docstring""" _a : Dict = F"""{sampling_rate}""" _a : Optional[Any] = '''1''' if format_for_conversion == "s16le": _a : Dict = 2 elif format_for_conversion == "f32le": _a : Optional[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) _a : Dict = platform.system() if system == "Linux": _a : Dict = '''alsa''' _a : Union[str, Any] = '''default''' elif system == "Darwin": _a : Union[str, Any] = '''avfoundation''' _a : List[str] = ''':0''' elif system == "Windows": _a : Optional[int] = '''dshow''' _a : str = '''default''' _a : Tuple = [ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] _a : Any = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample _a : str = _ffmpeg_stream(__a ,__a ) for item in iterator: yield item def __UpperCAmelCase ( __a : int ,__a : float ,__a : Optional[int] = None ,__a : Optional[Union[Tuple[float, float], float]] = None ,__a : str = "f32le" ,) -> Optional[int]: """simple docstring""" if stream_chunk_s is not None: _a : Tuple = stream_chunk_s else: _a : Tuple = chunk_length_s _a : Tuple = ffmpeg_microphone(__a ,__a ,format_for_conversion=__a ) if format_for_conversion == "s16le": _a : Any = np.intaa _a : Optional[int] = 2 elif format_for_conversion == "f32le": _a : Dict = np.floataa _a : List[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) if stride_length_s is None: _a : List[Any] = chunk_length_s / 6 _a : Optional[int] = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__a ,(int, float) ): _a : Optional[Any] = [stride_length_s, stride_length_s] _a : Optional[Any] = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample _a : str = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample _a : Optional[Any] = datetime.datetime.now() _a : Tuple = datetime.timedelta(seconds=__a ) for item in chunk_bytes_iter(__a ,__a ,stride=(stride_left, stride_right) ,stream=__a ): # Put everything back in numpy scale _a : Dict = np.frombuffer(item['''raw'''] ,dtype=__a ) _a : Dict = ( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) _a : str = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def __UpperCAmelCase ( __a : Optional[int] ,__a : int ,__a : Tuple[int, int] ,__a : bool = False ) -> Optional[int]: """simple docstring""" _a : Any = b'''''' _a , _a : List[str] = stride if stride_left + stride_right >= chunk_len: raise ValueError( F"""Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}""" ) _a : List[str] = 0 for raw in iterator: acc += raw if stream and len(__a ) < chunk_len: _a : Dict = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__a ) >= chunk_len: # We are flushing the accumulator _a : List[str] = (_stride_left, stride_right) _a : List[Any] = {'''raw''': acc[:chunk_len], '''stride''': stride} if stream: _a : List[Any] = False yield item _a : Optional[Any] = stride_left _a : Optional[Any] = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__a ) > stride_left: _a : Optional[Any] = {'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: _a : Dict = False yield item def __UpperCAmelCase ( __a : int ,__a : int ) -> Tuple: """simple docstring""" _a : Dict = 2**24 # 16Mo try: with subprocess.Popen(__a ,stdout=subprocess.PIPE ,bufsize=__a ) as ffmpeg_process: while True: _a : int = ffmpeg_process.stdout.read(__a ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
14
1
from typing import Optional, Tuple, Union import torch from einops import rearrange, reduce from diffusers import DDIMScheduler, DDPMScheduler, DiffusionPipeline, ImagePipelineOutput, UNetaDConditionModel from diffusers.schedulers.scheduling_ddim import DDIMSchedulerOutput from diffusers.schedulers.scheduling_ddpm import DDPMSchedulerOutput a__ = 8 def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Tuple=BITS ) -> List[str]: """simple docstring""" _a : Optional[Any] = x.device _a : int = (x * 255).int().clamp(0 ,255 ) _a : Optional[int] = 2 ** torch.arange(bits - 1 ,-1 ,-1 ,device=__a ) _a : Union[str, Any] = rearrange(__a ,'''d -> d 1 1''' ) _a : Tuple = rearrange(__a ,'''b c h w -> b c 1 h w''' ) _a : int = ((x & mask) != 0).float() _a : Dict = rearrange(__a ,'''b c d h w -> b (c d) h w''' ) _a : Any = bits * 2 - 1 return bits def __UpperCAmelCase ( __a : List[str] ,__a : List[str]=BITS ) -> Optional[Any]: """simple docstring""" _a : Any = x.device _a : Union[str, Any] = (x > 0).int() _a : Dict = 2 ** torch.arange(bits - 1 ,-1 ,-1 ,device=__a ,dtype=torch.intaa ) _a : Any = rearrange(__a ,'''d -> d 1 1''' ) _a : Dict = rearrange(__a ,'''b (c d) h w -> b c d h w''' ,d=8 ) _a : Optional[Any] = reduce(x * mask ,'''b c d h w -> b c h w''' ,'''sum''' ) return (dec / 255).clamp(0.0 ,1.0 ) def __UpperCAmelCase ( self : Optional[int] ,__a : torch.FloatTensor ,__a : int ,__a : torch.FloatTensor ,__a : float = 0.0 ,__a : bool = True ,__a : int=None ,__a : bool = True ,) -> Union[DDIMSchedulerOutput, Tuple]: """simple docstring""" if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf # Ideally, read DDIM paper in-detail understanding # Notation (<variable name> -> <name in paper> # - pred_noise_t -> e_theta(x_t, t) # - pred_original_sample -> f_theta(x_t, t) or x_0 # - std_dev_t -> sigma_t # - eta -> η # - pred_sample_direction -> "direction pointing to x_t" # - pred_prev_sample -> "x_t-1" # 1. get previous step value (=t-1) _a : List[str] = timestep - self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas _a : str = self.alphas_cumprod[timestep] _a : str = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod _a : Union[str, Any] = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _a : Union[str, Any] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 # 4. Clip "predicted x_0" _a : List[str] = self.bit_scale if self.config.clip_sample: _a : Tuple = torch.clamp(__a ,-scale ,__a ) # 5. compute variance: "sigma_t(η)" -> see formula (16) # σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1) _a : List[Any] = self._get_variance(__a ,__a ) _a : List[str] = eta * variance ** 0.5 if use_clipped_model_output: # the model_output is always re-derived from the clipped x_0 in Glide _a : Dict = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 # 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _a : List[Any] = (1 - alpha_prod_t_prev - std_dev_t**2) ** 0.5 * model_output # 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _a : Optional[Any] = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if eta > 0: # randn_like does not support generator https://github.com/pytorch/pytorch/issues/27072 _a : Optional[Any] = model_output.device if torch.is_tensor(__a ) else '''cpu''' _a : Tuple = torch.randn(model_output.shape ,dtype=model_output.dtype ,generator=__a ).to(__a ) _a : Any = self._get_variance(__a ,__a ) ** 0.5 * eta * noise _a : Dict = prev_sample + variance if not return_dict: return (prev_sample,) return DDIMSchedulerOutput(prev_sample=__a ,pred_original_sample=__a ) def __UpperCAmelCase ( self : List[Any] ,__a : torch.FloatTensor ,__a : int ,__a : torch.FloatTensor ,__a : str="epsilon" ,__a : Optional[Any]=None ,__a : bool = True ,) -> Union[DDPMSchedulerOutput, Tuple]: """simple docstring""" _a : int = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type in ["learned", "learned_range"]: _a , _a : int = torch.split(__a ,sample.shape[1] ,dim=1 ) else: _a : List[str] = None # 1. compute alphas, betas _a : Any = self.alphas_cumprod[t] _a : Dict = self.alphas_cumprod[t - 1] if t > 0 else self.one _a : int = 1 - alpha_prod_t _a : Union[str, Any] = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if prediction_type == "epsilon": _a : Union[str, Any] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif prediction_type == "sample": _a : Dict = model_output else: raise ValueError(F"""Unsupported prediction_type {prediction_type}.""" ) # 3. Clip "predicted x_0" _a : int = self.bit_scale if self.config.clip_sample: _a : Optional[Any] = torch.clamp(__a ,-scale ,__a ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf _a : List[Any] = (alpha_prod_t_prev ** 0.5 * self.betas[t]) / beta_prod_t _a : Optional[int] = self.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf _a : Optional[Any] = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise _a : Tuple = 0 if t > 0: _a : Any = torch.randn( model_output.size() ,dtype=model_output.dtype ,layout=model_output.layout ,generator=__a ).to(model_output.device ) _a : str = (self._get_variance(__a ,predicted_variance=__a ) ** 0.5) * noise _a : str = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return DDPMSchedulerOutput(prev_sample=__a ,pred_original_sample=__a ) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self , _a , _a , _a = 1.0 , ) -> Optional[Any]: super().__init__() _a : Optional[Any] = bit_scale _a : Dict = ( ddim_bit_scheduler_step if isinstance(_a , _a ) else ddpm_bit_scheduler_step ) self.register_modules(unet=_a , scheduler=_a ) @torch.no_grad() def __call__( self , _a = 2_5_6 , _a = 2_5_6 , _a = 5_0 , _a = None , _a = 1 , _a = "pil" , _a = True , **_a , ) -> Union[Tuple, ImagePipelineOutput]: _a : Dict = torch.randn( (batch_size, self.unet.config.in_channels, height, width) , generator=_a , ) _a : List[str] = decimal_to_bits(_a ) * self.bit_scale _a : str = latents.to(self.device ) self.scheduler.set_timesteps(_a ) for t in self.progress_bar(self.scheduler.timesteps ): # predict the noise residual _a : Any = self.unet(_a , _a ).sample # compute the previous noisy sample x_t -> x_t-1 _a : Dict = self.scheduler.step(_a , _a , _a ).prev_sample _a : str = bits_to_decimal(_a ) if output_type == "pil": _a : List[Any] = self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a )
14
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = KandinskyInpaintPipeline UpperCAmelCase__ : Optional[int] = ["prompt", "image_embeds", "negative_image_embeds", "image", "mask_image"] UpperCAmelCase__ : Optional[Any] = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", "mask_image", ] UpperCAmelCase__ : Optional[int] = [ "generator", "height", "width", "latents", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase__ : Any = False @property def __lowercase ( self ) -> Optional[int]: return 3_2 @property def __lowercase ( self ) -> int: return 3_2 @property def __lowercase ( self ) -> List[str]: return self.time_input_dim @property def __lowercase ( self ) -> List[str]: return self.time_input_dim * 4 @property def __lowercase ( self ) -> Optional[Any]: return 1_0_0 @property def __lowercase ( self ) -> Optional[Any]: _a : Any = XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[Any] = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) _a : Optional[int] = MultilingualCLIP(_a ) _a : Tuple = text_encoder.eval() return text_encoder @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[str] = { '''in_channels''': 9, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _a : Dict = UNetaDConditionModel(**_a ) return model @property def __lowercase ( self ) -> Optional[int]: return { "block_out_channels": [3_2, 6_4], "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": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowercase ( self ) -> Tuple: torch.manual_seed(0 ) _a : List[Any] = VQModel(**self.dummy_movq_kwargs ) return model def __lowercase ( self ) -> Any: _a : List[Any] = self.dummy_text_encoder _a : Optional[Any] = self.dummy_tokenizer _a : Optional[Any] = self.dummy_unet _a : Union[str, Any] = self.dummy_movq _a : Tuple = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''linear''' , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=_a , set_alpha_to_one=_a , steps_offset=1 , prediction_type='''epsilon''' , thresholding=_a , ) _a : str = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __lowercase ( self , _a , _a=0 ) -> int: _a : Union[str, Any] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _a : List[str] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _a : Tuple = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(_a ) ).to(_a ) _a : Dict = image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : Optional[int] = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) # create mask _a : Union[str, Any] = np.ones((6_4, 6_4) , dtype=np.floataa ) _a : List[str] = 0 if str(_a ).startswith('''mps''' ): _a : Tuple = torch.manual_seed(_a ) else: _a : Any = torch.Generator(device=_a ).manual_seed(_a ) _a : Any = { '''prompt''': '''horse''', '''image''': init_image, '''mask_image''': mask, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 2, '''guidance_scale''': 4.0, '''output_type''': '''np''', } return inputs def __lowercase ( self ) -> Optional[Any]: _a : Optional[Any] = '''cpu''' _a : List[Any] = self.get_dummy_components() _a : Tuple = self.pipeline_class(**_a ) _a : int = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _a : Any = pipe(**self.get_dummy_inputs(_a ) ) _a : str = output.images _a : Tuple = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _a : Union[str, Any] = image[0, -3:, -3:, -1] _a : Tuple = image_from_tuple[0, -3:, -3:, -1] print(F"""image.shape {image.shape}""" ) assert image.shape == (1, 6_4, 6_4, 3) _a : str = np.array( [0.832_6919, 0.7379_0467, 0.2091_8581, 0.930_9612, 0.551_1791, 0.4371_3328, 0.551_3321, 0.4992_2934, 0.5949_7786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def __lowercase ( self ) -> Dict: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowercase ( self ) -> Union[str, Any]: _a : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy''' ) _a : str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _a : Tuple = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) _a : Any = 0 _a : Optional[Any] = '''a hat''' _a : Optional[Any] = KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _a : Tuple = KandinskyInpaintPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-inpaint''' , torch_dtype=torch.floataa ) _a : Union[str, Any] = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _a : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) _a , _a : Dict = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _a : Optional[int] = pipeline( _a , image=_a , mask_image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type='''np''' , ) _a : Optional[int] = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(_a , _a )
14
1
import re import string from collections import Counter import sacrebleu import sacremoses from packaging import version import datasets a__ = ''' @inproceedings{xu-etal-2016-optimizing, title = {Optimizing Statistical Machine Translation for Text Simplification}, authors={Xu, Wei and Napoles, Courtney and Pavlick, Ellie and Chen, Quanze and Callison-Burch, Chris}, journal = {Transactions of the Association for Computational Linguistics}, volume = {4}, year={2016}, url = {https://www.aclweb.org/anthology/Q16-1029}, pages = {401--415 }, @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' a__ = '''\ WIKI_SPLIT is the combination of three metrics SARI, EXACT and SACREBLEU It can be used to evaluate the quality of machine-generated texts. ''' a__ = ''' Calculates sari score (between 0 and 100) given a list of source and predicted sentences, and a list of lists of reference sentences. It also computes the BLEU score as well as the exact match score. Args: sources: list of source sentences where each sentence should be a string. predictions: list of predicted sentences where each sentence should be a string. references: list of lists of reference sentences where each sentence should be a string. Returns: sari: sari score sacrebleu: sacrebleu score exact: exact score Examples: >>> sources=["About 95 species are currently accepted ."] >>> predictions=["About 95 you now get in ."] >>> references=[["About 95 species are currently known ."]] >>> wiki_split = datasets.load_metric("wiki_split") >>> results = wiki_split.compute(sources=sources, predictions=predictions, references=references) >>> print(results) {\'sari\': 21.805555555555557, \'sacrebleu\': 14.535768424205482, \'exact\': 0.0} ''' def __UpperCAmelCase ( __a : Optional[int] ) -> int: """simple docstring""" def remove_articles(__a : Tuple ): _a : Union[str, Any] = re.compile(R'''\b(a|an|the)\b''' ,re.UNICODE ) return re.sub(__a ,''' ''' ,__a ) def white_space_fix(__a : Any ): return " ".join(text.split() ) def remove_punc(__a : str ): _a : Tuple = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__a : int ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__a ) ) ) ) def __UpperCAmelCase ( __a : Any ,__a : int ) -> Optional[Any]: """simple docstring""" return int(normalize_answer(__a ) == normalize_answer(__a ) ) def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Optional[int] ) -> int: """simple docstring""" _a : Dict = [any(compute_exact(__a ,__a ) for ref in refs ) for pred, refs in zip(__a ,__a )] return (sum(__a ) / len(__a )) * 100 def __UpperCAmelCase ( __a : List[str] ,__a : Union[str, Any] ,__a : Optional[int] ,__a : Union[str, Any] ) -> Tuple: """simple docstring""" _a : Optional[Any] = [rgram for rgrams in rgramslist for rgram in rgrams] _a : Any = Counter(__a ) _a : Dict = Counter(__a ) _a : Tuple = Counter() for sgram, scount in sgramcounter.items(): _a : List[str] = scount * numref _a : Optional[Any] = Counter(__a ) _a : Dict = Counter() for cgram, ccount in cgramcounter.items(): _a : int = ccount * numref # KEEP _a : Optional[Any] = sgramcounter_rep & cgramcounter_rep _a : Dict = keepgramcounter_rep & rgramcounter _a : str = sgramcounter_rep & rgramcounter _a : Any = 0 _a : Optional[Any] = 0 for keepgram in keepgramcountergood_rep: keeptmpscorea += keepgramcountergood_rep[keepgram] / keepgramcounter_rep[keepgram] # Fix an alleged bug [2] in the keep score computation. # keeptmpscore2 += keepgramcountergood_rep[keepgram] / keepgramcounterall_rep[keepgram] keeptmpscorea += keepgramcountergood_rep[keepgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. _a : List[str] = 1 _a : List[str] = 1 if len(__a ) > 0: _a : Any = keeptmpscorea / len(__a ) if len(__a ) > 0: # Fix an alleged bug [2] in the keep score computation. # keepscore_recall = keeptmpscore2 / len(keepgramcounterall_rep) _a : Optional[int] = keeptmpscorea / sum(keepgramcounterall_rep.values() ) _a : List[Any] = 0 if keepscore_precision > 0 or keepscore_recall > 0: _a : str = 2 * keepscore_precision * keepscore_recall / (keepscore_precision + keepscore_recall) # DELETION _a : Tuple = sgramcounter_rep - cgramcounter_rep _a : Tuple = delgramcounter_rep - rgramcounter _a : List[Any] = sgramcounter_rep - rgramcounter _a : int = 0 _a : Union[str, Any] = 0 for delgram in delgramcountergood_rep: deltmpscorea += delgramcountergood_rep[delgram] / delgramcounter_rep[delgram] deltmpscorea += delgramcountergood_rep[delgram] / delgramcounterall_rep[delgram] # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. _a : int = 1 if len(__a ) > 0: _a : Any = deltmpscorea / len(__a ) # ADDITION _a : Union[str, Any] = set(__a ) - set(__a ) _a : str = set(__a ) & set(__a ) _a : Optional[int] = set(__a ) - set(__a ) _a : Dict = 0 for addgram in addgramcountergood: addtmpscore += 1 # Define 0/0=1 instead of 0 to give higher scores for predictions that match # a target exactly. _a : Optional[int] = 1 _a : str = 1 if len(__a ) > 0: _a : List[Any] = addtmpscore / len(__a ) if len(__a ) > 0: _a : Optional[Any] = addtmpscore / len(__a ) _a : Dict = 0 if addscore_precision > 0 or addscore_recall > 0: _a : Optional[Any] = 2 * addscore_precision * addscore_recall / (addscore_precision + addscore_recall) return (keepscore, delscore_precision, addscore) def __UpperCAmelCase ( __a : int ,__a : Tuple ,__a : Optional[int] ) -> str: """simple docstring""" _a : List[Any] = len(__a ) _a : int = ssent.split(''' ''' ) _a : List[Any] = csent.split(''' ''' ) _a : List[Any] = [] _a : Tuple = [] _a : Union[str, Any] = [] _a : Dict = [] _a : Dict = [] _a : Any = [] _a : List[Any] = [] _a : List[Any] = [] _a : List[Any] = [] _a : Optional[int] = [] for rsent in rsents: _a : Dict = rsent.split(''' ''' ) _a : Optional[Any] = [] _a : Tuple = [] _a : Optional[Any] = [] ragramslist.append(__a ) for i in range(0 ,len(__a ) - 1 ): if i < len(__a ) - 1: _a : Any = ragrams[i] + ''' ''' + ragrams[i + 1] ragrams.append(__a ) if i < len(__a ) - 2: _a : List[Any] = ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2] ragrams.append(__a ) if i < len(__a ) - 3: _a : List[str] = ragrams[i] + ''' ''' + ragrams[i + 1] + ''' ''' + ragrams[i + 2] + ''' ''' + ragrams[i + 3] ragrams.append(__a ) ragramslist.append(__a ) ragramslist.append(__a ) ragramslist.append(__a ) for i in range(0 ,len(__a ) - 1 ): if i < len(__a ) - 1: _a : str = sagrams[i] + ''' ''' + sagrams[i + 1] sagrams.append(__a ) if i < len(__a ) - 2: _a : Optional[int] = sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] sagrams.append(__a ) if i < len(__a ) - 3: _a : List[str] = sagrams[i] + ''' ''' + sagrams[i + 1] + ''' ''' + sagrams[i + 2] + ''' ''' + sagrams[i + 3] sagrams.append(__a ) for i in range(0 ,len(__a ) - 1 ): if i < len(__a ) - 1: _a : str = cagrams[i] + ''' ''' + cagrams[i + 1] cagrams.append(__a ) if i < len(__a ) - 2: _a : int = cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] cagrams.append(__a ) if i < len(__a ) - 3: _a : Optional[int] = cagrams[i] + ''' ''' + cagrams[i + 1] + ''' ''' + cagrams[i + 2] + ''' ''' + cagrams[i + 3] cagrams.append(__a ) ((_a) , (_a) , (_a)) : Union[str, Any] = SARIngram(__a ,__a ,__a ,__a ) ((_a) , (_a) , (_a)) : Tuple = SARIngram(__a ,__a ,__a ,__a ) ((_a) , (_a) , (_a)) : int = SARIngram(__a ,__a ,__a ,__a ) ((_a) , (_a) , (_a)) : List[str] = SARIngram(__a ,__a ,__a ,__a ) _a : List[Any] = sum([keepascore, keepascore, keepascore, keepascore] ) / 4 _a : List[str] = sum([delascore, delascore, delascore, delascore] ) / 4 _a : Optional[Any] = sum([addascore, addascore, addascore, addascore] ) / 4 _a : int = (avgkeepscore + avgdelscore + avgaddscore) / 3 return finalscore def __UpperCAmelCase ( __a : str ,__a : bool = True ,__a : str = "13a" ,__a : bool = True ) -> Optional[int]: """simple docstring""" if lowercase: _a : List[str] = sentence.lower() if tokenizer in ["13a", "intl"]: if version.parse(sacrebleu.__version__ ).major >= 2: _a : int = sacrebleu.metrics.bleu._get_tokenizer(__a )()(__a ) else: _a : Optional[int] = sacrebleu.TOKENIZERS[tokenizer]()(__a ) elif tokenizer == "moses": _a : str = sacremoses.MosesTokenizer().tokenize(__a ,return_str=__a ,escape=__a ) elif tokenizer == "penn": _a : Dict = sacremoses.MosesTokenizer().penn_tokenize(__a ,return_str=__a ) else: _a : Optional[int] = sentence if not return_str: _a : Optional[Any] = normalized_sent.split() return normalized_sent def __UpperCAmelCase ( __a : Optional[Any] ,__a : Optional[Any] ,__a : Optional[int] ) -> List[str]: """simple docstring""" if not (len(__a ) == len(__a ) == len(__a )): raise ValueError('''Sources length must match predictions and references lengths.''' ) _a : Any = 0 for src, pred, refs in zip(__a ,__a ,__a ): sari_score += SARIsent(normalize(__a ) ,normalize(__a ) ,[normalize(__a ) for sent in refs] ) _a : str = sari_score / len(__a ) return 100 * sari_score def __UpperCAmelCase ( __a : Optional[int] ,__a : Optional[int] ,__a : Any="exp" ,__a : str=None ,__a : str=False ,__a : Dict=False ,__a : List[str]=False ,) -> Optional[Any]: """simple docstring""" _a : List[str] = len(references[0] ) if any(len(__a ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) _a : str = [[refs[i] for refs in references] for i in range(__a )] _a : Union[str, Any] = sacrebleu.corpus_bleu( __a ,__a ,smooth_method=__a ,smooth_value=__a ,force=__a ,lowercase=__a ,use_effective_order=__a ,) return output.score @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Sequence(datasets.Value('''string''' , id='''sequence''' ) , id='''references''' ), } ) , codebase_urls=[ '''https://github.com/huggingface/transformers/blob/master/src/transformers/data/metrics/squad_metrics.py''', '''https://github.com/cocoxu/simplification/blob/master/SARI.py''', '''https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/sari_hook.py''', '''https://github.com/mjpost/sacreBLEU''', ] , reference_urls=[ '''https://www.aclweb.org/anthology/Q16-1029.pdf''', '''https://github.com/mjpost/sacreBLEU''', '''https://en.wikipedia.org/wiki/BLEU''', '''https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213''', ] , ) def __lowercase ( self , _a , _a , _a ) -> Optional[int]: _a : Optional[Any] = {} result.update({'''sari''': compute_sari(sources=_a , predictions=_a , references=_a )} ) result.update({'''sacrebleu''': compute_sacrebleu(predictions=_a , references=_a )} ) result.update({'''exact''': compute_em(predictions=_a , references=_a )} ) return result
14
import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_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.''' ) parser.add_argument( '''--original_config_file''', type=str, required=True, 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( '''--image_size''', default=512, 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( '''--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.)''') def __UpperCAmelCase ( __a : Any ) -> List[Any]: """simple docstring""" if string == "True": return True elif string == "False": return False else: raise ValueError(F"""could not parse string as bool {string}""" ) parser.add_argument( '''--use_linear_projection''', help='''Override for use linear projection''', required=False, type=parse_bool ) parser.add_argument('''--cross_attention_dim''', help='''Override for cross attention_dim''', required=False, type=int) a__ = parser.parse_args() a__ = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
14
1
from __future__ import annotations def __UpperCAmelCase ( __a : list[int] ,__a : int ) -> int: """simple docstring""" if len(__a ) < k or k < 0: raise ValueError('''Invalid Input''' ) _a : Any = sum(array[:k] ) for i in range(len(__a ) - k ): _a : int = current_sum - array[i] + array[i + k] _a : List[Any] = max(__a ,__a ) return max_sum if __name__ == "__main__": from doctest import testmod from random import randint testmod() a__ = [randint(-1000, 1000) for i in range(100)] a__ = randint(0, 110) print(f'''The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}''')
14
class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a , _a ) -> List[str]: _a : List[Any] = name _a : List[str] = value _a : List[str] = weight def __repr__( self ) -> Optional[int]: return F"""{self.__class__.__name__}({self.name}, {self.value}, {self.weight})""" def __lowercase ( self ) -> List[Any]: return self.value def __lowercase ( self ) -> int: return self.name def __lowercase ( self ) -> Optional[int]: return self.weight def __lowercase ( self ) -> Optional[Any]: return self.value / self.weight def __UpperCAmelCase ( __a : Optional[int] ,__a : Tuple ,__a : List[str] ) -> List[str]: """simple docstring""" _a : Optional[int] = [] for i in range(len(__a ) ): menu.append(Things(name[i] ,value[i] ,weight[i] ) ) return menu def __UpperCAmelCase ( __a : int ,__a : Union[str, Any] ,__a : int ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = sorted(__a ,key=__a ,reverse=__a ) _a : Any = [] _a , _a : Optional[int] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def __UpperCAmelCase ( ) -> int: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
14
1
from __future__ import annotations from decimal import Decimal from numpy import array def __UpperCAmelCase ( __a : list[list[float]] ) -> list[list[float]]: """simple docstring""" _a : Optional[int] = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(__a ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix _a : Dict = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creates a copy of the matrix with swapped positions of the elements _a : str = [[0.0, 0.0], [0.0, 0.0]] _a , _a : int = matrix[1][1], matrix[0][0] _a , _a : Dict = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(__a ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(__a ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule _a : str = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creating cofactor matrix _a : Union[str, Any] = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] _a : Optional[Any] = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) _a : Dict = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) _a : Tuple = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) _a : Tuple = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) _a : Any = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) _a : Any = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) _a : List[str] = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) _a : List[Any] = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) _a : int = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) _a : Union[str, Any] = array(__a ) for i in range(3 ): for j in range(3 ): _a : Dict = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix _a : Optional[Any] = array(__a ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(__a ) # Calculate the inverse of the matrix return [[float(d(__a ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError('''Please provide a matrix of size 2x2 or 3x3.''' )
14
import copy import inspect import unittest from transformers import PretrainedConfig, SwiftFormerConfig from transformers.testing_utils import ( require_torch, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwiftFormerForImageClassification, SwiftFormerModel from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=1_3 , _a=3 , _a=True , _a=True , _a=0.1 , _a=0.1 , _a=2_2_4 , _a=1_0_0_0 , _a=[3, 3, 6, 4] , _a=[4_8, 5_6, 1_1_2, 2_2_0] , ) -> Tuple: _a : Dict = parent _a : Optional[int] = batch_size _a : Optional[Any] = num_channels _a : Union[str, Any] = is_training _a : Tuple = use_labels _a : Dict = hidden_dropout_prob _a : List[Any] = attention_probs_dropout_prob _a : Dict = num_labels _a : List[str] = image_size _a : Dict = layer_depths _a : str = embed_dims def __lowercase ( self ) -> Optional[Any]: _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : int = None if self.use_labels: _a : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) _a : Dict = self.get_config() return config, pixel_values, labels def __lowercase ( self ) -> int: return SwiftFormerConfig( depths=self.layer_depths , embed_dims=self.embed_dims , mlp_ratio=4 , downsamples=[True, True, True, True] , hidden_act='''gelu''' , num_labels=self.num_labels , down_patch_size=3 , down_stride=2 , down_pad=1 , drop_rate=0.0 , drop_path_rate=0.0 , use_layer_scale=_a , layer_scale_init_value=1e-5 , ) def __lowercase ( self , _a , _a , _a ) -> str: _a : List[Any] = SwiftFormerModel(config=_a ) model.to(_a ) model.eval() _a : Optional[int] = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dims[-1], 7, 7) ) def __lowercase ( self , _a , _a , _a ) -> Optional[Any]: _a : List[str] = self.num_labels _a : Optional[int] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : List[str] = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) _a : Union[str, Any] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : Optional[Any] = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowercase ( self ) -> Tuple: ((_a) , (_a) , (_a)) : Optional[int] = self.prepare_config_and_inputs() _a : List[Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else () UpperCAmelCase__ : Optional[int] = ( {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Optional[Any] = False UpperCAmelCase__ : str = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : str = False def __lowercase ( self ) -> Optional[int]: _a : Union[str, Any] = SwiftFormerModelTester(self ) _a : int = ConfigTester( self , config_class=_a , has_text_modality=_a , hidden_size=3_7 , num_attention_heads=1_2 , num_hidden_layers=1_2 , ) def __lowercase ( self ) -> int: self.config_tester.run_common_tests() @unittest.skip(reason='''SwiftFormer does not use inputs_embeds''' ) def __lowercase ( self ) -> Union[str, Any]: pass def __lowercase ( self ) -> Dict: _a , _a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Any = model_class(_a ) _a : int = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def __lowercase ( self ) -> str: _a , _a : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Optional[int] = model_class(_a ) _a : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Tuple = [*signature.parameters.keys()] _a : List[str] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowercase ( self ) -> int: _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self ) -> Optional[int]: _a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self ) -> Optional[Any]: for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Any = SwiftFormerModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason='''SwiftFormer does not output attentions''' ) def __lowercase ( self ) -> List[Any]: pass def __lowercase ( self ) -> int: def check_hidden_states_output(_a , _a , _a ): _a : Optional[int] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Union[str, Any] = model(**self._prepare_for_class(_a , _a ) ) _a : Optional[Any] = outputs.hidden_states _a : Union[str, Any] = 8 self.assertEqual(len(_a ) , _a ) # TODO # SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width) # with the width and height being successively divided by 2, after every 2 blocks for i in range(len(_a ) ): self.assertEqual( hidden_states[i].shape , torch.Size( [ self.model_tester.batch_size, self.model_tester.embed_dims[i // 2], (self.model_tester.image_size // 4) // 2 ** (i // 2), (self.model_tester.image_size // 4) // 2 ** (i // 2), ] ) , ) _a , _a : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : str = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : List[str] = True check_hidden_states_output(_a , _a , _a ) def __lowercase ( self ) -> str: def _config_zero_init(_a ): _a : List[Any] = copy.deepcopy(_a ) for key in configs_no_init.__dict__.keys(): if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key: setattr(_a , _a , 1e-1_0 ) if isinstance(getattr(_a , _a , _a ) , _a ): _a : int = _config_zero_init(getattr(_a , _a ) ) setattr(_a , _a , _a ) return configs_no_init _a , _a : Any = self.model_tester.prepare_config_and_inputs_for_common() _a : Dict = _config_zero_init(_a ) for model_class in self.all_model_classes: _a : Dict = model_class(config=_a ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9) / 1e9).round().item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __lowercase ( self ) -> Optional[Any]: pass def __UpperCAmelCase ( ) -> Optional[Any]: """simple docstring""" _a : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self ) -> str: return ViTImageProcessor.from_pretrained('''MBZUAI/swiftformer-xs''' ) if is_vision_available() else None @slow def __lowercase ( self ) -> Dict: _a : Any = SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs''' ).to(_a ) _a : Any = self.default_image_processor _a : Any = prepare_img() _a : Any = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _a : Optional[Any] = model(**_a ) # verify the logits _a : List[str] = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _a ) _a : int = torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
14
1
import collections 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(R'''^\s+"([^"]+)",''') # Catches a line with objects between brackets only: ["foo", "bar"], a__ = re.compile(R'''^\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 __UpperCAmelCase ( __a : Optional[Any] ) -> Optional[int]: """simple docstring""" if _re_test_backend.search(__a ) is None: return None _a : List[str] = [b[0] for b in _re_backend.findall(__a )] backends.sort() return "_and_".join(__a ) def __UpperCAmelCase ( __a : Optional[int] ) -> int: """simple docstring""" with open(__a ,'''r''' ,encoding='''utf-8''' ,newline='''\n''' ) as f: _a : List[str] = f.readlines() _a : List[str] = 0 while line_index < len(__a ) and not lines[line_index].startswith('''_import_structure = {''' ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(__a ): return None # First grab the objects without a specific backend in _import_structure _a : List[Any] = [] while not lines[line_index].startswith('''if TYPE_CHECKING''' ) and find_backend(lines[line_index] ) is None: _a : str = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(__a ): _a : Optional[Any] = _re_one_line_import_struct.search(__a ).groups()[0] _a : Optional[int] = re.findall(R'''\[([^\]]+)\]''' ,__a ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(''', ''' )] ) line_index += 1 continue _a : Optional[Any] = _re_import_struct_key_value.search(__a ) if single_line_import_search is not None: _a : List[Any] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(''', ''' ) if len(__a ) > 0] objects.extend(__a ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) line_index += 1 _a : int = {'''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. _a : 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: _a : 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 _a : str = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 4 ): _a : str = lines[line_index] if _re_import_struct_add_one.search(__a ) is not None: objects.append(_re_import_struct_add_one.search(__a ).groups()[0] ) elif _re_import_struct_add_many.search(__a ) is not None: _a : Any = _re_import_struct_add_many.search(__a ).groups()[0].split(''', ''' ) _a : List[Any] = [obj[1:-1] for obj in imports if len(__a ) > 0] objects.extend(__a ) elif _re_between_brackets.search(__a ) is not None: _a : List[Any] = _re_between_brackets.search(__a ).groups()[0].split(''', ''' ) _a : Dict = [obj[1:-1] for obj in imports if len(__a ) > 0] objects.extend(__a ) elif _re_quote_object.search(__a ) is not None: objects.append(_re_quote_object.search(__a ).groups()[0] ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) elif line.startswith(''' ''' * 12 + '''"''' ): objects.append(line[13:-3] ) line_index += 1 _a : Any = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend _a : str = [] while ( line_index < len(__a ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('''else''' ) ): _a : Union[str, Any] = lines[line_index] _a : Optional[int] = _re_import.search(__a ) 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 _a : Optional[Any] = {'''none''': objects} # Let's continue with backend-specific objects while line_index < len(__a ): # If the line is an if is_backend_available, we grab all objects associated. _a : 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: _a : 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 _a : str = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 8 ): _a : Tuple = lines[line_index] _a : Optional[Any] = _re_import.search(__a ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 12 ): objects.append(line[12:-2] ) line_index += 1 _a : Optional[int] = objects else: line_index += 1 return import_dict_objects, type_hint_objects def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ) -> Tuple: """simple docstring""" def find_duplicates(__a : str ): return [k for k, v in collections.Counter(__a ).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!"] _a : Union[str, Any] = [] for key in import_dict_objects.keys(): _a : Tuple = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"""Duplicate _import_structure definitions for: {duplicate_imports}""" ) _a : Tuple = 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] ) ): _a : List[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 __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" _a : List[str] = [] for root, _, files in os.walk(__a ): if "__init__.py" in files: _a : Optional[Any] = os.path.join(__a ,'''__init__.py''' ) _a : Union[str, Any] = parse_init(__a ) if objects is not None: _a : Optional[int] = analyze_results(*__a ) if len(__a ) > 0: _a : int = F"""Problem in {fname}, both halves do not define the same objects.\n{errors[0]}""" failures.append('''\n'''.join(__a ) ) if len(__a ) > 0: raise ValueError('''\n\n'''.join(__a ) ) def __UpperCAmelCase ( ) -> List[Any]: """simple docstring""" _a : Dict = [] for path, directories, files in os.walk(__a ): for folder in directories: # Ignore private modules if folder.startswith('''_''' ): directories.remove(__a ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(__a ) / folder).glob('''*.py''' ) ) ) == 0: continue _a : Optional[Any] = str((Path(__a ) / folder).relative_to(__a ) ) _a : int = short_path.replace(os.path.sep ,'''.''' ) submodules.append(__a ) for fname in files: if fname == "__init__.py": continue _a : List[Any] = str((Path(__a ) / fname).relative_to(__a ) ) _a : Dict = short_path.replace('''.py''' ,'''''' ).replace(os.path.sep ,'''.''' ) if len(submodule.split('''.''' ) ) == 1: submodules.append(__a ) return submodules a__ = [ '''convert_pytorch_checkpoint_to_tf2''', '''modeling_flax_pytorch_utils''', '''models.esm.openfold_utils''', ] def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" from transformers.utils import direct_transformers_import _a : str = direct_transformers_import(__a ) _a : Dict = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(__a ,'''__init__.py''' ) ,'''r''' ) as f: _a : int = f.read() import_structure_keys.update(set(re.findall(R'''import_structure\[\"([^\"]*)\"\]''' ,__a ) ) ) _a : Optional[Any] = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(__a ) > 0: _a : List[Any] = '''\n'''.join(F"""- {module}""" for module in module_not_registered ) raise ValueError( '''The following submodules are not properly registed 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()
14
import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() a__ = logging.get_logger(__name__) def __UpperCAmelCase ( __a : str ) -> List[Any]: """simple docstring""" _a : Tuple = SwinConfig.from_pretrained( '''microsoft/swin-tiny-patch4-window7-224''' ,out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) _a : Dict = MaskFormerConfig(backbone_config=__a ) _a : Optional[Any] = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok _a : Optional[Any] = 847 _a : List[Any] = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok _a : Union[str, Any] = 150 _a : Any = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok _a : int = 171 _a : List[str] = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO _a : Dict = 133 _a : Optional[Any] = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok _a : List[Any] = 19 _a : Optional[Any] = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok _a : List[Any] = 65 _a : Dict = '''mapillary-vistas-id2label.json''' _a : Optional[int] = json.load(open(hf_hub_download(__a ,__a ,repo_type='''dataset''' ) ,'''r''' ) ) _a : Tuple = {int(__a ): v for k, v in idalabel.items()} return config def __UpperCAmelCase ( __a : Optional[Any] ) -> Tuple: """simple docstring""" _a : Optional[Any] = [] # stem # fmt: off rename_keys.append(('''backbone.patch_embed.proj.weight''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.proj.bias''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''model.pixel_level_module.encoder.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''model.pixel_level_module.encoder.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.layers.{i}.downsample.reduction.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(('''sem_seg_head.layer_4.weight''', '''model.pixel_level_module.decoder.fpn.stem.0.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.weight''', '''model.pixel_level_module.decoder.fpn.stem.1.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.bias''', '''model.pixel_level_module.decoder.fpn.stem.1.bias''') ) for source_index, target_index in zip(range(3 ,0 ,-1 ) ,range(0 ,3 ) ): rename_keys.append((F"""sem_seg_head.adapter_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(('''sem_seg_head.mask_features.weight''', '''model.pixel_level_module.decoder.mask_projection.weight''') ) rename_keys.append(('''sem_seg_head.mask_features.bias''', '''model.pixel_level_module.decoder.mask_projection.bias''') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.weight''', '''model.transformer_module.decoder.layernorm.weight''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.bias''', '''model.transformer_module.decoder.layernorm.bias''') ) # heads on top rename_keys.append(('''sem_seg_head.predictor.query_embed.weight''', '''model.transformer_module.queries_embedder.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.weight''', '''model.transformer_module.input_projection.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.bias''', '''model.transformer_module.input_projection.bias''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.weight''', '''class_predictor.weight''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.bias''', '''class_predictor.bias''') ) for i in range(3 ): rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", F"""mask_embedder.{i}.0.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", F"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ,__a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _a : str = dct.pop(__a ) _a : str = val def __UpperCAmelCase ( __a : List[Any] ,__a : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _a : Optional[Any] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) _a : Optional[int] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[int] = in_proj_weight[:dim, :] _a : List[Any] = in_proj_bias[: dim] _a : Optional[int] = in_proj_weight[ dim : dim * 2, : ] _a : Tuple = in_proj_bias[ dim : dim * 2 ] _a : int = in_proj_weight[ -dim :, : ] _a : Optional[int] = in_proj_bias[-dim :] # fmt: on def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ) -> List[Any]: """simple docstring""" _a : Optional[int] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : Union[str, Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Union[str, Any] = in_proj_weight[: hidden_size, :] _a : List[Any] = in_proj_bias[:config.hidden_size] _a : Dict = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Any = in_proj_bias[hidden_size : hidden_size * 2] _a : Tuple = in_proj_weight[-hidden_size :, :] _a : List[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) _a : List[str] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[Any] = in_proj_weight[: hidden_size, :] _a : Any = in_proj_bias[:config.hidden_size] _a : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Optional[Any] = in_proj_bias[hidden_size : hidden_size * 2] _a : List[str] = in_proj_weight[-hidden_size :, :] _a : int = in_proj_bias[-hidden_size :] # fmt: on def __UpperCAmelCase ( ) -> torch.Tensor: """simple docstring""" _a : str = '''http://images.cocodataset.org/val2017/000000039769.jpg''' _a : Dict = Image.open(requests.get(__a ,stream=__a ).raw ) return im @torch.no_grad() def __UpperCAmelCase ( __a : str ,__a : str ,__a : str ,__a : bool = False ) -> Union[str, Any]: """simple docstring""" _a : Optional[Any] = get_maskformer_config(__a ) # load original state_dict with open(__a ,'''rb''' ) as f: _a : str = pickle.load(__a ) _a : Union[str, Any] = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys _a : Any = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a ,__a ,__a ) read_in_swin_q_k_v(__a ,config.backbone_config ) read_in_decoder_q_k_v(__a ,__a ) # update to torch tensors for key, value in state_dict.items(): _a : Optional[int] = torch.from_numpy(__a ) # load 🤗 model _a : Dict = MaskFormerForInstanceSegmentation(__a ) model.eval() for name, param in model.named_parameters(): print(__a ,param.shape ) _a , _a : Tuple = model.load_state_dict(__a ,strict=__a ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(__a ) == 0, F"""Unexpected keys: {unexpected_keys}""" # verify results _a : Union[str, Any] = prepare_img() if "vistas" in model_name: _a : int = 65 elif "cityscapes" in model_name: _a : Tuple = 65_535 else: _a : str = 255 _a : Dict = True if '''ade''' in model_name else False _a : Optional[Any] = MaskFormerImageProcessor(ignore_index=__a ,reduce_labels=__a ) _a : Optional[Any] = image_processor(__a ,return_tensors='''pt''' ) _a : int = model(**__a ) print('''Logits:''' ,outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": _a : Union[str, Any] = torch.tensor( [[3.63_53, -4.47_70, -2.60_65], [0.50_81, -4.23_94, -3.53_43], [2.19_09, -5.03_53, -1.93_23]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] ,__a ,atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(__a ).mkdir(exist_ok=__a ) model.save_pretrained(__a ) image_processor.save_pretrained(__a ) if push_to_hub: print('''Pushing model and image processor to the hub...''' ) model.push_to_hub(F"""nielsr/{model_name}""" ) image_processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''maskformer-swin-tiny-ade''', type=str, help=('''Name of the MaskFormer model you\'d like to convert''',), ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl''', type=str, help='''Path to the original state dict (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) a__ = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
14
1
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''EleutherAI/gpt-neox-20b''': '''https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json''', # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = "gpt_neox" def __init__( self , _a=5_0_4_3_2 , _a=6_1_4_4 , _a=4_4 , _a=6_4 , _a=2_4_5_7_6 , _a="gelu" , _a=0.25 , _a=1_0_0_0_0 , _a=0.0 , _a=0.0 , _a=0.1 , _a=2_0_4_8 , _a=0.02 , _a=1e-5 , _a=True , _a=0 , _a=2 , _a=False , _a=True , _a=None , **_a , ) -> str: super().__init__(bos_token_id=_a , eos_token_id=_a , **_a ) _a : Union[str, Any] = vocab_size _a : Optional[Any] = max_position_embeddings _a : str = hidden_size _a : Tuple = num_hidden_layers _a : Optional[int] = num_attention_heads _a : int = intermediate_size _a : Dict = hidden_act _a : Union[str, Any] = rotary_pct _a : List[Any] = rotary_emb_base _a : List[Any] = attention_dropout _a : Tuple = hidden_dropout _a : Union[str, Any] = classifier_dropout _a : List[Any] = initializer_range _a : Optional[int] = layer_norm_eps _a : str = use_cache _a : int = tie_word_embeddings _a : Tuple = use_parallel_residual _a : Union[str, Any] = rope_scaling self._rope_scaling_validation() if self.hidden_size % self.num_attention_heads != 0: raise ValueError( '''The hidden size is not divisble by the number of attention heads! Make sure to update them!''' ) def __lowercase ( self ) -> List[str]: if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _a ) or len(self.rope_scaling ) != 2: raise ValueError( '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ''' F"""got {self.rope_scaling}""" ) _a : Optional[int] = self.rope_scaling.get('''type''' , _a ) _a : List[str] = self.rope_scaling.get('''factor''' , _a ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( F"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}""" ) if rope_scaling_factor is None or not isinstance(_a , _a ) or rope_scaling_factor <= 1.0: raise ValueError(F"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
14
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin a__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = XLMProphetNetTokenizer UpperCAmelCase__ : Optional[int] = False UpperCAmelCase__ : List[Any] = True def __lowercase ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _a : List[Any] = XLMProphetNetTokenizer(_a , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def __lowercase ( self ) -> Any: _a : Tuple = '''[PAD]''' _a : int = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def __lowercase ( self ) -> str: _a : Any = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''[PAD]''' ) self.assertEqual(vocab_keys[1] , '''[CLS]''' ) self.assertEqual(vocab_keys[-1] , '''j''' ) self.assertEqual(len(_a ) , 1_0_1_2 ) def __lowercase ( self ) -> Union[str, Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1_0_1_2 ) def __lowercase ( self ) -> str: _a : Tuple = XLMProphetNetTokenizer(_a , keep_accents=_a ) _a : Union[str, Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) _a : Optional[int] = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _a : List[Any] = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, -9, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, -9, 4] ] , ) _a : List[str] = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''[UNK]''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''[UNK]''', '''.''', ] , ) @cached_property def __lowercase ( self ) -> List[str]: return XLMProphetNetTokenizer.from_pretrained('''microsoft/xprophetnet-large-wiki100-cased''' ) @slow def __lowercase ( self ) -> Tuple: _a : str = '''Hello World!''' _a : Tuple = [3_5_3_8_9, 6_6_7_2, 4_9, 2] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @slow def __lowercase ( self ) -> str: # fmt: off _a : str = {'''input_ids''': [[1_1_0_7_3, 8_2_7_8_3, 1_8, 2_6, 8_2_7_8_3, 5_4_9, 5_1_5_4_0, 2_4_8, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 2_1_5_1_8_6, 1_3_2_5, 1_4_7, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 5_6_3_7_0, 5_3, 1_2_2_0_2_0, 2_0, 1_6_4_7_7, 2_7, 8_7_3_5_5, 4_5_4_8, 2_0, 4_7_2_8, 7_8_3_9_2, 1_7, 1_5_9_9_6_9, 1_8, 2_6, 2_4_4_9_1, 6_2_9, 1_5, 5_3_8, 2_2_7_0_4, 5_4_3_9, 1_5, 2_7_8_8, 2_4_4_9_1, 9_8_8_5, 1_5, 4_3_5_3_4, 6_0_5, 1_5, 8_1_4, 1_8_4_0_3, 3_3_2_0_0, 2_9, 1_5, 4_3_5_3_4, 2_4_4_5_8, 1_2_4_1_0, 1_1_1, 2_4_9_6_6, 8_3_6_6_9, 9_6_3_7, 1_4_4_0_6_8, 2_6, 8_5_0, 2_2_3_4_6, 2_7, 1_4_7, 2_4_9_6_6, 8_3_6_6_9, 8_3_4_9_0, 2_6, 3_9_1_1_3, 7_3_5, 2_7, 6_8_9, 6_5_6, 2_8_0_0, 1_3_3_9, 4_6_0_0, 5_3, 1_2_2_0_2_0, 1_1_5_7_8_5, 3_4, 8_1_6, 1_3_3_9, 4_6_8_8_7, 1_8, 1_4_7, 5_3_9_0_5, 1_9_5_1, 4_2_2_3_8, 4_1_1_7_0, 1_7_7_3_2, 8_3_4, 4_3_6, 1_5, 2_7_5_2_3, 9_8_7_3_3, 2_1_7, 1_4_7, 5_5_4_2, 4_9_8_1, 9_3_0, 1_7_3_4_7, 1_6, 2], [2_0_0_9_1, 6_2_9, 9_4, 8_2_7_8_6, 5_8, 4_9_0, 2_0, 1_5_2_8, 8_4, 5_3_9_0_5, 3_4_4, 8_0_5_9_2, 1_1_0_1_2_8, 1_8_8_2_2, 5_2_6_7, 1_3_0_6, 6_2, 1_5_2_5_3_7, 3_0_8, 7_9_9_7, 4_0_1, 1_2_4_4_2_7, 5_4_9, 3_5_4_4_2, 2_2_5, 1_0_9, 1_5_0_5_5, 2_5_7_4_8, 1_4_7, 7_1_1_9, 4_3_7_1_2, 3_4, 7_6_7, 1_3_5_3_6_6, 1_8, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5_9_2, 6_3_7_8_4, 1_1_9_4_6_6, 1_7, 1_4_7_8_0_8, 8_8_2_1_4, 1_8, 6_5_6, 8_1, 3_2, 3_2_9_6, 1_0_2_8_0, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a , model_name='''microsoft/xprophetnet-large-wiki100-cased''' , revision='''1acad1643ddd54a44df6a1b797ada8373685d90e''' , )
14
1
def __UpperCAmelCase ( __a : float ,__a : list[float] ) -> float: """simple docstring""" if discount_rate < 0: raise ValueError('''Discount rate cannot be negative''' ) if not cash_flows: raise ValueError('''Cash flows list cannot be empty''' ) _a : List[str] = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(__a ) ) return round(__a ,ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
14
import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Any = LxmertTokenizer UpperCAmelCase__ : Optional[Any] = LxmertTokenizerFast UpperCAmelCase__ : Any = True UpperCAmelCase__ : Dict = True def __lowercase ( self ) -> Union[str, Any]: super().setUp() _a : int = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _a : Any = 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 __lowercase ( self , _a ) -> List[str]: _a : Tuple = '''UNwant\u00E9d,running''' _a : str = '''unwanted, running''' return input_text, output_text def __lowercase ( self ) -> List[Any]: _a : str = self.tokenizer_class(self.vocab_file ) _a : str = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [7, 4, 5, 1_0, 8, 9] ) def __lowercase ( self ) -> List[Any]: if not self.test_rust_tokenizer: return _a : Optional[Any] = self.get_tokenizer() _a : str = self.get_rust_tokenizer() _a : Optional[Any] = '''I was born in 92000, and this is falsé.''' _a : Optional[Any] = tokenizer.tokenize(_a ) _a : List[Any] = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _a : List[Any] = tokenizer.encode(_a , add_special_tokens=_a ) _a : Any = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Dict = self.get_rust_tokenizer() _a : Optional[int] = tokenizer.encode(_a ) _a : Dict = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a )
14
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) a__ = { '''configuration_falcon''': ['''FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FalconConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = [ '''FALCON_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FalconForCausalLM''', '''FalconModel''', '''FalconPreTrainedModel''', '''FalconForSequenceClassification''', '''FalconForTokenClassification''', '''FalconForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys a__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
14
import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : Dict = '''ZinengTang/tvlt-base''' _a : List[str] = tempfile.mkdtemp() def __lowercase ( self , **_a ) -> int: return TvltImageProcessor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self , **_a ) -> List[Any]: return TvltFeatureExtractor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self ) -> Optional[int]: shutil.rmtree(self.tmpdirname ) def __lowercase ( self ) -> Dict: _a : Union[str, Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Optional[int] = TvltProcessor(image_processor=_a , feature_extractor=_a ) processor.save_pretrained(self.tmpdirname ) _a : Any = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor , _a ) self.assertIsInstance(processor.image_processor , _a ) def __lowercase ( self ) -> Any: _a : Optional[Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : Union[str, Any] = np.ones([1_2_0_0_0] ) _a : Dict = feature_extractor(_a , return_tensors='''np''' ) _a : Tuple = processor(audio=_a , return_tensors='''np''' ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> int: _a : Optional[Any] = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Optional[Any] = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[Any] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = image_processor(_a , return_tensors='''np''' ) _a : Optional[int] = processor(images=_a , return_tensors='''np''' ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> Union[str, Any]: _a : int = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Any = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[str] = np.ones([1_2_0_0_0] ) _a : Optional[int] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = processor(audio=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __lowercase ( self ) -> Union[str, Any]: _a : str = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' , )
14
1
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''bigcode/gpt_bigcode-santacoder''': '''https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json''', } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[Any] = "gpt_bigcode" UpperCAmelCase__ : Optional[Any] = ["past_key_values"] UpperCAmelCase__ : Tuple = { "hidden_size": "n_embd", "max_position_embeddings": "n_positions", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self , _a=5_0_2_5_7 , _a=1_0_2_4 , _a=7_6_8 , _a=1_2 , _a=1_2 , _a=None , _a="gelu_pytorch_tanh" , _a=0.1 , _a=0.1 , _a=0.1 , _a=1e-5 , _a=0.02 , _a=True , _a=True , _a=5_0_2_5_6 , _a=5_0_2_5_6 , _a=True , _a=True , _a=True , **_a , ) -> List[str]: _a : Optional[int] = vocab_size _a : Union[str, Any] = n_positions _a : Optional[int] = n_embd _a : List[Any] = n_layer _a : Optional[int] = n_head _a : Any = n_inner _a : Optional[int] = activation_function _a : Optional[int] = resid_pdrop _a : Union[str, Any] = embd_pdrop _a : Any = attn_pdrop _a : int = layer_norm_epsilon _a : List[Any] = initializer_range _a : List[str] = scale_attn_weights _a : List[Any] = use_cache _a : List[Any] = attention_softmax_in_fpaa _a : List[str] = scale_attention_softmax_in_fpaa _a : Dict = multi_query _a : List[Any] = bos_token_id _a : str = eos_token_id super().__init__(bos_token_id=_a , eos_token_id=_a , **_a )
14
def __UpperCAmelCase ( __a : str ) -> list: """simple docstring""" if n_term == "": return [] _a : list = [] for temp in range(int(__a ) ): series.append(F"""1/{temp + 1}""" if series else '''1''' ) return series if __name__ == "__main__": a__ = 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))
14
1
def __UpperCAmelCase ( __a : int ) -> str: """simple docstring""" _a : List[str] = int(__a ) if decimal in (0, 1): # Exit cases for the recursion return str(__a ) _a , _a : List[Any] = divmod(__a ,2 ) return binary_recursive(__a ) + str(__a ) def __UpperCAmelCase ( __a : str ) -> str: """simple docstring""" _a : Union[str, Any] = str(__a ).strip() if not number: raise ValueError('''No input value was provided''' ) _a : List[Any] = '''-''' if number.startswith('''-''' ) else '''''' _a : Union[str, Any] = number.lstrip('''-''' ) if not number.isnumeric(): raise ValueError('''Input value is not an integer''' ) return F"""{negative}0b{binary_recursive(int(__a ) )}""" if __name__ == "__main__": from doctest import testmod testmod()
14
import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Optional[int] ) -> Dict: """simple docstring""" return params[F"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :] def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[int] ,__a : int ,__a : List[str]="attention" ) -> List[str]: """simple docstring""" _a : str = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :] ) _a : Tuple = k_tmp.reshape(k_tmp.shape[0] ,k_tmp.shape[1] * k_tmp.shape[2] ) _a : Any = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :] ) _a : Dict = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] ,o_tmp.shape[2] ) _a : Union[str, Any] = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :] ) _a : Any = q_tmp.reshape(q_tmp.shape[0] ,q_tmp.shape[1] * q_tmp.shape[2] ) _a : Tuple = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :] ) _a : int = v_tmp.reshape(v_tmp.shape[0] ,v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Union[str, Any] ,__a : List[Any] ,__a : Any=False ) -> Any: """simple docstring""" if split_mlp_wi: _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :] _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :] _a : List[str] = (wi_a, wi_a) else: _a : List[str] = params[F"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :] _a : Optional[int] = params[F"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :] return wi, wo def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Union[str, Any] ,__a : str ) -> List[str]: """simple docstring""" return params[F"""{prefix}/{prefix}/{layer_name}/scale"""][:, i] def __UpperCAmelCase ( __a : dict ,*, __a : int ,__a : bool ,__a : bool = False ) -> Any: """simple docstring""" _a : Dict = traverse_util.flatten_dict(variables['''target'''] ) _a : Any = {'''/'''.join(__a ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi _a : Optional[int] = '''encoder/encoder/mlp/wi_0/kernel''' in old print('''Split MLP:''' ,__a ) _a : Tuple = collections.OrderedDict() # Shared embeddings. _a : Any = old['''token_embedder/embedding'''] # Encoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Optional[Any] = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_attention_layer_norm''' ) _a , _a , _a , _a : List[str] = tax_attention_lookup(__a ,__a ,'''encoder''' ,'''attention''' ) _a : List[str] = layer_norm _a : Optional[Any] = k.T _a : str = o.T _a : List[Any] = q.T _a : Tuple = v.T # Block i, layer 1 (MLP). _a : str = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Any = tax_mlp_lookup(__a ,__a ,'''encoder''' ,__a ) _a : str = layer_norm if split_mlp_wi: _a : List[Any] = wi[0].T _a : Any = wi[1].T else: _a : Any = wi.T _a : Optional[Any] = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Dict = tax_relpos_bias_lookup( __a ,__a ,'''encoder''' ).T _a : List[str] = old['''encoder/encoder_norm/scale'''] if not scalable_attention: _a : List[Any] = tax_relpos_bias_lookup( __a ,0 ,'''encoder''' ).T _a : Optional[Any] = tax_relpos_bias_lookup( __a ,0 ,'''decoder''' ).T if not is_encoder_only: # Decoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Union[str, Any] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_self_attention_layer_norm''' ) _a , _a , _a , _a : Optional[Any] = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''self_attention''' ) _a : Optional[Any] = layer_norm _a : Dict = k.T _a : str = o.T _a : str = q.T _a : List[str] = v.T # Block i, layer 1 (Cross Attention). _a : Any = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_cross_attention_layer_norm''' ) _a , _a , _a , _a : str = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''encoder_decoder_attention''' ) _a : Optional[Any] = layer_norm _a : Optional[int] = k.T _a : Dict = o.T _a : str = q.T _a : int = v.T # Block i, layer 2 (MLP). _a : Optional[int] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Tuple = tax_mlp_lookup(__a ,__a ,'''decoder''' ,__a ) _a : Optional[Any] = layer_norm if split_mlp_wi: _a : List[str] = wi[0].T _a : List[Any] = wi[1].T else: _a : Dict = wi.T _a : str = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Tuple = tax_relpos_bias_lookup(__a ,__a ,'''decoder''' ).T _a : Tuple = old['''decoder/decoder_norm/scale'''] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: _a : Any = old['''decoder/logits_dense/kernel'''].T return new def __UpperCAmelCase ( __a : Dict ,__a : bool ) -> Tuple: """simple docstring""" _a : Tuple = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: _a : Any = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: _a : Optional[int] = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''' ) _a : str = state_dict['''shared.weight'''] return state_dict def __UpperCAmelCase ( __a : List[str] ,__a : Union[str, Any] ,__a : Dict ,__a : Union[str, Any] ,__a : List[Any] ) -> int: """simple docstring""" _a : List[str] = checkpoints.load_tax_checkpoint(__a ) _a : str = convert_tax_to_pytorch( __a ,num_layers=config.num_layers ,is_encoder_only=__a ,scalable_attention=__a ) _a : str = make_state_dict(__a ,__a ) model.load_state_dict(__a ,strict=__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : Any ,__a : Union[str, Any] ,__a : bool = False ,__a : bool = False ,) -> Optional[Any]: """simple docstring""" _a : List[str] = MTaConfig.from_json_file(__a ) print(F"""Building PyTorch model from configuration: {config}""" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: _a : Any = UMTaEncoderModel(__a ) else: _a : Tuple = UMTaForConditionalGeneration(__a ) # Load weights from tf checkpoint load_tax_weights_in_ta(__a ,__a ,__a ,__a ,__a ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(__a ) # Verify that we can load the checkpoint. model.from_pretrained(__a ) print('''Done''' ) if __name__ == "__main__": a__ = argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''') # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained T5 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.''' ) parser.add_argument( '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False ) parser.add_argument( '''--scalable_attention''', action='''store_true''', help='''Whether the model uses scaled attention (umt5 model)''', default=False, ) a__ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
14
1
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor a__ = logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self , *_a , **_a ) -> None: warnings.warn( '''The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use YolosImageProcessor instead.''' , _a , ) super().__init__(*_a , **_a )
14
import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap a__ = '''Usage of script: script_name <size_of_canvas:int>''' a__ = [0] * 100 + [1] * 10 random.shuffle(choice) def __UpperCAmelCase ( __a : int ) -> list[list[bool]]: """simple docstring""" _a : int = [[False for i in range(__a )] for j in range(__a )] return canvas def __UpperCAmelCase ( __a : list[list[bool]] ) -> None: """simple docstring""" for i, row in enumerate(__a ): for j, _ in enumerate(__a ): _a : Optional[int] = bool(random.getrandbits(1 ) ) def __UpperCAmelCase ( __a : list[list[bool]] ) -> list[list[bool]]: """simple docstring""" _a : Any = np.array(__a ) _a : Optional[int] = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(__a ): for c, pt in enumerate(__a ): _a : Tuple = __judge_point( __a ,current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) _a : List[str] = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. _a : list[list[bool]] = current_canvas.tolist() return return_canvas def __UpperCAmelCase ( __a : bool ,__a : list[list[bool]] ) -> bool: """simple docstring""" _a : Optional[Any] = 0 _a : str = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. _a : Optional[int] = pt if pt: if alive < 2: _a : Dict = False elif alive == 2 or alive == 3: _a : Optional[Any] = True elif alive > 3: _a : str = False else: if alive == 3: _a : int = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) a__ = int(sys.argv[1]) # main working structure of this module. a__ = create_canvas(canvas_size) seed(c) a__ , a__ = plt.subplots() fig.show() a__ = ListedColormap(['''w''', '''k''']) try: while True: a__ = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
14
1
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_torch_available from transformers.testing_utils import require_torch, torch_device if is_torch_available(): from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self , _a ) -> str: for model_result in results.values(): for batch_size, sequence_length in zip(model_result['''bs'''] , model_result['''ss'''] ): _a : Optional[Any] = model_result['''result'''][batch_size][sequence_length] self.assertIsNotNone(_a ) def __lowercase ( self ) -> Tuple: _a : Tuple = '''sshleifer/tiny-gpt2''' _a : Dict = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : int = PyTorchBenchmark(_a ) _a : str = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowercase ( self ) -> str: _a : str = '''sgugger/tiny-distilbert-classification''' _a : str = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , only_pretrain_model=_a , ) _a : Any = PyTorchBenchmark(_a ) _a : Dict = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowercase ( self ) -> int: _a : Optional[Any] = '''sshleifer/tiny-gpt2''' _a : Optional[int] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , torchscript=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : int = PyTorchBenchmark(_a ) _a : Any = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''' ) def __lowercase ( self ) -> int: _a : Optional[int] = '''sshleifer/tiny-gpt2''' _a : Any = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , fpaa=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : int = PyTorchBenchmark(_a ) _a : Any = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowercase ( self ) -> List[Any]: _a : Union[str, Any] = '''sshleifer/tiny-gpt2''' _a : Dict = AutoConfig.from_pretrained(_a ) # set architectures equal to `None` _a : List[Any] = None _a : Tuple = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : List[Any] = PyTorchBenchmark(_a , configs=[config] ) _a : int = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowercase ( self ) -> Dict: _a : Tuple = '''sshleifer/tiny-gpt2''' _a : int = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : Dict = PyTorchBenchmark(_a ) _a : str = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) @unittest.skipIf(torch_device == '''cpu''' , '''Can\'t do half precision''' ) def __lowercase ( self ) -> Optional[Any]: _a : Any = '''sshleifer/tiny-gpt2''' _a : Tuple = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , fpaa=_a , multi_process=_a , ) _a : Tuple = PyTorchBenchmark(_a ) _a : List[str] = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def __lowercase ( self ) -> Any: _a : int = '''sshleifer/tiny-gpt2''' _a : List[str] = AutoConfig.from_pretrained(_a ) _a : List[str] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : Tuple = PyTorchBenchmark(_a , configs=[config] ) _a : List[str] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowercase ( self ) -> Union[str, Any]: _a : Union[str, Any] = '''sshleifer/tinier_bart''' _a : Optional[int] = AutoConfig.from_pretrained(_a ) _a : Optional[int] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : List[str] = PyTorchBenchmark(_a , configs=[config] ) _a : Any = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def __lowercase ( self ) -> int: _a : Optional[int] = '''sshleifer/tiny-gpt2''' _a : int = AutoConfig.from_pretrained(_a ) _a : Union[str, Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : Tuple = PyTorchBenchmark(_a , configs=[config] ) _a : str = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def __lowercase ( self ) -> List[Any]: _a : Dict = '''sshleifer/tinier_bart''' _a : Dict = AutoConfig.from_pretrained(_a ) _a : List[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_a , ) _a : int = PyTorchBenchmark(_a , configs=[config] ) _a : Any = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def __lowercase ( self ) -> List[str]: _a : List[str] = '''sshleifer/tiny-gpt2''' with tempfile.TemporaryDirectory() as tmp_dir: _a : Tuple = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , save_to_csv=_a , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(_a , '''inf_time.csv''' ) , train_memory_csv_file=os.path.join(_a , '''train_mem.csv''' ) , inference_memory_csv_file=os.path.join(_a , '''inf_mem.csv''' ) , train_time_csv_file=os.path.join(_a , '''train_time.csv''' ) , env_info_csv_file=os.path.join(_a , '''env.csv''' ) , multi_process=_a , ) _a : List[str] = PyTorchBenchmark(_a ) benchmark.run() self.assertTrue(Path(os.path.join(_a , '''inf_time.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(_a , '''train_time.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(_a , '''inf_mem.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(_a , '''train_mem.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(_a , '''env.csv''' ) ).exists() ) def __lowercase ( self ) -> Optional[Any]: _a : Optional[int] = '''sshleifer/tiny-gpt2''' def _check_summary_is_not_empty(_a ): self.assertTrue(hasattr(_a , '''sequential''' ) ) self.assertTrue(hasattr(_a , '''cumulative''' ) ) self.assertTrue(hasattr(_a , '''current''' ) ) self.assertTrue(hasattr(_a , '''total''' ) ) with tempfile.TemporaryDirectory() as tmp_dir: _a : str = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_a , inference=_a , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(_a , '''log.txt''' ) , log_print=_a , trace_memory_line_by_line=_a , multi_process=_a , ) _a : List[str] = PyTorchBenchmark(_a ) _a : Any = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) _check_summary_is_not_empty(result.train_summary ) self.assertTrue(Path(os.path.join(_a , '''log.txt''' ) ).exists() )
14
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''', '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''', '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''', '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''', '''funnel-transformer/intermediate''': ( '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json''' ), '''funnel-transformer/intermediate-base''': ( '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json''' ), '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''', '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''', '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''', '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''', } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = "funnel" UpperCAmelCase__ : Tuple = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self , _a=3_0_5_2_2 , _a=[4, 4, 4] , _a=None , _a=2 , _a=7_6_8 , _a=1_2 , _a=6_4 , _a=3_0_7_2 , _a="gelu_new" , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=None , _a=1e-9 , _a="mean" , _a="relative_shift" , _a=True , _a=True , _a=True , **_a , ) -> List[Any]: _a : Optional[int] = vocab_size _a : Dict = block_sizes _a : Optional[int] = [1] * len(_a ) if block_repeats is None else block_repeats assert len(_a ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." _a : int = num_decoder_layers _a : List[str] = d_model _a : Optional[Any] = n_head _a : Tuple = d_head _a : Dict = d_inner _a : List[str] = hidden_act _a : int = hidden_dropout _a : Union[str, Any] = attention_dropout _a : Tuple = activation_dropout _a : Optional[Any] = initializer_range _a : Dict = initializer_std _a : Union[str, Any] = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported.""" _a : Any = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported.""" _a : Optional[Any] = attention_type _a : int = separate_cls _a : Tuple = truncate_seq _a : List[Any] = pool_q_only super().__init__(**_a ) @property def __lowercase ( self ) -> Tuple: return sum(self.block_sizes ) @num_hidden_layers.setter def __lowercase ( self , _a ) -> List[str]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.''' ) @property def __lowercase ( self ) -> Optional[int]: return len(self.block_sizes ) @num_blocks.setter def __lowercase ( self , _a ) -> Dict: raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.''' )
14
1
import os from typing import List, Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...tokenization_utils_base import AddedToken from ...utils import logging a__ = logging.get_logger(__name__) a__ = {'''vocab_file''': '''vocab.txt'''} a__ = { '''vocab_file''': { '''facebook/esm2_t6_8M_UR50D''': '''https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt''', '''facebook/esm2_t12_35M_UR50D''': '''https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt''', }, } a__ = { '''facebook/esm2_t6_8M_UR50D''': 1024, '''facebook/esm2_t12_35M_UR50D''': 1024, } def __UpperCAmelCase ( __a : Tuple ) -> Tuple: """simple docstring""" with open(__a ,'''r''' ) as f: _a : Tuple = f.read().splitlines() return [l.strip() for l in lines] class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : int = VOCAB_FILES_NAMES UpperCAmelCase__ : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : Dict = ["input_ids", "attention_mask"] def __init__( self , _a , _a="<unk>" , _a="<cls>" , _a="<pad>" , _a="<mask>" , _a="<eos>" , **_a , ) -> Tuple: super().__init__(**_a ) _a : List[str] = load_vocab_file(_a ) _a : Optional[Any] = dict(enumerate(self.all_tokens ) ) _a : List[Any] = {tok: ind for ind, tok in enumerate(self.all_tokens )} _a : str = unk_token _a : Any = cls_token _a : int = pad_token _a : Optional[Any] = mask_token _a : Optional[int] = eos_token _a : List[Any] = self.all_tokens self._create_trie(self.unique_no_split_tokens ) def __lowercase ( self , _a ) -> str: return self._id_to_token.get(_a , self.unk_token ) def __lowercase ( self , _a ) -> int: return self._token_to_id.get(_a , self._token_to_id.get(self.unk_token ) ) def __lowercase ( self , _a , **_a ) -> List[Any]: return text.split() def __lowercase ( self , _a=False ) -> List[Any]: return len(self._id_to_token ) def __lowercase ( self ) -> Any: return {token: i for i, token in enumerate(self.all_tokens )} def __lowercase ( self , _a ) -> int: return self._token_to_id.get(_a , self._token_to_id.get(self.unk_token ) ) def __lowercase ( self , _a ) -> str: return self._id_to_token.get(_a , self.unk_token ) def __lowercase ( self , _a , _a = None ) -> List[int]: _a : Tuple = [self.cls_token_id] _a : List[str] = [self.eos_token_id] # No sep token in ESM vocabulary if token_ids_a is None: if self.eos_token_id is None: return cls + token_ids_a else: return cls + token_ids_a + sep elif self.eos_token_id is None: raise ValueError('''Cannot tokenize multiple sequences when EOS token is not set!''' ) return cls + token_ids_a + sep + token_ids_a + sep # Multiple inputs always have an EOS token def __lowercase ( self , _a , _a = None , _a = False ) -> List[int]: if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if token in self.all_special_ids else 0 for token in token_ids_a] _a : List[Any] = [1] + ([0] * len(_a )) + [1] if token_ids_a is not None: mask += [0] * len(_a ) + [1] return mask def __lowercase ( self , _a , _a ) -> str: _a : Optional[Any] = os.path.join(_a , (filename_prefix + '''-''' if filename_prefix else '''''') + '''vocab.txt''' ) with open(_a , '''w''' ) as f: f.write('''\n'''.join(self.all_tokens ) ) return (vocab_file,) @property def __lowercase ( self ) -> int: return self.get_vocab_size(with_added_tokens=_a ) def __lowercase ( self , _a , _a = False ) -> int: return super()._add_tokens(_a , special_tokens=_a )
14
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : int = "mobilenet_v1" def __init__( self , _a=3 , _a=2_2_4 , _a=1.0 , _a=8 , _a="relu6" , _a=True , _a=0.999 , _a=0.02 , _a=0.001 , **_a , ) -> List[Any]: super().__init__(**_a ) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''' ) _a : Tuple = num_channels _a : str = image_size _a : Tuple = depth_multiplier _a : Any = min_depth _a : int = hidden_act _a : Optional[Any] = tf_padding _a : str = classifier_dropout_prob _a : Optional[int] = initializer_range _a : Any = layer_norm_eps class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = version.parse("1.11" ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([('''pixel_values''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})] ) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> float: return 1e-4
14
1
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __UpperCAmelCase ( __a : str ,__a : Optional[int] ) -> str: """simple docstring""" assert isinstance(__a ,__a ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' ,[False, True] ) def __UpperCAmelCase ( __a : Any ,__a : Union[str, Any] ,__a : int ) -> Tuple: """simple docstring""" _a : str = tmp_path / '''cache''' _a : List[Any] = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _a : List[Any] = TextDatasetReader(__a ,cache_dir=__a ,keep_in_memory=__a ).read() _check_text_dataset(__a ,__a ) @pytest.mark.parametrize( '''features''' ,[ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] ,) def __UpperCAmelCase ( __a : Optional[int] ,__a : Optional[Any] ,__a : int ) -> List[str]: """simple docstring""" _a : Dict = tmp_path / '''cache''' _a : Optional[Any] = {'''text''': '''string'''} _a : Optional[Any] = features.copy() if features else default_expected_features _a : Tuple = ( Features({feature: Value(__a ) for feature, dtype in features.items()} ) if features is not None else None ) _a : List[str] = TextDatasetReader(__a ,features=__a ,cache_dir=__a ).read() _check_text_dataset(__a ,__a ) @pytest.mark.parametrize('''split''' ,[None, NamedSplit('''train''' ), '''train''', '''test'''] ) def __UpperCAmelCase ( __a : str ,__a : int ,__a : List[str] ) -> List[Any]: """simple docstring""" _a : Any = tmp_path / '''cache''' _a : List[str] = {'''text''': '''string'''} _a : str = TextDatasetReader(__a ,cache_dir=__a ,split=__a ).read() _check_text_dataset(__a ,__a ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' ,[str, list] ) def __UpperCAmelCase ( __a : int ,__a : int ,__a : str ) -> Optional[Any]: """simple docstring""" if issubclass(__a ,__a ): _a : Tuple = text_path elif issubclass(__a ,__a ): _a : Tuple = [text_path] _a : Union[str, Any] = tmp_path / '''cache''' _a : Any = {'''text''': '''string'''} _a : int = TextDatasetReader(__a ,cache_dir=__a ).read() _check_text_dataset(__a ,__a ) def __UpperCAmelCase ( __a : Tuple ,__a : Union[str, Any] ,__a : Optional[int]=("train",) ) -> List[Any]: """simple docstring""" assert isinstance(__a ,__a ) for split in splits: _a : Union[str, Any] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' ,[False, True] ) def __UpperCAmelCase ( __a : int ,__a : int ,__a : Optional[Any] ) -> Any: """simple docstring""" _a : List[str] = tmp_path / '''cache''' _a : List[str] = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): _a : Union[str, Any] = TextDatasetReader({'''train''': text_path} ,cache_dir=__a ,keep_in_memory=__a ).read() _check_text_datasetdict(__a ,__a ) @pytest.mark.parametrize( '''features''' ,[ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] ,) def __UpperCAmelCase ( __a : Dict ,__a : List[str] ,__a : Optional[int] ) -> Tuple: """simple docstring""" _a : Optional[Any] = tmp_path / '''cache''' # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" _a : Optional[Any] = {'''text''': '''string'''} _a : str = features.copy() if features else default_expected_features _a : Dict = ( Features({feature: Value(__a ) for feature, dtype in features.items()} ) if features is not None else None ) _a : Union[str, Any] = TextDatasetReader({'''train''': text_path} ,features=__a ,cache_dir=__a ).read() _check_text_datasetdict(__a ,__a ) @pytest.mark.parametrize('''split''' ,[None, NamedSplit('''train''' ), '''train''', '''test'''] ) def __UpperCAmelCase ( __a : int ,__a : int ,__a : Dict ) -> Dict: """simple docstring""" if split: _a : Union[str, Any] = {split: text_path} else: _a : Any = '''train''' _a : Any = {'''train''': text_path, '''test''': text_path} _a : List[str] = tmp_path / '''cache''' _a : Union[str, Any] = {'''text''': '''string'''} _a : Tuple = TextDatasetReader(__a ,cache_dir=__a ).read() _check_text_datasetdict(__a ,__a ,splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
14
a__ = '''Input must be a string of 8 numbers plus letter''' a__ = '''TRWAGMYFPDXBNJZSQVHLCKE''' def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" if not isinstance(__a ,__a ): _a : List[str] = F"""Expected string as input, found {type(__a ).__name__}""" raise TypeError(__a ) _a : List[Any] = spanish_id.replace('''-''' ,'''''' ).upper() if len(__a ) != 9: raise ValueError(__a ) try: _a : Any = int(spanish_id_clean[0:8] ) _a : str = spanish_id_clean[8] except ValueError as ex: raise ValueError(__a ) from ex if letter.isdigit(): raise ValueError(__a ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
14
1
import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) a__ = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation='''relu''') ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation='''relu''')) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation='''relu''')) classifier.add(layers.Dense(units=1, activation='''sigmoid''')) # Compiling the CNN classifier.compile( optimizer='''adam''', loss='''binary_crossentropy''', metrics=['''accuracy'''] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') a__ = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) a__ = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) a__ = train_datagen.flow_from_directory( '''dataset/training_set''', target_size=(64, 64), batch_size=32, class_mode='''binary''' ) a__ = test_datagen.flow_from_directory( '''dataset/test_set''', target_size=(64, 64), batch_size=32, class_mode='''binary''' ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save('''cnn.h5''') # Part 3 - Making new predictions a__ = tf.keras.preprocessing.image.load_img( '''dataset/single_prediction/image.png''', target_size=(64, 64) ) a__ = tf.keras.preprocessing.image.img_to_array(test_image) a__ = np.expand_dims(test_image, axis=0) a__ = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: a__ = '''Normal''' if result[0][0] == 1: a__ = '''Abnormality detected'''
14
from random import randint from tempfile import TemporaryFile import numpy as np def __UpperCAmelCase ( __a : Optional[Any] ,__a : int ,__a : Any ) -> int: """simple docstring""" _a : int = 0 if start < end: _a : Tuple = randint(__a ,__a ) _a : Tuple = a[end] _a : List[str] = a[pivot] _a : Any = temp _a , _a : Optional[int] = _in_place_partition(__a ,__a ,__a ) count += _in_place_quick_sort(__a ,__a ,p - 1 ) count += _in_place_quick_sort(__a ,p + 1 ,__a ) return count def __UpperCAmelCase ( __a : List[Any] ,__a : Tuple ,__a : Dict ) -> Dict: """simple docstring""" _a : Dict = 0 _a : Tuple = randint(__a ,__a ) _a : List[Any] = a[end] _a : str = a[pivot] _a : str = temp _a : Dict = start - 1 for index in range(__a ,__a ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value _a : int = new_pivot_index + 1 _a : Any = a[new_pivot_index] _a : Optional[int] = a[index] _a : str = temp _a : Union[str, Any] = a[new_pivot_index + 1] _a : Tuple = a[end] _a : Any = temp return new_pivot_index + 1, count a__ = TemporaryFile() a__ = 100 # 1000 elements are to be sorted a__ , a__ = 0, 1 # mean and standard deviation a__ = np.random.normal(mu, sigma, p) np.save(outfile, X) print('''The array is''') print(X) outfile.seek(0) # using the same array a__ = np.load(outfile) a__ = len(M) - 1 a__ = _in_place_quick_sort(M, 0, r) print( '''No of Comparisons for 100 elements selected from a standard normal distribution''' '''is :''' ) print(z)
14
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available a__ = { '''configuration_audio_spectrogram_transformer''': [ '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ASTConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = [ '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ASTForAudioClassification''', '''ASTModel''', '''ASTPreTrainedModel''', ] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = ['''ASTFeatureExtractor'''] if TYPE_CHECKING: from .configuration_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ASTConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_audio_spectrogram_transformer import ( AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ASTForAudioClassification, ASTModel, ASTPreTrainedModel, ) try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor else: import sys a__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
14
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = MgpstrTokenizer UpperCAmelCase__ : int = False UpperCAmelCase__ : Union[str, Any] = {} UpperCAmelCase__ : List[Any] = False def __lowercase ( self ) -> Any: super().setUp() # fmt: off _a : Tuple = ['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _a : Optional[int] = dict(zip(_a , range(len(_a ) ) ) ) _a : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) def __lowercase ( self , **_a ) -> Dict: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __lowercase ( self , _a ) -> Tuple: _a : List[str] = '''tester''' _a : Optional[Any] = '''tester''' return input_text, output_text @unittest.skip('''MGP-STR always lower cases letters.''' ) def __lowercase ( self ) -> Any: pass def __lowercase ( self ) -> Any: _a : Union[str, Any] = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a : int = '''[SPECIAL_TOKEN]''' tokenizer.add_special_tokens({'''cls_token''': special_token} ) _a : Tuple = tokenizer.encode([special_token] , add_special_tokens=_a ) self.assertEqual(len(_a ) , 1 ) _a : Tuple = tokenizer.decode(_a , skip_special_tokens=_a ) self.assertTrue(special_token not in decoded ) def __lowercase ( self ) -> Tuple: _a : List[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a , _a : int = self.get_input_output_texts(_a ) _a : List[str] = tokenizer.tokenize(_a ) _a : Optional[int] = tokenizer.convert_tokens_to_ids(_a ) _a : Tuple = tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Optional[int] = tokenizer.convert_ids_to_tokens(_a ) self.assertNotEqual(len(_a ) , 0 ) _a : int = tokenizer.decode(_a ) self.assertIsInstance(_a , _a ) self.assertEqual(text_a.replace(''' ''' , '''''' ) , _a ) @unittest.skip('''MGP-STR tokenizer only handles one sequence.''' ) def __lowercase ( self ) -> List[str]: pass @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer''' ) def __lowercase ( self ) -> Optional[Any]: pass
14
1
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler a__ = 16 a__ = 32 def __UpperCAmelCase ( __a : Accelerator ,__a : int = 16 ,__a : str = "bert-base-cased" ) -> Optional[int]: """simple docstring""" _a : Union[str, Any] = AutoTokenizer.from_pretrained(__a ) _a : Dict = load_dataset('''glue''' ,'''mrpc''' ) def tokenize_function(__a : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) _a : List[Any] = tokenizer(examples['''sentence1'''] ,examples['''sentence2'''] ,truncation=__a ,max_length=__a ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset _a : Any = datasets.map( __a ,batched=__a ,remove_columns=['''idx''', '''sentence1''', '''sentence2'''] ,load_from_cache_file=__a ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _a : Optional[Any] = tokenized_datasets.rename_column('''label''' ,'''labels''' ) def collate_fn(__a : List[str] ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(__a ,padding='''max_length''' ,max_length=128 ,return_tensors='''pt''' ) return tokenizer.pad(__a ,padding='''longest''' ,return_tensors='''pt''' ) # Instantiate dataloaders. _a : str = DataLoader( tokenized_datasets['''train'''] ,shuffle=__a ,collate_fn=__a ,batch_size=__a ) _a : int = DataLoader( tokenized_datasets['''validation'''] ,shuffle=__a ,collate_fn=__a ,batch_size=__a ) return train_dataloader, eval_dataloader def __UpperCAmelCase ( __a : List[str] ,__a : Union[str, Any] ,__a : Any ,__a : Optional[int] ) -> Tuple: """simple docstring""" model.eval() _a : List[Any] = 0 for step, batch in enumerate(__a ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): _a : Any = model(**__a ) _a : Optional[Any] = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times _a , _a : Any = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(__a ) - 1: _a : Optional[int] = predictions[: len(eval_dataloader.dataset ) - samples_seen] _a : Dict = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=__a ,references=__a ,) _a : List[str] = metric.compute() return eval_metric["accuracy"] def __UpperCAmelCase ( __a : int ,__a : Any ) -> Union[str, Any]: """simple docstring""" _a : int = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _a : Any = config['''lr'''] _a : List[Any] = int(config['''num_epochs'''] ) _a : List[Any] = int(config['''seed'''] ) _a : Tuple = int(config['''batch_size'''] ) _a : int = args.model_name_or_path set_seed(__a ) _a , _a : Optional[int] = get_dataloaders(__a ,__a ,__a ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _a : Dict = AutoModelForSequenceClassification.from_pretrained(__a ,return_dict=__a ) # Instantiate optimizer _a : Any = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) _a : Any = optimizer_cls(params=model.parameters() ,lr=__a ) if accelerator.state.deepspeed_plugin is not None: _a : List[Any] = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: _a : Dict = 1 _a : Optional[int] = (len(__a ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): _a : int = get_linear_schedule_with_warmup( optimizer=__a ,num_warmup_steps=0 ,num_training_steps=__a ,) else: _a : List[str] = DummyScheduler(__a ,total_num_steps=__a ,warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. _a , _a , _a , _a , _a : str = accelerator.prepare( __a ,__a ,__a ,__a ,__a ) # We need to keep track of how many total steps we have iterated over _a : Any = 0 # We also need to keep track of the stating epoch so files are named properly _a : Tuple = 0 _a : int = evaluate.load('''glue''' ,'''mrpc''' ) _a : Dict = num_epochs if args.partial_train_epoch is not None: _a : Union[str, Any] = args.partial_train_epoch if args.resume_from_checkpoint: accelerator.load_state(args.resume_from_checkpoint ) _a : List[str] = args.resume_from_checkpoint.split('''epoch_''' )[1] _a : Tuple = '''''' for char in epoch_string: if char.isdigit(): state_epoch_num += char else: break _a : Optional[Any] = int(__a ) + 1 _a : List[Any] = evaluation_loop(__a ,__a ,__a ,__a ) accelerator.print('''resumed checkpoint performance:''' ,__a ) accelerator.print('''resumed checkpoint\'s scheduler\'s lr:''' ,lr_scheduler.get_lr()[0] ) accelerator.print('''resumed optimizers\'s lr:''' ,optimizer.param_groups[0]['''lr'''] ) with open(os.path.join(args.output_dir ,F"""state_{starting_epoch-1}.json""" ) ,'''r''' ) as f: _a : int = json.load(__a ) assert resumed_state["accuracy"] == accuracy, "Accuracy mismatch, loading from checkpoint failed" assert ( resumed_state["lr"] == lr_scheduler.get_lr()[0] ), "Scheduler learning rate mismatch, loading from checkpoint failed" assert ( resumed_state["optimizer_lr"] == optimizer.param_groups[0]["lr"] ), "Optimizer learning rate mismatch, loading from checkpoint failed" assert resumed_state["epoch"] == starting_epoch - 1, "Epoch mismatch, loading from checkpoint failed" return # Now we train the model _a : Dict = {} for epoch in range(__a ,__a ): model.train() for step, batch in enumerate(__a ): _a : List[Any] = model(**__a ) _a : int = outputs.loss _a : Union[str, Any] = loss / gradient_accumulation_steps accelerator.backward(__a ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 _a : Tuple = F"""epoch_{epoch}""" _a : str = os.path.join(args.output_dir ,__a ) accelerator.save_state(__a ) _a : Any = evaluation_loop(__a ,__a ,__a ,__a ) _a : Tuple = accuracy _a : Optional[Any] = lr_scheduler.get_lr()[0] _a : Dict = optimizer.param_groups[0]['''lr'''] _a : Dict = epoch _a : List[Any] = overall_step accelerator.print(F"""epoch {epoch}:""" ,__a ) accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir ,F"""state_{epoch}.json""" ) ,'''w''' ) as f: json.dump(__a ,__a ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" _a : List[str] = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' ,type=__a ,default='''bert-base-cased''' ,help='''Path to pretrained model or model identifier from huggingface.co/models.''' ,required=__a ,) parser.add_argument( '''--output_dir''' ,type=__a ,default='''.''' ,help='''Optional save directory where all checkpoint folders will be stored. Default is the current working directory.''' ,) parser.add_argument( '''--resume_from_checkpoint''' ,type=__a ,default=__a ,help='''If the training should continue from a checkpoint folder.''' ,) parser.add_argument( '''--partial_train_epoch''' ,type=__a ,default=__a ,help='''If passed, the training will stop after this number of epochs.''' ,) parser.add_argument( '''--num_epochs''' ,type=__a ,default=2 ,help='''Number of train epochs.''' ,) _a : Optional[int] = parser.parse_args() _a : str = {'''lr''': 2E-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(__a ,__a ) if __name__ == "__main__": main()
14
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> List[Any]: _a : int = 0 def __lowercase ( self ) -> List[str]: _a : Dict = AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32''' ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : Optional[Any] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: # Ensure we can load the image processor from the feature extractor config with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' _a : Any = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Dict = CLIPConfig() # Create a dummy config file with image_proceesor_type _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : List[str] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally _a : Tuple = AutoImageProcessor.from_pretrained(_a ).to_dict() config_dict.pop('''image_processor_type''' ) _a : Tuple = CLIPImageProcessor(**_a ) # save in new folder model_config.save_pretrained(_a ) config.save_pretrained(_a ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) # make sure private variable is not incorrectly saved _a : Optional[int] = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Any: with self.assertRaisesRegex( _a , '''clip-base is not a local folder and is not a valid model identifier''' ): _a : Dict = AutoImageProcessor.from_pretrained('''clip-base''' ) def __lowercase ( self ) -> List[Any]: with self.assertRaisesRegex( _a , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): _a : List[str] = AutoImageProcessor.from_pretrained(_a , revision='''aaaaaa''' ) def __lowercase ( self ) -> Dict: with self.assertRaisesRegex( _a , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): _a : Optional[int] = AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowercase ( self ) -> Union[str, Any]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_a ): _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_a ): _a : Optional[Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) _a : Union[str, Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a , trust_remote_code=_a ) self.assertEqual(reloaded_image_processor.__class__.__name__ , '''NewImageProcessor''' ) def __lowercase ( self ) -> Dict: try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_a ): AutoImageProcessor.register(_a , _a ) with tempfile.TemporaryDirectory() as tmpdirname: _a : int = Path(_a ) / '''preprocessor_config.json''' _a : int = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : int = CustomImageProcessor.from_pretrained(_a ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def __lowercase ( self ) -> Union[str, Any]: class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = True try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # If remote code is not set, the default is to use local _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. _a : int = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub _a : Dict = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(not hasattr(_a , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
14
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) a__ = { '''configuration_perceiver''': ['''PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''PerceiverConfig''', '''PerceiverOnnxConfig'''], '''tokenization_perceiver''': ['''PerceiverTokenizer'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = ['''PerceiverFeatureExtractor'''] a__ = ['''PerceiverImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = [ '''PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''PerceiverForImageClassificationConvProcessing''', '''PerceiverForImageClassificationFourier''', '''PerceiverForImageClassificationLearned''', '''PerceiverForMaskedLM''', '''PerceiverForMultimodalAutoencoding''', '''PerceiverForOpticalFlow''', '''PerceiverForSequenceClassification''', '''PerceiverLayer''', '''PerceiverModel''', '''PerceiverPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys a__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
14
from __future__ import annotations from dataclasses import dataclass @dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : float UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase__ : TreeNode | None = None def __UpperCAmelCase ( __a : TreeNode | None ) -> bool: """simple docstring""" def is_valid_tree(__a : TreeNode | None ) -> bool: if node is None: return True if not isinstance(__a ,__a ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(__a ): raise ValueError( '''Each node should be type of TreeNode and data should be float.''' ) def is_binary_search_tree_recursive_check( __a : TreeNode | None ,__a : float ,__a : float ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left ,__a ,node.data ) and is_binary_search_tree_recursive_check( node.right ,node.data ,__a ) ) return is_binary_search_tree_recursive_check(__a ,-float('''inf''' ) ,float('''inf''' ) ) if __name__ == "__main__": import doctest doctest.testmod()
14
1
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def __UpperCAmelCase ( ) -> List[str]: """simple docstring""" _a : Optional[int] = ArgumentParser('''Transformers CLI tool''' ,usage='''transformers-cli <command> [<args>]''' ) _a : Any = parser.add_subparsers(help='''transformers-cli command helpers''' ) # Register commands ConvertCommand.register_subcommand(__a ) DownloadCommand.register_subcommand(__a ) EnvironmentCommand.register_subcommand(__a ) RunCommand.register_subcommand(__a ) ServeCommand.register_subcommand(__a ) UserCommands.register_subcommand(__a ) AddNewModelCommand.register_subcommand(__a ) AddNewModelLikeCommand.register_subcommand(__a ) LfsCommands.register_subcommand(__a ) PTtoTFCommand.register_subcommand(__a ) # Let's go _a : str = parser.parse_args() if not hasattr(__a ,'''func''' ): parser.print_help() exit(1 ) # Run _a : str = args.func(__a ) service.run() if __name__ == "__main__": main()
14
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake a__ = numpy.array([0, 0]) a__ = numpy.array([0.5, 0.8660254]) a__ = numpy.array([1, 0]) a__ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def __UpperCAmelCase ( __a : list[numpy.ndarray] ,__a : int ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = initial_vectors for _ in range(__a ): _a : int = iteration_step(__a ) return vectors def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = [] for i, start_vector in enumerate(vectors[:-1] ): _a : str = vectors[i + 1] new_vectors.append(__a ) _a : Optional[int] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 ,60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def __UpperCAmelCase ( __a : numpy.ndarray ,__a : float ) -> numpy.ndarray: """simple docstring""" _a : Tuple = numpy.radians(__a ) _a , _a : List[Any] = numpy.cos(__a ), numpy.sin(__a ) _a : Dict = numpy.array(((c, -s), (s, c)) ) return numpy.dot(__a ,__a ) def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> None: """simple docstring""" _a : str = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() _a , _a : Optional[int] = zip(*__a ) plt.plot(__a ,__a ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() a__ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
14
1
import unittest from transformers import RoFormerTokenizer, RoFormerTokenizerFast from transformers.testing_utils import require_rjieba, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_rjieba @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = RoFormerTokenizer UpperCAmelCase__ : Optional[Any] = RoFormerTokenizerFast UpperCAmelCase__ : Tuple = True UpperCAmelCase__ : List[str] = True def __lowercase ( self ) -> Any: super().setUp() def __lowercase ( self , **_a ) -> Any: return self.tokenizer_class.from_pretrained('''junnyu/roformer_chinese_base''' , **_a ) def __lowercase ( self , **_a ) -> Dict: return self.rust_tokenizer_class.from_pretrained('''junnyu/roformer_chinese_base''' , **_a ) def __lowercase ( self ) -> Tuple: _a : Dict = '''永和服装饰品有限公司,今天天气非常好''' _a : Optional[Any] = '''永和 服装 饰品 有限公司 , 今 天 天 气 非常 好''' return input_text, output_text def __lowercase ( self ) -> Dict: _a : Optional[int] = self.get_tokenizer() _a , _a : Any = self.get_chinese_input_output_texts() _a : int = tokenizer.tokenize(_a ) self.assertListEqual(_a , output_text.split() ) _a : Any = tokens + [tokenizer.unk_token] _a : List[str] = [2_2_9_4_3, 2_1_3_3_2, 3_4_4_3_1, 4_5_9_0_4, 1_1_7, 3_0_6, 1_2_3_1, 1_2_3_1, 2_6_5_3, 3_3_9_9_4, 1_2_6_6, 1_0_0] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) def __lowercase ( self ) -> str: _a : Dict = self.get_rust_tokenizer() _a , _a : List[str] = self.get_chinese_input_output_texts() _a : Dict = tokenizer.tokenize(_a ) self.assertListEqual(_a , output_text.split() ) _a : Union[str, Any] = tokens + [tokenizer.unk_token] _a : str = [2_2_9_4_3, 2_1_3_3_2, 3_4_4_3_1, 4_5_9_0_4, 1_1_7, 3_0_6, 1_2_3_1, 1_2_3_1, 2_6_5_3, 3_3_9_9_4, 1_2_6_6, 1_0_0] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) def __lowercase ( self ) -> List[Any]: pass def __lowercase ( self ) -> Tuple: pass def __lowercase ( self ) -> Optional[int]: pass
14
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def __UpperCAmelCase ( __a : Tuple ,__a : Dict ,__a : List[str] ,__a : Optional[Any] ,__a : Tuple ) -> Dict: """simple docstring""" with open(__a ) as metadata_file: _a : Optional[Any] = json.load(__a ) _a : List[Any] = LukeConfig(use_entity_aware_attention=__a ,**metadata['''model_config'''] ) # Load in the weights from the checkpoint_path _a : Optional[Any] = torch.load(__a ,map_location='''cpu''' )['''module'''] # Load the entity vocab file _a : Any = load_original_entity_vocab(__a ) # add an entry for [MASK2] _a : Union[str, Any] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 _a : Dict = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks _a : Optional[int] = AddedToken('''<ent>''' ,lstrip=__a ,rstrip=__a ) _a : Tuple = AddedToken('''<ent2>''' ,lstrip=__a ,rstrip=__a ) 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(__a ) with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''r''' ) as f: _a : List[str] = json.load(__a ) _a : Tuple = '''MLukeTokenizer''' with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''w''' ) as f: json.dump(__a ,__a ) with open(os.path.join(__a ,MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) ,'''w''' ) as f: json.dump(__a ,__a ) _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) # Initialize the embeddings of the special tokens _a : str = tokenizer.convert_tokens_to_ids(['''@'''] )[0] _a : Tuple = tokenizer.convert_tokens_to_ids(['''#'''] )[0] _a : Any = state_dict['''embeddings.word_embeddings.weight'''] _a : Optional[int] = word_emb[ent_init_index].unsqueeze(0 ) _a : Any = word_emb[enta_init_index].unsqueeze(0 ) _a : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: _a : Tuple = state_dict[bias_name] _a : Optional[Any] = decoder_bias[ent_init_index].unsqueeze(0 ) _a : Optional[int] = decoder_bias[enta_init_index].unsqueeze(0 ) _a : Dict = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: _a : Tuple = F"""encoder.layer.{layer_index}.attention.self.""" _a : List[Any] = state_dict[prefix + matrix_name] _a : Dict = state_dict[prefix + matrix_name] _a : List[Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks _a : Union[str, Any] = state_dict['''entity_embeddings.entity_embeddings.weight'''] _a : Optional[int] = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' _a : int = state_dict['''entity_predictions.bias'''] _a : int = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Optional[Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) _a : Optional[int] = LukeForMaskedLM(config=__a ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) _a : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): _a : Optional[int] = state_dict[key] else: _a : Tuple = state_dict[key] _a , _a : int = model.load_state_dict(__a ,strict=__a ) if set(__a ) != {"luke.embeddings.position_ids"}: raise ValueError(F"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(__a ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ,task='''entity_classification''' ) _a : int = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' _a : List[Any] = (0, 9) _a : Tuple = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : int = model(**__a ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base _a : List[str] = torch.Size((1, 33, 768) ) _a : Union[str, Any] = torch.tensor([[0.08_92, 0.05_96, -0.28_19], [0.01_34, 0.11_99, 0.05_73], [-0.01_69, 0.09_27, 0.06_44]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base _a : str = torch.Size((1, 1, 768) ) _a : List[Any] = torch.tensor([[-0.14_82, 0.06_09, 0.03_22]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" F""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify masked word/entity prediction _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) _a : Dict = '''Tokyo is the capital of <mask>.''' _a : List[str] = (24, 30) _a : Optional[int] = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : Optional[Any] = model(**__a ) _a : Any = encoding['''input_ids'''][0].tolist() _a : Optional[Any] = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) _a : Any = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(__a ) _a : Any = outputs.entity_logits[0][0].argmax().item() _a : Optional[Any] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(__a ) ) model.save_pretrained(__a ) def __UpperCAmelCase ( __a : List[Any] ) -> int: """simple docstring""" _a : Union[str, Any] = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] _a : int = [json.loads(__a ) for line in open(__a )] _a : List[Any] = {} for entry in data: _a : int = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: _a : List[Any] = entity_id break _a : Dict = F"""{language}:{entity_name}""" _a : int = entity_id return new_mapping if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) a__ = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
14
1
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ConvNextConfig, UperNetConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import UperNetForSemanticSegmentation from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=1_3 , _a=3_2 , _a=3 , _a=4 , _a=[1_0, 2_0, 3_0, 4_0] , _a=[2, 2, 3, 2] , _a=True , _a=True , _a=3_7 , _a="gelu" , _a=1_0 , _a=0.02 , _a=["stage2", "stage3", "stage4"] , _a=3 , _a=None , ) -> Any: _a : Dict = parent _a : List[str] = batch_size _a : Any = image_size _a : Optional[int] = num_channels _a : List[Any] = num_stages _a : List[str] = hidden_sizes _a : Tuple = depths _a : Any = is_training _a : Optional[int] = use_labels _a : Optional[Any] = intermediate_size _a : Optional[int] = hidden_act _a : Dict = type_sequence_label_size _a : str = initializer_range _a : Optional[int] = out_features _a : List[str] = num_labels _a : Optional[Any] = scope _a : Optional[int] = num_stages def __lowercase ( self ) -> List[Any]: _a : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : Tuple = None if self.use_labels: _a : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a : Any = self.get_config() return config, pixel_values, labels def __lowercase ( self ) -> Dict: return ConvNextConfig( num_channels=self.num_channels , num_stages=self.num_stages , hidden_sizes=self.hidden_sizes , depths=self.depths , is_training=self.is_training , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , out_features=self.out_features , ) def __lowercase ( self ) -> Union[str, Any]: return UperNetConfig( backbone_config=self.get_backbone_config() , hidden_size=5_1_2 , pool_scales=[1, 2, 3, 6] , use_auxiliary_head=_a , auxiliary_loss_weight=0.4 , auxiliary_in_channels=4_0 , auxiliary_channels=2_5_6 , auxiliary_num_convs=1 , auxiliary_concat_input=_a , loss_ignore_index=2_5_5 , num_labels=self.num_labels , ) def __lowercase ( self , _a , _a , _a ) -> Any: _a : Dict = UperNetForSemanticSegmentation(config=_a ) model.to(_a ) model.eval() _a : List[str] = model(_a ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def __lowercase ( self ) -> Optional[Any]: _a : Tuple = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ) : Dict = config_and_inputs _a : Union[str, Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Tuple = (UperNetForSemanticSegmentation,) if is_torch_available() else () UpperCAmelCase__ : Union[str, Any] = {"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {} UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : int = False UpperCAmelCase__ : List[Any] = False UpperCAmelCase__ : List[Any] = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : Tuple = False def __lowercase ( self ) -> str: _a : Dict = UperNetModelTester(self ) _a : Tuple = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=3_7 ) def __lowercase ( self ) -> int: 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 __lowercase ( self ) -> Optional[int]: return def __lowercase ( self ) -> Tuple: _a , _a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Tuple = model_class(_a ) _a : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Any = [*signature.parameters.keys()] _a : Any = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowercase ( self ) -> Tuple: _a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_a ) @unittest.skip(reason='''UperNet does not use inputs_embeds''' ) def __lowercase ( self ) -> Optional[int]: pass @unittest.skip(reason='''UperNet does not support input and output embeddings''' ) def __lowercase ( self ) -> Dict: pass @unittest.skip(reason='''UperNet does not have a base model''' ) def __lowercase ( self ) -> Dict: pass @unittest.skip(reason='''UperNet does not have a base model''' ) def __lowercase ( self ) -> int: pass @require_torch_multi_gpu @unittest.skip(reason='''UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''' ) def __lowercase ( self ) -> Any: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __lowercase ( self ) -> List[str]: pass def __lowercase ( self ) -> int: def check_hidden_states_output(_a , _a , _a ): _a : Union[str, Any] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Any = model(**self._prepare_for_class(_a , _a ) ) _a : Optional[Any] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _a : Optional[int] = self.model_tester.num_stages self.assertEqual(len(_a ) , expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) _a , _a : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : int = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : int = True check_hidden_states_output(_a , _a , _a ) def __lowercase ( self ) -> int: _a , _a : int = self.model_tester.prepare_config_and_inputs_for_common() _a : List[str] = _config_zero_init(_a ) _a : Tuple = _config_zero_init(configs_no_init.backbone_config ) for model_class in self.all_model_classes: _a : Dict = model_class(config=_a ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip(reason='''UperNet does not have tied weights''' ) def __lowercase ( self ) -> List[Any]: pass @slow def __lowercase ( self ) -> Union[str, Any]: for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Tuple = UperNetForSemanticSegmentation.from_pretrained(_a ) self.assertIsNotNone(_a ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" _a : str = hf_hub_download( repo_id='''hf-internal-testing/fixtures_ade20k''' ,repo_type='''dataset''' ,filename='''ADE_val_00000001.jpg''' ) _a : Optional[int] = Image.open(__a ).convert('''RGB''' ) return image @require_torch @require_vision @slow class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : List[str] = AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny''' ) _a : List[str] = UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny''' ).to(_a ) _a : List[str] = prepare_img() _a : Union[str, Any] = processor(images=_a , return_tensors='''pt''' ).to(_a ) with torch.no_grad(): _a : Union[str, Any] = model(**_a ) _a : str = torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2) ) self.assertEqual(outputs.logits.shape , _a ) _a : Tuple = torch.tensor( [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , _a , atol=1e-4 ) ) def __lowercase ( self ) -> str: _a : Tuple = AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny''' ) _a : Union[str, Any] = UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny''' ).to(_a ) _a : Dict = prepare_img() _a : Union[str, Any] = processor(images=_a , return_tensors='''pt''' ).to(_a ) with torch.no_grad(): _a : Dict = model(**_a ) _a : int = torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2) ) self.assertEqual(outputs.logits.shape , _a ) _a : Any = torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , _a , atol=1e-4 ) )
14
from scipy.stats import spearmanr import datasets a__ = ''' The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. ''' a__ = ''' Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {\'spearmanr\': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results[\'spearmanr\']) -0.7 >>> print(round(results[\'spearmanr_pvalue\'], 2)) 0.19 ''' a__ = R'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'''] , ) def __lowercase ( self , _a , _a , _a=False ) -> str: _a : int = spearmanr(_a , _a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
14
1
a__ = { '''meter''': '''m''', '''kilometer''': '''km''', '''megametre''': '''Mm''', '''gigametre''': '''Gm''', '''terametre''': '''Tm''', '''petametre''': '''Pm''', '''exametre''': '''Em''', '''zettametre''': '''Zm''', '''yottametre''': '''Ym''', } # Exponent of the factor(meter) a__ = { '''m''': 0, '''km''': 3, '''Mm''': 6, '''Gm''': 9, '''Tm''': 12, '''Pm''': 15, '''Em''': 18, '''Zm''': 21, '''Ym''': 24, } def __UpperCAmelCase ( __a : float ,__a : str ,__a : str ) -> float: """simple docstring""" _a : Optional[int] = from_type.lower().strip('''s''' ) _a : str = to_type.lower().strip('''s''' ) _a : str = UNIT_SYMBOL.get(__a ,__a ) _a : int = UNIT_SYMBOL.get(__a ,__a ) if from_sanitized not in METRIC_CONVERSION: _a : Union[str, Any] = ( F"""Invalid 'from_type' value: {from_type!r}.\n""" F"""Conversion abbreviations are: {', '.join(__a )}""" ) raise ValueError(__a ) if to_sanitized not in METRIC_CONVERSION: _a : List[str] = ( F"""Invalid 'to_type' value: {to_type!r}.\n""" F"""Conversion abbreviations are: {', '.join(__a )}""" ) raise ValueError(__a ) _a : List[Any] = METRIC_CONVERSION[from_sanitized] _a : Tuple = METRIC_CONVERSION[to_sanitized] _a : Union[str, Any] = 1 if from_exponent > to_exponent: _a : int = from_exponent - to_exponent else: _a : List[str] = -(to_exponent - from_exponent) return value * pow(10 ,__a ) if __name__ == "__main__": from doctest import testmod testmod()
14
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def __UpperCAmelCase ( __a : bytes ,__a : int ) -> np.array: """simple docstring""" _a : int = F"""{sampling_rate}""" _a : str = '''1''' _a : Optional[int] = '''f32le''' _a : Optional[Any] = [ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(__a ,stdin=subprocess.PIPE ,stdout=subprocess.PIPE ) as ffmpeg_process: _a : Any = ffmpeg_process.communicate(__a ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error _a : Optional[Any] = output_stream[0] _a : Optional[int] = np.frombuffer(__a ,np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def __UpperCAmelCase ( __a : int ,__a : float ,__a : str = "f32le" ,) -> str: """simple docstring""" _a : Dict = F"""{sampling_rate}""" _a : Optional[Any] = '''1''' if format_for_conversion == "s16le": _a : Dict = 2 elif format_for_conversion == "f32le": _a : Optional[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) _a : Dict = platform.system() if system == "Linux": _a : Dict = '''alsa''' _a : Union[str, Any] = '''default''' elif system == "Darwin": _a : Union[str, Any] = '''avfoundation''' _a : List[str] = ''':0''' elif system == "Windows": _a : Optional[int] = '''dshow''' _a : str = '''default''' _a : Tuple = [ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] _a : Any = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample _a : str = _ffmpeg_stream(__a ,__a ) for item in iterator: yield item def __UpperCAmelCase ( __a : int ,__a : float ,__a : Optional[int] = None ,__a : Optional[Union[Tuple[float, float], float]] = None ,__a : str = "f32le" ,) -> Optional[int]: """simple docstring""" if stream_chunk_s is not None: _a : Tuple = stream_chunk_s else: _a : Tuple = chunk_length_s _a : Tuple = ffmpeg_microphone(__a ,__a ,format_for_conversion=__a ) if format_for_conversion == "s16le": _a : Any = np.intaa _a : Optional[int] = 2 elif format_for_conversion == "f32le": _a : Dict = np.floataa _a : List[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) if stride_length_s is None: _a : List[Any] = chunk_length_s / 6 _a : Optional[int] = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__a ,(int, float) ): _a : Optional[Any] = [stride_length_s, stride_length_s] _a : Optional[Any] = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample _a : str = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample _a : Optional[Any] = datetime.datetime.now() _a : Tuple = datetime.timedelta(seconds=__a ) for item in chunk_bytes_iter(__a ,__a ,stride=(stride_left, stride_right) ,stream=__a ): # Put everything back in numpy scale _a : Dict = np.frombuffer(item['''raw'''] ,dtype=__a ) _a : Dict = ( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) _a : str = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def __UpperCAmelCase ( __a : Optional[int] ,__a : int ,__a : Tuple[int, int] ,__a : bool = False ) -> Optional[int]: """simple docstring""" _a : Any = b'''''' _a , _a : List[str] = stride if stride_left + stride_right >= chunk_len: raise ValueError( F"""Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}""" ) _a : List[str] = 0 for raw in iterator: acc += raw if stream and len(__a ) < chunk_len: _a : Dict = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__a ) >= chunk_len: # We are flushing the accumulator _a : List[str] = (_stride_left, stride_right) _a : List[Any] = {'''raw''': acc[:chunk_len], '''stride''': stride} if stream: _a : List[Any] = False yield item _a : Optional[Any] = stride_left _a : Optional[Any] = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__a ) > stride_left: _a : Optional[Any] = {'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: _a : Dict = False yield item def __UpperCAmelCase ( __a : int ,__a : int ) -> Tuple: """simple docstring""" _a : Dict = 2**24 # 16Mo try: with subprocess.Popen(__a ,stdout=subprocess.PIPE ,bufsize=__a ) as ffmpeg_process: while True: _a : int = ffmpeg_process.stdout.read(__a ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
14
1
from __future__ import annotations def __UpperCAmelCase ( __a : int | str ) -> bool: """simple docstring""" _a : List[str] = str(__a ) return n == n[::-1] def __UpperCAmelCase ( __a : int = 1_000_000 ) -> Optional[Any]: """simple docstring""" _a : List[Any] = 0 for i in range(1 ,__a ): if is_palindrome(__a ) and is_palindrome(bin(__a ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
14
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = KandinskyInpaintPipeline UpperCAmelCase__ : Optional[int] = ["prompt", "image_embeds", "negative_image_embeds", "image", "mask_image"] UpperCAmelCase__ : Optional[Any] = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", "mask_image", ] UpperCAmelCase__ : Optional[int] = [ "generator", "height", "width", "latents", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase__ : Any = False @property def __lowercase ( self ) -> Optional[int]: return 3_2 @property def __lowercase ( self ) -> int: return 3_2 @property def __lowercase ( self ) -> List[str]: return self.time_input_dim @property def __lowercase ( self ) -> List[str]: return self.time_input_dim * 4 @property def __lowercase ( self ) -> Optional[Any]: return 1_0_0 @property def __lowercase ( self ) -> Optional[Any]: _a : Any = XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[Any] = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) _a : Optional[int] = MultilingualCLIP(_a ) _a : Tuple = text_encoder.eval() return text_encoder @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[str] = { '''in_channels''': 9, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _a : Dict = UNetaDConditionModel(**_a ) return model @property def __lowercase ( self ) -> Optional[int]: return { "block_out_channels": [3_2, 6_4], "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": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowercase ( self ) -> Tuple: torch.manual_seed(0 ) _a : List[Any] = VQModel(**self.dummy_movq_kwargs ) return model def __lowercase ( self ) -> Any: _a : List[Any] = self.dummy_text_encoder _a : Optional[Any] = self.dummy_tokenizer _a : Optional[Any] = self.dummy_unet _a : Union[str, Any] = self.dummy_movq _a : Tuple = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''linear''' , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=_a , set_alpha_to_one=_a , steps_offset=1 , prediction_type='''epsilon''' , thresholding=_a , ) _a : str = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __lowercase ( self , _a , _a=0 ) -> int: _a : Union[str, Any] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _a : List[str] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _a : Tuple = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(_a ) ).to(_a ) _a : Dict = image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : Optional[int] = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) # create mask _a : Union[str, Any] = np.ones((6_4, 6_4) , dtype=np.floataa ) _a : List[str] = 0 if str(_a ).startswith('''mps''' ): _a : Tuple = torch.manual_seed(_a ) else: _a : Any = torch.Generator(device=_a ).manual_seed(_a ) _a : Any = { '''prompt''': '''horse''', '''image''': init_image, '''mask_image''': mask, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 2, '''guidance_scale''': 4.0, '''output_type''': '''np''', } return inputs def __lowercase ( self ) -> Optional[Any]: _a : Optional[Any] = '''cpu''' _a : List[Any] = self.get_dummy_components() _a : Tuple = self.pipeline_class(**_a ) _a : int = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _a : Any = pipe(**self.get_dummy_inputs(_a ) ) _a : str = output.images _a : Tuple = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _a : Union[str, Any] = image[0, -3:, -3:, -1] _a : Tuple = image_from_tuple[0, -3:, -3:, -1] print(F"""image.shape {image.shape}""" ) assert image.shape == (1, 6_4, 6_4, 3) _a : str = np.array( [0.832_6919, 0.7379_0467, 0.2091_8581, 0.930_9612, 0.551_1791, 0.4371_3328, 0.551_3321, 0.4992_2934, 0.5949_7786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def __lowercase ( self ) -> Dict: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowercase ( self ) -> Union[str, Any]: _a : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy''' ) _a : str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _a : Tuple = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) _a : Any = 0 _a : Optional[Any] = '''a hat''' _a : Optional[Any] = KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _a : Tuple = KandinskyInpaintPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-inpaint''' , torch_dtype=torch.floataa ) _a : Union[str, Any] = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _a : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) _a , _a : Dict = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _a : Optional[int] = pipeline( _a , image=_a , mask_image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type='''np''' , ) _a : Optional[int] = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(_a , _a )
14
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available a__ = { '''configuration_data2vec_audio''': ['''DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Data2VecAudioConfig'''], '''configuration_data2vec_text''': [ '''DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Data2VecTextConfig''', '''Data2VecTextOnnxConfig''', ], '''configuration_data2vec_vision''': [ '''DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Data2VecVisionConfig''', '''Data2VecVisionOnnxConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = [ '''DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Data2VecAudioForAudioFrameClassification''', '''Data2VecAudioForCTC''', '''Data2VecAudioForSequenceClassification''', '''Data2VecAudioForXVector''', '''Data2VecAudioModel''', '''Data2VecAudioPreTrainedModel''', ] a__ = [ '''DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Data2VecTextForCausalLM''', '''Data2VecTextForMaskedLM''', '''Data2VecTextForMultipleChoice''', '''Data2VecTextForQuestionAnswering''', '''Data2VecTextForSequenceClassification''', '''Data2VecTextForTokenClassification''', '''Data2VecTextModel''', '''Data2VecTextPreTrainedModel''', ] a__ = [ '''DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Data2VecVisionForImageClassification''', '''Data2VecVisionForMaskedImageModeling''', '''Data2VecVisionForSemanticSegmentation''', '''Data2VecVisionModel''', '''Data2VecVisionPreTrainedModel''', ] if is_tf_available(): a__ = [ '''TFData2VecVisionForImageClassification''', '''TFData2VecVisionForSemanticSegmentation''', '''TFData2VecVisionModel''', '''TFData2VecVisionPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys a__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
14
import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_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.''' ) parser.add_argument( '''--original_config_file''', type=str, required=True, 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( '''--image_size''', default=512, 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( '''--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.)''') def __UpperCAmelCase ( __a : Any ) -> List[Any]: """simple docstring""" if string == "True": return True elif string == "False": return False else: raise ValueError(F"""could not parse string as bool {string}""" ) parser.add_argument( '''--use_linear_projection''', help='''Override for use linear projection''', required=False, type=parse_bool ) parser.add_argument('''--cross_attention_dim''', help='''Override for cross attention_dim''', required=False, type=int) a__ = parser.parse_args() a__ = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
14
1
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = MgpstrTokenizer UpperCAmelCase__ : int = False UpperCAmelCase__ : Union[str, Any] = {} UpperCAmelCase__ : List[Any] = False def __lowercase ( self ) -> Any: super().setUp() # fmt: off _a : Tuple = ['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _a : Optional[int] = dict(zip(_a , range(len(_a ) ) ) ) _a : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) def __lowercase ( self , **_a ) -> Dict: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __lowercase ( self , _a ) -> Tuple: _a : List[str] = '''tester''' _a : Optional[Any] = '''tester''' return input_text, output_text @unittest.skip('''MGP-STR always lower cases letters.''' ) def __lowercase ( self ) -> Any: pass def __lowercase ( self ) -> Any: _a : Union[str, Any] = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a : int = '''[SPECIAL_TOKEN]''' tokenizer.add_special_tokens({'''cls_token''': special_token} ) _a : Tuple = tokenizer.encode([special_token] , add_special_tokens=_a ) self.assertEqual(len(_a ) , 1 ) _a : Tuple = tokenizer.decode(_a , skip_special_tokens=_a ) self.assertTrue(special_token not in decoded ) def __lowercase ( self ) -> Tuple: _a : List[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a , _a : int = self.get_input_output_texts(_a ) _a : List[str] = tokenizer.tokenize(_a ) _a : Optional[int] = tokenizer.convert_tokens_to_ids(_a ) _a : Tuple = tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Optional[int] = tokenizer.convert_ids_to_tokens(_a ) self.assertNotEqual(len(_a ) , 0 ) _a : int = tokenizer.decode(_a ) self.assertIsInstance(_a , _a ) self.assertEqual(text_a.replace(''' ''' , '''''' ) , _a ) @unittest.skip('''MGP-STR tokenizer only handles one sequence.''' ) def __lowercase ( self ) -> List[str]: pass @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer''' ) def __lowercase ( self ) -> Optional[Any]: pass
14
class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a , _a ) -> List[str]: _a : List[Any] = name _a : List[str] = value _a : List[str] = weight def __repr__( self ) -> Optional[int]: return F"""{self.__class__.__name__}({self.name}, {self.value}, {self.weight})""" def __lowercase ( self ) -> List[Any]: return self.value def __lowercase ( self ) -> int: return self.name def __lowercase ( self ) -> Optional[int]: return self.weight def __lowercase ( self ) -> Optional[Any]: return self.value / self.weight def __UpperCAmelCase ( __a : Optional[int] ,__a : Tuple ,__a : List[str] ) -> List[str]: """simple docstring""" _a : Optional[int] = [] for i in range(len(__a ) ): menu.append(Things(name[i] ,value[i] ,weight[i] ) ) return menu def __UpperCAmelCase ( __a : int ,__a : Union[str, Any] ,__a : int ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = sorted(__a ,key=__a ,reverse=__a ) _a : Any = [] _a , _a : Optional[int] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def __UpperCAmelCase ( ) -> int: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
14
1
def __UpperCAmelCase ( __a : Optional[Any] ) -> Any: """simple docstring""" _a : List[str] = [0] * len(__a ) _a : Union[str, Any] = [] _a : Dict = [1] * len(__a ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(__a ) ): if indegree[i] == 0: queue.append(__a ) while queue: _a : Optional[int] = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: _a : int = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(__a ) print(max(__a ) ) # Adjacency list of Graph a__ = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
14
import copy import inspect import unittest from transformers import PretrainedConfig, SwiftFormerConfig from transformers.testing_utils import ( require_torch, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwiftFormerForImageClassification, SwiftFormerModel from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=1_3 , _a=3 , _a=True , _a=True , _a=0.1 , _a=0.1 , _a=2_2_4 , _a=1_0_0_0 , _a=[3, 3, 6, 4] , _a=[4_8, 5_6, 1_1_2, 2_2_0] , ) -> Tuple: _a : Dict = parent _a : Optional[int] = batch_size _a : Optional[Any] = num_channels _a : Union[str, Any] = is_training _a : Tuple = use_labels _a : Dict = hidden_dropout_prob _a : List[Any] = attention_probs_dropout_prob _a : Dict = num_labels _a : List[str] = image_size _a : Dict = layer_depths _a : str = embed_dims def __lowercase ( self ) -> Optional[Any]: _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : int = None if self.use_labels: _a : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) _a : Dict = self.get_config() return config, pixel_values, labels def __lowercase ( self ) -> int: return SwiftFormerConfig( depths=self.layer_depths , embed_dims=self.embed_dims , mlp_ratio=4 , downsamples=[True, True, True, True] , hidden_act='''gelu''' , num_labels=self.num_labels , down_patch_size=3 , down_stride=2 , down_pad=1 , drop_rate=0.0 , drop_path_rate=0.0 , use_layer_scale=_a , layer_scale_init_value=1e-5 , ) def __lowercase ( self , _a , _a , _a ) -> str: _a : List[Any] = SwiftFormerModel(config=_a ) model.to(_a ) model.eval() _a : Optional[int] = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dims[-1], 7, 7) ) def __lowercase ( self , _a , _a , _a ) -> Optional[Any]: _a : List[str] = self.num_labels _a : Optional[int] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : List[str] = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) _a : Union[str, Any] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : Optional[Any] = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowercase ( self ) -> Tuple: ((_a) , (_a) , (_a)) : Optional[int] = self.prepare_config_and_inputs() _a : List[Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else () UpperCAmelCase__ : Optional[int] = ( {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Optional[Any] = False UpperCAmelCase__ : str = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : str = False def __lowercase ( self ) -> Optional[int]: _a : Union[str, Any] = SwiftFormerModelTester(self ) _a : int = ConfigTester( self , config_class=_a , has_text_modality=_a , hidden_size=3_7 , num_attention_heads=1_2 , num_hidden_layers=1_2 , ) def __lowercase ( self ) -> int: self.config_tester.run_common_tests() @unittest.skip(reason='''SwiftFormer does not use inputs_embeds''' ) def __lowercase ( self ) -> Union[str, Any]: pass def __lowercase ( self ) -> Dict: _a , _a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Any = model_class(_a ) _a : int = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def __lowercase ( self ) -> str: _a , _a : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Optional[int] = model_class(_a ) _a : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Tuple = [*signature.parameters.keys()] _a : List[str] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowercase ( self ) -> int: _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self ) -> Optional[int]: _a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self ) -> Optional[Any]: for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Any = SwiftFormerModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason='''SwiftFormer does not output attentions''' ) def __lowercase ( self ) -> List[Any]: pass def __lowercase ( self ) -> int: def check_hidden_states_output(_a , _a , _a ): _a : Optional[int] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Union[str, Any] = model(**self._prepare_for_class(_a , _a ) ) _a : Optional[Any] = outputs.hidden_states _a : Union[str, Any] = 8 self.assertEqual(len(_a ) , _a ) # TODO # SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width) # with the width and height being successively divided by 2, after every 2 blocks for i in range(len(_a ) ): self.assertEqual( hidden_states[i].shape , torch.Size( [ self.model_tester.batch_size, self.model_tester.embed_dims[i // 2], (self.model_tester.image_size // 4) // 2 ** (i // 2), (self.model_tester.image_size // 4) // 2 ** (i // 2), ] ) , ) _a , _a : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : str = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : List[str] = True check_hidden_states_output(_a , _a , _a ) def __lowercase ( self ) -> str: def _config_zero_init(_a ): _a : List[Any] = copy.deepcopy(_a ) for key in configs_no_init.__dict__.keys(): if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key: setattr(_a , _a , 1e-1_0 ) if isinstance(getattr(_a , _a , _a ) , _a ): _a : int = _config_zero_init(getattr(_a , _a ) ) setattr(_a , _a , _a ) return configs_no_init _a , _a : Any = self.model_tester.prepare_config_and_inputs_for_common() _a : Dict = _config_zero_init(_a ) for model_class in self.all_model_classes: _a : Dict = model_class(config=_a ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9) / 1e9).round().item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __lowercase ( self ) -> Optional[Any]: pass def __UpperCAmelCase ( ) -> Optional[Any]: """simple docstring""" _a : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self ) -> str: return ViTImageProcessor.from_pretrained('''MBZUAI/swiftformer-xs''' ) if is_vision_available() else None @slow def __lowercase ( self ) -> Dict: _a : Any = SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs''' ).to(_a ) _a : Any = self.default_image_processor _a : Any = prepare_img() _a : Any = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _a : Optional[Any] = model(**_a ) # verify the logits _a : List[str] = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _a ) _a : int = torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
14
1
a__ = '''Input must be a string of 8 numbers plus letter''' a__ = '''TRWAGMYFPDXBNJZSQVHLCKE''' def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" if not isinstance(__a ,__a ): _a : List[str] = F"""Expected string as input, found {type(__a ).__name__}""" raise TypeError(__a ) _a : List[Any] = spanish_id.replace('''-''' ,'''''' ).upper() if len(__a ) != 9: raise ValueError(__a ) try: _a : Any = int(spanish_id_clean[0:8] ) _a : str = spanish_id_clean[8] except ValueError as ex: raise ValueError(__a ) from ex if letter.isdigit(): raise ValueError(__a ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
14
import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() a__ = logging.get_logger(__name__) def __UpperCAmelCase ( __a : str ) -> List[Any]: """simple docstring""" _a : Tuple = SwinConfig.from_pretrained( '''microsoft/swin-tiny-patch4-window7-224''' ,out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) _a : Dict = MaskFormerConfig(backbone_config=__a ) _a : Optional[Any] = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok _a : Optional[Any] = 847 _a : List[Any] = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok _a : Union[str, Any] = 150 _a : Any = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok _a : int = 171 _a : List[str] = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO _a : Dict = 133 _a : Optional[Any] = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok _a : List[Any] = 19 _a : Optional[Any] = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok _a : List[Any] = 65 _a : Dict = '''mapillary-vistas-id2label.json''' _a : Optional[int] = json.load(open(hf_hub_download(__a ,__a ,repo_type='''dataset''' ) ,'''r''' ) ) _a : Tuple = {int(__a ): v for k, v in idalabel.items()} return config def __UpperCAmelCase ( __a : Optional[Any] ) -> Tuple: """simple docstring""" _a : Optional[Any] = [] # stem # fmt: off rename_keys.append(('''backbone.patch_embed.proj.weight''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.proj.bias''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''model.pixel_level_module.encoder.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''model.pixel_level_module.encoder.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.layers.{i}.downsample.reduction.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(('''sem_seg_head.layer_4.weight''', '''model.pixel_level_module.decoder.fpn.stem.0.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.weight''', '''model.pixel_level_module.decoder.fpn.stem.1.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.bias''', '''model.pixel_level_module.decoder.fpn.stem.1.bias''') ) for source_index, target_index in zip(range(3 ,0 ,-1 ) ,range(0 ,3 ) ): rename_keys.append((F"""sem_seg_head.adapter_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(('''sem_seg_head.mask_features.weight''', '''model.pixel_level_module.decoder.mask_projection.weight''') ) rename_keys.append(('''sem_seg_head.mask_features.bias''', '''model.pixel_level_module.decoder.mask_projection.bias''') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.weight''', '''model.transformer_module.decoder.layernorm.weight''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.bias''', '''model.transformer_module.decoder.layernorm.bias''') ) # heads on top rename_keys.append(('''sem_seg_head.predictor.query_embed.weight''', '''model.transformer_module.queries_embedder.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.weight''', '''model.transformer_module.input_projection.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.bias''', '''model.transformer_module.input_projection.bias''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.weight''', '''class_predictor.weight''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.bias''', '''class_predictor.bias''') ) for i in range(3 ): rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", F"""mask_embedder.{i}.0.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", F"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ,__a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _a : str = dct.pop(__a ) _a : str = val def __UpperCAmelCase ( __a : List[Any] ,__a : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _a : Optional[Any] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) _a : Optional[int] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[int] = in_proj_weight[:dim, :] _a : List[Any] = in_proj_bias[: dim] _a : Optional[int] = in_proj_weight[ dim : dim * 2, : ] _a : Tuple = in_proj_bias[ dim : dim * 2 ] _a : int = in_proj_weight[ -dim :, : ] _a : Optional[int] = in_proj_bias[-dim :] # fmt: on def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ) -> List[Any]: """simple docstring""" _a : Optional[int] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : Union[str, Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Union[str, Any] = in_proj_weight[: hidden_size, :] _a : List[Any] = in_proj_bias[:config.hidden_size] _a : Dict = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Any = in_proj_bias[hidden_size : hidden_size * 2] _a : Tuple = in_proj_weight[-hidden_size :, :] _a : List[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) _a : List[str] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[Any] = in_proj_weight[: hidden_size, :] _a : Any = in_proj_bias[:config.hidden_size] _a : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Optional[Any] = in_proj_bias[hidden_size : hidden_size * 2] _a : List[str] = in_proj_weight[-hidden_size :, :] _a : int = in_proj_bias[-hidden_size :] # fmt: on def __UpperCAmelCase ( ) -> torch.Tensor: """simple docstring""" _a : str = '''http://images.cocodataset.org/val2017/000000039769.jpg''' _a : Dict = Image.open(requests.get(__a ,stream=__a ).raw ) return im @torch.no_grad() def __UpperCAmelCase ( __a : str ,__a : str ,__a : str ,__a : bool = False ) -> Union[str, Any]: """simple docstring""" _a : Optional[Any] = get_maskformer_config(__a ) # load original state_dict with open(__a ,'''rb''' ) as f: _a : str = pickle.load(__a ) _a : Union[str, Any] = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys _a : Any = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a ,__a ,__a ) read_in_swin_q_k_v(__a ,config.backbone_config ) read_in_decoder_q_k_v(__a ,__a ) # update to torch tensors for key, value in state_dict.items(): _a : Optional[int] = torch.from_numpy(__a ) # load 🤗 model _a : Dict = MaskFormerForInstanceSegmentation(__a ) model.eval() for name, param in model.named_parameters(): print(__a ,param.shape ) _a , _a : Tuple = model.load_state_dict(__a ,strict=__a ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(__a ) == 0, F"""Unexpected keys: {unexpected_keys}""" # verify results _a : Union[str, Any] = prepare_img() if "vistas" in model_name: _a : int = 65 elif "cityscapes" in model_name: _a : Tuple = 65_535 else: _a : str = 255 _a : Dict = True if '''ade''' in model_name else False _a : Optional[Any] = MaskFormerImageProcessor(ignore_index=__a ,reduce_labels=__a ) _a : Optional[Any] = image_processor(__a ,return_tensors='''pt''' ) _a : int = model(**__a ) print('''Logits:''' ,outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": _a : Union[str, Any] = torch.tensor( [[3.63_53, -4.47_70, -2.60_65], [0.50_81, -4.23_94, -3.53_43], [2.19_09, -5.03_53, -1.93_23]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] ,__a ,atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(__a ).mkdir(exist_ok=__a ) model.save_pretrained(__a ) image_processor.save_pretrained(__a ) if push_to_hub: print('''Pushing model and image processor to the hub...''' ) model.push_to_hub(F"""nielsr/{model_name}""" ) image_processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''maskformer-swin-tiny-ade''', type=str, help=('''Name of the MaskFormer model you\'d like to convert''',), ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl''', type=str, help='''Path to the original state dict (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) a__ = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
14
1
from __future__ import annotations import math def __UpperCAmelCase ( __a : int ,__a : int ,__a : bool ,__a : list[int] ,__a : float ) -> int: """simple docstring""" if depth < 0: raise ValueError('''Depth cannot be less than 0''' ) if len(__a ) == 0: raise ValueError('''Scores cannot be empty''' ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 ,node_index * 2 ,__a ,__a ,__a ) ,minimax(depth + 1 ,node_index * 2 + 1 ,__a ,__a ,__a ) ,) return min( minimax(depth + 1 ,node_index * 2 ,__a ,__a ,__a ) ,minimax(depth + 1 ,node_index * 2 + 1 ,__a ,__a ,__a ) ,) def __UpperCAmelCase ( ) -> None: """simple docstring""" _a : List[Any] = [90, 23, 6, 33, 21, 65, 123, 34_423] _a : List[Any] = math.log(len(__a ) ,2 ) print('''Optimal value : ''' ,end='''''' ) print(minimax(0 ,0 ,__a ,__a ,__a ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
14
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin a__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = XLMProphetNetTokenizer UpperCAmelCase__ : Optional[int] = False UpperCAmelCase__ : List[Any] = True def __lowercase ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _a : List[Any] = XLMProphetNetTokenizer(_a , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def __lowercase ( self ) -> Any: _a : Tuple = '''[PAD]''' _a : int = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def __lowercase ( self ) -> str: _a : Any = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''[PAD]''' ) self.assertEqual(vocab_keys[1] , '''[CLS]''' ) self.assertEqual(vocab_keys[-1] , '''j''' ) self.assertEqual(len(_a ) , 1_0_1_2 ) def __lowercase ( self ) -> Union[str, Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1_0_1_2 ) def __lowercase ( self ) -> str: _a : Tuple = XLMProphetNetTokenizer(_a , keep_accents=_a ) _a : Union[str, Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) _a : Optional[int] = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _a : List[Any] = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, -9, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, -9, 4] ] , ) _a : List[str] = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''[UNK]''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''[UNK]''', '''.''', ] , ) @cached_property def __lowercase ( self ) -> List[str]: return XLMProphetNetTokenizer.from_pretrained('''microsoft/xprophetnet-large-wiki100-cased''' ) @slow def __lowercase ( self ) -> Tuple: _a : str = '''Hello World!''' _a : Tuple = [3_5_3_8_9, 6_6_7_2, 4_9, 2] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @slow def __lowercase ( self ) -> str: # fmt: off _a : str = {'''input_ids''': [[1_1_0_7_3, 8_2_7_8_3, 1_8, 2_6, 8_2_7_8_3, 5_4_9, 5_1_5_4_0, 2_4_8, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 2_1_5_1_8_6, 1_3_2_5, 1_4_7, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 5_6_3_7_0, 5_3, 1_2_2_0_2_0, 2_0, 1_6_4_7_7, 2_7, 8_7_3_5_5, 4_5_4_8, 2_0, 4_7_2_8, 7_8_3_9_2, 1_7, 1_5_9_9_6_9, 1_8, 2_6, 2_4_4_9_1, 6_2_9, 1_5, 5_3_8, 2_2_7_0_4, 5_4_3_9, 1_5, 2_7_8_8, 2_4_4_9_1, 9_8_8_5, 1_5, 4_3_5_3_4, 6_0_5, 1_5, 8_1_4, 1_8_4_0_3, 3_3_2_0_0, 2_9, 1_5, 4_3_5_3_4, 2_4_4_5_8, 1_2_4_1_0, 1_1_1, 2_4_9_6_6, 8_3_6_6_9, 9_6_3_7, 1_4_4_0_6_8, 2_6, 8_5_0, 2_2_3_4_6, 2_7, 1_4_7, 2_4_9_6_6, 8_3_6_6_9, 8_3_4_9_0, 2_6, 3_9_1_1_3, 7_3_5, 2_7, 6_8_9, 6_5_6, 2_8_0_0, 1_3_3_9, 4_6_0_0, 5_3, 1_2_2_0_2_0, 1_1_5_7_8_5, 3_4, 8_1_6, 1_3_3_9, 4_6_8_8_7, 1_8, 1_4_7, 5_3_9_0_5, 1_9_5_1, 4_2_2_3_8, 4_1_1_7_0, 1_7_7_3_2, 8_3_4, 4_3_6, 1_5, 2_7_5_2_3, 9_8_7_3_3, 2_1_7, 1_4_7, 5_5_4_2, 4_9_8_1, 9_3_0, 1_7_3_4_7, 1_6, 2], [2_0_0_9_1, 6_2_9, 9_4, 8_2_7_8_6, 5_8, 4_9_0, 2_0, 1_5_2_8, 8_4, 5_3_9_0_5, 3_4_4, 8_0_5_9_2, 1_1_0_1_2_8, 1_8_8_2_2, 5_2_6_7, 1_3_0_6, 6_2, 1_5_2_5_3_7, 3_0_8, 7_9_9_7, 4_0_1, 1_2_4_4_2_7, 5_4_9, 3_5_4_4_2, 2_2_5, 1_0_9, 1_5_0_5_5, 2_5_7_4_8, 1_4_7, 7_1_1_9, 4_3_7_1_2, 3_4, 7_6_7, 1_3_5_3_6_6, 1_8, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5_9_2, 6_3_7_8_4, 1_1_9_4_6_6, 1_7, 1_4_7_8_0_8, 8_8_2_1_4, 1_8, 6_5_6, 8_1, 3_2, 3_2_9_6, 1_0_2_8_0, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a , model_name='''microsoft/xprophetnet-large-wiki100-cased''' , revision='''1acad1643ddd54a44df6a1b797ada8373685d90e''' , )
14
1
import argparse import json import os from collections import OrderedDict import numpy as np import tensorflow as tf import torch def __UpperCAmelCase ( __a : int ) -> Tuple: """simple docstring""" _a : Optional[Any] = os.path.join(args.tf_model_dir ,'''parameters.json''' ) _a : Tuple = json.loads(open(__a ).read() ) if not params: raise ValueError( F"""It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file.""" ) if not args.output.endswith('''.pt''' ): _a : str = args.output + '''.pt''' _a : List[str] = OrderedDict() with tf.device('''/CPU:0''' ): _a : Optional[Any] = tf.train.load_checkpoint(args.tf_model_dir ) _a : str = reader.get_variable_to_shape_map() for key_name in shapes.keys(): _a : Any = reader.get_tensor(__a ).astype(np.floataa ) if key_name.endswith('''/adam_m''' ) or key_name.endswith('''/adam_v''' ): continue if key_name.startswith('''pasts/''' ): if key_name.startswith('''pasts/mlp''' ): _a : str = int(key_name[9] ) elif key_name.startswith('''pasts/out''' ): _a : List[Any] = 8 _a : str = '''model.sqout.%d.weight''' % (player * 2) # enter to nn.Sequencial with Tanh, so 2 at a time _a : Optional[Any] = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix _a : Any = torch.tensor(__a ) elif key_name.startswith('''model/moe''' ): _a : Dict = int(key_name[9:].split('''/''' )[0] ) if key_name.endswith('''/switch_gating/kernel''' ): _a : str = '''model.blocks.%d.feed_forward.mlp.router.classifier.weight''' % player _a : Dict = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix _a : Optional[int] = torch.tensor(__a ) elif key_name.endswith('''/softmlp/kernel''' ): _a : int = '''model.blocks.%d.feed_forward.soft_bypass_mlp.weight''' % player _a : List[Any] = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix _a : Dict = torch.tensor(__a ) elif key_name.endswith('''/wo/kernel''' ) or key_name.endswith('''/wi/kernel''' ): _a : Union[str, Any] = key_name[-9:-7] for i in range(16 ): _a : Dict = '''model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight''' % (player, i, nlayer) _a : Any = ( vnp[i].transpose([1, 0] ).copy() ) # In Mesh-Tensorflow, it is one array, so it is divided _a : List[Any] = torch.tensor(__a ) elif key_name.startswith('''model/mlp''' ): _a : Any = int(key_name[9:].split('''/''' )[0] ) if key_name.endswith('''/p1/kernel''' ): _a : str = '''model.blocks.%d.feed_forward.mlp.wi.weight''' % player _a : Dict = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix _a : Any = torch.tensor(__a ) elif key_name.endswith('''/p1/bias''' ): _a : Any = '''model.blocks.%d.feed_forward.mlp.wi.bias''' % player _a : int = vnp.copy() # same because it is one dimensional _a : Any = torch.tensor(__a ) elif key_name.endswith('''/p2/kernel''' ): _a : Tuple = '''model.blocks.%d.feed_forward.mlp.wo.weight''' % player _a : List[str] = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix _a : Union[str, Any] = torch.tensor(__a ) elif key_name.endswith('''/p2/bias''' ): _a : List[Any] = '''model.blocks.%d.feed_forward.mlp.wo.bias''' % player _a : List[str] = vnp.copy() # same because it is one dimensional _a : Dict = torch.tensor(__a ) elif key_name.startswith('''model/ln''' ): _a : Optional[int] = int(key_name[8:].split('''/''' )[0] ) if key_name.endswith('''/b''' ): _a : List[str] = '''model.blocks.%d.feed_forward.norm.bias''' % player _a : Union[str, Any] = vnp.copy() # same because it is one dimensional _a : str = torch.tensor(__a ) elif key_name.endswith('''/g''' ): _a : List[Any] = '''model.blocks.%d.feed_forward.norm.weight''' % player _a : int = vnp.copy() # same because it is one dimensional _a : List[str] = torch.tensor(__a ) elif key_name.startswith('''model/att''' ): _a : List[str] = int(key_name[9:].split('''/''' )[0] ) if key_name.endswith('''/qkv/kernel''' ): _a : List[str] = vnp.copy() # Compute same dimension as Mesh-tensorflow using einsum _a : int = state[:, 0, :, :] _a : int = state[:, 1, :, :] _a : Optional[Any] = state[:, 2, :, :] _a : Tuple = ( state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix _a : Union[str, Any] = ( state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix _a : str = ( state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix _a : str = '''model.blocks.%d.self_attn.self_attn.q_proj.weight''' % player _a : Optional[Any] = torch.tensor(__a ) _a : Tuple = '''model.blocks.%d.self_attn.self_attn.k_proj.weight''' % player _a : int = torch.tensor(__a ) _a : Optional[Any] = '''model.blocks.%d.self_attn.self_attn.v_proj.weight''' % player _a : Any = torch.tensor(__a ) elif key_name.endswith('''/o/kernel''' ): _a : Dict = '''model.blocks.%d.self_attn.self_attn.out_proj.weight''' % player _a : Union[str, Any] = ( vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]] ).transpose([1, 0] ).copy() ) # Mesh-Tensorflow is a diagonal matrix _a : List[Any] = torch.tensor(__a ) elif key_name.startswith('''model/an''' ): _a : Union[str, Any] = int(key_name[8:].split('''/''' )[0] ) if key_name.endswith('''/b''' ): _a : List[Any] = '''model.blocks.%d.self_attn.norm.bias''' % player _a : Any = vnp.copy() # same because it is one dimensional _a : Dict = torch.tensor(__a ) elif key_name.endswith('''/g''' ): _a : List[Any] = '''model.blocks.%d.self_attn.norm.weight''' % player _a : Optional[Any] = vnp.copy() # same because it is one dimensional _a : List[Any] = torch.tensor(__a ) elif ( key_name.startswith('''model/wte''' ) or key_name.startswith('''model/wpe''' ) or key_name.startswith('''model/ete''' ) ): _a : Optional[Any] = {'''wte''': '''embed_tokens''', '''wpe''': '''position_embeddings''', '''ete''': '''extra_position_embeddings'''}[ key_name[-3:] ] _a : Tuple = '''model.%s.weight''' % nlayer _a : Dict = vnp.copy() # same in embedded _a : Optional[int] = torch.tensor(__a ) if key_name.startswith('''model/wte''' ): _a : Optional[int] = '''lm_head.weight''' _a : Union[str, Any] = vnp.copy() # same in embedded _a : Union[str, Any] = torch.tensor(__a ) elif key_name.startswith('''model/wob''' ): _a : Union[str, Any] = '''final_logits_bias''' _a : str = vnp.copy() # same in embedded _a : str = state.reshape((1, -1) ) _a : Any = torch.tensor(__a ) elif key_name == "model/dense/kernel": _a : List[str] = '''model.last_project.weight''' _a : Optional[Any] = vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix _a : Union[str, Any] = torch.tensor(__a ) elif key_name == "model/dense_1/bias": _a : Optional[int] = '''model.last_project.bias''' _a : Dict = vnp.copy() # same because it is one dimensional _a : Tuple = torch.tensor(__a ) torch.save(__a ,args.output ) if __name__ == "__main__": a__ = argparse.ArgumentParser( description='''model converter.''', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('''--tf_model_dir''', metavar='''PATH''', type=str, required=True, help='''import model''') parser.add_argument('''--output''', metavar='''PATH''', type=str, required=True, help='''output model''') a__ = parser.parse_args() convert_tf_gptsan_to_pt(args)
14
import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Any = LxmertTokenizer UpperCAmelCase__ : Optional[Any] = LxmertTokenizerFast UpperCAmelCase__ : Any = True UpperCAmelCase__ : Dict = True def __lowercase ( self ) -> Union[str, Any]: super().setUp() _a : int = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _a : Any = 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 __lowercase ( self , _a ) -> List[str]: _a : Tuple = '''UNwant\u00E9d,running''' _a : str = '''unwanted, running''' return input_text, output_text def __lowercase ( self ) -> List[Any]: _a : str = self.tokenizer_class(self.vocab_file ) _a : str = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [7, 4, 5, 1_0, 8, 9] ) def __lowercase ( self ) -> List[Any]: if not self.test_rust_tokenizer: return _a : Optional[Any] = self.get_tokenizer() _a : str = self.get_rust_tokenizer() _a : Optional[Any] = '''I was born in 92000, and this is falsé.''' _a : Optional[Any] = tokenizer.tokenize(_a ) _a : List[Any] = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _a : List[Any] = tokenizer.encode(_a , add_special_tokens=_a ) _a : Any = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Dict = self.get_rust_tokenizer() _a : Optional[int] = tokenizer.encode(_a ) _a : Dict = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a )
14
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''microsoft/beit-base-patch16-224-pt22k''': ( '''https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json''' ), # See all BEiT models at https://huggingface.co/models?filter=beit } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = "beit" def __init__( self , _a=8_1_9_2 , _a=7_6_8 , _a=1_2 , _a=1_2 , _a=3_0_7_2 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1e-1_2 , _a=2_2_4 , _a=1_6 , _a=3 , _a=False , _a=False , _a=False , _a=False , _a=0.1 , _a=0.1 , _a=True , _a=[3, 5, 7, 1_1] , _a=[1, 2, 3, 6] , _a=True , _a=0.4 , _a=2_5_6 , _a=1 , _a=False , _a=2_5_5 , **_a , ) -> Tuple: super().__init__(**_a ) _a : Dict = vocab_size _a : Tuple = hidden_size _a : List[Any] = num_hidden_layers _a : int = num_attention_heads _a : List[Any] = intermediate_size _a : Any = hidden_act _a : Optional[int] = hidden_dropout_prob _a : Union[str, Any] = attention_probs_dropout_prob _a : Any = initializer_range _a : List[str] = layer_norm_eps _a : Dict = image_size _a : int = patch_size _a : Tuple = num_channels _a : Any = use_mask_token _a : int = use_absolute_position_embeddings _a : Optional[int] = use_relative_position_bias _a : List[Any] = use_shared_relative_position_bias _a : str = layer_scale_init_value _a : List[str] = drop_path_rate _a : List[Any] = use_mean_pooling # decode head attributes (semantic segmentation) _a : str = out_indices _a : Tuple = pool_scales # auxiliary head attributes (semantic segmentation) _a : Any = use_auxiliary_head _a : List[Any] = auxiliary_loss_weight _a : Optional[int] = auxiliary_channels _a : Tuple = auxiliary_num_convs _a : List[Any] = auxiliary_concat_input _a : Tuple = semantic_loss_ignore_index class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = version.parse("1.11" ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowercase ( self ) -> float: return 1e-4
14
import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : Dict = '''ZinengTang/tvlt-base''' _a : List[str] = tempfile.mkdtemp() def __lowercase ( self , **_a ) -> int: return TvltImageProcessor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self , **_a ) -> List[Any]: return TvltFeatureExtractor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self ) -> Optional[int]: shutil.rmtree(self.tmpdirname ) def __lowercase ( self ) -> Dict: _a : Union[str, Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Optional[int] = TvltProcessor(image_processor=_a , feature_extractor=_a ) processor.save_pretrained(self.tmpdirname ) _a : Any = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor , _a ) self.assertIsInstance(processor.image_processor , _a ) def __lowercase ( self ) -> Any: _a : Optional[Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : Union[str, Any] = np.ones([1_2_0_0_0] ) _a : Dict = feature_extractor(_a , return_tensors='''np''' ) _a : Tuple = processor(audio=_a , return_tensors='''np''' ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> int: _a : Optional[Any] = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Optional[Any] = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[Any] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = image_processor(_a , return_tensors='''np''' ) _a : Optional[int] = processor(images=_a , return_tensors='''np''' ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> Union[str, Any]: _a : int = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Any = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[str] = np.ones([1_2_0_0_0] ) _a : Optional[int] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = processor(audio=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __lowercase ( self ) -> Union[str, Any]: _a : str = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' , )
14
1
from __future__ import annotations def __UpperCAmelCase ( __a : str ) -> list[int]: """simple docstring""" return [ord(__a ) - 96 for elem in plain] def __UpperCAmelCase ( __a : list[int] ) -> str: """simple docstring""" return "".join(chr(elem + 96 ) for elem in encoded ) def __UpperCAmelCase ( ) -> None: """simple docstring""" _a : Dict = encode(input('''-> ''' ).strip().lower() ) print('''Encoded: ''' ,__a ) print('''Decoded:''' ,decode(__a ) ) if __name__ == "__main__": main()
14
def __UpperCAmelCase ( __a : str ) -> list: """simple docstring""" if n_term == "": return [] _a : list = [] for temp in range(int(__a ) ): series.append(F"""1/{temp + 1}""" if series else '''1''' ) return series if __name__ == "__main__": a__ = 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))
14
1
from heapq import heappop, heappush import numpy as np def __UpperCAmelCase ( __a : np.ndarray ,__a : tuple[int, int] ,__a : tuple[int, int] ,__a : bool ,) -> tuple[float | int, list[tuple[int, int]]]: """simple docstring""" _a , _a : Optional[Any] = grid.shape _a : Dict = [-1, 1, 0, 0] _a : str = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] _a , _a : Dict = [(0, source)], set() _a : Union[str, Any] = np.full((rows, cols) ,np.inf ) _a : Optional[Any] = 0 _a : Any = np.empty((rows, cols) ,dtype=__a ) _a : Tuple = None while queue: ((_a) , (_a)) : List[Any] = heappop(__a ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: _a : Any = [] while (x, y) != source: path.append((x, y) ) _a , _a : str = predecessors[x, y] path.append(__a ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(__a ) ): _a , _a : Tuple = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: _a : str = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(__a ,(dist + 1, (nx, ny)) ) _a : Dict = dist + 1 _a : List[str] = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
14
import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Optional[int] ) -> Dict: """simple docstring""" return params[F"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :] def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[int] ,__a : int ,__a : List[str]="attention" ) -> List[str]: """simple docstring""" _a : str = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :] ) _a : Tuple = k_tmp.reshape(k_tmp.shape[0] ,k_tmp.shape[1] * k_tmp.shape[2] ) _a : Any = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :] ) _a : Dict = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] ,o_tmp.shape[2] ) _a : Union[str, Any] = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :] ) _a : Any = q_tmp.reshape(q_tmp.shape[0] ,q_tmp.shape[1] * q_tmp.shape[2] ) _a : Tuple = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :] ) _a : int = v_tmp.reshape(v_tmp.shape[0] ,v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Union[str, Any] ,__a : List[Any] ,__a : Any=False ) -> Any: """simple docstring""" if split_mlp_wi: _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :] _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :] _a : List[str] = (wi_a, wi_a) else: _a : List[str] = params[F"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :] _a : Optional[int] = params[F"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :] return wi, wo def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Union[str, Any] ,__a : str ) -> List[str]: """simple docstring""" return params[F"""{prefix}/{prefix}/{layer_name}/scale"""][:, i] def __UpperCAmelCase ( __a : dict ,*, __a : int ,__a : bool ,__a : bool = False ) -> Any: """simple docstring""" _a : Dict = traverse_util.flatten_dict(variables['''target'''] ) _a : Any = {'''/'''.join(__a ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi _a : Optional[int] = '''encoder/encoder/mlp/wi_0/kernel''' in old print('''Split MLP:''' ,__a ) _a : Tuple = collections.OrderedDict() # Shared embeddings. _a : Any = old['''token_embedder/embedding'''] # Encoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Optional[Any] = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_attention_layer_norm''' ) _a , _a , _a , _a : List[str] = tax_attention_lookup(__a ,__a ,'''encoder''' ,'''attention''' ) _a : List[str] = layer_norm _a : Optional[Any] = k.T _a : str = o.T _a : List[Any] = q.T _a : Tuple = v.T # Block i, layer 1 (MLP). _a : str = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Any = tax_mlp_lookup(__a ,__a ,'''encoder''' ,__a ) _a : str = layer_norm if split_mlp_wi: _a : List[Any] = wi[0].T _a : Any = wi[1].T else: _a : Any = wi.T _a : Optional[Any] = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Dict = tax_relpos_bias_lookup( __a ,__a ,'''encoder''' ).T _a : List[str] = old['''encoder/encoder_norm/scale'''] if not scalable_attention: _a : List[Any] = tax_relpos_bias_lookup( __a ,0 ,'''encoder''' ).T _a : Optional[Any] = tax_relpos_bias_lookup( __a ,0 ,'''decoder''' ).T if not is_encoder_only: # Decoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Union[str, Any] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_self_attention_layer_norm''' ) _a , _a , _a , _a : Optional[Any] = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''self_attention''' ) _a : Optional[Any] = layer_norm _a : Dict = k.T _a : str = o.T _a : str = q.T _a : List[str] = v.T # Block i, layer 1 (Cross Attention). _a : Any = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_cross_attention_layer_norm''' ) _a , _a , _a , _a : str = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''encoder_decoder_attention''' ) _a : Optional[Any] = layer_norm _a : Optional[int] = k.T _a : Dict = o.T _a : str = q.T _a : int = v.T # Block i, layer 2 (MLP). _a : Optional[int] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Tuple = tax_mlp_lookup(__a ,__a ,'''decoder''' ,__a ) _a : Optional[Any] = layer_norm if split_mlp_wi: _a : List[str] = wi[0].T _a : List[Any] = wi[1].T else: _a : Dict = wi.T _a : str = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Tuple = tax_relpos_bias_lookup(__a ,__a ,'''decoder''' ).T _a : Tuple = old['''decoder/decoder_norm/scale'''] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: _a : Any = old['''decoder/logits_dense/kernel'''].T return new def __UpperCAmelCase ( __a : Dict ,__a : bool ) -> Tuple: """simple docstring""" _a : Tuple = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: _a : Any = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: _a : Optional[int] = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''' ) _a : str = state_dict['''shared.weight'''] return state_dict def __UpperCAmelCase ( __a : List[str] ,__a : Union[str, Any] ,__a : Dict ,__a : Union[str, Any] ,__a : List[Any] ) -> int: """simple docstring""" _a : List[str] = checkpoints.load_tax_checkpoint(__a ) _a : str = convert_tax_to_pytorch( __a ,num_layers=config.num_layers ,is_encoder_only=__a ,scalable_attention=__a ) _a : str = make_state_dict(__a ,__a ) model.load_state_dict(__a ,strict=__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : Any ,__a : Union[str, Any] ,__a : bool = False ,__a : bool = False ,) -> Optional[Any]: """simple docstring""" _a : List[str] = MTaConfig.from_json_file(__a ) print(F"""Building PyTorch model from configuration: {config}""" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: _a : Any = UMTaEncoderModel(__a ) else: _a : Tuple = UMTaForConditionalGeneration(__a ) # Load weights from tf checkpoint load_tax_weights_in_ta(__a ,__a ,__a ,__a ,__a ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(__a ) # Verify that we can load the checkpoint. model.from_pretrained(__a ) print('''Done''' ) if __name__ == "__main__": a__ = argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''') # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained T5 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.''' ) parser.add_argument( '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False ) parser.add_argument( '''--scalable_attention''', action='''store_true''', help='''Whether the model uses scaled attention (umt5 model)''', default=False, ) a__ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
14
1
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def __UpperCAmelCase ( __a : Any ,__a : str ,__a : List[Any]=1_024 ,__a : Optional[int]=1_024 ,__a : int=False ,**__a : str ) -> Optional[int]: """simple docstring""" _a : Tuple = AutoTokenizer.from_pretrained(__a ) _a : int = SeqaSeqDataset(__a ,__a ,__a ,__a ,type_path='''train''' ,**__a ) _a : List[str] = tok.pad_token_id def get_lens(__a : Any ): _a : List[Any] = tqdm( DataLoader(__a ,batch_size=512 ,num_workers=8 ,shuffle=__a ,collate_fn=ds.collate_fn ) ,desc=str(ds.len_file ) ,) _a : Optional[Any] = [] for batch in dl: _a : List[str] = batch['''input_ids'''].ne(__a ).sum(1 ).tolist() _a : Optional[Any] = batch['''labels'''].ne(__a ).sum(1 ).tolist() if consider_target: for src, tgt in zip(__a ,__a ): max_lens.append(max(__a ,__a ) ) else: max_lens.extend(__a ) return max_lens _a : List[str] = get_lens(__a ) _a : Tuple = SeqaSeqDataset(__a ,__a ,__a ,__a ,type_path='''val''' ,**__a ) _a : Union[str, Any] = get_lens(__a ) pickle_save(__a ,train_ds.len_file ) pickle_save(__a ,val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
14
import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap a__ = '''Usage of script: script_name <size_of_canvas:int>''' a__ = [0] * 100 + [1] * 10 random.shuffle(choice) def __UpperCAmelCase ( __a : int ) -> list[list[bool]]: """simple docstring""" _a : int = [[False for i in range(__a )] for j in range(__a )] return canvas def __UpperCAmelCase ( __a : list[list[bool]] ) -> None: """simple docstring""" for i, row in enumerate(__a ): for j, _ in enumerate(__a ): _a : Optional[int] = bool(random.getrandbits(1 ) ) def __UpperCAmelCase ( __a : list[list[bool]] ) -> list[list[bool]]: """simple docstring""" _a : Any = np.array(__a ) _a : Optional[int] = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(__a ): for c, pt in enumerate(__a ): _a : Tuple = __judge_point( __a ,current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) _a : List[str] = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. _a : list[list[bool]] = current_canvas.tolist() return return_canvas def __UpperCAmelCase ( __a : bool ,__a : list[list[bool]] ) -> bool: """simple docstring""" _a : Optional[Any] = 0 _a : str = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. _a : Optional[int] = pt if pt: if alive < 2: _a : Dict = False elif alive == 2 or alive == 3: _a : Optional[Any] = True elif alive > 3: _a : str = False else: if alive == 3: _a : int = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) a__ = int(sys.argv[1]) # main working structure of this module. a__ = create_canvas(canvas_size) seed(c) a__ , a__ = plt.subplots() fig.show() a__ = ListedColormap(['''w''', '''k''']) try: while True: a__ = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
14
1
a__ = { "joule": 1.0, "kilojoule": 1000, "megajoule": 1000000, "gigajoule": 1000000000, "wattsecond": 1.0, "watthour": 3600, "kilowatthour": 3600000, "newtonmeter": 1.0, "calorie_nutr": 4186.8, "kilocalorie_nutr": 4186800.00, "electronvolt": 1.6_0217_6634E-19, "britishthermalunit_it": 1055.05585, "footpound": 1.355818, } def __UpperCAmelCase ( __a : str ,__a : str ,__a : float ) -> float: """simple docstring""" if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: _a : Optional[Any] = ( F"""Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n""" F"""Valid values are: {', '.join(__a )}""" ) raise ValueError(__a ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
14
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''', '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''', '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''', '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''', '''funnel-transformer/intermediate''': ( '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json''' ), '''funnel-transformer/intermediate-base''': ( '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json''' ), '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''', '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''', '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''', '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''', } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = "funnel" UpperCAmelCase__ : Tuple = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self , _a=3_0_5_2_2 , _a=[4, 4, 4] , _a=None , _a=2 , _a=7_6_8 , _a=1_2 , _a=6_4 , _a=3_0_7_2 , _a="gelu_new" , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=None , _a=1e-9 , _a="mean" , _a="relative_shift" , _a=True , _a=True , _a=True , **_a , ) -> List[Any]: _a : Optional[int] = vocab_size _a : Dict = block_sizes _a : Optional[int] = [1] * len(_a ) if block_repeats is None else block_repeats assert len(_a ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." _a : int = num_decoder_layers _a : List[str] = d_model _a : Optional[Any] = n_head _a : Tuple = d_head _a : Dict = d_inner _a : List[str] = hidden_act _a : int = hidden_dropout _a : Union[str, Any] = attention_dropout _a : Tuple = activation_dropout _a : Optional[Any] = initializer_range _a : Dict = initializer_std _a : Union[str, Any] = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported.""" _a : Any = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported.""" _a : Optional[Any] = attention_type _a : int = separate_cls _a : Tuple = truncate_seq _a : List[Any] = pool_q_only super().__init__(**_a ) @property def __lowercase ( self ) -> Tuple: return sum(self.block_sizes ) @num_hidden_layers.setter def __lowercase ( self , _a ) -> List[str]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.''' ) @property def __lowercase ( self ) -> Optional[int]: return len(self.block_sizes ) @num_blocks.setter def __lowercase ( self , _a ) -> Dict: raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.''' )
14
1
import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : Dict = '''ZinengTang/tvlt-base''' _a : List[str] = tempfile.mkdtemp() def __lowercase ( self , **_a ) -> int: return TvltImageProcessor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self , **_a ) -> List[Any]: return TvltFeatureExtractor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self ) -> Optional[int]: shutil.rmtree(self.tmpdirname ) def __lowercase ( self ) -> Dict: _a : Union[str, Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Optional[int] = TvltProcessor(image_processor=_a , feature_extractor=_a ) processor.save_pretrained(self.tmpdirname ) _a : Any = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor , _a ) self.assertIsInstance(processor.image_processor , _a ) def __lowercase ( self ) -> Any: _a : Optional[Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : Union[str, Any] = np.ones([1_2_0_0_0] ) _a : Dict = feature_extractor(_a , return_tensors='''np''' ) _a : Tuple = processor(audio=_a , return_tensors='''np''' ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> int: _a : Optional[Any] = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Optional[Any] = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[Any] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = image_processor(_a , return_tensors='''np''' ) _a : Optional[int] = processor(images=_a , return_tensors='''np''' ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> Union[str, Any]: _a : int = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Any = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[str] = np.ones([1_2_0_0_0] ) _a : Optional[int] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = processor(audio=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __lowercase ( self ) -> Union[str, Any]: _a : str = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' , )
14
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : int = "mobilenet_v1" def __init__( self , _a=3 , _a=2_2_4 , _a=1.0 , _a=8 , _a="relu6" , _a=True , _a=0.999 , _a=0.02 , _a=0.001 , **_a , ) -> List[Any]: super().__init__(**_a ) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''' ) _a : Tuple = num_channels _a : str = image_size _a : Tuple = depth_multiplier _a : Any = min_depth _a : int = hidden_act _a : Optional[Any] = tf_padding _a : str = classifier_dropout_prob _a : Optional[int] = initializer_range _a : Any = layer_norm_eps class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = version.parse("1.11" ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([('''pixel_values''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})] ) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> float: return 1e-4
14
1
import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def __UpperCAmelCase ( __a : Any ) -> Dict: """simple docstring""" _a : Tuple = [] embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight""", F"""stage{idx}.patch_embed.proj.weight""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias""", F"""stage{idx}.patch_embed.proj.bias""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight""", F"""stage{idx}.patch_embed.norm.weight""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias""", F"""stage{idx}.patch_embed.norm.bias""", ) ) return embed def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = [] attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_q.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_q.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_k.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_k.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_v.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_v.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj.bias""", ) ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight""", F"""stage{idx}.blocks.{cnt}.mlp.fc1.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias""", F"""stage{idx}.blocks.{cnt}.mlp.fc1.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight""", F"""stage{idx}.blocks.{cnt}.mlp.fc2.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias""", F"""stage{idx}.blocks.{cnt}.mlp.fc2.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight""", F"""stage{idx}.blocks.{cnt}.norm1.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias""", F"""stage{idx}.blocks.{cnt}.norm1.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight""", F"""stage{idx}.blocks.{cnt}.norm2.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias""", F"""stage{idx}.blocks.{cnt}.norm2.bias""") ) return attention_weights def __UpperCAmelCase ( __a : Union[str, Any] ) -> str: """simple docstring""" _a : Tuple = [] token.append((F"""cvt.encoder.stages.{idx}.cls_token""", '''stage2.cls_token''') ) return token def __UpperCAmelCase ( ) -> Union[str, Any]: """simple docstring""" _a : Any = [] head.append(('''layernorm.weight''', '''norm.weight''') ) head.append(('''layernorm.bias''', '''norm.bias''') ) head.append(('''classifier.weight''', '''head.weight''') ) head.append(('''classifier.bias''', '''head.bias''') ) return head def __UpperCAmelCase ( __a : List[Any] ,__a : int ,__a : Any ,__a : List[str] ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = '''imagenet-1k-id2label.json''' _a : Union[str, Any] = 1_000 _a : str = '''huggingface/label-files''' _a : Union[str, Any] = num_labels _a : Optional[Any] = json.load(open(cached_download(hf_hub_url(__a ,__a ,repo_type='''dataset''' ) ) ,'''r''' ) ) _a : int = {int(__a ): v for k, v in idalabel.items()} _a : Tuple = idalabel _a : Union[str, Any] = {v: k for k, v in idalabel.items()} _a : Any = CvtConfig(num_labels=__a ,idalabel=__a ,labelaid=__a ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit('''/''' ,1 )[-1][4:6] == "13": _a : Optional[Any] = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit('''/''' ,1 )[-1][4:6] == "21": _a : Any = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: _a : Dict = [2, 2, 20] _a : List[Any] = [3, 12, 16] _a : List[str] = [192, 768, 1_024] _a : List[str] = CvtForImageClassification(__a ) _a : str = AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k''' ) _a : List[Any] = image_size _a : str = torch.load(__a ,map_location=torch.device('''cpu''' ) ) _a : List[Any] = OrderedDict() _a : Tuple = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: _a : Any = list_of_state_dict + cls_token(__a ) _a : List[str] = list_of_state_dict + embeddings(__a ) for cnt in range(config.depth[idx] ): _a : str = list_of_state_dict + attention(__a ,__a ) _a : Union[str, Any] = list_of_state_dict + final() for gg in list_of_state_dict: print(__a ) for i in range(len(__a ) ): _a : Dict = original_weights[list_of_state_dict[i][1]] model.load_state_dict(__a ) model.save_pretrained(__a ) image_processor.save_pretrained(__a ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": a__ = argparse.ArgumentParser() parser.add_argument( '''--cvt_model''', default='''cvt-w24''', type=str, help='''Name of the cvt model you\'d like to convert.''', ) parser.add_argument( '''--image_size''', default=384, type=int, help='''Input Image Size''', ) parser.add_argument( '''--cvt_file_name''', default=R'''cvtmodels\CvT-w24-384x384-IN-22k.pth''', type=str, help='''Input Image Size''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) a__ = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
14
a__ = '''Input must be a string of 8 numbers plus letter''' a__ = '''TRWAGMYFPDXBNJZSQVHLCKE''' def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" if not isinstance(__a ,__a ): _a : List[str] = F"""Expected string as input, found {type(__a ).__name__}""" raise TypeError(__a ) _a : List[Any] = spanish_id.replace('''-''' ,'''''' ).upper() if len(__a ) != 9: raise ValueError(__a ) try: _a : Any = int(spanish_id_clean[0:8] ) _a : str = spanish_id_clean[8] except ValueError as ex: raise ValueError(__a ) from ex if letter.isdigit(): raise ValueError(__a ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
14
1
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy a__ = logging.getLogger(__name__) a__ = '''pytorch_model.bin''' @dataclasses.dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : str = dataclasses.field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models."} ) UpperCAmelCase__ : Optional[str] = dataclasses.field( default=__lowercase , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co."} , ) @dataclasses.dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the training data."} ) UpperCAmelCase__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the data to predict on."} ) UpperCAmelCase__ : Optional[str] = dataclasses.field( default=__lowercase , metadata={"help": "A csv or a json file containing the validation data."} ) UpperCAmelCase__ : Optional[str] = dataclasses.field( default=__lowercase , metadata={"help": "The name of the task to train on."} , ) UpperCAmelCase__ : Optional[List[str]] = dataclasses.field( default=__lowercase , metadata={"help": "The list of labels for the task."} ) @dataclasses.dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : str = dataclasses.field( metadata={"help": "The output directory where the model predictions and checkpoints will be written."} ) UpperCAmelCase__ : Optional[str] = dataclasses.field( default="accuracy" , metadata={"help": "The evaluation metric used for the task."} ) UpperCAmelCase__ : Optional[str] = dataclasses.field( default="no" , metadata={ "help": "The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]" } , ) UpperCAmelCase__ : Optional[int] = dataclasses.field( default=10 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) UpperCAmelCase__ : Optional[float] = dataclasses.field( default=0.0 , metadata={ "help": "How much the specified evaluation metric must improve to satisfy early stopping conditions." } , ) UpperCAmelCase__ : Optional[bool] = dataclasses.field( default=__lowercase , metadata={"help": "Whether to filter the pseudo-labeled data based on the confidence score."} , ) UpperCAmelCase__ : Optional[bool] = dataclasses.field( default=__lowercase , metadata={"help": "Whether to filter the pseudo-labeled data based on the validation performance."} , ) UpperCAmelCase__ : Optional[bool] = dataclasses.field( default=__lowercase , metadata={"help": "Whether to fine-tune on labeled data after pseudo training."} , ) UpperCAmelCase__ : Optional[float] = dataclasses.field( default=0.0 , metadata={"help": "Confidence threshold for pseudo-labeled data filtering."} , ) UpperCAmelCase__ : Optional[int] = dataclasses.field( default=100 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) UpperCAmelCase__ : Optional[int] = dataclasses.field( default=__lowercase , metadata={"help": "Random seed for initialization."} , ) def __UpperCAmelCase ( __a : Tuple ,__a : Optional[int] ,__a : List[str] ,__a : Dict ,__a : str ,__a : Any ) -> List[str]: """simple docstring""" _a : Union[str, Any] = datasets.concatenate_datasets([infer_input, infer_output] ,axis=1 ) if args.do_filter_by_confidence: _a : Any = dataset.filter(lambda __a : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 _a : List[Any] = int(eval_result * len(__a ) ) print(__a ) _a : int = dataset.sort('''probability''' ,reverse=__a ) _a : Union[str, Any] = dataset.select(range(__a ) ) _a : Optional[int] = dataset.remove_columns(['''label''', '''probability'''] ) _a : List[str] = dataset.rename_column('''prediction''' ,'''label''' ) _a : List[str] = dataset.map(lambda __a : {"label": idalabel[example["label"]]} ) _a : int = dataset.shuffle(seed=args.seed ) _a : int = os.path.join(__a ,F"""train_pseudo.{args.data_file_extension}""" ) if args.data_file_extension == "csv": dataset.to_csv(__a ,index=__a ) else: dataset.to_json(__a ) def __UpperCAmelCase ( __a : Optional[Any] ,__a : str ,__a : Union[str, Any] ,__a : Dict ,**__a : List[Any] ) -> Tuple: """simple docstring""" _a : Optional[int] = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' ,datefmt='''%m/%d/%Y %H:%M:%S''' ,level=logging.INFO ,) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() _a : List[Any] = STModelArguments(model_name_or_path=__a ) _a : Union[str, Any] = STDataArguments(train_file=__a ,infer_file=__a ) _a : Optional[int] = STTrainingArguments(output_dir=__a ) _a : Optional[int] = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(__a ).items(): setattr(__a ,__a ,__a ) for key, value in kwargs.items(): if hasattr(__a ,__a ): setattr(__a ,__a ,__a ) # Sanity checks _a : List[str] = {} _a : List[Any] = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None _a : int = args.train_file _a : Optional[int] = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None _a : Dict = args.eval_file for key in data_files: _a : List[str] = data_files[key].split('''.''' )[-1] assert extension in ["csv", "json"], F"""`{key}_file` should be a csv or a json file.""" if args.data_file_extension is None: _a : List[str] = extension else: assert extension == args.data_file_extension, F"""`{key}_file` should be a {args.data_file_extension} file`.""" assert ( args.eval_metric in datasets.list_metrics() ), F"""{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.""" # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info('''Creating the initial data directory for self-training...''' ) _a : Union[str, Any] = F"""{args.output_dir}/self-train_iter-{{}}""".format _a : str = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir ,exist_ok=__a ) os.makedirs(__a ,exist_ok=__a ) accelerator.wait_for_everyone() _a : str = None _a : Union[str, Any] = None _a : Any = 0 _a : Any = False # Show the progress bar _a : Optional[int] = tqdm(range(args.max_selftrain_iterations ) ,disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 ,int(args.max_selftrain_iterations ) ): _a : Dict = data_dir_format(__a ) assert os.path.exists(__a ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 _a : Any = os.path.join(__a ,'''stage-1''' ) _a : Union[str, Any] = { '''accelerator''': accelerator, '''model_name_or_path''': args.model_name_or_path, '''cache_dir''': args.cache_dir, '''do_train''': True, '''train_file''': data_files['''train'''] if iteration == 0 else data_files['''train_pseudo'''], '''do_eval''': True if args.eval_file is not None else False, '''eval_file''': data_files['''eval'''], '''do_predict''': True, '''infer_file''': data_files['''infer'''], '''task_name''': args.task_name, '''label_list''': args.label_list, '''output_dir''': current_output_dir, '''eval_metric''': args.eval_metric, '''evaluation_strategy''': args.evaluation_strategy, '''early_stopping_patience''': args.early_stopping_patience, '''early_stopping_threshold''': args.early_stopping_threshold, '''seed''': args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(__a ,__a ): arguments_dict.update({key: value} ) _a : List[Any] = os.path.join(__a ,'''best-checkpoint''' ,__a ) if os.path.exists(__a ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.''' ,__a ,__a ,) else: logger.info('''***** Running self-training: iteration: %d, stage: 1 *****''' ,__a ) finetune(**__a ) accelerator.wait_for_everyone() assert os.path.exists(__a ) logger.info('''Self-training job completed: iteration: %d, stage: 1.''' ,__a ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data _a : Dict = os.path.join(__a ,'''best-checkpoint''' ) _a : Any = os.path.join(__a ,'''stage-2''' ) # Update arguments_dict _a : List[str] = model_path _a : Optional[int] = data_files['''train'''] _a : Any = current_output_dir _a : Optional[Any] = os.path.join(__a ,'''best-checkpoint''' ,__a ) if os.path.exists(__a ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.''' ,__a ,__a ,) else: logger.info('''***** Running self-training: iteration: %d, stage: 2 *****''' ,__a ) finetune(**__a ) accelerator.wait_for_everyone() assert os.path.exists(__a ) logger.info('''Self-training job completed: iteration: %d, stage: 2.''' ,__a ) _a : List[str] = iteration _a : Tuple = data_dir_format(iteration + 1 ) _a : List[Any] = AutoConfig.from_pretrained(os.path.join(__a ,'''best-checkpoint''' ) ) _a : Optional[int] = config.idalabel _a : Optional[Any] = os.path.join(__a ,'''eval_results_best-checkpoint.json''' ) _a : Union[str, Any] = os.path.join(__a ,'''test_results_best-checkpoint.json''' ) assert os.path.exists(__a ) with open(__a ,'''r''' ) as f: _a : Dict = float(json.load(__a )[args.eval_metric] ) _a : List[Any] = os.path.join(__a ,'''infer_output_best-checkpoint.csv''' ) assert os.path.exists(__a ) # Loading the dataset from local csv or json files. _a : Optional[Any] = load_dataset(args.data_file_extension ,data_files={'''data''': data_files['''infer''']} )['''data'''] _a : Optional[Any] = load_dataset('''csv''' ,data_files={'''data''': infer_output_file} )['''data'''] if accelerator.is_main_process: os.makedirs(__a ,exist_ok=__a ) shutil.copy(__a ,os.path.join(__a ,F"""eval_results_iter-{iteration}.json""" ) ) if os.path.exists(__a ): shutil.copy(__a ,os.path.join(__a ,F"""test_results_iter-{iteration}.json""" ) ) create_pseudo_labeled_data(__a ,__a ,__a ,__a ,__a ,__a ) accelerator.wait_for_everyone() _a : Optional[Any] = os.path.join(__a ,F"""train_pseudo.{args.data_file_extension}""" ) if args.evaluation_strategy != IntervalStrategy.NO.value: _a : Optional[int] = eval_result if best_iteration is None: _a : Optional[int] = new_iteration _a : Optional[int] = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: _a : int = new_iteration _a : Any = new_eval_result _a : Optional[Any] = 0 else: if new_eval_result == best_eval_result: _a : int = new_iteration _a : Dict = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: _a : Optional[Any] = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info('''Best iteration: %d''' ,__a ) logger.info('''Best evaluation result: %s = %f''' ,args.eval_metric ,__a ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(__a ,F"""eval_results_iter-{iteration}.json""" ) ,os.path.join(__a ,'''eval_results_best-iteration.json''' ) ,) else: # Assume that the last iteration is the best logger.info('''Best iteration: %d''' ,args.max_selftrain_iterations - 1 ) logger.info('''Best evaluation result: %s = %f''' ,args.eval_metric ,__a ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(__a ,F"""eval_results_iter-{args.max_selftrain_iterations - 1}.json""" ) ,os.path.join(__a ,'''eval_results_best-iteration.json''' ) ,)
14
from random import randint from tempfile import TemporaryFile import numpy as np def __UpperCAmelCase ( __a : Optional[Any] ,__a : int ,__a : Any ) -> int: """simple docstring""" _a : int = 0 if start < end: _a : Tuple = randint(__a ,__a ) _a : Tuple = a[end] _a : List[str] = a[pivot] _a : Any = temp _a , _a : Optional[int] = _in_place_partition(__a ,__a ,__a ) count += _in_place_quick_sort(__a ,__a ,p - 1 ) count += _in_place_quick_sort(__a ,p + 1 ,__a ) return count def __UpperCAmelCase ( __a : List[Any] ,__a : Tuple ,__a : Dict ) -> Dict: """simple docstring""" _a : Dict = 0 _a : Tuple = randint(__a ,__a ) _a : List[Any] = a[end] _a : str = a[pivot] _a : str = temp _a : Dict = start - 1 for index in range(__a ,__a ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value _a : int = new_pivot_index + 1 _a : Any = a[new_pivot_index] _a : Optional[int] = a[index] _a : str = temp _a : Union[str, Any] = a[new_pivot_index + 1] _a : Tuple = a[end] _a : Any = temp return new_pivot_index + 1, count a__ = TemporaryFile() a__ = 100 # 1000 elements are to be sorted a__ , a__ = 0, 1 # mean and standard deviation a__ = np.random.normal(mu, sigma, p) np.save(outfile, X) print('''The array is''') print(X) outfile.seek(0) # using the same array a__ = np.load(outfile) a__ = len(M) - 1 a__ = _in_place_quick_sort(M, 0, r) print( '''No of Comparisons for 100 elements selected from a standard normal distribution''' '''is :''' ) print(z)
14
1
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def __UpperCAmelCase ( __a : Tuple ,__a : Dict ,__a : List[str] ,__a : Optional[Any] ,__a : Tuple ) -> Dict: """simple docstring""" with open(__a ) as metadata_file: _a : Optional[Any] = json.load(__a ) _a : List[Any] = LukeConfig(use_entity_aware_attention=__a ,**metadata['''model_config'''] ) # Load in the weights from the checkpoint_path _a : Optional[Any] = torch.load(__a ,map_location='''cpu''' )['''module'''] # Load the entity vocab file _a : Any = load_original_entity_vocab(__a ) # add an entry for [MASK2] _a : Union[str, Any] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 _a : Dict = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks _a : Optional[int] = AddedToken('''<ent>''' ,lstrip=__a ,rstrip=__a ) _a : Tuple = AddedToken('''<ent2>''' ,lstrip=__a ,rstrip=__a ) 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(__a ) with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''r''' ) as f: _a : List[str] = json.load(__a ) _a : Tuple = '''MLukeTokenizer''' with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''w''' ) as f: json.dump(__a ,__a ) with open(os.path.join(__a ,MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) ,'''w''' ) as f: json.dump(__a ,__a ) _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) # Initialize the embeddings of the special tokens _a : str = tokenizer.convert_tokens_to_ids(['''@'''] )[0] _a : Tuple = tokenizer.convert_tokens_to_ids(['''#'''] )[0] _a : Any = state_dict['''embeddings.word_embeddings.weight'''] _a : Optional[int] = word_emb[ent_init_index].unsqueeze(0 ) _a : Any = word_emb[enta_init_index].unsqueeze(0 ) _a : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: _a : Tuple = state_dict[bias_name] _a : Optional[Any] = decoder_bias[ent_init_index].unsqueeze(0 ) _a : Optional[int] = decoder_bias[enta_init_index].unsqueeze(0 ) _a : Dict = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: _a : Tuple = F"""encoder.layer.{layer_index}.attention.self.""" _a : List[Any] = state_dict[prefix + matrix_name] _a : Dict = state_dict[prefix + matrix_name] _a : List[Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks _a : Union[str, Any] = state_dict['''entity_embeddings.entity_embeddings.weight'''] _a : Optional[int] = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' _a : int = state_dict['''entity_predictions.bias'''] _a : int = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Optional[Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) _a : Optional[int] = LukeForMaskedLM(config=__a ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) _a : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): _a : Optional[int] = state_dict[key] else: _a : Tuple = state_dict[key] _a , _a : int = model.load_state_dict(__a ,strict=__a ) if set(__a ) != {"luke.embeddings.position_ids"}: raise ValueError(F"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(__a ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ,task='''entity_classification''' ) _a : int = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' _a : List[Any] = (0, 9) _a : Tuple = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : int = model(**__a ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base _a : List[str] = torch.Size((1, 33, 768) ) _a : Union[str, Any] = torch.tensor([[0.08_92, 0.05_96, -0.28_19], [0.01_34, 0.11_99, 0.05_73], [-0.01_69, 0.09_27, 0.06_44]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base _a : str = torch.Size((1, 1, 768) ) _a : List[Any] = torch.tensor([[-0.14_82, 0.06_09, 0.03_22]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" F""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify masked word/entity prediction _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) _a : Dict = '''Tokyo is the capital of <mask>.''' _a : List[str] = (24, 30) _a : Optional[int] = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : Optional[Any] = model(**__a ) _a : Any = encoding['''input_ids'''][0].tolist() _a : Optional[Any] = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) _a : Any = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(__a ) _a : Any = outputs.entity_logits[0][0].argmax().item() _a : Optional[Any] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(__a ) ) model.save_pretrained(__a ) def __UpperCAmelCase ( __a : List[Any] ) -> int: """simple docstring""" _a : Union[str, Any] = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] _a : int = [json.loads(__a ) for line in open(__a )] _a : List[Any] = {} for entry in data: _a : int = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: _a : List[Any] = entity_id break _a : Dict = F"""{language}:{entity_name}""" _a : int = entity_id return new_mapping if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) a__ = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
14
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = MgpstrTokenizer UpperCAmelCase__ : int = False UpperCAmelCase__ : Union[str, Any] = {} UpperCAmelCase__ : List[Any] = False def __lowercase ( self ) -> Any: super().setUp() # fmt: off _a : Tuple = ['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _a : Optional[int] = dict(zip(_a , range(len(_a ) ) ) ) _a : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) def __lowercase ( self , **_a ) -> Dict: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __lowercase ( self , _a ) -> Tuple: _a : List[str] = '''tester''' _a : Optional[Any] = '''tester''' return input_text, output_text @unittest.skip('''MGP-STR always lower cases letters.''' ) def __lowercase ( self ) -> Any: pass def __lowercase ( self ) -> Any: _a : Union[str, Any] = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a : int = '''[SPECIAL_TOKEN]''' tokenizer.add_special_tokens({'''cls_token''': special_token} ) _a : Tuple = tokenizer.encode([special_token] , add_special_tokens=_a ) self.assertEqual(len(_a ) , 1 ) _a : Tuple = tokenizer.decode(_a , skip_special_tokens=_a ) self.assertTrue(special_token not in decoded ) def __lowercase ( self ) -> Tuple: _a : List[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a , _a : int = self.get_input_output_texts(_a ) _a : List[str] = tokenizer.tokenize(_a ) _a : Optional[int] = tokenizer.convert_tokens_to_ids(_a ) _a : Tuple = tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Optional[int] = tokenizer.convert_ids_to_tokens(_a ) self.assertNotEqual(len(_a ) , 0 ) _a : int = tokenizer.decode(_a ) self.assertIsInstance(_a , _a ) self.assertEqual(text_a.replace(''' ''' , '''''' ) , _a ) @unittest.skip('''MGP-STR tokenizer only handles one sequence.''' ) def __lowercase ( self ) -> List[str]: pass @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer''' ) def __lowercase ( self ) -> Optional[Any]: pass
14
1
import math def __UpperCAmelCase ( __a : list ,__a : int = 0 ,__a : int = 0 ) -> list: """simple docstring""" _a : int = end or len(__a ) for i in range(__a ,__a ): _a : Optional[Any] = i _a : str = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: _a : Union[str, Any] = array[temp_index - 1] temp_index -= 1 _a : Optional[Any] = temp_index_value return array def __UpperCAmelCase ( __a : list ,__a : int ,__a : int ) -> None: # Max Heap """simple docstring""" _a : Any = index _a : Any = 2 * index + 1 # Left Node _a : Optional[int] = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: _a : Union[str, Any] = left_index if right_index < heap_size and array[largest] < array[right_index]: _a : str = right_index if largest != index: _a , _a : Tuple = array[largest], array[index] heapify(__a ,__a ,__a ) def __UpperCAmelCase ( __a : list ) -> list: """simple docstring""" _a : Dict = len(__a ) for i in range(n // 2 ,-1 ,-1 ): heapify(__a ,__a ,__a ) for i in range(n - 1 ,0 ,-1 ): _a , _a : List[str] = array[0], array[i] heapify(__a ,0 ,__a ) return array def __UpperCAmelCase ( __a : list ,__a : int ,__a : int ,__a : int ) -> int: """simple docstring""" if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def __UpperCAmelCase ( __a : list ,__a : int ,__a : int ,__a : int ) -> int: """simple docstring""" _a : List[str] = low _a : List[Any] = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i _a , _a : int = array[j], array[i] i += 1 def __UpperCAmelCase ( __a : list ) -> list: """simple docstring""" if len(__a ) == 0: return array _a : int = 2 * math.ceil(math.loga(len(__a ) ) ) _a : Tuple = 16 return intro_sort(__a ,0 ,len(__a ) ,__a ,__a ) def __UpperCAmelCase ( __a : list ,__a : int ,__a : int ,__a : int ,__a : int ) -> list: """simple docstring""" while end - start > size_threshold: if max_depth == 0: return heap_sort(__a ) max_depth -= 1 _a : Dict = median_of_a(__a ,__a ,start + ((end - start) // 2) + 1 ,end - 1 ) _a : int = partition(__a ,__a ,__a ,__a ) intro_sort(__a ,__a ,__a ,__a ,__a ) _a : int = p return insertion_sort(__a ,__a ,__a ) if __name__ == "__main__": import doctest doctest.testmod() a__ = input('''Enter numbers separated by a comma : ''').strip() a__ = [float(item) for item in user_input.split(''',''')] print(sort(unsorted))
14
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> List[Any]: _a : int = 0 def __lowercase ( self ) -> List[str]: _a : Dict = AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32''' ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : Optional[Any] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: # Ensure we can load the image processor from the feature extractor config with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' _a : Any = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Dict = CLIPConfig() # Create a dummy config file with image_proceesor_type _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : List[str] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally _a : Tuple = AutoImageProcessor.from_pretrained(_a ).to_dict() config_dict.pop('''image_processor_type''' ) _a : Tuple = CLIPImageProcessor(**_a ) # save in new folder model_config.save_pretrained(_a ) config.save_pretrained(_a ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) # make sure private variable is not incorrectly saved _a : Optional[int] = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Any: with self.assertRaisesRegex( _a , '''clip-base is not a local folder and is not a valid model identifier''' ): _a : Dict = AutoImageProcessor.from_pretrained('''clip-base''' ) def __lowercase ( self ) -> List[Any]: with self.assertRaisesRegex( _a , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): _a : List[str] = AutoImageProcessor.from_pretrained(_a , revision='''aaaaaa''' ) def __lowercase ( self ) -> Dict: with self.assertRaisesRegex( _a , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): _a : Optional[int] = AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowercase ( self ) -> Union[str, Any]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_a ): _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_a ): _a : Optional[Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) _a : Union[str, Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a , trust_remote_code=_a ) self.assertEqual(reloaded_image_processor.__class__.__name__ , '''NewImageProcessor''' ) def __lowercase ( self ) -> Dict: try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_a ): AutoImageProcessor.register(_a , _a ) with tempfile.TemporaryDirectory() as tmpdirname: _a : int = Path(_a ) / '''preprocessor_config.json''' _a : int = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : int = CustomImageProcessor.from_pretrained(_a ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def __lowercase ( self ) -> Union[str, Any]: class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = True try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # If remote code is not set, the default is to use local _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. _a : int = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub _a : Dict = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(not hasattr(_a , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
14
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def __UpperCAmelCase ( ) -> str: """simple docstring""" _a : Optional[Any] = ArgumentParser('''Accelerate CLI tool''' ,usage='''accelerate <command> [<args>]''' ,allow_abbrev=__a ) _a : Dict = parser.add_subparsers(help='''accelerate command helpers''' ) # Register commands get_config_parser(subparsers=__a ) env_command_parser(subparsers=__a ) launch_command_parser(subparsers=__a ) tpu_command_parser(subparsers=__a ) test_command_parser(subparsers=__a ) # Let's go _a : str = parser.parse_args() if not hasattr(__a ,'''func''' ): parser.print_help() exit(1 ) # Run args.func(__a ) if __name__ == "__main__": main()
14
from __future__ import annotations from dataclasses import dataclass @dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : float UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase__ : TreeNode | None = None def __UpperCAmelCase ( __a : TreeNode | None ) -> bool: """simple docstring""" def is_valid_tree(__a : TreeNode | None ) -> bool: if node is None: return True if not isinstance(__a ,__a ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(__a ): raise ValueError( '''Each node should be type of TreeNode and data should be float.''' ) def is_binary_search_tree_recursive_check( __a : TreeNode | None ,__a : float ,__a : float ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left ,__a ,node.data ) and is_binary_search_tree_recursive_check( node.right ,node.data ,__a ) ) return is_binary_search_tree_recursive_check(__a ,-float('''inf''' ) ,float('''inf''' ) ) if __name__ == "__main__": import doctest doctest.testmod()
14
1
import heapq import sys import numpy as np a__ = tuple[int, int] class UpperCAmelCase_ : """simple docstring""" def __init__( self ) -> Dict: _a : str = [] _a : int = set() def __lowercase ( self ) -> Union[str, Any]: if not self.empty(): return self.elements[0][0] else: return float('''inf''' ) def __lowercase ( self ) -> Tuple: return len(self.elements ) == 0 def __lowercase ( self , _a , _a ) -> Optional[int]: if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(_a ) else: # update # print("update", item) _a : Optional[Any] = [] ((_a) , (_a)) : Optional[int] = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((_a) , (_a)) : Union[str, Any] = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def __lowercase ( self , _a ) -> List[Any]: if item in self.set: self.set.remove(_a ) _a : Optional[Any] = [] ((_a) , (_a)) : Union[str, Any] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((_a) , (_a)) : Optional[int] = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def __lowercase ( self ) -> Tuple: return self.elements[0][1] def __lowercase ( self ) -> int: ((_a) , (_a)) : List[Any] = heapq.heappop(self.elements ) self.set.remove(_a ) return (priority, item) def __UpperCAmelCase ( __a : TPos ,__a : TPos ) -> Optional[Any]: """simple docstring""" _a : int = np.array(__a ) _a : int = np.array(__a ) return np.linalg.norm(a - b ) def __UpperCAmelCase ( __a : TPos ,__a : TPos ) -> List[Any]: """simple docstring""" return consistent_heuristic(__a ,__a ) // t def __UpperCAmelCase ( __a : TPos ,__a : TPos ) -> str: """simple docstring""" return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def __UpperCAmelCase ( __a : TPos ,__a : int ,__a : TPos ,__a : dict[TPos, float] ) -> Optional[int]: """simple docstring""" _a : Optional[int] = g_function[start] + Wa * heuristics[i](__a ,__a ) return ans def __UpperCAmelCase ( __a : Any ,__a : Any ,__a : Dict ) -> Optional[Any]: """simple docstring""" _a : Optional[Any] = np.chararray((n, n) ) for i in range(__a ): for j in range(__a ): _a : Any = '''*''' for i in range(__a ): for j in range(__a ): if (j, (n - 1) - i) in blocks: _a : Dict = '''#''' _a : Optional[int] = '''-''' _a : List[str] = back_pointer[goal] while x != start: ((_a) , (_a)) : int = x # print(x) _a : Dict = '''-''' _a : Tuple = back_pointer[x] _a : Optional[int] = '''-''' for i in range(__a ): for j in range(__a ): if (i, j) == (0, n - 1): print(grid[i][j] ,end=''' ''' ) print('''<-- End position''' ,end=''' ''' ) else: print(grid[i][j] ,end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) print('''PATH TAKEN BY THE ALGORITHM IS:-''' ) _a : str = back_pointer[goal] while x != start: print(__a ,end=''' ''' ) _a : str = back_pointer[x] print(__a ) sys.exit() def __UpperCAmelCase ( __a : TPos ) -> List[str]: """simple docstring""" if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def __UpperCAmelCase ( __a : Union[str, Any] ,__a : List[str] ,__a : List[str] ,__a : Any ,__a : Dict ,__a : Optional[Any] ,__a : List[Any] ,__a : Any ,) -> Optional[Any]: """simple docstring""" for itera in range(__a ): open_list[itera].remove_element(__a ) # print("s", s) # print("j", j) ((_a) , (_a)) : Optional[Any] = s _a : Any = (x - 1, y) _a : Dict = (x + 1, y) _a : str = (x, y + 1) _a : str = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(__a ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(__a ) _a : Optional[int] = -1 _a : Union[str, Any] = float('''inf''' ) if valid(__a ) and g_function[neighbours] > g_function[s] + 1: _a : Dict = g_function[s] + 1 _a : Optional[Any] = s if neighbours not in close_list_anchor: open_list[0].put(__a ,key(__a ,0 ,__a ,__a ) ) if neighbours not in close_list_inad: for var in range(1 ,__a ): if key(__a ,__a ,__a ,__a ) <= Wa * key( __a ,0 ,__a ,__a ): open_list[j].put( __a ,key(__a ,__a ,__a ,__a ) ) def __UpperCAmelCase ( ) -> List[str]: """simple docstring""" _a : Optional[Any] = [] for x in range(1 ,5 ): for y in range(1 ,6 ): some_list.append((x, y) ) for x in range(15 ,20 ): some_list.append((x, 17) ) for x in range(10 ,19 ): for y in range(1 ,15 ): some_list.append((x, y) ) # L block for x in range(1 ,4 ): for y in range(12 ,19 ): some_list.append((x, y) ) for x in range(3 ,13 ): for y in range(16 ,19 ): some_list.append((x, y) ) return some_list a__ = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} a__ = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] a__ = make_common_ground() a__ = blocks_blk # hyper parameters a__ = 1 a__ = 1 a__ = 20 a__ = 3 # one consistent and two other inconsistent # start and end destination a__ = (0, 0) a__ = (n - 1, n - 1) a__ = 1 def __UpperCAmelCase ( __a : TPos ,__a : TPos ,__a : int ) -> List[Any]: """simple docstring""" _a : Optional[int] = {start: 0, goal: float('''inf''' )} _a : int = {start: -1, goal: -1} _a : Union[str, Any] = [] _a : Optional[int] = set() for i in range(__a ): open_list.append(PriorityQueue() ) open_list[i].put(__a ,key(__a ,__a ,__a ,__a ) ) _a : list[int] = [] _a : list[int] = [] while open_list[0].minkey() < float('''inf''' ): for i in range(1 ,__a ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float('''inf''' ): do_something(__a ,__a ,__a ) else: _a , _a : Any = open_list[i].top_show() visited.add(__a ) expand_state( __a ,__a ,__a ,__a ,__a ,__a ,__a ,__a ,) close_list_inad.append(__a ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float('''inf''' ): do_something(__a ,__a ,__a ) else: _a : Tuple = open_list[0].top_show() visited.add(__a ) expand_state( __a ,0 ,__a ,__a ,__a ,__a ,__a ,__a ,) close_list_anchor.append(__a ) print('''No path found to goal''' ) print() for i in range(n - 1 ,-1 ,-1 ): for j in range(__a ): if (j, i) in blocks: print('''#''' ,end=''' ''' ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print('''*''' ,end=''' ''' ) else: print('''-''' ,end=''' ''' ) else: print('''*''' ,end=''' ''' ) if (j, i) == (n - 1, n - 1): print('''<-- End position''' ,end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
14
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake a__ = numpy.array([0, 0]) a__ = numpy.array([0.5, 0.8660254]) a__ = numpy.array([1, 0]) a__ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def __UpperCAmelCase ( __a : list[numpy.ndarray] ,__a : int ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = initial_vectors for _ in range(__a ): _a : int = iteration_step(__a ) return vectors def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = [] for i, start_vector in enumerate(vectors[:-1] ): _a : str = vectors[i + 1] new_vectors.append(__a ) _a : Optional[int] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 ,60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def __UpperCAmelCase ( __a : numpy.ndarray ,__a : float ) -> numpy.ndarray: """simple docstring""" _a : Tuple = numpy.radians(__a ) _a , _a : List[Any] = numpy.cos(__a ), numpy.sin(__a ) _a : Dict = numpy.array(((c, -s), (s, c)) ) return numpy.dot(__a ,__a ) def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> None: """simple docstring""" _a : str = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() _a , _a : Optional[int] = zip(*__a ) plt.plot(__a ,__a ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() a__ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
14
1
a__ = '''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/''' def __UpperCAmelCase ( __a : bytes ) -> bytes: """simple docstring""" if not isinstance(__a ,__a ): _a : int = F"""a bytes-like object is required, not '{data.__class__.__name__}'""" raise TypeError(__a ) _a : Optional[int] = ''''''.join(bin(__a )[2:].zfill(8 ) for byte in data ) _a : str = len(__a ) % 6 != 0 if padding_needed: # The padding that will be added later _a : str = b'''=''' * ((6 - len(__a ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(__a ) % 6) else: _a : Tuple = b'''''' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] ,2 )] for index in range(0 ,len(__a ) ,6 ) ).encode() + padding ) def __UpperCAmelCase ( __a : str ) -> bytes: """simple docstring""" if not isinstance(__a ,__a ) and not isinstance(__a ,__a ): _a : Tuple = ( '''argument should be a bytes-like object or ASCII string, ''' F"""not '{encoded_data.__class__.__name__}'""" ) raise TypeError(__a ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(__a ,__a ): try: _a : Any = encoded_data.decode('''utf-8''' ) except UnicodeDecodeError: raise ValueError('''base64 encoded data should only contain ASCII characters''' ) _a : Tuple = encoded_data.count('''=''' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(__a ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one _a : Any = encoded_data[:-padding] _a : int = ''''''.join( bin(B64_CHARSET.index(__a ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: _a : Optional[int] = ''''''.join( bin(B64_CHARSET.index(__a ) )[2:].zfill(6 ) for char in encoded_data ) _a : str = [ int(binary_stream[index : index + 8] ,2 ) for index in range(0 ,len(__a ) ,8 ) ] return bytes(__a ) if __name__ == "__main__": import doctest doctest.testmod()
14
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def __UpperCAmelCase ( __a : Tuple ,__a : Dict ,__a : List[str] ,__a : Optional[Any] ,__a : Tuple ) -> Dict: """simple docstring""" with open(__a ) as metadata_file: _a : Optional[Any] = json.load(__a ) _a : List[Any] = LukeConfig(use_entity_aware_attention=__a ,**metadata['''model_config'''] ) # Load in the weights from the checkpoint_path _a : Optional[Any] = torch.load(__a ,map_location='''cpu''' )['''module'''] # Load the entity vocab file _a : Any = load_original_entity_vocab(__a ) # add an entry for [MASK2] _a : Union[str, Any] = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 _a : Dict = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks _a : Optional[int] = AddedToken('''<ent>''' ,lstrip=__a ,rstrip=__a ) _a : Tuple = AddedToken('''<ent2>''' ,lstrip=__a ,rstrip=__a ) 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(__a ) with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''r''' ) as f: _a : List[str] = json.load(__a ) _a : Tuple = '''MLukeTokenizer''' with open(os.path.join(__a ,'''tokenizer_config.json''' ) ,'''w''' ) as f: json.dump(__a ,__a ) with open(os.path.join(__a ,MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) ,'''w''' ) as f: json.dump(__a ,__a ) _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) # Initialize the embeddings of the special tokens _a : str = tokenizer.convert_tokens_to_ids(['''@'''] )[0] _a : Tuple = tokenizer.convert_tokens_to_ids(['''#'''] )[0] _a : Any = state_dict['''embeddings.word_embeddings.weight'''] _a : Optional[int] = word_emb[ent_init_index].unsqueeze(0 ) _a : Any = word_emb[enta_init_index].unsqueeze(0 ) _a : Union[str, Any] = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: _a : Tuple = state_dict[bias_name] _a : Optional[Any] = decoder_bias[ent_init_index].unsqueeze(0 ) _a : Optional[int] = decoder_bias[enta_init_index].unsqueeze(0 ) _a : Dict = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: _a : Tuple = F"""encoder.layer.{layer_index}.attention.self.""" _a : List[Any] = state_dict[prefix + matrix_name] _a : Dict = state_dict[prefix + matrix_name] _a : List[Any] = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks _a : Union[str, Any] = state_dict['''entity_embeddings.entity_embeddings.weight'''] _a : Optional[int] = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Any = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' _a : int = state_dict['''entity_predictions.bias'''] _a : int = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) _a : Optional[Any] = torch.cat([entity_prediction_bias, entity_mask_bias] ) _a : Optional[int] = LukeForMaskedLM(config=__a ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) _a : int = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): _a : Optional[int] = state_dict[key] else: _a : Tuple = state_dict[key] _a , _a : int = model.load_state_dict(__a ,strict=__a ) if set(__a ) != {"luke.embeddings.position_ids"}: raise ValueError(F"""Unexpected unexpected_keys: {unexpected_keys}""" ) if set(__a ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F"""Unexpected missing_keys: {missing_keys}""" ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ,task='''entity_classification''' ) _a : int = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' _a : List[Any] = (0, 9) _a : Tuple = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : int = model(**__a ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base _a : List[str] = torch.Size((1, 33, 768) ) _a : Union[str, Any] = torch.tensor([[0.08_92, 0.05_96, -0.28_19], [0.01_34, 0.11_99, 0.05_73], [-0.01_69, 0.09_27, 0.06_44]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}""" ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base _a : str = torch.Size((1, 1, 768) ) _a : List[Any] = torch.tensor([[-0.14_82, 0.06_09, 0.03_22]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F"""Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is""" F""" {expected_shape}""" ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] ,__a ,atol=1E-4 ): raise ValueError # Verify masked word/entity prediction _a : Optional[int] = MLukeTokenizer.from_pretrained(__a ) _a : Dict = '''Tokyo is the capital of <mask>.''' _a : List[str] = (24, 30) _a : Optional[int] = tokenizer(__a ,entity_spans=[span] ,return_tensors='''pt''' ) _a : Optional[Any] = model(**__a ) _a : Any = encoding['''input_ids'''][0].tolist() _a : Optional[Any] = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) _a : Any = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(__a ) _a : Any = outputs.entity_logits[0][0].argmax().item() _a : Optional[Any] = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(__a ) ) model.save_pretrained(__a ) def __UpperCAmelCase ( __a : List[Any] ) -> int: """simple docstring""" _a : Union[str, Any] = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] _a : int = [json.loads(__a ) for line in open(__a )] _a : List[Any] = {} for entry in data: _a : int = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: _a : List[Any] = entity_id break _a : Dict = F"""{language}:{entity_name}""" _a : int = entity_id return new_mapping if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) a__ = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
14
1
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 a__ = logging.get_logger(__name__) a__ = {'''vocab_file''': '''spiece.model'''} a__ = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', } } a__ = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } # Segments (not really needed) a__ = 0 a__ = 1 a__ = 2 a__ = 3 a__ = 4 class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = VOCAB_FILES_NAMES UpperCAmelCase__ : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : List[Any] = "left" def __init__( self , _a , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , _a = None , **_a , ) -> None: # Mask token behave like a normal word, i.e. include the space before it _a : Optional[int] = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token _a : List[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , sp_model_kwargs=self.sp_model_kwargs , **_a , ) _a : List[Any] = 3 _a : List[str] = do_lower_case _a : int = remove_space _a : Optional[Any] = keep_accents _a : List[Any] = vocab_file _a : Union[str, Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_a ) @property def __lowercase ( self ) -> int: return len(self.sp_model ) def __lowercase ( self ) -> List[Any]: _a : str = {self.convert_ids_to_tokens(_a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> Any: _a : Any = self.__dict__.copy() _a : Optional[Any] = None return state def __setstate__( self , _a ) -> Optional[Any]: _a : Tuple = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _a : Any = {} _a : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowercase ( self , _a ) -> int: if self.remove_space: _a : Union[str, Any] = ''' '''.join(inputs.strip().split() ) else: _a : Tuple = inputs _a : Dict = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: _a : Tuple = unicodedata.normalize('''NFKD''' , _a ) _a : Tuple = ''''''.join([c for c in outputs if not unicodedata.combining(_a )] ) if self.do_lower_case: _a : int = outputs.lower() return outputs def __lowercase ( self , _a ) -> List[str]: _a : int = self.preprocess_text(_a ) _a : int = self.sp_model.encode(_a , out_type=_a ) _a : List[str] = [] for piece in pieces: if len(_a ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): _a : Optional[int] = self.sp_model.EncodeAsPieces(piece[:-1].replace(_a , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: _a : Tuple = cur_pieces[1:] else: _a : List[Any] = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_a ) else: new_pieces.append(_a ) return new_pieces def __lowercase ( self , _a ) -> Union[str, Any]: return self.sp_model.PieceToId(_a ) def __lowercase ( self , _a ) -> Optional[Any]: return self.sp_model.IdToPiece(_a ) def __lowercase ( self , _a ) -> Any: _a : Optional[Any] = ''''''.join(_a ).replace(_a , ''' ''' ).strip() return out_string def __lowercase ( self , _a , _a = False , _a = None , _a = True , **_a , ) -> str: _a : int = kwargs.pop('''use_source_tokenizer''' , _a ) _a : str = self.convert_ids_to_tokens(_a , skip_special_tokens=_a ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 _a : int = [] _a : str = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_a ) ) _a : str = [] sub_texts.append(_a ) else: current_sub_text.append(_a ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_a ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens _a : Any = ''''''.join(_a ) _a : List[str] = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: _a : str = self.clean_up_tokenization(_a ) return clean_text else: return text def __lowercase ( self , _a , _a = None ) -> List[int]: _a : Optional[Any] = [self.sep_token_id] _a : Union[str, Any] = [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 __lowercase ( self , _a , _a = None , _a = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_a , token_ids_a=_a , already_has_special_tokens=_a ) if token_ids_a is not None: return ([0] * len(_a )) + [1] + ([0] * len(_a )) + [1, 1] return ([0] * len(_a )) + [1, 1] def __lowercase ( self , _a , _a = None ) -> List[int]: _a : Optional[int] = [self.sep_token_id] _a : Optional[int] = [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 __lowercase ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(_a ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _a : Optional[int] = os.path.join( _a , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _a ) elif not os.path.isfile(self.vocab_file ): with open(_a , '''wb''' ) as fi: _a : Any = self.sp_model.serialized_model_proto() fi.write(_a ) return (out_vocab_file,)
14
from scipy.stats import spearmanr import datasets a__ = ''' The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. ''' a__ = ''' Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {\'spearmanr\': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results[\'spearmanr\']) -0.7 >>> print(round(results[\'spearmanr_pvalue\'], 2)) 0.19 ''' a__ = R'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) , reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'''] , ) def __lowercase ( self , _a , _a , _a=False ) -> str: _a : int = spearmanr(_a , _a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
14
1
import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets a__ = datasets.logging.get_logger(__name__) a__ = '''\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = "Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric", author = "Moosavi, Nafise Sadat and Strube, Michael", booktitle = "Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)", month = aug, year = "2016", address = "Berlin, Germany", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/P16-1060", doi = "10.18653/v1/P16-1060", pages = "632--642", } ''' a__ = '''\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the "([pos] [word])" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a "-" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section "*_conll File Format"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. ''' a__ = ''' Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting \'keep_singletons=False\', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs. min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: \'mentions\': mentions \'muc\': MUC metric [Vilain et al, 1995] \'bcub\': B-cubed [Bagga and Baldwin, 1998] \'ceafe\': CEAFe [Luo et al., 2005] \'lea\': LEA [Moosavi and Strube, 2016] \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric(\'coval\') >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\', ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\', ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\', ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\', ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\', ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0} ''' def __UpperCAmelCase ( __a : Optional[int] ,__a : Tuple ,__a : str=False ,__a : Dict=False ,__a : Any=True ,__a : int=False ,__a : Union[str, Any]="dummy_doc" ) -> List[Any]: """simple docstring""" _a : int = {doc: key_lines} _a : Dict = {doc: sys_lines} _a : Tuple = {} _a : Dict = 0 _a : List[str] = 0 _a : List[Any] = 0 _a : Dict = 0 _a : int = 0 _a : int = 0 _a , _a : Optional[Any] = reader.get_doc_mentions(__a ,key_doc_lines[doc] ,__a ) key_singletons_num += singletons_num if NP_only or min_span: _a : Optional[int] = reader.set_annotated_parse_trees(__a ,key_doc_lines[doc] ,__a ,__a ) _a , _a : List[Any] = reader.get_doc_mentions(__a ,sys_doc_lines[doc] ,__a ) sys_singletons_num += singletons_num if NP_only or min_span: _a : Any = reader.set_annotated_parse_trees(__a ,key_doc_lines[doc] ,__a ,__a ) if remove_nested: _a , _a : str = reader.remove_nested_coref_mentions(__a ,__a ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters _a , _a : Optional[Any] = reader.remove_nested_coref_mentions(__a ,__a ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters _a : Dict = reader.get_mention_assignments(__a ,__a ) _a : List[str] = reader.get_mention_assignments(__a ,__a ) _a : List[str] = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( '''Number of removed nested coreferring mentions in the key ''' F"""annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}""" ) logger.info( '''Number of resulting singleton clusters in the key ''' F"""annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}""" ) if not keep_singletons: logger.info( F"""{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system """ '''files, respectively''' ) return doc_coref_infos def __UpperCAmelCase ( __a : Any ,__a : List[str] ,__a : Optional[Any] ,__a : Any ,__a : Optional[int] ,__a : List[str] ,__a : int ) -> List[Any]: """simple docstring""" _a : Optional[int] = get_coref_infos(__a ,__a ,__a ,__a ,__a ,__a ) _a : List[str] = {} _a : Union[str, Any] = 0 _a : Union[str, Any] = 0 for name, metric in metrics: _a , _a , _a : int = evaluator.evaluate_documents(__a ,__a ,beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F"""{name}/recall""": recall, F"""{name}/precision""": precision, F"""{name}/f1""": fa} ) logger.info( name.ljust(10 ) ,F"""Recall: {recall * 100:.2f}""" ,F""" Precision: {precision * 100:.2f}""" ,F""" F1: {fa * 100:.2f}""" ,) if conll_subparts_num == 3: _a : int = (conll / 3) * 100 logger.info(F"""CoNLL score: {conll:.2f}""" ) output_scores.update({'''conll_score''': conll} ) return output_scores def __UpperCAmelCase ( __a : int ) -> List[Any]: """simple docstring""" _a : List[Any] = False for line in key_lines: if not line.startswith('''#''' ): if len(line.split() ) > 6: _a : Any = line.split()[5] if not parse_col == "-": _a : Any = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): """simple docstring""" def __lowercase ( self ) -> List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''string''' ) ), '''references''': datasets.Sequence(datasets.Value('''string''' ) ), } ) , codebase_urls=['''https://github.com/ns-moosavi/coval'''] , reference_urls=[ '''https://github.com/ns-moosavi/coval''', '''https://www.aclweb.org/anthology/P16-1060''', '''http://www.conll.cemantix.org/2012/data.html''', ] , ) def __lowercase ( self , _a , _a , _a=True , _a=False , _a=False , _a=False ) -> Any: _a : List[Any] = [ ('''mentions''', evaluator.mentions), ('''muc''', evaluator.muc), ('''bcub''', evaluator.b_cubed), ('''ceafe''', evaluator.ceafe), ('''lea''', evaluator.lea), ] if min_span: _a : Any = util.check_gold_parse_annotation(_a ) if not has_gold_parse: raise NotImplementedError('''References should have gold parse annotation to use \'min_span\'.''' ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" _a : Union[str, Any] = evaluate( key_lines=_a , sys_lines=_a , metrics=_a , NP_only=_a , remove_nested=_a , keep_singletons=_a , min_span=_a , ) return score
14
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def __UpperCAmelCase ( __a : bytes ,__a : int ) -> np.array: """simple docstring""" _a : int = F"""{sampling_rate}""" _a : str = '''1''' _a : Optional[int] = '''f32le''' _a : Optional[Any] = [ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(__a ,stdin=subprocess.PIPE ,stdout=subprocess.PIPE ) as ffmpeg_process: _a : Any = ffmpeg_process.communicate(__a ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error _a : Optional[Any] = output_stream[0] _a : Optional[int] = np.frombuffer(__a ,np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def __UpperCAmelCase ( __a : int ,__a : float ,__a : str = "f32le" ,) -> str: """simple docstring""" _a : Dict = F"""{sampling_rate}""" _a : Optional[Any] = '''1''' if format_for_conversion == "s16le": _a : Dict = 2 elif format_for_conversion == "f32le": _a : Optional[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) _a : Dict = platform.system() if system == "Linux": _a : Dict = '''alsa''' _a : Union[str, Any] = '''default''' elif system == "Darwin": _a : Union[str, Any] = '''avfoundation''' _a : List[str] = ''':0''' elif system == "Windows": _a : Optional[int] = '''dshow''' _a : str = '''default''' _a : Tuple = [ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] _a : Any = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample _a : str = _ffmpeg_stream(__a ,__a ) for item in iterator: yield item def __UpperCAmelCase ( __a : int ,__a : float ,__a : Optional[int] = None ,__a : Optional[Union[Tuple[float, float], float]] = None ,__a : str = "f32le" ,) -> Optional[int]: """simple docstring""" if stream_chunk_s is not None: _a : Tuple = stream_chunk_s else: _a : Tuple = chunk_length_s _a : Tuple = ffmpeg_microphone(__a ,__a ,format_for_conversion=__a ) if format_for_conversion == "s16le": _a : Any = np.intaa _a : Optional[int] = 2 elif format_for_conversion == "f32le": _a : Dict = np.floataa _a : List[Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) if stride_length_s is None: _a : List[Any] = chunk_length_s / 6 _a : Optional[int] = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__a ,(int, float) ): _a : Optional[Any] = [stride_length_s, stride_length_s] _a : Optional[Any] = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample _a : str = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample _a : Optional[Any] = datetime.datetime.now() _a : Tuple = datetime.timedelta(seconds=__a ) for item in chunk_bytes_iter(__a ,__a ,stride=(stride_left, stride_right) ,stream=__a ): # Put everything back in numpy scale _a : Dict = np.frombuffer(item['''raw'''] ,dtype=__a ) _a : Dict = ( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) _a : str = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def __UpperCAmelCase ( __a : Optional[int] ,__a : int ,__a : Tuple[int, int] ,__a : bool = False ) -> Optional[int]: """simple docstring""" _a : Any = b'''''' _a , _a : List[str] = stride if stride_left + stride_right >= chunk_len: raise ValueError( F"""Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}""" ) _a : List[str] = 0 for raw in iterator: acc += raw if stream and len(__a ) < chunk_len: _a : Dict = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__a ) >= chunk_len: # We are flushing the accumulator _a : List[str] = (_stride_left, stride_right) _a : List[Any] = {'''raw''': acc[:chunk_len], '''stride''': stride} if stream: _a : List[Any] = False yield item _a : Optional[Any] = stride_left _a : Optional[Any] = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__a ) > stride_left: _a : Optional[Any] = {'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: _a : Dict = False yield item def __UpperCAmelCase ( __a : int ,__a : int ) -> Tuple: """simple docstring""" _a : Dict = 2**24 # 16Mo try: with subprocess.Popen(__a ,stdout=subprocess.PIPE ,bufsize=__a ) as ffmpeg_process: while True: _a : int = ffmpeg_process.stdout.read(__a ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
14
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL a__ = logging.get_logger(__name__) def __UpperCAmelCase ( __a : Union[str, Any] ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(__a ,(list, tuple) ) and isinstance(videos[0] ,(list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(__a ,(list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(__a ): return [[videos]] raise ValueError(F"""Could not make batched video from {videos}""" ) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[Any] = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = None , _a = True , _a = 1 / 2_5_5 , _a = True , _a = None , _a = None , **_a , ) -> None: super().__init__(**_a ) _a : Tuple = size if size is not None else {'''shortest_edge''': 2_2_4} _a : int = get_size_dict(_a , default_to_square=_a ) _a : str = crop_size if crop_size is not None else {'''height''': 2_2_4, '''width''': 2_2_4} _a : List[str] = get_size_dict(_a , param_name='''crop_size''' ) _a : Dict = do_resize _a : Any = size _a : str = do_center_crop _a : List[Any] = crop_size _a : str = resample _a : Optional[Any] = do_rescale _a : Union[str, Any] = rescale_factor _a : int = do_normalize _a : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _a : int = image_std if image_std is not None else IMAGENET_STANDARD_STD def __lowercase ( self , _a , _a , _a = PILImageResampling.BILINEAR , _a = None , **_a , ) -> np.ndarray: _a : Union[str, Any] = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" in size: _a : List[str] = get_resize_output_image_size(_a , size['''shortest_edge'''] , default_to_square=_a ) elif "height" in size and "width" in size: _a : Any = (size['''height'''], size['''width''']) else: raise ValueError(F"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}""" ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def __lowercase ( self , _a , _a , _a = None , **_a , ) -> np.ndarray: _a : Union[str, Any] = get_size_dict(_a ) 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(_a , size=(size['''height'''], size['''width''']) , data_format=_a , **_a ) def __lowercase ( self , _a , _a , _a = None , **_a , ) -> Dict: return rescale(_a , scale=_a , data_format=_a , **_a ) def __lowercase ( self , _a , _a , _a , _a = None , **_a , ) -> np.ndarray: return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def __lowercase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = 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.''' ) # All transformations expect numpy arrays. _a : Tuple = to_numpy_array(_a ) if do_resize: _a : str = self.resize(image=_a , size=_a , resample=_a ) if do_center_crop: _a : Optional[Any] = self.center_crop(_a , size=_a ) if do_rescale: _a : Any = self.rescale(image=_a , scale=_a ) if do_normalize: _a : Any = self.normalize(image=_a , mean=_a , std=_a ) _a : int = to_channel_dimension_format(_a , _a ) return image def __lowercase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ) -> PIL.Image.Image: _a : str = do_resize if do_resize is not None else self.do_resize _a : Union[str, Any] = resample if resample is not None else self.resample _a : Tuple = do_center_crop if do_center_crop is not None else self.do_center_crop _a : int = do_rescale if do_rescale is not None else self.do_rescale _a : Tuple = rescale_factor if rescale_factor is not None else self.rescale_factor _a : List[str] = do_normalize if do_normalize is not None else self.do_normalize _a : List[str] = image_mean if image_mean is not None else self.image_mean _a : Tuple = image_std if image_std is not None else self.image_std _a : Optional[int] = size if size is not None else self.size _a : Any = get_size_dict(_a , default_to_square=_a ) _a : Optional[Any] = crop_size if crop_size is not None else self.crop_size _a : List[Any] = get_size_dict(_a , param_name='''crop_size''' ) if not valid_images(_a ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) _a : int = make_batched(_a ) _a : str = [ [ self._preprocess_image( image=_a , do_resize=_a , size=_a , resample=_a , do_center_crop=_a , crop_size=_a , do_rescale=_a , rescale_factor=_a , do_normalize=_a , image_mean=_a , image_std=_a , data_format=_a , ) for img in video ] for video in videos ] _a : str = {'''pixel_values''': videos} return BatchFeature(data=_a , tensor_type=_a )
14
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = KandinskyInpaintPipeline UpperCAmelCase__ : Optional[int] = ["prompt", "image_embeds", "negative_image_embeds", "image", "mask_image"] UpperCAmelCase__ : Optional[Any] = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", "mask_image", ] UpperCAmelCase__ : Optional[int] = [ "generator", "height", "width", "latents", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase__ : Any = False @property def __lowercase ( self ) -> Optional[int]: return 3_2 @property def __lowercase ( self ) -> int: return 3_2 @property def __lowercase ( self ) -> List[str]: return self.time_input_dim @property def __lowercase ( self ) -> List[str]: return self.time_input_dim * 4 @property def __lowercase ( self ) -> Optional[Any]: return 1_0_0 @property def __lowercase ( self ) -> Optional[Any]: _a : Any = XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[Any] = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) _a : Optional[int] = MultilingualCLIP(_a ) _a : Tuple = text_encoder.eval() return text_encoder @property def __lowercase ( self ) -> str: torch.manual_seed(0 ) _a : List[str] = { '''in_channels''': 9, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _a : Dict = UNetaDConditionModel(**_a ) return model @property def __lowercase ( self ) -> Optional[int]: return { "block_out_channels": [3_2, 6_4], "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": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowercase ( self ) -> Tuple: torch.manual_seed(0 ) _a : List[Any] = VQModel(**self.dummy_movq_kwargs ) return model def __lowercase ( self ) -> Any: _a : List[Any] = self.dummy_text_encoder _a : Optional[Any] = self.dummy_tokenizer _a : Optional[Any] = self.dummy_unet _a : Union[str, Any] = self.dummy_movq _a : Tuple = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''linear''' , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=_a , set_alpha_to_one=_a , steps_offset=1 , prediction_type='''epsilon''' , thresholding=_a , ) _a : str = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __lowercase ( self , _a , _a=0 ) -> int: _a : Union[str, Any] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _a : List[str] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _a : Tuple = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(_a ) ).to(_a ) _a : Dict = image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : Optional[int] = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) # create mask _a : Union[str, Any] = np.ones((6_4, 6_4) , dtype=np.floataa ) _a : List[str] = 0 if str(_a ).startswith('''mps''' ): _a : Tuple = torch.manual_seed(_a ) else: _a : Any = torch.Generator(device=_a ).manual_seed(_a ) _a : Any = { '''prompt''': '''horse''', '''image''': init_image, '''mask_image''': mask, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 2, '''guidance_scale''': 4.0, '''output_type''': '''np''', } return inputs def __lowercase ( self ) -> Optional[Any]: _a : Optional[Any] = '''cpu''' _a : List[Any] = self.get_dummy_components() _a : Tuple = self.pipeline_class(**_a ) _a : int = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _a : Any = pipe(**self.get_dummy_inputs(_a ) ) _a : str = output.images _a : Tuple = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _a : Union[str, Any] = image[0, -3:, -3:, -1] _a : Tuple = image_from_tuple[0, -3:, -3:, -1] print(F"""image.shape {image.shape}""" ) assert image.shape == (1, 6_4, 6_4, 3) _a : str = np.array( [0.832_6919, 0.7379_0467, 0.2091_8581, 0.930_9612, 0.551_1791, 0.4371_3328, 0.551_3321, 0.4992_2934, 0.5949_7786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def __lowercase ( self ) -> Dict: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowercase ( self ) -> Union[str, Any]: _a : Tuple = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy''' ) _a : str = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _a : Tuple = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) _a : Any = 0 _a : Optional[Any] = '''a hat''' _a : Optional[Any] = KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _a : Tuple = KandinskyInpaintPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-inpaint''' , torch_dtype=torch.floataa ) _a : Union[str, Any] = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _a : Union[str, Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) _a , _a : Dict = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _a : Optional[int] = pipeline( _a , image=_a , mask_image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type='''np''' , ) _a : Optional[int] = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(_a , _a )
14
1
from decimal import Decimal, getcontext from math import ceil, factorial def __UpperCAmelCase ( __a : int ) -> str: """simple docstring""" if not isinstance(__a ,__a ): raise TypeError('''Undefined for non-integers''' ) elif precision < 1: raise ValueError('''Undefined for non-natural numbers''' ) _a : Union[str, Any] = precision _a : str = ceil(precision / 14 ) _a : Dict = 426_880 * Decimal(10_005 ).sqrt() _a : Optional[Any] = 1 _a : Optional[Any] = 13_591_409 _a : List[Any] = Decimal(__a ) for k in range(1 ,__a ): _a : Optional[Any] = factorial(6 * k ) // (factorial(3 * k ) * factorial(__a ) ** 3) linear_term += 545_140_134 exponential_term *= -262_537_412_640_768_000 partial_sum += Decimal(multinomial_term * linear_term ) / exponential_term return str(constant_term / partial_sum )[:-1] if __name__ == "__main__": a__ = 50 print(f'''The first {n} digits of pi is: {pi(n)}''')
14
import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_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.''' ) parser.add_argument( '''--original_config_file''', type=str, required=True, 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( '''--image_size''', default=512, 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( '''--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.)''') def __UpperCAmelCase ( __a : Any ) -> List[Any]: """simple docstring""" if string == "True": return True elif string == "False": return False else: raise ValueError(F"""could not parse string as bool {string}""" ) parser.add_argument( '''--use_linear_projection''', help='''Override for use linear projection''', required=False, type=parse_bool ) parser.add_argument('''--cross_attention_dim''', help='''Override for cross attention_dim''', required=False, type=int) a__ = parser.parse_args() a__ = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
14
1
import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch a__ = logging.get_logger(__name__) class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a = None , _a = None , _a=None , _a=None ) -> Union[str, Any]: if not conversation_id: _a : Dict = uuid.uuida() if past_user_inputs is None: _a : Any = [] if generated_responses is None: _a : int = [] _a : uuid.UUID = conversation_id _a : List[str] = past_user_inputs _a : List[str] = generated_responses _a : Optional[str] = text def __eq__( self , _a ) -> Any: if not isinstance(_a , _a ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def __lowercase ( self , _a , _a = False ) -> List[str]: if self.new_user_input: if overwrite: logger.warning( F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" was overwritten """ F"""with: \"{text}\".""" ) _a : Dict = text else: logger.warning( F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" new input """ F"""ignored: \"{text}\". Set `overwrite` to True to overwrite unprocessed user input""" ) else: _a : int = text def __lowercase ( self ) -> Optional[int]: if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) _a : List[Any] = None def __lowercase ( self , _a ) -> Optional[Any]: self.generated_responses.append(_a ) def __lowercase ( self ) -> str: for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self ) -> Optional[int]: _a : List[Any] = F"""Conversation id: {self.uuid} \n""" for is_user, text in self.iter_texts(): _a : int = '''user''' if is_user else '''bot''' output += F"""{name} >> {text} \n""" return output @add_end_docstrings( __lowercase , r"\n min_length_for_response (`int`, *optional*, defaults to 32):\n The minimum length (in number of tokens) for a response.\n minimum_tokens (`int`, *optional*, defaults to 10):\n The minimum length of tokens to leave for a response.\n " , ) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self , *_a , **_a ) -> List[str]: super().__init__(*_a , **_a ) if self.tokenizer.pad_token_id is None: _a : str = self.tokenizer.eos_token def __lowercase ( self , _a=None , _a=None , _a=None , **_a ) -> Union[str, Any]: _a : int = {} _a : str = {} _a : List[Any] = {} if min_length_for_response is not None: _a : int = min_length_for_response if minimum_tokens is not None: _a : str = minimum_tokens if "max_length" in generate_kwargs: _a : Optional[int] = generate_kwargs['''max_length'''] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: _a : Optional[int] = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(_a ) return preprocess_params, forward_params, postprocess_params def __call__( self , _a , _a=0 , **_a ) -> int: _a : Optional[int] = super().__call__(_a , num_workers=_a , **_a ) if isinstance(_a , _a ) and len(_a ) == 1: return outputs[0] return outputs def __lowercase ( self , _a , _a=3_2 ) -> Dict[str, Any]: if not isinstance(_a , _a ): raise ValueError('''ConversationalPipeline, expects Conversation as inputs''' ) if conversation.new_user_input is None: raise ValueError( F"""Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. """ '''Add user inputs with the conversation\'s `add_user_input` method''' ) if hasattr(self.tokenizer , '''_build_conversation_input_ids''' ): _a : List[Any] = self.tokenizer._build_conversation_input_ids(_a ) else: # If the tokenizer cannot handle conversations, we default to only the old version _a : str = self._legacy_parse_and_tokenize(_a ) if self.framework == "pt": _a : Optional[int] = torch.LongTensor([input_ids] ) elif self.framework == "tf": _a : Dict = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def __lowercase ( self , _a , _a=1_0 , **_a ) -> Union[str, Any]: _a : int = generate_kwargs.get('''max_length''' , self.model.config.max_length ) _a : Union[str, Any] = model_inputs['''input_ids'''].shape[1] if max_length - minimum_tokens < n: logger.warning(F"""Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})""" ) _a : Optional[int] = max_length - minimum_tokens _a : Any = model_inputs['''input_ids'''][:, -trim:] if "attention_mask" in model_inputs: _a : Optional[Any] = model_inputs['''attention_mask'''][:, -trim:] _a : Optional[Any] = model_inputs.pop('''conversation''' ) _a : Union[str, Any] = max_length _a : List[str] = self.model.generate(**_a , **_a ) if self.model.config.is_encoder_decoder: _a : str = 1 else: _a : Tuple = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def __lowercase ( self , _a , _a=True ) -> Union[str, Any]: _a : Optional[int] = model_outputs['''output_ids'''] _a : List[Any] = self.tokenizer.decode( output_ids[0] , skip_special_tokens=_a , clean_up_tokenization_spaces=_a , ) _a : str = model_outputs['''conversation'''] conversation.mark_processed() conversation.append_response(_a ) return conversation def __lowercase ( self , _a ) -> Dict: _a : Union[str, Any] = self.tokenizer.eos_token_id _a : Union[str, Any] = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(_a , add_special_tokens=_a ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(_a , add_special_tokens=_a ) ) if len(_a ) > self.tokenizer.model_max_length: _a : Optional[Any] = input_ids[-self.tokenizer.model_max_length :] return input_ids
14
class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a , _a ) -> List[str]: _a : List[Any] = name _a : List[str] = value _a : List[str] = weight def __repr__( self ) -> Optional[int]: return F"""{self.__class__.__name__}({self.name}, {self.value}, {self.weight})""" def __lowercase ( self ) -> List[Any]: return self.value def __lowercase ( self ) -> int: return self.name def __lowercase ( self ) -> Optional[int]: return self.weight def __lowercase ( self ) -> Optional[Any]: return self.value / self.weight def __UpperCAmelCase ( __a : Optional[int] ,__a : Tuple ,__a : List[str] ) -> List[str]: """simple docstring""" _a : Optional[int] = [] for i in range(len(__a ) ): menu.append(Things(name[i] ,value[i] ,weight[i] ) ) return menu def __UpperCAmelCase ( __a : int ,__a : Union[str, Any] ,__a : int ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = sorted(__a ,key=__a ,reverse=__a ) _a : Any = [] _a , _a : Optional[int] = 0.0, 0.0 for i in range(len(__a ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def __UpperCAmelCase ( ) -> int: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
14
1
from __future__ import annotations def __UpperCAmelCase ( __a : list[int] ,__a : int ) -> list[list[int]]: """simple docstring""" _a : list[list[int]] = [] _a : list[int] = [] _a : Optional[int] = 0 _a : Any = sum(__a ) create_state_space_tree(__a ,__a ,__a ,__a ,__a ,__a ) return result def __UpperCAmelCase ( __a : list[int] ,__a : int ,__a : int ,__a : list[int] ,__a : list[list[int]] ,__a : int ,) -> None: """simple docstring""" if sum(__a ) > max_sum or (remaining_nums_sum + sum(__a )) < max_sum: return if sum(__a ) == max_sum: result.append(__a ) return for index in range(__a ,len(__a ) ): create_state_space_tree( __a ,__a ,index + 1 ,[*path, nums[index]] ,__a ,remaining_nums_sum - nums[index] ,) a__ = [3, 34, 4, 12, 5, 2] a__ = 9 a__ = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
14
import copy import inspect import unittest from transformers import PretrainedConfig, SwiftFormerConfig from transformers.testing_utils import ( require_torch, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwiftFormerForImageClassification, SwiftFormerModel from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=1_3 , _a=3 , _a=True , _a=True , _a=0.1 , _a=0.1 , _a=2_2_4 , _a=1_0_0_0 , _a=[3, 3, 6, 4] , _a=[4_8, 5_6, 1_1_2, 2_2_0] , ) -> Tuple: _a : Dict = parent _a : Optional[int] = batch_size _a : Optional[Any] = num_channels _a : Union[str, Any] = is_training _a : Tuple = use_labels _a : Dict = hidden_dropout_prob _a : List[Any] = attention_probs_dropout_prob _a : Dict = num_labels _a : List[str] = image_size _a : Dict = layer_depths _a : str = embed_dims def __lowercase ( self ) -> Optional[Any]: _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : int = None if self.use_labels: _a : Optional[Any] = ids_tensor([self.batch_size] , self.num_labels ) _a : Dict = self.get_config() return config, pixel_values, labels def __lowercase ( self ) -> int: return SwiftFormerConfig( depths=self.layer_depths , embed_dims=self.embed_dims , mlp_ratio=4 , downsamples=[True, True, True, True] , hidden_act='''gelu''' , num_labels=self.num_labels , down_patch_size=3 , down_stride=2 , down_pad=1 , drop_rate=0.0 , drop_path_rate=0.0 , use_layer_scale=_a , layer_scale_init_value=1e-5 , ) def __lowercase ( self , _a , _a , _a ) -> str: _a : List[Any] = SwiftFormerModel(config=_a ) model.to(_a ) model.eval() _a : Optional[int] = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dims[-1], 7, 7) ) def __lowercase ( self , _a , _a , _a ) -> Optional[Any]: _a : List[str] = self.num_labels _a : Optional[int] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : List[str] = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) _a : Union[str, Any] = SwiftFormerForImageClassification(_a ) model.to(_a ) model.eval() _a : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : Optional[Any] = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowercase ( self ) -> Tuple: ((_a) , (_a) , (_a)) : Optional[int] = self.prepare_config_and_inputs() _a : List[Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else () UpperCAmelCase__ : Optional[int] = ( {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Optional[Any] = False UpperCAmelCase__ : str = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : str = False def __lowercase ( self ) -> Optional[int]: _a : Union[str, Any] = SwiftFormerModelTester(self ) _a : int = ConfigTester( self , config_class=_a , has_text_modality=_a , hidden_size=3_7 , num_attention_heads=1_2 , num_hidden_layers=1_2 , ) def __lowercase ( self ) -> int: self.config_tester.run_common_tests() @unittest.skip(reason='''SwiftFormer does not use inputs_embeds''' ) def __lowercase ( self ) -> Union[str, Any]: pass def __lowercase ( self ) -> Dict: _a , _a : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Any = model_class(_a ) _a : int = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def __lowercase ( self ) -> str: _a , _a : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Optional[int] = model_class(_a ) _a : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Tuple = [*signature.parameters.keys()] _a : List[str] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowercase ( self ) -> int: _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self ) -> Optional[int]: _a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self ) -> Optional[Any]: for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Any = SwiftFormerModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason='''SwiftFormer does not output attentions''' ) def __lowercase ( self ) -> List[Any]: pass def __lowercase ( self ) -> int: def check_hidden_states_output(_a , _a , _a ): _a : Optional[int] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : Union[str, Any] = model(**self._prepare_for_class(_a , _a ) ) _a : Optional[Any] = outputs.hidden_states _a : Union[str, Any] = 8 self.assertEqual(len(_a ) , _a ) # TODO # SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width) # with the width and height being successively divided by 2, after every 2 blocks for i in range(len(_a ) ): self.assertEqual( hidden_states[i].shape , torch.Size( [ self.model_tester.batch_size, self.model_tester.embed_dims[i // 2], (self.model_tester.image_size // 4) // 2 ** (i // 2), (self.model_tester.image_size // 4) // 2 ** (i // 2), ] ) , ) _a , _a : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : str = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : List[str] = True check_hidden_states_output(_a , _a , _a ) def __lowercase ( self ) -> str: def _config_zero_init(_a ): _a : List[Any] = copy.deepcopy(_a ) for key in configs_no_init.__dict__.keys(): if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key: setattr(_a , _a , 1e-1_0 ) if isinstance(getattr(_a , _a , _a ) , _a ): _a : int = _config_zero_init(getattr(_a , _a ) ) setattr(_a , _a , _a ) return configs_no_init _a , _a : Any = self.model_tester.prepare_config_and_inputs_for_common() _a : Dict = _config_zero_init(_a ) for model_class in self.all_model_classes: _a : Dict = model_class(config=_a ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9) / 1e9).round().item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __lowercase ( self ) -> Optional[Any]: pass def __UpperCAmelCase ( ) -> Optional[Any]: """simple docstring""" _a : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self ) -> str: return ViTImageProcessor.from_pretrained('''MBZUAI/swiftformer-xs''' ) if is_vision_available() else None @slow def __lowercase ( self ) -> Dict: _a : Any = SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs''' ).to(_a ) _a : Any = self.default_image_processor _a : Any = prepare_img() _a : Any = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _a : Optional[Any] = model(**_a ) # verify the logits _a : List[str] = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _a ) _a : int = torch.tensor([[-2.1_7_0_3e0_0, 2.1_1_0_7e0_0, -2.0_8_1_1e0_0]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
14
1
from typing import TYPE_CHECKING from ..utils import _LazyModule a__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys a__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
14
import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() a__ = logging.get_logger(__name__) def __UpperCAmelCase ( __a : str ) -> List[Any]: """simple docstring""" _a : Tuple = SwinConfig.from_pretrained( '''microsoft/swin-tiny-patch4-window7-224''' ,out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) _a : Dict = MaskFormerConfig(backbone_config=__a ) _a : Optional[Any] = '''huggingface/label-files''' if "ade20k-full" in model_name: # this should be ok _a : Optional[Any] = 847 _a : List[Any] = '''maskformer-ade20k-full-id2label.json''' elif "ade" in model_name: # this should be ok _a : Union[str, Any] = 150 _a : Any = '''ade20k-id2label.json''' elif "coco-stuff" in model_name: # this should be ok _a : int = 171 _a : List[str] = '''maskformer-coco-stuff-id2label.json''' elif "coco" in model_name: # TODO _a : Dict = 133 _a : Optional[Any] = '''coco-panoptic-id2label.json''' elif "cityscapes" in model_name: # this should be ok _a : List[Any] = 19 _a : Optional[Any] = '''cityscapes-id2label.json''' elif "vistas" in model_name: # this should be ok _a : List[Any] = 65 _a : Dict = '''mapillary-vistas-id2label.json''' _a : Optional[int] = json.load(open(hf_hub_download(__a ,__a ,repo_type='''dataset''' ) ,'''r''' ) ) _a : Tuple = {int(__a ): v for k, v in idalabel.items()} return config def __UpperCAmelCase ( __a : Optional[Any] ) -> Tuple: """simple docstring""" _a : Optional[Any] = [] # stem # fmt: off rename_keys.append(('''backbone.patch_embed.proj.weight''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.proj.bias''', '''model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''model.pixel_level_module.encoder.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''model.pixel_level_module.encoder.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.layers.{i}.downsample.reduction.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(('''sem_seg_head.layer_4.weight''', '''model.pixel_level_module.decoder.fpn.stem.0.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.weight''', '''model.pixel_level_module.decoder.fpn.stem.1.weight''') ) rename_keys.append(('''sem_seg_head.layer_4.norm.bias''', '''model.pixel_level_module.decoder.fpn.stem.1.bias''') ) for source_index, target_index in zip(range(3 ,0 ,-1 ) ,range(0 ,3 ) ): rename_keys.append((F"""sem_seg_head.adapter_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(('''sem_seg_head.mask_features.weight''', '''model.pixel_level_module.decoder.mask_projection.weight''') ) rename_keys.append(('''sem_seg_head.mask_features.bias''', '''model.pixel_level_module.decoder.mask_projection.bias''') ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.weight''', '''model.transformer_module.decoder.layernorm.weight''') ) rename_keys.append(('''sem_seg_head.predictor.transformer.decoder.norm.bias''', '''model.transformer_module.decoder.layernorm.bias''') ) # heads on top rename_keys.append(('''sem_seg_head.predictor.query_embed.weight''', '''model.transformer_module.queries_embedder.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.weight''', '''model.transformer_module.input_projection.weight''') ) rename_keys.append(('''sem_seg_head.predictor.input_proj.bias''', '''model.transformer_module.input_projection.bias''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.weight''', '''class_predictor.weight''') ) rename_keys.append(('''sem_seg_head.predictor.class_embed.bias''', '''class_predictor.bias''') ) for i in range(3 ): rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", F"""mask_embedder.{i}.0.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", F"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ,__a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _a : str = dct.pop(__a ) _a : str = val def __UpperCAmelCase ( __a : List[Any] ,__a : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" _a : Union[str, Any] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _a : Optional[Any] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) _a : Optional[int] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[int] = in_proj_weight[:dim, :] _a : List[Any] = in_proj_bias[: dim] _a : Optional[int] = in_proj_weight[ dim : dim * 2, : ] _a : Tuple = in_proj_bias[ dim : dim * 2 ] _a : int = in_proj_weight[ -dim :, : ] _a : Optional[int] = in_proj_bias[-dim :] # fmt: on def __UpperCAmelCase ( __a : List[str] ,__a : List[Any] ) -> List[Any]: """simple docstring""" _a : Optional[int] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : Union[str, Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Union[str, Any] = in_proj_weight[: hidden_size, :] _a : List[Any] = in_proj_bias[:config.hidden_size] _a : Dict = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Any = in_proj_bias[hidden_size : hidden_size * 2] _a : Tuple = in_proj_weight[-hidden_size :, :] _a : List[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) _a : List[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) _a : List[str] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _a : Optional[Any] = in_proj_weight[: hidden_size, :] _a : Any = in_proj_bias[:config.hidden_size] _a : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] _a : Optional[Any] = in_proj_bias[hidden_size : hidden_size * 2] _a : List[str] = in_proj_weight[-hidden_size :, :] _a : int = in_proj_bias[-hidden_size :] # fmt: on def __UpperCAmelCase ( ) -> torch.Tensor: """simple docstring""" _a : str = '''http://images.cocodataset.org/val2017/000000039769.jpg''' _a : Dict = Image.open(requests.get(__a ,stream=__a ).raw ) return im @torch.no_grad() def __UpperCAmelCase ( __a : str ,__a : str ,__a : str ,__a : bool = False ) -> Union[str, Any]: """simple docstring""" _a : Optional[Any] = get_maskformer_config(__a ) # load original state_dict with open(__a ,'''rb''' ) as f: _a : str = pickle.load(__a ) _a : Union[str, Any] = data['''model'''] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys _a : Any = create_rename_keys(__a ) for src, dest in rename_keys: rename_key(__a ,__a ,__a ) read_in_swin_q_k_v(__a ,config.backbone_config ) read_in_decoder_q_k_v(__a ,__a ) # update to torch tensors for key, value in state_dict.items(): _a : Optional[int] = torch.from_numpy(__a ) # load 🤗 model _a : Dict = MaskFormerForInstanceSegmentation(__a ) model.eval() for name, param in model.named_parameters(): print(__a ,param.shape ) _a , _a : Tuple = model.load_state_dict(__a ,strict=__a ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(__a ) == 0, F"""Unexpected keys: {unexpected_keys}""" # verify results _a : Union[str, Any] = prepare_img() if "vistas" in model_name: _a : int = 65 elif "cityscapes" in model_name: _a : Tuple = 65_535 else: _a : str = 255 _a : Dict = True if '''ade''' in model_name else False _a : Optional[Any] = MaskFormerImageProcessor(ignore_index=__a ,reduce_labels=__a ) _a : Optional[Any] = image_processor(__a ,return_tensors='''pt''' ) _a : int = model(**__a ) print('''Logits:''' ,outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": _a : Union[str, Any] = torch.tensor( [[3.63_53, -4.47_70, -2.60_65], [0.50_81, -4.23_94, -3.53_43], [2.19_09, -5.03_53, -1.93_23]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] ,__a ,atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(__a ).mkdir(exist_ok=__a ) model.save_pretrained(__a ) image_processor.save_pretrained(__a ) if push_to_hub: print('''Pushing model and image processor to the hub...''' ) model.push_to_hub(F"""nielsr/{model_name}""" ) image_processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": a__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''maskformer-swin-tiny-ade''', type=str, help=('''Name of the MaskFormer model you\'d like to convert''',), ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl''', type=str, help='''Path to the original state dict (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) a__ = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
14
1
def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" if not all(x.isalpha() for x in string ): raise ValueError('''String must only contain alphabetic characters.''' ) _a : Optional[Any] = sorted(string.lower() ) return len(__a ) == len(set(__a ) ) if __name__ == "__main__": a__ = input('''Enter a string ''').strip() a__ = is_isogram(input_str) print(f'''{input_str} is {"an" if isogram else "not an"} isogram.''')
14
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin a__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = XLMProphetNetTokenizer UpperCAmelCase__ : Optional[int] = False UpperCAmelCase__ : List[Any] = True def __lowercase ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _a : List[Any] = XLMProphetNetTokenizer(_a , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def __lowercase ( self ) -> Any: _a : Tuple = '''[PAD]''' _a : int = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def __lowercase ( self ) -> str: _a : Any = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''[PAD]''' ) self.assertEqual(vocab_keys[1] , '''[CLS]''' ) self.assertEqual(vocab_keys[-1] , '''j''' ) self.assertEqual(len(_a ) , 1_0_1_2 ) def __lowercase ( self ) -> Union[str, Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1_0_1_2 ) def __lowercase ( self ) -> str: _a : Tuple = XLMProphetNetTokenizer(_a , keep_accents=_a ) _a : Union[str, Any] = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) _a : Optional[int] = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _a : List[Any] = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, -9, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, -9, 4] ] , ) _a : List[str] = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''[UNK]''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''[UNK]''', '''.''', ] , ) @cached_property def __lowercase ( self ) -> List[str]: return XLMProphetNetTokenizer.from_pretrained('''microsoft/xprophetnet-large-wiki100-cased''' ) @slow def __lowercase ( self ) -> Tuple: _a : str = '''Hello World!''' _a : Tuple = [3_5_3_8_9, 6_6_7_2, 4_9, 2] self.assertListEqual(_a , self.big_tokenizer.encode(_a ) ) @slow def __lowercase ( self ) -> str: # fmt: off _a : str = {'''input_ids''': [[1_1_0_7_3, 8_2_7_8_3, 1_8, 2_6, 8_2_7_8_3, 5_4_9, 5_1_5_4_0, 2_4_8, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 2_1_5_1_8_6, 1_3_2_5, 1_4_7, 1_7_2_0_9, 1_3_0_1, 2_1_7, 2_0, 5_6_3_7_0, 5_3, 1_2_2_0_2_0, 2_0, 1_6_4_7_7, 2_7, 8_7_3_5_5, 4_5_4_8, 2_0, 4_7_2_8, 7_8_3_9_2, 1_7, 1_5_9_9_6_9, 1_8, 2_6, 2_4_4_9_1, 6_2_9, 1_5, 5_3_8, 2_2_7_0_4, 5_4_3_9, 1_5, 2_7_8_8, 2_4_4_9_1, 9_8_8_5, 1_5, 4_3_5_3_4, 6_0_5, 1_5, 8_1_4, 1_8_4_0_3, 3_3_2_0_0, 2_9, 1_5, 4_3_5_3_4, 2_4_4_5_8, 1_2_4_1_0, 1_1_1, 2_4_9_6_6, 8_3_6_6_9, 9_6_3_7, 1_4_4_0_6_8, 2_6, 8_5_0, 2_2_3_4_6, 2_7, 1_4_7, 2_4_9_6_6, 8_3_6_6_9, 8_3_4_9_0, 2_6, 3_9_1_1_3, 7_3_5, 2_7, 6_8_9, 6_5_6, 2_8_0_0, 1_3_3_9, 4_6_0_0, 5_3, 1_2_2_0_2_0, 1_1_5_7_8_5, 3_4, 8_1_6, 1_3_3_9, 4_6_8_8_7, 1_8, 1_4_7, 5_3_9_0_5, 1_9_5_1, 4_2_2_3_8, 4_1_1_7_0, 1_7_7_3_2, 8_3_4, 4_3_6, 1_5, 2_7_5_2_3, 9_8_7_3_3, 2_1_7, 1_4_7, 5_5_4_2, 4_9_8_1, 9_3_0, 1_7_3_4_7, 1_6, 2], [2_0_0_9_1, 6_2_9, 9_4, 8_2_7_8_6, 5_8, 4_9_0, 2_0, 1_5_2_8, 8_4, 5_3_9_0_5, 3_4_4, 8_0_5_9_2, 1_1_0_1_2_8, 1_8_8_2_2, 5_2_6_7, 1_3_0_6, 6_2, 1_5_2_5_3_7, 3_0_8, 7_9_9_7, 4_0_1, 1_2_4_4_2_7, 5_4_9, 3_5_4_4_2, 2_2_5, 1_0_9, 1_5_0_5_5, 2_5_7_4_8, 1_4_7, 7_1_1_9, 4_3_7_1_2, 3_4, 7_6_7, 1_3_5_3_6_6, 1_8, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5_9_2, 6_3_7_8_4, 1_1_9_4_6_6, 1_7, 1_4_7_8_0_8, 8_8_2_1_4, 1_8, 6_5_6, 8_1, 3_2, 3_2_9_6, 1_0_2_8_0, 1_6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a , model_name='''microsoft/xprophetnet-large-wiki100-cased''' , revision='''1acad1643ddd54a44df6a1b797ada8373685d90e''' , )
14
1
from typing import List, Optional, Tuple, Union import torch from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self , _a , _a ) -> Optional[Any]: super().__init__() # make sure scheduler can always be converted to DDIM _a : List[str] = DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=_a , scheduler=_a ) @torch.no_grad() def __call__( self , _a = 1 , _a = None , _a = 0.0 , _a = 5_0 , _a = None , _a = "pil" , _a = True , ) -> Union[ImagePipelineOutput, Tuple]: # Sample gaussian noise to begin loop if isinstance(self.unet.config.sample_size , _a ): _a : str = ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size, ) else: _a : Optional[int] = (batch_size, self.unet.config.in_channels, *self.unet.config.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.""" ) _a : int = randn_tensor(_a , generator=_a , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(_a ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output _a : Any = self.unet(_a , _a ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 _a : int = self.scheduler.step( _a , _a , _a , eta=_a , use_clipped_model_output=_a , generator=_a ).prev_sample _a : int = (image / 2 + 0.5).clamp(0 , 1 ) _a : Dict = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _a : List[str] = self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a )
14
import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Any = LxmertTokenizer UpperCAmelCase__ : Optional[Any] = LxmertTokenizerFast UpperCAmelCase__ : Any = True UpperCAmelCase__ : Dict = True def __lowercase ( self ) -> Union[str, Any]: super().setUp() _a : int = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _a : Any = 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 __lowercase ( self , _a ) -> List[str]: _a : Tuple = '''UNwant\u00E9d,running''' _a : str = '''unwanted, running''' return input_text, output_text def __lowercase ( self ) -> List[Any]: _a : str = self.tokenizer_class(self.vocab_file ) _a : str = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_a , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , [7, 4, 5, 1_0, 8, 9] ) def __lowercase ( self ) -> List[Any]: if not self.test_rust_tokenizer: return _a : Optional[Any] = self.get_tokenizer() _a : str = self.get_rust_tokenizer() _a : Optional[Any] = '''I was born in 92000, and this is falsé.''' _a : Optional[Any] = tokenizer.tokenize(_a ) _a : List[Any] = rust_tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _a : List[Any] = tokenizer.encode(_a , add_special_tokens=_a ) _a : Any = rust_tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Dict = self.get_rust_tokenizer() _a : Optional[int] = tokenizer.encode(_a ) _a : Dict = rust_tokenizer.encode(_a ) self.assertListEqual(_a , _a )
14
1
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = JukeboxTokenizer UpperCAmelCase__ : List[Any] = { "artist": "Zac Brown Band", "genres": "Country", "lyrics": "I met a traveller from an antique land,\n Who said \"Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ", } @require_torch def __lowercase ( self ) -> str: import torch _a : Optional[int] = JukeboxTokenizer.from_pretrained('''openai/jukebox-1b-lyrics''' ) _a : Union[str, Any] = tokenizer(**self.metas )['''input_ids'''] # fmt: off _a : str = [ torch.tensor([[ 0, 0, 0, 7_1_6_9, 5_0_7, 9, 7_6, 3_9, 3_1, 4_6, 7_6, 2_7, 7_6, 4_6, 4_4, 2_7, 4_8, 3_1, 3_8, 3_8, 3_1, 4_4, 7_6, 3_2, 4_4, 4_1, 3_9, 7_6, 2_7, 4_0, 7_6, 2_7, 4_0, 4_6, 3_5, 4_3, 4_7, 3_1, 7_6, 3_8, 2_7, 4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_3, 3_4, 4_1, 7_6, 4_5, 2_7, 3_5, 3_0, 7_6, 7_1, 2_0, 4_9, 4_1, 7_6, 4_8, 2_7, 4_5, 4_6, 7_6, 2_7, 4_0, 3_0, 7_6, 4_6, 4_4, 4_7, 4_0, 3_7, 3_8, 3_1, 4_5, 4_5, 7_6, 3_8, 3_1, 3_3, 4_5, 7_6, 4_1, 3_2, 7_6, 4_5, 4_6, 4_1, 4_0, 3_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_9, 4_6, 2_7, 4_0, 3_0, 7_6, 3_5, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_0, 3_1, 4_5, 3_1, 4_4, 4_6, 6_3, 7_6, 6_3, 7_6, 6_3, 7_6, 6_3, 7_6, 1_4, 3_1, 2_7, 4_4, 7_6, 4_6, 3_4, 3_1, 3_9, 6_4, 7_6, 4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 4_5, 2_7, 4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 8, 2_7, 3_8, 3_2, 7_6, 4_5, 4_7, 4_0, 3_7, 7_6, 2_7, 7_6, 4_5, 3_4, 2_7, 4_6, 4_6, 3_1, 4_4, 3_1, 3_0, 7_6, 4_8, 3_5, 4_5, 2_7, 3_3, 3_1, 7_6, 3_8, 3_5, 3_1, 4_5, 6_4, 7_6, 4_9, 3_4, 4_1, 4_5, 3_1, 7_6, 3_2, 4_4, 4_1, 4_9, 4_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1, 4_0, 3_0, 7_6, 4_9, 4_4, 3_5, 4_0, 3_7, 3_8, 3_1, 3_0, 7_6, 3_8, 3_5, 4_2, 6_4, 7_6, 2_7, 4_0, 3_0, 7_6, 4_5, 4_0, 3_1, 3_1, 4_4, 7_6, 4_1, 3_2, 7_6, 2_9, 4_1, 3_8, 3_0, 7_6, 2_9, 4_1, 3_9, 3_9, 2_7, 4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_0, 3_1, 3_8, 3_8, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_5, 4_6, 4_5, 7_6, 4_5, 2_9, 4_7, 3_8, 4_2, 4_6, 4_1, 4_4, 7_6, 4_9, 3_1, 3_8, 3_8, 7_6, 4_6, 3_4, 4_1, 4_5, 3_1, 7_6, 4_2, 2_7, 4_5, 4_5, 3_5, 4_1, 4_0, 4_5, 7_6, 4_4, 3_1, 2_7, 3_0, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_3, 3_4, 3_5, 2_9, 3_4, 7_6, 5_1, 3_1, 4_6, 7_6, 4_5, 4_7, 4_4, 4_8, 3_5, 4_8, 3_1, 6_4, 7_6, 4_5, 4_6, 2_7, 3_9, 4_2, 3_1, 3_0, 7_6, 4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 4_5, 3_1, 7_6, 3_8, 3_5, 3_2, 3_1, 3_8, 3_1, 4_5, 4_5, 7_6, 4_6, 3_4, 3_5, 4_0, 3_3, 4_5, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_0, 3_4, 3_1, 7_6, 3_4, 2_7, 4_0, 3_0, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_9, 4_1, 2_9, 3_7, 3_1, 3_0, 7_6, 4_6, 3_4, 3_1, 3_9, 6_4, 7_6, 2_7, 4_0, 3_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_4, 3_1, 2_7, 4_4, 4_6, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_2, 3_1, 3_0, 6_6, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1, 4_0, 3_0, 7_6, 4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 4_2, 3_1, 3_0, 3_1, 4_5, 4_6, 2_7, 3_8, 6_4, 7_6, 4_6, 3_4, 3_1, 4_5, 3_1, 7_6, 4_9, 4_1, 4_4, 3_0, 4_5, 7_6, 2_7, 4_2, 4_2, 3_1, 2_7, 4_4, 6_5, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_3, 5_1, 7_6, 4_0, 2_7, 3_9, 3_1, 7_6, 3_5, 4_5, 7_6, 1_5, 5_2, 5_1, 3_9, 2_7, 4_0, 3_0, 3_5, 2_7, 4_5, 6_4, 7_6, 1_1, 3_5, 4_0, 3_3, 7_6, 4_1, 3_2, 7_6, 1_1, 3_5, 4_0, 3_3, 4_5, 6_6, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_2, 4_1, 4_1, 3_7, 7_6, 4_1, 4_0, 7_6, 3_9, 5_1, 7_6, 2_3, 4_1, 4_4, 3_7, 4_5, 6_4, 7_6, 5_1, 3_1, 7_6, 1_3, 3_5, 3_3, 3_4, 4_6, 5_1, 6_4, 7_6, 2_7, 4_0, 3_0, 7_6, 3_0, 3_1, 4_5, 4_2, 2_7, 3_5, 4_4, 6_7, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_4, 4_1, 4_6, 3_4, 3_5, 4_0, 3_3, 7_6, 2_8, 3_1, 4_5, 3_5, 3_0, 3_1, 7_6, 4_4, 3_1, 3_9, 2_7, 3_5, 4_0, 4_5, 6_3, 7_6, 1_8, 4_1, 4_7, 4_0, 3_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_0, 3_1, 2_9, 2_7, 5_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_5, 3_2, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 2_9, 4_1, 3_8, 4_1, 4_5, 4_5, 2_7, 3_8, 7_6, 2_3, 4_4, 3_1, 2_9, 3_7, 6_4, 7_6, 2_8, 4_1, 4_7, 4_0, 3_0, 3_8, 3_1, 4_5, 4_5, 7_6, 2_7, 4_0, 3_0, 7_6, 2_8, 2_7, 4_4, 3_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_0, 3_4, 3_1, 7_6, 3_8, 4_1, 4_0, 3_1, 7_6, 2_7, 4_0, 3_0, 7_6, 3_8, 3_1, 4_8, 3_1, 3_8, 7_6, 4_5, 2_7, 4_0, 3_0, 4_5, 7_6, 4_5, 4_6, 4_4, 3_1, 4_6, 2_9, 3_4, 7_6, 3_2, 2_7, 4_4, 7_6, 2_7, 4_9, 2_7, 5_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def __lowercase ( self ) -> Union[str, Any]: import torch _a : List[str] = JukeboxTokenizer.from_pretrained('''openai/jukebox-5b-lyrics''' ) _a : List[Any] = tokenizer(**self.metas )['''input_ids'''] # fmt: off _a : Optional[int] = [ torch.tensor([[ 0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1, 9, 7_7, 3_9, 3_1, 4_6, 7_7, 2_7, 7_7, 4_6, 4_4, 2_7, 4_8, 3_1, 3_8, 3_8, 3_1, 4_4, 7_7, 3_2, 4_4, 4_1, 3_9, 7_7, 2_7, 4_0, 7_7, 2_7, 4_0, 4_6, 3_5, 4_3, 4_7, 3_1, 7_7, 3_8, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_3, 3_4, 4_1, 7_7, 4_5, 2_7, 3_5, 3_0, 7_7, 7_2, 2_0, 4_9, 4_1, 7_7, 4_8, 2_7, 4_5, 4_6, 7_7, 2_7, 4_0, 3_0, 7_7, 4_6, 4_4, 4_7, 4_0, 3_7, 3_8, 3_1, 4_5, 4_5, 7_7, 3_8, 3_1, 3_3, 4_5, 7_7, 4_1, 3_2, 7_7, 4_5, 4_6, 4_1, 4_0, 3_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_9, 4_6, 2_7, 4_0, 3_0, 7_7, 3_5, 4_0, 7_7, 4_6, 3_4, 3_1, 7_7, 3_0, 3_1, 4_5, 3_1, 4_4, 4_6, 6_3, 7_7, 6_3, 7_7, 6_3, 7_7, 6_3, 7_7, 1_4, 3_1, 2_7, 4_4, 7_7, 4_6, 3_4, 3_1, 3_9, 6_4, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 7_7, 4_5, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 8, 2_7, 3_8, 3_2, 7_7, 4_5, 4_7, 4_0, 3_7, 7_7, 2_7, 7_7, 4_5, 3_4, 2_7, 4_6, 4_6, 3_1, 4_4, 3_1, 3_0, 7_7, 4_8, 3_5, 4_5, 2_7, 3_3, 3_1, 7_7, 3_8, 3_5, 3_1, 4_5, 6_4, 7_7, 4_9, 3_4, 4_1, 4_5, 3_1, 7_7, 3_2, 4_4, 4_1, 4_9, 4_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1, 4_0, 3_0, 7_7, 4_9, 4_4, 3_5, 4_0, 3_7, 3_8, 3_1, 3_0, 7_7, 3_8, 3_5, 4_2, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 4_5, 4_0, 3_1, 3_1, 4_4, 7_7, 4_1, 3_2, 7_7, 2_9, 4_1, 3_8, 3_0, 7_7, 2_9, 4_1, 3_9, 3_9, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_0, 3_1, 3_8, 3_8, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 3_5, 4_6, 4_5, 7_7, 4_5, 2_9, 4_7, 3_8, 4_2, 4_6, 4_1, 4_4, 7_7, 4_9, 3_1, 3_8, 3_8, 7_7, 4_6, 3_4, 4_1, 4_5, 3_1, 7_7, 4_2, 2_7, 4_5, 4_5, 3_5, 4_1, 4_0, 4_5, 7_7, 4_4, 3_1, 2_7, 3_0, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_3, 3_4, 3_5, 2_9, 3_4, 7_7, 5_1, 3_1, 4_6, 7_7, 4_5, 4_7, 4_4, 4_8, 3_5, 4_8, 3_1, 6_4, 7_7, 4_5, 4_6, 2_7, 3_9, 4_2, 3_1, 3_0, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 4_5, 3_1, 7_7, 3_8, 3_5, 3_2, 3_1, 3_8, 3_1, 4_5, 4_5, 7_7, 4_6, 3_4, 3_5, 4_0, 3_3, 4_5, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_0, 3_4, 3_1, 7_7, 3_4, 2_7, 4_0, 3_0, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 3_9, 4_1, 2_9, 3_7, 3_1, 3_0, 7_7, 4_6, 3_4, 3_1, 3_9, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 4_6, 3_4, 3_1, 7_7, 3_4, 3_1, 2_7, 4_4, 4_6, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 3_2, 3_1, 3_0, 6_6, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1, 4_0, 3_0, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 7_7, 4_2, 3_1, 3_0, 3_1, 4_5, 4_6, 2_7, 3_8, 6_4, 7_7, 4_6, 3_4, 3_1, 4_5, 3_1, 7_7, 4_9, 4_1, 4_4, 3_0, 4_5, 7_7, 2_7, 4_2, 4_2, 3_1, 2_7, 4_4, 6_5, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_3, 5_1, 7_7, 4_0, 2_7, 3_9, 3_1, 7_7, 3_5, 4_5, 7_7, 1_5, 5_2, 5_1, 3_9, 2_7, 4_0, 3_0, 3_5, 2_7, 4_5, 6_4, 7_7, 1_1, 3_5, 4_0, 3_3, 7_7, 4_1, 3_2, 7_7, 1_1, 3_5, 4_0, 3_3, 4_5, 6_6, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_2, 4_1, 4_1, 3_7, 7_7, 4_1, 4_0, 7_7, 3_9, 5_1, 7_7, 2_3, 4_1, 4_4, 3_7, 4_5, 6_4, 7_7, 5_1, 3_1, 7_7, 1_3, 3_5, 3_3, 3_4, 4_6, 5_1, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 3_0, 3_1, 4_5, 4_2, 2_7, 3_5, 4_4, 6_7, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_4, 4_1, 4_6, 3_4, 3_5, 4_0, 3_3, 7_7, 2_8, 3_1, 4_5, 3_5, 3_0, 3_1, 7_7, 4_4, 3_1, 3_9, 2_7, 3_5, 4_0, 4_5, 6_3, 7_7, 1_8, 4_1, 4_7, 4_0, 3_0, 7_7, 4_6, 3_4, 3_1, 7_7, 3_0, 3_1, 2_9, 2_7, 5_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_5, 3_2, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 2_9, 4_1, 3_8, 4_1, 4_5, 4_5, 2_7, 3_8, 7_7, 2_3, 4_4, 3_1, 2_9, 3_7, 6_4, 7_7, 2_8, 4_1, 4_7, 4_0, 3_0, 3_8, 3_1, 4_5, 4_5, 7_7, 2_7, 4_0, 3_0, 7_7, 2_8, 2_7, 4_4, 3_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_0, 3_4, 3_1, 7_7, 3_8, 4_1, 4_0, 3_1, 7_7, 2_7, 4_0, 3_0, 7_7, 3_8, 3_1, 4_8, 3_1, 3_8, 7_7, 4_5, 2_7, 4_0, 3_0, 4_5, 7_7, 4_5, 4_6, 4_4, 3_1, 4_6, 2_9, 3_4, 7_7, 3_2, 2_7, 4_4, 7_7, 2_7, 4_9, 2_7, 5_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
14
import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> int: _a : Dict = '''ZinengTang/tvlt-base''' _a : List[str] = tempfile.mkdtemp() def __lowercase ( self , **_a ) -> int: return TvltImageProcessor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self , **_a ) -> List[Any]: return TvltFeatureExtractor.from_pretrained(self.checkpoint , **_a ) def __lowercase ( self ) -> Optional[int]: shutil.rmtree(self.tmpdirname ) def __lowercase ( self ) -> Dict: _a : Union[str, Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Optional[int] = TvltProcessor(image_processor=_a , feature_extractor=_a ) processor.save_pretrained(self.tmpdirname ) _a : Any = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor , _a ) self.assertIsInstance(processor.image_processor , _a ) def __lowercase ( self ) -> Any: _a : Optional[Any] = self.get_image_processor() _a : Dict = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : Union[str, Any] = np.ones([1_2_0_0_0] ) _a : Dict = feature_extractor(_a , return_tensors='''np''' ) _a : Tuple = processor(audio=_a , return_tensors='''np''' ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> int: _a : Optional[Any] = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Optional[Any] = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[Any] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = image_processor(_a , return_tensors='''np''' ) _a : Optional[int] = processor(images=_a , return_tensors='''np''' ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1e-2 ) def __lowercase ( self ) -> Union[str, Any]: _a : int = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Any = TvltProcessor(image_processor=_a , feature_extractor=_a ) _a : List[str] = np.ones([1_2_0_0_0] ) _a : Optional[int] = np.ones([3, 2_2_4, 2_2_4] ) _a : int = processor(audio=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __lowercase ( self ) -> Union[str, Any]: _a : str = self.get_image_processor() _a : Union[str, Any] = self.get_feature_extractor() _a : Dict = TvltProcessor(image_processor=_a , feature_extractor=_a ) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' , )
14
1
import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder a__ = '''__DUMMY_TRANSFORMERS_USER__''' a__ = '''Dummy User''' a__ = '''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''' a__ = '''https://hub-ci.huggingface.co''' a__ = CI_HUB_ENDPOINT + '''/datasets/{repo_id}/resolve/{revision}/{path}''' a__ = CI_HUB_ENDPOINT + '''/{repo_id}/resolve/{revision}/{filename}''' a__ = Path('''~/.huggingface/hub_ci_token''').expanduser() @pytest.fixture def __UpperCAmelCase ( __a : Tuple ) -> Dict: """simple docstring""" monkeypatch.setattr( '''huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE''' ,__a ) @pytest.fixture def __UpperCAmelCase ( __a : Dict ) -> List[str]: """simple docstring""" monkeypatch.setattr('''datasets.config.HF_ENDPOINT''' ,__a ) monkeypatch.setattr('''datasets.config.HUB_DATASETS_URL''' ,__a ) @pytest.fixture def __UpperCAmelCase ( __a : Optional[int] ) -> Union[str, Any]: """simple docstring""" monkeypatch.setattr('''huggingface_hub.hf_api.HfFolder.path_token''' ,__a ) @pytest.fixture def __UpperCAmelCase ( __a : Optional[int] ,__a : Dict ) -> Union[str, Any]: """simple docstring""" HfFolder.save_token(__a ) yield HfFolder.delete_token() @pytest.fixture(scope='''session''' ) def __UpperCAmelCase ( ) -> str: """simple docstring""" return HfApi(endpoint=__a ) @pytest.fixture(scope='''session''' ) def __UpperCAmelCase ( __a : HfApi ) -> List[Any]: """simple docstring""" _a : Optional[int] = HfFolder.get_token() HfFolder.save_token(__a ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(__a ) @pytest.fixture def __UpperCAmelCase ( __a : Optional[int] ) -> Dict: """simple docstring""" def _cleanup_repo(__a : int ): hf_api.delete_repo(__a ,token=__a ,repo_type='''dataset''' ) return _cleanup_repo @pytest.fixture def __UpperCAmelCase ( __a : List[str] ) -> int: """simple docstring""" @contextmanager def _temporary_repo(__a : Optional[Any] ): try: yield repo_id finally: cleanup_repo(__a ) return _temporary_repo @pytest.fixture(scope='''session''' ) def __UpperCAmelCase ( __a : HfApi ,__a : str ,__a : int ) -> Dict: """simple docstring""" _a : List[str] = F"""repo_txt_data-{int(time.time() * 10E3 )}""" _a : List[Any] = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__a ,token=__a ,repo_type='''dataset''' ,private=__a ) hf_api.upload_file( token=__a ,path_or_fileobj=str(__a ) ,path_in_repo='''data/text_data.txt''' ,repo_id=__a ,repo_type='''dataset''' ,) yield repo_id try: hf_api.delete_repo(__a ,token=__a ,repo_type='''dataset''' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __UpperCAmelCase ( __a : int ,__a : List[Any] ,__a : Dict ) -> List[Any]: """simple docstring""" return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope='''session''' ) def __UpperCAmelCase ( __a : HfApi ,__a : str ,__a : Tuple ) -> Dict: """simple docstring""" _a : List[str] = F"""repo_zipped_txt_data-{int(time.time() * 10E3 )}""" _a : Optional[int] = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__a ,token=__a ,repo_type='''dataset''' ,private=__a ) hf_api.upload_file( token=__a ,path_or_fileobj=str(__a ) ,path_in_repo='''data.zip''' ,repo_id=__a ,repo_type='''dataset''' ,) yield repo_id try: hf_api.delete_repo(__a ,token=__a ,repo_type='''dataset''' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __UpperCAmelCase ( __a : Optional[Any] ,__a : Tuple ,__a : Union[str, Any] ) -> Tuple: """simple docstring""" return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope='''session''' ) def __UpperCAmelCase ( __a : HfApi ,__a : Any ,__a : Dict ) -> str: """simple docstring""" _a : Dict = F"""repo_zipped_img_data-{int(time.time() * 10E3 )}""" _a : str = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(__a ,token=__a ,repo_type='''dataset''' ,private=__a ) hf_api.upload_file( token=__a ,path_or_fileobj=str(__a ) ,path_in_repo='''data.zip''' ,repo_id=__a ,repo_type='''dataset''' ,) yield repo_id try: hf_api.delete_repo(__a ,token=__a ,repo_type='''dataset''' ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : List[str] ) -> Optional[int]: """simple docstring""" return hf_private_dataset_repo_zipped_img_data_
14
def __UpperCAmelCase ( __a : str ) -> list: """simple docstring""" if n_term == "": return [] _a : list = [] for temp in range(int(__a ) ): series.append(F"""1/{temp + 1}""" if series else '''1''' ) return series if __name__ == "__main__": a__ = 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))
14
1
import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __init__( self ) -> Any: _a : Tuple = [] def __lowercase ( self , _a , _a , _a , **_a ) -> List[Any]: self.events.append('''on_init_end''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> int: self.events.append('''on_train_begin''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> Optional[int]: self.events.append('''on_train_end''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> int: self.events.append('''on_epoch_begin''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> Optional[int]: self.events.append('''on_epoch_end''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> Optional[Any]: self.events.append('''on_step_begin''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> Optional[Any]: self.events.append('''on_step_end''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> Dict: self.events.append('''on_evaluate''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> Dict: self.events.append('''on_predict''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> List[Any]: self.events.append('''on_save''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> Dict: self.events.append('''on_log''' ) def __lowercase ( self , _a , _a , _a , **_a ) -> int: self.events.append('''on_prediction_step''' ) @require_torch class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> Dict: _a : str = tempfile.mkdtemp() def __lowercase ( self ) -> Dict: shutil.rmtree(self.output_dir ) def __lowercase ( self , _a=0 , _a=0 , _a=6_4 , _a=6_4 , _a=None , _a=False , **_a ) -> str: # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. _a : List[Any] = RegressionDataset(length=_a ) _a : Any = RegressionDataset(length=_a ) _a : List[Any] = RegressionModelConfig(a=_a , b=_a ) _a : str = RegressionPreTrainedModel(_a ) _a : List[str] = TrainingArguments(self.output_dir , disable_tqdm=_a , report_to=[] , **_a ) return Trainer( _a , _a , train_dataset=_a , eval_dataset=_a , callbacks=_a , ) def __lowercase ( self , _a , _a ) -> Dict: self.assertEqual(len(_a ) , len(_a ) ) # Order doesn't matter _a : Optional[Any] = sorted(_a , key=lambda _a : cb.__name__ if isinstance(_a , _a ) else cb.__class__.__name__ ) _a : Union[str, Any] = sorted(_a , key=lambda _a : cb.__name__ if isinstance(_a , _a ) else cb.__class__.__name__ ) for cba, cba in zip(_a , _a ): if isinstance(_a , _a ) and isinstance(_a , _a ): self.assertEqual(_a , _a ) elif isinstance(_a , _a ) and not isinstance(_a , _a ): self.assertEqual(_a , cba.__class__ ) elif not isinstance(_a , _a ) and isinstance(_a , _a ): self.assertEqual(cba.__class__ , _a ) else: self.assertEqual(_a , _a ) def __lowercase ( self , _a ) -> Any: _a : Optional[Any] = ['''on_init_end''', '''on_train_begin'''] _a : str = 0 _a : Dict = len(trainer.get_eval_dataloader() ) _a : List[str] = ['''on_prediction_step'''] * len(trainer.get_eval_dataloader() ) + ['''on_log''', '''on_evaluate'''] for _ in range(trainer.state.num_train_epochs ): expected_events.append('''on_epoch_begin''' ) for _ in range(_a ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append('''on_log''' ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append('''on_save''' ) expected_events.append('''on_epoch_end''' ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def __lowercase ( self ) -> Tuple: _a : Dict = self.get_trainer() _a : str = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) # Callbacks passed at init are added to the default callbacks _a : List[Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(_a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback _a : List[str] = self.get_trainer(disable_tqdm=_a ) _a : Optional[Any] = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) def __lowercase ( self ) -> str: _a : List[str] = DEFAULT_CALLBACKS.copy() + [ProgressCallback] _a : int = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(_a ) expected_callbacks.remove(_a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) _a : str = self.get_trainer() _a : Union[str, Any] = trainer.pop_callback(_a ) self.assertEqual(cb.__class__ , _a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) trainer.add_callback(_a ) expected_callbacks.insert(0 , _a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) # We can also add, pop, or remove by instance _a : List[Any] = self.get_trainer() _a : Union[str, Any] = trainer.callback_handler.callbacks[0] trainer.remove_callback(_a ) expected_callbacks.remove(_a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) _a : Tuple = self.get_trainer() _a : List[Any] = trainer.callback_handler.callbacks[0] _a : Union[str, Any] = trainer.pop_callback(_a ) self.assertEqual(_a , _a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) trainer.add_callback(_a ) expected_callbacks.insert(0 , _a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , _a ) def __lowercase ( self ) -> Any: import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action='''ignore''' , category=_a ) _a : Union[str, Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() _a : Dict = trainer.callback_handler.callbacks[-2].events self.assertEqual(_a , self.get_expected_events(_a ) ) # Independent log/save/eval _a : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() _a : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(_a , self.get_expected_events(_a ) ) _a : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() _a : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(_a , self.get_expected_events(_a ) ) _a : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy='''steps''' ) trainer.train() _a : Union[str, Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(_a , self.get_expected_events(_a ) ) _a : Optional[Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy='''epoch''' ) trainer.train() _a : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(_a , self.get_expected_events(_a ) ) # A bit of everything _a : Optional[Any] = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=1_0 , eval_steps=5 , evaluation_strategy='''steps''' , ) trainer.train() _a : str = trainer.callback_handler.callbacks[-2].events self.assertEqual(_a , self.get_expected_events(_a ) ) # warning should be emitted for duplicated callbacks with patch('''transformers.trainer_callback.logger.warning''' ) as warn_mock: _a : int = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(_a ) in warn_mock.call_args[0][0]
14
import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Optional[int] ) -> Dict: """simple docstring""" return params[F"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :] def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[int] ,__a : int ,__a : List[str]="attention" ) -> List[str]: """simple docstring""" _a : str = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :] ) _a : Tuple = k_tmp.reshape(k_tmp.shape[0] ,k_tmp.shape[1] * k_tmp.shape[2] ) _a : Any = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :] ) _a : Dict = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] ,o_tmp.shape[2] ) _a : Union[str, Any] = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :] ) _a : Any = q_tmp.reshape(q_tmp.shape[0] ,q_tmp.shape[1] * q_tmp.shape[2] ) _a : Tuple = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :] ) _a : int = v_tmp.reshape(v_tmp.shape[0] ,v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def __UpperCAmelCase ( __a : Union[str, Any] ,__a : Union[str, Any] ,__a : List[Any] ,__a : Any=False ) -> Any: """simple docstring""" if split_mlp_wi: _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :] _a : Union[str, Any] = params[F"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :] _a : List[str] = (wi_a, wi_a) else: _a : List[str] = params[F"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :] _a : Optional[int] = params[F"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :] return wi, wo def __UpperCAmelCase ( __a : List[Any] ,__a : Optional[Any] ,__a : Union[str, Any] ,__a : str ) -> List[str]: """simple docstring""" return params[F"""{prefix}/{prefix}/{layer_name}/scale"""][:, i] def __UpperCAmelCase ( __a : dict ,*, __a : int ,__a : bool ,__a : bool = False ) -> Any: """simple docstring""" _a : Dict = traverse_util.flatten_dict(variables['''target'''] ) _a : Any = {'''/'''.join(__a ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi _a : Optional[int] = '''encoder/encoder/mlp/wi_0/kernel''' in old print('''Split MLP:''' ,__a ) _a : Tuple = collections.OrderedDict() # Shared embeddings. _a : Any = old['''token_embedder/embedding'''] # Encoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Optional[Any] = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_attention_layer_norm''' ) _a , _a , _a , _a : List[str] = tax_attention_lookup(__a ,__a ,'''encoder''' ,'''attention''' ) _a : List[str] = layer_norm _a : Optional[Any] = k.T _a : str = o.T _a : List[Any] = q.T _a : Tuple = v.T # Block i, layer 1 (MLP). _a : str = tax_layer_norm_lookup(__a ,__a ,'''encoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Any = tax_mlp_lookup(__a ,__a ,'''encoder''' ,__a ) _a : str = layer_norm if split_mlp_wi: _a : List[Any] = wi[0].T _a : Any = wi[1].T else: _a : Any = wi.T _a : Optional[Any] = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Dict = tax_relpos_bias_lookup( __a ,__a ,'''encoder''' ).T _a : List[str] = old['''encoder/encoder_norm/scale'''] if not scalable_attention: _a : List[Any] = tax_relpos_bias_lookup( __a ,0 ,'''encoder''' ).T _a : Optional[Any] = tax_relpos_bias_lookup( __a ,0 ,'''decoder''' ).T if not is_encoder_only: # Decoder. for i in range(__a ): # Block i, layer 0 (Self Attention). _a : Union[str, Any] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_self_attention_layer_norm''' ) _a , _a , _a , _a : Optional[Any] = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''self_attention''' ) _a : Optional[Any] = layer_norm _a : Dict = k.T _a : str = o.T _a : str = q.T _a : List[str] = v.T # Block i, layer 1 (Cross Attention). _a : Any = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_cross_attention_layer_norm''' ) _a , _a , _a , _a : str = tax_attention_lookup(__a ,__a ,'''decoder''' ,'''encoder_decoder_attention''' ) _a : Optional[Any] = layer_norm _a : Optional[int] = k.T _a : Dict = o.T _a : str = q.T _a : int = v.T # Block i, layer 2 (MLP). _a : Optional[int] = tax_layer_norm_lookup(__a ,__a ,'''decoder''' ,'''pre_mlp_layer_norm''' ) _a , _a : Tuple = tax_mlp_lookup(__a ,__a ,'''decoder''' ,__a ) _a : Optional[Any] = layer_norm if split_mlp_wi: _a : List[str] = wi[0].T _a : List[Any] = wi[1].T else: _a : Dict = wi.T _a : str = wo.T if scalable_attention: # convert the rel_embedding of each layer _a : Tuple = tax_relpos_bias_lookup(__a ,__a ,'''decoder''' ).T _a : Tuple = old['''decoder/decoder_norm/scale'''] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: _a : Any = old['''decoder/logits_dense/kernel'''].T return new def __UpperCAmelCase ( __a : Dict ,__a : bool ) -> Tuple: """simple docstring""" _a : Tuple = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: _a : Any = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: _a : Optional[int] = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''' ) _a : str = state_dict['''shared.weight'''] return state_dict def __UpperCAmelCase ( __a : List[str] ,__a : Union[str, Any] ,__a : Dict ,__a : Union[str, Any] ,__a : List[Any] ) -> int: """simple docstring""" _a : List[str] = checkpoints.load_tax_checkpoint(__a ) _a : str = convert_tax_to_pytorch( __a ,num_layers=config.num_layers ,is_encoder_only=__a ,scalable_attention=__a ) _a : str = make_state_dict(__a ,__a ) model.load_state_dict(__a ,strict=__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : Any ,__a : Union[str, Any] ,__a : bool = False ,__a : bool = False ,) -> Optional[Any]: """simple docstring""" _a : List[str] = MTaConfig.from_json_file(__a ) print(F"""Building PyTorch model from configuration: {config}""" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: _a : Any = UMTaEncoderModel(__a ) else: _a : Tuple = UMTaForConditionalGeneration(__a ) # Load weights from tf checkpoint load_tax_weights_in_ta(__a ,__a ,__a ,__a ,__a ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(__a ) # Verify that we can load the checkpoint. model.from_pretrained(__a ) print('''Done''' ) if __name__ == "__main__": a__ = argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''') # Required parameters parser.add_argument( '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the pre-trained T5 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.''' ) parser.add_argument( '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False ) parser.add_argument( '''--scalable_attention''', action='''store_true''', help='''Whether the model uses scaled attention (umt5 model)''', default=False, ) a__ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
14
1
import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaInpaintPipeline, 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 UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Tuple = KandinskyVaaInpaintPipeline UpperCAmelCase__ : List[str] = ["image_embeds", "negative_image_embeds", "image", "mask_image"] UpperCAmelCase__ : List[str] = [ "image_embeds", "negative_image_embeds", "image", "mask_image", ] UpperCAmelCase__ : int = [ "generator", "height", "width", "latents", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase__ : Dict = False @property def __lowercase ( self ) -> List[Any]: return 3_2 @property def __lowercase ( self ) -> List[str]: return 3_2 @property def __lowercase ( self ) -> Optional[Any]: return self.time_input_dim @property def __lowercase ( self ) -> Optional[int]: return self.time_input_dim * 4 @property def __lowercase ( self ) -> List[Any]: return 1_0_0 @property def __lowercase ( self ) -> Optional[int]: torch.manual_seed(0 ) _a : Optional[int] = { '''in_channels''': 9, # 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 : Optional[Any] = UNetaDConditionModel(**_a ) return model @property def __lowercase ( self ) -> Tuple: return { "block_out_channels": [3_2, 6_4], "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": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __lowercase ( self ) -> List[str]: torch.manual_seed(0 ) _a : Optional[Any] = VQModel(**self.dummy_movq_kwargs ) return model def __lowercase ( self ) -> Optional[int]: _a : List[str] = self.dummy_unet _a : List[Any] = self.dummy_movq _a : Union[str, Any] = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''linear''' , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=_a , set_alpha_to_one=_a , steps_offset=1 , prediction_type='''epsilon''' , thresholding=_a , ) _a : int = { '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __lowercase ( self , _a , _a=0 ) -> str: _a : List[str] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_a ) ).to(_a ) _a : int = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _a ) # create init_image _a : Union[str, Any] = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(_a ) ).to(_a ) _a : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] _a : Any = Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) # create mask _a : Optional[int] = np.ones((6_4, 6_4) , dtype=np.floataa ) _a : Dict = 0 if str(_a ).startswith('''mps''' ): _a : List[str] = torch.manual_seed(_a ) else: _a : Optional[int] = torch.Generator(device=_a ).manual_seed(_a ) _a : List[str] = { '''image''': init_image, '''mask_image''': mask, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 2, '''guidance_scale''': 4.0, '''output_type''': '''np''', } return inputs def __lowercase ( self ) -> Union[str, Any]: _a : str = '''cpu''' _a : Union[str, Any] = self.get_dummy_components() _a : Optional[int] = self.pipeline_class(**_a ) _a : Optional[Any] = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _a : Optional[Any] = pipe(**self.get_dummy_inputs(_a ) ) _a : Optional[Any] = output.images _a : Any = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _a : Dict = image[0, -3:, -3:, -1] _a : Optional[int] = image_from_tuple[0, -3:, -3:, -1] print(F"""image.shape {image.shape}""" ) assert image.shape == (1, 6_4, 6_4, 3) _a : int = np.array( [0.5077_5903, 0.4952_7195, 0.4882_4543, 0.5019_2237, 0.4864_4906, 0.4937_3814, 0.478_0598, 0.4723_4827, 0.4832_7848] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def __lowercase ( self ) -> Union[str, Any]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" 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]: _a : List[str] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy''' ) _a : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _a : Dict = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) _a : int = 0 _a : Dict = '''a hat''' _a : Dict = KandinskyVaaPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _a : Union[str, Any] = KandinskyVaaInpaintPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-decoder-inpaint''' , torch_dtype=torch.floataa ) _a : Union[str, Any] = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _a : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) _a , _a : Union[str, Any] = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _a : Union[str, Any] = pipeline( image=_a , mask_image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type='''np''' , ) _a : Tuple = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(_a , _a )
14
import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap a__ = '''Usage of script: script_name <size_of_canvas:int>''' a__ = [0] * 100 + [1] * 10 random.shuffle(choice) def __UpperCAmelCase ( __a : int ) -> list[list[bool]]: """simple docstring""" _a : int = [[False for i in range(__a )] for j in range(__a )] return canvas def __UpperCAmelCase ( __a : list[list[bool]] ) -> None: """simple docstring""" for i, row in enumerate(__a ): for j, _ in enumerate(__a ): _a : Optional[int] = bool(random.getrandbits(1 ) ) def __UpperCAmelCase ( __a : list[list[bool]] ) -> list[list[bool]]: """simple docstring""" _a : Any = np.array(__a ) _a : Optional[int] = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(__a ): for c, pt in enumerate(__a ): _a : Tuple = __judge_point( __a ,current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) _a : List[str] = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. _a : list[list[bool]] = current_canvas.tolist() return return_canvas def __UpperCAmelCase ( __a : bool ,__a : list[list[bool]] ) -> bool: """simple docstring""" _a : Optional[Any] = 0 _a : str = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. _a : Optional[int] = pt if pt: if alive < 2: _a : Dict = False elif alive == 2 or alive == 3: _a : Optional[Any] = True elif alive > 3: _a : str = False else: if alive == 3: _a : int = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) a__ = int(sys.argv[1]) # main working structure of this module. a__ = create_canvas(canvas_size) seed(c) a__ , a__ = plt.subplots() fig.show() a__ = ListedColormap(['''w''', '''k''']) try: while True: a__ = run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
14
1
from ...processing_utils import ProcessorMixin class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = "SpeechT5FeatureExtractor" UpperCAmelCase__ : Tuple = "SpeechT5Tokenizer" def __init__( self , _a , _a ) -> List[str]: super().__init__(_a , _a ) def __call__( self , *_a , **_a ) -> Dict: _a : Union[str, Any] = kwargs.pop('''audio''' , _a ) _a : Tuple = kwargs.pop('''text''' , _a ) _a : Union[str, Any] = kwargs.pop('''text_target''' , _a ) _a : Dict = kwargs.pop('''audio_target''' , _a ) _a : Union[str, Any] = kwargs.pop('''sampling_rate''' , _a ) if audio is not None and text is not None: raise ValueError( '''Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?''' ) if audio_target is not None and text_target is not None: raise ValueError( '''Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?''' ) if audio is None and audio_target is None and text is None and text_target is None: raise ValueError( '''You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.''' ) if audio is not None: _a : int = self.feature_extractor(_a , *_a , sampling_rate=_a , **_a ) elif text is not None: _a : Dict = self.tokenizer(_a , **_a ) else: _a : Tuple = None if audio_target is not None: _a : Optional[int] = self.feature_extractor(audio_target=_a , *_a , sampling_rate=_a , **_a ) _a : str = targets['''input_values'''] elif text_target is not None: _a : int = self.tokenizer(_a , **_a ) _a : Optional[Any] = targets['''input_ids'''] else: _a : Any = None if inputs is None: return targets if targets is not None: _a : int = labels _a : List[str] = targets.get('''attention_mask''' ) if decoder_attention_mask is not None: _a : Optional[Any] = decoder_attention_mask return inputs def __lowercase ( self , *_a , **_a ) -> Union[str, Any]: _a : str = kwargs.pop('''input_values''' , _a ) _a : Optional[Any] = kwargs.pop('''input_ids''' , _a ) _a : List[str] = kwargs.pop('''labels''' , _a ) if input_values is not None and input_ids is not None: raise ValueError('''Cannot process both `input_values` and `input_ids` inputs.''' ) if input_values is None and input_ids is None and labels is None: raise ValueError( '''You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.''' ) if input_values is not None: _a : int = self.feature_extractor.pad(_a , *_a , **_a ) elif input_ids is not None: _a : List[Any] = self.tokenizer.pad(_a , **_a ) else: _a : Tuple = None if labels is not None: if "input_ids" in labels or (isinstance(_a , _a ) and "input_ids" in labels[0]): _a : List[Any] = self.tokenizer.pad(_a , **_a ) _a : Any = targets['''input_ids'''] else: _a : Union[str, Any] = self.feature_extractor.feature_size _a : Any = self.feature_extractor.num_mel_bins _a : Optional[int] = self.feature_extractor.pad(_a , *_a , **_a ) _a : Optional[int] = feature_size_hack _a : Dict = targets['''input_values'''] else: _a : Dict = None if inputs is None: return targets if targets is not None: _a : Dict = labels _a : Union[str, Any] = targets.get('''attention_mask''' ) if decoder_attention_mask is not None: _a : Any = decoder_attention_mask return inputs def __lowercase ( self , *_a , **_a ) -> List[Any]: return self.tokenizer.batch_decode(*_a , **_a ) def __lowercase ( self , *_a , **_a ) -> Optional[Any]: return self.tokenizer.decode(*_a , **_a )
14
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''', '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''', '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''', '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''', '''funnel-transformer/intermediate''': ( '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json''' ), '''funnel-transformer/intermediate-base''': ( '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json''' ), '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''', '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''', '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''', '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''', } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = "funnel" UpperCAmelCase__ : Tuple = { "hidden_size": "d_model", "num_attention_heads": "n_head", } def __init__( self , _a=3_0_5_2_2 , _a=[4, 4, 4] , _a=None , _a=2 , _a=7_6_8 , _a=1_2 , _a=6_4 , _a=3_0_7_2 , _a="gelu_new" , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=None , _a=1e-9 , _a="mean" , _a="relative_shift" , _a=True , _a=True , _a=True , **_a , ) -> List[Any]: _a : Optional[int] = vocab_size _a : Dict = block_sizes _a : Optional[int] = [1] * len(_a ) if block_repeats is None else block_repeats assert len(_a ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." _a : int = num_decoder_layers _a : List[str] = d_model _a : Optional[Any] = n_head _a : Tuple = d_head _a : Dict = d_inner _a : List[str] = hidden_act _a : int = hidden_dropout _a : Union[str, Any] = attention_dropout _a : Tuple = activation_dropout _a : Optional[Any] = initializer_range _a : Dict = initializer_std _a : Union[str, Any] = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported.""" _a : Any = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported.""" _a : Optional[Any] = attention_type _a : int = separate_cls _a : Tuple = truncate_seq _a : List[Any] = pool_q_only super().__init__(**_a ) @property def __lowercase ( self ) -> Tuple: return sum(self.block_sizes ) @num_hidden_layers.setter def __lowercase ( self , _a ) -> List[str]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.''' ) @property def __lowercase ( self ) -> Optional[int]: return len(self.block_sizes ) @num_blocks.setter def __lowercase ( self , _a ) -> Dict: raise NotImplementedError('''This model does not support the setting of `num_blocks`. Please set `block_sizes`.''' )
14
1
def __UpperCAmelCase ( __a : List[Any] ,__a : int ,__a : List[Any] ,__a : List[Any] ) -> int: """simple docstring""" if height >= 1: move_tower(height - 1 ,__a ,__a ,__a ) move_disk(__a ,__a ) move_tower(height - 1 ,__a ,__a ,__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : List[Any] ) -> Tuple: """simple docstring""" print('''moving disk from''' ,__a ,'''to''' ,__a ) def __UpperCAmelCase ( ) -> int: """simple docstring""" _a : List[str] = int(input('''Height of hanoi: ''' ).strip() ) move_tower(__a ,'''A''' ,'''B''' ,'''C''' ) if __name__ == "__main__": main()
14
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : int = "mobilenet_v1" def __init__( self , _a=3 , _a=2_2_4 , _a=1.0 , _a=8 , _a="relu6" , _a=True , _a=0.999 , _a=0.02 , _a=0.001 , **_a , ) -> List[Any]: super().__init__(**_a ) if depth_multiplier <= 0: raise ValueError('''depth_multiplier must be greater than zero.''' ) _a : Tuple = num_channels _a : str = image_size _a : Tuple = depth_multiplier _a : Any = min_depth _a : int = hidden_act _a : Optional[Any] = tf_padding _a : str = classifier_dropout_prob _a : Optional[int] = initializer_range _a : Any = layer_norm_eps class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = version.parse("1.11" ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([('''pixel_values''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([('''logits''', {0: '''batch'''})] ) else: return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})] ) @property def __lowercase ( self ) -> float: return 1e-4
14
1
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def __UpperCAmelCase ( __a : Tuple ,__a : Tuple=7 ) -> Any: """simple docstring""" _a : Union[str, Any] = None if token is not None: _a : List[str] = {'''Accept''': '''application/vnd.github+json''', '''Authorization''': F"""Bearer {token}"""} # The id of a workflow (not of a workflow run) _a : Any = '''636036''' _a : Any = F"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += F"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" _a : List[Any] = requests.get(__a ,headers=__a ).json() return result["workflow_runs"] def __UpperCAmelCase ( __a : Union[str, Any] ) -> Tuple: """simple docstring""" _a : str = get_daily_ci_runs(__a ) _a : List[Any] = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": _a : int = workflow_run['''id'''] break return workflow_run_id def __UpperCAmelCase ( __a : Dict ,__a : str ,__a : Optional[int] ) -> Optional[Any]: """simple docstring""" _a : Dict = get_last_daily_ci_runs(__a ) if workflow_run_id is not None: _a : Optional[int] = get_artifacts_links(worflow_run_id=__a ,token=__a ) for artifact_name in artifact_names: if artifact_name in artifacts_links: _a : List[str] = artifacts_links[artifact_name] download_artifact( artifact_name=__a ,artifact_url=__a ,output_dir=__a ,token=__a ) def __UpperCAmelCase ( __a : List[Any] ,__a : str ,__a : Tuple ) -> Union[str, Any]: """simple docstring""" get_last_daily_ci_artifacts(__a ,__a ,__a ) _a : List[Any] = {} for artifact_name in artifact_names: _a : List[str] = os.path.join(__a ,F"""{artifact_name}.zip""" ) if os.path.isfile(__a ): _a : Any = {} with zipfile.ZipFile(__a ) as z: for filename in z.namelist(): if not os.path.isdir(__a ): # read the file with z.open(__a ) as f: _a : Optional[int] = f.read().decode('''UTF-8''' ) return results
14
a__ = '''Input must be a string of 8 numbers plus letter''' a__ = '''TRWAGMYFPDXBNJZSQVHLCKE''' def __UpperCAmelCase ( __a : str ) -> bool: """simple docstring""" if not isinstance(__a ,__a ): _a : List[str] = F"""Expected string as input, found {type(__a ).__name__}""" raise TypeError(__a ) _a : List[Any] = spanish_id.replace('''-''' ,'''''' ).upper() if len(__a ) != 9: raise ValueError(__a ) try: _a : Any = int(spanish_id_clean[0:8] ) _a : str = spanish_id_clean[8] except ValueError as ex: raise ValueError(__a ) from ex if letter.isdigit(): raise ValueError(__a ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
14
1
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = {} class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = "llama" UpperCAmelCase__ : List[str] = ["past_key_values"] def __init__( self , _a=3_2_0_0_0 , _a=4_0_9_6 , _a=1_1_0_0_8 , _a=3_2 , _a=3_2 , _a=None , _a="silu" , _a=2_0_4_8 , _a=0.02 , _a=1e-6 , _a=True , _a=0 , _a=1 , _a=2 , _a=1 , _a=False , _a=None , **_a , ) -> Dict: _a : str = vocab_size _a : List[Any] = max_position_embeddings _a : str = hidden_size _a : List[Any] = intermediate_size _a : Tuple = num_hidden_layers _a : Optional[int] = num_attention_heads # for backward compatibility if num_key_value_heads is None: _a : List[str] = num_attention_heads _a : Optional[int] = num_key_value_heads _a : Optional[Any] = hidden_act _a : Union[str, Any] = initializer_range _a : str = rms_norm_eps _a : List[Any] = pretraining_tp _a : Any = use_cache _a : int = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , tie_word_embeddings=_a , **_a , ) def __lowercase ( self ) -> Any: if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _a ) or len(self.rope_scaling ) != 2: raise ValueError( '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ''' F"""got {self.rope_scaling}""" ) _a : List[str] = self.rope_scaling.get('''type''' , _a ) _a : Dict = self.rope_scaling.get('''factor''' , _a ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( F"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}""" ) if rope_scaling_factor is None or not isinstance(_a , _a ) or rope_scaling_factor <= 1.0: raise ValueError(F"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
14
from random import randint from tempfile import TemporaryFile import numpy as np def __UpperCAmelCase ( __a : Optional[Any] ,__a : int ,__a : Any ) -> int: """simple docstring""" _a : int = 0 if start < end: _a : Tuple = randint(__a ,__a ) _a : Tuple = a[end] _a : List[str] = a[pivot] _a : Any = temp _a , _a : Optional[int] = _in_place_partition(__a ,__a ,__a ) count += _in_place_quick_sort(__a ,__a ,p - 1 ) count += _in_place_quick_sort(__a ,p + 1 ,__a ) return count def __UpperCAmelCase ( __a : List[Any] ,__a : Tuple ,__a : Dict ) -> Dict: """simple docstring""" _a : Dict = 0 _a : Tuple = randint(__a ,__a ) _a : List[Any] = a[end] _a : str = a[pivot] _a : str = temp _a : Dict = start - 1 for index in range(__a ,__a ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value _a : int = new_pivot_index + 1 _a : Any = a[new_pivot_index] _a : Optional[int] = a[index] _a : str = temp _a : Union[str, Any] = a[new_pivot_index + 1] _a : Tuple = a[end] _a : Any = temp return new_pivot_index + 1, count a__ = TemporaryFile() a__ = 100 # 1000 elements are to be sorted a__ , a__ = 0, 1 # mean and standard deviation a__ = np.random.normal(mu, sigma, p) np.save(outfile, X) print('''The array is''') print(X) outfile.seek(0) # using the same array a__ = np.load(outfile) a__ = len(M) - 1 a__ = _in_place_quick_sort(M, 0, r) print( '''No of Comparisons for 100 elements selected from a standard normal distribution''' '''is :''' ) print(z)
14
1
import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class UpperCAmelCase_ ( __lowercase ): """simple docstring""" def __lowercase ( self ) -> Any: _a : Dict = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(_a , '''tf_padding''' ) ) self.parent.assertTrue(hasattr(_a , '''depth_multiplier''' ) ) class UpperCAmelCase_ : """simple docstring""" def __init__( self , _a , _a=1_3 , _a=3 , _a=3_2 , _a=0.25 , _a=8 , _a=True , _a=1_0_2_4 , _a=3_2 , _a="relu6" , _a=0.1 , _a=0.02 , _a=True , _a=True , _a=1_0 , _a=None , ) -> Any: _a : List[str] = parent _a : Tuple = batch_size _a : int = num_channels _a : Dict = image_size _a : Any = depth_multiplier _a : Tuple = min_depth _a : List[Any] = tf_padding _a : Optional[int] = int(last_hidden_size * depth_multiplier ) _a : List[str] = output_stride _a : Dict = hidden_act _a : Optional[int] = classifier_dropout_prob _a : str = use_labels _a : Optional[int] = is_training _a : Dict = num_labels _a : Union[str, Any] = initializer_range _a : Optional[Any] = scope def __lowercase ( self ) -> Optional[int]: _a : Optional[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : List[Any] = None _a : Any = None if self.use_labels: _a : List[Any] = ids_tensor([self.batch_size] , self.num_labels ) _a : Any = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _a : str = self.get_config() return config, pixel_values, labels, pixel_labels def __lowercase ( self ) -> Union[str, Any]: return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , min_depth=self.min_depth , tf_padding=self.tf_padding , hidden_act=self.hidden_act , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def __lowercase ( self , _a , _a , _a , _a ) -> Dict: _a : Union[str, Any] = MobileNetVaModel(config=_a ) model.to(_a ) model.eval() _a : Optional[Any] = model(_a ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def __lowercase ( self , _a , _a , _a , _a ) -> Dict: _a : Any = self.num_labels _a : str = MobileNetVaForImageClassification(_a ) model.to(_a ) model.eval() _a : int = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowercase ( self ) -> Dict: _a : Dict = self.prepare_config_and_inputs() _a , _a , _a , _a : Tuple = config_and_inputs _a : List[str] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : str = (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else () UpperCAmelCase__ : Union[str, Any] = ( {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Dict = False UpperCAmelCase__ : List[Any] = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : str = False def __lowercase ( self ) -> str: _a : Optional[Any] = MobileNetVaModelTester(self ) _a : List[str] = MobileNetVaConfigTester(self , config_class=_a , has_text_modality=_a ) def __lowercase ( self ) -> str: self.config_tester.run_common_tests() @unittest.skip(reason='''MobileNetV1 does not use inputs_embeds''' ) def __lowercase ( self ) -> Union[str, Any]: pass @unittest.skip(reason='''MobileNetV1 does not support input and output embeddings''' ) def __lowercase ( self ) -> int: pass @unittest.skip(reason='''MobileNetV1 does not output attentions''' ) def __lowercase ( self ) -> Tuple: pass def __lowercase ( self ) -> List[str]: _a , _a : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : List[str] = model_class(_a ) _a : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Optional[int] = [*signature.parameters.keys()] _a : Any = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowercase ( self ) -> Tuple: _a : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowercase ( self ) -> List[Any]: def check_hidden_states_output(_a , _a , _a ): _a : Union[str, Any] = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _a : int = model(**self._prepare_for_class(_a , _a ) ) _a : Union[str, Any] = outputs.hidden_states _a : int = 2_6 self.assertEqual(len(_a ) , _a ) _a , _a : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : int = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _a : Any = True check_hidden_states_output(_a , _a , _a ) def __lowercase ( self ) -> List[Any]: _a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __lowercase ( self ) -> str: for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Tuple = MobileNetVaModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def __UpperCAmelCase ( ) -> str: """simple docstring""" _a : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" @cached_property def __lowercase ( self ) -> List[Any]: return ( MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224''' ) if is_vision_available() else None ) @slow def __lowercase ( self ) -> Tuple: _a : List[Any] = MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224''' ).to(_a ) _a : Any = self.default_image_processor _a : Dict = prepare_img() _a : Union[str, Any] = image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _a : Any = model(**_a ) # verify the logits _a : int = torch.Size((1, 1_0_0_1) ) self.assertEqual(outputs.logits.shape , _a ) _a : List[Any] = torch.tensor([-4.1739, -1.1233, 3.1205] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
14
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class UpperCAmelCase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[Any] = MgpstrTokenizer UpperCAmelCase__ : int = False UpperCAmelCase__ : Union[str, Any] = {} UpperCAmelCase__ : List[Any] = False def __lowercase ( self ) -> Any: super().setUp() # fmt: off _a : Tuple = ['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _a : Optional[int] = dict(zip(_a , range(len(_a ) ) ) ) _a : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) def __lowercase ( self , **_a ) -> Dict: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __lowercase ( self , _a ) -> Tuple: _a : List[str] = '''tester''' _a : Optional[Any] = '''tester''' return input_text, output_text @unittest.skip('''MGP-STR always lower cases letters.''' ) def __lowercase ( self ) -> Any: pass def __lowercase ( self ) -> Any: _a : Union[str, Any] = self.get_tokenizers(do_lower_case=_a ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a : int = '''[SPECIAL_TOKEN]''' tokenizer.add_special_tokens({'''cls_token''': special_token} ) _a : Tuple = tokenizer.encode([special_token] , add_special_tokens=_a ) self.assertEqual(len(_a ) , 1 ) _a : Tuple = tokenizer.decode(_a , skip_special_tokens=_a ) self.assertTrue(special_token not in decoded ) def __lowercase ( self ) -> Tuple: _a : List[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): _a , _a : int = self.get_input_output_texts(_a ) _a : List[str] = tokenizer.tokenize(_a ) _a : Optional[int] = tokenizer.convert_tokens_to_ids(_a ) _a : Tuple = tokenizer.encode(_a , add_special_tokens=_a ) self.assertListEqual(_a , _a ) _a : Optional[int] = tokenizer.convert_ids_to_tokens(_a ) self.assertNotEqual(len(_a ) , 0 ) _a : int = tokenizer.decode(_a ) self.assertIsInstance(_a , _a ) self.assertEqual(text_a.replace(''' ''' , '''''' ) , _a ) @unittest.skip('''MGP-STR tokenizer only handles one sequence.''' ) def __lowercase ( self ) -> List[str]: pass @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer''' ) def __lowercase ( self ) -> Optional[Any]: pass
14
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a__ = { '''configuration_informer''': [ '''INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''InformerConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ = [ '''INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''InformerForPrediction''', '''InformerModel''', '''InformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys a__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
14
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class UpperCAmelCase_ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self ) -> List[Any]: _a : int = 0 def __lowercase ( self ) -> List[str]: _a : Dict = AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32''' ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : Optional[Any] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: # Ensure we can load the image processor from the feature extractor config with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' _a : Any = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: _a : Dict = CLIPConfig() # Create a dummy config file with image_proceesor_type _a : Tuple = Path(_a ) / '''preprocessor_config.json''' _a : List[str] = Path(_a ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally _a : Tuple = AutoImageProcessor.from_pretrained(_a ).to_dict() config_dict.pop('''image_processor_type''' ) _a : Tuple = CLIPImageProcessor(**_a ) # save in new folder model_config.save_pretrained(_a ) config.save_pretrained(_a ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) # make sure private variable is not incorrectly saved _a : Optional[int] = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: _a : Optional[int] = Path(_a ) / '''preprocessor_config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) _a : List[str] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) def __lowercase ( self ) -> Any: with self.assertRaisesRegex( _a , '''clip-base is not a local folder and is not a valid model identifier''' ): _a : Dict = AutoImageProcessor.from_pretrained('''clip-base''' ) def __lowercase ( self ) -> List[Any]: with self.assertRaisesRegex( _a , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): _a : List[str] = AutoImageProcessor.from_pretrained(_a , revision='''aaaaaa''' ) def __lowercase ( self ) -> Dict: with self.assertRaisesRegex( _a , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): _a : Optional[int] = AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __lowercase ( self ) -> Union[str, Any]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_a ): _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_a ): _a : Optional[Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) _a : Union[str, Any] = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a , trust_remote_code=_a ) self.assertEqual(reloaded_image_processor.__class__.__name__ , '''NewImageProcessor''' ) def __lowercase ( self ) -> Dict: try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_a ): AutoImageProcessor.register(_a , _a ) with tempfile.TemporaryDirectory() as tmpdirname: _a : int = Path(_a ) / '''preprocessor_config.json''' _a : int = Path(_a ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(_a , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(_a , '''w''' ) ) _a : int = CustomImageProcessor.from_pretrained(_a ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_a ) _a : Optional[Any] = AutoImageProcessor.from_pretrained(_a ) self.assertIsInstance(_a , _a ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def __lowercase ( self ) -> Union[str, Any]: class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Tuple = True try: AutoConfig.register('''custom''' , _a ) AutoImageProcessor.register(_a , _a ) # If remote code is not set, the default is to use local _a : str = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. _a : int = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub _a : Dict = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=_a ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(not hasattr(_a , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
14
1
from ...configuration_utils import PretrainedConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''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 UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : List[Any] = "visual_bert" def __init__( self , _a=3_0_5_2_2 , _a=7_6_8 , _a=5_1_2 , _a=1_2 , _a=1_2 , _a=3_0_7_2 , _a="gelu" , _a=0.1 , _a=0.1 , _a=5_1_2 , _a=2 , _a=0.02 , _a=1e-1_2 , _a=False , _a=True , _a=1 , _a=0 , _a=2 , **_a , ) -> str: super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) _a : Any = vocab_size _a : List[str] = max_position_embeddings _a : Dict = hidden_size _a : Union[str, Any] = visual_embedding_dim _a : Tuple = num_hidden_layers _a : int = num_attention_heads _a : Union[str, Any] = intermediate_size _a : List[Any] = hidden_act _a : Union[str, Any] = hidden_dropout_prob _a : List[Any] = attention_probs_dropout_prob _a : Optional[Any] = initializer_range _a : Tuple = type_vocab_size _a : List[str] = layer_norm_eps _a : int = bypass_transformer _a : Optional[int] = special_visual_initialize
14
from __future__ import annotations from dataclasses import dataclass @dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : float UpperCAmelCase__ : TreeNode | None = None UpperCAmelCase__ : TreeNode | None = None def __UpperCAmelCase ( __a : TreeNode | None ) -> bool: """simple docstring""" def is_valid_tree(__a : TreeNode | None ) -> bool: if node is None: return True if not isinstance(__a ,__a ): return False try: float(node.data ) except (TypeError, ValueError): return False return is_valid_tree(node.left ) and is_valid_tree(node.right ) if not is_valid_tree(__a ): raise ValueError( '''Each node should be type of TreeNode and data should be float.''' ) def is_binary_search_tree_recursive_check( __a : TreeNode | None ,__a : float ,__a : float ) -> bool: if node is None: return True return ( left_bound < node.data < right_bound and is_binary_search_tree_recursive_check(node.left ,__a ,node.data ) and is_binary_search_tree_recursive_check( node.right ,node.data ,__a ) ) return is_binary_search_tree_recursive_check(__a ,-float('''inf''' ) ,float('''inf''' ) ) if __name__ == "__main__": import doctest doctest.testmod()
14
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL a__ = logging.get_logger(__name__) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BICUBIC , _a = True , _a = 1 / 2_5_5 , _a = True , _a = None , _a = None , _a = True , **_a , ) -> None: super().__init__(**_a ) _a : str = size if size is not None else {'''height''': 3_8_4, '''width''': 3_8_4} _a : Union[str, Any] = get_size_dict(_a , default_to_square=_a ) _a : str = do_resize _a : str = size _a : Optional[int] = resample _a : Optional[int] = do_rescale _a : Tuple = rescale_factor _a : str = do_normalize _a : Optional[int] = image_mean if image_mean is not None else OPENAI_CLIP_MEAN _a : Union[str, Any] = image_std if image_std is not None else OPENAI_CLIP_STD _a : int = do_convert_rgb def __lowercase ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ) -> np.ndarray: _a : List[str] = get_size_dict(_a , default_to_square=_a ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) _a : int = (size['''height'''], size['''width''']) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def __lowercase ( self , _a , _a , _a = None , **_a , ) -> Optional[int]: return rescale(_a , scale=_a , data_format=_a , **_a ) def __lowercase ( self , _a , _a , _a , _a = None , **_a , ) -> np.ndarray: return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def __lowercase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ) -> PIL.Image.Image: _a : str = do_resize if do_resize is not None else self.do_resize _a : Optional[int] = resample if resample is not None else self.resample _a : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale _a : Any = rescale_factor if rescale_factor is not None else self.rescale_factor _a : Union[str, Any] = do_normalize if do_normalize is not None else self.do_normalize _a : Optional[Any] = image_mean if image_mean is not None else self.image_mean _a : Tuple = image_std if image_std is not None else self.image_std _a : List[Any] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb _a : List[Any] = size if size is not None else self.size _a : Optional[Any] = get_size_dict(_a , default_to_square=_a ) _a : List[Any] = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: _a : List[str] = [convert_to_rgb(_a ) for image in images] # All transformations expect numpy arrays. _a : Dict = [to_numpy_array(_a ) for image in images] if do_resize: _a : Any = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_rescale: _a : List[str] = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: _a : List[Any] = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] _a : Optional[int] = [to_channel_dimension_format(_a , _a ) for image in images] _a : Optional[Any] = BatchFeature(data={'''pixel_values''': images} , tensor_type=_a ) return encoded_outputs
14
from __future__ import annotations import matplotlib.pyplot as plt # type: ignore import numpy # initial triangle of Koch snowflake a__ = numpy.array([0, 0]) a__ = numpy.array([0.5, 0.8660254]) a__ = numpy.array([1, 0]) a__ = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1] def __UpperCAmelCase ( __a : list[numpy.ndarray] ,__a : int ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = initial_vectors for _ in range(__a ): _a : int = iteration_step(__a ) return vectors def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> list[numpy.ndarray]: """simple docstring""" _a : Tuple = [] for i, start_vector in enumerate(vectors[:-1] ): _a : str = vectors[i + 1] new_vectors.append(__a ) _a : Optional[int] = end_vector - start_vector new_vectors.append(start_vector + difference_vector / 3 ) new_vectors.append( start_vector + difference_vector / 3 + rotate(difference_vector / 3 ,60 ) ) new_vectors.append(start_vector + difference_vector * 2 / 3 ) new_vectors.append(vectors[-1] ) return new_vectors def __UpperCAmelCase ( __a : numpy.ndarray ,__a : float ) -> numpy.ndarray: """simple docstring""" _a : Tuple = numpy.radians(__a ) _a , _a : List[Any] = numpy.cos(__a ), numpy.sin(__a ) _a : Dict = numpy.array(((c, -s), (s, c)) ) return numpy.dot(__a ,__a ) def __UpperCAmelCase ( __a : list[numpy.ndarray] ) -> None: """simple docstring""" _a : str = plt.gca() axes.set_aspect('''equal''' ) # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all # y-coordinates as inputs, which are constructed from the vector-list using # zip() _a , _a : Optional[int] = zip(*__a ) plt.plot(__a ,__a ) plt.show() if __name__ == "__main__": import doctest doctest.testmod() a__ = iterate(INITIAL_VECTORS, 5) plot(processed_vectors)
14
1