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
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def _SCREAMING_SNAKE_CASE ( ) -> List[Any]: """simple docstring""" __A = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=A__ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=A__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=A__ ) return parser.parse_args() def _SCREAMING_SNAKE_CASE ( ) -> List[str]: """simple docstring""" __A = parse_args() # Import training_script as a module. __A = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) __A = script_fpath.stem __A = importlib.import_module(A__ ) # Patch sys.argv __A = [args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
637
import os def a ( A__ = "matrix.txt" ) -> int: '''simple docstring''' with open(os.path.join(os.path.dirname(A__ ) , A__ ) ) as in_file: SCREAMING_SNAKE_CASE__ : Optional[Any] = in_file.read() SCREAMING_SNAKE_CASE__ : Optional[Any] = [[int(A__ ) for cell in row.split(''',''' )] for row in data.strip().splitlines()] SCREAMING_SNAKE_CASE__ : Dict = [[0 for cell in row] for row in grid] SCREAMING_SNAKE_CASE__ : Any = len(grid[0] ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = [[0 for i in range(A__ )] for j in range(A__ )] SCREAMING_SNAKE_CASE__ : Tuple = grid[0][0] for i in range(1 , A__ ): SCREAMING_SNAKE_CASE__ : List[str] = grid[0][i] + dp[0][i - 1] for i in range(1 , A__ ): SCREAMING_SNAKE_CASE__ : List[str] = grid[i][0] + dp[i - 1][0] for i in range(1 , A__ ): for j in range(1 , A__ ): SCREAMING_SNAKE_CASE__ : Optional[int] = grid[i][j] + min(dp[i - 1][j] , dp[i][j - 1] ) return dp[-1][-1] if __name__ == "__main__": print(F'''{solution() = }''')
35
0
import argparse import json import os from pathlib import Path import requests import torch from transformers import JukeboxConfig, JukeboxModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__: str = logging.get_logger(__name__) lowerCAmelCase__: Optional[int] = 'https://openaipublic.azureedge.net/jukebox/models/' lowerCAmelCase__: Optional[Any] = { 'jukebox-1b-lyrics': [ '5b/vqvae.pth.tar', '5b/prior_level_0.pth.tar', '5b/prior_level_1.pth.tar', '1b_lyrics/prior_level_2.pth.tar', ], 'jukebox-5b-lyrics': [ '5b/vqvae.pth.tar', '5b/prior_level_0.pth.tar', '5b/prior_level_1.pth.tar', '5b_lyrics/prior_level_2.pth.tar', ], } def __SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE ) -> Optional[Any]: if key.endswith('.model.1.bias' ) and len(key.split('.' ) ) > 10: SCREAMING_SNAKE_CASE_ : Tuple = key.replace('.model.1.bias' , '.conv1d_1.bias' ) elif key.endswith('.model.1.weight' ) and len(key.split('.' ) ) > 10: SCREAMING_SNAKE_CASE_ : str = key.replace('.model.1.weight' , '.conv1d_1.weight' ) elif key.endswith('.model.3.bias' ) and len(key.split('.' ) ) > 10: SCREAMING_SNAKE_CASE_ : int = key.replace('.model.3.bias' , '.conv1d_2.bias' ) elif key.endswith('.model.3.weight' ) and len(key.split('.' ) ) > 10: SCREAMING_SNAKE_CASE_ : Optional[int] = key.replace('.model.3.weight' , '.conv1d_2.weight' ) if "conditioner_blocks.0." in key: SCREAMING_SNAKE_CASE_ : Union[str, Any] = key.replace('conditioner_blocks.0' , 'conditioner_blocks' ) if "prime_prior" in key: SCREAMING_SNAKE_CASE_ : Optional[Any] = key.replace('prime_prior' , 'encoder' ) if ".emb." in key and "total" not in key and "absolute" not in key and "relative" not in key: SCREAMING_SNAKE_CASE_ : Tuple = key.replace('.emb.' , '.' ) if key.endswith('k' ): # replace vqvae.X.k with vqvae.X.codebook return key.replace('.k' , '.codebook' ) if "y_emb." in key: return key.replace('y_emb.' , 'metadata_embedding.' ) if "x_emb.emb." in key: SCREAMING_SNAKE_CASE_ : List[Any] = key.replace('0.x_emb.emb' , 'embed_tokens' ) if "prime_state_ln" in key: return key.replace('prime_state_ln' , 'encoder.final_layer_norm' ) if ".ln" in key: return key.replace('.ln' , '.layer_norm' ) if "_ln" in key: return key.replace('_ln' , '_layer_norm' ) if "prime_state_proj" in key: return key.replace('prime_state_proj' , 'encoder.proj_in' ) if "prime_x_out" in key: return key.replace('prime_x_out' , 'encoder.lm_head' ) if "prior.x_out" in key: return key.replace('x_out' , 'fc_proj_out' ) if "x_emb" in key: return key.replace('x_emb' , 'embed_tokens' ) return key def __SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> int: SCREAMING_SNAKE_CASE_ : Tuple = {} import re SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : List[Any] = re.compile( R'encoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : Optional[int] = re.compile(R'encoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : Optional[int] = re.compile(R'decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : List[Any] = re.compile( R'decoders.(\d*).level_blocks.(\d*).model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = re.compile(R'decoders.(\d*).level_blocks.(\d*).model.(\d*).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = re.compile(R'conditioner_blocks.(\d*).cond.model.(\d*).(\d).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : Optional[int] = re.compile( R'conditioner_blocks.(\d*).cond.model.(\d*).(\d).model.(\d*).model.(\d*).(bias|weight)' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = re.compile(R'conditioner_blocks.(\d*).cond.model.(\d*).(bias|weight)' ) for original_key, value in state_dict.items(): # rename vqvae.encoder keys if re_encoder_block_conv_in.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : List[str] = re_encoder_block_conv_in.match(A__ ) SCREAMING_SNAKE_CASE_ : Tuple = regex_match.groups() SCREAMING_SNAKE_CASE_ : int = int(groups[2] ) * 2 + int(groups[3] ) SCREAMING_SNAKE_CASE_ : Any = f'encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.{groups[-1]}' SCREAMING_SNAKE_CASE_ : Tuple = re_encoder_block_conv_in.sub(A__ , A__ ) elif re_encoder_block_resnet.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = re_encoder_block_resnet.match(A__ ) SCREAMING_SNAKE_CASE_ : List[Any] = regex_match.groups() SCREAMING_SNAKE_CASE_ : Union[str, Any] = int(groups[2] ) * 2 + int(groups[3] ) SCREAMING_SNAKE_CASE_ : Dict = {'''1''': 1, '''3''': 2}[groups[-2]] SCREAMING_SNAKE_CASE_ : Union[str, Any] = f'encoders.{groups[0]}.level_blocks.{groups[1]}.downsample_block.{block_index}.' SCREAMING_SNAKE_CASE_ : List[Any] = f'resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}' SCREAMING_SNAKE_CASE_ : List[Any] = prefix + resnet_block SCREAMING_SNAKE_CASE_ : List[Any] = re_encoder_block_resnet.sub(A__ , A__ ) elif re_encoder_block_proj_out.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = re_encoder_block_proj_out.match(A__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = regex_match.groups() SCREAMING_SNAKE_CASE_ : str = f'encoders.{groups[0]}.level_blocks.{groups[1]}.proj_out.{groups[-1]}' SCREAMING_SNAKE_CASE_ : Any = re_encoder_block_proj_out.sub(A__ , A__ ) # rename vqvae.decoder keys elif re_decoder_block_conv_out.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : str = re_decoder_block_conv_out.match(A__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = regex_match.groups() SCREAMING_SNAKE_CASE_ : Optional[int] = int(groups[2] ) * 2 + int(groups[3] ) - 2 SCREAMING_SNAKE_CASE_ : Tuple = f'decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.{groups[-1]}' SCREAMING_SNAKE_CASE_ : Any = re_decoder_block_conv_out.sub(A__ , A__ ) elif re_decoder_block_resnet.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = re_decoder_block_resnet.match(A__ ) SCREAMING_SNAKE_CASE_ : Dict = regex_match.groups() SCREAMING_SNAKE_CASE_ : Union[str, Any] = int(groups[2] ) * 2 + int(groups[3] ) - 2 SCREAMING_SNAKE_CASE_ : int = {'''1''': 1, '''3''': 2}[groups[-2]] SCREAMING_SNAKE_CASE_ : Dict = f'decoders.{groups[0]}.level_blocks.{groups[1]}.upsample_block.{block_index}.' SCREAMING_SNAKE_CASE_ : Dict = f'resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}' SCREAMING_SNAKE_CASE_ : Optional[int] = prefix + resnet_block SCREAMING_SNAKE_CASE_ : List[str] = re_decoder_block_resnet.sub(A__ , A__ ) elif re_decoder_block_proj_in.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = re_decoder_block_proj_in.match(A__ ) SCREAMING_SNAKE_CASE_ : Dict = regex_match.groups() SCREAMING_SNAKE_CASE_ : str = f'decoders.{groups[0]}.level_blocks.{groups[1]}.proj_in.{groups[-1]}' SCREAMING_SNAKE_CASE_ : str = re_decoder_block_proj_in.sub(A__ , A__ ) # rename prior cond.model to upsampler.upsample_block and resnet elif re_prior_cond_conv_out.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : int = re_prior_cond_conv_out.match(A__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = regex_match.groups() SCREAMING_SNAKE_CASE_ : Any = int(groups[1] ) * 2 + int(groups[2] ) - 2 SCREAMING_SNAKE_CASE_ : Optional[Any] = f'conditioner_blocks.upsampler.upsample_block.{block_index}.{groups[-1]}' SCREAMING_SNAKE_CASE_ : List[Any] = re_prior_cond_conv_out.sub(A__ , A__ ) elif re_prior_cond_resnet.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : Dict = re_prior_cond_resnet.match(A__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = regex_match.groups() SCREAMING_SNAKE_CASE_ : Optional[Any] = int(groups[1] ) * 2 + int(groups[2] ) - 2 SCREAMING_SNAKE_CASE_ : Optional[int] = {'''1''': 1, '''3''': 2}[groups[-2]] SCREAMING_SNAKE_CASE_ : List[Any] = f'conditioner_blocks.upsampler.upsample_block.{block_index}.' SCREAMING_SNAKE_CASE_ : str = f'resnet_block.{groups[-3]}.conv1d_{conv_index}.{groups[-1]}' SCREAMING_SNAKE_CASE_ : List[Any] = prefix + resnet_block SCREAMING_SNAKE_CASE_ : Union[str, Any] = re_prior_cond_resnet.sub(A__ , A__ ) elif re_prior_cond_proj_in.fullmatch(A__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = re_prior_cond_proj_in.match(A__ ) SCREAMING_SNAKE_CASE_ : Dict = regex_match.groups() SCREAMING_SNAKE_CASE_ : Optional[int] = f'conditioner_blocks.upsampler.proj_in.{groups[-1]}' SCREAMING_SNAKE_CASE_ : Union[str, Any] = re_prior_cond_proj_in.sub(A__ , A__ ) # keep original key else: SCREAMING_SNAKE_CASE_ : List[Any] = original_key SCREAMING_SNAKE_CASE_ : int = replace_key(A__ ) if f'{key_prefix}.{key}' not in model_state_dict or key is None: print(f'failed converting {original_key} to {key}, does not match' ) # handle missmatched shape elif value.shape != model_state_dict[f'{key_prefix}.{key}'].shape: SCREAMING_SNAKE_CASE_ : List[str] = model_state_dict[f'{key_prefix}.{key}'] print(f'{original_key}-> {key} : \nshape {val.shape} and { value.shape}, do not match' ) SCREAMING_SNAKE_CASE_ : List[str] = original_key SCREAMING_SNAKE_CASE_ : str = original_key SCREAMING_SNAKE_CASE_ : Tuple = value return new_dict @torch.no_grad() def __SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE=None , SCREAMING_SNAKE_CASE=None ) -> str: for file in MODEL_MAPPING[model_name]: if not os.path.isfile(f'{pytorch_dump_folder_path}/{file.split("/" )[-1]}' ): SCREAMING_SNAKE_CASE_ : Tuple = requests.get(f'{PREFIX}{file}' , allow_redirects=A__ ) os.makedirs(f'{pytorch_dump_folder_path}/' , exist_ok=A__ ) open(f'{pytorch_dump_folder_path}/{file.split("/" )[-1]}' , 'wb' ).write(r.content ) SCREAMING_SNAKE_CASE_ : Tuple = MODEL_MAPPING[model_name.split('/' )[-1]] SCREAMING_SNAKE_CASE_ : Optional[Any] = JukeboxConfig.from_pretrained(A__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = JukeboxModel(A__ ) SCREAMING_SNAKE_CASE_ : Any = [] SCREAMING_SNAKE_CASE_ : Any = {} for i, dict_name in enumerate(A__ ): SCREAMING_SNAKE_CASE_ : Dict = torch.load(f'{pytorch_dump_folder_path}/{dict_name.split("/" )[-1]}' )['''model'''] SCREAMING_SNAKE_CASE_ : Optional[Any] = {} for k in old_dic.keys(): if k.endswith('.b' ): SCREAMING_SNAKE_CASE_ : Optional[int] = old_dic[k] elif k.endswith('.w' ): SCREAMING_SNAKE_CASE_ : Any = old_dic[k] elif "level_2" not in dict_name and "cond.model." in k: SCREAMING_SNAKE_CASE_ : Tuple = old_dic[k] else: SCREAMING_SNAKE_CASE_ : List[Any] = old_dic[k] SCREAMING_SNAKE_CASE_ : Any = '''vqvae''' if i == 0 else f'priors.{3 - i}' SCREAMING_SNAKE_CASE_ : str = fix_jukebox_keys(A__ , model.state_dict() , A__ , A__ ) weight_dict.append(A__ ) SCREAMING_SNAKE_CASE_ : List[str] = weight_dict.pop(0 ) model.vqvae.load_state_dict(A__ ) for i in range(len(A__ ) ): model.priors[i].load_state_dict(weight_dict[2 - i] ) Path(A__ ).mkdir(exist_ok=A__ ) with open(f'{pytorch_dump_folder_path}/mapping.json' , 'w' ) as txtfile: json.dump(A__ , A__ ) print(f'Saving model {model_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(A__ ) return weight_dict if __name__ == "__main__": lowerCAmelCase__: Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="jukebox-5b-lyrics", type=str, help="Name of the model you\'d like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default="jukebox-5b-lyrics-converted", type=str, help="Path to the output PyTorch model directory.", ) lowerCAmelCase__: List[Any] = parser.parse_args() convert_openai_checkpoint(args.model_name, args.pytorch_dump_folder_path)
345
from math import factorial def a ( A__ = 2_0 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... SCREAMING_SNAKE_CASE__ : Dict = n // 2 return int(factorial(A__ ) / (factorial(A__ ) * factorial(n - k )) ) if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution(20)) else: try: a_ :str = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number.')
35
0
from __future__ import annotations def _lowerCamelCase( __snake_case ) -> None: create_state_space_tree(A__ , [] , 0 , [0 for i in range(len(A__ ) )] ) def _lowerCamelCase( __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: if index == len(A__ ): print(A__ ) return for i in range(len(A__ ) ): if not index_used[i]: current_sequence.append(sequence[i] ) __snake_case = True create_state_space_tree(A__ , A__ , index + 1 , A__ ) current_sequence.pop() __snake_case = False lowerCamelCase__ = [3, 1, 2, 4] generate_all_permutations(sequence) lowerCamelCase__ = ["A", "B", "C"] generate_all_permutations(sequence_a)
524
import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class lowercase : @staticmethod def lowercase__ ( *_lowercase : Optional[Any] , **_lowercase : str ): pass def a ( A__ ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class lowercase ( unittest.TestCase ): lowerCamelCase : int = MODEL_FOR_DEPTH_ESTIMATION_MAPPING def lowercase__ ( self : List[Any] , _lowercase : Tuple , _lowercase : Any , _lowercase : List[str] ): SCREAMING_SNAKE_CASE__ : List[str] = DepthEstimationPipeline(model=_lowercase , image_processor=_lowercase ) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def lowercase__ ( self : Union[str, Any] , _lowercase : int , _lowercase : int ): SCREAMING_SNAKE_CASE__ : Optional[int] = depth_estimator('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) self.assertEqual({'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )} , _lowercase ) import datasets SCREAMING_SNAKE_CASE__ : List[str] = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''' ) SCREAMING_SNAKE_CASE__ : Dict = depth_estimator( [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ] ) self.assertEqual( [ {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, ] , _lowercase , ) @require_tf @unittest.skip('''Depth estimation is not implemented in TF''' ) def lowercase__ ( self : Optional[int] ): pass @slow @require_torch def lowercase__ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE__ : List[str] = '''Intel/dpt-large''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = pipeline('''depth-estimation''' , model=_lowercase ) SCREAMING_SNAKE_CASE__ : Dict = depth_estimator('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) SCREAMING_SNAKE_CASE__ : List[str] = hashimage(outputs['''depth'''] ) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs['''predicted_depth'''].max().item() ) , 29.304 ) self.assertEqual(nested_simplify(outputs['''predicted_depth'''].min().item() ) , 2.662 ) @require_torch def lowercase__ ( self : str ): # This is highly irregular to have no small tests. self.skipTest('''There is not hf-internal-testing tiny model for either GLPN nor DPT''' )
35
0
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' snake_case_ : Optional[int] = tf.convert_to_tensor( [ [ 8.222_0991, # 3rd highest value; idx. 0 -0.562_0044, 5.2322_9752, 4.038_6393, -6.879_8378, -0.5478_5802, -3.201_2153, 2.9277_7176, 1.8817_1953, 7.3534_1276, # 5th highest value; idx. 9 8.4320_7833, # 2nd highest value; idx. 10 -9.8571_1836, -5.9620_9236, -1.1303_9161, -7.111_5294, -0.836_9633, -5.318_6408, 7.0642_7407, 0.8136_9344, -0.8202_3817, -5.917_9796, 0.5881_3443, -6.9977_8438, 4.7155_1189, -0.1877_1637, 7.4402_0759, # 4th highest value; idx. 25 9.3845_0987, # 1st highest value; idx. 26 2.1266_2941, -9.3256_2038, 2.3565_2522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.5842_5518, 4.5313_9238, -5.5751_0464, -6.2803_0699, -7.1952_9503, -4.0212_2551, 1.3933_7037, -6.0670_7057, 1.5948_0517, -9.64_3119, 0.0390_7799, 0.6723_1762, -8.8820_6726, 6.2711_5922, # 4th highest value; idx. 13 2.2852_0723, 4.8276_7506, 4.3042_1368, 8.827_5313, # 2nd highest value; idx. 17 5.4402_9958, # 5th highest value; idx. 18 -4.473_5794, 7.3857_9536, # 3rd highest value; idx. 20 -2.9105_1663, 2.6194_6077, -2.567_4762, -9.4895_9302, -4.0292_2645, -1.3541_6918, 9.6770_2323, # 1st highest value; idx. 27 -5.8947_8553, 1.8537_0467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) snake_case_ : Any = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 1_0], [0, 2_5], [0, 2_6], [1, 1_3], [1, 1_7], [1, 1_8], [1, 2_0], [1, 2_7]] , dtype=tf.intaa , ) # expected non filtered idx as noted above snake_case_ : Optional[Any] = tf.convert_to_tensor( [8.22_2099, 7.353_4126, 8.43_2078, 7.440_2075, 9.3_8451, 6.27_1159, 8.82_7531, 5.440_2995, 7.385_7956, 9.67_7023] , dtype=tf.floataa , ) # expected non filtered values as noted above snake_case_ : Optional[int] = tf_top_k_top_p_filtering(_lowercase , top_k=1_0 , top_p=0.6 , min_tokens_to_keep=4 ) snake_case_ : Union[str, Any] = output[output != -float("""inf""" )] snake_case_ : Dict = tf.cast( tf.where(tf.not_equal(_lowercase , tf.constant(-float("""inf""" ) , dtype=tf.floataa ) ) ) , dtype=tf.intaa , ) tf.debugging.assert_near(_lowercase , _lowercase , rtol=1E-12 ) tf.debugging.assert_equal(_lowercase , _lowercase ) @require_tf class _lowerCAmelCase ( unittest.TestCase , _UpperCAmelCase ): """simple docstring""" if is_tf_available(): _lowerCamelCase = { '''AutoModelForCausalLM''': TFAutoModelForCausalLM, '''AutoModelForSpeechSeq2Seq''': TFAutoModelForSpeechSeqaSeq, '''AutoModelForSeq2SeqLM''': TFAutoModelForSeqaSeqLM, '''AutoModelForVision2Seq''': TFAutoModelForVisionaSeq, '''LogitsProcessorList''': TFLogitsProcessorList, '''MinLengthLogitsProcessor''': TFMinLengthLogitsProcessor, '''create_tensor_fn''': tf.convert_to_tensor, '''floats_tensor''': floats_tensor, '''return_tensors''': '''tf''', } @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' snake_case_ : Any = TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""" ) snake_case_ : List[Any] = 2 snake_case_ : Dict = 2 class _lowerCAmelCase ( tf.Module ): """simple docstring""" def __init__( self , _lowercase ) -> Any: '''simple docstring''' super(_lowercase , self ).__init__() snake_case_ : Tuple = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name="""input_ids""" ), tf.TensorSpec((None, input_length) , tf.intaa , name="""attention_mask""" ), ) , jit_compile=_lowercase , ) def UpperCAmelCase__ ( self , _lowercase , _lowercase ) -> List[Any]: '''simple docstring''' snake_case_ : str = self.model.generate( input_ids=_lowercase , attention_mask=_lowercase , max_new_tokens=_lowercase , return_dict_in_generate=_lowercase , ) return {"sequences": outputs["sequences"]} snake_case_ : Any = [[2, 0], [1_0_2, 1_0_3]] snake_case_ : Any = [[1, 0], [1, 1]] snake_case_ : List[Any] = DummyModel(model=_lowercase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_lowercase , _lowercase , signatures={"""serving_default""": dummy_model.serving} ) snake_case_ : str = tf.saved_model.load(_lowercase ).signatures['''serving_default'''] for batch_size in range(1 , len(_lowercase ) + 1 ): snake_case_ : Union[str, Any] = { '''input_ids''': tf.constant(dummy_input_ids[:batch_size] ), '''attention_mask''': tf.constant(dummy_attention_masks[:batch_size] ), } snake_case_ : Optional[int] = serving_func(**_lowercase )['''sequences'''] snake_case_ : Optional[int] = test_model.generate(**_lowercase , max_new_tokens=_lowercase ) tf.debugging.assert_equal(_lowercase , _lowercase ) @slow def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' snake_case_ : List[str] = TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""" ) snake_case_ : Any = 1 snake_case_ : Tuple = 2 class _lowerCAmelCase ( tf.Module ): """simple docstring""" def __init__( self , _lowercase ) -> Any: '''simple docstring''' super(_lowercase , self ).__init__() snake_case_ : List[str] = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name="""input_ids""" ), tf.TensorSpec((batch_size, None) , tf.intaa , name="""attention_mask""" ), ) , jit_compile=_lowercase , ) def UpperCAmelCase__ ( self , _lowercase , _lowercase ) -> Union[str, Any]: '''simple docstring''' snake_case_ : Any = self.model.generate( input_ids=_lowercase , attention_mask=_lowercase , max_new_tokens=_lowercase , return_dict_in_generate=_lowercase , ) return {"sequences": outputs["sequences"]} snake_case_ : Union[str, Any] = [[2], [1_0_2, 1_0_3]] snake_case_ : int = [[1], [1, 1]] snake_case_ : Any = DummyModel(model=_lowercase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_lowercase , _lowercase , signatures={"""serving_default""": dummy_model.serving} ) snake_case_ : Optional[Any] = tf.saved_model.load(_lowercase ).signatures['''serving_default'''] for input_row in range(len(_lowercase ) ): snake_case_ : str = { '''input_ids''': tf.constant([dummy_input_ids[input_row]] ), '''attention_mask''': tf.constant([dummy_attention_masks[input_row]] ), } snake_case_ : List[str] = serving_func(**_lowercase )['''sequences'''] snake_case_ : Optional[int] = test_model.generate(**_lowercase , max_new_tokens=_lowercase ) tf.debugging.assert_equal(_lowercase , _lowercase ) @slow @require_tensorflow_text def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id="""google/flan-t5-small""" , filename="""spiece.model""" , local_dir=_lowercase ) class _lowerCAmelCase ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self ) -> Optional[int]: '''simple docstring''' super().__init__() snake_case_ : int = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(_lowercase , """spiece.model""" ) , """rb""" ).read() ) snake_case_ : Optional[Any] = TFAutoModelForSeqaSeqLM.from_pretrained("""hf-internal-testing/tiny-random-t5""" ) def UpperCAmelCase__ ( self , _lowercase , *_lowercase , **_lowercase ) -> Dict: '''simple docstring''' snake_case_ : Dict = self.tokenizer.tokenize(_lowercase ) snake_case_ : Union[str, Any] = text.pad_model_inputs( _lowercase , max_seq_length=6_4 , pad_value=self.model.config.pad_token_id ) snake_case_ : Optional[int] = self.model.generate(input_ids=_lowercase , attention_mask=_lowercase ) return self.tokenizer.detokenize(_lowercase ) snake_case_ : Any = CompleteSentenceTransformer() snake_case_ : str = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name="""inputs""" ) snake_case_ : Dict = complete_model(_lowercase ) snake_case_ : Optional[int] = tf.keras.Model(_lowercase , _lowercase ) keras_model.save(_lowercase ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' snake_case_ : Union[str, Any] = { '''do_sample''': True, '''num_beams''': 1, '''top_p''': 0.7, '''top_k''': 1_0, '''temperature''': 0.7, } snake_case_ : Any = 1_4 snake_case_ : Optional[Any] = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""" ) snake_case_ : Union[str, Any] = '''Hello, my dog is cute and''' snake_case_ : List[str] = tokenizer(_lowercase , return_tensors="""tf""" ) snake_case_ : List[str] = TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""" ) snake_case_ : List[str] = 6_3_8 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(""":/CPU:0""" ): tf.random.set_seed(0 ) snake_case_ : Dict = model.generate(**_lowercase , eos_token_id=_lowercase , **_lowercase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) snake_case_ : List[str] = [6_3_8, 1_9_8] with tf.device(""":/CPU:0""" ): tf.random.set_seed(0 ) snake_case_ : Optional[Any] = model.generate(**_lowercase , eos_token_id=_lowercase , **_lowercase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' snake_case_ : Union[str, Any] = AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-bart""" ) snake_case_ : List[Any] = '''Hugging Face is a technology company based in New York and Paris.''' snake_case_ : Optional[int] = bart_tokenizer(_lowercase , return_tensors="""tf""" ).input_ids snake_case_ : str = TFBartForConditionalGeneration.from_pretrained("""hf-internal-testing/tiny-random-bart""" ) snake_case_ : str = bart_model.generate(_lowercase ).numpy() class _lowerCAmelCase ( _UpperCAmelCase ): """simple docstring""" def UpperCAmelCase__ ( self , _lowercase , _lowercase=None , **_lowercase ) -> int: '''simple docstring''' return super().call(_lowercase , **_lowercase ) snake_case_ : List[Any] = FakeBart.from_pretrained("""hf-internal-testing/tiny-random-bart""" ) snake_case_ : List[Any] = bart_model.generate(_lowercase , foo="""bar""" ).numpy() self.assertTrue(np.array_equal(_lowercase , _lowercase ) ) class _lowerCAmelCase ( bart_model.model.encoder.__class__ ): """simple docstring""" def UpperCAmelCase__ ( self , _lowercase , **_lowercase ) -> List[Any]: '''simple docstring''' return super().call(_lowercase , **_lowercase ) snake_case_ : Dict = FakeEncoder(bart_model.config , bart_model.model.shared ) snake_case_ : List[Any] = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) snake_case_ : Dict = bart_model.generate(_lowercase ).numpy() with self.assertRaises(_lowercase ): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(_lowercase , foo="""bar""" )
58
def a ( A__ ) -> int: '''simple docstring''' if a < 0: raise ValueError('''Input value must be a positive integer''' ) elif isinstance(A__ , A__ ): raise TypeError('''Input value must be a \'int\' type''' ) return bin(A__ ).count('''1''' ) if __name__ == "__main__": import doctest doctest.testmod()
35
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case : str = {'configuration_xglm': ['XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XGLMConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Tuple = ['XGLMTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : List[Any] = ['XGLMTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Dict = [ 'XGLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'XGLMForCausalLM', 'XGLMModel', 'XGLMPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : List[Any] = [ 'FlaxXGLMForCausalLM', 'FlaxXGLMModel', 'FlaxXGLMPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Union[str, Any] = [ 'TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFXGLMForCausalLM', 'TFXGLMModel', 'TFXGLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys _snake_case : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure)
693
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL a_ :str = logging.get_logger(__name__) def a ( A__ , A__ , A__ , A__ ) -> Tuple[int, int]: '''simple docstring''' def constraint_to_multiple_of(A__ , A__ , A__=0 , A__=None ): SCREAMING_SNAKE_CASE__ : Optional[int] = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE__ : Any = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE__ : Any = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE__ : Union[str, Any] = (output_size, output_size) if isinstance(A__ , A__ ) else output_size SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple = get_image_size(A__ ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[str] = output_size # determine new height and width SCREAMING_SNAKE_CASE__ : List[str] = output_height / input_height SCREAMING_SNAKE_CASE__ : Dict = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE__ : List[str] = scale_width else: # fit height SCREAMING_SNAKE_CASE__ : Optional[Any] = scale_height SCREAMING_SNAKE_CASE__ : int = constraint_to_multiple_of(scale_height * input_height , multiple=A__ ) SCREAMING_SNAKE_CASE__ : int = constraint_to_multiple_of(scale_width * input_width , multiple=A__ ) return (new_height, new_width) class lowercase ( _UpperCAmelCase ): lowerCamelCase : List[str] = ['''pixel_values'''] def __init__( self : List[Any] , _lowercase : bool = True , _lowercase : Dict[str, int] = None , _lowercase : PILImageResampling = PILImageResampling.BILINEAR , _lowercase : bool = False , _lowercase : int = 1 , _lowercase : bool = True , _lowercase : Union[int, float] = 1 / 2_55 , _lowercase : bool = True , _lowercase : Optional[Union[float, List[float]]] = None , _lowercase : Optional[Union[float, List[float]]] = None , **_lowercase : List[Any] , ): super().__init__(**_lowercase ) SCREAMING_SNAKE_CASE__ : Dict = size if size is not None else {'''height''': 3_84, '''width''': 3_84} SCREAMING_SNAKE_CASE__ : Optional[int] = get_size_dict(_lowercase ) SCREAMING_SNAKE_CASE__ : List[str] = do_resize SCREAMING_SNAKE_CASE__ : Optional[int] = size SCREAMING_SNAKE_CASE__ : int = keep_aspect_ratio SCREAMING_SNAKE_CASE__ : Optional[Any] = ensure_multiple_of SCREAMING_SNAKE_CASE__ : List[str] = resample SCREAMING_SNAKE_CASE__ : Union[str, Any] = do_rescale SCREAMING_SNAKE_CASE__ : Optional[int] = rescale_factor SCREAMING_SNAKE_CASE__ : List[Any] = do_normalize SCREAMING_SNAKE_CASE__ : Tuple = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE__ : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowercase__ ( self : Optional[int] , _lowercase : np.ndarray , _lowercase : Dict[str, int] , _lowercase : bool = False , _lowercase : int = 1 , _lowercase : PILImageResampling = PILImageResampling.BICUBIC , _lowercase : Optional[Union[str, ChannelDimension]] = None , **_lowercase : Optional[int] , ): SCREAMING_SNAKE_CASE__ : List[Any] = get_size_dict(_lowercase ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = get_resize_output_image_size( _lowercase , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=_lowercase , multiple=_lowercase , ) return resize(_lowercase , size=_lowercase , resample=_lowercase , data_format=_lowercase , **_lowercase ) def lowercase__ ( self : List[str] , _lowercase : np.ndarray , _lowercase : Union[int, float] , _lowercase : Optional[Union[str, ChannelDimension]] = None , **_lowercase : str , ): return rescale(_lowercase , scale=_lowercase , data_format=_lowercase , **_lowercase ) def lowercase__ ( self : List[str] , _lowercase : np.ndarray , _lowercase : Union[float, List[float]] , _lowercase : Union[float, List[float]] , _lowercase : Optional[Union[str, ChannelDimension]] = None , **_lowercase : Optional[Any] , ): return normalize(_lowercase , mean=_lowercase , std=_lowercase , data_format=_lowercase , **_lowercase ) def lowercase__ ( self : Optional[Any] , _lowercase : ImageInput , _lowercase : bool = None , _lowercase : int = None , _lowercase : bool = None , _lowercase : int = None , _lowercase : PILImageResampling = None , _lowercase : bool = None , _lowercase : float = None , _lowercase : bool = None , _lowercase : Optional[Union[float, List[float]]] = None , _lowercase : Optional[Union[float, List[float]]] = None , _lowercase : Optional[Union[str, TensorType]] = None , _lowercase : ChannelDimension = ChannelDimension.FIRST , **_lowercase : Tuple , ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE__ : List[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE__ : List[str] = get_size_dict(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE__ : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE__ : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE__ : str = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE__ : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE__ : Optional[int] = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE__ : Tuple = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE__ : str = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE__ : Optional[Any] = make_list_of_images(_lowercase ) if not valid_images(_lowercase ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE__ : str = [to_numpy_array(_lowercase ) for image in images] if do_resize: SCREAMING_SNAKE_CASE__ : Any = [self.resize(image=_lowercase , size=_lowercase , resample=_lowercase ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE__ : Tuple = [self.rescale(image=_lowercase , scale=_lowercase ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE__ : Any = [self.normalize(image=_lowercase , mean=_lowercase , std=_lowercase ) for image in images] SCREAMING_SNAKE_CASE__ : Union[str, Any] = [to_channel_dimension_format(_lowercase , _lowercase ) for image in images] SCREAMING_SNAKE_CASE__ : str = {'''pixel_values''': images} return BatchFeature(data=_lowercase , tensor_type=_lowercase ) def lowercase__ ( self : Tuple , _lowercase : Optional[Any] , _lowercase : List[Tuple] = None ): SCREAMING_SNAKE_CASE__ : str = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_lowercase ) != len(_lowercase ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(_lowercase ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE__ : Tuple = [] for idx in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Dict = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=_lowercase ) SCREAMING_SNAKE_CASE__ : Any = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_lowercase ) else: SCREAMING_SNAKE_CASE__ : Any = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE__ : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
35
0
'''simple docstring''' import os import re import shutil from argparse import ArgumentParser, Namespace from datasets.commands import BaseDatasetsCLICommand from datasets.utils.logging import get_logger a__ : Any = '<<<<<<< This should probably be modified because it mentions: ' a__ : int = '=======\n>>>>>>>\n' a__ : Dict = [ 'TextEncoderConfig', 'ByteTextEncoder', 'SubwordTextEncoder', 'encoder_config', 'maybe_build_from_corpus', 'manual_dir', ] a__ : int = [ # (pattern, replacement) # Order is important here for some replacements (r'tfds\.core', r'datasets'), (r'tf\.io\.gfile\.GFile', r'open'), (r'tf\.([\w\d]+)', r'datasets.Value(\'\1\')'), (r'tfds\.features\.Text\(\)', r'datasets.Value(\'string\')'), (r'tfds\.features\.Text\(', r'datasets.Value(\'string\'),'), (r'features\s*=\s*tfds.features.FeaturesDict\(', r'features=datasets.Features('), (r'tfds\.features\.FeaturesDict\(', r'dict('), (r'The TensorFlow Datasets Authors', r'The TensorFlow Datasets Authors and the HuggingFace Datasets Authors'), (r'tfds\.', r'datasets.'), (r'dl_manager\.manual_dir', r'self.config.data_dir'), (r'self\.builder_config', r'self.config'), ] def __lowerCamelCase ( UpperCAmelCase_ ) ->List[Any]: return ConvertCommand(args.tfds_path , args.datasets_directory ) class __snake_case ( _UpperCAmelCase ): @staticmethod def _snake_case ( UpperCamelCase_ ) -> str: snake_case__ = parser.add_parser( 'convert' , help='Convert a TensorFlow Datasets dataset to a HuggingFace Datasets dataset.' , ) train_parser.add_argument( '--tfds_path' , type=_lowercase , required=_lowercase , help='Path to a TensorFlow Datasets folder to convert or a single tfds file to convert.' , ) train_parser.add_argument( '--datasets_directory' , type=_lowercase , required=_lowercase , help='Path to the HuggingFace Datasets folder.' ) train_parser.set_defaults(func=_lowercase ) def __init__( self , UpperCamelCase_ , UpperCamelCase_ , *UpperCamelCase_ ) -> Dict: snake_case__ = get_logger('datasets-cli/converting' ) snake_case__ = tfds_path snake_case__ = datasets_directory def _snake_case ( self ) -> Union[str, Any]: if os.path.isdir(self._tfds_path ): snake_case__ = os.path.abspath(self._tfds_path ) elif os.path.isfile(self._tfds_path ): snake_case__ = os.path.dirname(self._tfds_path ) else: raise ValueError('--tfds_path is neither a directory nor a file. Please check path.' ) snake_case__ = os.path.abspath(self._datasets_directory ) self._logger.info(F'''Converting datasets from {abs_tfds_path} to {abs_datasets_path}''' ) snake_case__ = [] snake_case__ = [] snake_case__ = {} if os.path.isdir(self._tfds_path ): snake_case__ = os.listdir(_lowercase ) else: snake_case__ = [os.path.basename(self._tfds_path )] for f_name in file_names: self._logger.info(F'''Looking at file {f_name}''' ) snake_case__ = os.path.join(_lowercase , _lowercase ) snake_case__ = os.path.join(_lowercase , _lowercase ) if not os.path.isfile(_lowercase ) or "__init__" in f_name or "_test" in f_name or ".py" not in f_name: self._logger.info('Skipping file' ) continue with open(_lowercase , encoding='utf-8' ) as f: snake_case__ = f.readlines() snake_case__ = [] snake_case__ = False snake_case__ = False snake_case__ = [] for line in lines: snake_case__ = line # Convert imports if "import tensorflow.compat.v2 as tf" in out_line: continue elif "@tfds.core" in out_line: continue elif "builder=self" in out_line: continue elif "import tensorflow_datasets.public_api as tfds" in out_line: snake_case__ = '''import datasets\n''' elif "import tensorflow" in out_line: # order is important here snake_case__ = '''''' continue elif "from absl import logging" in out_line: snake_case__ = '''from datasets import logging\n''' elif "getLogger" in out_line: snake_case__ = out_line.replace('getLogger' , 'get_logger' ) elif any(expression in out_line for expression in TO_HIGHLIGHT ): snake_case__ = True snake_case__ = list(filter(lambda UpperCamelCase_ : e in out_line , _lowercase ) ) out_lines.append(HIGHLIGHT_MESSAGE_PRE + str(_lowercase ) + '\n' ) out_lines.append(_lowercase ) out_lines.append(_lowercase ) continue else: for pattern, replacement in TO_CONVERT: snake_case__ = re.sub(_lowercase , _lowercase , _lowercase ) # Take care of saving utilities (to later move them together with main script) if "tensorflow_datasets" in out_line: snake_case__ = re.match(R'from\stensorflow_datasets.*import\s([^\.\r\n]+)' , _lowercase ) tfds_imports.extend(imp.strip() for imp in match.group(1 ).split(',' ) ) snake_case__ = '''from . import ''' + match.group(1 ) # Check we have not forget anything if "tf." in out_line or "tfds." in out_line or "tensorflow_datasets" in out_line: raise ValueError(F'''Error converting {out_line.strip()}''' ) if "GeneratorBasedBuilder" in out_line or "BeamBasedBuilder" in out_line: snake_case__ = True out_lines.append(_lowercase ) if is_builder or "wmt" in f_name: # We create a new directory for each dataset snake_case__ = f_name.replace('.py' , '' ) snake_case__ = os.path.join(_lowercase , _lowercase ) snake_case__ = os.path.join(_lowercase , _lowercase ) os.makedirs(_lowercase , exist_ok=_lowercase ) self._logger.info(F'''Adding directory {output_dir}''' ) imports_to_builder_map.update({imp: output_dir for imp in tfds_imports} ) else: # Utilities will be moved at the end utils_files.append(_lowercase ) if needs_manual_update: with_manual_update.append(_lowercase ) with open(_lowercase , 'w' , encoding='utf-8' ) as f: f.writelines(_lowercase ) self._logger.info(F'''Converted in {output_file}''' ) for utils_file in utils_files: try: snake_case__ = os.path.basename(_lowercase ) snake_case__ = imports_to_builder_map[f_name.replace('.py' , '' )] self._logger.info(F'''Moving {dest_folder} to {utils_file}''' ) shutil.copy(_lowercase , _lowercase ) except KeyError: self._logger.error(F'''Cannot find destination folder for {utils_file}. Please copy manually.''' ) if with_manual_update: for file_path in with_manual_update: self._logger.warning( F'''You need to manually update file {file_path} to remove configurations using \'TextEncoderConfig\'.''' )
368
from __future__ import annotations from typing import Any class lowercase : def __init__( self : int , _lowercase : int ): SCREAMING_SNAKE_CASE__ : List[str] = num_of_nodes SCREAMING_SNAKE_CASE__ : list[list[int]] = [] SCREAMING_SNAKE_CASE__ : dict[int, int] = {} def lowercase__ ( self : Union[str, Any] , _lowercase : int , _lowercase : int , _lowercase : int ): self.m_edges.append([u_node, v_node, weight] ) def lowercase__ ( self : Optional[int] , _lowercase : int ): if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def lowercase__ ( self : Optional[Any] , _lowercase : int ): if self.m_component[u_node] != u_node: for k in self.m_component: SCREAMING_SNAKE_CASE__ : Any = self.find_component(_lowercase ) def lowercase__ ( self : int , _lowercase : list[int] , _lowercase : int , _lowercase : int ): if component_size[u_node] <= component_size[v_node]: SCREAMING_SNAKE_CASE__ : Dict = v_node component_size[v_node] += component_size[u_node] self.set_component(_lowercase ) elif component_size[u_node] >= component_size[v_node]: SCREAMING_SNAKE_CASE__ : List[Any] = self.find_component(_lowercase ) component_size[u_node] += component_size[v_node] self.set_component(_lowercase ) def lowercase__ ( self : str ): SCREAMING_SNAKE_CASE__ : Optional[int] = [] SCREAMING_SNAKE_CASE__ : Any = 0 SCREAMING_SNAKE_CASE__ : list[Any] = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) SCREAMING_SNAKE_CASE__ : List[str] = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Union[str, Any] = edge SCREAMING_SNAKE_CASE__ : Tuple = self.m_component[u] SCREAMING_SNAKE_CASE__ : List[str] = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): SCREAMING_SNAKE_CASE__ : int = [u, v, w] for edge in minimum_weight_edge: if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[str] = edge SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.m_component[u] SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.m_component[v] if u_component != v_component: mst_weight += w self.union(_lowercase , _lowercase , _lowercase ) print(f"""Added edge [{u} - {v}]\nAdded weight: {w}\n""" ) num_of_components -= 1 SCREAMING_SNAKE_CASE__ : List[Any] = [-1] * self.m_num_of_nodes print(f"""The total weight of the minimal spanning tree is: {mst_weight}""" ) def a ( ) -> None: '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
35
0
'''simple docstring''' from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowercase__ : Optional[int] = {'UserAgent': UserAgent().random} def __lowerCamelCase ( _UpperCamelCase : Union[str, Any] ): '''simple docstring''' UpperCAmelCase_ = script.contents[0] UpperCAmelCase_ = json.loads(data[data.find('''{"config"''' ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class lowerCamelCase : '''simple docstring''' def __init__( self : str , UpperCAmelCase__ : Tuple ) ->List[Any]: UpperCAmelCase_ = f"""https://www.instagram.com/{username}/""" UpperCAmelCase_ = self.get_json() def lowerCAmelCase__ ( self : Any ) ->Union[str, Any]: UpperCAmelCase_ = requests.get(self.url , headers=_lowercase ).text UpperCAmelCase_ = BeautifulSoup(_lowercase , '''html.parser''' ).find_all('''script''' ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Dict ) ->Union[str, Any]: return f"""{self.__class__.__name__}('{self.username}')""" def __str__( self : List[Any] ) ->Any: return f"""{self.fullname} ({self.username}) is {self.biography}""" @property def lowerCAmelCase__ ( self : Any ) ->Tuple: return self.user_data["username"] @property def lowerCAmelCase__ ( self : Optional[Any] ) ->Union[str, Any]: return self.user_data["full_name"] @property def lowerCAmelCase__ ( self : int ) ->Union[str, Any]: return self.user_data["biography"] @property def lowerCAmelCase__ ( self : Optional[Any] ) ->Optional[int]: return self.user_data["business_email"] @property def lowerCAmelCase__ ( self : Dict ) ->Dict: return self.user_data["external_url"] @property def lowerCAmelCase__ ( self : List[Any] ) ->str: return self.user_data["edge_followed_by"]["count"] @property def lowerCAmelCase__ ( self : int ) ->Union[str, Any]: return self.user_data["edge_follow"]["count"] @property def lowerCAmelCase__ ( self : Optional[int] ) ->Tuple: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def lowerCAmelCase__ ( self : List[str] ) ->List[str]: return self.user_data["profile_pic_url_hd"] @property def lowerCAmelCase__ ( self : Tuple ) ->Tuple: return self.user_data["is_verified"] @property def lowerCAmelCase__ ( self : List[Any] ) ->Tuple: return self.user_data["is_private"] def __lowerCamelCase ( _UpperCamelCase : Optional[int] = "github" ): '''simple docstring''' import os if os.environ.get('''CI''' ): return # test failing on GitHub Actions UpperCAmelCase_ = InstagramUser(A__ ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , A__ ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 12_0000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "[email protected]" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith('''https://instagram.''' ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowercase__ : Optional[Any] = InstagramUser("github") print(instagram_user) print(F'''{instagram_user.number_of_posts = }''') print(F'''{instagram_user.number_of_followers = }''') print(F'''{instagram_user.number_of_followings = }''') print(F'''{instagram_user.email = }''') print(F'''{instagram_user.website = }''') print(F'''{instagram_user.profile_picture_url = }''') print(F'''{instagram_user.is_verified = }''') print(F'''{instagram_user.is_private = }''')
390
from typing import TYPE_CHECKING from ...utils import _LazyModule a_ :Tuple = {'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys a_ :Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
35
0
"""simple docstring""" def lowerCamelCase_ ( _lowerCamelCase : Dict , _lowerCamelCase : Optional[int] , _lowerCamelCase : str ): if principal <= 0: raise Exception('''Principal borrowed must be > 0''' ) if rate_per_annum < 0: raise Exception('''Rate of interest must be >= 0''' ) if years_to_repay <= 0 or not isinstance(A__ , A__ ): raise Exception('''Years to repay must be an integer > 0''' ) # Yearly rate is divided by 12 to get monthly rate lowerCamelCase_ = rate_per_annum / 1_2 # Years to repay is multiplied by 12 to get number of payments as payment is monthly lowerCamelCase_ = years_to_repay * 1_2 return ( principal * rate_per_month * (1 + rate_per_month) ** number_of_payments / ((1 + rate_per_month) ** number_of_payments - 1) ) if __name__ == "__main__": import doctest doctest.testmod()
142
def a ( A__ ) -> str: '''simple docstring''' return "".join([hex(A__ )[2:].zfill(2 ).upper() for byte in list(A__ )] ) def a ( A__ ) -> bytes: '''simple docstring''' if (len(A__ ) % 2) != 0: raise ValueError( '''Base16 encoded data is invalid: Data does not have an even number of hex digits.''' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(A__ ) <= set('''0123456789ABCDEF''' ): raise ValueError( '''Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.''' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 1_6 ) for i in range(0 , len(A__ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
35
0
"""simple docstring""" def UpperCamelCase ( _lowerCAmelCase : int ) -> float: if edge <= 0 or not isinstance(A__, A__ ): raise ValueError("""Length must be a positive.""" ) return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2) def UpperCamelCase ( _lowerCAmelCase : Union[str, Any] ) -> float: if edge <= 0 or not isinstance(A__, A__ ): raise ValueError("""Length must be a positive.""" ) return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3) if __name__ == "__main__": import doctest doctest.testmod()
238
import inspect import os import unittest from pathlib import Path import torch import accelerate from accelerate.test_utils import execute_subprocess_async from accelerate.test_utils.testing import run_command class lowercase ( unittest.TestCase ): lowerCamelCase : List[Any] = inspect.getfile(accelerate.test_utils ) lowerCamelCase : Optional[int] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_cli.py'''] ) lowerCamelCase : Any = ['''accelerate''', '''launch'''] lowerCamelCase : Dict = Path.home() / '''.cache/huggingface/accelerate''' lowerCamelCase : Optional[int] = '''default_config.yaml''' lowerCamelCase : Optional[Any] = config_folder / config_file lowerCamelCase : Optional[Any] = config_folder / '''_default_config.yaml''' lowerCamelCase : Optional[Any] = Path('''tests/test_configs''' ) @classmethod def lowercase__ ( cls : Any ): if cls.config_path.is_file(): cls.config_path.rename(cls.changed_path ) @classmethod def lowercase__ ( cls : List[Any] ): if cls.changed_path.is_file(): cls.changed_path.rename(cls.config_path ) def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Dict = self.base_cmd if torch.cuda.is_available() and (torch.cuda.device_count() > 1): cmd += ["--multi_gpu"] execute_subprocess_async(cmd + [self.test_file_path] , env=os.environ.copy() ) def lowercase__ ( self : Tuple ): for config in sorted(self.test_config_path.glob('''**/*.yaml''' ) ): with self.subTest(config_file=_lowercase ): execute_subprocess_async( self.base_cmd + ['''--config_file''', str(_lowercase ), self.test_file_path] , env=os.environ.copy() ) def lowercase__ ( self : Optional[int] ): execute_subprocess_async(['''accelerate''', '''test'''] , env=os.environ.copy() ) class lowercase ( unittest.TestCase ): lowerCamelCase : str = '''test-tpu''' lowerCamelCase : Tuple = '''us-central1-a''' lowerCamelCase : Optional[int] = '''ls''' lowerCamelCase : Dict = ['''accelerate''', '''tpu-config'''] lowerCamelCase : Tuple = '''cd /usr/share''' lowerCamelCase : List[Any] = '''tests/test_samples/test_command_file.sh''' lowerCamelCase : Any = '''Running gcloud compute tpus tpu-vm ssh''' def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = run_command( self.cmd + ['''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , _lowercase , ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : List[str] = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , _lowercase , ) def lowercase__ ( self : str ): SCREAMING_SNAKE_CASE__ : Optional[int] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--debug'''] , return_stdout=_lowercase ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : List[Any] ): SCREAMING_SNAKE_CASE__ : Optional[Any] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , _lowercase , ) def lowercase__ ( self : int ): SCREAMING_SNAKE_CASE__ : str = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--command''', '''echo "Hello World"''', '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all""" , _lowercase , ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : Any = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command_file''', self.command_file, '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Optional[int] = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command_file''', self.command_file, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : Any ): SCREAMING_SNAKE_CASE__ : List[Any] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : int ): SCREAMING_SNAKE_CASE__ : Optional[Any] = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--accelerate_version''', '''12.0.0''', '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , )
35
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase = logging.get_logger(__name__) _lowerCamelCase = { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json' ), # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text } class a ( _UpperCAmelCase ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = '''speech_to_text''' lowerCAmelCase : str = ['''past_key_values'''] lowerCAmelCase : Optional[Any] = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Any , __snake_case : List[Any]=1_00_00 , __snake_case : Union[str, Any]=12 , __snake_case : int=20_48 , __snake_case : Tuple=4 , __snake_case : Any=6 , __snake_case : int=20_48 , __snake_case : str=4 , __snake_case : Optional[Any]=0.0 , __snake_case : List[str]=0.0 , __snake_case : Dict=True , __snake_case : List[str]=True , __snake_case : List[str]="relu" , __snake_case : List[str]=2_56 , __snake_case : str=0.1 , __snake_case : Tuple=0.0 , __snake_case : List[Any]=0.0 , __snake_case : int=0.02 , __snake_case : int=2 , __snake_case : Any=True , __snake_case : List[str]=1 , __snake_case : List[str]=0 , __snake_case : Tuple=2 , __snake_case : Dict=60_00 , __snake_case : str=10_24 , __snake_case : int=2 , __snake_case : Any=(5, 5) , __snake_case : Tuple=10_24 , __snake_case : Optional[Any]=80 , __snake_case : Tuple=1 , **__snake_case : Optional[int] , ): UpperCAmelCase_ = vocab_size UpperCAmelCase_ = d_model UpperCAmelCase_ = encoder_ffn_dim UpperCAmelCase_ = encoder_layers UpperCAmelCase_ = encoder_attention_heads UpperCAmelCase_ = decoder_ffn_dim UpperCAmelCase_ = decoder_layers UpperCAmelCase_ = decoder_attention_heads UpperCAmelCase_ = dropout UpperCAmelCase_ = attention_dropout UpperCAmelCase_ = activation_dropout UpperCAmelCase_ = activation_function UpperCAmelCase_ = init_std UpperCAmelCase_ = encoder_layerdrop UpperCAmelCase_ = decoder_layerdrop UpperCAmelCase_ = use_cache UpperCAmelCase_ = encoder_layers UpperCAmelCase_ = scale_embedding # scale factor will be sqrt(d_model) if True UpperCAmelCase_ = max_source_positions UpperCAmelCase_ = max_target_positions UpperCAmelCase_ = num_conv_layers UpperCAmelCase_ = list(_lowercase ) UpperCAmelCase_ = conv_channels UpperCAmelCase_ = input_feat_per_channel UpperCAmelCase_ = input_channels if len(self.conv_kernel_sizes ) != self.num_conv_layers: raise ValueError( '''Configuration for convolutional module is incorrect. ''' '''It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` ''' F'but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes )}`, ' F'`config.num_conv_layers = {self.num_conv_layers}`.' ) super().__init__( pad_token_id=_lowercase , bos_token_id=_lowercase , eos_token_id=_lowercase , is_encoder_decoder=_lowercase , decoder_start_token_id=_lowercase , **_lowercase , )
144
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available a_ :List[str] = { 'configuration_groupvit': [ 'GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GroupViTConfig', 'GroupViTOnnxConfig', 'GroupViTTextConfig', 'GroupViTVisionConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ :str = [ 'GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GroupViTModel', 'GroupViTPreTrainedModel', 'GroupViTTextModel', 'GroupViTVisionModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ :List[Any] = [ 'TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFGroupViTModel', 'TFGroupViTPreTrainedModel', 'TFGroupViTTextModel', 'TFGroupViTVisionModel', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys a_ :Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
35
0
import requests from bsa import BeautifulSoup def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" _A = BeautifulSoup(requests.get(A__ , params=A__ ).content , 'html.parser' ) _A = soup.find('div' , attrs={'class': 'gs_ri'} ) _A = div.find('div' , attrs={'class': 'gs_fl'} ).find_all('a' ) return anchors[2].get_text() if __name__ == "__main__": __A : Optional[int] = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
27
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase ( _UpperCAmelCase ): def lowercase__ ( self : Optional[int] ): return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def lowercase__ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE__ : List[str] = {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']} return Dataset.from_dict(_lowercase ) def lowercase__ ( self : Dict ): SCREAMING_SNAKE_CASE__ : List[Any] = self._create_example_records() SCREAMING_SNAKE_CASE__ : int = Dataset.from_list(_lowercase ) self.assertListEqual(dset.column_names , ['''col_1''', '''col_2'''] ) for i, r in enumerate(_lowercase ): self.assertDictEqual(_lowercase , example_records[i] ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : Dict = self._create_example_records() SCREAMING_SNAKE_CASE__ : Optional[int] = Dataset.from_list(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def lowercase__ ( self : List[Any] ): # checks what happens with missing columns SCREAMING_SNAKE_CASE__ : List[str] = [{'''col_1''': 1}, {'''col_2''': '''x'''}] SCREAMING_SNAKE_CASE__ : Union[str, Any] = Dataset.from_list(_lowercase ) self.assertDictEqual(dset[0] , {'''col_1''': 1} ) self.assertDictEqual(dset[1] , {'''col_1''': None} ) # NB: first record is used for columns def lowercase__ ( self : int ): # checks if the type can be inferred from the second record SCREAMING_SNAKE_CASE__ : int = [{'''col_1''': []}, {'''col_1''': [1, 2]}] SCREAMING_SNAKE_CASE__ : int = Dataset.from_list(_lowercase ) self.assertEqual(dset.info.features['''col_1'''] , Sequence(Value('''int64''' ) ) ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : int = Dataset.from_list([] ) self.assertEqual(len(_lowercase ) , 0 ) self.assertListEqual(dset.column_names , [] )
35
0
import collections import os import re from pathlib import Path __a : Union[str, Any] = 'src/transformers' # Matches is_xxx_available() __a : int = re.compile(R"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} __a : List[Any] = re.compile(R"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __a : Any = re.compile(R"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available __a : int = re.compile(R"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") __a : Union[str, Any] = 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 : Union[str, Any] = re.compile(R"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", __a : Optional[int] = re.compile(R"^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], __a : Union[str, Any] = re.compile(R"^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo __a : Optional[Any] = re.compile(R"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: __a : Tuple = re.compile(R"^\s*try:") # Catches a line with else: __a : Union[str, Any] = re.compile(R"^\s*else:") def _SCREAMING_SNAKE_CASE ( __lowercase : Optional[int] ) -> int: """simple docstring""" if _re_test_backend.search(A__ ) is None: return None __A = [b[0] for b in _re_backend.findall(A__ )] backends.sort() return "_and_".join(A__ ) def _SCREAMING_SNAKE_CASE ( __lowercase : Union[str, Any] ) -> Any: """simple docstring""" with open(A__ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __A = f.readlines() __A = 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 = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: __A = 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 = _re_one_line_import_struct.search(A__ ).groups()[0] __A = re.findall(R"""\[([^\]]+)\]""" , A__ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue __A = _re_import_struct_key_value.search(A__ ) if single_line_import_search is not None: __A = [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 = {'''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 = 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 = 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 = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): __A = 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 = _re_import_struct_add_many.search(A__ ).groups()[0].split(""", """ ) __A = [obj[1:-1] for obj in imports if len(A__ ) > 0] objects.extend(A__ ) elif _re_between_brackets.search(A__ ) is not None: __A = _re_between_brackets.search(A__ ).groups()[0].split(""", """ ) __A = [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(""" """ * 1_2 + """\"""" ): objects.append(line[1_3:-3] ) line_index += 1 __A = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend __A = [] while ( line_index < len(A__ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): __A = lines[line_index] __A = _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 = {'''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 = 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 = 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 = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): __A = lines[line_index] __A = _re_import.search(A__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 1_2 ): objects.append(line[1_2:-2] ) line_index += 1 __A = objects else: line_index += 1 return import_dict_objects, type_hint_objects def _SCREAMING_SNAKE_CASE ( __lowercase : Any , __lowercase : Dict ) -> List[str]: """simple docstring""" def find_duplicates(__lowercase : List[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 = [] for key in import_dict_objects.keys(): __A = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(f"Duplicate _import_structure definitions for: {duplicate_imports}" ) __A = 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 = '''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 _SCREAMING_SNAKE_CASE ( ) -> str: """simple docstring""" __A = [] for root, _, files in os.walk(A__ ): if "__init__.py" in files: __A = os.path.join(A__ , """__init__.py""" ) __A = parse_init(A__ ) if objects is not None: __A = analyze_results(*A__ ) if len(A__ ) > 0: __A = 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 _SCREAMING_SNAKE_CASE ( ) -> Dict: """simple docstring""" __A = [] 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 = str((Path(A__ ) / folder).relative_to(A__ ) ) __A = short_path.replace(os.path.sep , """.""" ) submodules.append(A__ ) for fname in files: if fname == "__init__.py": continue __A = str((Path(A__ ) / fname).relative_to(A__ ) ) __A = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(A__ ) return submodules __a : Any = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', 'models.esm.openfold_utils', ] def _SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: """simple docstring""" from transformers.utils import direct_transformers_import __A = direct_transformers_import(A__ ) __A = 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 = f.read() import_structure_keys.update(set(re.findall(R"""import_structure\[\"([^\"]*)\"\]""" , A__ ) ) ) __A = [ 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 = '''\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()
637
import pickle import numpy as np from matplotlib import pyplot as plt class lowercase : def __init__( self : List[str] , _lowercase : Tuple , _lowercase : List[Any] , _lowercase : Tuple , _lowercase : Any , _lowercase : Optional[int] , _lowercase : str=0.2 , _lowercase : str=0.2 ): SCREAMING_SNAKE_CASE__ : List[Any] = bp_numa SCREAMING_SNAKE_CASE__ : Union[str, Any] = bp_numa SCREAMING_SNAKE_CASE__ : Union[str, Any] = bp_numa SCREAMING_SNAKE_CASE__ : List[str] = conva_get[:2] SCREAMING_SNAKE_CASE__ : str = conva_get[2] SCREAMING_SNAKE_CASE__ : Any = size_pa SCREAMING_SNAKE_CASE__ : Union[str, Any] = rate_w SCREAMING_SNAKE_CASE__ : Tuple = rate_t SCREAMING_SNAKE_CASE__ : Union[str, Any] = [ np.mat(-1 * np.random.rand(self.conva[0] , self.conva[0] ) + 0.5 ) for i in range(self.conva[1] ) ] SCREAMING_SNAKE_CASE__ : Dict = np.mat(-1 * np.random.rand(self.num_bpa , self.num_bpa ) + 0.5 ) SCREAMING_SNAKE_CASE__ : int = np.mat(-1 * np.random.rand(self.num_bpa , self.num_bpa ) + 0.5 ) SCREAMING_SNAKE_CASE__ : str = -2 * np.random.rand(self.conva[1] ) + 1 SCREAMING_SNAKE_CASE__ : Dict = -2 * np.random.rand(self.num_bpa ) + 1 SCREAMING_SNAKE_CASE__ : str = -2 * np.random.rand(self.num_bpa ) + 1 def lowercase__ ( self : Union[str, Any] , _lowercase : Any ): # save model dict with pickle SCREAMING_SNAKE_CASE__ : Dict = { '''num_bp1''': self.num_bpa, '''num_bp2''': self.num_bpa, '''num_bp3''': self.num_bpa, '''conv1''': self.conva, '''step_conv1''': self.step_conva, '''size_pooling1''': self.size_poolinga, '''rate_weight''': self.rate_weight, '''rate_thre''': self.rate_thre, '''w_conv1''': self.w_conva, '''wkj''': self.wkj, '''vji''': self.vji, '''thre_conv1''': self.thre_conva, '''thre_bp2''': self.thre_bpa, '''thre_bp3''': self.thre_bpa, } with open(_lowercase , '''wb''' ) as f: pickle.dump(_lowercase , _lowercase ) print(f"""Model saved: {save_path}""" ) @classmethod def lowercase__ ( cls : Dict , _lowercase : int ): # read saved model with open(_lowercase , '''rb''' ) as f: SCREAMING_SNAKE_CASE__ : Optional[Any] = pickle.load(_lowercase ) # noqa: S301 SCREAMING_SNAKE_CASE__ : Tuple = model_dic.get('''conv1''' ) conv_get.append(model_dic.get('''step_conv1''' ) ) SCREAMING_SNAKE_CASE__ : Tuple = model_dic.get('''size_pooling1''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model_dic.get('''num_bp1''' ) SCREAMING_SNAKE_CASE__ : Dict = model_dic.get('''num_bp2''' ) SCREAMING_SNAKE_CASE__ : Dict = model_dic.get('''num_bp3''' ) SCREAMING_SNAKE_CASE__ : Optional[int] = model_dic.get('''rate_weight''' ) SCREAMING_SNAKE_CASE__ : str = model_dic.get('''rate_thre''' ) # create model instance SCREAMING_SNAKE_CASE__ : Dict = CNN(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) # modify model parameter SCREAMING_SNAKE_CASE__ : List[str] = model_dic.get('''w_conv1''' ) SCREAMING_SNAKE_CASE__ : Optional[int] = model_dic.get('''wkj''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model_dic.get('''vji''' ) SCREAMING_SNAKE_CASE__ : str = model_dic.get('''thre_conv1''' ) SCREAMING_SNAKE_CASE__ : Any = model_dic.get('''thre_bp2''' ) SCREAMING_SNAKE_CASE__ : List[Any] = model_dic.get('''thre_bp3''' ) return conv_ins def lowercase__ ( self : str , _lowercase : Optional[int] ): return 1 / (1 + np.exp(-1 * x )) def lowercase__ ( self : Union[str, Any] , _lowercase : List[str] ): return round(_lowercase , 3 ) def lowercase__ ( self : List[str] , _lowercase : Union[str, Any] , _lowercase : int , _lowercase : Union[str, Any] , _lowercase : Union[str, Any] , _lowercase : Union[str, Any] ): # convolution process SCREAMING_SNAKE_CASE__ : Tuple = convs[0] SCREAMING_SNAKE_CASE__ : Optional[Any] = convs[1] SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.shape(_lowercase )[0] # get the data slice of original image data, data_focus SCREAMING_SNAKE_CASE__ : List[str] = [] for i_focus in range(0 , size_data - size_conv + 1 , _lowercase ): for j_focus in range(0 , size_data - size_conv + 1 , _lowercase ): SCREAMING_SNAKE_CASE__ : Optional[Any] = data[ i_focus : i_focus + size_conv, j_focus : j_focus + size_conv ] data_focus.append(_lowercase ) # calculate the feature map of every single kernel, and saved as list of matrix SCREAMING_SNAKE_CASE__ : Optional[Any] = [] SCREAMING_SNAKE_CASE__ : Union[str, Any] = int((size_data - size_conv) / conv_step + 1 ) for i_map in range(_lowercase ): SCREAMING_SNAKE_CASE__ : int = [] for i_focus in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Tuple = ( np.sum(np.multiply(data_focus[i_focus] , w_convs[i_map] ) ) - thre_convs[i_map] ) featuremap.append(self.sig(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.asmatrix(_lowercase ).reshape( _lowercase , _lowercase ) data_featuremap.append(_lowercase ) # expanding the data slice to One dimenssion SCREAMING_SNAKE_CASE__ : int = [] for each_focus in data_focus: focusa_list.extend(self.Expand_Mat(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.asarray(_lowercase ) return focus_list, data_featuremap def lowercase__ ( self : List[Any] , _lowercase : Tuple , _lowercase : Union[str, Any] , _lowercase : Optional[Any]="average_pool" ): # pooling process SCREAMING_SNAKE_CASE__ : List[str] = len(featuremaps[0] ) SCREAMING_SNAKE_CASE__ : List[Any] = int(size_map / size_pooling ) SCREAMING_SNAKE_CASE__ : List[str] = [] for i_map in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Any = featuremaps[i_map] SCREAMING_SNAKE_CASE__ : int = [] for i_focus in range(0 , _lowercase , _lowercase ): for j_focus in range(0 , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE__ : Dict = feature_map[ i_focus : i_focus + size_pooling, j_focus : j_focus + size_pooling, ] if pooling_type == "average_pool": # average pooling map_pooled.append(np.average(_lowercase ) ) elif pooling_type == "max_pooling": # max pooling map_pooled.append(np.max(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.asmatrix(_lowercase ).reshape(_lowercase , _lowercase ) featuremap_pooled.append(_lowercase ) return featuremap_pooled def lowercase__ ( self : Optional[Any] , _lowercase : Optional[Any] ): # expanding three dimension data to one dimension list SCREAMING_SNAKE_CASE__ : Dict = [] for i in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Optional[Any] = np.shape(data[i] ) SCREAMING_SNAKE_CASE__ : Tuple = data[i].reshape(1 , shapes[0] * shapes[1] ) SCREAMING_SNAKE_CASE__ : Dict = data_listed.getA().tolist()[0] data_expanded.extend(_lowercase ) SCREAMING_SNAKE_CASE__ : List[str] = np.asarray(_lowercase ) return data_expanded def lowercase__ ( self : Tuple , _lowercase : Optional[int] ): # expanding matrix to one dimension list SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.asarray(_lowercase ) SCREAMING_SNAKE_CASE__ : Any = np.shape(_lowercase ) SCREAMING_SNAKE_CASE__ : str = data_mat.reshape(1 , shapes[0] * shapes[1] ) return data_expanded def lowercase__ ( self : List[str] , _lowercase : List[str] , _lowercase : List[str] , _lowercase : Any , _lowercase : Optional[Any] , _lowercase : str ): SCREAMING_SNAKE_CASE__ : Optional[int] = [] SCREAMING_SNAKE_CASE__ : Dict = 0 for i_map in range(_lowercase ): SCREAMING_SNAKE_CASE__ : Any = np.ones((size_map, size_map) ) for i in range(0 , _lowercase , _lowercase ): for j in range(0 , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE__ : Tuple = pd_pool[ i_pool ] SCREAMING_SNAKE_CASE__ : Dict = i_pool + 1 SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.multiply( _lowercase , np.multiply(out_map[i_map] , (1 - out_map[i_map]) ) ) pd_all.append(_lowercase ) return pd_all def lowercase__ ( self : List[Any] , _lowercase : Any , _lowercase : Tuple , _lowercase : Optional[int] , _lowercase : Any , _lowercase : Tuple , _lowercase : int=bool ): # model traning print('''----------------------Start Training-------------------------''' ) print((''' - - Shape: Train_Data ''', np.shape(_lowercase )) ) print((''' - - Shape: Teach_Data ''', np.shape(_lowercase )) ) SCREAMING_SNAKE_CASE__ : Any = 0 SCREAMING_SNAKE_CASE__ : Tuple = [] SCREAMING_SNAKE_CASE__ : Optional[int] = 1_00_00 while rp < n_repeat and mse >= error_accuracy: SCREAMING_SNAKE_CASE__ : List[Any] = 0 print(f"""-------------Learning Time {rp}--------------""" ) for p in range(len(_lowercase ) ): # print('------------Learning Image: %d--------------'%p) SCREAMING_SNAKE_CASE__ : Any = np.asmatrix(datas_train[p] ) SCREAMING_SNAKE_CASE__ : str = np.asarray(datas_teach[p] ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] = self.convolute( _lowercase , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) SCREAMING_SNAKE_CASE__ : int = self.pooling(_lowercase , self.size_poolinga ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.shape(_lowercase ) SCREAMING_SNAKE_CASE__ : Dict = self._expand(_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = data_bp_input SCREAMING_SNAKE_CASE__ : Optional[int] = np.dot(_lowercase , self.vji.T ) - self.thre_bpa SCREAMING_SNAKE_CASE__ : Any = self.sig(_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.dot(_lowercase , self.wkj.T ) - self.thre_bpa SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.sig(_lowercase ) # --------------Model Leaning ------------------------ # calculate error and gradient--------------- SCREAMING_SNAKE_CASE__ : Tuple = np.multiply( (data_teach - bp_outa) , np.multiply(_lowercase , (1 - bp_outa) ) ) SCREAMING_SNAKE_CASE__ : List[Any] = np.multiply( np.dot(_lowercase , self.wkj ) , np.multiply(_lowercase , (1 - bp_outa) ) ) SCREAMING_SNAKE_CASE__ : List[Any] = np.dot(_lowercase , self.vji ) SCREAMING_SNAKE_CASE__ : Dict = pd_i_all / (self.size_poolinga * self.size_poolinga) SCREAMING_SNAKE_CASE__ : List[str] = pd_conva_pooled.T.getA().tolist() SCREAMING_SNAKE_CASE__ : Union[str, Any] = self._calculate_gradient_from_pool( _lowercase , _lowercase , shape_featuremapa[0] , shape_featuremapa[1] , self.size_poolinga , ) # weight and threshold learning process--------- # convolution layer for k_conv in range(self.conva[1] ): SCREAMING_SNAKE_CASE__ : Optional[Any] = self._expand_mat(pd_conva_all[k_conv] ) SCREAMING_SNAKE_CASE__ : Dict = self.rate_weight * np.dot(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self.w_conva[k_conv] + delta_w.reshape( (self.conva[0], self.conva[0]) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = ( self.thre_conva[k_conv] - np.sum(pd_conva_all[k_conv] ) * self.rate_thre ) # all connected layer SCREAMING_SNAKE_CASE__ : List[str] = self.wkj + pd_k_all.T * bp_outa * self.rate_weight SCREAMING_SNAKE_CASE__ : Optional[Any] = self.vji + pd_j_all.T * bp_outa * self.rate_weight SCREAMING_SNAKE_CASE__ : Optional[Any] = self.thre_bpa - pd_k_all * self.rate_thre SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.thre_bpa - pd_j_all * self.rate_thre # calculate the sum error of all single image SCREAMING_SNAKE_CASE__ : int = np.sum(abs(data_teach - bp_outa ) ) error_count += errors # print(' ----Teach ',data_teach) # print(' ----BP_output ',bp_out3) SCREAMING_SNAKE_CASE__ : Optional[Any] = rp + 1 SCREAMING_SNAKE_CASE__ : List[str] = error_count / patterns all_mse.append(_lowercase ) def draw_error(): SCREAMING_SNAKE_CASE__ : Union[str, Any] = [error_accuracy for i in range(int(n_repeat * 1.2 ) )] plt.plot(_lowercase , '''+-''' ) plt.plot(_lowercase , '''r--''' ) plt.xlabel('''Learning Times''' ) plt.ylabel('''All_mse''' ) plt.grid(_lowercase , alpha=0.5 ) plt.show() print('''------------------Training Complished---------------------''' ) print((''' - - Training epoch: ''', rp, f""" - - Mse: {mse:.6f}""") ) if draw_e: draw_error() return mse def lowercase__ ( self : Union[str, Any] , _lowercase : int ): # model predict SCREAMING_SNAKE_CASE__ : Dict = [] print('''-------------------Start Testing-------------------------''' ) print((''' - - Shape: Test_Data ''', np.shape(_lowercase )) ) for p in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Optional[int] = np.asmatrix(datas_test[p] ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[Any] = self.convolute( _lowercase , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) SCREAMING_SNAKE_CASE__ : Any = self.pooling(_lowercase , self.size_poolinga ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self._expand(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = data_bp_input SCREAMING_SNAKE_CASE__ : Optional[int] = bp_outa * self.vji.T - self.thre_bpa SCREAMING_SNAKE_CASE__ : Tuple = self.sig(_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = bp_outa * self.wkj.T - self.thre_bpa SCREAMING_SNAKE_CASE__ : Optional[Any] = self.sig(_lowercase ) produce_out.extend(bp_outa.getA().tolist() ) SCREAMING_SNAKE_CASE__ : str = [list(map(self.do_round , _lowercase ) ) for each in produce_out] return np.asarray(_lowercase ) def lowercase__ ( self : Optional[int] , _lowercase : Tuple ): # return the data of image after convoluting process so we can check it out SCREAMING_SNAKE_CASE__ : str = np.asmatrix(_lowercase ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Any = self.convolute( _lowercase , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) SCREAMING_SNAKE_CASE__ : Dict = self.pooling(_lowercase , self.size_poolinga ) return data_conveda, data_pooleda if __name__ == "__main__": pass
35
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase__: Union[str, Any] = { 'configuration_roformer': ['ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RoFormerConfig', 'RoFormerOnnxConfig'], 'tokenization_roformer': ['RoFormerTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__: Union[str, Any] = ['RoFormerTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__: int = [ 'ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'RoFormerForCausalLM', 'RoFormerForMaskedLM', 'RoFormerForMultipleChoice', 'RoFormerForQuestionAnswering', 'RoFormerForSequenceClassification', 'RoFormerForTokenClassification', 'RoFormerLayer', 'RoFormerModel', 'RoFormerPreTrainedModel', 'load_tf_weights_in_roformer', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__: List[Any] = [ 'TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRoFormerForCausalLM', 'TFRoFormerForMaskedLM', 'TFRoFormerForMultipleChoice', 'TFRoFormerForQuestionAnswering', 'TFRoFormerForSequenceClassification', 'TFRoFormerForTokenClassification', 'TFRoFormerLayer', 'TFRoFormerModel', 'TFRoFormerPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__: List[Any] = [ 'FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'FlaxRoFormerForMaskedLM', 'FlaxRoFormerForMultipleChoice', 'FlaxRoFormerForQuestionAnswering', 'FlaxRoFormerForSequenceClassification', 'FlaxRoFormerForTokenClassification', 'FlaxRoFormerModel', 'FlaxRoFormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys lowerCAmelCase__: Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
345
import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class lowercase : def __init__( self : Any , _lowercase : List[Any] , _lowercase : Optional[Any]=99 , _lowercase : Optional[int]=13 , _lowercase : Tuple=16 , _lowercase : Union[str, Any]=7 , _lowercase : Optional[Any]=True , _lowercase : int=True , _lowercase : Optional[Any]=True , _lowercase : str=False , _lowercase : Union[str, Any]=True , _lowercase : Tuple=2 , _lowercase : Any=32 , _lowercase : int=4 , _lowercase : Dict=4 , _lowercase : Dict=30 , _lowercase : Union[str, Any]=0 , _lowercase : List[str]=1 , _lowercase : Optional[Any]=2 , _lowercase : Tuple=None , ): SCREAMING_SNAKE_CASE__ : Any = parent SCREAMING_SNAKE_CASE__ : List[Any] = batch_size SCREAMING_SNAKE_CASE__ : List[str] = decoder_seq_length # For common tests SCREAMING_SNAKE_CASE__ : Optional[Any] = self.decoder_seq_length SCREAMING_SNAKE_CASE__ : Optional[int] = is_training SCREAMING_SNAKE_CASE__ : Tuple = use_attention_mask SCREAMING_SNAKE_CASE__ : Any = use_labels SCREAMING_SNAKE_CASE__ : Any = vocab_size SCREAMING_SNAKE_CASE__ : Union[str, Any] = d_model SCREAMING_SNAKE_CASE__ : Tuple = d_model SCREAMING_SNAKE_CASE__ : Optional[int] = decoder_layers SCREAMING_SNAKE_CASE__ : List[str] = decoder_layers SCREAMING_SNAKE_CASE__ : Optional[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE__ : List[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE__ : Optional[int] = decoder_attention_heads SCREAMING_SNAKE_CASE__ : str = eos_token_id SCREAMING_SNAKE_CASE__ : List[Any] = bos_token_id SCREAMING_SNAKE_CASE__ : str = pad_token_id SCREAMING_SNAKE_CASE__ : str = decoder_start_token_id SCREAMING_SNAKE_CASE__ : Optional[Any] = use_cache SCREAMING_SNAKE_CASE__ : Optional[int] = max_position_embeddings SCREAMING_SNAKE_CASE__ : Tuple = None SCREAMING_SNAKE_CASE__ : int = decoder_seq_length SCREAMING_SNAKE_CASE__ : Optional[int] = 2 SCREAMING_SNAKE_CASE__ : Tuple = 1 def lowercase__ ( self : Dict ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ : Optional[Any] = None if self.use_attention_mask: SCREAMING_SNAKE_CASE__ : List[str] = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = None if self.use_labels: SCREAMING_SNAKE_CASE__ : List[Any] = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ : Optional[int] = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def lowercase__ ( self : Dict , _lowercase : Any , _lowercase : Dict , _lowercase : Optional[Any] , _lowercase : Optional[Any] , ): SCREAMING_SNAKE_CASE__ : Dict = True SCREAMING_SNAKE_CASE__ : Optional[int] = TrOCRDecoder(config=_lowercase ).to(_lowercase ).eval() SCREAMING_SNAKE_CASE__ : Optional[int] = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass SCREAMING_SNAKE_CASE__ : Optional[Any] = model(_lowercase , use_cache=_lowercase ) SCREAMING_SNAKE_CASE__ : List[str] = model(_lowercase ) SCREAMING_SNAKE_CASE__ : Tuple = model(_lowercase , use_cache=_lowercase ) self.parent.assertTrue(len(_lowercase ) == len(_lowercase ) ) self.parent.assertTrue(len(_lowercase ) == len(_lowercase ) + 1 ) SCREAMING_SNAKE_CASE__ : int = outputs['''past_key_values'''] # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE__ : List[str] = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and SCREAMING_SNAKE_CASE__ : Tuple = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE__ : int = model(_lowercase )['''last_hidden_state'''] SCREAMING_SNAKE_CASE__ : List[Any] = model(_lowercase , past_key_values=_lowercase )['''last_hidden_state'''] # select random slice SCREAMING_SNAKE_CASE__ : int = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE__ : Dict = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() SCREAMING_SNAKE_CASE__ : str = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(_lowercase , _lowercase , atol=1E-3 ) def lowercase__ ( self : Optional[int] ): SCREAMING_SNAKE_CASE__ : Optional[int] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE__ : int = {'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_torch class lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): lowerCamelCase : List[str] = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () lowerCamelCase : Dict = (TrOCRForCausalLM,) if is_torch_available() else () lowerCamelCase : Tuple = {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {} lowerCamelCase : Any = True lowerCamelCase : int = False def lowercase__ ( self : List[Any] ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = TrOCRStandaloneDecoderModelTester(self , is_training=_lowercase ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = ConfigTester(self , config_class=_lowercase ) def lowercase__ ( self : Optional[Any] ): pass def lowercase__ ( self : List[Any] ): pass def lowercase__ ( self : str ): pass def lowercase__ ( self : Dict ): self.config_tester.run_common_tests() def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*_lowercase ) def lowercase__ ( self : Optional[Any] ): return @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def lowercase__ ( self : Tuple ): pass
35
0
def _lowerCamelCase( __snake_case = 1000 ) -> int: __snake_case = 3 __snake_case = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(F"{solution() = }")
524
import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( _UpperCAmelCase , unittest.TestCase ): lowerCamelCase : Tuple = LayoutLMTokenizer lowerCamelCase : Any = LayoutLMTokenizerFast lowerCamelCase : Tuple = True lowerCamelCase : List[Any] = True def lowercase__ ( self : Optional[int] ): super().setUp() SCREAMING_SNAKE_CASE__ : Optional[int] = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE__ : str = 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 : Optional[int] , **_lowercase : str ): return LayoutLMTokenizer.from_pretrained(self.tmpdirname , **_lowercase ) def lowercase__ ( self : Optional[Any] , _lowercase : Any ): SCREAMING_SNAKE_CASE__ : str = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE__ : Any = '''unwanted, running''' return input_text, output_text def lowercase__ ( self : str ): SCREAMING_SNAKE_CASE__ : List[str] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE__ : Any = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_lowercase , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowercase ) , [7, 4, 5, 10, 8, 9] ) def lowercase__ ( self : str ): pass
35
0
"""simple docstring""" import hashlib import unittest from typing import Dict import numpy as np from transformers import ( MODEL_FOR_MASK_GENERATION_MAPPING, TF_MODEL_FOR_MASK_GENERATION_MAPPING, is_vision_available, pipeline, ) from transformers.pipelines import MaskGenerationPipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) if is_vision_available(): from PIL import Image else: class _lowerCAmelCase : """simple docstring""" @staticmethod def UpperCAmelCase__ ( *_lowercase , **_lowercase ) -> Union[str, Any]: '''simple docstring''' pass def __lowerCAmelCase ( __UpperCamelCase : List[Any] ): '''simple docstring''' snake_case_ : Tuple = hashlib.mda(image.tobytes() ) return m.hexdigest()[:1_0] def __lowerCAmelCase ( __UpperCamelCase : Optional[int] ): '''simple docstring''' snake_case_ : Optional[Any] = np.array(A__ ) snake_case_ : Dict = npimg.shape return {"hash": hashimage(A__ ), "shape": shape} @is_pipeline_test @require_vision @require_torch class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" _lowerCamelCase = dict( (list(MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if MODEL_FOR_MASK_GENERATION_MAPPING else []) ) _lowerCamelCase = dict( (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items() ) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else []) ) def UpperCAmelCase__ ( self , _lowercase , _lowercase , _lowercase ) -> int: '''simple docstring''' snake_case_ : Dict = MaskGenerationPipeline(model=_lowercase , image_processor=_lowercase ) return image_segmenter, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def UpperCAmelCase__ ( self , _lowercase , _lowercase ) -> Optional[int]: '''simple docstring''' pass @require_tf @unittest.skip("""Image segmentation not implemented in TF""" ) def UpperCAmelCase__ ( self ) -> str: '''simple docstring''' pass @slow @require_torch def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' snake_case_ : str = pipeline("""mask-generation""" , model="""facebook/sam-vit-huge""" ) snake_case_ : List[Any] = image_segmenter("""http://images.cocodataset.org/val2017/000000039769.jpg""" , points_per_batch=2_5_6 ) # Shortening by hashing snake_case_ : Union[str, Any] = [] for i, o in enumerate(outputs["""masks"""] ): new_outupt += [{"mask": mask_to_test_readable(_lowercase ), "scores": outputs["scores"][i]}] # fmt: off self.assertEqual( nested_simplify(_lowercase , decimals=4 ) , [ {"""mask""": {"""hash""": """115ad19f5f""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0444}, {"""mask""": {"""hash""": """6affa964c6""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.021}, {"""mask""": {"""hash""": """dfe28a0388""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0167}, {"""mask""": {"""hash""": """c0a5f4a318""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0132}, {"""mask""": {"""hash""": """fe8065c197""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0053}, {"""mask""": {"""hash""": """e2d0b7a0b7""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9967}, {"""mask""": {"""hash""": """453c7844bd""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.993}, {"""mask""": {"""hash""": """3d44f2926d""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9909}, {"""mask""": {"""hash""": """64033ddc3f""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9879}, {"""mask""": {"""hash""": """801064ff79""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9834}, {"""mask""": {"""hash""": """6172f276ef""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9716}, {"""mask""": {"""hash""": """b49e60e084""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9612}, {"""mask""": {"""hash""": """a811e775fd""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9599}, {"""mask""": {"""hash""": """a6a8ebcf4b""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9552}, {"""mask""": {"""hash""": """9d8257e080""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9532}, {"""mask""": {"""hash""": """32de6454a8""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9516}, {"""mask""": {"""hash""": """af3d4af2c8""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9499}, {"""mask""": {"""hash""": """3c6db475fb""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9483}, {"""mask""": {"""hash""": """c290813fb9""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9464}, {"""mask""": {"""hash""": """b6f0b8f606""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.943}, {"""mask""": {"""hash""": """92ce16bfdf""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.943}, {"""mask""": {"""hash""": """c749b25868""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9408}, {"""mask""": {"""hash""": """efb6cab859""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9335}, {"""mask""": {"""hash""": """1ff2eafb30""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9326}, {"""mask""": {"""hash""": """788b798e24""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.9262}, {"""mask""": {"""hash""": """abea804f0e""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.8999}, {"""mask""": {"""hash""": """7b9e8ddb73""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.8986}, {"""mask""": {"""hash""": """cd24047c8a""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.8984}, {"""mask""": {"""hash""": """6943e6bcbd""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.8873}, {"""mask""": {"""hash""": """b5f47c9191""", """shape""": (4_8_0, 6_4_0)}, """scores""": 0.8871} ] , ) # fmt: on @require_torch @slow def UpperCAmelCase__ ( self ) -> Tuple: '''simple docstring''' snake_case_ : Tuple = '''facebook/sam-vit-huge''' snake_case_ : Union[str, Any] = pipeline("""mask-generation""" , model=_lowercase ) snake_case_ : Dict = image_segmenter( """http://images.cocodataset.org/val2017/000000039769.jpg""" , pred_iou_thresh=1 , points_per_batch=2_5_6 ) # Shortening by hashing snake_case_ : Optional[Any] = [] for i, o in enumerate(outputs["""masks"""] ): new_outupt += [{"mask": mask_to_test_readable(_lowercase ), "scores": outputs["scores"][i]}] self.assertEqual( nested_simplify(_lowercase , decimals=4 ) , [ {"""mask""": {"""hash""": """115ad19f5f""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0444}, {"""mask""": {"""hash""": """6affa964c6""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0210}, {"""mask""": {"""hash""": """dfe28a0388""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0167}, {"""mask""": {"""hash""": """c0a5f4a318""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0132}, {"""mask""": {"""hash""": """fe8065c197""", """shape""": (4_8_0, 6_4_0)}, """scores""": 1.0053}, ] , )
58
from __future__ import annotations def a ( A__ , A__ , A__ ) -> dict[str, float]: '''simple docstring''' if (voltage, current, resistance).count(0 ) != 1: raise ValueError('''One and only one argument must be 0''' ) if resistance < 0: raise ValueError('''Resistance cannot be negative''' ) if voltage == 0: return {"voltage": float(current * resistance )} elif current == 0: return {"current": voltage / resistance} elif resistance == 0: return {"resistance": voltage / current} else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
35
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import re from ..models.auto import AutoProcessor from ..models.vision_encoder_decoder import VisionEncoderDecoderModel from ..utils import is_vision_available from .base import PipelineTool if is_vision_available(): from PIL import Image class __SCREAMING_SNAKE_CASE ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE__ ='''naver-clova-ix/donut-base-finetuned-docvqa''' SCREAMING_SNAKE_CASE__ =( '''This is a tool that answers a question about an document (pdf). It takes an input named `document` which ''' '''should be the document containing the information, as well as a `question` that is the question about the ''' '''document. It returns a text that contains the answer to the question.''' ) SCREAMING_SNAKE_CASE__ ='''document_qa''' SCREAMING_SNAKE_CASE__ =AutoProcessor SCREAMING_SNAKE_CASE__ =VisionEncoderDecoderModel SCREAMING_SNAKE_CASE__ =['''image''', '''text'''] SCREAMING_SNAKE_CASE__ =['''text'''] def __init__( self, *_a, **_a ) -> Dict: if not is_vision_available(): raise ValueError("Pillow must be installed to use the DocumentQuestionAnsweringTool." ) super().__init__(*_lowercase, **_lowercase ) def __lowerCAmelCase ( self, _a, _a ) -> Any: __SCREAMING_SNAKE_CASE = '''<s_docvqa><s_question>{user_input}</s_question><s_answer>''' __SCREAMING_SNAKE_CASE = task_prompt.replace("{user_input}", _lowercase ) __SCREAMING_SNAKE_CASE = self.pre_processor.tokenizer( _lowercase, add_special_tokens=_lowercase, return_tensors="pt" ).input_ids __SCREAMING_SNAKE_CASE = self.pre_processor(_lowercase, return_tensors="pt" ).pixel_values return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values} def __lowerCAmelCase ( self, _a ) -> List[Any]: return self.model.generate( inputs["pixel_values"].to(self.device ), decoder_input_ids=inputs["decoder_input_ids"].to(self.device ), max_length=self.model.decoder.config.max_position_embeddings, early_stopping=_lowercase, pad_token_id=self.pre_processor.tokenizer.pad_token_id, eos_token_id=self.pre_processor.tokenizer.eos_token_id, use_cache=_lowercase, num_beams=1, bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]], return_dict_in_generate=_lowercase, ).sequences def __lowerCAmelCase ( self, _a ) -> Dict: __SCREAMING_SNAKE_CASE = self.pre_processor.batch_decode(_lowercase )[0] __SCREAMING_SNAKE_CASE = sequence.replace(self.pre_processor.tokenizer.eos_token, "" ) __SCREAMING_SNAKE_CASE = sequence.replace(self.pre_processor.tokenizer.pad_token, "" ) __SCREAMING_SNAKE_CASE = re.sub(r"<.*?>", "", _lowercase, count=1 ).strip() # remove first task start token __SCREAMING_SNAKE_CASE = self.pre_processor.tokenajson(_lowercase ) return sequence["answer"]
693
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer a_ :Tuple = logging.get_logger(__name__) a_ :Optional[Any] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} a_ :Optional[int] = { 'vocab_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } a_ :Dict = { 'vocab_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } a_ :Any = { 'vocab_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json' ), }, } a_ :Optional[int] = { 'facebook/dpr-ctx_encoder-single-nq-base': 5_12, 'facebook/dpr-ctx_encoder-multiset-base': 5_12, } a_ :List[Any] = { 'facebook/dpr-question_encoder-single-nq-base': 5_12, 'facebook/dpr-question_encoder-multiset-base': 5_12, } a_ :Tuple = { 'facebook/dpr-reader-single-nq-base': 5_12, 'facebook/dpr-reader-multiset-base': 5_12, } a_ :str = { 'facebook/dpr-ctx_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-ctx_encoder-multiset-base': {'do_lower_case': True}, } a_ :Optional[int] = { 'facebook/dpr-question_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-question_encoder-multiset-base': {'do_lower_case': True}, } a_ :Any = { 'facebook/dpr-reader-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-reader-multiset-base': {'do_lower_case': True}, } class lowercase ( _UpperCAmelCase ): lowerCamelCase : Optional[Any] = VOCAB_FILES_NAMES lowerCamelCase : int = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : List[str] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : int = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class lowercase ( _UpperCAmelCase ): lowerCamelCase : Optional[int] = VOCAB_FILES_NAMES lowerCamelCase : Union[str, Any] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Optional[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[int] = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION a_ :List[str] = collections.namedtuple( 'DPRSpanPrediction', ['span_score', 'relevance_score', 'doc_id', 'start_index', 'end_index', 'text'] ) a_ :Optional[int] = collections.namedtuple('DPRReaderOutput', ['start_logits', 'end_logits', 'relevance_logits']) a_ :Tuple = r'\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `\'tf\'`: Return TensorFlow `tf.constant` objects.\n - `\'pt\'`: Return PyTorch `torch.Tensor` objects.\n - `\'np\'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer\'s default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n ' @add_start_docstrings(_UpperCAmelCase ) class lowercase : def __call__( self : List[Any] , _lowercase : Any , _lowercase : Optional[str] = None , _lowercase : Optional[str] = None , _lowercase : Union[bool, str] = False , _lowercase : Union[bool, str] = False , _lowercase : Optional[int] = None , _lowercase : Optional[Union[str, TensorType]] = None , _lowercase : Optional[bool] = None , **_lowercase : str , ): if titles is None and texts is None: return super().__call__( _lowercase , padding=_lowercase , truncation=_lowercase , max_length=_lowercase , return_tensors=_lowercase , return_attention_mask=_lowercase , **_lowercase , ) elif titles is None or texts is None: SCREAMING_SNAKE_CASE__ : List[str] = titles if texts is None else texts return super().__call__( _lowercase , _lowercase , padding=_lowercase , truncation=_lowercase , max_length=_lowercase , return_tensors=_lowercase , return_attention_mask=_lowercase , **_lowercase , ) SCREAMING_SNAKE_CASE__ : Optional[Any] = titles if not isinstance(_lowercase , _lowercase ) else [titles] SCREAMING_SNAKE_CASE__ : Optional[int] = texts if not isinstance(_lowercase , _lowercase ) else [texts] SCREAMING_SNAKE_CASE__ : List[Any] = len(_lowercase ) SCREAMING_SNAKE_CASE__ : str = questions if not isinstance(_lowercase , _lowercase ) else [questions] * n_passages if len(_lowercase ) != len(_lowercase ): raise ValueError( f"""There should be as many titles than texts but got {len(_lowercase )} titles and {len(_lowercase )} texts.""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = super().__call__(_lowercase , _lowercase , padding=_lowercase , truncation=_lowercase )['''input_ids'''] SCREAMING_SNAKE_CASE__ : Tuple = super().__call__(_lowercase , add_special_tokens=_lowercase , padding=_lowercase , truncation=_lowercase )['''input_ids'''] SCREAMING_SNAKE_CASE__ : Optional[Any] = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_lowercase , _lowercase ) ] } if return_attention_mask is not False: SCREAMING_SNAKE_CASE__ : Optional[int] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) SCREAMING_SNAKE_CASE__ : Dict = attention_mask return self.pad(_lowercase , padding=_lowercase , max_length=_lowercase , return_tensors=_lowercase ) def lowercase__ ( self : List[Any] , _lowercase : BatchEncoding , _lowercase : DPRReaderOutput , _lowercase : int = 16 , _lowercase : int = 64 , _lowercase : int = 4 , ): SCREAMING_SNAKE_CASE__ : Optional[int] = reader_input['''input_ids'''] SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple = reader_output[:3] SCREAMING_SNAKE_CASE__ : Any = len(_lowercase ) SCREAMING_SNAKE_CASE__ : int = sorted(range(_lowercase ) , reverse=_lowercase , key=relevance_logits.__getitem__ ) SCREAMING_SNAKE_CASE__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: SCREAMING_SNAKE_CASE__ : Optional[int] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence SCREAMING_SNAKE_CASE__ : Any = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: SCREAMING_SNAKE_CASE__ : Dict = sequence_ids.index(self.pad_token_id ) else: SCREAMING_SNAKE_CASE__ : List[str] = len(_lowercase ) SCREAMING_SNAKE_CASE__ : Any = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=_lowercase , top_spans=_lowercase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=_lowercase , start_index=_lowercase , end_index=_lowercase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(_lowercase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def lowercase__ ( self : Dict , _lowercase : List[int] , _lowercase : List[int] , _lowercase : int , _lowercase : int , ): SCREAMING_SNAKE_CASE__ : Optional[int] = [] for start_index, start_score in enumerate(_lowercase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) SCREAMING_SNAKE_CASE__ : Optional[int] = sorted(_lowercase , key=lambda _lowercase : x[1] , reverse=_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f"""Wrong span indices: [{start_index}:{end_index}]""" ) SCREAMING_SNAKE_CASE__ : Tuple = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f"""Span is too long: {length} > {max_answer_length}""" ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_lowercase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_UpperCAmelCase ) class lowercase ( _UpperCAmelCase , _UpperCAmelCase ): lowerCamelCase : Dict = VOCAB_FILES_NAMES lowerCamelCase : Union[str, Any] = READER_PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : List[str] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : str = READER_PRETRAINED_INIT_CONFIGURATION lowerCamelCase : List[Any] = ['''input_ids''', '''attention_mask''']
35
0
'''simple docstring''' import argparse import json import torch from diffusers import DDPMScheduler, LDMPipeline, UNetaDModel, VQModel def __lowerCamelCase ( UpperCAmelCase_ , UpperCAmelCase_=1 ) ->int: if n_shave_prefix_segments >= 0: return ".".join(path.split('.' )[n_shave_prefix_segments:] ) else: return ".".join(path.split('.' )[:n_shave_prefix_segments] ) def __lowerCamelCase ( UpperCAmelCase_ , UpperCAmelCase_=0 ) ->List[str]: snake_case__ = [] for old_item in old_list: snake_case__ = old_item.replace('in_layers.0' , 'norm1' ) snake_case__ = new_item.replace('in_layers.2' , 'conv1' ) snake_case__ = new_item.replace('out_layers.0' , 'norm2' ) snake_case__ = new_item.replace('out_layers.3' , 'conv2' ) snake_case__ = new_item.replace('emb_layers.1' , 'time_emb_proj' ) snake_case__ = new_item.replace('skip_connection' , 'conv_shortcut' ) snake_case__ = shave_segments(A__ , n_shave_prefix_segments=A__ ) mapping.append({'old': old_item, 'new': new_item} ) return mapping def __lowerCamelCase ( UpperCAmelCase_ , UpperCAmelCase_=0 ) ->Optional[int]: snake_case__ = [] for old_item in old_list: snake_case__ = old_item snake_case__ = new_item.replace('norm.weight' , 'group_norm.weight' ) snake_case__ = new_item.replace('norm.bias' , 'group_norm.bias' ) snake_case__ = new_item.replace('proj_out.weight' , 'proj_attn.weight' ) snake_case__ = new_item.replace('proj_out.bias' , 'proj_attn.bias' ) snake_case__ = shave_segments(A__ , n_shave_prefix_segments=A__ ) mapping.append({'old': old_item, 'new': new_item} ) return mapping def __lowerCamelCase ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_=None , UpperCAmelCase_=None , UpperCAmelCase_=None ) ->Optional[int]: assert isinstance(A__ , A__ ), "Paths should be a list of dicts containing 'old' and 'new' keys." # Splits the attention layers into three variables. if attention_paths_to_split is not None: for path, path_map in attention_paths_to_split.items(): snake_case__ = old_checkpoint[path] snake_case__ = old_tensor.shape[0] // 3 snake_case__ = (-1, channels) if len(old_tensor.shape ) == 3 else (-1) snake_case__ = old_tensor.shape[0] // config['''num_head_channels'''] // 3 snake_case__ = old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:] ) snake_case__ = old_tensor.split(channels // num_heads , dim=1 ) snake_case__ = query.reshape(A__ ) snake_case__ = key.reshape(A__ ) snake_case__ = value.reshape(A__ ) for path in paths: snake_case__ = path['''new'''] # These have already been assigned if attention_paths_to_split is not None and new_path in attention_paths_to_split: continue # Global renaming happens here snake_case__ = new_path.replace('middle_block.0' , 'mid_block.resnets.0' ) snake_case__ = new_path.replace('middle_block.1' , 'mid_block.attentions.0' ) snake_case__ = new_path.replace('middle_block.2' , 'mid_block.resnets.1' ) if additional_replacements is not None: for replacement in additional_replacements: snake_case__ = new_path.replace(replacement['old'] , replacement['new'] ) # proj_attn.weight has to be converted from conv 1D to linear if "proj_attn.weight" in new_path: snake_case__ = old_checkpoint[path['''old''']][:, :, 0] else: snake_case__ = old_checkpoint[path['''old''']] def __lowerCamelCase ( UpperCAmelCase_ , UpperCAmelCase_ ) ->Any: snake_case__ = {} snake_case__ = checkpoint['''time_embed.0.weight'''] snake_case__ = checkpoint['''time_embed.0.bias'''] snake_case__ = checkpoint['''time_embed.2.weight'''] snake_case__ = checkpoint['''time_embed.2.bias'''] snake_case__ = checkpoint['''input_blocks.0.0.weight'''] snake_case__ = checkpoint['''input_blocks.0.0.bias'''] snake_case__ = checkpoint['''out.0.weight'''] snake_case__ = checkpoint['''out.0.bias'''] snake_case__ = checkpoint['''out.2.weight'''] snake_case__ = checkpoint['''out.2.bias'''] # Retrieves the keys for the input blocks only snake_case__ = len({'.'.join(layer.split('.' )[:2] ) for layer in checkpoint if 'input_blocks' in layer} ) snake_case__ = { layer_id: [key for key in checkpoint if f'''input_blocks.{layer_id}''' in key] for layer_id in range(A__ ) } # Retrieves the keys for the middle blocks only snake_case__ = len({'.'.join(layer.split('.' )[:2] ) for layer in checkpoint if 'middle_block' in layer} ) snake_case__ = { layer_id: [key for key in checkpoint if f'''middle_block.{layer_id}''' in key] for layer_id in range(A__ ) } # Retrieves the keys for the output blocks only snake_case__ = len({'.'.join(layer.split('.' )[:2] ) for layer in checkpoint if 'output_blocks' in layer} ) snake_case__ = { layer_id: [key for key in checkpoint if f'''output_blocks.{layer_id}''' in key] for layer_id in range(A__ ) } for i in range(1 , A__ ): snake_case__ = (i - 1) // (config['''num_res_blocks'''] + 1) snake_case__ = (i - 1) % (config['''num_res_blocks'''] + 1) snake_case__ = [key for key in input_blocks[i] if f'''input_blocks.{i}.0''' in key] snake_case__ = [key for key in input_blocks[i] if f'''input_blocks.{i}.1''' in key] if f'''input_blocks.{i}.0.op.weight''' in checkpoint: snake_case__ = checkpoint[ f'''input_blocks.{i}.0.op.weight''' ] snake_case__ = checkpoint[ f'''input_blocks.{i}.0.op.bias''' ] continue snake_case__ = renew_resnet_paths(A__ ) snake_case__ = {'''old''': f'''input_blocks.{i}.0''', '''new''': f'''down_blocks.{block_id}.resnets.{layer_in_block_id}'''} snake_case__ = {'''old''': '''resnets.2.op''', '''new''': '''downsamplers.0.op'''} assign_to_checkpoint( A__ , A__ , A__ , additional_replacements=[meta_path, resnet_op] , config=A__ ) if len(A__ ): snake_case__ = renew_attention_paths(A__ ) snake_case__ = { '''old''': f'''input_blocks.{i}.1''', '''new''': f'''down_blocks.{block_id}.attentions.{layer_in_block_id}''', } snake_case__ = { f'''input_blocks.{i}.1.qkv.bias''': { '''key''': f'''down_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias''', '''query''': f'''down_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias''', '''value''': f'''down_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias''', }, f'''input_blocks.{i}.1.qkv.weight''': { '''key''': f'''down_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight''', '''query''': f'''down_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight''', '''value''': f'''down_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight''', }, } assign_to_checkpoint( A__ , A__ , A__ , additional_replacements=[meta_path] , attention_paths_to_split=A__ , config=A__ , ) snake_case__ = middle_blocks[0] snake_case__ = middle_blocks[1] snake_case__ = middle_blocks[2] snake_case__ = renew_resnet_paths(A__ ) assign_to_checkpoint(A__ , A__ , A__ , config=A__ ) snake_case__ = renew_resnet_paths(A__ ) assign_to_checkpoint(A__ , A__ , A__ , config=A__ ) snake_case__ = renew_attention_paths(A__ ) snake_case__ = { '''middle_block.1.qkv.bias''': { '''key''': '''mid_block.attentions.0.key.bias''', '''query''': '''mid_block.attentions.0.query.bias''', '''value''': '''mid_block.attentions.0.value.bias''', }, '''middle_block.1.qkv.weight''': { '''key''': '''mid_block.attentions.0.key.weight''', '''query''': '''mid_block.attentions.0.query.weight''', '''value''': '''mid_block.attentions.0.value.weight''', }, } assign_to_checkpoint( A__ , A__ , A__ , attention_paths_to_split=A__ , config=A__ ) for i in range(A__ ): snake_case__ = i // (config['''num_res_blocks'''] + 1) snake_case__ = i % (config['''num_res_blocks'''] + 1) snake_case__ = [shave_segments(A__ , 2 ) for name in output_blocks[i]] snake_case__ = {} for layer in output_block_layers: snake_case__ = layer.split('.' )[0], shave_segments(A__ , 1 ) if layer_id in output_block_list: output_block_list[layer_id].append(A__ ) else: snake_case__ = [layer_name] if len(A__ ) > 1: snake_case__ = [key for key in output_blocks[i] if f'''output_blocks.{i}.0''' in key] snake_case__ = [key for key in output_blocks[i] if f'''output_blocks.{i}.1''' in key] snake_case__ = renew_resnet_paths(A__ ) snake_case__ = renew_resnet_paths(A__ ) snake_case__ = {'''old''': f'''output_blocks.{i}.0''', '''new''': f'''up_blocks.{block_id}.resnets.{layer_in_block_id}'''} assign_to_checkpoint(A__ , A__ , A__ , additional_replacements=[meta_path] , config=A__ ) if ["conv.weight", "conv.bias"] in output_block_list.values(): snake_case__ = list(output_block_list.values() ).index(['conv.weight', 'conv.bias'] ) snake_case__ = checkpoint[ f'''output_blocks.{i}.{index}.conv.weight''' ] snake_case__ = checkpoint[ f'''output_blocks.{i}.{index}.conv.bias''' ] # Clear attentions as they have been attributed above. if len(A__ ) == 2: snake_case__ = [] if len(A__ ): snake_case__ = renew_attention_paths(A__ ) snake_case__ = { '''old''': f'''output_blocks.{i}.1''', '''new''': f'''up_blocks.{block_id}.attentions.{layer_in_block_id}''', } snake_case__ = { f'''output_blocks.{i}.1.qkv.bias''': { '''key''': f'''up_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias''', '''query''': f'''up_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias''', '''value''': f'''up_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias''', }, f'''output_blocks.{i}.1.qkv.weight''': { '''key''': f'''up_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight''', '''query''': f'''up_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight''', '''value''': f'''up_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight''', }, } assign_to_checkpoint( A__ , A__ , A__ , additional_replacements=[meta_path] , attention_paths_to_split=to_split if any('qkv' in key for key in attentions ) else None , config=A__ , ) else: snake_case__ = renew_resnet_paths(A__ , n_shave_prefix_segments=1 ) for path in resnet_0_paths: snake_case__ = '''.'''.join(['output_blocks', str(A__ ), path['old']] ) snake_case__ = '''.'''.join(['up_blocks', str(A__ ), 'resnets', str(A__ ), path['new']] ) snake_case__ = checkpoint[old_path] return new_checkpoint if __name__ == "__main__": a__ : Dict = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the checkpoint to convert.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the architecture.''', ) parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') a__ : Optional[Any] = parser.parse_args() a__ : Any = torch.load(args.checkpoint_path) with open(args.config_file) as f: a__ : List[str] = json.loads(f.read()) a__ : Tuple = convert_ldm_checkpoint(checkpoint, config) if "ldm" in config: del config["ldm"] a__ : int = UNetaDModel(**config) model.load_state_dict(converted_checkpoint) try: a__ : Optional[int] = DDPMScheduler.from_config('''/'''.join(args.checkpoint_path.split('''/''')[:-1])) a__ : Dict = VQModel.from_pretrained('''/'''.join(args.checkpoint_path.split('''/''')[:-1])) a__ : Optional[Any] = LDMPipeline(unet=model, scheduler=scheduler, vae=vqvae) pipe.save_pretrained(args.dump_path) except: # noqa: E722 model.save_pretrained(args.dump_path)
368
import random def a ( A__ ) -> bool: '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = num - 1 SCREAMING_SNAKE_CASE__ : Optional[int] = 0 while s % 2 == 0: SCREAMING_SNAKE_CASE__ : Optional[Any] = s // 2 t += 1 for _ in range(5 ): SCREAMING_SNAKE_CASE__ : int = random.randrange(2 , num - 1 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = pow(A__ , A__ , A__ ) if v != 1: SCREAMING_SNAKE_CASE__ : List[str] = 0 while v != (num - 1): if i == t - 1: return False else: SCREAMING_SNAKE_CASE__ : Any = i + 1 SCREAMING_SNAKE_CASE__ : Union[str, Any] = (v**2) % num return True def a ( A__ ) -> bool: '''simple docstring''' if num < 2: return False SCREAMING_SNAKE_CASE__ : Optional[int] = [ 2, 3, 5, 7, 1_1, 1_3, 1_7, 1_9, 2_3, 2_9, 3_1, 3_7, 4_1, 4_3, 4_7, 5_3, 5_9, 6_1, 6_7, 7_1, 7_3, 7_9, 8_3, 8_9, 9_7, 1_0_1, 1_0_3, 1_0_7, 1_0_9, 1_1_3, 1_2_7, 1_3_1, 1_3_7, 1_3_9, 1_4_9, 1_5_1, 1_5_7, 1_6_3, 1_6_7, 1_7_3, 1_7_9, 1_8_1, 1_9_1, 1_9_3, 1_9_7, 1_9_9, 2_1_1, 2_2_3, 2_2_7, 2_2_9, 2_3_3, 2_3_9, 2_4_1, 2_5_1, 2_5_7, 2_6_3, 2_6_9, 2_7_1, 2_7_7, 2_8_1, 2_8_3, 2_9_3, 3_0_7, 3_1_1, 3_1_3, 3_1_7, 3_3_1, 3_3_7, 3_4_7, 3_4_9, 3_5_3, 3_5_9, 3_6_7, 3_7_3, 3_7_9, 3_8_3, 3_8_9, 3_9_7, 4_0_1, 4_0_9, 4_1_9, 4_2_1, 4_3_1, 4_3_3, 4_3_9, 4_4_3, 4_4_9, 4_5_7, 4_6_1, 4_6_3, 4_6_7, 4_7_9, 4_8_7, 4_9_1, 4_9_9, 5_0_3, 5_0_9, 5_2_1, 5_2_3, 5_4_1, 5_4_7, 5_5_7, 5_6_3, 5_6_9, 5_7_1, 5_7_7, 5_8_7, 5_9_3, 5_9_9, 6_0_1, 6_0_7, 6_1_3, 6_1_7, 6_1_9, 6_3_1, 6_4_1, 6_4_3, 6_4_7, 6_5_3, 6_5_9, 6_6_1, 6_7_3, 6_7_7, 6_8_3, 6_9_1, 7_0_1, 7_0_9, 7_1_9, 7_2_7, 7_3_3, 7_3_9, 7_4_3, 7_5_1, 7_5_7, 7_6_1, 7_6_9, 7_7_3, 7_8_7, 7_9_7, 8_0_9, 8_1_1, 8_2_1, 8_2_3, 8_2_7, 8_2_9, 8_3_9, 8_5_3, 8_5_7, 8_5_9, 8_6_3, 8_7_7, 8_8_1, 8_8_3, 8_8_7, 9_0_7, 9_1_1, 9_1_9, 9_2_9, 9_3_7, 9_4_1, 9_4_7, 9_5_3, 9_6_7, 9_7_1, 9_7_7, 9_8_3, 9_9_1, 9_9_7, ] if num in low_primes: return True for prime in low_primes: if (num % prime) == 0: return False return rabin_miller(A__ ) def a ( A__ = 1_0_2_4 ) -> int: '''simple docstring''' while True: SCREAMING_SNAKE_CASE__ : Any = random.randrange(2 ** (keysize - 1) , 2 ** (keysize) ) if is_prime_low_num(A__ ): return num if __name__ == "__main__": a_ :Dict = generate_large_prime() print(('Prime number:', num)) print(('is_prime_low_num:', is_prime_low_num(num)))
35
0
'''simple docstring''' import argparse import os import re import packaging.version lowercase__ : int = 'examples/' lowercase__ : Dict = { 'examples': (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), 'check_min_version("VERSION")\n'), 'init': (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), '__version__ = "VERSION"\n'), 'setup': (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), r'\1version="VERSION",'), 'doc': (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), 'release = "VERSION"\n'), } lowercase__ : Union[str, Any] = { 'init': 'src/transformers/__init__.py', 'setup': 'setup.py', } lowercase__ : List[str] = 'README.md' def __lowerCamelCase ( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any , _UpperCamelCase : str ): '''simple docstring''' with open(A__ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCAmelCase_ = f.read() UpperCAmelCase_ = REPLACE_PATTERNS[pattern] UpperCAmelCase_ = replace.replace('''VERSION''' , A__ ) UpperCAmelCase_ = re_pattern.sub(A__ , A__ ) with open(A__ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.write(A__ ) def __lowerCamelCase ( _UpperCamelCase : Tuple ): '''simple docstring''' for folder, directories, fnames in os.walk(A__ ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove('''research_projects''' ) if "legacy" in directories: directories.remove('''legacy''' ) for fname in fnames: if fname.endswith('''.py''' ): update_version_in_file(os.path.join(A__ , A__ ) , A__ , pattern='''examples''' ) def __lowerCamelCase ( _UpperCamelCase : List[str] , _UpperCamelCase : Optional[Any]=False ): '''simple docstring''' for pattern, fname in REPLACE_FILES.items(): update_version_in_file(A__ , A__ , A__ ) if not patch: update_version_in_examples(A__ ) def __lowerCamelCase ( ): '''simple docstring''' UpperCAmelCase_ = '''🤗 Transformers currently provides the following architectures''' UpperCAmelCase_ = '''1. Want to contribute a new model?''' with open(A__ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCAmelCase_ = f.readlines() # Find the start of the list. UpperCAmelCase_ = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 UpperCAmelCase_ = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('''1.''' ): UpperCAmelCase_ = lines[index].replace( '''https://huggingface.co/docs/transformers/main/model_doc''' , '''https://huggingface.co/docs/transformers/model_doc''' , ) index += 1 with open(A__ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(A__ ) def __lowerCamelCase ( ): '''simple docstring''' with open(REPLACE_FILES['''init'''] , '''r''' ) as f: UpperCAmelCase_ = f.read() UpperCAmelCase_ = REPLACE_PATTERNS['''init'''][0].search(A__ ).groups()[0] return packaging.version.parse(A__ ) def __lowerCamelCase ( _UpperCamelCase : Dict=False ): '''simple docstring''' UpperCAmelCase_ = get_version() if patch and default_version.is_devrelease: raise ValueError('''Can\'t create a patch version from the dev branch, checkout a released version!''' ) if default_version.is_devrelease: UpperCAmelCase_ = default_version.base_version elif patch: UpperCAmelCase_ = F"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}""" else: UpperCAmelCase_ = F"""{default_version.major}.{default_version.minor + 1}.0""" # Now let's ask nicely if that's the right one. UpperCAmelCase_ = input(F"""Which version are you releasing? [{default_version}]""" ) if len(A__ ) == 0: UpperCAmelCase_ = default_version print(F"""Updating version to {version}.""" ) global_version_update(A__ , patch=A__ ) if not patch: print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() def __lowerCamelCase ( ): '''simple docstring''' UpperCAmelCase_ = get_version() UpperCAmelCase_ = F"""{current_version.major}.{current_version.minor + 1}.0.dev0""" UpperCAmelCase_ = current_version.base_version # Check with the user we got that right. UpperCAmelCase_ = input(F"""Which version are we developing now? [{dev_version}]""" ) if len(A__ ) == 0: UpperCAmelCase_ = dev_version print(F"""Updating version to {version}.""" ) global_version_update(A__ ) print('''Cleaning main README, don\'t forget to run `make fix-copies`.''' ) clean_main_ref_in_model_list() if __name__ == "__main__": lowercase__ : Optional[Any] = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") lowercase__ : int = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
390
# Logistic Regression from scratch # In[62]: # In[63]: # importing all the required libraries import numpy as np from matplotlib import pyplot as plt from sklearn import datasets def a ( A__ ) -> List[Any]: '''simple docstring''' return 1 / (1 + np.exp(-z )) def a ( A__ , A__ ) -> Any: '''simple docstring''' return (-y * np.log(A__ ) - (1 - y) * np.log(1 - h )).mean() def a ( A__ , A__ , A__ ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = np.dot(A__ , A__ ) return np.sum(y * scores - np.log(1 + np.exp(A__ ) ) ) def a ( A__ , A__ , A__ , A__=7_0_0_0_0 ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = np.zeros(x.shape[1] ) for iterations in range(A__ ): SCREAMING_SNAKE_CASE__ : List[Any] = np.dot(A__ , A__ ) SCREAMING_SNAKE_CASE__ : Dict = sigmoid_function(A__ ) SCREAMING_SNAKE_CASE__ : int = np.dot(x.T , h - y ) / y.size SCREAMING_SNAKE_CASE__ : Union[str, Any] = theta - alpha * gradient # updating the weights SCREAMING_SNAKE_CASE__ : Optional[int] = np.dot(A__ , A__ ) SCREAMING_SNAKE_CASE__ : int = sigmoid_function(A__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = cost_function(A__ , A__ ) if iterations % 1_0_0 == 0: print(f"""loss: {j} \t""" ) # printing the loss after every 100 iterations return theta # In[68]: if __name__ == "__main__": a_ :str = datasets.load_iris() a_ :Dict = iris.data[:, :2] a_ :int = (iris.target != 0) * 1 a_ :Dict = 0.1 a_ :str = logistic_reg(alpha, x, y, max_iterations=7_00_00) print('theta: ', theta) # printing the theta i.e our weights vector def a ( A__ ) -> int: '''simple docstring''' return sigmoid_function( np.dot(A__ , A__ ) ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) plt.scatter(x[y == 0][:, 0], x[y == 0][:, 1], color='b', label='0') plt.scatter(x[y == 1][:, 0], x[y == 1][:, 1], color='r', label='1') ((a_) , (a_)) :str = (x[:, 0].min(), x[:, 0].max()) ((a_) , (a_)) :Tuple = (x[:, 1].min(), x[:, 1].max()) ((a_) , (a_)) :Dict = np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max)) a_ :Optional[int] = np.c_[xxa.ravel(), xxa.ravel()] a_ :Optional[int] = predict_prob(grid).reshape(xxa.shape) plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='black') plt.legend() plt.show()
35
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowercase : Tuple = logging.get_logger(__name__) __lowercase : int = { 'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json', # See all YOLOS models at https://huggingface.co/models?filter=yolos } class lowerCAmelCase ( _UpperCAmelCase ): """simple docstring""" __lowercase :List[Any] = '''yolos''' def __init__( self , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3_072 , UpperCamelCase__="gelu" , UpperCamelCase__=0.0 , UpperCamelCase__=0.0 , UpperCamelCase__=0.02 , UpperCamelCase__=1e-12 , UpperCamelCase__=[512, 864] , UpperCamelCase__=16 , UpperCamelCase__=3 , UpperCamelCase__=True , UpperCamelCase__=100 , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__=1 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=5 , UpperCamelCase__=2 , UpperCamelCase__=0.1 , **UpperCamelCase__ , ) -> List[str]: '''simple docstring''' super().__init__(**_lowercase ) lowerCamelCase_ = hidden_size lowerCamelCase_ = num_hidden_layers lowerCamelCase_ = num_attention_heads lowerCamelCase_ = intermediate_size lowerCamelCase_ = hidden_act lowerCamelCase_ = hidden_dropout_prob lowerCamelCase_ = attention_probs_dropout_prob lowerCamelCase_ = initializer_range lowerCamelCase_ = layer_norm_eps lowerCamelCase_ = image_size lowerCamelCase_ = patch_size lowerCamelCase_ = num_channels lowerCamelCase_ = qkv_bias lowerCamelCase_ = num_detection_tokens lowerCamelCase_ = use_mid_position_embeddings lowerCamelCase_ = auxiliary_loss # Hungarian matcher lowerCamelCase_ = class_cost lowerCamelCase_ = bbox_cost lowerCamelCase_ = giou_cost # Loss coefficients lowerCamelCase_ = bbox_loss_coefficient lowerCamelCase_ = giou_loss_coefficient lowerCamelCase_ = eos_coefficient class lowerCAmelCase ( _UpperCAmelCase ): """simple docstring""" __lowercase :List[Any] = version.parse("1.11" ) @property def _lowerCAmelCase ( self ) -> str: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowerCAmelCase ( self ) -> List[str]: '''simple docstring''' return 1e-4 @property def _lowerCAmelCase ( self ) -> int: '''simple docstring''' return 12
142
import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available from . import BaseDiffusersCLICommand def a ( A__ ) -> Tuple: '''simple docstring''' return EnvironmentCommand() class lowercase ( _UpperCAmelCase ): @staticmethod def lowercase__ ( _lowercase : ArgumentParser ): SCREAMING_SNAKE_CASE__ : Optional[int] = parser.add_parser('''env''' ) download_parser.set_defaults(func=_lowercase ) def lowercase__ ( self : List[Any] ): SCREAMING_SNAKE_CASE__ : Tuple = huggingface_hub.__version__ SCREAMING_SNAKE_CASE__ : List[Any] = '''not installed''' SCREAMING_SNAKE_CASE__ : List[Any] = '''NA''' if is_torch_available(): import torch SCREAMING_SNAKE_CASE__ : int = torch.__version__ SCREAMING_SNAKE_CASE__ : List[Any] = torch.cuda.is_available() SCREAMING_SNAKE_CASE__ : str = '''not installed''' if is_transformers_available(): import transformers SCREAMING_SNAKE_CASE__ : Optional[Any] = transformers.__version__ SCREAMING_SNAKE_CASE__ : Any = '''not installed''' if is_accelerate_available(): import accelerate SCREAMING_SNAKE_CASE__ : Union[str, Any] = accelerate.__version__ SCREAMING_SNAKE_CASE__ : Tuple = '''not installed''' if is_xformers_available(): import xformers SCREAMING_SNAKE_CASE__ : Tuple = xformers.__version__ SCREAMING_SNAKE_CASE__ : Optional[Any] = { '''`diffusers` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''PyTorch version (GPU?)''': f"""{pt_version} ({pt_cuda_available})""", '''Huggingface_hub version''': hub_version, '''Transformers version''': transformers_version, '''Accelerate version''': accelerate_version, '''xFormers version''': xformers_version, '''Using GPU in script?''': '''<fill in>''', '''Using distributed or parallel set-up in script?''': '''<fill in>''', } print('''\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n''' ) print(self.format_dict(_lowercase ) ) return info @staticmethod def lowercase__ ( _lowercase : Dict ): return "\n".join([f"""- {prop}: {val}""" for prop, val in d.items()] ) + "\n"
35
0
"""simple docstring""" class _UpperCAmelCase : def __init__( self ) -> List[str]: '''simple docstring''' _UpperCAmelCase : Optional[int] = '''''' _UpperCAmelCase : Optional[int] = '''''' _UpperCAmelCase : Union[str, Any] = [] def __snake_case ( self , _A , _A ) -> Union[str, Any]: '''simple docstring''' if m == -1: return n + 1 elif n == -1: return m + 1 elif self.dp[m][n] > -1: return self.dp[m][n] else: if self.worda[m] == self.worda[n]: _UpperCAmelCase : Optional[int] = self.__min_dist_top_down_dp(m - 1 , n - 1 ) else: _UpperCAmelCase : Union[str, Any] = self.__min_dist_top_down_dp(_lowercase , n - 1 ) _UpperCAmelCase : int = self.__min_dist_top_down_dp(m - 1 , _lowercase ) _UpperCAmelCase : Any = self.__min_dist_top_down_dp(m - 1 , n - 1 ) _UpperCAmelCase : str = 1 + min(_lowercase , _lowercase , _lowercase ) return self.dp[m][n] def __snake_case ( self , _A , _A ) -> List[Any]: '''simple docstring''' _UpperCAmelCase : Any = worda _UpperCAmelCase : str = worda _UpperCAmelCase : List[Any] = [[-1 for _ in range(len(_lowercase ) )] for _ in range(len(_lowercase ) )] return self.__min_dist_top_down_dp(len(_lowercase ) - 1 , len(_lowercase ) - 1 ) def __snake_case ( self , _A , _A ) -> List[str]: '''simple docstring''' _UpperCAmelCase : List[Any] = worda _UpperCAmelCase : Tuple = worda _UpperCAmelCase : List[str] = len(_lowercase ) _UpperCAmelCase : List[Any] = len(_lowercase ) _UpperCAmelCase : Tuple = [[0 for _ in range(n + 1 )] for _ in range(m + 1 )] for i in range(m + 1 ): for j in range(n + 1 ): if i == 0: # first string is empty _UpperCAmelCase : Union[str, Any] = j elif j == 0: # second string is empty _UpperCAmelCase : Optional[int] = i elif worda[i - 1] == worda[j - 1]: # last characters are equal _UpperCAmelCase : Optional[int] = self.dp[i - 1][j - 1] else: _UpperCAmelCase : Optional[int] = self.dp[i][j - 1] _UpperCAmelCase : Tuple = self.dp[i - 1][j] _UpperCAmelCase : List[Any] = self.dp[i - 1][j - 1] _UpperCAmelCase : Optional[Any] = 1 + min(_lowercase , _lowercase , _lowercase ) return self.dp[m][n] if __name__ == "__main__": lowerCamelCase__ : Optional[int] = EditDistance() print('''****************** Testing Edit Distance DP Algorithm ******************''') print() lowerCamelCase__ : List[Any] = input('''Enter the first string: ''').strip() lowerCamelCase__ : Dict = input('''Enter the second string: ''').strip() print() print(F'''The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}''') print(F'''The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}''') print() print('''*************** End of Testing Edit Distance DP Algorithm ***************''')
238
import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def a ( A__ , A__ , A__ ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple = RemBertConfig.from_json_file(A__ ) print('''Building PyTorch model from configuration: {}'''.format(str(A__ ) ) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = RemBertModel(A__ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(A__ , A__ , A__ ) # Save pytorch-model print('''Save PyTorch model to {}'''.format(A__ ) ) torch.save(model.state_dict() , A__ ) if __name__ == "__main__": a_ :Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--rembert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained RemBERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) a_ :Optional[Any] = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
35
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _lowerCamelCase = logging.get_logger(__name__) _lowerCamelCase = { 'andreasmadsen/efficient_mlm_m0.40': ( 'https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json' ), } class a ( _UpperCAmelCase ): '''simple docstring''' lowerCAmelCase : int = '''roberta-prelayernorm''' def __init__( self : int , __snake_case : Any=5_02_65 , __snake_case : Union[str, Any]=7_68 , __snake_case : Any=12 , __snake_case : List[Any]=12 , __snake_case : List[str]=30_72 , __snake_case : Union[str, Any]="gelu" , __snake_case : str=0.1 , __snake_case : Dict=0.1 , __snake_case : Optional[int]=5_12 , __snake_case : Any=2 , __snake_case : str=0.02 , __snake_case : Optional[int]=1E-12 , __snake_case : Tuple=1 , __snake_case : Optional[int]=0 , __snake_case : int=2 , __snake_case : Optional[Any]="absolute" , __snake_case : List[str]=True , __snake_case : int=None , **__snake_case : List[Any] , ): super().__init__(pad_token_id=_lowercase , bos_token_id=_lowercase , eos_token_id=_lowercase , **_lowercase ) UpperCAmelCase_ = vocab_size UpperCAmelCase_ = hidden_size UpperCAmelCase_ = num_hidden_layers UpperCAmelCase_ = num_attention_heads UpperCAmelCase_ = hidden_act UpperCAmelCase_ = intermediate_size UpperCAmelCase_ = hidden_dropout_prob UpperCAmelCase_ = attention_probs_dropout_prob UpperCAmelCase_ = max_position_embeddings UpperCAmelCase_ = type_vocab_size UpperCAmelCase_ = initializer_range UpperCAmelCase_ = layer_norm_eps UpperCAmelCase_ = position_embedding_type UpperCAmelCase_ = use_cache UpperCAmelCase_ = classifier_dropout class a ( _UpperCAmelCase ): '''simple docstring''' @property def lowerCamelCase_ ( self : Optional[Any] ): if self.task == "multiple-choice": UpperCAmelCase_ = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCAmelCase_ = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
144
from sklearn.metrics import recall_score import datasets a_ :int = '\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n' a_ :Union[str, Any] = '\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the \'positive class\' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.\n - `\'binary\'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n - `\'micro\'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n - `\'macro\'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - `\'weighted\'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n - `\'samples\'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n - `\'warn\'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n - `0`: If there is a zero division, the return value is `0`.\n - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n Example 1-A simple example with some errors\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n >>> print(results)\n {\'recall\': 0.6666666666666666}\n\n Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n >>> print(results)\n {\'recall\': 0.5}\n\n Example 3-The same example as Example 1, but with `sample_weight` included.\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n >>> print(results)\n {\'recall\': 0.55}\n\n Example 4-A multiclass example, using different averages.\n >>> recall_metric = datasets.load_metric(\'recall\')\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'macro\')\n >>> print(results)\n {\'recall\': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'micro\')\n >>> print(results)\n {\'recall\': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'weighted\')\n >>> print(results)\n {\'recall\': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {\'recall\': array([1., 0., 0.])}\n' a_ :Optional[Any] = '\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def lowercase__ ( self : int ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''' ) ), '''references''': datasets.Sequence(datasets.Value('''int32''' ) ), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html'''] , ) def lowercase__ ( self : Tuple , _lowercase : Optional[Any] , _lowercase : Optional[Any] , _lowercase : Optional[int]=None , _lowercase : Tuple=1 , _lowercase : List[Any]="binary" , _lowercase : Any=None , _lowercase : Optional[int]="warn" , ): SCREAMING_SNAKE_CASE__ : Optional[Any] = recall_score( _lowercase , _lowercase , labels=_lowercase , pos_label=_lowercase , average=_lowercase , sample_weight=_lowercase , zero_division=_lowercase , ) return {"recall": float(_lowercase ) if score.size == 1 else score}
35
0
import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class lowerCamelCase: '''simple docstring''' def __init__( self , snake_case_ , snake_case_=2 , snake_case_=32 , snake_case_=16 , snake_case_=3 , snake_case_=True , snake_case_=True , snake_case_=32 , snake_case_=4 , snake_case_=[0, 1, 2, 3] , snake_case_=4 , snake_case_=37 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=0.02 , snake_case_=3 , snake_case_=[1, 384, 24, 24] , snake_case_=True , snake_case_=None , ): _A = parent _A = batch_size _A = image_size _A = patch_size _A = num_channels _A = is_training _A = use_labels _A = hidden_size _A = num_hidden_layers _A = backbone_out_indices _A = num_attention_heads _A = intermediate_size _A = hidden_act _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = initializer_range _A = num_labels _A = backbone_featmap_shape _A = scope _A = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) _A = (image_size // patch_size) ** 2 _A = num_patches + 1 def lowerCAmelCase__ ( self ): _A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _A = None if self.use_labels: _A = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _A = self.get_config() return config, pixel_values, labels def lowerCAmelCase__ ( self ): _A = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, '''hidden_sizes''': [96, 192, 384, 768], '''num_groups''': 2, } return DPTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , backbone_out_indices=self.backbone_out_indices , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_lowercase , initializer_range=self.initializer_range , is_hybrid=self.is_hybrid , backbone_config=_lowercase , backbone_featmap_shape=self.backbone_featmap_shape , ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ ): _A = DPTModel(config=_lowercase ) model.to(_lowercase ) model.eval() _A = model(_lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ ): _A = self.num_labels _A = DPTForDepthEstimation(_lowercase ) model.to(_lowercase ) model.eval() _A = model(_lowercase ) self.parent.assertEqual(result.predicted_depth.shape , (self.batch_size, self.image_size, self.image_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ ): _A = self.num_labels _A = DPTForSemanticSegmentation(_lowercase ) model.to(_lowercase ) model.eval() _A = model(_lowercase , labels=_lowercase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def lowerCAmelCase__ ( self ): _A = self.prepare_config_and_inputs() _A = config_and_inputs _A = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowerCamelCase( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' __magic_name__ = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () __magic_name__ = ( { '''depth-estimation''': DPTForDepthEstimation, '''feature-extraction''': DPTModel, '''image-segmentation''': DPTForSemanticSegmentation, } if is_torch_available() else {} ) __magic_name__ = False __magic_name__ = False __magic_name__ = False def lowerCAmelCase__ ( self ): _A = DPTModelTester(self ) _A = ConfigTester(self , config_class=_lowercase , has_text_modality=_lowercase , hidden_size=37 ) def lowerCAmelCase__ ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='DPT does not use inputs_embeds' ) def lowerCAmelCase__ ( self ): pass def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(_lowercase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _A = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_lowercase , nn.Linear ) ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(_lowercase ) _A = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _A = [*signature.parameters.keys()] _A = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _lowercase ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowercase ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*_lowercase ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_lowercase ) def lowerCAmelCase__ ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue _A = self.model_tester.prepare_config_and_inputs_for_common() _A = True if model_class in get_values(_lowercase ): continue _A = model_class(_lowercase ) model.to(_lowercase ) model.train() _A = self._prepare_for_class(_lowercase , _lowercase , return_labels=_lowercase ) _A = model(**_lowercase ).loss loss.backward() def lowerCAmelCase__ ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue _A = self.model_tester.prepare_config_and_inputs_for_common() _A = False _A = True if model_class in get_values(_lowercase ) or not model_class.supports_gradient_checkpointing: continue _A = model_class(_lowercase ) model.to(_lowercase ) model.gradient_checkpointing_enable() model.train() _A = self._prepare_for_class(_lowercase , _lowercase , return_labels=_lowercase ) _A = model(**_lowercase ).loss loss.backward() def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs_for_common() _A = _config_zero_init(_lowercase ) for model_class in self.all_model_classes: _A = model_class(config=_lowercase ) # Skip the check for the backbone _A = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": _A = [F"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F"Parameter {name} of model {model_class} seems not properly initialized" , ) @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def lowerCAmelCase__ ( self ): pass @slow def lowerCAmelCase__ ( self ): for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: _A = DPTModel.from_pretrained(_lowercase ) self.assertIsNotNone(_lowercase ) def lowerCAmelCase__ ( self ): # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type _A = self.model_tester.prepare_config_and_inputs_for_common() _A = '''add''' with self.assertRaises(_lowercase ): _A = DPTForDepthEstimation(_lowercase ) def __lowerCAmelCase( ) -> List[str]: """simple docstring""" _A = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision @slow class lowerCamelCase( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase__ ( self ): _A = DPTImageProcessor.from_pretrained('Intel/dpt-hybrid-midas' ) _A = DPTForDepthEstimation.from_pretrained('Intel/dpt-hybrid-midas' ).to(_lowercase ) _A = prepare_img() _A = image_processor(images=_lowercase , return_tensors='pt' ).to(_lowercase ) # forward pass with torch.no_grad(): _A = model(**_lowercase ) _A = outputs.predicted_depth # verify the predicted depth _A = torch.Size((1, 384, 384) ) self.assertEqual(predicted_depth.shape , _lowercase ) _A = torch.tensor( [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]] ).to(_lowercase ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100 , _lowercase , atol=1E-4 ) )
27
import logging import os from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union from filelock import FileLock from transformers import PreTrainedTokenizer, is_tf_available, is_torch_available a_ :List[Any] = logging.getLogger(__name__) @dataclass class lowercase : lowerCamelCase : str lowerCamelCase : List[str] lowerCamelCase : Optional[List[str]] @dataclass class lowercase : lowerCamelCase : List[int] lowerCamelCase : List[int] lowerCamelCase : Optional[List[int]] = None lowerCamelCase : Optional[List[int]] = None class lowercase ( _UpperCAmelCase ): lowerCamelCase : Optional[Any] = '''train''' lowerCamelCase : Tuple = '''dev''' lowerCamelCase : Any = '''test''' class lowercase : @staticmethod def lowercase__ ( _lowercase : Any , _lowercase : Union[Split, str] ): raise NotImplementedError @staticmethod def lowercase__ ( _lowercase : str ): raise NotImplementedError @staticmethod def lowercase__ ( _lowercase : List[InputExample] , _lowercase : List[str] , _lowercase : int , _lowercase : PreTrainedTokenizer , _lowercase : int=False , _lowercase : Optional[Any]="[CLS]" , _lowercase : Tuple=1 , _lowercase : Optional[Any]="[SEP]" , _lowercase : Tuple=False , _lowercase : Optional[Any]=False , _lowercase : List[Any]=0 , _lowercase : Optional[int]=0 , _lowercase : Optional[Any]=-1_00 , _lowercase : Tuple=0 , _lowercase : Union[str, Any]=True , ): SCREAMING_SNAKE_CASE__ : Tuple = {label: i for i, label in enumerate(_lowercase )} SCREAMING_SNAKE_CASE__ : Dict = [] for ex_index, example in enumerate(_lowercase ): if ex_index % 1_00_00 == 0: logger.info('''Writing example %d of %d''' , _lowercase , len(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Tuple = [] SCREAMING_SNAKE_CASE__ : Optional[Any] = [] for word, label in zip(example.words , example.labels ): SCREAMING_SNAKE_CASE__ : Optional[Any] = tokenizer.tokenize(_lowercase ) # bert-base-multilingual-cased sometimes output "nothing ([]) when calling tokenize with just a space. if len(_lowercase ) > 0: tokens.extend(_lowercase ) # Use the real label id for the first token of the word, and padding ids for the remaining tokens label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(_lowercase ) - 1) ) # Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa. SCREAMING_SNAKE_CASE__ : Tuple = tokenizer.num_special_tokens_to_add() if len(_lowercase ) > max_seq_length - special_tokens_count: SCREAMING_SNAKE_CASE__ : List[str] = tokens[: (max_seq_length - special_tokens_count)] SCREAMING_SNAKE_CASE__ : Any = label_ids[: (max_seq_length - special_tokens_count)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens += [sep_token] label_ids += [pad_token_label_id] if sep_token_extra: # roberta uses an extra separator b/w pairs of sentences tokens += [sep_token] label_ids += [pad_token_label_id] SCREAMING_SNAKE_CASE__ : Optional[int] = [sequence_a_segment_id] * len(_lowercase ) if cls_token_at_end: tokens += [cls_token] label_ids += [pad_token_label_id] segment_ids += [cls_token_segment_id] else: SCREAMING_SNAKE_CASE__ : Optional[Any] = [cls_token] + tokens SCREAMING_SNAKE_CASE__ : Tuple = [pad_token_label_id] + label_ids SCREAMING_SNAKE_CASE__ : Tuple = [cls_token_segment_id] + segment_ids SCREAMING_SNAKE_CASE__ : Optional[int] = tokenizer.convert_tokens_to_ids(_lowercase ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. SCREAMING_SNAKE_CASE__ : str = [1 if mask_padding_with_zero else 0] * len(_lowercase ) # Zero-pad up to the sequence length. SCREAMING_SNAKE_CASE__ : List[str] = max_seq_length - len(_lowercase ) if pad_on_left: SCREAMING_SNAKE_CASE__ : Union[str, Any] = ([pad_token] * padding_length) + input_ids SCREAMING_SNAKE_CASE__ : str = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask SCREAMING_SNAKE_CASE__ : Tuple = ([pad_token_segment_id] * padding_length) + segment_ids SCREAMING_SNAKE_CASE__ : int = ([pad_token_label_id] * padding_length) + label_ids else: input_ids += [pad_token] * padding_length input_mask += [0 if mask_padding_with_zero else 1] * padding_length segment_ids += [pad_token_segment_id] * padding_length label_ids += [pad_token_label_id] * padding_length assert len(_lowercase ) == max_seq_length assert len(_lowercase ) == max_seq_length assert len(_lowercase ) == max_seq_length assert len(_lowercase ) == max_seq_length if ex_index < 5: logger.info('''*** Example ***''' ) logger.info('''guid: %s''' , example.guid ) logger.info('''tokens: %s''' , ''' '''.join([str(_lowercase ) for x in tokens] ) ) logger.info('''input_ids: %s''' , ''' '''.join([str(_lowercase ) for x in input_ids] ) ) logger.info('''input_mask: %s''' , ''' '''.join([str(_lowercase ) for x in input_mask] ) ) logger.info('''segment_ids: %s''' , ''' '''.join([str(_lowercase ) for x in segment_ids] ) ) logger.info('''label_ids: %s''' , ''' '''.join([str(_lowercase ) for x in label_ids] ) ) if "token_type_ids" not in tokenizer.model_input_names: SCREAMING_SNAKE_CASE__ : List[Any] = None features.append( InputFeatures( input_ids=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , label_ids=_lowercase ) ) return features if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset class lowercase ( _UpperCAmelCase ): lowerCamelCase : List[InputFeatures] lowerCamelCase : int = nn.CrossEntropyLoss().ignore_index def __init__( self : int , _lowercase : TokenClassificationTask , _lowercase : str , _lowercase : PreTrainedTokenizer , _lowercase : List[str] , _lowercase : str , _lowercase : Optional[int] = None , _lowercase : Optional[int]=False , _lowercase : Split = Split.train , ): # Load data features from cache or dataset file SCREAMING_SNAKE_CASE__ : List[Any] = os.path.join( _lowercase , '''cached_{}_{}_{}'''.format(mode.value , tokenizer.__class__.__name__ , str(_lowercase ) ) , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE__ : Optional[int] = cached_features_file + '''.lock''' with FileLock(_lowercase ): if os.path.exists(_lowercase ) and not overwrite_cache: logger.info(f"""Loading features from cached file {cached_features_file}""" ) SCREAMING_SNAKE_CASE__ : Any = torch.load(_lowercase ) else: logger.info(f"""Creating features from dataset file at {data_dir}""" ) SCREAMING_SNAKE_CASE__ : str = token_classification_task.read_examples_from_file(_lowercase , _lowercase ) # TODO clean up all this to leverage built-in features of tokenizers SCREAMING_SNAKE_CASE__ : Any = token_classification_task.convert_examples_to_features( _lowercase , _lowercase , _lowercase , _lowercase , cls_token_at_end=bool(model_type in ['''xlnet'''] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['''xlnet'''] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=_lowercase , pad_on_left=bool(tokenizer.padding_side == '''left''' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info(f"""Saving features into cached file {cached_features_file}""" ) torch.save(self.features , _lowercase ) def __len__( self : Tuple ): return len(self.features ) def __getitem__( self : Optional[int] , _lowercase : List[str] ): return self.features[i] if is_tf_available(): import tensorflow as tf class lowercase : lowerCamelCase : List[InputFeatures] lowerCamelCase : int = -100 def __init__( self : int , _lowercase : TokenClassificationTask , _lowercase : str , _lowercase : PreTrainedTokenizer , _lowercase : List[str] , _lowercase : str , _lowercase : Optional[int] = None , _lowercase : List[str]=False , _lowercase : Split = Split.train , ): SCREAMING_SNAKE_CASE__ : Optional[int] = token_classification_task.read_examples_from_file(_lowercase , _lowercase ) # TODO clean up all this to leverage built-in features of tokenizers SCREAMING_SNAKE_CASE__ : List[str] = token_classification_task.convert_examples_to_features( _lowercase , _lowercase , _lowercase , _lowercase , cls_token_at_end=bool(model_type in ['''xlnet'''] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ['''xlnet'''] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=_lowercase , pad_on_left=bool(tokenizer.padding_side == '''left''' ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) def gen(): for ex in self.features: if ex.token_type_ids is None: yield ( {"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label_ids, ) else: yield ( { "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label_ids, ) if "token_type_ids" not in tokenizer.model_input_names: SCREAMING_SNAKE_CASE__ : int = tf.data.Dataset.from_generator( _lowercase , ({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa}, tf.intaa) , ( {'''input_ids''': tf.TensorShape([None] ), '''attention_mask''': tf.TensorShape([None] )}, tf.TensorShape([None] ), ) , ) else: SCREAMING_SNAKE_CASE__ : int = tf.data.Dataset.from_generator( _lowercase , ({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa, '''token_type_ids''': tf.intaa}, tf.intaa) , ( { '''input_ids''': tf.TensorShape([None] ), '''attention_mask''': tf.TensorShape([None] ), '''token_type_ids''': tf.TensorShape([None] ), }, tf.TensorShape([None] ), ) , ) def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Optional[int] = self.dataset.apply(tf.data.experimental.assert_cardinality(len(self.features ) ) ) return self.dataset def __len__( self : Dict ): return len(self.features ) def __getitem__( self : Optional[Any] , _lowercase : Union[str, Any] ): return self.features[i]
35
0
import argparse import math import traceback import dateutil.parser as date_parser import requests def _SCREAMING_SNAKE_CASE ( __lowercase : Dict ) -> List[str]: """simple docstring""" __A = {} __A = job['''started_at'''] __A = job['''completed_at'''] __A = date_parser.parse(A__ ) __A = date_parser.parse(A__ ) __A = round((end_datetime - start_datetime).total_seconds() / 6_0.0 ) __A = start __A = end __A = duration_in_min return job_info def _SCREAMING_SNAKE_CASE ( __lowercase : Any , __lowercase : Optional[int]=None ) -> Dict: """simple docstring""" __A = None if token is not None: __A = {'''Accept''': '''application/vnd.github+json''', '''Authorization''': f"Bearer {token}"} __A = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100" __A = requests.get(A__ , headers=A__ ).json() __A = {} try: job_time.update({job["""name"""]: extract_time_from_single_job(A__ ) for job in result["""jobs"""]} ) __A = math.ceil((result["""total_count"""] - 1_0_0) / 1_0_0 ) for i in range(A__ ): __A = requests.get(url + f"&page={i + 2}" , headers=A__ ).json() job_time.update({job["""name"""]: extract_time_from_single_job(A__ ) for job in result["""jobs"""]} ) return job_time except Exception: print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}" ) return {} if __name__ == "__main__": __a : Any = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") __a : Any = parser.parse_args() __a : str = get_job_time(args.workflow_run_id) __a : Optional[int] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f"""{k}: {v['duration']}""")
637
import os def a ( A__ = "matrix.txt" ) -> int: '''simple docstring''' with open(os.path.join(os.path.dirname(A__ ) , A__ ) ) as in_file: SCREAMING_SNAKE_CASE__ : Optional[Any] = in_file.read() SCREAMING_SNAKE_CASE__ : Optional[Any] = [[int(A__ ) for cell in row.split(''',''' )] for row in data.strip().splitlines()] SCREAMING_SNAKE_CASE__ : Dict = [[0 for cell in row] for row in grid] SCREAMING_SNAKE_CASE__ : Any = len(grid[0] ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = [[0 for i in range(A__ )] for j in range(A__ )] SCREAMING_SNAKE_CASE__ : Tuple = grid[0][0] for i in range(1 , A__ ): SCREAMING_SNAKE_CASE__ : List[str] = grid[0][i] + dp[0][i - 1] for i in range(1 , A__ ): SCREAMING_SNAKE_CASE__ : List[str] = grid[i][0] + dp[i - 1][0] for i in range(1 , A__ ): for j in range(1 , A__ ): SCREAMING_SNAKE_CASE__ : Optional[int] = grid[i][j] + min(dp[i - 1][j] , dp[i][j - 1] ) return dp[-1][-1] if __name__ == "__main__": print(F'''{solution() = }''')
35
0
def __SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE = 100 ) -> int: SCREAMING_SNAKE_CASE_ : List[Any] = set() SCREAMING_SNAKE_CASE_ : Tuple = 0 SCREAMING_SNAKE_CASE_ : Union[str, Any] = n + 1 # maximum limit for a in range(2 , A__ ): for b in range(2 , A__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = a**b # calculates the current power collect_powers.add(A__ ) # adds the result to the set return len(A__ ) if __name__ == "__main__": print("Number of terms ", solution(int(str(input()).strip())))
345
from math import factorial def a ( A__ = 2_0 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... SCREAMING_SNAKE_CASE__ : Dict = n // 2 return int(factorial(A__ ) / (factorial(A__ ) * factorial(n - k )) ) if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution(20)) else: try: a_ :str = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number.')
35
0
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( "compression_format, is_archive" , [ ("7z", True), ("bz2", False), ("gzip", False), ("lz4", False), ("tar", True), ("xz", False), ("zip", True), ("zstd", False), ] , ) def _lowerCamelCase( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> Union[str, Any]: __snake_case = { '''7z''': (seven_zip_file, SevenZipExtractor), '''bz2''': (bza_file, BzipaExtractor), '''gzip''': (gz_file, GzipExtractor), '''lz4''': (lza_file, LzaExtractor), '''tar''': (tar_file, TarExtractor), '''xz''': (xz_file, XzExtractor), '''zip''': (zip_file, ZipExtractor), '''zstd''': (zstd_file, ZstdExtractor), } __snake_case = input_paths_and_base_extractors[compression_format] if input_path is None: __snake_case = f"""for '{compression_format}' compression_format, """ if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(A__ ) assert base_extractor.is_extractable(A__ ) __snake_case = tmp_path / ('''extracted''' if is_archive else '''extracted.txt''') base_extractor.extract(A__ , A__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name __snake_case = file_path.read_text(encoding="utf-8" ) else: __snake_case = output_path.read_text(encoding="utf-8" ) __snake_case = text_file.read_text(encoding="utf-8" ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( "compression_format, is_archive" , [ ("7z", True), ("bz2", False), ("gzip", False), ("lz4", False), ("tar", True), ("xz", False), ("zip", True), ("zstd", False), ] , ) def _lowerCamelCase( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> Optional[Any]: __snake_case = { '''7z''': seven_zip_file, '''bz2''': bza_file, '''gzip''': gz_file, '''lz4''': lza_file, '''tar''': tar_file, '''xz''': xz_file, '''zip''': zip_file, '''zstd''': zstd_file, } __snake_case = input_paths[compression_format] if input_path is None: __snake_case = f"""for '{compression_format}' compression_format, """ if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(A__ ) __snake_case = Extractor.infer_extractor_format(A__ ) assert extractor_format is not None __snake_case = tmp_path / ('''extracted''' if is_archive else '''extracted.txt''') Extractor.extract(A__ , A__ , A__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name __snake_case = file_path.read_text(encoding="utf-8" ) else: __snake_case = output_path.read_text(encoding="utf-8" ) __snake_case = text_file.read_text(encoding="utf-8" ) assert extracted_file_content == expected_file_content @pytest.fixture def _lowerCamelCase( __snake_case , __snake_case ) -> str: import tarfile __snake_case = tmp_path / '''data_dot_dot''' directory.mkdir() __snake_case = directory / '''tar_file_with_dot_dot.tar''' with tarfile.TarFile(A__ , "w" ) as f: f.add(A__ , arcname=os.path.join(".." , text_file.name ) ) return path @pytest.fixture def _lowerCamelCase( __snake_case ) -> Union[str, Any]: import tarfile __snake_case = tmp_path / '''data_sym_link''' directory.mkdir() __snake_case = directory / '''tar_file_with_sym_link.tar''' os.symlink(".." , directory / "subdir" , target_is_directory=A__ ) with tarfile.TarFile(A__ , "w" ) as f: f.add(str(directory / "subdir" ) , arcname="subdir" ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( "insecure_tar_file, error_log" , [("tar_file_with_dot_dot", "illegal path"), ("tar_file_with_sym_link", "Symlink")] , ) def _lowerCamelCase( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> Optional[Any]: __snake_case = { '''tar_file_with_dot_dot''': tar_file_with_dot_dot, '''tar_file_with_sym_link''': tar_file_with_sym_link, } __snake_case = insecure_tar_files[insecure_tar_file] __snake_case = tmp_path / '''extracted''' TarExtractor.extract(A__ , A__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def _lowerCamelCase( __snake_case ) -> str: __snake_case = tmpdir / '''not_a_zip_file''' # From: https://github.com/python/cpython/pull/5053 __snake_case = ( b'''\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00''' b'''\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6\'\x00\x00\x00\x15I''' b'''DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07''' b'''\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82''' ) with not_a_zip_file.open("wb" ) as f: f.write(A__ ) assert zipfile.is_zipfile(str(A__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(A__ ) # but we're right
524
import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class lowercase : @staticmethod def lowercase__ ( *_lowercase : Optional[Any] , **_lowercase : str ): pass def a ( A__ ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class lowercase ( unittest.TestCase ): lowerCamelCase : int = MODEL_FOR_DEPTH_ESTIMATION_MAPPING def lowercase__ ( self : List[Any] , _lowercase : Tuple , _lowercase : Any , _lowercase : List[str] ): SCREAMING_SNAKE_CASE__ : List[str] = DepthEstimationPipeline(model=_lowercase , image_processor=_lowercase ) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def lowercase__ ( self : Union[str, Any] , _lowercase : int , _lowercase : int ): SCREAMING_SNAKE_CASE__ : Optional[int] = depth_estimator('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) self.assertEqual({'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )} , _lowercase ) import datasets SCREAMING_SNAKE_CASE__ : List[str] = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''' ) SCREAMING_SNAKE_CASE__ : Dict = depth_estimator( [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ] ) self.assertEqual( [ {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, {'''predicted_depth''': ANY(torch.Tensor ), '''depth''': ANY(Image.Image )}, ] , _lowercase , ) @require_tf @unittest.skip('''Depth estimation is not implemented in TF''' ) def lowercase__ ( self : Optional[int] ): pass @slow @require_torch def lowercase__ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE__ : List[str] = '''Intel/dpt-large''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = pipeline('''depth-estimation''' , model=_lowercase ) SCREAMING_SNAKE_CASE__ : Dict = depth_estimator('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) SCREAMING_SNAKE_CASE__ : List[str] = hashimage(outputs['''depth'''] ) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs['''predicted_depth'''].max().item() ) , 29.304 ) self.assertEqual(nested_simplify(outputs['''predicted_depth'''].min().item() ) , 2.662 ) @require_torch def lowercase__ ( self : str ): # This is highly irregular to have no small tests. self.skipTest('''There is not hf-internal-testing tiny model for either GLPN nor DPT''' )
35
0
"""simple docstring""" import argparse import torch from torch import nn from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration def __lowerCAmelCase ( __UpperCamelCase : List[Any] ): '''simple docstring''' snake_case_ : List[Any] = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''encoder.embed_positions._float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(A__ , A__ ) def __lowerCAmelCase ( __UpperCamelCase : Union[str, Any] ): '''simple docstring''' snake_case_ : List[str] = emb.weight.shape snake_case_ : List[Any] = nn.Linear(A__ , A__ , bias=A__ ) snake_case_ : Tuple = emb.weight.data return lin_layer def __lowerCAmelCase ( __UpperCamelCase : Dict ): '''simple docstring''' snake_case_ : Tuple = torch.load(A__ , map_location="""cpu""" ) snake_case_ : Tuple = mam_aaa['''args'''] or mam_aaa['''cfg''']['''model'''] snake_case_ : Any = mam_aaa['''model'''] remove_ignore_keys_(A__ ) snake_case_ : Optional[Any] = state_dict['''encoder.embed_tokens.weight'''].shape[0] snake_case_ : Any = MaMaaaConfig( vocab_size=A__ , max_position_embeddings=1_0_2_4 , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , encoder_layerdrop=args.encoder_layerdrop , decoder_layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , ) snake_case_ : List[str] = state_dict['''decoder.embed_tokens.weight'''] snake_case_ : Dict = MaMaaaForConditionalGeneration(A__ ) model.model.load_state_dict(A__ , strict=A__ ) snake_case_ : int = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": __lowerCAmelCase : Any = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowerCAmelCase : Any = parser.parse_args() __lowerCAmelCase : int = convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß) model.save_pretrained(args.pytorch_dump_folder_path)
58
def a ( A__ ) -> int: '''simple docstring''' if a < 0: raise ValueError('''Input value must be a positive integer''' ) elif isinstance(A__ , A__ ): raise TypeError('''Input value must be a \'int\' type''' ) return bin(A__ ).count('''1''' ) if __name__ == "__main__": import doctest doctest.testmod()
35
0
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import PoolFormerImageProcessor class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self, _a, _a=7, _a=3, _a=30, _a=4_00, _a=True, _a=None, _a=0.9, _a=None, _a=True, _a=[0.5, 0.5, 0.5], _a=[0.5, 0.5, 0.5], ) -> int: __SCREAMING_SNAKE_CASE = size if size is not None else {'''shortest_edge''': 30} __SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {'''height''': 30, '''width''': 30} __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = min_resolution __SCREAMING_SNAKE_CASE = max_resolution __SCREAMING_SNAKE_CASE = do_resize_and_center_crop __SCREAMING_SNAKE_CASE = size __SCREAMING_SNAKE_CASE = crop_pct __SCREAMING_SNAKE_CASE = crop_size __SCREAMING_SNAKE_CASE = do_normalize __SCREAMING_SNAKE_CASE = image_mean __SCREAMING_SNAKE_CASE = image_std def __lowerCAmelCase ( self ) -> Optional[Any]: return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __SCREAMING_SNAKE_CASE ( _UpperCAmelCase , unittest.TestCase ): SCREAMING_SNAKE_CASE__ =PoolFormerImageProcessor if is_vision_available() else None def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = PoolFormerImageProcessingTester(self ) @property def __lowerCAmelCase ( self ) -> Dict: return self.image_processor_tester.prepare_image_processor_dict() def __lowerCAmelCase ( self ) -> Optional[Any]: __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowercase, "do_resize_and_center_crop" ) ) self.assertTrue(hasattr(_lowercase, "size" ) ) self.assertTrue(hasattr(_lowercase, "crop_pct" ) ) self.assertTrue(hasattr(_lowercase, "do_normalize" ) ) self.assertTrue(hasattr(_lowercase, "image_mean" ) ) self.assertTrue(hasattr(_lowercase, "image_std" ) ) def __lowerCAmelCase ( self ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size, {"shortest_edge": 30} ) self.assertEqual(image_processor.crop_size, {"height": 30, "width": 30} ) __SCREAMING_SNAKE_CASE = self.image_processing_class.from_dict(self.image_processor_dict, size=42, crop_size=84 ) self.assertEqual(image_processor.size, {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size, {"height": 84, "width": 84} ) def __lowerCAmelCase ( self ) -> Dict: pass def __lowerCAmelCase ( self ) -> List[Any]: # Initialize image_processing __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester, equal_resolution=_lowercase ) for image in image_inputs: self.assertIsInstance(_lowercase, Image.Image ) # Test not batched input __SCREAMING_SNAKE_CASE = image_processing(image_inputs[0], return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) # Test batched __SCREAMING_SNAKE_CASE = image_processing(_lowercase, return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) def __lowerCAmelCase ( self ) -> Optional[Any]: # Initialize image_processing __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester, equal_resolution=_lowercase, numpify=_lowercase ) for image in image_inputs: self.assertIsInstance(_lowercase, np.ndarray ) # Test not batched input __SCREAMING_SNAKE_CASE = image_processing(image_inputs[0], return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) # Test batched __SCREAMING_SNAKE_CASE = image_processing(_lowercase, return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) def __lowerCAmelCase ( self ) -> Any: # Initialize image_processing __SCREAMING_SNAKE_CASE = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __SCREAMING_SNAKE_CASE = prepare_image_inputs(self.image_processor_tester, equal_resolution=_lowercase, torchify=_lowercase ) for image in image_inputs: self.assertIsInstance(_lowercase, torch.Tensor ) # Test not batched input __SCREAMING_SNAKE_CASE = image_processing(image_inputs[0], return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ), ) # Test batched __SCREAMING_SNAKE_CASE = image_processing(_lowercase, return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ), )
693
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL a_ :str = logging.get_logger(__name__) def a ( A__ , A__ , A__ , A__ ) -> Tuple[int, int]: '''simple docstring''' def constraint_to_multiple_of(A__ , A__ , A__=0 , A__=None ): SCREAMING_SNAKE_CASE__ : Optional[int] = round(val / multiple ) * multiple if max_val is not None and x > max_val: SCREAMING_SNAKE_CASE__ : Any = math.floor(val / multiple ) * multiple if x < min_val: SCREAMING_SNAKE_CASE__ : Any = math.ceil(val / multiple ) * multiple return x SCREAMING_SNAKE_CASE__ : Union[str, Any] = (output_size, output_size) if isinstance(A__ , A__ ) else output_size SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple = get_image_size(A__ ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[str] = output_size # determine new height and width SCREAMING_SNAKE_CASE__ : List[str] = output_height / input_height SCREAMING_SNAKE_CASE__ : Dict = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width SCREAMING_SNAKE_CASE__ : List[str] = scale_width else: # fit height SCREAMING_SNAKE_CASE__ : Optional[Any] = scale_height SCREAMING_SNAKE_CASE__ : int = constraint_to_multiple_of(scale_height * input_height , multiple=A__ ) SCREAMING_SNAKE_CASE__ : int = constraint_to_multiple_of(scale_width * input_width , multiple=A__ ) return (new_height, new_width) class lowercase ( _UpperCAmelCase ): lowerCamelCase : List[str] = ['''pixel_values'''] def __init__( self : List[Any] , _lowercase : bool = True , _lowercase : Dict[str, int] = None , _lowercase : PILImageResampling = PILImageResampling.BILINEAR , _lowercase : bool = False , _lowercase : int = 1 , _lowercase : bool = True , _lowercase : Union[int, float] = 1 / 2_55 , _lowercase : bool = True , _lowercase : Optional[Union[float, List[float]]] = None , _lowercase : Optional[Union[float, List[float]]] = None , **_lowercase : List[Any] , ): super().__init__(**_lowercase ) SCREAMING_SNAKE_CASE__ : Dict = size if size is not None else {'''height''': 3_84, '''width''': 3_84} SCREAMING_SNAKE_CASE__ : Optional[int] = get_size_dict(_lowercase ) SCREAMING_SNAKE_CASE__ : List[str] = do_resize SCREAMING_SNAKE_CASE__ : Optional[int] = size SCREAMING_SNAKE_CASE__ : int = keep_aspect_ratio SCREAMING_SNAKE_CASE__ : Optional[Any] = ensure_multiple_of SCREAMING_SNAKE_CASE__ : List[str] = resample SCREAMING_SNAKE_CASE__ : Union[str, Any] = do_rescale SCREAMING_SNAKE_CASE__ : Optional[int] = rescale_factor SCREAMING_SNAKE_CASE__ : List[Any] = do_normalize SCREAMING_SNAKE_CASE__ : Tuple = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE__ : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowercase__ ( self : Optional[int] , _lowercase : np.ndarray , _lowercase : Dict[str, int] , _lowercase : bool = False , _lowercase : int = 1 , _lowercase : PILImageResampling = PILImageResampling.BICUBIC , _lowercase : Optional[Union[str, ChannelDimension]] = None , **_lowercase : Optional[int] , ): SCREAMING_SNAKE_CASE__ : List[Any] = get_size_dict(_lowercase ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = get_resize_output_image_size( _lowercase , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=_lowercase , multiple=_lowercase , ) return resize(_lowercase , size=_lowercase , resample=_lowercase , data_format=_lowercase , **_lowercase ) def lowercase__ ( self : List[str] , _lowercase : np.ndarray , _lowercase : Union[int, float] , _lowercase : Optional[Union[str, ChannelDimension]] = None , **_lowercase : str , ): return rescale(_lowercase , scale=_lowercase , data_format=_lowercase , **_lowercase ) def lowercase__ ( self : List[str] , _lowercase : np.ndarray , _lowercase : Union[float, List[float]] , _lowercase : Union[float, List[float]] , _lowercase : Optional[Union[str, ChannelDimension]] = None , **_lowercase : Optional[Any] , ): return normalize(_lowercase , mean=_lowercase , std=_lowercase , data_format=_lowercase , **_lowercase ) def lowercase__ ( self : Optional[Any] , _lowercase : ImageInput , _lowercase : bool = None , _lowercase : int = None , _lowercase : bool = None , _lowercase : int = None , _lowercase : PILImageResampling = None , _lowercase : bool = None , _lowercase : float = None , _lowercase : bool = None , _lowercase : Optional[Union[float, List[float]]] = None , _lowercase : Optional[Union[float, List[float]]] = None , _lowercase : Optional[Union[str, TensorType]] = None , _lowercase : ChannelDimension = ChannelDimension.FIRST , **_lowercase : Tuple , ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE__ : List[Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE__ : List[str] = get_size_dict(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio SCREAMING_SNAKE_CASE__ : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of SCREAMING_SNAKE_CASE__ : Tuple = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE__ : str = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE__ : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE__ : Optional[int] = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE__ : Tuple = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE__ : str = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE__ : Optional[Any] = make_list_of_images(_lowercase ) if not valid_images(_lowercase ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE__ : str = [to_numpy_array(_lowercase ) for image in images] if do_resize: SCREAMING_SNAKE_CASE__ : Any = [self.resize(image=_lowercase , size=_lowercase , resample=_lowercase ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE__ : Tuple = [self.rescale(image=_lowercase , scale=_lowercase ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE__ : Any = [self.normalize(image=_lowercase , mean=_lowercase , std=_lowercase ) for image in images] SCREAMING_SNAKE_CASE__ : Union[str, Any] = [to_channel_dimension_format(_lowercase , _lowercase ) for image in images] SCREAMING_SNAKE_CASE__ : str = {'''pixel_values''': images} return BatchFeature(data=_lowercase , tensor_type=_lowercase ) def lowercase__ ( self : Tuple , _lowercase : Optional[Any] , _lowercase : List[Tuple] = None ): SCREAMING_SNAKE_CASE__ : str = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_lowercase ) != len(_lowercase ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(_lowercase ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = target_sizes.numpy() SCREAMING_SNAKE_CASE__ : Tuple = [] for idx in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Dict = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=_lowercase ) SCREAMING_SNAKE_CASE__ : Any = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_lowercase ) else: SCREAMING_SNAKE_CASE__ : Any = logits.argmax(dim=1 ) SCREAMING_SNAKE_CASE__ : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
35
0
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, RobertaTokenizer, RobertaTokenizerFast from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __snake_case ( _UpperCAmelCase , unittest.TestCase ): __lowerCAmelCase = RobertaTokenizer __lowerCAmelCase = RobertaTokenizerFast __lowerCAmelCase = True __lowerCAmelCase = {'''cls_token''': '''<s>'''} def _snake_case ( self ) -> List[Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt snake_case__ = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] snake_case__ = dict(zip(_lowercase , range(len(_lowercase ) ) ) ) snake_case__ = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] snake_case__ = {'''unk_token''': '''<unk>'''} snake_case__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) snake_case__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(_lowercase ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(_lowercase ) ) def _snake_case ( self , **UpperCamelCase_ ) -> Any: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_lowercase ) def _snake_case ( self , **UpperCamelCase_ ) -> Tuple: kwargs.update(self.special_tokens_map ) return RobertaTokenizerFast.from_pretrained(self.tmpdirname , **_lowercase ) def _snake_case ( self , UpperCamelCase_ ) -> Dict: snake_case__ = '''lower newer''' snake_case__ = '''lower newer''' return input_text, output_text def _snake_case ( self ) -> int: snake_case__ = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) snake_case__ = '''lower newer''' snake_case__ = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] snake_case__ = tokenizer.tokenize(_lowercase ) # , add_prefix_space=True) self.assertListEqual(_lowercase , _lowercase ) snake_case__ = tokens + [tokenizer.unk_token] snake_case__ = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowercase ) , _lowercase ) def _snake_case ( self ) -> List[Any]: snake_case__ = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=_lowercase ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=_lowercase ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def _snake_case ( self ) -> Tuple: snake_case__ = self.tokenizer_class.from_pretrained('roberta-base' ) snake_case__ = tokenizer.encode('sequence builders' , add_special_tokens=_lowercase ) snake_case__ = tokenizer.encode('multi-sequence build' , add_special_tokens=_lowercase ) snake_case__ = tokenizer.encode( 'sequence builders' , add_special_tokens=_lowercase , add_prefix_space=_lowercase ) snake_case__ = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=_lowercase , add_prefix_space=_lowercase ) snake_case__ = tokenizer.build_inputs_with_special_tokens(_lowercase ) snake_case__ = tokenizer.build_inputs_with_special_tokens(_lowercase , _lowercase ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _snake_case ( self ) -> str: snake_case__ = self.get_tokenizer() snake_case__ = '''Encode this sequence.''' snake_case__ = tokenizer.byte_encoder[''' '''.encode('utf-8' )[0]] # Testing encoder arguments snake_case__ = tokenizer.encode(_lowercase , add_special_tokens=_lowercase , add_prefix_space=_lowercase ) snake_case__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_lowercase , _lowercase ) snake_case__ = tokenizer.encode(_lowercase , add_special_tokens=_lowercase , add_prefix_space=_lowercase ) snake_case__ = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_lowercase , _lowercase ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) snake_case__ = tokenizer.encode(_lowercase , add_special_tokens=_lowercase ) snake_case__ = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_lowercase , _lowercase ) # Testing spaces after special tokens snake_case__ = '''<mask>''' tokenizer.add_special_tokens( {'mask_token': AddedToken(_lowercase , lstrip=_lowercase , rstrip=_lowercase )} ) # mask token has a left space snake_case__ = tokenizer.convert_tokens_to_ids(_lowercase ) snake_case__ = '''Encode <mask> sequence''' snake_case__ = '''Encode <mask>sequence''' snake_case__ = tokenizer.encode(_lowercase ) snake_case__ = encoded.index(_lowercase ) snake_case__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_lowercase , _lowercase ) snake_case__ = tokenizer.encode(_lowercase ) snake_case__ = encoded.index(_lowercase ) snake_case__ = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_lowercase , _lowercase ) def _snake_case ( self ) -> Any: pass def _snake_case ( self ) -> int: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): snake_case__ = self.rust_tokenizer_class.from_pretrained(_lowercase , **_lowercase ) snake_case__ = self.tokenizer_class.from_pretrained(_lowercase , **_lowercase ) snake_case__ = '''A, <mask> AllenNLP sentence.''' snake_case__ = tokenizer_r.encode_plus(_lowercase , add_special_tokens=_lowercase , return_token_type_ids=_lowercase ) snake_case__ = tokenizer_p.encode_plus(_lowercase , add_special_tokens=_lowercase , return_token_type_ids=_lowercase ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) snake_case__ = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) snake_case__ = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( _lowercase , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( _lowercase , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def _snake_case ( self ) -> Tuple: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): snake_case__ = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) snake_case__ = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , _lowercase ) self.assertEqual(post_processor_state['add_prefix_space'] , _lowercase ) self.assertEqual(post_processor_state['trim_offsets'] , _lowercase ) def _snake_case ( self ) -> str: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): snake_case__ = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` snake_case__ = F'''{text_of_1_token} {text_of_1_token}''' snake_case__ = self.rust_tokenizer_class.from_pretrained( _lowercase , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = tokenizer_r(_lowercase , return_offsets_mapping=_lowercase , add_special_tokens=_lowercase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowercase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowercase ) + 1, len(_lowercase ) + 1 + len(_lowercase )) , ) snake_case__ = self.rust_tokenizer_class.from_pretrained( _lowercase , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = tokenizer_r(_lowercase , return_offsets_mapping=_lowercase , add_special_tokens=_lowercase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowercase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowercase ) + 1, len(_lowercase ) + 1 + len(_lowercase )) , ) snake_case__ = self.rust_tokenizer_class.from_pretrained( _lowercase , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = tokenizer_r(_lowercase , return_offsets_mapping=_lowercase , add_special_tokens=_lowercase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowercase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowercase ), len(_lowercase ) + 1 + len(_lowercase )) , ) snake_case__ = self.rust_tokenizer_class.from_pretrained( _lowercase , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = tokenizer_r(_lowercase , return_offsets_mapping=_lowercase , add_special_tokens=_lowercase ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_lowercase )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_lowercase ), len(_lowercase ) + 1 + len(_lowercase )) , ) snake_case__ = F''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) snake_case__ = self.rust_tokenizer_class.from_pretrained( _lowercase , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = tokenizer_r(_lowercase , return_offsets_mapping=_lowercase , add_special_tokens=_lowercase ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_lowercase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_lowercase ) + 1, 1 + len(_lowercase ) + 1 + len(_lowercase )) , ) snake_case__ = self.rust_tokenizer_class.from_pretrained( _lowercase , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = tokenizer_r(_lowercase , return_offsets_mapping=_lowercase , add_special_tokens=_lowercase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_lowercase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_lowercase ), 1 + len(_lowercase ) + 1 + len(_lowercase )) , ) snake_case__ = self.rust_tokenizer_class.from_pretrained( _lowercase , use_fast=_lowercase , add_prefix_space=_lowercase , trim_offsets=_lowercase ) snake_case__ = tokenizer_r(_lowercase , return_offsets_mapping=_lowercase , add_special_tokens=_lowercase ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_lowercase )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_lowercase ), 1 + len(_lowercase ) + 1 + len(_lowercase )) , )
368
from __future__ import annotations from typing import Any class lowercase : def __init__( self : int , _lowercase : int ): SCREAMING_SNAKE_CASE__ : List[str] = num_of_nodes SCREAMING_SNAKE_CASE__ : list[list[int]] = [] SCREAMING_SNAKE_CASE__ : dict[int, int] = {} def lowercase__ ( self : Union[str, Any] , _lowercase : int , _lowercase : int , _lowercase : int ): self.m_edges.append([u_node, v_node, weight] ) def lowercase__ ( self : Optional[int] , _lowercase : int ): if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def lowercase__ ( self : Optional[Any] , _lowercase : int ): if self.m_component[u_node] != u_node: for k in self.m_component: SCREAMING_SNAKE_CASE__ : Any = self.find_component(_lowercase ) def lowercase__ ( self : int , _lowercase : list[int] , _lowercase : int , _lowercase : int ): if component_size[u_node] <= component_size[v_node]: SCREAMING_SNAKE_CASE__ : Dict = v_node component_size[v_node] += component_size[u_node] self.set_component(_lowercase ) elif component_size[u_node] >= component_size[v_node]: SCREAMING_SNAKE_CASE__ : List[Any] = self.find_component(_lowercase ) component_size[u_node] += component_size[v_node] self.set_component(_lowercase ) def lowercase__ ( self : str ): SCREAMING_SNAKE_CASE__ : Optional[int] = [] SCREAMING_SNAKE_CASE__ : Any = 0 SCREAMING_SNAKE_CASE__ : list[Any] = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) SCREAMING_SNAKE_CASE__ : List[str] = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Union[str, Any] = edge SCREAMING_SNAKE_CASE__ : Tuple = self.m_component[u] SCREAMING_SNAKE_CASE__ : List[str] = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): SCREAMING_SNAKE_CASE__ : int = [u, v, w] for edge in minimum_weight_edge: if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[str] = edge SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.m_component[u] SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.m_component[v] if u_component != v_component: mst_weight += w self.union(_lowercase , _lowercase , _lowercase ) print(f"""Added edge [{u} - {v}]\nAdded weight: {w}\n""" ) num_of_components -= 1 SCREAMING_SNAKE_CASE__ : List[Any] = [-1] * self.m_num_of_nodes print(f"""The total weight of the minimal spanning tree is: {mst_weight}""" ) def a ( ) -> None: '''simple docstring''' if __name__ == "__main__": import doctest doctest.testmod()
35
0
'''simple docstring''' from ...utils import logging from ..ta.modeling_tf_ta import TFTaEncoderModel, TFTaForConditionalGeneration, TFTaModel from .configuration_mta import MTaConfig lowercase__ : Union[str, Any] = logging.get_logger(__name__) lowercase__ : Union[str, Any] = 'T5Config' class lowerCamelCase ( _UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ = '''mt5''' lowerCAmelCase__ = MTaConfig class lowerCamelCase ( _UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ = '''mt5''' lowerCAmelCase__ = MTaConfig class lowerCamelCase ( _UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ = '''mt5''' lowerCAmelCase__ = MTaConfig
390
from typing import TYPE_CHECKING from ...utils import _LazyModule a_ :Tuple = {'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys a_ :Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
35
0
"""simple docstring""" __lowercase : int = 6_5_5_2_1 def lowerCamelCase_ ( _lowerCamelCase : str ): lowerCamelCase_ = 1 lowerCamelCase_ = 0 for plain_chr in plain_text: lowerCamelCase_ = (a + ord(A__ )) % MOD_ADLER lowerCamelCase_ = (b + a) % MOD_ADLER return (b << 1_6) | a
142
def a ( A__ ) -> str: '''simple docstring''' return "".join([hex(A__ )[2:].zfill(2 ).upper() for byte in list(A__ )] ) def a ( A__ ) -> bytes: '''simple docstring''' if (len(A__ ) % 2) != 0: raise ValueError( '''Base16 encoded data is invalid: Data does not have an even number of hex digits.''' ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(A__ ) <= set('''0123456789ABCDEF''' ): raise ValueError( '''Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.''' ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] , 1_6 ) for i in range(0 , len(A__ ) , 2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
35
0
"""simple docstring""" import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel lowerCamelCase__ : Tuple = { 'text_branch': 'text_model', 'audio_branch': 'audio_model.audio_encoder', 'attn': 'attention.self', 'self.proj': 'output.dense', 'attention.self_mask': 'attn_mask', 'mlp.fc1': 'intermediate.dense', 'mlp.fc2': 'output.dense', 'norm1': 'layernorm_before', 'norm2': 'layernorm_after', 'bn0': 'batch_norm', } lowerCamelCase__ : List[str] = AutoFeatureExtractor.from_pretrained('''laion/clap-htsat-unfused''', truncation='''rand_trunc''') def UpperCamelCase ( _lowerCAmelCase : Optional[Any], _lowerCAmelCase : str=False ) -> List[str]: _UpperCAmelCase : Optional[int] = create_model( """HTSAT-tiny""", """roberta""", A__, precision="""fp32""", device="""cuda:0""" if torch.cuda.is_available() else """cpu""", enable_fusion=A__, fusion_type="""aff_2d""" if enable_fusion else None, ) return model, model_cfg def UpperCamelCase ( _lowerCAmelCase : Dict ) -> Union[str, Any]: _UpperCAmelCase : Dict = {} _UpperCAmelCase : Optional[int] = r'''.*sequential.(\d+).*''' _UpperCAmelCase : Any = r'''.*_projection.(\d+).*''' for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: _UpperCAmelCase : Dict = key.replace(A__, A__ ) if re.match(A__, A__ ): # replace sequential layers with list _UpperCAmelCase : Union[str, Any] = re.match(A__, A__ ).group(1 ) _UpperCAmelCase : Optional[Any] = key.replace(f'''sequential.{sequential_layer}.''', f'''layers.{int(A__ )//3}.linear.''' ) elif re.match(A__, A__ ): _UpperCAmelCase : Optional[int] = int(re.match(A__, A__ ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... _UpperCAmelCase : Optional[int] = 1 if projecton_layer == 0 else 2 _UpperCAmelCase : Optional[int] = key.replace(f'''_projection.{projecton_layer}.''', f'''_projection.linear{transformers_projection_layer}.''' ) if "audio" and "qkv" in key: # split qkv into query key and value _UpperCAmelCase : Optional[int] = value _UpperCAmelCase : Tuple = mixed_qkv.size(0 ) // 3 _UpperCAmelCase : Optional[int] = mixed_qkv[:qkv_dim] _UpperCAmelCase : Tuple = mixed_qkv[qkv_dim : qkv_dim * 2] _UpperCAmelCase : Optional[Any] = mixed_qkv[qkv_dim * 2 :] _UpperCAmelCase : str = query_layer _UpperCAmelCase : Dict = key_layer _UpperCAmelCase : Union[str, Any] = value_layer else: _UpperCAmelCase : List[Any] = value return model_state_dict def UpperCamelCase ( _lowerCAmelCase : Tuple, _lowerCAmelCase : Optional[Any], _lowerCAmelCase : Union[str, Any], _lowerCAmelCase : Optional[Any]=False ) -> List[Any]: _UpperCAmelCase : str = init_clap(A__, enable_fusion=A__ ) clap_model.eval() _UpperCAmelCase : int = clap_model.state_dict() _UpperCAmelCase : Optional[Any] = rename_state_dict(A__ ) _UpperCAmelCase : str = ClapConfig() _UpperCAmelCase : List[Any] = enable_fusion _UpperCAmelCase : List[Any] = ClapModel(A__ ) # ignore the spectrogram embedding layer model.load_state_dict(A__, strict=A__ ) model.save_pretrained(A__ ) transformers_config.save_pretrained(A__ ) if __name__ == "__main__": lowerCamelCase__ : List[Any] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument('''--enable_fusion''', action='''store_true''', help='''Whether to enable fusion or not''') lowerCamelCase__ : Any = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
238
import inspect import os import unittest from pathlib import Path import torch import accelerate from accelerate.test_utils import execute_subprocess_async from accelerate.test_utils.testing import run_command class lowercase ( unittest.TestCase ): lowerCamelCase : List[Any] = inspect.getfile(accelerate.test_utils ) lowerCamelCase : Optional[int] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_cli.py'''] ) lowerCamelCase : Any = ['''accelerate''', '''launch'''] lowerCamelCase : Dict = Path.home() / '''.cache/huggingface/accelerate''' lowerCamelCase : Optional[int] = '''default_config.yaml''' lowerCamelCase : Optional[Any] = config_folder / config_file lowerCamelCase : Optional[Any] = config_folder / '''_default_config.yaml''' lowerCamelCase : Optional[Any] = Path('''tests/test_configs''' ) @classmethod def lowercase__ ( cls : Any ): if cls.config_path.is_file(): cls.config_path.rename(cls.changed_path ) @classmethod def lowercase__ ( cls : List[Any] ): if cls.changed_path.is_file(): cls.changed_path.rename(cls.config_path ) def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Dict = self.base_cmd if torch.cuda.is_available() and (torch.cuda.device_count() > 1): cmd += ["--multi_gpu"] execute_subprocess_async(cmd + [self.test_file_path] , env=os.environ.copy() ) def lowercase__ ( self : Tuple ): for config in sorted(self.test_config_path.glob('''**/*.yaml''' ) ): with self.subTest(config_file=_lowercase ): execute_subprocess_async( self.base_cmd + ['''--config_file''', str(_lowercase ), self.test_file_path] , env=os.environ.copy() ) def lowercase__ ( self : Optional[int] ): execute_subprocess_async(['''accelerate''', '''test'''] , env=os.environ.copy() ) class lowercase ( unittest.TestCase ): lowerCamelCase : str = '''test-tpu''' lowerCamelCase : Tuple = '''us-central1-a''' lowerCamelCase : Optional[int] = '''ls''' lowerCamelCase : Dict = ['''accelerate''', '''tpu-config'''] lowerCamelCase : Tuple = '''cd /usr/share''' lowerCamelCase : List[Any] = '''tests/test_samples/test_command_file.sh''' lowerCamelCase : Any = '''Running gcloud compute tpus tpu-vm ssh''' def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = run_command( self.cmd + ['''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , _lowercase , ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : List[str] = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , _lowercase , ) def lowercase__ ( self : str ): SCREAMING_SNAKE_CASE__ : Optional[int] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--debug'''] , return_stdout=_lowercase ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : List[Any] ): SCREAMING_SNAKE_CASE__ : Optional[Any] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , _lowercase , ) def lowercase__ ( self : int ): SCREAMING_SNAKE_CASE__ : str = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--command''', '''echo "Hello World"''', '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all""" , _lowercase , ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : Any = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command_file''', self.command_file, '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : Tuple ): SCREAMING_SNAKE_CASE__ : Optional[int] = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command_file''', self.command_file, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : Any ): SCREAMING_SNAKE_CASE__ : List[Any] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--debug'''] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , ) def lowercase__ ( self : int ): SCREAMING_SNAKE_CASE__ : Optional[Any] = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--accelerate_version''', '''12.0.0''', '''--debug''', ] , return_stdout=_lowercase , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all""" , _lowercase , )
35
0
import json import os import unittest from transformers import AutoTokenizer, GPTaTokenizer, GPTaTokenizerFast from transformers.models.gpta.tokenization_gpta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class a ( _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' lowerCAmelCase : Tuple = GPTaTokenizer lowerCAmelCase : Dict = GPTaTokenizerFast lowerCAmelCase : Any = True lowerCAmelCase : Tuple = {'''add_prefix_space''': True} lowerCAmelCase : Dict = False def lowerCamelCase_ ( self : str ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCAmelCase_ = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', '''<|endoftext|>''', ] UpperCAmelCase_ = dict(zip(_lowercase , range(len(_lowercase ) ) ) ) UpperCAmelCase_ = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCAmelCase_ = {'''unk_token''': '''<unk>'''} UpperCAmelCase_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCAmelCase_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_lowercase ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_lowercase ) ) def lowerCamelCase_ ( self : int , **__snake_case : Any ): kwargs.update(self.special_tokens_map ) return GPTaTokenizer.from_pretrained(self.tmpdirname , **_lowercase ) def lowerCamelCase_ ( self : Optional[int] , **__snake_case : Tuple ): kwargs.update(self.special_tokens_map ) return GPTaTokenizerFast.from_pretrained(self.tmpdirname , **_lowercase ) def lowerCamelCase_ ( self : Any , __snake_case : Optional[Any] ): UpperCAmelCase_ = '''lower newer''' UpperCAmelCase_ = '''lower newer''' return input_text, output_text def lowerCamelCase_ ( self : int ): UpperCAmelCase_ = GPTaTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) UpperCAmelCase_ = '''lower newer''' UpperCAmelCase_ = ['''\u0120low''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCAmelCase_ = tokenizer.tokenize(_lowercase , add_prefix_space=_lowercase ) self.assertListEqual(_lowercase , _lowercase ) UpperCAmelCase_ = tokens + [tokenizer.unk_token] UpperCAmelCase_ = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowercase ) , _lowercase ) def lowerCamelCase_ ( self : int ): if not self.test_rust_tokenizer: return UpperCAmelCase_ = self.get_tokenizer() UpperCAmelCase_ = self.get_rust_tokenizer(add_prefix_space=_lowercase ) UpperCAmelCase_ = '''lower newer''' # Testing tokenization UpperCAmelCase_ = tokenizer.tokenize(_lowercase , add_prefix_space=_lowercase ) UpperCAmelCase_ = rust_tokenizer.tokenize(_lowercase ) self.assertListEqual(_lowercase , _lowercase ) # Testing conversion to ids without special tokens UpperCAmelCase_ = tokenizer.encode(_lowercase , add_special_tokens=_lowercase , add_prefix_space=_lowercase ) UpperCAmelCase_ = rust_tokenizer.encode(_lowercase , add_special_tokens=_lowercase ) self.assertListEqual(_lowercase , _lowercase ) # Testing conversion to ids with special tokens UpperCAmelCase_ = self.get_rust_tokenizer(add_prefix_space=_lowercase ) UpperCAmelCase_ = tokenizer.encode(_lowercase , add_prefix_space=_lowercase ) UpperCAmelCase_ = rust_tokenizer.encode(_lowercase ) self.assertListEqual(_lowercase , _lowercase ) # Testing the unknown token UpperCAmelCase_ = tokens + [rust_tokenizer.unk_token] UpperCAmelCase_ = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(_lowercase ) , _lowercase ) def lowerCamelCase_ ( self : List[str] , *__snake_case : Union[str, Any] , **__snake_case : str ): # It's very difficult to mix/test pretokenization with byte-level # And get both GPT2 and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def lowerCamelCase_ ( self : str , __snake_case : Optional[int]=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): UpperCAmelCase_ = self.rust_tokenizer_class.from_pretrained(_lowercase , **_lowercase ) # Simple input UpperCAmelCase_ = '''This is a simple input''' UpperCAmelCase_ = ['''This is a simple input 1''', '''This is a simple input 2'''] UpperCAmelCase_ = ('''This is a simple input''', '''This is a pair''') UpperCAmelCase_ = [ ('''This is a simple input 1''', '''This is a simple input 2'''), ('''This is a simple pair 1''', '''This is a simple pair 2'''), ] # Simple input tests self.assertRaises(_lowercase , tokenizer_r.encode , _lowercase , max_length=_lowercase , padding='''max_length''' ) # Simple input self.assertRaises(_lowercase , tokenizer_r.encode_plus , _lowercase , max_length=_lowercase , padding='''max_length''' ) # Simple input self.assertRaises( _lowercase , tokenizer_r.batch_encode_plus , _lowercase , max_length=_lowercase , padding='''max_length''' , ) # Pair input self.assertRaises(_lowercase , tokenizer_r.encode , _lowercase , max_length=_lowercase , padding='''max_length''' ) # Pair input self.assertRaises(_lowercase , tokenizer_r.encode_plus , _lowercase , max_length=_lowercase , padding='''max_length''' ) # Pair input self.assertRaises( _lowercase , tokenizer_r.batch_encode_plus , _lowercase , max_length=_lowercase , padding='''max_length''' , ) def lowerCamelCase_ ( self : Optional[Any] ): UpperCAmelCase_ = GPTaTokenizer.from_pretrained(self.tmpdirname , pad_token='''<pad>''' ) # Simple input UpperCAmelCase_ = '''This is a simple input''' UpperCAmelCase_ = ['''This is a simple input looooooooong''', '''This is a simple input'''] UpperCAmelCase_ = ('''This is a simple input''', '''This is a pair''') UpperCAmelCase_ = [ ('''This is a simple input loooooong''', '''This is a simple input'''), ('''This is a simple pair loooooong''', '''This is a simple pair'''), ] UpperCAmelCase_ = tokenizer.pad_token_id UpperCAmelCase_ = tokenizer(_lowercase , padding='''max_length''' , max_length=30 , return_tensors='''np''' ) UpperCAmelCase_ = tokenizer(_lowercase , padding=_lowercase , truncate=_lowercase , return_tensors='''np''' ) UpperCAmelCase_ = tokenizer(*_lowercase , padding='''max_length''' , max_length=60 , return_tensors='''np''' ) UpperCAmelCase_ = tokenizer(_lowercase , padding=_lowercase , truncate=_lowercase , return_tensors='''np''' ) # s # test single string max_length padding self.assertEqual(out_s['''input_ids'''].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s['''input_ids'''] ) self.assertTrue(0 in out_s['''attention_mask'''] ) # s2 # test automatic padding self.assertEqual(out_sa['''input_ids'''].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa['''input_ids'''][0] ) self.assertFalse(0 in out_sa['''attention_mask'''][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa['''input_ids'''][1] ) self.assertTrue(0 in out_sa['''attention_mask'''][1] ) # p # test single pair max_length padding self.assertEqual(out_p['''input_ids'''].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p['''input_ids'''] ) self.assertTrue(0 in out_p['''attention_mask'''] ) # p2 # test automatic padding pair self.assertEqual(out_pa['''input_ids'''].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa['''input_ids'''][0] ) self.assertFalse(0 in out_pa['''attention_mask'''][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa['''input_ids'''][1] ) self.assertTrue(0 in out_pa['''attention_mask'''][1] ) def lowerCamelCase_ ( self : int ): UpperCAmelCase_ = '''$$$''' UpperCAmelCase_ = GPTaTokenizer.from_pretrained(self.tmpdirname , bos_token=_lowercase , add_bos_token=_lowercase ) UpperCAmelCase_ = '''This is a simple input''' UpperCAmelCase_ = ['''This is a simple input 1''', '''This is a simple input 2'''] UpperCAmelCase_ = tokenizer.bos_token_id UpperCAmelCase_ = tokenizer(_lowercase ) UpperCAmelCase_ = tokenizer(_lowercase ) self.assertEqual(out_s.input_ids[0] , _lowercase ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) UpperCAmelCase_ = tokenizer.decode(out_s.input_ids ) UpperCAmelCase_ = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , _lowercase ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) def lowerCamelCase_ ( self : Optional[Any] ): pass def lowerCamelCase_ ( self : int ): # TODO: change to self.get_tokenizers() when the fast version is implemented UpperCAmelCase_ = [self.get_tokenizer(do_lower_case=_lowercase , add_bos_token=_lowercase )] for tokenizer in tokenizers: with self.subTest(F'{tokenizer.__class__.__name__}' ): UpperCAmelCase_ = '''Encode this.''' UpperCAmelCase_ = '''This one too please.''' UpperCAmelCase_ = tokenizer.encode(_lowercase , add_special_tokens=_lowercase ) encoded_sequence += tokenizer.encode(_lowercase , add_special_tokens=_lowercase ) UpperCAmelCase_ = tokenizer.encode_plus( _lowercase , _lowercase , add_special_tokens=_lowercase , return_special_tokens_mask=_lowercase , ) UpperCAmelCase_ = encoded_sequence_dict['''input_ids'''] UpperCAmelCase_ = encoded_sequence_dict['''special_tokens_mask'''] self.assertEqual(len(_lowercase ) , len(_lowercase ) ) UpperCAmelCase_ = [ (x if not special_tokens_mask[i] else None) for i, x in enumerate(_lowercase ) ] UpperCAmelCase_ = [x for x in filtered_sequence if x is not None] self.assertEqual(_lowercase , _lowercase ) @require_tokenizers class a ( unittest.TestCase ): '''simple docstring''' def lowerCamelCase_ ( self : List[str] ): # More context: # https://huggingface.co/wjmcat/opt-350m-paddle/discussions/1 # https://huggingface.slack.com/archives/C01N44FJDHT/p1653511495183519 # https://github.com/huggingface/transformers/pull/17088#discussion_r871246439 UpperCAmelCase_ = AutoTokenizer.from_pretrained('''facebook/opt-350m''' , from_slow=_lowercase ) UpperCAmelCase_ = '''A photo of a cat''' UpperCAmelCase_ = tokenizer.encode( _lowercase , ) self.assertEqual(_lowercase , [2, 2_50, 13_45, 9, 10, 47_58] ) tokenizer.save_pretrained('''test_opt''' ) UpperCAmelCase_ = AutoTokenizer.from_pretrained('''./test_opt''' ) UpperCAmelCase_ = tokenizer.encode( _lowercase , ) self.assertEqual(_lowercase , [2, 2_50, 13_45, 9, 10, 47_58] ) def lowerCamelCase_ ( self : List[str] ): UpperCAmelCase_ = AutoTokenizer.from_pretrained('''facebook/opt-350m''' , use_slow=_lowercase ) UpperCAmelCase_ = '''A photo of a cat''' UpperCAmelCase_ = tokenizer.encode( _lowercase , ) # Same as above self.assertEqual(_lowercase , [2, 2_50, 13_45, 9, 10, 47_58] ) @unittest.skip('''This test is failing because of a bug in the fast tokenizer''' ) def lowerCamelCase_ ( self : List[Any] ): UpperCAmelCase_ = AutoTokenizer.from_pretrained('''facebook/opt-350m''' , from_slow=_lowercase ) UpperCAmelCase_ = '''bos''' UpperCAmelCase_ = tokenizer.get_vocab()['''bos'''] UpperCAmelCase_ = '''A photo of a cat''' UpperCAmelCase_ = tokenizer.encode( _lowercase , ) # We changed the bos token self.assertEqual(_lowercase , [3_19_57, 2_50, 13_45, 9, 10, 47_58] ) tokenizer.save_pretrained('''./tok''' ) UpperCAmelCase_ = AutoTokenizer.from_pretrained('''./tok''' ) self.assertTrue(tokenizer.is_fast ) UpperCAmelCase_ = tokenizer.encode( _lowercase , ) self.assertEqual(_lowercase , [3_19_57, 2_50, 13_45, 9, 10, 47_58] )
144
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available a_ :List[str] = { 'configuration_groupvit': [ 'GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GroupViTConfig', 'GroupViTOnnxConfig', 'GroupViTTextConfig', 'GroupViTVisionConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ :str = [ 'GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GroupViTModel', 'GroupViTPreTrainedModel', 'GroupViTTextModel', 'GroupViTVisionModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ :List[Any] = [ 'TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFGroupViTModel', 'TFGroupViTPreTrainedModel', 'TFGroupViTTextModel', 'TFGroupViTVisionModel', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys a_ :Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
35
0
import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , ) -> Dict: """simple docstring""" if attention_mask is None: _A = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: _A = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: _A = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=A__ ) if decoder_head_mask is None: _A = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=A__ ) if cross_attn_head_mask is None: _A = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=A__ ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class lowerCamelCase: '''simple docstring''' def __init__( self , snake_case_ , snake_case_=13 , snake_case_=7 , snake_case_=True , snake_case_=False , snake_case_=99 , snake_case_=16 , snake_case_=2 , snake_case_=4 , snake_case_=4 , snake_case_="relu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=0.0 , snake_case_=0.0 , snake_case_=20 , snake_case_=2 , snake_case_=1 , snake_case_=0 , ): _A = parent _A = batch_size _A = seq_length _A = is_training _A = use_labels _A = vocab_size _A = hidden_size _A = num_hidden_layers _A = num_attention_heads _A = intermediate_size _A = hidden_act _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = encoder_layerdrop _A = decoder_layerdrop _A = max_position_embeddings _A = eos_token_id _A = pad_token_id _A = bos_token_id def lowerCAmelCase__ ( self ): _A = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _A = self.eos_token_id # Eos Token _A = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input _A = input_ids.clamp(self.pad_token_id + 1 ) _A = decoder_input_ids.clamp(self.pad_token_id + 1 ) _A = self.get_config() _A = prepare_mam_aaa_inputs_dict(_lowercase , _lowercase , _lowercase ) return config, inputs_dict def lowerCAmelCase__ ( self ): return MaMaaaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , encoder_layerdrop=self.encoder_layerdrop , decoder_layerdrop=self.decoder_layerdrop , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , ) def lowerCAmelCase__ ( self ): _A = self.prepare_config_and_inputs() return config, inputs_dict def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): _A = MaMaaaModel(config=_lowercase ).get_decoder().to(_lowercase ).eval() _A = inputs_dict['''input_ids'''] _A = inputs_dict['''attention_mask'''] _A = inputs_dict['''head_mask'''] # first forward pass _A = model(_lowercase , attention_mask=_lowercase , head_mask=_lowercase , use_cache=_lowercase ) _A = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids _A = ids_tensor((self.batch_size, 3) , config.vocab_size ) _A = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and _A = torch.cat([input_ids, next_tokens] , dim=-1 ) _A = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) _A = model(_lowercase , attention_mask=_lowercase )['''last_hidden_state'''] _A = model(_lowercase , attention_mask=_lowercase , past_key_values=_lowercase )[ '''last_hidden_state''' ] # select random slice _A = ids_tensor((1,) , output_from_past.shape[-1] ).item() _A = output_from_no_past[:, -3:, random_slice_idx].detach() _A = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_lowercase , _lowercase , atol=1E-2 ) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): _A = MaMaaaModel(config=_lowercase ).to(_lowercase ).eval() _A = model(**_lowercase ) _A = outputs.encoder_last_hidden_state _A = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _A = model.get_encoder() encoder.save_pretrained(_lowercase ) _A = MaMaaaEncoder.from_pretrained(_lowercase ).to(_lowercase ) _A = encoder(inputs_dict['input_ids'] , attention_mask=inputs_dict['attention_mask'] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) with tempfile.TemporaryDirectory() as tmpdirname: _A = model.get_decoder() decoder.save_pretrained(_lowercase ) _A = MaMaaaDecoder.from_pretrained(_lowercase ).to(_lowercase ) _A = decoder( input_ids=inputs_dict['decoder_input_ids'] , attention_mask=inputs_dict['decoder_attention_mask'] , encoder_hidden_states=_lowercase , encoder_attention_mask=inputs_dict['attention_mask'] , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class lowerCamelCase( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' __magic_name__ = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) __magic_name__ = (MaMaaaForConditionalGeneration,) if is_torch_available() else () __magic_name__ = ( { '''conversational''': MaMaaaForConditionalGeneration, '''feature-extraction''': MaMaaaModel, '''summarization''': MaMaaaForConditionalGeneration, '''text2text-generation''': MaMaaaForConditionalGeneration, '''translation''': MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) __magic_name__ = True __magic_name__ = True __magic_name__ = False __magic_name__ = False def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def lowerCAmelCase__ ( self ): _A = MaMaaaModelTester(self ) _A = ConfigTester(self , config_class=_lowercase ) def lowerCAmelCase__ ( self ): self.config_tester.run_common_tests() def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _A = model_class(_lowercase ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_lowercase ) _A = model_class.from_pretrained(_lowercase , output_loading_info=_lowercase ) self.assertEqual(info['missing_keys'] , [] ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*_lowercase ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*_lowercase ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): _A = model_class(_lowercase ) model.to(_lowercase ) model.eval() _A = copy.deepcopy(self._prepare_for_class(_lowercase , _lowercase ) ) if not self.is_encoder_decoder: _A = inputs['''input_ids'''] del inputs["input_ids"] else: _A = inputs['''input_ids'''] _A = inputs.get('decoder_input_ids' , _lowercase ) del inputs["input_ids"] inputs.pop('decoder_input_ids' , _lowercase ) _A = model.get_input_embeddings() if not self.is_encoder_decoder: _A = wte(_lowercase ) else: _A = wte(_lowercase ) _A = wte(_lowercase ) with torch.no_grad(): model(**_lowercase )[0] def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() _A = input_dict['''input_ids'''] _A = input_ids.ne(1 ).to(_lowercase ) _A = MaMaaaForConditionalGeneration(_lowercase ).eval().to(_lowercase ) if torch_device == "cuda": model.half() model.generate(_lowercase , attention_mask=_lowercase ) model.generate(num_beams=4 , do_sample=_lowercase , early_stopping=_lowercase , num_return_sequences=3 ) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" return torch.tensor(A__ , dtype=torch.long , device=A__ ) __A : int = 1E-4 @require_torch @require_sentencepiece @require_tokenizers @slow class lowerCamelCase( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase__ ( self ): return MaMaaaTokenizer.from_pretrained('facebook/m2m100_418M' ) def lowerCAmelCase__ ( self ): _A = MaMaaaModel.from_pretrained('facebook/m2m100_418M' ).to(_lowercase ) _A = _long_tensor([[12_8028, 98, 12, 3_0527, 2732, 159, 7755, 6_1904, 3_9144, 38, 2]] ) _A = _long_tensor([[2, 12_8028, 98, 12, 3_0527, 2732, 159, 7755, 6_1904, 3_9144, 38]] ) _A = prepare_mam_aaa_inputs_dict(model.config , _lowercase , _lowercase ) with torch.no_grad(): _A = model(**_lowercase )[0] _A = torch.Size((1, 11, 1024) ) self.assertEqual(output.shape , _lowercase ) # change to expected output here _A = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]] , device=_lowercase ) self.assertTrue(torch.allclose(output[:, :3, :3] , _lowercase , atol=_lowercase ) ) def lowerCAmelCase__ ( self ): _A = MaMaaaForConditionalGeneration.from_pretrained('facebook/m2m100_418M' ).to(_lowercase ) # change to intended input _A = _long_tensor([[12_8028, 98, 12, 3_0527, 2732, 159, 7755, 6_1904, 3_9144, 38, 2]] ) _A = _long_tensor([[2, 12_8028, 98, 12, 3_0527, 2732, 159, 7755, 6_1904, 3_9144, 38]] ) _A = prepare_mam_aaa_inputs_dict(model.config , _lowercase , _lowercase ) with torch.no_grad(): _A = model(**_lowercase )[0] _A = torch.Size((1, 11, model.config.vocab_size) ) self.assertEqual(output.shape , _lowercase ) # change to expected output here _A = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]] , device=_lowercase ) self.assertTrue(torch.allclose(output[:, :3, :3] , _lowercase , atol=_lowercase ) ) def lowerCAmelCase__ ( self ): _A = MaMaaaForConditionalGeneration.from_pretrained('facebook/m2m100_418M' ).to(_lowercase ) _A = MaMaaaTokenizer.from_pretrained('facebook/m2m100_418M' , src_lang='fr' , tgt_lang='en' ) _A = [ '''L\'affaire NSA souligne l\'absence totale de débat sur le renseignement''', '''Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.''', '''Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent''' ''' Fabius convoque l\'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de''' ''' l\'ampleur de la surveillance américaine sur l\'ensemble des communications en France.''', ] # The below article tests that we don't add any hypotheses outside of the top n_beams _A = tokenizer(_lowercase , padding=_lowercase , return_tensors='pt' ) _A = model.generate( input_ids=dct['input_ids'].to(_lowercase ) , attention_mask=dct['attention_mask'].to(_lowercase ) , num_beams=5 , forced_bos_token_id=tokenizer.get_lang_id('en' ) , ) _A = [ '''The NSA case highlights the total absence of intelligence debate''', '''I think there are two levels of response from the French government.''', '''When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.''' ''' Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all''' ''' communications in France.''', ] _A = tokenizer.batch_decode( hypotheses_batch.tolist() , clean_up_tokenization_spaces=_lowercase , skip_special_tokens=_lowercase ) assert generated == expected_en
27
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase ( _UpperCAmelCase ): def lowercase__ ( self : Optional[int] ): return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def lowercase__ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE__ : List[str] = {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']} return Dataset.from_dict(_lowercase ) def lowercase__ ( self : Dict ): SCREAMING_SNAKE_CASE__ : List[Any] = self._create_example_records() SCREAMING_SNAKE_CASE__ : int = Dataset.from_list(_lowercase ) self.assertListEqual(dset.column_names , ['''col_1''', '''col_2'''] ) for i, r in enumerate(_lowercase ): self.assertDictEqual(_lowercase , example_records[i] ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : Dict = self._create_example_records() SCREAMING_SNAKE_CASE__ : Optional[int] = Dataset.from_list(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def lowercase__ ( self : List[Any] ): # checks what happens with missing columns SCREAMING_SNAKE_CASE__ : List[str] = [{'''col_1''': 1}, {'''col_2''': '''x'''}] SCREAMING_SNAKE_CASE__ : Union[str, Any] = Dataset.from_list(_lowercase ) self.assertDictEqual(dset[0] , {'''col_1''': 1} ) self.assertDictEqual(dset[1] , {'''col_1''': None} ) # NB: first record is used for columns def lowercase__ ( self : int ): # checks if the type can be inferred from the second record SCREAMING_SNAKE_CASE__ : int = [{'''col_1''': []}, {'''col_1''': [1, 2]}] SCREAMING_SNAKE_CASE__ : int = Dataset.from_list(_lowercase ) self.assertEqual(dset.info.features['''col_1'''] , Sequence(Value('''int64''' ) ) ) def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : int = Dataset.from_list([] ) self.assertEqual(len(_lowercase ) , 0 ) self.assertListEqual(dset.column_names , [] )
35
0
from __future__ import annotations def _SCREAMING_SNAKE_CASE ( __lowercase : List[str] ) -> float: """simple docstring""" if not nums: raise ValueError("""List is empty""" ) return sum(A__ ) / len(A__ ) if __name__ == "__main__": import doctest doctest.testmod()
637
import pickle import numpy as np from matplotlib import pyplot as plt class lowercase : def __init__( self : List[str] , _lowercase : Tuple , _lowercase : List[Any] , _lowercase : Tuple , _lowercase : Any , _lowercase : Optional[int] , _lowercase : str=0.2 , _lowercase : str=0.2 ): SCREAMING_SNAKE_CASE__ : List[Any] = bp_numa SCREAMING_SNAKE_CASE__ : Union[str, Any] = bp_numa SCREAMING_SNAKE_CASE__ : Union[str, Any] = bp_numa SCREAMING_SNAKE_CASE__ : List[str] = conva_get[:2] SCREAMING_SNAKE_CASE__ : str = conva_get[2] SCREAMING_SNAKE_CASE__ : Any = size_pa SCREAMING_SNAKE_CASE__ : Union[str, Any] = rate_w SCREAMING_SNAKE_CASE__ : Tuple = rate_t SCREAMING_SNAKE_CASE__ : Union[str, Any] = [ np.mat(-1 * np.random.rand(self.conva[0] , self.conva[0] ) + 0.5 ) for i in range(self.conva[1] ) ] SCREAMING_SNAKE_CASE__ : Dict = np.mat(-1 * np.random.rand(self.num_bpa , self.num_bpa ) + 0.5 ) SCREAMING_SNAKE_CASE__ : int = np.mat(-1 * np.random.rand(self.num_bpa , self.num_bpa ) + 0.5 ) SCREAMING_SNAKE_CASE__ : str = -2 * np.random.rand(self.conva[1] ) + 1 SCREAMING_SNAKE_CASE__ : Dict = -2 * np.random.rand(self.num_bpa ) + 1 SCREAMING_SNAKE_CASE__ : str = -2 * np.random.rand(self.num_bpa ) + 1 def lowercase__ ( self : Union[str, Any] , _lowercase : Any ): # save model dict with pickle SCREAMING_SNAKE_CASE__ : Dict = { '''num_bp1''': self.num_bpa, '''num_bp2''': self.num_bpa, '''num_bp3''': self.num_bpa, '''conv1''': self.conva, '''step_conv1''': self.step_conva, '''size_pooling1''': self.size_poolinga, '''rate_weight''': self.rate_weight, '''rate_thre''': self.rate_thre, '''w_conv1''': self.w_conva, '''wkj''': self.wkj, '''vji''': self.vji, '''thre_conv1''': self.thre_conva, '''thre_bp2''': self.thre_bpa, '''thre_bp3''': self.thre_bpa, } with open(_lowercase , '''wb''' ) as f: pickle.dump(_lowercase , _lowercase ) print(f"""Model saved: {save_path}""" ) @classmethod def lowercase__ ( cls : Dict , _lowercase : int ): # read saved model with open(_lowercase , '''rb''' ) as f: SCREAMING_SNAKE_CASE__ : Optional[Any] = pickle.load(_lowercase ) # noqa: S301 SCREAMING_SNAKE_CASE__ : Tuple = model_dic.get('''conv1''' ) conv_get.append(model_dic.get('''step_conv1''' ) ) SCREAMING_SNAKE_CASE__ : Tuple = model_dic.get('''size_pooling1''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model_dic.get('''num_bp1''' ) SCREAMING_SNAKE_CASE__ : Dict = model_dic.get('''num_bp2''' ) SCREAMING_SNAKE_CASE__ : Dict = model_dic.get('''num_bp3''' ) SCREAMING_SNAKE_CASE__ : Optional[int] = model_dic.get('''rate_weight''' ) SCREAMING_SNAKE_CASE__ : str = model_dic.get('''rate_thre''' ) # create model instance SCREAMING_SNAKE_CASE__ : Dict = CNN(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) # modify model parameter SCREAMING_SNAKE_CASE__ : List[str] = model_dic.get('''w_conv1''' ) SCREAMING_SNAKE_CASE__ : Optional[int] = model_dic.get('''wkj''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model_dic.get('''vji''' ) SCREAMING_SNAKE_CASE__ : str = model_dic.get('''thre_conv1''' ) SCREAMING_SNAKE_CASE__ : Any = model_dic.get('''thre_bp2''' ) SCREAMING_SNAKE_CASE__ : List[Any] = model_dic.get('''thre_bp3''' ) return conv_ins def lowercase__ ( self : str , _lowercase : Optional[int] ): return 1 / (1 + np.exp(-1 * x )) def lowercase__ ( self : Union[str, Any] , _lowercase : List[str] ): return round(_lowercase , 3 ) def lowercase__ ( self : List[str] , _lowercase : Union[str, Any] , _lowercase : int , _lowercase : Union[str, Any] , _lowercase : Union[str, Any] , _lowercase : Union[str, Any] ): # convolution process SCREAMING_SNAKE_CASE__ : Tuple = convs[0] SCREAMING_SNAKE_CASE__ : Optional[Any] = convs[1] SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.shape(_lowercase )[0] # get the data slice of original image data, data_focus SCREAMING_SNAKE_CASE__ : List[str] = [] for i_focus in range(0 , size_data - size_conv + 1 , _lowercase ): for j_focus in range(0 , size_data - size_conv + 1 , _lowercase ): SCREAMING_SNAKE_CASE__ : Optional[Any] = data[ i_focus : i_focus + size_conv, j_focus : j_focus + size_conv ] data_focus.append(_lowercase ) # calculate the feature map of every single kernel, and saved as list of matrix SCREAMING_SNAKE_CASE__ : Optional[Any] = [] SCREAMING_SNAKE_CASE__ : Union[str, Any] = int((size_data - size_conv) / conv_step + 1 ) for i_map in range(_lowercase ): SCREAMING_SNAKE_CASE__ : int = [] for i_focus in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Tuple = ( np.sum(np.multiply(data_focus[i_focus] , w_convs[i_map] ) ) - thre_convs[i_map] ) featuremap.append(self.sig(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.asmatrix(_lowercase ).reshape( _lowercase , _lowercase ) data_featuremap.append(_lowercase ) # expanding the data slice to One dimenssion SCREAMING_SNAKE_CASE__ : int = [] for each_focus in data_focus: focusa_list.extend(self.Expand_Mat(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.asarray(_lowercase ) return focus_list, data_featuremap def lowercase__ ( self : List[Any] , _lowercase : Tuple , _lowercase : Union[str, Any] , _lowercase : Optional[Any]="average_pool" ): # pooling process SCREAMING_SNAKE_CASE__ : List[str] = len(featuremaps[0] ) SCREAMING_SNAKE_CASE__ : List[Any] = int(size_map / size_pooling ) SCREAMING_SNAKE_CASE__ : List[str] = [] for i_map in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Any = featuremaps[i_map] SCREAMING_SNAKE_CASE__ : int = [] for i_focus in range(0 , _lowercase , _lowercase ): for j_focus in range(0 , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE__ : Dict = feature_map[ i_focus : i_focus + size_pooling, j_focus : j_focus + size_pooling, ] if pooling_type == "average_pool": # average pooling map_pooled.append(np.average(_lowercase ) ) elif pooling_type == "max_pooling": # max pooling map_pooled.append(np.max(_lowercase ) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.asmatrix(_lowercase ).reshape(_lowercase , _lowercase ) featuremap_pooled.append(_lowercase ) return featuremap_pooled def lowercase__ ( self : Optional[Any] , _lowercase : Optional[Any] ): # expanding three dimension data to one dimension list SCREAMING_SNAKE_CASE__ : Dict = [] for i in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Optional[Any] = np.shape(data[i] ) SCREAMING_SNAKE_CASE__ : Tuple = data[i].reshape(1 , shapes[0] * shapes[1] ) SCREAMING_SNAKE_CASE__ : Dict = data_listed.getA().tolist()[0] data_expanded.extend(_lowercase ) SCREAMING_SNAKE_CASE__ : List[str] = np.asarray(_lowercase ) return data_expanded def lowercase__ ( self : Tuple , _lowercase : Optional[int] ): # expanding matrix to one dimension list SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.asarray(_lowercase ) SCREAMING_SNAKE_CASE__ : Any = np.shape(_lowercase ) SCREAMING_SNAKE_CASE__ : str = data_mat.reshape(1 , shapes[0] * shapes[1] ) return data_expanded def lowercase__ ( self : List[str] , _lowercase : List[str] , _lowercase : List[str] , _lowercase : Any , _lowercase : Optional[Any] , _lowercase : str ): SCREAMING_SNAKE_CASE__ : Optional[int] = [] SCREAMING_SNAKE_CASE__ : Dict = 0 for i_map in range(_lowercase ): SCREAMING_SNAKE_CASE__ : Any = np.ones((size_map, size_map) ) for i in range(0 , _lowercase , _lowercase ): for j in range(0 , _lowercase , _lowercase ): SCREAMING_SNAKE_CASE__ : Tuple = pd_pool[ i_pool ] SCREAMING_SNAKE_CASE__ : Dict = i_pool + 1 SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.multiply( _lowercase , np.multiply(out_map[i_map] , (1 - out_map[i_map]) ) ) pd_all.append(_lowercase ) return pd_all def lowercase__ ( self : List[Any] , _lowercase : Any , _lowercase : Tuple , _lowercase : Optional[int] , _lowercase : Any , _lowercase : Tuple , _lowercase : int=bool ): # model traning print('''----------------------Start Training-------------------------''' ) print((''' - - Shape: Train_Data ''', np.shape(_lowercase )) ) print((''' - - Shape: Teach_Data ''', np.shape(_lowercase )) ) SCREAMING_SNAKE_CASE__ : Any = 0 SCREAMING_SNAKE_CASE__ : Tuple = [] SCREAMING_SNAKE_CASE__ : Optional[int] = 1_00_00 while rp < n_repeat and mse >= error_accuracy: SCREAMING_SNAKE_CASE__ : List[Any] = 0 print(f"""-------------Learning Time {rp}--------------""" ) for p in range(len(_lowercase ) ): # print('------------Learning Image: %d--------------'%p) SCREAMING_SNAKE_CASE__ : Any = np.asmatrix(datas_train[p] ) SCREAMING_SNAKE_CASE__ : str = np.asarray(datas_teach[p] ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] = self.convolute( _lowercase , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) SCREAMING_SNAKE_CASE__ : int = self.pooling(_lowercase , self.size_poolinga ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.shape(_lowercase ) SCREAMING_SNAKE_CASE__ : Dict = self._expand(_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = data_bp_input SCREAMING_SNAKE_CASE__ : Optional[int] = np.dot(_lowercase , self.vji.T ) - self.thre_bpa SCREAMING_SNAKE_CASE__ : Any = self.sig(_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.dot(_lowercase , self.wkj.T ) - self.thre_bpa SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.sig(_lowercase ) # --------------Model Leaning ------------------------ # calculate error and gradient--------------- SCREAMING_SNAKE_CASE__ : Tuple = np.multiply( (data_teach - bp_outa) , np.multiply(_lowercase , (1 - bp_outa) ) ) SCREAMING_SNAKE_CASE__ : List[Any] = np.multiply( np.dot(_lowercase , self.wkj ) , np.multiply(_lowercase , (1 - bp_outa) ) ) SCREAMING_SNAKE_CASE__ : List[Any] = np.dot(_lowercase , self.vji ) SCREAMING_SNAKE_CASE__ : Dict = pd_i_all / (self.size_poolinga * self.size_poolinga) SCREAMING_SNAKE_CASE__ : List[str] = pd_conva_pooled.T.getA().tolist() SCREAMING_SNAKE_CASE__ : Union[str, Any] = self._calculate_gradient_from_pool( _lowercase , _lowercase , shape_featuremapa[0] , shape_featuremapa[1] , self.size_poolinga , ) # weight and threshold learning process--------- # convolution layer for k_conv in range(self.conva[1] ): SCREAMING_SNAKE_CASE__ : Optional[Any] = self._expand_mat(pd_conva_all[k_conv] ) SCREAMING_SNAKE_CASE__ : Dict = self.rate_weight * np.dot(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self.w_conva[k_conv] + delta_w.reshape( (self.conva[0], self.conva[0]) ) SCREAMING_SNAKE_CASE__ : Optional[Any] = ( self.thre_conva[k_conv] - np.sum(pd_conva_all[k_conv] ) * self.rate_thre ) # all connected layer SCREAMING_SNAKE_CASE__ : List[str] = self.wkj + pd_k_all.T * bp_outa * self.rate_weight SCREAMING_SNAKE_CASE__ : Optional[Any] = self.vji + pd_j_all.T * bp_outa * self.rate_weight SCREAMING_SNAKE_CASE__ : Optional[Any] = self.thre_bpa - pd_k_all * self.rate_thre SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.thre_bpa - pd_j_all * self.rate_thre # calculate the sum error of all single image SCREAMING_SNAKE_CASE__ : int = np.sum(abs(data_teach - bp_outa ) ) error_count += errors # print(' ----Teach ',data_teach) # print(' ----BP_output ',bp_out3) SCREAMING_SNAKE_CASE__ : Optional[Any] = rp + 1 SCREAMING_SNAKE_CASE__ : List[str] = error_count / patterns all_mse.append(_lowercase ) def draw_error(): SCREAMING_SNAKE_CASE__ : Union[str, Any] = [error_accuracy for i in range(int(n_repeat * 1.2 ) )] plt.plot(_lowercase , '''+-''' ) plt.plot(_lowercase , '''r--''' ) plt.xlabel('''Learning Times''' ) plt.ylabel('''All_mse''' ) plt.grid(_lowercase , alpha=0.5 ) plt.show() print('''------------------Training Complished---------------------''' ) print((''' - - Training epoch: ''', rp, f""" - - Mse: {mse:.6f}""") ) if draw_e: draw_error() return mse def lowercase__ ( self : Union[str, Any] , _lowercase : int ): # model predict SCREAMING_SNAKE_CASE__ : Dict = [] print('''-------------------Start Testing-------------------------''' ) print((''' - - Shape: Test_Data ''', np.shape(_lowercase )) ) for p in range(len(_lowercase ) ): SCREAMING_SNAKE_CASE__ : Optional[int] = np.asmatrix(datas_test[p] ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[Any] = self.convolute( _lowercase , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) SCREAMING_SNAKE_CASE__ : Any = self.pooling(_lowercase , self.size_poolinga ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self._expand(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = data_bp_input SCREAMING_SNAKE_CASE__ : Optional[int] = bp_outa * self.vji.T - self.thre_bpa SCREAMING_SNAKE_CASE__ : Tuple = self.sig(_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = bp_outa * self.wkj.T - self.thre_bpa SCREAMING_SNAKE_CASE__ : Optional[Any] = self.sig(_lowercase ) produce_out.extend(bp_outa.getA().tolist() ) SCREAMING_SNAKE_CASE__ : str = [list(map(self.do_round , _lowercase ) ) for each in produce_out] return np.asarray(_lowercase ) def lowercase__ ( self : Optional[int] , _lowercase : Tuple ): # return the data of image after convoluting process so we can check it out SCREAMING_SNAKE_CASE__ : str = np.asmatrix(_lowercase ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Any = self.convolute( _lowercase , self.conva , self.w_conva , self.thre_conva , conv_step=self.step_conva , ) SCREAMING_SNAKE_CASE__ : Dict = self.pooling(_lowercase , self.size_poolinga ) return data_conveda, data_pooleda if __name__ == "__main__": pass
35
0
import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder lowerCAmelCase__: Tuple = logging.get_logger(__name__) # pylint: disable=invalid-name lowerCAmelCase__: Dict = 256 class snake_case_ ( _UpperCAmelCase ): __lowerCamelCase : List[Any] = ['''melgan'''] def __init__( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): super().__init__() # From MELGAN SCREAMING_SNAKE_CASE_ : List[str] = math.log(1e-5 ) # Matches MelGAN training. SCREAMING_SNAKE_CASE_ : str = 4.0 # Largest value for most examples SCREAMING_SNAKE_CASE_ : str = 128 self.register_modules( notes_encoder=_lowercase , continuous_encoder=_lowercase , decoder=_lowercase , scheduler=_lowercase , melgan=_lowercase , ) def __A ( self , __lowerCAmelCase , __lowerCAmelCase=(-1.0, 1.0) , __lowerCAmelCase=False ): SCREAMING_SNAKE_CASE_ : Optional[int] = output_range if clip: SCREAMING_SNAKE_CASE_ : Tuple = torch.clip(_lowercase , self.min_value , self.max_value ) # Scale to [0, 1]. SCREAMING_SNAKE_CASE_ : List[str] = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def __A ( self , __lowerCAmelCase , __lowerCAmelCase=(-1.0, 1.0) , __lowerCAmelCase=False ): SCREAMING_SNAKE_CASE_ : Tuple = input_range SCREAMING_SNAKE_CASE_ : str = torch.clip(_lowercase , _lowercase , _lowercase ) if clip else outputs # Scale to [0, 1]. SCREAMING_SNAKE_CASE_ : int = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def __A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): SCREAMING_SNAKE_CASE_ : str = input_tokens > 0 SCREAMING_SNAKE_CASE_ : int = self.notes_encoder( encoder_input_tokens=_lowercase , encoder_inputs_mask=_lowercase ) SCREAMING_SNAKE_CASE_ : List[str] = self.continuous_encoder( encoder_inputs=_lowercase , encoder_inputs_mask=_lowercase ) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def __A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): SCREAMING_SNAKE_CASE_ : Dict = noise_time if not torch.is_tensor(_lowercase ): SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device ) elif torch.is_tensor(_lowercase ) and len(timesteps.shape ) == 0: SCREAMING_SNAKE_CASE_ : Any = timesteps[None].to(input_tokens.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML SCREAMING_SNAKE_CASE_ : int = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device ) SCREAMING_SNAKE_CASE_ : Any = self.decoder( encodings_and_masks=_lowercase , decoder_input_tokens=_lowercase , decoder_noise_time=_lowercase ) return logits @torch.no_grad() def __call__( self , __lowerCAmelCase , __lowerCAmelCase = None , __lowerCAmelCase = 100 , __lowerCAmelCase = True , __lowerCAmelCase = "numpy" , __lowerCAmelCase = None , __lowerCAmelCase = 1 , ): if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_lowercase , _lowercase ) or callback_steps <= 0) ): raise ValueError( F'`callback_steps` has to be a positive integer but is {callback_steps} of type' F' {type(_lowercase )}.' ) SCREAMING_SNAKE_CASE_ : Tuple = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa ) SCREAMING_SNAKE_CASE_ : Tuple = np.zeros([1, 0, self.n_dims] , np.floataa ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=_lowercase , device=self.device ) for i, encoder_input_tokens in enumerate(_lowercase ): if i == 0: SCREAMING_SNAKE_CASE_ : Dict = torch.from_numpy(pred_mel[:1].copy() ).to( device=self.device , dtype=self.decoder.dtype ) # The first chunk has no previous context. SCREAMING_SNAKE_CASE_ : Tuple = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=_lowercase , device=self.device ) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. SCREAMING_SNAKE_CASE_ : Dict = ones SCREAMING_SNAKE_CASE_ : Dict = self.scale_features( _lowercase , output_range=[-1.0, 1.0] , clip=_lowercase ) SCREAMING_SNAKE_CASE_ : List[Any] = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens] ).to(device=self.device ) , continuous_inputs=_lowercase , continuous_mask=_lowercase , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop SCREAMING_SNAKE_CASE_ : List[Any] = randn_tensor( shape=encoder_continuous_inputs.shape , generator=_lowercase , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(_lowercase ) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): SCREAMING_SNAKE_CASE_ : int = self.decode( encodings_and_masks=_lowercase , input_tokens=_lowercase , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 SCREAMING_SNAKE_CASE_ : Any = self.scheduler.step(_lowercase , _lowercase , _lowercase , generator=_lowercase ).prev_sample SCREAMING_SNAKE_CASE_ : List[Any] = self.scale_to_features(_lowercase , input_range=[-1.0, 1.0] ) SCREAMING_SNAKE_CASE_ : List[str] = mel[:1] SCREAMING_SNAKE_CASE_ : List[str] = mel.cpu().float().numpy() SCREAMING_SNAKE_CASE_ : Optional[Any] = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1 ) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_lowercase , _lowercase ) logger.info('Generated segment' , _lowercase ) if output_type == "numpy" and not is_onnx_available(): raise ValueError( 'Cannot return output in \'np\' format if ONNX is not available. Make sure to have ONNX installed or set \'output_type\' to \'mel\'.' ) elif output_type == "numpy" and self.melgan is None: raise ValueError( 'Cannot return output in \'np\' format if melgan component is not defined. Make sure to define `self.melgan` or set \'output_type\' to \'mel\'.' ) if output_type == "numpy": SCREAMING_SNAKE_CASE_ : List[Any] = self.melgan(input_features=full_pred_mel.astype(np.floataa ) ) else: SCREAMING_SNAKE_CASE_ : Dict = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=_lowercase )
345
import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class lowercase : def __init__( self : Any , _lowercase : List[Any] , _lowercase : Optional[Any]=99 , _lowercase : Optional[int]=13 , _lowercase : Tuple=16 , _lowercase : Union[str, Any]=7 , _lowercase : Optional[Any]=True , _lowercase : int=True , _lowercase : Optional[Any]=True , _lowercase : str=False , _lowercase : Union[str, Any]=True , _lowercase : Tuple=2 , _lowercase : Any=32 , _lowercase : int=4 , _lowercase : Dict=4 , _lowercase : Dict=30 , _lowercase : Union[str, Any]=0 , _lowercase : List[str]=1 , _lowercase : Optional[Any]=2 , _lowercase : Tuple=None , ): SCREAMING_SNAKE_CASE__ : Any = parent SCREAMING_SNAKE_CASE__ : List[Any] = batch_size SCREAMING_SNAKE_CASE__ : List[str] = decoder_seq_length # For common tests SCREAMING_SNAKE_CASE__ : Optional[Any] = self.decoder_seq_length SCREAMING_SNAKE_CASE__ : Optional[int] = is_training SCREAMING_SNAKE_CASE__ : Tuple = use_attention_mask SCREAMING_SNAKE_CASE__ : Any = use_labels SCREAMING_SNAKE_CASE__ : Any = vocab_size SCREAMING_SNAKE_CASE__ : Union[str, Any] = d_model SCREAMING_SNAKE_CASE__ : Tuple = d_model SCREAMING_SNAKE_CASE__ : Optional[int] = decoder_layers SCREAMING_SNAKE_CASE__ : List[str] = decoder_layers SCREAMING_SNAKE_CASE__ : Optional[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE__ : List[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE__ : Optional[int] = decoder_attention_heads SCREAMING_SNAKE_CASE__ : str = eos_token_id SCREAMING_SNAKE_CASE__ : List[Any] = bos_token_id SCREAMING_SNAKE_CASE__ : str = pad_token_id SCREAMING_SNAKE_CASE__ : str = decoder_start_token_id SCREAMING_SNAKE_CASE__ : Optional[Any] = use_cache SCREAMING_SNAKE_CASE__ : Optional[int] = max_position_embeddings SCREAMING_SNAKE_CASE__ : Tuple = None SCREAMING_SNAKE_CASE__ : int = decoder_seq_length SCREAMING_SNAKE_CASE__ : Optional[int] = 2 SCREAMING_SNAKE_CASE__ : Tuple = 1 def lowercase__ ( self : Dict ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ : Optional[Any] = None if self.use_attention_mask: SCREAMING_SNAKE_CASE__ : List[str] = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = None if self.use_labels: SCREAMING_SNAKE_CASE__ : List[Any] = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ : Optional[int] = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def lowercase__ ( self : Dict , _lowercase : Any , _lowercase : Dict , _lowercase : Optional[Any] , _lowercase : Optional[Any] , ): SCREAMING_SNAKE_CASE__ : Dict = True SCREAMING_SNAKE_CASE__ : Optional[int] = TrOCRDecoder(config=_lowercase ).to(_lowercase ).eval() SCREAMING_SNAKE_CASE__ : Optional[int] = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass SCREAMING_SNAKE_CASE__ : Optional[Any] = model(_lowercase , use_cache=_lowercase ) SCREAMING_SNAKE_CASE__ : List[str] = model(_lowercase ) SCREAMING_SNAKE_CASE__ : Tuple = model(_lowercase , use_cache=_lowercase ) self.parent.assertTrue(len(_lowercase ) == len(_lowercase ) ) self.parent.assertTrue(len(_lowercase ) == len(_lowercase ) + 1 ) SCREAMING_SNAKE_CASE__ : int = outputs['''past_key_values'''] # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE__ : List[str] = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and SCREAMING_SNAKE_CASE__ : Tuple = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE__ : int = model(_lowercase )['''last_hidden_state'''] SCREAMING_SNAKE_CASE__ : List[Any] = model(_lowercase , past_key_values=_lowercase )['''last_hidden_state'''] # select random slice SCREAMING_SNAKE_CASE__ : int = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE__ : Dict = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() SCREAMING_SNAKE_CASE__ : str = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(_lowercase , _lowercase , atol=1E-3 ) def lowercase__ ( self : Optional[int] ): SCREAMING_SNAKE_CASE__ : Optional[int] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE__ : int = {'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_torch class lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): lowerCamelCase : List[str] = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () lowerCamelCase : Dict = (TrOCRForCausalLM,) if is_torch_available() else () lowerCamelCase : Tuple = {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {} lowerCamelCase : Any = True lowerCamelCase : int = False def lowercase__ ( self : List[Any] ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = TrOCRStandaloneDecoderModelTester(self , is_training=_lowercase ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = ConfigTester(self , config_class=_lowercase ) def lowercase__ ( self : Optional[Any] ): pass def lowercase__ ( self : List[Any] ): pass def lowercase__ ( self : str ): pass def lowercase__ ( self : Dict ): self.config_tester.run_common_tests() def lowercase__ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*_lowercase ) def lowercase__ ( self : Optional[Any] ): return @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def lowercase__ ( self : Tuple ): pass
35
0
import enum import os from hashlib import shaaaa from typing import Optional from .. import config from .logging import get_logger lowerCamelCase__ = get_logger(__name__) class UpperCamelCase ( enum.Enum ): __UpperCamelCase = '''all_checks''' __UpperCamelCase = '''basic_checks''' __UpperCamelCase = '''no_checks''' class UpperCamelCase ( _UpperCAmelCase ): pass class UpperCamelCase ( _UpperCAmelCase ): pass class UpperCamelCase ( _UpperCAmelCase ): pass class UpperCamelCase ( _UpperCAmelCase ): pass def _lowerCamelCase( __snake_case , __snake_case , __snake_case=None ) -> Optional[int]: if expected_checksums is None: logger.info("Unable to verify checksums." ) return if len(set(A__ ) - set(A__ ) ) > 0: raise ExpectedMoreDownloadedFiles(str(set(A__ ) - set(A__ ) ) ) if len(set(A__ ) - set(A__ ) ) > 0: raise UnexpectedDownloadedFile(str(set(A__ ) - set(A__ ) ) ) __snake_case = [url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]] __snake_case = ''' for ''' + verification_name if verification_name is not None else '''''' if len(A__ ) > 0: raise NonMatchingChecksumError( f"""Checksums didn't match{for_verification_name}:\n""" f"""{bad_urls}\n""" "Set `verification_mode=\'no_checks\'` to skip checksums verification and ignore this error" ) logger.info("All the checksums matched successfully" + for_verification_name ) class UpperCamelCase ( _UpperCAmelCase ): pass class UpperCamelCase ( _UpperCAmelCase ): pass class UpperCamelCase ( _UpperCAmelCase ): pass class UpperCamelCase ( _UpperCAmelCase ): pass def _lowerCamelCase( __snake_case , __snake_case ) -> Dict: if expected_splits is None: logger.info("Unable to verify splits sizes." ) return if len(set(A__ ) - set(A__ ) ) > 0: raise ExpectedMoreSplits(str(set(A__ ) - set(A__ ) ) ) if len(set(A__ ) - set(A__ ) ) > 0: raise UnexpectedSplits(str(set(A__ ) - set(A__ ) ) ) __snake_case = [ {'''expected''': expected_splits[name], '''recorded''': recorded_splits[name]} for name in expected_splits if expected_splits[name].num_examples != recorded_splits[name].num_examples ] if len(A__ ) > 0: raise NonMatchingSplitsSizesError(str(A__ ) ) logger.info("All the splits matched successfully." ) def _lowerCamelCase( __snake_case , __snake_case = True ) -> dict: if record_checksum: __snake_case = shaaaa() with open(A__ , "rb" ) as f: for chunk in iter(lambda: f.read(1 << 20 ) , b"" ): m.update(A__ ) __snake_case = m.hexdigest() else: __snake_case = None return {"num_bytes": os.path.getsize(A__ ), "checksum": checksum} def _lowerCamelCase( __snake_case ) -> List[Any]: if dataset_size and config.IN_MEMORY_MAX_SIZE: return dataset_size < config.IN_MEMORY_MAX_SIZE else: return False
524
import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( _UpperCAmelCase , unittest.TestCase ): lowerCamelCase : Tuple = LayoutLMTokenizer lowerCamelCase : Any = LayoutLMTokenizerFast lowerCamelCase : Tuple = True lowerCamelCase : List[Any] = True def lowercase__ ( self : Optional[int] ): super().setUp() SCREAMING_SNAKE_CASE__ : Optional[int] = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE__ : str = 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 : Optional[int] , **_lowercase : str ): return LayoutLMTokenizer.from_pretrained(self.tmpdirname , **_lowercase ) def lowercase__ ( self : Optional[Any] , _lowercase : Any ): SCREAMING_SNAKE_CASE__ : str = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE__ : Any = '''unwanted, running''' return input_text, output_text def lowercase__ ( self : str ): SCREAMING_SNAKE_CASE__ : List[str] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE__ : Any = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(_lowercase , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowercase ) , [7, 4, 5, 10, 8, 9] ) def lowercase__ ( self : str ): pass
35
0
"""simple docstring""" import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __lowerCAmelCase : str = logging.get_logger(__name__) def __lowerCAmelCase ( __UpperCamelCase : Union[str, Any] , __UpperCamelCase : str , __UpperCamelCase : List[Any] , __UpperCamelCase : Tuple ): '''simple docstring''' def constraint_to_multiple_of(__UpperCamelCase : Tuple , __UpperCamelCase : int , __UpperCamelCase : Optional[int]=0 , __UpperCamelCase : List[Any]=None ): snake_case_ : Optional[int] = round(val / multiple ) * multiple if max_val is not None and x > max_val: snake_case_ : Any = math.floor(val / multiple ) * multiple if x < min_val: snake_case_ : Any = math.ceil(val / multiple ) * multiple return x snake_case_ : Union[str, Any] = (output_size, output_size) if isinstance(A__ , A__ ) else output_size snake_case_ : Tuple = get_image_size(A__ ) snake_case_ : List[str] = output_size # determine new height and width snake_case_ : List[str] = output_height / input_height snake_case_ : Dict = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width snake_case_ : List[str] = scale_width else: # fit height snake_case_ : Optional[Any] = scale_height snake_case_ : int = constraint_to_multiple_of(scale_height * input_height , multiple=A__ ) snake_case_ : int = constraint_to_multiple_of(scale_width * input_width , multiple=A__ ) return (new_height, new_width) class _lowerCAmelCase ( _UpperCAmelCase ): """simple docstring""" _lowerCamelCase = ['''pixel_values'''] def __init__( self , _lowercase = True , _lowercase = None , _lowercase = PILImageResampling.BILINEAR , _lowercase = False , _lowercase = 1 , _lowercase = True , _lowercase = 1 / 2_5_5 , _lowercase = True , _lowercase = None , _lowercase = None , **_lowercase , ) -> Any: '''simple docstring''' super().__init__(**_lowercase ) snake_case_ : Dict = size if size is not None else {'''height''': 3_8_4, '''width''': 3_8_4} snake_case_ : Optional[int] = get_size_dict(_lowercase ) snake_case_ : List[str] = do_resize snake_case_ : Optional[int] = size snake_case_ : int = keep_aspect_ratio snake_case_ : Optional[Any] = ensure_multiple_of snake_case_ : List[str] = resample snake_case_ : Union[str, Any] = do_rescale snake_case_ : Optional[int] = rescale_factor snake_case_ : List[Any] = do_normalize snake_case_ : Tuple = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN snake_case_ : Any = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCAmelCase__ ( self , _lowercase , _lowercase , _lowercase = False , _lowercase = 1 , _lowercase = PILImageResampling.BICUBIC , _lowercase = None , **_lowercase , ) -> Optional[int]: '''simple docstring''' snake_case_ : List[Any] = get_size_dict(_lowercase ) if "height" not in size or "width" not in size: raise ValueError(f'The size dictionary must contain the keys \'height\' and \'width\'. Got {size.keys()}' ) snake_case_ : Union[str, Any] = get_resize_output_image_size( _lowercase , output_size=(size["""height"""], size["""width"""]) , keep_aspect_ratio=_lowercase , multiple=_lowercase , ) return resize(_lowercase , size=_lowercase , resample=_lowercase , data_format=_lowercase , **_lowercase ) def UpperCAmelCase__ ( self , _lowercase , _lowercase , _lowercase = None , **_lowercase , ) -> List[str]: '''simple docstring''' return rescale(_lowercase , scale=_lowercase , data_format=_lowercase , **_lowercase ) def UpperCAmelCase__ ( self , _lowercase , _lowercase , _lowercase , _lowercase = None , **_lowercase , ) -> Any: '''simple docstring''' return normalize(_lowercase , mean=_lowercase , std=_lowercase , data_format=_lowercase , **_lowercase ) def UpperCAmelCase__ ( self , _lowercase , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = None , _lowercase = ChannelDimension.FIRST , **_lowercase , ) -> Tuple: '''simple docstring''' snake_case_ : Union[str, Any] = do_resize if do_resize is not None else self.do_resize snake_case_ : List[Any] = size if size is not None else self.size snake_case_ : List[str] = get_size_dict(_lowercase ) snake_case_ : List[Any] = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio snake_case_ : List[str] = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of snake_case_ : Tuple = resample if resample is not None else self.resample snake_case_ : str = do_rescale if do_rescale is not None else self.do_rescale snake_case_ : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case_ : Optional[int] = do_normalize if do_normalize is not None else self.do_normalize snake_case_ : Tuple = image_mean if image_mean is not None else self.image_mean snake_case_ : str = image_std if image_std is not None else self.image_std snake_case_ : Optional[Any] = make_list_of_images(_lowercase ) if not valid_images(_lowercase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. snake_case_ : str = [to_numpy_array(_lowercase ) for image in images] if do_resize: snake_case_ : Any = [self.resize(image=_lowercase , size=_lowercase , resample=_lowercase ) for image in images] if do_rescale: snake_case_ : Tuple = [self.rescale(image=_lowercase , scale=_lowercase ) for image in images] if do_normalize: snake_case_ : Any = [self.normalize(image=_lowercase , mean=_lowercase , std=_lowercase ) for image in images] snake_case_ : Union[str, Any] = [to_channel_dimension_format(_lowercase , _lowercase ) for image in images] snake_case_ : str = {'''pixel_values''': images} return BatchFeature(data=_lowercase , tensor_type=_lowercase ) def UpperCAmelCase__ ( self , _lowercase , _lowercase = None ) -> Tuple: '''simple docstring''' snake_case_ : str = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_lowercase ) != len(_lowercase ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_lowercase ): snake_case_ : Union[str, Any] = target_sizes.numpy() snake_case_ : Tuple = [] for idx in range(len(_lowercase ) ): snake_case_ : Dict = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_lowercase ) snake_case_ : Any = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_lowercase ) else: snake_case_ : Any = logits.argmax(dim=1 ) snake_case_ : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
58
from __future__ import annotations def a ( A__ , A__ , A__ ) -> dict[str, float]: '''simple docstring''' if (voltage, current, resistance).count(0 ) != 1: raise ValueError('''One and only one argument must be 0''' ) if resistance < 0: raise ValueError('''Resistance cannot be negative''' ) if voltage == 0: return {"voltage": float(current * resistance )} elif current == 0: return {"current": voltage / resistance} elif resistance == 0: return {"resistance": voltage / current} else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
35
0
import importlib.metadata import warnings from copy import deepcopy from packaging import version from ..utils import logging from .import_utils import is_accelerate_available, is_bitsandbytes_available if is_bitsandbytes_available(): import bitsandbytes as bnb import torch import torch.nn as nn from ..pytorch_utils import ConvaD if is_accelerate_available(): from accelerate import init_empty_weights from accelerate.utils import find_tied_parameters _snake_case : Dict = logging.get_logger(__name__) def _A ( __snake_case :Optional[Any] , __snake_case :str , __snake_case :str , __snake_case :Dict=None , __snake_case :List[str]=None ) -> Dict: """simple docstring""" if "." in tensor_name: __SCREAMING_SNAKE_CASE = tensor_name.split("." ) for split in splits[:-1]: __SCREAMING_SNAKE_CASE = getattr(A__ , A__ ) if new_module is None: raise ValueError(f'''{module} has no attribute {split}.''' ) __SCREAMING_SNAKE_CASE = new_module __SCREAMING_SNAKE_CASE = splits[-1] if tensor_name not in module._parameters and tensor_name not in module._buffers: raise ValueError(f'''{module} does not have a parameter or a buffer named {tensor_name}.''' ) __SCREAMING_SNAKE_CASE = tensor_name in module._buffers __SCREAMING_SNAKE_CASE = getattr(A__ , A__ ) if old_value.device == torch.device("meta" ) and device not in ["meta", torch.device("meta" )] and value is None: raise ValueError(f'''{tensor_name} is on the meta device, we need a `value` to put in on {device}.''' ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False if is_buffer or not is_bitsandbytes_available(): __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False else: __SCREAMING_SNAKE_CASE = hasattr(bnb.nn , "Params4bit" ) and isinstance(module._parameters[tensor_name] , bnb.nn.Paramsabit ) __SCREAMING_SNAKE_CASE = isinstance(module._parameters[tensor_name] , bnb.nn.IntaParams ) if is_abit or is_abit: __SCREAMING_SNAKE_CASE = module._parameters[tensor_name] if param.device.type != "cuda": if value is None: __SCREAMING_SNAKE_CASE = old_value.to(A__ ) elif isinstance(A__ , torch.Tensor ): __SCREAMING_SNAKE_CASE = value.to("cpu" ) if value.dtype == torch.inta: __SCREAMING_SNAKE_CASE = version.parse(importlib.metadata.version("bitsandbytes" ) ) > version.parse( "0.37.2" ) if not is_abit_serializable: raise ValueError( "Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. " "Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`." ) else: __SCREAMING_SNAKE_CASE = torch.tensor(A__ , device="cpu" ) # Support models using `Conv1D` in place of `nn.Linear` (e.g. gpt2) by transposing the weight matrix prior to quantization. # Since weights are saved in the correct "orientation", we skip transposing when loading. if issubclass(module.source_cls , A__ ) and fpaa_statistics is None: __SCREAMING_SNAKE_CASE = new_value.T __SCREAMING_SNAKE_CASE = old_value.__dict__ if is_abit: __SCREAMING_SNAKE_CASE = bnb.nn.IntaParams(A__ , requires_grad=A__ , **A__ ).to(A__ ) elif is_abit: __SCREAMING_SNAKE_CASE = bnb.nn.Paramsabit(A__ , requires_grad=A__ , **A__ ).to(A__ ) __SCREAMING_SNAKE_CASE = new_value if fpaa_statistics is not None: setattr(module.weight , "SCB" , fpaa_statistics.to(A__ ) ) else: if value is None: __SCREAMING_SNAKE_CASE = old_value.to(A__ ) elif isinstance(A__ , torch.Tensor ): __SCREAMING_SNAKE_CASE = value.to(A__ ) else: __SCREAMING_SNAKE_CASE = torch.tensor(A__ , device=A__ ) if is_buffer: __SCREAMING_SNAKE_CASE = new_value else: __SCREAMING_SNAKE_CASE = nn.Parameter(A__ , requires_grad=old_value.requires_grad ) __SCREAMING_SNAKE_CASE = new_value def _A ( __snake_case :Dict , __snake_case :Dict=None , __snake_case :Optional[Any]=None , __snake_case :Tuple=None , __snake_case :str=False ) -> Tuple: """simple docstring""" for name, module in model.named_children(): if current_key_name is None: __SCREAMING_SNAKE_CASE = [] current_key_name.append(A__ ) if (isinstance(A__ , nn.Linear ) or isinstance(A__ , A__ )) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` if not any(key in ".".join(A__ ) for key in modules_to_not_convert ): with init_empty_weights(): if isinstance(A__ , A__ ): __SCREAMING_SNAKE_CASE = module.weight.shape else: __SCREAMING_SNAKE_CASE = module.in_features __SCREAMING_SNAKE_CASE = module.out_features if quantization_config.quantization_method() == "llm_int8": __SCREAMING_SNAKE_CASE = bnb.nn.LinearabitLt( A__ , A__ , module.bias is not None , has_fpaa_weights=quantization_config.llm_inta_has_fpaa_weight , threshold=quantization_config.llm_inta_threshold , ) __SCREAMING_SNAKE_CASE = True else: if ( quantization_config.llm_inta_skip_modules is not None and name in quantization_config.llm_inta_skip_modules ): pass else: __SCREAMING_SNAKE_CASE = bnb.nn.Linearabit( A__ , A__ , module.bias is not None , quantization_config.bnb_abit_compute_dtype , compress_statistics=quantization_config.bnb_abit_use_double_quant , quant_type=quantization_config.bnb_abit_quant_type , ) __SCREAMING_SNAKE_CASE = True # Store the module class in case we need to transpose the weight later __SCREAMING_SNAKE_CASE = type(A__ ) # Force requires grad to False to avoid unexpected errors model._modules[name].requires_grad_(A__ ) if len(list(module.children() ) ) > 0: __SCREAMING_SNAKE_CASE = _replace_with_bnb_linear( A__ , A__ , A__ , A__ , has_been_replaced=A__ , ) # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def _A ( __snake_case :List[str] , __snake_case :int=None , __snake_case :Union[str, Any]=None , __snake_case :List[Any]=None ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = ['''lm_head'''] if modules_to_not_convert is None else modules_to_not_convert __SCREAMING_SNAKE_CASE = _replace_with_bnb_linear( A__ , A__ , A__ , A__ ) if not has_been_replaced: logger.warning( "You are loading your model in 8bit or 4bit but no linear modules were found in your model." " Please double check your model architecture, or submit an issue on github if you think this is" " a bug." ) return model def _A ( *__snake_case :Tuple , **__snake_case :Optional[Any] ) -> Optional[int]: """simple docstring""" warnings.warn( "`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead" , A__ , ) return replace_with_bnb_linear(*A__ , **A__ ) def _A ( *__snake_case :str , **__snake_case :Optional[int] ) -> Union[str, Any]: """simple docstring""" warnings.warn( "`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead" , A__ , ) return set_module_quantized_tensor_to_device(*A__ , **A__ ) def _A ( __snake_case :Any ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = deepcopy(A__ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` tied_model.tie_weights() __SCREAMING_SNAKE_CASE = find_tied_parameters(A__ ) # For compatibility with Accelerate < 0.18 if isinstance(A__ , A__ ): __SCREAMING_SNAKE_CASE = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: __SCREAMING_SNAKE_CASE = sum(A__ , [] ) __SCREAMING_SNAKE_CASE = len(A__ ) > 0 # Check if it is a base model __SCREAMING_SNAKE_CASE = not hasattr(A__ , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head __SCREAMING_SNAKE_CASE = list(model.named_children() ) __SCREAMING_SNAKE_CASE = [list_modules[-1][0]] # add last module together with tied weights __SCREAMING_SNAKE_CASE = set(A__ ) - set(A__ ) __SCREAMING_SNAKE_CASE = list(set(A__ ) ) + list(A__ ) # remove ".weight" from the keys __SCREAMING_SNAKE_CASE = ['''.weight''', '''.bias'''] __SCREAMING_SNAKE_CASE = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: __SCREAMING_SNAKE_CASE = name.replace(A__ , "" ) filtered_module_names.append(A__ ) return filtered_module_names
693
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer a_ :Tuple = logging.get_logger(__name__) a_ :Optional[Any] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} a_ :Optional[int] = { 'vocab_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-ctx_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-ctx_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } a_ :Dict = { 'vocab_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-question_encoder-single-nq-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-question_encoder-multiset-base': ( 'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json' ), }, } a_ :Any = { 'vocab_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'facebook/dpr-reader-single-nq-base': ( 'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json' ), 'facebook/dpr-reader-multiset-base': ( 'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json' ), }, } a_ :Optional[int] = { 'facebook/dpr-ctx_encoder-single-nq-base': 5_12, 'facebook/dpr-ctx_encoder-multiset-base': 5_12, } a_ :List[Any] = { 'facebook/dpr-question_encoder-single-nq-base': 5_12, 'facebook/dpr-question_encoder-multiset-base': 5_12, } a_ :Tuple = { 'facebook/dpr-reader-single-nq-base': 5_12, 'facebook/dpr-reader-multiset-base': 5_12, } a_ :str = { 'facebook/dpr-ctx_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-ctx_encoder-multiset-base': {'do_lower_case': True}, } a_ :Optional[int] = { 'facebook/dpr-question_encoder-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-question_encoder-multiset-base': {'do_lower_case': True}, } a_ :Any = { 'facebook/dpr-reader-single-nq-base': {'do_lower_case': True}, 'facebook/dpr-reader-multiset-base': {'do_lower_case': True}, } class lowercase ( _UpperCAmelCase ): lowerCamelCase : Optional[Any] = VOCAB_FILES_NAMES lowerCamelCase : int = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : List[str] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : int = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class lowercase ( _UpperCAmelCase ): lowerCamelCase : Optional[int] = VOCAB_FILES_NAMES lowerCamelCase : Union[str, Any] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : Optional[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : Optional[int] = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION a_ :List[str] = collections.namedtuple( 'DPRSpanPrediction', ['span_score', 'relevance_score', 'doc_id', 'start_index', 'end_index', 'text'] ) a_ :Optional[int] = collections.namedtuple('DPRReaderOutput', ['start_logits', 'end_logits', 'relevance_logits']) a_ :Tuple = r'\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `\'tf\'`: Return TensorFlow `tf.constant` objects.\n - `\'pt\'`: Return PyTorch `torch.Tensor` objects.\n - `\'np\'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer\'s default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n ' @add_start_docstrings(_UpperCAmelCase ) class lowercase : def __call__( self : List[Any] , _lowercase : Any , _lowercase : Optional[str] = None , _lowercase : Optional[str] = None , _lowercase : Union[bool, str] = False , _lowercase : Union[bool, str] = False , _lowercase : Optional[int] = None , _lowercase : Optional[Union[str, TensorType]] = None , _lowercase : Optional[bool] = None , **_lowercase : str , ): if titles is None and texts is None: return super().__call__( _lowercase , padding=_lowercase , truncation=_lowercase , max_length=_lowercase , return_tensors=_lowercase , return_attention_mask=_lowercase , **_lowercase , ) elif titles is None or texts is None: SCREAMING_SNAKE_CASE__ : List[str] = titles if texts is None else texts return super().__call__( _lowercase , _lowercase , padding=_lowercase , truncation=_lowercase , max_length=_lowercase , return_tensors=_lowercase , return_attention_mask=_lowercase , **_lowercase , ) SCREAMING_SNAKE_CASE__ : Optional[Any] = titles if not isinstance(_lowercase , _lowercase ) else [titles] SCREAMING_SNAKE_CASE__ : Optional[int] = texts if not isinstance(_lowercase , _lowercase ) else [texts] SCREAMING_SNAKE_CASE__ : List[Any] = len(_lowercase ) SCREAMING_SNAKE_CASE__ : str = questions if not isinstance(_lowercase , _lowercase ) else [questions] * n_passages if len(_lowercase ) != len(_lowercase ): raise ValueError( f"""There should be as many titles than texts but got {len(_lowercase )} titles and {len(_lowercase )} texts.""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = super().__call__(_lowercase , _lowercase , padding=_lowercase , truncation=_lowercase )['''input_ids'''] SCREAMING_SNAKE_CASE__ : Tuple = super().__call__(_lowercase , add_special_tokens=_lowercase , padding=_lowercase , truncation=_lowercase )['''input_ids'''] SCREAMING_SNAKE_CASE__ : Optional[Any] = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_lowercase , _lowercase ) ] } if return_attention_mask is not False: SCREAMING_SNAKE_CASE__ : Optional[int] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) SCREAMING_SNAKE_CASE__ : Dict = attention_mask return self.pad(_lowercase , padding=_lowercase , max_length=_lowercase , return_tensors=_lowercase ) def lowercase__ ( self : List[Any] , _lowercase : BatchEncoding , _lowercase : DPRReaderOutput , _lowercase : int = 16 , _lowercase : int = 64 , _lowercase : int = 4 , ): SCREAMING_SNAKE_CASE__ : Optional[int] = reader_input['''input_ids'''] SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple = reader_output[:3] SCREAMING_SNAKE_CASE__ : Any = len(_lowercase ) SCREAMING_SNAKE_CASE__ : int = sorted(range(_lowercase ) , reverse=_lowercase , key=relevance_logits.__getitem__ ) SCREAMING_SNAKE_CASE__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: SCREAMING_SNAKE_CASE__ : Optional[int] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence SCREAMING_SNAKE_CASE__ : Any = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: SCREAMING_SNAKE_CASE__ : Dict = sequence_ids.index(self.pad_token_id ) else: SCREAMING_SNAKE_CASE__ : List[str] = len(_lowercase ) SCREAMING_SNAKE_CASE__ : Any = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=_lowercase , top_spans=_lowercase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=_lowercase , start_index=_lowercase , end_index=_lowercase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(_lowercase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def lowercase__ ( self : Dict , _lowercase : List[int] , _lowercase : List[int] , _lowercase : int , _lowercase : int , ): SCREAMING_SNAKE_CASE__ : Optional[int] = [] for start_index, start_score in enumerate(_lowercase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) SCREAMING_SNAKE_CASE__ : Optional[int] = sorted(_lowercase , key=lambda _lowercase : x[1] , reverse=_lowercase ) SCREAMING_SNAKE_CASE__ : Optional[Any] = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f"""Wrong span indices: [{start_index}:{end_index}]""" ) SCREAMING_SNAKE_CASE__ : Tuple = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f"""Span is too long: {length} > {max_answer_length}""" ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_lowercase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_UpperCAmelCase ) class lowercase ( _UpperCAmelCase , _UpperCAmelCase ): lowerCamelCase : Dict = VOCAB_FILES_NAMES lowerCamelCase : Union[str, Any] = READER_PRETRAINED_VOCAB_FILES_MAP lowerCamelCase : List[str] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase : str = READER_PRETRAINED_INIT_CONFIGURATION lowerCamelCase : List[Any] = ['''input_ids''', '''attention_mask''']
35
0
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() a__ : Optional[int] = logging.get_logger(__name__) def __lowerCamelCase ( UpperCAmelCase_ ) ->List[str]: snake_case__ = torch.load(A__ , map_location='cpu' ) if "model" in sd.keys(): snake_case__ = torch.load(A__ , map_location='cpu' )['''model'''] # pop unnecessary weights snake_case__ = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(A__ ) snake_case__ = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: snake_case__ = sd.pop(A__ ) snake_case__ = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: snake_case__ = sd[key] # We split QKV in separate Q,K,V snake_case__ = key.replace('.qkv_proj.' , '.q_proj.' ) snake_case__ = key.replace('.qkv_proj.' , '.k_proj.' ) snake_case__ = key.replace('.qkv_proj.' , '.v_proj.' ) snake_case__ = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 snake_case__ = torch.split(A__ , depth // 3 , dim=0 ) snake_case__ = q snake_case__ = k snake_case__ = v del sd[key] return sd @torch.no_grad() def __lowerCamelCase ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_=None ) ->List[str]: snake_case__ = load_checkpoint(A__ ) if config is not None: snake_case__ = OPTConfig.from_pretrained(A__ ) else: snake_case__ = OPTConfig() snake_case__ = OPTModel(A__ ).half().eval() model.load_state_dict(A__ ) # Check results Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) if __name__ == "__main__": a__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--fairseq_path''', type=str, help=( '''path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:''' ''' https://huggingface.co/models?other=opt_metasq''' ), ) parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--hf_config''', default=None, type=str, help='''Define HF config.''') a__ : Union[str, Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
368
import random def a ( A__ ) -> bool: '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = num - 1 SCREAMING_SNAKE_CASE__ : Optional[int] = 0 while s % 2 == 0: SCREAMING_SNAKE_CASE__ : Optional[Any] = s // 2 t += 1 for _ in range(5 ): SCREAMING_SNAKE_CASE__ : int = random.randrange(2 , num - 1 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = pow(A__ , A__ , A__ ) if v != 1: SCREAMING_SNAKE_CASE__ : List[str] = 0 while v != (num - 1): if i == t - 1: return False else: SCREAMING_SNAKE_CASE__ : Any = i + 1 SCREAMING_SNAKE_CASE__ : Union[str, Any] = (v**2) % num return True def a ( A__ ) -> bool: '''simple docstring''' if num < 2: return False SCREAMING_SNAKE_CASE__ : Optional[int] = [ 2, 3, 5, 7, 1_1, 1_3, 1_7, 1_9, 2_3, 2_9, 3_1, 3_7, 4_1, 4_3, 4_7, 5_3, 5_9, 6_1, 6_7, 7_1, 7_3, 7_9, 8_3, 8_9, 9_7, 1_0_1, 1_0_3, 1_0_7, 1_0_9, 1_1_3, 1_2_7, 1_3_1, 1_3_7, 1_3_9, 1_4_9, 1_5_1, 1_5_7, 1_6_3, 1_6_7, 1_7_3, 1_7_9, 1_8_1, 1_9_1, 1_9_3, 1_9_7, 1_9_9, 2_1_1, 2_2_3, 2_2_7, 2_2_9, 2_3_3, 2_3_9, 2_4_1, 2_5_1, 2_5_7, 2_6_3, 2_6_9, 2_7_1, 2_7_7, 2_8_1, 2_8_3, 2_9_3, 3_0_7, 3_1_1, 3_1_3, 3_1_7, 3_3_1, 3_3_7, 3_4_7, 3_4_9, 3_5_3, 3_5_9, 3_6_7, 3_7_3, 3_7_9, 3_8_3, 3_8_9, 3_9_7, 4_0_1, 4_0_9, 4_1_9, 4_2_1, 4_3_1, 4_3_3, 4_3_9, 4_4_3, 4_4_9, 4_5_7, 4_6_1, 4_6_3, 4_6_7, 4_7_9, 4_8_7, 4_9_1, 4_9_9, 5_0_3, 5_0_9, 5_2_1, 5_2_3, 5_4_1, 5_4_7, 5_5_7, 5_6_3, 5_6_9, 5_7_1, 5_7_7, 5_8_7, 5_9_3, 5_9_9, 6_0_1, 6_0_7, 6_1_3, 6_1_7, 6_1_9, 6_3_1, 6_4_1, 6_4_3, 6_4_7, 6_5_3, 6_5_9, 6_6_1, 6_7_3, 6_7_7, 6_8_3, 6_9_1, 7_0_1, 7_0_9, 7_1_9, 7_2_7, 7_3_3, 7_3_9, 7_4_3, 7_5_1, 7_5_7, 7_6_1, 7_6_9, 7_7_3, 7_8_7, 7_9_7, 8_0_9, 8_1_1, 8_2_1, 8_2_3, 8_2_7, 8_2_9, 8_3_9, 8_5_3, 8_5_7, 8_5_9, 8_6_3, 8_7_7, 8_8_1, 8_8_3, 8_8_7, 9_0_7, 9_1_1, 9_1_9, 9_2_9, 9_3_7, 9_4_1, 9_4_7, 9_5_3, 9_6_7, 9_7_1, 9_7_7, 9_8_3, 9_9_1, 9_9_7, ] if num in low_primes: return True for prime in low_primes: if (num % prime) == 0: return False return rabin_miller(A__ ) def a ( A__ = 1_0_2_4 ) -> int: '''simple docstring''' while True: SCREAMING_SNAKE_CASE__ : Any = random.randrange(2 ** (keysize - 1) , 2 ** (keysize) ) if is_prime_low_num(A__ ): return num if __name__ == "__main__": a_ :Dict = generate_large_prime() print(('Prime number:', num)) print(('is_prime_low_num:', is_prime_low_num(num)))
35
0
'''simple docstring''' import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: lowercase__ : Union[str, Any] = None lowercase__ : str = logging.get_logger(__name__) lowercase__ : str = {'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'} lowercase__ : Union[str, Any] = { 'vocab_file': { 't5-small': 'https://huggingface.co/t5-small/resolve/main/spiece.model', 't5-base': 'https://huggingface.co/t5-base/resolve/main/spiece.model', 't5-large': 'https://huggingface.co/t5-large/resolve/main/spiece.model', 't5-3b': 'https://huggingface.co/t5-3b/resolve/main/spiece.model', 't5-11b': 'https://huggingface.co/t5-11b/resolve/main/spiece.model', }, 'tokenizer_file': { 't5-small': 'https://huggingface.co/t5-small/resolve/main/tokenizer.json', 't5-base': 'https://huggingface.co/t5-base/resolve/main/tokenizer.json', 't5-large': 'https://huggingface.co/t5-large/resolve/main/tokenizer.json', 't5-3b': 'https://huggingface.co/t5-3b/resolve/main/tokenizer.json', 't5-11b': 'https://huggingface.co/t5-11b/resolve/main/tokenizer.json', }, } # TODO(PVP) - this should be removed in Transformers v5 lowercase__ : Optional[int] = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } class lowerCamelCase ( _UpperCAmelCase ): '''simple docstring''' lowerCAmelCase__ = VOCAB_FILES_NAMES lowerCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase__ = ['''input_ids''', '''attention_mask'''] lowerCAmelCase__ = TaTokenizer lowerCAmelCase__ = [] def __init__( self : Optional[int] , UpperCAmelCase__ : Any=None , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : Dict="</s>" , UpperCAmelCase__ : int="<unk>" , UpperCAmelCase__ : int="<pad>" , UpperCAmelCase__ : List[Any]=100 , UpperCAmelCase__ : Dict=None , **UpperCAmelCase__ : Tuple , ) ->str: # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: UpperCAmelCase_ = [f"""<extra_id_{i}>""" for i in range(_lowercase )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens UpperCAmelCase_ = len(set(filter(lambda UpperCAmelCase__ : bool('''extra_id_''' in str(_lowercase ) ) , _lowercase ) ) ) if extra_tokens != extra_ids: raise ValueError( f"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are""" ''' provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids''' ''' tokens''' ) super().__init__( _lowercase , tokenizer_file=_lowercase , eos_token=_lowercase , unk_token=_lowercase , pad_token=_lowercase , extra_ids=_lowercase , additional_special_tokens=_lowercase , **_lowercase , ) UpperCAmelCase_ = vocab_file UpperCAmelCase_ = False if not self.vocab_file else True UpperCAmelCase_ = extra_ids @staticmethod def lowerCAmelCase__ ( UpperCAmelCase__ : Any , UpperCAmelCase__ : str , UpperCAmelCase__ : Union[str, Any] ) ->Any: if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: UpperCAmelCase_ = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( '''This tokenizer was incorrectly instantiated with a model max length of''' f""" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this""" ''' behavior is kept to avoid breaking backwards compatibility when padding/encoding with''' ''' `truncation is True`.\n- Be aware that you SHOULD NOT rely on''' f""" {pretrained_model_name_or_path} automatically truncating your input to""" f""" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences""" f""" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with""" ''' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please''' ''' instantiate this tokenizer with `model_max_length` set to your preferred value.''' , _lowercase , ) return max_model_length def lowerCAmelCase__ ( self : List[str] , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[str] = None ) ->Dict: if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(_lowercase ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCAmelCase_ = os.path.join( _lowercase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_lowercase ): copyfile(self.vocab_file , _lowercase ) logger.info(f"""Copy vocab file to {out_vocab_file}""" ) return (out_vocab_file,) def lowerCAmelCase__ ( self : List[Any] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) ->str: UpperCAmelCase_ = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: UpperCAmelCase_ = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def lowerCAmelCase__ ( self : List[str] , UpperCAmelCase__ : List[int] , UpperCAmelCase__ : Optional[List[int]] = None ) ->Any: UpperCAmelCase_ = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def lowerCAmelCase__ ( self : List[str] ) ->Dict: return list( set(filter(lambda UpperCAmelCase__ : bool(re.search(r'''<extra_id_\d+>''' , _lowercase ) ) is not None , self.additional_special_tokens ) ) ) def lowerCAmelCase__ ( self : List[str] ) ->str: return [self.convert_tokens_to_ids(_lowercase ) for token in self.get_sentinel_tokens()]
390
# Logistic Regression from scratch # In[62]: # In[63]: # importing all the required libraries import numpy as np from matplotlib import pyplot as plt from sklearn import datasets def a ( A__ ) -> List[Any]: '''simple docstring''' return 1 / (1 + np.exp(-z )) def a ( A__ , A__ ) -> Any: '''simple docstring''' return (-y * np.log(A__ ) - (1 - y) * np.log(1 - h )).mean() def a ( A__ , A__ , A__ ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = np.dot(A__ , A__ ) return np.sum(y * scores - np.log(1 + np.exp(A__ ) ) ) def a ( A__ , A__ , A__ , A__=7_0_0_0_0 ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE__ : int = np.zeros(x.shape[1] ) for iterations in range(A__ ): SCREAMING_SNAKE_CASE__ : List[Any] = np.dot(A__ , A__ ) SCREAMING_SNAKE_CASE__ : Dict = sigmoid_function(A__ ) SCREAMING_SNAKE_CASE__ : int = np.dot(x.T , h - y ) / y.size SCREAMING_SNAKE_CASE__ : Union[str, Any] = theta - alpha * gradient # updating the weights SCREAMING_SNAKE_CASE__ : Optional[int] = np.dot(A__ , A__ ) SCREAMING_SNAKE_CASE__ : int = sigmoid_function(A__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = cost_function(A__ , A__ ) if iterations % 1_0_0 == 0: print(f"""loss: {j} \t""" ) # printing the loss after every 100 iterations return theta # In[68]: if __name__ == "__main__": a_ :str = datasets.load_iris() a_ :Dict = iris.data[:, :2] a_ :int = (iris.target != 0) * 1 a_ :Dict = 0.1 a_ :str = logistic_reg(alpha, x, y, max_iterations=7_00_00) print('theta: ', theta) # printing the theta i.e our weights vector def a ( A__ ) -> int: '''simple docstring''' return sigmoid_function( np.dot(A__ , A__ ) ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) plt.scatter(x[y == 0][:, 0], x[y == 0][:, 1], color='b', label='0') plt.scatter(x[y == 1][:, 0], x[y == 1][:, 1], color='r', label='1') ((a_) , (a_)) :str = (x[:, 0].min(), x[:, 0].max()) ((a_) , (a_)) :Tuple = (x[:, 1].min(), x[:, 1].max()) ((a_) , (a_)) :Dict = np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max)) a_ :Optional[int] = np.c_[xxa.ravel(), xxa.ravel()] a_ :Optional[int] = predict_prob(grid).reshape(xxa.shape) plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='black') plt.legend() plt.show()
35
0
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
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 _A ( snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = MgpstrTokenizer __lowerCamelCase : Tuple = False __lowerCamelCase : int = {} __lowerCamelCase : Optional[int] = False def snake_case_ ( self ): '''simple docstring''' super().setUp() # fmt: off snake_case : Optional[int] = ["""[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 snake_case : Union[str, Any] = dict(zip(SCREAMING_SNAKE_CASE_ ,range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) snake_case : Dict = 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(SCREAMING_SNAKE_CASE_ ) + """\n""" ) def snake_case_ ( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return MgpstrTokenizer.from_pretrained(self.tmpdirname ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : str = """tester""" snake_case : Tuple = """tester""" return input_text, output_text @unittest.skip("""MGP-STR always lower cases letters.""" ) def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE_ ) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): snake_case : Union[str, Any] = """[SPECIAL_TOKEN]""" tokenizer.add_special_tokens({"""cls_token""": special_token} ) snake_case : str = tokenizer.encode([special_token] ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,1 ) snake_case : Dict = tokenizer.decode(SCREAMING_SNAKE_CASE_ ,skip_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertTrue(special_token not in decoded ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): snake_case , snake_case : List[str] = self.get_input_output_texts(SCREAMING_SNAKE_CASE_ ) snake_case : Any = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) snake_case : str = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = tokenizer.encode(SCREAMING_SNAKE_CASE_ ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Dict = tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(len(SCREAMING_SNAKE_CASE_ ) ,0 ) snake_case : Any = tokenizer.decode(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(text_a.replace(""" """ ,"""""" ) ,SCREAMING_SNAKE_CASE_ ) @unittest.skip("""MGP-STR tokenizer only handles one sequence.""" ) def snake_case_ ( self ): '''simple docstring''' pass @unittest.skip("""inputs cannot be pretokenized in MgpstrTokenizer""" ) def snake_case_ ( self ): '''simple docstring''' pass
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class _A ( unittest.TestCase ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : Union[str, Any] = UNetaDModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=3 ,out_channels=3 ,down_block_types=("""DownBlock2D""", """AttnDownBlock2D""") ,up_block_types=("""AttnUpBlock2D""", """UpBlock2D""") ,) return model def snake_case_ ( self ): '''simple docstring''' snake_case : str = self.dummy_uncond_unet snake_case : Union[str, Any] = ScoreSdeVeScheduler() snake_case : int = ScoreSdeVePipeline(unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ) sde_ve.to(SCREAMING_SNAKE_CASE_ ) sde_ve.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = torch.manual_seed(0 ) snake_case : Dict = sde_ve(num_inference_steps=2 ,output_type="""numpy""" ,generator=SCREAMING_SNAKE_CASE_ ).images snake_case : Tuple = torch.manual_seed(0 ) snake_case : str = sde_ve(num_inference_steps=2 ,output_type="""numpy""" ,generator=SCREAMING_SNAKE_CASE_ ,return_dict=SCREAMING_SNAKE_CASE_ )[ 0 ] snake_case : Union[str, Any] = image[0, -3:, -3:, -1] snake_case : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case : Tuple = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = """google/ncsnpp-church-256""" snake_case : Any = UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = ScoreSdeVeScheduler.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = ScoreSdeVePipeline(unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ) sde_ve.to(SCREAMING_SNAKE_CASE_ ) sde_ve.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = torch.manual_seed(0 ) snake_case : str = sde_ve(num_inference_steps=10 ,output_type="""numpy""" ,generator=SCREAMING_SNAKE_CASE_ ).images snake_case : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) snake_case : Tuple = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __lowercase : str = logging.get_logger(__name__) __lowercase : Dict = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class _A ( snake_case , snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = '''convnextv2''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.0 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : Dict = num_channels snake_case : List[str] = patch_size snake_case : List[Any] = num_stages snake_case : Union[str, Any] = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes snake_case : Optional[Any] = [3, 3, 9, 3] if depths is None else depths snake_case : List[Any] = hidden_act snake_case : Optional[Any] = initializer_range snake_case : str = layer_norm_eps snake_case : str = drop_path_rate snake_case : Any = image_size snake_case : Tuple = ["""stem"""] + [F"""stage{idx}""" for idx in range(1 ,len(self.depths ) + 1 )] snake_case , snake_case : str = get_aligned_output_features_output_indices( out_features=SCREAMING_SNAKE_CASE_ ,out_indices=SCREAMING_SNAKE_CASE_ ,stage_names=self.stage_names )
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
from math import factorial __lowercase : Optional[Any] = {str(d): factorial(d) for d in range(10)} def lowercase ( __A : int ) -> int: '''simple docstring''' return sum(DIGIT_FACTORIAL[d] for d in str(__A ) ) def lowercase ( ) -> int: '''simple docstring''' snake_case : Dict = 7 * factorial(9 ) + 1 return sum(i for i in range(3 , __A ) if sum_of_digit_factorial(__A ) == i ) if __name__ == "__main__": print(f'''{solution() = }''')
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) 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()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1
import json import os from collections import Counter import torch import torchvision import torchvision.transforms as transforms from PIL import Image from torch import nn from torch.utils.data import Dataset __lowercase : Tuple = {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)} class _A ( nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Optional[int] = torchvision.models.resnetaaa(pretrained=SCREAMING_SNAKE_CASE_ ) snake_case : str = list(model.children() )[:-2] snake_case : Union[str, Any] = nn.Sequential(*SCREAMING_SNAKE_CASE_ ) snake_case : str = nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds] ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' # Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048 snake_case : Optional[int] = self.pool(self.model(SCREAMING_SNAKE_CASE_ ) ) snake_case : Optional[int] = torch.flatten(SCREAMING_SNAKE_CASE_ ,start_dim=2 ) snake_case : Optional[Any] = out.transpose(1 ,2 ).contiguous() return out # BxNx2048 class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Optional[int] = [json.loads(SCREAMING_SNAKE_CASE_ ) for l in open(SCREAMING_SNAKE_CASE_ )] snake_case : Any = os.path.dirname(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = tokenizer snake_case : Optional[int] = labels snake_case : Tuple = len(SCREAMING_SNAKE_CASE_ ) snake_case : str = max_seq_length snake_case : Tuple = transforms def __len__( self ): '''simple docstring''' return len(self.data ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = torch.LongTensor(self.tokenizer.encode(self.data[index]["""text"""] ,add_special_tokens=SCREAMING_SNAKE_CASE_ ) ) snake_case , snake_case , snake_case : Tuple = sentence[0], sentence[1:-1], sentence[-1] snake_case : Dict = sentence[: self.max_seq_length] snake_case : Union[str, Any] = torch.zeros(self.n_classes ) snake_case : Dict = 1 snake_case : List[Any] = Image.open(os.path.join(self.data_dir ,self.data[index]["""img"""] ) ).convert("""RGB""" ) snake_case : Optional[int] = self.transforms(SCREAMING_SNAKE_CASE_ ) return { "image_start_token": start_token, "image_end_token": end_token, "sentence": sentence, "image": image, "label": label, } def snake_case_ ( self ): '''simple docstring''' snake_case : Any = Counter() for row in self.data: label_freqs.update(row["""label"""] ) return label_freqs def lowercase ( __A : List[Any] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[str] = [len(row["""sentence"""] ) for row in batch] snake_case , snake_case : Any = len(__A ), max(__A ) snake_case : Optional[Any] = torch.zeros(__A , __A , dtype=torch.long ) snake_case : List[str] = torch.zeros(__A , __A , dtype=torch.long ) for i_batch, (input_row, length) in enumerate(zip(__A , __A ) ): snake_case : List[Any] = input_row["""sentence"""] snake_case : List[Any] = 1 snake_case : List[Any] = torch.stack([row["""image"""] for row in batch] ) snake_case : int = torch.stack([row["""label"""] for row in batch] ) snake_case : int = torch.stack([row["""image_start_token"""] for row in batch] ) snake_case : List[Any] = torch.stack([row["""image_end_token"""] for row in batch] ) return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor def lowercase ( ) -> List[Any]: '''simple docstring''' return [ "Crime", "Drama", "Thriller", "Action", "Comedy", "Romance", "Documentary", "Short", "Mystery", "History", "Family", "Adventure", "Fantasy", "Sci-Fi", "Western", "Horror", "Sport", "War", "Music", "Musical", "Animation", "Biography", "Film-Noir", ] def lowercase ( ) -> Tuple: '''simple docstring''' return transforms.Compose( [ transforms.Resize(256 ), transforms.CenterCrop(224 ), transforms.ToTensor(), transforms.Normalize( mean=[0.46_777_044, 0.44_531_429, 0.40_661_017] , std=[0.12_221_994, 0.12_145_835, 0.14_380_469] , ), ] )
36
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
1
from __future__ import annotations import collections import pprint from pathlib import Path def lowercase ( __A : str ) -> str: '''simple docstring''' return "".join(sorted(__A ) ) def lowercase ( __A : str ) -> list[str]: '''simple docstring''' return word_by_signature[signature(__A )] __lowercase : str = Path(__file__).parent.joinpath('''words.txt''').read_text(encoding='''utf-8''') __lowercase : Any = sorted({word.strip().lower() for word in data.splitlines()}) __lowercase : List[str] = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": __lowercase : List[str] = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open('''anagrams.txt''', '''w''') as file: file.write('''all_anagrams = \n ''') file.write(pprint.pformat(all_anagrams))
36
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 __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''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 _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
1
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = '''▁''' __lowercase : str = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', } __lowercase : List[str] = { '''vocab_file''': { '''facebook/s2t-small-librispeech-asr''': ( '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json''' ), }, '''spm_file''': { '''facebook/s2t-small-librispeech-asr''': ( '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model''' ) }, } __lowercase : Union[str, Any] = { '''facebook/s2t-small-librispeech-asr''': 1_024, } __lowercase : List[Any] = ['''pt''', '''fr''', '''ru''', '''nl''', '''ro''', '''it''', '''es''', '''de'''] __lowercase : Optional[int] = {'''mustc''': MUSTC_LANGS} class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = VOCAB_FILES_NAMES __lowerCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Optional[int] = MAX_MODEL_INPUT_SIZES __lowerCamelCase : Tuple = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_="<s>" ,SCREAMING_SNAKE_CASE_="</s>" ,SCREAMING_SNAKE_CASE_="<pad>" ,SCREAMING_SNAKE_CASE_="<unk>" ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Optional[int] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=SCREAMING_SNAKE_CASE_ ,eos_token=SCREAMING_SNAKE_CASE_ ,unk_token=SCREAMING_SNAKE_CASE_ ,pad_token=SCREAMING_SNAKE_CASE_ ,do_upper_case=SCREAMING_SNAKE_CASE_ ,do_lower_case=SCREAMING_SNAKE_CASE_ ,tgt_lang=SCREAMING_SNAKE_CASE_ ,lang_codes=SCREAMING_SNAKE_CASE_ ,sp_model_kwargs=self.sp_model_kwargs ,**SCREAMING_SNAKE_CASE_ ,) snake_case : Optional[int] = do_upper_case snake_case : Optional[Any] = do_lower_case snake_case : Optional[Any] = load_json(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = {v: k for k, v in self.encoder.items()} snake_case : Tuple = spm_file snake_case : Optional[Any] = load_spm(SCREAMING_SNAKE_CASE_ ,self.sp_model_kwargs ) if lang_codes is not None: snake_case : List[Any] = lang_codes snake_case : str = LANGUAGES[lang_codes] snake_case : Tuple = [F"""<lang:{lang}>""" for lang in self.langs] snake_case : List[str] = {lang: self.sp_model.PieceToId(F"""<lang:{lang}>""" ) for lang in self.langs} snake_case : Dict = self.lang_tokens snake_case : Tuple = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang ) else: snake_case : str = {} @property def snake_case_ ( self ): '''simple docstring''' return len(self.encoder ) @property def snake_case_ ( self ): '''simple docstring''' return self._tgt_lang @tgt_lang.setter def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Optional[int] = new_tgt_lang self.set_tgt_lang_special_tokens(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = self.lang_code_to_id[tgt_lang] snake_case : Union[str, Any] = [lang_code_id] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.sp_model.encode(SCREAMING_SNAKE_CASE_ ,out_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.encoder.get(SCREAMING_SNAKE_CASE_ ,self.encoder[self.unk_token] ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.decoder.get(SCREAMING_SNAKE_CASE_ ,self.unk_token ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] snake_case : int = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: snake_case : Tuple = self.sp_model.decode(SCREAMING_SNAKE_CASE_ ) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " snake_case : Optional[int] = [] else: current_sub_tokens.append(SCREAMING_SNAKE_CASE_ ) snake_case : str = self.sp_model.decode(SCREAMING_SNAKE_CASE_ ) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + [self.eos_token_id] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ ,token_ids_a=SCREAMING_SNAKE_CASE_ ,already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = [1] * len(self.prefix_tokens ) snake_case : Tuple = [1] if token_ids_a is None: return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE_ )) + suffix_ones return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE_ )) + ([0] * len(SCREAMING_SNAKE_CASE_ )) + suffix_ones def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = self.encoder.copy() vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): '''simple docstring''' snake_case : Tuple = self.__dict__.copy() snake_case : List[Any] = None return state def __setstate__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : int = d # for backward compatibility if not hasattr(self ,"""sp_model_kwargs""" ): snake_case : List[str] = {} snake_case : Dict = load_spm(self.spm_file ,self.sp_model_kwargs ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Union[str, Any] = Path(SCREAMING_SNAKE_CASE_ ) assert save_dir.is_dir(), F"""{save_directory} should be a directory""" snake_case : List[Any] = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) snake_case : List[Any] = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder ,SCREAMING_SNAKE_CASE_ ) if os.path.abspath(self.spm_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file ,SCREAMING_SNAKE_CASE_ ) elif not os.path.isfile(self.spm_file ): with open(SCREAMING_SNAKE_CASE_ ,"""wb""" ) as fi: snake_case : List[Any] = self.sp_model.serialized_model_proto() fi.write(SCREAMING_SNAKE_CASE_ ) return (str(SCREAMING_SNAKE_CASE_ ), str(SCREAMING_SNAKE_CASE_ )) def lowercase ( __A : str , __A : Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: '''simple docstring''' snake_case : Optional[int] = sentencepiece.SentencePieceProcessor(**__A ) spm.Load(str(__A ) ) return spm def lowercase ( __A : str ) -> Union[Dict, List]: '''simple docstring''' with open(__A , """r""" ) as f: return json.load(__A ) def lowercase ( __A : str , __A : str ) -> None: '''simple docstring''' with open(__A , """w""" ) as f: json.dump(__A , __A , indent=2 )
36
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''decision_transformer''' __lowerCamelCase : Optional[Any] = ['''past_key_values'''] __lowerCamelCase : Tuple = { '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=17 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="relu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Any = state_dim snake_case : Optional[Any] = act_dim snake_case : Union[str, Any] = hidden_size snake_case : Any = max_ep_len snake_case : int = action_tanh snake_case : Any = vocab_size snake_case : Any = n_positions snake_case : List[str] = n_layer snake_case : int = n_head snake_case : Optional[int] = n_inner snake_case : List[Any] = activation_function snake_case : Tuple = resid_pdrop snake_case : Optional[Any] = embd_pdrop snake_case : Dict = attn_pdrop snake_case : List[str] = layer_norm_epsilon snake_case : Union[str, Any] = initializer_range snake_case : Optional[Any] = scale_attn_weights snake_case : str = use_cache snake_case : int = scale_attn_by_inverse_layer_idx snake_case : Tuple = reorder_and_upcast_attn snake_case : Tuple = bos_token_id snake_case : List[str] = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) __lowercase : Any = logging.getLogger() def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = argparse.ArgumentParser() parser.add_argument("""-f""" ) snake_case : Optional[Any] = parser.parse_args() return args.f class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = logging.StreamHandler(sys.stdout ) logger.addHandler(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : str = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 ,"""run_glue_deebert.py""" ) with patch.object(SCREAMING_SNAKE_CASE_ ,"""argv""" ,SCREAMING_SNAKE_CASE_ ): snake_case : List[str] = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(SCREAMING_SNAKE_CASE_ ,0.6_66 ) @slow @require_torch_non_multi_gpu def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = """ --model_type roberta --model_name_or_path roberta-base --task_name MRPC --do_train --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --max_seq_length 128 --per_gpu_eval_batch_size=1 --per_gpu_train_batch_size=8 --learning_rate 2e-4 --num_train_epochs 3 --overwrite_output_dir --seed 42 --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --save_steps 0 --overwrite_cache --eval_after_first_stage """.split() self.run_and_check(SCREAMING_SNAKE_CASE_ ) snake_case : Any = """ --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --eval_each_highway --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 """.split() self.run_and_check(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = """ --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --early_exit_entropy 0.1 --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 """.split() self.run_and_check(SCREAMING_SNAKE_CASE_ )
36
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
36
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=snake_case ) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : str = field(default='''image-classification''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) __lowerCamelCase : ClassVar[Features] = Features({'''image''': Image()} ) __lowerCamelCase : ClassVar[Features] = Features({'''labels''': ClassLabel} ) __lowerCamelCase : str = "image" __lowerCamelCase : str = "labels" def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if self.label_column not in features: raise ValueError(F"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] ,SCREAMING_SNAKE_CASE_ ): raise ValueError(F"""Column {self.label_column} is not a ClassLabel.""" ) snake_case : Any = copy.deepcopy(self ) snake_case : List[str] = self.label_schema.copy() snake_case : Union[str, Any] = features[self.label_column] snake_case : List[str] = label_schema return task_template @property def snake_case_ ( self ): '''simple docstring''' return { self.image_column: "image", self.label_column: "labels", }
36
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
1
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent __lowercase : Union[str, Any] = {'''UserAgent''': UserAgent().random} def lowercase ( __A : Optional[Any] ) -> dict: '''simple docstring''' snake_case : str = script.contents[0] snake_case : List[str] = json.loads(data[data.find("""{\"config\"""" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Tuple = F"""https://www.instagram.com/{username}/""" snake_case : List[Any] = self.get_json() def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = requests.get(self.url ,headers=SCREAMING_SNAKE_CASE_ ).text snake_case : int = BeautifulSoup(SCREAMING_SNAKE_CASE_ ,"""html.parser""" ).find_all("""script""" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self ): '''simple docstring''' return F"""{self.__class__.__name__}('{self.username}')""" def __str__( self ): '''simple docstring''' return F"""{self.fullname} ({self.username}) is {self.biography}""" @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["username"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["full_name"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["biography"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["business_email"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["external_url"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["edge_followed_by"]["count"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["edge_follow"]["count"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["edge_owner_to_timeline_media"]["count"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["profile_pic_url_hd"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["is_verified"] @property def snake_case_ ( self ): '''simple docstring''' return self.user_data["is_private"] def lowercase ( __A : str = "github" ) -> None: '''simple docstring''' import os if os.environ.get("""CI""" ): return # test failing on GitHub Actions snake_case : List[str] = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 12_0000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "[email protected]" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("""https://instagram.""" ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() __lowercase : int = InstagramUser('''github''') print(instagram_user) print(f'''{instagram_user.number_of_posts = }''') print(f'''{instagram_user.number_of_followers = }''') print(f'''{instagram_user.number_of_followings = }''') print(f'''{instagram_user.email = }''') print(f'''{instagram_user.website = }''') print(f'''{instagram_user.profile_picture_url = }''') print(f'''{instagram_user.is_verified = }''') print(f'''{instagram_user.is_private = }''')
36
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
1
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( '''The `image_to_image.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionImg2ImgPipeline` instead.''' )
36
import warnings from ..trainer import Trainer from ..utils import logging __lowercase : str = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(args=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
import gc import random import tempfile import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline from diffusers.utils import floats_tensor, nightly, torch_device from diffusers.utils.testing_utils import require_torch_gpu class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def snake_case_ ( self ): '''simple docstring''' snake_case : str = 1 snake_case : str = 3 snake_case : Optional[Any] = (32, 32) snake_case : int = floats_tensor((batch_size, num_channels) + sizes ,rng=random.Random(0 ) ).to(SCREAMING_SNAKE_CASE_ ) return image @property def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : str = UNetaDConditionModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=4 ,out_channels=4 ,down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,cross_attention_dim=32 ,) return model @property def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : Dict = AutoencoderKL( block_out_channels=[32, 64] ,in_channels=3 ,out_channels=3 ,down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,latent_channels=4 ,) return model @property def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : Any = CLIPTextConfig( bos_token_id=0 ,eos_token_id=2 ,hidden_size=32 ,intermediate_size=37 ,layer_norm_eps=1E-05 ,num_attention_heads=4 ,num_hidden_layers=5 ,pad_token_id=1 ,vocab_size=1000 ,) return CLIPTextModel(SCREAMING_SNAKE_CASE_ ) @property def snake_case_ ( self ): '''simple docstring''' def extract(*SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): class _A : '''simple docstring''' def __init__( self ): '''simple docstring''' snake_case : Optional[Any] = torch.ones([0] ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' self.pixel_values.to(SCREAMING_SNAKE_CASE_ ) return self return Out() return extract def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case : Optional[Any] = self.dummy_cond_unet snake_case : Optional[int] = DDIMScheduler( beta_start=0.0_00_85 ,beta_end=0.0_12 ,beta_schedule="""scaled_linear""" ,clip_sample=SCREAMING_SNAKE_CASE_ ,set_alpha_to_one=SCREAMING_SNAKE_CASE_ ,) snake_case : Any = self.dummy_vae snake_case : List[str] = self.dummy_text_encoder snake_case : int = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) # make sure here that pndm scheduler skips prk snake_case : Dict = StableDiffusionPipeline( unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ,vae=SCREAMING_SNAKE_CASE_ ,text_encoder=SCREAMING_SNAKE_CASE_ ,tokenizer=SCREAMING_SNAKE_CASE_ ,safety_checker=SCREAMING_SNAKE_CASE_ ,feature_extractor=self.dummy_extractor ,) snake_case : int = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : str = """A painting of a squirrel eating a burger""" snake_case : List[Any] = torch.Generator(device=SCREAMING_SNAKE_CASE_ ).manual_seed(0 ) snake_case : Tuple = sd_pipe([prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type="""np""" ) snake_case : Any = output.images snake_case : Union[str, Any] = torch.Generator(device=SCREAMING_SNAKE_CASE_ ).manual_seed(0 ) snake_case : Dict = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type="""np""" ,return_dict=SCREAMING_SNAKE_CASE_ ,)[0] snake_case : Dict = image[0, -3:, -3:, -1] snake_case : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) snake_case : Any = np.array([0.57_56, 0.61_18, 0.50_05, 0.50_41, 0.54_71, 0.47_26, 0.49_76, 0.48_65, 0.48_64] ) 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 snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = """cpu""" # ensure determinism for the device-dependent torch.Generator snake_case : Optional[int] = self.dummy_cond_unet snake_case : Any = PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE_ ) snake_case : str = self.dummy_vae snake_case : Dict = self.dummy_text_encoder snake_case : Tuple = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) # make sure here that pndm scheduler skips prk snake_case : Dict = StableDiffusionPipeline( unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ,vae=SCREAMING_SNAKE_CASE_ ,text_encoder=SCREAMING_SNAKE_CASE_ ,tokenizer=SCREAMING_SNAKE_CASE_ ,safety_checker=SCREAMING_SNAKE_CASE_ ,feature_extractor=self.dummy_extractor ,) snake_case : Dict = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = """A painting of a squirrel eating a burger""" snake_case : Dict = torch.Generator(device=SCREAMING_SNAKE_CASE_ ).manual_seed(0 ) snake_case : Optional[int] = sd_pipe([prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type="""np""" ) snake_case : str = output.images snake_case : Dict = torch.Generator(device=SCREAMING_SNAKE_CASE_ ).manual_seed(0 ) snake_case : str = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type="""np""" ,return_dict=SCREAMING_SNAKE_CASE_ ,)[0] snake_case : int = image[0, -3:, -3:, -1] snake_case : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) snake_case : int = np.array([0.51_25, 0.57_16, 0.48_28, 0.50_60, 0.56_50, 0.47_68, 0.51_85, 0.48_95, 0.49_93] ) 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 snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = StableDiffusionPipeline.from_pretrained( """hf-internal-testing/tiny-stable-diffusion-lms-pipe""" ,safety_checker=SCREAMING_SNAKE_CASE_ ) assert isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) assert isinstance(pipe.scheduler ,SCREAMING_SNAKE_CASE_ ) assert pipe.safety_checker is None snake_case : str = pipe("""example prompt""" ,num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Any = StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_ ) # sanity check that the pipeline still works assert pipe.safety_checker is None snake_case : int = pipe("""example prompt""" ,num_inference_steps=2 ).images[0] assert image is not None @unittest.skipIf(torch_device != """cuda""" ,"""This test requires a GPU""" ) def snake_case_ ( self ): '''simple docstring''' snake_case : Any = self.dummy_cond_unet snake_case : Tuple = PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE_ ) snake_case : int = self.dummy_vae snake_case : int = self.dummy_text_encoder snake_case : Any = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) # put models in fp16 snake_case : Dict = unet.half() snake_case : Optional[int] = vae.half() snake_case : Any = bert.half() # make sure here that pndm scheduler skips prk snake_case : Any = StableDiffusionPipeline( unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ,vae=SCREAMING_SNAKE_CASE_ ,text_encoder=SCREAMING_SNAKE_CASE_ ,tokenizer=SCREAMING_SNAKE_CASE_ ,safety_checker=SCREAMING_SNAKE_CASE_ ,feature_extractor=self.dummy_extractor ,) snake_case : int = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = """A painting of a squirrel eating a burger""" snake_case : int = sd_pipe([prompt] ,num_inference_steps=2 ,output_type="""np""" ).images assert image.shape == (1, 64, 64, 3) @nightly @require_torch_gpu class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5""" ,safety_checker=SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) snake_case : Dict = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = ( """portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle""" """ coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with""" """ anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and""" """ children from bahnhof zoo, detailed """ ) snake_case : List[Any] = 4003660346 snake_case : Optional[Any] = 7 # without safety guidance (sld_guidance_scale = 0) snake_case : List[str] = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : int = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=SCREAMING_SNAKE_CASE_ ,num_inference_steps=50 ,output_type="""np""" ,width=512 ,height=512 ,sld_guidance_scale=0 ,) snake_case : Optional[Any] = output.images snake_case : Union[str, Any] = image[0, -3:, -3:, -1] snake_case : Optional[int] = [0.22_78, 0.22_31, 0.22_49, 0.23_33, 0.23_03, 0.18_85, 0.22_73, 0.21_44, 0.21_76] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 # without safety guidance (strong configuration) snake_case : int = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=SCREAMING_SNAKE_CASE_ ,num_inference_steps=50 ,output_type="""np""" ,width=512 ,height=512 ,sld_guidance_scale=2000 ,sld_warmup_steps=7 ,sld_threshold=0.0_25 ,sld_momentum_scale=0.5 ,sld_mom_beta=0.7 ,) snake_case : Dict = output.images snake_case : Optional[int] = image[0, -3:, -3:, -1] snake_case : str = [0.23_83, 0.22_76, 0.2_36, 0.21_92, 0.21_86, 0.20_53, 0.19_71, 0.19_01, 0.17_19] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5""" ,safety_checker=SCREAMING_SNAKE_CASE_ ) snake_case : int = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) snake_case : List[Any] = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = """padme amidala taking a bath artwork, safe for work, no nudity""" snake_case : Optional[Any] = 2734971755 snake_case : Dict = 7 snake_case : Tuple = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : int = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=SCREAMING_SNAKE_CASE_ ,num_inference_steps=50 ,output_type="""np""" ,width=512 ,height=512 ,sld_guidance_scale=0 ,) snake_case : Union[str, Any] = output.images snake_case : Union[str, Any] = image[0, -3:, -3:, -1] snake_case : Any = [0.35_02, 0.36_22, 0.33_96, 0.36_42, 0.34_78, 0.33_18, 0.35, 0.33_48, 0.32_97] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 snake_case : str = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=SCREAMING_SNAKE_CASE_ ,num_inference_steps=50 ,output_type="""np""" ,width=512 ,height=512 ,sld_guidance_scale=2000 ,sld_warmup_steps=7 ,sld_threshold=0.0_25 ,sld_momentum_scale=0.5 ,sld_mom_beta=0.7 ,) snake_case : str = output.images snake_case : Optional[Any] = image[0, -3:, -3:, -1] snake_case : Any = [0.55_31, 0.52_06, 0.48_95, 0.51_56, 0.51_82, 0.47_51, 0.48_02, 0.48_03, 0.44_43] assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def snake_case_ ( self ): '''simple docstring''' snake_case : Any = StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5""" ) snake_case : str = sd_pipe.to(SCREAMING_SNAKE_CASE_ ) sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = ( """the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.""" """ leyendecker""" ) snake_case : List[Any] = 1044355234 snake_case : Optional[Any] = 12 snake_case : Union[str, Any] = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : Dict = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=SCREAMING_SNAKE_CASE_ ,num_inference_steps=50 ,output_type="""np""" ,width=512 ,height=512 ,sld_guidance_scale=0 ,) snake_case : str = output.images snake_case : Optional[int] = image[0, -3:, -3:, -1] snake_case : Tuple = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-7 snake_case : str = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) snake_case : str = sd_pipe( [prompt] ,generator=SCREAMING_SNAKE_CASE_ ,guidance_scale=SCREAMING_SNAKE_CASE_ ,num_inference_steps=50 ,output_type="""np""" ,width=512 ,height=512 ,sld_guidance_scale=2000 ,sld_warmup_steps=7 ,sld_threshold=0.0_25 ,sld_momentum_scale=0.5 ,sld_mom_beta=0.7 ,) snake_case : Dict = output.images snake_case : Optional[Any] = image[0, -3:, -3:, -1] snake_case : Optional[Any] = np.array([0.58_18, 0.62_85, 0.68_35, 0.60_19, 0.6_25, 0.67_54, 0.60_96, 0.63_34, 0.65_61] ) assert image.shape == (1, 512, 512, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
36
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __lowercase : List[str] = ['''text''', '''image''', '''audio'''] def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[int] = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__A , __A ): inputs.append(create_inputs(__A ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def lowercase ( __A : List ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = [] for output in outputs: if isinstance(__A , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__A , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__A , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) snake_case : Dict = self.tool.inputs for _input in inputs: if isinstance(_input ,SCREAMING_SNAKE_CASE_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) snake_case : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = create_inputs(self.tool.inputs ) snake_case : str = self.tool(*SCREAMING_SNAKE_CASE_ ) # There is a single output if len(self.tool.outputs ) == 1: snake_case : Union[str, Any] = [outputs] self.assertListEqual(output_types(SCREAMING_SNAKE_CASE_ ) ,self.tool.outputs ) def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = create_inputs(self.tool.inputs ) snake_case : int = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) ) for output, output_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.outputs ): snake_case : List[str] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = create_inputs(self.tool.inputs ) snake_case : Any = [] for _input, input_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.inputs ): if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error snake_case : Tuple = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) )
36
1
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
1
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
1
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
1
import os import sys __lowercase : Union[str, Any] = os.path.join(os.path.dirname(__file__), '''src''') sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) __lowercase : str = [ '''torch''', '''numpy''', '''tokenizers''', '''filelock''', '''requests''', '''tqdm''', '''regex''', '''sentencepiece''', '''sacremoses''', '''importlib_metadata''', '''huggingface_hub''', ] @add_start_docstrings(AutoConfig.__doc__ ) def lowercase ( *__A : Optional[int] , **__A : Union[str, Any] ) -> Dict: '''simple docstring''' return AutoConfig.from_pretrained(*__A , **__A ) @add_start_docstrings(AutoTokenizer.__doc__ ) def lowercase ( *__A : List[Any] , **__A : str ) -> List[Any]: '''simple docstring''' return AutoTokenizer.from_pretrained(*__A , **__A ) @add_start_docstrings(AutoModel.__doc__ ) def lowercase ( *__A : Tuple , **__A : Optional[Any] ) -> int: '''simple docstring''' return AutoModel.from_pretrained(*__A , **__A ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def lowercase ( *__A : Any , **__A : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' return AutoModelForCausalLM.from_pretrained(*__A , **__A ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def lowercase ( *__A : Tuple , **__A : Dict ) -> Union[str, Any]: '''simple docstring''' return AutoModelForMaskedLM.from_pretrained(*__A , **__A ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def lowercase ( *__A : Optional[Any] , **__A : Tuple ) -> Optional[int]: '''simple docstring''' return AutoModelForSequenceClassification.from_pretrained(*__A , **__A ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def lowercase ( *__A : Union[str, Any] , **__A : int ) -> int: '''simple docstring''' return AutoModelForQuestionAnswering.from_pretrained(*__A , **__A )
36
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
1
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable __lowercase : Union[str, Any] = {'''configuration_dpt''': ['''DPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPTConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[str] = ['''DPTFeatureExtractor'''] __lowercase : Dict = ['''DPTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[Any] = [ '''DPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DPTForDepthEstimation''', '''DPTForSemanticSegmentation''', '''DPTModel''', '''DPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys __lowercase : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
1
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation def lowercase ( __A : List[Any] ) -> Any: '''simple docstring''' snake_case : Optional[Any] = 384 if "tiny" in model_name: snake_case : Optional[Any] = [3, 3, 9, 3] snake_case : Optional[int] = [96, 192, 384, 768] if "small" in model_name: snake_case : Union[str, Any] = [3, 3, 27, 3] snake_case : int = [96, 192, 384, 768] if "base" in model_name: snake_case : Optional[Any] = [3, 3, 27, 3] snake_case : int = [128, 256, 512, 1024] snake_case : Optional[int] = 512 if "large" in model_name: snake_case : Optional[Any] = [3, 3, 27, 3] snake_case : Any = [192, 384, 768, 1536] snake_case : int = 768 if "xlarge" in model_name: snake_case : Tuple = [3, 3, 27, 3] snake_case : Any = [256, 512, 1024, 2048] snake_case : Optional[int] = 1024 # set label information snake_case : Optional[int] = 150 snake_case : str = """huggingface/label-files""" snake_case : str = """ade20k-id2label.json""" snake_case : Any = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Dict = {int(__A ): v for k, v in idalabel.items()} snake_case : Any = {v: k for k, v in idalabel.items()} snake_case : int = ConvNextConfig( depths=__A , hidden_sizes=__A , out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] ) snake_case : Tuple = UperNetConfig( backbone_config=__A , auxiliary_in_channels=__A , num_labels=__A , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : List[str] ) -> Any: '''simple docstring''' snake_case : Union[str, Any] = [] # fmt: off # stem rename_keys.append(("""backbone.downsample_layers.0.0.weight""", """backbone.embeddings.patch_embeddings.weight""") ) rename_keys.append(("""backbone.downsample_layers.0.0.bias""", """backbone.embeddings.patch_embeddings.bias""") ) rename_keys.append(("""backbone.downsample_layers.0.1.weight""", """backbone.embeddings.layernorm.weight""") ) rename_keys.append(("""backbone.downsample_layers.0.1.bias""", """backbone.embeddings.layernorm.bias""") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f"""backbone.stages.{i}.{j}.gamma""", f"""backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.depthwise_conv.weight""", f"""backbone.encoder.stages.{i}.layers.{j}.dwconv.weight""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.depthwise_conv.bias""", f"""backbone.encoder.stages.{i}.layers.{j}.dwconv.bias""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.norm.weight""", f"""backbone.encoder.stages.{i}.layers.{j}.layernorm.weight""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.norm.bias""", f"""backbone.encoder.stages.{i}.layers.{j}.layernorm.bias""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.pointwise_conv1.weight""", f"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.pointwise_conv1.bias""", f"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.pointwise_conv2.weight""", f"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight""") ) rename_keys.append((f"""backbone.stages.{i}.{j}.pointwise_conv2.bias""", f"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias""") ) if i > 0: rename_keys.append((f"""backbone.downsample_layers.{i}.0.weight""", f"""backbone.encoder.stages.{i}.downsampling_layer.0.weight""") ) rename_keys.append((f"""backbone.downsample_layers.{i}.0.bias""", f"""backbone.encoder.stages.{i}.downsampling_layer.0.bias""") ) rename_keys.append((f"""backbone.downsample_layers.{i}.1.weight""", f"""backbone.encoder.stages.{i}.downsampling_layer.1.weight""") ) rename_keys.append((f"""backbone.downsample_layers.{i}.1.bias""", f"""backbone.encoder.stages.{i}.downsampling_layer.1.bias""") ) rename_keys.append((f"""backbone.norm{i}.weight""", f"""backbone.hidden_states_norms.stage{i+1}.weight""") ) rename_keys.append((f"""backbone.norm{i}.bias""", f"""backbone.hidden_states_norms.stage{i+1}.bias""") ) # decode head rename_keys.extend( [ ("""decode_head.conv_seg.weight""", """decode_head.classifier.weight"""), ("""decode_head.conv_seg.bias""", """decode_head.classifier.bias"""), ("""auxiliary_head.conv_seg.weight""", """auxiliary_head.classifier.weight"""), ("""auxiliary_head.conv_seg.bias""", """auxiliary_head.classifier.bias"""), ] ) # fmt: on return rename_keys def lowercase ( __A : Union[str, Any] , __A : List[Any] , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Optional[Any] = dct.pop(__A ) snake_case : List[Any] = val def lowercase ( __A : str , __A : Dict , __A : List[str] ) -> List[Any]: '''simple docstring''' snake_case : Tuple = { """upernet-convnext-tiny""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth""", """upernet-convnext-small""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth""", """upernet-convnext-base""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth""", """upernet-convnext-large""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth""", """upernet-convnext-xlarge""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth""", } snake_case : List[str] = model_name_to_url[model_name] snake_case : Optional[int] = torch.hub.load_state_dict_from_url(__A , map_location="""cpu""" )["""state_dict"""] snake_case : Optional[int] = get_upernet_config(__A ) snake_case : Optional[Any] = UperNetForSemanticSegmentation(__A ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): snake_case : Tuple = state_dict.pop(__A ) if "bn" in key: snake_case : Dict = key.replace("""bn""" , """batch_norm""" ) snake_case : Union[str, Any] = val # rename keys snake_case : str = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) model.load_state_dict(__A ) # verify on image snake_case : Optional[int] = """https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg""" snake_case : List[Any] = Image.open(requests.get(__A , stream=__A ).raw ).convert("""RGB""" ) snake_case : List[str] = SegformerImageProcessor() snake_case : Optional[int] = processor(__A , return_tensors="""pt""" ).pixel_values with torch.no_grad(): snake_case : Optional[Any] = model(__A ) if model_name == "upernet-convnext-tiny": snake_case : Any = torch.tensor( [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]] ) elif model_name == "upernet-convnext-small": snake_case : List[Any] = torch.tensor( [[-8.8_236, -8.8_236, -8.6_771], [-8.8_236, -8.8_236, -8.6_771], [-8.7_638, -8.7_638, -8.6_240]] ) elif model_name == "upernet-convnext-base": snake_case : Union[str, Any] = torch.tensor( [[-8.8_558, -8.8_558, -8.6_905], [-8.8_558, -8.8_558, -8.6_905], [-8.7_669, -8.7_669, -8.6_021]] ) elif model_name == "upernet-convnext-large": snake_case : Optional[Any] = torch.tensor( [[-8.6_660, -8.6_660, -8.6_210], [-8.6_660, -8.6_660, -8.6_210], [-8.6_310, -8.6_310, -8.5_964]] ) elif model_name == "upernet-convnext-xlarge": snake_case : Tuple = torch.tensor( [[-8.4_980, -8.4_980, -8.3_977], [-8.4_980, -8.4_980, -8.3_977], [-8.4_379, -8.4_379, -8.3_412]] ) print("""Logits:""" , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , __A , atol=1E-4 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(f"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) print(f"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model and processor for {model_name} to hub""" ) model.push_to_hub(f"""openmmlab/{model_name}""" ) processor.push_to_hub(f"""openmmlab/{model_name}""" ) if __name__ == "__main__": __lowercase : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''upernet-convnext-tiny''', type=str, choices=[f'''upernet-convnext-{size}''' for size in ['''tiny''', '''small''', '''base''', '''large''', '''xlarge''']], help='''Name of the ConvNext UperNet model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) __lowercase : Tuple = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
1
import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput __lowercase : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name def lowercase ( __A : Union[List, PIL.Image.Image, torch.Tensor] ) -> Any: '''simple docstring''' warnings.warn( """The preprocess method is deprecated and will be removed in a future version. Please""" """ use VaeImageProcessor.preprocess instead""" , __A , ) if isinstance(__A , torch.Tensor ): return image elif isinstance(__A , PIL.Image.Image ): snake_case : Optional[int] = [image] if isinstance(image[0] , PIL.Image.Image ): snake_case , snake_case : Optional[Any] = image[0].size snake_case , snake_case : Union[str, Any] = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 snake_case : List[str] = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION["""lanczos"""] ) )[None, :] for i in image] snake_case : Tuple = np.concatenate(__A , axis=0 ) snake_case : str = np.array(__A ).astype(np.floataa ) / 255.0 snake_case : Any = image.transpose(0 , 3 , 1 , 2 ) snake_case : List[str] = 2.0 * image - 1.0 snake_case : int = torch.from_numpy(__A ) elif isinstance(image[0] , torch.Tensor ): snake_case : Tuple = torch.cat(__A , dim=0 ) return image def lowercase ( __A : Union[List, PIL.Image.Image, torch.Tensor] ) -> Optional[int]: '''simple docstring''' if isinstance(__A , torch.Tensor ): return mask elif isinstance(__A , PIL.Image.Image ): snake_case : int = [mask] if isinstance(mask[0] , PIL.Image.Image ): snake_case , snake_case : List[str] = mask[0].size snake_case , snake_case : Any = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 snake_case : Optional[int] = [np.array(m.convert("""L""" ).resize((w, h) , resample=PIL_INTERPOLATION["""nearest"""] ) )[None, :] for m in mask] snake_case : List[Any] = np.concatenate(__A , axis=0 ) snake_case : Optional[Any] = mask.astype(np.floataa ) / 255.0 snake_case : Dict = 0 snake_case : List[Any] = 1 snake_case : Dict = torch.from_numpy(__A ) elif isinstance(mask[0] , torch.Tensor ): snake_case : List[Any] = torch.cat(__A , dim=0 ) return mask class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : UNetaDModel __lowerCamelCase : RePaintScheduler def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() self.register_modules(unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ) @torch.no_grad() def __call__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 250 ,SCREAMING_SNAKE_CASE_ = 0.0 ,SCREAMING_SNAKE_CASE_ = 10 ,SCREAMING_SNAKE_CASE_ = 10 ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = "pil" ,SCREAMING_SNAKE_CASE_ = True ,): '''simple docstring''' snake_case : Optional[int] = image snake_case : int = _preprocess_image(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = original_image.to(device=self.device ,dtype=self.unet.dtype ) snake_case : int = _preprocess_mask(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = mask_image.to(device=self.device ,dtype=self.unet.dtype ) snake_case : Optional[int] = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) and len(SCREAMING_SNAKE_CASE_ ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(SCREAMING_SNAKE_CASE_ )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) snake_case : int = original_image.shape snake_case : str = randn_tensor(SCREAMING_SNAKE_CASE_ ,generator=SCREAMING_SNAKE_CASE_ ,device=self.device ,dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,self.device ) snake_case : int = eta snake_case : Any = self.scheduler.timesteps[0] + 1 snake_case : int = generator[0] if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual snake_case : List[Any] = self.unet(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ).sample # compute previous image: x_t -> x_t-1 snake_case : Tuple = self.scheduler.step(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ).prev_sample else: # compute the reverse: x_t-1 -> x_t snake_case : Optional[Any] = self.scheduler.undo_step(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = t snake_case : Any = (image / 2 + 0.5).clamp(0 ,1 ) snake_case : Optional[Any] = image.cpu().permute(0 ,2 ,3 ,1 ).numpy() if output_type == "pil": snake_case : Optional[int] = self.numpy_to_pil(SCREAMING_SNAKE_CASE_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE_ )
36
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
1
from manim import * class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' snake_case : Any = Rectangle(height=0.5 ,width=0.5 ) snake_case : Tuple = Rectangle(height=0.46 ,width=0.46 ).set_stroke(width=0 ) snake_case : Optional[int] = [mem.copy() for i in range(6 )] snake_case : Optional[int] = [mem.copy() for i in range(6 )] snake_case : Any = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0 ) snake_case : Tuple = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0 ) snake_case : List[str] = VGroup(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0 ) snake_case : str = Text("""CPU""" ,font_size=24 ) snake_case : List[str] = Group(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0.5 ,aligned_edge=SCREAMING_SNAKE_CASE_ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(SCREAMING_SNAKE_CASE_ ) snake_case : int = [mem.copy() for i in range(1 )] snake_case : int = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0 ) snake_case : List[Any] = Text("""GPU""" ,font_size=24 ) snake_case : Dict = Group(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0.5 ,aligned_edge=SCREAMING_SNAKE_CASE_ ) gpu.align_to(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) gpu.set_x(gpu.get_x() - 1 ) self.add(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = [mem.copy() for i in range(6 )] snake_case : int = VGroup(*SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0 ) snake_case : int = Text("""Model""" ,font_size=24 ) snake_case : Any = Group(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ).arrange(SCREAMING_SNAKE_CASE_ ,buff=0.5 ,aligned_edge=SCREAMING_SNAKE_CASE_ ) model.move_to([3, -1.0, 0] ) self.play( Create(SCREAMING_SNAKE_CASE_ ,run_time=1 ) ,Create(SCREAMING_SNAKE_CASE_ ,run_time=1 ) ,Create(SCREAMING_SNAKE_CASE_ ,run_time=1 ) ,) snake_case : Dict = MarkupText( F"""First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM.""" ,font_size=24 ,) snake_case : Optional[int] = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) snake_case : Tuple = MarkupText( F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model""" ,font_size=18 ,) key_text.move_to([-5, 2.4, 0] ) step_a.move_to([2, 2, 0] ) self.play(Write(SCREAMING_SNAKE_CASE_ ,run_time=2.5 ) ,Write(SCREAMING_SNAKE_CASE_ ) ,Write(SCREAMING_SNAKE_CASE_ ) ) self.add(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = [] snake_case : int = [] snake_case : Optional[Any] = [] for i, rect in enumerate(SCREAMING_SNAKE_CASE_ ): snake_case : Optional[int] = Rectangle(height=0.46 ,width=0.46 ).set_stroke(width=0.0 ).set_fill(SCREAMING_SNAKE_CASE_ ,opacity=0.7 ) cpu_target.move_to(SCREAMING_SNAKE_CASE_ ) cpu_target.generate_target() snake_case : str = 0.46 / 4 snake_case : Dict = 0.46 / 3 if i == 0: cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) ,buff=0.02 ,direction=SCREAMING_SNAKE_CASE_ ) cpu_target.target.set_x(cpu_target.target.get_x() + 0.1 ) elif i == 3: cpu_target.target.next_to(cpu_targs[0].target ,direction=SCREAMING_SNAKE_CASE_ ,buff=0.0 ) else: cpu_target.target.next_to(cpu_targs[i - 1].target ,direction=SCREAMING_SNAKE_CASE_ ,buff=0.0 ) cpu_targs.append(SCREAMING_SNAKE_CASE_ ) first_animations.append(rect.animate(run_time=0.5 ).set_stroke(SCREAMING_SNAKE_CASE_ ) ) second_animations.append(MoveToTarget(SCREAMING_SNAKE_CASE_ ,run_time=1.5 ) ) self.play(*SCREAMING_SNAKE_CASE_ ) self.play(*SCREAMING_SNAKE_CASE_ ) self.wait()
36
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
1
import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() __lowercase : Tuple = logging.get_logger('''transformers.models.speecht5''') def lowercase ( __A : Dict , __A : Union[str, Any] , __A : int ) -> Any: '''simple docstring''' hf_model.apply_weight_norm() snake_case : Any = checkpoint["""input_conv.weight_g"""] snake_case : Any = checkpoint["""input_conv.weight_v"""] snake_case : Optional[Any] = checkpoint["""input_conv.bias"""] for i in range(len(config.upsample_rates ) ): snake_case : Optional[Any] = checkpoint[f"""upsamples.{i}.1.weight_g"""] snake_case : List[str] = checkpoint[f"""upsamples.{i}.1.weight_v"""] snake_case : Any = checkpoint[f"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): snake_case : Any = checkpoint[f"""blocks.{i}.convs1.{j}.1.weight_g"""] snake_case : Union[str, Any] = checkpoint[f"""blocks.{i}.convs1.{j}.1.weight_v"""] snake_case : str = checkpoint[f"""blocks.{i}.convs1.{j}.1.bias"""] snake_case : Any = checkpoint[f"""blocks.{i}.convs2.{j}.1.weight_g"""] snake_case : List[Any] = checkpoint[f"""blocks.{i}.convs2.{j}.1.weight_v"""] snake_case : str = checkpoint[f"""blocks.{i}.convs2.{j}.1.bias"""] snake_case : Union[str, Any] = checkpoint["""output_conv.1.weight_g"""] snake_case : Tuple = checkpoint["""output_conv.1.weight_v"""] snake_case : Dict = checkpoint["""output_conv.1.bias"""] hf_model.remove_weight_norm() @torch.no_grad() def lowercase ( __A : List[Any] , __A : Optional[Any] , __A : List[str] , __A : Optional[Any]=None , __A : List[str]=None , ) -> str: '''simple docstring''' if config_path is not None: snake_case : str = SpeechTaHifiGanConfig.from_pretrained(__A ) else: snake_case : Tuple = SpeechTaHifiGanConfig() snake_case : Optional[Any] = SpeechTaHifiGan(__A ) snake_case : str = torch.load(__A ) load_weights(orig_checkpoint["""model"""]["""generator"""] , __A , __A ) snake_case : List[Any] = np.load(__A ) snake_case : Tuple = stats[0].reshape(-1 ) snake_case : str = stats[1].reshape(-1 ) snake_case : Optional[int] = torch.from_numpy(__A ).float() snake_case : Tuple = torch.from_numpy(__A ).float() model.save_pretrained(__A ) if repo_id: print("""Pushing to the hub...""" ) model.push_to_hub(__A ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to original checkpoint''') parser.add_argument('''--stats_path''', required=True, default=None, type=str, help='''Path to stats.npy file''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) __lowercase : List[str] = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
1
from typing import TYPE_CHECKING from ...utils import _LazyModule __lowercase : Union[str, Any] = {'''tokenization_bertweet''': ['''BertweetTokenizer''']} if TYPE_CHECKING: from .tokenization_bertweet import BertweetTokenizer else: import sys __lowercase : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
import torch import torch.nn as nn from transformers.modeling_utils import ModuleUtilsMixin from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class _A ( snake_case , snake_case , snake_case ): '''simple docstring''' @register_to_config def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' super().__init__() snake_case : Union[str, Any] = nn.Embedding(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = nn.Embedding(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = False snake_case : List[str] = nn.Dropout(p=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = TaConfig( vocab_size=SCREAMING_SNAKE_CASE_ ,d_model=SCREAMING_SNAKE_CASE_ ,num_heads=SCREAMING_SNAKE_CASE_ ,d_kv=SCREAMING_SNAKE_CASE_ ,d_ff=SCREAMING_SNAKE_CASE_ ,dropout_rate=SCREAMING_SNAKE_CASE_ ,feed_forward_proj=SCREAMING_SNAKE_CASE_ ,is_decoder=SCREAMING_SNAKE_CASE_ ,is_encoder_decoder=SCREAMING_SNAKE_CASE_ ,) snake_case : str = nn.ModuleList() for lyr_num in range(SCREAMING_SNAKE_CASE_ ): snake_case : Any = TaBlock(SCREAMING_SNAKE_CASE_ ) self.encoders.append(SCREAMING_SNAKE_CASE_ ) snake_case : int = TaLayerNorm(SCREAMING_SNAKE_CASE_ ) snake_case : Dict = nn.Dropout(p=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = self.token_embedder(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = encoder_input_tokens.shape[1] snake_case : str = torch.arange(SCREAMING_SNAKE_CASE_ ,device=encoder_input_tokens.device ) x += self.position_encoding(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = self.dropout_pre(SCREAMING_SNAKE_CASE_ ) # inverted the attention mask snake_case : List[str] = encoder_input_tokens.size() snake_case : List[str] = self.get_extended_attention_mask(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for lyr in self.encoders: snake_case : Tuple = lyr(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )[0] snake_case : Optional[int] = self.layer_norm(SCREAMING_SNAKE_CASE_ ) return self.dropout_post(SCREAMING_SNAKE_CASE_ ), encoder_inputs_mask
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
def lowercase ( __A : int ) -> bool: '''simple docstring''' return str(__A ) == str(__A )[::-1] def lowercase ( __A : int ) -> int: '''simple docstring''' return int(__A ) + int(str(__A )[::-1] ) def lowercase ( __A : int = 1_0000 ) -> int: '''simple docstring''' snake_case : str = [] for num in range(1 , __A ): snake_case : Union[str, Any] = 0 snake_case : str = num while iterations < 50: snake_case : int = sum_reverse(__A ) iterations += 1 if is_palindrome(__A ): break else: lychrel_nums.append(__A ) return len(__A ) if __name__ == "__main__": print(f'''{solution() = }''')
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
__lowercase : dict[tuple[int, int, int], int] = {} def lowercase ( __A : int , __A : int , __A : int ) -> int: '''simple docstring''' if late == 3 or absent == 2: return 0 # if we have no days left, and have not failed any other rules, # we have a prize string if days == 0: return 1 # No easy solution, so now we need to do the recursive calculation # First, check if the combination is already in the cache, and # if yes, return the stored value from there since we already # know the number of possible prize strings from this point on snake_case : Optional[Any] = (days, absent, late) if key in cache: return cache[key] # now we calculate the three possible ways that can unfold from # this point on, depending on our attendance today # 1) if we are late (but not absent), the "absent" counter stays as # it is, but the "late" counter increases by one snake_case : Union[str, Any] = _calculate(days - 1 , __A , late + 1 ) # 2) if we are absent, the "absent" counter increases by 1, and the # "late" counter resets to 0 snake_case : str = _calculate(days - 1 , absent + 1 , 0 ) # 3) if we are on time, this resets the "late" counter and keeps the # absent counter snake_case : List[str] = _calculate(days - 1 , __A , 0 ) snake_case : List[str] = state_late + state_absent + state_ontime snake_case : List[Any] = prizestrings return prizestrings def lowercase ( __A : int = 30 ) -> int: '''simple docstring''' return _calculate(__A , absent=0 , late=0 ) if __name__ == "__main__": print(solution())
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
from argparse import ArgumentParser from ..pipelines import Pipeline, PipelineDataFormat, get_supported_tasks, pipeline from ..utils import logging from . import BaseTransformersCLICommand __lowercase : str = logging.get_logger(__name__) # pylint: disable=invalid-name def lowercase ( __A : str ) -> Dict: '''simple docstring''' if not path: return "pipe" for ext in PipelineDataFormat.SUPPORTED_FORMATS: if path.endswith(__A ): return ext raise Exception( f"""Unable to determine file format from file extension {path}. """ f"""Please provide the format through --format {PipelineDataFormat.SUPPORTED_FORMATS}""" ) def lowercase ( __A : Optional[Any] ) -> Tuple: '''simple docstring''' snake_case : Optional[Any] = pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) snake_case : Optional[int] = try_infer_format_from_ext(args.input ) if args.format == """infer""" else args.format snake_case : Any = PipelineDataFormat.from_str( format=__A , output_path=args.output , input_path=args.input , column=args.column if args.column else nlp.default_input_names , overwrite=args.overwrite , ) return RunCommand(__A , __A ) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = nlp snake_case : int = reader @staticmethod def snake_case_ ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = parser.add_parser("""run""" ,help="""Run a pipeline through the CLI""" ) run_parser.add_argument("""--task""" ,choices=get_supported_tasks() ,help="""Task to run""" ) run_parser.add_argument("""--input""" ,type=SCREAMING_SNAKE_CASE_ ,help="""Path to the file to use for inference""" ) run_parser.add_argument("""--output""" ,type=SCREAMING_SNAKE_CASE_ ,help="""Path to the file that will be used post to write results.""" ) run_parser.add_argument("""--model""" ,type=SCREAMING_SNAKE_CASE_ ,help="""Name or path to the model to instantiate.""" ) run_parser.add_argument("""--config""" ,type=SCREAMING_SNAKE_CASE_ ,help="""Name or path to the model's config to instantiate.""" ) run_parser.add_argument( """--tokenizer""" ,type=SCREAMING_SNAKE_CASE_ ,help="""Name of the tokenizer to use. (default: same as the model name)""" ) run_parser.add_argument( """--column""" ,type=SCREAMING_SNAKE_CASE_ ,help="""Name of the column to use as input. (For multi columns input as QA use column1,columns2)""" ,) run_parser.add_argument( """--format""" ,type=SCREAMING_SNAKE_CASE_ ,default="""infer""" ,choices=PipelineDataFormat.SUPPORTED_FORMATS ,help="""Input format to read from""" ,) run_parser.add_argument( """--device""" ,type=SCREAMING_SNAKE_CASE_ ,default=-1 ,help="""Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)""" ,) run_parser.add_argument("""--overwrite""" ,action="""store_true""" ,help="""Allow overwriting the output file.""" ) run_parser.set_defaults(func=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case , snake_case : Tuple = self._nlp, [] for entry in self._reader: snake_case : Any = nlp(**SCREAMING_SNAKE_CASE_ ) if self._reader.is_multi_columns else nlp(SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): outputs.append(SCREAMING_SNAKE_CASE_ ) else: outputs += output # Saving data if self._nlp.binary_output: snake_case : Any = self._reader.save_binary(SCREAMING_SNAKE_CASE_ ) logger.warning(F"""Current pipeline requires output to be in binary format, saving at {binary_path}""" ) else: self._reader.save(SCREAMING_SNAKE_CASE_ )
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) 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()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1
import math import random def lowercase ( __A : float , __A : bool = False ) -> float: '''simple docstring''' if deriv: return value * (1 - value) return 1 / (1 + math.exp(-value )) # Initial Value __lowercase : Optional[int] = 0.02 def lowercase ( __A : int , __A : int ) -> float: '''simple docstring''' snake_case : Tuple = float(2 * (random.randint(1 , 100 )) - 1 ) for _ in range(__A ): # Forward propagation snake_case : Optional[int] = sigmoid_function(INITIAL_VALUE * weight ) # How much did we miss? snake_case : Tuple = (expected / 100) - layer_a # Error delta snake_case : str = layer_1_error * sigmoid_function(__A , __A ) # Update weight weight += INITIAL_VALUE * layer_1_delta return layer_a * 100 if __name__ == "__main__": import doctest doctest.testmod() __lowercase : Dict = int(input('''Expected value: ''')) __lowercase : Optional[int] = int(input('''Number of propagations: ''')) print(forward_propagation(expected, number_propagations))
36
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
1
import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[Any] = { '''facebook/encodec_24khz''': '''https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json''', '''facebook/encodec_48khz''': '''https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''encodec''' def __init__( self ,SCREAMING_SNAKE_CASE_=[1.5, 3.0, 6.0, 12.0, 24.0] ,SCREAMING_SNAKE_CASE_=24000 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=[8, 5, 4, 2] ,SCREAMING_SNAKE_CASE_="weight_norm" ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_="reflect" ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Optional[Any] = target_bandwidths snake_case : Tuple = sampling_rate snake_case : str = audio_channels snake_case : int = normalize snake_case : Tuple = chunk_length_s snake_case : Tuple = overlap snake_case : Tuple = hidden_size snake_case : Dict = num_filters snake_case : List[Any] = num_residual_layers snake_case : Optional[Any] = upsampling_ratios snake_case : Union[str, Any] = norm_type snake_case : List[Any] = kernel_size snake_case : List[str] = last_kernel_size snake_case : List[Any] = residual_kernel_size snake_case : int = dilation_growth_rate snake_case : str = use_causal_conv snake_case : Tuple = pad_mode snake_case : int = compress snake_case : str = num_lstm_layers snake_case : List[Any] = trim_right_ratio snake_case : Any = codebook_size snake_case : List[Any] = codebook_dim if codebook_dim is not None else hidden_size snake_case : Dict = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( F"""self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}""" ) super().__init__(**SCREAMING_SNAKE_CASE_ ) @property def snake_case_ ( self ): '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def snake_case_ ( self ): '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 ,int((1.0 - self.overlap) * self.chunk_length ) ) @property def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def snake_case_ ( self ): '''simple docstring''' return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
36
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 __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''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 _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
1
import inspect import unittest class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' try: import diffusers # noqa: F401 except ImportError: assert False def snake_case_ ( self ): '''simple docstring''' import diffusers from diffusers.dependency_versions_table import deps snake_case : str = inspect.getmembers(SCREAMING_SNAKE_CASE_ ,inspect.isclass ) for cls_name, cls_module in all_classes: if "dummy_" in cls_module.__module__: for backend in cls_module._backends: if backend == "k_diffusion": snake_case : Dict = """k-diffusion""" elif backend == "invisible_watermark": snake_case : Union[str, Any] = """invisible-watermark""" assert backend in deps, F"""{backend} is not in the deps table!"""
36
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''decision_transformer''' __lowerCamelCase : Optional[Any] = ['''past_key_values'''] __lowerCamelCase : Tuple = { '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=17 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="relu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Any = state_dim snake_case : Optional[Any] = act_dim snake_case : Union[str, Any] = hidden_size snake_case : Any = max_ep_len snake_case : int = action_tanh snake_case : Any = vocab_size snake_case : Any = n_positions snake_case : List[str] = n_layer snake_case : int = n_head snake_case : Optional[int] = n_inner snake_case : List[Any] = activation_function snake_case : Tuple = resid_pdrop snake_case : Optional[Any] = embd_pdrop snake_case : Dict = attn_pdrop snake_case : List[str] = layer_norm_epsilon snake_case : Union[str, Any] = initializer_range snake_case : Optional[Any] = scale_attn_weights snake_case : str = use_cache snake_case : int = scale_attn_by_inverse_layer_idx snake_case : Tuple = reorder_and_upcast_attn snake_case : Tuple = bos_token_id snake_case : List[str] = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class _A ( snake_case , snake_case ): '''simple docstring''' @register_to_config def __init__( self ,*, SCREAMING_SNAKE_CASE_ = 4 ,SCREAMING_SNAKE_CASE_ = 768 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__() snake_case : Tuple = nn.Parameter(torch.zeros(SCREAMING_SNAKE_CASE_ ) ) # parameters for additional clip time embeddings snake_case : Optional[Any] = nn.Linear(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Dict = nn.Linear(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # parameters for encoder hidden states snake_case : List[Any] = clip_extra_context_tokens snake_case : Dict = nn.Linear( SCREAMING_SNAKE_CASE_ ,self.clip_extra_context_tokens * cross_attention_dim ) snake_case : str = nn.Linear(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,*, SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings snake_case : Optional[Any] = image_embeddings.shape[0] snake_case : List[Any] = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) snake_case : Tuple = classifier_free_guidance_embeddings.expand( SCREAMING_SNAKE_CASE_ ,-1 ) snake_case : List[Any] = torch.cat([classifier_free_guidance_embeddings, image_embeddings] ,dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] snake_case : Optional[int] = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... snake_case : List[Any] = self.embedding_proj(SCREAMING_SNAKE_CASE_ ) snake_case : Any = self.clip_image_embeddings_project_to_time_embeddings(SCREAMING_SNAKE_CASE_ ) snake_case : Any = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" snake_case : Union[str, Any] = self.clip_extra_context_tokens_proj(SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = clip_extra_context_tokens.reshape(SCREAMING_SNAKE_CASE_ ,-1 ,self.clip_extra_context_tokens ) snake_case : Dict = clip_extra_context_tokens.permute(0 ,2 ,1 ) snake_case : str = self.encoder_hidden_states_proj(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = self.text_encoder_hidden_states_norm(SCREAMING_SNAKE_CASE_ ) snake_case : str = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] ,dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
36
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
36
1
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class _A ( unittest.TestCase ): '''simple docstring''' __lowerCamelCase : int = JukeboxTokenizer __lowerCamelCase : int = { '''artist''': '''Zac Brown Band''', '''genres''': '''Country''', '''lyrics''': '''I met a traveller from an antique land, Who said "Two vast and trunkless legs of stone Stand in the desert. . . . Near them, on the sand, Half sunk a shattered visage lies, whose frown, And wrinkled lip, and sneer of cold command, Tell that its sculptor well those passions read Which yet survive, stamped on these lifeless things, The hand that mocked them, and the heart that fed; And on the pedestal, these words appear: My name is Ozymandias, King of Kings; Look on my Works, ye Mighty, and despair! Nothing beside remains. Round the decay Of that colossal Wreck, boundless and bare The lone and level sands stretch far away ''', } @require_torch def snake_case_ ( self ): '''simple docstring''' import torch snake_case : Optional[Any] = JukeboxTokenizer.from_pretrained("""openai/jukebox-1b-lyrics""" ) snake_case : List[Any] = tokenizer(**self.metas )["""input_ids"""] # fmt: off snake_case : int = [ torch.tensor([[ 0, 0, 0, 7169, 507, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), torch.tensor([[0, 0, 0, 1069, 11]] ), ] # 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 snake_case_ ( self ): '''simple docstring''' import torch snake_case : str = JukeboxTokenizer.from_pretrained("""openai/jukebox-5b-lyrics""" ) snake_case : Any = tokenizer(**self.metas )["""input_ids"""] # fmt: off snake_case : Tuple = [ torch.tensor([[ 0, 0, 0, 1069, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 1069, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1069, 11, -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] ) )
36
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
1
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[Any] = logging.get_logger(__name__) __lowercase : Dict = { '''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 _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''xlnet''' __lowerCamelCase : Tuple = ['''mems'''] __lowerCamelCase : List[Any] = { '''n_token''': '''vocab_size''', # Backward compatibility '''hidden_size''': '''d_model''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=32000 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=24 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_="bi" ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=-1 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_="last" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_="tanh" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=5 ,SCREAMING_SNAKE_CASE_=5 ,SCREAMING_SNAKE_CASE_=5 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=2 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = vocab_size snake_case : List[str] = d_model snake_case : Optional[int] = n_layer snake_case : List[str] = 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})""" ) snake_case : Dict = d_model // n_head snake_case : Any = ff_activation snake_case : Optional[int] = d_inner snake_case : Optional[int] = untie_r snake_case : Optional[int] = attn_type snake_case : Any = initializer_range snake_case : Union[str, Any] = layer_norm_eps snake_case : Union[str, Any] = dropout snake_case : int = mem_len snake_case : List[str] = reuse_len snake_case : List[Any] = bi_data snake_case : Dict = clamp_len snake_case : int = same_length snake_case : Any = summary_type snake_case : Union[str, Any] = summary_use_proj snake_case : int = summary_activation snake_case : List[Any] = summary_last_dropout snake_case : List[str] = start_n_top snake_case : Optional[int] = end_n_top snake_case : Union[str, Any] = bos_token_id snake_case : Any = pad_token_id snake_case : str = 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.""" ,SCREAMING_SNAKE_CASE_ ,) snake_case : int = kwargs["""use_cache"""] snake_case : str = use_mems_eval snake_case : Optional[int] = use_mems_train super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) @property def snake_case_ ( self ): '''simple docstring''' logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" ) return -1 @max_position_embeddings.setter def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' # 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.""" )
36
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) __lowercase : List[Any] = { '''configuration_owlvit''': [ '''OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OwlViTConfig''', '''OwlViTOnnxConfig''', '''OwlViTTextConfig''', '''OwlViTVisionConfig''', ], '''processing_owlvit''': ['''OwlViTProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Any = ['''OwlViTFeatureExtractor'''] __lowercase : str = ['''OwlViTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[Any] = [ '''OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''OwlViTModel''', '''OwlViTPreTrainedModel''', '''OwlViTTextModel''', '''OwlViTVisionModel''', '''OwlViTForObjectDetection''', ] if TYPE_CHECKING: from .configuration_owlvit import ( OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, OwlViTConfig, OwlViTOnnxConfig, OwlViTTextConfig, OwlViTVisionConfig, ) from .processing_owlvit import OwlViTProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_owlvit import OwlViTFeatureExtractor from .image_processing_owlvit import OwlViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_owlvit import ( OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST, OwlViTForObjectDetection, OwlViTModel, OwlViTPreTrainedModel, OwlViTTextModel, OwlViTVisionModel, ) else: import sys __lowercase : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
import warnings from ..trainer import Trainer from ..utils import logging __lowercase : str = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(args=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def lowercase ( __A : Tuple , __A : Tuple ) -> List[Any]: '''simple docstring''' assert isinstance(__A , __A ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""" , [False, True] ) def lowercase ( __A : Union[str, Any] , __A : int , __A : Any , __A : Any ) -> Any: '''simple docstring''' snake_case : str = tmp_path / """cache""" snake_case : Union[str, Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): snake_case : Any = SqlDatasetReader( """dataset""" , """sqlite:///""" + sqlite_path , cache_dir=__A , keep_in_memory=__A ).read() _check_sql_dataset(__A , __A ) @require_sqlalchemy @pytest.mark.parametrize( """features""" , [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ] , ) def lowercase ( __A : int , __A : str , __A : Optional[Any] , __A : int ) -> int: '''simple docstring''' snake_case : Optional[int] = tmp_path / """cache""" snake_case : List[Any] = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} snake_case : List[Any] = features.copy() if features else default_expected_features snake_case : Any = ( Features({feature: Value(__A ) for feature, dtype in features.items()} ) if features is not None else None ) snake_case : List[str] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , features=__A , cache_dir=__A ).read() _check_sql_dataset(__A , __A ) def lowercase ( __A : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' with contextlib.closing(sqlitea.connect(__A ) ) as con: snake_case : int = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def lowercase ( __A : Dict , __A : Any , __A : Dict ) -> int: '''simple docstring''' snake_case : Tuple = tmp_path / """cache""" snake_case : List[Any] = os.path.join(__A , """tmp.sql""" ) snake_case : Optional[int] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=__A ).read() SqlDatasetWriter(__A , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=1 ).write() snake_case : Any = iter_sql_file(__A ) snake_case : Tuple = iter_sql_file(__A ) for rowa, rowa in zip(__A , __A ): assert rowa == rowa @require_sqlalchemy def lowercase ( __A : List[Any] , __A : str , __A : Optional[Any] ) -> str: '''simple docstring''' snake_case : List[Any] = tmp_path / """cache""" snake_case : Tuple = os.path.join(__A , """tmp.sql""" ) snake_case : Tuple = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=__A ).read() SqlDatasetWriter(__A , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=2 ).write() snake_case : str = iter_sql_file(__A ) snake_case : Optional[int] = iter_sql_file(__A ) for rowa, rowa in zip(__A , __A ): assert rowa == rowa @require_sqlalchemy def lowercase ( __A : Optional[int] , __A : Dict , __A : Dict ) -> Tuple: '''simple docstring''' snake_case : Optional[Any] = tmp_path / """cache""" snake_case : Dict = os.path.join(__A , """tmp.sql""" ) snake_case : Optional[Any] = SqlDatasetReader("""dataset""" , """sqlite:///""" + sqlite_path , cache_dir=__A ).read() with pytest.raises(__A ): SqlDatasetWriter(__A , """dataset""" , """sqlite:///""" + output_sqlite_path , num_proc=0 ).write()
36
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __lowercase : List[str] = ['''text''', '''image''', '''audio'''] def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[int] = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__A , __A ): inputs.append(create_inputs(__A ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def lowercase ( __A : List ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = [] for output in outputs: if isinstance(__A , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__A , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__A , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) snake_case : Dict = self.tool.inputs for _input in inputs: if isinstance(_input ,SCREAMING_SNAKE_CASE_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) snake_case : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = create_inputs(self.tool.inputs ) snake_case : str = self.tool(*SCREAMING_SNAKE_CASE_ ) # There is a single output if len(self.tool.outputs ) == 1: snake_case : Union[str, Any] = [outputs] self.assertListEqual(output_types(SCREAMING_SNAKE_CASE_ ) ,self.tool.outputs ) def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = create_inputs(self.tool.inputs ) snake_case : int = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) ) for output, output_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.outputs ): snake_case : List[str] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = create_inputs(self.tool.inputs ) snake_case : Any = [] for _input, input_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.inputs ): if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error snake_case : Tuple = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) )
36
1
from .constants import ( MODEL_NAME, OPTIMIZER_NAME, RNG_STATE_NAME, SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, SCALER_NAME, SCHEDULER_NAME, TORCH_LAUNCH_PARAMS, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ) from .dataclasses import ( BnbQuantizationConfig, ComputeEnvironment, CustomDtype, DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, DynamoBackend, FPaRecipeKwargs, FullyShardedDataParallelPlugin, GradientAccumulationPlugin, GradScalerKwargs, InitProcessGroupKwargs, KwargsHandler, LoggerType, MegatronLMPlugin, PrecisionType, ProjectConfiguration, RNGType, SageMakerDistributedType, TensorInformation, TorchDynamoPlugin, ) from .environment import get_int_from_env, parse_choice_from_env, parse_flag_from_env from .imports import ( get_ccl_version, is_abit_bnb_available, is_abit_bnb_available, is_aim_available, is_bfaa_available, is_bnb_available, is_botoa_available, is_ccl_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_fpa_available, is_ipex_available, is_megatron_lm_available, is_mlflow_available, is_mps_available, is_npu_available, is_rich_available, is_safetensors_available, is_sagemaker_available, is_tensorboard_available, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) from .modeling import ( check_device_map, check_tied_parameters_in_config, check_tied_parameters_on_same_device, compute_module_sizes, convert_file_size_to_int, dtype_byte_size, find_tied_parameters, get_balanced_memory, get_max_layer_size, get_max_memory, get_mixed_precision_context_manager, id_tensor_storage, infer_auto_device_map, load_checkpoint_in_model, load_offloaded_weights, load_state_dict, named_module_tensors, retie_parameters, set_module_tensor_to_device, shard_checkpoint, ) from .offload import ( OffloadedWeightsLoader, PrefixedDataset, extract_submodules_state_dict, load_offloaded_weight, offload_state_dict, offload_weight, save_offload_index, ) from .operations import ( broadcast, broadcast_object_list, concatenate, convert_outputs_to_fpaa, convert_to_fpaa, find_batch_size, find_device, gather, gather_object, get_data_structure, honor_type, initialize_tensors, is_namedtuple, is_tensor_information, is_torch_tensor, listify, pad_across_processes, recursively_apply, reduce, send_to_device, slice_tensors, ) from .versions import compare_versions, is_torch_version if is_deepspeed_available(): from .deepspeed import ( DeepSpeedEngineWrapper, DeepSpeedOptimizerWrapper, DeepSpeedSchedulerWrapper, DummyOptim, DummyScheduler, HfDeepSpeedConfig, ) from .bnb import has_abit_bnb_layers, load_and_quantize_model from .fsdp_utils import load_fsdp_model, load_fsdp_optimizer, save_fsdp_model, save_fsdp_optimizer from .launch import ( PrepareForLaunch, _filter_args, prepare_deepspeed_cmd_env, prepare_multi_gpu_env, prepare_sagemager_args_inputs, prepare_simple_launcher_cmd_env, prepare_tpu, ) from .megatron_lm import ( AbstractTrainStep, BertTrainStep, GPTTrainStep, MegatronEngine, MegatronLMDummyDataLoader, MegatronLMDummyScheduler, MegatronLMOptimizerWrapper, MegatronLMSchedulerWrapper, TaTrainStep, avg_losses_across_data_parallel_group, gather_across_data_parallel_groups, ) from .megatron_lm import initialize as megatron_lm_initialize from .megatron_lm import prepare_data_loader as megatron_lm_prepare_data_loader from .megatron_lm import prepare_model as megatron_lm_prepare_model from .megatron_lm import prepare_optimizer as megatron_lm_prepare_optimizer from .megatron_lm import prepare_scheduler as megatron_lm_prepare_scheduler from .memory import find_executable_batch_size, release_memory from .other import ( extract_model_from_parallel, get_pretty_name, is_port_in_use, merge_dicts, patch_environment, save, wait_for_everyone, write_basic_config, ) from .random import set_seed, synchronize_rng_state, synchronize_rng_states from .torch_xla import install_xla from .tqdm import tqdm from .transformer_engine import convert_model, has_transformer_engine_layers
36
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
1
class _A : '''simple docstring''' def __init__( self ): '''simple docstring''' snake_case : dict[str, TrieNode] = {} # Mapping from char to TrieNode snake_case : Optional[int] = False def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' for word in words: self.insert(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : int = self for char in word: if char not in curr.nodes: snake_case : Optional[Any] = TrieNode() snake_case : Optional[Any] = curr.nodes[char] snake_case : List[Any] = True def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Tuple = self for char in word: if char not in curr.nodes: return False snake_case : Any = curr.nodes[char] return curr.is_leaf def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' def _delete(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) -> bool: if index == len(SCREAMING_SNAKE_CASE_ ): # If word does not exist if not curr.is_leaf: return False snake_case : Tuple = False return len(curr.nodes ) == 0 snake_case : Dict = word[index] snake_case : Any = curr.nodes.get(SCREAMING_SNAKE_CASE_ ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted snake_case : List[Any] = _delete(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self ,SCREAMING_SNAKE_CASE_ ,0 ) def lowercase ( __A : TrieNode , __A : str ) -> None: '''simple docstring''' if node.is_leaf: print(__A , end=""" """ ) for key, value in node.nodes.items(): print_words(__A , word + key ) def lowercase ( ) -> bool: '''simple docstring''' snake_case : Any = """banana bananas bandana band apple all beast""".split() snake_case : Dict = TrieNode() root.insert_many(__A ) # print_words(root, "") assert all(root.find(__A ) for word in words ) assert root.find("""banana""" ) assert not root.find("""bandanas""" ) assert not root.find("""apps""" ) assert root.find("""apple""" ) assert root.find("""all""" ) root.delete("""all""" ) assert not root.find("""all""" ) root.delete("""banana""" ) assert not root.find("""banana""" ) assert root.find("""bananas""" ) return True def lowercase ( __A : str , __A : bool ) -> None: '''simple docstring''' print(str(__A ) , """works!""" if passes else """doesn't work :(""" ) def lowercase ( ) -> None: '''simple docstring''' assert test_trie() def lowercase ( ) -> None: '''simple docstring''' print_results("""Testing trie functionality""" , test_trie() ) if __name__ == "__main__": main()
36
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
1
import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' snake_case : List[Any] = np.random.default_rng(SCREAMING_SNAKE_CASE_ ) snake_case : Dict = length snake_case : Optional[Any] = rng.normal(size=(length,) ).astype(np.floataa ) snake_case : Dict = a * self.x + b + rng.normal(scale=0.1 ,size=(length,) ).astype(np.floataa ) def __len__( self ): '''simple docstring''' return self.length def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return {"x": self.x[i], "y": self.y[i]} class _A ( torch.nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=False ): '''simple docstring''' super().__init__() snake_case : List[Any] = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) snake_case : Optional[Any] = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) snake_case : Dict = True def snake_case_ ( self ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' if self.first_batch: print(F"""Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}""" ) snake_case : Tuple = False return x * self.a[0] + self.b[0] class _A ( torch.nn.Module ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=False ): '''simple docstring''' super().__init__() snake_case : Tuple = torch.nn.Parameter(torch.tensor(SCREAMING_SNAKE_CASE_ ).float() ) snake_case : Optional[Any] = torch.nn.Parameter(torch.tensor(SCREAMING_SNAKE_CASE_ ).float() ) snake_case : Dict = True def snake_case_ ( self ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' if self.first_batch: print(F"""Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}""" ) snake_case : int = False return x * self.a + self.b def lowercase ( __A : Dict , __A : int = 16 ) -> List[str]: '''simple docstring''' from datasets import load_dataset from transformers import AutoTokenizer snake_case : Tuple = AutoTokenizer.from_pretrained("""bert-base-cased""" ) snake_case : Tuple = {"""train""": """tests/test_samples/MRPC/train.csv""", """validation""": """tests/test_samples/MRPC/dev.csv"""} snake_case : List[str] = load_dataset("""csv""" , data_files=__A ) snake_case : str = datasets["""train"""].unique("""label""" ) snake_case : Any = {v: i for i, v in enumerate(__A )} def tokenize_function(__A : List[Any] ): # max_length=None => use the model max length (it's actually the default) snake_case : Tuple = tokenizer( examples["""sentence1"""] , examples["""sentence2"""] , truncation=__A , max_length=__A , padding="""max_length""" ) if "label" in examples: snake_case : str = [label_to_id[l] for l in examples["""label"""]] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset snake_case : Dict = datasets.map( __A , batched=__A , remove_columns=["""sentence1""", """sentence2""", """label"""] , ) def collate_fn(__A : Dict ): # 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. snake_case : str = DataLoader(tokenized_datasets["""train"""] , shuffle=__A , collate_fn=__A , batch_size=2 ) snake_case : Union[str, Any] = DataLoader(tokenized_datasets["""validation"""] , shuffle=__A , collate_fn=__A , batch_size=1 ) return train_dataloader, eval_dataloader
36
from __future__ import annotations def lowercase ( __A : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
36
1
def lowercase ( __A : int ) -> str: '''simple docstring''' if isinstance(__A , __A ): raise TypeError("""'float' object cannot be interpreted as an integer""" ) if isinstance(__A , __A ): raise TypeError("""'str' object cannot be interpreted as an integer""" ) if num == 0: return "0b0" snake_case : List[str] = False if num < 0: snake_case : Union[str, Any] = True snake_case : Dict = -num snake_case : list[int] = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(__A ) for e in binary ) return "0b" + "".join(str(__A ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
36
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __lowercase : List[str] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Tuple = '''vision-encoder-decoder''' __lowerCamelCase : List[Any] = True def __init__( self ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"""A configuraton of type {self.model_type} cannot be instantiated because """ F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}""" ) snake_case : Union[str, Any] = kwargs.pop("""encoder""" ) snake_case : Any = encoder_config.pop("""model_type""" ) snake_case : Optional[Any] = kwargs.pop("""decoder""" ) snake_case : Union[str, Any] = decoder_config.pop("""model_type""" ) snake_case : Any = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = AutoConfig.for_model(SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : int = True @classmethod def snake_case_ ( cls ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config""" ) snake_case : Tuple = True snake_case : Union[str, Any] = True return cls(encoder=encoder_config.to_dict() ,decoder=decoder_config.to_dict() ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case : Union[str, Any] = self.encoder.to_dict() snake_case : Union[str, Any] = self.decoder.to_dict() snake_case : Dict = self.__class__.model_type return output class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4 @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}} ) class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = OrderedDict() snake_case : Optional[int] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Union[str, Any] = {0: """batch""", 1: """past_decoder_sequence + sequence"""} snake_case : Optional[Any] = {0: """batch""", 1: """encoder_sequence"""} return common_inputs def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = -1 ,SCREAMING_SNAKE_CASE_ = False ,SCREAMING_SNAKE_CASE_ = None ,): '''simple docstring''' import torch snake_case : Optional[Any] = OrderedDict() snake_case : Tuple = super().generate_dummy_inputs( SCREAMING_SNAKE_CASE_ ,batch_size=SCREAMING_SNAKE_CASE_ ,seq_length=SCREAMING_SNAKE_CASE_ ,is_pair=SCREAMING_SNAKE_CASE_ ,framework=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case : List[Any] = dummy_input["""input_ids"""].shape snake_case : Optional[int] = (batch, encoder_sequence, self._config.encoder_hidden_size) snake_case : List[str] = dummy_input.pop("""input_ids""" ) snake_case : int = dummy_input.pop("""attention_mask""" ) snake_case : Dict = torch.zeros(SCREAMING_SNAKE_CASE_ ) return common_inputs class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' pass def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = "default" ): '''simple docstring''' snake_case : int = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
36
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : Optional[int] = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[str] = '''timm_backbone''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = backbone snake_case : Dict = num_channels snake_case : Optional[int] = features_only snake_case : Any = use_pretrained_backbone snake_case : Union[str, Any] = True snake_case : Optional[int] = out_indices if out_indices is not None else (-1,)
36
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
1
from __future__ import annotations from math import gcd def lowercase ( __A : int , __A : int = 2 , __A : int = 1 , __A : int = 3 , ) -> int | None: '''simple docstring''' if num < 2: raise ValueError("""The input value cannot be less than 2""" ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(__A : int , __A : int , __A : int ) -> int: return (pow(__A , 2 ) + step) % modulus for _ in range(__A ): # These track the position within the cycle detection logic. snake_case : str = seed snake_case : int = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. snake_case : Union[str, Any] = rand_fn(__A , __A , __A ) snake_case : Dict = rand_fn(__A , __A , __A ) snake_case : int = rand_fn(__A , __A , __A ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. snake_case : int = gcd(hare - tortoise , __A ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. snake_case : Optional[Any] = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse __lowercase : Any = argparse.ArgumentParser() parser.add_argument( '''num''', type=int, help='''The value to find a divisor of''', ) parser.add_argument( '''--attempts''', type=int, default=3, help='''The number of attempts before giving up''', ) __lowercase : List[Any] = parser.parse_args() __lowercase : Tuple = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f'''{args.num} is probably prime''') else: __lowercase : Optional[int] = args.num // divisor print(f'''{args.num} = {divisor} * {quotient}''')
36
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
1
def lowercase ( __A : int , __A : int ) -> int: '''simple docstring''' return int((input_a, input_a).count(0 ) == 0 ) def lowercase ( ) -> None: '''simple docstring''' assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
36
from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('''To use the rich extension, install rich with `pip install rich`''')
36
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __lowercase : Any = logging.get_logger(__name__) def lowercase ( __A : Optional[Any] ) -> Dict: '''simple docstring''' snake_case : Dict = """huggingface/label-files""" snake_case : int = """imagenet-1k-id2label.json""" snake_case : Tuple = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) snake_case : Any = {int(__A ): v for k, v in idalabel.items()} snake_case : Dict = {v: k for k, v in idalabel.items()} snake_case : Any = """std_conv""" if """bit""" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" snake_case : List[Any] = BitConfig( conv_layer=__A , num_labels=1000 , idalabel=__A , labelaid=__A , ) return config def lowercase ( __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if "stem.conv" in name: snake_case : List[str] = name.replace("""stem.conv""" , """bit.embedder.convolution""" ) if "blocks" in name: snake_case : List[str] = name.replace("""blocks""" , """layers""" ) if "head.fc" in name: snake_case : Optional[int] = name.replace("""head.fc""" , """classifier.1""" ) if name.startswith("""norm""" ): snake_case : Optional[Any] = """bit.""" + name if "bit" not in name and "classifier" not in name: snake_case : Tuple = """bit.encoder.""" + name return name def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def lowercase ( __A : Any , __A : Union[str, Any] , __A : str=False ) -> Optional[int]: '''simple docstring''' snake_case : str = get_config(__A ) # load original model from timm snake_case : Tuple = create_model(__A , pretrained=__A ) timm_model.eval() # load state_dict of original model snake_case : List[str] = timm_model.state_dict() for key in state_dict.copy().keys(): snake_case : List[Any] = state_dict.pop(__A ) snake_case : Union[str, Any] = val.squeeze() if """head""" in key else val # load HuggingFace model snake_case : List[Any] = BitForImageClassification(__A ) model.eval() model.load_state_dict(__A ) # create image processor snake_case : Dict = create_transform(**resolve_data_config({} , model=__A ) ) snake_case : Optional[Any] = transform.transforms snake_case : List[Any] = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case : Union[str, Any] = BitImageProcessor( do_resize=__A , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__A , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__A , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case : Dict = prepare_img() snake_case : List[str] = transform(__A ).unsqueeze(0 ) snake_case : int = processor(__A , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__A , __A ) # verify logits with torch.no_grad(): snake_case : Optional[int] = model(__A ) snake_case : Dict = outputs.logits print("""Logits:""" , logits[0, :3] ) print("""Predicted class:""" , model.config.idalabel[logits.argmax(-1 ).item()] ) snake_case : int = timm_model(__A ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__A , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__A ).mkdir(exist_ok=__A ) print(f"""Saving model {model_name} and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model {model_name} and processor to the hub""" ) model.push_to_hub(f"""ybelkada/{model_name}""" ) processor.push_to_hub(f"""ybelkada/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''resnetv2_50x1_bitm''', type=str, help='''Name of the BiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub.''', ) __lowercase : Union[str, Any] = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
36
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __lowercase : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : str __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[str] = None @dataclass(frozen=snake_case ) class _A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[Union[int, float]] = None __lowerCamelCase : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : str = hans_processors[task]() snake_case : str = os.path.join( SCREAMING_SNAKE_CASE_ ,"""cached_{}_{}_{}_{}""".format( """dev""" if evaluate else """train""" ,tokenizer.__class__.__name__ ,str(SCREAMING_SNAKE_CASE_ ) ,SCREAMING_SNAKE_CASE_ ,) ,) snake_case : Dict = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : List[Any] = label_list[2], label_list[1] snake_case : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. snake_case : Any = cached_features_file + """.lock""" with FileLock(SCREAMING_SNAKE_CASE_ ): if os.path.exists(SCREAMING_SNAKE_CASE_ ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) snake_case : int = torch.load(SCREAMING_SNAKE_CASE_ ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) snake_case : Union[str, Any] = ( processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) ) logger.info("""Training examples: %s""" ,len(SCREAMING_SNAKE_CASE_ ) ) snake_case : Dict = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) logger.info("""Saving features into cached file %s""" ,SCREAMING_SNAKE_CASE_ ) torch.save(self.features ,SCREAMING_SNAKE_CASE_ ) def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list if is_tf_available(): import tensorflow as tf class _A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = 128 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_ = False ,): '''simple docstring''' snake_case : Any = hans_processors[task]() snake_case : List[str] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) snake_case , snake_case : int = label_list[2], label_list[1] snake_case : List[str] = label_list snake_case : int = processor.get_dev_examples(SCREAMING_SNAKE_CASE_ ) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_ ) snake_case : Any = hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc="""convert examples to features""" ): if ex_index % 10000 == 0: logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) snake_case : Any = tf.data.Dataset.from_generator( SCREAMING_SNAKE_CASE_ ,( { """example_id""": tf.intaa, """input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa, }, tf.intaa, ) ,( { """example_id""": tf.TensorShape([] ), """input_ids""": tf.TensorShape([None, None] ), """attention_mask""": tf.TensorShape([None, None] ), """token_type_ids""": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case_ ( self ): '''simple docstring''' return self.dataset def __len__( self ): '''simple docstring''' return len(self.features ) def __getitem__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self.features[i] def snake_case_ ( self ): '''simple docstring''' return self.label_list class _A ( snake_case ): '''simple docstring''' def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_train_set.txt""" ) ) ,"""train""" ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_ ,"""heuristics_evaluation_set.txt""" ) ) ,"""dev""" ) def snake_case_ ( self ): '''simple docstring''' return ["contradiction", "entailment", "neutral"] def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : List[str] = [] for i, line in enumerate(SCREAMING_SNAKE_CASE_ ): if i == 0: continue snake_case : Any = """%s-%s""" % (set_type, line[0]) snake_case : Optional[int] = line[5] snake_case : Union[str, Any] = line[6] snake_case : Optional[Any] = line[7][2:] if line[7].startswith("""ex""" ) else line[7] snake_case : Dict = line[0] examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_ ,text_a=SCREAMING_SNAKE_CASE_ ,text_b=SCREAMING_SNAKE_CASE_ ,label=SCREAMING_SNAKE_CASE_ ,pairID=SCREAMING_SNAKE_CASE_ ) ) return examples def lowercase ( __A : List[InputExample] , __A : List[str] , __A : int , __A : PreTrainedTokenizer , ) -> Tuple: '''simple docstring''' snake_case : List[Any] = {label: i for i, label in enumerate(__A )} snake_case : Union[str, Any] = [] for ex_index, example in tqdm.tqdm(enumerate(__A ) , desc="""convert examples to features""" ): if ex_index % 1_0000 == 0: logger.info("""Writing example %d""" % (ex_index) ) snake_case : Union[str, Any] = tokenizer( example.text_a , example.text_b , add_special_tokens=__A , max_length=__A , padding="""max_length""" , truncation=__A , return_overflowing_tokens=__A , ) snake_case : Tuple = label_map[example.label] if example.label in label_map else 0 snake_case : Tuple = int(example.pairID ) features.append(InputFeatures(**__A , label=__A , pairID=__A ) ) for i, example in enumerate(examples[:5] ): logger.info("""*** Example ***""" ) logger.info(f"""guid: {example}""" ) logger.info(f"""features: {features[i]}""" ) return features __lowercase : Dict = { '''hans''': 3, } __lowercase : Union[str, Any] = { '''hans''': HansProcessor, }
36
1
import os import pytest from attr import dataclass __lowercase : Optional[int] = '''us-east-1''' # defaults region @dataclass class _A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : Dict = '''arn:aws:iam::558105141721:role/sagemaker_execution_role''' __lowerCamelCase : Optional[Any] = { '''task_name''': '''mnli''', '''per_device_train_batch_size''': 1_6, '''per_device_eval_batch_size''': 1_6, '''do_train''': True, '''do_eval''': True, '''do_predict''': True, '''output_dir''': '''/opt/ml/model''', '''overwrite_output_dir''': True, '''max_steps''': 5_0_0, '''save_steps''': 5_5_0_0, } __lowerCamelCase : List[str] = {**hyperparameters, '''max_steps''': 1_0_0_0} @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def snake_case_ ( self ): '''simple docstring''' return F"""{self.framework}-transfromers-test""" @property def snake_case_ ( self ): '''simple docstring''' return F"""./tests/sagemaker/scripts/{self.framework}""" @property def snake_case_ ( self ): '''simple docstring''' if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="""class""" ) def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[Any] = SageMakerTestEnvironment(framework=request.cls.framework )
36
from __future__ import annotations def lowercase ( __A : int ) -> list[int]: '''simple docstring''' snake_case : Dict = 2 snake_case : int = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A ) if n > 1: factors.append(__A ) return factors if __name__ == "__main__": import doctest doctest.testmod()
36
1
import inspect import logging import os import random import shutil import tempfile import unittest import pytest import torch from torch import nn from torch.utils.data import DataLoader, TensorDataset from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_cuda from accelerate.utils import ProjectConfiguration, set_seed __lowercase : str = logging.getLogger(__name__) def lowercase ( __A : List[Any]=2 , __A : List[Any]=3 , __A : Optional[int]=16 , __A : int = 10 , __A : int = 2 ) -> Dict: '''simple docstring''' def get_dataset(__A : Optional[int] ): snake_case : Union[str, Any] = torch.randn(batch_size * n_batches , 1 ) return TensorDataset(__A , a * x + b + 0.1 * torch.randn(batch_size * n_batches , 1 ) ) snake_case : Union[str, Any] = get_dataset(__A ) snake_case : Tuple = get_dataset(__A ) snake_case : Any = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) snake_case : Optional[int] = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) return (train_dataloader, valid_dataloader) def lowercase ( __A : str , __A : Optional[Any] , __A : List[str] , __A : int , __A : Union[str, Any] , __A : List[str]=None ) -> Optional[int]: '''simple docstring''' snake_case : List[Any] = [] for epoch in range(__A ): # Train quickly model.train() for batch in dataloader: snake_case , snake_case : List[str] = batch snake_case : int = model(__A ) snake_case : Tuple = torch.nn.functional.mse_loss(__A , __A ) accelerator.backward(__A ) optimizer.step() optimizer.zero_grad() rands.append(random.random() ) # Introduce some randomness if scheduler is not None: scheduler.step() return rands class _A ( nn.Module ): '''simple docstring''' def __init__( self ): '''simple docstring''' super().__init__() snake_case : str = nn.Parameter(torch.randn(1 ) ) snake_case : int = nn.Parameter(torch.randn(1 ) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' return x * self.a + self.b class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) snake_case : Tuple = DummyModel() snake_case : Any = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) snake_case , snake_case : Dict = dummy_dataloaders() snake_case : Optional[Any] = ProjectConfiguration(total_limit=1 ,project_dir=SCREAMING_SNAKE_CASE_ ,automatic_checkpoint_naming=SCREAMING_SNAKE_CASE_ ) # Train baseline snake_case : Any = Accelerator(project_config=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case , snake_case , snake_case : Dict = accelerator.prepare( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Save initial accelerator.save_state() # Save second state accelerator.save_state() self.assertEqual(len(os.listdir(accelerator.project_dir ) ) ,1 ) def snake_case_ ( self ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) snake_case : List[str] = DummyModel() snake_case : Optional[Any] = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) snake_case , snake_case : str = dummy_dataloaders() # Train baseline snake_case : str = Accelerator() snake_case , snake_case , snake_case , snake_case : Optional[int] = accelerator.prepare( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Save initial snake_case : Optional[Any] = os.path.join(SCREAMING_SNAKE_CASE_ ,"""initial""" ) accelerator.save_state(SCREAMING_SNAKE_CASE_ ) ((snake_case) , (snake_case)) : str = model.a.item(), model.b.item() snake_case : Optional[int] = optimizer.state_dict() snake_case : Union[str, Any] = train(3 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ((snake_case) , (snake_case)) : Any = model.a.item(), model.b.item() snake_case : List[str] = optimizer.state_dict() # Train partially set_seed(42 ) snake_case : List[str] = DummyModel() snake_case : Optional[Any] = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) snake_case , snake_case : Union[str, Any] = dummy_dataloaders() snake_case : List[Any] = Accelerator() snake_case , snake_case , snake_case , snake_case : List[str] = accelerator.prepare( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) accelerator.load_state(SCREAMING_SNAKE_CASE_ ) ((snake_case) , (snake_case)) : Tuple = model.a.item(), model.b.item() snake_case : Optional[int] = optimizer.state_dict() self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = train(2 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Save everything snake_case : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE_ ,"""checkpoint""" ) accelerator.save_state(SCREAMING_SNAKE_CASE_ ) # Load everything back in and make sure all states work accelerator.load_state(SCREAMING_SNAKE_CASE_ ) test_rands += train(1 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ((snake_case) , (snake_case)) : Optional[Any] = model.a.item(), model.b.item() snake_case : int = optimizer.state_dict() self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) snake_case : str = DummyModel() snake_case : List[str] = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) snake_case , snake_case : int = dummy_dataloaders() snake_case : Union[str, Any] = ProjectConfiguration(automatic_checkpoint_naming=SCREAMING_SNAKE_CASE_ ) # Train baseline snake_case : Optional[Any] = Accelerator(project_dir=SCREAMING_SNAKE_CASE_ ,project_config=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case , snake_case , snake_case : Any = accelerator.prepare( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Save initial accelerator.save_state() ((snake_case) , (snake_case)) : List[str] = model.a.item(), model.b.item() snake_case : Union[str, Any] = optimizer.state_dict() snake_case : Any = train(3 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ((snake_case) , (snake_case)) : List[Any] = model.a.item(), model.b.item() snake_case : str = optimizer.state_dict() # Train partially set_seed(42 ) snake_case : Tuple = DummyModel() snake_case : Union[str, Any] = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) snake_case , snake_case : Optional[Any] = dummy_dataloaders() snake_case : Optional[int] = ProjectConfiguration(iteration=1 ,automatic_checkpoint_naming=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = Accelerator(project_dir=SCREAMING_SNAKE_CASE_ ,project_config=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case , snake_case , snake_case : str = accelerator.prepare( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) accelerator.load_state(os.path.join(SCREAMING_SNAKE_CASE_ ,"""checkpoints""" ,"""checkpoint_0""" ) ) ((snake_case) , (snake_case)) : Tuple = model.a.item(), model.b.item() snake_case : Optional[int] = optimizer.state_dict() self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : str = train(2 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Save everything accelerator.save_state() # Load everything back in and make sure all states work accelerator.load_state(os.path.join(SCREAMING_SNAKE_CASE_ ,"""checkpoints""" ,"""checkpoint_1""" ) ) test_rands += train(1 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ((snake_case) , (snake_case)) : Dict = model.a.item(), model.b.item() snake_case : Optional[int] = optimizer.state_dict() self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : str = torch.tensor([1, 2, 3] ) snake_case : Optional[Any] = torch.tensor([2, 3, 4] ) snake_case : List[str] = DummyModel() snake_case : Union[str, Any] = torch.optim.Adam(net.parameters() ) snake_case : Any = Accelerator() with self.assertRaises(SCREAMING_SNAKE_CASE_ ) as ve: accelerator.register_for_checkpointing(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = str(ve.exception ) self.assertTrue("""Item at index 0""" in message ) self.assertTrue("""Item at index 1""" in message ) self.assertFalse("""Item at index 2""" in message ) self.assertFalse("""Item at index 3""" in message ) def snake_case_ ( self ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) snake_case : Optional[int] = DummyModel() snake_case : Any = torch.optim.Adam(params=model.parameters() ,lr=1E-3 ) snake_case : Tuple = torch.optim.lr_scheduler.StepLR(SCREAMING_SNAKE_CASE_ ,step_size=1 ,gamma=0.99 ) snake_case , snake_case : Optional[Any] = dummy_dataloaders() snake_case : int = ProjectConfiguration(automatic_checkpoint_naming=SCREAMING_SNAKE_CASE_ ) # Train baseline snake_case : List[Any] = Accelerator(project_dir=SCREAMING_SNAKE_CASE_ ,project_config=SCREAMING_SNAKE_CASE_ ) snake_case , snake_case , snake_case , snake_case , snake_case : Tuple = accelerator.prepare( SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) # Save initial accelerator.save_state() snake_case : Optional[int] = scheduler.state_dict() train(3 ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) self.assertNotEqual(SCREAMING_SNAKE_CASE_ ,scheduler.state_dict() ) # Load everything back in and make sure all states work accelerator.load_state(os.path.join(SCREAMING_SNAKE_CASE_ ,"""checkpoints""" ,"""checkpoint_0""" ) ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,scheduler.state_dict() ) def snake_case_ ( self ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdir: set_seed(42 ) snake_case : List[Any] = DummyModel() snake_case : int = ProjectConfiguration(automatic_checkpoint_naming=SCREAMING_SNAKE_CASE_ ,total_limit=2 ) # Train baseline snake_case : List[str] = Accelerator(project_dir=SCREAMING_SNAKE_CASE_ ,project_config=SCREAMING_SNAKE_CASE_ ) snake_case : str = accelerator.prepare(SCREAMING_SNAKE_CASE_ ) # Save 3 states: for _ in range(11 ): accelerator.save_state() self.assertTrue(not os.path.exists(os.path.join(SCREAMING_SNAKE_CASE_ ,"""checkpoints""" ,"""checkpoint_0""" ) ) ) self.assertTrue(os.path.exists(os.path.join(SCREAMING_SNAKE_CASE_ ,"""checkpoints""" ,"""checkpoint_9""" ) ) ) self.assertTrue(os.path.exists(os.path.join(SCREAMING_SNAKE_CASE_ ,"""checkpoints""" ,"""checkpoint_10""" ) ) ) @require_cuda def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = ["""torchrun""", F"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__ )] execute_subprocess_async(SCREAMING_SNAKE_CASE_ ,env=os.environ.copy() ) if __name__ == "__main__": __lowercase : int = '''/tmp/accelerate/state_checkpointing''' __lowercase : Optional[int] = DummyModel() __lowercase : Optional[int] = torch.optim.Adam(params=model.parameters(), lr=1E-3) __lowercase : Tuple = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.99) __lowercase , __lowercase : List[str] = dummy_dataloaders() __lowercase : Optional[Any] = ProjectConfiguration(automatic_checkpoint_naming=True) # Train baseline __lowercase : Dict = Accelerator(project_dir=savedir, project_config=project_config, mixed_precision='''no''') if accelerator.process_index == 0: if os.path.exists(savedir): shutil.rmtree(savedir) os.makedirs(savedir) __lowercase , __lowercase , __lowercase , __lowercase , __lowercase : Optional[Any] = accelerator.prepare( model, optimizer, train_dataloader, valid_dataloader, scheduler ) __lowercase , __lowercase : Optional[int] = accelerator.prepare(model, optimizer) train(3, model, train_dataloader, optimizer, accelerator, scheduler) # Check that the intial optimizer is loaded on the GPU for group in optimizer.param_groups: __lowercase : Optional[Any] = group['''params'''][0].device break assert param_device.type == accelerator.device.type __lowercase : Optional[Any] = model.cpu() accelerator.wait_for_everyone() accelerator.save_state() accelerator.wait_for_everyone() # Check CPU state accelerator.load_state(os.path.join(savedir, '''checkpoints''', '''checkpoint_0'''), map_location='''cpu''') for group in optimizer.param_groups: __lowercase : int = group['''params'''][0].device break assert ( param_device.type == torch.device('''cpu''').type ), f"Loaded optimizer states did not match, expected to be loaded on the CPU but got {param_device}" # Check device state model.to(accelerator.device) accelerator.load_state(os.path.join(savedir, '''checkpoints''', '''checkpoint_0'''), map_location='''on_device''') for group in optimizer.param_groups: __lowercase : Any = group['''params'''][0].device break assert ( param_device.type == accelerator.device.type ), f"Loaded optimizer states did not match, expected to be loaded on {accelerator.device} but got {param_device}" # Check error with pytest.raises(TypeError, match='''Unsupported optimizer map location passed'''): accelerator.load_state(os.path.join(savedir, '''checkpoints''', '''checkpoint_0'''), map_location='''invalid''') accelerator.wait_for_everyone() if accelerator.process_index == 0: shutil.rmtree(savedir) accelerator.wait_for_everyone()
36
import numpy as np def lowercase ( __A : np.array ) -> np.array: '''simple docstring''' return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
36
1
import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def lowercase ( __A : Dict ) -> Optional[Any]: '''simple docstring''' def wrapper(*__A : Dict , **__A : Tuple ): snake_case : Tuple = timeit.default_timer() snake_case : Any = func(*__A , **__A ) snake_case : int = timeit.default_timer() - starttime return delta snake_case : Dict = func.__name__ return wrapper def lowercase ( __A : dict , __A : Dict=100 , __A : int=None ) -> int: '''simple docstring''' snake_case : Optional[Any] = [] snake_case : List[Any] = seq_shapes or {} for i in range(__A ): snake_case : Dict = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(__A , _ArrayXD ): snake_case : Any = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(__A , datasets.Value ): if v.dtype == "string": snake_case : Optional[int] = """The small grey turtle was surprisingly fast when challenged.""" else: snake_case : Union[str, Any] = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(__A , datasets.Sequence ): while isinstance(__A , datasets.Sequence ): snake_case : Union[str, Any] = v.feature snake_case : Any = seq_shapes[k] snake_case : Tuple = np.random.rand(*__A ).astype(v.dtype ) snake_case : Optional[Any] = data dummy_data.append((i, example) ) return dummy_data def lowercase ( __A : List[str] , __A : Dict , __A : Tuple=100 , __A : Optional[int]=None ) -> Dict: '''simple docstring''' snake_case : Union[str, Any] = generate_examples(__A , num_examples=__A , seq_shapes=__A ) with ArrowWriter(features=__A , path=__A ) as writer: for key, record in dummy_data: snake_case : Any = features.encode_example(__A ) writer.write(__A ) snake_case , snake_case : Dict = writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.""" ) snake_case : Optional[int] = datasets.Dataset.from_file(filename=__A , info=datasets.DatasetInfo(features=__A ) ) return dataset
36
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __lowercase : Optional[int] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def lowercase ( __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' for pegasus_name, hf_name in PATTERNS: snake_case : Dict = k.replace(__A , __A ) return k def lowercase ( __A : dict , __A : dict ) -> PegasusForConditionalGeneration: '''simple docstring''' snake_case : Dict = DEFAULTS.copy() cfg_kwargs.update(__A ) snake_case : int = PegasusConfig(**__A ) snake_case : List[Any] = PegasusForConditionalGeneration(__A ) snake_case : Optional[Any] = torch_model.model.state_dict() snake_case : Optional[int] = {} for k, v in tf_weights.items(): snake_case : str = rename_state_dict_key(__A ) if new_k not in sd: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: snake_case : Optional[Any] = v.T snake_case : List[Any] = torch.tensor(__A , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected snake_case : List[str] = torch.zeros_like(mapping["""shared.weight"""][cfg.pad_token_id + 1] ) snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Optional[Any] = mapping["""shared.weight"""] snake_case : Tuple = {k: torch.zeros_like(__A ) for k, v in sd.items() if k.endswith("""bias""" ) and k not in mapping} mapping.update(**__A ) snake_case , snake_case : Union[str, Any] = torch_model.model.load_state_dict(__A , strict=__A ) snake_case : Union[str, Any] = [ k for k in missing if k not in ["""encoder.embed_positions.weight""", """decoder.embed_positions.weight"""] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def lowercase ( __A : int="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: '''simple docstring''' snake_case : Optional[Any] = tf.train.list_variables(__A ) snake_case : Union[str, Any] = {} snake_case : List[str] = ["""Adafactor""", """global_step"""] for name, shape in tqdm(__A , desc="""converting tf checkpoint to dict""" ): snake_case : str = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case : List[str] = tf.train.load_variable(__A , __A ) snake_case : Optional[Any] = array return tf_weights def lowercase ( __A : str , __A : str ) -> Optional[int]: '''simple docstring''' snake_case : Dict = Path(__A ).parent.name snake_case : Dict = task_specific_params[f"""summarization_{dataset}"""]["""max_position_embeddings"""] snake_case : Any = PegasusTokenizer.from_pretrained("""sshleifer/pegasus""" , model_max_length=__A ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__A ) # convert model snake_case : Dict = get_tf_weights_as_numpy(__A ) snake_case : List[Any] = task_specific_params[f"""summarization_{dataset}"""] if dataset == "large": snake_case : Optional[int] = task_specific_params snake_case : Optional[int] = convert_pegasus(__A , __A ) torch_model.save_pretrained(__A ) snake_case : int = torch_model.state_dict() sd.pop("""model.decoder.embed_positions.weight""" ) sd.pop("""model.encoder.embed_positions.weight""" ) torch.save(__A , Path(__A ) / """pytorch_model.bin""" ) if __name__ == "__main__": __lowercase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') __lowercase : List[Any] = parser.parse_args() if args.save_dir is None: __lowercase : Optional[Any] = Path(args.tf_ckpt_path).parent.name __lowercase : Union[str, Any] = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
36
1
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class _A ( pl.LightningModule ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' super().__init__() snake_case : Dict = model snake_case : Optional[int] = 2 snake_case : Optional[Any] = nn.Linear(self.model.config.hidden_size ,self.num_labels ) def snake_case_ ( self ): '''simple docstring''' pass def lowercase ( __A : str , __A : str , __A : str ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[Any] = LongformerModel.from_pretrained(__A ) snake_case : Tuple = LightningModel(__A ) snake_case : Optional[int] = torch.load(__A , map_location=torch.device("""cpu""" ) ) lightning_model.load_state_dict(ckpt["""state_dict"""] ) # init longformer question answering model snake_case : Dict = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(f"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": __lowercase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--longformer_model''', default=None, type=str, required=True, help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''', ) parser.add_argument( '''--longformer_question_answering_ckpt_path''', default=None, type=str, required=True, help='''Path the official PyTorch Lightning Checkpoint.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __lowercase : List[str] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
36
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowercase : Any = { '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''], '''tokenization_roformer''': ['''RoFormerTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Optional[Any] = ['''RoFormerTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Optional[Any] = [ '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RoFormerForCausalLM''', '''RoFormerForMaskedLM''', '''RoFormerForMultipleChoice''', '''RoFormerForQuestionAnswering''', '''RoFormerForSequenceClassification''', '''RoFormerForTokenClassification''', '''RoFormerLayer''', '''RoFormerModel''', '''RoFormerPreTrainedModel''', '''load_tf_weights_in_roformer''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : int = [ '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFRoFormerForCausalLM''', '''TFRoFormerForMaskedLM''', '''TFRoFormerForMultipleChoice''', '''TFRoFormerForQuestionAnswering''', '''TFRoFormerForSequenceClassification''', '''TFRoFormerForTokenClassification''', '''TFRoFormerLayer''', '''TFRoFormerModel''', '''TFRoFormerPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : List[str] = [ '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxRoFormerForMaskedLM''', '''FlaxRoFormerForMultipleChoice''', '''FlaxRoFormerForQuestionAnswering''', '''FlaxRoFormerForSequenceClassification''', '''FlaxRoFormerForTokenClassification''', '''FlaxRoFormerModel''', '''FlaxRoFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys __lowercase : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
import argparse import collections import json import os import re import string import sys import numpy as np __lowercase : Optional[Any] = re.compile(r'''\b(a|an|the)\b''', re.UNICODE) __lowercase : Optional[int] = None def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : int = argparse.ArgumentParser("""Official evaluation script for SQuAD version 2.0.""" ) parser.add_argument("""data_file""" , metavar="""data.json""" , help="""Input data JSON file.""" ) parser.add_argument("""pred_file""" , metavar="""pred.json""" , help="""Model predictions.""" ) parser.add_argument( """--out-file""" , """-o""" , metavar="""eval.json""" , help="""Write accuracy metrics to file (default is stdout).""" ) parser.add_argument( """--na-prob-file""" , """-n""" , metavar="""na_prob.json""" , help="""Model estimates of probability of no answer.""" ) parser.add_argument( """--na-prob-thresh""" , """-t""" , type=__A , default=1.0 , help="""Predict \"\" if no-answer probability exceeds this (default = 1.0).""" , ) parser.add_argument( """--out-image-dir""" , """-p""" , metavar="""out_images""" , default=__A , help="""Save precision-recall curves to directory.""" ) parser.add_argument("""--verbose""" , """-v""" , action="""store_true""" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowercase ( __A : Union[str, Any] ) -> int: '''simple docstring''' snake_case : Any = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : int = bool(qa["""answers"""]["""text"""] ) return qid_to_has_ans def lowercase ( __A : int ) -> Optional[int]: '''simple docstring''' def remove_articles(__A : List[Any] ): return ARTICLES_REGEX.sub(""" """ , __A ) def white_space_fix(__A : Union[str, Any] ): return " ".join(text.split() ) def remove_punc(__A : Tuple ): snake_case : Optional[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__A : Any ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(__A ) ) ) ) def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if not s: return [] return normalize_answer(__A ).split() def lowercase ( __A : Optional[int] , __A : int ) -> List[Any]: '''simple docstring''' return int(normalize_answer(__A ) == normalize_answer(__A ) ) def lowercase ( __A : Any , __A : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case : Tuple = get_tokens(__A ) snake_case : str = get_tokens(__A ) snake_case : Dict = collections.Counter(__A ) & collections.Counter(__A ) snake_case : Optional[int] = sum(common.values() ) if len(__A ) == 0 or len(__A ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 snake_case : List[Any] = 1.0 * num_same / len(__A ) snake_case : int = 1.0 * num_same / len(__A ) snake_case : Dict = (2 * precision * recall) / (precision + recall) return fa def lowercase ( __A : List[Any] , __A : int ) -> str: '''simple docstring''' snake_case : Tuple = {} snake_case : Tuple = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: snake_case : str = qa["""id"""] snake_case : Union[str, Any] = [t for t in qa["""answers"""]["""text"""] if normalize_answer(__A )] if not gold_answers: # For unanswerable questions, only correct answer is empty string snake_case : Optional[Any] = [""""""] if qid not in preds: print(f"""Missing prediction for {qid}""" ) continue snake_case : Dict = preds[qid] # Take max over all gold answers snake_case : Union[str, Any] = max(compute_exact(__A , __A ) for a in gold_answers ) snake_case : Optional[int] = max(compute_fa(__A , __A ) for a in gold_answers ) return exact_scores, fa_scores def lowercase ( __A : str , __A : Any , __A : List[Any] , __A : List[Any] ) -> Dict: '''simple docstring''' snake_case : Optional[int] = {} for qid, s in scores.items(): snake_case : Any = na_probs[qid] > na_prob_thresh if pred_na: snake_case : str = float(not qid_to_has_ans[qid] ) else: snake_case : List[Any] = s return new_scores def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str]=None ) -> int: '''simple docstring''' if not qid_list: snake_case : List[str] = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores.values() ) / total), ("""f1""", 100.0 * sum(fa_scores.values() ) / total), ("""total""", total), ] ) else: snake_case : Any = len(__A ) return collections.OrderedDict( [ ("""exact""", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("""f1""", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("""total""", total), ] ) def lowercase ( __A : Optional[Any] , __A : Tuple , __A : List[str] ) -> Optional[Any]: '''simple docstring''' for k in new_eval: snake_case : str = new_eval[k] def lowercase ( __A : Tuple , __A : int , __A : Dict , __A : Dict ) -> int: '''simple docstring''' plt.step(__A , __A , color="""b""" , alpha=0.2 , where="""post""" ) plt.fill_between(__A , __A , step="""post""" , alpha=0.2 , color="""b""" ) plt.xlabel("""Recall""" ) plt.ylabel("""Precision""" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(__A ) plt.savefig(__A ) plt.clf() def lowercase ( __A : Optional[Any] , __A : Union[str, Any] , __A : Dict , __A : Tuple , __A : Optional[Any]=None , __A : List[str]=None ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = sorted(__A , key=lambda __A : na_probs[k] ) snake_case : Any = 0.0 snake_case : str = 1.0 snake_case : Tuple = 0.0 snake_case : str = [1.0] snake_case : Any = [0.0] snake_case : Dict = 0.0 for i, qid in enumerate(__A ): if qid_to_has_ans[qid]: true_pos += scores[qid] snake_case : str = true_pos / float(i + 1 ) snake_case : List[str] = true_pos / float(__A ) if i == len(__A ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(__A ) recalls.append(__A ) if out_image: plot_pr_curve(__A , __A , __A , __A ) return {"ap": 100.0 * avg_prec} def lowercase ( __A : Any , __A : Optional[int] , __A : Tuple , __A : Tuple , __A : List[Any] , __A : Optional[Any] ) -> Optional[Any]: '''simple docstring''' if out_image_dir and not os.path.exists(__A ): os.makedirs(__A ) snake_case : Tuple = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return snake_case : str = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_exact.png""" ) , title="""Precision-Recall curve for Exact Match score""" , ) snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_f1.png""" ) , title="""Precision-Recall curve for F1 score""" , ) snake_case : Dict = {k: float(__A ) for k, v in qid_to_has_ans.items()} snake_case : int = make_precision_recall_eval( __A , __A , __A , __A , out_image=os.path.join(__A , """pr_oracle.png""" ) , title="""Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)""" , ) merge_eval(__A , __A , """pr_exact""" ) merge_eval(__A , __A , """pr_f1""" ) merge_eval(__A , __A , """pr_oracle""" ) def lowercase ( __A : List[Any] , __A : Union[str, Any] , __A : Union[str, Any] , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' if not qid_list: return snake_case : int = [na_probs[k] for k in qid_list] snake_case : List[str] = np.ones_like(__A ) / float(len(__A ) ) plt.hist(__A , weights=__A , bins=20 , range=(0.0, 1.0) ) plt.xlabel("""Model probability of no-answer""" ) plt.ylabel("""Proportion of dataset""" ) plt.title(f"""Histogram of no-answer probability: {name}""" ) plt.savefig(os.path.join(__A , f"""na_prob_hist_{name}.png""" ) ) plt.clf() def lowercase ( __A : List[Any] , __A : Tuple , __A : Tuple , __A : Any ) -> Dict: '''simple docstring''' snake_case : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) snake_case : str = num_no_ans snake_case : Optional[Any] = cur_score snake_case : Optional[Any] = 0.0 snake_case : List[Any] = sorted(__A , key=lambda __A : na_probs[k] ) for i, qid in enumerate(__A ): if qid not in scores: continue if qid_to_has_ans[qid]: snake_case : Dict = scores[qid] else: if preds[qid]: snake_case : Dict = -1 else: snake_case : str = 0 cur_score += diff if cur_score > best_score: snake_case : Union[str, Any] = cur_score snake_case : List[Any] = na_probs[qid] return 100.0 * best_score / len(__A ), best_thresh def lowercase ( __A : Dict , __A : str , __A : str , __A : int , __A : str , __A : Any ) -> List[str]: '''simple docstring''' snake_case , snake_case : Optional[int] = find_best_thresh(__A , __A , __A , __A ) snake_case , snake_case : str = find_best_thresh(__A , __A , __A , __A ) snake_case : List[str] = best_exact snake_case : List[Any] = exact_thresh snake_case : Optional[Any] = best_fa snake_case : Optional[int] = fa_thresh def lowercase ( ) -> Any: '''simple docstring''' with open(OPTS.data_file ) as f: snake_case : Dict = json.load(__A ) snake_case : Union[str, Any] = dataset_json["""data"""] with open(OPTS.pred_file ) as f: snake_case : int = json.load(__A ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: snake_case : Any = json.load(__A ) else: snake_case : Any = {k: 0.0 for k in preds} snake_case : Optional[int] = make_qid_to_has_ans(__A ) # maps qid to True/False snake_case : Dict = [k for k, v in qid_to_has_ans.items() if v] snake_case : Optional[int] = [k for k, v in qid_to_has_ans.items() if not v] snake_case , snake_case : Optional[Any] = get_raw_scores(__A , __A ) snake_case : Tuple = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[Any] = apply_no_ans_threshold(__A , __A , __A , OPTS.na_prob_thresh ) snake_case : Optional[int] = make_eval_dict(__A , __A ) if has_ans_qids: snake_case : Any = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """HasAns""" ) if no_ans_qids: snake_case : str = make_eval_dict(__A , __A , qid_list=__A ) merge_eval(__A , __A , """NoAns""" ) if OPTS.na_prob_file: find_all_best_thresh(__A , __A , __A , __A , __A , __A ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(__A , __A , __A , __A , __A , OPTS.out_image_dir ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """hasAns""" ) histogram_na_prob(__A , __A , OPTS.out_image_dir , """noAns""" ) if OPTS.out_file: with open(OPTS.out_file , """w""" ) as f: json.dump(__A , __A ) else: print(json.dumps(__A , indent=2 ) ) if __name__ == "__main__": __lowercase : Union[str, Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
36
1
import os import re import shutil import sys import tempfile import unittest import black __lowercase : int = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. __lowercase : str = ''' \""" Output class for the scheduler\'s step function output. Args: prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the denoising loop. pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images): The predicted denoised sample (x_{0}) based on the model output from the current timestep. `pred_original_sample` can be used to preview progress or for guidance. \""" prev_sample: torch.FloatTensor pred_original_sample: Optional[torch.FloatTensor] = None ''' class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' snake_case : int = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir ,"""schedulers/""" ) ) snake_case : str = self.diffusers_dir shutil.copy( os.path.join(SCREAMING_SNAKE_CASE_ ,"""src/diffusers/schedulers/scheduling_ddpm.py""" ) ,os.path.join(self.diffusers_dir ,"""schedulers/scheduling_ddpm.py""" ) ,) def snake_case_ ( self ): '''simple docstring''' snake_case : str = """src/diffusers""" shutil.rmtree(self.diffusers_dir ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=None ): '''simple docstring''' snake_case : str = comment + F"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: snake_case : Tuple = comment + F"""\nclass {class_name}(nn.Module):\n""" + overwrite_result snake_case : str = black.Mode(target_versions={black.TargetVersion.PYaa} ,line_length=119 ) snake_case : int = black.format_str(SCREAMING_SNAKE_CASE_ ,mode=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = os.path.join(self.diffusers_dir ,"""new_code.py""" ) with open(SCREAMING_SNAKE_CASE_ ,"""w""" ,newline="""\n""" ) as f: f.write(SCREAMING_SNAKE_CASE_ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(SCREAMING_SNAKE_CASE_ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name ,overwrite=SCREAMING_SNAKE_CASE_ ) with open(SCREAMING_SNAKE_CASE_ ,"""r""" ) as f: self.assertTrue(f.read() ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : Any = check_copies.find_code_in_diffusers("""schedulers.scheduling_ddpm.DDPMSchedulerOutput""" ) self.assertEqual(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' # Base copy consistency self.check_copy_consistency( """# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput""" ,"""DDPMSchedulerOutput""" ,REFERENCE_CODE + """\n""" ,) # With no empty line at the end self.check_copy_consistency( """# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput""" ,"""DDPMSchedulerOutput""" ,SCREAMING_SNAKE_CASE_ ,) # Copy consistency with rename self.check_copy_consistency( """# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test""" ,"""TestSchedulerOutput""" ,re.sub("""DDPM""" ,"""Test""" ,SCREAMING_SNAKE_CASE_ ) ,) # Copy consistency with a really long name snake_case : Optional[int] = """TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason""" self.check_copy_consistency( F"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" ,F"""{long_class_name}SchedulerOutput""" ,re.sub("""Bert""" ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ,) # Copy consistency with overwrite self.check_copy_consistency( """# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test""" ,"""TestSchedulerOutput""" ,SCREAMING_SNAKE_CASE_ ,overwrite_result=re.sub("""DDPM""" ,"""Test""" ,SCREAMING_SNAKE_CASE_ ) ,)
36
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch __lowercase : Dict = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['''pixel_values'''] def __init__( self ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = PILImageResampling.BILINEAR ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = 1 / 255 ,SCREAMING_SNAKE_CASE_ = True ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = True ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = size if size is not None else {"""shortest_edge""": 224} snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else {"""height""": 256, """width""": 256} snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : Optional[Any] = do_resize snake_case : Union[str, Any] = size snake_case : Dict = resample snake_case : Dict = do_rescale snake_case : Dict = rescale_factor snake_case : List[str] = do_center_crop snake_case : Dict = crop_size snake_case : Any = do_flip_channel_order def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = PIL.Image.BILINEAR ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : str = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) if "shortest_edge" not in size: raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}""" ) snake_case : List[Any] = get_resize_output_image_size(SCREAMING_SNAKE_CASE_ ,size=size["""shortest_edge"""] ,default_to_square=SCREAMING_SNAKE_CASE_ ) return resize(SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) 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()}""" ) return center_crop(SCREAMING_SNAKE_CASE_ ,size=(size["""height"""], size["""width"""]) ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE_ ,data_format=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = None ,SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : List[Any] = do_resize if do_resize is not None else self.do_resize snake_case : List[str] = resample if resample is not None else self.resample snake_case : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale snake_case : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor snake_case : str = do_center_crop if do_center_crop is not None else self.do_center_crop snake_case : Union[str, Any] = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) snake_case : Tuple = size if size is not None else self.size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,default_to_square=SCREAMING_SNAKE_CASE_ ) snake_case : str = crop_size if crop_size is not None else self.crop_size snake_case : Optional[Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ,param_name="""crop_size""" ) snake_case : List[Any] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) # All transformations expect numpy arrays. snake_case : Dict = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: snake_case : Union[str, Any] = [self.resize(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ,resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_center_crop: snake_case : Optional[Any] = [self.center_crop(image=SCREAMING_SNAKE_CASE_ ,size=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: snake_case : Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ ,scale=SCREAMING_SNAKE_CASE_ ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: snake_case : Optional[int] = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : List[Any] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) for image in images] snake_case : int = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ ,tensor_type=SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ = None ): '''simple docstring''' snake_case : Dict = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): snake_case : int = target_sizes.numpy() snake_case : Optional[Any] = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): snake_case : Optional[int] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) ,size=target_sizes[idx] ,mode="""bilinear""" ,align_corners=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: snake_case : Tuple = logits.argmax(dim=1 ) snake_case : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
36
1
from queue import PriorityQueue from typing import Any import numpy as np def lowercase ( __A : dict , __A : str , __A : set , __A : set , __A : dict , __A : dict , __A : PriorityQueue , __A : dict , __A : float | int , ) -> float | int: '''simple docstring''' for nxt, d in graph[v]: if nxt in visited_forward: continue snake_case : Any = cst_fwd.get(__A , np.inf ) snake_case : Any = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) snake_case : Dict = new_cost_f snake_case : int = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: snake_case : Optional[Any] = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def lowercase ( __A : str , __A : str , __A : dict , __A : dict ) -> int: '''simple docstring''' snake_case : Optional[int] = -1 snake_case : Union[str, Any] = set() snake_case : Optional[int] = set() snake_case : Union[str, Any] = {source: 0} snake_case : Optional[Any] = {destination: 0} snake_case : Any = {source: None} snake_case : Any = {destination: None} snake_case : PriorityQueue[Any] = PriorityQueue() snake_case : PriorityQueue[Any] = PriorityQueue() snake_case : List[Any] = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): snake_case , snake_case : str = queue_forward.get() visited_forward.add(__A ) snake_case , snake_case : str = queue_backward.get() visited_backward.add(__A ) snake_case : List[Any] = pass_and_relaxation( __A , __A , __A , __A , __A , __A , __A , __A , __A , ) snake_case : Any = pass_and_relaxation( __A , __A , __A , __A , __A , __A , __A , __A , __A , ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: snake_case : Any = shortest_distance return shortest_path_distance __lowercase : List[Any] = { '''B''': [['''C''', 1]], '''C''': [['''D''', 1]], '''D''': [['''F''', 1]], '''E''': [['''B''', 1], ['''G''', 2]], '''F''': [], '''G''': [['''F''', 1]], } __lowercase : Optional[int] = { '''B''': [['''E''', 1]], '''C''': [['''B''', 1]], '''D''': [['''C''', 1]], '''F''': [['''D''', 1], ['''G''', 1]], '''E''': [[None, np.inf]], '''G''': [['''E''', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
36
import fire from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer def lowercase ( __A : str , __A : str , **__A : Optional[int] ) -> Optional[Any]: '''simple docstring''' snake_case : int = AutoConfig.from_pretrained(__A , **__A ) snake_case : Tuple = AutoModelForSeqaSeqLM.from_config(__A ) model.save_pretrained(__A ) AutoTokenizer.from_pretrained(__A ).save_pretrained(__A ) return model if __name__ == "__main__": fire.Fire(save_randomly_initialized_version)
36
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, ) __lowercase : Optional[int] = logging.getLogger(__name__) __lowercase : Optional[Any] = {'''facebook/bart-base''': BartForConditionalGeneration} __lowercase : str = {'''facebook/bart-base''': BartTokenizer} def lowercase ( ) -> int: '''simple docstring''' snake_case : List[str] = 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.""" ) snake_case : str = parser.parse_args() return args def lowercase ( __A : int , __A : Union[str, Any]="cpu" ) -> Tuple: '''simple docstring''' snake_case : Any = model_dict[model_name].from_pretrained(__A ).to(__A ) snake_case : Any = tokenizer_dict[model_name].from_pretrained(__A ) if model_name in ["facebook/bart-base"]: snake_case : Tuple = 0 snake_case : List[str] = None snake_case : List[Any] = 0 return huggingface_model, tokenizer def lowercase ( __A : Optional[int] , __A : List[Any] , __A : List[Any] , __A : Union[str, Any] , __A : Tuple ) -> Tuple: '''simple docstring''' model.eval() snake_case : Tuple = None snake_case : Any = torch.jit.script(BARTBeamSearchGenerator(__A ) ) with torch.no_grad(): snake_case : str = """My friends are cool but they eat too many carbs.""" snake_case : Optional[Any] = tokenizer([ARTICLE_TO_SUMMARIZE] , max_length=1024 , return_tensors="""pt""" ).to(model.device ) snake_case : Optional[int] = 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 ) ) snake_case : Any = remove_dup_initializers(os.path.abspath(__A ) ) logger.info("""Deduplicated and optimized model written to {}""".format(__A ) ) snake_case : Union[str, Any] = onnxruntime.InferenceSession(__A ) snake_case : Optional[int] = 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 lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[int] = parse_args() snake_case : str = 5 snake_case : Union[str, 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() snake_case : Union[str, Any] = torch.device(args.device ) snake_case , snake_case : Dict = 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: snake_case : Union[str, Any] = args.max_length if args.num_beams: snake_case : Any = args.num_beams if args.output_file_path: snake_case : Optional[int] = args.output_file_path else: snake_case : str = """BART.onnx""" logger.info("""Exporting model to ONNX""" ) export_and_validate_model(__A , __A , __A , __A , __A ) if __name__ == "__main__": main()
36
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 __lowercase : Any = logging.get_logger(__name__) __lowercase : str = { '''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 _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Dict = '''mobilenet_v1''' def __init__( self ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=224 ,SCREAMING_SNAKE_CASE_=1.0 ,SCREAMING_SNAKE_CASE_=8 ,SCREAMING_SNAKE_CASE_="relu6" ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=0.9_99 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=0.0_01 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case : List[Any] = num_channels snake_case : str = image_size snake_case : List[Any] = depth_multiplier snake_case : Optional[int] = min_depth snake_case : Union[str, Any] = hidden_act snake_case : int = tf_padding snake_case : Optional[int] = classifier_dropout_prob snake_case : Tuple = initializer_range snake_case : List[str] = layer_norm_eps class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : Optional[Any] = version.parse('''1.11''' ) @property def snake_case_ ( self ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def snake_case_ ( self ): '''simple docstring''' return 1E-4
36
1
from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
36
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowercase : List[str] = logging.get_logger(__name__) __lowercase : List[str] = { '''edbeeching/decision-transformer-gym-hopper-medium''': ( '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json''' ), # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''decision_transformer''' __lowerCamelCase : Optional[Any] = ['''past_key_values'''] __lowerCamelCase : Tuple = { '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self ,SCREAMING_SNAKE_CASE_=17 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=1024 ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=None ,SCREAMING_SNAKE_CASE_="relu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=1E-5 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=50256 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=False ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' snake_case : Any = state_dim snake_case : Optional[Any] = act_dim snake_case : Union[str, Any] = hidden_size snake_case : Any = max_ep_len snake_case : int = action_tanh snake_case : Any = vocab_size snake_case : Any = n_positions snake_case : List[str] = n_layer snake_case : int = n_head snake_case : Optional[int] = n_inner snake_case : List[Any] = activation_function snake_case : Tuple = resid_pdrop snake_case : Optional[Any] = embd_pdrop snake_case : Dict = attn_pdrop snake_case : List[str] = layer_norm_epsilon snake_case : Union[str, Any] = initializer_range snake_case : Optional[Any] = scale_attn_weights snake_case : str = use_cache snake_case : int = scale_attn_by_inverse_layer_idx snake_case : Tuple = reorder_and_upcast_attn snake_case : Tuple = bos_token_id snake_case : List[str] = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowercase : str = {'''configuration_xlnet''': ['''XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLNetConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Optional[int] = ['''XLNetTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Union[str, Any] = ['''XLNetTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : str = [ '''XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLNetForMultipleChoice''', '''XLNetForQuestionAnswering''', '''XLNetForQuestionAnsweringSimple''', '''XLNetForSequenceClassification''', '''XLNetForTokenClassification''', '''XLNetLMHeadModel''', '''XLNetModel''', '''XLNetPreTrainedModel''', '''load_tf_weights_in_xlnet''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase : Tuple = [ '''TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLNetForMultipleChoice''', '''TFXLNetForQuestionAnsweringSimple''', '''TFXLNetForSequenceClassification''', '''TFXLNetForTokenClassification''', '''TFXLNetLMHeadModel''', '''TFXLNetMainLayer''', '''TFXLNetModel''', '''TFXLNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet import XLNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet_fast import XLNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlnet import ( XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, XLNetForMultipleChoice, XLNetForQuestionAnswering, XLNetForQuestionAnsweringSimple, XLNetForSequenceClassification, XLNetForTokenClassification, XLNetLMHeadModel, XLNetModel, XLNetPreTrainedModel, load_tf_weights_in_xlnet, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlnet import ( TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLNetForMultipleChoice, TFXLNetForQuestionAnsweringSimple, TFXLNetForSequenceClassification, TFXLNetForTokenClassification, TFXLNetLMHeadModel, TFXLNetMainLayer, TFXLNetModel, TFXLNetPreTrainedModel, ) else: import sys __lowercase : List[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
36
from ..utils import ( OptionalDependencyNotAvailable, is_flax_available, is_scipy_available, is_torch_available, is_torchsde_available, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_pt_objects import * # noqa F403 else: from .scheduling_consistency_models import CMStochasticIterativeScheduler from .scheduling_ddim import DDIMScheduler from .scheduling_ddim_inverse import DDIMInverseScheduler from .scheduling_ddim_parallel import DDIMParallelScheduler from .scheduling_ddpm import DDPMScheduler from .scheduling_ddpm_parallel import DDPMParallelScheduler from .scheduling_deis_multistep import DEISMultistepScheduler from .scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler from .scheduling_dpmsolver_multistep_inverse import DPMSolverMultistepInverseScheduler from .scheduling_dpmsolver_singlestep import DPMSolverSinglestepScheduler from .scheduling_euler_ancestral_discrete import EulerAncestralDiscreteScheduler from .scheduling_euler_discrete import EulerDiscreteScheduler from .scheduling_heun_discrete import HeunDiscreteScheduler from .scheduling_ipndm import IPNDMScheduler from .scheduling_k_dpm_2_ancestral_discrete import KDPMaAncestralDiscreteScheduler from .scheduling_k_dpm_2_discrete import KDPMaDiscreteScheduler from .scheduling_karras_ve import KarrasVeScheduler from .scheduling_pndm import PNDMScheduler from .scheduling_repaint import RePaintScheduler from .scheduling_sde_ve import ScoreSdeVeScheduler from .scheduling_sde_vp import ScoreSdeVpScheduler from .scheduling_unclip import UnCLIPScheduler from .scheduling_unipc_multistep import UniPCMultistepScheduler from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin from .scheduling_vq_diffusion import VQDiffusionScheduler try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_flax_objects import * # noqa F403 else: from .scheduling_ddim_flax import FlaxDDIMScheduler from .scheduling_ddpm_flax import FlaxDDPMScheduler from .scheduling_dpmsolver_multistep_flax import FlaxDPMSolverMultistepScheduler from .scheduling_karras_ve_flax import FlaxKarrasVeScheduler from .scheduling_lms_discrete_flax import FlaxLMSDiscreteScheduler from .scheduling_pndm_flax import FlaxPNDMScheduler from .scheduling_sde_ve_flax import FlaxScoreSdeVeScheduler from .scheduling_utils_flax import ( FlaxKarrasDiffusionSchedulers, FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left, ) try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .scheduling_lms_discrete import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ..utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .scheduling_dpmsolver_sde import DPMSolverSDEScheduler
36
1
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 lowercase ( __A : List[str] , __A : int , __A : str ) -> List[str]: '''simple docstring''' return params[f"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :] def lowercase ( __A : List[str] , __A : Tuple , __A : List[Any] , __A : List[Any]="attention" ) -> Optional[int]: '''simple docstring''' snake_case : Dict = np.ascontiguousarray(params[f"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :] ) snake_case : Any = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) snake_case : Optional[Any] = np.ascontiguousarray(params[f"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :] ) snake_case : Union[str, Any] = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) snake_case : List[str] = np.ascontiguousarray(params[f"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :] ) snake_case : Union[str, Any] = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) snake_case : Union[str, Any] = np.ascontiguousarray(params[f"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :] ) snake_case : Any = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def lowercase ( __A : str , __A : List[str] , __A : Dict , __A : Union[str, Any]=False ) -> Optional[Any]: '''simple docstring''' if split_mlp_wi: snake_case : List[Any] = params[f"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :] snake_case : Optional[int] = params[f"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :] snake_case : Any = (wi_a, wi_a) else: snake_case : Optional[int] = params[f"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :] snake_case : Union[str, Any] = params[f"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :] return wi, wo def lowercase ( __A : Dict , __A : List[Any] , __A : Union[str, Any] , __A : int ) -> Any: '''simple docstring''' return params[f"""{prefix}/{prefix}/{layer_name}/scale"""][:, i] def lowercase ( __A : dict , *, __A : int , __A : bool , __A : bool = False ) -> Union[str, Any]: '''simple docstring''' snake_case : int = traverse_util.flatten_dict(variables["""target"""] ) snake_case : Dict = {"""/""".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 snake_case : Optional[int] = """encoder/encoder/mlp/wi_0/kernel""" in old print("""Split MLP:""" , __A ) snake_case : List[Any] = collections.OrderedDict() # Shared embeddings. snake_case : Optional[Any] = old["""token_embedder/embedding"""] # Encoder. for i in range(__A ): # Block i, layer 0 (Self Attention). snake_case : Optional[Any] = tax_layer_norm_lookup(__A , __A , """encoder""" , """pre_attention_layer_norm""" ) snake_case , snake_case , snake_case , snake_case : Any = tax_attention_lookup(__A , __A , """encoder""" , """attention""" ) snake_case : Optional[int] = layer_norm snake_case : Tuple = k.T snake_case : Optional[Any] = o.T snake_case : Dict = q.T snake_case : Dict = v.T # Block i, layer 1 (MLP). snake_case : Dict = tax_layer_norm_lookup(__A , __A , """encoder""" , """pre_mlp_layer_norm""" ) snake_case , snake_case : Any = tax_mlp_lookup(__A , __A , """encoder""" , __A ) snake_case : Dict = layer_norm if split_mlp_wi: snake_case : Union[str, Any] = wi[0].T snake_case : str = wi[1].T else: snake_case : List[Any] = wi.T snake_case : Dict = wo.T if scalable_attention: # convert the rel_embedding of each layer snake_case : Optional[int] = tax_relpos_bias_lookup( __A , __A , """encoder""" ).T snake_case : Any = old["""encoder/encoder_norm/scale"""] if not scalable_attention: snake_case : int = tax_relpos_bias_lookup( __A , 0 , """encoder""" ).T snake_case : Tuple = 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). snake_case : int = tax_layer_norm_lookup(__A , __A , """decoder""" , """pre_self_attention_layer_norm""" ) snake_case , snake_case , snake_case , snake_case : Any = tax_attention_lookup(__A , __A , """decoder""" , """self_attention""" ) snake_case : Union[str, Any] = layer_norm snake_case : str = k.T snake_case : Dict = o.T snake_case : Optional[Any] = q.T snake_case : int = v.T # Block i, layer 1 (Cross Attention). snake_case : int = tax_layer_norm_lookup(__A , __A , """decoder""" , """pre_cross_attention_layer_norm""" ) snake_case , snake_case , snake_case , snake_case : Optional[Any] = tax_attention_lookup(__A , __A , """decoder""" , """encoder_decoder_attention""" ) snake_case : Union[str, Any] = layer_norm snake_case : int = k.T snake_case : Dict = o.T snake_case : Optional[Any] = q.T snake_case : Tuple = v.T # Block i, layer 2 (MLP). snake_case : int = tax_layer_norm_lookup(__A , __A , """decoder""" , """pre_mlp_layer_norm""" ) snake_case , snake_case : Union[str, Any] = tax_mlp_lookup(__A , __A , """decoder""" , __A ) snake_case : List[Any] = layer_norm if split_mlp_wi: snake_case : Tuple = wi[0].T snake_case : Any = wi[1].T else: snake_case : Union[str, Any] = wi.T snake_case : Union[str, Any] = wo.T if scalable_attention: # convert the rel_embedding of each layer snake_case : int = tax_relpos_bias_lookup(__A , __A , """decoder""" ).T snake_case : Union[str, Any] = 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: snake_case : Dict = old["""decoder/logits_dense/kernel"""].T return new def lowercase ( __A : str , __A : bool ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[Any] = 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: snake_case : Optional[int] = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: snake_case : str = 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.""" ) snake_case : Any = state_dict["""shared.weight"""] return state_dict def lowercase ( __A : int , __A : Dict , __A : Union[str, Any] , __A : Union[str, Any] , __A : List[Any] ) -> List[Any]: '''simple docstring''' snake_case : int = checkpoints.load_tax_checkpoint(__A ) snake_case : Any = convert_tax_to_pytorch( __A , num_layers=config.num_layers , is_encoder_only=__A , scalable_attention=__A ) snake_case : Optional[int] = make_state_dict(__A , __A ) model.load_state_dict(__A , strict=__A ) def lowercase ( __A : str , __A : int , __A : Optional[int] , __A : bool = False , __A : bool = False , ) -> List[str]: '''simple docstring''' snake_case : 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: snake_case : Tuple = UMTaEncoderModel(__A ) else: snake_case : str = 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__": __lowercase : str = 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, ) __lowercase : List[str] = 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, )
36
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase ( __A : Dict , __A : Union[str, Any] , __A : List[str] ) -> Any: '''simple docstring''' snake_case : Tuple = { """en""": """Machine learning is great, isn't it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] snake_case : Optional[Any] = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } snake_case : Optional[int] = f"""{src_lang}-{tgt_lang}""" snake_case : Any = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn't seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn't support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR's WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) """ os.makedirs(__A , exist_ok=__A ) snake_case : Union[str, Any] = os.path.join(__A , """README.md""" ) print(f"""Generating {path}""" ) with open(__A , """w""" , encoding="""utf-8""" ) as f: f.write(__A ) # make sure we are under the root of the project __lowercase : int = Path(__file__).resolve().parent.parent.parent __lowercase : List[str] = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __lowercase , __lowercase , __lowercase : List[str] = model_name.split('''-''') __lowercase : str = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
36
1
import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def lowercase ( __A : Tuple ) -> Union[str, Any]: '''simple docstring''' snake_case : Optional[int] = SwinConfig(image_size=192 ) if "base" in model_name: snake_case : Dict = 6 snake_case : Optional[Any] = 128 snake_case : Dict = (2, 2, 18, 2) snake_case : Optional[int] = (4, 8, 16, 32) elif "large" in model_name: snake_case : Tuple = 12 snake_case : int = 192 snake_case : Dict = (2, 2, 18, 2) snake_case : int = (6, 12, 24, 48) else: raise ValueError("""Model not supported, only supports base and large variants""" ) snake_case : Union[str, Any] = window_size snake_case : str = embed_dim snake_case : List[Any] = depths snake_case : Tuple = num_heads return config def lowercase ( __A : List[str] ) -> Union[str, Any]: '''simple docstring''' if "encoder.mask_token" in name: snake_case : Any = name.replace("""encoder.mask_token""" , """embeddings.mask_token""" ) if "encoder.patch_embed.proj" in name: snake_case : Tuple = name.replace("""encoder.patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "encoder.patch_embed.norm" in name: snake_case : Optional[int] = name.replace("""encoder.patch_embed.norm""" , """embeddings.norm""" ) if "attn.proj" in name: snake_case : List[Any] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: snake_case : Optional[int] = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: snake_case : Optional[int] = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: snake_case : List[Any] = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: snake_case : List[str] = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: snake_case : Union[str, Any] = name.replace("""mlp.fc2""" , """output.dense""" ) if name == "encoder.norm.weight": snake_case : int = """layernorm.weight""" if name == "encoder.norm.bias": snake_case : Union[str, Any] = """layernorm.bias""" if "decoder" in name: pass else: snake_case : Union[str, Any] = """swin.""" + name return name def lowercase ( __A : List[Any] , __A : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for key in orig_state_dict.copy().keys(): snake_case : Optional[int] = orig_state_dict.pop(__A ) if "attn_mask" in key: pass elif "qkv" in key: snake_case : List[Any] = key.split(""".""" ) snake_case : Optional[int] = int(key_split[2] ) snake_case : Union[str, Any] = int(key_split[4] ) snake_case : str = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: snake_case : Optional[Any] = val[:dim, :] snake_case : Optional[Any] = val[ dim : dim * 2, : ] snake_case : str = val[-dim:, :] else: snake_case : int = val[ :dim ] snake_case : str = val[ dim : dim * 2 ] snake_case : int = val[ -dim: ] else: snake_case : Dict = val return orig_state_dict def lowercase ( __A : Tuple , __A : Tuple , __A : Any , __A : int ) -> Dict: '''simple docstring''' snake_case : Any = torch.load(__A , map_location="""cpu""" )["""model"""] snake_case : List[Any] = get_swin_config(__A ) snake_case : Optional[Any] = SwinForMaskedImageModeling(__A ) model.eval() snake_case : str = convert_state_dict(__A , __A ) model.load_state_dict(__A ) snake_case : List[Any] = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case : Optional[int] = ViTImageProcessor(size={"""height""": 192, """width""": 192} ) snake_case : Any = Image.open(requests.get(__A , stream=__A ).raw ) snake_case : Tuple = image_processor(images=__A , return_tensors="""pt""" ) with torch.no_grad(): snake_case : Tuple = model(**__A ).logits print(outputs.keys() ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(f"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__A ) if push_to_hub: print(f"""Pushing model and image processor for {model_name} to hub""" ) model.push_to_hub(f"""microsoft/{model_name}""" ) image_processor.push_to_hub(f"""microsoft/{model_name}""" ) if __name__ == "__main__": __lowercase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''swin-base-simmim-window6-192''', type=str, choices=['''swin-base-simmim-window6-192''', '''swin-large-simmim-window12-192'''], help='''Name of the Swin SimMIM model you\'d like to convert.''', ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth''', type=str, help='''Path to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) __lowercase : str = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
36
__lowercase : List[str] = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' __lowercase : str = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] __lowercase : List[str] = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
36
1
import contextlib import importlib import io import unittest import transformers # Try to import everything from transformers to ensure every object can be loaded. from transformers import * # noqa F406 from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available if is_torch_available(): from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification if is_tf_available(): from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification if is_flax_available(): from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification __lowercase : Tuple = DUMMY_UNKNOWN_IDENTIFIER # An actual model hosted on huggingface.co __lowercase : Optional[Any] = '''main''' # Default branch name __lowercase : List[str] = '''f2c752cfc5c0ab6f4bdec59acea69eefbee381c2''' # One particular commit (not the top of `main`) __lowercase : List[Any] = '''aaaaaaa''' # This commit does not exist, so we should 404. __lowercase : Union[str, Any] = '''d9e9f15bc825e4b2c9249e9578f884bbcb5e3684''' # Sha-1 of config.json on the top of `main`, for checking purposes __lowercase : Tuple = '''4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3''' @contextlib.contextmanager def lowercase ( ) -> str: '''simple docstring''' print("""Welcome!""" ) yield print("""Bye!""" ) @contextlib.contextmanager def lowercase ( ) -> List[str]: '''simple docstring''' print("""Bonjour!""" ) yield print("""Au revoir!""" ) class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' # If the spec is missing, importlib would not be able to import the module dynamically. assert transformers.__spec__ is not None assert importlib.util.find_spec("""transformers""" ) is not None class _A ( unittest.TestCase ): '''simple docstring''' @unittest.mock.patch("""sys.stdout""" ,new_callable=io.StringIO ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with ContextManagers([] ): print("""Transformers are awesome!""" ) # The print statement adds a new line at the end of the output self.assertEqual(mock_stdout.getvalue() ,"""Transformers are awesome!\n""" ) @unittest.mock.patch("""sys.stdout""" ,new_callable=io.StringIO ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with ContextManagers([context_en()] ): print("""Transformers are awesome!""" ) # The output should be wrapped with an English welcome and goodbye self.assertEqual(mock_stdout.getvalue() ,"""Welcome!\nTransformers are awesome!\nBye!\n""" ) @unittest.mock.patch("""sys.stdout""" ,new_callable=io.StringIO ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' with ContextManagers([context_fr(), context_en()] ): print("""Transformers are awesome!""" ) # The output should be wrapped with an English and French welcome and goodbye self.assertEqual(mock_stdout.getvalue() ,"""Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n""" ) @require_torch def snake_case_ ( self ): '''simple docstring''' self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""labels"""] ) self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""labels""", """next_sentence_label"""] ) self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""start_positions""", """end_positions"""] ) class _A ( snake_case ): '''simple docstring''' pass self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""labels"""] ) @require_tf def snake_case_ ( self ): '''simple docstring''' self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""labels"""] ) self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""labels""", """next_sentence_label"""] ) self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""start_positions""", """end_positions"""] ) class _A ( snake_case ): '''simple docstring''' pass self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,["""labels"""] ) @require_flax def snake_case_ ( self ): '''simple docstring''' # Flax models don't have labels self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,[] ) self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,[] ) self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,[] ) class _A ( snake_case ): '''simple docstring''' pass self.assertEqual(find_labels(SCREAMING_SNAKE_CASE_ ) ,[] )
36
import warnings from ..trainer import Trainer from ..utils import logging __lowercase : str = logging.get_logger(__name__) class _A ( snake_case ): '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_=None ,**SCREAMING_SNAKE_CASE_ ): '''simple docstring''' warnings.warn( """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """ """instead.""" ,SCREAMING_SNAKE_CASE_ ,) super().__init__(args=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ )
36
1
# Copyright (c) 2021-, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def lowercase ( __A : Union[str, Any] , __A : Any , __A : Any=0 ) -> Any: '''simple docstring''' if name is None: snake_case : str = None else: snake_case : int = """.""" * max(0 , spaces - 2 ) + """# {:""" + str(50 - spaces ) + """s}""" snake_case : Tuple = fmt.format(__A ) # Print and recurse (if needed). if isinstance(__A , __A ): if msg is not None: print(__A ) for k in val.keys(): recursive_print(__A , val[k] , spaces + 2 ) elif isinstance(__A , torch.Tensor ): print(__A , """:""" , val.size() ) else: print(__A , """:""" , __A ) def lowercase ( __A : Any , __A : Dict , __A : str , __A : Tuple , __A : Optional[int] ) -> List[str]: '''simple docstring''' snake_case : int = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] snake_case : List[str] = (num_heads, hidden_size, num_splits) + input_shape[1:] snake_case : Optional[int] = param.view(*__A ) snake_case : Dict = param.transpose(0 , 2 ) snake_case : int = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] snake_case : Tuple = (num_heads, num_splits, hidden_size) + input_shape[1:] snake_case : Union[str, Any] = param.view(*__A ) snake_case : Dict = param.transpose(0 , 1 ).contiguous() snake_case : Any = param.view(*__A ) return param def lowercase ( __A : str , __A : Dict , __A : str ) -> List[Any]: '''simple docstring''' snake_case : Dict = {} # old versions did not store training args snake_case : int = input_state_dict.get("""args""" , __A ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) snake_case : Dict = ds_args.padded_vocab_size snake_case : Dict = ds_args.max_position_embeddings snake_case : Optional[Any] = ds_args.hidden_size snake_case : List[str] = ds_args.num_layers snake_case : str = ds_args.num_attention_heads snake_case : Optional[Any] = ds_args.ffn_hidden_size # pprint(config) # The number of heads. snake_case : List[str] = config.n_head # The hidden_size per head. snake_case : List[Any] = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): snake_case : List[Any] = input_state_dict["""checkpoint_version"""] else: snake_case : int = 0.0 # The model. snake_case : Optional[int] = input_state_dict["""model"""] # The language model. snake_case : Union[str, Any] = model["""language_model"""] # The embeddings. snake_case : List[str] = lm["""embedding"""] # The word embeddings. snake_case : Any = embeddings["""word_embeddings"""]["""weight"""] # Truncate the embedding table to vocab_size rows. snake_case : Union[str, Any] = word_embeddings[: config.vocab_size, :] snake_case : str = word_embeddings # The position embeddings. snake_case : Tuple = embeddings["""position_embeddings"""]["""weight"""] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] snake_case : Optional[Any] = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f"""pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don't match""" ) # Store the position embeddings. snake_case : Tuple = pos_embeddings # The transformer. snake_case : Union[str, Any] = lm["""transformer"""] if """transformer""" in lm.keys() else lm["""encoder"""] # The regex to extract layer names. snake_case : Optional[int] = re.compile(r"""layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)""" ) # The simple map of names for "automated" rules. snake_case : str = { """attention.dense""": """.attn.c_proj.""", """self_attention.dense""": """.attn.c_proj.""", """mlp.dense_h_to_4h""": """.mlp.c_fc.""", """mlp.dense_4h_to_h""": """.mlp.c_proj.""", } # Extract the layers. for key, val in transformer.items(): # Match the name. snake_case : Optional[Any] = layer_re.match(__A ) # Stop if that's not a layer if m is None: break # The index of the layer. snake_case : List[str] = int(m.group(1 ) ) # The name of the operation. snake_case : Any = m.group(2 ) # Is it a weight or a bias? snake_case : Any = m.group(3 ) # The name of the layer. snake_case : Tuple = f"""transformer.h.{layer_idx}""" # For layernorm(s), simply store the layer norm. if op_name.endswith("""layernorm""" ): snake_case : Any = """ln_1""" if op_name.startswith("""input""" ) else """ln_2""" snake_case : Any = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. snake_case : Any = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , __A , __A ) snake_case : List[str] = causal_mask # Insert a "dummy" tensor for masked_bias. snake_case : Optional[int] = torch.tensor(-1E4 , dtype=torch.floataa ) snake_case : Any = masked_bias snake_case : Optional[Any] = fix_query_key_value_ordering(__A , __A , 3 , __A , __A ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. snake_case : int = out_val.transpose(0 , 1 ).contiguous() # Store. snake_case : List[Any] = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": snake_case : List[str] = fix_query_key_value_ordering(__A , __A , 3 , __A , __A ) # Store. No change of shape. snake_case : List[str] = out_val # Transpose the weights. elif weight_or_bias == "weight": snake_case : str = megatron_to_transformers[op_name] snake_case : Tuple = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": snake_case : List[Any] = megatron_to_transformers[op_name] snake_case : int = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. snake_case : List[Any] = transformer["""final_layernorm.weight"""] snake_case : Any = transformer["""final_layernorm.bias"""] # For LM head, transformers' wants the matrix to weight embeddings. snake_case : List[Any] = word_embeddings # It should be done! return output_state_dict def lowercase ( ) -> Any: '''simple docstring''' snake_case : List[Any] = argparse.ArgumentParser() parser.add_argument("""--print-checkpoint-structure""" , action="""store_true""" ) parser.add_argument( """path_to_checkpoint""" , type=__A , help="""Path to the checkpoint file (.zip archive or direct .pt file)""" , ) parser.add_argument( """--config_file""" , default="""""" , type=__A , help="""An optional config json file describing the pre-trained model.""" , ) snake_case : List[Any] = parser.parse_args() # Extract the basename. snake_case : Tuple = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f"""Extracting PyTorch state dictionary from {args.path_to_checkpoint}""" ) if args.path_to_checkpoint.endswith(""".zip""" ): with zipfile.ZipFile(args.path_to_checkpoint , """r""" ) as checkpoint: with checkpoint.open("""release/mp_rank_00/model_optim_rng.pt""" ) as pytorch_dict: snake_case : List[str] = torch.load(__A , map_location="""cpu""" ) else: snake_case : int = torch.load(args.path_to_checkpoint , map_location="""cpu""" ) snake_case : Dict = input_state_dict.get("""args""" , __A ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: snake_case : int = """gelu_fast""" elif ds_args.openai_gelu: snake_case : Union[str, Any] = """gelu_new""" else: snake_case : int = """gelu""" else: # in the very early days this used to be "gelu_new" snake_case : str = """gelu_new""" # Spell out all parameters in case the defaults change. snake_case : List[Any] = GPTaConfig( vocab_size=5_0257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=__A , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1E-5 , initializer_range=0.02 , summary_type="""cls_index""" , summary_use_proj=__A , summary_activation=__A , summary_proj_to_labels=__A , summary_first_dropout=0.1 , scale_attn_weights=__A , use_cache=__A , bos_token_id=5_0256 , eos_token_id=5_0256 , ) else: snake_case : int = GPTaConfig.from_json_file(args.config_file ) snake_case : int = ["""GPT2LMHeadModel"""] # Convert. print("""Converting""" ) snake_case : str = convert_megatron_checkpoint(__A , __A , __A ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(__A , __A ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: snake_case : str = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": snake_case : Tuple = """gpt2""" elif tokenizer_type == "PretrainedFromHF": snake_case : List[str] = ds_args.tokenizer_name_or_path else: raise ValueError(f"""Unrecognized tokenizer_type {tokenizer_type}""" ) else: snake_case : Any = """gpt2""" snake_case : List[str] = AutoTokenizer.from_pretrained(__A ) snake_case : Optional[Any] = type(__A ).__name__ snake_case : Tuple = tokenizer_class # Store the config to file. print("""Saving config""" ) config.save_pretrained(__A ) # Save tokenizer based on args print(f"""Adding {tokenizer_class} tokenizer files""" ) tokenizer.save_pretrained(__A ) # Store the state_dict to file. snake_case : List[str] = os.path.join(__A , """pytorch_model.bin""" ) print(f"""Saving checkpoint to \"{output_checkpoint_file}\"""" ) torch.save(__A , __A ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
36
from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image __lowercase : List[str] = ['''text''', '''image''', '''audio'''] def lowercase ( __A : List[str] ) -> List[str]: '''simple docstring''' snake_case : Optional[int] = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(__A , __A ): inputs.append(create_inputs(__A ) ) else: raise ValueError(f"""Invalid type requested: {input_type}""" ) return inputs def lowercase ( __A : List ) -> Union[str, Any]: '''simple docstring''' snake_case : Dict = [] for output in outputs: if isinstance(__A , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__A , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__A , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(f"""Invalid output: {output}""" ) return output_types @is_tool_test class _A : '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) snake_case : Dict = self.tool.inputs for _input in inputs: if isinstance(_input ,SCREAMING_SNAKE_CASE_ ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) snake_case : int = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = create_inputs(self.tool.inputs ) snake_case : str = self.tool(*SCREAMING_SNAKE_CASE_ ) # There is a single output if len(self.tool.outputs ) == 1: snake_case : Union[str, Any] = [outputs] self.assertListEqual(output_types(SCREAMING_SNAKE_CASE_ ) ,self.tool.outputs ) def snake_case_ ( self ): '''simple docstring''' self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = create_inputs(self.tool.inputs ) snake_case : int = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Any = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) ) for output, output_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.outputs ): snake_case : List[str] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = create_inputs(self.tool.inputs ) snake_case : Any = [] for _input, input_type in zip(SCREAMING_SNAKE_CASE_ ,self.tool.inputs ): if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error snake_case : Tuple = self.tool(*SCREAMING_SNAKE_CASE_ ) if not isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): snake_case : Union[str, Any] = [outputs] self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) ,len(self.tool.outputs ) )
36
1
import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed __lowercase : Union[str, Any] = '''true''' def lowercase ( __A : str , __A : Dict=82 , __A : List[Any]=16 ) -> Optional[int]: '''simple docstring''' set_seed(42 ) snake_case : List[Any] = RegressionModel() snake_case : Tuple = deepcopy(__A ) snake_case : Optional[int] = RegressionDataset(length=__A ) snake_case : Tuple = DataLoader(__A , batch_size=__A ) model.to(accelerator.device ) snake_case , snake_case : Tuple = accelerator.prepare(__A , __A ) return model, ddp_model, dataloader def lowercase ( __A : Accelerator , __A : List[Any]=False ) -> List[Any]: '''simple docstring''' snake_case : Dict = AutoTokenizer.from_pretrained("""hf-internal-testing/mrpc-bert-base-cased""" ) snake_case : Optional[Any] = load_dataset("""glue""" , """mrpc""" , split="""validation""" ) def tokenize_function(__A : Tuple ): snake_case : List[Any] = tokenizer(examples["""sentence1"""] , examples["""sentence2"""] , truncation=__A , max_length=__A ) return outputs with accelerator.main_process_first(): snake_case : Dict = dataset.map( __A , batched=__A , remove_columns=["""idx""", """sentence1""", """sentence2"""] , ) snake_case : Any = tokenized_datasets.rename_column("""label""" , """labels""" ) def collate_fn(__A : List[str] ): if use_longest: return tokenizer.pad(__A , padding="""longest""" , return_tensors="""pt""" ) return tokenizer.pad(__A , padding="""max_length""" , max_length=128 , return_tensors="""pt""" ) return DataLoader(__A , shuffle=__A , collate_fn=__A , batch_size=16 ) def lowercase ( __A : Any , __A : List[str] ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[int] = Accelerator(dispatch_batches=__A , split_batches=__A ) snake_case : Optional[Any] = get_dataloader(__A , not dispatch_batches ) snake_case : Optional[Any] = AutoModelForSequenceClassification.from_pretrained( """hf-internal-testing/mrpc-bert-base-cased""" , return_dict=__A ) snake_case , snake_case : List[str] = accelerator.prepare(__A , __A ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def lowercase ( __A : Any , __A : Dict , __A : Any ) -> List[str]: '''simple docstring''' snake_case : str = [] for batch in dataloader: snake_case , snake_case : List[Any] = batch.values() with torch.no_grad(): snake_case : Optional[Any] = model(__A ) snake_case , snake_case : Union[str, Any] = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) snake_case , snake_case : Optional[Any] = [], [] for logit, targ in logits_and_targets: logits.append(__A ) targs.append(__A ) snake_case , snake_case : Union[str, Any] = torch.cat(__A ), torch.cat(__A ) return logits, targs def lowercase ( __A : Accelerator , __A : Union[str, Any]=82 , __A : Any=False , __A : Dict=False , __A : Tuple=16 ) -> int: '''simple docstring''' snake_case , snake_case , snake_case : Any = get_basic_setup(__A , __A , __A ) snake_case , snake_case : Optional[Any] = generate_predictions(__A , __A , __A ) assert ( len(__A ) == num_samples ), f"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(__A )}""" def lowercase ( __A : bool = False , __A : bool = False ) -> Optional[int]: '''simple docstring''' snake_case : int = evaluate.load("""glue""" , """mrpc""" ) snake_case , snake_case : str = get_mrpc_setup(__A , __A ) # First do baseline snake_case , snake_case , snake_case : Union[str, Any] = setup["""no"""] model.to(__A ) model.eval() for batch in dataloader: batch.to(__A ) with torch.inference_mode(): snake_case : Union[str, Any] = model(**__A ) snake_case : str = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=__A , references=batch["""labels"""] ) snake_case : Optional[int] = metric.compute() # Then do distributed snake_case , snake_case , snake_case : int = setup["""ddp"""] model.eval() for batch in dataloader: with torch.inference_mode(): snake_case : List[str] = model(**__A ) snake_case : Tuple = outputs.logits.argmax(dim=-1 ) snake_case : Any = batch["""labels"""] snake_case , snake_case : Dict = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=__A , references=__A ) snake_case : Union[str, Any] = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), f"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : Optional[int] = Accelerator(split_batches=__A , dispatch_batches=__A ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print("""**Testing gather_for_metrics**""" ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(__A , __A ) accelerator.state._reset_state() if accelerator.is_local_main_process: print("""**Test torch metrics**""" ) for split_batches in [True, False]: for dispatch_batches in [True, False]: snake_case : Union[str, Any] = Accelerator(split_batches=__A , dispatch_batches=__A ) if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(__A , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print("""**Test last batch is not dropped when perfectly divisible**""" ) snake_case : List[Any] = Accelerator() test_torch_metrics(__A , 512 ) accelerator.state._reset_state() def lowercase ( __A : int ) -> str: '''simple docstring''' main() if __name__ == "__main__": main()
36
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) __lowercase : Optional[Any] = pytest.mark.integration @pytest.mark.parametrize("""path""" , ["""paws""", """csv"""] ) def lowercase ( __A : Optional[Any] , __A : Optional[Any] ) -> str: '''simple docstring''' inspect_dataset(__A , __A ) snake_case : List[str] = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.parametrize("""path""" , ["""accuracy"""] ) def lowercase ( __A : Optional[int] , __A : Any ) -> Optional[Any]: '''simple docstring''' inspect_metric(__A , __A ) snake_case : Any = path + """.py""" assert script_name in os.listdir(__A ) assert "__pycache__" not in os.listdir(__A ) @pytest.mark.parametrize( """path, config_name, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Tuple , __A : Dict , __A : Any ) -> Optional[int]: '''simple docstring''' snake_case : List[str] = get_dataset_config_info(__A , config_name=__A ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Tuple , __A : Any , __A : List[str] ) -> Optional[int]: '''simple docstring''' with pytest.raises(__A ): get_dataset_config_info(__A , config_name=__A ) @pytest.mark.parametrize( """path, expected""" , [ ("""squad""", """plain_text"""), ("""acronym_identification""", """default"""), ("""lhoestq/squad""", """plain_text"""), ("""lhoestq/test""", """default"""), ("""lhoestq/demo1""", """lhoestq--demo1"""), ("""dalle-mini/wit""", """dalle-mini--wit"""), ] , ) def lowercase ( __A : Any , __A : Dict ) -> Dict: '''simple docstring''' snake_case : int = get_dataset_config_names(__A ) assert expected in config_names @pytest.mark.parametrize( """path, expected_configs, expected_splits_in_first_config""" , [ ("""squad""", ["""plain_text"""], ["""train""", """validation"""]), ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]), ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[Any] , __A : Dict , __A : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case : List[Any] = get_dataset_infos(__A ) assert list(infos.keys() ) == expected_configs snake_case : Any = expected_configs[0] assert expected_config in infos snake_case : Any = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( """path, expected_config, expected_splits""" , [ ("""squad""", """plain_text""", ["""train""", """validation"""]), ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]), ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]), ] , ) def lowercase ( __A : Optional[int] , __A : Tuple , __A : Union[str, Any] ) -> Optional[int]: '''simple docstring''' snake_case : Dict = get_dataset_infos(__A ) assert expected_config in infos snake_case : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( """path, config_name, expected_exception""" , [ ("""paws""", None, ValueError), ] , ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> int: '''simple docstring''' with pytest.raises(__A ): get_dataset_split_names(__A , config_name=__A )
36
1
import os from itertools import chain from random import randrange, shuffle import pytest from .sola import PokerHand __lowercase : Union[str, Any] = ( '''4S 3H 2C 7S 5H''', '''9D 8H 2C 6S 7H''', '''2D 6D 9D TH 7D''', '''TC 8C 2S JH 6C''', '''JH 8S TH AH QH''', '''TS KS 5S 9S AC''', '''KD 6S 9D TH AD''', '''KS 8D 4D 9S 4S''', # pair '''8C 4S KH JS 4D''', # pair '''QH 8H KD JH 8S''', # pair '''KC 4H KS 2H 8D''', # pair '''KD 4S KC 3H 8S''', # pair '''AH 8S AS KC JH''', # pair '''3H 4C 4H 3S 2H''', # 2 pairs '''5S 5D 2C KH KH''', # 2 pairs '''3C KH 5D 5S KH''', # 2 pairs '''AS 3C KH AD KH''', # 2 pairs '''7C 7S 3S 7H 5S''', # 3 of a kind '''7C 7S KH 2H 7H''', # 3 of a kind '''AC KH QH AH AS''', # 3 of a kind '''2H 4D 3C AS 5S''', # straight (low ace) '''3C 5C 4C 2C 6H''', # straight '''6S 8S 7S 5H 9H''', # straight '''JS QS 9H TS KH''', # straight '''QC KH TS JS AH''', # straight (high ace) '''8C 9C 5C 3C TC''', # flush '''3S 8S 9S 5S KS''', # flush '''4C 5C 9C 8C KC''', # flush '''JH 8H AH KH QH''', # flush '''3D 2H 3H 2C 2D''', # full house '''2H 2C 3S 3H 3D''', # full house '''KH KC 3S 3H 3D''', # full house '''JC 6H JS JD JH''', # 4 of a kind '''JC 7H JS JD JH''', # 4 of a kind '''JC KH JS JD JH''', # 4 of a kind '''2S AS 4S 5S 3S''', # straight flush (low ace) '''2D 6D 3D 4D 5D''', # straight flush '''5C 6C 3C 7C 4C''', # straight flush '''JH 9H TH KH QH''', # straight flush '''JH AH TH KH QH''', # royal flush (high ace straight flush) ) __lowercase : Dict = ( ('''2H 3H 4H 5H 6H''', '''KS AS TS QS JS''', '''Loss'''), ('''2H 3H 4H 5H 6H''', '''AS AD AC AH JD''', '''Win'''), ('''AS AH 2H AD AC''', '''JS JD JC JH 3D''', '''Win'''), ('''2S AH 2H AS AC''', '''JS JD JC JH AD''', '''Loss'''), ('''2S AH 2H AS AC''', '''2H 3H 5H 6H 7H''', '''Win'''), ('''AS 3S 4S 8S 2S''', '''2H 3H 5H 6H 7H''', '''Win'''), ('''2H 3H 5H 6H 7H''', '''2S 3H 4H 5S 6C''', '''Win'''), ('''2S 3H 4H 5S 6C''', '''3D 4C 5H 6H 2S''', '''Tie'''), ('''2S 3H 4H 5S 6C''', '''AH AC 5H 6H AS''', '''Win'''), ('''2S 2H 4H 5S 4C''', '''AH AC 5H 6H AS''', '''Loss'''), ('''2S 2H 4H 5S 4C''', '''AH AC 5H 6H 7S''', '''Win'''), ('''6S AD 7H 4S AS''', '''AH AC 5H 6H 7S''', '''Loss'''), ('''2S AH 4H 5S KC''', '''AH AC 5H 6H 7S''', '''Loss'''), ('''2S 3H 6H 7S 9C''', '''7H 3C TH 6H 9S''', '''Loss'''), ('''4S 5H 6H TS AC''', '''3S 5H 6H TS AC''', '''Win'''), ('''2S AH 4H 5S 6C''', '''AD 4C 5H 6H 2C''', '''Tie'''), ('''AS AH 3H AD AC''', '''AS AH 2H AD AC''', '''Win'''), ('''AH AC 5H 5C QS''', '''AH AC 5H 5C KS''', '''Loss'''), ('''AH AC 5H 5C QS''', '''KH KC 5H 5C QS''', '''Win'''), ('''7C 7S KH 2H 7H''', '''3C 3S AH 2H 3H''', '''Win'''), ('''3C 3S AH 2H 3H''', '''7C 7S KH 2H 7H''', '''Loss'''), ('''6H 5H 4H 3H 2H''', '''5H 4H 3H 2H AH''', '''Win'''), ('''5H 4H 3H 2H AH''', '''5H 4H 3H 2H AH''', '''Tie'''), ('''5H 4H 3H 2H AH''', '''6H 5H 4H 3H 2H''', '''Loss'''), ('''AH AD KS KC AC''', '''AH KD KH AC KC''', '''Win'''), ('''2H 4D 3C AS 5S''', '''2H 4D 3C 6S 5S''', '''Loss'''), ('''2H 3S 3C 3H 2S''', '''3S 3C 2S 2H 2D''', '''Win'''), ('''4D 6D 5D 2D JH''', '''3S 8S 3H TC KH''', '''Loss'''), ('''4S 6C 8S 3S 7S''', '''AD KS 2D 7D 7C''', '''Loss'''), ('''6S 4C 7H 8C 3H''', '''5H JC AH 9D 9C''', '''Loss'''), ('''9D 9H JH TC QH''', '''3C 2S JS 5C 7H''', '''Win'''), ('''2H TC 8S AD 9S''', '''4H TS 7H 2C 5C''', '''Win'''), ('''9D 3S 2C 7S 7C''', '''JC TD 3C TC 9H''', '''Loss'''), ) __lowercase : Dict = ( ('''2H 3H 4H 5H 6H''', True), ('''AS AH 2H AD AC''', False), ('''2H 3H 5H 6H 7H''', True), ('''KS AS TS QS JS''', True), ('''8H 9H QS JS TH''', False), ('''AS 3S 4S 8S 2S''', True), ) __lowercase : Tuple = ( ('''2H 3H 4H 5H 6H''', True), ('''AS AH 2H AD AC''', False), ('''2H 3H 5H 6H 7H''', False), ('''KS AS TS QS JS''', True), ('''8H 9H QS JS TH''', True), ) __lowercase : Tuple = ( ('''2H 4D 3C AS 5S''', True, [5, 4, 3, 2, 14]), ('''2H 5D 3C AS 5S''', False, [14, 5, 5, 3, 2]), ('''JH QD KC AS TS''', False, [14, 13, 12, 11, 10]), ('''9D 3S 2C 7S 7C''', False, [9, 7, 7, 3, 2]), ) __lowercase : Optional[Any] = ( ('''JH AH TH KH QH''', 0), ('''JH 9H TH KH QH''', 0), ('''JC KH JS JD JH''', 7), ('''KH KC 3S 3H 3D''', 6), ('''8C 9C 5C 3C TC''', 0), ('''JS QS 9H TS KH''', 0), ('''7C 7S KH 2H 7H''', 3), ('''3C KH 5D 5S KH''', 2), ('''QH 8H KD JH 8S''', 1), ('''2D 6D 9D TH 7D''', 0), ) __lowercase : int = ( ('''JH AH TH KH QH''', 23), ('''JH 9H TH KH QH''', 22), ('''JC KH JS JD JH''', 21), ('''KH KC 3S 3H 3D''', 20), ('''8C 9C 5C 3C TC''', 19), ('''JS QS 9H TS KH''', 18), ('''7C 7S KH 2H 7H''', 17), ('''3C KH 5D 5S KH''', 16), ('''QH 8H KD JH 8S''', 15), ('''2D 6D 9D TH 7D''', 14), ) def lowercase ( ) -> Tuple: '''simple docstring''' snake_case , snake_case : List[str] = randrange(len(__A ) ), randrange(len(__A ) ) snake_case : int = ["""Loss""", """Tie""", """Win"""][(play >= oppo) + (play > oppo)] snake_case , snake_case : Optional[Any] = SORTED_HANDS[play], SORTED_HANDS[oppo] return hand, other, expected def lowercase ( __A : int = 100 ) -> Any: '''simple docstring''' return (generate_random_hand() for _ in range(__A )) @pytest.mark.parametrize("""hand, expected""" , __A ) def lowercase ( __A : Tuple , __A : Tuple ) -> Optional[int]: '''simple docstring''' assert PokerHand(__A )._is_flush() == expected @pytest.mark.parametrize("""hand, expected""" , __A ) def lowercase ( __A : List[str] , __A : Tuple ) -> List[Any]: '''simple docstring''' assert PokerHand(__A )._is_straight() == expected @pytest.mark.parametrize("""hand, expected, card_values""" , __A ) def lowercase ( __A : List[Any] , __A : Tuple , __A : Dict ) -> List[str]: '''simple docstring''' snake_case : List[Any] = PokerHand(__A ) assert player._is_five_high_straight() == expected assert player._card_values == card_values @pytest.mark.parametrize("""hand, expected""" , __A ) def lowercase ( __A : int , __A : List[Any] ) -> Dict: '''simple docstring''' assert PokerHand(__A )._is_same_kind() == expected @pytest.mark.parametrize("""hand, expected""" , __A ) def lowercase ( __A : int , __A : Optional[int] ) -> Union[str, Any]: '''simple docstring''' assert PokerHand(__A )._hand_type == expected @pytest.mark.parametrize("""hand, other, expected""" , __A ) def lowercase ( __A : Optional[int] , __A : List[str] , __A : int ) -> int: '''simple docstring''' assert PokerHand(__A ).compare_with(PokerHand(__A ) ) == expected @pytest.mark.parametrize("""hand, other, expected""" , generate_random_hands() ) def lowercase ( __A : Optional[int] , __A : Any , __A : Dict ) -> Optional[Any]: '''simple docstring''' assert PokerHand(__A ).compare_with(PokerHand(__A ) ) == expected def lowercase ( ) -> Optional[int]: '''simple docstring''' snake_case : Dict = [PokerHand(__A ) for hand in SORTED_HANDS] snake_case : Union[str, Any] = poker_hands.copy() shuffle(__A ) snake_case : Any = chain(sorted(__A ) ) for index, hand in enumerate(__A ): assert hand == poker_hands[index] def lowercase ( ) -> str: '''simple docstring''' snake_case : str = [PokerHand("""2D AC 3H 4H 5S""" ), PokerHand("""2S 3H 4H 5S 6C""" )] pokerhands.sort(reverse=__A ) assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C" def lowercase ( ) -> Union[str, Any]: '''simple docstring''' snake_case : List[str] = PokerHand("""2C 4S AS 3D 5C""" ) snake_case : int = True snake_case : Optional[Any] = [5, 4, 3, 2, 14] for _ in range(10 ): assert pokerhand._is_five_high_straight() == expected assert pokerhand._card_values == expected_card_values def lowercase ( ) -> Optional[Any]: '''simple docstring''' snake_case : Dict = 0 snake_case : List[str] = os.path.abspath(os.path.dirname(__A ) ) snake_case : str = os.path.join(__A , """poker_hands.txt""" ) with open(__A ) as file_hand: for line in file_hand: snake_case : Dict = line[:14].strip() snake_case : Optional[int] = line[15:].strip() snake_case , snake_case : Optional[int] = PokerHand(__A ), PokerHand(__A ) snake_case : Optional[Any] = player.compare_with(__A ) if output == "Win": answer += 1 assert answer == 376
36
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig __lowercase : Optional[Any] = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _A ( snake_case ): '''simple docstring''' __lowerCamelCase : int = '''albert''' def __init__( self ,SCREAMING_SNAKE_CASE_=30000 ,SCREAMING_SNAKE_CASE_=128 ,SCREAMING_SNAKE_CASE_=4096 ,SCREAMING_SNAKE_CASE_=12 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_=64 ,SCREAMING_SNAKE_CASE_=16384 ,SCREAMING_SNAKE_CASE_=1 ,SCREAMING_SNAKE_CASE_="gelu_new" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=1E-12 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_="absolute" ,SCREAMING_SNAKE_CASE_=0 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=3 ,**SCREAMING_SNAKE_CASE_ ,): '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ ,bos_token_id=SCREAMING_SNAKE_CASE_ ,eos_token_id=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = vocab_size snake_case : int = embedding_size snake_case : int = hidden_size snake_case : List[Any] = num_hidden_layers snake_case : int = num_hidden_groups snake_case : List[str] = num_attention_heads snake_case : List[str] = inner_group_num snake_case : Any = hidden_act snake_case : Any = intermediate_size snake_case : Union[str, Any] = hidden_dropout_prob snake_case : List[Any] = attention_probs_dropout_prob snake_case : Tuple = max_position_embeddings snake_case : Any = type_vocab_size snake_case : Optional[Any] = initializer_range snake_case : int = layer_norm_eps snake_case : Optional[int] = classifier_dropout_prob snake_case : str = position_embedding_type class _A ( snake_case ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' if self.task == "multiple-choice": snake_case : List[Any] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case : int = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis), ] )
36
1