code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin __A = logging.get_logger(__name__) enable_full_determinism() class _lowerCAmelCase ( a , a , unittest.TestCase ): """simple docstring""" __magic_name__ :Optional[Any] = UNetaDModel __magic_name__ :int = """sample""" @property def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Dict = 4 lowerCAmelCase__ :List[Any] = 3 lowerCAmelCase__ :Dict = (3_2, 3_2) lowerCAmelCase__ :int = floats_tensor((batch_size, num_channels) + sizes ).to(__UpperCAmelCase ) lowerCAmelCase__ :Optional[int] = torch.tensor([1_0] ).to(__UpperCAmelCase ) return {"sample": noise, "timestep": time_step} @property def snake_case ( self ): '''simple docstring''' return (3, 3_2, 3_2) @property def snake_case ( self ): '''simple docstring''' return (3, 3_2, 3_2) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :List[Any] = { 'block_out_channels': (3_2, 6_4), 'down_block_types': ('DownBlock2D', 'AttnDownBlock2D'), 'up_block_types': ('AttnUpBlock2D', 'UpBlock2D'), 'attention_head_dim': 3, 'out_channels': 3, 'in_channels': 3, 'layers_per_block': 2, 'sample_size': 3_2, } lowerCAmelCase__ :int = self.dummy_input return init_dict, inputs_dict class _lowerCAmelCase ( a , a , unittest.TestCase ): """simple docstring""" __magic_name__ :Dict = UNetaDModel __magic_name__ :Dict = """sample""" @property def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :str = 4 lowerCAmelCase__ :int = 4 lowerCAmelCase__ :List[Any] = (3_2, 3_2) lowerCAmelCase__ :List[str] = floats_tensor((batch_size, num_channels) + sizes ).to(__UpperCAmelCase ) lowerCAmelCase__ :Optional[Any] = torch.tensor([1_0] ).to(__UpperCAmelCase ) return {"sample": noise, "timestep": time_step} @property def snake_case ( self ): '''simple docstring''' return (4, 3_2, 3_2) @property def snake_case ( self ): '''simple docstring''' return (4, 3_2, 3_2) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :List[str] = { 'sample_size': 3_2, 'in_channels': 4, 'out_channels': 4, 'layers_per_block': 2, 'block_out_channels': (3_2, 6_4), 'attention_head_dim': 3_2, 'down_block_types': ('DownBlock2D', 'DownBlock2D'), 'up_block_types': ('UpBlock2D', 'UpBlock2D'), } lowerCAmelCase__ :Optional[int] = self.dummy_input return init_dict, inputs_dict def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ :str = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' , output_loading_info=__UpperCAmelCase ) self.assertIsNotNone(__UpperCAmelCase ) self.assertEqual(len(loading_info['missing_keys'] ) , 0 ) model.to(__UpperCAmelCase ) lowerCAmelCase__ :Union[str, Any] = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != 'cuda' , 'This test is supposed to run on GPU' ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ :Optional[int] = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' , output_loading_info=__UpperCAmelCase ) model.to(__UpperCAmelCase ) lowerCAmelCase__ :Optional[int] = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != 'cuda' , 'This test is supposed to run on GPU' ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ :str = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' , output_loading_info=__UpperCAmelCase ) model_accelerate.to(__UpperCAmelCase ) model_accelerate.eval() lowerCAmelCase__ :List[str] = torch.randn( 1 , model_accelerate.config.in_channels , model_accelerate.config.sample_size , model_accelerate.config.sample_size , generator=torch.manual_seed(0 ) , ) lowerCAmelCase__ :Dict = noise.to(__UpperCAmelCase ) lowerCAmelCase__ :Tuple = torch.tensor([1_0] * noise.shape[0] ).to(__UpperCAmelCase ) lowerCAmelCase__ :Any = model_accelerate(__UpperCAmelCase , __UpperCAmelCase )['sample'] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() lowerCAmelCase__ , lowerCAmelCase__ :List[str] = UNetaDModel.from_pretrained( 'fusing/unet-ldm-dummy-update' , output_loading_info=__UpperCAmelCase , low_cpu_mem_usage=__UpperCAmelCase ) model_normal_load.to(__UpperCAmelCase ) model_normal_load.eval() lowerCAmelCase__ :List[Any] = model_normal_load(__UpperCAmelCase , __UpperCAmelCase )['sample'] assert torch_all_close(__UpperCAmelCase , __UpperCAmelCase , rtol=1E-3 ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Optional[Any] = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' ) model.eval() model.to(__UpperCAmelCase ) lowerCAmelCase__ :List[str] = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) lowerCAmelCase__ :str = noise.to(__UpperCAmelCase ) lowerCAmelCase__ :Any = torch.tensor([1_0] * noise.shape[0] ).to(__UpperCAmelCase ) with torch.no_grad(): lowerCAmelCase__ :Union[str, Any] = model(__UpperCAmelCase , __UpperCAmelCase ).sample lowerCAmelCase__ :str = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off lowerCAmelCase__ :Union[str, Any] = torch.tensor([-13.32_58, -20.11_00, -15.98_73, -17.66_17, -23.05_96, -17.94_19, -13.36_75, -16.18_89, -12.38_00] ) # fmt: on self.assertTrue(torch_all_close(__UpperCAmelCase , __UpperCAmelCase , rtol=1E-3 ) ) class _lowerCAmelCase ( a , a , unittest.TestCase ): """simple docstring""" __magic_name__ :Dict = UNetaDModel __magic_name__ :Optional[Any] = """sample""" @property def snake_case ( self , __UpperCAmelCase=(3_2, 3_2) ): '''simple docstring''' lowerCAmelCase__ :List[Any] = 4 lowerCAmelCase__ :Union[str, Any] = 3 lowerCAmelCase__ :List[Any] = floats_tensor((batch_size, num_channels) + sizes ).to(__UpperCAmelCase ) lowerCAmelCase__ :Tuple = torch.tensor(batch_size * [1_0] ).to(dtype=torch.intaa , device=__UpperCAmelCase ) return {"sample": noise, "timestep": time_step} @property def snake_case ( self ): '''simple docstring''' return (3, 3_2, 3_2) @property def snake_case ( self ): '''simple docstring''' return (3, 3_2, 3_2) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Optional[Any] = { 'block_out_channels': [3_2, 6_4, 6_4, 6_4], 'in_channels': 3, 'layers_per_block': 1, 'out_channels': 3, 'time_embedding_type': 'fourier', 'norm_eps': 1E-6, 'mid_block_scale_factor': math.sqrt(2.0 ), 'norm_num_groups': None, 'down_block_types': [ 'SkipDownBlock2D', 'AttnSkipDownBlock2D', 'SkipDownBlock2D', 'SkipDownBlock2D', ], 'up_block_types': [ 'SkipUpBlock2D', 'SkipUpBlock2D', 'AttnSkipUpBlock2D', 'SkipUpBlock2D', ], } lowerCAmelCase__ :Optional[Any] = self.dummy_input return init_dict, inputs_dict @slow def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ :Optional[int] = UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256' , output_loading_info=__UpperCAmelCase ) self.assertIsNotNone(__UpperCAmelCase ) self.assertEqual(len(loading_info['missing_keys'] ) , 0 ) model.to(__UpperCAmelCase ) lowerCAmelCase__ :Optional[int] = self.dummy_input lowerCAmelCase__ :List[Any] = floats_tensor((4, 3) + (2_5_6, 2_5_6) ).to(__UpperCAmelCase ) lowerCAmelCase__ :List[str] = noise lowerCAmelCase__ :int = model(**__UpperCAmelCase ) assert image is not None, "Make sure output is not None" @slow def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Dict = UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256' ) model.to(__UpperCAmelCase ) lowerCAmelCase__ :Union[str, Any] = 4 lowerCAmelCase__ :List[Any] = 3 lowerCAmelCase__ :List[str] = (2_5_6, 2_5_6) lowerCAmelCase__ :Union[str, Any] = torch.ones((batch_size, num_channels) + sizes ).to(__UpperCAmelCase ) lowerCAmelCase__ :Dict = torch.tensor(batch_size * [1E-4] ).to(__UpperCAmelCase ) with torch.no_grad(): lowerCAmelCase__ :Union[str, Any] = model(__UpperCAmelCase , __UpperCAmelCase ).sample lowerCAmelCase__ :Union[str, Any] = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off lowerCAmelCase__ :List[str] = torch.tensor([-48_42.86_91, -64_99.66_31, -38_00.19_53, -79_78.26_86, -1_09_80.71_29, -2_00_28.85_35, 81_48.28_22, 23_42.29_05, 5_67.76_08] ) # fmt: on self.assertTrue(torch_all_close(__UpperCAmelCase , __UpperCAmelCase , rtol=1E-2 ) ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Any = UNetaDModel.from_pretrained('fusing/ncsnpp-ffhq-ve-dummy-update' ) model.to(__UpperCAmelCase ) lowerCAmelCase__ :Union[str, Any] = 4 lowerCAmelCase__ :List[Any] = 3 lowerCAmelCase__ :Tuple = (3_2, 3_2) lowerCAmelCase__ :Tuple = torch.ones((batch_size, num_channels) + sizes ).to(__UpperCAmelCase ) lowerCAmelCase__ :List[Any] = torch.tensor(batch_size * [1E-4] ).to(__UpperCAmelCase ) with torch.no_grad(): lowerCAmelCase__ :str = model(__UpperCAmelCase , __UpperCAmelCase ).sample lowerCAmelCase__ :Optional[Any] = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off lowerCAmelCase__ :Optional[int] = torch.tensor([-0.03_25, -0.09_00, -0.08_69, -0.03_32, -0.07_25, -0.02_70, -0.01_01, 0.02_27, 0.02_56] ) # fmt: on self.assertTrue(torch_all_close(__UpperCAmelCase , __UpperCAmelCase , rtol=1E-2 ) ) def snake_case ( self ): '''simple docstring''' pass
293
"""simple docstring""" from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time __A = Lock() def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->List[Any]: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 10 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(_SCREAMING_SNAKE_CASE ) process_lock.release() # receive your right neighbor's value process_lock.acquire() lowerCAmelCase__ :Any = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left lowerCAmelCase__ :Tuple = min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(_SCREAMING_SNAKE_CASE ) process_lock.release() # receive your left neighbor's value process_lock.acquire() lowerCAmelCase__ :Optional[int] = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right lowerCAmelCase__ :Optional[int] = max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # after all swaps are performed, send the values back to main result_pipe[1].send(_SCREAMING_SNAKE_CASE ) def __A (_SCREAMING_SNAKE_CASE ) ->Optional[int]: """simple docstring""" lowerCAmelCase__ :str = [] lowerCAmelCase__ :Optional[Any] = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop lowerCAmelCase__ :List[str] = Pipe() lowerCAmelCase__ :List[Any] = Pipe() process_array_.append( Process( target=_SCREAMING_SNAKE_CASE , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) lowerCAmelCase__ :Dict = temp_rs lowerCAmelCase__ :Optional[Any] = temp_rr for i in range(1 , len(_SCREAMING_SNAKE_CASE ) - 1 ): lowerCAmelCase__ :Union[str, Any] = Pipe() lowerCAmelCase__ :List[str] = Pipe() process_array_.append( Process( target=_SCREAMING_SNAKE_CASE , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) lowerCAmelCase__ :Union[str, Any] = temp_rs lowerCAmelCase__ :Any = temp_rr process_array_.append( Process( target=_SCREAMING_SNAKE_CASE , args=( len(_SCREAMING_SNAKE_CASE ) - 1, arr[len(_SCREAMING_SNAKE_CASE ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(_SCREAMING_SNAKE_CASE ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(_SCREAMING_SNAKE_CASE ) ): lowerCAmelCase__ :str = result_pipe[p][0].recv() process_array_[p].join() return arr def __A () ->List[Any]: """simple docstring""" lowerCAmelCase__ :Union[str, Any] = list(range(10 , 0 , -1 ) ) print('Initial List' ) print(*_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ :List[str] = odd_even_transposition(_SCREAMING_SNAKE_CASE ) print('Sorted List\n' ) print(*_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
293
1
import itertools from dataclasses import dataclass from typing import Optional import pandas as pd import pyarrow as pa import datasets from datasets.table import table_cast @dataclass class UpperCamelCase__ ( datasets.BuilderConfig ): """simple docstring""" UpperCAmelCase_ =None class UpperCamelCase__ ( datasets.ArrowBasedBuilder ): """simple docstring""" UpperCAmelCase_ =PandasConfig def _UpperCamelCase ( self ) -> int: return datasets.DatasetInfo(features=self.config.features ) def _UpperCamelCase ( self , _A ) -> Tuple: if not self.config.data_files: raise ValueError(F'''At least one data file must be specified, but got data_files={self.config.data_files}''' ) SCREAMING_SNAKE_CASE_ = dl_manager.download_and_extract(self.config.data_files ) if isinstance(_A , (str, list, tuple) ): SCREAMING_SNAKE_CASE_ = data_files if isinstance(_A , _A ): SCREAMING_SNAKE_CASE_ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive SCREAMING_SNAKE_CASE_ = [dl_manager.iter_files(_A ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] SCREAMING_SNAKE_CASE_ = [] for split_name, files in data_files.items(): if isinstance(_A , _A ): SCREAMING_SNAKE_CASE_ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive SCREAMING_SNAKE_CASE_ = [dl_manager.iter_files(_A ) for file in files] splits.append(datasets.SplitGenerator(name=_A , gen_kwargs={'''files''': files} ) ) return splits def _UpperCamelCase ( self , _A ) -> pa.Table: if self.config.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example SCREAMING_SNAKE_CASE_ = table_cast(_A , self.config.features.arrow_schema ) return pa_table def _UpperCamelCase ( self , _A ) -> Any: for i, file in enumerate(itertools.chain.from_iterable(_A ) ): with open(_A , '''rb''' ) as f: SCREAMING_SNAKE_CASE_ = pa.Table.from_pandas(pd.read_pickle(_A ) ) yield i, self._cast_table(_A )
257
from graphs.minimum_spanning_tree_kruskal import kruskal def A__ ( ): SCREAMING_SNAKE_CASE_ = 9 SCREAMING_SNAKE_CASE_ = [ [0, 1, 4], [0, 7, 8], [1, 2, 8], [7, 8, 7], [7, 6, 1], [2, 8, 2], [8, 6, 6], [2, 3, 7], [2, 5, 4], [6, 5, 2], [3, 5, 14], [3, 4, 9], [5, 4, 10], [1, 7, 11], ] SCREAMING_SNAKE_CASE_ = kruskal(__lowerCamelCase, __lowerCamelCase ) SCREAMING_SNAKE_CASE_ = [ [7, 6, 1], [2, 8, 2], [6, 5, 2], [0, 1, 4], [2, 5, 4], [2, 3, 7], [0, 7, 8], [3, 4, 9], ] assert sorted(__lowerCamelCase ) == sorted(__lowerCamelCase )
257
1
'''simple docstring''' import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetrImageProcessor class lowercase__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , __snake_case , __snake_case=7 , __snake_case=3 , __snake_case=30 , __snake_case=400 , __snake_case=True , __snake_case=None , __snake_case=True , __snake_case=1 / 255 , __snake_case=True , __snake_case=[0.5, 0.5, 0.5] , __snake_case=[0.5, 0.5, 0.5] , __snake_case=True , ): # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p _SCREAMING_SNAKE_CASE : Optional[int] = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 1333} _SCREAMING_SNAKE_CASE : int = parent _SCREAMING_SNAKE_CASE : List[Any] = batch_size _SCREAMING_SNAKE_CASE : Tuple = num_channels _SCREAMING_SNAKE_CASE : Union[str, Any] = min_resolution _SCREAMING_SNAKE_CASE : Union[str, Any] = max_resolution _SCREAMING_SNAKE_CASE : List[Any] = do_resize _SCREAMING_SNAKE_CASE : Union[str, Any] = size _SCREAMING_SNAKE_CASE : Optional[int] = do_rescale _SCREAMING_SNAKE_CASE : Union[str, Any] = rescale_factor _SCREAMING_SNAKE_CASE : List[str] = do_normalize _SCREAMING_SNAKE_CASE : Optional[int] = image_mean _SCREAMING_SNAKE_CASE : List[Any] = image_std _SCREAMING_SNAKE_CASE : Dict = do_pad def UpperCAmelCase_ ( self ): return { "do_resize": self.do_resize, "size": self.size, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_pad": self.do_pad, } def UpperCAmelCase_ ( self , __snake_case , __snake_case=False ): if not batched: _SCREAMING_SNAKE_CASE : int = image_inputs[0] if isinstance(__snake_case , Image.Image ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : List[Any] = image.size else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Tuple = image.shape[1], image.shape[2] if w < h: _SCREAMING_SNAKE_CASE : List[Any] = int(self.size["""shortest_edge"""] * h / w ) _SCREAMING_SNAKE_CASE : List[str] = self.size["""shortest_edge"""] elif w > h: _SCREAMING_SNAKE_CASE : Optional[Any] = self.size["""shortest_edge"""] _SCREAMING_SNAKE_CASE : Optional[int] = int(self.size["""shortest_edge"""] * w / h ) else: _SCREAMING_SNAKE_CASE : Optional[int] = self.size["""shortest_edge"""] _SCREAMING_SNAKE_CASE : Union[str, Any] = self.size["""shortest_edge"""] else: _SCREAMING_SNAKE_CASE : Optional[int] = [] for image in image_inputs: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : List[Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) _SCREAMING_SNAKE_CASE : Tuple = max(__snake_case , key=lambda __snake_case : item[0] )[0] _SCREAMING_SNAKE_CASE : Optional[int] = max(__snake_case , key=lambda __snake_case : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase__ ( _snake_case , unittest.TestCase ): '''simple docstring''' A_ : Union[str, Any] = DetrImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self ): _SCREAMING_SNAKE_CASE : List[str] = DetrImageProcessingTester(self ) @property def UpperCAmelCase_ ( self ): return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self ): _SCREAMING_SNAKE_CASE : Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__snake_case , """image_mean""" ) ) self.assertTrue(hasattr(__snake_case , """image_std""" ) ) self.assertTrue(hasattr(__snake_case , """do_normalize""" ) ) self.assertTrue(hasattr(__snake_case , """do_rescale""" ) ) self.assertTrue(hasattr(__snake_case , """rescale_factor""" ) ) self.assertTrue(hasattr(__snake_case , """do_resize""" ) ) self.assertTrue(hasattr(__snake_case , """size""" ) ) self.assertTrue(hasattr(__snake_case , """do_pad""" ) ) def UpperCAmelCase_ ( self ): _SCREAMING_SNAKE_CASE : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 1333} ) self.assertEqual(image_processor.do_pad , __snake_case ) _SCREAMING_SNAKE_CASE : Tuple = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=__snake_case ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , __snake_case ) def UpperCAmelCase_ ( self ): pass def UpperCAmelCase_ ( self ): # Initialize image_processing _SCREAMING_SNAKE_CASE : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__snake_case ) for image in image_inputs: self.assertIsInstance(__snake_case , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE : List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : int = self.image_processor_tester.get_expected_values(__snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Any = self.image_processor_tester.get_expected_values(__snake_case , batched=__snake_case ) _SCREAMING_SNAKE_CASE : Optional[int] = image_processing(__snake_case , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self ): # Initialize image_processing _SCREAMING_SNAKE_CASE : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _SCREAMING_SNAKE_CASE : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__snake_case , numpify=__snake_case ) for image in image_inputs: self.assertIsInstance(__snake_case , np.ndarray ) # Test not batched input _SCREAMING_SNAKE_CASE : List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Tuple = self.image_processor_tester.get_expected_values(__snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _SCREAMING_SNAKE_CASE : Any = image_processing(__snake_case , return_tensors="""pt""" ).pixel_values _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Tuple = self.image_processor_tester.get_expected_values(__snake_case , batched=__snake_case ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self ): # Initialize image_processing _SCREAMING_SNAKE_CASE : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _SCREAMING_SNAKE_CASE : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__snake_case , torchify=__snake_case ) for image in image_inputs: self.assertIsInstance(__snake_case , torch.Tensor ) # Test not batched input _SCREAMING_SNAKE_CASE : str = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Tuple = self.image_processor_tester.get_expected_values(__snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _SCREAMING_SNAKE_CASE : Optional[Any] = image_processing(__snake_case , return_tensors="""pt""" ).pixel_values _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : Dict = self.image_processor_tester.get_expected_values(__snake_case , batched=__snake_case ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def UpperCAmelCase_ ( self ): # prepare image and target _SCREAMING_SNAKE_CASE : str = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: _SCREAMING_SNAKE_CASE : Optional[Any] = json.loads(f.read() ) _SCREAMING_SNAKE_CASE : str = {"""image_id""": 3_9769, """annotations""": target} # encode them _SCREAMING_SNAKE_CASE : List[Any] = DetrImageProcessor.from_pretrained("""facebook/detr-resnet-50""" ) _SCREAMING_SNAKE_CASE : int = image_processing(images=__snake_case , annotations=__snake_case , return_tensors="""pt""" ) # verify pixel values _SCREAMING_SNAKE_CASE : Optional[int] = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding["""pixel_values"""].shape , __snake_case ) _SCREAMING_SNAKE_CASE : List[str] = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , __snake_case , atol=1e-4 ) ) # verify area _SCREAMING_SNAKE_CASE : Any = torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , __snake_case ) ) # verify boxes _SCREAMING_SNAKE_CASE : Optional[int] = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , __snake_case ) _SCREAMING_SNAKE_CASE : int = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , __snake_case , atol=1e-3 ) ) # verify image_id _SCREAMING_SNAKE_CASE : int = torch.tensor([3_9769] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , __snake_case ) ) # verify is_crowd _SCREAMING_SNAKE_CASE : Tuple = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , __snake_case ) ) # verify class_labels _SCREAMING_SNAKE_CASE : Tuple = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , __snake_case ) ) # verify orig_size _SCREAMING_SNAKE_CASE : int = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , __snake_case ) ) # verify size _SCREAMING_SNAKE_CASE : Any = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , __snake_case ) ) @slow def UpperCAmelCase_ ( self ): # prepare image, target and masks_path _SCREAMING_SNAKE_CASE : Union[str, Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: _SCREAMING_SNAKE_CASE : int = json.loads(f.read() ) _SCREAMING_SNAKE_CASE : Optional[Any] = {"""file_name""": """000000039769.png""", """image_id""": 3_9769, """segments_info""": target} _SCREAMING_SNAKE_CASE : List[Any] = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them _SCREAMING_SNAKE_CASE : Tuple = DetrImageProcessor.from_pretrained("""facebook/detr-resnet-50-panoptic""" ) _SCREAMING_SNAKE_CASE : Tuple = image_processing(images=__snake_case , annotations=__snake_case , masks_path=__snake_case , return_tensors="""pt""" ) # verify pixel values _SCREAMING_SNAKE_CASE : Optional[Any] = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding["""pixel_values"""].shape , __snake_case ) _SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , __snake_case , atol=1e-4 ) ) # verify area _SCREAMING_SNAKE_CASE : Tuple = torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , __snake_case ) ) # verify boxes _SCREAMING_SNAKE_CASE : str = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , __snake_case ) _SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , __snake_case , atol=1e-3 ) ) # verify image_id _SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([3_9769] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , __snake_case ) ) # verify is_crowd _SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , __snake_case ) ) # verify class_labels _SCREAMING_SNAKE_CASE : int = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , __snake_case ) ) # verify masks _SCREAMING_SNAKE_CASE : List[Any] = 82_2873 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , __snake_case ) # verify orig_size _SCREAMING_SNAKE_CASE : str = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , __snake_case ) ) # verify size _SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , __snake_case ) )
200
'''simple docstring''' from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging UpperCAmelCase_ : List[Any] = logging.get_logger(__name__) def snake_case_ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): """simple docstring""" try: with open(SCREAMING_SNAKE_CASE__ , """rb""" ) as flax_state_f: _SCREAMING_SNAKE_CASE : Dict = from_bytes(SCREAMING_SNAKE_CASE__ , flax_state_f.read() ) except UnpicklingError as e: try: with open(SCREAMING_SNAKE_CASE__ ) as f: if f.read().startswith("""version""" ): raise OSError( """You seem to have cloned a repository without having git-lfs installed. Please""" """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the""" """ folder you cloned.""" ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(f"""Unable to convert {model_file} to Flax deserializable object. """ ) return load_flax_weights_in_pytorch_model(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def snake_case_ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights _SCREAMING_SNAKE_CASE : List[Any] = flatten_dict(jax.tree_util.tree_map(lambda SCREAMING_SNAKE_CASE__ : x.dtype == jnp.bfloataa , SCREAMING_SNAKE_CASE__ ) ).values() if any(SCREAMING_SNAKE_CASE__ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) _SCREAMING_SNAKE_CASE : Dict = jax.tree_util.tree_map( lambda SCREAMING_SNAKE_CASE__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , SCREAMING_SNAKE_CASE__ ) _SCREAMING_SNAKE_CASE : Optional[Any] = """""" _SCREAMING_SNAKE_CASE : str = flatten_dict(SCREAMING_SNAKE_CASE__ , sep=""".""" ) _SCREAMING_SNAKE_CASE : str = pt_model.state_dict() # keep track of unexpected & missing keys _SCREAMING_SNAKE_CASE : Tuple = [] _SCREAMING_SNAKE_CASE : int = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): _SCREAMING_SNAKE_CASE : Any = flax_key_tuple.split(""".""" ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: _SCREAMING_SNAKE_CASE : Optional[Any] = flax_key_tuple_array[:-1] + ["""weight"""] _SCREAMING_SNAKE_CASE : List[str] = jnp.transpose(SCREAMING_SNAKE_CASE__ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": _SCREAMING_SNAKE_CASE : Union[str, Any] = flax_key_tuple_array[:-1] + ["""weight"""] _SCREAMING_SNAKE_CASE : Any = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": _SCREAMING_SNAKE_CASE : Optional[int] = flax_key_tuple_array[:-1] + ["""weight"""] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(SCREAMING_SNAKE_CASE__ ): _SCREAMING_SNAKE_CASE : Optional[int] = ( flax_key_tuple_string.replace("""_0""" , """.0""" ) .replace("""_1""" , """.1""" ) .replace("""_2""" , """.2""" ) .replace("""_3""" , """.3""" ) .replace("""_4""" , """.4""" ) .replace("""_5""" , """.5""" ) .replace("""_6""" , """.6""" ) .replace("""_7""" , """.7""" ) .replace("""_8""" , """.8""" ) .replace("""_9""" , """.9""" ) ) _SCREAMING_SNAKE_CASE : Tuple = """.""".join(SCREAMING_SNAKE_CASE__ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( f"""Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected """ f"""to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) else: # add weight to pytorch dict _SCREAMING_SNAKE_CASE : Union[str, Any] = np.asarray(SCREAMING_SNAKE_CASE__ ) if not isinstance(SCREAMING_SNAKE_CASE__ , np.ndarray ) else flax_tensor _SCREAMING_SNAKE_CASE : int = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) # remove from missing keys missing_keys.remove(SCREAMING_SNAKE_CASE__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(SCREAMING_SNAKE_CASE__ ) pt_model.load_state_dict(SCREAMING_SNAKE_CASE__ ) # re-transform missing_keys to list _SCREAMING_SNAKE_CASE : Optional[Any] = list(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" f""" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing""" f""" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture""" """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" f""" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect""" """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) if len(SCREAMING_SNAKE_CASE__ ) > 0: logger.warning( f"""Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly""" f""" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to""" """ use it for predictions and inference.""" ) return pt_model
200
1
'''simple docstring''' from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging snake_case__ = logging.get_logger(__name__) snake_case__ = { 'Helsinki-NLP/opus-mt-en-de': 'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json', # See all Marian models at https://huggingface.co/models?filter=marian } class UpperCamelCase_ (_lowerCAmelCase ): """simple docstring""" _lowerCAmelCase = 'marian' _lowerCAmelCase = ['past_key_values'] _lowerCAmelCase = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'} def __init__( self : Tuple , _lowerCamelCase : Union[str, Any]=58101 , _lowerCamelCase : List[str]=None , _lowerCamelCase : Optional[Any]=1024 , _lowerCamelCase : int=12 , _lowerCamelCase : Optional[int]=4096 , _lowerCamelCase : List[Any]=16 , _lowerCamelCase : Optional[int]=12 , _lowerCamelCase : List[Any]=4096 , _lowerCamelCase : Optional[Any]=16 , _lowerCamelCase : List[str]=0.0 , _lowerCamelCase : List[str]=0.0 , _lowerCamelCase : Any=True , _lowerCamelCase : Optional[int]=True , _lowerCamelCase : Tuple="gelu" , _lowerCamelCase : Dict=1024 , _lowerCamelCase : Any=0.1 , _lowerCamelCase : Dict=0.0 , _lowerCamelCase : Dict=0.0 , _lowerCamelCase : Optional[Any]=0.02 , _lowerCamelCase : Optional[Any]=58100 , _lowerCamelCase : Optional[int]=False , _lowerCamelCase : Any=58100 , _lowerCamelCase : List[str]=0 , _lowerCamelCase : Union[str, Any]=0 , _lowerCamelCase : Optional[int]=True , **_lowerCamelCase : Tuple , ): """simple docstring""" A_ : str = vocab_size A_ : List[str] = decoder_vocab_size or vocab_size A_ : str = max_position_embeddings A_ : Dict = d_model A_ : Dict = encoder_ffn_dim A_ : Tuple = encoder_layers A_ : Union[str, Any] = encoder_attention_heads A_ : Any = decoder_ffn_dim A_ : Dict = decoder_layers A_ : List[Any] = decoder_attention_heads A_ : Optional[Any] = dropout A_ : str = attention_dropout A_ : List[str] = activation_dropout A_ : Any = activation_function A_ : Optional[int] = init_std A_ : Union[str, Any] = encoder_layerdrop A_ : Any = decoder_layerdrop A_ : Dict = use_cache A_ : Optional[int] = encoder_layers A_ : Optional[Any] = scale_embedding # scale factor will be sqrt(d_model) if True A_ : Union[str, Any] = share_encoder_decoder_embeddings super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , is_encoder_decoder=SCREAMING_SNAKE_CASE_ , decoder_start_token_id=SCREAMING_SNAKE_CASE_ , forced_eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) class UpperCamelCase_ (_lowerCAmelCase ): """simple docstring""" @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs def _a ( self : str ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: A_ : Union[str, Any] = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: A_ : Optional[Any] = {0: """batch"""} A_ : int = {0: """batch""", 1: """past_decoder_sequence + sequence"""} else: A_ : int = {0: """batch""", 1: """decoder_sequence"""} A_ : Tuple = {0: """batch""", 1: """decoder_sequence"""} if self.use_past: self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. A_ : str = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: A_ : Any = self.num_layers for i in range(SCREAMING_SNAKE_CASE_ ): A_ : Any = {0: """batch""", 2: """past_sequence + sequence"""} A_ : Tuple = {0: """batch""", 2: """past_sequence + sequence"""} else: A_ : str = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs def _a ( self : int ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: A_ : Optional[Any] = super().outputs else: A_ : Tuple = super(SCREAMING_SNAKE_CASE_ , self ).outputs if self.use_past: A_ : Any = self.num_layers for i in range(SCREAMING_SNAKE_CASE_ ): A_ : Optional[Any] = {0: """batch""", 2: """past_sequence + sequence"""} A_ : Optional[Any] = {0: """batch""", 2: """past_sequence + sequence"""} return common_outputs def _a ( self : Optional[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : Optional[Any] = -1 , _lowerCamelCase : List[Any] = -1 , _lowerCamelCase : int = False , _lowerCamelCase : List[str] = None , ): """simple docstring""" A_ : str = self._generate_dummy_inputs_for_encoder_and_decoder( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Generate decoder inputs A_ : Any = seq_length if not self.use_past else 1 A_ : Optional[int] = self._generate_dummy_inputs_for_encoder_and_decoder( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A_ : int = {f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()} A_ : Any = dict(**SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch A_ : int = common_inputs["""input_ids"""].shape A_ : Union[str, Any] = common_inputs["""decoder_input_ids"""].shape[1] A_ : str = self.num_attention_heads A_ : List[Any] = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) A_ : int = decoder_seq_length + 3 A_ : Any = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) A_ : Tuple = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )] , dim=1 ) A_ : int = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered A_ : Optional[int] = self.num_layers A_ : Optional[Any] = min(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A_ : Tuple = max(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) - min_num_layers A_ : Optional[int] = """encoder""" if num_encoder_layers > num_decoder_layers else """decoder""" for _ in range(SCREAMING_SNAKE_CASE_ ): common_inputs["past_key_values"].append( ( torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ ), ) ) # TODO: test this. A_ : Any = encoder_shape if remaining_side_name == """encoder""" else decoder_shape for _ in range(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): common_inputs["past_key_values"].append((torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ )) ) return common_inputs def _a ( self : List[str] , _lowerCamelCase : List[str] , _lowerCamelCase : Optional[Any] = -1 , _lowerCamelCase : str = -1 , _lowerCamelCase : int = False , _lowerCamelCase : Optional[int] = None , ): """simple docstring""" A_ : Tuple = self._generate_dummy_inputs_for_encoder_and_decoder( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch A_ : Tuple = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values A_ : List[str] = seqlen + 2 A_ : Optional[int] = self.num_layers A_ : Any = self.num_attention_heads A_ : int = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) A_ : Optional[int] = common_inputs["""attention_mask"""].dtype A_ : int = torch.cat( [common_inputs['''attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , dtype=SCREAMING_SNAKE_CASE_ )] , dim=1 ) A_ : Union[str, Any] = [ (torch.zeros(SCREAMING_SNAKE_CASE_ ), torch.zeros(SCREAMING_SNAKE_CASE_ )) for _ in range(SCREAMING_SNAKE_CASE_ ) ] return common_inputs def _a ( self : Dict , _lowerCamelCase : Optional[Any] , _lowerCamelCase : Any = -1 , _lowerCamelCase : Any = -1 , _lowerCamelCase : Optional[Any] = False , _lowerCamelCase : Union[str, Any] = None , ): """simple docstring""" A_ : Optional[int] = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A_ : List[Any] = tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE_ ) A_ : List[Any] = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=SCREAMING_SNAKE_CASE_ ) # Generate dummy inputs according to compute batch and sequence A_ : str = [""" """.join([tokenizer.unk_token] ) * seq_length] * batch_size A_ : Optional[int] = dict(tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) ) return common_inputs def _a ( self : List[Any] , _lowerCamelCase : Optional[Any] , _lowerCamelCase : str = -1 , _lowerCamelCase : Tuple = -1 , _lowerCamelCase : str = False , _lowerCamelCase : int = None , ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: A_ : Dict = self._generate_dummy_inputs_for_default_and_seqaseq_lm( SCREAMING_SNAKE_CASE_ , batch_size=SCREAMING_SNAKE_CASE_ , seq_length=SCREAMING_SNAKE_CASE_ , is_pair=SCREAMING_SNAKE_CASE_ , framework=SCREAMING_SNAKE_CASE_ ) else: A_ : Union[str, Any] = self._generate_dummy_inputs_for_causal_lm( SCREAMING_SNAKE_CASE_ , batch_size=SCREAMING_SNAKE_CASE_ , seq_length=SCREAMING_SNAKE_CASE_ , is_pair=SCREAMING_SNAKE_CASE_ , framework=SCREAMING_SNAKE_CASE_ ) return common_inputs def _a ( self : Optional[int] , _lowerCamelCase : Any , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Any , _lowerCamelCase : Optional[int] ): """simple docstring""" if self.task in ["default", "seq2seq-lm"]: A_ : Optional[int] = super()._flatten_past_key_values_(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: A_ : str = super(SCREAMING_SNAKE_CASE_ , self )._flatten_past_key_values_( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @property def _a ( self : Union[str, Any] ): """simple docstring""" return 1E-4
365
'''simple docstring''' import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class UpperCamelCase_ : """simple docstring""" def __init__( self : Optional[Any] , _lowerCamelCase : int , _lowerCamelCase : List[str]=3 , _lowerCamelCase : Any=32 , _lowerCamelCase : Union[str, Any]=3 , _lowerCamelCase : int=10 , _lowerCamelCase : Union[str, Any]=[8, 16, 32, 64] , _lowerCamelCase : Dict=[1, 1, 2, 1] , _lowerCamelCase : Union[str, Any]=True , _lowerCamelCase : Optional[int]=True , _lowerCamelCase : Any="relu" , _lowerCamelCase : Optional[Any]=3 , _lowerCamelCase : Optional[Any]=None , _lowerCamelCase : Dict=["stage2", "stage3", "stage4"] , _lowerCamelCase : Union[str, Any]=[2, 3, 4] , _lowerCamelCase : Tuple=1 , ): """simple docstring""" A_ : List[str] = parent A_ : List[str] = batch_size A_ : Union[str, Any] = image_size A_ : Tuple = num_channels A_ : Any = embeddings_size A_ : int = hidden_sizes A_ : Optional[Any] = depths A_ : List[Any] = is_training A_ : Optional[int] = use_labels A_ : int = hidden_act A_ : Tuple = num_labels A_ : Union[str, Any] = scope A_ : List[Any] = len(_lowerCamelCase ) A_ : Union[str, Any] = out_features A_ : List[Any] = out_indices A_ : Dict = num_groups def _a ( self : Optional[int] ): """simple docstring""" A_ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Union[str, Any] = None if self.use_labels: A_ : Any = ids_tensor([self.batch_size] , self.num_labels ) A_ : Any = self.get_config() return config, pixel_values, labels def _a ( self : Union[str, Any] ): """simple docstring""" return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def _a ( self : List[Any] , _lowerCamelCase : List[str] , _lowerCamelCase : List[str] , _lowerCamelCase : Optional[Any] ): """simple docstring""" A_ : Any = BitModel(config=_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : int = model(_lowerCamelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def _a ( self : Optional[int] , _lowerCamelCase : List[Any] , _lowerCamelCase : str , _lowerCamelCase : Optional[int] ): """simple docstring""" A_ : Dict = self.num_labels A_ : Optional[Any] = BitForImageClassification(_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : List[Any] = model(_lowerCamelCase , labels=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _a ( self : Any , _lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : List[Any] ): """simple docstring""" A_ : List[Any] = BitBackbone(config=_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : int = model(_lowerCamelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None A_ : Optional[Any] = None A_ : int = BitBackbone(config=_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : Optional[int] = model(_lowerCamelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def _a ( self : List[Any] ): """simple docstring""" A_ : Union[str, Any] = self.prepare_config_and_inputs() A_ ,A_ ,A_ : Union[str, Any] = config_and_inputs A_ : str = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase_ (a__, a__, unittest.TestCase ): """simple docstring""" _lowerCAmelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () _lowerCAmelCase = ( {'feature-extraction': BitModel, 'image-classification': BitForImageClassification} if is_torch_available() else {} ) _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False _lowerCAmelCase = False def _a ( self : Optional[Any] ): """simple docstring""" A_ : List[str] = BitModelTester(self ) A_ : Optional[Any] = ConfigTester(self , config_class=_lowerCamelCase , has_text_modality=_lowerCamelCase ) def _a ( self : Optional[Any] ): """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _a ( self : List[Any] ): """simple docstring""" return @unittest.skip(reason='''Bit does not output attentions''' ) def _a ( self : str ): """simple docstring""" pass @unittest.skip(reason='''Bit does not use inputs_embeds''' ) def _a ( self : Union[str, Any] ): """simple docstring""" pass @unittest.skip(reason='''Bit does not support input and output embeddings''' ) def _a ( self : Any ): """simple docstring""" pass def _a ( self : List[Any] ): """simple docstring""" A_ ,A_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Dict = model_class(_lowerCamelCase ) A_ : Dict = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : int = [*signature.parameters.keys()] A_ : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _lowerCamelCase ) def _a ( self : Optional[Any] ): """simple docstring""" A_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCamelCase ) def _a ( self : Optional[Any] ): """simple docstring""" A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_lowerCamelCase ) def _a ( self : Tuple ): """simple docstring""" A_ ,A_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : str = model_class(config=_lowerCamelCase ) for name, module in model.named_modules(): if isinstance(_lowerCamelCase , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=f'Parameter {name} of model {model_class} seems not properly initialized' , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=f'Parameter {name} of model {model_class} seems not properly initialized' , ) def _a ( self : int ): """simple docstring""" def check_hidden_states_output(_lowerCamelCase : Union[str, Any] , _lowerCamelCase : Dict , _lowerCamelCase : int ): A_ : Union[str, Any] = model_class(_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() with torch.no_grad(): A_ : Union[str, Any] = model(**self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) ) A_ : int = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states A_ : List[Any] = self.model_tester.num_stages self.assertEqual(len(_lowerCamelCase ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) A_ ,A_ : str = self.model_tester.prepare_config_and_inputs_for_common() A_ : Tuple = ['''preactivation''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: A_ : Tuple = layer_type A_ : Optional[Any] = True check_hidden_states_output(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A_ : List[str] = True check_hidden_states_output(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) @unittest.skip(reason='''Bit does not use feedforward chunking''' ) def _a ( self : Tuple ): """simple docstring""" pass def _a ( self : str ): """simple docstring""" A_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase ) @slow def _a ( self : Union[str, Any] ): """simple docstring""" for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : List[Any] = BitModel.from_pretrained(_lowerCamelCase ) self.assertIsNotNone(_lowerCamelCase ) def snake_case__ ( ) -> Optional[int]: A_ : Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class UpperCamelCase_ (unittest.TestCase ): """simple docstring""" @cached_property def _a ( self : List[Any] ): """simple docstring""" return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _a ( self : Dict ): """simple docstring""" A_ : Optional[int] = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_lowerCamelCase ) A_ : Union[str, Any] = self.default_image_processor A_ : Optional[int] = prepare_img() A_ : int = image_processor(images=_lowerCamelCase , return_tensors='''pt''' ).to(_lowerCamelCase ) # forward pass with torch.no_grad(): A_ : Union[str, Any] = model(**_lowerCamelCase ) # verify the logits A_ : Dict = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _lowerCamelCase ) A_ : Tuple = torch.tensor([[-0.65_26, -0.52_63, -1.43_98]] ).to(_lowerCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _lowerCamelCase , atol=1E-4 ) ) @require_torch class UpperCamelCase_ (a__, unittest.TestCase ): """simple docstring""" _lowerCAmelCase = (BitBackbone,) if is_torch_available() else () _lowerCAmelCase = BitConfig _lowerCAmelCase = False def _a ( self : List[str] ): """simple docstring""" A_ : Union[str, Any] = BitModelTester(self )
4
0
'''simple docstring''' from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING a_ = logging.get_logger(__name__) @add_end_docstrings(lowerCamelCase ) class __SCREAMING_SNAKE_CASE ( lowerCamelCase ): def __init__( self : str , *__lowercase : Union[str, Any] , **__lowercase : Any ) -> List[str]: super().__init__(*__lowercase , **__lowercase ) requires_backends(self , '''vision''' ) self.check_model_type( TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == '''tf''' else MODEL_FOR_VISION_2_SEQ_MAPPING ) def __magic_name__ ( self : Optional[Any] , __lowercase : Optional[Any]=None , __lowercase : int=None , __lowercase : List[str]=None ) -> int: SCREAMING_SNAKE_CASE__ : List[str] ={} SCREAMING_SNAKE_CASE__ : List[str] ={} if prompt is not None: SCREAMING_SNAKE_CASE__ : str =prompt if generate_kwargs is not None: SCREAMING_SNAKE_CASE__ : str =generate_kwargs if max_new_tokens is not None: if "generate_kwargs" not in forward_kwargs: SCREAMING_SNAKE_CASE__ : List[str] ={} if "max_new_tokens" in forward_kwargs["generate_kwargs"]: raise ValueError( '''\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,''' ''' please use only one''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] =max_new_tokens return preprocess_params, forward_kwargs, {} def __call__( self : Union[str, Any] , __lowercase : Union[str, List[str], "Image.Image", List["Image.Image"]] , **__lowercase : Optional[Any] ) -> int: return super().__call__(__lowercase , **__lowercase ) def __magic_name__ ( self : str , __lowercase : Tuple , __lowercase : Dict=None ) -> List[Any]: SCREAMING_SNAKE_CASE__ : str =load_image(__lowercase ) if prompt is not None: if not isinstance(__lowercase , __lowercase ): raise ValueError( F"Received an invalid text input, got - {type(__lowercase )} - but expected a single string. " '''Note also that one single text can be provided for conditional image to text generation.''' ) SCREAMING_SNAKE_CASE__ : List[str] =self.model.config.model_type if model_type == "git": SCREAMING_SNAKE_CASE__ : List[Any] =self.image_processor(images=__lowercase , return_tensors=self.framework ) SCREAMING_SNAKE_CASE__ : Optional[int] =self.tokenizer(text=__lowercase , add_special_tokens=__lowercase ).input_ids SCREAMING_SNAKE_CASE__ : Any =[self.tokenizer.cls_token_id] + input_ids SCREAMING_SNAKE_CASE__ : str =torch.tensor(__lowercase ).unsqueeze(0 ) model_inputs.update({'''input_ids''': input_ids} ) elif model_type == "pix2struct": SCREAMING_SNAKE_CASE__ : Dict =self.image_processor(images=__lowercase , header_text=__lowercase , return_tensors=self.framework ) elif model_type != "vision-encoder-decoder": # vision-encoder-decoder does not support conditional generation SCREAMING_SNAKE_CASE__ : List[str] =self.image_processor(images=__lowercase , return_tensors=self.framework ) SCREAMING_SNAKE_CASE__ : Optional[Any] =self.tokenizer(__lowercase , return_tensors=self.framework ) model_inputs.update(__lowercase ) else: raise ValueError(F"Model type {model_type} does not support conditional text generation" ) else: SCREAMING_SNAKE_CASE__ : Union[str, Any] =self.image_processor(images=__lowercase , return_tensors=self.framework ) if self.model.config.model_type == "git" and prompt is None: SCREAMING_SNAKE_CASE__ : Optional[Any] =None return model_inputs def __magic_name__ ( self : Optional[int] , __lowercase : Dict , __lowercase : Any=None ) -> Union[str, Any]: # Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the # pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first. if ( "input_ids" in model_inputs and isinstance(model_inputs['''input_ids'''] , __lowercase ) and all(x is None for x in model_inputs['''input_ids'''] ) ): SCREAMING_SNAKE_CASE__ : List[str] =None if generate_kwargs is None: SCREAMING_SNAKE_CASE__ : int ={} # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py` # parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas # the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name` # in the `_prepare_model_inputs` method. SCREAMING_SNAKE_CASE__ : Optional[int] =model_inputs.pop(self.model.main_input_name ) SCREAMING_SNAKE_CASE__ : Union[str, Any] =self.model.generate(__lowercase , **__lowercase , **__lowercase ) return model_outputs def __magic_name__ ( self : List[str] , __lowercase : Optional[Any] ) -> Dict: SCREAMING_SNAKE_CASE__ : str =[] for output_ids in model_outputs: SCREAMING_SNAKE_CASE__ : int ={ '''generated_text''': self.tokenizer.decode( __lowercase , skip_special_tokens=__lowercase , ) } records.append(__lowercase ) return records
152
'''simple docstring''' import socket def _a( ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple =socket.socket(socket.AF_INET, socket.SOCK_STREAM ) SCREAMING_SNAKE_CASE__ : str =socket.gethostname() SCREAMING_SNAKE_CASE__ : List[Any] =1_2_3_1_2 sock.connect((host, port) ) sock.send(B'''Hello server!''' ) with open('''Received_file''', '''wb''' ) as out_file: print('''File opened''' ) print('''Receiving data...''' ) while True: SCREAMING_SNAKE_CASE__ : List[str] =sock.recv(1_0_2_4 ) if not data: break out_file.write(UpperCamelCase__ ) print('''Successfully received the file''' ) sock.close() print('''Connection closed''' ) if __name__ == "__main__": main()
152
1
"""simple docstring""" import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def UpperCAmelCase__ (snake_case__ : int , snake_case__ : Optional[int] , snake_case__ : Tuple ): """simple docstring""" if isinstance(snake_case__ , torch.Tensor ): return image elif isinstance(snake_case__ , PIL.Image.Image ): _snake_case : Tuple = [image] if isinstance(image[0] , PIL.Image.Image ): _snake_case : Union[str, Any] = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION["""lanczos"""] ) )[None, :] for i in image] _snake_case : Tuple = np.concatenate(snake_case__ , axis=0 ) _snake_case : Union[str, Any] = np.array(snake_case__ ).astype(np.floataa ) / 2_55.0 _snake_case : str = image.transpose(0 , 3 , 1 , 2 ) _snake_case : Optional[Any] = 2.0 * image - 1.0 _snake_case : List[str] = torch.from_numpy(snake_case__ ) elif isinstance(image[0] , torch.Tensor ): _snake_case : Any = torch.cat(snake_case__ , dim=0 ) return image def UpperCAmelCase__ (snake_case__ : Union[str, Any] , snake_case__ : List[str] , snake_case__ : Optional[int] , snake_case__ : str=0.99_95 ): """simple docstring""" if not isinstance(snake_case__ , np.ndarray ): _snake_case : Any = True _snake_case : List[str] = va.device _snake_case : Optional[Any] = va.cpu().numpy() _snake_case : Union[str, Any] = va.cpu().numpy() _snake_case : str = np.sum(va * va / (np.linalg.norm(snake_case__ ) * np.linalg.norm(snake_case__ )) ) if np.abs(snake_case__ ) > DOT_THRESHOLD: _snake_case : Tuple = (1 - t) * va + t * va else: _snake_case : Tuple = np.arccos(snake_case__ ) _snake_case : List[Any] = np.sin(snake_case__ ) _snake_case : Optional[int] = theta_a * t _snake_case : Optional[int] = np.sin(snake_case__ ) _snake_case : str = np.sin(theta_a - theta_t ) / sin_theta_a _snake_case : Tuple = sin_theta_t / sin_theta_a _snake_case : Union[str, Any] = sa * va + sa * va if inputs_are_torch: _snake_case : int = torch.from_numpy(snake_case__ ).to(snake_case__ ) return va def UpperCAmelCase__ (snake_case__ : List[Any] , snake_case__ : int ): """simple docstring""" _snake_case : Union[str, Any] = F.normalize(snake_case__ , dim=-1 ) _snake_case : Optional[Any] = F.normalize(snake_case__ , dim=-1 ) return (x - y).norm(dim=-1 ).div(2 ).arcsin().pow(2 ).mul(2 ) def UpperCAmelCase__ (snake_case__ : Any , snake_case__ : str ): """simple docstring""" for param in model.parameters(): _snake_case : Dict = value class lowercase( __a ): '''simple docstring''' def __init__( self: Any, a_: AutoencoderKL, a_: CLIPTextModel, a_: CLIPModel, a_: CLIPTokenizer, a_: UNetaDConditionModel, a_: Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler], a_: CLIPFeatureExtractor, a_: int=None, a_: Optional[int]=None, a_: List[str]=None, ): '''simple docstring''' super().__init__() self.register_modules( vae=a_, text_encoder=a_, clip_model=a_, tokenizer=a_, unet=a_, scheduler=a_, feature_extractor=a_, coca_model=a_, coca_tokenizer=a_, coca_transform=a_, ) _snake_case : Any = ( feature_extractor.size if isinstance(feature_extractor.size, a_ ) else feature_extractor.size["""shortest_edge"""] ) _snake_case : List[Any] = transforms.Normalize(mean=feature_extractor.image_mean, std=feature_extractor.image_std ) set_requires_grad(self.text_encoder, a_ ) set_requires_grad(self.clip_model, a_ ) def UpperCamelCase_ ( self: Union[str, Any], a_: Optional[Union[str, int]] = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _snake_case : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(a_ ) def UpperCamelCase_ ( self: List[str] ): '''simple docstring''' self.enable_attention_slicing(a_ ) def UpperCamelCase_ ( self: List[Any] ): '''simple docstring''' set_requires_grad(self.vae, a_ ) def UpperCamelCase_ ( self: Optional[Any] ): '''simple docstring''' set_requires_grad(self.vae, a_ ) def UpperCamelCase_ ( self: List[Any] ): '''simple docstring''' set_requires_grad(self.unet, a_ ) def UpperCamelCase_ ( self: List[str] ): '''simple docstring''' set_requires_grad(self.unet, a_ ) def UpperCamelCase_ ( self: Optional[Any], a_: Optional[int], a_: Union[str, Any], a_: List[Any] ): '''simple docstring''' _snake_case : Any = min(int(num_inference_steps * strength ), a_ ) _snake_case : Union[str, Any] = max(num_inference_steps - init_timestep, 0 ) _snake_case : int = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def UpperCamelCase_ ( self: Union[str, Any], a_: List[Any], a_: int, a_: Tuple, a_: List[Any], a_: Tuple, a_: str=None ): '''simple docstring''' if not isinstance(a_, torch.Tensor ): raise ValueError(f"`image` has to be of type `torch.Tensor` but is {type(a_ )}" ) _snake_case : Tuple = image.to(device=a_, dtype=a_ ) if isinstance(a_, a_ ): _snake_case : Union[str, Any] = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(a_ ) ] _snake_case : int = torch.cat(a_, dim=0 ) else: _snake_case : int = self.vae.encode(a_ ).latent_dist.sample(a_ ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _snake_case : Dict = 0.18_215 * init_latents _snake_case : str = init_latents.repeat_interleave(a_, dim=0 ) _snake_case : str = randn_tensor(init_latents.shape, generator=a_, device=a_, dtype=a_ ) # get latents _snake_case : int = self.scheduler.add_noise(a_, a_, a_ ) _snake_case : Tuple = init_latents return latents def UpperCamelCase_ ( self: Tuple, a_: Any ): '''simple docstring''' _snake_case : Union[str, Any] = self.coca_transform(a_ ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): _snake_case : Tuple = self.coca_model.generate(transformed_image.to(device=self.device, dtype=self.coca_model.dtype ) ) _snake_case : Union[str, Any] = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split("""<end_of_text>""" )[0].replace("""<start_of_text>""", """""" ).rstrip(""" .,""" ) def UpperCamelCase_ ( self: int, a_: str, a_: Tuple ): '''simple docstring''' _snake_case : List[str] = self.feature_extractor.preprocess(a_ ) _snake_case : str = torch.from_numpy(clip_image_input["""pixel_values"""][0] ).unsqueeze(0 ).to(self.device ).half() _snake_case : int = self.clip_model.get_image_features(a_ ) _snake_case : Optional[Any] = image_embeddings_clip / image_embeddings_clip.norm(p=2, dim=-1, keepdim=a_ ) _snake_case : Union[str, Any] = image_embeddings_clip.repeat_interleave(a_, dim=0 ) return image_embeddings_clip @torch.enable_grad() def UpperCamelCase_ ( self: Any, a_: Optional[int], a_: Tuple, a_: Dict, a_: Tuple, a_: str, a_: List[str], a_: str, ): '''simple docstring''' _snake_case : Optional[Any] = latents.detach().requires_grad_() _snake_case : Optional[Any] = self.scheduler.scale_model_input(a_, a_ ) # predict the noise residual _snake_case : str = self.unet(a_, a_, encoder_hidden_states=a_ ).sample if isinstance(self.scheduler, (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): _snake_case : int = self.scheduler.alphas_cumprod[timestep] _snake_case : Optional[Any] = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _snake_case : List[str] = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 _snake_case : Union[str, Any] = torch.sqrt(a_ ) _snake_case : Optional[Any] = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler, a_ ): _snake_case : Optional[int] = self.scheduler.sigmas[index] _snake_case : int = latents - sigma * noise_pred else: raise ValueError(f"scheduler type {type(self.scheduler )} not supported" ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _snake_case : List[Any] = 1 / 0.18_215 * sample _snake_case : List[str] = self.vae.decode(a_ ).sample _snake_case : Tuple = (image / 2 + 0.5).clamp(0, 1 ) _snake_case : int = transforms.Resize(self.feature_extractor_size )(a_ ) _snake_case : str = self.normalize(a_ ).to(latents.dtype ) _snake_case : Dict = self.clip_model.get_image_features(a_ ) _snake_case : Union[str, Any] = image_embeddings_clip / image_embeddings_clip.norm(p=2, dim=-1, keepdim=a_ ) _snake_case : Any = spherical_dist_loss(a_, a_ ).mean() * clip_guidance_scale _snake_case : str = -torch.autograd.grad(a_, a_ )[0] if isinstance(self.scheduler, a_ ): _snake_case : Dict = latents.detach() + grads * (sigma**2) _snake_case : List[str] = noise_pred_original else: _snake_case : List[Any] = noise_pred_original - torch.sqrt(a_ ) * grads return noise_pred, latents @torch.no_grad() def __call__( self: List[Any], a_: Union[torch.FloatTensor, PIL.Image.Image], a_: Union[torch.FloatTensor, PIL.Image.Image], a_: Optional[str] = None, a_: Optional[str] = None, a_: Optional[int] = 512, a_: Optional[int] = 512, a_: float = 0.6, a_: Optional[int] = 50, a_: Optional[float] = 7.5, a_: Optional[int] = 1, a_: float = 0.0, a_: Optional[float] = 100, a_: Optional[torch.Generator] = None, a_: Optional[str] = "pil", a_: bool = True, a_: float = 0.8, a_: float = 0.1, a_: float = 0.1, ): '''simple docstring''' if isinstance(a_, a_ ) and len(a_ ) != batch_size: raise ValueError(f"You have passed {batch_size} batch_size, but only {len(a_ )} generators." ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if isinstance(a_, torch.Generator ) and batch_size > 1: _snake_case : Union[str, Any] = [generator] + [None] * (batch_size - 1) _snake_case : List[str] = [ ("""model""", self.coca_model is None), ("""tokenizer""", self.coca_tokenizer is None), ("""transform""", self.coca_transform is None), ] _snake_case : List[str] = [x[0] for x in coca_is_none if x[1]] _snake_case : Dict = """, """.join(a_ ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(a_ ): raise ValueError( f"Content prompt is None and CoCa [{coca_is_none_str}] is None." f"Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline." ) _snake_case : Union[str, Any] = self.get_image_description(a_ ) if style_prompt is None: if len(a_ ): raise ValueError( f"Style prompt is None and CoCa [{coca_is_none_str}] is None." f" Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline." ) _snake_case : Union[str, Any] = self.get_image_description(a_ ) # get prompt text embeddings for content and style _snake_case : Union[str, Any] = self.tokenizer( a_, padding="""max_length""", max_length=self.tokenizer.model_max_length, truncation=a_, return_tensors="""pt""", ) _snake_case : Optional[Any] = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] _snake_case : Optional[int] = self.tokenizer( a_, padding="""max_length""", max_length=self.tokenizer.model_max_length, truncation=a_, return_tensors="""pt""", ) _snake_case : Union[str, Any] = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] _snake_case : Dict = slerp(a_, a_, a_ ) # duplicate text embeddings for each generation per prompt _snake_case : List[Any] = text_embeddings.repeat_interleave(a_, dim=0 ) # set timesteps _snake_case : List[Any] = """offset""" in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) _snake_case : Dict = {} if accepts_offset: _snake_case : Optional[Any] = 1 self.scheduler.set_timesteps(a_, **a_ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) _snake_case : Optional[Any] = self.get_timesteps(a_, a_, self.device ) _snake_case : List[str] = timesteps[:1].repeat(a_ ) # Preprocess image _snake_case : List[Any] = preprocess(a_, a_, a_ ) _snake_case : Tuple = self.prepare_latents( a_, a_, a_, text_embeddings.dtype, self.device, a_ ) _snake_case : List[Any] = preprocess(a_, a_, a_ ) _snake_case : Tuple = self.prepare_latents( a_, a_, a_, text_embeddings.dtype, self.device, a_ ) _snake_case : str = slerp(a_, a_, a_ ) if clip_guidance_scale > 0: _snake_case : int = self.get_clip_image_embeddings(a_, a_ ) _snake_case : str = self.get_clip_image_embeddings(a_, a_ ) _snake_case : str = slerp( a_, a_, a_ ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. _snake_case : List[str] = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _snake_case : str = content_text_input.input_ids.shape[-1] _snake_case : Optional[int] = self.tokenizer([""""""], padding="""max_length""", max_length=a_, return_tensors="""pt""" ) _snake_case : List[str] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt _snake_case : List[str] = uncond_embeddings.repeat_interleave(a_, dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _snake_case : Dict = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. _snake_case : str = (batch_size, self.unet.config.in_channels, height // 8, width // 8) _snake_case : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps _snake_case : Dict = torch.randn(a_, generator=a_, device="""cpu""", dtype=a_ ).to( self.device ) else: _snake_case : Any = torch.randn(a_, generator=a_, device=self.device, dtype=a_ ) else: if latents.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) _snake_case : Union[str, Any] = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _snake_case : Union[str, Any] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] _snake_case : Any = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _snake_case : Tuple = {} if accepts_eta: _snake_case : Optional[int] = eta # check if the scheduler accepts generator _snake_case : Tuple = """generator""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: _snake_case : Any = generator with self.progress_bar(total=a_ ): for i, t in enumerate(a_ ): # expand the latents if we are doing classifier free guidance _snake_case : Dict = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _snake_case : Optional[Any] = self.scheduler.scale_model_input(a_, a_ ) # predict the noise residual _snake_case : Tuple = self.unet(a_, a_, encoder_hidden_states=a_ ).sample # perform classifier free guidance if do_classifier_free_guidance: _snake_case : List[str] = noise_pred.chunk(2 ) _snake_case : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: _snake_case : int = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) _snake_case : Any = self.cond_fn( a_, a_, a_, a_, a_, a_, a_, ) # compute the previous noisy sample x_t -> x_t-1 _snake_case : int = self.scheduler.step(a_, a_, a_, **a_ ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _snake_case : Optional[Any] = 1 / 0.18_215 * latents _snake_case : Tuple = self.vae.decode(a_ ).sample _snake_case : Optional[int] = (image / 2 + 0.5).clamp(0, 1 ) _snake_case : List[str] = image.cpu().permute(0, 2, 3, 1 ).numpy() if output_type == "pil": _snake_case : List[Any] = self.numpy_to_pil(a_ ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=a_, nsfw_content_detected=a_ )
368
"""simple docstring""" import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging A_ = logging.get_logger(__name__) def UpperCAmelCase__ (snake_case__ : List[str] , snake_case__ : Dict , snake_case__ : Union[str, Any] , snake_case__ : int=False ): """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( """Loading a PyTorch model in Flax, requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise if not is_sharded: _snake_case : Dict = os.path.abspath(snake_case__ ) logger.info(F"Loading PyTorch weights from {pt_path}" ) _snake_case : Tuple = torch.load(snake_case__ , map_location="""cpu""" ) logger.info(F"PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters." ) _snake_case : int = convert_pytorch_state_dict_to_flax(snake_case__ , snake_case__ ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files _snake_case : Dict = convert_pytorch_sharded_state_dict_to_flax(snake_case__ , snake_case__ ) return flax_state_dict def UpperCAmelCase__ (snake_case__ : Tuple[str] , snake_case__ : np.ndarray , snake_case__ : Dict[str, jnp.ndarray] , snake_case__ : str , ): """simple docstring""" def is_key_or_prefix_key_in_dict(snake_case__ : Tuple[str] ) -> bool: return len(set(snake_case__ ) & {key, (model_prefix,) + key} ) > 0 # layer norm _snake_case : Any = pt_tuple_key[:-1] + ("""scale""",) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(snake_case__ ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean _snake_case : Optional[Any] = pt_tuple_key[:-1] + ("""mean""",) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(snake_case__ ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var _snake_case : Any = pt_tuple_key[:-1] + ("""var""",) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(snake_case__ ): return renamed_pt_tuple_key, pt_tensor # embedding _snake_case : Any = pt_tuple_key[:-1] + ("""embedding""",) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(snake_case__ ): return renamed_pt_tuple_key, pt_tensor # conv layer _snake_case : Optional[int] = pt_tuple_key[:-1] + ("""kernel""",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(snake_case__ ): _snake_case : Dict = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer _snake_case : List[str] = pt_tuple_key[:-1] + ("""kernel""",) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(snake_case__ ): _snake_case : List[Any] = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight _snake_case : List[Any] = pt_tuple_key[:-1] + ("""weight""",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias _snake_case : Tuple = pt_tuple_key[:-1] + ("""bias""",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 _snake_case : Optional[Any] = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): _snake_case : Union[str, Any] = pt_tuple_key[-2] + """_g""" elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): _snake_case : Dict = pt_tuple_key[-2] + """_v""" if name is not None: _snake_case : Union[str, Any] = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def UpperCAmelCase__ (snake_case__ : Union[str, Any] , snake_case__ : Optional[int] ): """simple docstring""" _snake_case : Tuple = {k: v.numpy() for k, v in pt_state_dict.items()} _snake_case : int = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: _snake_case : Dict = flax_model.params["""params"""] else: _snake_case : List[Any] = flax_model.params _snake_case : Tuple = flatten_dict(snake_case__ ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: _snake_case : Union[str, Any] = flatten_dict(flax_model.params["""batch_stats"""] ) random_flax_state_dict.update(snake_case__ ) _snake_case : Tuple = {} _snake_case : Dict = (model_prefix not in flax_model_params) and ( model_prefix in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) _snake_case : Optional[int] = (model_prefix in flax_model_params) and ( model_prefix not in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): _snake_case : int = tuple(pt_key.split(""".""" ) ) # remove base model prefix if necessary _snake_case : Optional[Any] = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: _snake_case : Union[str, Any] = pt_tuple_key[1:] # Correctly rename weight parameters _snake_case , _snake_case : int = rename_key_and_reshape_tensor( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # add model prefix if necessary _snake_case : Dict = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: _snake_case : int = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: _snake_case : Union[str, Any] = jnp.asarray(snake_case__ ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(snake_case__ , snake_case__ ) continue # also add unexpected weight so that warning is thrown _snake_case : List[Any] = jnp.asarray(snake_case__ ) else: # also add unexpected weight so that warning is thrown _snake_case : Optional[Any] = jnp.asarray(snake_case__ ) return unflatten_dict(snake_case__ ) def UpperCAmelCase__ (snake_case__ : Optional[int] , snake_case__ : Dict ): """simple docstring""" import torch # Load the index _snake_case : str = {} for shard_file in shard_filenames: # load using msgpack utils _snake_case : Union[str, Any] = torch.load(snake_case__ ) _snake_case : Optional[Any] = {k: v.numpy() for k, v in pt_state_dict.items()} _snake_case : List[str] = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: _snake_case : str = flax_model.params["""params"""] _snake_case : List[Any] = flatten_dict(snake_case__ ) random_flax_state_dict.update(flatten_dict(flax_model.params["""batch_stats"""] ) ) else: _snake_case : List[Any] = flax_model.params _snake_case : Tuple = flatten_dict(snake_case__ ) _snake_case : Tuple = (model_prefix not in flax_model_params) and ( model_prefix in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) _snake_case : Optional[Any] = (model_prefix in flax_model_params) and ( model_prefix not in {k.split(""".""" )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): _snake_case : List[str] = tuple(pt_key.split(""".""" ) ) # remove base model prefix if necessary _snake_case : str = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: _snake_case : Optional[Any] = pt_tuple_key[1:] # Correctly rename weight parameters _snake_case , _snake_case : Optional[Any] = rename_key_and_reshape_tensor( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # add model prefix if necessary _snake_case : List[str] = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: _snake_case : Any = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: _snake_case : Optional[int] = jnp.asarray(snake_case__ ) continue if "var" in flax_key[-1]: _snake_case : Any = jnp.asarray(snake_case__ ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(snake_case__ , snake_case__ ) continue # also add unexpected weight so that warning is thrown _snake_case : List[str] = jnp.asarray(snake_case__ ) else: # also add unexpected weight so that warning is thrown _snake_case : Optional[Any] = jnp.asarray(snake_case__ ) return unflatten_dict(snake_case__ ) def UpperCAmelCase__ (snake_case__ : List[str] , snake_case__ : Optional[Any] ): """simple docstring""" _snake_case : Optional[Any] = os.path.abspath(snake_case__ ) logger.info(F"Loading Flax weights from {flax_checkpoint_path}" ) # import correct flax class _snake_case : Union[str, Any] = getattr(snake_case__ , """Flax""" + model.__class__.__name__ ) # load flax weight dict with open(snake_case__ , """rb""" ) as state_f: try: _snake_case : Dict = from_bytes(snake_case__ , state_f.read() ) except UnpicklingError: raise EnvironmentError(F"Unable to convert {flax_checkpoint_path} to Flax deserializable object. " ) return load_flax_weights_in_pytorch_model(snake_case__ , snake_case__ ) def UpperCAmelCase__ (snake_case__ : Tuple , snake_case__ : Optional[int] ): """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( """Loading a Flax weights in PyTorch, requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights _snake_case : Optional[int] = flatten_dict(jax.tree_util.tree_map(lambda snake_case__ : x.dtype == jnp.bfloataa , snake_case__ ) ).values() if any(snake_case__ ): # convert all weights to fp32 if the are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) _snake_case : Optional[int] = jax.tree_util.tree_map( lambda snake_case__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , snake_case__ ) _snake_case : Dict = flatten_dict(snake_case__ ) _snake_case : Optional[Any] = pt_model.state_dict() _snake_case : Union[str, Any] = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split(""".""" )[0] for k in pt_model_dict.keys()} ) _snake_case : Optional[int] = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split(""".""" )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys _snake_case : str = [] _snake_case : Tuple = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): _snake_case : Tuple = flax_key_tuple[0] == pt_model.base_model_prefix _snake_case : Optional[Any] = """.""".join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: _snake_case : List[str] = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: _snake_case : Union[str, Any] = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(snake_case__ ) not in pt_model_dict: # conv layer _snake_case : Union[str, Any] = flax_key_tuple[:-1] + ("""weight""",) _snake_case : Optional[int] = jnp.transpose(snake_case__ , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(snake_case__ ) not in pt_model_dict: # linear layer _snake_case : Optional[int] = flax_key_tuple[:-1] + ("""weight""",) _snake_case : Union[str, Any] = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: _snake_case : int = flax_key_tuple[:-1] + ("""weight""",) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: _snake_case : Tuple = flax_key_tuple[:-1] + ("""running_mean""",) elif "var" in flax_key_tuple[-1]: _snake_case : Optional[int] = flax_key_tuple[:-1] + ("""running_var""",) if "batch_stats" in flax_state: _snake_case : int = """.""".join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: _snake_case : int = """.""".join(snake_case__ ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. _snake_case : Optional[Any] = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: _snake_case : List[str] = key.split(""".""" ) _snake_case : Optional[int] = None if key_components[-3::2] == ["parametrizations", "original0"]: _snake_case : int = key_components[-2] + """_g""" elif key_components[-3::2] == ["parametrizations", "original1"]: _snake_case : Union[str, Any] = key_components[-2] + """_v""" if name is not None: _snake_case : Dict = key_components[:-3] + [name] _snake_case : Dict = """.""".join(snake_case__ ) _snake_case : str = key if flax_key in special_pt_names: _snake_case : Union[str, Any] = special_pt_names[flax_key] if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F"Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected " F"to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}." ) else: # add weight to pytorch dict _snake_case : List[str] = np.asarray(snake_case__ ) if not isinstance(snake_case__ , np.ndarray ) else flax_tensor _snake_case : List[Any] = torch.from_numpy(snake_case__ ) # remove from missing keys missing_keys.remove(snake_case__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(snake_case__ ) pt_model.load_state_dict(snake_case__ ) # re-transform missing_keys to list _snake_case : List[str] = list(snake_case__ ) if len(snake_case__ ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" F" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing" F" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture" """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" F" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect" """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) else: logger.warning(F"All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n" ) if len(snake_case__ ) > 0: logger.warning( F"Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly" F" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to" """ use it for predictions and inference.""" ) else: logger.warning( F"All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n" """If your task is similar to the task the model of the checkpoint was trained on, """ F"you can already use {pt_model.__class__.__name__} for predictions without further training." ) return pt_model
132
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase_ = logging.get_logger(__name__) lowercase_ = { 'microsoft/focalnet-tiny': 'https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json', } class __lowerCAmelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): _a = """focalnet""" def __init__( self , lowerCAmelCase=224 , lowerCAmelCase=4 , lowerCAmelCase=3 , lowerCAmelCase=96 , lowerCAmelCase=False , lowerCAmelCase=[192, 384, 768, 768] , lowerCAmelCase=[2, 2, 6, 2] , lowerCAmelCase=[2, 2, 2, 2] , lowerCAmelCase=[3, 3, 3, 3] , lowerCAmelCase="gelu" , lowerCAmelCase=4.0 , lowerCAmelCase=0.0 , lowerCAmelCase=0.1 , lowerCAmelCase=False , lowerCAmelCase=1e-4 , lowerCAmelCase=False , lowerCAmelCase=False , lowerCAmelCase=False , lowerCAmelCase=0.02 , lowerCAmelCase=1e-5 , lowerCAmelCase=32 , lowerCAmelCase=None , lowerCAmelCase=None , **lowerCAmelCase , ) -> List[str]: '''simple docstring''' super().__init__(**lowerCAmelCase ) _lowercase =image_size _lowercase =patch_size _lowercase =num_channels _lowercase =embed_dim _lowercase =use_conv_embed _lowercase =hidden_sizes _lowercase =depths _lowercase =focal_levels _lowercase =focal_windows _lowercase =hidden_act _lowercase =mlp_ratio _lowercase =hidden_dropout_prob _lowercase =drop_path_rate _lowercase =use_layerscale _lowercase =layerscale_value _lowercase =use_post_layernorm _lowercase =use_post_layernorm_in_modulation _lowercase =normalize_modulator _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =encoder_stride _lowercase =['stem'] + [F'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] _lowercase , _lowercase =get_aligned_output_features_output_indices( out_features=lowerCAmelCase , out_indices=lowerCAmelCase , stage_names=self.stage_names )
205
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowercase_ = 1_6 lowercase_ = 3_2 def a ( A__ : Accelerator , A__ : int = 16 , A__ : str = "bert-base-cased" ) -> Optional[int]: """simple docstring""" _lowercase =AutoTokenizer.from_pretrained(A__ ) _lowercase =load_dataset('glue' , 'mrpc' ) def tokenize_function(A__ : Optional[int] ): # max_length=None => use the model max length (it's actually the default) _lowercase =tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=A__ , max_length=A__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset _lowercase =datasets.map( A__ , batched=A__ , remove_columns=['idx', 'sentence1', 'sentence2'] , load_from_cache_file=A__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _lowercase =tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(A__ : List[str] ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(A__ , padding='max_length' , max_length=128 , return_tensors='pt' ) return tokenizer.pad(A__ , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. _lowercase =DataLoader( tokenized_datasets['train'] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) _lowercase =DataLoader( tokenized_datasets['validation'] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) return train_dataloader, eval_dataloader def a ( A__ : Optional[Any] , A__ : Optional[int] , A__ : List[str] , A__ : Dict ) -> Dict: """simple docstring""" model.eval() _lowercase =0 for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): _lowercase =model(**A__ ) _lowercase =outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times _lowercase , _lowercase =accelerator.gather( (predictions, batch['labels']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(A__ ) - 1: _lowercase =predictions[: len(eval_dataloader.dataset ) - samples_seen] _lowercase =references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=A__ , references=A__ , ) _lowercase =metric.compute() return eval_metric["accuracy"] def a ( A__ : str , A__ : Optional[Any] ) -> Optional[Any]: """simple docstring""" _lowercase =Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _lowercase =config['lr'] _lowercase =int(config['num_epochs'] ) _lowercase =int(config['seed'] ) _lowercase =int(config['batch_size'] ) _lowercase =args.model_name_or_path set_seed(A__ ) _lowercase , _lowercase =get_dataloaders(A__ , A__ , A__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _lowercase =AutoModelForSequenceClassification.from_pretrained(A__ , return_dict=A__ ) # Instantiate optimizer _lowercase =( AdamW if accelerator.state.deepspeed_plugin is None or 'optimizer' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) _lowercase =optimizer_cls(params=model.parameters() , lr=A__ ) if accelerator.state.deepspeed_plugin is not None: _lowercase =accelerator.state.deepspeed_plugin.deepspeed_config[ 'gradient_accumulation_steps' ] else: _lowercase =1 _lowercase =(len(A__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): _lowercase =get_linear_schedule_with_warmup( optimizer=A__ , num_warmup_steps=0 , num_training_steps=A__ , ) else: _lowercase =DummyScheduler(A__ , total_num_steps=A__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. _lowercase , _lowercase , _lowercase , _lowercase , _lowercase =accelerator.prepare( A__ , A__ , A__ , A__ , A__ ) # We need to keep track of how many total steps we have iterated over _lowercase =0 # We also need to keep track of the stating epoch so files are named properly _lowercase =0 _lowercase =evaluate.load('glue' , 'mrpc' ) _lowercase =num_epochs if args.partial_train_epoch is not None: _lowercase =args.partial_train_epoch if args.resume_from_checkpoint: accelerator.load_state(args.resume_from_checkpoint ) _lowercase =args.resume_from_checkpoint.split('epoch_' )[1] _lowercase ='' for char in epoch_string: if char.isdigit(): state_epoch_num += char else: break _lowercase =int(A__ ) + 1 _lowercase =evaluation_loop(A__ , A__ , A__ , A__ ) accelerator.print('resumed checkpoint performance:' , A__ ) accelerator.print('resumed checkpoint\'s scheduler\'s lr:' , lr_scheduler.get_lr()[0] ) accelerator.print('resumed optimizers\'s lr:' , optimizer.param_groups[0]['lr'] ) with open(os.path.join(args.output_dir , F'''state_{starting_epoch-1}.json''' ) , 'r' ) as f: _lowercase =json.load(A__ ) assert resumed_state["accuracy"] == accuracy, "Accuracy mismatch, loading from checkpoint failed" assert ( resumed_state["lr"] == lr_scheduler.get_lr()[0] ), "Scheduler learning rate mismatch, loading from checkpoint failed" assert ( resumed_state["optimizer_lr"] == optimizer.param_groups[0]["lr"] ), "Optimizer learning rate mismatch, loading from checkpoint failed" assert resumed_state["epoch"] == starting_epoch - 1, "Epoch mismatch, loading from checkpoint failed" return # Now we train the model _lowercase ={} for epoch in range(A__ , A__ ): model.train() for step, batch in enumerate(A__ ): _lowercase =model(**A__ ) _lowercase =outputs.loss _lowercase =loss / gradient_accumulation_steps accelerator.backward(A__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 _lowercase =F'''epoch_{epoch}''' _lowercase =os.path.join(args.output_dir , A__ ) accelerator.save_state(A__ ) _lowercase =evaluation_loop(A__ , A__ , A__ , A__ ) _lowercase =accuracy _lowercase =lr_scheduler.get_lr()[0] _lowercase =optimizer.param_groups[0]['lr'] _lowercase =epoch _lowercase =overall_step accelerator.print(F'''epoch {epoch}:''' , A__ ) accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , F'''state_{epoch}.json''' ) , 'w' ) as f: json.dump(A__ , A__ ) def a ( ) -> Tuple: """simple docstring""" _lowercase =argparse.ArgumentParser(description='Simple example of training script tracking peak GPU memory usage.' ) parser.add_argument( '--model_name_or_path' , type=A__ , default='bert-base-cased' , help='Path to pretrained model or model identifier from huggingface.co/models.' , required=A__ , ) parser.add_argument( '--output_dir' , type=A__ , default='.' , help='Optional save directory where all checkpoint folders will be stored. Default is the current working directory.' , ) parser.add_argument( '--resume_from_checkpoint' , type=A__ , default=A__ , help='If the training should continue from a checkpoint folder.' , ) parser.add_argument( '--partial_train_epoch' , type=A__ , default=A__ , help='If passed, the training will stop after this number of epochs.' , ) parser.add_argument( '--num_epochs' , type=A__ , default=2 , help='Number of train epochs.' , ) _lowercase =parser.parse_args() _lowercase ={'lr': 2e-5, 'num_epochs': args.num_epochs, 'seed': 42, 'batch_size': 16} training_function(A__ , A__ ) if __name__ == "__main__": main()
205
1
"""simple docstring""" import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() a : Union[str, Any] = logging.get_logger(__name__) a : int = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } a : Any = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def _SCREAMING_SNAKE_CASE ( _lowercase : Union[str, Any] , _lowercase : List[Any] , _lowercase : Optional[Any] , _lowercase : List[Any] , _lowercase : Tuple ) ->str: '''simple docstring''' for attribute in key.split("." ): a : Union[str, Any] = getattr(_lowercase , _lowercase ) if weight_type is not None: a : Optional[Any] = getattr(_lowercase , _lowercase ).shape else: a : Optional[int] = hf_pointer.shape assert hf_shape == value.shape, ( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": a : Any = value elif weight_type == "weight_g": a : Optional[int] = value elif weight_type == "weight_v": a : Tuple = value elif weight_type == "bias": a : Dict = value else: a : List[str] = value logger.info(F"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.""" ) def _SCREAMING_SNAKE_CASE ( _lowercase : Optional[int] , _lowercase : Tuple ) ->int: '''simple docstring''' a : str = [] a : List[Any] = fairseq_model.state_dict() a : Dict = hf_model.feature_extractor a : str = hf_model.adapter for name, value in fairseq_dict.items(): a : Dict = False if "conv_layers" in name: load_conv_layer( _lowercase , _lowercase , _lowercase , _lowercase , hf_model.config.feat_extract_norm == "group" , ) a : Optional[int] = True elif any(x in name for x in ["adaptor", "w2v_encoder.proj.", "w2v_proj_ln."] ): load_adapter(_lowercase , _lowercase , _lowercase , _lowercase ) a : Dict = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: a : Optional[int] = True if "*" in mapped_key: a : Any = name.split(_lowercase )[0].split("." )[-2] a : int = mapped_key.replace("*" , _lowercase ) if "weight_g" in name: a : Any = "weight_g" elif "weight_v" in name: a : Tuple = "weight_v" elif "bias" in name: a : List[str] = "bias" elif "weight" in name: a : Tuple = "weight" else: a : Any = None set_recursively(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) continue if not is_used: unused_weights.append(_lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) def _SCREAMING_SNAKE_CASE ( _lowercase : Optional[Any] , _lowercase : Optional[int] , _lowercase : Any , _lowercase : Dict , _lowercase : List[str] ) ->List[str]: '''simple docstring''' a : List[Any] = full_name.split("conv_layers." )[-1] a : Any = name.split("." ) a : List[Any] = int(items[0] ) a : str = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) a : List[Any] = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) a : int = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) a : Union[str, Any] = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) a : Dict = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(_lowercase ) def _SCREAMING_SNAKE_CASE ( _lowercase : Any , _lowercase : Any , _lowercase : Optional[int] , _lowercase : Union[str, Any] ) ->Optional[int]: '''simple docstring''' a : Any = full_name.split("adaptor." )[-1] a : str = name.split("." ) if items[1].isdigit(): a : int = int(items[1] ) else: a : List[str] = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found.""" a : Optional[Any] = value logger.info(F"""Adapter proj layer norm bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found.""" a : str = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found.""" a : Dict = value logger.info(F"""Adapter proj layer bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found.""" a : Optional[Any] = value logger.info(F"""Adapter proj layer weight was initialized from {full_name}.""" ) elif isinstance(_lowercase , _lowercase ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found.""" a : List[str] = value logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found.""" a : Tuple = value logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) else: unused_weights.append(_lowercase ) def _SCREAMING_SNAKE_CASE ( _lowercase : Tuple ) ->List[str]: '''simple docstring''' a, a : Dict = emb.weight.shape a : str = nn.Linear(_lowercase , _lowercase , bias=_lowercase ) a : Optional[int] = emb.weight.data return lin_layer @torch.no_grad() def _SCREAMING_SNAKE_CASE ( _lowercase : List[Any] , _lowercase : int , _lowercase : str , _lowercase : List[str] , _lowercase : Optional[int] , _lowercase : Dict , _lowercase : Any , _lowercase : int , _lowercase : Optional[int] , _lowercase : List[str] , _lowercase : Tuple , ) ->List[Any]: '''simple docstring''' a : int = WavaVecaConfig.from_pretrained( _lowercase , add_adapter=_lowercase , adapter_stride=_lowercase , adapter_kernel_size=_lowercase , use_auth_token=_lowercase , output_hidden_size=_lowercase , ) a : Any = MBartConfig.from_pretrained(_lowercase ) # load model a, a, a : int = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ "config_yaml": config_yaml_path, "data": "/".join(dict_path.split("/" )[:-1] ), "w2v_path": checkpoint_path, "load_pretrained_decoder_from": None, } , ) a : Optional[int] = model[0].eval() # load feature extractor a : Dict = WavaVecaFeatureExtractor.from_pretrained(_lowercase , use_auth_token=_lowercase ) # set weights for wav2vec2 encoder a : Optional[Any] = WavaVecaModel(_lowercase ) recursively_load_weights_wavaveca(model.encoder , _lowercase ) # load decoder weights a : Optional[int] = MBartForCausalLM(_lowercase ) a, a : List[Any] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_lowercase ) logger.warning(F"""The following keys are missing when loading the decoder weights: {missing_keys}""" ) logger.warning(F"""The following keys are unexpected when loading the decoder weights: {unexpected_keys}""" ) a : Optional[Any] = SpeechEncoderDecoderModel(encoder=_lowercase , decoder=_lowercase ) a : Union[str, Any] = False a : List[Any] = MBartaaTokenizer(_lowercase ) tokenizer.save_pretrained(_lowercase ) a : Union[str, Any] = hf_wavavec.config.to_dict() a : Optional[Any] = tokenizer.pad_token_id a : str = tokenizer.bos_token_id a : List[str] = tokenizer.eos_token_id a : int = "mbart50" a : Any = "wav2vec2" a : Union[str, Any] = tokenizer.eos_token_id a : Union[str, Any] = 25_0004 a : int = tokenizer.eos_token_id a : List[str] = SpeechEncoderDecoderConfig.from_dict(_lowercase ) hf_wavavec.save_pretrained(_lowercase ) feature_extractor.save_pretrained(_lowercase ) if __name__ == "__main__": a : 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('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_yaml_path''', default=None, type=str, help='''Path to yaml file of fine-tuned model''') parser.add_argument( '''--encoder_config_path''', default='''facebook/wav2vec2-xls-r-1b''', type=str, help='''Path to hf encoder wav2vec2 checkpoint config''', ) parser.add_argument( '''--decoder_config_path''', default='''facebook/mbart-large-50-one-to-many-mmt''', type=str, help='''Path to hf decoder checkpoint config''', ) parser.add_argument('''--add_adapter''', default=True, type=bool, help='''whethere to add model adapter layers''') parser.add_argument('''--adapter_stride''', default=2, type=int, help='''stride of adapter layers''') parser.add_argument('''--adapter_kernel_size''', default=3, type=int, help='''kernel size of adapter layers''') parser.add_argument('''--encoder_output_dim''', default=1024, type=int, help='''encoder output dim''') parser.add_argument('''--start_token_id''', default=250004, type=int, help='''`decoder_start_token_id` of model config''') a : Optional[Any] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
79
"""simple docstring""" from datetime import datetime import requests def _SCREAMING_SNAKE_CASE ( _lowercase : str ) ->bytes: '''simple docstring''' a : Optional[Any] = "https://downloadgram.net/wp-json/wppress/video-downloader/video?url=" a : int = requests.get(base_url + url ).json()[0]["urls"][0]["src"] return requests.get(_lowercase ).content if __name__ == "__main__": a : str = input('''Enter Video/IGTV url: ''').strip() a : str = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4''' with open(file_name, '''wb''') as fp: fp.write(download_video(url)) print(F'''Done. Video saved to disk as {file_name}.''')
79
1
from __future__ import annotations from cmath import sqrt def __lowercase ( a__ , a__ , a__ ) -> tuple[complex, complex]: if a == 0: raise ValueError('Coefficient \'a\' must not be zero.' ) __SCREAMING_SNAKE_CASE = b * b - 4 * a * c __SCREAMING_SNAKE_CASE = (-b + sqrt(a__ )) / (2 * a) __SCREAMING_SNAKE_CASE = (-b - sqrt(a__ )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def __lowercase ( ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = quadratic_roots(a=5 , b=6 , c=1 ) print(f"""The solutions are: {solutiona} and {solutiona}""" ) if __name__ == "__main__": main()
257
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() lowerCAmelCase__ : List[Any] =logging.get_logger(__name__) def __lowercase ( a__ ) -> List[str]: __SCREAMING_SNAKE_CASE = 'huggingface/label-files' __SCREAMING_SNAKE_CASE = 'imagenet-1k-id2label.json' __SCREAMING_SNAKE_CASE = json.load(open(hf_hub_download(a__ , a__ , repo_type='dataset' ) , 'r' ) ) __SCREAMING_SNAKE_CASE = {int(a__ ): v for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE = {v: k for k, v in idalabel.items()} __SCREAMING_SNAKE_CASE = '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" __SCREAMING_SNAKE_CASE = BitConfig( conv_layer=a__ , num_labels=10_00 , idalabel=a__ , labelaid=a__ , ) return config def __lowercase ( a__ ) -> str: if "stem.conv" in name: __SCREAMING_SNAKE_CASE = name.replace('stem.conv' , 'bit.embedder.convolution' ) if "blocks" in name: __SCREAMING_SNAKE_CASE = name.replace('blocks' , 'layers' ) if "head.fc" in name: __SCREAMING_SNAKE_CASE = name.replace('head.fc' , 'classifier.1' ) if name.startswith('norm' ): __SCREAMING_SNAKE_CASE = 'bit.' + name if "bit" not in name and "classifier" not in name: __SCREAMING_SNAKE_CASE = 'bit.encoder.' + name return name def __lowercase ( ) -> int: __SCREAMING_SNAKE_CASE = 'http://images.cocodataset.org/val2017/000000039769.jpg' __SCREAMING_SNAKE_CASE = Image.open(requests.get(a__ , stream=a__ ).raw ) return im @torch.no_grad() def __lowercase ( a__ , a__ , a__=False ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = get_config(a__ ) # load original model from timm __SCREAMING_SNAKE_CASE = create_model(a__ , pretrained=a__ ) timm_model.eval() # load state_dict of original model __SCREAMING_SNAKE_CASE = timm_model.state_dict() for key in state_dict.copy().keys(): __SCREAMING_SNAKE_CASE = state_dict.pop(a__ ) __SCREAMING_SNAKE_CASE = val.squeeze() if 'head' in key else val # load HuggingFace model __SCREAMING_SNAKE_CASE = BitForImageClassification(a__ ) model.eval() model.load_state_dict(a__ ) # create image processor __SCREAMING_SNAKE_CASE = create_transform(**resolve_data_config({} , model=a__ ) ) __SCREAMING_SNAKE_CASE = transform.transforms __SCREAMING_SNAKE_CASE = { 'bilinear': PILImageResampling.BILINEAR, 'bicubic': PILImageResampling.BICUBIC, 'nearest': PILImageResampling.NEAREST, } __SCREAMING_SNAKE_CASE = 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() , ) __SCREAMING_SNAKE_CASE = prepare_img() __SCREAMING_SNAKE_CASE = transform(a__ ).unsqueeze(0 ) __SCREAMING_SNAKE_CASE = processor(a__ , return_tensors='pt' ).pixel_values # verify pixel values assert torch.allclose(a__ , a__ ) # verify logits with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(a__ ) __SCREAMING_SNAKE_CASE = outputs.logits print('Logits:' , logits[0, :3] ) print('Predicted class:' , model.config.idalabel[logits.argmax(-1 ).item()] ) __SCREAMING_SNAKE_CASE = 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__": lowerCAmelCase__ : 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.''', ) lowerCAmelCase__ : int =parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
257
1
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( ImageTextPipelineOutput, UniDiffuserPipeline, ) else: from .modeling_text_decoder import UniDiffuserTextDecoder from .modeling_uvit import UniDiffuserModel, UTransformeraDModel from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline
0
'''simple docstring''' import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import numpy as np import pytest from datasets.arrow_dataset import Dataset from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex from .utils import require_elasticsearch, require_faiss A__ : Tuple = pytest.mark.integration @require_faiss class snake_case__ ( SCREAMING_SNAKE_CASE_ ): def A_ ( self : Any ) -> Tuple: '''simple docstring''' __snake_case : Dict = Dataset.from_dict({'filename': ['my_name-train' + '_' + str(__a ) for x in np.arange(30 ).tolist()]} ) return dset def A_ ( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' import faiss __snake_case : Dataset = self._create_dummy_dataset() __snake_case : Dict = dset.map( lambda __a , __a : {"vecs": i * np.ones(5 , dtype=np.floataa )} , with_indices=__a , keep_in_memory=__a ) __snake_case : List[Any] = dset.add_faiss_index('vecs' , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT ) __snake_case , __snake_case : Any = dset.get_nearest_examples('vecs' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) dset.drop_index('vecs' ) def A_ ( self : Tuple ) -> Any: '''simple docstring''' import faiss __snake_case : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='vecs' , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT , ) __snake_case , __snake_case : Any = dset.get_nearest_examples('vecs' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) def A_ ( self : List[Any] ) -> Dict: '''simple docstring''' import faiss __snake_case : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='vecs' , metric_type=faiss.METRIC_INNER_PRODUCT , ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=__a ) as tmp_file: dset.save_faiss_index('vecs' , tmp_file.name ) dset.load_faiss_index('vecs2' , tmp_file.name ) os.unlink(tmp_file.name ) __snake_case , __snake_case : str = dset.get_nearest_examples('vecs2' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) def A_ ( self : Union[str, Any] ) -> Dict: '''simple docstring''' __snake_case : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='vecs' ) dset.drop_index('vecs' ) self.assertRaises(__a , partial(dset.get_nearest_examples , 'vecs2' , np.ones(5 , dtype=np.floataa ) ) ) def A_ ( self : List[str] ) -> List[str]: '''simple docstring''' from elasticsearch import Elasticsearch __snake_case : Dataset = self._create_dummy_dataset() with patch('elasticsearch.Elasticsearch.search' ) as mocked_search, patch( 'elasticsearch.client.IndicesClient.create' ) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk' ) as mocked_bulk: __snake_case : Any = {'acknowledged': True} mocked_bulk.return_value([(True, None)] * 30 ) __snake_case : Dict = {'hits': {'hits': [{'_score': 1, '_id': 29}]}} __snake_case : Union[str, Any] = Elasticsearch() dset.add_elasticsearch_index('filename' , es_client=__a ) __snake_case , __snake_case : str = dset.get_nearest_examples('filename' , 'my_name-train_29' ) self.assertEqual(examples['filename'][0] , 'my_name-train_29' ) @require_faiss class snake_case__ ( SCREAMING_SNAKE_CASE_ ): def A_ ( self : str ) -> int: '''simple docstring''' import faiss __snake_case : int = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) # add vectors index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsNotNone(index.faiss_index ) self.assertEqual(index.faiss_index.ntotal , 5 ) index.add_vectors(np.zeros((5, 5) , dtype=np.floataa ) ) self.assertEqual(index.faiss_index.ntotal , 10 ) # single query __snake_case : Dict = np.zeros(5 , dtype=np.floataa ) __snake_case : List[str] = 1 __snake_case , __snake_case : List[Any] = index.search(__a ) self.assertRaises(__a , index.search , query.reshape(-1 , 1 ) ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) # batched queries __snake_case : List[str] = np.eye(5 , dtype=np.floataa )[::-1] __snake_case , __snake_case : Dict = index.search_batch(__a ) self.assertRaises(__a , index.search_batch , queries[0] ) __snake_case : Any = [scores[0] for scores in total_scores] __snake_case : List[Any] = [indices[0] for indices in total_indices] self.assertGreater(np.min(__a ) , 0 ) self.assertListEqual([4, 3, 2, 1, 0] , __a ) def A_ ( self : int ) -> int: '''simple docstring''' import faiss __snake_case : int = FaissIndex(string_factory='Flat' ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) __snake_case : List[str] = FaissIndex(string_factory='LSH' ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexLSH ) with self.assertRaises(__a ): __snake_case : Dict = FaissIndex(string_factory='Flat' , custom_index=faiss.IndexFlat(5 ) ) def A_ ( self : str ) -> Dict: '''simple docstring''' import faiss __snake_case : Tuple = faiss.IndexFlat(5 ) __snake_case : List[Any] = FaissIndex(custom_index=__a ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) def A_ ( self : List[Any] ) -> int: '''simple docstring''' import faiss __snake_case : Optional[Any] = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=__a ) as tmp_file: index.save(tmp_file.name ) __snake_case : List[Any] = FaissIndex.load(tmp_file.name ) os.unlink(tmp_file.name ) __snake_case : List[Any] = np.zeros(5 , dtype=np.floataa ) __snake_case : Any = 1 __snake_case , __snake_case : int = index.search(__a ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) @require_faiss def a_ ( _UpperCAmelCase : str ) -> Optional[int]: import faiss __snake_case : int = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 ,dtype=np.floataa ) ) __snake_case : Dict = 'index.faiss' __snake_case : Any = f'''mock://{index_name}''' index.save(_UpperCAmelCase ,storage_options=mockfs.storage_options ) __snake_case : Any = FaissIndex.load(_UpperCAmelCase ,storage_options=mockfs.storage_options ) __snake_case : Any = np.zeros(5 ,dtype=np.floataa ) __snake_case : Any = 1 __snake_case , __snake_case : Tuple = index.search(_UpperCAmelCase ) assert scores[0] > 0 assert indices[0] == 1 @require_elasticsearch class snake_case__ ( SCREAMING_SNAKE_CASE_ ): def A_ ( self : List[str] ) -> List[str]: '''simple docstring''' from elasticsearch import Elasticsearch with patch('elasticsearch.Elasticsearch.search' ) as mocked_search, patch( 'elasticsearch.client.IndicesClient.create' ) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk' ) as mocked_bulk: __snake_case : int = Elasticsearch() __snake_case : Dict = {'acknowledged': True} __snake_case : List[Any] = ElasticSearchIndex(es_client=__a ) mocked_bulk.return_value([(True, None)] * 3 ) index.add_documents(['foo', 'bar', 'foobar'] ) # single query __snake_case : Optional[Any] = 'foo' __snake_case : int = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} __snake_case , __snake_case : List[Any] = index.search(__a ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # single query with timeout __snake_case : Dict = 'foo' __snake_case : Dict = {'hits': {'hits': [{'_score': 1, '_id': 0}]}} __snake_case , __snake_case : Optional[Any] = index.search(__a , request_timeout=30 ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # batched queries __snake_case : List[Any] = ['foo', 'bar', 'foobar'] __snake_case : str = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} __snake_case , __snake_case : Any = index.search_batch(__a ) __snake_case : Any = [scores[0] for scores in total_scores] __snake_case : Tuple = [indices[0] for indices in total_indices] self.assertGreater(np.min(__a ) , 0 ) self.assertListEqual([1, 1, 1] , __a ) # batched queries with timeout __snake_case : Tuple = ['foo', 'bar', 'foobar'] __snake_case : List[Any] = {'hits': {'hits': [{'_score': 1, '_id': 1}]}} __snake_case , __snake_case : int = index.search_batch(__a , request_timeout=30 ) __snake_case : Any = [scores[0] for scores in total_scores] __snake_case : Dict = [indices[0] for indices in total_indices] self.assertGreater(np.min(__a ) , 0 ) self.assertListEqual([1, 1, 1] , __a )
0
1
import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class __lowerCAmelCase ( unittest.TestCase ): def snake_case ( self ): """simple docstring""" _lowerCAmelCase = { """task_specific_params""": { """summarization""": {"""length_penalty""": 1.0, """max_length""": 128, """min_length""": 12, """num_beams""": 4}, """summarization_cnn""": {"""length_penalty""": 2.0, """max_length""": 142, """min_length""": 56, """num_beams""": 4}, """summarization_xsum""": {"""length_penalty""": 1.0, """max_length""": 62, """min_length""": 11, """num_beams""": 6}, } } _lowerCAmelCase = { """task_specific_params.summarization.length_penalty""": 1.0, """task_specific_params.summarization.max_length""": 128, """task_specific_params.summarization.min_length""": 12, """task_specific_params.summarization.num_beams""": 4, """task_specific_params.summarization_cnn.length_penalty""": 2.0, """task_specific_params.summarization_cnn.max_length""": 142, """task_specific_params.summarization_cnn.min_length""": 56, """task_specific_params.summarization_cnn.num_beams""": 4, """task_specific_params.summarization_xsum.length_penalty""": 1.0, """task_specific_params.summarization_xsum.max_length""": 62, """task_specific_params.summarization_xsum.min_length""": 11, """task_specific_params.summarization_xsum.num_beams""": 6, } self.assertEqual(flatten_dict(_snake_case ) , _snake_case ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(_snake_case ) , x.transpose() ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(_snake_case , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = torch.tensor(_snake_case ) self.assertTrue(np.allclose(transpose(_snake_case ) , transpose(_snake_case ).numpy() ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) _lowerCAmelCase = torch.tensor(_snake_case ) self.assertTrue(np.allclose(transpose(_snake_case , axes=(1, 2, 0) ) , transpose(_snake_case , axes=(1, 2, 0) ).numpy() ) ) @require_tf def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = tf.constant(_snake_case ) self.assertTrue(np.allclose(transpose(_snake_case ) , transpose(_snake_case ).numpy() ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) _lowerCAmelCase = tf.constant(_snake_case ) self.assertTrue(np.allclose(transpose(_snake_case , axes=(1, 2, 0) ) , transpose(_snake_case , axes=(1, 2, 0) ).numpy() ) ) @require_flax def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = jnp.array(_snake_case ) self.assertTrue(np.allclose(transpose(_snake_case ) , np.asarray(transpose(_snake_case ) ) ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) _lowerCAmelCase = jnp.array(_snake_case ) self.assertTrue(np.allclose(transpose(_snake_case , axes=(1, 2, 0) ) , np.asarray(transpose(_snake_case , axes=(1, 2, 0) ) ) ) ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(_snake_case , (4, 3) ) , np.reshape(_snake_case , (4, 3) ) ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(_snake_case , (12, 5) ) , np.reshape(_snake_case , (12, 5) ) ) ) @require_torch def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = torch.tensor(_snake_case ) self.assertTrue(np.allclose(reshape(_snake_case , (4, 3) ) , reshape(_snake_case , (4, 3) ).numpy() ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) _lowerCAmelCase = torch.tensor(_snake_case ) self.assertTrue(np.allclose(reshape(_snake_case , (12, 5) ) , reshape(_snake_case , (12, 5) ).numpy() ) ) @require_tf def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = tf.constant(_snake_case ) self.assertTrue(np.allclose(reshape(_snake_case , (4, 3) ) , reshape(_snake_case , (4, 3) ).numpy() ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) _lowerCAmelCase = tf.constant(_snake_case ) self.assertTrue(np.allclose(reshape(_snake_case , (12, 5) ) , reshape(_snake_case , (12, 5) ).numpy() ) ) @require_flax def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = jnp.array(_snake_case ) self.assertTrue(np.allclose(reshape(_snake_case , (4, 3) ) , np.asarray(reshape(_snake_case , (4, 3) ) ) ) ) _lowerCAmelCase = np.random.randn(3 , 4 , 5 ) _lowerCAmelCase = jnp.array(_snake_case ) self.assertTrue(np.allclose(reshape(_snake_case , (12, 5) ) , np.asarray(reshape(_snake_case , (12, 5) ) ) ) ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(_snake_case ) , np.squeeze(_snake_case ) ) ) _lowerCAmelCase = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(_snake_case , axis=2 ) , np.squeeze(_snake_case , axis=2 ) ) ) @require_torch def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(1 , 3 , 4 ) _lowerCAmelCase = torch.tensor(_snake_case ) self.assertTrue(np.allclose(squeeze(_snake_case ) , squeeze(_snake_case ).numpy() ) ) _lowerCAmelCase = np.random.randn(1 , 4 , 1 , 5 ) _lowerCAmelCase = torch.tensor(_snake_case ) self.assertTrue(np.allclose(squeeze(_snake_case , axis=2 ) , squeeze(_snake_case , axis=2 ).numpy() ) ) @require_tf def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(1 , 3 , 4 ) _lowerCAmelCase = tf.constant(_snake_case ) self.assertTrue(np.allclose(squeeze(_snake_case ) , squeeze(_snake_case ).numpy() ) ) _lowerCAmelCase = np.random.randn(1 , 4 , 1 , 5 ) _lowerCAmelCase = tf.constant(_snake_case ) self.assertTrue(np.allclose(squeeze(_snake_case , axis=2 ) , squeeze(_snake_case , axis=2 ).numpy() ) ) @require_flax def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(1 , 3 , 4 ) _lowerCAmelCase = jnp.array(_snake_case ) self.assertTrue(np.allclose(squeeze(_snake_case ) , np.asarray(squeeze(_snake_case ) ) ) ) _lowerCAmelCase = np.random.randn(1 , 4 , 1 , 5 ) _lowerCAmelCase = jnp.array(_snake_case ) self.assertTrue(np.allclose(squeeze(_snake_case , axis=2 ) , np.asarray(squeeze(_snake_case , axis=2 ) ) ) ) def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(_snake_case , axis=1 ) , np.expand_dims(_snake_case , axis=1 ) ) ) @require_torch def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = torch.tensor(_snake_case ) self.assertTrue(np.allclose(expand_dims(_snake_case , axis=1 ) , expand_dims(_snake_case , axis=1 ).numpy() ) ) @require_tf def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = tf.constant(_snake_case ) self.assertTrue(np.allclose(expand_dims(_snake_case , axis=1 ) , expand_dims(_snake_case , axis=1 ).numpy() ) ) @require_flax def snake_case ( self ): """simple docstring""" _lowerCAmelCase = np.random.randn(3 , 4 ) _lowerCAmelCase = jnp.array(_snake_case ) self.assertTrue(np.allclose(expand_dims(_snake_case , axis=1 ) , np.asarray(expand_dims(_snake_case , axis=1 ) ) ) )
82
'''simple docstring''' import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() __snake_case =logging.get_logger(__name__) def a_ ( lowerCamelCase : Any ): lowerCAmelCase = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): lowerCAmelCase = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): lowerCAmelCase = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCAmelCase = key[key.find('patch_embed' ) + len('patch_embed' )] lowerCAmelCase = key.replace(f'''patch_embed{idx}''' , f'''patch_embeddings.{int(lowerCamelCase )-1}''' ) if "norm" in key: lowerCAmelCase = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCAmelCase = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] lowerCAmelCase = key.replace(f'''layer_norm{idx}''' , f'''layer_norm.{int(lowerCamelCase )-1}''' ) if "layer_norm1" in key: lowerCAmelCase = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: lowerCAmelCase = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 lowerCAmelCase = key[key.find('block' ) + len('block' )] lowerCAmelCase = key.replace(f'''block{idx}''' , f'''block.{int(lowerCamelCase )-1}''' ) if "attn.q" in key: lowerCAmelCase = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: lowerCAmelCase = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: lowerCAmelCase = key.replace('attn' , 'attention.self' ) if "fc1" in key: lowerCAmelCase = key.replace('fc1' , 'dense1' ) if "fc2" in key: lowerCAmelCase = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: lowerCAmelCase = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: lowerCAmelCase = key.replace('linear_fuse.conv' , 'linear_fuse' ) lowerCAmelCase = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCAmelCase = key[key.find('linear_c' ) + len('linear_c' )] lowerCAmelCase = key.replace(f'''linear_c{idx}''' , f'''linear_c.{int(lowerCamelCase )-1}''' ) if "bot_conv" in key: lowerCAmelCase = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: lowerCAmelCase = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: lowerCAmelCase = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: lowerCAmelCase = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: lowerCAmelCase = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: lowerCAmelCase = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: lowerCAmelCase = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): lowerCAmelCase = key.replace('module.last_layer_depth' , 'head.head' ) lowerCAmelCase = value return new_state_dict def a_ ( lowerCamelCase : List[str] , lowerCamelCase : str ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.weight''' ) lowerCAmelCase = state_dict.pop(f'''glpn.encoder.block.{i}.{j}.attention.self.kv.bias''' ) # next, add keys and values (in that order) to the state dict lowerCAmelCase = kv_weight[ : config.hidden_sizes[i], : ] lowerCAmelCase = kv_bias[: config.hidden_sizes[i]] lowerCAmelCase = kv_weight[ config.hidden_sizes[i] :, : ] lowerCAmelCase = kv_bias[config.hidden_sizes[i] :] def a_ ( ): lowerCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' lowerCAmelCase = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Tuple , lowerCamelCase : Optional[Any]=False , lowerCamelCase : List[str]=None ): lowerCAmelCase = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCAmelCase = GLPNImageProcessor() # prepare image lowerCAmelCase = prepare_img() lowerCAmelCase = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict lowerCAmelCase = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys lowerCAmelCase = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict lowerCAmelCase = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass lowerCAmelCase = model(lowerCamelCase ) lowerCAmelCase = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCAmelCase = torch.tensor( [[4.4_147, 4.0_873, 4.0_673], [3.7_890, 3.2_881, 3.1_525], [3.7_674, 3.5_423, 3.4_913]] ) elif "kitti" in model_name: lowerCAmelCase = torch.tensor( [[3.4_291, 2.7_865, 2.5_151], [3.2_841, 2.7_021, 2.3_502], [3.1_147, 2.4_625, 2.2_481]] ) else: raise ValueError(f'''Unknown model name: {model_name}''' ) lowerCAmelCase = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": __snake_case =argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, 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 folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) __snake_case =parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
4
0
"""simple docstring""" from typing import Optional, Tuple import jax import jax.numpy as jnp from flax import linen as nn from flax.core.frozen_dict import FrozenDict from transformers import CLIPConfig, FlaxPreTrainedModel from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule def __snake_case ( SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[str]=1E-12 ) -> List[str]: '''simple docstring''' _UpperCAmelCase : Optional[int] = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(SCREAMING_SNAKE_CASE__ , axis=1 ) , a_min=SCREAMING_SNAKE_CASE__ ) ).T _UpperCAmelCase : Optional[Any] = jnp.divide(emb_a.T , jnp.clip(jnp.linalg.norm(SCREAMING_SNAKE_CASE__ , axis=1 ) , a_min=SCREAMING_SNAKE_CASE__ ) ).T return jnp.matmul(SCREAMING_SNAKE_CASE__ , norm_emb_a.T ) class UpperCAmelCase_ ( nn.Module ): __SCREAMING_SNAKE_CASE : CLIPConfig __SCREAMING_SNAKE_CASE : jnp.dtype = jnp.floataa def snake_case_ ( self : Dict ): _UpperCAmelCase : Any = FlaxCLIPVisionModule(self.config.vision_config ) _UpperCAmelCase : str = nn.Dense(self.config.projection_dim , use_bias=A , dtype=self.dtype ) _UpperCAmelCase : Optional[int] = self.param("concept_embeds" , jax.nn.initializers.ones , (1_7, self.config.projection_dim) ) _UpperCAmelCase : Tuple = self.param( "special_care_embeds" , jax.nn.initializers.ones , (3, self.config.projection_dim) ) _UpperCAmelCase : List[Any] = self.param("concept_embeds_weights" , jax.nn.initializers.ones , (1_7,) ) _UpperCAmelCase : Optional[int] = self.param("special_care_embeds_weights" , jax.nn.initializers.ones , (3,) ) def __call__( self : Any , A : Tuple ): _UpperCAmelCase : Optional[int] = self.vision_model(A )[1] _UpperCAmelCase : int = self.visual_projection(A ) _UpperCAmelCase : int = jax_cosine_distance(A , self.special_care_embeds ) _UpperCAmelCase : int = jax_cosine_distance(A , self.concept_embeds ) # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign image inputs _UpperCAmelCase : Tuple = 0.0 _UpperCAmelCase : Dict = special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment _UpperCAmelCase : List[str] = jnp.round(A , 3 ) _UpperCAmelCase : str = jnp.any(special_scores > 0 , axis=1 , keepdims=A ) # Use a lower threshold if an image has any special care concept _UpperCAmelCase : List[Any] = is_special_care * 0.01 _UpperCAmelCase : Union[str, Any] = cos_dist - self.concept_embeds_weights[None, :] + special_adjustment _UpperCAmelCase : Union[str, Any] = jnp.round(A , 3 ) _UpperCAmelCase : Union[str, Any] = jnp.any(concept_scores > 0 , axis=1 ) return has_nsfw_concepts class UpperCAmelCase_ ( _UpperCamelCase ): __SCREAMING_SNAKE_CASE : int = CLIPConfig __SCREAMING_SNAKE_CASE : Any = 'clip_input' __SCREAMING_SNAKE_CASE : Union[str, Any] = FlaxStableDiffusionSafetyCheckerModule def __init__( self : Any , A : CLIPConfig , A : Optional[Tuple] = None , A : int = 0 , A : jnp.dtype = jnp.floataa , A : bool = True , **A : List[Any] , ): if input_shape is None: _UpperCAmelCase : List[str] = (1, 2_2_4, 2_2_4, 3) _UpperCAmelCase : List[str] = self.module_class(config=A , dtype=A , **A ) super().__init__(A , A , input_shape=A , seed=A , dtype=A , _do_init=_do_init ) def snake_case_ ( self : Union[str, Any] , A : jax.random.KeyArray , A : Tuple , A : FrozenDict = None ): # init input tensor _UpperCAmelCase : Optional[Any] = jax.random.normal(A , A ) _UpperCAmelCase , _UpperCAmelCase : Optional[int] = jax.random.split(A ) _UpperCAmelCase : Any = {"params": params_rng, "dropout": dropout_rng} _UpperCAmelCase : Union[str, Any] = self.module.init(A , A )["params"] return random_params def __call__( self : Tuple , A : Union[str, Any] , A : dict = None , ): _UpperCAmelCase : Dict = jnp.transpose(A , (0, 2, 3, 1) ) return self.module.apply( {"params": params or self.params} , jnp.array(A , dtype=jnp.floataa ) , rngs={} , )
202
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _lowerCAmelCase : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase : Optional[Any] = ["GPTSw3Tokenizer"] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys _lowerCAmelCase : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
202
1
import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : List[Any] = "▁" lowerCamelCase__ : Union[str, Any] = {"vocab_file": "prophetnet.tokenizer"} lowerCamelCase__ : Dict = { "vocab_file": { "microsoft/xprophetnet-large-wiki100-cased": ( "https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer" ), } } lowerCamelCase__ : Any = { "microsoft/xprophetnet-large-wiki100-cased": {"do_lower_case": False}, } lowerCamelCase__ : Tuple = { "microsoft/xprophetnet-large-wiki100-cased": 512, } def UpperCAmelCase_ ( __UpperCAmelCase : Dict ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = collections.OrderedDict() with open(__lowerCAmelCase , 'r' , encoding='utf-8' ) as reader: SCREAMING_SNAKE_CASE_ = reader.readlines() for index, token in enumerate(__lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = token.rstrip('\n' ) SCREAMING_SNAKE_CASE_ = index return vocab class lowerCamelCase_ ( UpperCamelCase_ ): '''simple docstring''' lowercase_ = VOCAB_FILES_NAMES lowercase_ = PRETRAINED_VOCAB_FILES_MAP lowercase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ = ["""input_ids""", """attention_mask"""] def __init__( self : List[Any] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[Any]="[SEP]" , _lowerCAmelCase : int="[SEP]" , _lowerCAmelCase : List[Any]="[SEP]" , _lowerCAmelCase : Optional[Any]="[UNK]" , _lowerCAmelCase : str="[PAD]" , _lowerCAmelCase : Any="[CLS]" , _lowerCAmelCase : Any="[MASK]" , _lowerCAmelCase : List[Any] = None , **_lowerCAmelCase : str , ): SCREAMING_SNAKE_CASE_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_a , eos_token=_a , sep_token=_a , unk_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , sp_model_kwargs=self.sp_model_kwargs , **_a , ) try: import sentencepiece as spm except ImportError: logger.warning( 'You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece' ' pip install sentencepiece' ) raise SCREAMING_SNAKE_CASE_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_a ) ) SCREAMING_SNAKE_CASE_ = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE_ = {"""[PAD]""": 0, """[CLS]""": 1, """[SEP]""": 2, """[UNK]""": 3, """[MASK]""": 4} for i in range(10 ): SCREAMING_SNAKE_CASE_ = F"[unused{i}]" SCREAMING_SNAKE_CASE_ = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE_ = 12 SCREAMING_SNAKE_CASE_ = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(_a ) def __getstate__( self : Dict ): SCREAMING_SNAKE_CASE_ = self.__dict__.copy() SCREAMING_SNAKE_CASE_ = None return state def __setstate__( self : List[Any] , _lowerCAmelCase : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = d try: import sentencepiece as spm except ImportError: logger.warning( 'You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece' ' pip install sentencepiece' ) raise # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Union[str, Any] = None , _lowerCAmelCase : Any = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_a , token_ids_a=_a , already_has_special_tokens=_a ) if token_ids_a is None: return ([0] * len(_a )) + [1] return ([0] * len(_a )) + [1] + ([0] * len(_a )) + [1] def lowerCAmelCase_ ( self : str , _lowerCAmelCase : Any , _lowerCAmelCase : Optional[int] = None ): SCREAMING_SNAKE_CASE_ = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def lowerCAmelCase_ ( self : List[Any] ): return len(self.sp_model ) + self.fairseq_offset def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = {self.convert_ids_to_tokens(_a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : str ): return self.sp_model.encode(_a , out_type=_a ) def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Tuple ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE_ = self.sp_model.PieceToId(_a ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def lowerCAmelCase_ ( self : Optional[int] , _lowerCAmelCase : List[str] ): if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def lowerCAmelCase_ ( self : Any , _lowerCAmelCase : List[str] ): SCREAMING_SNAKE_CASE_ = """""".join(_a ).replace(_a , ' ' ).strip() return out_string def lowerCAmelCase_ ( self : List[Any] , _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Union[str, Any] = None ): if not os.path.isdir(_a ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE_ = os.path.join( _a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _a ) elif not os.path.isfile(self.vocab_file ): with open(_a , 'wb' ) as fi: SCREAMING_SNAKE_CASE_ = self.sp_model.serialized_model_proto() fi.write(_a ) return (out_vocab_file,) def lowerCAmelCase_ ( self : Union[str, Any] , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int = None ): if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
225
"""simple docstring""" 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 a :Optional[int] = ["text", "image", "audio"] def _lowercase ( __lowerCAmelCase ) -> List[Any]: SCREAMING_SNAKE_CASE__ : List[Any] = [] 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(__lowerCAmelCase , __lowerCAmelCase ): inputs.append(create_inputs(__lowerCAmelCase ) ) else: raise ValueError(F'''Invalid type requested: {input_type}''' ) return inputs def _lowercase ( __lowerCAmelCase ) -> List[str]: SCREAMING_SNAKE_CASE__ : Tuple = [] for output in outputs: if isinstance(__lowerCAmelCase , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(__lowerCAmelCase , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(__lowerCAmelCase , (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 _a ( self ) -> str: """simple docstring""" self.assertTrue(hasattr(self.tool , """inputs""" ) ) self.assertTrue(hasattr(self.tool , """outputs""" ) ) SCREAMING_SNAKE_CASE__ : List[Any] = self.tool.inputs for _input in inputs: if isinstance(_input , _a ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) SCREAMING_SNAKE_CASE__ : Dict = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def _a ( self ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = create_inputs(self.tool.inputs ) SCREAMING_SNAKE_CASE__ : Optional[Any] = self.tool(*_a ) # There is a single output if len(self.tool.outputs ) == 1: SCREAMING_SNAKE_CASE__ : List[Any] = [outputs] self.assertListEqual(output_types(_a ) , self.tool.outputs ) def _a ( self ) -> List[Any]: """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 _a ( self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = create_inputs(self.tool.inputs ) SCREAMING_SNAKE_CASE__ : Dict = self.tool(*_a ) if not isinstance(_a , _a ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = [outputs] self.assertEqual(len(_a ) , len(self.tool.outputs ) ) for output, output_type in zip(_a , self.tool.outputs ): SCREAMING_SNAKE_CASE__ : Optional[Any] = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(_a , _a ) ) def _a ( self ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = create_inputs(self.tool.inputs ) SCREAMING_SNAKE_CASE__ : List[Any] = [] for _input, input_type in zip(_a , self.tool.inputs ): if isinstance(_a , _a ): _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 SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.tool(*_a ) if not isinstance(_a , _a ): SCREAMING_SNAKE_CASE__ : Optional[Any] = [outputs] self.assertEqual(len(_a ) , len(self.tool.outputs ) )
132
0
'''simple docstring''' import math from collections.abc import Iterator from itertools import takewhile def __a ( UpperCAmelCase ) ->List[Any]: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(__a ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def __a ( ) ->List[Any]: """simple docstring""" A = 2 while True: if is_prime(__a ): yield num num += 1 def __a ( UpperCAmelCase = 2000000 ) ->Dict: """simple docstring""" return sum(takewhile(lambda UpperCAmelCase : x < n , prime_generator() ) ) if __name__ == "__main__": print(f"{solution() = }")
350
'''simple docstring''' import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def __a ( ) ->str: """simple docstring""" A = argparse.ArgumentParser() parser.add_argument("""--model_ckpt""" , type=UpperCAmelCase , default="""microsoft/unixcoder-base-nine""" ) parser.add_argument("""--num_epochs""" , type=UpperCAmelCase , default=5 ) parser.add_argument("""--batch_size""" , type=UpperCAmelCase , default=6 ) parser.add_argument("""--gradient_accumulation_steps""" , type=UpperCAmelCase , default=1 ) parser.add_argument("""--freeze""" , type=UpperCAmelCase , default=UpperCAmelCase ) parser.add_argument("""--learning_rate""" , type=UpperCAmelCase , default=5E-4 ) parser.add_argument("""--seed""" , type=UpperCAmelCase , default=0 ) parser.add_argument("""--lr_scheduler_type""" , type=UpperCAmelCase , default="""cosine""" ) parser.add_argument("""--num_warmup_steps""" , type=UpperCAmelCase , default=10 ) parser.add_argument("""--weight_decay""" , type=UpperCAmelCase , default=0.01 ) parser.add_argument("""--output_dir""" , type=UpperCAmelCase , default="""./results""" ) return parser.parse_args() _lowerCamelCase : Optional[Any] = load('accuracy') def __a ( UpperCAmelCase ) ->Any: """simple docstring""" A , A = eval_pred A = np.argmax(UpperCAmelCase , axis=1 ) return metric.compute(predictions=UpperCAmelCase , references=UpperCAmelCase ) class __UpperCAmelCase ( A__ ): '''simple docstring''' def __init__(self : Union[str, Any] , _lowerCAmelCase : Any ): super().__init__() A = trainer def A (self : Dict , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[str] , _lowerCAmelCase : Any , **_lowerCAmelCase : List[Any] ): if control.should_evaluate: A = deepcopy(_lowerCAmelCase ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix="""train""" ) return control_copy def __a ( ) ->Optional[int]: """simple docstring""" A = get_args() set_seed(args.seed ) A = load_dataset("""codeparrot/codecomplex""" , split="""train""" ) A = dataset.train_test_split(test_size=0.2 ) A = train_test["""test"""].train_test_split(test_size=0.5 ) A = DatasetDict( { """train""": train_test["""train"""], """test""": test_validation["""train"""], """valid""": test_validation["""test"""], } ) print("""Loading tokenizer and model""" ) A = AutoTokenizer.from_pretrained(args.model_ckpt ) A = tokenizer.eos_token A = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) A = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): A = False A = ClassLabel(num_classes=7 , names=list(set(train_test_validation["""train"""]["""complexity"""] ) ) ) def tokenize(UpperCAmelCase ): A = tokenizer(example["""src"""] , truncation=UpperCAmelCase , max_length=1024 ) A = labels.straint(example["""complexity"""] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } A = train_test_validation.map( UpperCAmelCase , batched=UpperCAmelCase , remove_columns=train_test_validation["""train"""].column_names , ) A = DataCollatorWithPadding(tokenizer=UpperCAmelCase ) A = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy="""epoch""" , save_strategy="""epoch""" , logging_strategy="""epoch""" , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.01 , metric_for_best_model="""accuracy""" , run_name="""complexity-java""" , report_to="""wandb""" , ) A = Trainer( model=UpperCAmelCase , args=UpperCAmelCase , train_dataset=tokenized_datasets["""train"""] , eval_dataset=tokenized_datasets["""valid"""] , tokenizer=UpperCAmelCase , data_collator=UpperCAmelCase , compute_metrics=UpperCAmelCase , ) print("""Training...""" ) trainer.add_callback(CustomCallback(UpperCAmelCase ) ) trainer.train() if __name__ == "__main__": main()
337
0
'''simple docstring''' def __lowercase ( __lowercase ) -> bool: '''simple docstring''' return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
79
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase_ = { '''configuration_jukebox''': [ '''JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''JukeboxConfig''', '''JukeboxPriorConfig''', '''JukeboxVQVAEConfig''', ], '''tokenization_jukebox''': ['''JukeboxTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ '''JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST''', '''JukeboxModel''', '''JukeboxPreTrainedModel''', '''JukeboxVQVAE''', '''JukeboxPrior''', ] if TYPE_CHECKING: from .configuration_jukebox import ( JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP, JukeboxConfig, JukeboxPriorConfig, JukeboxVQVAEConfig, ) from .tokenization_jukebox import JukeboxTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_jukebox import ( JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST, JukeboxModel, JukeboxPreTrainedModel, JukeboxPrior, JukeboxVQVAE, ) else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
79
1
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def __lowerCamelCase ( __a :Optional[int] , __a :str ) -> Optional[Any]: """simple docstring""" A__ = k_size // 2 A__ = mgrid[0 - center : k_size - center, 0 - center : k_size - center] A__ = 1 / (2 * pi * sigma) * exp(-(square(_a ) + square(_a )) / (2 * square(_a )) ) return g def __lowerCamelCase ( __a :Optional[int] , __a :Dict , __a :List[str] ) -> Any: """simple docstring""" A__ = image.shape[0], image.shape[1] # dst image height and width A__ = height - k_size + 1 A__ = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows A__ = zeros((dst_height * dst_width, k_size * k_size) ) A__ = 0 for i, j in product(range(_a ) , range(_a ) ): A__ = ravel(image[i : i + k_size, j : j + k_size] ) A__ = window row += 1 # turn the kernel into shape(k*k, 1) A__ = gen_gaussian_kernel(_a , _a ) A__ = ravel(_a ) # reshape and get the dst image A__ = dot(_a , _a ).reshape(_a , _a ).astype(_a ) return dst if __name__ == "__main__": # read original image A : Tuple = imread(R'''../image_data/lena.jpg''') # turn image in gray scale value A : Union[str, Any] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size A : Any = gaussian_filter(gray, 3, sigma=1) A : List[str] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow('''gaussian filter with 3x3 mask''', gaussianaxa) imshow('''gaussian filter with 5x5 mask''', gaussianaxa) waitKey()
365
def __lowerCamelCase ( __a :float , __a :list[float] ) -> float: """simple docstring""" if discount_rate < 0: raise ValueError("""Discount rate cannot be negative""" ) if not cash_flows: raise ValueError("""Cash flows list cannot be empty""" ) A__ = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(__a ) ) return round(__a , ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
276
0
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( ImageTextPipelineOutput, UniDiffuserPipeline, ) else: from .modeling_text_decoder import UniDiffuserTextDecoder from .modeling_uvit import UniDiffuserModel, UTransformeraDModel from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline
0
import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import numpy as np import pytest from datasets.arrow_dataset import Dataset from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex from .utils import require_elasticsearch, require_faiss UpperCAmelCase__ = pytest.mark.integration @require_faiss class lowercase_ ( lowercase ): '''simple docstring''' def __lowerCAmelCase ( self : Tuple ) ->Any: """simple docstring""" a = Dataset.from_dict({'''filename''': ['''my_name-train''' + '''_''' + str(__UpperCAmelCase ) for x in np.arange(30 ).tolist()]} ) return dset def __lowerCAmelCase ( self : Tuple ) ->Any: """simple docstring""" import faiss a = self._create_dummy_dataset() a = dset.map( lambda __UpperCAmelCase , __UpperCAmelCase : {"vecs": i * np.ones(5 , dtype=np.floataa )} , with_indices=__UpperCAmelCase , keep_in_memory=__UpperCAmelCase ) a = dset.add_faiss_index('''vecs''' , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT ) a , a = dset.get_nearest_examples('''vecs''' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['''filename'''][0] , '''my_name-train_29''' ) dset.drop_index('''vecs''' ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" import faiss a = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='''vecs''' , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT , ) a , a = dset.get_nearest_examples('''vecs''' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['''filename'''][0] , '''my_name-train_29''' ) def __lowerCAmelCase ( self : Any ) ->Union[str, Any]: """simple docstring""" import faiss a = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='''vecs''' , metric_type=faiss.METRIC_INNER_PRODUCT , ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=__UpperCAmelCase ) as tmp_file: dset.save_faiss_index('''vecs''' , tmp_file.name ) dset.load_faiss_index('''vecs2''' , tmp_file.name ) os.unlink(tmp_file.name ) a , a = dset.get_nearest_examples('''vecs2''' , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples['''filename'''][0] , '''my_name-train_29''' ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" a = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name='''vecs''' ) dset.drop_index('''vecs''' ) self.assertRaises(__UpperCAmelCase , partial(dset.get_nearest_examples , '''vecs2''' , np.ones(5 , dtype=np.floataa ) ) ) def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" from elasticsearch import Elasticsearch a = self._create_dummy_dataset() with patch('''elasticsearch.Elasticsearch.search''' ) as mocked_search, patch( '''elasticsearch.client.IndicesClient.create''' ) as mocked_index_create, patch('''elasticsearch.helpers.streaming_bulk''' ) as mocked_bulk: a = {'''acknowledged''': True} mocked_bulk.return_value([(True, None)] * 30 ) a = {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 29}]}} a = Elasticsearch() dset.add_elasticsearch_index('''filename''' , es_client=__UpperCAmelCase ) a , a = dset.get_nearest_examples('''filename''' , '''my_name-train_29''' ) self.assertEqual(examples['''filename'''][0] , '''my_name-train_29''' ) @require_faiss class lowercase_ ( lowercase ): '''simple docstring''' def __lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" import faiss a = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) # add vectors index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsNotNone(index.faiss_index ) self.assertEqual(index.faiss_index.ntotal , 5 ) index.add_vectors(np.zeros((5, 5) , dtype=np.floataa ) ) self.assertEqual(index.faiss_index.ntotal , 10 ) # single query a = np.zeros(5 , dtype=np.floataa ) a = 1 a , a = index.search(__UpperCAmelCase ) self.assertRaises(__UpperCAmelCase , index.search , query.reshape(-1 , 1 ) ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) # batched queries a = np.eye(5 , dtype=np.floataa )[::-1] a , a = index.search_batch(__UpperCAmelCase ) self.assertRaises(__UpperCAmelCase , index.search_batch , queries[0] ) a = [scores[0] for scores in total_scores] a = [indices[0] for indices in total_indices] self.assertGreater(np.min(__UpperCAmelCase ) , 0 ) self.assertListEqual([4, 3, 2, 1, 0] , __UpperCAmelCase ) def __lowerCAmelCase ( self : Any ) ->List[Any]: """simple docstring""" import faiss a = FaissIndex(string_factory='''Flat''' ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) a = FaissIndex(string_factory='''LSH''' ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexLSH ) with self.assertRaises(__UpperCAmelCase ): a = FaissIndex(string_factory='''Flat''' , custom_index=faiss.IndexFlat(5 ) ) def __lowerCAmelCase ( self : int ) ->Optional[Any]: """simple docstring""" import faiss a = faiss.IndexFlat(5 ) a = FaissIndex(custom_index=__UpperCAmelCase ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" import faiss a = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=__UpperCAmelCase ) as tmp_file: index.save(tmp_file.name ) a = FaissIndex.load(tmp_file.name ) os.unlink(tmp_file.name ) a = np.zeros(5 , dtype=np.floataa ) a = 1 a , a = index.search(__UpperCAmelCase ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) @require_faiss def _a ( a :Dict ) -> Any: import faiss a = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) a = '''index.faiss''' a = F"""mock://{index_name}""" index.save(a , storage_options=mockfs.storage_options ) a = FaissIndex.load(a , storage_options=mockfs.storage_options ) a = np.zeros(5 , dtype=np.floataa ) a = 1 a , a = index.search(a ) assert scores[0] > 0 assert indices[0] == 1 @require_elasticsearch class lowercase_ ( lowercase ): '''simple docstring''' def __lowerCAmelCase ( self : int ) ->List[Any]: """simple docstring""" from elasticsearch import Elasticsearch with patch('''elasticsearch.Elasticsearch.search''' ) as mocked_search, patch( '''elasticsearch.client.IndicesClient.create''' ) as mocked_index_create, patch('''elasticsearch.helpers.streaming_bulk''' ) as mocked_bulk: a = Elasticsearch() a = {'''acknowledged''': True} a = ElasticSearchIndex(es_client=__UpperCAmelCase ) mocked_bulk.return_value([(True, None)] * 3 ) index.add_documents(['''foo''', '''bar''', '''foobar'''] ) # single query a = '''foo''' a = {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 0}]}} a , a = index.search(__UpperCAmelCase ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # single query with timeout a = '''foo''' a = {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 0}]}} a , a = index.search(__UpperCAmelCase , request_timeout=30 ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # batched queries a = ['''foo''', '''bar''', '''foobar'''] a = {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 1}]}} a , a = index.search_batch(__UpperCAmelCase ) a = [scores[0] for scores in total_scores] a = [indices[0] for indices in total_indices] self.assertGreater(np.min(__UpperCAmelCase ) , 0 ) self.assertListEqual([1, 1, 1] , __UpperCAmelCase ) # batched queries with timeout a = ['''foo''', '''bar''', '''foobar'''] a = {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 1}]}} a , a = index.search_batch(__UpperCAmelCase , request_timeout=30 ) a = [scores[0] for scores in total_scores] a = [indices[0] for indices in total_indices] self.assertGreater(np.min(__UpperCAmelCase ) , 0 ) self.assertListEqual([1, 1, 1] , __UpperCAmelCase )
0
1
"""simple docstring""" import logging import os import sys from dataclasses import dataclass, field from importlib import import_module from typing import Dict, List, Optional, Tuple import numpy as np from seqeval.metrics import accuracy_score, fa_score, precision_score, recall_score from torch import nn from utils_ner import Split, TokenClassificationDataset, TokenClassificationTask import transformers from transformers import ( AutoConfig, AutoModelForTokenClassification, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process lowercase__ : Union[str, Any] = logging.getLogger(__name__) @dataclass class UpperCamelCase__ : """simple docstring""" _SCREAMING_SNAKE_CASE = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) _SCREAMING_SNAKE_CASE = field( default=lowercase_, metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) _SCREAMING_SNAKE_CASE = field( default="""NER""", metadata={"""help""": """Task type to fine tune in training (e.g. NER, POS, etc)"""} ) _SCREAMING_SNAKE_CASE = field( default=lowercase_, metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) _SCREAMING_SNAKE_CASE = field(default=lowercase_, metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. _SCREAMING_SNAKE_CASE = field( default=lowercase_, metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}, ) @dataclass class UpperCamelCase__ : """simple docstring""" _SCREAMING_SNAKE_CASE = field( metadata={"""help""": """The input data dir. Should contain the .txt files for a CoNLL-2003-formatted task."""} ) _SCREAMING_SNAKE_CASE = field( default=lowercase_, metadata={"""help""": """Path to a file containing all labels. If not specified, CoNLL-2003 labels are used."""}, ) _SCREAMING_SNAKE_CASE = field( default=128, metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) }, ) _SCREAMING_SNAKE_CASE = field( default=lowercase_, metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) def UpperCamelCase_ ( ) -> Any: """simple docstring""" lowerCAmelCase_ : str = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. lowerCAmelCase_ : List[Any] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: lowerCAmelCase_ : Optional[Any] = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. Use" ' --overwrite_output_dir to overcome.' ) lowerCAmelCase_ : Any = import_module('tasks' ) try: lowerCAmelCase_ : Union[str, Any] = getattr(lowerCAmelCase__ , model_args.task_type ) lowerCAmelCase_ : TokenClassificationTask = token_classification_task_clazz() except AttributeError: raise ValueError( f"Task {model_args.task_type} needs to be defined as a TokenClassificationTask subclass in {module}. " f"Available tasks classes are: {TokenClassificationTask.__subclasses__()}" ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , lowerCAmelCase__ ) # Set seed set_seed(training_args.seed ) # Prepare CONLL-2003 task lowerCAmelCase_ : List[Any] = token_classification_task.get_labels(data_args.labels ) lowerCAmelCase_ : Dict[int, str] = dict(enumerate(lowerCAmelCase__ ) ) lowerCAmelCase_ : Tuple = len(lowerCAmelCase__ ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. lowerCAmelCase_ : int = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=lowerCAmelCase__ , idalabel=lowerCAmelCase__ , labelaid={label: i for i, label in enumerate(lowerCAmelCase__ )} , cache_dir=model_args.cache_dir , ) lowerCAmelCase_ : str = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast , ) lowerCAmelCase_ : Any = AutoModelForTokenClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=lowerCAmelCase__ , cache_dir=model_args.cache_dir , ) # Get datasets lowerCAmelCase_ : Optional[int] = ( TokenClassificationDataset( token_classification_task=lowerCAmelCase__ , data_dir=data_args.data_dir , tokenizer=lowerCAmelCase__ , labels=lowerCAmelCase__ , model_type=config.model_type , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) lowerCAmelCase_ : List[Any] = ( TokenClassificationDataset( token_classification_task=lowerCAmelCase__ , data_dir=data_args.data_dir , tokenizer=lowerCAmelCase__ , labels=lowerCAmelCase__ , model_type=config.model_type , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def align_predictions(lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : np.ndarray ) -> Tuple[List[int], List[int]]: lowerCAmelCase_ : List[str] = np.argmax(lowerCAmelCase__ , axis=2 ) lowerCAmelCase_ : Union[str, Any] = preds.shape lowerCAmelCase_ : Optional[Any] = [[] for _ in range(lowerCAmelCase__ )] lowerCAmelCase_ : List[str] = [[] for _ in range(lowerCAmelCase__ )] for i in range(lowerCAmelCase__ ): for j in range(lowerCAmelCase__ ): if label_ids[i, j] != nn.CrossEntropyLoss().ignore_index: out_label_list[i].append(label_map[label_ids[i][j]] ) preds_list[i].append(label_map[preds[i][j]] ) return preds_list, out_label_list def compute_metrics(lowerCAmelCase__ : EvalPrediction ) -> Dict: lowerCAmelCase_ : Optional[Any] = align_predictions(p.predictions , p.label_ids ) return { "accuracy_score": accuracy_score(lowerCAmelCase__ , lowerCAmelCase__ ), "precision": precision_score(lowerCAmelCase__ , lowerCAmelCase__ ), "recall": recall_score(lowerCAmelCase__ , lowerCAmelCase__ ), "f1": fa_score(lowerCAmelCase__ , lowerCAmelCase__ ), } # Data collator lowerCAmelCase_ : str = DataCollatorWithPadding(lowerCAmelCase__ , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer lowerCAmelCase_ : Dict = Trainer( model=lowerCAmelCase__ , args=lowerCAmelCase__ , train_dataset=lowerCAmelCase__ , eval_dataset=lowerCAmelCase__ , compute_metrics=lowerCAmelCase__ , data_collator=lowerCAmelCase__ , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_process_zero(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation lowerCAmelCase_ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) lowerCAmelCase_ : Union[str, Any] = trainer.evaluate() lowerCAmelCase_ : Tuple = os.path.join(training_args.output_dir , 'eval_results.txt' ) if trainer.is_world_process_zero(): with open(lowerCAmelCase__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' , lowerCAmelCase__ , lowerCAmelCase__ ) writer.write('%s = %s\n' % (key, value) ) results.update(lowerCAmelCase__ ) # Predict if training_args.do_predict: lowerCAmelCase_ : List[str] = TokenClassificationDataset( token_classification_task=lowerCAmelCase__ , data_dir=data_args.data_dir , tokenizer=lowerCAmelCase__ , labels=lowerCAmelCase__ , model_type=config.model_type , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.test , ) lowerCAmelCase_ : Optional[int] = trainer.predict(lowerCAmelCase__ ) lowerCAmelCase_ : Tuple = align_predictions(lowerCAmelCase__ , lowerCAmelCase__ ) lowerCAmelCase_ : List[str] = os.path.join(training_args.output_dir , 'test_results.txt' ) if trainer.is_world_process_zero(): with open(lowerCAmelCase__ , 'w' ) as writer: for key, value in metrics.items(): logger.info(' %s = %s' , lowerCAmelCase__ , lowerCAmelCase__ ) writer.write('%s = %s\n' % (key, value) ) # Save predictions lowerCAmelCase_ : int = os.path.join(training_args.output_dir , 'test_predictions.txt' ) if trainer.is_world_process_zero(): with open(lowerCAmelCase__ , 'w' ) as writer: with open(os.path.join(data_args.data_dir , 'test.txt' ) , 'r' ) as f: token_classification_task.write_predictions_to_file(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return results def UpperCamelCase_ ( lowerCAmelCase__ : Any ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
369
"""simple docstring""" import unittest from transformers import AlbertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, ) from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCamelCase__ : """simple docstring""" def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Dict=1_3 , SCREAMING_SNAKE_CASE_ : List[Any]=7 , SCREAMING_SNAKE_CASE_ : Dict=True , SCREAMING_SNAKE_CASE_ : Optional[Any]=True , SCREAMING_SNAKE_CASE_ : Optional[Any]=True , SCREAMING_SNAKE_CASE_ : str=True , SCREAMING_SNAKE_CASE_ : List[str]=9_9 , SCREAMING_SNAKE_CASE_ : int=1_6 , SCREAMING_SNAKE_CASE_ : List[str]=3_6 , SCREAMING_SNAKE_CASE_ : List[Any]=6 , SCREAMING_SNAKE_CASE_ : Tuple=6 , SCREAMING_SNAKE_CASE_ : List[Any]=6 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=3_7 , SCREAMING_SNAKE_CASE_ : Tuple="gelu" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Optional[Any]=5_1_2 , SCREAMING_SNAKE_CASE_ : List[str]=1_6 , SCREAMING_SNAKE_CASE_ : List[str]=2 , SCREAMING_SNAKE_CASE_ : List[Any]=0.02 , SCREAMING_SNAKE_CASE_ : Dict=3 , SCREAMING_SNAKE_CASE_ : int=4 , SCREAMING_SNAKE_CASE_ : Tuple=None , ): lowerCAmelCase_ : Any = parent lowerCAmelCase_ : Optional[int] = batch_size lowerCAmelCase_ : Dict = seq_length lowerCAmelCase_ : Tuple = is_training lowerCAmelCase_ : str = use_input_mask lowerCAmelCase_ : Union[str, Any] = use_token_type_ids lowerCAmelCase_ : Tuple = use_labels lowerCAmelCase_ : Optional[int] = vocab_size lowerCAmelCase_ : Any = embedding_size lowerCAmelCase_ : Optional[Any] = hidden_size lowerCAmelCase_ : str = num_hidden_layers lowerCAmelCase_ : Optional[Any] = num_hidden_groups lowerCAmelCase_ : Dict = num_attention_heads lowerCAmelCase_ : Optional[Any] = intermediate_size lowerCAmelCase_ : Any = hidden_act lowerCAmelCase_ : Union[str, Any] = hidden_dropout_prob lowerCAmelCase_ : int = attention_probs_dropout_prob lowerCAmelCase_ : int = max_position_embeddings lowerCAmelCase_ : List[Any] = type_vocab_size lowerCAmelCase_ : Any = type_sequence_label_size lowerCAmelCase_ : Optional[int] = initializer_range lowerCAmelCase_ : Tuple = num_labels lowerCAmelCase_ : Dict = num_choices lowerCAmelCase_ : Tuple = scope def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): lowerCAmelCase_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase_ : str = None if self.use_input_mask: lowerCAmelCase_ : int = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase_ : List[Any] = None if self.use_token_type_ids: lowerCAmelCase_ : str = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCAmelCase_ : List[str] = None lowerCAmelCase_ : Union[str, Any] = None lowerCAmelCase_ : str = None if self.use_labels: lowerCAmelCase_ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase_ : Dict = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase_ : List[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE__ ( self : Dict ): return AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , num_hidden_groups=self.num_hidden_groups , ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : str ): lowerCAmelCase_ : Union[str, Any] = AlbertModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : List[str] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : List[Any] = model(SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : int = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : int , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple ): lowerCAmelCase_ : Optional[Any] = AlbertForPreTraining(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : Optional[Any] = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , sentence_order_label=SCREAMING_SNAKE_CASE_ , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.sop_logits.shape , (self.batch_size, config.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Dict ): lowerCAmelCase_ : str = AlbertForMaskedLM(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : str = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] ): lowerCAmelCase_ : List[str] = AlbertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : Any = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , start_positions=SCREAMING_SNAKE_CASE_ , end_positions=SCREAMING_SNAKE_CASE_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def SCREAMING_SNAKE_CASE__ ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict ): lowerCAmelCase_ : Union[str, Any] = self.num_labels lowerCAmelCase_ : Union[str, Any] = AlbertForSequenceClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : Union[str, Any] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : Any , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): lowerCAmelCase_ : List[str] = self.num_labels lowerCAmelCase_ : List[Any] = AlbertForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : str = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : List[str] ): lowerCAmelCase_ : Optional[Any] = self.num_choices lowerCAmelCase_ : int = AlbertForMultipleChoice(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : Optional[int] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase_ : List[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase_ : List[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCAmelCase_ : List[Any] = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , token_type_ids=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): lowerCAmelCase_ : Optional[int] = self.prepare_config_and_inputs() ( ( lowerCAmelCase_ ) ,( lowerCAmelCase_ ) ,( lowerCAmelCase_ ) ,( lowerCAmelCase_ ) ,( lowerCAmelCase_ ) ,( lowerCAmelCase_ ) ,( lowerCAmelCase_ ) , ) : Optional[int] = config_and_inputs lowerCAmelCase_ : Dict = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class UpperCamelCase__ ( lowercase_, lowercase_, unittest.TestCase ): """simple docstring""" _SCREAMING_SNAKE_CASE = ( ( AlbertModel, AlbertForPreTraining, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertForQuestionAnswering, ) if is_torch_available() else () ) _SCREAMING_SNAKE_CASE = ( { """feature-extraction""": AlbertModel, """fill-mask""": AlbertForMaskedLM, """question-answering""": AlbertForQuestionAnswering, """text-classification""": AlbertForSequenceClassification, """token-classification""": AlbertForTokenClassification, """zero-shot""": AlbertForSequenceClassification, } if is_torch_available() else {} ) _SCREAMING_SNAKE_CASE = True def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : str=False ): lowerCAmelCase_ : List[str] = super()._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_labels=SCREAMING_SNAKE_CASE_ ) if return_labels: if model_class in get_values(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase_ : List[str] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ ) return inputs_dict def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): lowerCAmelCase_ : str = AlbertModelTester(self ) lowerCAmelCase_ : Optional[int] = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , hidden_size=3_7 ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self : Tuple ): lowerCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): lowerCAmelCase_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): lowerCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): lowerCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): lowerCAmelCase_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : str ): lowerCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): lowerCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCAmelCase_ : int = type self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) @slow def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase_ : Optional[Any] = AlbertModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @require_torch class UpperCamelCase__ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE__ ( self : List[str] ): lowerCAmelCase_ : Any = AlbertModel.from_pretrained('albert-base-v2' ) lowerCAmelCase_ : Tuple = torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) lowerCAmelCase_ : Tuple = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): lowerCAmelCase_ : List[Any] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ )[0] lowerCAmelCase_ : str = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : int = torch.tensor( [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) )
289
0
"""simple docstring""" # coding=utf-8 # Copyright 2020 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import sys import transformers _A : List[str] = """3""" print("""Python version:""", sys.version) print("""transformers version:""", transformers.__version__) try: import torch print("""Torch version:""", torch.__version__) print("""Cuda available:""", torch.cuda.is_available()) print("""Cuda version:""", torch.version.cuda) print("""CuDNN version:""", torch.backends.cudnn.version()) print("""Number of GPUs available:""", torch.cuda.device_count()) print("""NCCL version:""", torch.cuda.nccl.version()) except ImportError: print("""Torch version:""", None) try: import deepspeed print("""DeepSpeed version:""", deepspeed.__version__) except ImportError: print("""DeepSpeed version:""", None) try: import tensorflow as tf print("""TensorFlow version:""", tf.__version__) print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU"""))) print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU"""))) except ImportError: print("""TensorFlow version:""", None)
202
"""simple docstring""" def __magic_name__ ( __snake_case : int , __snake_case : int , __snake_case : int ) -> float: lowercase : List[Any] = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def __magic_name__ ( ) -> int: print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
202
1
'''simple docstring''' import os 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_pegasus import PegasusTokenizer else: lowerCAmelCase_ = None lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = "▁" lowerCAmelCase_ = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} lowerCAmelCase_ = { "vocab_file": {"google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"}, "tokenizer_file": { "google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json" }, } lowerCAmelCase_ = { "google/pegasus-xsum": 5_1_2, } class lowerCamelCase ( snake_case_ ): snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = PegasusTokenizer snake_case_ = ['''input_ids''', '''attention_mask'''] def __init__( self, lowercase_=None, lowercase_=None, lowercase_="<pad>", lowercase_="</s>", lowercase_="<unk>", lowercase_="<mask_2>", lowercase_="<mask_1>", lowercase_=None, lowercase_=103, **lowercase_, ) -> Optional[int]: snake_case = offset if additional_special_tokens is not None: if not isinstance(lowercase_, lowercase_ ): raise TypeError( F'''additional_special_tokens should be of type {type(lowercase_ )}, but is''' F''' {type(lowercase_ )}''' ) snake_case = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ F'''<unk_{i}>''' for i in range(len(lowercase_ ), self.offset - 1 ) ] if len(set(lowercase_ ) ) != len(lowercase_ ): raise ValueError( 'Please make sure that the provided additional_special_tokens do not contain an incorrectly' F''' shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.''' ) snake_case = additional_special_tokens_extended else: snake_case = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [F'''<unk_{i}>''' for i in range(2, self.offset )] super().__init__( lowercase_, tokenizer_file=lowercase_, pad_token=lowercase_, eos_token=lowercase_, unk_token=lowercase_, mask_token=lowercase_, mask_token_sent=lowercase_, offset=lowercase_, additional_special_tokens=lowercase_, **lowercase_, ) snake_case = vocab_file snake_case = False if not self.vocab_file else True def _lowerCamelCase ( self, lowercase_ ) -> Optional[Any]: snake_case = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( 'There should be 3 special tokens: mask_token, pad_token, and eos_token +' F''' {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}''' ) return [1 if x in all_special_ids else 0 for x in seq] def _lowerCamelCase ( self, lowercase_, lowercase_ = None, lowercase_ = False ) -> List[int]: if already_has_special_tokens: return self._special_token_mask(lowercase_ ) elif token_ids_a is None: return self._special_token_mask(lowercase_ ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def _lowerCamelCase ( self, lowercase_, lowercase_=None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def _lowerCamelCase ( self, lowercase_, lowercase_ = None ) -> Tuple[str]: 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 snake_case = 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_ ) return (out_vocab_file,)
356
'''simple docstring''' from __future__ import annotations def __magic_name__ ( A ) -> list: if len(A ) == 0: return [] snake_case , snake_case = min(A ), max(A ) snake_case = int(max_value - min_value ) + 1 snake_case = [[] for _ in range(A )] for i in my_list: buckets[int(i - min_value )].append(A ) return [v for bucket in buckets for v in sorted(A )] if __name__ == "__main__": from doctest import testmod testmod() assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bucket_sort([0, 1, -1_0, 1_5, 2, -2]) == [-1_0, -2, 0, 1, 2, 1_5]
332
0
from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class UpperCamelCase_ : '''simple docstring''' UpperCAmelCase__ = 42 UpperCAmelCase__ = 42 class UpperCamelCase_ : '''simple docstring''' def __init__( self : str , UpperCAmelCase__ : int) ->str: '''simple docstring''' A__ = [[] for _ in range(UpperCAmelCase__)] A__ = size def __getitem__( self : List[Any] , UpperCAmelCase__ : int) ->Iterator[Edge]: '''simple docstring''' return iter(self._graph[vertex]) @property def SCREAMING_SNAKE_CASE ( self : str) ->Union[str, Any]: '''simple docstring''' return self._size def SCREAMING_SNAKE_CASE ( self : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int) ->Optional[Any]: '''simple docstring''' if weight not in (0, 1): raise ValueError('''Edge weight must be either 0 or 1.''') if to_vertex < 0 or to_vertex >= self.size: raise ValueError('''Vertex indexes must be in [0; size).''') self._graph[from_vertex].append(Edge(UpperCAmelCase__ , UpperCAmelCase__)) def SCREAMING_SNAKE_CASE ( self : List[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int) ->int | None: '''simple docstring''' A__ = deque([start_vertex]) A__ = [None] * self.size A__ = 0 while queue: A__ = queue.popleft() A__ = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: A__ = current_distance + edge.weight A__ = distances[edge.destination_vertex] if ( isinstance(UpperCAmelCase__ , UpperCAmelCase__) and new_distance >= dest_vertex_distance ): continue A__ = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex) else: queue.append(edge.destination_vertex) if distances[finish_vertex] is None: raise ValueError('''No path from start_vertex to finish_vertex.''') return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
14
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __a = { '''configuration_ctrl''': ['''CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CTRLConfig'''], '''tokenization_ctrl''': ['''CTRLTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ '''CTRL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CTRLForSequenceClassification''', '''CTRLLMHeadModel''', '''CTRLModel''', '''CTRLPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ '''TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFCTRLForSequenceClassification''', '''TFCTRLLMHeadModel''', '''TFCTRLModel''', '''TFCTRLPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
337
0
'''simple docstring''' import os # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_doctest_list.py snake_case__ : Tuple = '''.''' if __name__ == "__main__": snake_case__ : Dict = os.path.join(REPO_PATH, '''utils/documentation_tests.txt''') snake_case__ : List[str] = [] snake_case__ : Optional[Any] = [] with open(doctest_file_path) as fp: for line in fp: snake_case__ : Optional[Any] = line.strip() snake_case__ : Any = os.path.join(REPO_PATH, line) if not (os.path.isfile(path) or os.path.isdir(path)): non_existent_paths.append(line) all_paths.append(path) if len(non_existent_paths) > 0: snake_case__ : Tuple = '''\n'''.join(non_existent_paths) raise ValueError(f'''`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}''') if all_paths != sorted(all_paths): raise ValueError('''Files in `utils/documentation_tests.txt` are not in alphabetical order.''')
274
'''simple docstring''' from typing import List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case__ : List[Any] = logging.get_logger(__name__) snake_case__ : Optional[Any] = { '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''', } class __SCREAMING_SNAKE_CASE ( lowerCamelCase_ ): '''simple docstring''' lowerCamelCase_ :List[str] = '''autoformer''' lowerCamelCase_ :Optional[Any] = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', '''num_hidden_layers''': '''encoder_layers''', } def __init__( self , snake_case_ = None , snake_case_ = None , snake_case_ = "student_t" , snake_case_ = "nll" , snake_case_ = 1 , snake_case_ = [1, 2, 3, 4, 5, 6, 7] , snake_case_ = True , snake_case_ = 0 , snake_case_ = 0 , snake_case_ = 0 , snake_case_ = 0 , snake_case_ = None , snake_case_ = None , snake_case_ = 6_4 , snake_case_ = 2 , snake_case_ = 2 , snake_case_ = 2 , snake_case_ = 2 , snake_case_ = 3_2 , snake_case_ = 3_2 , snake_case_ = "gelu" , snake_case_ = 0.1 , snake_case_ = 0.1 , snake_case_ = 0.1 , snake_case_ = 0.1 , snake_case_ = 0.1 , snake_case_ = 1_0_0 , snake_case_ = 0.02 , snake_case_ = True , snake_case_=True , snake_case_ = 1_0 , snake_case_ = 2_5 , snake_case_ = 3 , **snake_case_ , ): '''simple docstring''' UpperCAmelCase_ : List[Any] = prediction_length UpperCAmelCase_ : List[str] = context_length if context_length is not None else prediction_length UpperCAmelCase_ : Optional[int] = distribution_output UpperCAmelCase_ : Optional[int] = loss UpperCAmelCase_ : Union[str, Any] = input_size UpperCAmelCase_ : int = num_time_features UpperCAmelCase_ : List[str] = lags_sequence UpperCAmelCase_ : Any = scaling UpperCAmelCase_ : Any = num_dynamic_real_features UpperCAmelCase_ : int = num_static_real_features UpperCAmelCase_ : Optional[Any] = num_static_categorical_features if cardinality is not None and num_static_categorical_features > 0: if len(snake_case_ ) != num_static_categorical_features: raise ValueError( 'The cardinality should be a list of the same length as `num_static_categorical_features`' ) UpperCAmelCase_ : List[Any] = cardinality else: UpperCAmelCase_ : Optional[int] = [0] if embedding_dimension is not None and num_static_categorical_features > 0: if len(snake_case_ ) != num_static_categorical_features: raise ValueError( 'The embedding dimension should be a list of the same length as `num_static_categorical_features`' ) UpperCAmelCase_ : List[str] = embedding_dimension else: UpperCAmelCase_ : List[Any] = [min(5_0 , (cat + 1) // 2 ) for cat in self.cardinality] UpperCAmelCase_ : List[str] = num_parallel_samples # Transformer architecture configuration UpperCAmelCase_ : Union[str, Any] = input_size * len(self.lags_sequence ) + self._number_of_features UpperCAmelCase_ : str = d_model UpperCAmelCase_ : str = encoder_attention_heads UpperCAmelCase_ : str = decoder_attention_heads UpperCAmelCase_ : str = encoder_ffn_dim UpperCAmelCase_ : str = decoder_ffn_dim UpperCAmelCase_ : str = encoder_layers UpperCAmelCase_ : str = decoder_layers UpperCAmelCase_ : str = dropout UpperCAmelCase_ : Optional[int] = attention_dropout UpperCAmelCase_ : Tuple = activation_dropout UpperCAmelCase_ : Any = encoder_layerdrop UpperCAmelCase_ : Tuple = decoder_layerdrop UpperCAmelCase_ : List[str] = activation_function UpperCAmelCase_ : Tuple = init_std UpperCAmelCase_ : Union[str, Any] = use_cache # Autoformer UpperCAmelCase_ : Any = label_length UpperCAmelCase_ : Union[str, Any] = moving_average UpperCAmelCase_ : Tuple = autocorrelation_factor super().__init__(is_encoder_decoder=snake_case_ , **snake_case_ ) @property def _UpperCamelCase ( self ): '''simple docstring''' return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
274
1
import numpy as np import torch from torch.utils.data import Dataset from utils import logger class A__ ( __snake_case ): def __init__( self , A_ , A_ ): '''simple docstring''' UpperCamelCase : List[str] = params UpperCamelCase : Union[str, Any] = np.array(A_ ) UpperCamelCase : Optional[int] = np.array([len(A_ ) for t in data] ) self.check() self.remove_long_sequences() self.remove_empty_sequences() self.remove_unknown_sequences() self.check() self.print_statistics() def __getitem__( self , A_ ): '''simple docstring''' return (self.token_ids[index], self.lengths[index]) def __len__( self ): '''simple docstring''' return len(self.lengths ) def __UpperCamelCase( self ): '''simple docstring''' assert len(self.token_ids ) == len(self.lengths ) assert all(self.lengths[i] == len(self.token_ids[i] ) for i in range(len(self.lengths ) ) ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Union[str, Any] = self.params.max_model_input_size UpperCamelCase : Dict = self.lengths > max_len logger.info(F"""Splitting {sum(A_ )} too long sequences.""" ) def divide_chunks(A_ , A_ ): return [l[i : i + n] for i in range(0 , len(A_ ) , A_ )] UpperCamelCase : List[str] = [] UpperCamelCase : Tuple = [] if self.params.mlm: UpperCamelCase , UpperCamelCase : Dict = self.params.special_tok_ids["cls_token"], self.params.special_tok_ids["sep_token"] else: UpperCamelCase , UpperCamelCase : Dict = self.params.special_tok_ids["bos_token"], self.params.special_tok_ids["eos_token"] for seq_, len_ in zip(self.token_ids , self.lengths ): assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_ if len_ <= max_len: new_tok_ids.append(seq_ ) new_lengths.append(len_ ) else: UpperCamelCase : Dict = [] for sub_s in divide_chunks(seq_ , max_len - 2 ): if sub_s[0] != cls_id: UpperCamelCase : Union[str, Any] = np.insert(A_ , 0 , A_ ) if sub_s[-1] != sep_id: UpperCamelCase : Optional[int] = np.insert(A_ , len(A_ ) , A_ ) assert len(A_ ) <= max_len assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s sub_seqs.append(A_ ) new_tok_ids.extend(A_ ) new_lengths.extend([len(A_ ) for l in sub_seqs] ) UpperCamelCase : Union[str, Any] = np.array(A_ ) UpperCamelCase : Union[str, Any] = np.array(A_ ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Tuple = len(self ) UpperCamelCase : Dict = self.lengths > 11 UpperCamelCase : List[str] = self.token_ids[indices] UpperCamelCase : Tuple = self.lengths[indices] UpperCamelCase : Optional[Any] = len(self ) logger.info(F"""Remove {init_size - new_size} too short (<=11 tokens) sequences.""" ) def __UpperCamelCase( self ): '''simple docstring''' if "unk_token" not in self.params.special_tok_ids: return else: UpperCamelCase : List[str] = self.params.special_tok_ids["unk_token"] UpperCamelCase : int = len(self ) UpperCamelCase : str = np.array([np.count_nonzero(a == unk_token_id ) for a in self.token_ids] ) UpperCamelCase : List[Any] = (unk_occs / self.lengths) < 0.5 UpperCamelCase : List[Any] = self.token_ids[indices] UpperCamelCase : str = self.lengths[indices] UpperCamelCase : Dict = len(self ) logger.info(F"""Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).""" ) def __UpperCamelCase( self ): '''simple docstring''' if not self.params.is_master: return logger.info(F"""{len(self )} sequences""" ) # data_len = sum(self.lengths) # nb_unique_tokens = len(Counter(list(chain(*self.token_ids)))) # logger.info(f'{data_len} tokens ({nb_unique_tokens} unique)') # unk_idx = self.params.special_tok_ids['unk_token'] # nb_unknown = sum([(t==unk_idx).sum() for t in self.token_ids]) # logger.info(f'{nb_unknown} unknown tokens (covering {100*nb_unknown/data_len:.2f}% of the data)') def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase : str = [t[0] for t in batch] UpperCamelCase : str = [t[1] for t in batch] assert len(A_ ) == len(A_ ) # Max for paddings UpperCamelCase : Union[str, Any] = max(A_ ) # Pad token ids if self.params.mlm: UpperCamelCase : List[Any] = self.params.special_tok_ids["pad_token"] else: UpperCamelCase : Dict = self.params.special_tok_ids["unk_token"] UpperCamelCase : Optional[int] = [list(t.astype(A_ ) ) + [pad_idx] * (max_seq_len_ - len(A_ )) for t in token_ids] assert len(tk_ ) == len(A_ ) assert all(len(A_ ) == max_seq_len_ for t in tk_ ) UpperCamelCase : Any = torch.tensor(tk_ ) # (bs, max_seq_len_) UpperCamelCase : Any = torch.tensor(A_ ) # (bs) return tk_t, lg_t
52
'''simple docstring''' A__: Optional[int] = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] A__: Any = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] A__: int = { 0: '''Sunday''', 1: '''Monday''', 2: '''Tuesday''', 3: '''Wednesday''', 4: '''Thursday''', 5: '''Friday''', 6: '''Saturday''', } def SCREAMING_SNAKE_CASE_ ( _UpperCAmelCase : int ,_UpperCAmelCase : int ,_UpperCAmelCase : int ) -> str: assert len(str(_UpperCAmelCase ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: _a : List[str] =year // 100 _a : List[str] =(5 * (century % 4) + 2) % 7 _a : Optional[int] =year % 100 _a : Any =centurian % 12 _a : int =( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 _a : Optional[Any] =( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) _a : str =(dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
276
0
import socket def lowerCamelCase__ ( ) -> Optional[int]: __snake_case = socket.socket(socket.AF_INET , socket.SOCK_STREAM ) __snake_case = socket.gethostname() __snake_case = 1_2312 sock.connect((host, port) ) sock.send(B'''Hello server!''' ) with open('''Received_file''' , '''wb''' ) as out_file: print('''File opened''' ) print('''Receiving data...''' ) while True: __snake_case = sock.recv(1024 ) if not data: break out_file.write(_snake_case ) print('''Successfully received the file''' ) sock.close() print('''Connection closed''' ) if __name__ == "__main__": main()
352
from typing import List, Union import numpy as np from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING snake_case_ = logging.get_logger(__name__) @add_end_docstrings(_UpperCAmelCase ) class SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase ): def __init__(self : int , *a__ : List[Any] , **a__ : Dict ): """simple docstring""" super().__init__(*a__ , **a__ ) requires_backends(self , '''vision''' ) self.check_model_type(a__ ) def __call__(self : Optional[Any] , a__ : Union[str, List[str], "Image.Image", List["Image.Image"]] , **a__ : List[str] ): """simple docstring""" return super().__call__(a__ , **a__ ) def a (self : int , **a__ : int ): """simple docstring""" return {}, {}, {} def a (self : Optional[int] , a__ : Optional[int] ): """simple docstring""" __snake_case = load_image(a__ ) __snake_case = image.size __snake_case = self.image_processor(images=a__ , return_tensors=self.framework ) return model_inputs def a (self : List[Any] , a__ : Union[str, Any] ): """simple docstring""" __snake_case = self.model(**a__ ) return model_outputs def a (self : int , a__ : str ): """simple docstring""" __snake_case = model_outputs.predicted_depth __snake_case = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode='''bicubic''' , align_corners=a__ ) __snake_case = prediction.squeeze().cpu().numpy() __snake_case = (output * 255 / np.max(a__ )).astype('''uint8''' ) __snake_case = Image.fromarray(a__ ) __snake_case = {} __snake_case = predicted_depth __snake_case = depth return output_dict
238
0
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { "EleutherAI/gpt-neo-1.3B": "https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json", # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo } class lowercase ( lowerCAmelCase_ ): _SCREAMING_SNAKE_CASE = 'gpt_neo' _SCREAMING_SNAKE_CASE = ['past_key_values'] _SCREAMING_SNAKE_CASE = {'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers'} def __init__( self , lowercase=50_257 , lowercase=2_048 , lowercase=2_048 , lowercase=24 , lowercase=[[["global", "local"], 12]] , lowercase=16 , lowercase=None , lowercase=256 , lowercase="gelu_new" , lowercase=0.0 , lowercase=0.0 , lowercase=0.0 , lowercase=0.1 , lowercase=1e-5 , lowercase=0.02 , lowercase=True , lowercase=50_256 , lowercase=50_256 , **lowercase , ) -> List[Any]: lowerCAmelCase = vocab_size lowerCAmelCase = max_position_embeddings lowerCAmelCase = hidden_size lowerCAmelCase = num_layers lowerCAmelCase = num_heads lowerCAmelCase = intermediate_size lowerCAmelCase = window_size lowerCAmelCase = activation_function lowerCAmelCase = resid_dropout lowerCAmelCase = embed_dropout lowerCAmelCase = attention_dropout lowerCAmelCase = classifier_dropout lowerCAmelCase = layer_norm_epsilon lowerCAmelCase = initializer_range lowerCAmelCase = use_cache lowerCAmelCase = bos_token_id lowerCAmelCase = eos_token_id lowerCAmelCase = attention_types lowerCAmelCase = self.expand_attention_types_params(__lowerCAmelCase ) if len(self.attention_layers ) != self.num_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.attention_layers)` == `config.num_layers` """ f'but is `len(config.attention_layers) = {len(self.attention_layers )}`, ' f'`config.num_layers = {self.num_layers}`. ' """`config.attention_layers` is prepared using `config.attention_types`. """ """Please verify the value of `config.attention_types` argument.""" ) super().__init__(bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) @staticmethod def _snake_case ( lowercase ) -> List[str]: lowerCAmelCase = [] for item in attention_types: for _ in range(item[1] ): attentions.extend(item[0] ) return attentions def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' import torch lowerCAmelCase = input.size() lowerCAmelCase = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase = shape[dimension] lowerCAmelCase = torch.arange(0 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.div(sizedim - size , SCREAMING_SNAKE_CASE , rounding_mode="""floor""" ) + 1 lowerCAmelCase = torch.arange(SCREAMING_SNAKE_CASE ) + low_indices[:min_length][:, None] lowerCAmelCase = [slice(SCREAMING_SNAKE_CASE )] * rank lowerCAmelCase = indices lowerCAmelCase = input[s] lowerCAmelCase = list(range(0 , rank + 1 ) ) perm.append(perm.pop(dimension + 1 ) ) return sliced.permute(SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' import torch lowerCAmelCase = torch.arange(1 , SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.remainder(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase = remainders == 0 lowerCAmelCase = candidates[divisor_indices] lowerCAmelCase = torch.max(SCREAMING_SNAKE_CASE ) return largest_divisor, torch.div(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , rounding_mode="""floor""" ) class lowercase ( lowerCAmelCase_ ): @property def _snake_case ( self ) -> List[Any]: lowerCAmelCase = OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}} ) if self.use_past: self.fill_with_past_key_values_(__lowerCAmelCase , direction="""inputs""" ) lowerCAmelCase = {0: """batch""", 1: """past_sequence + sequence"""} else: lowerCAmelCase = {0: """batch""", 1: """sequence"""} return common_inputs @property def _snake_case ( self ) -> Any: return self._config.num_heads def _snake_case ( self , lowercase , lowercase = -1 , lowercase = -1 , lowercase = False , lowercase = None , ) -> Any: lowerCAmelCase = super(__lowerCAmelCase , self ).generate_dummy_inputs( __lowerCAmelCase , batch_size=__lowerCAmelCase , seq_length=__lowerCAmelCase , is_pair=__lowerCAmelCase , framework=__lowerCAmelCase ) # We need to order the input in the way they appears in the forward() lowerCAmelCase = OrderedDict({"""input_ids""": common_inputs["""input_ids"""]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""" ) else: import torch lowerCAmelCase , lowerCAmelCase = common_inputs["""input_ids"""].shape # Not using the same length for past_key_values lowerCAmelCase = seqlen + 2 lowerCAmelCase = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) lowerCAmelCase = [ (torch.zeros(__lowerCAmelCase ), torch.zeros(__lowerCAmelCase )) for _ in range(self.num_layers ) ] lowerCAmelCase = common_inputs["""attention_mask"""] if self.use_past: lowerCAmelCase = ordered_inputs["""attention_mask"""].dtype lowerCAmelCase = torch.cat( [ordered_inputs["""attention_mask"""], torch.ones(__lowerCAmelCase , __lowerCAmelCase , dtype=__lowerCAmelCase )] , dim=1 ) return ordered_inputs @property def _snake_case ( self ) -> Optional[int]: return 13
46
"""simple docstring""" import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { """b0""": efficientnet.EfficientNetBa, """b1""": efficientnet.EfficientNetBa, """b2""": efficientnet.EfficientNetBa, """b3""": efficientnet.EfficientNetBa, """b4""": efficientnet.EfficientNetBa, """b5""": efficientnet.EfficientNetBa, """b6""": efficientnet.EfficientNetBa, """b7""": efficientnet.EfficientNetBa, } UpperCAmelCase__ = { """b0""": { """hidden_dim""": 1_2_8_0, """width_coef""": 1.0, """depth_coef""": 1.0, """image_size""": 2_2_4, """dropout_rate""": 0.2, """dw_padding""": [], }, """b1""": { """hidden_dim""": 1_2_8_0, """width_coef""": 1.0, """depth_coef""": 1.1, """image_size""": 2_4_0, """dropout_rate""": 0.2, """dw_padding""": [1_6], }, """b2""": { """hidden_dim""": 1_4_0_8, """width_coef""": 1.1, """depth_coef""": 1.2, """image_size""": 2_6_0, """dropout_rate""": 0.3, """dw_padding""": [5, 8, 1_6], }, """b3""": { """hidden_dim""": 1_5_3_6, """width_coef""": 1.2, """depth_coef""": 1.4, """image_size""": 3_0_0, """dropout_rate""": 0.3, """dw_padding""": [5, 1_8], }, """b4""": { """hidden_dim""": 1_7_9_2, """width_coef""": 1.4, """depth_coef""": 1.8, """image_size""": 3_8_0, """dropout_rate""": 0.4, """dw_padding""": [6], }, """b5""": { """hidden_dim""": 2_0_4_8, """width_coef""": 1.6, """depth_coef""": 2.2, """image_size""": 4_5_6, """dropout_rate""": 0.4, """dw_padding""": [1_3, 2_7], }, """b6""": { """hidden_dim""": 2_3_0_4, """width_coef""": 1.8, """depth_coef""": 2.6, """image_size""": 5_2_8, """dropout_rate""": 0.5, """dw_padding""": [3_1], }, """b7""": { """hidden_dim""": 2_5_6_0, """width_coef""": 2.0, """depth_coef""": 3.1, """image_size""": 6_0_0, """dropout_rate""": 0.5, """dw_padding""": [1_8], }, } def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = EfficientNetConfig() _UpperCAmelCase = CONFIG_MAP[model_name]["""hidden_dim"""] _UpperCAmelCase = CONFIG_MAP[model_name]["""width_coef"""] _UpperCAmelCase = CONFIG_MAP[model_name]["""depth_coef"""] _UpperCAmelCase = CONFIG_MAP[model_name]["""image_size"""] _UpperCAmelCase = CONFIG_MAP[model_name]["""dropout_rate"""] _UpperCAmelCase = CONFIG_MAP[model_name]["""dw_padding"""] _UpperCAmelCase = """huggingface/label-files""" _UpperCAmelCase = """imagenet-1k-id2label.json""" _UpperCAmelCase = 10_00 _UpperCAmelCase = json.load(open(hf_hub_download(lowercase ,lowercase ,repo_type="""dataset""" ) ,"""r""" ) ) _UpperCAmelCase = {int(lowercase ): v for k, v in idalabel.items()} _UpperCAmelCase = idalabel _UpperCAmelCase = {v: k for k, v in idalabel.items()} return config def __UpperCAmelCase ( ): """simple docstring""" _UpperCAmelCase = """http://images.cocodataset.org/val2017/000000039769.jpg""" _UpperCAmelCase = Image.open(requests.get(lowercase ,stream=lowercase ).raw ) return im def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = CONFIG_MAP[model_name]["""image_size"""] _UpperCAmelCase = EfficientNetImageProcessor( size={"""height""": size, """width""": size} ,image_mean=[0.4_85, 0.4_56, 0.4_06] ,image_std=[0.47_85_39_44, 0.4_73_28_64, 0.47_43_41_63] ,do_center_crop=lowercase ,) return preprocessor def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [v.split("""_""" )[0].split("""block""" )[1] for v in original_param_names if v.startswith("""block""" )] _UpperCAmelCase = sorted(set(lowercase ) ) _UpperCAmelCase = len(lowercase ) _UpperCAmelCase = {b: str(lowercase ) for b, i in zip(lowercase ,range(lowercase ) )} _UpperCAmelCase = [] rename_keys.append(("""stem_conv/kernel:0""", """embeddings.convolution.weight""") ) rename_keys.append(("""stem_bn/gamma:0""", """embeddings.batchnorm.weight""") ) rename_keys.append(("""stem_bn/beta:0""", """embeddings.batchnorm.bias""") ) rename_keys.append(("""stem_bn/moving_mean:0""", """embeddings.batchnorm.running_mean""") ) rename_keys.append(("""stem_bn/moving_variance:0""", """embeddings.batchnorm.running_var""") ) for b in block_names: _UpperCAmelCase = block_name_mapping[b] rename_keys.append((f'''block{b}_expand_conv/kernel:0''', f'''encoder.blocks.{hf_b}.expansion.expand_conv.weight''') ) rename_keys.append((f'''block{b}_expand_bn/gamma:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.weight''') ) rename_keys.append((f'''block{b}_expand_bn/beta:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.bias''') ) rename_keys.append( (f'''block{b}_expand_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_mean''') ) rename_keys.append( (f'''block{b}_expand_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_var''') ) rename_keys.append( (f'''block{b}_dwconv/depthwise_kernel:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight''') ) rename_keys.append((f'''block{b}_bn/gamma:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight''') ) rename_keys.append((f'''block{b}_bn/beta:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias''') ) rename_keys.append( (f'''block{b}_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean''') ) rename_keys.append( (f'''block{b}_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var''') ) rename_keys.append((f'''block{b}_se_reduce/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.weight''') ) rename_keys.append((f'''block{b}_se_reduce/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.bias''') ) rename_keys.append((f'''block{b}_se_expand/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.weight''') ) rename_keys.append((f'''block{b}_se_expand/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.bias''') ) rename_keys.append( (f'''block{b}_project_conv/kernel:0''', f'''encoder.blocks.{hf_b}.projection.project_conv.weight''') ) rename_keys.append((f'''block{b}_project_bn/gamma:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.weight''') ) rename_keys.append((f'''block{b}_project_bn/beta:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.bias''') ) rename_keys.append( (f'''block{b}_project_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_mean''') ) rename_keys.append( (f'''block{b}_project_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_var''') ) rename_keys.append(("""top_conv/kernel:0""", """encoder.top_conv.weight""") ) rename_keys.append(("""top_bn/gamma:0""", """encoder.top_bn.weight""") ) rename_keys.append(("""top_bn/beta:0""", """encoder.top_bn.bias""") ) rename_keys.append(("""top_bn/moving_mean:0""", """encoder.top_bn.running_mean""") ) rename_keys.append(("""top_bn/moving_variance:0""", """encoder.top_bn.running_var""") ) _UpperCAmelCase = {} for item in rename_keys: if item[0] in original_param_names: _UpperCAmelCase = """efficientnet.""" + item[1] _UpperCAmelCase = """classifier.weight""" _UpperCAmelCase = """classifier.bias""" return key_mapping def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ): """simple docstring""" for key, value in tf_params.items(): if "normalization" in key: continue _UpperCAmelCase = key_mapping[key] if "_conv" in key and "kernel" in key: _UpperCAmelCase = torch.from_numpy(lowercase ).permute(3 ,2 ,0 ,1 ) elif "depthwise_kernel" in key: _UpperCAmelCase = torch.from_numpy(lowercase ).permute(2 ,3 ,0 ,1 ) elif "kernel" in key: _UpperCAmelCase = torch.from_numpy(np.transpose(lowercase ) ) else: _UpperCAmelCase = torch.from_numpy(lowercase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(lowercase ) @torch.no_grad() def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = model_classes[model_name]( include_top=lowercase ,weights="""imagenet""" ,input_tensor=lowercase ,input_shape=lowercase ,pooling=lowercase ,classes=10_00 ,classifier_activation="""softmax""" ,) _UpperCAmelCase = original_model.trainable_variables _UpperCAmelCase = original_model.non_trainable_variables _UpperCAmelCase = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: _UpperCAmelCase = param.numpy() _UpperCAmelCase = list(tf_params.keys() ) # Load HuggingFace model _UpperCAmelCase = get_efficientnet_config(lowercase ) _UpperCAmelCase = EfficientNetForImageClassification(lowercase ).eval() _UpperCAmelCase = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print("""Converting parameters...""" ) _UpperCAmelCase = rename_keys(lowercase ) replace_params(lowercase ,lowercase ,lowercase ) # Initialize preprocessor and preprocess input image _UpperCAmelCase = convert_image_processor(lowercase ) _UpperCAmelCase = preprocessor(images=prepare_img() ,return_tensors="""pt""" ) # HF model inference hf_model.eval() with torch.no_grad(): _UpperCAmelCase = hf_model(**lowercase ) _UpperCAmelCase = outputs.logits.detach().numpy() # Original model inference _UpperCAmelCase = False _UpperCAmelCase = CONFIG_MAP[model_name]["""image_size"""] _UpperCAmelCase = prepare_img().resize((image_size, image_size) ,resample=PIL.Image.NEAREST ) _UpperCAmelCase = image.img_to_array(lowercase ) _UpperCAmelCase = np.expand_dims(lowercase ,axis=0 ) _UpperCAmelCase = original_model.predict(lowercase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(lowercase ,lowercase ,atol=1E-3 ), "The predicted logits are not the same." print("""Model outputs match!""" ) if save_model: # Create folder to save model if not os.path.isdir(lowercase ): os.mkdir(lowercase ) # Save converted model and image processor hf_model.save_pretrained(lowercase ) preprocessor.save_pretrained(lowercase ) if push_to_hub: # Push model and image processor to hub print(f'''Pushing converted {model_name} to the hub...''' ) _UpperCAmelCase = f'''efficientnet-{model_name}''' preprocessor.push_to_hub(lowercase ) hf_model.push_to_hub(lowercase ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""b0""", type=str, help="""Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].""", ) parser.add_argument( """--pytorch_dump_folder_path""", default="""hf_model""", type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument("""--save_model""", action="""store_true""", help="""Save model to local""") parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Push model and image processor to the hub""") UpperCAmelCase__ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
289
0
import argparse import os import re import packaging.version a__ = '''examples/''' a__ = { '''examples''': (re.compile(R'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''), '''init''': (re.compile(R'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''), '''setup''': (re.compile(R'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), R'''\1version="VERSION",'''), '''doc''': (re.compile(R'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''), } a__ = { '''init''': '''src/transformers/__init__.py''', '''setup''': '''setup.py''', } a__ = '''README.md''' def __UpperCAmelCase ( __a : List[str] ,__a : int ,__a : Optional[Any] ) -> int: """simple docstring""" with open(__a ,'''r''' ,encoding='''utf-8''' ,newline='''\n''' ) as f: _a : Tuple = f.read() _a , _a : str = REPLACE_PATTERNS[pattern] _a : List[str] = replace.replace('''VERSION''' ,__a ) _a : List[Any] = re_pattern.sub(__a ,__a ) with open(__a ,'''w''' ,encoding='''utf-8''' ,newline='''\n''' ) as f: f.write(__a ) def __UpperCAmelCase ( __a : Any ) -> List[Any]: """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 __UpperCAmelCase ( __a : List[Any] ,__a : List[str]=False ) -> int: """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 __UpperCAmelCase ( ) -> List[str]: """simple docstring""" _a : Optional[Any] = '''🤗 Transformers currently provides the following architectures''' _a : str = '''1. Want to contribute a new model?''' with open(__a ,'''r''' ,encoding='''utf-8''' ,newline='''\n''' ) as f: _a : Optional[int] = f.readlines() # Find the start of the list. _a : Optional[int] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 _a : List[Any] = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('''1.''' ): _a : Tuple = 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 __UpperCAmelCase ( ) -> List[str]: """simple docstring""" with open(REPLACE_FILES['''init'''] ,'''r''' ) as f: _a : Optional[Any] = f.read() _a : Optional[Any] = REPLACE_PATTERNS['''init'''][0].search(__a ).groups()[0] return packaging.version.parse(__a ) def __UpperCAmelCase ( __a : Dict=False ) -> str: """simple docstring""" _a : Optional[Any] = 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: _a : List[Any] = default_version.base_version elif patch: _a : str = F"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}""" else: _a : List[str] = F"""{default_version.major}.{default_version.minor + 1}.0""" # Now let's ask nicely if that's the right one. _a : Dict = input(F"""Which version are you releasing? [{default_version}]""" ) if len(__a ) == 0: _a : int = 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 __UpperCAmelCase ( ) -> Tuple: """simple docstring""" _a : str = get_version() _a : int = F"""{current_version.major}.{current_version.minor + 1}.0.dev0""" _a : List[Any] = current_version.base_version # Check with the user we got that right. _a : Union[str, Any] = input(F"""Which version are we developing now? [{dev_version}]""" ) if len(__a ) == 0: _a : List[str] = 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__": a__ = argparse.ArgumentParser() parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''') parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''') a__ = 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()
15
def __UpperCAmelCase ( __a : int ) -> int: """simple docstring""" if n == 1 or not isinstance(__a ,__a ): return 0 elif n == 2: return 1 else: _a : Any = [0, 1] for i in range(2 ,n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def __UpperCAmelCase ( __a : int ) -> int: """simple docstring""" _a : Any = 0 _a : Dict = 2 while digits < n: index += 1 _a : Dict = len(str(fibonacci(__a ) ) ) return index def __UpperCAmelCase ( __a : int = 1_000 ) -> int: """simple docstring""" return fibonacci_digits_index(__a ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
15
1
'''simple docstring''' import re def SCREAMING_SNAKE_CASE__ ( __A ) -> str: if len(re.findall('[ATCG]' , __A ) ) != len(__A ): raise ValueError('Invalid Strand' ) return dna.translate(dna.maketrans('ATCG' , 'TAGC' ) ) if __name__ == "__main__": import doctest doctest.testmod()
42
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import _LazyModule _lowercase : int = {'processing_wav2vec2_with_lm': ['Wav2Vec2ProcessorWithLM']} if TYPE_CHECKING: from .processing_wavaveca_with_lm import WavaVecaProcessorWithLM else: import sys _lowercase : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
332
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 _a ( lowerCamelCase: Tuple , lowerCamelCase: Optional[Any] , lowerCamelCase: Dict , lowerCamelCase: Optional[Any]=None , lowerCamelCase: Dict=None , lowerCamelCase: Optional[int]=None , lowerCamelCase: Dict=None , lowerCamelCase: Tuple=None , ) -> Tuple: '''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=lowerCamelCase ) if decoder_head_mask is None: __A = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCamelCase ) if cross_attn_head_mask is None: __A = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCamelCase ) 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 A_ : def __init__(self :Any , _UpperCamelCase :List[Any] , _UpperCamelCase :List[str]=13 , _UpperCamelCase :List[Any]=7 , _UpperCamelCase :Any=True , _UpperCamelCase :Union[str, Any]=False , _UpperCamelCase :str=99 , _UpperCamelCase :str=16 , _UpperCamelCase :Union[str, Any]=2 , _UpperCamelCase :Dict=4 , _UpperCamelCase :Any=4 , _UpperCamelCase :List[Any]="relu" , _UpperCamelCase :str=0.1 , _UpperCamelCase :str=0.1 , _UpperCamelCase :Dict=0.0 , _UpperCamelCase :int=0.0 , _UpperCamelCase :List[str]=20 , _UpperCamelCase :Union[str, Any]=2 , _UpperCamelCase :Any=1 , _UpperCamelCase :Union[str, Any]=0 , )-> List[str]: __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 :int )-> int: __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(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) return config, inputs_dict def _lowerCAmelCase (self :Optional[int] )-> Optional[Any]: 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 :Union[str, Any] )-> int: __A , __A = self.prepare_config_and_inputs() return config, inputs_dict def _lowerCAmelCase (self :List[str] , _UpperCamelCase :Any , _UpperCamelCase :List[str] )-> Any: __A = MaMaaaModel(config=_UpperCamelCase ).get_decoder().to(_UpperCamelCase ).eval() __A = inputs_dict['''input_ids'''] __A = inputs_dict['''attention_mask'''] __A = inputs_dict['''head_mask'''] # first forward pass __A = model(_UpperCamelCase , attention_mask=_UpperCamelCase , head_mask=_UpperCamelCase , use_cache=_UpperCamelCase ) __A , __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(_UpperCamelCase , attention_mask=_UpperCamelCase )['''last_hidden_state'''] __A = model(_UpperCamelCase , attention_mask=_UpperCamelCase , past_key_values=_UpperCamelCase )[ '''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(_UpperCamelCase , _UpperCamelCase , atol=1e-2 ) ) def _lowerCAmelCase (self :List[Any] , _UpperCamelCase :Dict , _UpperCamelCase :Any )-> Optional[Any]: __A = MaMaaaModel(config=_UpperCamelCase ).to(_UpperCamelCase ).eval() __A = model(**_UpperCamelCase ) __A = outputs.encoder_last_hidden_state __A = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: __A = model.get_encoder() encoder.save_pretrained(_UpperCamelCase ) __A = MaMaaaEncoder.from_pretrained(_UpperCamelCase ).to(_UpperCamelCase ) __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(_UpperCamelCase ) __A = MaMaaaDecoder.from_pretrained(_UpperCamelCase ).to(_UpperCamelCase ) __A = decoder( input_ids=inputs_dict['''decoder_input_ids'''] , attention_mask=inputs_dict['''decoder_attention_mask'''] , encoder_hidden_states=_UpperCamelCase , 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 A_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , unittest.TestCase ): lowerCAmelCase__ = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) lowerCAmelCase__ = (MaMaaaForConditionalGeneration,) if is_torch_available() else () lowerCAmelCase__ = ( { """conversational""": MaMaaaForConditionalGeneration, """feature-extraction""": MaMaaaModel, """summarization""": MaMaaaForConditionalGeneration, """text2text-generation""": MaMaaaForConditionalGeneration, """translation""": MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) lowerCAmelCase__ = True lowerCAmelCase__ = True lowerCAmelCase__ = False lowerCAmelCase__ = False def _lowerCAmelCase (self :Tuple , _UpperCamelCase :Optional[Any] , _UpperCamelCase :int , _UpperCamelCase :Any , _UpperCamelCase :Optional[Any] , _UpperCamelCase :str )-> List[str]: 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 :Any )-> Optional[Any]: __A = MaMaaaModelTester(self ) __A = ConfigTester(self , config_class=_UpperCamelCase ) def _lowerCAmelCase (self :Any )-> Optional[Any]: self.config_tester.run_common_tests() def _lowerCAmelCase (self :str )-> Any: __A , __A = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: __A = model_class(_UpperCamelCase ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_UpperCamelCase ) __A , __A = model_class.from_pretrained(_UpperCamelCase , output_loading_info=_UpperCamelCase ) self.assertEqual(info['''missing_keys'''] , [] ) def _lowerCAmelCase (self :Dict )-> List[str]: __A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*_UpperCamelCase ) def _lowerCAmelCase (self :Optional[int] )-> Any: __A = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*_UpperCamelCase ) def _lowerCAmelCase (self :Tuple )-> Optional[Any]: __A , __A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): __A = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() __A = copy.deepcopy(self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) 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''' , _UpperCamelCase ) del inputs["input_ids"] inputs.pop('''decoder_input_ids''' , _UpperCamelCase ) __A = model.get_input_embeddings() if not self.is_encoder_decoder: __A = wte(_UpperCamelCase ) else: __A = wte(_UpperCamelCase ) __A = wte(_UpperCamelCase ) with torch.no_grad(): model(**_UpperCamelCase )[0] def _lowerCAmelCase (self :List[Any] )-> List[str]: __A , __A = self.model_tester.prepare_config_and_inputs() __A = input_dict['''input_ids'''] __A = input_ids.ne(1 ).to(_UpperCamelCase ) __A = MaMaaaForConditionalGeneration(_UpperCamelCase ).eval().to(_UpperCamelCase ) if torch_device == "cuda": model.half() model.generate(_UpperCamelCase , attention_mask=_UpperCamelCase ) model.generate(num_beams=4 , do_sample=_UpperCamelCase , early_stopping=_UpperCamelCase , num_return_sequences=3 ) def _a ( lowerCamelCase: Tuple ) -> str: '''simple docstring''' return torch.tensor(lowerCamelCase , dtype=torch.long , device=lowerCamelCase ) snake_case__ : Dict = 1e-4 @require_torch @require_sentencepiece @require_tokenizers @slow class A_ ( unittest.TestCase ): @cached_property def _lowerCAmelCase (self :Optional[Any] )-> List[str]: return MaMaaaTokenizer.from_pretrained('''facebook/m2m100_418M''' ) def _lowerCAmelCase (self :Dict )-> Union[str, Any]: __A = MaMaaaModel.from_pretrained('''facebook/m2m100_418M''' ).to(_UpperCamelCase ) __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 , _UpperCamelCase , _UpperCamelCase ) with torch.no_grad(): __A = model(**_UpperCamelCase )[0] __A = torch.Size((1, 11, 1024) ) self.assertEqual(output.shape , _UpperCamelCase ) # change to expected output here __A = torch.tensor( [[-0.7_7_8_0, -0.1_6_7_6, 0.1_0_3_8], [-6.7_5_5_6, -1.3_9_9_2, 0.0_5_6_7], [-7.5_3_8_3, -0.5_9_2_0, -0.2_7_7_9]] , device=_UpperCamelCase ) self.assertTrue(torch.allclose(output[:, :3, :3] , _UpperCamelCase , atol=_UpperCamelCase ) ) def _lowerCAmelCase (self :Dict )-> str: __A = MaMaaaForConditionalGeneration.from_pretrained('''facebook/m2m100_418M''' ).to(_UpperCamelCase ) # 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 , _UpperCamelCase , _UpperCamelCase ) with torch.no_grad(): __A = model(**_UpperCamelCase )[0] __A = torch.Size((1, 11, model.config.vocab_size) ) self.assertEqual(output.shape , _UpperCamelCase ) # change to expected output here __A = torch.tensor( [[-1.0_4_4_8, -1.0_4_1_1, 3.7_9_9_2], [-3.2_1_9_1, -3.2_3_8_6, -1.3_4_5_1], [-3.6_2_1_0, -3.5_9_9_3, 0.4_9_2_5]] , device=_UpperCamelCase ) self.assertTrue(torch.allclose(output[:, :3, :3] , _UpperCamelCase , atol=_UpperCamelCase ) ) def _lowerCAmelCase (self :List[str] )-> Optional[Any]: __A = MaMaaaForConditionalGeneration.from_pretrained('''facebook/m2m100_418M''' ).to(_UpperCamelCase ) __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(_UpperCamelCase , padding=_UpperCamelCase , return_tensors='''pt''' ) __A = model.generate( input_ids=dct['''input_ids'''].to(_UpperCamelCase ) , attention_mask=dct['''attention_mask'''].to(_UpperCamelCase ) , 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=_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) assert generated == expected_en
355
from __future__ import annotations snake_case__ : Dict = [True] * 1000001 snake_case__ : int = 2 while i * i <= 1000000: if seive[i]: for j in range(i * i, 1000001, i): snake_case__ : str = False i += 1 def _a ( lowerCamelCase: int ) -> bool: '''simple docstring''' return seive[n] def _a ( lowerCamelCase: int ) -> bool: '''simple docstring''' return any(digit in '''02468''' for digit in str(lowerCamelCase ) ) def _a ( lowerCamelCase: int = 1_00_00_00 ) -> list[int]: '''simple docstring''' __A = [2] # result already includes the number 2. for num in range(3 , limit + 1 , 2 ): if is_prime(lowerCamelCase ) and not contains_an_even_digit(lowerCamelCase ): __A = str(lowerCamelCase ) __A = [int(str_num[j:] + str_num[:j] ) for j in range(len(lowerCamelCase ) )] if all(is_prime(lowerCamelCase ) for i in list_nums ): result.append(lowerCamelCase ) return result def _a ( ) -> int: '''simple docstring''' return len(find_circular_primes() ) if __name__ == "__main__": print(f'{len(find_circular_primes()) = }')
250
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : Optional[int] = { '''configuration_table_transformer''': [ '''TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TableTransformerConfig''', '''TableTransformerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = [ '''TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TableTransformerForObjectDetection''', '''TableTransformerModel''', '''TableTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys A : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
1
'''simple docstring''' import time import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers.generation import ( MaxLengthCriteria, MaxNewTokensCriteria, MaxTimeCriteria, StoppingCriteriaList, validate_stopping_criteria, ) @require_torch class _a ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = 3 SCREAMING_SNAKE_CASE : Tuple = 250 SCREAMING_SNAKE_CASE : List[Any] = ids_tensor((batch_size, length), A ) SCREAMING_SNAKE_CASE : Tuple = torch.ones((batch_size, length), device=A, dtype=torch.float ) / length return input_ids, scores def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = self._get_tensors(5 ) SCREAMING_SNAKE_CASE : List[str] = StoppingCriteriaList( [ MaxLengthCriteria(max_length=10 ), MaxTimeCriteria(max_time=0.1 ), ] ) self.assertFalse(criteria(A, A ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = self._get_tensors(9 ) self.assertFalse(criteria(A, A ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = self._get_tensors(10 ) self.assertTrue(criteria(A, A ) ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = MaxLengthCriteria(max_length=10 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = self._get_tensors(5 ) self.assertFalse(criteria(A, A ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = self._get_tensors(9 ) self.assertFalse(criteria(A, A ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = self._get_tensors(10 ) self.assertTrue(criteria(A, A ) ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = MaxNewTokensCriteria(start_length=5, max_new_tokens=5 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = self._get_tensors(5 ) self.assertFalse(criteria(A, A ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Any = self._get_tensors(9 ) self.assertFalse(criteria(A, A ) ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = self._get_tensors(10 ) self.assertTrue(criteria(A, A ) ) SCREAMING_SNAKE_CASE : List[Any] = StoppingCriteriaList([criteria] ) self.assertEqual(criteria_list.max_length, 10 ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[int] = self._get_tensors(5 ) SCREAMING_SNAKE_CASE : int = MaxTimeCriteria(max_time=0.1 ) self.assertFalse(criteria(A, A ) ) SCREAMING_SNAKE_CASE : Optional[int] = MaxTimeCriteria(max_time=0.1, initial_timestamp=time.time() - 0.2 ) self.assertTrue(criteria(A, A ) ) def UpperCamelCase_ ( self ): '''simple docstring''' validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ), 10 ) with self.assertWarns(A ): validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10 )] ), 11 ) SCREAMING_SNAKE_CASE : Optional[Any] = validate_stopping_criteria(StoppingCriteriaList(), 11 ) self.assertEqual(len(A ), 1 )
246
'''simple docstring''' import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class _a : '''simple docstring''' def __init__( self, A, A=2, A=3, A=4, A=2, A=7, A=True, A=True, A=True, A=True, A=99, A=36, A=3, A=4, A=37, A="gelu", A=0.1, A=0.1, A=512, A=16, A=2, A=0.02, A=6, A=6, A=3, A=4, A=None, A=1_000, ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = parent SCREAMING_SNAKE_CASE : List[Any] = batch_size SCREAMING_SNAKE_CASE : Optional[int] = num_channels SCREAMING_SNAKE_CASE : Optional[int] = image_size SCREAMING_SNAKE_CASE : List[str] = patch_size SCREAMING_SNAKE_CASE : List[Any] = text_seq_length SCREAMING_SNAKE_CASE : Tuple = is_training SCREAMING_SNAKE_CASE : Tuple = use_input_mask SCREAMING_SNAKE_CASE : Optional[int] = use_token_type_ids SCREAMING_SNAKE_CASE : Optional[Any] = use_labels SCREAMING_SNAKE_CASE : Optional[int] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : List[str] = num_hidden_layers SCREAMING_SNAKE_CASE : Union[str, Any] = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : str = max_position_embeddings SCREAMING_SNAKE_CASE : List[str] = type_vocab_size SCREAMING_SNAKE_CASE : Tuple = type_sequence_label_size SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : str = coordinate_size SCREAMING_SNAKE_CASE : Tuple = shape_size SCREAMING_SNAKE_CASE : Optional[Any] = num_labels SCREAMING_SNAKE_CASE : int = num_choices SCREAMING_SNAKE_CASE : str = scope SCREAMING_SNAKE_CASE : Optional[int] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE : Union[str, Any] = text_seq_length SCREAMING_SNAKE_CASE : List[str] = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE : int = self.text_seq_length + self.image_seq_length def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = ids_tensor([self.batch_size, self.text_seq_length], self.vocab_size ) SCREAMING_SNAKE_CASE : int = ids_tensor([self.batch_size, self.text_seq_length, 4], self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE : str = bbox[i, j, 3] SCREAMING_SNAKE_CASE : Optional[Any] = bbox[i, j, 1] SCREAMING_SNAKE_CASE : Tuple = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE : Optional[Any] = bbox[i, j, 2] SCREAMING_SNAKE_CASE : Any = bbox[i, j, 0] SCREAMING_SNAKE_CASE : Tuple = t SCREAMING_SNAKE_CASE : List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Any = None if self.use_input_mask: SCREAMING_SNAKE_CASE : List[Any] = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.text_seq_length], self.type_vocab_size ) SCREAMING_SNAKE_CASE : Optional[Any] = None SCREAMING_SNAKE_CASE : Tuple = None if self.use_labels: SCREAMING_SNAKE_CASE : int = ids_tensor([self.batch_size], self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.text_seq_length], self.num_labels ) SCREAMING_SNAKE_CASE : int = LayoutLMvaConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, initializer_range=self.initializer_range, coordinate_size=self.coordinate_size, shape_size=self.shape_size, input_size=self.image_size, patch_size=self.patch_size, ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def UpperCamelCase_ ( self, A, A, A, A, A, A, A, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = LayoutLMvaModel(config=A ) model.to(A ) model.eval() # text + image SCREAMING_SNAKE_CASE : Optional[int] = model(A, pixel_values=A ) SCREAMING_SNAKE_CASE : Union[str, Any] = model( A, bbox=A, pixel_values=A, attention_mask=A, token_type_ids=A ) SCREAMING_SNAKE_CASE : List[str] = model(A, bbox=A, pixel_values=A, token_type_ids=A ) SCREAMING_SNAKE_CASE : Optional[int] = model(A, bbox=A, pixel_values=A ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE : List[Any] = model(A ) self.parent.assertEqual( result.last_hidden_state.shape, (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE : List[str] = model(pixel_values=A ) self.parent.assertEqual( result.last_hidden_state.shape, (self.batch_size, self.image_seq_length, self.hidden_size) ) def UpperCamelCase_ ( self, A, A, A, A, A, A, A, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE : List[str] = LayoutLMvaForSequenceClassification(A ) model.to(A ) model.eval() SCREAMING_SNAKE_CASE : Dict = model( A, bbox=A, pixel_values=A, attention_mask=A, token_type_ids=A, labels=A, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self, A, A, A, A, A, A, A, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.num_labels SCREAMING_SNAKE_CASE : str = LayoutLMvaForTokenClassification(config=A ) model.to(A ) model.eval() SCREAMING_SNAKE_CASE : str = model( A, bbox=A, pixel_values=A, attention_mask=A, token_type_ids=A, labels=A, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.text_seq_length, self.num_labels) ) def UpperCamelCase_ ( self, A, A, A, A, A, A, A, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = LayoutLMvaForQuestionAnswering(config=A ) model.to(A ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = model( A, bbox=A, pixel_values=A, attention_mask=A, token_type_ids=A, start_positions=A, end_positions=A, ) self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length) ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) : Any = config_and_inputs SCREAMING_SNAKE_CASE : Dict = { 'input_ids': input_ids, 'bbox': bbox, 'pixel_values': pixel_values, 'token_type_ids': token_type_ids, 'attention_mask': input_mask, } return config, inputs_dict @require_torch class _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' A : Optional[int] = False A : List[str] = False A : Union[str, Any] = False A : Optional[Any] = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) A : List[Any] = ( {'''document-question-answering''': LayoutLMvaForQuestionAnswering, '''feature-extraction''': LayoutLMvaModel} if is_torch_available() else {} ) def UpperCamelCase_ ( self, A, A, A, A, A ): '''simple docstring''' return True def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = LayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE : Optional[Any] = ConfigTester(self, config_class=A, hidden_size=37 ) def UpperCamelCase_ ( self, A, A, A=False ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = copy.deepcopy(A ) if model_class in get_values(A ): SCREAMING_SNAKE_CASE : Optional[int] = { k: v.unsqueeze(1 ).expand(-1, self.model_tester.num_choices, -1 ).contiguous() if isinstance(A, torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(A ): SCREAMING_SNAKE_CASE : Optional[Any] = torch.ones(self.model_tester.batch_size, dtype=torch.long, device=A ) elif model_class in get_values(A ): SCREAMING_SNAKE_CASE : Dict = torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=A ) SCREAMING_SNAKE_CASE : Dict = torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=A ) elif model_class in [ *get_values(A ), ]: SCREAMING_SNAKE_CASE : str = torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=A ) elif model_class in [ *get_values(A ), ]: SCREAMING_SNAKE_CASE : Union[str, Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length), dtype=torch.long, device=A, ) return inputs_dict def UpperCamelCase_ ( self ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : List[str] = type self.model_tester.create_and_check_model(*A ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A ) @slow def UpperCamelCase_ ( self ): '''simple docstring''' for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[Any] = LayoutLMvaModel.from_pretrained(A ) self.assertIsNotNone(A ) def lowercase__( ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch class _a ( unittest.TestCase ): '''simple docstring''' @cached_property def UpperCamelCase_ ( self ): '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=A ) if is_vision_available() else None @slow def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base' ).to(A ) SCREAMING_SNAKE_CASE : Any = self.default_image_processor SCREAMING_SNAKE_CASE : Any = prepare_img() SCREAMING_SNAKE_CASE : Dict = image_processor(images=A, return_tensors='pt' ).pixel_values.to(A ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([[1, 2]] ) SCREAMING_SNAKE_CASE : Tuple = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass SCREAMING_SNAKE_CASE : Union[str, Any] = model( input_ids=input_ids.to(A ), bbox=bbox.to(A ), pixel_values=pixel_values.to(A ), ) # verify the logits SCREAMING_SNAKE_CASE : str = torch.Size((1, 199, 768) ) self.assertEqual(outputs.last_hidden_state.shape, A ) SCREAMING_SNAKE_CASE : Tuple = torch.tensor( [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] ).to(A ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3], A, atol=1E-4 ) )
246
1
import argparse import collections import json import os import re import string import sys import numpy as np __lowerCamelCase : List[str] = re.compile(R'''\b(a|an|the)\b''', re.UNICODE) __lowerCamelCase : Dict = None def _snake_case ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = 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=lowerCAmelCase , 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=lowerCAmelCase , 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 _snake_case ( lowerCAmelCase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: SCREAMING_SNAKE_CASE_ : Optional[Any] = bool(qa["answers"]["text"] ) return qid_to_has_ans def _snake_case ( lowerCAmelCase : Optional[Any] ): """simple docstring""" def remove_articles(lowerCAmelCase : List[Any] ): return ARTICLES_REGEX.sub(" " , lowerCAmelCase ) def white_space_fix(lowerCAmelCase : Optional[Any] ): return " ".join(text.split() ) def remove_punc(lowerCAmelCase : List[str] ): SCREAMING_SNAKE_CASE_ : List[str] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(lowerCAmelCase : List[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCAmelCase ) ) ) ) def _snake_case ( lowerCAmelCase : str ): """simple docstring""" if not s: return [] return normalize_answer(lowerCAmelCase ).split() def _snake_case ( lowerCAmelCase : List[Any] , lowerCAmelCase : int ): """simple docstring""" return int(normalize_answer(lowerCAmelCase ) == normalize_answer(lowerCAmelCase ) ) def _snake_case ( lowerCAmelCase : Union[str, Any] , lowerCAmelCase : Any ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = get_tokens(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Optional[Any] = get_tokens(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : str = collections.Counter(lowerCAmelCase ) & collections.Counter(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = sum(common.values() ) if len(lowerCAmelCase ) == 0 or len(lowerCAmelCase ) == 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 SCREAMING_SNAKE_CASE_ : str = 1.0 * num_same / len(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : List[str] = 1.0 * num_same / len(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : List[str] = (2 * precision * recall) / (precision + recall) return fa def _snake_case ( lowerCAmelCase : int , lowerCAmelCase : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = {} SCREAMING_SNAKE_CASE_ : Dict = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: SCREAMING_SNAKE_CASE_ : Tuple = qa["id"] SCREAMING_SNAKE_CASE_ : Dict = [t for t in qa["answers"]["text"] if normalize_answer(lowerCAmelCase )] if not gold_answers: # For unanswerable questions, only correct answer is empty string SCREAMING_SNAKE_CASE_ : Any = [""] if qid not in preds: print(f'Missing prediction for {qid}' ) continue SCREAMING_SNAKE_CASE_ : str = preds[qid] # Take max over all gold answers SCREAMING_SNAKE_CASE_ : str = max(compute_exact(lowerCAmelCase , lowerCAmelCase ) for a in gold_answers ) SCREAMING_SNAKE_CASE_ : int = max(compute_fa(lowerCAmelCase , lowerCAmelCase ) for a in gold_answers ) return exact_scores, fa_scores def _snake_case ( lowerCAmelCase : List[str] , lowerCAmelCase : Dict , lowerCAmelCase : List[str] , lowerCAmelCase : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = {} for qid, s in scores.items(): SCREAMING_SNAKE_CASE_ : Optional[int] = na_probs[qid] > na_prob_thresh if pred_na: SCREAMING_SNAKE_CASE_ : Optional[int] = float(not qid_to_has_ans[qid] ) else: SCREAMING_SNAKE_CASE_ : Optional[int] = s return new_scores def _snake_case ( lowerCAmelCase : List[Any] , lowerCAmelCase : Dict , lowerCAmelCase : Optional[Any]=None ): """simple docstring""" if not qid_list: SCREAMING_SNAKE_CASE_ : Optional[Any] = len(lowerCAmelCase ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: SCREAMING_SNAKE_CASE_ : Any = len(lowerCAmelCase ) 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 _snake_case ( lowerCAmelCase : Union[str, Any] , lowerCAmelCase : List[Any] , lowerCAmelCase : List[Any] ): """simple docstring""" for k in new_eval: SCREAMING_SNAKE_CASE_ : Union[str, Any] = new_eval[k] def _snake_case ( lowerCAmelCase : str , lowerCAmelCase : Any , lowerCAmelCase : str , lowerCAmelCase : Union[str, Any] ): """simple docstring""" plt.step(lowerCAmelCase , lowerCAmelCase , color="b" , alpha=0.2 , where="post" ) plt.fill_between(lowerCAmelCase , lowerCAmelCase , 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(lowerCAmelCase ) plt.savefig(lowerCAmelCase ) plt.clf() def _snake_case ( lowerCAmelCase : Optional[Any] , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : List[Any] , lowerCAmelCase : List[Any]=None , lowerCAmelCase : Union[str, Any]=None ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = sorted(lowerCAmelCase , key=lambda lowerCAmelCase : na_probs[k] ) SCREAMING_SNAKE_CASE_ : List[str] = 0.0 SCREAMING_SNAKE_CASE_ : int = 1.0 SCREAMING_SNAKE_CASE_ : List[Any] = 0.0 SCREAMING_SNAKE_CASE_ : Optional[Any] = [1.0] SCREAMING_SNAKE_CASE_ : List[str] = [0.0] SCREAMING_SNAKE_CASE_ : Optional[int] = 0.0 for i, qid in enumerate(lowerCAmelCase ): if qid_to_has_ans[qid]: true_pos += scores[qid] SCREAMING_SNAKE_CASE_ : Tuple = true_pos / float(i + 1 ) SCREAMING_SNAKE_CASE_ : List[str] = true_pos / float(lowerCAmelCase ) if i == len(lowerCAmelCase ) - 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(lowerCAmelCase ) recalls.append(lowerCAmelCase ) if out_image: plot_pr_curve(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) return {"ap": 100.0 * avg_prec} def _snake_case ( lowerCAmelCase : List[Any] , lowerCAmelCase : Any , lowerCAmelCase : List[Any] , lowerCAmelCase : str , lowerCAmelCase : Optional[Any] , lowerCAmelCase : Optional[int] ): """simple docstring""" if out_image_dir and not os.path.exists(lowerCAmelCase ): os.makedirs(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : int = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return SCREAMING_SNAKE_CASE_ : List[Any] = make_precision_recall_eval( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , out_image=os.path.join(lowerCAmelCase , "pr_exact.png" ) , title="Precision-Recall curve for Exact Match score" , ) SCREAMING_SNAKE_CASE_ : int = make_precision_recall_eval( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , out_image=os.path.join(lowerCAmelCase , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) SCREAMING_SNAKE_CASE_ : Dict = {k: float(lowerCAmelCase ) for k, v in qid_to_has_ans.items()} SCREAMING_SNAKE_CASE_ : Any = make_precision_recall_eval( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , out_image=os.path.join(lowerCAmelCase , "pr_oracle.png" ) , title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)" , ) merge_eval(lowerCAmelCase , lowerCAmelCase , "pr_exact" ) merge_eval(lowerCAmelCase , lowerCAmelCase , "pr_f1" ) merge_eval(lowerCAmelCase , lowerCAmelCase , "pr_oracle" ) def _snake_case ( lowerCAmelCase : Optional[int] , lowerCAmelCase : Tuple , lowerCAmelCase : Optional[Any] , lowerCAmelCase : List[Any] ): """simple docstring""" if not qid_list: return SCREAMING_SNAKE_CASE_ : int = [na_probs[k] for k in qid_list] SCREAMING_SNAKE_CASE_ : List[Any] = np.ones_like(lowerCAmelCase ) / float(len(lowerCAmelCase ) ) plt.hist(lowerCAmelCase , weights=lowerCAmelCase , bins=2_0 , 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(lowerCAmelCase , f'na_prob_hist_{name}.png' ) ) plt.clf() def _snake_case ( lowerCAmelCase : Dict , lowerCAmelCase : Union[str, Any] , lowerCAmelCase : int , lowerCAmelCase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) SCREAMING_SNAKE_CASE_ : List[str] = num_no_ans SCREAMING_SNAKE_CASE_ : List[str] = cur_score SCREAMING_SNAKE_CASE_ : List[Any] = 0.0 SCREAMING_SNAKE_CASE_ : List[str] = sorted(lowerCAmelCase , key=lambda lowerCAmelCase : na_probs[k] ) for i, qid in enumerate(lowerCAmelCase ): if qid not in scores: continue if qid_to_has_ans[qid]: SCREAMING_SNAKE_CASE_ : Tuple = scores[qid] else: if preds[qid]: SCREAMING_SNAKE_CASE_ : int = -1 else: SCREAMING_SNAKE_CASE_ : str = 0 cur_score += diff if cur_score > best_score: SCREAMING_SNAKE_CASE_ : Optional[int] = cur_score SCREAMING_SNAKE_CASE_ : str = na_probs[qid] return 100.0 * best_score / len(lowerCAmelCase ), best_thresh def _snake_case ( lowerCAmelCase : Any , lowerCAmelCase : str , lowerCAmelCase : List[str] , lowerCAmelCase : str , lowerCAmelCase : Union[str, Any] , lowerCAmelCase : Any ): """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = find_best_thresh(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = find_best_thresh(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : List[Any] = best_exact SCREAMING_SNAKE_CASE_ : Dict = exact_thresh SCREAMING_SNAKE_CASE_ : List[str] = best_fa SCREAMING_SNAKE_CASE_ : int = fa_thresh def _snake_case ( ): """simple docstring""" with open(OPTS.data_file ) as f: SCREAMING_SNAKE_CASE_ : int = json.load(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = dataset_json["data"] with open(OPTS.pred_file ) as f: SCREAMING_SNAKE_CASE_ : int = json.load(lowerCAmelCase ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: SCREAMING_SNAKE_CASE_ : Union[str, Any] = json.load(lowerCAmelCase ) else: SCREAMING_SNAKE_CASE_ : str = {k: 0.0 for k in preds} SCREAMING_SNAKE_CASE_ : str = make_qid_to_has_ans(lowerCAmelCase ) # maps qid to True/False SCREAMING_SNAKE_CASE_ : int = [k for k, v in qid_to_has_ans.items() if v] SCREAMING_SNAKE_CASE_ : List[str] = [k for k, v in qid_to_has_ans.items() if not v] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[int] = get_raw_scores(lowerCAmelCase , lowerCAmelCase ) SCREAMING_SNAKE_CASE_ : Tuple = apply_no_ans_threshold(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , OPTS.na_prob_thresh ) SCREAMING_SNAKE_CASE_ : Any = apply_no_ans_threshold(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , OPTS.na_prob_thresh ) SCREAMING_SNAKE_CASE_ : List[str] = make_eval_dict(lowerCAmelCase , lowerCAmelCase ) if has_ans_qids: SCREAMING_SNAKE_CASE_ : int = make_eval_dict(lowerCAmelCase , lowerCAmelCase , qid_list=lowerCAmelCase ) merge_eval(lowerCAmelCase , lowerCAmelCase , "HasAns" ) if no_ans_qids: SCREAMING_SNAKE_CASE_ : Union[str, Any] = make_eval_dict(lowerCAmelCase , lowerCAmelCase , qid_list=lowerCAmelCase ) merge_eval(lowerCAmelCase , lowerCAmelCase , "NoAns" ) if OPTS.na_prob_file: find_all_best_thresh(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , OPTS.out_image_dir ) histogram_na_prob(lowerCAmelCase , lowerCAmelCase , OPTS.out_image_dir , "hasAns" ) histogram_na_prob(lowerCAmelCase , lowerCAmelCase , OPTS.out_image_dir , "noAns" ) if OPTS.out_file: with open(OPTS.out_file , "w" ) as f: json.dump(lowerCAmelCase , lowerCAmelCase ) else: print(json.dumps(lowerCAmelCase , indent=2 ) ) if __name__ == "__main__": __lowerCamelCase : List[Any] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
18
"""simple docstring""" from typing import Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images from ...utils import TensorType, logging _lowercase : Tuple = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' _a = ['pixel_values'] def __init__( self : Optional[Any], lowerCamelCase : bool = True, lowerCamelCase : Union[int, float] = 1 / 255, lowerCamelCase : bool = True, lowerCamelCase : int = 8, **lowerCamelCase : Tuple, )-> None: super().__init__(**lowerCamelCase ) lowerCamelCase__ : int =do_rescale lowerCamelCase__ : Dict =rescale_factor lowerCamelCase__ : Union[str, Any] =do_pad lowerCamelCase__ : Union[str, Any] =pad_size def snake_case ( self : int, lowerCamelCase : np.ndarray, lowerCamelCase : float, lowerCamelCase : Optional[Union[str, ChannelDimension]] = None, **lowerCamelCase : int )-> np.ndarray: return rescale(lowerCamelCase, scale=lowerCamelCase, data_format=lowerCamelCase, **lowerCamelCase ) def snake_case ( self : Optional[Any], lowerCamelCase : np.ndarray, lowerCamelCase : int, lowerCamelCase : Optional[Union[str, ChannelDimension]] = None )-> List[Any]: lowerCamelCase__ , lowerCamelCase__ : Optional[int] =get_image_size(lowerCamelCase ) lowerCamelCase__ : List[str] =(old_height // size + 1) * size - old_height lowerCamelCase__ : List[str] =(old_width // size + 1) * size - old_width return pad(lowerCamelCase, ((0, pad_height), (0, pad_width)), mode='''symmetric''', data_format=lowerCamelCase ) def snake_case ( self : List[Any], lowerCamelCase : ImageInput, lowerCamelCase : Optional[bool] = None, lowerCamelCase : Optional[float] = None, lowerCamelCase : Optional[bool] = None, lowerCamelCase : Optional[int] = None, lowerCamelCase : Optional[Union[str, TensorType]] = None, lowerCamelCase : Union[str, ChannelDimension] = ChannelDimension.FIRST, **lowerCamelCase : Union[str, Any], )-> Dict: lowerCamelCase__ : List[str] =do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase__ : Tuple =rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase__ : str =do_pad if do_pad is not None else self.do_pad lowerCamelCase__ : int =pad_size if pad_size is not None else self.pad_size lowerCamelCase__ : Optional[int] =make_list_of_images(lowerCamelCase ) if not valid_images(lowerCamelCase ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) # All transformations expect numpy arrays. lowerCamelCase__ : Tuple =[to_numpy_array(lowerCamelCase ) for image in images] if do_rescale: lowerCamelCase__ : Tuple =[self.rescale(image=lowerCamelCase, scale=lowerCamelCase ) for image in images] if do_pad: lowerCamelCase__ : Tuple =[self.pad(lowerCamelCase, size=lowerCamelCase ) for image in images] lowerCamelCase__ : int =[to_channel_dimension_format(lowerCamelCase, lowerCamelCase ) for image in images] lowerCamelCase__ : Dict ={'''pixel_values''': images} return BatchFeature(data=lowerCamelCase, tensor_type=lowerCamelCase )
238
0
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, )
356
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _UpperCamelCase = { '''configuration_squeezebert''': [ '''SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SqueezeBertConfig''', '''SqueezeBertOnnxConfig''', ], '''tokenization_squeezebert''': ['''SqueezeBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCamelCase = ['''SqueezeBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCamelCase = [ '''SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SqueezeBertForMaskedLM''', '''SqueezeBertForMultipleChoice''', '''SqueezeBertForQuestionAnswering''', '''SqueezeBertForSequenceClassification''', '''SqueezeBertForTokenClassification''', '''SqueezeBertModel''', '''SqueezeBertModule''', '''SqueezeBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys _UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
335
0
import argparse import os import re import packaging.version SCREAMING_SNAKE_CASE :int = 'examples/' SCREAMING_SNAKE_CASE :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'), } SCREAMING_SNAKE_CASE :Optional[Any] = { 'init': 'src/transformers/__init__.py', 'setup': 'setup.py', } SCREAMING_SNAKE_CASE :Optional[Any] = 'README.md' def UpperCAmelCase ( a_ , a_ , a_ ) -> Optional[Any]: """simple docstring""" with open(a_ , "r" , encoding="utf-8" , newline="\n" ) as f: __A = f.read() __A , __A = REPLACE_PATTERNS[pattern] __A = replace.replace("VERSION" , a_ ) __A = re_pattern.sub(a_ , a_ ) with open(a_ , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(a_ ) def UpperCAmelCase ( a_ ) -> Optional[Any]: """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 UpperCAmelCase ( a_ , a_=False ) -> Any: """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 UpperCAmelCase ( ) -> List[Any]: """simple docstring""" __A = "🤗 Transformers currently provides the following architectures" __A = "1. Want to contribute a new model?" with open(a_ , "r" , encoding="utf-8" , newline="\n" ) as f: __A = f.readlines() # Find the start of the list. __A = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __A = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): __A = 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 UpperCAmelCase ( ) -> Dict: """simple docstring""" with open(REPLACE_FILES["init"] , "r" ) as f: __A = f.read() __A = REPLACE_PATTERNS["init"][0].search(a_ ).groups()[0] return packaging.version.parse(a_ ) def UpperCAmelCase ( a_=False ) -> Dict: """simple docstring""" __A = 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: __A = default_version.base_version elif patch: __A = F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: __A = F'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. __A = input(F'''Which version are you releasing? [{default_version}]''' ) if len(a_ ) == 0: __A = 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 UpperCAmelCase ( ) -> List[Any]: """simple docstring""" __A = get_version() __A = F'''{current_version.major}.{current_version.minor + 1}.0.dev0''' __A = current_version.base_version # Check with the user we got that right. __A = input(F'''Which version are we developing now? [{dev_version}]''' ) if len(a_ ) == 0: __A = 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__": SCREAMING_SNAKE_CASE :str = 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.') SCREAMING_SNAKE_CASE :Dict = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('Nothing to do after a patch :-)') else: post_release_work()
15
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version SCREAMING_SNAKE_CASE :Union[str, Any] = get_logger(__name__) class UpperCAmelCase : '''simple docstring''' snake_case_ = "dummy_data" snake_case_ = "datasets" snake_case_ = False def __init__( self : Optional[int] ,A : str ,A : str ,A : Union[Version, str] ,A : Optional[str] = None ,A : bool = False ,A : bool = True ,A : Optional[List[Callable]] = None ,): __A = 0 __A = dataset_name __A = cache_dir __A = use_local_dummy_data __A = config # download_callbacks take a single url as input __A = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __A = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __A = str(A ) # to be downloaded __A = None __A = None @property def UpperCamelCase_ ( self : Union[str, Any] ): if self._dummy_file is None: __A = self.download_dummy_data() return self._dummy_file @property def UpperCamelCase_ ( self : Optional[Any] ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("dummy" ,self.config.name ,self.version_name ) # structure is dummy / version_name return os.path.join("dummy" ,self.version_name ) @property def UpperCamelCase_ ( self : List[Any] ): return os.path.join(self.dummy_data_folder ,"dummy_data.zip" ) def UpperCamelCase_ ( self : Tuple ): __A = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __A = cached_path( A ,cache_dir=self.cache_dir ,extract_compressed_file=A ,force_extract=A ) return os.path.join(A ,self.dummy_file_name ) @property def UpperCamelCase_ ( self : str ): return os.path.join(self.datasets_scripts_dir ,self.dataset_name ,self.dummy_zip_file ) @property def UpperCamelCase_ ( self : Any ): if self._bucket_url is None: __A = hf_github_url(self.dataset_name ,self.dummy_zip_file.replace(os.sep ,"/" ) ) return self._bucket_url @property def UpperCamelCase_ ( self : Tuple ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep ,"/" ).split("/" )[:-1] ) def UpperCamelCase_ ( self : List[str] ,A : List[Any] ,*A : Dict ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __A = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __A = self.dummy_file_name # special case when data_url is a dict if isinstance(A ,A ): return self.create_dummy_data_dict(A ,A ) elif isinstance(A ,(list, tuple) ): return self.create_dummy_data_list(A ,A ) else: return self.create_dummy_data_single(A ,A ) def UpperCamelCase_ ( self : str ,A : List[Any] ,*A : List[Any] ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : List[str] ,A : List[str] ,A : Tuple ): return self.download_and_extract(A ) def UpperCamelCase_ ( self : Any ,A : Any ,*A : Optional[Any] ,**A : List[str] ): return path def UpperCamelCase_ ( self : str ): return {} def UpperCamelCase_ ( self : int ,A : int ,A : Tuple ): __A = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(A ,A ): for single_url in single_urls: download_callback(A ) else: __A = single_urls download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(A ,A ): __A = [os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) for x in single_urls] else: __A = single_urls __A = os.path.join(A ,urllib.parse.quote_plus(Path(A ).name ) ) __A = value # make sure that values are unique if all(isinstance(A ,A ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __A = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def UpperCamelCase_ ( self : Union[str, Any] ,A : str ,A : str ): __A = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __A = all(bool(re.findall("[0-9]{3,}-of-[0-9]{3,}" ,A ) ) for url in data_url ) __A = all( url.startswith("https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __A = [data_url[0]] * len(A ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(single_url.split("/" )[-1] ) ) dummy_data_list.append(A ) return dummy_data_list def UpperCamelCase_ ( self : str ,A : List[Any] ,A : Optional[Any] ): for download_callback in self.download_callbacks: download_callback(A ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __A = os.path.join(A ,urllib.parse.quote_plus(data_url.split("/" )[-1] ) ) if os.path.exists(A ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def UpperCamelCase_ ( self : int ): pass def UpperCamelCase_ ( self : Dict ): pass def UpperCamelCase_ ( self : Optional[Any] ,A : List[Any] ): def _iter_archive_members(A : Optional[Any] ): # this preserves the order of the members inside the ZIP archive __A = Path(self.dummy_file ).parent __A = path.relative_to(A ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __A = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(A ) __A = Path(A ) __A = _iter_archive_members(A ) if self.use_local_dummy_data else path.rglob("*" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((".", "__") ): yield file_path.relative_to(A ).as_posix(), file_path.open("rb" ) def UpperCamelCase_ ( self : List[Any] ,A : Any ): if not isinstance(A ,A ): __A = [paths] for path in paths: if os.path.isfile(A ): if os.path.basename(A ).startswith((".", "__") ): return yield path else: for dirpath, dirnames, filenames in os.walk(A ): if os.path.basename(A ).startswith((".", "__") ): continue dirnames.sort() for filename in sorted(A ): if filename.startswith((".", "__") ): continue yield os.path.join(A ,A )
15
1
"""simple docstring""" def UpperCAmelCase ( UpperCAmelCase ) -> set: snake_case_ = set() # edges = list of graph's edges snake_case_ = get_edges(UpperCAmelCase ) # While there are still elements in edges list, take an arbitrary edge # (from_node, to_node) and add his extremity to chosen_vertices and then # remove all arcs adjacent to the from_node and to_node while edges: snake_case_ , snake_case_ = edges.pop() chosen_vertices.add(UpperCAmelCase ) chosen_vertices.add(UpperCAmelCase ) for edge in edges.copy(): if from_node in edge or to_node in edge: edges.discard(UpperCAmelCase ) return chosen_vertices def UpperCAmelCase ( UpperCAmelCase ) -> set: snake_case_ = set() for from_node, to_nodes in graph.items(): for to_node in to_nodes: edges.add((from_node, to_node) ) return edges if __name__ == "__main__": import doctest doctest.testmod() # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")
312
"""simple docstring""" from ..utils import DummyObject, requires_backends class UpperCamelCase ( metaclass=lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = ["keras_nlp"] def __init__( self, *lowerCAmelCase__, **lowerCAmelCase__) -> int: requires_backends(self, ['keras_nlp'])
312
1
'''simple docstring''' def A_ ( snake_case , snake_case , snake_case ): def update_area_of_max_square(snake_case , snake_case ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 SCREAMING_SNAKE_CASE:Union[str, Any] = update_area_of_max_square(snake_case , col + 1 ) SCREAMING_SNAKE_CASE:List[str] = update_area_of_max_square(row + 1 , col + 1 ) SCREAMING_SNAKE_CASE:Optional[Any] = update_area_of_max_square(row + 1 , snake_case ) if mat[row][col]: SCREAMING_SNAKE_CASE:Any = 1 + min([right, diagonal, down] ) SCREAMING_SNAKE_CASE:Tuple = max(largest_square_area[0] , snake_case ) return sub_problem_sol else: return 0 SCREAMING_SNAKE_CASE:Optional[Any] = [0] update_area_of_max_square(0 , 0 ) return largest_square_area[0] def A_ ( snake_case , snake_case , snake_case ): def update_area_of_max_square_using_dp_array( snake_case , snake_case , snake_case ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] SCREAMING_SNAKE_CASE:List[str] = update_area_of_max_square_using_dp_array(snake_case , col + 1 , snake_case ) SCREAMING_SNAKE_CASE:List[str] = update_area_of_max_square_using_dp_array(row + 1 , col + 1 , snake_case ) SCREAMING_SNAKE_CASE:List[Any] = update_area_of_max_square_using_dp_array(row + 1 , snake_case , snake_case ) if mat[row][col]: SCREAMING_SNAKE_CASE:List[Any] = 1 + min([right, diagonal, down] ) SCREAMING_SNAKE_CASE:Optional[int] = max(largest_square_area[0] , snake_case ) SCREAMING_SNAKE_CASE:List[str] = sub_problem_sol return sub_problem_sol else: return 0 SCREAMING_SNAKE_CASE:int = [0] SCREAMING_SNAKE_CASE:Dict = [[-1] * cols for _ in range(snake_case )] update_area_of_max_square_using_dp_array(0 , 0 , snake_case ) return largest_square_area[0] def A_ ( snake_case , snake_case , snake_case ): SCREAMING_SNAKE_CASE:int = [[0] * (cols + 1) for _ in range(rows + 1 )] SCREAMING_SNAKE_CASE:Tuple = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): SCREAMING_SNAKE_CASE:Dict = dp_array[row][col + 1] SCREAMING_SNAKE_CASE:List[Any] = dp_array[row + 1][col + 1] SCREAMING_SNAKE_CASE:Optional[int] = dp_array[row + 1][col] if mat[row][col] == 1: SCREAMING_SNAKE_CASE:str = 1 + min(snake_case , snake_case , snake_case ) SCREAMING_SNAKE_CASE:Optional[Any] = max(dp_array[row][col] , snake_case ) else: SCREAMING_SNAKE_CASE:Optional[Any] = 0 return largest_square_area def A_ ( snake_case , snake_case , snake_case ): SCREAMING_SNAKE_CASE:Union[str, Any] = [0] * (cols + 1) SCREAMING_SNAKE_CASE:str = [0] * (cols + 1) SCREAMING_SNAKE_CASE:List[str] = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): SCREAMING_SNAKE_CASE:int = current_row[col + 1] SCREAMING_SNAKE_CASE:List[Any] = next_row[col + 1] SCREAMING_SNAKE_CASE:Union[str, Any] = next_row[col] if mat[row][col] == 1: SCREAMING_SNAKE_CASE:str = 1 + min(snake_case , snake_case , snake_case ) SCREAMING_SNAKE_CASE:List[str] = max(current_row[col] , snake_case ) else: SCREAMING_SNAKE_CASE:int = 0 SCREAMING_SNAKE_CASE:List[Any] = current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
139
'''simple docstring''' from __future__ import annotations import requests def _A ( snake_case ) -> dict: _lowercase : Dict = F'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty''' return requests.get(snake_case ).json() def _A ( snake_case = 10 ) -> list[dict]: _lowercase : List[Any] = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" _lowercase : List[str] = requests.get(snake_case ).json()[:max_stories] return [get_hackernews_story(snake_case ) for story_id in story_ids] def _A ( snake_case = 10 ) -> str: _lowercase : Union[str, Any] = hackernews_top_stories(snake_case ) return "\n".join("* [{title}]({url})".format(**snake_case ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
250
0
"""simple docstring""" import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class lowerCamelCase__ ( unittest.TestCase ): def _UpperCamelCase ( self ): UpperCAmelCase = """ZinengTang/tvlt-base""" UpperCAmelCase = tempfile.mkdtemp() def _UpperCamelCase ( self ,**A ): return TvltImageProcessor.from_pretrained(self.checkpoint ,**A ) def _UpperCamelCase ( self ,**A ): return TvltFeatureExtractor.from_pretrained(self.checkpoint ,**A ) def _UpperCamelCase ( self ): shutil.rmtree(self.tmpdirname ) def _UpperCamelCase ( self ): UpperCAmelCase = self.get_image_processor() UpperCAmelCase = self.get_feature_extractor() UpperCAmelCase = TvltProcessor(image_processor=A ,feature_extractor=A ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase = TvltProcessor.from_pretrained(self.tmpdirname ) self.assertIsInstance(processor.feature_extractor ,A ) self.assertIsInstance(processor.image_processor ,A ) def _UpperCamelCase ( self ): UpperCAmelCase = self.get_image_processor() UpperCAmelCase = self.get_feature_extractor() UpperCAmelCase = TvltProcessor(image_processor=A ,feature_extractor=A ) UpperCAmelCase = np.ones([12_000] ) UpperCAmelCase = feature_extractor(A ,return_tensors="""np""" ) UpperCAmelCase = processor(audio=A ,return_tensors="""np""" ) for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() ,input_processor[key].sum() ,delta=1e-2 ) def _UpperCamelCase ( self ): UpperCAmelCase = self.get_image_processor() UpperCAmelCase = self.get_feature_extractor() UpperCAmelCase = TvltProcessor(image_processor=A ,feature_extractor=A ) UpperCAmelCase = np.ones([3, 224, 224] ) UpperCAmelCase = image_processor(A ,return_tensors="""np""" ) UpperCAmelCase = processor(images=A ,return_tensors="""np""" ) for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() ,input_processor[key].sum() ,delta=1e-2 ) def _UpperCamelCase ( self ): UpperCAmelCase = self.get_image_processor() UpperCAmelCase = self.get_feature_extractor() UpperCAmelCase = TvltProcessor(image_processor=A ,feature_extractor=A ) UpperCAmelCase = np.ones([12_000] ) UpperCAmelCase = np.ones([3, 224, 224] ) UpperCAmelCase = processor(audio=A ,images=A ) self.assertListEqual(list(inputs.keys() ) ,["""audio_values""", """audio_mask""", """pixel_values""", """pixel_mask"""] ) # test if it raises when no input is passed with pytest.raises(A ): processor() def _UpperCamelCase ( self ): UpperCAmelCase = self.get_image_processor() UpperCAmelCase = self.get_feature_extractor() UpperCAmelCase = TvltProcessor(image_processor=A ,feature_extractor=A ) self.assertListEqual( processor.model_input_names ,image_processor.model_input_names + feature_extractor.model_input_names ,msg="""`processor` and `image_processor`+`feature_extractor` model input names do not match""" ,)
359
"""simple docstring""" def _a ( _snake_case = 6008_5147_5143 ): """simple docstring""" try: UpperCAmelCase = int(_snake_case ) except (TypeError, ValueError): raise TypeError("""Parameter n must be int or castable to int.""" ) if n <= 0: raise ValueError("""Parameter n must be greater than or equal to one.""" ) UpperCAmelCase = 1 UpperCAmelCase = 2 while i * i <= n: while n % i == 0: UpperCAmelCase = i n //= i i += 1 if n > 1: UpperCAmelCase = n return int(_snake_case ) if __name__ == "__main__": print(F"""{solution() = }""")
234
0
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class _UpperCAmelCase : def __init__( self , _A , _A=13 , _A=7 , _A=True , _A=True , _A=True , _A=True , _A=99 , _A=32 , _A=2 , _A=4 , _A=37 , _A="gelu" , _A=0.1 , _A=0.1 , _A=5_12 , _A=16 , _A=2 , _A=0.02 , _A=3 , _A=4 , _A=None , ) -> Tuple: '''simple docstring''' _UpperCAmelCase : str = parent _UpperCAmelCase : str = 13 _UpperCAmelCase : int = 7 _UpperCAmelCase : Any = True _UpperCAmelCase : Union[str, Any] = True _UpperCAmelCase : int = True _UpperCAmelCase : Union[str, Any] = True _UpperCAmelCase : Dict = 99 _UpperCAmelCase : int = 3_84 _UpperCAmelCase : Tuple = 2 _UpperCAmelCase : List[Any] = 4 _UpperCAmelCase : List[str] = 37 _UpperCAmelCase : str = """gelu""" _UpperCAmelCase : Any = 0.1 _UpperCAmelCase : int = 0.1 _UpperCAmelCase : str = 5_12 _UpperCAmelCase : int = 16 _UpperCAmelCase : List[str] = 2 _UpperCAmelCase : Tuple = 0.02 _UpperCAmelCase : Tuple = 3 _UpperCAmelCase : Dict = 4 _UpperCAmelCase : List[Any] = 1_28 _UpperCAmelCase : Optional[Any] = 2 _UpperCAmelCase : List[Any] = 9 _UpperCAmelCase : Dict = 1 _UpperCAmelCase : Dict = None def __snake_case ( self ) -> List[Any]: '''simple docstring''' _UpperCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : Any = None if self.use_input_mask: _UpperCAmelCase : Dict = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase : Tuple = None if self.use_token_type_ids: _UpperCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _UpperCAmelCase : Tuple = None _UpperCAmelCase : Any = None _UpperCAmelCase : List[Any] = None if self.use_labels: _UpperCAmelCase : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCAmelCase : Tuple = ids_tensor([self.batch_size] , self.num_choices ) _UpperCAmelCase : List[str] = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=_A , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __snake_case ( self , _A , _A , _A , _A , _A , _A , _A ) -> int: '''simple docstring''' _UpperCAmelCase : int = TFConvBertModel(config=_A ) _UpperCAmelCase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} _UpperCAmelCase : List[Any] = [input_ids, input_mask] _UpperCAmelCase : List[str] = model(_A ) _UpperCAmelCase : Optional[Any] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case ( self , _A , _A , _A , _A , _A , _A , _A ) -> List[str]: '''simple docstring''' _UpperCAmelCase : Optional[Any] = TFConvBertForMaskedLM(config=_A ) _UpperCAmelCase : Any = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _UpperCAmelCase : Any = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __snake_case ( self , _A , _A , _A , _A , _A , _A , _A ) -> Optional[int]: '''simple docstring''' _UpperCAmelCase : str = self.num_labels _UpperCAmelCase : int = TFConvBertForSequenceClassification(config=_A ) _UpperCAmelCase : Tuple = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _UpperCAmelCase : List[str] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __snake_case ( self , _A , _A , _A , _A , _A , _A , _A ) -> str: '''simple docstring''' _UpperCAmelCase : Dict = self.num_choices _UpperCAmelCase : Optional[int] = TFConvBertForMultipleChoice(config=_A ) _UpperCAmelCase : Optional[int] = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase : str = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase : int = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) _UpperCAmelCase : Optional[Any] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } _UpperCAmelCase : List[str] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __snake_case ( self , _A , _A , _A , _A , _A , _A , _A ) -> Optional[Any]: '''simple docstring''' _UpperCAmelCase : Any = self.num_labels _UpperCAmelCase : Union[str, Any] = TFConvBertForTokenClassification(config=_A ) _UpperCAmelCase : Optional[Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _UpperCAmelCase : Optional[Any] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __snake_case ( self , _A , _A , _A , _A , _A , _A , _A ) -> str: '''simple docstring''' _UpperCAmelCase : Optional[int] = TFConvBertForQuestionAnswering(config=_A ) _UpperCAmelCase : Optional[int] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } _UpperCAmelCase : int = model(_A ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __snake_case ( self ) -> Any: '''simple docstring''' _UpperCAmelCase : Optional[Any] = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) : Any = config_and_inputs _UpperCAmelCase : List[str] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _UpperCAmelCase ( __a , __a , unittest.TestCase): __a : str = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) __a : List[str] = ( { """feature-extraction""": TFConvBertModel, """fill-mask""": TFConvBertForMaskedLM, """question-answering""": TFConvBertForQuestionAnswering, """text-classification""": TFConvBertForSequenceClassification, """token-classification""": TFConvBertForTokenClassification, """zero-shot""": TFConvBertForSequenceClassification, } if is_tf_available() else {} ) __a : Optional[Any] = False __a : int = False __a : List[str] = False def __snake_case ( self ) -> List[str]: '''simple docstring''' _UpperCAmelCase : List[str] = TFConvBertModelTester(self ) _UpperCAmelCase : Optional[int] = ConfigTester(self , config_class=_A , hidden_size=37 ) def __snake_case ( self ) -> Dict: '''simple docstring''' self.config_tester.run_common_tests() def __snake_case ( self ) -> Tuple: '''simple docstring''' _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def __snake_case ( self ) -> Tuple: '''simple docstring''' _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_A ) def __snake_case ( self ) -> str: '''simple docstring''' _UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_A ) def __snake_case ( self ) -> int: '''simple docstring''' _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_A ) def __snake_case ( self ) -> Dict: '''simple docstring''' _UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_A ) def __snake_case ( self ) -> Optional[Any]: '''simple docstring''' _UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_A ) @slow def __snake_case ( self ) -> int: '''simple docstring''' _UpperCAmelCase , _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : Any = True _UpperCAmelCase : Union[str, Any] = True if hasattr(_A , """use_cache""" ): _UpperCAmelCase : str = True _UpperCAmelCase : Union[str, Any] = getattr(self.model_tester , """encoder_seq_length""" , self.model_tester.seq_length ) _UpperCAmelCase : Dict = getattr(self.model_tester , """key_length""" , _A ) for model_class in self.all_model_classes: _UpperCAmelCase : Union[str, Any] = self._prepare_for_class(_A , _A ) _UpperCAmelCase : List[str] = model_class(_A ) _UpperCAmelCase : Optional[Any] = len(model(_A ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_A , saved_model=_A ) _UpperCAmelCase : Optional[Any] = os.path.join(_A , """saved_model""" , """1""" ) _UpperCAmelCase : Optional[int] = tf.keras.models.load_model(_A ) _UpperCAmelCase : Dict = model(_A ) if self.is_encoder_decoder: _UpperCAmelCase : Dict = outputs["""encoder_hidden_states"""] _UpperCAmelCase : Dict = outputs["""encoder_attentions"""] else: _UpperCAmelCase : Union[str, Any] = outputs["""hidden_states"""] _UpperCAmelCase : Tuple = outputs["""attentions"""] self.assertEqual(len(_A ) , _A ) _UpperCAmelCase : Optional[int] = getattr( self.model_tester , """expected_num_hidden_layers""" , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(_A ) , _A ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(_A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def __snake_case ( self ) -> List[str]: '''simple docstring''' _UpperCAmelCase : Optional[Any] = TFConvBertModel.from_pretrained("""YituTech/conv-bert-base""" ) self.assertIsNotNone(_A ) def __snake_case ( self ) -> Tuple: '''simple docstring''' _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : str = True _UpperCAmelCase : int = getattr(self.model_tester , """decoder_seq_length""" , self.model_tester.seq_length ) _UpperCAmelCase : Any = getattr(self.model_tester , """encoder_seq_length""" , self.model_tester.seq_length ) _UpperCAmelCase : Union[str, Any] = getattr(self.model_tester , """key_length""" , _A ) _UpperCAmelCase : int = getattr(self.model_tester , """key_length""" , _A ) def check_decoder_attentions_output(_A ): _UpperCAmelCase : List[Any] = len(_A ) self.assertEqual(out_len % 2 , 0 ) _UpperCAmelCase : Tuple = outputs.decoder_attentions self.assertEqual(len(_A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(_A ): _UpperCAmelCase : Union[str, Any] = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(_A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: _UpperCAmelCase : List[Any] = True _UpperCAmelCase : Dict = False _UpperCAmelCase : List[Any] = model_class(_A ) _UpperCAmelCase : Optional[Any] = model(self._prepare_for_class(_A , _A ) ) _UpperCAmelCase : Optional[int] = len(_A ) self.assertEqual(config.output_hidden_states , _A ) check_encoder_attentions_output(_A ) if self.is_encoder_decoder: _UpperCAmelCase : Dict = model_class(_A ) _UpperCAmelCase : Optional[Any] = model(self._prepare_for_class(_A , _A ) ) self.assertEqual(config.output_hidden_states , _A ) check_decoder_attentions_output(_A ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] _UpperCAmelCase : Tuple = True _UpperCAmelCase : List[Any] = model_class(_A ) _UpperCAmelCase : Tuple = model(self._prepare_for_class(_A , _A ) ) self.assertEqual(config.output_hidden_states , _A ) check_encoder_attentions_output(_A ) # Check attention is always last and order is fine _UpperCAmelCase : Optional[Any] = True _UpperCAmelCase : Union[str, Any] = True _UpperCAmelCase : str = model_class(_A ) _UpperCAmelCase : Union[str, Any] = model(self._prepare_for_class(_A , _A ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(_A ) ) self.assertEqual(model.config.output_hidden_states , _A ) check_encoder_attentions_output(_A ) @require_tf class _UpperCAmelCase ( unittest.TestCase): @slow def __snake_case ( self ) -> int: '''simple docstring''' _UpperCAmelCase : Any = TFConvBertModel.from_pretrained("""YituTech/conv-bert-base""" ) _UpperCAmelCase : Dict = tf.constant([[0, 1, 2, 3, 4, 5]] ) _UpperCAmelCase : str = model(_A )[0] _UpperCAmelCase : Optional[Any] = [1, 6, 7_68] self.assertEqual(output.shape , _A ) _UpperCAmelCase : Dict = tf.constant( [ [ [-0.03475493, -0.4686034, -0.30638832], [0.22637248, -0.26988646, -0.7423424], [0.10324868, -0.45013508, -0.58280784], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , _A , atol=1e-4 )
246
"""simple docstring""" def UpperCamelCase ( _lowerCAmelCase : int ) -> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 _UpperCAmelCase : Optional[Any] = 1 _UpperCAmelCase : List[str] = 1 while repunit: _UpperCAmelCase : Tuple = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def UpperCamelCase ( _lowerCAmelCase : int = 1000000 ) -> int: _UpperCAmelCase : Any = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(_lowerCAmelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(F'''{solution() = }''')
246
1
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType _lowerCamelCase : List[Any] = logging.get_logger(__name__) _lowerCamelCase : str = { "microsoft/layoutlmv3-base": "https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json", } class __snake_case (_a ): lowerCAmelCase__ = "layoutlmv3" def __init__( self : Union[str, Any] , _UpperCAmelCase : List[str]=5_0265 , _UpperCAmelCase : Optional[int]=768 , _UpperCAmelCase : Union[str, Any]=12 , _UpperCAmelCase : Any=12 , _UpperCAmelCase : Union[str, Any]=3072 , _UpperCAmelCase : Any="gelu" , _UpperCAmelCase : Optional[int]=0.1 , _UpperCAmelCase : List[Any]=0.1 , _UpperCAmelCase : str=512 , _UpperCAmelCase : str=2 , _UpperCAmelCase : Optional[int]=0.02 , _UpperCAmelCase : int=1E-5 , _UpperCAmelCase : Optional[Any]=1 , _UpperCAmelCase : Union[str, Any]=0 , _UpperCAmelCase : Dict=2 , _UpperCAmelCase : int=1024 , _UpperCAmelCase : str=128 , _UpperCAmelCase : List[Any]=128 , _UpperCAmelCase : int=True , _UpperCAmelCase : Union[str, Any]=32 , _UpperCAmelCase : int=128 , _UpperCAmelCase : Union[str, Any]=64 , _UpperCAmelCase : str=256 , _UpperCAmelCase : List[Any]=True , _UpperCAmelCase : Dict=True , _UpperCAmelCase : Optional[int]=True , _UpperCAmelCase : str=224 , _UpperCAmelCase : Tuple=3 , _UpperCAmelCase : Optional[Any]=16 , _UpperCAmelCase : int=None , **_UpperCAmelCase : Any , ) -> str: '''simple docstring''' super().__init__( vocab_size=_UpperCAmelCase , hidden_size=_UpperCAmelCase , num_hidden_layers=_UpperCAmelCase , num_attention_heads=_UpperCAmelCase , intermediate_size=_UpperCAmelCase , hidden_act=_UpperCAmelCase , hidden_dropout_prob=_UpperCAmelCase , attention_probs_dropout_prob=_UpperCAmelCase , max_position_embeddings=_UpperCAmelCase , type_vocab_size=_UpperCAmelCase , initializer_range=_UpperCAmelCase , layer_norm_eps=_UpperCAmelCase , pad_token_id=_UpperCAmelCase , bos_token_id=_UpperCAmelCase , eos_token_id=_UpperCAmelCase , **_UpperCAmelCase , ) _lowerCAmelCase : Any = max_ad_position_embeddings _lowerCAmelCase : Optional[Any] = coordinate_size _lowerCAmelCase : Optional[int] = shape_size _lowerCAmelCase : Optional[Any] = has_relative_attention_bias _lowerCAmelCase : List[Any] = rel_pos_bins _lowerCAmelCase : Any = max_rel_pos _lowerCAmelCase : Optional[int] = has_spatial_attention_bias _lowerCAmelCase : Union[str, Any] = rel_ad_pos_bins _lowerCAmelCase : Union[str, Any] = max_rel_ad_pos _lowerCAmelCase : Dict = text_embed _lowerCAmelCase : Union[str, Any] = visual_embed _lowerCAmelCase : Tuple = input_size _lowerCAmelCase : Union[str, Any] = num_channels _lowerCAmelCase : Any = patch_size _lowerCAmelCase : Optional[int] = classifier_dropout class __snake_case (_a ): lowerCAmelCase__ = version.parse("1.12" ) @property def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> float: '''simple docstring''' return 1E-5 @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: '''simple docstring''' return 12 def SCREAMING_SNAKE_CASE ( self : List[str] , _UpperCAmelCase : "ProcessorMixin" , _UpperCAmelCase : int = -1 , _UpperCAmelCase : int = -1 , _UpperCAmelCase : bool = False , _UpperCAmelCase : Optional["TensorType"] = None , _UpperCAmelCase : int = 3 , _UpperCAmelCase : int = 40 , _UpperCAmelCase : int = 40 , ) -> Mapping[str, Any]: '''simple docstring''' setattr(processor.image_processor , """apply_ocr""" , _UpperCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX _lowerCAmelCase : List[Any] = compute_effective_axis_dimension( _UpperCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX _lowerCAmelCase : List[Any] = processor.tokenizer.num_special_tokens_to_add(_UpperCAmelCase ) _lowerCAmelCase : Dict = compute_effective_axis_dimension( _UpperCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=_UpperCAmelCase ) # Generate dummy inputs according to compute batch and sequence _lowerCAmelCase : Optional[Any] = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes _lowerCAmelCase : int = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) _lowerCAmelCase : Tuple = self._generate_dummy_images(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) _lowerCAmelCase : Tuple = dict( processor( _UpperCAmelCase , text=_UpperCAmelCase , boxes=_UpperCAmelCase , return_tensors=_UpperCAmelCase , ) ) return inputs
358
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) _lowerCamelCase : List[str] = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } _lowerCamelCase : Dict = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def _UpperCAmelCase (UpperCamelCase_ : List[Any] , UpperCamelCase_ : Dict , UpperCamelCase_ : int , UpperCamelCase_ : Union[str, Any] , UpperCamelCase_ : int ): '''simple docstring''' for attribute in key.split(""".""" ): _lowerCAmelCase : Optional[int] = getattr(UpperCamelCase_ , UpperCamelCase_ ) if weight_type is not None: _lowerCAmelCase : Optional[Any] = getattr(UpperCamelCase_ , UpperCamelCase_ ).shape else: _lowerCAmelCase : Any = hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": _lowerCAmelCase : Tuple = value elif weight_type == "weight_g": _lowerCAmelCase : Optional[int] = value elif weight_type == "weight_v": _lowerCAmelCase : str = value elif weight_type == "bias": _lowerCAmelCase : str = value else: _lowerCAmelCase : Tuple = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def _UpperCAmelCase (UpperCamelCase_ : List[Any] , UpperCamelCase_ : Union[str, Any] ): '''simple docstring''' _lowerCAmelCase : List[str] = [] _lowerCAmelCase : Tuple = fairseq_model.state_dict() _lowerCAmelCase : str = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight _lowerCAmelCase : Any = None for name, value in fairseq_dict.items(): _lowerCAmelCase : Union[str, Any] = False if "conv_layers" in name: load_conv_layer( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , hf_model.config.feat_extract_norm == """group""" , ) _lowerCAmelCase : Any = True elif name.split(""".""" )[0] == "proj": _lowerCAmelCase : Union[str, Any] = fairseq_model.proj _lowerCAmelCase : Optional[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: _lowerCAmelCase : Union[str, Any] = True if "*" in mapped_key: _lowerCAmelCase : Union[str, Any] = name.split(UpperCamelCase_ )[0].split(""".""" )[-2] _lowerCAmelCase : Optional[Any] = mapped_key.replace("""*""" , UpperCamelCase_ ) if "weight_g" in name: _lowerCAmelCase : List[str] = """weight_g""" elif "weight_v" in name: _lowerCAmelCase : Tuple = """weight_v""" elif "bias" in name: _lowerCAmelCase : Dict = """bias""" elif "weight" in name: _lowerCAmelCase : Optional[int] = """weight""" else: _lowerCAmelCase : Optional[Any] = None set_recursively(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) continue if not is_used: unused_weights.append(UpperCamelCase_ ) logger.warning(F"Unused weights: {unused_weights}" ) return proj_weight def _UpperCAmelCase (UpperCamelCase_ : Any , UpperCamelCase_ : Optional[Any] , UpperCamelCase_ : str , UpperCamelCase_ : Tuple , UpperCamelCase_ : int ): '''simple docstring''' _lowerCAmelCase : List[str] = full_name.split("""conv_layers.""" )[-1] _lowerCAmelCase : Optional[int] = name.split(""".""" ) _lowerCAmelCase : List[str] = int(items[0] ) _lowerCAmelCase : Dict = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) _lowerCAmelCase : List[Any] = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) _lowerCAmelCase : Any = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) _lowerCAmelCase : Tuple = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) _lowerCAmelCase : Union[str, Any] = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(UpperCamelCase_ ) def _UpperCAmelCase (UpperCamelCase_ : int ): '''simple docstring''' _lowerCAmelCase , _lowerCAmelCase : Optional[int] = emb.weight.shape _lowerCAmelCase : Union[str, Any] = nn.Linear(UpperCamelCase_ , UpperCamelCase_ , bias=UpperCamelCase_ ) _lowerCAmelCase : List[Any] = emb.weight.data return lin_layer def _UpperCAmelCase (UpperCamelCase_ : Any ): '''simple docstring''' with open(UpperCamelCase_ , """r""" , encoding="""utf-8""" ) as f: _lowerCAmelCase : Optional[int] = f.readlines() _lowerCAmelCase : Dict = [line.split(""" """ )[0] for line in lines] _lowerCAmelCase : Dict = len(UpperCamelCase_ ) _lowerCAmelCase : Optional[int] = { """<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3, } vocab_dict.update(dict(zip(UpperCamelCase_ , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def _UpperCAmelCase (UpperCamelCase_ : List[Any] , UpperCamelCase_ : Optional[Any] , UpperCamelCase_ : Optional[Any] , UpperCamelCase_ : Any , UpperCamelCase_ : List[Any] , UpperCamelCase_ : Union[str, Any] , UpperCamelCase_ : List[str] , ): '''simple docstring''' _lowerCAmelCase : Any = WavaVecaConfig.from_pretrained(UpperCamelCase_ ) _lowerCAmelCase : Union[str, Any] = SpeechaTextaConfig.from_pretrained( UpperCamelCase_ , vocab_size=UpperCamelCase_ , decoder_layers=UpperCamelCase_ , do_stable_layer_norm=UpperCamelCase_ ) _lowerCAmelCase : List[str] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=UpperCamelCase_ , return_attention_mask=UpperCamelCase_ , ) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : str = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) _lowerCAmelCase : Tuple = model[0].eval() # set weights for wav2vec2 encoder _lowerCAmelCase : Union[str, Any] = WavaVecaModel(UpperCamelCase_ ) _lowerCAmelCase : Union[str, Any] = recursively_load_weights_wavaveca(model.encoder , UpperCamelCase_ ) _lowerCAmelCase : List[str] = SpeechaTextaForCausalLM(UpperCamelCase_ ) _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=UpperCamelCase_ ) # set output linear layer unexpected_keys.remove("""embed_out""" ) _lowerCAmelCase : Dict = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(F"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(F"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) _lowerCAmelCase : List[Any] = SpeechEncoderDecoderModel(encoder=UpperCamelCase_ , decoder=UpperCamelCase_ ) _lowerCAmelCase : Any = False # add projection layer _lowerCAmelCase : List[Any] = nn.Parameter(projection_layer.weight ) _lowerCAmelCase : Union[str, Any] = nn.Parameter(projection_layer.bias ) _lowerCAmelCase : Any = create_vocab_dict(UpperCamelCase_ ) with open(os.path.join(UpperCamelCase_ , """vocab.json""" ) , """w""" ) as fp: json.dump(UpperCamelCase_ , UpperCamelCase_ ) _lowerCAmelCase : Any = SpeechaTextaTokenizer(os.path.join(UpperCamelCase_ , """vocab.json""" ) ) tokenizer.save_pretrained(UpperCamelCase_ ) _lowerCAmelCase : str = hf_wavavec.config.to_dict() _lowerCAmelCase : Any = tokenizer.pad_token_id _lowerCAmelCase : List[str] = tokenizer.bos_token_id _lowerCAmelCase : Any = tokenizer.eos_token_id _lowerCAmelCase : Union[str, Any] = """speech_to_text_2""" _lowerCAmelCase : Any = """wav2vec2""" _lowerCAmelCase : str = SpeechEncoderDecoderConfig.from_dict(UpperCamelCase_ ) hf_wavavec.save_pretrained(UpperCamelCase_ ) feature_extractor.save_pretrained(UpperCamelCase_ ) if __name__ == "__main__": _lowerCamelCase : int = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument( "--encoder_config_path", default="facebook/wav2vec2-large-lv60", type=str, help="Path to hf encoder wav2vec2 checkpoint config", ) parser.add_argument( "--decoder_config_path", default="facebook/s2t-small-mustc-en-fr-st", type=str, help="Path to hf decoder s2t checkpoint config", ) parser.add_argument("--vocab_size", default=1_0_2_2_4, type=int, help="Vocab size of decoder") parser.add_argument("--num_decoder_layers", default=7, type=int, help="Number of decoder layers") _lowerCamelCase : Optional[Any] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
159
0
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
86
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class a ( unittest.TestCase ): """simple docstring""" def __init__( self: Optional[Any] , UpperCamelCase: Any , UpperCamelCase: Optional[int]=7 , UpperCamelCase: str=3 , UpperCamelCase: int=30 , UpperCamelCase: int=4_00 , UpperCamelCase: Union[str, Any]=True , UpperCamelCase: Tuple=None , UpperCamelCase: Any=True , UpperCamelCase: int=[0.5, 0.5, 0.5] , UpperCamelCase: Any=[0.5, 0.5, 0.5] , UpperCamelCase: Optional[Any]=True , UpperCamelCase: List[Any]=1 / 2_55 , UpperCamelCase: Tuple=True , ): """simple docstring""" A__ = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33} A__ = parent A__ = batch_size A__ = num_channels A__ = min_resolution A__ = max_resolution A__ = do_resize A__ = size A__ = do_normalize A__ = image_mean A__ = image_std A__ = do_rescale A__ = rescale_factor A__ = do_pad def UpperCamelCase ( self: Optional[Any] ): """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCamelCase ( self: Any , UpperCamelCase: List[str] , UpperCamelCase: int=False ): """simple docstring""" if not batched: A__ = image_inputs[0] if isinstance(UpperCamelCase , Image.Image ): A__ , A__ = image.size else: A__ , A__ = image.shape[1], image.shape[2] if w < h: A__ = int(self.size["""shortest_edge"""] * h / w ) A__ = self.size["""shortest_edge"""] elif w > h: A__ = self.size["""shortest_edge"""] A__ = int(self.size["""shortest_edge"""] * w / h ) else: A__ = self.size["""shortest_edge"""] A__ = self.size["""shortest_edge"""] else: A__ = [] for image in image_inputs: A__ , A__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[0] )[0] A__ = max(UpperCamelCase , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class a ( _lowerCamelCase, unittest.TestCase ): """simple docstring""" UpperCAmelCase = YolosImageProcessor if is_vision_available() else None def UpperCamelCase ( self: Optional[int] ): """simple docstring""" A__ = YolosImageProcessingTester(self ) @property def UpperCamelCase ( self: Optional[int] ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase ( self: Union[str, Any] ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase , """image_mean""" ) ) self.assertTrue(hasattr(UpperCamelCase , """image_std""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """do_resize""" ) ) self.assertTrue(hasattr(UpperCamelCase , """size""" ) ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 13_33} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) A__ = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=UpperCamelCase ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84} ) self.assertEqual(image_processor.do_pad , UpperCamelCase ) def UpperCamelCase ( self: str ): """simple docstring""" pass def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , Image.Image ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: Tuple ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , numpify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , np.ndarray ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test not batched input A__ = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A__ = image_processing(UpperCamelCase , return_tensors="""pt""" ).pixel_values A__ , A__ = self.image_processor_tester.get_expected_values(UpperCamelCase , batched=UpperCamelCase ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCamelCase ( self: str ): """simple docstring""" A__ = self.image_processing_class(**self.image_processor_dict ) A__ = self.image_processing_class(do_resize=UpperCamelCase , do_normalize=UpperCamelCase , do_rescale=UpperCamelCase ) # create random PyTorch tensors A__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase , torchify=UpperCamelCase ) for image in image_inputs: self.assertIsInstance(UpperCamelCase , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors A__ = image_processing_a.pad(UpperCamelCase , return_tensors="""pt""" ) A__ = image_processing_a(UpperCamelCase , return_tensors="""pt""" ) self.assertTrue( torch.allclose(encoded_images_with_method["""pixel_values"""] , encoded_images["""pixel_values"""] , atol=1e-4 ) ) @slow def UpperCamelCase ( self: str ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""image_id""": 3_97_69, """annotations""": target} # encode them A__ = YolosImageProcessor.from_pretrained("""hustvl/yolos-small""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) ) @slow def UpperCamelCase ( self: int ): """simple docstring""" A__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""" ) as f: A__ = json.loads(f.read() ) A__ = {"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target} A__ = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" ) # encode them A__ = YolosImageProcessor(format="""coco_panoptic""" ) A__ = image_processing(images=UpperCamelCase , annotations=UpperCamelCase , masks_path=UpperCamelCase , return_tensors="""pt""" ) # verify pixel values A__ = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding["""pixel_values"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , UpperCamelCase , atol=1e-4 ) ) # verify area A__ = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , UpperCamelCase ) ) # verify boxes A__ = torch.Size([6, 4] ) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , UpperCamelCase ) A__ = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , UpperCamelCase , atol=1e-3 ) ) # verify image_id A__ = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , UpperCamelCase ) ) # verify is_crowd A__ = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , UpperCamelCase ) ) # verify class_labels A__ = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , UpperCamelCase ) ) # verify masks A__ = 82_28_73 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , UpperCamelCase ) # verify orig_size A__ = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , UpperCamelCase ) ) # verify size A__ = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , UpperCamelCase ) )
335
0
"""simple docstring""" import math def snake_case( __magic_name__ ) -> Optional[Any]: '''simple docstring''' if not isinstance(__lowerCamelCase , __lowerCamelCase ): lowercase : Optional[int] = F"""Input value of [number={number}] must be an integer""" raise TypeError(__lowerCamelCase ) if number < 1: lowercase : Optional[int] = F"""Input value of [number={number}] must be > 0""" raise ValueError(__lowerCamelCase ) elif number == 1: return 3 elif number == 2: return 5 else: lowercase : Dict = int(math.log(number // 3 , 2 ) ) + 2 lowercase : str = [3, 5] lowercase : Union[str, Any] = 2 lowercase : Any = 3 for block in range(1 , __lowerCamelCase ): for _ in range(__lowerCamelCase ): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] ) proth_index += 1 increment *= 2 return proth_list[number - 1] if __name__ == "__main__": import doctest doctest.testmod() for number in range(11): lowerCAmelCase_ = 0 try: lowerCAmelCase_ = proth(number) except ValueError: print(f'''ValueError: there is no {number}th Proth number''') continue print(f'''The {number}th Proth number: {value}''')
360
from sklearn.metrics import mean_squared_error import datasets lowerCAmelCase_ = '\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n' lowerCAmelCase_ = '\\nMean Squared Error(MSE) is the average of the square of difference between the predicted\nand actual values.\n' lowerCAmelCase_ = '\nArgs:\n predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Estimated target values.\n references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Ground truth (correct) target values.\n sample_weight: array-like of shape (n_samples,), default=None\n Sample weights.\n multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"\n Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n "raw_values" : Returns a full set of errors in case of multioutput input.\n\n "uniform_average" : Errors of all outputs are averaged with uniform weight.\n\n squared : bool, default=True\n If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.\n\nReturns:\n mse : mean squared error.\nExamples:\n\n >>> mse_metric = datasets.load_metric("mse")\n >>> predictions = [2.5, 0.0, 2, 8]\n >>> references = [3, -0.5, 2, 7]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'mse\': 0.375}\n >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)\n >>> print(rmse_result)\n {\'mse\': 0.6123724356957945}\n\n If you\'re using multi-dimensional lists, then set the config as follows :\n\n >>> mse_metric = datasets.load_metric("mse", "multilist")\n >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n >>> references = [[0, 2], [-1, 2], [8, -5]]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'mse\': 0.7083333333333334}\n >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mse\': array([0.41666667, 1. ])}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def __a ( self : List[Any] ) -> Optional[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html''' ] , ) def __a ( self : List[Any] ) -> int: """simple docstring""" if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value('''float''' ) ), "references": datasets.Sequence(datasets.Value('''float''' ) ), } else: return { "predictions": datasets.Value('''float''' ), "references": datasets.Value('''float''' ), } def __a ( self : Any , _A : Dict , _A : Any , _A : Any=None , _A : Any="uniform_average" , _A : Optional[Any]=True ) -> Dict: """simple docstring""" lowercase : Any = mean_squared_error( _A , _A , sample_weight=_A , multioutput=_A , squared=_A ) return {"mse": mse}
116
0
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( "split_dict" ,[ SplitDict(), SplitDict({"train": SplitInfo(name="train" ,num_bytes=1337 ,num_examples=42 ,dataset_name="my_dataset" )} ), SplitDict({"train": SplitInfo(name="train" ,num_bytes=1337 ,num_examples=42 )} ), SplitDict({"train": SplitInfo()} ), ] ,) def __snake_case ( __UpperCamelCase : SplitDict ): """simple docstring""" A_ = split_dict._to_yaml_list() assert len(__UpperCamelCase ) == len(__UpperCamelCase ) A_ = SplitDict._from_yaml_list(__UpperCamelCase ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump A_ = None # the split name of split_dict takes over the name of the split info object A_ = split_name assert split_dict == reloaded @pytest.mark.parametrize( "split_info" ,[SplitInfo(), SplitInfo(dataset_name=__UpperCamelCase ), SplitInfo(dataset_name="my_dataset" )] ) def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = asdict(SplitDict({"train": split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
312
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def __snake_case ( ): """simple docstring""" A_ = { "repo_name": ["test_repo1", "test_repo2", "test_repo3"], "path": ["test_1.py", "test_2.py", "unit_test.py"], "content": ["a " * 20, "a " * 30, "b " * 7], } A_ = Dataset.from_dict(__UpperCamelCase ) return dataset class _a ( snake_case_ ): """simple docstring""" def __A ( self : Union[str, Any] ): A_ = get_dataset() A_ = make_duplicate_clusters(UpperCAmelCase , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def __A ( self : List[Any] ): A_ = get_dataset() A_ , A_ = deduplicate_dataset(UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , 2 ) print(UpperCAmelCase ) self.assertEqual(duplicate_clusters[0][0]["copies"] , 2 ) self.assertEqual(duplicate_clusters[0][0]["is_extreme"] , UpperCAmelCase )
312
1
"""simple docstring""" import csv from collections import defaultdict from dataclasses import dataclass, field from typing import List, Optional import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import ScalarFormatter from transformers import HfArgumentParser def _A ( UpperCamelCase_ : Union[str, Any]=None, UpperCamelCase_ : int=None) -> Tuple: '''simple docstring''' return field(default_factory=lambda: default, metadata=UpperCamelCase_) @dataclass class _lowerCAmelCase : """simple docstring""" __UpperCAmelCase : str = field( metadata={"help": "The csv file to plot."} ,) __UpperCAmelCase : bool = field( default=lowercase ,metadata={"help": "Whether to plot along batch size or sequence length. Defaults to sequence length."} ,) __UpperCAmelCase : bool = field( default=lowercase ,metadata={"help": "Whether the csv file has time results or memory results. Defaults to memory results."} ,) __UpperCAmelCase : bool = field( default=lowercase ,metadata={"help": "Disable logarithmic scale when plotting"} ,) __UpperCAmelCase : bool = field( default=lowercase ,metadata={ "help": "Whether the csv file has training results or inference results. Defaults to inference results." } ,) __UpperCAmelCase : Optional[str] = field( default=lowercase ,metadata={"help": "Filename under which the plot will be saved. If unused no plot is saved."} ,) __UpperCAmelCase : Optional[List[str]] = list_field( default=lowercase ,metadata={"help": "List of model names that are used instead of the ones in the csv file."} ) def _A ( UpperCamelCase_ : Any) -> Dict: '''simple docstring''' try: int(UpperCamelCase_) return True except ValueError: return False def _A ( UpperCamelCase_ : Optional[int]) -> Optional[Any]: '''simple docstring''' try: float(UpperCamelCase_) return True except ValueError: return False class _lowerCAmelCase : """simple docstring""" def __init__( self : Optional[Any], UpperCAmelCase__ : Optional[Any] ): __lowercase = args __lowercase = defaultdict(lambda: {"bsz": [], "seq_len": [], "result": {}} ) with open(self.args.csv_file, newline="" ) as csv_file: __lowercase = csv.DictReader(UpperCAmelCase__ ) for row in reader: __lowercase = row["model"] self.result_dict[model_name]["bsz"].append(int(row["batch_size"] ) ) self.result_dict[model_name]["seq_len"].append(int(row["sequence_length"] ) ) if can_convert_to_int(row["result"] ): # value is not None __lowercase = int(row["result"] ) elif can_convert_to_float(row["result"] ): # value is not None __lowercase = float(row["result"] ) def _lowercase ( self : Optional[Any] ): __lowercase ,__lowercase = plt.subplots() __lowercase = "Time usage" if self.args.is_time else "Memory usage" __lowercase = title_str + " for training" if self.args.is_train else title_str + " for inference" if not self.args.no_log_scale: # set logarithm scales ax.set_xscale("log" ) ax.set_yscale("log" ) for axis in [ax.xaxis, ax.yaxis]: axis.set_major_formatter(ScalarFormatter() ) for model_name_idx, model_name in enumerate(self.result_dict.keys() ): __lowercase = sorted(set(self.result_dict[model_name]["bsz"] ) ) __lowercase = sorted(set(self.result_dict[model_name]["seq_len"] ) ) __lowercase = self.result_dict[model_name]["result"] ((__lowercase) ,(__lowercase)) = ( (batch_sizes, sequence_lengths) if self.args.plot_along_batch else (sequence_lengths, batch_sizes) ) __lowercase = ( model_name if self.args.short_model_names is None else self.args.short_model_names[model_name_idx] ) for inner_loop_value in inner_loop_array: if self.args.plot_along_batch: __lowercase = np.asarray( [results[(x, inner_loop_value)] for x in x_axis_array if (x, inner_loop_value) in results], dtype=UpperCAmelCase__, ) else: __lowercase = np.asarray( [results[(inner_loop_value, x)] for x in x_axis_array if (inner_loop_value, x) in results], dtype=np.floataa, ) ((__lowercase) ,(__lowercase)) = ( ("batch_size", "len") if self.args.plot_along_batch else ("in #tokens", "bsz") ) __lowercase = np.asarray(UpperCAmelCase__, UpperCAmelCase__ )[: len(UpperCAmelCase__ )] plt.scatter( UpperCAmelCase__, UpperCAmelCase__, label=F"""{label_model_name} - {inner_loop_label}: {inner_loop_value}""" ) plt.plot(UpperCAmelCase__, UpperCAmelCase__, "--" ) title_str += F""" {label_model_name} vs.""" __lowercase = title_str[:-4] __lowercase = "Time in s" if self.args.is_time else "Memory in MB" # plot plt.title(UpperCAmelCase__ ) plt.xlabel(UpperCAmelCase__ ) plt.ylabel(UpperCAmelCase__ ) plt.legend() if self.args.figure_png_file is not None: plt.savefig(self.args.figure_png_file ) else: plt.show() def _A ( ) -> List[str]: '''simple docstring''' __lowercase = HfArgumentParser(UpperCamelCase_) __lowercase = parser.parse_args_into_dataclasses()[0] __lowercase = Plot(args=UpperCamelCase_) plot.plot() if __name__ == "__main__": main()
355
"""simple docstring""" import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration _a = 5_00_00 _a = 50_00 _a , _a = os.path.split(__file__) _a = os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json')) @get_duration def _A ( UpperCamelCase_ : datasets.Dataset, UpperCamelCase_ : List[str]) -> List[str]: '''simple docstring''' for i in range(UpperCamelCase_): __lowercase = dataset[i] @get_duration def _A ( UpperCamelCase_ : datasets.Dataset, UpperCamelCase_ : List[Any], UpperCamelCase_ : int) -> Dict: '''simple docstring''' for i in range(0, len(UpperCamelCase_), UpperCamelCase_): __lowercase = dataset[i : i + batch_size] @get_duration def _A ( UpperCamelCase_ : datasets.Dataset, UpperCamelCase_ : Any, UpperCamelCase_ : Optional[int]) -> List[str]: '''simple docstring''' with dataset.formatted_as(type=UpperCamelCase_): for i in range(UpperCamelCase_): __lowercase = dataset[i] @get_duration def _A ( UpperCamelCase_ : datasets.Dataset, UpperCamelCase_ : str, UpperCamelCase_ : Union[str, Any], UpperCamelCase_ : Union[str, Any]) -> Dict: '''simple docstring''' with dataset.formatted_as(type=UpperCamelCase_): for i in range(0, UpperCamelCase_, UpperCamelCase_): __lowercase = dataset[i : i + batch_size] def _A ( ) -> List[str]: '''simple docstring''' __lowercase = {"num examples": SPEED_TEST_N_EXAMPLES} __lowercase = [ (read, {"length": SMALL_TEST}), (read, {"length": SPEED_TEST_N_EXAMPLES}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 100}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1000}), (read_formatted, {"type": "numpy", "length": SMALL_TEST}), (read_formatted, {"type": "pandas", "length": SMALL_TEST}), (read_formatted, {"type": "torch", "length": SMALL_TEST}), (read_formatted, {"type": "tensorflow", "length": SMALL_TEST}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 1000}), ] __lowercase = [ (read, {"length": SMALL_TEST}), (read, {"length": SPEED_TEST_N_EXAMPLES}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 100}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1000}), (read_formatted, {"type": "numpy", "length": SMALL_TEST}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 1000}), ] with tempfile.TemporaryDirectory() as tmp_dir: print("generating dataset") __lowercase = datasets.Features( {"list": datasets.Sequence(datasets.Value("float32")), "numbers": datasets.Value("float32")}) __lowercase = generate_example_dataset( os.path.join(UpperCamelCase_, "dataset.arrow"), UpperCamelCase_, num_examples=UpperCamelCase_, seq_shapes={"list": (100,)}, ) print("first set of iterations") for func, kwargs in functions: print(func.__name__, str(UpperCamelCase_)) __lowercase = func(UpperCamelCase_, **UpperCamelCase_) print("shuffling dataset") __lowercase = dataset.shuffle() print("Second set of iterations (after shuffling") for func, kwargs in functions_shuffled: print("shuffled ", func.__name__, str(UpperCamelCase_)) __lowercase = func( UpperCamelCase_, **UpperCamelCase_) with open(UpperCamelCase_, "wb") as f: f.write(json.dumps(UpperCamelCase_).encode("utf-8")) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
144
0
import requests from bsa import BeautifulSoup def lowerCamelCase__ ( _a = "AAPL"): SCREAMING_SNAKE_CASE : List[str] = f"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}" SCREAMING_SNAKE_CASE : Optional[int] = BeautifulSoup(requests.get(__lowerCAmelCase).text , "html.parser") SCREAMING_SNAKE_CASE : Union[str, Any] = "My(6px) Pos(r) smartphone_Mt(6px)" return soup.find("div" , class_=class_).find("span").text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(F'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
76
'''simple docstring''' from collections import deque from math import floor from random import random from time import time class lowerCAmelCase__ : def __init__( self : Union[str, Any] ) ->Union[str, Any]: '''simple docstring''' _UpperCAmelCase : List[Any] = {} def lowerCAmelCase__ ( self : Dict , lowerCamelCase__ : Tuple , lowerCamelCase__ : List[str] , lowerCamelCase__ : int=1 ) ->Optional[Any]: '''simple docstring''' if self.graph.get(lowerCamelCase__ ): if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: _UpperCAmelCase : Dict = [[w, v]] if not self.graph.get(lowerCamelCase__ ): _UpperCAmelCase : str = [] def lowerCAmelCase__ ( self : Any ) ->Tuple: '''simple docstring''' return list(self.graph ) def lowerCAmelCase__ ( self : Tuple , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : str ) ->Any: '''simple docstring''' if self.graph.get(lowerCamelCase__ ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(lowerCamelCase__ ) def lowerCAmelCase__ ( self : int , lowerCamelCase__ : Optional[Any]=-2 , lowerCamelCase__ : Optional[int]=-1 ) ->int: '''simple docstring''' if s == d: return [] _UpperCAmelCase : Tuple = [] _UpperCAmelCase : Optional[int] = [] if s == -2: _UpperCAmelCase : Union[str, Any] = list(self.graph )[0] stack.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) _UpperCAmelCase : Optional[Any] = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _UpperCAmelCase : str = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(lowerCamelCase__ ) return visited else: stack.append(node[1] ) visited.append(node[1] ) _UpperCAmelCase : Union[str, Any] = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(lowerCamelCase__ ) != 0: _UpperCAmelCase : Tuple = stack[len(lowerCamelCase__ ) - 1] else: _UpperCAmelCase : Optional[int] = ss # check if se have reached the starting point if len(lowerCamelCase__ ) == 0: return visited def lowerCAmelCase__ ( self : List[Any] , lowerCamelCase__ : Any=-1 ) ->str: '''simple docstring''' if c == -1: _UpperCAmelCase : str = floor(random() * 1_00_00 ) + 10 for i in range(lowerCamelCase__ ): # every vertex has max 100 edges for _ in range(floor(random() * 1_02 ) + 1 ): _UpperCAmelCase : Tuple = floor(random() * c ) + 1 if n != i: self.add_pair(lowerCamelCase__ , lowerCamelCase__ , 1 ) def lowerCAmelCase__ ( self : Any , lowerCamelCase__ : Union[str, Any]=-2 ) ->Union[str, Any]: '''simple docstring''' _UpperCAmelCase : List[str] = deque() _UpperCAmelCase : int = [] if s == -2: _UpperCAmelCase : str = list(self.graph )[0] d.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) while d: _UpperCAmelCase : int = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def lowerCAmelCase__ ( self : Dict , lowerCamelCase__ : Union[str, Any] ) ->Dict: '''simple docstring''' _UpperCAmelCase : Dict = 0 for x in self.graph: for y in self.graph[x]: if y[1] == u: count += 1 return count def lowerCAmelCase__ ( self : int , lowerCamelCase__ : Union[str, Any] ) ->int: '''simple docstring''' return len(self.graph[u] ) def lowerCAmelCase__ ( self : int , lowerCamelCase__ : Any=-2 ) ->int: '''simple docstring''' _UpperCAmelCase : Optional[Any] = [] _UpperCAmelCase : List[str] = [] if s == -2: _UpperCAmelCase : str = list(self.graph )[0] stack.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) _UpperCAmelCase : List[Any] = s _UpperCAmelCase : Tuple = [] while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _UpperCAmelCase : str = s for node in self.graph[s]: if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _UpperCAmelCase : List[Any] = node[1] break # check if all the children are visited if s == ss: sorted_nodes.append(stack.pop() ) if len(lowerCamelCase__ ) != 0: _UpperCAmelCase : Dict = stack[len(lowerCamelCase__ ) - 1] else: _UpperCAmelCase : List[str] = ss # check if se have reached the starting point if len(lowerCamelCase__ ) == 0: return sorted_nodes def lowerCAmelCase__ ( self : Union[str, Any] ) ->Optional[Any]: '''simple docstring''' _UpperCAmelCase : Tuple = [] _UpperCAmelCase : str = [] _UpperCAmelCase : int = list(self.graph )[0] stack.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) _UpperCAmelCase : str = -2 _UpperCAmelCase : Optional[int] = [] _UpperCAmelCase : Union[str, Any] = s _UpperCAmelCase : Tuple = False _UpperCAmelCase : List[str] = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _UpperCAmelCase : Optional[Any] = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _UpperCAmelCase : Optional[int] = len(lowerCamelCase__ ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _UpperCAmelCase : Dict = node[1] break # check if all the children are visited if s == ss: stack.pop() _UpperCAmelCase : List[str] = True if len(lowerCamelCase__ ) != 0: _UpperCAmelCase : Tuple = stack[len(lowerCamelCase__ ) - 1] else: _UpperCAmelCase : Optional[Any] = False indirect_parents.append(lowerCamelCase__ ) _UpperCAmelCase : Any = s _UpperCAmelCase : Dict = ss # check if se have reached the starting point if len(lowerCamelCase__ ) == 0: return list(lowerCamelCase__ ) def lowerCAmelCase__ ( self : int ) ->Optional[int]: '''simple docstring''' _UpperCAmelCase : Union[str, Any] = [] _UpperCAmelCase : Optional[Any] = [] _UpperCAmelCase : str = list(self.graph )[0] stack.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) _UpperCAmelCase : Any = -2 _UpperCAmelCase : Optional[Any] = [] _UpperCAmelCase : Tuple = s _UpperCAmelCase : List[str] = False _UpperCAmelCase : Optional[Any] = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _UpperCAmelCase : Optional[int] = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _UpperCAmelCase : Optional[int] = len(lowerCamelCase__ ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _UpperCAmelCase : Tuple = node[1] break # check if all the children are visited if s == ss: stack.pop() _UpperCAmelCase : Dict = True if len(lowerCamelCase__ ) != 0: _UpperCAmelCase : List[str] = stack[len(lowerCamelCase__ ) - 1] else: _UpperCAmelCase : Union[str, Any] = False indirect_parents.append(lowerCamelCase__ ) _UpperCAmelCase : Optional[Any] = s _UpperCAmelCase : Optional[Any] = ss # check if se have reached the starting point if len(lowerCamelCase__ ) == 0: return False def lowerCAmelCase__ ( self : str , lowerCamelCase__ : Tuple=-2 , lowerCamelCase__ : Union[str, Any]=-1 ) ->Any: '''simple docstring''' _UpperCAmelCase : Dict = time() self.dfs(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : Tuple = time() return end - begin def lowerCAmelCase__ ( self : List[Any] , lowerCamelCase__ : Dict=-2 ) ->Union[str, Any]: '''simple docstring''' _UpperCAmelCase : Optional[Any] = time() self.bfs(lowerCamelCase__ ) _UpperCAmelCase : Tuple = time() return end - begin class lowerCAmelCase__ : def __init__( self : Tuple ) ->int: '''simple docstring''' _UpperCAmelCase : Any = {} def lowerCAmelCase__ ( self : int , lowerCamelCase__ : List[str] , lowerCamelCase__ : str , lowerCamelCase__ : Tuple=1 ) ->int: '''simple docstring''' if self.graph.get(lowerCamelCase__ ): # if there already is a edge if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: # if u does not exist _UpperCAmelCase : Dict = [[w, v]] # add the other way if self.graph.get(lowerCamelCase__ ): # if there already is a edge if self.graph[v].count([w, u] ) == 0: self.graph[v].append([w, u] ) else: # if u does not exist _UpperCAmelCase : Tuple = [[w, u]] def lowerCAmelCase__ ( self : Dict , lowerCamelCase__ : Any , lowerCamelCase__ : Optional[Any] ) ->Dict: '''simple docstring''' if self.graph.get(lowerCamelCase__ ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(lowerCamelCase__ ) # the other way round if self.graph.get(lowerCamelCase__ ): for _ in self.graph[v]: if _[1] == u: self.graph[v].remove(lowerCamelCase__ ) def lowerCAmelCase__ ( self : List[str] , lowerCamelCase__ : List[str]=-2 , lowerCamelCase__ : List[str]=-1 ) ->List[Any]: '''simple docstring''' if s == d: return [] _UpperCAmelCase : List[Any] = [] _UpperCAmelCase : List[Any] = [] if s == -2: _UpperCAmelCase : List[Any] = list(self.graph )[0] stack.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) _UpperCAmelCase : List[Any] = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _UpperCAmelCase : Optional[Any] = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(lowerCamelCase__ ) return visited else: stack.append(node[1] ) visited.append(node[1] ) _UpperCAmelCase : Optional[int] = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(lowerCamelCase__ ) != 0: _UpperCAmelCase : Optional[int] = stack[len(lowerCamelCase__ ) - 1] else: _UpperCAmelCase : Union[str, Any] = ss # check if se have reached the starting point if len(lowerCamelCase__ ) == 0: return visited def lowerCAmelCase__ ( self : Any , lowerCamelCase__ : Dict=-1 ) ->str: '''simple docstring''' if c == -1: _UpperCAmelCase : str = floor(random() * 1_00_00 ) + 10 for i in range(lowerCamelCase__ ): # every vertex has max 100 edges for _ in range(floor(random() * 1_02 ) + 1 ): _UpperCAmelCase : str = floor(random() * c ) + 1 if n != i: self.add_pair(lowerCamelCase__ , lowerCamelCase__ , 1 ) def lowerCAmelCase__ ( self : str , lowerCamelCase__ : Union[str, Any]=-2 ) ->Any: '''simple docstring''' _UpperCAmelCase : str = deque() _UpperCAmelCase : Optional[int] = [] if s == -2: _UpperCAmelCase : Union[str, Any] = list(self.graph )[0] d.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) while d: _UpperCAmelCase : Any = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def lowerCAmelCase__ ( self : Union[str, Any] , lowerCamelCase__ : Dict ) ->Optional[Any]: '''simple docstring''' return len(self.graph[u] ) def lowerCAmelCase__ ( self : str ) ->Any: '''simple docstring''' _UpperCAmelCase : int = [] _UpperCAmelCase : str = [] _UpperCAmelCase : Optional[int] = list(self.graph )[0] stack.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) _UpperCAmelCase : Any = -2 _UpperCAmelCase : Union[str, Any] = [] _UpperCAmelCase : str = s _UpperCAmelCase : int = False _UpperCAmelCase : List[Any] = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _UpperCAmelCase : int = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _UpperCAmelCase : Union[str, Any] = len(lowerCamelCase__ ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _UpperCAmelCase : Union[str, Any] = node[1] break # check if all the children are visited if s == ss: stack.pop() _UpperCAmelCase : List[Any] = True if len(lowerCamelCase__ ) != 0: _UpperCAmelCase : List[str] = stack[len(lowerCamelCase__ ) - 1] else: _UpperCAmelCase : Dict = False indirect_parents.append(lowerCamelCase__ ) _UpperCAmelCase : Dict = s _UpperCAmelCase : Dict = ss # check if se have reached the starting point if len(lowerCamelCase__ ) == 0: return list(lowerCamelCase__ ) def lowerCAmelCase__ ( self : Optional[Any] ) ->Dict: '''simple docstring''' _UpperCAmelCase : List[Any] = [] _UpperCAmelCase : List[str] = [] _UpperCAmelCase : Tuple = list(self.graph )[0] stack.append(lowerCamelCase__ ) visited.append(lowerCamelCase__ ) _UpperCAmelCase : Dict = -2 _UpperCAmelCase : Union[str, Any] = [] _UpperCAmelCase : Dict = s _UpperCAmelCase : Dict = False _UpperCAmelCase : str = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: _UpperCAmelCase : Union[str, Any] = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): _UpperCAmelCase : int = len(lowerCamelCase__ ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) _UpperCAmelCase : Union[str, Any] = node[1] break # check if all the children are visited if s == ss: stack.pop() _UpperCAmelCase : List[Any] = True if len(lowerCamelCase__ ) != 0: _UpperCAmelCase : List[Any] = stack[len(lowerCamelCase__ ) - 1] else: _UpperCAmelCase : Optional[Any] = False indirect_parents.append(lowerCamelCase__ ) _UpperCAmelCase : List[str] = s _UpperCAmelCase : List[Any] = ss # check if se have reached the starting point if len(lowerCamelCase__ ) == 0: return False def lowerCAmelCase__ ( self : Optional[Any] ) ->int: '''simple docstring''' return list(self.graph ) def lowerCAmelCase__ ( self : int , lowerCamelCase__ : Dict=-2 , lowerCamelCase__ : Dict=-1 ) ->List[Any]: '''simple docstring''' _UpperCAmelCase : Optional[Any] = time() self.dfs(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : int = time() return end - begin def lowerCAmelCase__ ( self : int , lowerCamelCase__ : Any=-2 ) ->int: '''simple docstring''' _UpperCAmelCase : Optional[Any] = time() self.bfs(lowerCamelCase__ ) _UpperCAmelCase : Any = time() return end - begin
234
0
'''simple docstring''' from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _UpperCamelCase ( A ): '''simple docstring''' lowerCAmelCase__ = ["""image_processor""", """tokenizer"""] lowerCAmelCase__ = """BlipImageProcessor""" lowerCAmelCase__ = """AutoTokenizer""" def __init__( self : List[str] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict): '''simple docstring''' __lowercase =False super().__init__(_lowerCAmelCase , _lowerCAmelCase) __lowercase =self.image_processor def __call__( self : Dict , _lowerCAmelCase : ImageInput = None , _lowerCAmelCase : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , _lowerCAmelCase : bool = True , _lowerCAmelCase : Union[bool, str, PaddingStrategy] = False , _lowerCAmelCase : Union[bool, str, TruncationStrategy] = None , _lowerCAmelCase : Optional[int] = None , _lowerCAmelCase : int = 0 , _lowerCAmelCase : Optional[int] = None , _lowerCAmelCase : Optional[bool] = None , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = False , _lowerCAmelCase : bool = True , _lowerCAmelCase : Optional[Union[str, TensorType]] = None , **_lowerCAmelCase : Any , ): '''simple docstring''' if images is None and text is None: raise ValueError('You have to specify either images or text.') # Get only text if images is None: __lowercase =self.tokenizer __lowercase =self.tokenizer( text=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , max_length=_lowerCAmelCase , stride=_lowerCAmelCase , pad_to_multiple_of=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , return_overflowing_tokens=_lowerCAmelCase , return_special_tokens_mask=_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase , return_length=_lowerCAmelCase , verbose=_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase , ) return text_encoding # add pixel_values __lowercase =self.image_processor(_lowerCAmelCase , return_tensors=_lowerCAmelCase) if text is not None: __lowercase =self.tokenizer( text=_lowerCAmelCase , add_special_tokens=_lowerCAmelCase , padding=_lowerCAmelCase , truncation=_lowerCAmelCase , max_length=_lowerCAmelCase , stride=_lowerCAmelCase , pad_to_multiple_of=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , return_overflowing_tokens=_lowerCAmelCase , return_special_tokens_mask=_lowerCAmelCase , return_offsets_mapping=_lowerCAmelCase , return_token_type_ids=_lowerCAmelCase , return_length=_lowerCAmelCase , verbose=_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase , ) else: __lowercase =None if text_encoding is not None: encoding_image_processor.update(_lowerCAmelCase) return encoding_image_processor def __lowerCamelCase ( self : List[Any] , *_lowerCAmelCase : Dict , **_lowerCAmelCase : List[str]): '''simple docstring''' return self.tokenizer.batch_decode(*_lowerCAmelCase , **_lowerCAmelCase) def __lowerCamelCase ( self : str , *_lowerCAmelCase : Optional[int] , **_lowerCAmelCase : Tuple): '''simple docstring''' return self.tokenizer.decode(*_lowerCAmelCase , **_lowerCAmelCase) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def __lowerCamelCase ( self : Dict): '''simple docstring''' __lowercase =self.tokenizer.model_input_names __lowercase =self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
353
'''simple docstring''' from math import factorial def _A ( _lowerCAmelCase = 20 ): """simple docstring""" __lowercase =2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... __lowercase =n // 2 return int(factorial(_lowerCAmelCase ) / (factorial(_lowerCAmelCase ) * factorial(n - k )) ) if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution(20)) else: try: lowerCamelCase = int(sys.argv[1]) print(solution(n)) except ValueError: print("""Invalid entry - please enter a number.""")
48
0
'''simple docstring''' import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DetrImageProcessor class a__( unittest.TestCase ): '''simple docstring''' def __init__( self , __lowerCAmelCase , __lowerCAmelCase=7 , __lowerCAmelCase=3 , __lowerCAmelCase=30 , __lowerCAmelCase=400 , __lowerCAmelCase=True , __lowerCAmelCase=None , __lowerCAmelCase=True , __lowerCAmelCase=1 / 255 , __lowerCAmelCase=True , __lowerCAmelCase=[0.5, 0.5, 0.5] , __lowerCAmelCase=[0.5, 0.5, 0.5] , __lowerCAmelCase=True , ): """simple docstring""" lowerCAmelCase = size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 1333} lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = num_channels lowerCAmelCase = min_resolution lowerCAmelCase = max_resolution lowerCAmelCase = do_resize lowerCAmelCase = size lowerCAmelCase = do_rescale lowerCAmelCase = rescale_factor lowerCAmelCase = do_normalize lowerCAmelCase = image_mean lowerCAmelCase = image_std lowerCAmelCase = do_pad def a_ ( self): """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_pad": self.do_pad, } def a_ ( self , __lowerCAmelCase , __lowerCAmelCase=False): """simple docstring""" if not batched: lowerCAmelCase = image_inputs[0] if isinstance(_lowerCAmelCase , Image.Image): lowerCAmelCase , lowerCAmelCase = image.size else: lowerCAmelCase , lowerCAmelCase = image.shape[1], image.shape[2] if w < h: lowerCAmelCase = int(self.size["""shortest_edge"""] * h / w) lowerCAmelCase = self.size["""shortest_edge"""] elif w > h: lowerCAmelCase = self.size["""shortest_edge"""] lowerCAmelCase = int(self.size["""shortest_edge"""] * w / h) else: lowerCAmelCase = self.size["""shortest_edge"""] lowerCAmelCase = self.size["""shortest_edge"""] else: lowerCAmelCase = [] for image in image_inputs: lowerCAmelCase , lowerCAmelCase = self.get_expected_values([image]) expected_values.append((expected_height, expected_width)) lowerCAmelCase = max(_lowerCAmelCase , key=lambda __lowerCAmelCase: item[0])[0] lowerCAmelCase = max(_lowerCAmelCase , key=lambda __lowerCAmelCase: item[1])[1] return expected_height, expected_width @require_torch @require_vision class a__( lowerCAmelCase__ , unittest.TestCase ): '''simple docstring''' UpperCAmelCase_ : List[str] = DetrImageProcessor if is_vision_available() else None def a_ ( self): """simple docstring""" lowerCAmelCase = DetrImageProcessingTester(self) @property def a_ ( self): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a_ ( self): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(_lowerCAmelCase , """image_mean""")) self.assertTrue(hasattr(_lowerCAmelCase , """image_std""")) self.assertTrue(hasattr(_lowerCAmelCase , """do_normalize""")) self.assertTrue(hasattr(_lowerCAmelCase , """do_rescale""")) self.assertTrue(hasattr(_lowerCAmelCase , """rescale_factor""")) self.assertTrue(hasattr(_lowerCAmelCase , """do_resize""")) self.assertTrue(hasattr(_lowerCAmelCase , """size""")) self.assertTrue(hasattr(_lowerCAmelCase , """do_pad""")) def a_ ( self): """simple docstring""" lowerCAmelCase = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {"""shortest_edge""": 18, """longest_edge""": 1333}) self.assertEqual(image_processor.do_pad , _lowerCAmelCase) lowerCAmelCase = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=_lowerCAmelCase) self.assertEqual(image_processor.size , {"""shortest_edge""": 42, """longest_edge""": 84}) self.assertEqual(image_processor.do_pad , _lowerCAmelCase) def a_ ( self): """simple docstring""" pass def a_ ( self): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict) # create random PIL images lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCAmelCase) for image in image_inputs: self.assertIsInstance(_lowerCAmelCase , Image.Image) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors="""pt""").pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(_lowerCAmelCase) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(_lowerCAmelCase , batched=_lowerCAmelCase) lowerCAmelCase = image_processing(_lowerCAmelCase , return_tensors="""pt""").pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def a_ ( self): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCAmelCase , numpify=_lowerCAmelCase) for image in image_inputs: self.assertIsInstance(_lowerCAmelCase , np.ndarray) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors="""pt""").pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(_lowerCAmelCase) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase = image_processing(_lowerCAmelCase , return_tensors="""pt""").pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(_lowerCAmelCase , batched=_lowerCAmelCase) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def a_ ( self): """simple docstring""" lowerCAmelCase = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_lowerCAmelCase , torchify=_lowerCAmelCase) for image in image_inputs: self.assertIsInstance(_lowerCAmelCase , torch.Tensor) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors="""pt""").pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(_lowerCAmelCase) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase = image_processing(_lowerCAmelCase , return_tensors="""pt""").pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(_lowerCAmelCase , batched=_lowerCAmelCase) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def a_ ( self): """simple docstring""" lowerCAmelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""") with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt""" , """r""") as f: lowerCAmelCase = json.loads(f.read()) lowerCAmelCase = {"""image_id""": 39769, """annotations""": target} # encode them lowerCAmelCase = DetrImageProcessor.from_pretrained("""facebook/detr-resnet-50""") lowerCAmelCase = image_processing(images=_lowerCAmelCase , annotations=_lowerCAmelCase , return_tensors="""pt""") # verify pixel values lowerCAmelCase = torch.Size([1, 3, 800, 1066]) self.assertEqual(encoding["""pixel_values"""].shape , _lowerCAmelCase) lowerCAmelCase = torch.tensor([0.2796, 0.3138, 0.3481]) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , _lowerCAmelCase , atol=1E-4)) # verify area lowerCAmelCase = torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , _lowerCAmelCase)) # verify boxes lowerCAmelCase = torch.Size([6, 4]) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , _lowerCAmelCase) lowerCAmelCase = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , _lowerCAmelCase , atol=1E-3)) # verify image_id lowerCAmelCase = torch.tensor([39769]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , _lowerCAmelCase)) # verify is_crowd lowerCAmelCase = torch.tensor([0, 0, 0, 0, 0, 0]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , _lowerCAmelCase)) # verify class_labels lowerCAmelCase = torch.tensor([75, 75, 63, 65, 17, 17]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , _lowerCAmelCase)) # verify orig_size lowerCAmelCase = torch.tensor([480, 640]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , _lowerCAmelCase)) # verify size lowerCAmelCase = torch.tensor([800, 1066]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , _lowerCAmelCase)) @slow def a_ ( self): """simple docstring""" lowerCAmelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""") with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt""" , """r""") as f: lowerCAmelCase = json.loads(f.read()) lowerCAmelCase = {"""file_name""": """000000039769.png""", """image_id""": 39769, """segments_info""": target} lowerCAmelCase = pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""") # encode them lowerCAmelCase = DetrImageProcessor.from_pretrained("""facebook/detr-resnet-50-panoptic""") lowerCAmelCase = image_processing(images=_lowerCAmelCase , annotations=_lowerCAmelCase , masks_path=_lowerCAmelCase , return_tensors="""pt""") # verify pixel values lowerCAmelCase = torch.Size([1, 3, 800, 1066]) self.assertEqual(encoding["""pixel_values"""].shape , _lowerCAmelCase) lowerCAmelCase = torch.tensor([0.2796, 0.3138, 0.3481]) self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3] , _lowerCAmelCase , atol=1E-4)) # verify area lowerCAmelCase = torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""] , _lowerCAmelCase)) # verify boxes lowerCAmelCase = torch.Size([6, 4]) self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape , _lowerCAmelCase) lowerCAmelCase = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0] , _lowerCAmelCase , atol=1E-3)) # verify image_id lowerCAmelCase = torch.tensor([39769]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""] , _lowerCAmelCase)) # verify is_crowd lowerCAmelCase = torch.tensor([0, 0, 0, 0, 0, 0]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""] , _lowerCAmelCase)) # verify class_labels lowerCAmelCase = torch.tensor([17, 17, 63, 75, 75, 93]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""] , _lowerCAmelCase)) # verify masks lowerCAmelCase = 822873 self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item() , _lowerCAmelCase) # verify orig_size lowerCAmelCase = torch.tensor([480, 640]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""] , _lowerCAmelCase)) # verify size lowerCAmelCase = torch.tensor([800, 1066]) self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""] , _lowerCAmelCase))
272
from __future__ import annotations from collections import namedtuple def _lowerCAmelCase ( lowerCAmelCase_ :float , lowerCAmelCase_ :float , lowerCAmelCase_ :float )->tuple: '''simple docstring''' snake_case_ = namedtuple("result" , "name value" ) if (voltage, current, power).count(0 ) != 1: raise ValueError("Only one argument must be 0" ) elif power < 0: raise ValueError( "Power cannot be negative in any electrical/electronics system" ) elif voltage == 0: return result("voltage" , power / current ) elif current == 0: return result("current" , power / voltage ) elif power == 0: return result("power" , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError("Exactly one argument must be 0" ) if __name__ == "__main__": import doctest doctest.testmod()
159
0
'''simple docstring''' import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class _lowercase ( _lowercase ): a = (IPNDMScheduler,) a = (("""num_inference_steps""", 50),) def lowerCamelCase_ ( self: Optional[Any] , **UpperCamelCase__: Union[str, Any] ): lowerCamelCase__ : Optional[Any] = {"""num_train_timesteps""": 1_000} config.update(**lowerCamelCase_ ) return config def lowerCamelCase_ ( self: Optional[int] , UpperCamelCase__: List[Any]=0 , **UpperCamelCase__: Any ): lowerCamelCase__ : Union[str, Any] = dict(self.forward_default_kwargs ) lowerCamelCase__ : Optional[int] = kwargs.pop("""num_inference_steps""" , lowerCamelCase_ ) lowerCamelCase__ : List[Any] = self.dummy_sample lowerCamelCase__ : int = 0.1 * sample lowerCamelCase__ : List[Any] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowerCamelCase__ : Any = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : Any = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCamelCase__ : Optional[Any] = dummy_past_residuals[:] if time_step is None: lowerCamelCase__ : Any = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCamelCase__ : int = scheduler_class.from_pretrained(lowerCamelCase_ ) new_scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals lowerCamelCase__ : int = dummy_past_residuals[:] lowerCamelCase__ : List[Any] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Optional[Any] = new_scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" lowerCamelCase__ : int = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Tuple = new_scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowerCamelCase_ ( self: str ): pass def lowerCamelCase_ ( self: Any , UpperCamelCase__: Dict=0 , **UpperCamelCase__: int ): lowerCamelCase__ : Any = dict(self.forward_default_kwargs ) lowerCamelCase__ : Optional[Any] = kwargs.pop("""num_inference_steps""" , lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = self.dummy_sample lowerCamelCase__ : Dict = 0.1 * sample lowerCamelCase__ : Optional[Any] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: lowerCamelCase__ : Union[str, Any] = self.get_scheduler_config() lowerCamelCase__ : Dict = scheduler_class(**lowerCamelCase_ ) scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residuals (must be after setting timesteps) lowerCamelCase__ : Union[str, Any] = dummy_past_residuals[:] if time_step is None: lowerCamelCase__ : int = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(lowerCamelCase_ ) lowerCamelCase__ : List[Any] = scheduler_class.from_pretrained(lowerCamelCase_ ) # copy over dummy past residuals new_scheduler.set_timesteps(lowerCamelCase_ ) # copy over dummy past residual (must be after setting timesteps) lowerCamelCase__ : List[Any] = dummy_past_residuals[:] lowerCamelCase__ : List[str] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Dict = new_scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" lowerCamelCase__ : Optional[int] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Dict = new_scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowerCamelCase_ ( self: Tuple , **UpperCamelCase__: Optional[int] ): lowerCamelCase__ : Optional[Any] = self.scheduler_classes[0] lowerCamelCase__ : Union[str, Any] = self.get_scheduler_config(**lowerCamelCase_ ) lowerCamelCase__ : List[Any] = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : int = 10 lowerCamelCase__ : Dict = self.dummy_model() lowerCamelCase__ : List[Any] = self.dummy_sample_deter scheduler.set_timesteps(lowerCamelCase_ ) for i, t in enumerate(scheduler.timesteps ): lowerCamelCase__ : Any = model(lowerCamelCase_ , lowerCamelCase_ ) lowerCamelCase__ : Dict = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ).prev_sample for i, t in enumerate(scheduler.timesteps ): lowerCamelCase__ : Optional[Any] = model(lowerCamelCase_ , lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ).prev_sample return sample def lowerCamelCase_ ( self: Tuple ): lowerCamelCase__ : Union[str, Any] = dict(self.forward_default_kwargs ) lowerCamelCase__ : Optional[int] = kwargs.pop("""num_inference_steps""" , lowerCamelCase_ ) for scheduler_class in self.scheduler_classes: lowerCamelCase__ : int = self.get_scheduler_config() lowerCamelCase__ : Dict = scheduler_class(**lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = self.dummy_sample lowerCamelCase__ : Any = 0.1 * sample if num_inference_steps is not None and hasattr(lowerCamelCase_ , """set_timesteps""" ): scheduler.set_timesteps(lowerCamelCase_ ) elif num_inference_steps is not None and not hasattr(lowerCamelCase_ , """set_timesteps""" ): lowerCamelCase__ : Dict = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCamelCase__ : Tuple = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] lowerCamelCase__ : Any = dummy_past_residuals[:] lowerCamelCase__ : Tuple = scheduler.timesteps[5] lowerCamelCase__ : List[Any] = scheduler.timesteps[6] lowerCamelCase__ : Any = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCamelCase__ : Optional[Any] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) lowerCamelCase__ : int = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample lowerCamelCase__ : List[Any] = scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , **lowerCamelCase_ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def lowerCamelCase_ ( self: Tuple ): for timesteps in [100, 1_000]: self.check_over_configs(num_train_timesteps=lowerCamelCase_ , time_step=lowerCamelCase_ ) def lowerCamelCase_ ( self: List[str] ): for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=lowerCamelCase_ , time_step=lowerCamelCase_ ) def lowerCamelCase_ ( self: Optional[Any] ): lowerCamelCase__ : Union[str, Any] = self.full_loop() lowerCamelCase__ : Optional[Any] = torch.mean(torch.abs(lowerCamelCase_ ) ) assert abs(result_mean.item() - 2_540_529 ) < 10
361
'''simple docstring''' import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) _A : str ={ '''sample_size''': 32, '''in_channels''': 3, '''out_channels''': 3, '''layers_per_block''': 2, '''num_class_embeds''': 1_000, '''block_out_channels''': [32, 64], '''attention_head_dim''': 8, '''down_block_types''': [ '''ResnetDownsampleBlock2D''', '''AttnDownBlock2D''', ], '''up_block_types''': [ '''AttnUpBlock2D''', '''ResnetUpsampleBlock2D''', ], '''resnet_time_scale_shift''': '''scale_shift''', '''upsample_type''': '''resnet''', '''downsample_type''': '''resnet''', } _A : Union[str, Any] ={ '''sample_size''': 64, '''in_channels''': 3, '''out_channels''': 3, '''layers_per_block''': 3, '''num_class_embeds''': 1_000, '''block_out_channels''': [192, 192 * 2, 192 * 3, 192 * 4], '''attention_head_dim''': 64, '''down_block_types''': [ '''ResnetDownsampleBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', ], '''up_block_types''': [ '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''ResnetUpsampleBlock2D''', ], '''resnet_time_scale_shift''': '''scale_shift''', '''upsample_type''': '''resnet''', '''downsample_type''': '''resnet''', } _A : Dict ={ '''sample_size''': 256, '''in_channels''': 3, '''out_channels''': 3, '''layers_per_block''': 2, '''num_class_embeds''': None, '''block_out_channels''': [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4], '''attention_head_dim''': 64, '''down_block_types''': [ '''ResnetDownsampleBlock2D''', '''ResnetDownsampleBlock2D''', '''ResnetDownsampleBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', ], '''up_block_types''': [ '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''ResnetUpsampleBlock2D''', '''ResnetUpsampleBlock2D''', '''ResnetUpsampleBlock2D''', ], '''resnet_time_scale_shift''': '''default''', '''upsample_type''': '''resnet''', '''downsample_type''': '''resnet''', } _A : Dict ={ '''num_train_timesteps''': 40, '''sigma_min''': 0.002, '''sigma_max''': 80.0, } _A : str ={ '''num_train_timesteps''': 201, '''sigma_min''': 0.002, '''sigma_max''': 80.0, } _A : int ={ '''num_train_timesteps''': 151, '''sigma_min''': 0.002, '''sigma_max''': 80.0, } def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> Dict: if isinstance(UpperCamelCase , UpperCamelCase ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise argparse.ArgumentTypeError("""boolean value expected""" ) def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase=False ) -> Any: lowerCamelCase__ : Any = checkpoint[f'''{old_prefix}.in_layers.0.weight'''] lowerCamelCase__ : int = checkpoint[f'''{old_prefix}.in_layers.0.bias'''] lowerCamelCase__ : Any = checkpoint[f'''{old_prefix}.in_layers.2.weight'''] lowerCamelCase__ : Any = checkpoint[f'''{old_prefix}.in_layers.2.bias'''] lowerCamelCase__ : Optional[Any] = checkpoint[f'''{old_prefix}.emb_layers.1.weight'''] lowerCamelCase__ : Optional[int] = checkpoint[f'''{old_prefix}.emb_layers.1.bias'''] lowerCamelCase__ : Dict = checkpoint[f'''{old_prefix}.out_layers.0.weight'''] lowerCamelCase__ : Tuple = checkpoint[f'''{old_prefix}.out_layers.0.bias'''] lowerCamelCase__ : str = checkpoint[f'''{old_prefix}.out_layers.3.weight'''] lowerCamelCase__ : int = checkpoint[f'''{old_prefix}.out_layers.3.bias'''] if has_skip: lowerCamelCase__ : Tuple = checkpoint[f'''{old_prefix}.skip_connection.weight'''] lowerCamelCase__ : List[Any] = checkpoint[f'''{old_prefix}.skip_connection.bias'''] return new_checkpoint def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase=None ) -> str: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict = checkpoint[f'''{old_prefix}.qkv.weight'''].chunk(3 , dim=0 ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : str = checkpoint[f'''{old_prefix}.qkv.bias'''].chunk(3 , dim=0 ) lowerCamelCase__ : Any = checkpoint[f'''{old_prefix}.norm.weight'''] lowerCamelCase__ : Optional[int] = checkpoint[f'''{old_prefix}.norm.bias'''] lowerCamelCase__ : List[Any] = weight_q.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : List[Any] = bias_q.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Any = weight_k.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Optional[Any] = bias_k.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Dict = weight_v.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Union[str, Any] = bias_v.squeeze(-1 ).squeeze(-1 ) lowerCamelCase__ : Optional[Any] = ( checkpoint[f'''{old_prefix}.proj_out.weight'''].squeeze(-1 ).squeeze(-1 ) ) lowerCamelCase__ : Dict = checkpoint[f'''{old_prefix}.proj_out.bias'''].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> List[Any]: lowerCamelCase__ : str = torch.load(UpperCamelCase , map_location="""cpu""" ) lowerCamelCase__ : Optional[int] = {} lowerCamelCase__ : Optional[int] = checkpoint["""time_embed.0.weight"""] lowerCamelCase__ : List[Any] = checkpoint["""time_embed.0.bias"""] lowerCamelCase__ : int = checkpoint["""time_embed.2.weight"""] lowerCamelCase__ : Optional[Any] = checkpoint["""time_embed.2.bias"""] if unet_config["num_class_embeds"] is not None: lowerCamelCase__ : Optional[Any] = checkpoint["""label_emb.weight"""] lowerCamelCase__ : Tuple = checkpoint["""input_blocks.0.0.weight"""] lowerCamelCase__ : List[str] = checkpoint["""input_blocks.0.0.bias"""] lowerCamelCase__ : Optional[Any] = unet_config["""down_block_types"""] lowerCamelCase__ : Any = unet_config["""layers_per_block"""] lowerCamelCase__ : Any = unet_config["""attention_head_dim"""] lowerCamelCase__ : List[Any] = unet_config["""block_out_channels"""] lowerCamelCase__ : str = 1 lowerCamelCase__ : str = channels_list[0] for i, layer_type in enumerate(UpperCamelCase ): lowerCamelCase__ : List[Any] = channels_list[i] lowerCamelCase__ : List[Any] = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(UpperCamelCase ): lowerCamelCase__ : int = f'''down_blocks.{i}.resnets.{j}''' lowerCamelCase__ : Dict = f'''input_blocks.{current_layer}.0''' lowerCamelCase__ : Tuple = True if j == 0 and downsample_block_has_skip else False lowerCamelCase__ : List[Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(UpperCamelCase ): lowerCamelCase__ : Tuple = f'''down_blocks.{i}.resnets.{j}''' lowerCamelCase__ : Optional[Any] = f'''input_blocks.{current_layer}.0''' lowerCamelCase__ : str = True if j == 0 and downsample_block_has_skip else False lowerCamelCase__ : Union[str, Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) lowerCamelCase__ : Any = f'''down_blocks.{i}.attentions.{j}''' lowerCamelCase__ : Dict = f'''input_blocks.{current_layer}.1''' lowerCamelCase__ : Tuple = convert_attention( UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) current_layer += 1 if i != len(UpperCamelCase ) - 1: lowerCamelCase__ : Tuple = f'''down_blocks.{i}.downsamplers.0''' lowerCamelCase__ : str = f'''input_blocks.{current_layer}.0''' lowerCamelCase__ : Union[str, Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) current_layer += 1 lowerCamelCase__ : Union[str, Any] = current_channels # hardcoded the mid-block for now lowerCamelCase__ : Any = """mid_block.resnets.0""" lowerCamelCase__ : Optional[Any] = """middle_block.0""" lowerCamelCase__ : int = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) lowerCamelCase__ : List[Any] = """mid_block.attentions.0""" lowerCamelCase__ : Dict = """middle_block.1""" lowerCamelCase__ : int = convert_attention(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) lowerCamelCase__ : Any = """mid_block.resnets.1""" lowerCamelCase__ : Tuple = """middle_block.2""" lowerCamelCase__ : int = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) lowerCamelCase__ : Union[str, Any] = 0 lowerCamelCase__ : Any = unet_config["""up_block_types"""] for i, layer_type in enumerate(UpperCamelCase ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): lowerCamelCase__ : int = f'''up_blocks.{i}.resnets.{j}''' lowerCamelCase__ : Optional[Any] = f'''output_blocks.{current_layer}.0''' lowerCamelCase__ : Any = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) current_layer += 1 if i != len(UpperCamelCase ) - 1: lowerCamelCase__ : Dict = f'''up_blocks.{i}.upsamplers.0''' lowerCamelCase__ : List[str] = f'''output_blocks.{current_layer-1}.1''' lowerCamelCase__ : Optional[Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): lowerCamelCase__ : str = f'''up_blocks.{i}.resnets.{j}''' lowerCamelCase__ : List[Any] = f'''output_blocks.{current_layer}.0''' lowerCamelCase__ : Optional[Any] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , has_skip=UpperCamelCase ) lowerCamelCase__ : Optional[Any] = f'''up_blocks.{i}.attentions.{j}''' lowerCamelCase__ : Any = f'''output_blocks.{current_layer}.1''' lowerCamelCase__ : Optional[int] = convert_attention( UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) current_layer += 1 if i != len(UpperCamelCase ) - 1: lowerCamelCase__ : Tuple = f'''up_blocks.{i}.upsamplers.0''' lowerCamelCase__ : Tuple = f'''output_blocks.{current_layer-1}.2''' lowerCamelCase__ : List[str] = convert_resnet(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) lowerCamelCase__ : Dict = checkpoint["""out.0.weight"""] lowerCamelCase__ : Dict = checkpoint["""out.0.bias"""] lowerCamelCase__ : Dict = checkpoint["""out.2.weight"""] lowerCamelCase__ : Tuple = checkpoint["""out.2.bias"""] return new_checkpoint if __name__ == "__main__": _A : Tuple =argparse.ArgumentParser() parser.add_argument('''--unet_path''', default=None, type=str, required=True, help='''Path to the unet.pt to convert.''') parser.add_argument( '''--dump_path''', default=None, type=str, required=True, help='''Path to output the converted UNet model.''' ) parser.add_argument('''--class_cond''', default=True, type=str, help='''Whether the model is class-conditional.''') _A : Tuple =parser.parse_args() _A : Optional[int] =strabool(args.class_cond) _A : List[str] =os.path.basename(args.unet_path) print(F'Checkpoint: {ckpt_name}') # Get U-Net config if "imagenet64" in ckpt_name: _A : int =IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): _A : Tuple =LSUN_256_UNET_CONFIG elif "test" in ckpt_name: _A : Any =TEST_UNET_CONFIG else: raise ValueError(F'Checkpoint type {ckpt_name} is not currently supported.') if not args.class_cond: _A : str =None _A : Optional[int] =con_pt_to_diffuser(args.unet_path, unet_config) _A : Optional[int] =UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: _A : Tuple =CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: _A : int =CT_IMAGENET_64_SCHEDULER_CONFIG elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): _A : Union[str, Any] =CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(F'Checkpoint type {ckpt_name} is not currently supported.') _A : str =CMStochasticIterativeScheduler(**scheduler_config) _A : Optional[Any] =ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
129
0
lowercase_ = "Alexander Joslin" import operator as op from .stack import Stack def _snake_case( SCREAMING_SNAKE_CASE__ : str ) -> int: '''simple docstring''' A__ = {'*': op.mul, '/': op.truediv, '+': op.add, '-': op.sub} A__ = Stack() A__ = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(SCREAMING_SNAKE_CASE__ ) ) elif i in operators: # RULE 2 operator_stack.push(SCREAMING_SNAKE_CASE__ ) elif i == ")": # RULE 4 A__ = operator_stack.peek() operator_stack.pop() A__ = operand_stack.peek() operand_stack.pop() A__ = operand_stack.peek() operand_stack.pop() A__ = operators[opr](SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) operand_stack.push(SCREAMING_SNAKE_CASE__ ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": lowercase_ = "(5 + ((4 * 2) * (2 + 3)))" # answer = 45 print(f"""{equation} = {dijkstras_two_stack_algorithm(equation)}""")
7
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available SCREAMING_SNAKE_CASE_:str = { """configuration_transfo_xl""": ["""TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TransfoXLConfig"""], """tokenization_transfo_xl""": ["""TransfoXLCorpus""", """TransfoXLTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_:Union[str, Any] = [ """TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST""", """AdaptiveEmbedding""", """TransfoXLForSequenceClassification""", """TransfoXLLMHeadModel""", """TransfoXLModel""", """TransfoXLPreTrainedModel""", """load_tf_weights_in_transfo_xl""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_:Any = [ """TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFAdaptiveEmbedding""", """TFTransfoXLForSequenceClassification""", """TFTransfoXLLMHeadModel""", """TFTransfoXLMainLayer""", """TFTransfoXLModel""", """TFTransfoXLPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_transfo_xl import ( TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, AdaptiveEmbedding, TransfoXLForSequenceClassification, TransfoXLLMHeadModel, TransfoXLModel, TransfoXLPreTrainedModel, load_tf_weights_in_transfo_xl, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_transfo_xl import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFAdaptiveEmbedding, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLMainLayer, TFTransfoXLModel, TFTransfoXLPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_:Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
116
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import _LazyModule _lowerCAmelCase : List[str] = {'''tokenization_byt5''': ['''ByT5Tokenizer''']} if TYPE_CHECKING: from .tokenization_byta import ByTaTokenizer else: import sys _lowerCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
340
"""simple docstring""" import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def lowerCamelCase_( _lowerCamelCase ) -> Optional[int]: '''simple docstring''' _lowerCamelCase : Optional[Any] = np.inf def set_batch_size(_lowerCamelCase ) -> None: nonlocal batch_size if isinstance(_lowerCamelCase , _lowerCamelCase ): _lowerCamelCase : Optional[int] = min(_lowerCamelCase , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(_lowerCamelCase , _lowerCamelCase ): _lowerCamelCase : Union[str, Any] = min(_lowerCamelCase , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(_lowerCamelCase , _lowerCamelCase ) and feature.dtype == "binary": _lowerCamelCase : List[str] = min(_lowerCamelCase , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(_lowerCamelCase , _lowerCamelCase ) return None if batch_size is np.inf else batch_size class A_ ( _a ): def __init__( self: Optional[int] ,__lowerCAmelCase: NestedDataStructureLike[PathLike] ,__lowerCAmelCase: Optional[NamedSplit] = None ,__lowerCAmelCase: Optional[Features] = None ,__lowerCAmelCase: str = None ,__lowerCAmelCase: bool = False ,__lowerCAmelCase: bool = False ,__lowerCAmelCase: Optional[int] = None ,**__lowerCAmelCase: int ,): '''simple docstring''' super().__init__( __lowerCAmelCase ,split=__lowerCAmelCase ,features=__lowerCAmelCase ,cache_dir=__lowerCAmelCase ,keep_in_memory=__lowerCAmelCase ,streaming=__lowerCAmelCase ,num_proc=__lowerCAmelCase ,**__lowerCAmelCase ,) _lowerCamelCase : Tuple = path_or_paths if isinstance(__lowerCAmelCase ,__lowerCAmelCase ) else {self.split: path_or_paths} _lowerCamelCase : Any = _PACKAGED_DATASETS_MODULES["parquet"][1] _lowerCamelCase : int = Parquet( cache_dir=__lowerCAmelCase ,data_files=__lowerCAmelCase ,features=__lowerCAmelCase ,hash=__lowerCAmelCase ,**__lowerCAmelCase ,) def _lowercase ( self: Optional[int] ): '''simple docstring''' if self.streaming: _lowerCamelCase : List[Any] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: _lowerCamelCase : Tuple = None _lowerCamelCase : Optional[int] = None _lowerCamelCase : List[str] = None _lowerCamelCase : str = None self.builder.download_and_prepare( download_config=__lowerCAmelCase ,download_mode=__lowerCAmelCase ,verification_mode=__lowerCAmelCase ,base_path=__lowerCAmelCase ,num_proc=self.num_proc ,) _lowerCamelCase : Any = self.builder.as_dataset( split=self.split ,verification_mode=__lowerCAmelCase ,in_memory=self.keep_in_memory ) return dataset class A_ : def __init__( self: str ,__lowerCAmelCase: Dataset ,__lowerCAmelCase: Union[PathLike, BinaryIO] ,__lowerCAmelCase: Optional[int] = None ,**__lowerCAmelCase: List[Any] ,): '''simple docstring''' _lowerCamelCase : Any = dataset _lowerCamelCase : Any = path_or_buf _lowerCamelCase : Any = batch_size or get_writer_batch_size(dataset.features ) _lowerCamelCase : List[str] = parquet_writer_kwargs def _lowercase ( self: Tuple ): '''simple docstring''' _lowerCamelCase : Tuple = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf ,(str, bytes, os.PathLike) ): with open(self.path_or_buf ,"wb+" ) as buffer: _lowerCamelCase : str = self._write(file_obj=__lowerCAmelCase ,batch_size=__lowerCAmelCase ,**self.parquet_writer_kwargs ) else: _lowerCamelCase : Optional[int] = self._write(file_obj=self.path_or_buf ,batch_size=__lowerCAmelCase ,**self.parquet_writer_kwargs ) return written def _lowercase ( self: Optional[Any] ,__lowerCAmelCase: BinaryIO ,__lowerCAmelCase: int ,**__lowerCAmelCase: Optional[int] ): '''simple docstring''' _lowerCamelCase : List[str] = 0 _lowerCamelCase : Optional[int] = parquet_writer_kwargs.pop("path_or_buf" ,__lowerCAmelCase ) _lowerCamelCase : List[str] = self.dataset.features.arrow_schema _lowerCamelCase : str = pq.ParquetWriter(__lowerCAmelCase ,schema=__lowerCAmelCase ,**__lowerCAmelCase ) for offset in logging.tqdm( range(0 ,len(self.dataset ) ,__lowerCAmelCase ) ,unit="ba" ,disable=not logging.is_progress_bar_enabled() ,desc="Creating parquet from Arrow format" ,): _lowerCamelCase : List[str] = query_table( table=self.dataset._data ,key=slice(__lowerCAmelCase ,offset + batch_size ) ,indices=self.dataset._indices if self.dataset._indices is not None else None ,) writer.write_table(__lowerCAmelCase ) written += batch.nbytes writer.close() return written
340
1
"""simple docstring""" from collections import defaultdict def __A ( a_ :str , a_ :str) -> bool: __a : Tuple = first_str.lower().strip() __a : Dict = second_str.lower().strip() # Remove whitespace __a : List[str] = first_str.replace(''' ''' , '''''') __a : List[Any] = second_str.replace(''' ''' , '''''') # Strings of different lengths are not anagrams if len(lowerCamelCase__) != len(lowerCamelCase__): return False # Default values for count should be 0 __a : defaultdict[str, int] = defaultdict(lowerCamelCase__) # For each character in input strings, # increment count in the corresponding for i in range(len(lowerCamelCase__)): count[first_str[i]] += 1 count[second_str[i]] -= 1 return all(_count == 0 for _count in count.values()) if __name__ == "__main__": from doctest import testmod testmod() A = input('''Enter the first string ''').strip() A = input('''Enter the second string ''').strip() A = check_anagrams(input_a, input_b) print(F'{input_a} and {input_b} are {"" if status else "not "}anagrams.')
160
"""simple docstring""" import os def _snake_case ( ) -> Dict: with open(os.path.dirname(lowerCamelCase__ ) + "/p022_names.txt" ) as file: lowerCamelCase_ : str =str(file.readlines()[0] ) lowerCamelCase_ : Union[str, Any] =names.replace("\"" , "" ).split("," ) names.sort() lowerCamelCase_ : str =0 lowerCamelCase_ : Optional[int] =0 for i, name in enumerate(lowerCamelCase__ ): for letter in name: name_score += ord(lowerCamelCase__ ) - 64 total_score += (i + 1) * name_score lowerCamelCase_ : List[Any] =0 return total_score if __name__ == "__main__": print(solution())
144
0
from math import pi def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ ): '''simple docstring''' return 2 * pi * radius * (angle / 360) if __name__ == "__main__": print(arc_length(9_0, 1_0))
367
"""simple docstring""" import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class UpperCamelCase_ ( UpperCamelCase , UpperCamelCase , unittest.TestCase): """simple docstring""" snake_case__ : str = IFInpaintingSuperResolutionPipeline snake_case__ : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ : Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"}) snake_case__ : Dict = PipelineTesterMixin.required_optional_params - {"latents"} def UpperCAmelCase_ ( self : List[Any] ) -> Optional[int]: return self._get_superresolution_dummy_components() def UpperCAmelCase_ ( self : List[str] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[str]=0 ) -> List[str]: if str(UpperCAmelCase__ ).startswith("mps" ): __SCREAMING_SNAKE_CASE = torch.manual_seed(UpperCAmelCase__ ) else: __SCREAMING_SNAKE_CASE = torch.Generator(device=UpperCAmelCase__ ).manual_seed(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 1_6, 1_6) , rng=random.Random(UpperCAmelCase__ ) ).to(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 3_2, 3_2) , rng=random.Random(UpperCAmelCase__ ) ).to(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 3_2, 3_2) , rng=random.Random(UpperCAmelCase__ ) ).to(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = { "prompt": "A painting of a squirrel eating a burger", "image": image, "original_image": original_image, "mask_image": mask_image, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def UpperCAmelCase_ ( self : str ) -> Dict: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def UpperCAmelCase_ ( self : str ) -> Tuple: self._test_save_load_optional_components() @unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA" ) def UpperCAmelCase_ ( self : Tuple ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1E-1 ) def UpperCAmelCase_ ( self : Optional[Any] ) -> List[Any]: self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def UpperCAmelCase_ ( self : Any ) -> Union[str, Any]: self._test_save_load_local() def UpperCAmelCase_ ( self : Union[str, Any] ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
195
0
"""simple docstring""" import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import MaMaaaTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from transformers.utils import is_sentencepiece_available if is_sentencepiece_available(): from transformers.models.mam_aaa.tokenization_mam_aaa import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin if is_sentencepiece_available(): lowercase__ = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right lowercase__ = 12_8022 lowercase__ = 12_8028 @require_sentencepiece class lowerCAmelCase__ ( lowerCAmelCase__, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MaMaaaTokenizer lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = True def A_ ( self ): super().setUp() _lowerCamelCase : Union[str, Any] = ["</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est", "\u0120", "<pad>"] _lowerCamelCase : str = dict(zip(UpperCamelCase__ , range(len(UpperCamelCase__ ) ) ) ) _lowerCamelCase : List[str] = Path(self.tmpdirname ) save_json(UpperCamelCase__ , save_dir / VOCAB_FILES_NAMES['vocab_file'] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(UpperCamelCase__ , save_dir / VOCAB_FILES_NAMES['spm_file'] ) _lowerCamelCase : Tuple = MaMaaaTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def A_ ( self , **lowercase ): return MaMaaaTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def A_ ( self , lowercase ): return ( "This is a test", "This is a test", ) def A_ ( self ): _lowerCamelCase : str = "</s>" _lowerCamelCase : int = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase__ ) , UpperCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase__ ) , UpperCamelCase__ ) def A_ ( self ): _lowerCamelCase : Tuple = self.get_tokenizer() _lowerCamelCase : int = list(tokenizer.get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '</s>' ) self.assertEqual(vocab_keys[1] , '<unk>' ) self.assertEqual(vocab_keys[-1] , '<s>' ) self.assertEqual(len(UpperCamelCase__ ) , tokenizer.vocab_size + len(tokenizer.get_added_vocab() ) ) @unittest.skip('Skip this test while all models are still to be uploaded.' ) def A_ ( self ): pass def A_ ( self ): _lowerCamelCase : List[Any] = self.get_tokenizer() _lowerCamelCase : Optional[Any] = tokenizer.tokenize('This is a test' ) self.assertListEqual(UpperCamelCase__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCamelCase__ ) , [2, 3, 4, 5, 6] , ) _lowerCamelCase : List[Any] = tokenizer.convert_ids_to_tokens([2, 3, 4, 5, 6] ) self.assertListEqual(UpperCamelCase__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) _lowerCamelCase : str = tokenizer.convert_tokens_to_string(UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , 'This is a test' ) @slow def A_ ( self ): # fmt: off _lowerCamelCase : Optional[int] = {"input_ids": [[128022, 110108, 397, 11, 38272, 2247, 124811, 285, 18105, 1586, 207, 7, 39534, 4428, 397, 1019, 18105, 1586, 207, 7, 41337, 16786, 241, 7, 20214, 17, 125690, 10398, 7, 44378, 58069, 68342, 7798, 7343, 11, 299, 33310, 4, 158, 37350, 94077, 4569, 299, 33310, 90, 4, 52840, 290, 4, 31270, 112, 299, 682, 4, 52840, 39953, 14079, 193, 52519, 90894, 17894, 120697, 11, 40445, 551, 17, 1019, 52519, 90894, 17756, 963, 11, 40445, 480, 17, 9792, 1120, 5173, 1393, 6240, 16786, 241, 120996, 28, 1245, 1393, 118240, 11123, 1019, 93612, 2691, 10618, 98058, 120409, 1928, 279, 4, 40683, 367, 178, 207, 1019, 103, 103121, 506, 65296, 5, 2], [128022, 21217, 367, 117, 125450, 128, 719, 7, 7308, 40, 93612, 12669, 1116, 16704, 71, 17785, 3699, 15592, 35, 144, 9584, 241, 11943, 713, 950, 799, 2247, 88427, 150, 149, 118813, 120706, 1019, 106906, 81518, 28, 1224, 22799, 397, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [128022, 1658, 123311, 5155, 5578, 4722, 279, 14947, 2366, 1120, 1197, 14, 1348, 9232, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCamelCase__ , model_name='facebook/m2m100_418M' , revision='c168bae485c864188cf9aa0e4108b0b6934dc91e' , ) @require_torch @require_sentencepiece @require_tokenizers class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = """facebook/m2m100_418M""" lowerCamelCase__ = [ """In my opinion, there are two levels of response from the French government.""", """NSA Affair Emphasizes Complete Lack of Debate on Intelligence""", ] lowerCamelCase__ = [ """Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.""", """L'affaire NSA souligne l'absence totale de débat sur le renseignement""", ] # fmt: off lowerCamelCase__ = [EN_CODE, 5_93, 19_49, 11_57_81, 4, 7_15_86, 42_34, 6_06_33, 12_62_33, 4_32, 12_38_08, 1_55_92, 11_97, 11_71_32, 12_06_18, 5, 2] @classmethod def A_ ( cls ): _lowerCamelCase : MaMaaaTokenizer = MaMaaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='en' , tgt_lang='fr' ) _lowerCamelCase : Dict = 1 return cls def A_ ( self ): self.assertEqual(self.tokenizer.get_lang_id('ar' ) , 128006 ) self.assertEqual(self.tokenizer.get_lang_id('en' ) , 128022 ) self.assertEqual(self.tokenizer.get_lang_id('ro' ) , 128076 ) self.assertEqual(self.tokenizer.get_lang_id('mr' ) , 128063 ) def A_ ( self ): _lowerCamelCase : List[str] = self.tokenizer.get_vocab() self.assertEqual(len(UpperCamelCase__ ) , self.tokenizer.vocab_size ) self.assertEqual(vocab['<unk>'] , 3 ) self.assertIn(self.tokenizer.get_lang_token('en' ) , UpperCamelCase__ ) def A_ ( self ): _lowerCamelCase : Any = "en" _lowerCamelCase : Optional[Any] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , UpperCamelCase__ ) def A_ ( self ): self.assertIn(UpperCamelCase__ , self.tokenizer.all_special_ids ) # fmt: off _lowerCamelCase : Any = [FR_CODE, 5364, 82, 8642, 4, 294, 47, 8, 14028, 136, 3286, 9706, 6, 90797, 6, 144012, 162, 88128, 30061, 5, 2] # fmt: on _lowerCamelCase : List[str] = self.tokenizer.decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) _lowerCamelCase : Any = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) self.assertNotIn(self.tokenizer.eos_token , UpperCamelCase__ ) def A_ ( self ): _lowerCamelCase : List[Any] = tempfile.mkdtemp() _lowerCamelCase : List[Any] = self.tokenizer.lang_token_to_id self.tokenizer.save_pretrained(UpperCamelCase__ ) _lowerCamelCase : Tuple = MaMaaaTokenizer.from_pretrained(UpperCamelCase__ ) self.assertDictEqual(new_tok.lang_token_to_id , UpperCamelCase__ ) @require_torch def A_ ( self ): _lowerCamelCase : Any = "en" _lowerCamelCase : Optional[Any] = "fr" _lowerCamelCase : List[Any] = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=UpperCamelCase__ , return_tensors='pt' ) _lowerCamelCase : Dict = shift_tokens_right( batch['labels'] , self.tokenizer.pad_token_id , self.tokenizer.eos_token_id ) for k in batch: _lowerCamelCase : Any = batch[k].tolist() # batch = {k: v.tolist() for k,v in batch.items()} # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 # batch.decoder_inputs_ids[0][0] == assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == FR_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2] == [2, FR_CODE] @require_torch def A_ ( self ): _lowerCamelCase : Union[str, Any] = "mr" self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('mr' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) _lowerCamelCase : List[str] = "zh" self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('zh' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) @require_torch def A_ ( self ): _lowerCamelCase : List[Any] = "mr" self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('mr' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) _lowerCamelCase : Dict = "zh" self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('zh' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) @require_torch def A_ ( self ): _lowerCamelCase : List[Any] = self.tokenizer._build_translation_inputs('A test' , return_tensors='pt' , src_lang='en' , tgt_lang='ar' ) self.assertEqual( nested_simplify(UpperCamelCase__ ) , { # en_XX, A, test, EOS 'input_ids': [[128022, 58, 4183, 2]], 'attention_mask': [[1, 1, 1, 1]], # ar_AR 'forced_bos_token_id': 128006, } , )
96
from math import sqrt def A ( _SCREAMING_SNAKE_CASE = 100_0000 ) -> int: lowerCamelCase : int = 0 lowerCamelCase : int = 0 lowerCamelCase : int while num_cuboids <= limit: max_cuboid_size += 1 for sum_shortest_sides in range(2 ,2 * max_cuboid_size + 1 ): if sqrt(sum_shortest_sides**2 + max_cuboid_size**2 ).is_integer(): num_cuboids += ( min(_SCREAMING_SNAKE_CASE ,sum_shortest_sides // 2 ) - max(1 ,sum_shortest_sides - max_cuboid_size ) + 1 ) return max_cuboid_size if __name__ == "__main__": print(f'''{solution() = }''')
48
0
"""simple docstring""" from __future__ import annotations def lowercase ( _snake_case : list[int] , _snake_case : int ) ->bool: """simple docstring""" if len(_snake_case ) == 0: return False __snake_case : int = len(_snake_case ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , _snake_case ) else: return binary_search(a_list[midpoint + 1 :] , _snake_case ) if __name__ == "__main__": SCREAMING_SNAKE_CASE : List[str] = input("""Enter numbers separated by comma:\n""").strip() SCREAMING_SNAKE_CASE : int = [int(item.strip()) for item in user_input.split(""",""")] SCREAMING_SNAKE_CASE : List[Any] = int(input("""Enter the number to be found in the list:\n""").strip()) SCREAMING_SNAKE_CASE : Dict = """""" if binary_search(sequence, target) else """not """ print(F'{target} was {not_str}found in {sequence}')
24
"""simple docstring""" def lowercase ( _snake_case : int = 100 ) ->int: """simple docstring""" __snake_case : str = n * (n + 1) * (2 * n + 1) / 6 __snake_case : Dict = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(F'{solution() = }')
24
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class lowerCAmelCase_ ( _lowercase , unittest.TestCase ): '''simple docstring''' _lowerCamelCase: Union[str, Any] = KandinskyVaaImgaImgPipeline _lowerCamelCase: Tuple = ['''image_embeds''', '''negative_image_embeds''', '''image'''] _lowerCamelCase: Any = [ '''image_embeds''', '''negative_image_embeds''', '''image''', ] _lowerCamelCase: int = [ '''generator''', '''height''', '''width''', '''strength''', '''guidance_scale''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] _lowerCamelCase: Dict = False @property def _SCREAMING_SNAKE_CASE ( self : Any ) -> Tuple: return 32 @property def _SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> str: return 32 @property def _SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[int]: return self.time_input_dim @property def _SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self.time_input_dim * 4 @property def _SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[Any]: return 100 @property def _SCREAMING_SNAKE_CASE ( self : int ) -> Any: torch.manual_seed(0 ) A = { 'in_channels': 4, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } A = UNetaDConditionModel(**A_ ) return model @property def _SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Dict: return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[int]: torch.manual_seed(0 ) A = VQModel(**self.dummy_movq_kwargs ) return model def _SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: A = self.dummy_unet A = self.dummy_movq A = { 'num_train_timesteps': 1000, 'beta_schedule': 'linear', 'beta_start': 0.0_00_85, 'beta_end': 0.0_12, 'clip_sample': False, 'set_alpha_to_one': False, 'steps_offset': 0, 'prediction_type': 'epsilon', 'thresholding': False, } A = DDIMScheduler(**A_ ) A = { 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def _SCREAMING_SNAKE_CASE ( self : Optional[int] ,A_ : List[str] ,A_ : Union[str, Any]=0 ) -> Optional[int]: A = floats_tensor((1, self.text_embedder_hidden_size) ,rng=random.Random(A_ ) ).to(A_ ) A = floats_tensor((1, self.text_embedder_hidden_size) ,rng=random.Random(seed + 1 ) ).to( A_ ) # create init_image A = floats_tensor((1, 3, 64, 64) ,rng=random.Random(A_ ) ).to(A_ ) A = image.cpu().permute(0 ,2 ,3 ,1 )[0] A = Image.fromarray(np.uinta(A_ ) ).convert('RGB' ).resize((256, 256) ) if str(A_ ).startswith('mps' ): A = torch.manual_seed(A_ ) else: A = torch.Generator(device=A_ ).manual_seed(A_ ) A = { 'image': init_image, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 10, 'guidance_scale': 7.0, 'strength': 0.2, 'output_type': 'np', } return inputs def _SCREAMING_SNAKE_CASE ( self : int ) -> Tuple: A = 'cpu' A = self.get_dummy_components() A = self.pipeline_class(**A_ ) A = pipe.to(A_ ) pipe.set_progress_bar_config(disable=A_ ) A = pipe(**self.get_dummy_inputs(A_ ) ) A = output.images A = pipe( **self.get_dummy_inputs(A_ ) ,return_dict=A_ ,)[0] A = image[0, -3:, -3:, -1] A = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) A = np.array( [0.6_19_97_78, 0.63_98_44_06, 0.46_14_57_85, 0.62_94_49_84, 0.5_62_22_15, 0.47_30_61_32, 0.47_44_14_56, 0.4_60_76_06, 0.48_71_92_63] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[int]: A = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinskyv22/kandinskyv22_img2img_frog.npy' ) A = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) A = 'A red cartoon frog, 4k' A = KandinskyVaaPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior' ,torch_dtype=torch.floataa ) pipe_prior.to(A_ ) A = KandinskyVaaImgaImgPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-decoder' ,torch_dtype=torch.floataa ) A = pipeline.to(A_ ) pipeline.set_progress_bar_config(disable=A_ ) A = torch.Generator(device='cpu' ).manual_seed(0 ) A , A = pipe_prior( A_ ,generator=A_ ,num_inference_steps=5 ,negative_prompt='' ,).to_tuple() A = pipeline( image=A_ ,image_embeds=A_ ,negative_image_embeds=A_ ,generator=A_ ,num_inference_steps=100 ,height=768 ,width=768 ,strength=0.2 ,output_type='np' ,) A = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(A_ ,A_ )
74
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline __snake_case : Dict =logging.get_logger(__name__) @add_end_docstrings(lowerCamelCase__) class lowerCamelCase__ ( lowerCamelCase__): '''simple docstring''' def __init__(self ,**__lowerCamelCase ) -> Optional[Any]: """simple docstring""" super().__init__(**__lowerCamelCase ) if self.framework != "pt": raise ValueError(f"""The {self.__class__} is only available in PyTorch.""" ) # No specific FOR_XXX available yet def __call__(self ,__lowerCamelCase ,**__lowerCamelCase ) -> List[Any]: """simple docstring""" return super().__call__(__lowerCamelCase ,**__lowerCamelCase ) def lowerCAmelCase__ (self ,**__lowerCamelCase ) -> Any: """simple docstring""" lowerCAmelCase__ : str = {} if "candidate_labels" in kwargs: lowerCAmelCase__ : List[str] = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: lowerCAmelCase__ : int = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def lowerCAmelCase__ (self ,__lowerCamelCase ,__lowerCamelCase=None ,__lowerCamelCase="This is a sound of {}." ) -> str: """simple docstring""" if isinstance(__lowerCamelCase ,__lowerCamelCase ): if audio.startswith('''http://''' ) or audio.startswith('''https://''' ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png lowerCAmelCase__ : List[str] = requests.get(__lowerCamelCase ).content else: with open(__lowerCamelCase ,'''rb''' ) as f: lowerCAmelCase__ : int = f.read() if isinstance(__lowerCamelCase ,__lowerCamelCase ): lowerCAmelCase__ : Tuple = ffmpeg_read(__lowerCamelCase ,self.feature_extractor.sampling_rate ) if not isinstance(__lowerCamelCase ,np.ndarray ): raise ValueError('''We expect a numpy ndarray as input''' ) if len(audio.shape ) != 1: raise ValueError('''We expect a single channel audio input for ZeroShotAudioClassificationPipeline''' ) lowerCAmelCase__ : Any = self.feature_extractor( [audio] ,sampling_rate=self.feature_extractor.sampling_rate ,return_tensors='''pt''' ) lowerCAmelCase__ : Union[str, Any] = candidate_labels lowerCAmelCase__ : str = [hypothesis_template.format(__lowerCamelCase ) for x in candidate_labels] lowerCAmelCase__ : Any = self.tokenizer(__lowerCamelCase ,return_tensors=self.framework ,padding=__lowerCamelCase ) lowerCAmelCase__ : List[Any] = [text_inputs] return inputs def lowerCAmelCase__ (self ,__lowerCamelCase ) -> Dict: """simple docstring""" lowerCAmelCase__ : Union[str, Any] = model_inputs.pop('''candidate_labels''' ) lowerCAmelCase__ : List[str] = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] ,__lowerCamelCase ): lowerCAmelCase__ : List[str] = text_inputs[0] else: # Batching case. lowerCAmelCase__ : List[str] = text_inputs[0][0] lowerCAmelCase__ : Union[str, Any] = self.model(**__lowerCamelCase ,**__lowerCamelCase ) lowerCAmelCase__ : Any = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_audio, } return model_outputs def lowerCAmelCase__ (self ,__lowerCamelCase ) -> Optional[int]: """simple docstring""" lowerCAmelCase__ : Optional[int] = model_outputs.pop('''candidate_labels''' ) lowerCAmelCase__ : Optional[Any] = model_outputs['''logits'''][0] if self.framework == "pt": lowerCAmelCase__ : str = logits.softmax(dim=0 ) lowerCAmelCase__ : Dict = probs.tolist() else: raise ValueError('''`tf` framework not supported.''' ) lowerCAmelCase__ : Any = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(__lowerCamelCase ,__lowerCamelCase ) ,key=lambda __lowerCamelCase : -x[0] ) ] return result
129
0
'''simple docstring''' import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __UpperCAmelCase ( unittest.TestCase ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_=13 , lowerCAmelCase_=7 , lowerCAmelCase_=True , lowerCAmelCase_=True , lowerCAmelCase_=True , lowerCAmelCase_=True , lowerCAmelCase_=99 , lowerCAmelCase_=32 , lowerCAmelCase_=5 , lowerCAmelCase_=4 , lowerCAmelCase_=37 , lowerCAmelCase_="gelu" , lowerCAmelCase_=0.1 , lowerCAmelCase_=0.1 , lowerCAmelCase_=5_12 , lowerCAmelCase_=16 , lowerCAmelCase_=2 , lowerCAmelCase_=0.02 , lowerCAmelCase_=4 , ): """simple docstring""" _snake_case = parent _snake_case = batch_size _snake_case = seq_length _snake_case = is_training _snake_case = use_attention_mask _snake_case = use_token_type_ids _snake_case = use_labels _snake_case = vocab_size _snake_case = hidden_size _snake_case = num_hidden_layers _snake_case = num_attention_heads _snake_case = intermediate_size _snake_case = hidden_act _snake_case = hidden_dropout_prob _snake_case = attention_probs_dropout_prob _snake_case = max_position_embeddings _snake_case = type_vocab_size _snake_case = type_sequence_label_size _snake_case = initializer_range _snake_case = num_choices def lowerCamelCase ( self ): """simple docstring""" _snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _snake_case = None if self.use_attention_mask: _snake_case = random_attention_mask([self.batch_size, self.seq_length] ) _snake_case = None if self.use_token_type_ids: _snake_case = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _snake_case = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowercase_ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCamelCase ( self ): """simple docstring""" _snake_case = self.prepare_config_and_inputs() _snake_case , _snake_case , _snake_case , _snake_case = config_and_inputs _snake_case = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask} return config, inputs_dict @require_flax class __UpperCAmelCase ( _lowerCamelCase , unittest.TestCase ): __lowercase = True __lowercase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = FlaxRoFormerModelTester(self ) @slow def lowerCamelCase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: _snake_case = model_class_name.from_pretrained('junnyu/roformer_chinese_small' , from_pt=lowercase_ ) _snake_case = model(np.ones((1, 1) ) ) self.assertIsNotNone(lowercase_ ) @require_flax class __UpperCAmelCase ( unittest.TestCase ): @slow def lowerCamelCase ( self ): """simple docstring""" _snake_case = FlaxRoFormerForMaskedLM.from_pretrained('junnyu/roformer_chinese_base' ) _snake_case = jnp.array([[0, 1, 2, 3, 4, 5]] ) _snake_case = model(lowercase_ )[0] _snake_case = 5_00_00 _snake_case = (1, 6, vocab_size) self.assertEqual(output.shape , lowercase_ ) _snake_case = jnp.array( [[[-0.1205, -1.0265, 0.2922], [-1.5134, 0.1974, 0.1519], [-5.0135, -3.9003, -0.8404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , lowercase_ , atol=1E-4 ) )
369
'''simple docstring''' import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch lowercase : Any = logging.get_logger(__name__) class __UpperCAmelCase : def __init__( self , lowerCAmelCase_ = None , lowerCAmelCase_ = None , lowerCAmelCase_=None , lowerCAmelCase_=None ): """simple docstring""" if not conversation_id: _snake_case = uuid.uuida() if past_user_inputs is None: _snake_case = [] if generated_responses is None: _snake_case = [] _snake_case = conversation_id _snake_case = past_user_inputs _snake_case = generated_responses _snake_case = text def __eq__( self , lowerCAmelCase_ ): """simple docstring""" if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ = False ): """simple docstring""" if self.new_user_input: if overwrite: logger.warning( F'User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ' F'with: "{text}".' ) _snake_case = text else: logger.warning( F'User input added while unprocessed input was existing: "{self.new_user_input}" new input ' F'ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input' ) else: _snake_case = text def lowerCamelCase ( self ): """simple docstring""" if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) _snake_case = None def lowerCamelCase ( self , lowerCAmelCase_ ): """simple docstring""" self.generated_responses.append(lowerCAmelCase_ ) def lowerCamelCase ( self ): """simple docstring""" for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self ): """simple docstring""" _snake_case = F'Conversation id: {self.uuid} \n' for is_user, text in self.iter_texts(): _snake_case = 'user' if is_user else 'bot' output += F'{name} >> {text} \n' return output @add_end_docstrings( _lowerCamelCase , r""" min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. """ , ) class __UpperCAmelCase ( _lowerCamelCase ): def __init__( self , *lowerCAmelCase_ , **lowerCAmelCase_ ): """simple docstring""" super().__init__(*lowerCAmelCase_ , **lowerCAmelCase_ ) if self.tokenizer.pad_token_id is None: _snake_case = self.tokenizer.eos_token def lowerCamelCase ( self , lowerCAmelCase_=None , lowerCAmelCase_=None , lowerCAmelCase_=None , **lowerCAmelCase_ ): """simple docstring""" _snake_case = {} _snake_case = {} _snake_case = {} if min_length_for_response is not None: _snake_case = min_length_for_response if minimum_tokens is not None: _snake_case = minimum_tokens if "max_length" in generate_kwargs: _snake_case = generate_kwargs['max_length'] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: _snake_case = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(lowerCAmelCase_ ) return preprocess_params, forward_params, postprocess_params def __call__( self , lowerCAmelCase_ , lowerCAmelCase_=0 , **lowerCAmelCase_ ): """simple docstring""" _snake_case = super().__call__(lowerCAmelCase_ , num_workers=lowerCAmelCase_ , **lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) and len(lowerCAmelCase_ ) == 1: return outputs[0] return outputs def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=32 ): """simple docstring""" if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): raise ValueError('ConversationalPipeline, expects Conversation as inputs' ) if conversation.new_user_input is None: raise ValueError( F'Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ' 'Add user inputs with the conversation\'s `add_user_input` method' ) if hasattr(self.tokenizer , '_build_conversation_input_ids' ): _snake_case = self.tokenizer._build_conversation_input_ids(lowerCAmelCase_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version _snake_case = self._legacy_parse_and_tokenize(lowerCAmelCase_ ) if self.framework == "pt": _snake_case = torch.LongTensor([input_ids] ) elif self.framework == "tf": _snake_case = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=10 , **lowerCAmelCase_ ): """simple docstring""" _snake_case = generate_kwargs.get('max_length' , self.model.config.max_length ) _snake_case = model_inputs['input_ids'].shape[1] if max_length - minimum_tokens < n: logger.warning(F'Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})' ) _snake_case = max_length - minimum_tokens _snake_case = model_inputs['input_ids'][:, -trim:] if "attention_mask" in model_inputs: _snake_case = model_inputs['attention_mask'][:, -trim:] _snake_case = model_inputs.pop('conversation' ) _snake_case = max_length _snake_case = self.model.generate(**lowerCAmelCase_ , **lowerCAmelCase_ ) if self.model.config.is_encoder_decoder: _snake_case = 1 else: _snake_case = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def lowerCamelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=True ): """simple docstring""" _snake_case = model_outputs['output_ids'] _snake_case = self.tokenizer.decode( output_ids[0] , skip_special_tokens=lowerCAmelCase_ , clean_up_tokenization_spaces=lowerCAmelCase_ , ) _snake_case = model_outputs['conversation'] conversation.mark_processed() conversation.append_response(lowerCAmelCase_ ) return conversation def lowerCamelCase ( self , lowerCAmelCase_ ): """simple docstring""" _snake_case = self.tokenizer.eos_token_id _snake_case = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(lowerCAmelCase_ , add_special_tokens=lowerCAmelCase_ ) ) if len(lowerCAmelCase_ ) > self.tokenizer.model_max_length: _snake_case = input_ids[-self.tokenizer.model_max_length :] return input_ids
160
0
import random from .binary_exp_mod import bin_exp_mod def _a ( UpperCamelCase_ : Union[str, Any] , UpperCamelCase_ : Any=1_000 ) -> str: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd lowerCAmelCase__ = n - 1 lowerCAmelCase__ = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) lowerCAmelCase__ = 0 while count < prec: lowerCAmelCase__ = random.randint(2 , n - 1 ) lowerCAmelCase__ = bin_exp_mod(UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) if b != 1: lowerCAmelCase__ = True for _ in range(UpperCamelCase_ ): if b == n - 1: lowerCAmelCase__ = False break lowerCAmelCase__ = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": a_ = abs(int(input('''Enter bound : ''').strip())) print('''Here\'s the list of primes:''') print(''', '''.join(str(i) for i in range(n + 1) if is_prime_big(i)))
340
import collections import importlib.util import os import re from pathlib import Path a_ = '''src/transformers''' # Matches is_xxx_available() a_ = re.compile(r'''is\_([a-z_]*)_available()''') # Catches a one-line _import_struct = {xxx} a_ = re.compile(r'''^_import_structure\s+=\s+\{([^\}]+)\}''') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] a_ = re.compile(r'''\s+"\S*":\s+\[([^\]]*)\]''') # Catches a line if not is_foo_available a_ = re.compile(r'''^\s*if\s+not\s+is\_[a-z_]*\_available\(\)''') # Catches a line _import_struct["bla"].append("foo") a_ = re.compile(r'''^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)''') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] a_ = re.compile(r'''^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]''') # Catches a line with an object between quotes and a comma: "MyModel", a_ = re.compile('''^\s+"([^"]+)",''') # Catches a line with objects between brackets only: ["foo", "bar"], a_ = re.compile('''^\s+\[([^\]]+)\]''') # Catches a line with from foo import bar, bla, boo a_ = re.compile(r'''\s+from\s+\S*\s+import\s+([^\(\s].*)\n''') # Catches a line with try: a_ = re.compile(r'''^\s*try:''') # Catches a line with else: a_ = re.compile(r'''^\s*else:''') def _a ( UpperCamelCase_ : Union[str, Any] ) -> List[str]: """simple docstring""" if _re_test_backend.search(UpperCamelCase_ ) is None: return None lowerCAmelCase__ = [b[0] for b in _re_backend.findall(UpperCamelCase_ )] backends.sort() return "_and_".join(UpperCamelCase_ ) def _a ( UpperCamelCase_ : Optional[int] ) -> Tuple: """simple docstring""" with open(UpperCamelCase_ , "r" , encoding="utf-8" , newline="\n" ) as f: lowerCAmelCase__ = f.readlines() lowerCAmelCase__ = 0 while line_index < len(UpperCamelCase_ ) and not lines[line_index].startswith("_import_structure = {" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(UpperCamelCase_ ): return None # First grab the objects without a specific backend in _import_structure lowerCAmelCase__ = [] while not lines[line_index].startswith("if TYPE_CHECKING" ) and find_backend(lines[line_index] ) is None: lowerCAmelCase__ = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(UpperCamelCase_ ): lowerCAmelCase__ = _re_one_line_import_struct.search(UpperCamelCase_ ).groups()[0] lowerCAmelCase__ = re.findall("\[([^\]]+)\]" , UpperCamelCase_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(", " )] ) line_index += 1 continue lowerCAmelCase__ = _re_import_struct_key_value.search(UpperCamelCase_ ) if single_line_import_search is not None: lowerCAmelCase__ = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(", " ) if len(UpperCamelCase_ ) > 0] objects.extend(UpperCamelCase_ ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) line_index += 1 lowerCAmelCase__ = {"none": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("if TYPE_CHECKING" ): # If the line is an if not is_backend_available, we grab all objects associated. lowerCAmelCase__ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: lowerCAmelCase__ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 lowerCAmelCase__ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 4 ): lowerCAmelCase__ = lines[line_index] if _re_import_struct_add_one.search(UpperCamelCase_ ) is not None: objects.append(_re_import_struct_add_one.search(UpperCamelCase_ ).groups()[0] ) elif _re_import_struct_add_many.search(UpperCamelCase_ ) is not None: lowerCAmelCase__ = _re_import_struct_add_many.search(UpperCamelCase_ ).groups()[0].split(", " ) lowerCAmelCase__ = [obj[1:-1] for obj in imports if len(UpperCamelCase_ ) > 0] objects.extend(UpperCamelCase_ ) elif _re_between_brackets.search(UpperCamelCase_ ) is not None: lowerCAmelCase__ = _re_between_brackets.search(UpperCamelCase_ ).groups()[0].split(", " ) lowerCAmelCase__ = [obj[1:-1] for obj in imports if len(UpperCamelCase_ ) > 0] objects.extend(UpperCamelCase_ ) elif _re_quote_object.search(UpperCamelCase_ ) is not None: objects.append(_re_quote_object.search(UpperCamelCase_ ).groups()[0] ) elif line.startswith(" " * 8 + "\"" ): objects.append(line[9:-3] ) elif line.startswith(" " * 12 + "\"" ): objects.append(line[13:-3] ) line_index += 1 lowerCAmelCase__ = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend lowerCAmelCase__ = [] while ( line_index < len(UpperCamelCase_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("else" ) ): lowerCAmelCase__ = lines[line_index] lowerCAmelCase__ = _re_import.search(UpperCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 lowerCAmelCase__ = {"none": objects} # Let's continue with backend-specific objects while line_index < len(UpperCamelCase_ ): # If the line is an if is_backend_available, we grab all objects associated. lowerCAmelCase__ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: lowerCAmelCase__ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 lowerCAmelCase__ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(" " * 8 ): lowerCAmelCase__ = lines[line_index] lowerCAmelCase__ = _re_import.search(UpperCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 12 ): objects.append(line[12:-2] ) line_index += 1 lowerCAmelCase__ = objects else: line_index += 1 return import_dict_objects, type_hint_objects def _a ( UpperCamelCase_ : int , UpperCamelCase_ : Optional[Any] ) -> str: """simple docstring""" def find_duplicates(UpperCamelCase_ : str ): return [k for k, v in collections.Counter(UpperCamelCase_ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] lowerCAmelCase__ = [] for key in import_dict_objects.keys(): lowerCAmelCase__ = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"Duplicate _import_structure definitions for: {duplicate_imports}" ) lowerCAmelCase__ = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F"Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}" ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): lowerCAmelCase__ = "base imports" if key == "none" else F"{key} backend" errors.append(F"Differences for {name}:" ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F" {a} in TYPE_HINT but not in _import_structure." ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F" {a} in _import_structure but not in TYPE_HINT." ) return errors def _a ( ) -> List[Any]: """simple docstring""" lowerCAmelCase__ = [] for root, _, files in os.walk(UpperCamelCase_ ): if "__init__.py" in files: lowerCAmelCase__ = os.path.join(UpperCamelCase_ , "__init__.py" ) lowerCAmelCase__ = parse_init(UpperCamelCase_ ) if objects is not None: lowerCAmelCase__ = analyze_results(*UpperCamelCase_ ) if len(UpperCamelCase_ ) > 0: lowerCAmelCase__ = F"Problem in {fname}, both halves do not define the same objects.\n{errors[0]}" failures.append("\n".join(UpperCamelCase_ ) ) if len(UpperCamelCase_ ) > 0: raise ValueError("\n\n".join(UpperCamelCase_ ) ) def _a ( ) -> str: """simple docstring""" lowerCAmelCase__ = [] for path, directories, files in os.walk(UpperCamelCase_ ): for folder in directories: # Ignore private modules if folder.startswith("_" ): directories.remove(UpperCamelCase_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(UpperCamelCase_ ) / folder).glob("*.py" ) ) ) == 0: continue lowerCAmelCase__ = str((Path(UpperCamelCase_ ) / folder).relative_to(UpperCamelCase_ ) ) lowerCAmelCase__ = short_path.replace(os.path.sep , "." ) submodules.append(UpperCamelCase_ ) for fname in files: if fname == "__init__.py": continue lowerCAmelCase__ = str((Path(UpperCamelCase_ ) / fname).relative_to(UpperCamelCase_ ) ) lowerCAmelCase__ = short_path.replace(".py" , "" ).replace(os.path.sep , "." ) if len(submodule.split("." ) ) == 1: submodules.append(UpperCamelCase_ ) return submodules a_ = [ '''convert_pytorch_checkpoint_to_tf2''', '''modeling_flax_pytorch_utils''', ] def _a ( ) -> int: """simple docstring""" lowerCAmelCase__ = importlib.util.spec_from_file_location( "transformers" , os.path.join(UpperCamelCase_ , "__init__.py" ) , submodule_search_locations=[PATH_TO_TRANSFORMERS] , ) lowerCAmelCase__ = spec.loader.load_module() lowerCAmelCase__ = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys() ] if len(UpperCamelCase_ ) > 0: lowerCAmelCase__ = "\n".join(F"- {module}" for module in module_not_registered ) raise ValueError( "The following submodules are not properly registered in the main init of Transformers:\n" F"{list_of_modules}\n" "Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value." ) if __name__ == "__main__": check_all_inits() check_submodules()
340
1
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def UpperCamelCase ( __lowercase : int ,__lowercase : List[str] ): '''simple docstring''' assert isinstance(__lowercase ,__lowercase ) 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 @pytest.mark.parametrize('keep_in_memory' ,[False, True] ) def UpperCamelCase ( __lowercase : Union[str, Any] ,__lowercase : List[str] ,__lowercase : List[Any] ): '''simple docstring''' A_ : List[Any] = tmp_path / 'cache' A_ : Optional[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(): A_ : Optional[Any] = ParquetDatasetReader(__lowercase ,cache_dir=__lowercase ,keep_in_memory=__lowercase ).read() _check_parquet_dataset(__lowercase ,__lowercase ) @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 UpperCamelCase ( __lowercase : Any ,__lowercase : Tuple ,__lowercase : str ): '''simple docstring''' A_ : Tuple = tmp_path / 'cache' A_ : str = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A_ : List[str] = features.copy() if features else default_expected_features A_ : List[str] = ( Features({feature: Value(__lowercase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Tuple = ParquetDatasetReader(__lowercase ,features=__lowercase ,cache_dir=__lowercase ).read() _check_parquet_dataset(__lowercase ,__lowercase ) @pytest.mark.parametrize('split' ,[None, NamedSplit('train' ), 'train', 'test'] ) def UpperCamelCase ( __lowercase : Union[str, Any] ,__lowercase : List[Any] ,__lowercase : str ): '''simple docstring''' A_ : List[Any] = tmp_path / 'cache' A_ : Dict = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A_ : List[str] = ParquetDatasetReader(__lowercase ,cache_dir=__lowercase ,split=__lowercase ).read() _check_parquet_dataset(__lowercase ,__lowercase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('path_type' ,[str, list] ) def UpperCamelCase ( __lowercase : Dict ,__lowercase : List[str] ,__lowercase : List[str] ): '''simple docstring''' if issubclass(__lowercase ,__lowercase ): A_ : Any = parquet_path elif issubclass(__lowercase ,__lowercase ): A_ : List[Any] = [parquet_path] A_ : Tuple = tmp_path / 'cache' A_ : Union[str, Any] = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A_ : int = ParquetDatasetReader(__lowercase ,cache_dir=__lowercase ).read() _check_parquet_dataset(__lowercase ,__lowercase ) def UpperCamelCase ( __lowercase : List[str] ,__lowercase : Tuple ,__lowercase : str=("train",) ): '''simple docstring''' assert isinstance(__lowercase ,__lowercase ) for split in splits: A_ : Optional[int] = dataset_dict[split] 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 @pytest.mark.parametrize('keep_in_memory' ,[False, True] ) def UpperCamelCase ( __lowercase : Tuple ,__lowercase : List[Any] ,__lowercase : int ): '''simple docstring''' A_ : Dict = tmp_path / 'cache' A_ : 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(): A_ : int = ParquetDatasetReader( {'train': parquet_path} ,cache_dir=__lowercase ,keep_in_memory=__lowercase ).read() _check_parquet_datasetdict(__lowercase ,__lowercase ) @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 UpperCamelCase ( __lowercase : int ,__lowercase : Optional[int] ,__lowercase : Tuple ): '''simple docstring''' A_ : Any = tmp_path / 'cache' A_ : Tuple = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A_ : Union[str, Any] = features.copy() if features else default_expected_features A_ : Any = ( Features({feature: Value(__lowercase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : List[Any] = ParquetDatasetReader({'train': parquet_path} ,features=__lowercase ,cache_dir=__lowercase ).read() _check_parquet_datasetdict(__lowercase ,__lowercase ) @pytest.mark.parametrize('split' ,[None, NamedSplit('train' ), 'train', 'test'] ) def UpperCamelCase ( __lowercase : Optional[Any] ,__lowercase : Optional[Any] ,__lowercase : List[str] ): '''simple docstring''' if split: A_ : Tuple = {split: parquet_path} else: A_ : Optional[int] = 'train' A_ : int = {'train': parquet_path, 'test': parquet_path} A_ : List[str] = tmp_path / 'cache' A_ : int = {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'} A_ : Union[str, Any] = ParquetDatasetReader(__lowercase ,cache_dir=__lowercase ).read() _check_parquet_datasetdict(__lowercase ,__lowercase ,splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def UpperCamelCase ( __lowercase : Optional[int] ,__lowercase : Dict ): '''simple docstring''' A_ : Union[str, Any] = ParquetDatasetWriter(__lowercase ,tmp_path / 'foo.parquet' ) assert writer.write() > 0 A_ : List[Any] = pq.ParquetFile(tmp_path / 'foo.parquet' ) A_ : Dict = pf.read() assert dataset.data.table == output_table def UpperCamelCase ( __lowercase : Union[str, Any] ,__lowercase : Dict ): '''simple docstring''' A_ : Any = str(shared_datadir / 'test_image_rgb.jpg' ) A_ : Any = {'image': [image_path]} A_ : Any = Features({'image': Image()} ) A_ : int = Dataset.from_dict(__lowercase ,features=__lowercase ) A_ : Optional[int] = ParquetDatasetWriter(__lowercase ,tmp_path / 'foo.parquet' ) assert writer.write() > 0 A_ : int = Dataset.from_parquet(str(tmp_path / 'foo.parquet' ) ) assert dataset.features == reloaded_dataset.features A_ : Optional[int] = ParquetDatasetReader(str(tmp_path / 'foo.parquet' ) ,streaming=__lowercase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( 'feature, expected' ,[ (Features({'foo': Value('int32' )} ), None), (Features({'image': Image(), 'foo': Value('int32' )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({'nested': Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] ,) def UpperCamelCase ( __lowercase : Union[str, Any] ,__lowercase : Optional[int] ): '''simple docstring''' assert get_writer_batch_size(__lowercase ) == expected
192
import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 _UpperCAmelCase = { """return_dict""": False, """output_hidden_states""": True, """output_attentions""": True, """torchscript""": True, """torch_dtype""": """float16""", """use_bfloat16""": True, """tf_legacy_loss""": True, """pruned_heads""": {"""a""": 1}, """tie_word_embeddings""": False, """is_decoder""": True, """cross_attention_hidden_size""": 128, """add_cross_attention""": True, """tie_encoder_decoder""": True, """max_length""": 50, """min_length""": 3, """do_sample""": True, """early_stopping""": True, """num_beams""": 3, """num_beam_groups""": 3, """diversity_penalty""": 0.5, """temperature""": 2.0, """top_k""": 10, """top_p""": 0.7, """typical_p""": 0.2, """repetition_penalty""": 0.8, """length_penalty""": 0.8, """no_repeat_ngram_size""": 5, """encoder_no_repeat_ngram_size""": 5, """bad_words_ids""": [1, 2, 3], """num_return_sequences""": 3, """chunk_size_feed_forward""": 5, """output_scores""": True, """return_dict_in_generate""": True, """forced_bos_token_id""": 2, """forced_eos_token_id""": 3, """remove_invalid_values""": True, """architectures""": ["""BertModel"""], """finetuning_task""": """translation""", """id2label""": {0: """label"""}, """label2id""": {"""label""": """0"""}, """tokenizer_class""": """BertTokenizerFast""", """prefix""": """prefix""", """bos_token_id""": 6, """pad_token_id""": 7, """eos_token_id""": 8, """sep_token_id""": 9, """decoder_start_token_id""": 10, """exponential_decay_length_penalty""": (5, 1.01), """suppress_tokens""": [0, 1], """begin_suppress_tokens""": 2, """task_specific_params""": {"""translation""": """some_params"""}, """problem_type""": """regression""", } @is_staging_test class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def lowerCAmelCase_ ( cls ): """simple docstring""" A_ : Optional[int] = TOKEN HfFolder.save_token(lowercase ) @classmethod def lowerCAmelCase_ ( cls ): """simple docstring""" try: delete_repo(token=cls._token , repo_id='test-config' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='valid_org/test-config-org' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='test-dynamic-config' ) except HTTPError: pass def lowerCAmelCase_ ( self ): """simple docstring""" A_ : Union[str, Any] = BertConfig( vocab_size=9_9 , hidden_size=3_2 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=3_7 ) config.push_to_hub('test-config' , use_auth_token=self._token ) A_ : Dict = BertConfig.from_pretrained(F'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowercase , getattr(lowercase , lowercase ) ) # Reset repo delete_repo(token=self._token , repo_id='test-config' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(lowercase , repo_id='test-config' , push_to_hub=lowercase , use_auth_token=self._token ) A_ : Dict = BertConfig.from_pretrained(F'''{USER}/test-config''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowercase , getattr(lowercase , lowercase ) ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : List[str] = BertConfig( vocab_size=9_9 , hidden_size=3_2 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=3_7 ) config.push_to_hub('valid_org/test-config-org' , use_auth_token=self._token ) A_ : List[Any] = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowercase , getattr(lowercase , lowercase ) ) # Reset repo delete_repo(token=self._token , repo_id='valid_org/test-config-org' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( lowercase , repo_id='valid_org/test-config-org' , push_to_hub=lowercase , use_auth_token=self._token ) A_ : Optional[Any] = BertConfig.from_pretrained('valid_org/test-config-org' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowercase , getattr(lowercase , lowercase ) ) def lowerCAmelCase_ ( self ): """simple docstring""" CustomConfig.register_for_auto_class() A_ : Optional[int] = CustomConfig(attribute=4_2 ) config.push_to_hub('test-dynamic-config' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map , {'AutoConfig': 'custom_configuration.CustomConfig'} ) A_ : Optional[int] = AutoConfig.from_pretrained(F'''{USER}/test-dynamic-config''' , trust_remote_code=lowercase ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ , 'CustomConfig' ) self.assertEqual(new_config.attribute , 4_2 ) class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self ): """simple docstring""" A_ : int = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated A_ : Optional[int] = c.n_embd + 1 # int A_ : List[str] = c.resid_pdrop + 1.0 # float A_ : str = not c.scale_attn_weights # bool A_ : Optional[int] = c.summary_type + 'foo' # str c.update_from_string( F'''n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}''' ) self.assertEqual(lowercase , c.n_embd , 'mismatch for key: n_embd' ) self.assertEqual(lowercase , c.resid_pdrop , 'mismatch for key: resid_pdrop' ) self.assertEqual(lowercase , c.scale_attn_weights , 'mismatch for key: scale_attn_weights' ) self.assertEqual(lowercase , c.summary_type , 'mismatch for key: summary_type' ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : Optional[int] = PretrainedConfig() A_ : int = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( lowercase , ['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version'] ) A_ : List[str] = [key for key, value in config_common_kwargs.items() if value == getattr(lowercase , lowercase )] if len(lowercase ) > 0: raise ValueError( 'The following keys are set with the default values in' ' `test_configuration_common.config_common_kwargs` pick another value for them:' F''' {', '.join(lowercase )}.''' ) def lowerCAmelCase_ ( self ): """simple docstring""" with self.assertRaises(lowercase ): # config is in subfolder, the following should not work without specifying the subfolder A_ : Union[str, Any] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' ) A_ : Optional[int] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' , subfolder='bert' ) self.assertIsNotNone(lowercase ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : int = mock.Mock() A_ : int = 5_0_0 A_ : Union[str, Any] = {} A_ : List[str] = HTTPError A_ : List[Any] = {} # Download this model to make sure it's in the cache. A_ : Tuple = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('requests.Session.request' , return_value=lowercase ) as mock_head: A_ : Optional[int] = BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' ) # This check we did call the fake head request mock_head.assert_called() def lowerCAmelCase_ ( self ): """simple docstring""" A_ : Any = BertConfig.from_pretrained( 'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json' ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : int = AutoConfig.from_pretrained('bert-base-cased' ) A_ : Tuple = ['config.4.0.0.json'] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(lowercase ) A_ : Dict = 2 json.dump(configuration.to_dict() , open(os.path.join(lowercase , 'config.4.0.0.json' ) , 'w' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 A_ : Tuple = AutoConfig.from_pretrained(lowercase ) self.assertEqual(new_configuration.hidden_size , 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 A_ : int = ['config.42.0.0.json'] A_ : str = 7_6_8 configuration.save_pretrained(lowercase ) shutil.move(os.path.join(lowercase , 'config.4.0.0.json' ) , os.path.join(lowercase , 'config.42.0.0.json' ) ) A_ : str = AutoConfig.from_pretrained(lowercase ) self.assertEqual(new_configuration.hidden_size , 7_6_8 ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : List[Any] = 'hf-internal-testing/test-two-configs' import transformers as new_transformers A_ : List[Any] = 'v4.0.0' A_ , A_ : List[str] = new_transformers.models.auto.AutoConfig.from_pretrained( lowercase , return_unused_kwargs=lowercase ) self.assertEqual(new_configuration.hidden_size , 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(lowercase , {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers A_ : Optional[int] = 'v3.0.0' A_ : List[Any] = old_transformers.models.auto.AutoConfig.from_pretrained(lowercase ) self.assertEqual(old_configuration.hidden_size , 7_6_8 )
192
1
"""simple docstring""" import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def UpperCAmelCase__ (): """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(__SCREAMING_SNAKE_CASE ): requests.request("""GET""" , """https://huggingface.co""" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("""GET""" , """https://huggingface.co""" , timeout=1.0 ) @pytest.mark.integration def UpperCAmelCase__ (): """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("""GET""" , """https://huggingface.co""" ) def UpperCAmelCase__ (): """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(__SCREAMING_SNAKE_CASE ): http_head("""https://huggingface.co""" )
64
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class A_ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self ): lowercase = tempfile.mkdtemp() lowercase = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', '的', '价', '格', '是', '15', '便', 'alex', '##andra', ',', '。', '-', 't', 'shirt', ] lowercase = 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] ) ) lowercase = { 'do_resize': True, 'size': {'height': 224, 'width': 224}, 'do_center_crop': True, 'crop_size': {'height': 18, 'width': 18}, 'do_normalize': True, 'image_mean': [0.48_145_466, 0.4_578_275, 0.40_821_073], 'image_std': [0.26_862_954, 0.26_130_258, 0.27_577_711], 'do_convert_rgb': True, } lowercase = os.path.join(self.tmpdirname , snake_case ) with open(self.image_processor_file , 'w' , encoding='utf-8' ) as fp: json.dump(snake_case , snake_case ) def SCREAMING_SNAKE_CASE__ ( self , **snake_case ): return BertTokenizer.from_pretrained(self.tmpdirname , **snake_case ) def SCREAMING_SNAKE_CASE__ ( self , **snake_case ): return BertTokenizerFast.from_pretrained(self.tmpdirname , **snake_case ) def SCREAMING_SNAKE_CASE__ ( self , **snake_case ): return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **snake_case ) def SCREAMING_SNAKE_CASE__ ( self ): shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowercase = [Image.fromarray(np.moveaxis(snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_tokenizer() lowercase = self.get_rust_tokenizer() lowercase = self.get_image_processor() lowercase = ChineseCLIPProcessor(tokenizer=snake_case , image_processor=snake_case ) processor_slow.save_pretrained(self.tmpdirname ) lowercase = ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=snake_case ) lowercase = ChineseCLIPProcessor(tokenizer=snake_case , image_processor=snake_case ) processor_fast.save_pretrained(self.tmpdirname ) lowercase = ChineseCLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , snake_case ) self.assertIsInstance(processor_fast.tokenizer , snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , snake_case ) self.assertIsInstance(processor_fast.image_processor , snake_case ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowercase = self.get_tokenizer(cls_token='(CLS)' , sep_token='(SEP)' ) lowercase = self.get_image_processor(do_normalize=snake_case ) lowercase = ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token='(CLS)' , sep_token='(SEP)' , do_normalize=snake_case ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , snake_case ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , snake_case ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = ChineseCLIPProcessor(tokenizer=snake_case , image_processor=snake_case ) lowercase = self.prepare_image_inputs() lowercase = image_processor(snake_case , return_tensors='np' ) lowercase = processor(images=snake_case , return_tensors='np' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = ChineseCLIPProcessor(tokenizer=snake_case , image_processor=snake_case ) lowercase = 'Alexandra,T-shirt的价格是15便士。' lowercase = processor(text=snake_case ) lowercase = tokenizer(snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = ChineseCLIPProcessor(tokenizer=snake_case , image_processor=snake_case ) lowercase = 'Alexandra,T-shirt的价格是15便士。' lowercase = self.prepare_image_inputs() lowercase = processor(text=snake_case , images=snake_case ) self.assertListEqual(list(inputs.keys() ) , ['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values'] ) # test if it raises when no input is passed with pytest.raises(snake_case ): processor() def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = ChineseCLIPProcessor(tokenizer=snake_case , image_processor=snake_case ) lowercase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowercase = processor.batch_decode(snake_case ) lowercase = tokenizer.batch_decode(snake_case ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = ChineseCLIPProcessor(tokenizer=snake_case , image_processor=snake_case ) lowercase = 'Alexandra,T-shirt的价格是15便士。' lowercase = self.prepare_image_inputs() lowercase = processor(text=snake_case , images=snake_case ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
195
0
'''simple docstring''' import os snake_case__ = {"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 1_00, """D""": 5_00, """M""": 10_00} def snake_case__ ( lowerCamelCase__ : str ) -> int: A_ : Optional[Any] = 0 A_ : Any = 0 while index < len(lowerCamelCase__ ) - 1: A_ : List[str] = SYMBOLS[numerals[index]] A_ : Any = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def snake_case__ ( lowerCamelCase__ : int ) -> str: A_ : int = '''''' A_ : Optional[Any] = num // 1_0_0_0 numerals += m_count * "M" num %= 1_0_0_0 A_ : str = num // 1_0_0 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 1_0_0 A_ : Optional[Any] = num // 1_0 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 1_0 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def snake_case__ ( lowerCamelCase__ : str = "/p089_roman.txt" ) -> int: A_ : Optional[Any] = 0 with open(os.path.dirname(lowerCamelCase__ ) + roman_numerals_filename ) as filea: A_ : Any = filea.readlines() for line in lines: A_ : Any = line.strip() A_ : List[str] = parse_roman_numerals(lowerCamelCase__ ) A_ : List[str] = generate_roman_numerals(lowerCamelCase__ ) savings += len(lowerCamelCase__ ) - len(lowerCamelCase__ ) return savings if __name__ == "__main__": print(F'{solution() = }')
4
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import AlignProcessor, EfficientNetImageProcessor @require_vision class UpperCamelCase_ (unittest.TestCase ): """simple docstring""" def _a ( self : Union[str, Any] ): """simple docstring""" A_ : Any = tempfile.mkdtemp() A_ : List[Any] = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] A_ : 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] ) ) A_ : Tuple = { '''do_resize''': True, '''size''': 20, '''do_center_crop''': True, '''crop_size''': 18, '''do_normalize''': True, '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], } A_ : List[Any] = os.path.join(self.tmpdirname , _lowerCamelCase ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_lowerCamelCase , _lowerCamelCase ) def _a ( self : Dict , **_lowerCamelCase : Tuple ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_lowerCamelCase ) def _a ( self : Optional[int] , **_lowerCamelCase : Optional[int] ): """simple docstring""" return BertTokenizerFast.from_pretrained(self.tmpdirname , **_lowerCamelCase ) def _a ( self : Optional[Any] , **_lowerCamelCase : Tuple ): """simple docstring""" return EfficientNetImageProcessor.from_pretrained(self.tmpdirname , **_lowerCamelCase ) def _a ( self : Tuple ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _a ( self : int ): """simple docstring""" A_ : Union[str, Any] = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] A_ : Any = [Image.fromarray(np.moveaxis(_lowerCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def _a ( self : int ): """simple docstring""" A_ : Tuple = self.get_tokenizer() A_ : Tuple = self.get_rust_tokenizer() A_ : Dict = self.get_image_processor() A_ : List[Any] = AlignProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) processor_slow.save_pretrained(self.tmpdirname ) A_ : str = AlignProcessor.from_pretrained(self.tmpdirname , use_fast=_lowerCamelCase ) A_ : Any = AlignProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) processor_fast.save_pretrained(self.tmpdirname ) A_ : List[Any] = AlignProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _lowerCamelCase ) self.assertIsInstance(processor_fast.tokenizer , _lowerCamelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _lowerCamelCase ) self.assertIsInstance(processor_fast.image_processor , _lowerCamelCase ) def _a ( self : List[Any] ): """simple docstring""" A_ : List[str] = AlignProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A_ : Optional[int] = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) A_ : Tuple = self.get_image_processor(do_normalize=_lowerCamelCase , padding_value=1.0 ) A_ : List[str] = AlignProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=_lowerCamelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _lowerCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _lowerCamelCase ) def _a ( self : Union[str, Any] ): """simple docstring""" A_ : Dict = self.get_image_processor() A_ : Any = self.get_tokenizer() A_ : List[str] = AlignProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A_ : Any = self.prepare_image_inputs() A_ : List[Any] = image_processor(_lowerCamelCase , return_tensors='''np''' ) A_ : str = processor(images=_lowerCamelCase , return_tensors='''np''' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def _a ( self : Dict ): """simple docstring""" A_ : str = self.get_image_processor() A_ : List[str] = self.get_tokenizer() A_ : Optional[int] = AlignProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A_ : int = '''lower newer''' A_ : str = processor(text=_lowerCamelCase ) A_ : Dict = tokenizer(_lowerCamelCase , padding='''max_length''' , max_length=64 ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _a ( self : str ): """simple docstring""" A_ : Optional[int] = self.get_image_processor() A_ : Optional[Any] = self.get_tokenizer() A_ : List[str] = AlignProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A_ : List[Any] = '''lower newer''' A_ : Optional[int] = self.prepare_image_inputs() A_ : List[Any] = processor(text=_lowerCamelCase , images=_lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(_lowerCamelCase ): processor() def _a ( self : List[str] ): """simple docstring""" A_ : Optional[Any] = self.get_image_processor() A_ : Optional[int] = self.get_tokenizer() A_ : List[Any] = AlignProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A_ : str = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A_ : str = processor.batch_decode(_lowerCamelCase ) A_ : Union[str, Any] = tokenizer.batch_decode(_lowerCamelCase ) self.assertListEqual(_lowerCamelCase , _lowerCamelCase ) def _a ( self : Tuple ): """simple docstring""" A_ : str = self.get_image_processor() A_ : Tuple = self.get_tokenizer() A_ : Any = AlignProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A_ : str = '''lower newer''' A_ : List[str] = self.prepare_image_inputs() A_ : Tuple = processor(text=_lowerCamelCase , images=_lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
4
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast from ...utils import logging if TYPE_CHECKING: from ...feature_extraction_utils import FeatureExtractionMixin from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType snake_case_ = logging.get_logger(__name__) snake_case_ = { 'openai/whisper-base': 'https://huggingface.co/openai/whisper-base/resolve/main/config.json', } # fmt: off snake_case_ = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 357, 366, 438, 532, 685, 705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377, 1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211, 4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 10563, 10786, 11420, 11709, 11907, 13163, 13697, 13700, 14808, 15306, 16410, 16791, 17992, 19203, 19510, 20724, 22305, 22935, 27007, 30109, 30420, 33409, 34949, 40283, 40493, 40549, 47282, 49146, 50257, 50359, 50360, 50361 ] snake_case_ = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 359, 503, 522, 542, 873, 893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627, 3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647, 7273, 9061, 9383, 10428, 10929, 11938, 12033, 12331, 12562, 13793, 14157, 14635, 15265, 15618, 16553, 16604, 18362, 18956, 20075, 21675, 22520, 26130, 26161, 26435, 28279, 29464, 31650, 32302, 32470, 36865, 42863, 47425, 49870, 50254, 50258, 50360, 50361, 50362 ] class SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase ): A_ : int = 'whisper' A_ : int = ['past_key_values'] A_ : Union[str, Any] = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'} def __init__(self : int , a__ : List[str]=5_1865 , a__ : int=80 , a__ : Optional[int]=6 , a__ : List[Any]=4 , a__ : Optional[Any]=6 , a__ : List[str]=4 , a__ : Optional[int]=1536 , a__ : Tuple=1536 , a__ : Tuple=0.0 , a__ : List[Any]=0.0 , a__ : Optional[int]=5_0257 , a__ : Any=True , a__ : Union[str, Any]=True , a__ : Any="gelu" , a__ : Union[str, Any]=256 , a__ : Dict=0.0 , a__ : Dict=0.0 , a__ : Any=0.0 , a__ : Dict=0.0_2 , a__ : Optional[Any]=False , a__ : List[Any]=1500 , a__ : Dict=448 , a__ : Optional[int]=5_0256 , a__ : Any=5_0256 , a__ : Tuple=5_0256 , a__ : Union[str, Any]=None , a__ : Any=[220, 5_0256] , a__ : Tuple=False , a__ : Any=256 , a__ : int=False , a__ : Optional[int]=0.0_5 , a__ : Dict=10 , a__ : List[Any]=2 , a__ : List[str]=0.0 , a__ : Optional[int]=10 , a__ : Dict=0 , a__ : List[Any]=7 , **a__ : Union[str, Any] , ): """simple docstring""" __snake_case = vocab_size __snake_case = num_mel_bins __snake_case = d_model __snake_case = encoder_layers __snake_case = encoder_attention_heads __snake_case = decoder_layers __snake_case = decoder_attention_heads __snake_case = decoder_ffn_dim __snake_case = encoder_ffn_dim __snake_case = dropout __snake_case = attention_dropout __snake_case = activation_dropout __snake_case = activation_function __snake_case = init_std __snake_case = encoder_layerdrop __snake_case = decoder_layerdrop __snake_case = use_cache __snake_case = encoder_layers __snake_case = scale_embedding # scale factor will be sqrt(d_model) if True __snake_case = max_source_positions __snake_case = max_target_positions # Audio Classification-specific parameters. Feel free to ignore for other classes. __snake_case = classifier_proj_size __snake_case = use_weighted_layer_sum # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __snake_case = apply_spec_augment __snake_case = mask_time_prob __snake_case = mask_time_length __snake_case = mask_time_min_masks __snake_case = mask_feature_prob __snake_case = mask_feature_length __snake_case = mask_feature_min_masks __snake_case = median_filter_width super().__init__( pad_token_id=a__ , bos_token_id=a__ , eos_token_id=a__ , is_encoder_decoder=a__ , decoder_start_token_id=a__ , suppress_tokens=a__ , begin_suppress_tokens=a__ , **a__ , ) class SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase ): @property def a (self : List[str] ): """simple docstring""" __snake_case = OrderedDict( [ ('''input_features''', {0: '''batch''', 1: '''feature_size''', 2: '''encoder_sequence'''}), ] ) if self.use_past: __snake_case = {0: '''batch'''} else: __snake_case = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(a__ , direction='''inputs''' ) return common_inputs def a (self : Dict , a__ : Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"] , a__ : int = -1 , a__ : int = -1 , a__ : bool = False , a__ : Optional["TensorType"] = None , a__ : int = 2_2050 , a__ : float = 5.0 , a__ : int = 220 , ): """simple docstring""" __snake_case = OrderedDict() __snake_case = OnnxConfig.generate_dummy_inputs( self , preprocessor=preprocessor.feature_extractor , batch_size=a__ , framework=a__ , sampling_rate=a__ , time_duration=a__ , frequency=a__ , ) __snake_case = encoder_inputs['''input_features'''].shape[2] __snake_case = encoder_sequence_length // 2 if self.use_past else seq_length __snake_case = super().generate_dummy_inputs( preprocessor.tokenizer , a__ , a__ , a__ , a__ ) __snake_case = encoder_inputs.pop('''input_features''' ) __snake_case = decoder_inputs.pop('''decoder_input_ids''' ) if "past_key_values" in decoder_inputs: __snake_case = decoder_inputs.pop('''past_key_values''' ) return dummy_inputs @property def a (self : Union[str, Any] ): """simple docstring""" return 1E-3
24
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class SCREAMING_SNAKE_CASE__ : @staticmethod def a (*a__ : List[str] , **a__ : List[str] ): """simple docstring""" pass def lowerCamelCase__ ( snake_case_ : int ) -> Optional[int]: return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. snake_case_ = ( 'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png' ) @is_pipeline_test @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): A_ : Optional[Any] = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def a (self : List[Any] , a__ : Tuple , a__ : Union[str, Any] , a__ : Any ): """simple docstring""" __snake_case = pipeline( '''document-question-answering''' , model=a__ , tokenizer=a__ , image_processor=a__ ) __snake_case = INVOICE_URL __snake_case = list(zip(*apply_tesseract(load_image(a__ ) , a__ , '''''' ) ) ) __snake_case = '''What is the placebo?''' __snake_case = [ { '''image''': load_image(a__ ), '''question''': question, }, { '''image''': image, '''question''': question, }, { '''image''': image, '''question''': question, '''word_boxes''': word_boxes, }, ] return dqa_pipeline, examples def a (self : Union[str, Any] , a__ : Optional[int] , a__ : Dict ): """simple docstring""" __snake_case = dqa_pipeline(a__ , top_k=2 ) self.assertEqual( a__ , [ [ {'''score''': ANY(a__ ), '''answer''': ANY(a__ ), '''start''': ANY(a__ ), '''end''': ANY(a__ )}, {'''score''': ANY(a__ ), '''answer''': ANY(a__ ), '''start''': ANY(a__ ), '''end''': ANY(a__ )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def a (self : Dict ): """simple docstring""" __snake_case = pipeline('''document-question-answering''' , model='''hf-internal-testing/tiny-random-layoutlmv2''' ) __snake_case = INVOICE_URL __snake_case = '''How many cats are there?''' __snake_case = [ {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39}, {'''score''': 0.0_0_0_1, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40}, ] __snake_case = dqa_pipeline(image=a__ , question=a__ , top_k=2 ) self.assertEqual(nested_simplify(a__ , decimals=4 ) , a__ ) __snake_case = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual(nested_simplify(a__ , decimals=4 ) , a__ ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably __snake_case = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' __snake_case = dqa_pipeline(image=a__ , question=a__ , top_k=2 ) self.assertEqual(a__ , [] ) # We can optionnally pass directly the words and bounding boxes __snake_case = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' __snake_case = [] __snake_case = [] __snake_case = dqa_pipeline(image=a__ , question=a__ , words=a__ , boxes=a__ , top_k=2 ) self.assertEqual(a__ , [] ) @slow @require_torch @require_detectrona @require_pytesseract def a (self : str ): """simple docstring""" __snake_case = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , ) __snake_case = INVOICE_URL __snake_case = '''What is the invoice number?''' __snake_case = dqa_pipeline(image=a__ , question=a__ , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __snake_case = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __snake_case = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ [ {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def a (self : List[Any] ): """simple docstring""" __snake_case = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , max_seq_len=50 , ) __snake_case = INVOICE_URL __snake_case = '''What is the invoice number?''' __snake_case = dqa_pipeline(image=a__ , question=a__ , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __snake_case = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __snake_case = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ [ {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def a (self : Tuple ): """simple docstring""" __snake_case = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=a__ ) __snake_case = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=a__ , revision='''3dc6de3''' , ) __snake_case = INVOICE_URL __snake_case = '''What is the invoice number?''' __snake_case = dqa_pipeline(image=a__ , question=a__ , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) __snake_case = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) __snake_case = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] ] * 2 , ) __snake_case = list(zip(*apply_tesseract(load_image(a__ ) , a__ , '''''' ) ) ) # This model should also work if `image` is set to None __snake_case = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def a (self : Dict ): """simple docstring""" __snake_case = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=a__ ) __snake_case = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=a__ , revision='''3dc6de3''' , max_seq_len=50 , ) __snake_case = INVOICE_URL __snake_case = '''What is the invoice number?''' __snake_case = dqa_pipeline(image=a__ , question=a__ , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) __snake_case = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) __snake_case = list(zip(*apply_tesseract(load_image(a__ ) , a__ , '''''' ) ) ) # This model should also work if `image` is set to None __snake_case = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(a__ , decimals=4 ) , [ {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) @slow @require_torch def a (self : Tuple ): """simple docstring""" __snake_case = pipeline( '''document-question-answering''' , model='''naver-clova-ix/donut-base-finetuned-docvqa''' , tokenizer=AutoTokenizer.from_pretrained('''naver-clova-ix/donut-base-finetuned-docvqa''' ) , feature_extractor='''naver-clova-ix/donut-base-finetuned-docvqa''' , ) __snake_case = INVOICE_URL __snake_case = '''What is the invoice number?''' __snake_case = dqa_pipeline(image=a__ , question=a__ , top_k=2 ) self.assertEqual(nested_simplify(a__ , decimals=4 ) , [{'''answer''': '''us-001'''}] ) @require_tf @unittest.skip('''Document question answering not implemented in TF''' ) def a (self : List[str] ): """simple docstring""" pass
24
1
"""simple docstring""" import argparse import os import shutil from pathlib import Path import onnx import torch from packaging import version from torch.onnx import export from diffusers import OnnxRuntimeModel, OnnxStableDiffusionPipeline, StableDiffusionPipeline lowerCAmelCase_ : Tuple = version.parse(version.parse(torch.__version__).base_version) < version.parse('''1.11''') def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=False , ): '''simple docstring''' output_path.parent.mkdir(parents=lowerCAmelCase , exist_ok=lowerCAmelCase ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( lowerCAmelCase , lowerCAmelCase , f=output_path.as_posix() , input_names=lowerCAmelCase , output_names=lowerCAmelCase , dynamic_axes=lowerCAmelCase , do_constant_folding=lowerCAmelCase , use_external_data_format=lowerCAmelCase , enable_onnx_checker=lowerCAmelCase , opset_version=lowerCAmelCase , ) else: export( lowerCAmelCase , lowerCAmelCase , f=output_path.as_posix() , input_names=lowerCAmelCase , output_names=lowerCAmelCase , dynamic_axes=lowerCAmelCase , do_constant_folding=lowerCAmelCase , opset_version=lowerCAmelCase , ) @torch.no_grad() def _lowerCAmelCase ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = False ): '''simple docstring''' UpperCAmelCase = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): UpperCAmelCase = """cuda""" elif fpaa and not torch.cuda.is_available(): raise ValueError("""`float16` model export is only supported on GPUs with CUDA""" ) else: UpperCAmelCase = """cpu""" UpperCAmelCase = StableDiffusionPipeline.from_pretrained(lowerCAmelCase , torch_dtype=lowerCAmelCase ).to(lowerCAmelCase ) UpperCAmelCase = Path(lowerCAmelCase ) # TEXT ENCODER UpperCAmelCase = pipeline.text_encoder.config.max_position_embeddings UpperCAmelCase = pipeline.text_encoder.config.hidden_size UpperCAmelCase = pipeline.tokenizer( """A sample prompt""" , padding="""max_length""" , max_length=pipeline.tokenizer.model_max_length , truncation=lowerCAmelCase , return_tensors="""pt""" , ) onnx_export( pipeline.text_encoder , model_args=(text_input.input_ids.to(device=lowerCAmelCase , dtype=torch.intaa )) , output_path=output_path / """text_encoder""" / """model.onnx""" , ordered_input_names=["""input_ids"""] , output_names=["""last_hidden_state""", """pooler_output"""] , dynamic_axes={ """input_ids""": {0: """batch""", 1: """sequence"""}, } , opset=lowerCAmelCase , ) del pipeline.text_encoder # UNET UpperCAmelCase = pipeline.unet.config.in_channels UpperCAmelCase = pipeline.unet.config.sample_size UpperCAmelCase = output_path / """unet""" / """model.onnx""" onnx_export( pipeline.unet , model_args=( torch.randn(2 , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ).to(device=lowerCAmelCase , dtype=lowerCAmelCase ), torch.randn(2 ).to(device=lowerCAmelCase , dtype=lowerCAmelCase ), torch.randn(2 , lowerCAmelCase , lowerCAmelCase ).to(device=lowerCAmelCase , dtype=lowerCAmelCase ), False, ) , output_path=lowerCAmelCase , ordered_input_names=["""sample""", """timestep""", """encoder_hidden_states""", """return_dict"""] , output_names=["""out_sample"""] , dynamic_axes={ """sample""": {0: """batch""", 1: """channels""", 2: """height""", 3: """width"""}, """timestep""": {0: """batch"""}, """encoder_hidden_states""": {0: """batch""", 1: """sequence"""}, } , opset=lowerCAmelCase , use_external_data_format=lowerCAmelCase , ) UpperCAmelCase = str(unet_path.absolute().as_posix() ) UpperCAmelCase = os.path.dirname(lowerCAmelCase ) UpperCAmelCase = onnx.load(lowerCAmelCase ) # clean up existing tensor files shutil.rmtree(lowerCAmelCase ) os.mkdir(lowerCAmelCase ) # collate external tensor files into one onnx.save_model( lowerCAmelCase , lowerCAmelCase , save_as_external_data=lowerCAmelCase , all_tensors_to_one_file=lowerCAmelCase , location="""weights.pb""" , convert_attribute=lowerCAmelCase , ) del pipeline.unet # VAE ENCODER UpperCAmelCase = pipeline.vae UpperCAmelCase = vae_encoder.config.in_channels UpperCAmelCase = vae_encoder.config.sample_size # need to get the raw tensor output (sample) from the encoder UpperCAmelCase = lambda lowerCAmelCase , lowerCAmelCase : vae_encoder.encode(lowerCAmelCase , lowerCAmelCase )[0].sample() onnx_export( lowerCAmelCase , model_args=( torch.randn(1 , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ).to(device=lowerCAmelCase , dtype=lowerCAmelCase ), False, ) , output_path=output_path / """vae_encoder""" / """model.onnx""" , ordered_input_names=["""sample""", """return_dict"""] , output_names=["""latent_sample"""] , dynamic_axes={ """sample""": {0: """batch""", 1: """channels""", 2: """height""", 3: """width"""}, } , opset=lowerCAmelCase , ) # VAE DECODER UpperCAmelCase = pipeline.vae UpperCAmelCase = vae_decoder.config.latent_channels UpperCAmelCase = vae_decoder.config.out_channels # forward only through the decoder part UpperCAmelCase = vae_encoder.decode onnx_export( lowerCAmelCase , model_args=( torch.randn(1 , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ).to(device=lowerCAmelCase , dtype=lowerCAmelCase ), False, ) , output_path=output_path / """vae_decoder""" / """model.onnx""" , ordered_input_names=["""latent_sample""", """return_dict"""] , output_names=["""sample"""] , dynamic_axes={ """latent_sample""": {0: """batch""", 1: """channels""", 2: """height""", 3: """width"""}, } , opset=lowerCAmelCase , ) del pipeline.vae # SAFETY CHECKER if pipeline.safety_checker is not None: UpperCAmelCase = pipeline.safety_checker UpperCAmelCase = safety_checker.config.vision_config.num_channels UpperCAmelCase = safety_checker.config.vision_config.image_size UpperCAmelCase = safety_checker.forward_onnx onnx_export( pipeline.safety_checker , model_args=( torch.randn( 1 , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , ).to(device=lowerCAmelCase , dtype=lowerCAmelCase ), torch.randn(1 , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ).to(device=lowerCAmelCase , dtype=lowerCAmelCase ), ) , output_path=output_path / """safety_checker""" / """model.onnx""" , ordered_input_names=["""clip_input""", """images"""] , output_names=["""out_images""", """has_nsfw_concepts"""] , dynamic_axes={ """clip_input""": {0: """batch""", 1: """channels""", 2: """height""", 3: """width"""}, """images""": {0: """batch""", 1: """height""", 2: """width""", 3: """channels"""}, } , opset=lowerCAmelCase , ) del pipeline.safety_checker UpperCAmelCase = OnnxRuntimeModel.from_pretrained(output_path / """safety_checker""" ) UpperCAmelCase = pipeline.feature_extractor else: UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = OnnxStableDiffusionPipeline( vae_encoder=OnnxRuntimeModel.from_pretrained(output_path / """vae_encoder""" ) , vae_decoder=OnnxRuntimeModel.from_pretrained(output_path / """vae_decoder""" ) , text_encoder=OnnxRuntimeModel.from_pretrained(output_path / """text_encoder""" ) , tokenizer=pipeline.tokenizer , unet=OnnxRuntimeModel.from_pretrained(output_path / """unet""" ) , scheduler=pipeline.scheduler , safety_checker=lowerCAmelCase , feature_extractor=lowerCAmelCase , requires_safety_checker=safety_checker is not None , ) onnx_pipeline.save_pretrained(lowerCAmelCase ) print("""ONNX pipeline saved to""" , lowerCAmelCase ) del pipeline del onnx_pipeline UpperCAmelCase = OnnxStableDiffusionPipeline.from_pretrained(lowerCAmelCase , provider="""CPUExecutionProvider""" ) print("""ONNX pipeline is loadable""" ) if __name__ == "__main__": lowerCAmelCase_ : Tuple = argparse.ArgumentParser() parser.add_argument( '''--model_path''', type=str, required=True, help='''Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).''', ) parser.add_argument('''--output_path''', type=str, required=True, help='''Path to the output model.''') parser.add_argument( '''--opset''', default=1_4, type=int, help='''The version of the ONNX operator set to use.''', ) parser.add_argument('''--fp16''', action='''store_true''', default=False, help='''Export the models in `float16` mode''') lowerCAmelCase_ : Union[str, Any] = parser.parse_args() convert_models(args.model_path, args.output_path, args.opset, args.fpaa)
248
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ : Dict = logging.get_logger(__name__) lowerCAmelCase_ : List[str] = { '''microsoft/unispeech-large-1500h-cv''': ( '''https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json''' ), # See all UniSpeech models at https://huggingface.co/models?filter=unispeech } class UpperCamelCase_ ( a_ ): _A : Dict = 'unispeech' def __init__( self , snake_case__=32 , snake_case__=7_68 , snake_case__=12 , snake_case__=12 , snake_case__=30_72 , snake_case__="gelu" , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=0.02 , snake_case__=1e-5 , snake_case__="group" , snake_case__="gelu" , snake_case__=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12) , snake_case__=(5, 2, 2, 2, 2, 2, 2) , snake_case__=(10, 3, 3, 3, 3, 2, 2) , snake_case__=False , snake_case__=1_28 , snake_case__=16 , snake_case__=False , snake_case__=True , snake_case__=0.05 , snake_case__=10 , snake_case__=2 , snake_case__=0.0 , snake_case__=10 , snake_case__=0 , snake_case__=3_20 , snake_case__=2 , snake_case__=0.1 , snake_case__=1_00 , snake_case__=2_56 , snake_case__=2_56 , snake_case__=0.1 , snake_case__="mean" , snake_case__=False , snake_case__=False , snake_case__=2_56 , snake_case__=80 , snake_case__=0 , snake_case__=1 , snake_case__=2 , snake_case__=0.5 , **snake_case__ , ) -> Dict: """simple docstring""" super().__init__(**snake_case__ , pad_token_id=snake_case__ , bos_token_id=snake_case__ , eos_token_id=snake_case__ ) UpperCAmelCase = hidden_size UpperCAmelCase = feat_extract_norm UpperCAmelCase = feat_extract_activation UpperCAmelCase = list(snake_case__ ) UpperCAmelCase = list(snake_case__ ) UpperCAmelCase = list(snake_case__ ) UpperCAmelCase = conv_bias UpperCAmelCase = num_conv_pos_embeddings UpperCAmelCase = num_conv_pos_embedding_groups UpperCAmelCase = len(self.conv_dim ) UpperCAmelCase = num_hidden_layers UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_dropout UpperCAmelCase = attention_dropout UpperCAmelCase = activation_dropout UpperCAmelCase = feat_proj_dropout UpperCAmelCase = final_dropout UpperCAmelCase = layerdrop UpperCAmelCase = layer_norm_eps UpperCAmelCase = initializer_range UpperCAmelCase = num_ctc_classes UpperCAmelCase = vocab_size UpperCAmelCase = do_stable_layer_norm UpperCAmelCase = use_weighted_layer_sum UpperCAmelCase = classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==""" """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =""" f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase = apply_spec_augment UpperCAmelCase = mask_time_prob UpperCAmelCase = mask_time_length UpperCAmelCase = mask_time_min_masks UpperCAmelCase = mask_feature_prob UpperCAmelCase = mask_feature_length UpperCAmelCase = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCAmelCase = num_codevectors_per_group UpperCAmelCase = num_codevector_groups UpperCAmelCase = contrastive_logits_temperature UpperCAmelCase = feat_quantizer_dropout UpperCAmelCase = num_negatives UpperCAmelCase = codevector_dim UpperCAmelCase = proj_codevector_dim UpperCAmelCase = diversity_loss_weight # ctc loss UpperCAmelCase = ctc_loss_reduction UpperCAmelCase = ctc_zero_infinity # pretraining loss UpperCAmelCase = replace_prob @property def UpperCamelCase_ ( self ) -> Tuple: """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
248
1
"""simple docstring""" def _snake_case ( _snake_case : str ): return " ".join(input_str.split()[::-1] ) if __name__ == "__main__": import doctest doctest.testmod()
60
"""simple docstring""" def __A ( a_ :int = 60_08_51_47_51_43) -> int: try: __a : List[Any] = int(a_) except (TypeError, ValueError): raise TypeError('''Parameter n must be int or castable to int.''') if n <= 0: raise ValueError('''Parameter n must be greater than or equal to one.''') __a : int = 1 __a : List[Any] = 2 while i * i <= n: while n % i == 0: __a : List[str] = i n //= i i += 1 if n > 1: __a : Optional[int] = n return int(a_) if __name__ == "__main__": print(F'{solution() = }')
160
0
"""simple docstring""" from manim import * class _UpperCAmelCase ( __snake_case ): def a ( self : List[str] ): __UpperCAmelCase = Rectangle(height=0.5 , width=0.5 ) __UpperCAmelCase = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) __UpperCAmelCase = [mem.copy() for i in range(6 )] __UpperCAmelCase = [mem.copy() for i in range(6 )] __UpperCAmelCase = VGroup(*lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0 ) __UpperCAmelCase = VGroup(*lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0 ) __UpperCAmelCase = VGroup(lowerCamelCase_ , lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0 ) __UpperCAmelCase = Text('''CPU''' , font_size=24 ) __UpperCAmelCase = Group(lowerCamelCase_ , lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0.5 , aligned_edge=lowerCamelCase_ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(lowerCamelCase_ ) __UpperCAmelCase = [mem.copy() for i in range(1 )] __UpperCAmelCase = VGroup(*lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0 ) __UpperCAmelCase = Text('''GPU''' , font_size=24 ) __UpperCAmelCase = Group(lowerCamelCase_ , lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0.5 , aligned_edge=lowerCamelCase_ ) gpu.align_to(lowerCamelCase_ , lowerCamelCase_ ) gpu.set_x(gpu.get_x() - 1 ) self.add(lowerCamelCase_ ) __UpperCAmelCase = [mem.copy() for i in range(6 )] __UpperCAmelCase = VGroup(*lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0 ) __UpperCAmelCase = Text('''Model''' , font_size=24 ) __UpperCAmelCase = Group(lowerCamelCase_ , lowerCamelCase_ ).arrange(lowerCamelCase_ , buff=0.5 , aligned_edge=lowerCamelCase_ ) model.move_to([3, -1.0, 0] ) self.play( Create(lowerCamelCase_ , run_time=1 ) , Create(lowerCamelCase_ , run_time=1 ) , Create(lowerCamelCase_ , run_time=1 ) , ) __UpperCAmelCase = MarkupText( F'''First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM.''' , font_size=24 , ) __UpperCAmelCase = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) __UpperCAmelCase = 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(lowerCamelCase_ , run_time=2.5 ) , Write(lowerCamelCase_ ) , Write(lowerCamelCase_ ) ) self.add(lowerCamelCase_ ) __UpperCAmelCase = [] __UpperCAmelCase = [] __UpperCAmelCase = [] for i, rect in enumerate(lowerCamelCase_ ): __UpperCAmelCase = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0.0 ).set_fill(lowerCamelCase_ , opacity=0.7 ) cpu_target.move_to(lowerCamelCase_ ) cpu_target.generate_target() __UpperCAmelCase = 0.46 / 4 __UpperCAmelCase = 0.46 / 3 if i == 0: cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=lowerCamelCase_ ) 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=lowerCamelCase_ , buff=0.0 ) else: cpu_target.target.next_to(cpu_targs[i - 1].target , direction=lowerCamelCase_ , buff=0.0 ) cpu_targs.append(lowerCamelCase_ ) first_animations.append(rect.animate(run_time=0.5 ).set_stroke(lowerCamelCase_ ) ) second_animations.append(MoveToTarget(lowerCamelCase_ , run_time=1.5 ) ) self.play(*lowerCamelCase_ ) self.play(*lowerCamelCase_ ) self.wait()
365
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging _lowercase : List[Any] = logging.get_logger(__name__) _lowercase : int = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class _UpperCAmelCase ( _lowerCAmelCase ): a__ : Optional[Any] = "bloom" a__ : List[Any] = ["past_key_values"] a__ : Optional[Any] = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , _lowercase : Dict=25_08_80 , _lowercase : str=64 , _lowercase : int=2 , _lowercase : Union[str, Any]=8 , _lowercase : Optional[Any]=1E-5 , _lowercase : Dict=0.02 , _lowercase : Optional[int]=True , _lowercase : Any=1 , _lowercase : Dict=2 , _lowercase : Optional[Any]=False , _lowercase : Union[str, Any]=0.0 , _lowercase : str=0.0 , _lowercase : str=1 , _lowercase : int=False , **_lowercase : List[str] , ): __UpperCAmelCase = vocab_size # Backward compatibility with n_embed kwarg __UpperCAmelCase = kwargs.pop('''n_embed''' , _lowercase ) __UpperCAmelCase = hidden_size if n_embed is None else n_embed __UpperCAmelCase = n_layer __UpperCAmelCase = n_head __UpperCAmelCase = layer_norm_epsilon __UpperCAmelCase = initializer_range __UpperCAmelCase = use_cache __UpperCAmelCase = pretraining_tp __UpperCAmelCase = apply_residual_connection_post_layernorm __UpperCAmelCase = hidden_dropout __UpperCAmelCase = attention_dropout __UpperCAmelCase = bos_token_id __UpperCAmelCase = eos_token_id __UpperCAmelCase = slow_but_exact super().__init__(bos_token_id=_lowercase , eos_token_id=_lowercase , **_lowercase ) class _UpperCAmelCase ( _lowerCAmelCase ): a__ : List[str] = version.parse("1.12" ) def __init__( self : Optional[int] , _lowercase : PretrainedConfig , _lowercase : str = "default" , _lowercase : List[PatchingSpec] = None , _lowercase : bool = False , ): super().__init__(_lowercase , task=_lowercase , patching_specs=_lowercase , use_past=_lowercase ) if not getattr(self._config , '''pad_token_id''' , _lowercase ): # TODO: how to do that better? __UpperCAmelCase = 0 @property def a ( self : Optional[int] ): __UpperCAmelCase = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(_lowercase , direction='''inputs''' , inverted_values_shape=_lowercase ) __UpperCAmelCase = {0: '''batch''', 1: '''past_sequence + sequence'''} else: __UpperCAmelCase = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def a ( self : Any ): return self._config.n_layer @property def a ( self : Tuple ): return self._config.n_head @property def a ( self : Dict ): return 1E-3 def a ( self : List[str] , _lowercase : "PreTrainedTokenizer" , _lowercase : int = -1 , _lowercase : int = -1 , _lowercase : bool = False , _lowercase : Optional["TensorType"] = None , ): __UpperCAmelCase = super(_lowercase , self ).generate_dummy_inputs( _lowercase , batch_size=_lowercase , seq_length=_lowercase , is_pair=_lowercase , framework=_lowercase ) # We need to order the input in the way they appears in the forward() __UpperCAmelCase = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch __UpperCAmelCase , __UpperCAmelCase = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values __UpperCAmelCase = seqlen + 2 __UpperCAmelCase = self._config.hidden_size // self.num_attention_heads __UpperCAmelCase = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) __UpperCAmelCase = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) __UpperCAmelCase = [ (torch.zeros(_lowercase ), torch.zeros(_lowercase )) for _ in range(self.num_layers ) ] __UpperCAmelCase = common_inputs['''attention_mask'''] if self.use_past: __UpperCAmelCase = ordered_inputs['''attention_mask'''].dtype __UpperCAmelCase = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(_lowercase , _lowercase , dtype=_lowercase )] , dim=1 ) return ordered_inputs @property def a ( self : Any ): return 13
86
0
import argparse import fairseq import torch from torch import nn from transformers import ( MBartaaTokenizer, MBartConfig, MBartForCausalLM, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() A_ : List[str] = logging.get_logger(__name__) A_ : Optional[Any] = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } A_ : int = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def UpperCamelCase (lowercase_: List[str] , lowercase_: int , lowercase_: int , lowercase_: Dict , lowercase_: Dict ) -> int: for attribute in key.split(""".""" ): A__ : Tuple = getattr(lowercase_ , lowercase_ ) if weight_type is not None: A__ : Optional[int] = getattr(lowercase_ , lowercase_ ).shape else: A__ : List[str] = hf_pointer.shape assert hf_shape == value.shape, ( f"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" f""" {value.shape} for {full_name}""" ) if weight_type == "weight": A__ : Optional[int] = value elif weight_type == "weight_g": A__ : List[Any] = value elif weight_type == "weight_v": A__ : Optional[int] = value elif weight_type == "bias": A__ : Optional[Any] = value else: A__ : str = value logger.info(f"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.""" ) def UpperCamelCase (lowercase_: Dict , lowercase_: Tuple ) -> List[Any]: A__ : List[str] = [] A__ : Optional[Any] = fairseq_model.state_dict() A__ : Any = hf_model.feature_extractor A__ : Optional[int] = hf_model.adapter for name, value in fairseq_dict.items(): A__ : List[str] = False if "conv_layers" in name: load_conv_layer( lowercase_ , lowercase_ , lowercase_ , lowercase_ , hf_model.config.feat_extract_norm == """group""" , ) A__ : Optional[int] = True elif any(x in name for x in ["""adaptor""", """w2v_encoder.proj.""", """w2v_proj_ln."""] ): load_adapter(lowercase_ , lowercase_ , lowercase_ , lowercase_ ) A__ : Any = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: A__ : int = True if "*" in mapped_key: A__ : Dict = name.split(lowercase_ )[0].split(""".""" )[-2] A__ : Optional[Any] = mapped_key.replace("""*""" , lowercase_ ) if "weight_g" in name: A__ : List[Any] = """weight_g""" elif "weight_v" in name: A__ : List[str] = """weight_v""" elif "bias" in name: A__ : Optional[Any] = """bias""" elif "weight" in name: A__ : Dict = """weight""" else: A__ : str = None set_recursively(lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ) continue if not is_used: unused_weights.append(lowercase_ ) logger.warning(f"""Unused weights: {unused_weights}""" ) def UpperCamelCase (lowercase_: List[str] , lowercase_: Optional[Any] , lowercase_: List[str] , lowercase_: Dict , lowercase_: List[str] ) -> int: A__ : List[str] = full_name.split("""conv_layers.""" )[-1] A__ : Tuple = name.split(""".""" ) A__ : Dict = int(items[0] ) A__ : Optional[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) A__ : Union[str, Any] = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) A__ : Dict = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) A__ : str = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) A__ : Dict = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(lowercase_ ) def UpperCamelCase (lowercase_: Dict , lowercase_: Optional[Any] , lowercase_: Optional[int] , lowercase_: str ) -> List[str]: A__ : List[Any] = full_name.split("""adaptor.""" )[-1] A__ : Dict = name.split(""".""" ) if items[1].isdigit(): A__ : str = int(items[1] ) else: A__ : Dict = None if "adaptor" not in full_name: if "proj_ln" in full_name: # has to be layer norm if "bias" in name: assert ( value.shape == adapter.proj_layer_norm.bias.data.shape ), f"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found.""" A__ : Tuple = value logger.info(f"""Adapter proj layer norm bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj_layer_norm.weight.data.shape ), f"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found.""" A__ : str = value else: # has to be projection layer if "bias" in name: assert ( value.shape == adapter.proj.bias.data.shape ), f"""{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found.""" A__ : Dict = value logger.info(f"""Adapter proj layer bias was initialized from {full_name}.""" ) if "weight" in name: assert ( value.shape == adapter.proj.weight.data.shape ), f"""{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found.""" A__ : Union[str, Any] = value logger.info(f"""Adapter proj layer weight was initialized from {full_name}.""" ) elif isinstance(lowercase_ , lowercase_ ): if "bias" in name: assert ( value.shape == adapter.layers[layer_id].conv.bias.data.shape ), f"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found.""" A__ : str = value logger.info(f"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) elif "weight" in name: assert ( value.shape == adapter.layers[layer_id].conv.weight.data.shape ), f"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found.""" A__ : List[Any] = value logger.info(f"""Adapter layer {layer_id} bias was initialized from {full_name}.""" ) else: unused_weights.append(lowercase_ ) def UpperCamelCase (lowercase_: str ) -> Union[str, Any]: A__ , A__ : List[str] = emb.weight.shape A__ : Union[str, Any] = nn.Linear(lowercase_ , lowercase_ , bias=lowercase_ ) A__ : Optional[Any] = emb.weight.data return lin_layer @torch.no_grad() def UpperCamelCase (lowercase_: Union[str, Any] , lowercase_: Tuple , lowercase_: Union[str, Any] , lowercase_: Optional[int] , lowercase_: Tuple , lowercase_: Optional[Any] , lowercase_: str , lowercase_: str , lowercase_: Dict , lowercase_: List[str] , lowercase_: Tuple , ) -> Union[str, Any]: A__ : Optional[Any] = WavaVecaConfig.from_pretrained( lowercase_ , add_adapter=lowercase_ , adapter_stride=lowercase_ , adapter_kernel_size=lowercase_ , use_auth_token=lowercase_ , output_hidden_size=lowercase_ , ) A__ : List[str] = MBartConfig.from_pretrained(lowercase_ ) # load model A__ , A__ , A__ : Any = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={ """config_yaml""": config_yaml_path, """data""": """/""".join(dict_path.split("""/""" )[:-1] ), """w2v_path""": checkpoint_path, """load_pretrained_decoder_from""": None, } , ) A__ : Tuple = model[0].eval() # load feature extractor A__ : str = WavaVecaFeatureExtractor.from_pretrained(lowercase_ , use_auth_token=lowercase_ ) # set weights for wav2vec2 encoder A__ : Optional[Any] = WavaVecaModel(lowercase_ ) recursively_load_weights_wavaveca(model.encoder , lowercase_ ) # load decoder weights A__ : Optional[int] = MBartForCausalLM(lowercase_ ) A__ , A__ : List[Any] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=lowercase_ ) logger.warning(f"""The following keys are missing when loading the decoder weights: {missing_keys}""" ) logger.warning(f"""The following keys are unexpected when loading the decoder weights: {unexpected_keys}""" ) A__ : Union[str, Any] = SpeechEncoderDecoderModel(encoder=lowercase_ , decoder=lowercase_ ) A__ : Optional[int] = False A__ : Dict = MBartaaTokenizer(lowercase_ ) tokenizer.save_pretrained(lowercase_ ) A__ : str = hf_wavavec.config.to_dict() A__ : Union[str, Any] = tokenizer.pad_token_id A__ : List[str] = tokenizer.bos_token_id A__ : Tuple = tokenizer.eos_token_id A__ : List[Any] = """mbart50""" A__ : Tuple = """wav2vec2""" A__ : Union[str, Any] = tokenizer.eos_token_id A__ : Tuple = 250004 A__ : int = tokenizer.eos_token_id A__ : Any = SpeechEncoderDecoderConfig.from_dict(lowercase_ ) hf_wavavec.save_pretrained(lowercase_ ) feature_extractor.save_pretrained(lowercase_ ) if __name__ == "__main__": A_ : int = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_yaml_path', default=None, type=str, help='Path to yaml file of fine-tuned model') parser.add_argument( '--encoder_config_path', default='facebook/wav2vec2-xls-r-1b', type=str, help='Path to hf encoder wav2vec2 checkpoint config', ) parser.add_argument( '--decoder_config_path', default='facebook/mbart-large-50-one-to-many-mmt', type=str, help='Path to hf decoder checkpoint config', ) parser.add_argument('--add_adapter', default=True, type=bool, help='whethere to add model adapter layers') parser.add_argument('--adapter_stride', default=2, type=int, help='stride of adapter layers') parser.add_argument('--adapter_kernel_size', default=3, type=int, help='kernel size of adapter layers') parser.add_argument('--encoder_output_dim', default=1024, type=int, help='encoder output dim') parser.add_argument('--start_token_id', default=25_0004, type=int, help='`decoder_start_token_id` of model config') A_ : List[str] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, args.config_yaml_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, add_adapter=args.add_adapter, adapter_kernel_size=args.adapter_kernel_size, adapter_stride=args.adapter_stride, decoder_start_token_id=args.start_token_id, encoder_output_dim=args.encoder_output_dim, )
192
import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class _a (unittest.TestCase ): '''simple docstring''' @property def __A ( self ): torch.manual_seed(0 ) A__ : int = 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 @property def __A ( self ): torch.manual_seed(0 ) A__ : Dict = VQModel( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=3 , ) return model @property def __A ( self ): torch.manual_seed(0 ) A__ : int = 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(A__ ) def __A ( self ): A__ : int = self.dummy_uncond_unet A__ : Optional[int] = DDIMScheduler() A__ : List[Any] = self.dummy_vq_model A__ : Optional[int] = LDMPipeline(unet=A__ , vqvae=A__ , scheduler=A__ ) ldm.to(A__ ) ldm.set_progress_bar_config(disable=A__ ) A__ : Tuple = torch.manual_seed(0 ) A__ : List[Any] = ldm(generator=A__ , num_inference_steps=2 , output_type="""numpy""" ).images A__ : Optional[int] = torch.manual_seed(0 ) A__ : List[Any] = ldm(generator=A__ , num_inference_steps=2 , output_type="""numpy""" , return_dict=A__ )[0] A__ : str = image[0, -3:, -3:, -1] A__ : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) A__ : Dict = np.array([0.8_5_1_2, 0.8_1_8, 0.6_4_1_1, 0.6_8_0_8, 0.4_4_6_5, 0.5_6_1_8, 0.4_6, 0.6_2_3_1, 0.5_1_7_2] ) A__ : List[Any] = 1e-2 if torch_device != """mps""" else 3e-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < tolerance @slow @require_torch class _a (unittest.TestCase ): '''simple docstring''' def __A ( self ): A__ : Optional[Any] = LDMPipeline.from_pretrained("""CompVis/ldm-celebahq-256""" ) ldm.to(A__ ) ldm.set_progress_bar_config(disable=A__ ) A__ : Tuple = torch.manual_seed(0 ) A__ : List[Any] = ldm(generator=A__ , num_inference_steps=5 , output_type="""numpy""" ).images A__ : int = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) A__ : Any = np.array([0.4_3_9_9, 0.4_4_9_7_5, 0.4_6_8_2_5, 0.4_7_4, 0.4_3_5_9, 0.4_5_8_1, 0.4_5_0_9_5, 0.4_3_4_1, 0.4_4_4_7] ) A__ : int = 1e-2 if torch_device != """mps""" else 3e-2 assert np.abs(image_slice.flatten() - expected_slice ).max() < tolerance
192
1
"""simple docstring""" import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() __SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Tuple = [ ['attention', 'attn'], ['encoder_attention', 'encoder_attn'], ['q_lin', 'q_proj'], ['k_lin', 'k_proj'], ['v_lin', 'v_proj'], ['out_lin', 'out_proj'], ['norm_embeddings', 'layernorm_embedding'], ['position_embeddings', 'embed_positions'], ['embeddings', 'embed_tokens'], ['ffn.lin', 'fc'], ] def _a ( _SCREAMING_SNAKE_CASE ) -> Any: if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: snake_case_ = k.replace(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if k.startswith("""encoder""" ): snake_case_ = k.replace(""".attn""" , """.self_attn""" ) snake_case_ = k.replace("""norm1""" , """self_attn_layer_norm""" ) snake_case_ = k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): snake_case_ = k.replace("""norm1""" , """self_attn_layer_norm""" ) snake_case_ = k.replace("""norm2""" , """encoder_attn_layer_norm""" ) snake_case_ = k.replace("""norm3""" , """final_layer_norm""" ) return k def _a ( _SCREAMING_SNAKE_CASE ) -> Tuple: snake_case_ = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: snake_case_ = sd.pop(_SCREAMING_SNAKE_CASE ) snake_case_ = k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd snake_case_ = v __SCREAMING_SNAKE_CASE : List[Any] = ['START'] @torch.no_grad() def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: snake_case_ = torch.load(_SCREAMING_SNAKE_CASE , map_location="""cpu""" ) snake_case_ = model["""model"""] snake_case_ = BlenderbotConfig.from_json_file(_SCREAMING_SNAKE_CASE ) snake_case_ = BlenderbotForConditionalGeneration(_SCREAMING_SNAKE_CASE ) snake_case_ = m.model.state_dict().keys() snake_case_ = [] snake_case_ = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue snake_case_ = rename_state_dict_key(_SCREAMING_SNAKE_CASE ) if new_k not in valid_keys: failures.append([k, new_k] ) else: snake_case_ = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(_SCREAMING_SNAKE_CASE ) m.model.load_state_dict(_SCREAMING_SNAKE_CASE , strict=_SCREAMING_SNAKE_CASE ) m.half() m.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('--src_path', type=str, help='like blenderbot-model.bin') parser.add_argument('--save_dir', default='hf_blenderbot', type=str, help='Where to save converted model.') parser.add_argument( '--hf_config_json', default='blenderbot-3b-config.json', type=str, help='Path to config to use' ) __SCREAMING_SNAKE_CASE : Any = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
233
"""simple docstring""" def _a ( _SCREAMING_SNAKE_CASE ) -> int: assert column_title.isupper() snake_case_ = 0 snake_case_ = len(_SCREAMING_SNAKE_CASE ) - 1 snake_case_ = 0 while index >= 0: snake_case_ = (ord(column_title[index] ) - 64) * pow(26 , _SCREAMING_SNAKE_CASE ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
233
1
'''simple docstring''' import os __snake_case ={"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000} def a_ ( lowerCamelCase : str ): lowerCAmelCase = 0 lowerCAmelCase = 0 while index < len(lowerCamelCase ) - 1: lowerCAmelCase = SYMBOLS[numerals[index]] lowerCAmelCase = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def a_ ( lowerCamelCase : int ): lowerCAmelCase = '' lowerCAmelCase = num // 1000 numerals += m_count * "M" num %= 1000 lowerCAmelCase = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCAmelCase = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def a_ ( lowerCamelCase : str = "/p089_roman.txt" ): lowerCAmelCase = 0 with open(os.path.dirname(lowerCamelCase ) + roman_numerals_filename ) as filea: lowerCAmelCase = filea.readlines() for line in lines: lowerCAmelCase = line.strip() lowerCAmelCase = parse_roman_numerals(lowerCamelCase ) lowerCAmelCase = generate_roman_numerals(lowerCamelCase ) savings += len(lowerCamelCase ) - len(lowerCamelCase ) return savings if __name__ == "__main__": print(F'''{solution() = }''')
4
'''simple docstring''' import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self : str ) -> List[Any]: lowerCAmelCase = torch.nn.Linear(1_0 , 1_0 ) lowerCAmelCase = torch.optim.SGD(model.parameters() , 0.1 ) lowerCAmelCase = Accelerator() lowerCAmelCase = accelerator.prepare(UpperCAmelCase__ ) try: pickle.loads(pickle.dumps(UpperCAmelCase__ ) ) except Exception as e: self.fail(F'''Accelerated optimizer pickling failed with {e}''' ) AcceleratorState._reset_state()
4
1
"""simple docstring""" import re import string import numpy as np import datasets _lowercase : List[str] = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' _lowercase : Any = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' _lowercase : Dict = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __SCREAMING_SNAKE_CASE ( datasets.Metric ): '''simple docstring''' def snake_case ( self : str )-> List[str]: return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { '''predictions''': datasets.Value('''string''', id='''sequence''' ), '''references''': datasets.Value('''string''', id='''sequence''' ), } ), reference_urls=[], ) def snake_case ( self : List[str], lowerCamelCase : Optional[int], lowerCamelCase : Optional[int], lowerCamelCase : int=None, lowerCamelCase : List[Any]=False, lowerCamelCase : List[Any]=False, lowerCamelCase : int=False, )-> Optional[Any]: if regexes_to_ignore is not None: for s in regexes_to_ignore: lowerCamelCase__ : int =np.array([re.sub(__SCREAMING_SNAKE_CASE, '''''', __SCREAMING_SNAKE_CASE ) for x in predictions] ) lowerCamelCase__ : List[Any] =np.array([re.sub(__SCREAMING_SNAKE_CASE, '''''', __SCREAMING_SNAKE_CASE ) for x in references] ) else: lowerCamelCase__ : Any =np.asarray(__SCREAMING_SNAKE_CASE ) lowerCamelCase__ : List[Any] =np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: lowerCamelCase__ : Optional[int] =np.char.lower(__SCREAMING_SNAKE_CASE ) lowerCamelCase__ : Any =np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: lowerCamelCase__ : Optional[Any] =string.punctuation.maketrans('''''', '''''', string.punctuation ) lowerCamelCase__ : Optional[int] =np.char.translate(__SCREAMING_SNAKE_CASE, table=__SCREAMING_SNAKE_CASE ) lowerCamelCase__ : int =np.char.translate(__SCREAMING_SNAKE_CASE, table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: lowerCamelCase__ : Union[str, Any] =string.digits.maketrans('''''', '''''', string.digits ) lowerCamelCase__ : int =np.char.translate(__SCREAMING_SNAKE_CASE, table=__SCREAMING_SNAKE_CASE ) lowerCamelCase__ : Tuple =np.char.translate(__SCREAMING_SNAKE_CASE, table=__SCREAMING_SNAKE_CASE ) lowerCamelCase__ : int =predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
363
"""simple docstring""" from ...processing_utils import ProcessorMixin class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): '''simple docstring''' _a = 'SpeechT5FeatureExtractor' _a = 'SpeechT5Tokenizer' def __init__( self : Dict, lowerCamelCase : Optional[int], lowerCamelCase : str )-> Any: super().__init__(lowerCamelCase, lowerCamelCase ) def __call__( self : Tuple, *lowerCamelCase : List[str], **lowerCamelCase : Optional[int] )-> List[str]: lowerCamelCase__ : List[Any] =kwargs.pop('''audio''', lowerCamelCase ) lowerCamelCase__ : List[str] =kwargs.pop('''text''', lowerCamelCase ) lowerCamelCase__ : int =kwargs.pop('''text_target''', lowerCamelCase ) lowerCamelCase__ : Dict =kwargs.pop('''audio_target''', lowerCamelCase ) lowerCamelCase__ : Any =kwargs.pop('''sampling_rate''', lowerCamelCase ) if audio is not None and text is not None: raise ValueError( '''Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?''' ) if audio_target is not None and text_target is not None: raise ValueError( '''Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?''' ) if audio is None and audio_target is None and text is None and text_target is None: raise ValueError( '''You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process.''' ) if audio is not None: lowerCamelCase__ : Union[str, Any] =self.feature_extractor(lowerCamelCase, *lowerCamelCase, sampling_rate=lowerCamelCase, **lowerCamelCase ) elif text is not None: lowerCamelCase__ : List[Any] =self.tokenizer(lowerCamelCase, **lowerCamelCase ) else: lowerCamelCase__ : Any =None if audio_target is not None: lowerCamelCase__ : List[str] =self.feature_extractor(audio_target=lowerCamelCase, *lowerCamelCase, sampling_rate=lowerCamelCase, **lowerCamelCase ) lowerCamelCase__ : Tuple =targets['''input_values'''] elif text_target is not None: lowerCamelCase__ : Dict =self.tokenizer(lowerCamelCase, **lowerCamelCase ) lowerCamelCase__ : int =targets['''input_ids'''] else: lowerCamelCase__ : List[str] =None if inputs is None: return targets if targets is not None: lowerCamelCase__ : Dict =labels lowerCamelCase__ : Any =targets.get('''attention_mask''' ) if decoder_attention_mask is not None: lowerCamelCase__ : Dict =decoder_attention_mask return inputs def snake_case ( self : int, *lowerCamelCase : Optional[Any], **lowerCamelCase : Optional[int] )-> Optional[Any]: lowerCamelCase__ : List[Any] =kwargs.pop('''input_values''', lowerCamelCase ) lowerCamelCase__ : Union[str, Any] =kwargs.pop('''input_ids''', lowerCamelCase ) lowerCamelCase__ : Optional[Any] =kwargs.pop('''labels''', lowerCamelCase ) if input_values is not None and input_ids is not None: raise ValueError('''Cannot process both `input_values` and `input_ids` inputs.''' ) if input_values is None and input_ids is None and labels is None: raise ValueError( '''You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded.''' ) if input_values is not None: lowerCamelCase__ : List[str] =self.feature_extractor.pad(lowerCamelCase, *lowerCamelCase, **lowerCamelCase ) elif input_ids is not None: lowerCamelCase__ : Tuple =self.tokenizer.pad(lowerCamelCase, **lowerCamelCase ) else: lowerCamelCase__ : Any =None if labels is not None: if "input_ids" in labels or (isinstance(lowerCamelCase, lowerCamelCase ) and "input_ids" in labels[0]): lowerCamelCase__ : str =self.tokenizer.pad(lowerCamelCase, **lowerCamelCase ) lowerCamelCase__ : List[Any] =targets['''input_ids'''] else: lowerCamelCase__ : Any =self.feature_extractor.feature_size lowerCamelCase__ : Optional[Any] =self.feature_extractor.num_mel_bins lowerCamelCase__ : Optional[int] =self.feature_extractor.pad(lowerCamelCase, *lowerCamelCase, **lowerCamelCase ) lowerCamelCase__ : List[Any] =feature_size_hack lowerCamelCase__ : Tuple =targets['''input_values'''] else: lowerCamelCase__ : Optional[Any] =None if inputs is None: return targets if targets is not None: lowerCamelCase__ : Tuple =labels lowerCamelCase__ : Optional[int] =targets.get('''attention_mask''' ) if decoder_attention_mask is not None: lowerCamelCase__ : Optional[Any] =decoder_attention_mask return inputs def snake_case ( self : List[str], *lowerCamelCase : Union[str, Any], **lowerCamelCase : List[Any] )-> List[Any]: return self.tokenizer.batch_decode(*lowerCamelCase, **lowerCamelCase ) def snake_case ( self : List[str], *lowerCamelCase : List[Any], **lowerCamelCase : Tuple )-> int: return self.tokenizer.decode(*lowerCamelCase, **lowerCamelCase )
272
0
import argparse import torch from transformers import MobileBertConfig, MobileBertForPreTraining, load_tf_weights_in_mobilebert from transformers.utils import logging logging.set_verbosity_info() def _UpperCAmelCase ( a__ , a__ , a__): '''simple docstring''' a_ : Tuple = MobileBertConfig.from_json_file(a__) print(f'''Building PyTorch model from configuration: {config}''') a_ : Dict = MobileBertForPreTraining(a__) # Load weights from tf checkpoint a_ : List[str] = load_tf_weights_in_mobilebert(a__ , a__ , a__) # Save pytorch-model print(f'''Save PyTorch model to {pytorch_dump_path}''') torch.save(model.state_dict() , a__) if __name__ == "__main__": __snake_case : str = argparse.ArgumentParser() # Required parameters parser.add_argument( """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--mobilebert_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained MobileBERT model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) __snake_case : Optional[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.mobilebert_config_file, args.pytorch_dump_path)
248
from typing import Any class A__: """simple docstring""" def __init__( self , _lowercase ) -> List[str]: a_ : List[str] = data a_ : Optional[int] = None def __repr__( self ) -> str: return F'''Node({self.data})''' class A__: """simple docstring""" def __init__( self ) -> Optional[Any]: a_ : Dict = None def __iter__( self ) -> Any: a_ : Optional[Any] = self.head while node: yield node.data a_ : Union[str, Any] = node.next def __len__( self ) -> int: return sum(1 for _ in self ) def __repr__( self ) -> str: return "->".join([str(_lowercase ) for item in self] ) def __getitem__( self , _lowercase ) -> Any: if not 0 <= index < len(self ): raise ValueError("""list index out of range.""" ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self , _lowercase , _lowercase ) -> None: if not 0 <= index < len(self ): raise ValueError("""list index out of range.""" ) a_ : Optional[Any] = self.head for _ in range(_lowercase ): a_ : List[str] = current.next a_ : Any = data def UpperCamelCase__ ( self , _lowercase ) -> None: self.insert_nth(len(self ) , _lowercase ) def UpperCamelCase__ ( self , _lowercase ) -> None: self.insert_nth(0 , _lowercase ) def UpperCamelCase__ ( self , _lowercase , _lowercase ) -> None: if not 0 <= index <= len(self ): raise IndexError("""list index out of range""" ) a_ : Optional[int] = Node(_lowercase ) if self.head is None: a_ : int = new_node elif index == 0: a_ : List[Any] = self.head # link new_node to head a_ : Any = new_node else: a_ : Optional[int] = self.head for _ in range(index - 1 ): a_ : Optional[int] = temp.next a_ : Optional[int] = temp.next a_ : int = new_node def UpperCamelCase__ ( self ) -> None: # print every node data print(self ) def UpperCamelCase__ ( self ) -> Any: return self.delete_nth(0 ) def UpperCamelCase__ ( self ) -> Any: # delete from tail return self.delete_nth(len(self ) - 1 ) def UpperCamelCase__ ( self , _lowercase = 0 ) -> Any: if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("""List index out of range.""" ) a_ : Optional[int] = self.head # default first node if index == 0: a_ : List[Any] = self.head.next else: a_ : List[Any] = self.head for _ in range(index - 1 ): a_ : List[Any] = temp.next a_ : Any = temp.next a_ : Any = temp.next.next return delete_node.data def UpperCamelCase__ ( self ) -> bool: return self.head is None def UpperCamelCase__ ( self ) -> None: a_ : Any = None a_ : Union[str, Any] = self.head while current: # Store the current node's next node. a_ : Dict = current.next # Make the current node's next point backwards a_ : Optional[Any] = prev # Make the previous node be the current node a_ : Optional[int] = current # Make the current node the next node (to progress iteration) a_ : List[str] = next_node # Return prev in order to put the head at the end a_ : Dict = prev def _UpperCAmelCase ( ): '''simple docstring''' a_ : Union[str, Any] = LinkedList() assert linked_list.is_empty() is True assert str(a__) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(1_0): assert len(a__) == i linked_list.insert_nth(a__ , i + 1) assert str(a__) == "->".join(str(a__) for i in range(1 , 1_1)) linked_list.insert_head(0) linked_list.insert_tail(1_1) assert str(a__) == "->".join(str(a__) for i in range(0 , 1_2)) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9) == 1_0 assert linked_list.delete_tail() == 1_1 assert len(a__) == 9 assert str(a__) == "->".join(str(a__) for i in range(1 , 1_0)) assert all(linked_list[i] == i + 1 for i in range(0 , 9)) is True for i in range(0 , 9): a_ : Dict = -i assert all(linked_list[i] == -i for i in range(0 , 9)) is True linked_list.reverse() assert str(a__) == "->".join(str(a__) for i in range(-8 , 1)) def _UpperCAmelCase ( ): '''simple docstring''' a_ : int = [ -9, 1_0_0, Node(7_7_3_4_5_1_1_2), """dlrow olleH""", 7, 5_5_5_5, 0, -192.5_5555, """Hello, world!""", 77.9, Node(1_0), None, None, 12.20, ] a_ : Optional[int] = LinkedList() for i in test_input: linked_list.insert_tail(a__) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(a__) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head a_ : Union[str, Any] = linked_list.delete_head() assert result == -9 assert ( str(a__) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail a_ : Any = linked_list.delete_tail() assert result == 12.2 assert ( str(a__) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list a_ : List[Any] = linked_list.delete_nth(1_0) assert result is None assert ( str(a__) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("""Hello again, world!""")) assert ( str(a__) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(a__) assert ( str(a__) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(a__) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def _UpperCAmelCase ( ): '''simple docstring''' from doctest import testmod testmod() a_ : List[Any] = LinkedList() linked_list.insert_head(input("""Inserting 1st at head """).strip()) linked_list.insert_head(input("""Inserting 2nd at head """).strip()) print("""\nPrint list:""") linked_list.print_list() linked_list.insert_tail(input("""\nInserting 1st at tail """).strip()) linked_list.insert_tail(input("""Inserting 2nd at tail """).strip()) print("""\nPrint list:""") linked_list.print_list() print("""\nDelete head""") linked_list.delete_head() print("""Delete tail""") linked_list.delete_tail() print("""\nPrint list:""") linked_list.print_list() print("""\nReverse linked list""") linked_list.reverse() print("""\nPrint list:""") linked_list.print_list() print("""\nString representation of linked list:""") print(a__) print("""\nReading/changing Node data using indexing:""") print(f'''Element at Position 1: {linked_list[1]}''') a_ : List[Any] = input("""Enter New Value: """).strip() print("""New list:""") print(a__) print(f'''length of linked_list is : {len(a__)}''') if __name__ == "__main__": main()
248
1
"""simple docstring""" import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers __UpperCamelCase : Optional[int] = '''python tqdm regex requests packaging filelock numpy tokenizers'''.split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append('''dataclasses''') if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append('''importlib_metadata''') for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : List[str] , _UpperCAmelCase : Union[str, Any]=None ): require_version(deps[pkg] , snake_case__ )
365
"""simple docstring""" import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( 'split_dict' , [ SplitDict(), SplitDict({'train': SplitInfo(name='train' , num_bytes=1337 , num_examples=42 , dataset_name='my_dataset' )} ), SplitDict({'train': SplitInfo(name='train' , num_bytes=1337 , num_examples=42 )} ), SplitDict({'train': SplitInfo()} ), ] , ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : SplitDict ): lowerCAmelCase = split_dict._to_yaml_list() assert len(_UpperCAmelCase ) == len(_UpperCAmelCase ) lowerCAmelCase = SplitDict._from_yaml_list(_UpperCAmelCase ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump lowerCAmelCase = None # the split name of split_dict takes over the name of the split info object lowerCAmelCase = split_name assert split_dict == reloaded @pytest.mark.parametrize( 'split_info' , [SplitInfo(), SplitInfo(dataset_name=_UpperCAmelCase ), SplitInfo(dataset_name='my_dataset' )] ) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : List[str] ): # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files lowerCAmelCase = asdict(SplitDict({'train': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
309
0
"""simple docstring""" from __future__ import annotations def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = 0.00 __lowerCAmelCase = 0 for resistor in resistors: if resistor <= 0: __lowerCAmelCase = f"Resistor at index {index} has a negative or zero value!" raise ValueError(_UpperCamelCase ) first_sum += 1 / float(_UpperCamelCase ) index += 1 return 1 / first_sum def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' __lowerCAmelCase = 0.00 __lowerCAmelCase = 0 for resistor in resistors: sum_r += resistor if resistor < 0: __lowerCAmelCase = f"Resistor at index {index} has a negative value!" raise ValueError(_UpperCamelCase ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
57
"""simple docstring""" import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class A__ ( _lowerCamelCase , unittest.TestCase): A_ : Union[str, Any] = BarthezTokenizer A_ : Tuple = BarthezTokenizerFast A_ : Dict = True A_ : List[str] = True def __lowerCamelCase ( self ): super().setUp() __lowerCAmelCase : str = BarthezTokenizerFast.from_pretrained('moussaKam/mbarthez' ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = tokenizer def __lowerCamelCase ( self ): __lowerCAmelCase : Optional[Any] = '<pad>' __lowerCAmelCase : Union[str, Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): __lowerCAmelCase : List[Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , '<mask>' ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , 10_11_22 ) def __lowerCamelCase ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 10_11_22 ) @require_torch def __lowerCamelCase ( self ): __lowerCAmelCase : List[str] = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] __lowerCAmelCase : Optional[Any] = [0, 57, 30_18, 7_03_07, 91, 2] __lowerCAmelCase : Optional[int] = self.tokenizer( _SCREAMING_SNAKE_CASE , max_length=len(_SCREAMING_SNAKE_CASE ) , padding=_SCREAMING_SNAKE_CASE , truncation=_SCREAMING_SNAKE_CASE , return_tensors='pt' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) __lowerCAmelCase : List[str] = batch.input_ids.tolist()[0] self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): if not self.test_rust_tokenizer: return __lowerCAmelCase : Tuple = self.get_tokenizer() __lowerCAmelCase : Optional[int] = self.get_rust_tokenizer() __lowerCAmelCase : List[str] = 'I was born in 92000, and this is falsé.' __lowerCAmelCase : Optional[int] = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Union[str, Any] = rust_tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[Any] = tokenizer.encode(_SCREAMING_SNAKE_CASE , add_special_tokens=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = rust_tokenizer.encode(_SCREAMING_SNAKE_CASE , add_special_tokens=_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = self.get_rust_tokenizer() __lowerCAmelCase : List[Any] = tokenizer.encode(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Dict = rust_tokenizer.encode(_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @slow def __lowerCamelCase ( self ): # fmt: off __lowerCAmelCase : str = {'input_ids': [[0, 4_90, 1_43_28, 45_07, 3_54, 47, 4_36_69, 95, 25, 7_81_17, 2_02_15, 1_97_79, 1_90, 22, 4_00, 4, 3_53_43, 8_03_10, 6_03, 86, 2_49_37, 1_05, 3_34_38, 9_47_62, 1_96, 3_96_42, 7, 15, 1_59_33, 1_73, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_05_34, 87, 25, 66, 33_58, 1_96, 5_52_89, 8, 8_29_61, 81, 22_04, 7_52_03, 7, 15, 7_63, 1_29_56, 2_16, 1_78, 1_43_28, 95_95, 13_77, 6_96_93, 7, 4_48, 7_10_21, 1_96, 1_81_06, 14_37, 1_39_74, 1_08, 90_83, 4, 4_93_15, 7, 39, 86, 13_26, 27_93, 4_63_33, 4, 4_48, 1_96, 7_45_88, 7, 4_93_15, 7, 39, 21, 8_22, 3_84_70, 74, 21, 6_67_23, 6_24_80, 8, 2_20_50, 5, 2]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. __lowerCAmelCase : Union[str, Any] = [ 'Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ' 'utilisé principalement dans le domaine du traitement automatique des langues (TAL).', 'À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ' 'pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ' 'telles que la traduction et la synthèse de texte.', ] self.tokenizer_integration_test_util( expected_encoding=_SCREAMING_SNAKE_CASE , model_name='moussaKam/mbarthez' , revision='c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6' , sequences=_SCREAMING_SNAKE_CASE , )
86
0
'''simple docstring''' import time import warnings from abc import ABC from copy import deepcopy from typing import Optional import torch from ..utils import add_start_docstrings, logging lowercase = logging.get_logger(__name__) lowercase = R'\n Args:\n input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):\n Indices of input sequence tokens in the vocabulary.\n\n Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and\n [`PreTrainedTokenizer.__call__`] for details.\n\n [What are input IDs?](../glossary#input-ids)\n scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`):\n Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax\n or scores for each vocabulary token after SoftMax.\n kwargs (`Dict[str, Any]`, *optional*):\n Additional stopping criteria specific kwargs.\n\n Return:\n `bool`. `False` indicates we should continue, `True` indicates we should stop.\n\n' class A ( __snake_case ): @add_start_docstrings(SCREAMING_SNAKE_CASE ) def __call__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" raise NotImplementedError('''StoppingCriteria needs to be subclassed''' ) class A ( __snake_case ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = None ) -> Dict: """simple docstring""" A : Union[str, Any] = max_length A : List[Any] = max_position_embeddings @add_start_docstrings(SCREAMING_SNAKE_CASE ) def __call__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" A : Any = input_ids.shape[-1] A : List[Any] = cur_len >= self.max_length if self.max_position_embeddings is not None and not is_done and cur_len >= self.max_position_embeddings: logger.warning_once( '''This is a friendly reminder - the current text generation call will exceed the model\'s predefined ''' F'maximum length ({self.max_position_embeddings}). Depending on the model, you may observe ' '''exceptions, performance degradation, or nothing at all.''' ) return is_done class A ( __snake_case ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" warnings.warn( '''The class `MaxNewTokensCriteria` is deprecated. ''' F'Please use `MaxLengthCriteria(max_length={start_length + max_new_tokens})` ' '''with `max_length = start_length + max_new_tokens` instead.''' , SCREAMING_SNAKE_CASE , ) A : Optional[Any] = start_length A : Optional[int] = max_new_tokens A : Optional[Any] = start_length + max_new_tokens @add_start_docstrings(SCREAMING_SNAKE_CASE ) def __call__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" return input_ids.shape[-1] >= self.max_length class A ( __snake_case ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = None ) -> List[Any]: """simple docstring""" A : List[str] = max_time A : Optional[int] = time.time() if initial_timestamp is None else initial_timestamp @add_start_docstrings(SCREAMING_SNAKE_CASE ) def __call__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" return time.time() - self.initial_timestamp > self.max_time class A ( __snake_case ): @add_start_docstrings(SCREAMING_SNAKE_CASE ) def __call__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" return any(criteria(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for criteria in self ) @property def __lowerCAmelCase ( self ) -> Optional[int]: """simple docstring""" for stopping_criterium in self: if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return stopping_criterium.max_length elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): return stopping_criterium.max_length return None def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' A : Any = stopping_criteria.max_length A : Dict = deepcopy(snake_case__ ) if stopping_max_length is not None and stopping_max_length != max_length: warnings.warn('''You set different `max_length` for stopping criteria and `max_length` parameter''' , snake_case__ ) elif stopping_max_length is None: new_stopping_criteria.append(MaxLengthCriteria(max_length=snake_case__ ) ) return new_stopping_criteria
356
'''simple docstring''' # limitations under the License. from typing import Optional, Tuple, Union import torch from diffusers import DiffusionPipeline, ImagePipelineOutput class A ( __snake_case ): def __init__( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> Dict: """simple docstring""" super().__init__() self.register_modules(unet=SCREAMING_SNAKE_CASE , scheduler=SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self , SCREAMING_SNAKE_CASE = 1 , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = 50 , SCREAMING_SNAKE_CASE = "pil" , SCREAMING_SNAKE_CASE = True , **SCREAMING_SNAKE_CASE , ) -> Union[ImagePipelineOutput, Tuple]: """simple docstring""" A : List[Any] = torch.randn( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=SCREAMING_SNAKE_CASE , ) A : Optional[Any] = image.to(self.device ) # set step values self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output A : Tuple = self.unet(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 A : List[Any] = self.scheduler.step(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ).prev_sample A : List[Any] = (image / 2 + 0.5).clamp(0 , 1 ) A : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": A : List[Any] = self.numpy_to_pil(SCREAMING_SNAKE_CASE ) if not return_dict: return (image,), "This is a local test" return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE ), "This is a local test"
311
0
import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class lowerCAmelCase ( __a ): '''simple docstring''' def __init__( self : str ) -> List[str]: """simple docstring""" __lowercase : str = [] def lowerCAmelCase ( self : int , __a : Optional[int] , __a : Tuple , __a : Optional[int] , **__a : Optional[int] ) -> int: """simple docstring""" self.events.append("""on_init_end""" ) def lowerCAmelCase ( self : int , __a : Dict , __a : str , __a : Dict , **__a : Optional[Any] ) -> Optional[Any]: """simple docstring""" self.events.append("""on_train_begin""" ) def lowerCAmelCase ( self : Optional[Any] , __a : Union[str, Any] , __a : str , __a : Dict , **__a : Dict ) -> Any: """simple docstring""" self.events.append("""on_train_end""" ) def lowerCAmelCase ( self : Any , __a : Any , __a : Union[str, Any] , __a : Union[str, Any] , **__a : List[str] ) -> Optional[Any]: """simple docstring""" self.events.append("""on_epoch_begin""" ) def lowerCAmelCase ( self : str , __a : int , __a : Union[str, Any] , __a : Optional[Any] , **__a : List[str] ) -> str: """simple docstring""" self.events.append("""on_epoch_end""" ) def lowerCAmelCase ( self : List[str] , __a : List[str] , __a : Any , __a : Optional[int] , **__a : str ) -> str: """simple docstring""" self.events.append("""on_step_begin""" ) def lowerCAmelCase ( self : Optional[Any] , __a : Any , __a : Tuple , __a : int , **__a : Optional[int] ) -> Dict: """simple docstring""" self.events.append("""on_step_end""" ) def lowerCAmelCase ( self : Optional[Any] , __a : Union[str, Any] , __a : Dict , __a : List[str] , **__a : Union[str, Any] ) -> Tuple: """simple docstring""" self.events.append("""on_evaluate""" ) def lowerCAmelCase ( self : Optional[Any] , __a : Union[str, Any] , __a : Any , __a : Any , **__a : Optional[Any] ) -> Optional[int]: """simple docstring""" self.events.append("""on_predict""" ) def lowerCAmelCase ( self : Any , __a : List[Any] , __a : Optional[int] , __a : Optional[int] , **__a : Optional[int] ) -> Any: """simple docstring""" self.events.append("""on_save""" ) def lowerCAmelCase ( self : Optional[int] , __a : Union[str, Any] , __a : List[str] , __a : List[Any] , **__a : str ) -> str: """simple docstring""" self.events.append("""on_log""" ) def lowerCAmelCase ( self : Dict , __a : Union[str, Any] , __a : Union[str, Any] , __a : List[Any] , **__a : str ) -> int: """simple docstring""" self.events.append("""on_prediction_step""" ) @require_torch class lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase ( self : int ) -> Any: """simple docstring""" __lowercase : Any = tempfile.mkdtemp() def lowerCAmelCase ( self : str ) -> int: """simple docstring""" shutil.rmtree(self.output_dir ) def lowerCAmelCase ( self : List[Any] , __a : int=0 , __a : Optional[int]=0 , __a : str=64 , __a : Optional[Any]=64 , __a : List[Any]=None , __a : Tuple=False , **__a : str ) -> str: """simple docstring""" __lowercase : Union[str, Any] = RegressionDataset(length=__a ) __lowercase : Any = RegressionDataset(length=__a ) __lowercase : Union[str, Any] = RegressionModelConfig(a=__a , b=__a ) __lowercase : int = RegressionPreTrainedModel(__a ) __lowercase : Any = TrainingArguments(self.output_dir , disable_tqdm=__a , report_to=[] , **__a ) return Trainer( __a , __a , train_dataset=__a , eval_dataset=__a , callbacks=__a , ) def lowerCAmelCase ( self : Union[str, Any] , __a : Optional[Any] , __a : Dict ) -> List[Any]: """simple docstring""" self.assertEqual(len(__a ) , len(__a ) ) # Order doesn't matter __lowercase : List[str] = sorted(__a , key=lambda __a : cb.__name__ if isinstance(__a , __a ) else cb.__class__.__name__ ) __lowercase : Optional[int] = sorted(__a , key=lambda __a : cb.__name__ if isinstance(__a , __a ) else cb.__class__.__name__ ) for cba, cba in zip(__a , __a ): if isinstance(__a , __a ) and isinstance(__a , __a ): self.assertEqual(__a , __a ) elif isinstance(__a , __a ) and not isinstance(__a , __a ): self.assertEqual(__a , cba.__class__ ) elif not isinstance(__a , __a ) and isinstance(__a , __a ): self.assertEqual(cba.__class__ , __a ) else: self.assertEqual(__a , __a ) def lowerCAmelCase ( self : Union[str, Any] , __a : Optional[int] ) -> int: """simple docstring""" __lowercase : List[str] = ["""on_init_end""", """on_train_begin"""] __lowercase : Tuple = 0 __lowercase : int = len(trainer.get_eval_dataloader() ) __lowercase : List[Any] = ["""on_prediction_step"""] * len(trainer.get_eval_dataloader() ) + ["""on_log""", """on_evaluate"""] for _ in range(trainer.state.num_train_epochs ): expected_events.append("""on_epoch_begin""" ) for _ in range(__a ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("""on_log""" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("""on_save""" ) expected_events.append("""on_epoch_end""" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def lowerCAmelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" __lowercase : Dict = self.get_trainer() __lowercase : Optional[int] = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) # Callbacks passed at init are added to the default callbacks __lowercase : Optional[Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(__a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback __lowercase : Any = self.get_trainer(disable_tqdm=__a ) __lowercase : Any = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) def lowerCAmelCase ( self : List[Any] ) -> List[str]: """simple docstring""" __lowercase : int = DEFAULT_CALLBACKS.copy() + [ProgressCallback] __lowercase : int = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(__a ) expected_callbacks.remove(__a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) __lowercase : str = self.get_trainer() __lowercase : List[Any] = trainer.pop_callback(__a ) self.assertEqual(cb.__class__ , __a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) trainer.add_callback(__a ) expected_callbacks.insert(0 , __a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) # We can also add, pop, or remove by instance __lowercase : Dict = self.get_trainer() __lowercase : Tuple = trainer.callback_handler.callbacks[0] trainer.remove_callback(__a ) expected_callbacks.remove(__a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) __lowercase : Union[str, Any] = self.get_trainer() __lowercase : str = trainer.callback_handler.callbacks[0] __lowercase : List[str] = trainer.pop_callback(__a ) self.assertEqual(__a , __a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) trainer.add_callback(__a ) expected_callbacks.insert(0 , __a ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __a ) def lowerCAmelCase ( self : Any ) -> List[Any]: """simple docstring""" import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="""ignore""" , category=__a ) __lowercase : Optional[Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() __lowercase : List[str] = trainer.callback_handler.callbacks[-2].events self.assertEqual(__a , self.get_expected_events(__a ) ) # Independent log/save/eval __lowercase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() __lowercase : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(__a , self.get_expected_events(__a ) ) __lowercase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() __lowercase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(__a , self.get_expected_events(__a ) ) __lowercase : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="""steps""" ) trainer.train() __lowercase : Union[str, Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(__a , self.get_expected_events(__a ) ) __lowercase : Union[str, Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="""epoch""" ) trainer.train() __lowercase : str = trainer.callback_handler.callbacks[-2].events self.assertEqual(__a , self.get_expected_events(__a ) ) # A bit of everything __lowercase : Optional[int] = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=10 , eval_steps=5 , evaluation_strategy="""steps""" , ) trainer.train() __lowercase : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(__a , self.get_expected_events(__a ) ) # warning should be emitted for duplicated callbacks with patch("""transformers.trainer_callback.logger.warning""" ) as warn_mock: __lowercase : List[str] = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(__a ) in warn_mock.call_args[0][0]
233
import argparse from transformers import ( TapasConfig, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasTokenizer, load_tf_weights_in_tapas, ) from transformers.utils import logging logging.set_verbosity_info() def snake_case_ ( lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Dict , lowerCAmelCase_ : Any , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : int ): # Initialise PyTorch model. # If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of # TapasConfig to False. # initialize configuration from json file __lowercase : Optional[int] = TapasConfig.from_json_file(lowerCAmelCase_ ) # set absolute/relative position embeddings parameter __lowercase : Optional[Any] = reset_position_index_per_cell # set remaining parameters of TapasConfig as well as the model based on the task if task == "SQA": __lowercase : Union[str, Any] = TapasForQuestionAnswering(config=lowerCAmelCase_ ) elif task == "WTQ": # run_task_main.py hparams __lowercase : List[Any] = 4 __lowercase : Union[str, Any] = True # hparam_utils.py hparams __lowercase : Any = 0.664_694 __lowercase : Tuple = 0.207_951 __lowercase : Dict = 0.121_194 __lowercase : List[str] = True __lowercase : str = True __lowercase : Dict = False __lowercase : Tuple = 0.0_352_513 __lowercase : List[Any] = TapasForQuestionAnswering(config=lowerCAmelCase_ ) elif task == "WIKISQL_SUPERVISED": # run_task_main.py hparams __lowercase : Optional[int] = 4 __lowercase : int = False # hparam_utils.py hparams __lowercase : Tuple = 36.4_519 __lowercase : str = 0.903_421 __lowercase : List[Any] = 222.088 __lowercase : Union[str, Any] = True __lowercase : Tuple = True __lowercase : Union[str, Any] = True __lowercase : Optional[Any] = 0.763_141 __lowercase : str = TapasForQuestionAnswering(config=lowerCAmelCase_ ) elif task == "TABFACT": __lowercase : List[Any] = TapasForSequenceClassification(config=lowerCAmelCase_ ) elif task == "MLM": __lowercase : Optional[int] = TapasForMaskedLM(config=lowerCAmelCase_ ) elif task == "INTERMEDIATE_PRETRAINING": __lowercase : Dict = TapasModel(config=lowerCAmelCase_ ) else: raise ValueError(F"Task {task} not supported." ) print(F"Building PyTorch model from configuration: {config}" ) # Load weights from tf checkpoint load_tf_weights_in_tapas(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) # Save pytorch-model (weights and configuration) print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(lowerCAmelCase_ ) # Save tokenizer files print(F"Save tokenizer files to {pytorch_dump_path}" ) __lowercase : Any = TapasTokenizer(vocab_file=tf_checkpoint_path[:-10] + """vocab.txt""" , model_max_length=512 ) tokenizer.save_pretrained(lowerCAmelCase_ ) print("""Used relative position embeddings:""" , model.config.reset_position_index_per_cell ) if __name__ == "__main__": lowerCamelCase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''SQA''', type=str, help='''Model task for which to convert a checkpoint. Defaults to SQA.''' ) parser.add_argument( '''--reset_position_index_per_cell''', default=False, action='''store_true''', help='''Whether to use relative position embeddings or not. Defaults to True.''', ) parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--tapas_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained TAPAS model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) lowerCamelCase : int = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.task, args.reset_position_index_per_cell, args.tf_checkpoint_path, args.tapas_config_file, args.pytorch_dump_path, )
233
1
from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 lowerCamelCase__ = { # 1536-bit 5: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 2048-bit 14: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AACAA68FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 3072-bit 15: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64''' + '''ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7''' + '''ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B''' + '''F12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31''' + '''43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 4096-bit 16: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64''' + '''ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7''' + '''ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B''' + '''F12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31''' + '''43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7''' + '''88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA''' + '''2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6''' + '''287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED''' + '''1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9''' + '''93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199''' + '''FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 6144-bit 17: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08''' + '''8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B''' + '''302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9''' + '''A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6''' + '''49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8''' + '''FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C''' + '''180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718''' + '''3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D''' + '''04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D''' + '''B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226''' + '''1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC''' + '''E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26''' + '''99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB''' + '''04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2''' + '''233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127''' + '''D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492''' + '''36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406''' + '''AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918''' + '''DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151''' + '''2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03''' + '''F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F''' + '''BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA''' + '''CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B''' + '''B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632''' + '''387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E''' + '''6DCC4024FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 8192-bit 18: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64''' + '''ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7''' + '''ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B''' + '''F12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31''' + '''43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7''' + '''88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA''' + '''2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6''' + '''287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED''' + '''1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9''' + '''93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492''' + '''36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD''' + '''F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831''' + '''179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B''' + '''DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF''' + '''5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6''' + '''D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3''' + '''23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA''' + '''CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328''' + '''06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C''' + '''DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE''' + '''12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4''' + '''38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300''' + '''741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568''' + '''3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9''' + '''22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B''' + '''4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A''' + '''062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36''' + '''4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1''' + '''B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92''' + '''4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47''' + '''9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71''' + '''60C980DD98EDD3DFFFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, } class _UpperCAmelCase : '''simple docstring''' def __init__( self : List[Any] , lowercase_ : int = 14) -> None: """simple docstring""" if group not in primes: raise ValueError("Unsupported Group") _UpperCamelCase = primes[group]["prime"] _UpperCamelCase = primes[group]["generator"] _UpperCamelCase = int(hexlify(urandom(32)) , base=16) def __UpperCAmelCase ( self : int) -> str: """simple docstring""" return hex(self.__private_key)[2:] def __UpperCAmelCase ( self : Dict) -> str: """simple docstring""" _UpperCamelCase = pow(self.generator , self.__private_key , self.prime) return hex(lowercase_)[2:] def __UpperCAmelCase ( self : Dict , lowercase_ : int) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(lowercase_ , (self.prime - 1) // 2 , self.prime) == 1 ) def __UpperCAmelCase ( self : List[str] , lowercase_ : str) -> str: """simple docstring""" _UpperCamelCase = int(lowercase_ , base=16) if not self.is_valid_public_key(lowercase_): raise ValueError("Invalid public key") _UpperCamelCase = pow(lowercase_ , self.__private_key , self.prime) return shaaaa(str(lowercase_).encode()).hexdigest() @staticmethod def __UpperCAmelCase ( lowercase_ : int , lowercase_ : int) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(lowercase_ , (prime - 1) // 2 , lowercase_) == 1 ) @staticmethod def __UpperCAmelCase ( lowercase_ : str , lowercase_ : str , lowercase_ : int = 14) -> str: """simple docstring""" _UpperCamelCase = int(lowercase_ , base=16) _UpperCamelCase = int(lowercase_ , base=16) _UpperCamelCase = primes[group]["prime"] if not DiffieHellman.is_valid_public_key_static(lowercase_ , lowercase_): raise ValueError("Invalid public key") _UpperCamelCase = pow(lowercase_ , lowercase_ , lowercase_) return shaaaa(str(lowercase_).encode()).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
63
import requests from bsa import BeautifulSoup def lowerCAmelCase__ ( a__ = "https://www.worldometers.info/coronavirus" ) ->dict: '''simple docstring''' _UpperCamelCase = BeautifulSoup(requests.get(a__ ).text , "html.parser" ) _UpperCamelCase = soup.findAll("h1" ) _UpperCamelCase = soup.findAll("div" , {"class": "maincounter-number"} ) keys += soup.findAll("span" , {"class": "panel-title"} ) values += soup.findAll("div" , {"class": "number-table-main"} ) return {key.text.strip(): value.text.strip() for key, value in zip(a__ , a__ )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(F"{key}\n{value}\n")
63
1
import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): UpperCAmelCase__ = BertJapaneseTokenizer UpperCAmelCase__ = False UpperCAmelCase__ = True def A_ ( self : Optional[int] ) -> Dict: super().setUp() lowerCamelCase__ : str = [ '[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは', '世界', '##世界', '、', '##、', '。', '##。', ] lowerCamelCase__ : 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 A_ ( self : Tuple , UpperCAmelCase : Dict ) -> Optional[int]: lowerCamelCase__ : Tuple = 'こんにちは、世界。 \nこんばんは、世界。' lowerCamelCase__ : Union[str, Any] = 'こんにちは 、 世界 。 こんばんは 、 世界 。' return input_text, output_text def A_ ( self : List[str] , UpperCAmelCase : List[Any] ) -> str: lowerCamelCase__ , lowerCamelCase__ : str = self.get_input_output_texts(UpperCAmelCase ) lowerCamelCase__ : int = tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) lowerCamelCase__ : Union[str, Any] = tokenizer.decode(UpperCAmelCase , clean_up_tokenization_spaces=UpperCAmelCase ) return text, ids def A_ ( self : Dict ) -> List[str]: pass # TODO add if relevant def A_ ( self : List[Any] ) -> Dict: pass # TODO add if relevant def A_ ( self : Any ) -> Dict: pass # TODO add if relevant def A_ ( self : Any ) -> Optional[int]: lowerCamelCase__ : Optional[Any] = self.tokenizer_class(self.vocab_file ) lowerCamelCase__ : str = tokenizer.tokenize('こんにちは、世界。\nこんばんは、世界。' ) self.assertListEqual(UpperCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) def A_ ( self : Any ) -> Tuple: lowerCamelCase__ : Optional[int] = self.tokenizer_class(self.vocab_file , word_tokenizer_type='mecab' ) self.assertIsNotNone(UpperCAmelCase ) lowerCamelCase__ : Any = 'こんにちは、世界。\nこんばんは、世界。' lowerCamelCase__ : Union[str, Any] = tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) lowerCamelCase__ : Any = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(UpperCAmelCase , 'wb' ) as handle: pickle.dump(UpperCAmelCase , UpperCAmelCase ) with open(UpperCAmelCase , 'rb' ) as handle: lowerCamelCase__ : Tuple = pickle.load(UpperCAmelCase ) lowerCamelCase__ : List[str] = tokenizer_new.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) def A_ ( self : List[Any] ) -> List[Any]: lowerCamelCase__ : Union[str, Any] = MecabTokenizer(mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def A_ ( self : Union[str, Any] ) -> Any: try: lowerCamelCase__ : Optional[int] = MecabTokenizer(mecab_dic='unidic_lite' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def A_ ( self : int ) -> Union[str, Any]: try: lowerCamelCase__ : Optional[Any] = MecabTokenizer(mecab_dic='unidic' ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def A_ ( self : str ) -> Optional[int]: lowerCamelCase__ : Union[str, Any] = MecabTokenizer(do_lower_case=UpperCAmelCase , mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iphone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) def A_ ( self : int ) -> List[str]: try: lowerCamelCase__ : Optional[int] = MecabTokenizer( do_lower_case=UpperCAmelCase , normalize_text=UpperCAmelCase , mecab_option='-d /usr/local/lib/mecab/dic/jumandic' ) except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '\u3000', '。'] , ) def A_ ( self : Dict ) -> Tuple: lowerCamelCase__ : Any = MecabTokenizer(normalize_text=UpperCAmelCase , mecab_dic='ipadic' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', ' ', '。'] , ) @require_sudachi def A_ ( self : List[Any] ) -> Optional[Any]: lowerCamelCase__ : Dict = self.tokenizer_class(self.vocab_file , word_tokenizer_type='sudachi' ) self.assertIsNotNone(UpperCAmelCase ) lowerCamelCase__ : Union[str, Any] = 'こんにちは、世界。\nこんばんは、世界。' lowerCamelCase__ : List[Any] = tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) lowerCamelCase__ : Optional[Any] = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(UpperCAmelCase , 'wb' ) as handle: pickle.dump(UpperCAmelCase , UpperCAmelCase ) with open(UpperCAmelCase , 'rb' ) as handle: lowerCamelCase__ : Optional[int] = pickle.load(UpperCAmelCase ) lowerCamelCase__ : Optional[int] = tokenizer_new.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) @require_sudachi def A_ ( self : Dict ) -> int: lowerCamelCase__ : Any = SudachiTokenizer(sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '] , ) @require_sudachi def A_ ( self : Dict ) -> Optional[Any]: lowerCamelCase__ : Optional[Any] = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='A' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国', '人', '参政', '権'] ) @require_sudachi def A_ ( self : Any ) -> int: lowerCamelCase__ : Union[str, Any] = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='B' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国人', '参政権'] ) @require_sudachi def A_ ( self : Any ) -> Union[str, Any]: lowerCamelCase__ : Dict = SudachiTokenizer(sudachi_dict_type='core' , sudachi_split_mode='C' ) self.assertListEqual(tokenizer.tokenize('外国人参政権' ) , ['外国人参政権'] ) @require_sudachi def A_ ( self : str ) -> Optional[int]: lowerCamelCase__ : List[str] = SudachiTokenizer(do_lower_case=UpperCAmelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iphone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '] , ) @require_sudachi def A_ ( self : Union[str, Any] ) -> Tuple: lowerCamelCase__ : int = SudachiTokenizer(normalize_text=UpperCAmelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , [' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', '\u3000', '。', ' ', ' '] , ) @require_sudachi def A_ ( self : List[Any] ) -> Tuple: lowerCamelCase__ : Union[str, Any] = SudachiTokenizer(trim_whitespace=UpperCAmelCase , sudachi_dict_type='core' ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'] , ) @require_jumanpp def A_ ( self : str ) -> List[str]: lowerCamelCase__ : Union[str, Any] = self.tokenizer_class(self.vocab_file , word_tokenizer_type='jumanpp' ) self.assertIsNotNone(UpperCAmelCase ) lowerCamelCase__ : Any = 'こんにちは、世界。\nこんばんは、世界。' lowerCamelCase__ : Any = tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , ['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) lowerCamelCase__ : Any = os.path.join(self.tmpdirname , 'tokenizer.bin' ) with open(UpperCAmelCase , 'wb' ) as handle: pickle.dump(UpperCAmelCase , UpperCAmelCase ) with open(UpperCAmelCase , 'rb' ) as handle: lowerCamelCase__ : Optional[int] = pickle.load(UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = tokenizer_new.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) @require_jumanpp def A_ ( self : Tuple ) -> Tuple: lowerCamelCase__ : List[str] = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def A_ ( self : str ) -> Optional[Any]: lowerCamelCase__ : List[str] = JumanppTokenizer(do_lower_case=UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iphone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def A_ ( self : Tuple ) -> List[str]: lowerCamelCase__ : int = JumanppTokenizer(normalize_text=UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['ア', 'ッ', 'フ', '゚', 'ル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'] , ) @require_jumanpp def A_ ( self : str ) -> Dict: lowerCamelCase__ : Optional[Any] = JumanppTokenizer(trim_whitespace=UpperCAmelCase ) self.assertListEqual( tokenizer.tokenize(' \tアップルストアでiPhone8 が \n 発売された 。 ' ) , ['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '。'] , ) @require_jumanpp def A_ ( self : Any ) -> Any: lowerCamelCase__ : Union[str, Any] = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize('ありがとうございますm(_ _)m見つけるのが大変です。' ) , ['ありがとう', 'ございます', 'm(_ _)m', '見つける', 'の', 'が', '大変です', '。'] , ) def A_ ( self : List[Any] ) -> int: lowerCamelCase__ : Optional[int] = ['[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは'] lowerCamelCase__ : int = {} for i, token in enumerate(UpperCAmelCase ): lowerCamelCase__ : List[Any] = i lowerCamelCase__ : int = WordpieceTokenizer(vocab=UpperCAmelCase , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('こんにちは' ) , ['こんにちは'] ) self.assertListEqual(tokenizer.tokenize('こんばんは' ) , ['こん', '##ばんは'] ) self.assertListEqual(tokenizer.tokenize('こんばんは こんばんにちは こんにちは' ) , ['こん', '##ばんは', '[UNK]', 'こんにちは'] ) def A_ ( self : Tuple ) -> str: lowerCamelCase__ : int = BertJapaneseTokenizer.from_pretrained('nlp-waseda/roberta-base-japanese-with-auto-jumanpp' ) lowerCamelCase__ : List[str] = tokenizer.subword_tokenizer lowerCamelCase__ : Any = subword_tokenizer.tokenize('国境 の 長い トンネル を 抜ける と 雪国 であった 。' ) self.assertListEqual(UpperCAmelCase , ['▁国境', '▁の', '▁長い', '▁トンネル', '▁を', '▁抜ける', '▁と', '▁雪', '国', '▁であった', '▁。'] ) lowerCamelCase__ : Optional[int] = subword_tokenizer.tokenize('こんばんは こんばん にち は こんにちは' ) self.assertListEqual(UpperCAmelCase , ['▁こん', 'ばん', 'は', '▁こん', 'ばん', '▁に', 'ち', '▁は', '▁こんにちは'] ) def A_ ( self : Dict ) -> List[Any]: lowerCamelCase__ : Union[str, Any] = self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese' ) lowerCamelCase__ : int = tokenizer.encode('ありがとう。' , add_special_tokens=UpperCAmelCase ) lowerCamelCase__ : List[str] = tokenizer.encode('どういたしまして。' , add_special_tokens=UpperCAmelCase ) lowerCamelCase__ : Optional[int] = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase ) lowerCamelCase__ : List[str] = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase , UpperCAmelCase ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): UpperCAmelCase__ = BertJapaneseTokenizer UpperCAmelCase__ = False def A_ ( self : Dict ) -> Any: super().setUp() lowerCamelCase__ : List[Any] = ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。'] lowerCamelCase__ : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def A_ ( self : List[Any] , **UpperCAmelCase : str ) -> List[str]: return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type='character' , **UpperCAmelCase ) def A_ ( self : List[str] , UpperCAmelCase : Union[str, Any] ) -> List[Any]: lowerCamelCase__ : str = 'こんにちは、世界。 \nこんばんは、世界。' lowerCamelCase__ : List[str] = 'こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。' return input_text, output_text def A_ ( self : Optional[Any] ) -> List[Any]: pass # TODO add if relevant def A_ ( self : Tuple ) -> Union[str, Any]: pass # TODO add if relevant def A_ ( self : Optional[Any] ) -> Optional[int]: pass # TODO add if relevant def A_ ( self : Tuple ) -> Tuple: lowerCamelCase__ : Optional[int] = self.tokenizer_class(self.vocab_file , subword_tokenizer_type='character' ) lowerCamelCase__ : List[str] = tokenizer.tokenize('こんにちは、世界。 \nこんばんは、世界。' ) self.assertListEqual( UpperCAmelCase , ['こ', 'ん', 'に', 'ち', 'は', '、', '世', '界', '。', 'こ', 'ん', 'ば', 'ん', 'は', '、', '世', '界', '。'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12] ) def A_ ( self : Dict ) -> Any: lowerCamelCase__ : Any = ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。'] lowerCamelCase__ : Optional[int] = {} for i, token in enumerate(UpperCAmelCase ): lowerCamelCase__ : Union[str, Any] = i lowerCamelCase__ : Optional[int] = CharacterTokenizer(vocab=UpperCAmelCase , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('こんにちは' ) , ['こ', 'ん', 'に', 'ち', 'は'] ) self.assertListEqual(tokenizer.tokenize('こんにちほ' ) , ['こ', 'ん', 'に', 'ち', '[UNK]'] ) def A_ ( self : Any ) -> str: lowerCamelCase__ : Dict = self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese-char' ) lowerCamelCase__ : List[Any] = tokenizer.encode('ありがとう。' , add_special_tokens=UpperCAmelCase ) lowerCamelCase__ : Union[str, Any] = tokenizer.encode('どういたしまして。' , add_special_tokens=UpperCAmelCase ) lowerCamelCase__ : Tuple = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase ) lowerCamelCase__ : Optional[int] = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase , UpperCAmelCase ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class lowerCAmelCase ( unittest.TestCase ): def A_ ( self : List[str] ) -> Dict: lowerCamelCase__ : Union[str, Any] = 'cl-tohoku/bert-base-japanese' lowerCamelCase__ : int = AutoTokenizer.from_pretrained(UpperCAmelCase ) self.assertIsInstance(UpperCAmelCase , UpperCAmelCase ) class lowerCAmelCase ( unittest.TestCase ): def A_ ( self : Tuple ) -> Optional[int]: lowerCamelCase__ : Union[str, Any] = 'cl-tohoku/bert-base-japanese' with self.assertLogs('transformers' , level='WARNING' ) as cm: BertTokenizer.from_pretrained(UpperCAmelCase ) self.assertTrue( cm.records[0].message.startswith( 'The tokenizer class you load from this checkpoint is not the same type as the class this function' ' is called from.' ) ) lowerCamelCase__ : Optional[Any] = 'bert-base-cased' with self.assertLogs('transformers' , level='WARNING' ) as cm: BertJapaneseTokenizer.from_pretrained(UpperCAmelCase ) self.assertTrue( cm.records[0].message.startswith( 'The tokenizer class you load from this checkpoint is not the same type as the class this function' ' is called from.' ) )
50
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class a__( unittest.TestCase ): '''simple docstring''' UpperCAmelCase_ : Dict = ViTImageProcessor if is_vision_available() else None @property def a_ ( self): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a_ ( self): """simple docstring""" lowerCAmelCase = (3, 32, 128) lowerCAmelCase = tempfile.mkdtemp() # fmt: off lowerCAmelCase = ["""[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 lowerCAmelCase = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase)))) lowerCAmelCase = 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(__lowerCAmelCase) + """\n""") lowerCAmelCase = { """do_normalize""": False, """do_resize""": True, """image_processor_type""": """ViTImageProcessor""", """resample""": 3, """size""": {"""height""": 32, """width""": 128}, } lowerCAmelCase = os.path.join(self.tmpdirname , __lowerCAmelCase) with open(self.image_processor_file , """w""" , encoding="""utf-8""") as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase) def a_ ( self , **__lowerCAmelCase): """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase) def a_ ( self , **__lowerCAmelCase): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase) def a_ ( self): """simple docstring""" shutil.rmtree(self.tmpdirname) def a_ ( self): """simple docstring""" lowerCAmelCase = np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta) lowerCAmelCase = Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1)) return image_input def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_image_processor() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) processor.save_pretrained(self.tmpdirname) lowerCAmelCase = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab()) self.assertIsInstance(processor.char_tokenizer , __lowerCAmelCase) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string()) self.assertIsInstance(processor.image_processor , __lowerCAmelCase) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = self.get_image_processor() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) processor.save_pretrained(self.tmpdirname) lowerCAmelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""") lowerCAmelCase = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0) lowerCAmelCase = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.char_tokenizer , __lowerCAmelCase) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor , __lowerCAmelCase) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = self.prepare_image_inputs() lowerCAmelCase = image_processor(__lowerCAmelCase , return_tensors="""np""") lowerCAmelCase = processor(images=__lowerCAmelCase , return_tensors="""np""") for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = """test""" lowerCAmelCase = processor(text=__lowerCAmelCase) lowerCAmelCase = tokenizer(__lowerCAmelCase) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key]) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = """test""" lowerCAmelCase = self.prepare_image_inputs() lowerCAmelCase = processor(text=__lowerCAmelCase , images=__lowerCAmelCase) self.assertListEqual(list(inputs.keys()) , ["""pixel_values""", """labels"""]) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase): processor() def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] lowerCAmelCase = processor.char_decode(__lowerCAmelCase) lowerCAmelCase = tokenizer.batch_decode(__lowerCAmelCase) lowerCAmelCase = [seq.replace(""" """ , """""") for seq in decoded_tok] self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = None lowerCAmelCase = self.prepare_image_inputs() lowerCAmelCase = processor(text=__lowerCAmelCase , images=__lowerCAmelCase) self.assertListEqual(list(inputs.keys()) , processor.model_input_names) def a_ ( self): """simple docstring""" lowerCAmelCase = self.get_image_processor() lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = MgpstrProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase) lowerCAmelCase = torch.randn(1 , 27 , 38) lowerCAmelCase = torch.randn(1 , 27 , 50257) lowerCAmelCase = torch.randn(1 , 27 , 30522) lowerCAmelCase = processor.batch_decode([char_input, bpe_input, wp_input]) self.assertListEqual(list(results.keys()) , ["""generated_text""", """scores""", """char_preds""", """bpe_preds""", """wp_preds"""])
272
0
import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging lowercase : List[str] = logging.get_logger(__name__) def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple , _lowerCamelCase : List[Any]) -> Any: '''simple docstring''' __UpperCamelCase : str = set() __UpperCamelCase : Optional[Any] = [] def parse_line(_lowerCamelCase : Tuple): for line in fp: if isinstance(_lowerCamelCase , _lowerCamelCase): __UpperCamelCase : Tuple = line.decode("UTF-8") if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(" "): # process a single warning and move it to `selected_warnings`. if len(_lowerCamelCase) > 0: __UpperCamelCase : Optional[Any] = "\n".join(_lowerCamelCase) # Only keep the warnings specified in `targets` if any(F': {x}: ' in warning for x in targets): selected_warnings.add(_lowerCamelCase) buffer.clear() continue else: __UpperCamelCase : Optional[Any] = line.strip() buffer.append(_lowerCamelCase) if from_gh: for filename in os.listdir(_lowerCamelCase): __UpperCamelCase : Any = os.path.join(_lowerCamelCase , _lowerCamelCase) if not os.path.isdir(_lowerCamelCase): # read the file if filename != "warnings.txt": continue with open(_lowerCamelCase) as fp: parse_line(_lowerCamelCase) else: try: with zipfile.ZipFile(_lowerCamelCase) as z: for filename in z.namelist(): if not os.path.isdir(_lowerCamelCase): # read the file if filename != "warnings.txt": continue with z.open(_lowerCamelCase) as fp: parse_line(_lowerCamelCase) except Exception: logger.warning( F'{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.') return selected_warnings def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any , _lowerCamelCase : Optional[int]) -> Dict: '''simple docstring''' __UpperCamelCase : Union[str, Any] = set() __UpperCamelCase : str = [os.path.join(_lowerCamelCase , _lowerCamelCase) for p in os.listdir(_lowerCamelCase) if (p.endswith(".zip") or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(_lowerCamelCase , _lowerCamelCase)) return selected_warnings if __name__ == "__main__": def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple) -> str: '''simple docstring''' return values.split(",") lowercase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') parser.add_argument( '--output_dir', type=str, required=True, help='Where to store the downloaded artifacts and other result files.', ) parser.add_argument('--token', default=None, type=str, help='A token that has actions:read permission.') # optional parameters parser.add_argument( '--targets', default='DeprecationWarning,UserWarning,FutureWarning', type=list_str, help='Comma-separated list of target warning(s) which we want to extract.', ) parser.add_argument( '--from_gh', action='store_true', help='If running from a GitHub action workflow and collecting warnings from its artifacts.', ) lowercase : Union[str, Any] = parser.parse_args() lowercase : Tuple = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links lowercase : int = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, 'artifacts.json'), 'w', encoding='UTF-8') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print('=' * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts lowercase : Any = extract_warnings(args.output_dir, args.targets) lowercase : int = sorted(selected_warnings) with open(os.path.join(args.output_dir, 'selected_warnings.json'), 'w', encoding='UTF-8') as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
151
import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness lowercase : Any = '\\n@misc{chen2021evaluating,\n title={Evaluating Large Language Models Trained on Code},\n author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \\nand Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \\nand Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \\nand Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \\nand Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \\nand Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \\nand Mohammad Bavarian and Clemens Winter and Philippe Tillet \\nand Felipe Petroski Such and Dave Cummings and Matthias Plappert \\nand Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \\nand William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \\nand Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \\nand William Saunders and Christopher Hesse and Andrew N. Carr \\nand Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \\nand Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \\nand Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \\nand Sam McCandlish and Ilya Sutskever and Wojciech Zaremba},\n year={2021},\n eprint={2107.03374},\n archivePrefix={arXiv},\n primaryClass={cs.LG}\n}\n' lowercase : str = '\\nThis metric implements the evaluation harness for the HumanEval problem solving dataset\ndescribed in the paper "Evaluating Large Language Models Trained on Code"\n(https://arxiv.org/abs/2107.03374).\n' lowercase : str = '\nCalculates how good are predictions given some references, using certain scores\nArgs:\n predictions: list of candidates to evaluate. Each candidates should be a list\n of strings with several code candidates to solve the problem.\n references: a list with a test for each prediction. Each test should evaluate the\n correctness of a code candidate.\n k: number of code candidates to consider in the evaluation (Default: [1, 10, 100])\n num_workers: number of workers used to evaluate the canidate programs (Default: 4).\n timeout:\nReturns:\n pass_at_k: dict with pass rates for each k\n results: dict with granular results of each unittest\nExamples:\n >>> code_eval = datasets.load_metric("code_eval")\n >>> test_cases = ["assert add(2,3)==5"]\n >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]]\n >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2])\n >>> print(pass_at_k)\n {\'pass@1\': 0.5, \'pass@2\': 1.0}\n' lowercase : List[str] = '\n################################################################################\n !!!WARNING!!!\n################################################################################\nThe "code_eval" metric executes untrusted model-generated code in Python.\nAlthough it is highly unlikely that model-generated code will do something\novertly malicious in response to this test suite, model-generated code may act\ndestructively due to a lack of model capability or alignment.\nUsers are strongly encouraged to sandbox this evaluation suite so that it\ndoes not perform destructive actions on their host or network. For more\ninformation on how OpenAI sandboxes its code, see the paper "Evaluating Large\nLanguage Models Trained on Code" (https://arxiv.org/abs/2107.03374).\n\nOnce you have read this disclaimer and taken appropriate precautions,\nset the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this\nwith:\n\n>>> import os\n>>> os.environ["HF_ALLOW_CODE_EVAL"] = "1"\n\n################################################################################\\n' lowercase : List[Any] = 'The MIT License\n\nCopyright (c) OpenAI (https://openai.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): '''simple docstring''' def _lowerCamelCase ( self :List[Any] ) -> List[Any]: return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Value("string" ), } ) , homepage="https://github.com/openai/human-eval" , codebase_urls=["https://github.com/openai/human-eval"] , reference_urls=["https://github.com/openai/human-eval"] , license=_LICENSE , ) def _lowerCamelCase ( self :str , a :Tuple , a :str , a :Tuple=[1, 1_0, 1_0_0] , a :Optional[Any]=4 , a :Optional[int]=3.0 ) -> Dict: if os.getenv("HF_ALLOW_CODE_EVAL" , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError("This metric is currently not supported on Windows." ) with ThreadPoolExecutor(max_workers=a ) as executor: __UpperCamelCase : List[Any] = [] __UpperCamelCase : str = Counter() __UpperCamelCase : Tuple = 0 __UpperCamelCase : Dict = defaultdict(a ) for task_id, (candidates, test_case) in enumerate(zip(a , a ) ): for candidate in candidates: __UpperCamelCase : List[str] = candidate + "\n" + test_case __UpperCamelCase : Tuple = (test_program, timeout, task_id, completion_id[task_id]) __UpperCamelCase : str = executor.submit(a , *a ) futures.append(a ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(a ): __UpperCamelCase : int = future.result() results[result["task_id"]].append((result["completion_id"], result) ) __UpperCamelCase , __UpperCamelCase : Tuple = [], [] for result in results.values(): result.sort() __UpperCamelCase : List[Any] = [r[1]["passed"] for r in result] total.append(len(a ) ) correct.append(sum(a ) ) __UpperCamelCase : Union[str, Any] = np.array(a ) __UpperCamelCase : Dict = np.array(a ) __UpperCamelCase : List[str] = k __UpperCamelCase : Optional[int] = {f'pass@{k}': estimate_pass_at_k(a , a , a ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : int , _lowerCamelCase : str , _lowerCamelCase : Union[str, Any]) -> Dict: '''simple docstring''' def estimator(_lowerCamelCase : int , _lowerCamelCase : int , _lowerCamelCase : int) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1)) if isinstance(_lowerCamelCase , _lowerCamelCase): __UpperCamelCase : List[Any] = itertools.repeat(_lowerCamelCase , len(_lowerCamelCase)) else: assert len(_lowerCamelCase) == len(_lowerCamelCase) __UpperCamelCase : Optional[int] = iter(_lowerCamelCase) return np.array([estimator(int(_lowerCamelCase) , int(_lowerCamelCase) , _lowerCamelCase) for n, c in zip(_lowerCamelCase , _lowerCamelCase)])
151
1
def _A ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): """simple docstring""" while a != 0: a__ , a__ : Optional[Any] =b % a, a return b def _A ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): """simple docstring""" if gcd(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) != 1: a__ : Optional[Any] =f'''mod inverse of {a!r} and {m!r} does not exist''' raise ValueError(SCREAMING_SNAKE_CASE ) a__ , a__ , a__ : Dict =1, 0, a a__ , a__ , a__ : List[Any] =0, 1, m while va != 0: a__ : str =ua // va a__ , a__ , a__ , a__ , a__ , a__ : Dict =(ua - q * va), (ua - q * va), (ua - q * va), va, va, va return ua % m
95
'''simple docstring''' import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class a_ : def __init__( self , snake_case_ , snake_case_=1_3 , snake_case_=3_0 , snake_case_=2 , snake_case_=3 , snake_case_=True , snake_case_=True , snake_case_=3_2 , snake_case_=5 , snake_case_=4 , snake_case_=3_7 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=1_0 , snake_case_=0.02 , snake_case_=None , ): _lowerCAmelCase : Optional[Any] = parent _lowerCAmelCase : Any = batch_size _lowerCAmelCase : Tuple = image_size _lowerCAmelCase : int = patch_size _lowerCAmelCase : Any = num_channels _lowerCAmelCase : str = is_training _lowerCAmelCase : Any = use_labels _lowerCAmelCase : List[Any] = hidden_size _lowerCAmelCase : int = num_hidden_layers _lowerCAmelCase : Dict = num_attention_heads _lowerCAmelCase : Union[str, Any] = intermediate_size _lowerCAmelCase : Dict = hidden_act _lowerCAmelCase : str = hidden_dropout_prob _lowerCAmelCase : Optional[int] = attention_probs_dropout_prob _lowerCAmelCase : Any = type_sequence_label_size _lowerCAmelCase : str = initializer_range _lowerCAmelCase : Optional[Any] = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _lowerCAmelCase : List[Any] = (image_size // patch_size) ** 2 _lowerCAmelCase : Dict = num_patches + 1 def __UpperCamelCase ( self ): _lowerCAmelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _lowerCAmelCase : List[str] = None if self.use_labels: _lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowerCAmelCase : Optional[Any] = self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self ): return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def __UpperCamelCase ( self , snake_case_ , snake_case_ , snake_case_ ): _lowerCAmelCase : List[Any] = ViTMSNModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() _lowerCAmelCase : Optional[Any] = model(snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCamelCase ( self , snake_case_ , snake_case_ , snake_case_ ): _lowerCAmelCase : Tuple = self.type_sequence_label_size _lowerCAmelCase : int = ViTMSNForImageClassification(snake_case_ ) model.to(snake_case_ ) model.eval() _lowerCAmelCase : Optional[int] = model(snake_case_ , labels=snake_case_ ) print("""Pixel and labels shape: {pixel_values.shape}, {labels.shape}""" ) print("""Labels: {labels}""" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images _lowerCAmelCase : int = 1 _lowerCAmelCase : List[str] = ViTMSNForImageClassification(snake_case_ ) model.to(snake_case_ ) model.eval() _lowerCAmelCase : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _lowerCAmelCase : Optional[int] = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __UpperCamelCase ( self ): _lowerCAmelCase : List[str] = self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : List[Any] = config_and_inputs _lowerCAmelCase : Any = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class a_ (_a , _a , unittest.TestCase ): __lowerCAmelCase : Tuple = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __lowerCAmelCase : Optional[int] = ( {"""feature-extraction""": ViTMSNModel, """image-classification""": ViTMSNForImageClassification} if is_torch_available() else {} ) __lowerCAmelCase : Dict = False __lowerCAmelCase : Optional[Any] = False __lowerCAmelCase : List[str] = False __lowerCAmelCase : Any = False def __UpperCamelCase ( self ): _lowerCAmelCase : Tuple = ViTMSNModelTester(self ) _lowerCAmelCase : int = ConfigTester(self , config_class=snake_case_ , has_text_modality=snake_case_ , hidden_size=3_7 ) def __UpperCamelCase ( self ): self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMSN does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): _lowerCAmelCase , _lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase : List[str] = model_class(snake_case_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _lowerCAmelCase : List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case_ , nn.Linear ) ) def __UpperCamelCase ( self ): _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase : Optional[int] = model_class(snake_case_ ) _lowerCAmelCase : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCAmelCase : Optional[Any] = [*signature.parameters.keys()] _lowerCAmelCase : List[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , snake_case_ ) def __UpperCamelCase ( self ): _lowerCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def __UpperCamelCase ( self ): _lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case_ ) @slow def __UpperCamelCase ( self ): for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase : Optional[int] = ViTMSNModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) def _UpperCAmelCase ( ) -> Tuple: _lowerCAmelCase : List[str] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class a_ (unittest.TestCase ): @cached_property def __UpperCamelCase ( self ): return ViTImageProcessor.from_pretrained("""facebook/vit-msn-small""" ) if is_vision_available() else None @slow def __UpperCamelCase ( self ): torch.manual_seed(2 ) _lowerCAmelCase : Dict = ViTMSNForImageClassification.from_pretrained("""facebook/vit-msn-small""" ).to(snake_case_ ) _lowerCAmelCase : Dict = self.default_image_processor _lowerCAmelCase : Any = prepare_img() _lowerCAmelCase : List[str] = image_processor(images=snake_case_ , return_tensors="""pt""" ).to(snake_case_ ) # forward pass with torch.no_grad(): _lowerCAmelCase : Dict = model(**snake_case_ ) # verify the logits _lowerCAmelCase : Dict = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , snake_case_ ) _lowerCAmelCase : Tuple = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(snake_case_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case_ , atol=1E-4 ) )
309
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCAmelCase : Union[str, Any] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : int = TFAutoModel.from_pretrained(snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Union[str, Any] = AutoModel.from_pretrained(snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCAmelCase : Optional[int] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : List[Any] = TFAutoModelForPreTraining.from_pretrained(snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : str = AutoModelForPreTraining.from_pretrained(snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : str = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Union[str, Any] = TFAutoModelForCausalLM.from_pretrained(snake_case__ , from_pt=snake_case__ ) lowerCAmelCase , lowerCAmelCase : str = TFAutoModelForCausalLM.from_pretrained( snake_case__ , output_loading_info=snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Dict = AutoModelForCausalLM.from_pretrained(snake_case__ , from_tf=snake_case__ ) lowerCAmelCase , lowerCAmelCase : str = AutoModelForCausalLM.from_pretrained( snake_case__ , output_loading_info=snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : Union[str, Any] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : List[Any] = TFAutoModelWithLMHead.from_pretrained(snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Union[str, Any] = AutoModelWithLMHead.from_pretrained(snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : Any = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Dict = TFAutoModelForMaskedLM.from_pretrained(snake_case__ , from_pt=snake_case__ ) lowerCAmelCase , lowerCAmelCase : List[Any] = TFAutoModelForMaskedLM.from_pretrained( snake_case__ , output_loading_info=snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Optional[int] = AutoModelForMaskedLM.from_pretrained(snake_case__ , from_tf=snake_case__ ) lowerCAmelCase , lowerCAmelCase : Union[str, Any] = AutoModelForMaskedLM.from_pretrained( snake_case__ , output_loading_info=snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase : Union[str, Any] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(snake_case__ , from_pt=snake_case__ ) lowerCAmelCase , lowerCAmelCase : Union[str, Any] = TFAutoModelForSeqaSeqLM.from_pretrained( snake_case__ , output_loading_info=snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Any = AutoModelForSeqaSeqLM.from_pretrained(snake_case__ , from_tf=snake_case__ ) lowerCAmelCase , lowerCAmelCase : Any = AutoModelForSeqaSeqLM.from_pretrained( snake_case__ , output_loading_info=snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCAmelCase : Optional[int] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : List[Any] = TFAutoModelForSequenceClassification.from_pretrained(snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Any = AutoModelForSequenceClassification.from_pretrained(snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) @slow def lowercase__ ( self ): """simple docstring""" for model_name in ["bert-base-uncased"]: lowerCAmelCase : Optional[int] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : Optional[int] = TFAutoModelForQuestionAnswering.from_pretrained(snake_case__ , from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) lowerCAmelCase : List[Any] = AutoModelForQuestionAnswering.from_pretrained(snake_case__ , from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = TFAutoModelWithLMHead.from_pretrained(snake_case__ , from_pt=snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) self.assertEqual(model.num_parameters() , 14_410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) , 14_410 ) lowerCAmelCase : str = AutoModelWithLMHead.from_pretrained(snake_case__ , from_tf=snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) self.assertEqual(model.num_parameters() , 14_410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) , 14_410 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Any = TFAutoModelWithLMHead.from_pretrained(snake_case__ , from_pt=snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) self.assertEqual(model.num_parameters() , 14_410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) , 14_410 ) lowerCAmelCase : Dict = AutoModelWithLMHead.from_pretrained(snake_case__ , from_tf=snake_case__ ) self.assertIsInstance(snake_case__ , snake_case__ ) self.assertEqual(model.num_parameters() , 14_410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) , 14_410 )
133
"""simple docstring""" import baseaa def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' return baseaa.baaencode(string.encode("utf-8" ) ) def a__ ( SCREAMING_SNAKE_CASE : bytes ): '''simple docstring''' return baseaa.baadecode(SCREAMING_SNAKE_CASE ).decode("utf-8" ) if __name__ == "__main__": lowerCAmelCase__ = '''Hello World!''' lowerCAmelCase__ = baseaa_encode(test) print(encoded) lowerCAmelCase__ = baseaa_decode(encoded) print(decoded)
133
1
from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case_ = logging.get_logger(__name__) snake_case_ = { "google/realm-cc-news-pretrained-embedder": ( "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json" ), "google/realm-cc-news-pretrained-encoder": ( "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json" ), "google/realm-cc-news-pretrained-scorer": ( "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json" ), "google/realm-cc-news-pretrained-openqa": ( "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json" ), "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json", "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json", "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json", "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json", # See all REALM models at https://huggingface.co/models?filter=realm } class SCREAMING_SNAKE_CASE__ (lowercase__ ): __lowerCamelCase : List[Any] = "realm" def __init__( self , a=3_0522 , a=768 , a=128 , a=12 , a=12 , a=8 , a=3072 , a="gelu_new" , a=0.1 , a=0.1 , a=512 , a=2 , a=0.02 , a=1e-12 , a=256 , a=10 , a=1e-3 , a=5 , a=320 , a=1335_3718 , a=5000 , a=1 , a=0 , a=2 , **a , ): super().__init__(pad_token_id=a , bos_token_id=a , eos_token_id=a , **a) # Common config lowercase__ : str = vocab_size lowercase__ : Dict = max_position_embeddings lowercase__ : Union[str, Any] = hidden_size lowercase__ : List[Any] = retriever_proj_size lowercase__ : str = num_hidden_layers lowercase__ : List[str] = num_attention_heads lowercase__ : Optional[Any] = num_candidates lowercase__ : List[Any] = intermediate_size lowercase__ : Union[str, Any] = hidden_act lowercase__ : Tuple = hidden_dropout_prob lowercase__ : Any = attention_probs_dropout_prob lowercase__ : Dict = initializer_range lowercase__ : List[Any] = type_vocab_size lowercase__ : Any = layer_norm_eps # Reader config lowercase__ : Any = span_hidden_size lowercase__ : List[Any] = max_span_width lowercase__ : Any = reader_layer_norm_eps lowercase__ : Union[str, Any] = reader_beam_size lowercase__ : List[Any] = reader_seq_len # Retrieval config lowercase__ : Union[str, Any] = num_block_records lowercase__ : Any = searcher_beam_size
214
'''simple docstring''' import argparse from collections import defaultdict import yaml a : str = "docs/source/en/_toctree.yml" def lowercase ( __magic_name__ ): '''simple docstring''' UpperCAmelCase : Dict = defaultdict(__magic_name__ ) for doc in model_doc: counts[doc["local"]] += 1 UpperCAmelCase : List[Any] = [key for key, value in counts.items() if value > 1] UpperCAmelCase : Dict = [] for duplicate_key in duplicates: UpperCAmelCase : Union[str, Any] = list({doc["title"] for doc in model_doc if doc["local"] == duplicate_key} ) if len(__magic_name__ ) > 1: raise ValueError( F"{duplicate_key} is present several times in the documentation table of content at " "`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the " "others." ) # Only add this once new_doc.append({"local": duplicate_key, "title": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc["local"]] == 1] ) # Sort return sorted(__magic_name__ , key=lambda __magic_name__ : s["title"].lower() ) def lowercase ( __magic_name__=False ): '''simple docstring''' with open(__magic_name__ , encoding="utf-8" ) as f: UpperCAmelCase : Any = yaml.safe_load(f.read() ) # Get to the API doc UpperCAmelCase : Optional[int] = 0 while content[api_idx]["title"] != "API": api_idx += 1 UpperCAmelCase : Union[str, Any] = content[api_idx]["sections"] # Then to the model doc UpperCAmelCase : Any = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 UpperCAmelCase : str = api_doc[model_idx]["sections"] UpperCAmelCase : Any = [(idx, section) for idx, section in enumerate(__magic_name__ ) if "sections" in section] UpperCAmelCase : Optional[int] = False for idx, modality_doc in modalities_docs: UpperCAmelCase : int = modality_doc["sections"] UpperCAmelCase : int = clean_model_doc_toc(__magic_name__ ) if old_modality_doc != new_modality_doc: UpperCAmelCase : int = True if overwrite: UpperCAmelCase : Dict = new_modality_doc if diff: if overwrite: UpperCAmelCase : Any = model_doc UpperCAmelCase : Any = api_doc with open(__magic_name__ , "w" , encoding="utf-8" ) as f: f.write(yaml.dump(__magic_name__ , allow_unicode=__magic_name__ ) ) else: raise ValueError( "The model doc part of the table of content is not properly sorted, run `make style` to fix this." ) if __name__ == "__main__": a : Optional[Any] = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") a : Optional[Any] = parser.parse_args() check_model_doc(args.fix_and_overwrite)
311
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_realm import RealmTokenizer a_ = logging.get_logger(__name__) a_ = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} a_ = { 'vocab_file': { 'google/realm-cc-news-pretrained-embedder': ( 'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt' ), 'google/realm-cc-news-pretrained-encoder': ( 'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt' ), 'google/realm-cc-news-pretrained-scorer': ( 'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt' ), 'google/realm-cc-news-pretrained-openqa': ( 'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt' ), 'google/realm-orqa-nq-openqa': 'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt', 'google/realm-orqa-nq-reader': 'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt', 'google/realm-orqa-wq-openqa': 'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt', 'google/realm-orqa-wq-reader': 'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt', }, 'tokenizer_file': { 'google/realm-cc-news-pretrained-embedder': ( 'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont' ), 'google/realm-cc-news-pretrained-encoder': ( 'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json' ), 'google/realm-cc-news-pretrained-scorer': ( 'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json' ), 'google/realm-cc-news-pretrained-openqa': ( 'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json' ), 'google/realm-orqa-nq-openqa': ( 'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json' ), 'google/realm-orqa-nq-reader': ( 'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json' ), 'google/realm-orqa-wq-openqa': ( 'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json' ), 'google/realm-orqa-wq-reader': ( 'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json' ), }, } a_ = { 'google/realm-cc-news-pretrained-embedder': 512, 'google/realm-cc-news-pretrained-encoder': 512, 'google/realm-cc-news-pretrained-scorer': 512, 'google/realm-cc-news-pretrained-openqa': 512, 'google/realm-orqa-nq-openqa': 512, 'google/realm-orqa-nq-reader': 512, 'google/realm-orqa-wq-openqa': 512, 'google/realm-orqa-wq-reader': 512, } a_ = { 'google/realm-cc-news-pretrained-embedder': {'do_lower_case': True}, 'google/realm-cc-news-pretrained-encoder': {'do_lower_case': True}, 'google/realm-cc-news-pretrained-scorer': {'do_lower_case': True}, 'google/realm-cc-news-pretrained-openqa': {'do_lower_case': True}, 'google/realm-orqa-nq-openqa': {'do_lower_case': True}, 'google/realm-orqa-nq-reader': {'do_lower_case': True}, 'google/realm-orqa-wq-openqa': {'do_lower_case': True}, 'google/realm-orqa-wq-reader': {'do_lower_case': True}, } class _lowercase ( snake_case_ ): lowercase = VOCAB_FILES_NAMES lowercase = PRETRAINED_VOCAB_FILES_MAP lowercase = PRETRAINED_INIT_CONFIGURATION lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase = RealmTokenizer def __init__( self : str , snake_case : Tuple=None , snake_case : Tuple=None , snake_case : Any=True , snake_case : Union[str, Any]="[UNK]" , snake_case : Any="[SEP]" , snake_case : int="[PAD]" , snake_case : Optional[Any]="[CLS]" , snake_case : Any="[MASK]" , snake_case : Dict=True , snake_case : int=None , **snake_case : Any , ) -> List[Any]: """simple docstring""" super().__init__( snake_case , tokenizer_file=snake_case , do_lower_case=snake_case , unk_token=snake_case , sep_token=snake_case , pad_token=snake_case , cls_token=snake_case , mask_token=snake_case , tokenize_chinese_chars=snake_case , strip_accents=snake_case , **snake_case , ) UpperCamelCase_ : Optional[int] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , snake_case ) != do_lower_case or normalizer_state.get('strip_accents' , snake_case ) != strip_accents or normalizer_state.get('handle_chinese_chars' , snake_case ) != tokenize_chinese_chars ): UpperCamelCase_ : Any = getattr(snake_case , normalizer_state.pop('type' ) ) UpperCamelCase_ : Tuple = do_lower_case UpperCamelCase_ : Optional[Any] = strip_accents UpperCamelCase_ : List[Any] = tokenize_chinese_chars UpperCamelCase_ : List[Any] = normalizer_class(**snake_case ) UpperCamelCase_ : Optional[int] = do_lower_case def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : List[Any] , **snake_case : Tuple ) -> Any: """simple docstring""" UpperCamelCase_ : Any = PaddingStrategy.MAX_LENGTH UpperCamelCase_ : Optional[Any] = text UpperCamelCase_ : str = kwargs.pop('text_pair' , snake_case ) UpperCamelCase_ : List[str] = kwargs.pop('return_tensors' , snake_case ) UpperCamelCase_ : str = { 'input_ids': [], 'attention_mask': [], 'token_type_ids': [], } for idx, candidate_text in enumerate(snake_case ): if batch_text_pair is not None: UpperCamelCase_ : Optional[Any] = batch_text_pair[idx] else: UpperCamelCase_ : Optional[Any] = None UpperCamelCase_ : int = super().__call__(snake_case , snake_case , return_tensors=snake_case , **snake_case ) UpperCamelCase_ : Optional[int] = encoded_candidates.get('input_ids' ) UpperCamelCase_ : Union[str, Any] = encoded_candidates.get('attention_mask' ) UpperCamelCase_ : Tuple = encoded_candidates.get('token_type_ids' ) if encoded_input_ids is not None: output_data["input_ids"].append(snake_case ) if encoded_attention_mask is not None: output_data["attention_mask"].append(snake_case ) if encoded_token_type_ids is not None: output_data["token_type_ids"].append(snake_case ) UpperCamelCase_ : Any = {key: item for key, item in output_data.items() if len(snake_case ) != 0} return BatchEncoding(snake_case , tensor_type=snake_case ) def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Tuple , snake_case : Union[str, Any]=None ) -> str: """simple docstring""" UpperCamelCase_ : Tuple = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def SCREAMING_SNAKE_CASE__ ( self : List[Any] , snake_case : List[int] , snake_case : Optional[List[int]] = None ) -> List[int]: """simple docstring""" UpperCamelCase_ : int = [self.sep_token_id] UpperCamelCase_ : Optional[int] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE__ ( self : List[str] , snake_case : str , snake_case : Optional[str] = None ) -> Tuple[str]: """simple docstring""" UpperCamelCase_ : Any = self._tokenizer.model.save(snake_case , name=snake_case ) return tuple(snake_case )
50
import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() a_ = logging.get_logger(__name__) def __lowercase ( lowerCamelCase : Optional[int] ): UpperCamelCase_ : Dict = OrderedDict() for key, value in state_dict.items(): if key.startswith('module.encoder' ): UpperCamelCase_ : Tuple = key.replace('module.encoder' , 'glpn.encoder' ) if key.startswith('module.decoder' ): UpperCamelCase_ : List[Any] = key.replace('module.decoder' , 'decoder.stages' ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 UpperCamelCase_ : Optional[Any] = key[key.find('patch_embed' ) + len('patch_embed' )] UpperCamelCase_ : List[str] = key.replace(F"patch_embed{idx}" , F"patch_embeddings.{int(lowerCamelCase )-1}" ) if "norm" in key: UpperCamelCase_ : int = key.replace('norm' , 'layer_norm' ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 UpperCamelCase_ : int = key[key.find('glpn.encoder.layer_norm' ) + len('glpn.encoder.layer_norm' )] UpperCamelCase_ : int = key.replace(F"layer_norm{idx}" , F"layer_norm.{int(lowerCamelCase )-1}" ) if "layer_norm1" in key: UpperCamelCase_ : Optional[int] = key.replace('layer_norm1' , 'layer_norm_1' ) if "layer_norm2" in key: UpperCamelCase_ : str = key.replace('layer_norm2' , 'layer_norm_2' ) if "block" in key: # replace for example block1 by block.0 UpperCamelCase_ : Union[str, Any] = key[key.find('block' ) + len('block' )] UpperCamelCase_ : Dict = key.replace(F"block{idx}" , F"block.{int(lowerCamelCase )-1}" ) if "attn.q" in key: UpperCamelCase_ : Optional[Any] = key.replace('attn.q' , 'attention.self.query' ) if "attn.proj" in key: UpperCamelCase_ : Any = key.replace('attn.proj' , 'attention.output.dense' ) if "attn" in key: UpperCamelCase_ : Optional[int] = key.replace('attn' , 'attention.self' ) if "fc1" in key: UpperCamelCase_ : int = key.replace('fc1' , 'dense1' ) if "fc2" in key: UpperCamelCase_ : Optional[int] = key.replace('fc2' , 'dense2' ) if "linear_pred" in key: UpperCamelCase_ : Tuple = key.replace('linear_pred' , 'classifier' ) if "linear_fuse" in key: UpperCamelCase_ : Union[str, Any] = key.replace('linear_fuse.conv' , 'linear_fuse' ) UpperCamelCase_ : Tuple = key.replace('linear_fuse.bn' , 'batch_norm' ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 UpperCamelCase_ : Optional[Any] = key[key.find('linear_c' ) + len('linear_c' )] UpperCamelCase_ : List[str] = key.replace(F"linear_c{idx}" , F"linear_c.{int(lowerCamelCase )-1}" ) if "bot_conv" in key: UpperCamelCase_ : Union[str, Any] = key.replace('bot_conv' , '0.convolution' ) if "skip_conv1" in key: UpperCamelCase_ : Optional[int] = key.replace('skip_conv1' , '1.convolution' ) if "skip_conv2" in key: UpperCamelCase_ : List[Any] = key.replace('skip_conv2' , '2.convolution' ) if "fusion1" in key: UpperCamelCase_ : Tuple = key.replace('fusion1' , '1.fusion' ) if "fusion2" in key: UpperCamelCase_ : Any = key.replace('fusion2' , '2.fusion' ) if "fusion3" in key: UpperCamelCase_ : int = key.replace('fusion3' , '3.fusion' ) if "fusion" in key and "conv" in key: UpperCamelCase_ : Optional[Any] = key.replace('conv' , 'convolutional_layer' ) if key.startswith('module.last_layer_depth' ): UpperCamelCase_ : str = key.replace('module.last_layer_depth' , 'head.head' ) UpperCamelCase_ : Optional[Any] = value return new_state_dict def __lowercase ( lowerCamelCase : Optional[Any] , lowerCamelCase : List[str] ): # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) UpperCamelCase_ : Union[str, Any] = state_dict.pop(F"glpn.encoder.block.{i}.{j}.attention.self.kv.weight" ) UpperCamelCase_ : int = state_dict.pop(F"glpn.encoder.block.{i}.{j}.attention.self.kv.bias" ) # next, add keys and values (in that order) to the state dict UpperCamelCase_ : Union[str, Any] = kv_weight[ : config.hidden_sizes[i], : ] UpperCamelCase_ : int = kv_bias[: config.hidden_sizes[i]] UpperCamelCase_ : Union[str, Any] = kv_weight[ config.hidden_sizes[i] :, : ] UpperCamelCase_ : Optional[int] = kv_bias[config.hidden_sizes[i] :] def __lowercase ( ): UpperCamelCase_ : Optional[int] = 'http://images.cocodataset.org/val2017/000000039769.jpg' UpperCamelCase_ : Optional[Any] = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ) return image @torch.no_grad() def __lowercase ( lowerCamelCase : Tuple , lowerCamelCase : List[str] , lowerCamelCase : Dict=False , lowerCamelCase : Optional[int]=None ): UpperCamelCase_ : List[Any] = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) UpperCamelCase_ : Tuple = GLPNImageProcessor() # prepare image UpperCamelCase_ : List[Any] = prepare_img() UpperCamelCase_ : str = image_processor(images=lowerCamelCase , return_tensors='pt' ).pixel_values logger.info('Converting model...' ) # load original state dict UpperCamelCase_ : Any = torch.load(lowerCamelCase , map_location=torch.device('cpu' ) ) # rename keys UpperCamelCase_ : str = rename_keys(lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(lowerCamelCase , lowerCamelCase ) # create HuggingFace model and load state dict UpperCamelCase_ : Dict = GLPNForDepthEstimation(lowerCamelCase ) model.load_state_dict(lowerCamelCase ) model.eval() # forward pass UpperCamelCase_ : Optional[Any] = model(lowerCamelCase ) UpperCamelCase_ : str = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: UpperCamelCase_ : Tuple = torch.tensor( [[4.4_1_4_7, 4.0_8_7_3, 4.0_6_7_3], [3.7_8_9_0, 3.2_8_8_1, 3.1_5_2_5], [3.7_6_7_4, 3.5_4_2_3, 3.4_9_1_3]] ) elif "kitti" in model_name: UpperCamelCase_ : Any = torch.tensor( [[3.4_2_9_1, 2.7_8_6_5, 2.5_1_5_1], [3.2_8_4_1, 2.7_0_2_1, 2.3_5_0_2], [3.1_1_4_7, 2.4_6_2_5, 2.2_4_8_1]] ) else: raise ValueError(F"Unknown model name: {model_name}" ) UpperCamelCase_ : Tuple = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCamelCase , atol=1e-4 ) print('Looks ok!' ) # finally, push to hub if required if push_to_hub: logger.info('Pushing model and image processor to the hub...' ) model.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCamelCase , lowerCamelCase ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowerCamelCase , ) if __name__ == "__main__": a_ = argparse.ArgumentParser() parser.add_argument( '--checkpoint_path', default=None, 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 folder to output PyTorch model.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to upload the model to the HuggingFace hub.' ) parser.add_argument( '--model_name', default='glpn-kitti', type=str, help='Name of the model in case you\'re pushing to the hub.', ) a_ = parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
50
1
'''simple docstring''' import unittest from transformers import PegasusConfig, PegasusTokenizer, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html lowerCAmelCase_ : List[str] = 'platform' import jax import jax.numpy as jnp import numpy as np from transformers import FlaxPegasusForConditionalGeneration, FlaxPegasusModel @require_flax class __SCREAMING_SNAKE_CASE : """simple docstring""" __a =PegasusConfig __a ={} __a ='gelu' def __init__( self : Optional[Any] , __a : str , __a : List[str]=13 , __a : Union[str, Any]=7 , __a : List[str]=True , __a : Optional[int]=False , __a : Tuple=99 , __a : Dict=32 , __a : str=5 , __a : Any=4 , __a : Optional[int]=37 , __a : Optional[int]=0.1 , __a : Tuple=0.1 , __a : Optional[Any]=20 , __a : List[str]=2 , __a : Optional[Any]=1 , __a : Optional[int]=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_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = eos_token_id _a = pad_token_id _a = bos_token_id def UpperCamelCase__ ( self : Any ): _a = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ).clip(3 , self.vocab_size ) _a = np.expand_dims(np.array([self.eos_token_id] * self.batch_size ) , 1 ) _a = np.concatenate([input_ids, eos_tensor] , axis=1 ) _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) _a = prepare_pegasus_inputs_dict(__a , __a , __a ) return config, inputs_dict def UpperCamelCase__ ( self : str , __a : Tuple , __a : List[Any] , __a : Optional[int] ): _a = 20 _a = model_class_name(__a ) _a = model.encode(inputs_dict["input_ids"] ) _a , _a = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) _a = model.init_cache(decoder_input_ids.shape[0] , __a , __a ) _a = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype="i4" ) _a = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) _a = model.decode( decoder_input_ids[:, :-1] , __a , decoder_attention_mask=__a , past_key_values=__a , decoder_position_ids=__a , ) _a = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) _a = model.decode( decoder_input_ids[:, -1:] , __a , decoder_attention_mask=__a , past_key_values=outputs_cache.past_key_values , decoder_position_ids=__a , ) _a = model.decode(__a , __a ) _a = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'Max diff is {diff}' ) def UpperCamelCase__ ( self : Tuple , __a : Tuple , __a : int , __a : Dict ): _a = 20 _a = model_class_name(__a ) _a = model.encode(inputs_dict["input_ids"] ) _a , _a = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) _a = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) _a = model.init_cache(decoder_input_ids.shape[0] , __a , __a ) _a = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) _a = model.decode( decoder_input_ids[:, :-1] , __a , decoder_attention_mask=__a , past_key_values=__a , decoder_position_ids=__a , ) _a = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) _a = model.decode( decoder_input_ids[:, -1:] , __a , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=__a , decoder_position_ids=__a , ) _a = model.decode(__a , __a , decoder_attention_mask=__a ) _a = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=f'Max diff is {diff}' ) def _lowerCamelCase ( lowercase : Tuple , lowercase : Optional[int] , lowercase : int , lowercase : str=None , lowercase : Any=None , ) -> Union[str, Any]: if attention_mask is None: _a = np.not_equal(lowercase , config.pad_token_id ).astype(np.inta ) if decoder_attention_mask is None: _a = np.concatenate( [ np.ones(decoder_input_ids[:, :1].shape , dtype=np.inta ), np.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ).astype(np.inta ), ] , axis=-1 , ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, } @require_flax class __SCREAMING_SNAKE_CASE (lowerCamelCase_ , unittest.TestCase ): """simple docstring""" __a =( ( FlaxPegasusForConditionalGeneration, FlaxPegasusModel, ) if is_flax_available() else () ) __a =(FlaxPegasusForConditionalGeneration,) if is_flax_available() else () __a =True __a =False __a =False __a =False def UpperCamelCase__ ( self : Tuple ): _a = FlaxPegasusModelTester(self ) _a = ConfigTester(self , config_class=__a ) def UpperCamelCase__ ( self : Dict ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self : List[Any] ): _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(__a , __a , __a ) def UpperCamelCase__ ( self : str ): _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(__a , __a , __a ) def UpperCamelCase__ ( self : Any ): _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): _a = self._prepare_for_class(__a , __a ) _a = model_class(__a ) @jax.jit def encode_jitted(__a : Dict , __a : Dict=None , **__a : str ): return model.encode(input_ids=__a , attention_mask=__a ) with self.subTest("JIT Enabled" ): _a = encode_jitted(**__a ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): _a = encode_jitted(**__a ).to_tuple() self.assertEqual(len(__a ) , len(__a ) ) for jitted_output, output in zip(__a , __a ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCamelCase__ ( self : Optional[int] ): _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): _a = model_class(__a ) _a = model.encode(inputs_dict["input_ids"] , inputs_dict["attention_mask"] ) _a = { "decoder_input_ids": inputs_dict["decoder_input_ids"], "decoder_attention_mask": inputs_dict["decoder_attention_mask"], "encoder_outputs": encoder_outputs, } @jax.jit def decode_jitted(__a : Dict , __a : List[str] , __a : str ): return model.decode( decoder_input_ids=__a , decoder_attention_mask=__a , encoder_outputs=__a , ) with self.subTest("JIT Enabled" ): _a = decode_jitted(**__a ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): _a = decode_jitted(**__a ).to_tuple() self.assertEqual(len(__a ) , len(__a ) ) for jitted_output, output in zip(__a , __a ): self.assertEqual(jitted_output.shape , output.shape ) @slow def UpperCamelCase__ ( self : List[Any] ): for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained("google/pegasus-large" , from_pt=__a ) _a = np.ones((1, 1) ) _a = model(__a ) self.assertIsNotNone(__a ) @slow def UpperCamelCase__ ( self : str ): _a = FlaxPegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum" ) _a = PegasusTokenizer.from_pretrained("google/pegasus-xsum" ) _a = [ " PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.", " The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning 'Oh I think you're nominated'\", said Dappy.\"And I was like 'Oh yeah, which one?' And now we've got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it's best of us to come down and mingle with everyone and say hello to the cameras. And now we find we've got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn't be too disappointed if they didn't win this time around.\"At the end of the day we're grateful to be where we are in our careers.\"If it don't happen then it don't happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers' All These Things That I've Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year's Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" ", ] _a = [ "California's largest electricity provider has turned off power to hundreds of thousands of customers.", "Pop group N-Dubz have revealed they were surprised to get four nominations for this year's Mobo Awards.", ] _a = tokenizer(__a , return_tensors="np" , truncation=__a , max_length=5_12 , padding=__a ) _a = model.generate(**__a , num_beams=2 ).sequences _a = tokenizer.batch_decode(__a , skip_special_tokens=__a ) assert tgt_text == decoded
63
'''simple docstring''' def _lowerCamelCase ( lowercase : int = 100 ) -> int: _a = 0 _a = 0 for i in range(1 , n + 1 ): sum_of_squares += i**2 sum_of_ints += i return sum_of_ints**2 - sum_of_squares if __name__ == "__main__": print(f"""{solution() = }""")
63
1
"""simple docstring""" 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 UpperCamelCase__ : """simple docstring""" def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int=2 , SCREAMING_SNAKE_CASE_ : List[Any]=3_2 , SCREAMING_SNAKE_CASE_ : Dict=1_6 , SCREAMING_SNAKE_CASE_ : int=3 , SCREAMING_SNAKE_CASE_ : int=True , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=3_2 , SCREAMING_SNAKE_CASE_ : Dict=4 , SCREAMING_SNAKE_CASE_ : Any=[0, 1, 2, 3] , SCREAMING_SNAKE_CASE_ : Tuple=4 , SCREAMING_SNAKE_CASE_ : List[str]=3_7 , SCREAMING_SNAKE_CASE_ : Any="gelu" , SCREAMING_SNAKE_CASE_ : Any=0.1 , SCREAMING_SNAKE_CASE_ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.02 , SCREAMING_SNAKE_CASE_ : Any=3 , SCREAMING_SNAKE_CASE_ : Optional[Any]=[1, 3_8_4, 2_4, 2_4] , SCREAMING_SNAKE_CASE_ : List[str]=True , SCREAMING_SNAKE_CASE_ : str=None , ): lowerCAmelCase_ : Any = parent lowerCAmelCase_ : Union[str, Any] = batch_size lowerCAmelCase_ : Optional[int] = image_size lowerCAmelCase_ : Tuple = patch_size lowerCAmelCase_ : List[Any] = num_channels lowerCAmelCase_ : Any = is_training lowerCAmelCase_ : str = use_labels lowerCAmelCase_ : Any = hidden_size lowerCAmelCase_ : int = num_hidden_layers lowerCAmelCase_ : List[Any] = backbone_out_indices lowerCAmelCase_ : Any = num_attention_heads lowerCAmelCase_ : Optional[Any] = intermediate_size lowerCAmelCase_ : List[str] = hidden_act lowerCAmelCase_ : str = hidden_dropout_prob lowerCAmelCase_ : int = attention_probs_dropout_prob lowerCAmelCase_ : Dict = initializer_range lowerCAmelCase_ : List[Any] = num_labels lowerCAmelCase_ : Any = backbone_featmap_shape lowerCAmelCase_ : Optional[int] = scope lowerCAmelCase_ : Union[str, Any] = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) lowerCAmelCase_ : int = (image_size // patch_size) ** 2 lowerCAmelCase_ : Optional[int] = num_patches + 1 def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ): lowerCAmelCase_ : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase_ : int = None if self.use_labels: lowerCAmelCase_ : str = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) lowerCAmelCase_ : Union[str, Any] = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE__ ( self : Dict ): lowerCAmelCase_ : Dict = { 'global_padding': 'same', 'layer_type': 'bottleneck', 'depths': [3, 4, 9], 'out_features': ['stage1', 'stage2', 'stage3'], 'embedding_dynamic_padding': True, 'hidden_sizes': [9_6, 1_9_2, 3_8_4, 7_6_8], '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=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , is_hybrid=self.is_hybrid , backbone_config=SCREAMING_SNAKE_CASE_ , backbone_featmap_shape=self.backbone_featmap_shape , ) def SCREAMING_SNAKE_CASE__ ( self : Any , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple ): lowerCAmelCase_ : str = DPTModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : Optional[int] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self : Any , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict ): lowerCAmelCase_ : str = self.num_labels lowerCAmelCase_ : Any = DPTForDepthEstimation(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : List[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.predicted_depth.shape , (self.batch_size, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE__ ( self : int , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ): lowerCAmelCase_ : Tuple = self.num_labels lowerCAmelCase_ : Dict = DPTForSemanticSegmentation(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() lowerCAmelCase_ : int = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): lowerCAmelCase_ : List[str] = self.prepare_config_and_inputs() lowerCAmelCase_ ,lowerCAmelCase_ ,lowerCAmelCase_ : Any = config_and_inputs lowerCAmelCase_ : List[str] = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCamelCase__ ( lowercase_, lowercase_, unittest.TestCase ): """simple docstring""" _SCREAMING_SNAKE_CASE = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () _SCREAMING_SNAKE_CASE = ( { """depth-estimation""": DPTForDepthEstimation, """feature-extraction""": DPTModel, """image-segmentation""": DPTForSemanticSegmentation, } if is_torch_available() else {} ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False def SCREAMING_SNAKE_CASE__ ( self : Dict ): lowerCAmelCase_ : List[Any] = DPTModelTester(self ) lowerCAmelCase_ : Union[str, Any] = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , has_text_modality=SCREAMING_SNAKE_CASE_ , hidden_size=3_7 ) def SCREAMING_SNAKE_CASE__ ( self : int ): self.config_tester.run_common_tests() @unittest.skip(reason='DPT does not use inputs_embeds' ) def SCREAMING_SNAKE_CASE__ ( self : Any ): pass def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): lowerCAmelCase_ ,lowerCAmelCase_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : int = model_class(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCAmelCase_ : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ , nn.Linear ) ) def SCREAMING_SNAKE_CASE__ ( self : int ): lowerCAmelCase_ ,lowerCAmelCase_ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : List[Any] = model_class(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase_ : Optional[Any] = [*signature.parameters.keys()] lowerCAmelCase_ : Union[str, Any] = ['pixel_values'] self.assertListEqual(arg_names[:1] , SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : Dict ): lowerCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : str ): lowerCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ): lowerCAmelCase_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue lowerCAmelCase_ ,lowerCAmelCase_ : int = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase_ : Union[str, Any] = True if model_class in get_values(SCREAMING_SNAKE_CASE_ ): continue lowerCAmelCase_ : Dict = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.train() lowerCAmelCase_ : List[Any] = self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : int = model(**SCREAMING_SNAKE_CASE_ ).loss loss.backward() def SCREAMING_SNAKE_CASE__ ( self : int ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue lowerCAmelCase_ ,lowerCAmelCase_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase_ : Any = False lowerCAmelCase_ : str = True if model_class in get_values(SCREAMING_SNAKE_CASE_ ) or not model_class.supports_gradient_checkpointing: continue lowerCAmelCase_ : Union[str, Any] = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.gradient_checkpointing_enable() model.train() lowerCAmelCase_ : int = self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , return_labels=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Dict = model(**SCREAMING_SNAKE_CASE_ ).loss loss.backward() def SCREAMING_SNAKE_CASE__ ( self : Tuple ): lowerCAmelCase_ ,lowerCAmelCase_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase_ : List[Any] = _config_zero_init(SCREAMING_SNAKE_CASE_ ) for model_class in self.all_model_classes: lowerCAmelCase_ : str = model_class(config=SCREAMING_SNAKE_CASE_ ) # Skip the check for the backbone lowerCAmelCase_ : List[str] = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": lowerCAmelCase_ : Any = [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 SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): pass @slow def SCREAMING_SNAKE_CASE__ ( self : str ): for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: lowerCAmelCase_ : Any = DPTModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE__ ( self : Any ): # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type lowerCAmelCase_ ,lowerCAmelCase_ : str = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase_ : List[Any] = 'add' with self.assertRaises(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase_ : List[str] = DPTForDepthEstimation(SCREAMING_SNAKE_CASE_ ) def UpperCamelCase_ ( ) -> int: """simple docstring""" lowerCAmelCase_ : Union[str, Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision @slow class UpperCamelCase__ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE__ ( self : int ): lowerCAmelCase_ : Optional[Any] = DPTImageProcessor.from_pretrained('Intel/dpt-hybrid-midas' ) lowerCAmelCase_ : Union[str, Any] = DPTForDepthEstimation.from_pretrained('Intel/dpt-hybrid-midas' ).to(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Any = prepare_img() lowerCAmelCase_ : Union[str, Any] = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors='pt' ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): lowerCAmelCase_ : Union[str, Any] = model(**SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Dict = outputs.predicted_depth # verify the predicted depth lowerCAmelCase_ : Dict = torch.Size((1, 3_8_4, 3_8_4) ) self.assertEqual(predicted_depth.shape , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : List[Any] = torch.tensor( [[[5.64_37, 5.61_46, 5.65_11], [5.43_71, 5.56_49, 5.59_58], [5.52_15, 5.51_84, 5.52_93]]] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_0_0 , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) )
289
"""simple docstring""" import baseaa def UpperCamelCase_ ( lowerCAmelCase__ : str ) -> bytes: """simple docstring""" return baseaa.aaaencode(string.encode('utf-8' ) ) def UpperCamelCase_ ( lowerCAmelCase__ : bytes ) -> str: """simple docstring""" return baseaa.aaadecode(lowerCAmelCase__ ).decode('utf-8' ) if __name__ == "__main__": import doctest doctest.testmod()
289
1
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer lowercase__ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowercase__ = { "vocab_file": { "google/electra-small-generator": ( "https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt" ), "google/electra-base-generator": "https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt", "google/electra-large-generator": ( "https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt" ), "google/electra-small-discriminator": ( "https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt" ), "google/electra-base-discriminator": ( "https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt" ), "google/electra-large-discriminator": ( "https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt" ), }, "tokenizer_file": { "google/electra-small-generator": ( "https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json" ), "google/electra-base-generator": ( "https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json" ), "google/electra-large-generator": ( "https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json" ), "google/electra-small-discriminator": ( "https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json" ), "google/electra-base-discriminator": ( "https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json" ), "google/electra-large-discriminator": ( "https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json" ), }, } lowercase__ = { "google/electra-small-generator": 512, "google/electra-base-generator": 512, "google/electra-large-generator": 512, "google/electra-small-discriminator": 512, "google/electra-base-discriminator": 512, "google/electra-large-discriminator": 512, } lowercase__ = { "google/electra-small-generator": {"do_lower_case": True}, "google/electra-base-generator": {"do_lower_case": True}, "google/electra-large-generator": {"do_lower_case": True}, "google/electra-small-discriminator": {"do_lower_case": True}, "google/electra-base-discriminator": {"do_lower_case": True}, "google/electra-large-discriminator": {"do_lower_case": True}, } class A_ ( _snake_case ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = VOCAB_FILES_NAMES UpperCAmelCase_ : List[str] = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : Tuple = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase_ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase_ : Any = ElectraTokenizer def __init__( self : str , lowercase_ : Dict=None , lowercase_ : int=None , lowercase_ : Any=True , lowercase_ : Dict="[UNK]" , lowercase_ : Any="[SEP]" , lowercase_ : Optional[int]="[PAD]" , lowercase_ : int="[CLS]" , lowercase_ : Optional[int]="[MASK]" , lowercase_ : Dict=True , lowercase_ : Dict=None , **lowercase_ : Any , ) -> Tuple: super().__init__( lowercase_ , tokenizer_file=lowercase_ , do_lower_case=lowercase_ , unk_token=lowercase_ , sep_token=lowercase_ , pad_token=lowercase_ , cls_token=lowercase_ , mask_token=lowercase_ , tokenize_chinese_chars=lowercase_ , strip_accents=lowercase_ , **lowercase_ , ) UpperCAmelCase : int = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , lowercase_ ) != do_lower_case or normalizer_state.get('strip_accents' , lowercase_ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , lowercase_ ) != tokenize_chinese_chars ): UpperCAmelCase : Optional[int] = getattr(lowercase_ , normalizer_state.pop('type' ) ) UpperCAmelCase : Any = do_lower_case UpperCAmelCase : str = strip_accents UpperCAmelCase : str = tokenize_chinese_chars UpperCAmelCase : Dict = normalizer_class(**lowercase_ ) UpperCAmelCase : List[str] = do_lower_case def UpperCAmelCase_ ( self : List[Any] , lowercase_ : Union[str, Any] , lowercase_ : Dict=None ) -> Any: UpperCAmelCase : List[Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def UpperCAmelCase_ ( self : List[Any] , lowercase_ : List[int] , lowercase_ : Optional[List[int]] = None ) -> List[int]: UpperCAmelCase : List[Any] = [self.sep_token_id] UpperCAmelCase : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase_ ( self : Optional[int] , lowercase_ : str , lowercase_ : Optional[str] = None ) -> Tuple[str]: UpperCAmelCase : Dict = self._tokenizer.model.save(lowercase_ , name=lowercase_ ) return tuple(lowercase_ )
151
'''simple docstring''' def UpperCamelCase( UpperCAmelCase_ ): return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def UpperCamelCase( UpperCAmelCase_ ): UpperCAmelCase : List[Any] = 0 UpperCAmelCase : Dict = len(UpperCAmelCase_ ) # No of vertices in graph UpperCAmelCase : Tuple = [0] * n UpperCAmelCase : List[Any] = [False] * n def dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): UpperCAmelCase : List[str] = True UpperCAmelCase : Dict = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , id_ ) UpperCAmelCase : Optional[Any] = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge UpperCAmelCase : Dict = min(low[at] , low[to] ) UpperCAmelCase : list[tuple[int, int]] = [] for i in range(UpperCAmelCase_ ): if not visited[i]: dfs(UpperCAmelCase_ , -1 , UpperCAmelCase_ , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
151
1
from __future__ import annotations def UpperCamelCase__( UpperCamelCase__ : tuple[int, int] , UpperCamelCase__ : int )->list[tuple[int, int]]: A__ , A__ = position A__ = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] A__ = [] for position in positions: A__ , A__ = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(UpperCamelCase__ ) return permissible_positions def UpperCamelCase__( UpperCamelCase__ : list[list[int]] )->bool: return not any(elem == 0 for row in board for elem in row ) def UpperCamelCase__( UpperCamelCase__ : list[list[int]] , UpperCamelCase__ : tuple[int, int] , UpperCamelCase__ : int )->bool: if is_complete(UpperCamelCase__ ): return True for position in get_valid_pos(UpperCamelCase__ , len(UpperCamelCase__ ) ): A__ , A__ = position if board[y][x] == 0: A__ = curr + 1 if open_knight_tour_helper(UpperCamelCase__ , UpperCamelCase__ , curr + 1 ): return True A__ = 0 return False def UpperCamelCase__( UpperCamelCase__ : int )->list[list[int]]: A__ = [[0 for i in range(UpperCamelCase__ )] for j in range(UpperCamelCase__ )] for i in range(UpperCamelCase__ ): for j in range(UpperCamelCase__ ): A__ = 1 if open_knight_tour_helper(UpperCamelCase__ , (i, j) , 1 ): return board A__ = 0 A__ = f"Open Kight Tour cannot be performed on a board of size {n}" raise ValueError(UpperCamelCase__ ) if __name__ == "__main__": import doctest doctest.testmod()
39
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch from ..models.clipseg import CLIPSegForImageSegmentation from ..utils import is_vision_available, requires_backends from .base import PipelineTool if is_vision_available(): from PIL import Image class SCREAMING_SNAKE_CASE__ ( UpperCamelCase__ ): __SCREAMING_SNAKE_CASE = ( '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.''' '''It takes two arguments named `image` which should be the original image, and `label` which should be a text ''' '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.''' ) __SCREAMING_SNAKE_CASE = '''CIDAS/clipseg-rd64-refined''' __SCREAMING_SNAKE_CASE = '''image_segmenter''' __SCREAMING_SNAKE_CASE = CLIPSegForImageSegmentation __SCREAMING_SNAKE_CASE = ['''image''', '''text'''] __SCREAMING_SNAKE_CASE = ['''image'''] def __init__( self,*__lowerCamelCase,**__lowerCamelCase ): requires_backends(self,['''vision'''] ) super().__init__(*__lowerCamelCase,**__lowerCamelCase ) def UpperCamelCase ( self,__lowerCamelCase,__lowerCamelCase ): return self.pre_processor(text=[label],images=[image],padding=__lowerCamelCase,return_tensors='''pt''' ) def UpperCamelCase ( self,__lowerCamelCase ): with torch.no_grad(): A__ = self.model(**__lowerCamelCase ).logits return logits def UpperCamelCase ( self,__lowerCamelCase ): A__ = outputs.cpu().detach().numpy() A__ = 0 A__ = 1 return Image.fromarray((array * 255).astype(np.uinta ) )
39
1
def __SCREAMING_SNAKE_CASE ( snake_case_ ): '''simple docstring''' _UpperCAmelCase = len(snake_case_ ) for i in range(snake_case_ ): for j in range(i + 1 , snake_case_ ): if numbers[j] < numbers[i]: _UpperCAmelCase , _UpperCAmelCase = numbers[j], numbers[i] return numbers if __name__ == "__main__": lowercase_ : Optional[Any] = input('Enter numbers separated by a comma:\n').strip() lowercase_ : Dict = [int(item) for item in user_input.split(',')] print(exchange_sort(unsorted))
133
from typing import List, Union import numpy as np from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING lowercase_ : Tuple = logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class __lowerCAmelCase ( UpperCAmelCase__ ): def __init__( self : str , *snake_case__ : Any , **snake_case__ : str ): """simple docstring""" super().__init__(*snake_case__ , **snake_case__ ) requires_backends(self , "vision" ) self.check_model_type(snake_case__ ) def __call__( self : Dict , snake_case__ : Union[str, List[str], "Image.Image", List["Image.Image"]] , **snake_case__ : List[Any] ): """simple docstring""" return super().__call__(snake_case__ , **snake_case__ ) def UpperCamelCase ( self : List[Any] , **snake_case__ : Tuple ): """simple docstring""" return {}, {}, {} def UpperCamelCase ( self : Any , snake_case__ : Optional[int] ): """simple docstring""" _UpperCAmelCase = load_image(snake_case__ ) _UpperCAmelCase = image.size _UpperCAmelCase = self.image_processor(images=snake_case__ , return_tensors=self.framework ) return model_inputs def UpperCamelCase ( self : int , snake_case__ : int ): """simple docstring""" _UpperCAmelCase = self.model(**snake_case__ ) return model_outputs def UpperCamelCase ( self : List[Any] , snake_case__ : str ): """simple docstring""" _UpperCAmelCase = model_outputs.predicted_depth _UpperCAmelCase = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1 ) , size=self.image_size[::-1] , mode="bicubic" , align_corners=snake_case__ ) _UpperCAmelCase = prediction.squeeze().cpu().numpy() _UpperCAmelCase = (output * 255 / np.max(snake_case__ )).astype("uint8" ) _UpperCAmelCase = Image.fromarray(snake_case__ ) _UpperCAmelCase = {} _UpperCAmelCase = predicted_depth _UpperCAmelCase = depth return output_dict
133
1
"""simple docstring""" import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _UpperCamelCase: Tuple = logging.get_logger(__name__) _UpperCamelCase: List[str] = {'vocab_file': 'vocab.txt', 'emoji_file': 'emoji.json'} _UpperCamelCase: Optional[Any] = { 'vocab_file': { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt', }, 'emoji_file': { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json', }, } _UpperCamelCase: int = { 'abeja/gpt-neox-japanese-2.7b': 2_0_4_8, } def lowercase__ ( _UpperCAmelCase , _UpperCAmelCase ) -> List[str]: '''simple docstring''' with open(_UpperCAmelCase , 'r' , encoding='utf-8' ) as f: lowercase : List[str] = json.loads(f.read() ) lowercase : int = collections.OrderedDict() lowercase : int = collections.OrderedDict() lowercase : Dict = collections.OrderedDict() with open(_UpperCAmelCase , 'r' , encoding='utf-8' ) as f: lowercase : int = f.readlines() lowercase : Optional[Any] = [[t.rstrip('\n' )] if (t == ',' or ',' not in t) else t.rstrip('\n' ).split(',' ) for t in token] for idx, b in enumerate(_UpperCAmelCase ): lowercase : Union[str, Any] = b lowercase : Any = idx for wd in b: lowercase : Union[str, Any] = idx return vocab, raw_vocab, ids_to_tokens, emoji class a__ ( SCREAMING_SNAKE_CASE__ ): _lowerCamelCase = VOCAB_FILES_NAMES _lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase = ['input_ids', 'attention_mask'] def __init__( self : Any, lowerCAmelCase : List[str], lowerCAmelCase : List[Any], lowerCAmelCase : int="<|endoftext|>", lowerCAmelCase : List[str]="<|endoftext|>", lowerCAmelCase : Tuple="<|startoftext|>", lowerCAmelCase : int="<|endoftext|>", lowerCAmelCase : Dict=False, **lowerCAmelCase : Any, ) -> Any: super().__init__( unk_token=lowerCAmelCase, pad_token=lowerCAmelCase, bos_token=lowerCAmelCase, eos_token=lowerCAmelCase, do_clean_text=lowerCAmelCase, **lowerCAmelCase, ) if not os.path.isfile(lowerCAmelCase ): raise ValueError( f'''Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained''' ' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) if not os.path.isfile(lowerCAmelCase ): raise ValueError( f'''Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google''' ' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) lowercase : List[str] = do_clean_text lowercase , lowercase , lowercase , lowercase : List[str] = load_vocab_and_emoji(lowerCAmelCase, lowerCAmelCase ) lowercase : Optional[Any] = SubWordJapaneseTokenizer( vocab=self.vocab, ids_to_tokens=self.ids_to_tokens, emoji=self.emoji ) @property def lowercase ( self : str ) -> str: # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab return len(self.raw_vocab ) def lowercase ( self : Any ) -> Optional[Any]: return dict(self.raw_vocab, **self.added_tokens_encoder ) def lowercase ( self : Optional[Any], lowerCAmelCase : int ) -> Optional[Any]: return self.subword_tokenizer.tokenize(lowerCAmelCase, clean=self.do_clean_text ) def lowercase ( self : List[str], lowerCAmelCase : Any ) -> Optional[Any]: return self.vocab.get(lowerCAmelCase, self.vocab.get(self.unk_token ) ) def lowercase ( self : Any, lowerCAmelCase : Any ) -> Union[str, Any]: return self.subword_tokenizer.convert_id_to_token(lowerCAmelCase ) def lowercase ( self : List[str], lowerCAmelCase : Union[str, Any] ) -> Union[str, Any]: lowercase : Dict = ''.join(lowerCAmelCase ).strip() return out_string def lowercase ( self : Union[str, Any], lowerCAmelCase : "Conversation" ) -> List[int]: lowercase : Any = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(lowerCAmelCase, add_special_tokens=lowerCAmelCase ) + [self.eos_token_id] ) if len(lowerCAmelCase ) > self.model_max_length: lowercase : int = input_ids[-self.model_max_length :] return input_ids def lowercase ( self : int, lowerCAmelCase : str, lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: lowercase : int = 0 if os.path.isdir(lowerCAmelCase ): lowercase : Union[str, Any] = os.path.join( lowerCAmelCase, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) lowercase : Optional[Any] = os.path.join( lowerCAmelCase, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'] ) else: lowercase : int = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file'] ) lowercase : List[Any] = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file'] ) with open(lowerCAmelCase, 'w', encoding='utf-8' ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( f'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.''' ' Please check that the vocabulary is not corrupted!' ) lowercase : int = token_index writer.write(','.join(lowerCAmelCase ) + '\n' ) index += 1 with open(lowerCAmelCase, 'w', encoding='utf-8' ) as writer: json.dump(self.emoji, lowerCAmelCase ) return vocab_file, emoji_file class a__ ( SCREAMING_SNAKE_CASE__ ): def __init__( self : Dict, lowerCAmelCase : List[str], lowerCAmelCase : int, lowerCAmelCase : Tuple ) -> int: lowercase : List[str] = vocab # same as swe lowercase : List[Any] = ids_to_tokens # same as bpe lowercase : Any = emoji lowercase : Any = np.max([len(lowerCAmelCase ) for w in self.vocab.keys()] ) lowercase : int = re.compile(R'(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)' ) lowercase : Dict = re.compile(R'[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*' ) lowercase : Dict = re.compile(R'[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}' ) lowercase : Tuple = re.compile( R'([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) lowercase : List[Any] = re.compile( R'(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) lowercase : int = re.compile( R'((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*' ) lowercase : Tuple = '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿' lowercase : Union[str, Any] = '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟' lowercase : List[Any] = str.maketrans({k: '<BLOCK>' for k in keisen + blocks} ) def __len__( self : List[Any] ) -> Optional[int]: return len(self.ids_to_tokens ) def lowercase ( self : int, lowerCAmelCase : Optional[int] ) -> Any: lowercase : Union[str, Any] = self.content_repattera.sub('<URL>', lowerCAmelCase ) lowercase : List[str] = self.content_repattera.sub('<EMAIL>', lowerCAmelCase ) lowercase : Optional[Any] = self.content_repattera.sub('<TEL>', lowerCAmelCase ) lowercase : Union[str, Any] = self.content_repattera.sub('<DATE>', lowerCAmelCase ) lowercase : Tuple = self.content_repattera.sub('<DATE>', lowerCAmelCase ) lowercase : Union[str, Any] = self.content_repattera.sub('<PRICE>', lowerCAmelCase ) lowercase : int = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: lowercase : List[Any] = content.replace('<BLOCK><BLOCK>', '<BLOCK>' ) return content def lowercase ( self : str, lowerCAmelCase : Any, lowerCAmelCase : Any=False ) -> str: lowercase : str = text.replace(' ', '<SP>' ) lowercase : Optional[int] = text.replace(' ', '<SP>' ) lowercase : Union[str, Any] = text.replace('\r\n', '<BR>' ) lowercase : Optional[int] = text.replace('\n', '<BR>' ) lowercase : List[str] = text.replace('\r', '<BR>' ) lowercase : Optional[int] = text.replace('\t', '<TAB>' ) lowercase : List[str] = text.replace('—', 'ー' ) lowercase : List[str] = text.replace('−', 'ー' ) for k, v in self.emoji["emoji"].items(): if k in text: lowercase : Tuple = text.replace(lowerCAmelCase, lowerCAmelCase ) if clean: lowercase : List[str] = self.clean_text(lowerCAmelCase ) def check_simbol(lowerCAmelCase : Optional[Any] ): lowercase : Union[str, Any] = x.encode() if len(lowerCAmelCase ) == 1 and len(lowerCAmelCase ) == 2: lowercase : Union[str, Any] = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0xC_2_A_1 and c <= 0xC_2_B_F) or (c >= 0xC_7_8_0 and c <= 0xC_7_8_3) or (c >= 0xC_A_B_9 and c <= 0xC_B_B_F) or (c >= 0xC_C_8_0 and c <= 0xC_D_A_2) ): return True return False def checkuae(lowerCAmelCase : Union[str, Any] ): lowercase : str = x.encode() if len(lowerCAmelCase ) == 1 and len(lowerCAmelCase ) == 3: lowercase : Tuple = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0xE_2_8_0_8_0 and c <= 0xE_2_B_0_7_F: return True return False lowercase : Optional[int] = 0 lowercase : Union[str, Any] = [] while pos < len(lowerCAmelCase ): lowercase : Union[str, Any] = min(len(lowerCAmelCase ), pos + self.maxlen + 1 ) if text[pos] == '<' else pos + 3 lowercase : Any = [] # (token_id, token, pos) for e in range(lowerCAmelCase, lowerCAmelCase, -1 ): lowercase : Any = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(lowerCAmelCase ) > 2: lowercase : Any = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(lowerCAmelCase ) > 0: # the smallest token_id is adopted lowercase , lowercase , lowercase : Dict = sorted(lowerCAmelCase, key=lambda lowerCAmelCase : x[0] )[0] result.append(lowerCAmelCase ) lowercase : Union[str, Any] = e else: lowercase : Optional[Any] = pos + 1 lowercase : Union[str, Any] = text[pos:end] if check_simbol(lowerCAmelCase ): result.append('<KIGOU>' ) elif checkuae(lowerCAmelCase ): result.append('<U2000U2BFF>' ) else: for i in wd.encode('utf-8' ): result.append('<|byte%d|>' % i ) lowercase : Union[str, Any] = end return result def lowercase ( self : Dict, lowerCAmelCase : Any, lowerCAmelCase : List[Any]="\n" ) -> List[str]: lowercase : List[str] = [] lowercase : Any = [] lowercase : int = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(lowerCAmelCase ) > 0: words.append(bytearray(lowerCAmelCase ).decode('utf-8', errors='replace' ) ) lowercase : str = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji['emoji_inv'][word] ) elif word == "<SP>": words.append(' ' ) elif word == "<BR>": words.append(lowerCAmelCase ) elif word == "<TAB>": words.append('\t' ) elif word == "<BLOCK>": words.append('▀' ) elif word == "<KIGOU>": words.append('ǀ' ) elif word == "<U2000U2BFF>": words.append('‖' ) else: words.append(lowerCAmelCase ) if len(lowerCAmelCase ) > 0: words.append(bytearray(lowerCAmelCase ).decode('utf-8', errors='replace' ) ) lowercase : Dict = ''.join(lowerCAmelCase ) return text
53
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCamelCase: str = logging.get_logger(__name__) _UpperCamelCase: Union[str, Any] = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class a__ ( SCREAMING_SNAKE_CASE__ ): _lowerCamelCase = 'mgp-str' def __init__( self : Tuple, lowerCAmelCase : str=[32, 128], lowerCAmelCase : List[Any]=4, lowerCAmelCase : Union[str, Any]=3, lowerCAmelCase : Union[str, Any]=27, lowerCAmelCase : Union[str, Any]=38, lowerCAmelCase : Tuple=50257, lowerCAmelCase : Dict=30522, lowerCAmelCase : Optional[int]=768, lowerCAmelCase : Optional[int]=12, lowerCAmelCase : Optional[int]=12, lowerCAmelCase : Union[str, Any]=4.0, lowerCAmelCase : Any=True, lowerCAmelCase : Optional[int]=False, lowerCAmelCase : Optional[int]=1e-5, lowerCAmelCase : List[str]=0.0, lowerCAmelCase : Optional[Any]=0.0, lowerCAmelCase : List[str]=0.0, lowerCAmelCase : Dict=False, lowerCAmelCase : Union[str, Any]=0.02, **lowerCAmelCase : Optional[int], ) -> List[Any]: super().__init__(**lowerCAmelCase ) lowercase : int = image_size lowercase : Dict = patch_size lowercase : List[str] = num_channels lowercase : Union[str, Any] = max_token_length lowercase : str = num_character_labels lowercase : Tuple = num_bpe_labels lowercase : Tuple = num_wordpiece_labels lowercase : Optional[Any] = hidden_size lowercase : Tuple = num_hidden_layers lowercase : Optional[Any] = num_attention_heads lowercase : Tuple = mlp_ratio lowercase : Union[str, Any] = distilled lowercase : List[str] = layer_norm_eps lowercase : Optional[int] = drop_rate lowercase : Tuple = qkv_bias lowercase : int = attn_drop_rate lowercase : Any = drop_path_rate lowercase : Optional[Any] = output_aa_attentions lowercase : Optional[Any] = initializer_range
53
1
import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): UpperCAmelCase__ = CpmAntTokenizer UpperCAmelCase__ = False def A_ ( self : List[str] ) -> Union[str, Any]: super().setUp() lowerCamelCase__ : List[Any] = [ '<d>', '</d>', '<s>', '</s>', '</_>', '<unk>', '<pad>', '</n>', '我', '是', 'C', 'P', 'M', 'A', 'n', 't', ] lowerCamelCase__ : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) @tooslow def A_ ( self : Union[str, Any] ) -> Tuple: lowerCamelCase__ : int = CpmAntTokenizer.from_pretrained('openbmb/cpm-ant-10b' ) lowerCamelCase__ : Any = '今天天气真好!' lowerCamelCase__ : Dict = ['今天', '天气', '真', '好', '!'] lowerCamelCase__ : Any = tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : List[str] = '今天天气真好!' lowerCamelCase__ : List[str] = [tokenizer.bos_token] + tokens lowerCamelCase__ : int = [6, 9802, 14962, 2082, 831, 244] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , UpperCAmelCase ) lowerCamelCase__ : str = tokenizer.decode(UpperCAmelCase ) self.assertEqual(UpperCAmelCase , UpperCAmelCase )
50
import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging _UpperCAmelCase : Optional[int] = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Dict: lowerCamelCase__ : str = set() lowerCamelCase__ : Any = [] def parse_line(_UpperCAmelCase ): for line in fp: if isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowerCamelCase__ : Any = line.decode('UTF-8' ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(' ' ): # process a single warning and move it to `selected_warnings`. if len(_UpperCAmelCase ) > 0: lowerCamelCase__ : str = '\n'.join(_UpperCAmelCase ) # Only keep the warnings specified in `targets` if any(F""": {x}: """ in warning for x in targets ): selected_warnings.add(_UpperCAmelCase ) buffer.clear() continue else: lowerCamelCase__ : List[str] = line.strip() buffer.append(_UpperCAmelCase ) if from_gh: for filename in os.listdir(_UpperCAmelCase ): lowerCamelCase__ : Dict = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) if not os.path.isdir(_UpperCAmelCase ): # read the file if filename != "warnings.txt": continue with open(_UpperCAmelCase ) as fp: parse_line(_UpperCAmelCase ) else: try: with zipfile.ZipFile(_UpperCAmelCase ) as z: for filename in z.namelist(): if not os.path.isdir(_UpperCAmelCase ): # read the file if filename != "warnings.txt": continue with z.open(_UpperCAmelCase ) as fp: parse_line(_UpperCAmelCase ) except Exception: logger.warning( F"""{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.""" ) return selected_warnings def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase ) -> Union[str, Any]: lowerCamelCase__ : Tuple = set() lowerCamelCase__ : Optional[int] = [os.path.join(_UpperCAmelCase , _UpperCAmelCase ) for p in os.listdir(_UpperCAmelCase ) if (p.endswith('.zip' ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(_UpperCAmelCase , _UpperCAmelCase ) ) return selected_warnings if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> Tuple: return values.split(',' ) _UpperCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument("""--workflow_run_id""", type=str, required=True, help="""A GitHub Actions workflow run id.""") parser.add_argument( """--output_dir""", type=str, required=True, help="""Where to store the downloaded artifacts and other result files.""", ) parser.add_argument("""--token""", default=None, type=str, help="""A token that has actions:read permission.""") # optional parameters parser.add_argument( """--targets""", default="""DeprecationWarning,UserWarning,FutureWarning""", type=list_str, help="""Comma-separated list of target warning(s) which we want to extract.""", ) parser.add_argument( """--from_gh""", action="""store_true""", help="""If running from a GitHub action workflow and collecting warnings from its artifacts.""", ) _UpperCAmelCase : Union[str, Any] = parser.parse_args() _UpperCAmelCase : Dict = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links _UpperCAmelCase : Union[str, Any] = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, """artifacts.json"""), """w""", encoding="""UTF-8""") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print("""=""" * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts _UpperCAmelCase : Dict = extract_warnings(args.output_dir, args.targets) _UpperCAmelCase : Optional[Any] = sorted(selected_warnings) with open(os.path.join(args.output_dir, """selected_warnings.json"""), """w""", encoding="""UTF-8""") as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
50
1
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print("Googling.....") lowerCamelCase : str = "https://www.google.com/search?q=" + " ".join(sys.argv[1:]) lowerCamelCase : Dict = requests.get(url, headers={"UserAgent": UserAgent().random}) # res.raise_for_status() with open("project1a.html", "wb") as out_file: # only for knowing the class for data in res.iter_content(1_0_0_0_0): out_file.write(data) lowerCamelCase : Tuple = BeautifulSoup(res.text, "html.parser") lowerCamelCase : Optional[Any] = list(soup.select(".eZt8xd"))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get("href")) else: webbrowser.open(f'''https://google.com{link.get("href")}''')
114
'''simple docstring''' import os def _lowerCAmelCase ( ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =os.path.dirname(os.path.realpath(_UpperCamelCase ) ) _SCREAMING_SNAKE_CASE =os.path.join(_UpperCamelCase , 'triangle.txt' ) with open(_UpperCamelCase ) as f: _SCREAMING_SNAKE_CASE =f.readlines() _SCREAMING_SNAKE_CASE =[] for line in triangle: _SCREAMING_SNAKE_CASE =[] for number in line.strip().split(' ' ): numbers_from_line.append(int(_UpperCamelCase ) ) a.append(_UpperCamelCase ) for i in range(1 , len(_UpperCamelCase ) ): for j in range(len(a[i] ) ): _SCREAMING_SNAKE_CASE =a[i - 1][j] if j != len(a[i - 1] ) else 0 _SCREAMING_SNAKE_CASE =a[i - 1][j - 1] if j > 0 else 0 a[i][j] += max(_UpperCamelCase , _UpperCamelCase ) return max(a[-1] ) if __name__ == "__main__": print(solution())
114
1
"""simple docstring""" import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """_float_tensor""", """decoder.output_projection.weight""", ] for k in ignore_keys: state_dict.pop(lowercase ,lowercase ) def __UpperCAmelCase ( lowercase ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = emb.weight.shape _UpperCAmelCase = nn.Linear(lowercase ,lowercase ,bias=lowercase ) _UpperCAmelCase = emb.weight.data return lin_layer def __UpperCAmelCase ( lowercase ,lowercase="facebook/mbart-large-en-ro" ,lowercase=False ,lowercase=False ): """simple docstring""" _UpperCAmelCase = torch.load(lowercase ,map_location="""cpu""" )["""model"""] remove_ignore_keys_(lowercase ) _UpperCAmelCase = state_dict["""encoder.embed_tokens.weight"""].shape[0] _UpperCAmelCase = MBartConfig.from_pretrained(lowercase ,vocab_size=lowercase ) if mbart_aa and finetuned: _UpperCAmelCase = """relu""" _UpperCAmelCase = state_dict["""decoder.embed_tokens.weight"""] _UpperCAmelCase = MBartForConditionalGeneration(lowercase ) model.model.load_state_dict(lowercase ) if finetuned: _UpperCAmelCase = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """fairseq_path""", type=str, help="""bart.large, bart.large.cnn or a 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.""") parser.add_argument( """--hf_config""", default="""facebook/mbart-large-cc25""", type=str, help="""Which huggingface architecture to use: mbart-large""", ) parser.add_argument("""--mbart_50""", action="""store_true""", help="""whether the model is mMART-50 checkpoint""") parser.add_argument("""--finetuned""", action="""store_true""", help="""whether the model is a fine-tuned checkpoint""") UpperCAmelCase__ = parser.parse_args() UpperCAmelCase__ = convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
289
"""simple docstring""" UpperCAmelCase__ = { """meter""": """m""", """kilometer""": """km""", """megametre""": """Mm""", """gigametre""": """Gm""", """terametre""": """Tm""", """petametre""": """Pm""", """exametre""": """Em""", """zettametre""": """Zm""", """yottametre""": """Ym""", } # Exponent of the factor(meter) UpperCAmelCase__ = { """m""": 0, """km""": 3, """Mm""": 6, """Gm""": 9, """Tm""": 1_2, """Pm""": 1_5, """Em""": 1_8, """Zm""": 2_1, """Ym""": 2_4, } def __UpperCAmelCase ( lowercase ,lowercase ,lowercase ): """simple docstring""" _UpperCAmelCase = from_type.lower().strip("""s""" ) _UpperCAmelCase = to_type.lower().strip("""s""" ) _UpperCAmelCase = UNIT_SYMBOL.get(lowercase ,lowercase ) _UpperCAmelCase = UNIT_SYMBOL.get(lowercase ,lowercase ) if from_sanitized not in METRIC_CONVERSION: _UpperCAmelCase = ( f'''Invalid \'from_type\' value: {from_type!r}.\n''' f'''Conversion abbreviations are: {", ".join(lowercase )}''' ) raise ValueError(lowercase ) if to_sanitized not in METRIC_CONVERSION: _UpperCAmelCase = ( f'''Invalid \'to_type\' value: {to_type!r}.\n''' f'''Conversion abbreviations are: {", ".join(lowercase )}''' ) raise ValueError(lowercase ) _UpperCAmelCase = METRIC_CONVERSION[from_sanitized] _UpperCAmelCase = METRIC_CONVERSION[to_sanitized] _UpperCAmelCase = 1 if from_exponent > to_exponent: _UpperCAmelCase = from_exponent - to_exponent else: _UpperCAmelCase = -(to_exponent - from_exponent) return value * pow(10 ,lowercase ) if __name__ == "__main__": from doctest import testmod testmod()
289
1
'''simple docstring''' import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import evaluate import numpy as np from datasets import load_dataset import transformers from transformers import ( AutoConfig, AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('''4.31.0''') require_version('''datasets>=1.8.0''', '''To fix: pip install -r examples/pytorch/text-classification/requirements.txt''') __snake_case = logging.getLogger(__name__) @dataclass class lowercase : """simple docstring""" _a = field( default=1_28 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) _a = field( default=lowerCAmelCase_ , metadata={ 'help': ( 'Whether to pad all samples to `max_seq_length`. ' 'If False, will pad the samples dynamically when batching to the maximum length in the batch.' ) } , ) _a = field( default=lowerCAmelCase_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _a = field( default=lowerCAmelCase_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) _a = field( default=lowerCAmelCase_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of prediction examples to this ' 'value if set.' ) } , ) @dataclass class lowercase : """simple docstring""" _a = field( default=lowerCAmelCase_ , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Evaluation language. Also train language if `train_language` is set to None.'} ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Train language if it is different from the evaluation language.'} ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'arg to indicate if tokenizer should do lower case in AutoTokenizer.from_pretrained()'} , ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'} , ) _a = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) _a = field( default=lowerCAmelCase_ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) _a = field( default=lowerCAmelCase_ , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def a ( ) -> List[str]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. UpperCamelCase__ :List[Any] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :List[Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_xnli''' , __a ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCamelCase__ :Any = training_args.get_process_log_level() logger.setLevel(__a ) datasets.utils.logging.set_verbosity(__a ) transformers.utils.logging.set_verbosity(__a ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + f'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. UpperCamelCase__ :Any = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase__ :Dict = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None: logger.info( f'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # In distributed training, the load_dataset function guarantees that only one local process can concurrently # download the dataset. # Downloading and loading xnli dataset from the hub. if training_args.do_train: if model_args.train_language is None: UpperCamelCase__ :Tuple = load_dataset( '''xnli''' , model_args.language , split='''train''' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) else: UpperCamelCase__ :Any = load_dataset( '''xnli''' , model_args.train_language , split='''train''' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ :Optional[int] = train_dataset.features['''label'''].names if training_args.do_eval: UpperCamelCase__ :int = load_dataset( '''xnli''' , model_args.language , split='''validation''' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ :int = eval_dataset.features['''label'''].names if training_args.do_predict: UpperCamelCase__ :Dict = load_dataset( '''xnli''' , model_args.language , split='''test''' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ :List[str] = predict_dataset.features['''label'''].names # Labels UpperCamelCase__ :Optional[int] = len(__a ) # Load pretrained model and tokenizer # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase__ :Optional[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__a , idalabel={str(__a ): label for i, label in enumerate(__a )} , labelaid={label: i for i, label in enumerate(__a )} , finetuning_task='''xnli''' , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ :int = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , do_lower_case=model_args.do_lower_case , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase__ :int = AutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__a , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # Preprocessing the datasets # Padding strategy if data_args.pad_to_max_length: UpperCamelCase__ :Dict = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch UpperCamelCase__ :Tuple = False def preprocess_function(__a ): # Tokenize the texts return tokenizer( examples['''premise'''] , examples['''hypothesis'''] , padding=__a , max_length=data_args.max_seq_length , truncation=__a , ) if training_args.do_train: if data_args.max_train_samples is not None: UpperCamelCase__ :Optional[Any] = min(len(__a ) , data_args.max_train_samples ) UpperCamelCase__ :str = train_dataset.select(range(__a ) ) with training_args.main_process_first(desc='''train dataset map pre-processing''' ): UpperCamelCase__ :Optional[Any] = train_dataset.map( __a , batched=__a , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on train dataset''' , ) # Log a few random samples from the training set: for index in random.sample(range(len(__a ) ) , 3 ): logger.info(f'''Sample {index} of the training set: {train_dataset[index]}.''' ) if training_args.do_eval: if data_args.max_eval_samples is not None: UpperCamelCase__ :Optional[Any] = min(len(__a ) , data_args.max_eval_samples ) UpperCamelCase__ :Optional[Any] = eval_dataset.select(range(__a ) ) with training_args.main_process_first(desc='''validation dataset map pre-processing''' ): UpperCamelCase__ :Optional[int] = eval_dataset.map( __a , batched=__a , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on validation dataset''' , ) if training_args.do_predict: if data_args.max_predict_samples is not None: UpperCamelCase__ :Optional[Any] = min(len(__a ) , data_args.max_predict_samples ) UpperCamelCase__ :Union[str, Any] = predict_dataset.select(range(__a ) ) with training_args.main_process_first(desc='''prediction dataset map pre-processing''' ): UpperCamelCase__ :Union[str, Any] = predict_dataset.map( __a , batched=__a , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on prediction dataset''' , ) # Get the metric function UpperCamelCase__ :Union[str, Any] = evaluate.load('''xnli''' ) # You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__a ): UpperCamelCase__ :Union[str, Any] = p.predictions[0] if isinstance(p.predictions , __a ) else p.predictions UpperCamelCase__ :List[Any] = np.argmax(__a , axis=1 ) return metric.compute(predictions=__a , references=p.label_ids ) # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: UpperCamelCase__ :Any = default_data_collator elif training_args.fpaa: UpperCamelCase__ :Optional[int] = DataCollatorWithPadding(__a , pad_to_multiple_of=8 ) else: UpperCamelCase__ :Dict = None # Initialize our Trainer UpperCamelCase__ :Tuple = Trainer( model=__a , args=__a , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=__a , tokenizer=__a , data_collator=__a , ) # Training if training_args.do_train: UpperCamelCase__ :List[Any] = None if training_args.resume_from_checkpoint is not None: UpperCamelCase__ :Optional[Any] = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase__ :List[str] = last_checkpoint UpperCamelCase__ :Any = trainer.train(resume_from_checkpoint=__a ) UpperCamelCase__ :str = train_result.metrics UpperCamelCase__ :List[Any] = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__a ) ) UpperCamelCase__ :Optional[int] = min(__a , len(__a ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , __a ) trainer.save_metrics('''train''' , __a ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) UpperCamelCase__ :Any = trainer.evaluate(eval_dataset=__a ) UpperCamelCase__ :Union[str, Any] = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__a ) UpperCamelCase__ :Union[str, Any] = min(__a , len(__a ) ) trainer.log_metrics('''eval''' , __a ) trainer.save_metrics('''eval''' , __a ) # Prediction if training_args.do_predict: logger.info('''*** Predict ***''' ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ :Optional[Any] = trainer.predict(__a , metric_key_prefix='''predict''' ) UpperCamelCase__ :int = ( data_args.max_predict_samples if data_args.max_predict_samples is not None else len(__a ) ) UpperCamelCase__ :str = min(__a , len(__a ) ) trainer.log_metrics('''predict''' , __a ) trainer.save_metrics('''predict''' , __a ) UpperCamelCase__ :str = np.argmax(__a , axis=1 ) UpperCamelCase__ :str = os.path.join(training_args.output_dir , '''predictions.txt''' ) if trainer.is_world_process_zero(): with open(__a , '''w''' ) as writer: writer.write('''index\tprediction\n''' ) for index, item in enumerate(__a ): UpperCamelCase__ :Dict = label_list[item] writer.write(f'''{index}\t{item}\n''' ) if __name__ == "__main__": main()
352
'''simple docstring''' from collections import Counter from timeit import timeit def a ( __a = "" , ) -> bool: '''simple docstring''' return sum(c % 2 for c in Counter(input_str.replace(''' ''' , '''''' ).lower() ).values() ) < 2 def a ( __a = "" ) -> bool: '''simple docstring''' if len(__a ) == 0: return True UpperCamelCase__ :List[Any] = input_str.replace(''' ''' , '''''' ).lower() # character_freq_dict: Stores the frequency of every character in the input string UpperCamelCase__ :dict[str, int] = {} for character in lower_case_input_str: UpperCamelCase__ :Optional[int] = character_freq_dict.get(__a , 0 ) + 1 UpperCamelCase__ :List[str] = 0 for character_count in character_freq_dict.values(): if character_count % 2: odd_char += 1 if odd_char > 1: return False return True def a ( __a = "" ) -> None: '''simple docstring''' print('''\nFor string = ''' , __a , ''':''' ) print( '''> can_string_be_rearranged_as_palindrome_counter()''' , '''\tans =''' , can_string_be_rearranged_as_palindrome_counter(__a ) , '''\ttime =''' , timeit( '''z.can_string_be_rearranged_as_palindrome_counter(z.check_str)''' , setup='''import __main__ as z''' , ) , '''seconds''' , ) print( '''> can_string_be_rearranged_as_palindrome()''' , '''\tans =''' , can_string_be_rearranged_as_palindrome(__a ) , '''\ttime =''' , timeit( '''z.can_string_be_rearranged_as_palindrome(z.check_str)''' , setup='''import __main__ as z''' , ) , '''seconds''' , ) if __name__ == "__main__": __snake_case = input( '''Enter string to determine if it can be rearranged as a palindrome or not: ''' ).strip() benchmark(check_str) __snake_case = can_string_be_rearranged_as_palindrome_counter(check_str) print(F"""{check_str} can {'' if status else 'not '}be rearranged as a palindrome""")
219
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() _a = logging.get_logger(__name__) def __A ( __lowerCAmelCase , __lowerCAmelCase=False )-> Union[str, Any]: """simple docstring""" _UpperCAmelCase = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'vit.embeddings.cls_token'), ('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'vit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" _UpperCAmelCase = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) return rename_keys def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=False )-> List[str]: """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: _UpperCAmelCase = '' else: _UpperCAmelCase = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _UpperCAmelCase = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) _UpperCAmelCase = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase = in_proj_weight[ : config.hidden_size, : ] _UpperCAmelCase = in_proj_bias[: config.hidden_size] _UpperCAmelCase = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _UpperCAmelCase = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] _UpperCAmelCase = in_proj_weight[ -config.hidden_size :, : ] _UpperCAmelCase = in_proj_bias[-config.hidden_size :] def __A ( __lowerCAmelCase )-> Optional[Any]: """simple docstring""" _UpperCAmelCase = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(__lowerCAmelCase , __lowerCAmelCase ) def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> int: """simple docstring""" _UpperCAmelCase = dct.pop(__lowerCAmelCase ) _UpperCAmelCase = val def __A ( )-> str: """simple docstring""" _UpperCAmelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' _UpperCAmelCase = Image.open(requests.get(__lowerCAmelCase , stream=__lowerCAmelCase ).raw ) return im @torch.no_grad() def __A ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=True )-> List[str]: """simple docstring""" _UpperCAmelCase = ViTConfig() # patch_size if model_name[-1] == "8": _UpperCAmelCase = 8 # set labels if required if not base_model: _UpperCAmelCase = 1_000 _UpperCAmelCase = 'huggingface/label-files' _UpperCAmelCase = 'imagenet-1k-id2label.json' _UpperCAmelCase = json.load(open(hf_hub_download(__lowerCAmelCase , __lowerCAmelCase , repo_type='dataset' ) , 'r' ) ) _UpperCAmelCase = {int(__lowerCAmelCase ): v for k, v in idalabel.items()} _UpperCAmelCase = idalabel _UpperCAmelCase = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: _UpperCAmelCase = 384 _UpperCAmelCase = 1_536 _UpperCAmelCase = 12 _UpperCAmelCase = 6 # load original model from torch hub _UpperCAmelCase = torch.hub.load('facebookresearch/dino:main' , __lowerCAmelCase ) original_model.eval() # load state_dict of original model, remove and rename some keys _UpperCAmelCase = original_model.state_dict() if base_model: remove_classification_head_(__lowerCAmelCase ) _UpperCAmelCase = create_rename_keys(__lowerCAmelCase , base_model=__lowerCAmelCase ) for src, dest in rename_keys: rename_key(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) read_in_q_k_v(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # load HuggingFace model if base_model: _UpperCAmelCase = ViTModel(__lowerCAmelCase , add_pooling_layer=__lowerCAmelCase ).eval() else: _UpperCAmelCase = ViTForImageClassification(__lowerCAmelCase ).eval() model.load_state_dict(__lowerCAmelCase ) # Check outputs on an image, prepared by ViTImageProcessor _UpperCAmelCase = ViTImageProcessor() _UpperCAmelCase = image_processor(images=prepare_img() , return_tensors='pt' ) _UpperCAmelCase = encoding['pixel_values'] _UpperCAmelCase = model(__lowerCAmelCase ) if base_model: _UpperCAmelCase = original_model(__lowerCAmelCase ) assert torch.allclose(__lowerCAmelCase , outputs.last_hidden_state[:, 0, :] , atol=1E-1 ) else: _UpperCAmelCase = original_model(__lowerCAmelCase ) assert logits.shape == outputs.logits.shape assert torch.allclose(__lowerCAmelCase , outputs.logits , atol=1E-3 ) Path(__lowerCAmelCase ).mkdir(exist_ok=__lowerCAmelCase ) print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowerCAmelCase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowerCAmelCase ) if __name__ == "__main__": _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''dino_vitb16''', type=str, help='''Name of the model trained with DINO 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( '''--base_model''', action='''store_true''', help='''Whether to only convert the base model (no projection head weights).''', ) parser.set_defaults(base_model=True) _a = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
39
import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class __lowerCamelCase : """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=13 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=16 , UpperCAmelCase=[1, 2, 1] , UpperCAmelCase=[2, 2, 4] , UpperCAmelCase=2 , UpperCAmelCase=2.0 , UpperCAmelCase=True , UpperCAmelCase=0.0 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase="gelu" , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=True , UpperCAmelCase=10 , UpperCAmelCase=8 , UpperCAmelCase=["stage1", "stage2", "stage3"] , UpperCAmelCase=[1, 2, 3] , ): """simple docstring""" _UpperCAmelCase = parent _UpperCAmelCase = batch_size _UpperCAmelCase = image_size _UpperCAmelCase = patch_size _UpperCAmelCase = num_channels _UpperCAmelCase = embed_dim _UpperCAmelCase = depths _UpperCAmelCase = num_heads _UpperCAmelCase = window_size _UpperCAmelCase = mlp_ratio _UpperCAmelCase = qkv_bias _UpperCAmelCase = hidden_dropout_prob _UpperCAmelCase = attention_probs_dropout_prob _UpperCAmelCase = drop_path_rate _UpperCAmelCase = hidden_act _UpperCAmelCase = use_absolute_embeddings _UpperCAmelCase = patch_norm _UpperCAmelCase = layer_norm_eps _UpperCAmelCase = initializer_range _UpperCAmelCase = is_training _UpperCAmelCase = scope _UpperCAmelCase = use_labels _UpperCAmelCase = type_sequence_label_size _UpperCAmelCase = encoder_stride _UpperCAmelCase = out_features _UpperCAmelCase = out_indices def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _UpperCAmelCase = None if self.use_labels: _UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase = self.get_config() return config, pixel_values, labels def UpperCamelCase ( self ): """simple docstring""" return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinModel(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _UpperCAmelCase = model(UpperCAmelCase ) _UpperCAmelCase = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) _UpperCAmelCase = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinBackbone(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _UpperCAmelCase = model(UpperCAmelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [13, 16, 16, 16] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , [16, 32, 64] ) # verify ValueError with self.parent.assertRaises(UpperCAmelCase ): _UpperCAmelCase = ['stem'] _UpperCAmelCase = MaskFormerSwinBackbone(config=UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.prepare_config_and_inputs() _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = config_and_inputs _UpperCAmelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class __lowerCamelCase ( snake_case__ , snake_case__ , unittest.TestCase): """simple docstring""" UpperCamelCase__ = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) UpperCamelCase__ = {"feature-extraction": MaskFormerSwinModel} if is_torch_available() else {} UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinModelTester(self ) _UpperCAmelCase = ConfigTester(self , config_class=UpperCAmelCase , embed_dim=37 ) @require_torch_multi_gpu @unittest.skip( reason=( '`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with' ' `nn.DataParallel`' ) ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self ): """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCamelCase ( self ): """simple docstring""" return def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*UpperCAmelCase ) @unittest.skip('Swin does not use inputs_embeds' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip('Swin does not support feedforward chunking' ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(UpperCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _UpperCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase , nn.Linear ) ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCAmelCase = model_class(UpperCAmelCase ) _UpperCAmelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCAmelCase = [*signature.parameters.keys()] _UpperCAmelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase ) @unittest.skip(reason='MaskFormerSwin is only used as backbone and doesn\'t support output_attentions' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip(reason='MaskFormerSwin is only used as an internal backbone' ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() with torch.no_grad(): _UpperCAmelCase = model(**self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) _UpperCAmelCase = outputs.hidden_states _UpperCAmelCase = getattr( self.model_tester , 'expected_num_hidden_layers' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(UpperCAmelCase ) , UpperCAmelCase ) # Swin has a different seq_length _UpperCAmelCase = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) _UpperCAmelCase = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = 3 _UpperCAmelCase = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) _UpperCAmelCase = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) _UpperCAmelCase = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) _UpperCAmelCase = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _UpperCAmelCase = True self.check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , (padded_height, padded_width) ) @unittest.skip(reason='MaskFormerSwin doesn\'t have pretrained checkpoints' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin' ) def UpperCamelCase ( self ): """simple docstring""" pass @unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin' ) def UpperCamelCase ( self ): """simple docstring""" pass def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(UpperCAmelCase ): _UpperCAmelCase = 0 return t def check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase={} ): with torch.no_grad(): _UpperCAmelCase = model(**UpperCAmelCase , return_dict=UpperCAmelCase , **UpperCAmelCase ) _UpperCAmelCase = model(**UpperCAmelCase , return_dict=UpperCAmelCase , **UpperCAmelCase ).to_tuple() def recursive_check(UpperCAmelCase , UpperCAmelCase ): if isinstance(UpperCAmelCase , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(UpperCAmelCase , UpperCAmelCase ): recursive_check(UpperCAmelCase , UpperCAmelCase ) elif isinstance(UpperCAmelCase , UpperCAmelCase ): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values() ): recursive_check(UpperCAmelCase , UpperCAmelCase ) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(UpperCAmelCase ) , set_nan_tensor_to_zero(UpperCAmelCase ) , atol=1e-5 ) , msg=( 'Tuple and dict output are not equal. Difference:' F""" {torch.max(torch.abs(tuple_object - dict_object ) )}. Tuple has `nan`:""" F""" {torch.isnan(UpperCAmelCase ).any()} and `inf`: {torch.isinf(UpperCAmelCase )}. Dict has""" F""" `nan`: {torch.isnan(UpperCAmelCase ).any()} and `inf`: {torch.isinf(UpperCAmelCase )}.""" ) , ) recursive_check(UpperCAmelCase , UpperCAmelCase ) for model_class in self.all_model_classes: _UpperCAmelCase = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , {'output_hidden_states': True} ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) _UpperCAmelCase = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) check_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , {'output_hidden_states': True} ) @require_torch class __lowerCamelCase ( unittest.TestCase , snake_case__): """simple docstring""" UpperCamelCase__ = (MaskFormerSwinBackbone,) if is_torch_available() else () UpperCamelCase__ = MaskFormerSwinConfig def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = MaskFormerSwinModelTester(self ) def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase , _UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase = inputs_dict['pixel_values'].shape[0] for backbone_class in self.all_model_classes: _UpperCAmelCase = backbone_class(UpperCAmelCase ) backbone.to(UpperCAmelCase ) backbone.eval() _UpperCAmelCase = backbone(**UpperCAmelCase ) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , UpperCAmelCase ) self.assertTrue(len(outputs.feature_maps ) == len(backbone.channels ) ) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels ): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels) ) self.assertIsNone(outputs.hidden_states ) self.assertIsNone(outputs.attentions ) # Test output_hidden_states=True _UpperCAmelCase = backbone(**UpperCAmelCase , output_hidden_states=UpperCAmelCase ) self.assertIsNotNone(outputs.hidden_states ) self.assertTrue(len(outputs.hidden_states ) , len(backbone.stage_names ) ) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels ): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels) ) # Test output_attentions=True if self.has_attentions: _UpperCAmelCase = backbone(**UpperCAmelCase , output_attentions=UpperCAmelCase ) self.assertIsNotNone(outputs.attentions )
39
1
# This script creates a super tiny model that is useful inside tests, when we just want to test that # the machinery works, without needing to the check the quality of the outcomes. # # This version creates a tiny model through reduction of a normal pre-trained model, but keeping the # full vocab, merges file, and thus also resulting in a larger model due to a large vocab size. # This gives ~3MB in total for all files. # # If you want a 50 times smaller than this see `fsmt-make-super-tiny-model.py`, which is slightly more complicated # # # It will be used then as "stas/tiny-wmt19-en-de" # Build from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration _A : Dict = 'facebook/wmt19-en-de' _A : Dict = FSMTTokenizer.from_pretrained(mname) # get the correct vocab sizes, etc. from the master model _A : Dict = FSMTConfig.from_pretrained(mname) config.update( dict( d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) ) _A : List[Any] = FSMTForConditionalGeneration(config) print(F'''num of params {tiny_model.num_parameters()}''') # Test _A : Optional[Any] = tokenizer(['Making tiny model'], return_tensors='pt') _A : Dict = tiny_model(**batch) print('test output:', len(outputs.logits[0])) # Save _A : Union[str, Any] = 'tiny-wmt19-en-de' tiny_model.half() # makes it smaller tiny_model.save_pretrained(mname_tiny) tokenizer.save_pretrained(mname_tiny) print(F'''Generated {mname_tiny}''') # Upload # transformers-cli upload tiny-wmt19-en-de
265
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _A : Tuple = logging.get_logger(__name__) _A : List[str] = { 'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json', # See all Nat models at https://huggingface.co/models?filter=nat } class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ,lowerCAmelCase_ ): _UpperCAmelCase : Dict = "nat" _UpperCAmelCase : Tuple = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : List[Any] , A : Union[str, Any]=4 , A : str=3 , A : List[Any]=6_4 , A : Optional[Any]=[3, 4, 6, 5] , A : int=[2, 4, 8, 1_6] , A : Optional[int]=7 , A : List[Any]=3.0 , A : str=True , A : str=0.0 , A : Any=0.0 , A : int=0.1 , A : Tuple="gelu" , A : List[Any]=0.02 , A : str=1e-5 , A : Optional[int]=0.0 , A : Optional[Any]=None , A : Dict=None , **A : str , ) ->Union[str, Any]: super().__init__(**A ) lowerCamelCase__ : Any = patch_size lowerCamelCase__ : Optional[Any] = num_channels lowerCamelCase__ : Any = embed_dim lowerCamelCase__ : str = depths lowerCamelCase__ : Union[str, Any] = len(A ) lowerCamelCase__ : int = num_heads lowerCamelCase__ : Optional[int] = kernel_size lowerCamelCase__ : Optional[int] = mlp_ratio lowerCamelCase__ : List[Any] = qkv_bias lowerCamelCase__ : Tuple = hidden_dropout_prob lowerCamelCase__ : List[str] = attention_probs_dropout_prob lowerCamelCase__ : List[Any] = drop_path_rate lowerCamelCase__ : Union[str, Any] = hidden_act lowerCamelCase__ : Union[str, Any] = layer_norm_eps lowerCamelCase__ : List[Any] = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase__ : str = int(embed_dim * 2 ** (len(A ) - 1) ) lowerCamelCase__ : Dict = layer_scale_init_value lowerCamelCase__ : Dict = ['''stem'''] + [F"stage{idx}" for idx in range(1 , len(A ) + 1 )] lowerCamelCase__ , lowerCamelCase__ : str = get_aligned_output_features_output_indices( out_features=A , out_indices=A , stage_names=self.stage_names )
265
1
'''simple docstring''' def lowercase__ ( __lowercase : int ) -> list[int]: """simple docstring""" if num <= 0: raise ValueError('Input must be a positive integer' ) __UpperCamelCase = [True] * (num + 1) __UpperCamelCase = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , __lowercase ): __UpperCamelCase = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() a__ : str =int(input('''Enter a positive integer: ''').strip()) print(prime_sieve_eratosthenes(user_num))
53
'''simple docstring''' import asyncio import os import re import sys import tempfile import unittest from contextlib import contextmanager from copy import deepcopy from distutils.util import strtobool from enum import Enum from importlib.util import find_spec from pathlib import Path from unittest.mock import patch import pyarrow as pa import pytest import requests from packaging import version from datasets import config if config.PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata def lowercase__ ( __lowercase : List[str] , __lowercase : Union[str, Any]=False ) -> Tuple: """simple docstring""" try: __UpperCamelCase = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __UpperCamelCase = default else: # KEY is set, convert it to True or False. try: __UpperCamelCase = strtobool(__lowercase ) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F'''If set, {key} must be yes or no.''' ) return _value a__ : str =parse_flag_from_env('''RUN_SLOW''', default=False) a__ : Union[str, Any] =parse_flag_from_env('''RUN_REMOTE''', default=False) a__ : List[str] =parse_flag_from_env('''RUN_LOCAL''', default=True) a__ : Optional[int] =parse_flag_from_env('''RUN_PACKAGED''', default=True) # Compression a__ : Any =pytest.mark.skipif(not config.LZ4_AVAILABLE, reason='''test requires lz4''') a__ : Optional[int] =pytest.mark.skipif(not config.PY7ZR_AVAILABLE, reason='''test requires py7zr''') a__ : List[str] =pytest.mark.skipif(not config.ZSTANDARD_AVAILABLE, reason='''test requires zstandard''') # Audio a__ : Any =pytest.mark.skipif( # On Windows and OS X, soundfile installs sndfile find_spec('''soundfile''') is None or version.parse(importlib_metadata.version('''soundfile''')) < version.parse('''0.12.0'''), reason='''test requires sndfile>=0.12.1: \'pip install \"soundfile>=0.12.1\"\'; ''', ) # Beam a__ : Tuple =pytest.mark.skipif( not config.BEAM_AVAILABLE or config.DILL_VERSION >= version.parse('''0.3.2'''), reason='''test requires apache-beam and a compatible dill version''', ) # Dill-cloudpickle compatibility a__ : Union[str, Any] =pytest.mark.skipif( config.DILL_VERSION <= version.parse('''0.3.2'''), reason='''test requires dill>0.3.2 for cloudpickle compatibility''', ) # Windows a__ : int =pytest.mark.skipif( sys.platform == '''win32''', reason='''test should not be run on Windows''', ) def lowercase__ ( __lowercase : Optional[Any] ) -> Optional[int]: """simple docstring""" try: import faiss # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires faiss' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Union[str, Any] ) -> Any: """simple docstring""" try: import regex # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires regex' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Tuple ) -> List[Any]: """simple docstring""" try: import elasticsearch # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires elasticsearch' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Union[str, Any] ) -> Tuple: """simple docstring""" try: import sqlalchemy # noqa except ImportError: __UpperCamelCase = unittest.skip('test requires sqlalchemy' )(__lowercase ) return test_case def lowercase__ ( __lowercase : List[str] ) -> List[str]: """simple docstring""" if not config.TORCH_AVAILABLE: __UpperCamelCase = unittest.skip('test requires PyTorch' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Optional[Any] ) -> List[str]: """simple docstring""" if not config.TF_AVAILABLE: __UpperCamelCase = unittest.skip('test requires TensorFlow' )(__lowercase ) return test_case def lowercase__ ( __lowercase : int ) -> Union[str, Any]: """simple docstring""" if not config.JAX_AVAILABLE: __UpperCamelCase = unittest.skip('test requires JAX' )(__lowercase ) return test_case def lowercase__ ( __lowercase : str ) -> Optional[Any]: """simple docstring""" if not config.PIL_AVAILABLE: __UpperCamelCase = unittest.skip('test requires Pillow' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Dict ) -> Any: """simple docstring""" try: import transformers # noqa F401 except ImportError: return unittest.skip('test requires transformers' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : int ) -> int: """simple docstring""" try: import tiktoken # noqa F401 except ImportError: return unittest.skip('test requires tiktoken' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : str ) -> int: """simple docstring""" try: import spacy # noqa F401 except ImportError: return unittest.skip('test requires spacy' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : str ) -> Any: """simple docstring""" def _require_spacy_model(__lowercase : Any ): try: import spacy # noqa F401 spacy.load(__lowercase ) except ImportError: return unittest.skip('test requires spacy' )(__lowercase ) except OSError: return unittest.skip('test requires spacy model \'{}\''.format(__lowercase ) )(__lowercase ) else: return test_case return _require_spacy_model def lowercase__ ( __lowercase : Union[str, Any] ) -> str: """simple docstring""" try: import pyspark # noqa F401 except ImportError: return unittest.skip('test requires pyspark' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : Optional[int] ) -> Optional[Any]: """simple docstring""" try: import joblibspark # noqa F401 except ImportError: return unittest.skip('test requires joblibspark' )(__lowercase ) else: return test_case def lowercase__ ( __lowercase : List[Any] ) -> List[str]: """simple docstring""" if not _run_slow_tests or _run_slow_tests == 0: __UpperCamelCase = unittest.skip('test is slow' )(__lowercase ) return test_case def lowercase__ ( __lowercase : List[Any] ) -> List[str]: """simple docstring""" if not _run_local_tests or _run_local_tests == 0: __UpperCamelCase = unittest.skip('test is local' )(__lowercase ) return test_case def lowercase__ ( __lowercase : str ) -> List[str]: """simple docstring""" if not _run_packaged_tests or _run_packaged_tests == 0: __UpperCamelCase = unittest.skip('test is packaged' )(__lowercase ) return test_case def lowercase__ ( __lowercase : Optional[int] ) -> Any: """simple docstring""" if not _run_remote_tests or _run_remote_tests == 0: __UpperCamelCase = unittest.skip('test requires remote' )(__lowercase ) return test_case def lowercase__ ( *__lowercase : Optional[Any] ) -> Tuple: """simple docstring""" def decorate(cls : int ): for name, fn in cls.__dict__.items(): if callable(__lowercase ) and name.startswith('test' ): for decorator in decorators: __UpperCamelCase = decorator(__lowercase ) setattr(cls , __lowercase , __lowercase ) return cls return decorate class snake_case ( __lowerCamelCase ): """simple docstring""" pass class snake_case ( __lowerCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Any =0 SCREAMING_SNAKE_CASE_ : List[Any] =1 SCREAMING_SNAKE_CASE_ : Union[str, Any] =2 @contextmanager def lowercase__ ( __lowercase : List[str]=OfflineSimulationMode.CONNECTION_FAILS , __lowercase : Dict=1e-16 ) -> List[Any]: """simple docstring""" __UpperCamelCase = requests.Session().request def timeout_request(__lowercase : List[Any] , __lowercase : Tuple , __lowercase : List[Any] , **__lowercase : List[str] ): # Change the url to an invalid url so that the connection hangs __UpperCamelCase = 'https://10.255.255.1' if kwargs.get('timeout' ) is None: raise RequestWouldHangIndefinitelyError( F'''Tried a call to {url} in offline mode with no timeout set. Please set a timeout.''' ) __UpperCamelCase = timeout try: return online_request(__lowercase , __lowercase , **__lowercase ) except Exception as e: # The following changes in the error are just here to make the offline timeout error prettier __UpperCamelCase = url __UpperCamelCase = e.args[0] __UpperCamelCase = (max_retry_error.args[0].replace('10.255.255.1' , F'''OfflineMock[{url}]''' ),) __UpperCamelCase = (max_retry_error,) raise def raise_connection_error(__lowercase : int , __lowercase : List[str] , **__lowercase : Union[str, Any] ): raise requests.ConnectionError('Offline mode is enabled.' , request=__lowercase ) if mode is OfflineSimulationMode.CONNECTION_FAILS: with patch('requests.Session.send' , __lowercase ): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 with patch('requests.Session.request' , __lowercase ): yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch('datasets.config.HF_DATASETS_OFFLINE' , __lowercase ): yield else: raise ValueError('Please use a value from the OfflineSimulationMode enum.' ) @contextmanager def lowercase__ ( *__lowercase : Any , **__lowercase : Dict ) -> Dict: """simple docstring""" __UpperCamelCase = str(Path().resolve() ) with tempfile.TemporaryDirectory(*__lowercase , **__lowercase ) as tmp_dir: try: os.chdir(__lowercase ) yield finally: os.chdir(__lowercase ) @contextmanager def lowercase__ ( ) -> Optional[Any]: """simple docstring""" import gc gc.collect() __UpperCamelCase = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory > 0, "Arrow memory didn't increase." @contextmanager def lowercase__ ( ) -> Optional[Any]: """simple docstring""" import gc gc.collect() __UpperCamelCase = pa.total_allocated_bytes() yield assert pa.total_allocated_bytes() - previous_allocated_memory <= 0, "Arrow memory wasn't expected to increase." def lowercase__ ( __lowercase : List[str] , __lowercase : int ) -> Union[str, Any]: """simple docstring""" return deepcopy(__lowercase ).integers(0 , 100 , 10 ).tolist() == deepcopy(__lowercase ).integers(0 , 100 , 10 ).tolist() def lowercase__ ( __lowercase : str ) -> List[str]: """simple docstring""" import decorator from requests.exceptions import HTTPError def _wrapper(__lowercase : List[Any] , *__lowercase : Tuple , **__lowercase : Union[str, Any] ): try: return func(*__lowercase , **__lowercase ) except HTTPError as err: if str(__lowercase ).startswith('500' ) or str(__lowercase ).startswith('502' ): pytest.xfail(str(__lowercase ) ) raise err return decorator.decorator(_wrapper , __lowercase ) class snake_case : """simple docstring""" def __init__( self : int , __A : Any , __A : str , __A : List[Any] ): __UpperCamelCase = returncode __UpperCamelCase = stdout __UpperCamelCase = stderr async def lowercase__ ( __lowercase : Any , __lowercase : Optional[int] ) -> str: """simple docstring""" while True: __UpperCamelCase = await stream.readline() if line: callback(__lowercase ) else: break async def lowercase__ ( __lowercase : Optional[int] , __lowercase : Union[str, Any]=None , __lowercase : Any=None , __lowercase : Optional[Any]=None , __lowercase : int=False , __lowercase : List[Any]=False ) -> _RunOutput: """simple docstring""" if echo: print('\nRunning: ' , ' '.join(__lowercase ) ) __UpperCamelCase = await asyncio.create_subprocess_exec( cmd[0] , *cmd[1:] , stdin=__lowercase , stdout=asyncio.subprocess.PIPE , stderr=asyncio.subprocess.PIPE , env=__lowercase , ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __UpperCamelCase = [] __UpperCamelCase = [] def tee(__lowercase : Optional[Any] , __lowercase : Dict , __lowercase : List[str] , __lowercase : Tuple="" ): __UpperCamelCase = line.decode('utf-8' ).rstrip() sink.append(__lowercase ) if not quiet: print(__lowercase , __lowercase , file=__lowercase ) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ _read_stream(p.stdout , lambda __lowercase : tee(__lowercase , __lowercase , sys.stdout , label='stdout:' ) ), _read_stream(p.stderr , lambda __lowercase : tee(__lowercase , __lowercase , sys.stderr , label='stderr:' ) ), ] , timeout=__lowercase , ) return _RunOutput(await p.wait() , __lowercase , __lowercase ) def lowercase__ ( __lowercase : Dict , __lowercase : Any=None , __lowercase : int=None , __lowercase : int=180 , __lowercase : int=False , __lowercase : str=True ) -> _RunOutput: """simple docstring""" __UpperCamelCase = asyncio.get_event_loop() __UpperCamelCase = loop.run_until_complete( _stream_subprocess(__lowercase , env=__lowercase , stdin=__lowercase , timeout=__lowercase , quiet=__lowercase , echo=__lowercase ) ) __UpperCamelCase = ' '.join(__lowercase ) if result.returncode > 0: __UpperCamelCase = '\n'.join(result.stderr ) raise RuntimeError( F'''\'{cmd_str}\' failed with returncode {result.returncode}\n\n''' F'''The combined stderr from workers follows:\n{stderr}''' ) # check that the subprocess actually did run and produced some output, should the test rely on # the remote side to do the testing if not result.stdout and not result.stderr: raise RuntimeError(F'''\'{cmd_str}\' produced no output.''' ) return result def lowercase__ ( ) -> List[str]: """simple docstring""" __UpperCamelCase = os.environ.get('PYTEST_XDIST_WORKER' , 'gw0' ) __UpperCamelCase = re.sub(R'^gw' , '' , __lowercase , 0 , re.M ) return int(__lowercase ) def lowercase__ ( ) -> List[Any]: """simple docstring""" __UpperCamelCase = 29500 __UpperCamelCase = pytest_xdist_worker_id() return port + uniq_delta
53
1
'''simple docstring''' import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process _lowerCamelCase : Tuple = logging.getLogger(__name__) _lowerCamelCase : Union[str, Any] = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) _lowerCamelCase : Dict = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowerCAmelCase = field( default=A__ , metadata={ '''help''': ( '''The model checkpoint for weights initialization. Leave None if you want to train a model from''' ''' scratch.''' ) } , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(A__ )} , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''The input training data file (a text file).'''} ) __lowerCAmelCase = field( default=A__ , metadata={ '''help''': ( '''The input training data files (multiple files in glob format). ''' '''Very often splitting large files to smaller files can prevent tokenizer going out of memory''' ) } , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''An optional input train ref data file for whole word mask in Chinese.'''} , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''An optional input eval ref data file for whole word mask in Chinese.'''} , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''Whether distinct lines of text in the dataset are to be handled as distinct sequences.'''} , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''Train with masked-language modeling loss instead of language modeling.'''} ) __lowerCAmelCase = field(default=A__ , metadata={'''help''': '''Whether ot not to use whole word mask.'''} ) __lowerCAmelCase = field( default=0.15 , metadata={'''help''': '''Ratio of tokens to mask for masked language modeling loss'''} ) __lowerCAmelCase = field( default=1 / 6 , metadata={ '''help''': ( '''Ratio of length of a span of masked tokens to surrounding context length for permutation language''' ''' modeling.''' ) } , ) __lowerCAmelCase = field( default=5 , metadata={'''help''': '''Maximum length of a span of masked tokens for permutation language modeling.'''} ) __lowerCAmelCase = field( default=-1 , metadata={ '''help''': ( '''Optional input sequence length after tokenization.''' '''The training dataset will be truncated in block of this size for training.''' '''Default to the model max input length for single sentence inputs (take into account special tokens).''' ) } , ) __lowerCAmelCase = field( default=A__ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) def __a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = False , UpperCAmelCase = None , ) ->Tuple: """simple docstring""" def _dataset(UpperCAmelCase , UpperCAmelCase=None ): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError("""You need to set world whole masking and mlm to True for Chinese Whole Word Mask""" ) return LineByLineWithRefDataset( tokenizer=UpperCAmelCase , file_path=UpperCAmelCase , block_size=args.block_size , ref_path=UpperCAmelCase , ) return LineByLineTextDataset(tokenizer=UpperCAmelCase , file_path=UpperCAmelCase , block_size=args.block_size ) else: return TextDataset( tokenizer=UpperCAmelCase , file_path=UpperCAmelCase , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=UpperCAmelCase , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file ) elif args.train_data_files: return ConcatDataset([_dataset(UpperCAmelCase ) for f in glob(args.train_data_files )] ) else: return _dataset(args.train_data_file , args.train_ref_file ) def __a ( ) ->Optional[Any]: """simple docstring""" A = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A , A , A = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( """Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file """ """or remove the --do_eval argument.""" ) if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" """ --overwrite_output_dir to overcome.""" ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s""" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("""Training/evaluation parameters %s""" , UpperCAmelCase ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: A = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: A = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: A = CONFIG_MAPPING[model_args.model_type]() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.tokenizer_name: A = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: A = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: raise ValueError( """You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another""" """ script, save it,and load it from here, using --tokenizer_name""" ) if model_args.model_name_or_path: A = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=UpperCAmelCase , cache_dir=model_args.cache_dir , ) else: logger.info("""Training new model from scratch""" ) A = AutoModelWithLMHead.from_config(UpperCAmelCase ) model.resize_token_embeddings(len(UpperCAmelCase ) ) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( """BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the""" """--mlm flag (masked language modeling).""" ) if data_args.block_size <= 0: A = tokenizer.max_len # Our input block size will be the max possible for the model else: A = min(data_args.block_size , tokenizer.max_len ) # Get datasets A = ( get_dataset(UpperCAmelCase , tokenizer=UpperCAmelCase , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) A = ( get_dataset(UpperCAmelCase , tokenizer=UpperCAmelCase , evaluate=UpperCAmelCase , cache_dir=model_args.cache_dir ) if training_args.do_eval else None ) if config.model_type == "xlnet": A = DataCollatorForPermutationLanguageModeling( tokenizer=UpperCAmelCase , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: A = DataCollatorForWholeWordMask( tokenizer=UpperCAmelCase , mlm_probability=data_args.mlm_probability ) else: A = DataCollatorForLanguageModeling( tokenizer=UpperCAmelCase , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer A = Trainer( model=UpperCAmelCase , args=UpperCAmelCase , data_collator=UpperCAmelCase , train_dataset=UpperCAmelCase , eval_dataset=UpperCAmelCase , prediction_loss_only=UpperCAmelCase , ) # Training if training_args.do_train: A = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ) else None ) trainer.train(model_path=UpperCAmelCase ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation A = {} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) A = trainer.evaluate() A = math.exp(eval_output["""eval_loss"""] ) A = {"""perplexity""": perplexity} A = os.path.join(training_args.output_dir , """eval_results_lm.txt""" ) if trainer.is_world_master(): with open(UpperCAmelCase , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , UpperCAmelCase , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) results.update(UpperCAmelCase ) return results def __a ( UpperCAmelCase ) ->str: """simple docstring""" main() if __name__ == "__main__": main()
337
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCamelCase : Optional[Any] = { 'configuration_swinv2': ['SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Swinv2Config'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : List[str] = [ 'SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST', 'Swinv2ForImageClassification', 'Swinv2ForMaskedImageModeling', 'Swinv2Model', 'Swinv2PreTrainedModel', ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys _lowerCamelCase : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
337
1
from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class a : """simple docstring""" a : int a : Node | None = None a : Node | None = None def lowerCamelCase__ ( ): __UpperCAmelCase : Dict = Node(1 ) __UpperCAmelCase : Tuple = Node(2 ) __UpperCAmelCase : Any = Node(3 ) __UpperCAmelCase : List[str] = Node(4 ) __UpperCAmelCase : List[str] = Node(5 ) return tree def lowerCamelCase__ ( __lowerCamelCase : Node | None ): return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def lowerCamelCase__ ( __lowerCamelCase : Node | None ): return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def lowerCamelCase__ ( __lowerCamelCase : Node | None ): return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def lowerCamelCase__ ( __lowerCamelCase : Node | None ): return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def lowerCamelCase__ ( __lowerCamelCase : Node | None ): __UpperCAmelCase : list[Any] = [] if root is None: return output __UpperCAmelCase : List[Any] = deque([root] ) while process_queue: __UpperCAmelCase : int = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def lowerCamelCase__ ( __lowerCamelCase : Node | None , __lowerCamelCase : int ): __UpperCAmelCase : list[Any] = [] def populate_output(__lowerCamelCase : Node | None , __lowerCamelCase : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(__lowerCamelCase , __lowerCamelCase ) return output def lowerCamelCase__ ( __lowerCamelCase : Node | None , __lowerCamelCase : int ): __UpperCAmelCase : list[Any] = [] def populate_output(__lowerCamelCase : Node | None , __lowerCamelCase : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(__lowerCamelCase , __lowerCamelCase ) return output def lowerCamelCase__ ( __lowerCamelCase : Node | None ): if root is None: return [] __UpperCAmelCase : list[Sequence[Node | None]] = [] __UpperCAmelCase : Tuple = 0 __UpperCAmelCase : Tuple = height(__lowerCamelCase ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(__lowerCamelCase , __lowerCamelCase ) ) __UpperCAmelCase : Tuple = 1 else: output.append(get_nodes_from_right_to_left(__lowerCamelCase , __lowerCamelCase ) ) __UpperCAmelCase : int = 0 return output def lowerCamelCase__ ( ): # Main function for testing. __UpperCAmelCase : Optional[int] = make_tree() print(f"""In-order Traversal: {inorder(__lowerCamelCase )}""" ) print(f"""Pre-order Traversal: {preorder(__lowerCamelCase )}""" ) print(f"""Post-order Traversal: {postorder(__lowerCamelCase )}""" , """\n""" ) print(f"""Height of Tree: {height(__lowerCamelCase )}""" , """\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(__lowerCamelCase ) , """\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 , height(__lowerCamelCase ) + 1 ): print(f"""Level {level}:""" , get_nodes_from_left_to_right(__lowerCamelCase , level=__lowerCamelCase ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(__lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
114
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a : Union[str, Any] = {"configuration_timm_backbone": ["TimmBackboneConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Tuple = ["TimmBackbone"] if TYPE_CHECKING: from .configuration_timm_backbone import TimmBackboneConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timm_backbone import TimmBackbone else: import sys a : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
114
1
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _UpperCAmelCase = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase = ['MMBTForClassification', 'MMBTModel', 'ModalEmbeddings'] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys _UpperCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
328
import math def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ ) -> List[str]: if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(UpperCamelCase_ ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("This should never happen" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. _UpperCAmelCase = 'Enter the base and the power separated by a comma: ' _UpperCAmelCase , _UpperCAmelCase = map(int, input(prompt).split(',')) _UpperCAmelCase , _UpperCAmelCase = map(int, input(prompt).split(',')) # We find the log of each number, using the function res(), which takes two # arguments. _UpperCAmelCase = res(xa, ya) _UpperCAmelCase = res(xa, ya) # We check for the largest number if resa > resa: print('Largest number is', xa, '^', ya) elif resa > resa: print('Largest number is', xa, '^', ya) else: print('Both are equal')
328
1
'''simple docstring''' class _lowercase : def __init__( self: Optional[Any] ): lowerCamelCase__ : dict[str, TrieNode] = {} # Mapping from char to TrieNode lowerCamelCase__ : List[str] = False def lowerCamelCase_ ( self: str , UpperCamelCase__: list[str] ): for word in words: self.insert(UpperCamelCase__ ) def lowerCamelCase_ ( self: List[str] , UpperCamelCase__: str ): lowerCamelCase__ : List[Any] = self for char in word: if char not in curr.nodes: lowerCamelCase__ : Tuple = TrieNode() lowerCamelCase__ : List[Any] = curr.nodes[char] lowerCamelCase__ : Any = True def lowerCamelCase_ ( self: Union[str, Any] , UpperCamelCase__: str ): lowerCamelCase__ : Union[str, Any] = self for char in word: if char not in curr.nodes: return False lowerCamelCase__ : Any = curr.nodes[char] return curr.is_leaf def lowerCamelCase_ ( self: str , UpperCamelCase__: str ): def _delete(UpperCamelCase__: TrieNode , UpperCamelCase__: str , UpperCamelCase__: int ) -> bool: if index == len(UpperCamelCase__ ): # If word does not exist if not curr.is_leaf: return False lowerCamelCase__ : str = False return len(curr.nodes ) == 0 lowerCamelCase__ : List[str] = word[index] lowerCamelCase__ : Dict = curr.nodes.get(UpperCamelCase__ ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted lowerCamelCase__ : List[Any] = _delete(UpperCamelCase__ , UpperCamelCase__ , index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self , UpperCamelCase__ , 0 ) def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> None: if node.is_leaf: print(UpperCamelCase , end=""" """ ) for key, value in node.nodes.items(): print_words(UpperCamelCase , word + key ) def SCREAMING_SNAKE_CASE_ () -> bool: lowerCamelCase__ : str = """banana bananas bandana band apple all beast""".split() lowerCamelCase__ : Union[str, Any] = TrieNode() root.insert_many(UpperCamelCase ) # print_words(root, "") assert all(root.find(UpperCamelCase ) 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 SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> None: print(str(UpperCamelCase ) , """works!""" if passes else """doesn't work :(""" ) def SCREAMING_SNAKE_CASE_ () -> None: assert test_trie() def SCREAMING_SNAKE_CASE_ () -> None: print_results("""Testing trie functionality""" , test_trie() ) if __name__ == "__main__": main()
41
import unittest from pathlib import Path from shutil import copyfile from transformers import SPIECE_UNDERLINE, is_sentencepiece_available from transformers.models.speech_to_text import SpeechaTextTokenizer from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin __lowerCamelCase : str = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_sentencepiece_available(): import sentencepiece as sp __lowerCamelCase : Any = 5 __lowerCamelCase : Dict = 10 @require_sentencepiece @require_tokenizers class __snake_case ( lowerCamelCase_ , unittest.TestCase ): lowerCAmelCase_ = SpeechaTextTokenizer lowerCAmelCase_ = False lowerCAmelCase_ = True def __a ( self : Tuple ): """simple docstring""" super().setUp() SCREAMING_SNAKE_CASE__ = sp.SentencePieceProcessor() spm_model.Load(_lowercase ) SCREAMING_SNAKE_CASE__ = ["""<s>""", """<pad>""", """</s>""", """<unk>"""] vocab += [spm_model.IdToPiece(id_ ) for id_ in range(len(_lowercase ) )] SCREAMING_SNAKE_CASE__ = dict(zip(_lowercase , range(len(_lowercase ) ) ) ) SCREAMING_SNAKE_CASE__ = Path(self.tmpdirname ) save_json(_lowercase , save_dir / VOCAB_FILES_NAMES["""vocab_file"""] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(_lowercase , save_dir / VOCAB_FILES_NAMES["""spm_file"""] ) SCREAMING_SNAKE_CASE__ = SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = """<pad>""" SCREAMING_SNAKE_CASE__ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_lowercase ) , _lowercase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_lowercase ) , _lowercase ) def __a ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<s>""" ) self.assertEqual(vocab_keys[1] , """<pad>""" ) self.assertEqual(vocab_keys[-1] , """j""" ) self.assertEqual(len(_lowercase ) , 10_01 ) def __a ( self : List[Any] ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 10_01 ) def __a ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE__ = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(_lowercase , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_lowercase ) , [2_89, 50, 14, 1_74, 3_86] , ) SCREAMING_SNAKE_CASE__ = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( _lowercase , [SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """."""] , ) SCREAMING_SNAKE_CASE__ = tokenizer.convert_tokens_to_ids(_lowercase ) self.assertListEqual(_lowercase , [12, 25, 88, 59, 28, 23, 11, 4, 6_06, 3_51, 3_51, 3_51, 7, 16, 70, 50, 76, 84, 10, 4, 8] ) SCREAMING_SNAKE_CASE__ = tokenizer.convert_ids_to_tokens(_lowercase ) self.assertListEqual( _lowercase , [SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """."""] , ) @slow def __a ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = {"""input_ids""": [[37_91, 7_97, 31, 11, 64, 7_97, 31, 24_29, 4_33, 12, 11_76, 12, 20, 7_86, 9_15, 1_42, 24_13, 2_40, 37, 32_38, 7_97, 31, 11, 35, 93, 9_15, 1_42, 24_13, 2_40, 37, 55_40, 5_67, 12_76, 93, 37, 6_10, 40, 62, 4_55, 6_57, 10_42, 1_23, 7_80, 1_77, 37, 3_09, 2_41, 12_98, 5_14, 20, 2_92, 27_37, 1_14, 24_69, 2_41, 85, 64, 3_02, 5_48, 5_28, 4_23, 4, 5_09, 4_06, 4_23, 37, 6_01, 4, 7_77, 3_02, 5_48, 5_28, 4_23, 2_84, 4, 33_88, 5_11, 4_59, 4, 35_55, 40, 3_21, 3_02, 7_05, 4, 33_88, 5_11, 5_83, 3_26, 5, 5, 5, 62, 33_10, 5_60, 1_77, 26_80, 2_17, 15_08, 32, 31, 8_53, 4_18, 64, 5_83, 5_11, 16_05, 62, 35, 93, 5_60, 1_77, 26_80, 2_17, 15_08, 15_21, 64, 5_83, 5_11, 5_19, 62, 20, 15_15, 7_64, 20, 1_49, 2_61, 56_25, 79_72, 20, 55_40, 5_67, 12_76, 93, 39_25, 16_75, 11, 15, 8_02, 79_72, 5_76, 2_17, 15_08, 11, 35, 93, 12_53, 24_41, 15, 2_89, 6_52, 31, 4_16, 3_21, 38_42, 1_15, 40, 9_11, 8, 4_76, 6_19, 4, 3_80, 1_42, 4_23, 3_35, 2_40, 35, 93, 2_64, 8, 11, 3_35, 5_69, 4_20, 1_63, 5, 2], [2_60, 5_48, 5_28, 4_23, 20, 4_51, 20, 26_81, 11_53, 34_34, 20, 55_40, 37, 5_67, 1_26, 12_53, 24_41, 33_76, 4_49, 2_10, 4_31, 15_63, 1_77, 7_67, 55_40, 11, 12_03, 4_72, 11, 29_53, 6_85, 2_85, 3_64, 7_06, 11_53, 20, 67_99, 20, 28_69, 20, 44_64, 1_26, 40, 24_29, 20, 10_40, 8_66, 26_64, 4_18, 20, 3_18, 20, 17_26, 1_86, 20, 2_65, 5_22, 35, 93, 21_91, 46_34, 20, 10_40, 12, 67_99, 15, 2_28, 23_56, 1_42, 31, 11, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_75, 26_66, 6_84, 15_82, 11_76, 12, 6_27, 1_49, 6_19, 20, 49_02, 5_63, 11, 20, 1_49, 2_61, 34_20, 23_56, 1_74, 1_42, 47_14, 1_31, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_lowercase , model_name="""facebook/s2t-small-mustc-en-de-st""" , revision="""a14f04cf0776c02f62a8cb800cf7909e15ea23ad""" , ) @require_sentencepiece class __snake_case ( unittest.TestCase ): lowerCAmelCase_ = "valhalla/s2t_mustc_multilinguial_medium" lowerCAmelCase_ = "C'est trop cool" lowerCAmelCase_ = "Esto es genial" @classmethod def __a ( cls : Any ): """simple docstring""" SCREAMING_SNAKE_CASE__ = SpeechaTextTokenizer.from_pretrained(cls.checkpoint_name ) return cls def __a ( self : Dict ): """simple docstring""" self.assertEqual(self.tokenizer.lang_code_to_id["""pt"""] , 4 ) self.assertEqual(self.tokenizer.lang_code_to_id["""ru"""] , 6 ) self.assertEqual(self.tokenizer.lang_code_to_id["""it"""] , 9 ) self.assertEqual(self.tokenizer.lang_code_to_id["""de"""] , 11 ) def __a ( self : Union[str, Any] ): """simple docstring""" self.assertEqual(self.tokenizer.vocab_size , 1_00_00 ) def __a ( self : int ): """simple docstring""" self.assertIn(_lowercase , self.tokenizer.all_special_ids ) SCREAMING_SNAKE_CASE__ = [ES_CODE, 4, 16_01, 47, 76_47, 2] SCREAMING_SNAKE_CASE__ = self.tokenizer.decode(_lowercase , skip_special_tokens=_lowercase ) SCREAMING_SNAKE_CASE__ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_lowercase ) self.assertEqual(_lowercase , _lowercase ) self.assertNotIn(self.tokenizer.eos_token , _lowercase ) def __a ( self : Any ): """simple docstring""" SCREAMING_SNAKE_CASE__ = """fr""" SCREAMING_SNAKE_CASE__ = self.tokenizer(self.french_text ).input_ids self.assertEqual(encoded[0] , _lowercase ) self.assertEqual(encoded[-1] , self.tokenizer.eos_token_id ) def __a ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = """fr""" self.assertListEqual(self.tokenizer.prefix_tokens , [FR_CODE] ) SCREAMING_SNAKE_CASE__ = """es""" self.assertListEqual(self.tokenizer.prefix_tokens , [ES_CODE] )
219
0
import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def __lowerCamelCase ( snake_case__ ,snake_case__ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = args.log_outputs _SCREAMING_SNAKE_CASE = """_""".join(args.dataset.split("""/""" ) + [args.config, args.split] ) # load metric _SCREAMING_SNAKE_CASE = load_metric("""wer""" ) _SCREAMING_SNAKE_CASE = load_metric("""cer""" ) # compute metrics _SCREAMING_SNAKE_CASE = wer.compute(references=result["""target"""] ,predictions=result["""prediction"""] ) _SCREAMING_SNAKE_CASE = cer.compute(references=result["""target"""] ,predictions=result["""prediction"""] ) # print & log results _SCREAMING_SNAKE_CASE = F'WER: {wer_result}\nCER: {cer_result}' print(snake_case_ ) with open(F'{dataset_id}_eval_results.txt' ,"""w""" ) as f: f.write(snake_case_ ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: _SCREAMING_SNAKE_CASE = F'log_{dataset_id}_predictions.txt' _SCREAMING_SNAKE_CASE = F'log_{dataset_id}_targets.txt' with open(snake_case_ ,"""w""" ) as p, open(snake_case_ ,"""w""" ) as t: # mapping function to write output def write_to_file(snake_case__ ,snake_case__ ): p.write(F'{i}' + """\n""" ) p.write(batch["""prediction"""] + """\n""" ) t.write(F'{i}' + """\n""" ) t.write(batch["""target"""] + """\n""" ) result.map(snake_case_ ,with_indices=snake_case_ ) def __lowerCamelCase ( snake_case__ ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE = """[,?.!\-\;\:\"“%‘”�—’…–]""" # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training _SCREAMING_SNAKE_CASE = re.sub(snake_case_ ,"""""" ,text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! _SCREAMING_SNAKE_CASE = ["""\n\n""", """\n""", """ """, """ """] for t in token_sequences_to_ignore: _SCREAMING_SNAKE_CASE = """ """.join(text.split(snake_case_ ) ) return text def __lowerCamelCase ( snake_case__ ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE = load_dataset(args.dataset ,args.config ,split=args.split ,use_auth_token=snake_case_ ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor _SCREAMING_SNAKE_CASE = AutoFeatureExtractor.from_pretrained(args.model_id ) _SCREAMING_SNAKE_CASE = feature_extractor.sampling_rate # resample audio _SCREAMING_SNAKE_CASE = dataset.cast_column("""audio""" ,Audio(sampling_rate=snake_case_ ) ) # load eval pipeline if args.device is None: _SCREAMING_SNAKE_CASE = 0 if torch.cuda.is_available() else -1 _SCREAMING_SNAKE_CASE = pipeline("""automatic-speech-recognition""" ,model=args.model_id ,device=args.device ) # map function to decode audio def map_to_pred(snake_case__ ): _SCREAMING_SNAKE_CASE = asr( batch["""audio"""]["""array"""] ,chunk_length_s=args.chunk_length_s ,stride_length_s=args.stride_length_s ) _SCREAMING_SNAKE_CASE = prediction["""text"""] _SCREAMING_SNAKE_CASE = normalize_text(batch["""sentence"""] ) return batch # run inference on all examples _SCREAMING_SNAKE_CASE = dataset.map(snake_case_ ,remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(snake_case_ ,snake_case_ ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model_id''', type=str, required=True, help='''Model identifier. Should be loadable with 🤗 Transformers''' ) parser.add_argument( '''--dataset''', type=str, required=True, help='''Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets''', ) parser.add_argument( '''--config''', type=str, required=True, help='''Config of the dataset. *E.g.* `\'en\'` for Common Voice''' ) parser.add_argument('''--split''', type=str, required=True, help='''Split of the dataset. *E.g.* `\'test\'`''') parser.add_argument( '''--chunk_length_s''', type=float, default=None, help='''Chunk length in seconds. Defaults to 5 seconds.''' ) parser.add_argument( '''--stride_length_s''', type=float, default=None, help='''Stride of the audio chunks. Defaults to 1 second.''' ) parser.add_argument( '''--log_outputs''', action='''store_true''', help='''If defined, write outputs to log file for analysis.''' ) parser.add_argument( '''--device''', type=int, default=None, help='''The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.''', ) UpperCamelCase = parser.parse_args() main(args)
355
import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaInpaintPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __UpperCAmelCase (_UpperCAmelCase ,unittest.TestCase ): __snake_case : List[str] = KandinskyVaaInpaintPipeline __snake_case : Union[str, Any] = ["image_embeds", "negative_image_embeds", "image", "mask_image"] __snake_case : Tuple = [ "image_embeds", "negative_image_embeds", "image", "mask_image", ] __snake_case : str = [ "generator", "height", "width", "latents", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] __snake_case : List[str] = False @property def UpperCamelCase ( self: Tuple ): '''simple docstring''' return 32 @property def UpperCamelCase ( self: Optional[int] ): '''simple docstring''' return 32 @property def UpperCamelCase ( self: List[Any] ): '''simple docstring''' return self.time_input_dim @property def UpperCamelCase ( self: Optional[Any] ): '''simple docstring''' return self.time_input_dim * 4 @property def UpperCamelCase ( self: Union[str, Any] ): '''simple docstring''' return 100 @property def UpperCamelCase ( self: str ): '''simple docstring''' torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = { """in_channels""": 9, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """image""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } _SCREAMING_SNAKE_CASE = UNetaDConditionModel(**UpperCAmelCase_ ) return model @property def UpperCamelCase ( self: Union[str, Any] ): '''simple docstring''' return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def UpperCamelCase ( self: List[Any] ): '''simple docstring''' torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE = VQModel(**self.dummy_movq_kwargs ) return model def UpperCamelCase ( self: List[Any] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = self.dummy_unet _SCREAMING_SNAKE_CASE = self.dummy_movq _SCREAMING_SNAKE_CASE = DDIMScheduler( num_train_timesteps=1_000 , beta_schedule="""linear""" , beta_start=0.0_00_85 , beta_end=0.0_12 , clip_sample=UpperCAmelCase_ , set_alpha_to_one=UpperCAmelCase_ , steps_offset=1 , prediction_type="""epsilon""" , thresholding=UpperCAmelCase_ , ) _SCREAMING_SNAKE_CASE = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def UpperCamelCase ( self: Dict , UpperCAmelCase_: Optional[int] , UpperCAmelCase_: List[str]=0 ): '''simple docstring''' _SCREAMING_SNAKE_CASE = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(UpperCAmelCase_ ) ).to(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( UpperCAmelCase_ ) # create init_image _SCREAMING_SNAKE_CASE = floats_tensor((1, 3, 64, 64) , rng=random.Random(UpperCAmelCase_ ) ).to(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = image.cpu().permute(0 , 2 , 3 , 1 )[0] _SCREAMING_SNAKE_CASE = Image.fromarray(np.uinta(UpperCAmelCase_ ) ).convert("""RGB""" ).resize((256, 256) ) # create mask _SCREAMING_SNAKE_CASE = np.ones((64, 64) , dtype=np.floataa ) _SCREAMING_SNAKE_CASE = 0 if str(UpperCAmelCase_ ).startswith("""mps""" ): _SCREAMING_SNAKE_CASE = torch.manual_seed(UpperCAmelCase_ ) else: _SCREAMING_SNAKE_CASE = torch.Generator(device=UpperCAmelCase_ ).manual_seed(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = { """image""": init_image, """mask_image""": mask, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 2, """guidance_scale""": 4.0, """output_type""": """np""", } return inputs def UpperCamelCase ( self: str ): '''simple docstring''' _SCREAMING_SNAKE_CASE = """cpu""" _SCREAMING_SNAKE_CASE = self.get_dummy_components() _SCREAMING_SNAKE_CASE = self.pipeline_class(**UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = pipe.to(UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = pipe(**self.get_dummy_inputs(UpperCAmelCase_ ) ) _SCREAMING_SNAKE_CASE = output.images _SCREAMING_SNAKE_CASE = pipe( **self.get_dummy_inputs(UpperCAmelCase_ ) , return_dict=UpperCAmelCase_ , )[0] _SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] _SCREAMING_SNAKE_CASE = image_from_tuple[0, -3:, -3:, -1] print(F'image.shape {image.shape}' ) assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE = np.array( [0.50_77_59_03, 0.49_52_71_95, 0.48_82_45_43, 0.50_19_22_37, 0.48_64_49_06, 0.49_37_38_14, 0.4_78_05_98, 0.47_23_48_27, 0.48_32_78_48] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' def UpperCamelCase ( self: int ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __UpperCAmelCase (unittest.TestCase ): def UpperCamelCase ( self: List[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase ( self: List[str] ): '''simple docstring''' _SCREAMING_SNAKE_CASE = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy""" ) _SCREAMING_SNAKE_CASE = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) _SCREAMING_SNAKE_CASE = np.ones((768, 768) , dtype=np.floataa ) _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = """a hat""" _SCREAMING_SNAKE_CASE = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = KandinskyVaaInpaintPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder-inpaint""" , torch_dtype=torch.floataa ) _SCREAMING_SNAKE_CASE = pipeline.to(UpperCAmelCase_ ) pipeline.set_progress_bar_config(disable=UpperCAmelCase_ ) _SCREAMING_SNAKE_CASE = torch.Generator(device="""cpu""" ).manual_seed(0 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = pipe_prior( UpperCAmelCase_ , generator=UpperCAmelCase_ , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() _SCREAMING_SNAKE_CASE = pipeline( image=UpperCAmelCase_ , mask_image=UpperCAmelCase_ , image_embeds=UpperCAmelCase_ , negative_image_embeds=UpperCAmelCase_ , generator=UpperCAmelCase_ , num_inference_steps=100 , height=768 , width=768 , output_type="""np""" , ) _SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(UpperCAmelCase_ , UpperCAmelCase_ )
125
0
'''simple docstring''' a : Tuple = [ 9_9_9, 8_0_0, 7_9_9, 6_0_0, 5_9_9, 5_0_0, 4_0_0, 3_9_9, 3_7_7, 3_5_5, 3_3_3, 3_1_1, 2_8_8, 2_6_6, 2_4_4, 2_2_2, 2_0_0, 1_9_9, 1_7_7, 1_5_5, 1_3_3, 1_1_1, 8_8, 6_6, 4_4, 2_2, 0, ] a : Dict = [ 9_9_9, 9_7_6, 9_5_2, 9_2_8, 9_0_5, 8_8_2, 8_5_8, 8_5_7, 8_1_0, 7_6_2, 7_1_5, 7_1_4, 5_7_2, 4_2_9, 4_2_8, 2_8_6, 2_8_5, 2_3_8, 1_9_0, 1_4_3, 1_4_2, 1_1_8, 9_5, 7_1, 4_7, 2_4, 0, ] a : Union[str, Any] = [ 9_9_9, 9_8_8, 9_7_7, 9_6_6, 9_5_5, 9_4_4, 9_3_3, 9_2_2, 9_1_1, 9_0_0, 8_9_9, 8_7_9, 8_5_9, 8_4_0, 8_2_0, 8_0_0, 7_9_9, 7_6_6, 7_3_3, 7_0_0, 6_9_9, 6_5_0, 6_0_0, 5_9_9, 5_0_0, 4_9_9, 4_0_0, 3_9_9, 3_5_0, 3_0_0, 2_9_9, 2_6_6, 2_3_3, 2_0_0, 1_9_9, 1_7_9, 1_5_9, 1_4_0, 1_2_0, 1_0_0, 9_9, 8_8, 7_7, 6_6, 5_5, 4_4, 3_3, 2_2, 1_1, 0, ] a : Tuple = [ 9_9_9, 9_9_5, 9_9_2, 9_8_9, 9_8_5, 9_8_1, 9_7_8, 9_7_5, 9_7_1, 9_6_7, 9_6_4, 9_6_1, 9_5_7, 9_5_6, 9_5_1, 9_4_7, 9_4_2, 9_3_7, 9_3_3, 9_2_8, 9_2_3, 9_1_9, 9_1_4, 9_1_3, 9_0_8, 9_0_3, 8_9_7, 8_9_2, 8_8_7, 8_8_1, 8_7_6, 8_7_1, 8_7_0, 8_6_4, 8_5_8, 8_5_2, 8_4_6, 8_4_0, 8_3_4, 8_2_8, 8_2_7, 8_2_0, 8_1_3, 8_0_6, 7_9_9, 7_9_2, 7_8_5, 7_8_4, 7_7_7, 7_7_0, 7_6_3, 7_5_6, 7_4_9, 7_4_2, 7_4_1, 7_3_3, 7_2_4, 7_1_6, 7_0_7, 6_9_9, 6_9_8, 6_8_8, 6_7_7, 6_6_6, 6_5_6, 6_5_5, 6_4_5, 6_3_4, 6_2_3, 6_1_3, 6_1_2, 5_9_8, 5_8_4, 5_7_0, 5_6_9, 5_5_5, 5_4_1, 5_2_7, 5_2_6, 5_0_5, 4_8_4, 4_8_3, 4_6_2, 4_4_0, 4_3_9, 3_9_6, 3_9_5, 3_5_2, 3_5_1, 3_0_8, 3_0_7, 2_6_4, 2_6_3, 2_2_0, 2_1_9, 1_7_6, 1_3_2, 8_8, 4_4, 0, ] a : Dict = [ 9_9_9, 9_9_7, 9_9_5, 9_9_2, 9_9_0, 9_8_8, 9_8_6, 9_8_4, 9_8_1, 9_7_9, 9_7_7, 9_7_5, 9_7_2, 9_7_0, 9_6_8, 9_6_6, 9_6_4, 9_6_1, 9_5_9, 9_5_7, 9_5_6, 9_5_4, 9_5_1, 9_4_9, 9_4_6, 9_4_4, 9_4_1, 9_3_9, 9_3_6, 9_3_4, 9_3_1, 9_2_9, 9_2_6, 9_2_4, 9_2_1, 9_1_9, 9_1_6, 9_1_4, 9_1_3, 9_1_0, 9_0_7, 9_0_5, 9_0_2, 8_9_9, 8_9_6, 8_9_3, 8_9_1, 8_8_8, 8_8_5, 8_8_2, 8_7_9, 8_7_7, 8_7_4, 8_7_1, 8_7_0, 8_6_7, 8_6_4, 8_6_1, 8_5_8, 8_5_5, 8_5_2, 8_4_9, 8_4_6, 8_4_3, 8_4_0, 8_3_7, 8_3_4, 8_3_1, 8_2_8, 8_2_7, 8_2_4, 8_2_1, 8_1_7, 8_1_4, 8_1_1, 8_0_8, 8_0_4, 8_0_1, 7_9_8, 7_9_5, 7_9_1, 7_8_8, 7_8_5, 7_8_4, 7_8_0, 7_7_7, 7_7_4, 7_7_0, 7_6_6, 7_6_3, 7_6_0, 7_5_6, 7_5_2, 7_4_9, 7_4_6, 7_4_2, 7_4_1, 7_3_7, 7_3_3, 7_3_0, 7_2_6, 7_2_2, 7_1_8, 7_1_4, 7_1_0, 7_0_7, 7_0_3, 6_9_9, 6_9_8, 6_9_4, 6_9_0, 6_8_5, 6_8_1, 6_7_7, 6_7_3, 6_6_9, 6_6_4, 6_6_0, 6_5_6, 6_5_5, 6_5_0, 6_4_6, 6_4_1, 6_3_6, 6_3_2, 6_2_7, 6_2_2, 6_1_8, 6_1_3, 6_1_2, 6_0_7, 6_0_2, 5_9_6, 5_9_1, 5_8_6, 5_8_0, 5_7_5, 5_7_0, 5_6_9, 5_6_3, 5_5_7, 5_5_1, 5_4_5, 5_3_9, 5_3_3, 5_2_7, 5_2_6, 5_1_9, 5_1_2, 5_0_5, 4_9_8, 4_9_1, 4_8_4, 4_8_3, 4_7_4, 4_6_6, 4_5_7, 4_4_9, 4_4_0, 4_3_9, 4_2_8, 4_1_8, 4_0_7, 3_9_6, 3_9_5, 3_8_1, 3_6_6, 3_5_2, 3_5_1, 3_3_0, 3_0_8, 3_0_7, 2_8_6, 2_6_4, 2_6_3, 2_4_2, 2_2_0, 2_1_9, 1_7_6, 1_7_5, 1_3_2, 1_3_1, 8_8, 4_4, 0, ] a : Union[str, Any] = [ 9_9_9, 9_9_1, 9_8_2, 9_7_4, 9_6_6, 9_5_8, 9_5_0, 9_4_1, 9_3_3, 9_2_5, 9_1_6, 9_0_8, 9_0_0, 8_9_9, 8_7_4, 8_5_0, 8_2_5, 8_0_0, 7_9_9, 7_0_0, 6_0_0, 5_0_0, 4_0_0, 3_0_0, 2_0_0, 1_0_0, 0, ] a : Any = [ 9_9_9, 9_9_2, 9_8_5, 9_7_8, 9_7_1, 9_6_4, 9_5_7, 9_4_9, 9_4_2, 9_3_5, 9_2_8, 9_2_1, 9_1_4, 9_0_7, 9_0_0, 8_9_9, 8_7_9, 8_5_9, 8_4_0, 8_2_0, 8_0_0, 7_9_9, 7_6_6, 7_3_3, 7_0_0, 6_9_9, 6_5_0, 6_0_0, 5_9_9, 5_0_0, 4_9_9, 4_0_0, 3_9_9, 3_0_0, 2_9_9, 2_0_0, 1_9_9, 1_0_0, 9_9, 0, ] a : List[str] = [ 9_9_9, 9_9_6, 9_9_2, 9_8_9, 9_8_5, 9_8_2, 9_7_9, 9_7_5, 9_7_2, 9_6_8, 9_6_5, 9_6_1, 9_5_8, 9_5_5, 9_5_1, 9_4_8, 9_4_4, 9_4_1, 9_3_8, 9_3_4, 9_3_1, 9_2_7, 9_2_4, 9_2_0, 9_1_7, 9_1_4, 9_1_0, 9_0_7, 9_0_3, 9_0_0, 8_9_9, 8_9_1, 8_8_4, 8_7_6, 8_6_9, 8_6_1, 8_5_3, 8_4_6, 8_3_8, 8_3_0, 8_2_3, 8_1_5, 8_0_8, 8_0_0, 7_9_9, 7_8_8, 7_7_7, 7_6_6, 7_5_5, 7_4_4, 7_3_3, 7_2_2, 7_1_1, 7_0_0, 6_9_9, 6_8_8, 6_7_7, 6_6_6, 6_5_5, 6_4_4, 6_3_3, 6_2_2, 6_1_1, 6_0_0, 5_9_9, 5_8_5, 5_7_1, 5_5_7, 5_4_2, 5_2_8, 5_1_4, 5_0_0, 4_9_9, 4_8_5, 4_7_1, 4_5_7, 4_4_2, 4_2_8, 4_1_4, 4_0_0, 3_9_9, 3_7_9, 3_5_9, 3_4_0, 3_2_0, 3_0_0, 2_9_9, 2_7_9, 2_5_9, 2_4_0, 2_2_0, 2_0_0, 1_9_9, 1_6_6, 1_3_3, 1_0_0, 9_9, 6_6, 3_3, 0, ]
265
'''simple docstring''' def __lowerCamelCase ( _lowercase , _lowercase ) -> int: if b == 0: return 1 if (b % 2) == 0: return actual_power(_lowercase , int(b / 2 ) ) * actual_power(_lowercase , int(b / 2 ) ) else: return a * actual_power(_lowercase , int(b / 2 ) ) * actual_power(_lowercase , int(b / 2 ) ) def __lowerCamelCase ( _lowercase , _lowercase ) -> float: if b < 0: return 1 / actual_power(_lowercase , _lowercase ) return actual_power(_lowercase , _lowercase ) if __name__ == "__main__": print(power(-2, -3))
265
1
'''simple docstring''' from __future__ import annotations import math def lowerCamelCase__ ( _A , _A , _A , _A , _A ): if depth < 0: raise ValueError('Depth cannot be less than 0' ) if not scores: raise ValueError('Scores cannot be empty' ) if depth == height: return scores[node_index] return ( max( minimax(depth + 1 , node_index * 2 , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , ) if is_max else min( minimax(depth + 1 , node_index * 2 , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) , ) ) def lowerCamelCase__ ( ): a : Dict = [90, 23, 6, 33, 21, 65, 123, 3_4423] a : Union[str, Any] = math.log(len(_UpperCAmelCase ) , 2 ) print(f"""Optimal value : {minimax(0 , 0 , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase )}""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
350
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def lowerCamelCase__ ( _A , _A , _A=0 ): # Format the message. if name is None: a : Tuple = None else: a : Dict = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' a : 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 lowerCamelCase__ ( _A , _A , _A , _A , _A ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. a : str = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] a : List[Any] = (num_heads, hidden_size, num_splits) + input_shape[1:] a : int = param.view(*_A ) a : List[str] = param.transpose(0 , 2 ) a : Union[str, Any] = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] a : Union[str, Any] = (num_heads, num_splits, hidden_size) + input_shape[1:] a : List[str] = param.view(*_A ) a : Union[str, Any] = param.transpose(0 , 1 ).contiguous() a : List[Any] = param.view(*_A ) return param def lowerCamelCase__ ( _A , _A , _A ): # The converted output model. a : Optional[Any] = {} # old versions did not store training args a : Dict = 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)) a : Union[str, Any] = ds_args.padded_vocab_size a : str = ds_args.max_position_embeddings a : Dict = ds_args.hidden_size a : Union[str, Any] = ds_args.num_layers a : Dict = ds_args.num_attention_heads a : int = ds_args.ffn_hidden_size # pprint(config) # The number of heads. a : Any = config.n_head # The hidden_size per head. a : Tuple = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): a : Any = input_state_dict['checkpoint_version'] else: a : Any = 0.0 # The model. a : Optional[int] = input_state_dict['model'] # The language model. a : Optional[Any] = model['language_model'] # The embeddings. a : List[str] = lm['embedding'] # The word embeddings. a : List[Any] = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. a : Dict = word_embeddings[: config.vocab_size, :] a : int = word_embeddings # The position embeddings. a : Tuple = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] a : List[str] = 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. a : Optional[Any] = pos_embeddings # The transformer. a : Union[str, Any] = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. a : List[Any] = re.compile(r'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. a : Optional[Any] = { '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. a : Tuple = layer_re.match(_A ) # Stop if that's not a layer if m is None: break # The index of the layer. a : Union[str, Any] = int(m.group(1 ) ) # The name of the operation. a : Optional[int] = m.group(2 ) # Is it a weight or a bias? a : Optional[int] = m.group(3 ) # The name of the layer. a : Any = f"""transformer.h.{layer_idx}""" # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): a : str = 'ln_1' if op_name.startswith('input' ) else 'ln_2' a : Tuple = 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. a : Dict = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , _A , _A ) a : Optional[Any] = causal_mask # Insert a "dummy" tensor for masked_bias. a : List[Any] = torch.tensor(-1E4 , dtype=torch.floataa ) a : List[str] = masked_bias a : Union[str, 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. a : int = out_val.transpose(0 , 1 ).contiguous() # Store. a : int = 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": a : str = fix_query_key_value_ordering(_A , _A , 3 , _A , _A ) # Store. No change of shape. a : List[str] = out_val # Transpose the weights. elif weight_or_bias == "weight": a : Tuple = megatron_to_transformers[op_name] a : List[str] = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": a : Dict = megatron_to_transformers[op_name] a : Optional[Any] = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. a : str = transformer['final_layernorm.weight'] a : List[str] = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. a : Optional[int] = word_embeddings # It should be done! return output_state_dict def lowerCamelCase__ ( ): # Create the argument parser. a : Dict = 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.' , ) a : Union[str, Any] = parser.parse_args() # Extract the basename. a : Optional[Any] = 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: a : Union[str, Any] = torch.load(_A , map_location='cpu' ) else: a : Any = torch.load(args.path_to_checkpoint , map_location='cpu' ) a : List[Any] = 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: a : int = 'gelu_fast' elif ds_args.openai_gelu: a : Dict = 'gelu_new' else: a : Any = 'gelu' else: # in the very early days this used to be "gelu_new" a : Any = 'gelu_new' # Spell out all parameters in case the defaults change. a : Tuple = 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: a : str = GPTaConfig.from_json_file(args.config_file ) a : Any = ['GPT2LMHeadModel'] # Convert. print('Converting' ) a : Union[str, Any] = 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: a : Union[str, Any] = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": a : Tuple = 'gpt2' elif tokenizer_type == "PretrainedFromHF": a : List[str] = ds_args.tokenizer_name_or_path else: raise ValueError(f"""Unrecognized tokenizer_type {tokenizer_type}""" ) else: a : Optional[Any] = 'gpt2' a : Tuple = AutoTokenizer.from_pretrained(_A ) a : str = type(_A ).__name__ a : List[str] = 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. a : Optional[int] = os.path.join(_A , 'pytorch_model.bin' ) print(f"""Saving checkpoint to \"{output_checkpoint_file}\"""" ) torch.save(_A , _A ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
96
0
from datetime import datetime import matplotlib.pyplot as plt import torch def __lowercase ( _UpperCamelCase ) ->int: """simple docstring""" for param in module.parameters(): lowercase : Optional[int] = False def __lowercase ( ) ->Tuple: """simple docstring""" lowercase : Any = '''cuda''' if torch.cuda.is_available() else '''cpu''' if torch.backends.mps.is_available() and torch.backends.mps.is_built(): lowercase : str = '''mps''' if device == "mps": print( '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch''' ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues''' ''' with generations.''' ) return device def __lowercase ( _UpperCamelCase ) ->Union[str, Any]: """simple docstring""" lowercase : List[str] = plt.imshow(_UpperCamelCase ) fig.axes.get_xaxis().set_visible(_UpperCamelCase ) fig.axes.get_yaxis().set_visible(_UpperCamelCase ) plt.show() def __lowercase ( ) ->Optional[int]: """simple docstring""" lowercase : Tuple = datetime.now() lowercase : str = current_time.strftime('''%H:%M:%S''' ) return timestamp
337
from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging __a = logging.get_logger(__name__) __a = { '''deepmind/language-perceiver''': '''https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json''', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class __SCREAMING_SNAKE_CASE ( A__ ): A : List[str] = 'perceiver' def __init__( self , SCREAMING_SNAKE_CASE__=256 , SCREAMING_SNAKE_CASE__=1280 , SCREAMING_SNAKE_CASE__=768 , SCREAMING_SNAKE_CASE__=1 , SCREAMING_SNAKE_CASE__=26 , SCREAMING_SNAKE_CASE__=8 , SCREAMING_SNAKE_CASE__=8 , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__="kv" , SCREAMING_SNAKE_CASE__=1 , SCREAMING_SNAKE_CASE__=1 , SCREAMING_SNAKE_CASE__="gelu" , SCREAMING_SNAKE_CASE__=0.1 , SCREAMING_SNAKE_CASE__=0.02 , SCREAMING_SNAKE_CASE__=1E-12 , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=262 , SCREAMING_SNAKE_CASE__=2048 , SCREAMING_SNAKE_CASE__=56 , SCREAMING_SNAKE_CASE__=[368, 496] , SCREAMING_SNAKE_CASE__=16 , SCREAMING_SNAKE_CASE__=1920 , SCREAMING_SNAKE_CASE__=16 , SCREAMING_SNAKE_CASE__=[1, 16, 224, 224] , **SCREAMING_SNAKE_CASE__ , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowercase : Any = num_latents lowercase : Union[str, Any] = d_latents lowercase : str = d_model lowercase : int = num_blocks lowercase : str = num_self_attends_per_block lowercase : List[str] = num_self_attention_heads lowercase : List[str] = num_cross_attention_heads lowercase : int = qk_channels lowercase : List[Any] = v_channels lowercase : int = cross_attention_shape_for_attention lowercase : Tuple = self_attention_widening_factor lowercase : Dict = cross_attention_widening_factor lowercase : Any = hidden_act lowercase : Optional[Any] = attention_probs_dropout_prob lowercase : Union[str, Any] = initializer_range lowercase : Any = layer_norm_eps lowercase : Any = use_query_residual # masked language modeling attributes lowercase : List[str] = vocab_size lowercase : Dict = max_position_embeddings # image classification attributes lowercase : int = image_size # flow attributes lowercase : List[Any] = train_size # multimodal autoencoding attributes lowercase : List[Any] = num_frames lowercase : Union[str, Any] = audio_samples_per_frame lowercase : int = samples_per_patch lowercase : Optional[int] = output_shape class __SCREAMING_SNAKE_CASE ( A__ ): @property def __lowerCamelCase ( self ): if self.task == "multiple-choice": lowercase : Tuple = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowercase : Dict = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''inputs''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] ) @property def __lowerCamelCase ( self ): return 1E-4 def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = -1 , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = 3 , SCREAMING_SNAKE_CASE__ = 40 , SCREAMING_SNAKE_CASE__ = 40 , ): # copied from `transformers.onnx.config.OnnxConfig` and slightly altered/simplified if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX lowercase : str = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX lowercase : Union[str, Any] = preprocessor.num_special_tokens_to_add(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=SCREAMING_SNAKE_CASE__ ) # Generate dummy inputs according to compute batch and sequence lowercase : Optional[Any] = [''' '''.join(['''a'''] ) * seq_length] * batch_size lowercase : Any = dict(preprocessor(SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ ) ) lowercase : Union[str, Any] = inputs.pop('''input_ids''' ) return inputs elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX lowercase : List[str] = compute_effective_axis_dimension(SCREAMING_SNAKE_CASE__ , fixed_dimension=OnnxConfig.default_fixed_batch ) lowercase : List[str] = self._generate_dummy_images(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = dict(preprocessor(images=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ ) ) lowercase : Union[str, Any] = inputs.pop('''pixel_values''' ) return inputs else: raise ValueError( '''Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.''' )
337
1
"""simple docstring""" import math from collections.abc import Iterator from itertools import takewhile def _lowerCAmelCase ( UpperCamelCase_ ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(__lowerCAmelCase ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _lowerCAmelCase ( ): __SCREAMING_SNAKE_CASE = 2 while True: if is_prime(__lowerCAmelCase ): yield num num += 1 def _lowerCAmelCase ( UpperCamelCase_ = 200_0000 ): return sum(takewhile(lambda UpperCamelCase_ : x < n , prime_generator() ) ) if __name__ == "__main__": print(F"""{solution() = }""")
362
"""simple docstring""" from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def _lowerCAmelCase ( ): __SCREAMING_SNAKE_CASE = HfArgumentParser(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses()[0] __SCREAMING_SNAKE_CASE = TensorFlowBenchmark(args=UpperCamelCase_ ) try: __SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses()[0] except ValueError as e: __SCREAMING_SNAKE_CASE = """Arg --no_{0} is no longer used, please use --no-{0} instead.""" __SCREAMING_SNAKE_CASE = """ """.join(str(UpperCamelCase_ ).split(""" """ )[:-1] ) __SCREAMING_SNAKE_CASE = """""" __SCREAMING_SNAKE_CASE = eval(str(UpperCamelCase_ ).split(""" """ )[-1] ) __SCREAMING_SNAKE_CASE = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(UpperCamelCase_ ) if len(UpperCamelCase_ ) > 0: __SCREAMING_SNAKE_CASE = full_error_msg + begin_error_msg + str(UpperCamelCase_ ) raise ValueError(UpperCamelCase_ ) benchmark.run() if __name__ == "__main__": main()
255
0