code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str ) -> Dict: return number | (1 << position) def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : Any , UpperCAmelCase_ : int ) -> str: return number & ~(1 << position) def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] ) -> Optional[Any]: return number ^ (1 << position) def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : str ) -> str: return ((number >> position) & 1) == 1 def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Any] ) -> Optional[Any]: return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
443
import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class lowerCAmelCase_ ( unittest.TestCase ): UpperCAmelCase = MODEL_FOR_CAUSAL_LM_MAPPING UpperCAmelCase = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def UpperCamelCase_ ( self : str ): _UpperCamelCase = pipeline(task='''text-generation''' , model='''sshleifer/tiny-ctrl''' , framework='''pt''' ) # Using `do_sample=False` to force deterministic output _UpperCamelCase = text_generator('''This is a test''' , do_sample=_A ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.''' ''' oscope. FiliFili@@''' ) } ] , ) _UpperCamelCase = text_generator(['''This is a test''', '''This is a second test'''] ) self.assertEqual( _A , [ [ { '''generated_text''': ( '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.''' ''' oscope. FiliFili@@''' ) } ], [ { '''generated_text''': ( '''This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy''' ''' oscope. oscope. FiliFili@@''' ) } ], ] , ) _UpperCamelCase = text_generator('''This is a test''' , do_sample=_A , num_return_sequences=2 , return_tensors=_A ) self.assertEqual( _A , [ {'''generated_token_ids''': ANY(_A )}, {'''generated_token_ids''': ANY(_A )}, ] , ) _UpperCamelCase = text_generator.model.config.eos_token_id _UpperCamelCase = '''<pad>''' _UpperCamelCase = text_generator( ['''This is a test''', '''This is a second test'''] , do_sample=_A , num_return_sequences=2 , batch_size=2 , return_tensors=_A , ) self.assertEqual( _A , [ [ {'''generated_token_ids''': ANY(_A )}, {'''generated_token_ids''': ANY(_A )}, ], [ {'''generated_token_ids''': ANY(_A )}, {'''generated_token_ids''': ANY(_A )}, ], ] , ) @require_tf def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = pipeline(task='''text-generation''' , model='''sshleifer/tiny-ctrl''' , framework='''tf''' ) # Using `do_sample=False` to force deterministic output _UpperCamelCase = text_generator('''This is a test''' , do_sample=_A ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵''' ''' please,''' ) } ] , ) _UpperCamelCase = text_generator(['''This is a test''', '''This is a second test'''] , do_sample=_A ) self.assertEqual( _A , [ [ { '''generated_text''': ( '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵''' ''' please,''' ) } ], [ { '''generated_text''': ( '''This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes''' ''' Cannes 閲閲Cannes Cannes Cannes 攵 please,''' ) } ], ] , ) def UpperCamelCase_ ( self : int , _A : str , _A : Union[str, Any] , _A : Any ): _UpperCamelCase = TextGenerationPipeline(model=_A , tokenizer=_A ) return text_generator, ["This is a test", "Another test"] def UpperCamelCase_ ( self : Union[str, Any] ): _UpperCamelCase = '''Hello I believe in''' _UpperCamelCase = pipeline('''text-generation''' , model='''hf-internal-testing/tiny-random-gpt2''' ) _UpperCamelCase = text_generator(_A ) self.assertEqual( _A , [{'''generated_text''': '''Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe'''}] , ) _UpperCamelCase = text_generator(_A , stop_sequence=''' fe''' ) self.assertEqual(_A , [{'''generated_text''': '''Hello I believe in fe'''}] ) def UpperCamelCase_ ( self : Any , _A : List[Any] , _A : Union[str, Any] ): _UpperCamelCase = text_generator.model _UpperCamelCase = text_generator.tokenizer _UpperCamelCase = text_generator('''This is a test''' ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test''' ) ) _UpperCamelCase = text_generator('''This is a test''' , return_full_text=_A ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertNotIn('''This is a test''' , outputs[0]['''generated_text'''] ) _UpperCamelCase = pipeline(task='''text-generation''' , model=_A , tokenizer=_A , return_full_text=_A ) _UpperCamelCase = text_generator('''This is a test''' ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertNotIn('''This is a test''' , outputs[0]['''generated_text'''] ) _UpperCamelCase = text_generator('''This is a test''' , return_full_text=_A ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test''' ) ) _UpperCamelCase = text_generator(['''This is great !''', '''Something else'''] , num_return_sequences=2 , do_sample=_A ) self.assertEqual( _A , [ [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], ] , ) if text_generator.tokenizer.pad_token is not None: _UpperCamelCase = text_generator( ['''This is great !''', '''Something else'''] , num_return_sequences=2 , batch_size=2 , do_sample=_A ) self.assertEqual( _A , [ [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], ] , ) with self.assertRaises(_A ): _UpperCamelCase = text_generator('''test''' , return_full_text=_A , return_text=_A ) with self.assertRaises(_A ): _UpperCamelCase = text_generator('''test''' , return_full_text=_A , return_tensors=_A ) with self.assertRaises(_A ): _UpperCamelCase = text_generator('''test''' , return_text=_A , return_tensors=_A ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): _UpperCamelCase = text_generator('''''' ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) else: with self.assertRaises((ValueError, AssertionError) ): _UpperCamelCase = text_generator('''''' ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. _UpperCamelCase = ['''RwkvForCausalLM''', '''XGLMForCausalLM''', '''GPTNeoXForCausalLM'''] if ( tokenizer.model_max_length < 1_0000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator('''This is a test''' * 500 , max_new_tokens=20 ) _UpperCamelCase = text_generator('''This is a test''' * 500 , handle_long_generation='''hole''' , max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(_A ): text_generator( '''This is a test''' * 500 , handle_long_generation='''hole''' , max_new_tokens=tokenizer.model_max_length + 10 , ) @require_torch @require_accelerate @require_torch_gpu def UpperCamelCase_ ( self : Optional[int] ): import torch # Classic `model_kwargs` _UpperCamelCase = pipeline( model='''hf-internal-testing/tiny-random-bloom''' , model_kwargs={'''device_map''': '''auto''', '''torch_dtype''': torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) _UpperCamelCase = pipe('''This is a test''' ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test test test test test test test test test test test test test test test test''' ''' test''' ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device_map='''auto''' , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) _UpperCamelCase = pipe('''This is a test''' ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test test test test test test test test test test test test test test test test''' ''' test''' ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device_map='''auto''' ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) _UpperCamelCase = pipe('''This is a test''' ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test test test test test test test test test test test test test test test test''' ''' test''' ) } ] , ) @require_torch @require_torch_gpu def UpperCamelCase_ ( self : Union[str, Any] ): import torch _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device=0 , torch_dtype=torch.floataa ) pipe('''This is a test''' ) @require_torch @require_accelerate @require_torch_gpu def UpperCamelCase_ ( self : Optional[int] ): import torch _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device_map='''auto''' , torch_dtype=torch.floataa ) pipe('''This is a test''' , do_sample=_A , top_p=0.5 ) def UpperCamelCase_ ( self : Tuple ): _UpperCamelCase = '''Hello world''' _UpperCamelCase = pipeline('''text-generation''' , model='''hf-internal-testing/tiny-random-gpt2''' ) if text_generator.model.framework == "tf": _UpperCamelCase = logging.get_logger('''transformers.generation.tf_utils''' ) else: _UpperCamelCase = logging.get_logger('''transformers.generation.utils''' ) _UpperCamelCase = '''Both `max_new_tokens`''' # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(_A ) as cl: _UpperCamelCase = text_generator(_A , max_length=10 , max_new_tokens=1 ) self.assertIn(_A , cl.out ) # The user only sets one -> no warning with CaptureLogger(_A ) as cl: _UpperCamelCase = text_generator(_A , max_new_tokens=1 ) self.assertNotIn(_A , cl.out ) with CaptureLogger(_A ) as cl: _UpperCamelCase = text_generator(_A , max_length=10 ) self.assertNotIn(_A , cl.out )
10
0
'''simple docstring''' import gc import unittest import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DDPMScheduler, PriorTransformer, StableUnCLIPPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class lowerCAmelCase__ ( __lowercase , __lowercase , __lowercase , unittest.TestCase ): """simple docstring""" lowerCAmelCase__ = StableUnCLIPPipeline lowerCAmelCase__ = TEXT_TO_IMAGE_PARAMS lowerCAmelCase__ = TEXT_TO_IMAGE_BATCH_PARAMS lowerCAmelCase__ = TEXT_TO_IMAGE_IMAGE_PARAMS lowerCAmelCase__ = TEXT_TO_IMAGE_IMAGE_PARAMS # TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false lowerCAmelCase__ = False def UpperCAmelCase__ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = 32 __SCREAMING_SNAKE_CASE = embedder_hidden_size # prior components torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = CLIPTextModelWithProjection( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=_A , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = PriorTransformer( num_attention_heads=2 , attention_head_dim=12 , embedding_dim=_A , num_layers=1 , ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = DDPMScheduler( variance_type="""fixed_small_log""" , prediction_type="""sample""" , num_train_timesteps=1_000 , clip_sample=_A , clip_sample_range=5.0 , beta_schedule="""squaredcos_cap_v2""" , ) # regular denoising components torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = StableUnCLIPImageNormalizer(embedding_dim=_A ) __SCREAMING_SNAKE_CASE = DDPMScheduler(beta_schedule="""squaredcos_cap_v2""" ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""CrossAttnDownBlock2D""", """DownBlock2D""") , up_block_types=("""UpBlock2D""", """CrossAttnUpBlock2D""") , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type="""projection""" , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=_A , layers_per_block=1 , upcast_attention=_A , use_linear_projection=_A , ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = DDIMScheduler( beta_schedule="""scaled_linear""" , beta_start=0.00085 , beta_end=0.012 , prediction_type="""v_prediction""" , set_alpha_to_one=_A , steps_offset=1 , ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = AutoencoderKL() __SCREAMING_SNAKE_CASE = { # prior components """prior_tokenizer""": prior_tokenizer, """prior_text_encoder""": prior_text_encoder, """prior""": prior, """prior_scheduler""": prior_scheduler, # image noising components """image_normalizer""": image_normalizer, """image_noising_scheduler""": image_noising_scheduler, # regular denoising components """tokenizer""": tokenizer, """text_encoder""": text_encoder, """unet""": unet, """scheduler""": scheduler, """vae""": vae, } return components def UpperCAmelCase__ ( self : Dict , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Dict=0 ) -> int: """simple docstring""" if str(_A ).startswith("""mps""" ): __SCREAMING_SNAKE_CASE = torch.manual_seed(_A ) else: __SCREAMING_SNAKE_CASE = torch.Generator(device=_A ).manual_seed(_A ) __SCREAMING_SNAKE_CASE = { """prompt""": """A painting of a squirrel eating a burger""", """generator""": generator, """num_inference_steps""": 2, """prior_num_inference_steps""": 2, """output_type""": """numpy""", } return inputs def UpperCAmelCase__ ( self : Dict ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE = torch_device == """cpu""" self._test_attention_slicing_forward_pass(test_max_difference=_A ) def UpperCAmelCase__ ( self : List[Any] ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = torch_device in ["""cpu""", """mps"""] self._test_inference_batch_single_identical(test_max_difference=_A ) @slow @require_torch_gpu class lowerCAmelCase__ ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self : Optional[Any] ) -> List[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase__ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy""" ) __SCREAMING_SNAKE_CASE = StableUnCLIPPipeline.from_pretrained("""fusing/stable-unclip-2-1-l""" , torch_dtype=torch.floataa ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE = torch.Generator(device="""cpu""" ).manual_seed(0 ) __SCREAMING_SNAKE_CASE = pipe("""anime turle""" , generator=_A , output_type="""np""" ) __SCREAMING_SNAKE_CASE = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_A , _A ) def UpperCAmelCase__ ( self : Optional[Any] ) -> str: """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __SCREAMING_SNAKE_CASE = StableUnCLIPPipeline.from_pretrained("""fusing/stable-unclip-2-1-l""" , torch_dtype=torch.floataa ) __SCREAMING_SNAKE_CASE = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __SCREAMING_SNAKE_CASE = pipe( """anime turtle""" , prior_num_inference_steps=2 , num_inference_steps=2 , output_type="""np""" , ) __SCREAMING_SNAKE_CASE = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
627
def _snake_case ( __snake_case = 100 ): _UpperCamelCase = (n * (n + 1) // 2) ** 2 _UpperCamelCase = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(f'{solution() = }')
10
0
"""simple docstring""" from .imports import is_rich_available if is_rich_available(): from rich.traceback import install install(show_locals=False) else: raise ModuleNotFoundError('To use the rich extension, install rich with `pip install rich`')
473
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL _lowerCAmelCase = logging.get_logger(__name__) def _snake_case ( __snake_case , __snake_case , __snake_case , __snake_case ): def constraint_to_multiple_of(__snake_case , __snake_case , __snake_case=0 , __snake_case=None ): _UpperCamelCase = round(val / multiple ) * multiple if max_val is not None and x > max_val: _UpperCamelCase = math.floor(val / multiple ) * multiple if x < min_val: _UpperCamelCase = math.ceil(val / multiple ) * multiple return x _UpperCamelCase = (output_size, output_size) if isinstance(__snake_case , __snake_case ) else output_size _UpperCamelCase , _UpperCamelCase = get_image_size(__snake_case ) _UpperCamelCase , _UpperCamelCase = output_size # determine new height and width _UpperCamelCase = output_height / input_height _UpperCamelCase = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width _UpperCamelCase = scale_width else: # fit height _UpperCamelCase = scale_height _UpperCamelCase = constraint_to_multiple_of(scale_height * input_height , multiple=__snake_case ) _UpperCamelCase = constraint_to_multiple_of(scale_width * input_width , multiple=__snake_case ) return (new_height, new_width) class lowerCAmelCase_ ( __lowercase ): UpperCAmelCase = ["pixel_values"] def __init__( self : List[Any] , _A : bool = True , _A : Dict[str, int] = None , _A : PILImageResampling = PILImageResampling.BILINEAR , _A : bool = False , _A : int = 1 , _A : bool = True , _A : Union[int, float] = 1 / 255 , _A : bool = True , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , **_A : List[str] , ): super().__init__(**_A ) _UpperCamelCase = size if size is not None else {'''height''': 384, '''width''': 384} _UpperCamelCase = get_size_dict(_A ) _UpperCamelCase = do_resize _UpperCamelCase = size _UpperCamelCase = keep_aspect_ratio _UpperCamelCase = ensure_multiple_of _UpperCamelCase = resample _UpperCamelCase = do_rescale _UpperCamelCase = rescale_factor _UpperCamelCase = do_normalize _UpperCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _UpperCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase_ ( self : List[str] , _A : np.ndarray , _A : Dict[str, int] , _A : bool = False , _A : int = 1 , _A : PILImageResampling = PILImageResampling.BICUBIC , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Any , ): _UpperCamelCase = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(F"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) _UpperCamelCase = get_resize_output_image_size( _A , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=_A , multiple=_A , ) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def UpperCamelCase_ ( self : str , _A : np.ndarray , _A : Union[int, float] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Any , ): return rescale(_A , scale=_A , data_format=_A , **_A ) def UpperCamelCase_ ( self : int , _A : np.ndarray , _A : Union[float, List[float]] , _A : Union[float, List[float]] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Any , ): return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def UpperCamelCase_ ( self : Optional[int] , _A : ImageInput , _A : bool = None , _A : int = None , _A : bool = None , _A : int = None , _A : PILImageResampling = None , _A : bool = None , _A : float = None , _A : bool = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[str, TensorType]] = None , _A : ChannelDimension = ChannelDimension.FIRST , **_A : str , ): _UpperCamelCase = do_resize if do_resize is not None else self.do_resize _UpperCamelCase = size if size is not None else self.size _UpperCamelCase = get_size_dict(_A ) _UpperCamelCase = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio _UpperCamelCase = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of _UpperCamelCase = resample if resample is not None else self.resample _UpperCamelCase = do_rescale if do_rescale is not None else self.do_rescale _UpperCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor _UpperCamelCase = do_normalize if do_normalize is not None else self.do_normalize _UpperCamelCase = image_mean if image_mean is not None else self.image_mean _UpperCamelCase = image_std if image_std is not None else self.image_std _UpperCamelCase = make_list_of_images(_A ) if not valid_images(_A ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. _UpperCamelCase = [to_numpy_array(_A ) for image in images] if do_resize: _UpperCamelCase = [self.resize(image=_A , size=_A , resample=_A ) for image in images] if do_rescale: _UpperCamelCase = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: _UpperCamelCase = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] _UpperCamelCase = [to_channel_dimension_format(_A , _A ) for image in images] _UpperCamelCase = {'''pixel_values''': images} return BatchFeature(data=_A , tensor_type=_A ) def UpperCamelCase_ ( self : Any , _A : Any , _A : List[Tuple] = None ): _UpperCamelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_A ) != len(_A ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(_A ): _UpperCamelCase = target_sizes.numpy() _UpperCamelCase = [] for idx in range(len(_A ) ): _UpperCamelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=_A ) _UpperCamelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_A ) else: _UpperCamelCase = logits.argmax(dim=1 ) _UpperCamelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
10
0
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL _UpperCAmelCase = logging.get_logger(__name__) def UpperCamelCase ( __lowercase : List[str] ,__lowercase : Dict ,__lowercase : List[str] ,__lowercase : List[str] ): '''simple docstring''' def constraint_to_multiple_of(__lowercase : Union[str, Any] ,__lowercase : Optional[int] ,__lowercase : Any=0 ,__lowercase : Union[str, Any]=None ): A_ : Union[str, Any] = round(val / multiple ) * multiple if max_val is not None and x > max_val: A_ : Optional[Any] = math.floor(val / multiple ) * multiple if x < min_val: A_ : Union[str, Any] = math.ceil(val / multiple ) * multiple return x A_ : List[Any] = (output_size, output_size) if isinstance(__snake_case ,__snake_case ) else output_size A_ , A_ : Tuple = get_image_size(__snake_case ) A_ , A_ : Dict = output_size # determine new height and width A_ : Tuple = output_height / input_height A_ : List[Any] = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width A_ : str = scale_width else: # fit height A_ : Optional[int] = scale_height A_ : List[str] = constraint_to_multiple_of(scale_height * input_height ,multiple=__snake_case ) A_ : List[str] = constraint_to_multiple_of(scale_width * input_width ,multiple=__snake_case ) return (new_height, new_width) class UpperCAmelCase ( __lowercase ): '''simple docstring''' lowerCamelCase_ = ['''pixel_values'''] def __init__( self , lowercase = True , lowercase = None , lowercase = PILImageResampling.BILINEAR , lowercase = False , lowercase = 1 , lowercase = True , lowercase = 1 / 2_5_5 , lowercase = True , lowercase = None , lowercase = None , **lowercase , ): """simple docstring""" super().__init__(**_A ) A_ : Any = size if size is not None else {'height': 3_8_4, 'width': 3_8_4} A_ : Dict = get_size_dict(_A ) A_ : Union[str, Any] = do_resize A_ : Optional[int] = size A_ : Optional[Any] = keep_aspect_ratio A_ : Union[str, Any] = ensure_multiple_of A_ : List[Any] = resample A_ : Optional[int] = do_rescale A_ : str = rescale_factor A_ : int = do_normalize A_ : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN A_ : Dict = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowerCAmelCase_ ( self , lowercase , lowercase , lowercase = False , lowercase = 1 , lowercase = PILImageResampling.BICUBIC , lowercase = None , **lowercase , ): """simple docstring""" A_ : List[str] = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(F'''The size dictionary must contain the keys \'height\' and \'width\'. Got {size.keys()}''' ) A_ : Any = get_resize_output_image_size( _A , output_size=(size['height'], size['width']) , keep_aspect_ratio=_A , multiple=_A , ) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def lowerCAmelCase_ ( self , lowercase , lowercase , lowercase = None , **lowercase , ): """simple docstring""" return rescale(_A , scale=_A , data_format=_A , **_A ) def lowerCAmelCase_ ( self , lowercase , lowercase , lowercase , lowercase = None , **lowercase , ): """simple docstring""" return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def lowerCAmelCase_ ( self , lowercase , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = None , lowercase = ChannelDimension.FIRST , **lowercase , ): """simple docstring""" A_ : str = do_resize if do_resize is not None else self.do_resize A_ : Union[str, Any] = size if size is not None else self.size A_ : Optional[int] = get_size_dict(_A ) A_ : int = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio A_ : Dict = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of A_ : Optional[int] = resample if resample is not None else self.resample A_ : List[Any] = do_rescale if do_rescale is not None else self.do_rescale A_ : Dict = rescale_factor if rescale_factor is not None else self.rescale_factor A_ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize A_ : Optional[Any] = image_mean if image_mean is not None else self.image_mean A_ : str = image_std if image_std is not None else self.image_std A_ : Dict = make_list_of_images(_A ) if not valid_images(_A ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # All transformations expect numpy arrays. A_ : Optional[Any] = [to_numpy_array(_A ) for image in images] if do_resize: A_ : Dict = [self.resize(image=_A , size=_A , resample=_A ) for image in images] if do_rescale: A_ : Dict = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: A_ : Dict = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] A_ : Any = [to_channel_dimension_format(_A , _A ) for image in images] A_ : str = {'pixel_values': images} return BatchFeature(data=_A , tensor_type=_A ) def lowerCAmelCase_ ( self , lowercase , lowercase = None ): """simple docstring""" A_ : int = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_A ) != len(_A ): raise ValueError( 'Make sure that you pass in as many target sizes as the batch dimension of the logits' ) if is_torch_tensor(_A ): A_ : List[Any] = target_sizes.numpy() A_ : str = [] for idx in range(len(_A ) ): A_ : int = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='bilinear' , align_corners=_A ) A_ : List[str] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_A ) else: A_ : List[Any] = logits.argmax(dim=1 ) A_ : Tuple = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
558
import os import re import shutil import sys import tempfile import unittest import black _lowerCAmelCase = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. _lowerCAmelCase = " \"\"\"\n Output class for the scheduler's step function output.\n\n Args:\n prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n denoising loop.\n pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n `pred_original_sample` can be used to preview progress or for guidance.\n \"\"\"\n\n prev_sample: torch.FloatTensor\n pred_original_sample: Optional[torch.FloatTensor] = None\n" class lowerCAmelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self : List[Any] ): _UpperCamelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , '''schedulers/''' ) ) _UpperCamelCase = self.diffusers_dir shutil.copy( os.path.join(_A , '''src/diffusers/schedulers/scheduling_ddpm.py''' ) , os.path.join(self.diffusers_dir , '''schedulers/scheduling_ddpm.py''' ) , ) def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = '''src/diffusers''' shutil.rmtree(self.diffusers_dir ) def UpperCamelCase_ ( self : str , _A : List[str] , _A : Optional[Any] , _A : List[str] , _A : Optional[int]=None ): _UpperCamelCase = comment + F"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: _UpperCamelCase = comment + F"""\nclass {class_name}(nn.Module):\n""" + overwrite_result _UpperCamelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _UpperCamelCase = black.format_str(_A , mode=_A ) _UpperCamelCase = os.path.join(self.diffusers_dir , '''new_code.py''' ) with open(_A , '''w''' , newline='''\n''' ) as f: f.write(_A ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_A ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_A ) with open(_A , '''r''' ) as f: self.assertTrue(f.read() , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = check_copies.find_code_in_diffusers('''schedulers.scheduling_ddpm.DDPMSchedulerOutput''' ) self.assertEqual(_A , _A ) def UpperCamelCase_ ( self : List[str] ): # Base copy consistency self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , _A , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , re.sub('''DDPM''' , '''Test''' , _A ) , ) # Copy consistency with a really long name _UpperCamelCase = '''TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( F"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , F"""{long_class_name}SchedulerOutput""" , re.sub('''Bert''' , _A , _A ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , _A , overwrite_result=re.sub('''DDPM''' , '''Test''' , _A ) , )
10
0
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { 'asapp/sew-tiny-100k': 'https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json', # See all SEW models at https://huggingface.co/models?filter=sew } class UpperCAmelCase_ ( __lowercase ): UpperCamelCase ="sew" def __init__( self , UpperCamelCase_=32 , UpperCamelCase_=7_68 , UpperCamelCase_=12 , UpperCamelCase_=12 , UpperCamelCase_=30_72 , UpperCamelCase_=2 , UpperCamelCase_="gelu" , UpperCamelCase_=0.1 , UpperCamelCase_=0.1 , UpperCamelCase_=0.1 , UpperCamelCase_=0.0 , UpperCamelCase_=0.1 , UpperCamelCase_=0.1 , UpperCamelCase_=0.0_2 , UpperCamelCase_=1E-5 , UpperCamelCase_="group" , UpperCamelCase_="gelu" , UpperCamelCase_=(64, 1_28, 1_28, 1_28, 1_28, 2_56, 2_56, 2_56, 2_56, 5_12, 5_12, 5_12, 5_12) , UpperCamelCase_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , UpperCamelCase_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , UpperCamelCase_=False , UpperCamelCase_=1_28 , UpperCamelCase_=16 , UpperCamelCase_=True , UpperCamelCase_=0.0_5 , UpperCamelCase_=10 , UpperCamelCase_=2 , UpperCamelCase_=0.0 , UpperCamelCase_=10 , UpperCamelCase_=0 , UpperCamelCase_="mean" , UpperCamelCase_=False , UpperCamelCase_=False , UpperCamelCase_=2_56 , UpperCamelCase_=0 , UpperCamelCase_=1 , UpperCamelCase_=2 , **UpperCamelCase_ , ) -> Any: super().__init__(**_A , pad_token_id=_A , bos_token_id=_A , eos_token_id=_A ) __lowercase : List[Any] = hidden_size __lowercase : Dict = feat_extract_norm __lowercase : Dict = feat_extract_activation __lowercase : Optional[int] = list(_A ) __lowercase : Optional[int] = list(_A ) __lowercase : Any = list(_A ) __lowercase : Union[str, Any] = conv_bias __lowercase : int = num_conv_pos_embeddings __lowercase : List[Any] = num_conv_pos_embedding_groups __lowercase : Union[str, Any] = len(self.conv_dim ) __lowercase : Optional[int] = num_hidden_layers __lowercase : Optional[Any] = intermediate_size __lowercase : Optional[int] = squeeze_factor __lowercase : Union[str, Any] = hidden_act __lowercase : Optional[int] = num_attention_heads __lowercase : Optional[int] = hidden_dropout __lowercase : List[str] = attention_dropout __lowercase : List[str] = activation_dropout __lowercase : Union[str, Any] = feat_proj_dropout __lowercase : str = final_dropout __lowercase : List[str] = layerdrop __lowercase : Dict = layer_norm_eps __lowercase : Optional[Any] = initializer_range __lowercase : Any = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect.''' '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,''' F"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)""" F"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __lowercase : str = apply_spec_augment __lowercase : Optional[int] = mask_time_prob __lowercase : str = mask_time_length __lowercase : Dict = mask_time_min_masks __lowercase : List[Any] = mask_feature_prob __lowercase : str = mask_feature_length __lowercase : List[Any] = mask_feature_min_masks # ctc loss __lowercase : str = ctc_loss_reduction __lowercase : int = ctc_zero_infinity # sequence classification __lowercase : List[str] = use_weighted_layer_sum __lowercase : Dict = classifier_proj_size @property def _lowerCamelCase ( self ) -> Dict: return functools.reduce(operator.mul , self.conv_stride , 1 )
76
import json import logging import os import re import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import datasets import numpy as np import torch import torchaudio from packaging import version from torch import nn import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaProcessor, is_apex_available, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse("1.6"): _lowerCAmelCase = True from torch.cuda.amp import autocast _lowerCAmelCase = logging.getLogger(__name__) def _snake_case ( __snake_case=None , __snake_case=None ): return field(default_factory=lambda: default , metadata=__snake_case ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) UpperCAmelCase = field( default=0.1, metadata={"help": "The dropout ratio for the attention probabilities."} ) UpperCAmelCase = field( default=0.1, metadata={"help": "The dropout ratio for activations inside the fully connected layer."} ) UpperCAmelCase = field( default=0.1, metadata={ "help": "The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler." }, ) UpperCAmelCase = field( default=0.1, metadata={"help": "The dropout probabilitiy for all 1D convolutional layers in feature extractor."}, ) UpperCAmelCase = field( default=0.0_5, metadata={ "help": ( "Propability of each feature vector along the time axis to be chosen as the start of the vector" "span to be masked. Approximately ``mask_time_prob * sequence_length // mask_time_length`` feature" "vectors will be masked along the time axis. This is only relevant if ``apply_spec_augment is True``." ) }, ) UpperCAmelCase = field(default=0.0, metadata={"help": "The LayerDrop probability."} ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = field( default=__lowercase, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) UpperCAmelCase = field( default="train+validation", metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" }, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Overwrite the cached preprocessed datasets or not."} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "The number of processes to use for the preprocessing."}, ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) }, ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of validation examples to this " "value if set." ) }, ) UpperCAmelCase = list_field( default=[",", "?", ".", "!", "-", ";", ":", "\"\"", "%", "'", "\"", "�"], metadata={"help": "A list of characters to remove from the transcripts."}, ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = 42 UpperCAmelCase = True UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None def __call__( self : Union[str, Any] , _A : List[Dict[str, Union[List[int], torch.Tensor]]] ): # split inputs and labels since they have to be of different lenghts and need # different padding methods _UpperCamelCase = [{'''input_values''': feature['''input_values''']} for feature in features] _UpperCamelCase = [{'''input_ids''': feature['''labels''']} for feature in features] _UpperCamelCase = self.processor.pad( _A , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) _UpperCamelCase = self.processor.pad( labels=_A , padding=self.padding , max_length=self.max_length_labels , pad_to_multiple_of=self.pad_to_multiple_of_labels , return_tensors='''pt''' , ) # replace padding with -100 to ignore loss correctly _UpperCamelCase = labels_batch['''input_ids'''].masked_fill(labels_batch.attention_mask.ne(1 ) , -100 ) _UpperCamelCase = labels return batch class lowerCAmelCase_ ( __lowercase ): def UpperCamelCase_ ( self : Dict , _A : nn.Module , _A : Dict[str, Union[torch.Tensor, Any]] ): model.train() _UpperCamelCase = self._prepare_inputs(_A ) if self.use_amp: with autocast(): _UpperCamelCase = self.compute_loss(_A , _A ) else: _UpperCamelCase = self.compute_loss(_A , _A ) if self.args.n_gpu > 1: if model.module.config.ctc_loss_reduction == "mean": _UpperCamelCase = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": _UpperCamelCase = loss.sum() / (inputs['''labels'''] >= 0).sum() else: raise ValueError(F"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']""" ) if self.args.gradient_accumulation_steps > 1: _UpperCamelCase = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(_A ).backward() elif self.use_apex: with amp.scale_loss(_A , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(_A ) else: loss.backward() return loss.detach() def _snake_case ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _UpperCamelCase = 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. _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = parser.parse_args_into_dataclasses() # Detecting last checkpoint. _UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _UpperCamelCase = 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.''' ) # 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 )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # 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}""" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info('''Training/evaluation parameters %s''' , __snake_case ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: _UpperCamelCase = datasets.load_dataset( '''common_voice''' , data_args.dataset_config_name , split=data_args.train_split_name ) _UpperCamelCase = datasets.load_dataset('''common_voice''' , data_args.dataset_config_name , split='''test''' ) # Create and save tokenizer _UpperCamelCase = f"""[{"".join(data_args.chars_to_ignore )}]""" def remove_special_characters(__snake_case ): _UpperCamelCase = re.sub(__snake_case , '''''' , batch['''sentence'''] ).lower() + ''' ''' return batch _UpperCamelCase = train_dataset.map(__snake_case , remove_columns=['''sentence'''] ) _UpperCamelCase = eval_dataset.map(__snake_case , remove_columns=['''sentence'''] ) def extract_all_chars(__snake_case ): _UpperCamelCase = ''' '''.join(batch['''text'''] ) _UpperCamelCase = list(set(__snake_case ) ) return {"vocab": [vocab], "all_text": [all_text]} _UpperCamelCase = train_dataset.map( __snake_case , batched=__snake_case , batch_size=-1 , keep_in_memory=__snake_case , remove_columns=train_dataset.column_names , ) _UpperCamelCase = train_dataset.map( __snake_case , batched=__snake_case , batch_size=-1 , keep_in_memory=__snake_case , remove_columns=eval_dataset.column_names , ) _UpperCamelCase = list(set(vocab_train['''vocab'''][0] ) | set(vocab_test['''vocab'''][0] ) ) _UpperCamelCase = {v: k for k, v in enumerate(__snake_case )} _UpperCamelCase = vocab_dict[''' '''] del vocab_dict[" "] _UpperCamelCase = len(__snake_case ) _UpperCamelCase = len(__snake_case ) with open('''vocab.json''' , '''w''' ) as vocab_file: json.dump(__snake_case , __snake_case ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _UpperCamelCase = WavaVecaCTCTokenizer( '''vocab.json''' , unk_token='''[UNK]''' , pad_token='''[PAD]''' , word_delimiter_token='''|''' , ) _UpperCamelCase = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0.0 , do_normalize=__snake_case , return_attention_mask=__snake_case ) _UpperCamelCase = WavaVecaProcessor(feature_extractor=__snake_case , tokenizer=__snake_case ) _UpperCamelCase = WavaVecaForCTC.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , activation_dropout=model_args.activation_dropout , attention_dropout=model_args.attention_dropout , hidden_dropout=model_args.hidden_dropout , feat_proj_dropout=model_args.feat_proj_dropout , mask_time_prob=model_args.mask_time_prob , gradient_checkpointing=training_args.gradient_checkpointing , layerdrop=model_args.layerdrop , ctc_loss_reduction='''mean''' , pad_token_id=processor.tokenizer.pad_token_id , vocab_size=len(processor.tokenizer ) , ) if data_args.max_train_samples is not None: _UpperCamelCase = min(len(__snake_case ) , data_args.max_train_samples ) _UpperCamelCase = train_dataset.select(range(__snake_case ) ) if data_args.max_val_samples is not None: _UpperCamelCase = eval_dataset.select(range(data_args.max_val_samples ) ) _UpperCamelCase = torchaudio.transforms.Resample(48000 , 16000 ) # Preprocessing the datasets. # We need to read the aduio files as arrays and tokenize the targets. def speech_file_to_array_fn(__snake_case ): _UpperCamelCase , _UpperCamelCase = torchaudio.load(batch['''path'''] ) _UpperCamelCase = resampler(__snake_case ).squeeze().numpy() _UpperCamelCase = 16000 _UpperCamelCase = batch['''text'''] return batch _UpperCamelCase = train_dataset.map( __snake_case , remove_columns=train_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) _UpperCamelCase = eval_dataset.map( __snake_case , remove_columns=eval_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) def prepare_dataset(__snake_case ): # check that all files have the correct sampling rate assert ( len(set(batch['''sampling_rate'''] ) ) == 1 ), f"""Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}.""" _UpperCamelCase = processor( audio=batch['''speech'''] , text=batch['''target_text'''] , sampling_rate=batch['''sampling_rate'''][0] ) batch.update(__snake_case ) return batch _UpperCamelCase = train_dataset.map( __snake_case , remove_columns=train_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=__snake_case , num_proc=data_args.preprocessing_num_workers , ) _UpperCamelCase = eval_dataset.map( __snake_case , remove_columns=eval_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=__snake_case , num_proc=data_args.preprocessing_num_workers , ) # Metric _UpperCamelCase = datasets.load_metric('''wer''' ) def compute_metrics(__snake_case ): _UpperCamelCase = pred.predictions _UpperCamelCase = np.argmax(__snake_case , axis=-1 ) _UpperCamelCase = processor.tokenizer.pad_token_id _UpperCamelCase = processor.batch_decode(__snake_case ) # we do not want to group tokens when computing the metrics _UpperCamelCase = processor.batch_decode(pred.label_ids , group_tokens=__snake_case ) _UpperCamelCase = wer_metric.compute(predictions=__snake_case , references=__snake_case ) return {"wer": wer} if model_args.freeze_feature_extractor: model.freeze_feature_extractor() # Data collator _UpperCamelCase = DataCollatorCTCWithPadding(processor=__snake_case , padding=__snake_case ) # Initialize our Trainer _UpperCamelCase = CTCTrainer( model=__snake_case , data_collator=__snake_case , args=__snake_case , compute_metrics=__snake_case , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=processor.feature_extractor , ) # Training if training_args.do_train: if last_checkpoint is not None: _UpperCamelCase = last_checkpoint elif os.path.isdir(model_args.model_name_or_path ): _UpperCamelCase = model_args.model_name_or_path else: _UpperCamelCase = None # Save the feature_extractor and the tokenizer if is_main_process(training_args.local_rank ): processor.save_pretrained(training_args.output_dir ) _UpperCamelCase = trainer.train(resume_from_checkpoint=__snake_case ) trainer.save_model() _UpperCamelCase = train_result.metrics _UpperCamelCase = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__snake_case ) ) _UpperCamelCase = min(__snake_case , len(__snake_case ) ) trainer.log_metrics('''train''' , __snake_case ) trainer.save_metrics('''train''' , __snake_case ) trainer.save_state() # Evaluation _UpperCamelCase = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) _UpperCamelCase = trainer.evaluate() _UpperCamelCase = data_args.max_val_samples if data_args.max_val_samples is not None else len(__snake_case ) _UpperCamelCase = min(__snake_case , len(__snake_case ) ) trainer.log_metrics('''eval''' , __snake_case ) trainer.save_metrics('''eval''' , __snake_case ) return results if __name__ == "__main__": main()
10
0
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class a_ : def __init__( self : Any , __lowerCAmelCase : int , __lowerCAmelCase : int=1_2 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : Optional[int]=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : str=9_9 , __lowerCAmelCase : str=3_2 , __lowerCAmelCase : int=3_2 , __lowerCAmelCase : Optional[Any]=2 , __lowerCAmelCase : Dict=4 , __lowerCAmelCase : int=3_7 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : str=0.1 , __lowerCAmelCase : Any=5_1_2 , __lowerCAmelCase : int=0.02 , __lowerCAmelCase : Optional[Any]=0 , __lowerCAmelCase : Dict=None , ): __snake_case = parent __snake_case = batch_size __snake_case = seq_length __snake_case = is_training __snake_case = use_input_mask __snake_case = use_labels __snake_case = vocab_size __snake_case = hidden_size __snake_case = projection_dim __snake_case = num_hidden_layers __snake_case = num_attention_heads __snake_case = intermediate_size __snake_case = dropout __snake_case = attention_dropout __snake_case = max_position_embeddings __snake_case = initializer_range __snake_case = scope __snake_case = bos_token_id def lowercase__ ( self : List[str] ): __snake_case = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case = None if self.use_input_mask: __snake_case = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: __snake_case = input_mask.numpy() __snake_case , __snake_case = input_mask.shape __snake_case = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_A ): __snake_case = 1 __snake_case = 0 __snake_case = self.get_config() return config, input_ids, tf.convert_to_tensor(_A ) def lowercase__ ( self : str ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def lowercase__ ( self : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[Any] ): __snake_case = TFBlipTextModel(config=_A ) __snake_case = model(_A , attention_mask=_A , training=_A ) __snake_case = model(_A , training=_A ) 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 lowercase__ ( self : Tuple ): __snake_case = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case = config_and_inputs __snake_case = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class a_ ( __lowercase , unittest.TestCase ): lowercase_ : Dict = (TFBlipTextModel,) if is_tf_available() else () lowercase_ : Any = False lowercase_ : List[Any] = False lowercase_ : List[Any] = False def lowercase__ ( self : Dict ): __snake_case = BlipTextModelTester(self ) __snake_case = ConfigTester(self , config_class=_A , hidden_size=3_7 ) def lowercase__ ( self : Dict ): self.config_tester.run_common_tests() def lowercase__ ( self : int ): __snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def lowercase__ ( self : List[Any] ): pass def lowercase__ ( self : Tuple ): pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def lowercase__ ( self : Dict ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def lowercase__ ( self : Dict ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def lowercase__ ( self : List[str] ): pass @slow def lowercase__ ( self : Optional[int] ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __snake_case = TFBlipTextModel.from_pretrained(_A ) self.assertIsNotNone(_A ) def lowercase__ ( self : int , __lowerCAmelCase : Optional[int]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_A )
356
import math class lowerCAmelCase_ : def __init__( self : Tuple , _A : int=0 ): # a graph with Node 0,1,...,N-1 _UpperCamelCase = n _UpperCamelCase = [ [math.inf for j in range(0 , _A )] for i in range(0 , _A ) ] # adjacency matrix for weight _UpperCamelCase = [ [math.inf for j in range(0 , _A )] for i in range(0 , _A ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase_ ( self : Dict , _A : str , _A : List[str] , _A : Optional[Any] ): _UpperCamelCase = w def UpperCamelCase_ ( self : Optional[int] ): for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): _UpperCamelCase = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase_ ( self : List[str] , _A : Optional[int] , _A : Optional[int] ): return self.dp[u][v] if __name__ == "__main__": _lowerCAmelCase = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
10
0
'''simple docstring''' import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def snake_case_ ( __snake_case : List[str] , __snake_case : Union[str, Any] , __snake_case : Tuple , __snake_case : Union[str, Any] , __snake_case : List[str]) -> Any: # Load configuration defined in the metadata file with open(__snake_case) as metadata_file: lowerCAmelCase_ = json.load(__snake_case) lowerCAmelCase_ = LukeConfig(use_entity_aware_attention=__snake_case , **metadata['''model_config''']) # Load in the weights from the checkpoint_path lowerCAmelCase_ = torch.load(__snake_case , map_location='''cpu''')['''module'''] # Load the entity vocab file lowerCAmelCase_ = load_original_entity_vocab(__snake_case) # add an entry for [MASK2] lowerCAmelCase_ = max(entity_vocab.values()) + 1 config.entity_vocab_size += 1 lowerCAmelCase_ = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name''']) # Add special tokens to the token vocabulary for downstream tasks lowerCAmelCase_ = AddedToken('''<ent>''' , lstrip=__snake_case , rstrip=__snake_case) lowerCAmelCase_ = AddedToken('''<ent2>''' , lstrip=__snake_case , rstrip=__snake_case) tokenizer.add_special_tokens({'''additional_special_tokens''': [entity_token_a, entity_token_a]}) config.vocab_size += 2 print(F'''Saving tokenizer to {pytorch_dump_folder_path}''') tokenizer.save_pretrained(__snake_case) with open(os.path.join(__snake_case , '''tokenizer_config.json''') , '''r''') as f: lowerCAmelCase_ = json.load(__snake_case) lowerCAmelCase_ = '''MLukeTokenizer''' with open(os.path.join(__snake_case , '''tokenizer_config.json''') , '''w''') as f: json.dump(__snake_case , __snake_case) with open(os.path.join(__snake_case , MLukeTokenizer.vocab_files_names['''entity_vocab_file''']) , '''w''') as f: json.dump(__snake_case , __snake_case) lowerCAmelCase_ = MLukeTokenizer.from_pretrained(__snake_case) # Initialize the embeddings of the special tokens lowerCAmelCase_ = tokenizer.convert_tokens_to_ids(['''@'''])[0] lowerCAmelCase_ = tokenizer.convert_tokens_to_ids(['''#'''])[0] lowerCAmelCase_ = state_dict['''embeddings.word_embeddings.weight'''] lowerCAmelCase_ = word_emb[ent_init_index].unsqueeze(0) lowerCAmelCase_ = word_emb[enta_init_index].unsqueeze(0) lowerCAmelCase_ = torch.cat([word_emb, ent_emb, enta_emb]) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: lowerCAmelCase_ = state_dict[bias_name] lowerCAmelCase_ = decoder_bias[ent_init_index].unsqueeze(0) lowerCAmelCase_ = decoder_bias[enta_init_index].unsqueeze(0) lowerCAmelCase_ = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias]) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers): for matrix_name in ["query.weight", "query.bias"]: lowerCAmelCase_ = F'''encoder.layer.{layer_index}.attention.self.''' lowerCAmelCase_ = state_dict[prefix + matrix_name] lowerCAmelCase_ = state_dict[prefix + matrix_name] lowerCAmelCase_ = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks lowerCAmelCase_ = state_dict['''entity_embeddings.entity_embeddings.weight'''] lowerCAmelCase_ = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0) lowerCAmelCase_ = torch.cat([entity_emb, entity_mask_emb]) # add [MASK2] for 'entity_predictions.bias' lowerCAmelCase_ = state_dict['''entity_predictions.bias'''] lowerCAmelCase_ = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0) lowerCAmelCase_ = torch.cat([entity_prediction_bias, entity_mask_bias]) lowerCAmelCase_ = LukeForMaskedLM(config=__snake_case).eval() state_dict.pop('''entity_predictions.decoder.weight''') state_dict.pop('''lm_head.decoder.weight''') state_dict.pop('''lm_head.decoder.bias''') lowerCAmelCase_ = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''') or key.startswith('''entity_predictions''')): lowerCAmelCase_ = state_dict[key] else: lowerCAmelCase_ = state_dict[key] lowerCAmelCase_ ,lowerCAmelCase_ = model.load_state_dict(__snake_case , strict=__snake_case) if set(__snake_case) != {"luke.embeddings.position_ids"}: raise ValueError(F'''Unexpected unexpected_keys: {unexpected_keys}''') if set(__snake_case) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F'''Unexpected missing_keys: {missing_keys}''') model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs lowerCAmelCase_ = MLukeTokenizer.from_pretrained(__snake_case , task='''entity_classification''') lowerCAmelCase_ = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' lowerCAmelCase_ = (0, 9) lowerCAmelCase_ = tokenizer(__snake_case , entity_spans=[span] , return_tensors='''pt''') lowerCAmelCase_ = model(**__snake_case) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base lowerCAmelCase_ = torch.Size((1, 33, 768)) lowerCAmelCase_ = torch.tensor([[0.0_8_9_2, 0.0_5_9_6, -0.2_8_1_9], [0.0_1_3_4, 0.1_1_9_9, 0.0_5_7_3], [-0.0_1_6_9, 0.0_9_2_7, 0.0_6_4_4]]) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F'''Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}''') if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , __snake_case , atol=1E-4): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base lowerCAmelCase_ = torch.Size((1, 1, 768)) lowerCAmelCase_ = torch.tensor([[-0.1_4_8_2, 0.0_6_0_9, 0.0_3_2_2]]) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F'''Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is''' F''' {expected_shape}''') if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , __snake_case , atol=1E-4): raise ValueError # Verify masked word/entity prediction lowerCAmelCase_ = MLukeTokenizer.from_pretrained(__snake_case) lowerCAmelCase_ = '''Tokyo is the capital of <mask>.''' lowerCAmelCase_ = (24, 30) lowerCAmelCase_ = tokenizer(__snake_case , entity_spans=[span] , return_tensors='''pt''') lowerCAmelCase_ = model(**__snake_case) lowerCAmelCase_ = encoding['''input_ids'''][0].tolist() lowerCAmelCase_ = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''')) lowerCAmelCase_ = outputs.logits[0][mask_position_id].argmax(dim=-1) assert "Japan" == tokenizer.decode(__snake_case) lowerCAmelCase_ = outputs.entity_logits[0][0].argmax().item() lowerCAmelCase_ = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''')][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(__snake_case)) model.save_pretrained(__snake_case) def snake_case_ ( __snake_case : List[str]) -> Dict: lowerCAmelCase_ = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] lowerCAmelCase_ = [json.loads(__snake_case) for line in open(__snake_case)] lowerCAmelCase_ = {} for entry in data: lowerCAmelCase_ = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: lowerCAmelCase_ = entity_id break lowerCAmelCase_ = F'''{language}:{entity_name}''' lowerCAmelCase_ = entity_id return new_mapping if __name__ == "__main__": A_ : Union[str, Any] =argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) A_ : Optional[Any] =parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging _lowerCAmelCase = logging.get_logger(__name__) def _snake_case ( __snake_case=None , __snake_case=None ): return field(default_factory=lambda: default , metadata=__snake_case ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = list_field( default=[], metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) }, ) UpperCAmelCase = list_field( default=[8], metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) UpperCAmelCase = list_field( default=[8, 32, 128, 512], metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Use FP16 to accelerate inference."} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Benchmark training of model"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Verbose memory tracing"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."}, ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" }, ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Trace memory line by line"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Save result to a CSV file"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Save all print statements in a log file"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Whether to print environment information"} ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) }, ) UpperCAmelCase = field( default=F"""inference_time_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving time results to csv."}, ) UpperCAmelCase = field( default=F"""inference_memory_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving memory results to csv."}, ) UpperCAmelCase = field( default=F"""train_time_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving time results to csv for training."}, ) UpperCAmelCase = field( default=F"""train_memory_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving memory results to csv for training."}, ) UpperCAmelCase = field( default=F"""env_info_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving environment information."}, ) UpperCAmelCase = field( default=F"""log_{round(time() )}.csv""", metadata={"help": "Log filename used if print statements are saved in log."}, ) UpperCAmelCase = field(default=3, metadata={"help": "Times an experiment will be run."} ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) }, ) def UpperCamelCase_ ( self : Union[str, Any] ): warnings.warn( F"""The class {self.__class__} is deprecated. Hugging Face Benchmarking utils""" ''' are deprecated in general and it is advised to use external Benchmarking libraries ''' ''' to benchmark Transformer models.''' , _A , ) def UpperCamelCase_ ( self : str ): return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def UpperCamelCase_ ( self : List[Any] ): if len(self.models ) <= 0: raise ValueError( '''Please make sure you provide at least one model name / model identifier, *e.g.* `--models''' ''' bert-base-cased` or `args.models = [\'bert-base-cased\'].''' ) return self.models @property def UpperCamelCase_ ( self : Optional[int] ): if not self.multi_process: return False elif self.is_tpu: logger.info('''Multiprocessing is currently not possible on TPU.''' ) return False else: return True
10
0
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class UpperCamelCase_ : def __init__( self :Union[str, Any] , __A :List[Any] , __A :List[Any]=2 , __A :Tuple=True , __A :Any=False , __A :Dict=10 , __A :Optional[int]=3 , __A :Union[str, Any]=32 * 8 , __A :Optional[Any]=32 * 8 , __A :List[str]=4 , __A :Dict=64 , ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE__ = parent SCREAMING_SNAKE_CASE__ = batch_size SCREAMING_SNAKE_CASE__ = is_training SCREAMING_SNAKE_CASE__ = use_auxiliary_loss SCREAMING_SNAKE_CASE__ = num_queries SCREAMING_SNAKE_CASE__ = num_channels SCREAMING_SNAKE_CASE__ = min_size SCREAMING_SNAKE_CASE__ = max_size SCREAMING_SNAKE_CASE__ = num_labels SCREAMING_SNAKE_CASE__ = hidden_dim SCREAMING_SNAKE_CASE__ = hidden_dim def _snake_case ( self :List[Any] ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( _A ) SCREAMING_SNAKE_CASE__ = torch.ones([self.batch_size, self.min_size, self.max_size] , device=_A ) SCREAMING_SNAKE_CASE__ = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=_A ) > 0.5 ).float() SCREAMING_SNAKE_CASE__ = (torch.rand((self.batch_size, self.num_labels) , device=_A ) > 0.5).long() SCREAMING_SNAKE_CASE__ = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def _snake_case ( self :int ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ = MaskaFormerConfig( hidden_size=self.hidden_dim , ) SCREAMING_SNAKE_CASE__ = self.num_queries SCREAMING_SNAKE_CASE__ = self.num_labels SCREAMING_SNAKE_CASE__ = [1, 1, 1, 1] SCREAMING_SNAKE_CASE__ = self.num_channels SCREAMING_SNAKE_CASE__ = 64 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = self.hidden_dim SCREAMING_SNAKE_CASE__ = self.hidden_dim SCREAMING_SNAKE_CASE__ = self.hidden_dim return config def _snake_case ( self :List[str] ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE__ = {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask} return config, inputs_dict def _snake_case ( self :List[str] , __A :Optional[int] , __A :Union[str, Any] ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ = output.encoder_hidden_states SCREAMING_SNAKE_CASE__ = output.pixel_decoder_hidden_states SCREAMING_SNAKE_CASE__ = output.transformer_decoder_hidden_states self.parent.assertTrue(len(_A ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_A ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_A ) , config.decoder_layers ) def _snake_case ( self :Union[str, Any] , __A :List[Any] , __A :Dict , __A :int , __A :List[Any]=False ) -> int: """simple docstring""" with torch.no_grad(): SCREAMING_SNAKE_CASE__ = MaskaFormerModel(config=_A ) model.to(_A ) model.eval() SCREAMING_SNAKE_CASE__ = model(pixel_values=_A , pixel_mask=_A ) SCREAMING_SNAKE_CASE__ = model(_A , output_hidden_states=_A ) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.hidden_dim) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(_A , _A ) def _snake_case ( self :Union[str, Any] , __A :Optional[Any] , __A :Any , __A :Any , __A :Any , __A :Tuple ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ = MaskaFormerForUniversalSegmentation(config=_A ) model.to(_A ) model.eval() def comm_check_on_output(__A :List[Any] ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): SCREAMING_SNAKE_CASE__ = model(pixel_values=_A , pixel_mask=_A ) SCREAMING_SNAKE_CASE__ = model(_A ) comm_check_on_output(_A ) SCREAMING_SNAKE_CASE__ = model( pixel_values=_A , pixel_mask=_A , mask_labels=_A , class_labels=_A ) comm_check_on_output(_A ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class UpperCamelCase_ ( __lowercase , __lowercase , unittest.TestCase ): lowerCamelCase_ = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () lowerCamelCase_ = {"feature-extraction": MaskaFormerModel} if is_torch_available() else {} lowerCamelCase_ = False lowerCamelCase_ = False lowerCamelCase_ = False lowerCamelCase_ = False def _snake_case ( self :Union[str, Any] ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ = MaskaFormerModelTester(self ) SCREAMING_SNAKE_CASE__ = ConfigTester(self , config_class=_A , has_text_modality=_A ) def _snake_case ( self :Optional[Any] ) -> str: """simple docstring""" self.config_tester.run_common_tests() def _snake_case ( self :List[str] ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(_A , **_A , output_hidden_states=_A ) def _snake_case ( self :Tuple ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_A ) @unittest.skip(reason="""Mask2Former does not use inputs_embeds""" ) def _snake_case ( self :Dict ) -> List[Any]: """simple docstring""" pass @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method""" ) def _snake_case ( self :str ) -> int: """simple docstring""" pass @unittest.skip(reason="""Mask2Former is not a generative model""" ) def _snake_case ( self :Union[str, Any] ) -> str: """simple docstring""" pass @unittest.skip(reason="""Mask2Former does not use token embeddings""" ) def _snake_case ( self :List[str] ) -> Any: """simple docstring""" pass @require_torch_multi_gpu @unittest.skip( reason="""Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`""" ) def _snake_case ( self :Tuple ) -> int: """simple docstring""" pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def _snake_case ( self :Union[str, Any] ) -> Tuple: """simple docstring""" pass def _snake_case ( self :Optional[int] ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ = model_class(_A ) SCREAMING_SNAKE_CASE__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE__ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _A ) @slow def _snake_case ( self :int ) -> List[str]: """simple docstring""" for model_name in ["facebook/mask2former-swin-small-coco-instance"]: SCREAMING_SNAKE_CASE__ = MaskaFormerModel.from_pretrained(_A ) self.assertIsNotNone(_A ) def _snake_case ( self :Union[str, Any] ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ = (self.model_tester.min_size,) * 2 SCREAMING_SNAKE_CASE__ = { """pixel_values""": torch.randn((2, 3, *size) , device=_A ), """mask_labels""": torch.randn((2, 10, *size) , device=_A ), """class_labels""": torch.zeros(2 , 10 , device=_A ).long(), } SCREAMING_SNAKE_CASE__ = self.model_tester.get_config() SCREAMING_SNAKE_CASE__ = MaskaFormerForUniversalSegmentation(_A ).to(_A ) SCREAMING_SNAKE_CASE__ = model(**_A ) self.assertTrue(outputs.loss is not None ) def _snake_case ( self :Tuple ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(_A , **_A , output_hidden_states=_A ) def _snake_case ( self :List[str] ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ = model_class(_A ).to(_A ) SCREAMING_SNAKE_CASE__ = model(**_A , output_attentions=_A ) self.assertTrue(outputs.attentions is not None ) def _snake_case ( self :Optional[Any] ) -> Dict: """simple docstring""" if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE__ = self.all_model_classes[1] SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE__ = model_class(_A ) model.to(_A ) model.train() SCREAMING_SNAKE_CASE__ = model(_A , mask_labels=_A , class_labels=_A ).loss loss.backward() def _snake_case ( self :Tuple ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE__ = self.all_model_classes[1] SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = model_class(_A ).to(_A ) model.train() SCREAMING_SNAKE_CASE__ = model(_A , mask_labels=_A , class_labels=_A ) SCREAMING_SNAKE_CASE__ = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE__ = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE__ = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE__ = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=_A ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) _lowerCamelCase = 1e-4 def SCREAMING_SNAKE_CASE__ ( ): SCREAMING_SNAKE_CASE__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_vision @slow class UpperCamelCase_ ( unittest.TestCase ): @cached_property def _snake_case ( self :Any ) -> Any: """simple docstring""" return "facebook/mask2former-swin-small-coco-instance" @cached_property def _snake_case ( self :Union[str, Any] ) -> Any: """simple docstring""" return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def _snake_case ( self :Optional[Any] ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE__ = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(_A ) SCREAMING_SNAKE_CASE__ = self.default_image_processor SCREAMING_SNAKE_CASE__ = prepare_img() SCREAMING_SNAKE_CASE__ = image_processor(_A , return_tensors="""pt""" ).to(_A ) SCREAMING_SNAKE_CASE__ = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_A , (1, 3, 384, 384) ) with torch.no_grad(): SCREAMING_SNAKE_CASE__ = model(**_A ) SCREAMING_SNAKE_CASE__ = torch.tensor( [[-0.2_7_9_0, -1.0_7_1_7, -1.1_6_6_8], [-0.5_1_2_8, -0.3_1_2_8, -0.4_9_8_7], [-0.5_8_3_2, 0.1_9_7_1, -0.0_1_9_7]] ).to(_A ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , _A , atol=_A ) ) SCREAMING_SNAKE_CASE__ = torch.tensor( [[0.8_9_7_3, 1.1_8_4_7, 1.1_7_7_6], [1.1_9_3_4, 1.5_0_4_0, 1.5_1_2_8], [1.1_1_5_3, 1.4_4_8_6, 1.4_9_5_1]] ).to(_A ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , _A , atol=_A ) ) SCREAMING_SNAKE_CASE__ = torch.tensor( [[2.1_1_5_2, 1.7_0_0_0, -0.8_6_0_3], [1.5_8_0_8, 1.8_0_0_4, -0.9_3_5_3], [1.6_0_4_3, 1.7_4_9_5, -0.5_9_9_9]] ).to(_A ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , _A , atol=_A ) ) def _snake_case ( self :List[Any] ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(_A ).eval() SCREAMING_SNAKE_CASE__ = self.default_image_processor SCREAMING_SNAKE_CASE__ = prepare_img() SCREAMING_SNAKE_CASE__ = image_processor(_A , return_tensors="""pt""" ).to(_A ) SCREAMING_SNAKE_CASE__ = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_A , (1, 3, 384, 384) ) with torch.no_grad(): SCREAMING_SNAKE_CASE__ = model(**_A ) # masks_queries_logits SCREAMING_SNAKE_CASE__ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) SCREAMING_SNAKE_CASE__ = [ [-8.7_8_3_9, -9.0_0_5_6, -8.8_1_2_1], [-7.4_1_0_4, -7.0_3_1_3, -6.5_4_0_1], [-6.6_1_0_5, -6.3_4_2_7, -6.4_6_7_5], ] SCREAMING_SNAKE_CASE__ = torch.tensor(_A ).to(_A ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _A , atol=_A ) ) # class_queries_logits SCREAMING_SNAKE_CASE__ = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1) ) SCREAMING_SNAKE_CASE__ = torch.tensor( [ [1.8_3_2_4, -8.0_8_3_5, -4.1_9_2_2], [0.8_4_5_0, -9.0_0_5_0, -3.6_0_5_3], [0.3_0_4_5, -7.7_2_9_3, -3.0_2_7_5], ] ).to(_A ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _A , atol=_A ) ) def _snake_case ( self :str ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(_A ).eval() SCREAMING_SNAKE_CASE__ = self.default_image_processor SCREAMING_SNAKE_CASE__ = image_processor( [np.zeros((3, 800, 1333) ), np.zeros((3, 800, 1333) )] , segmentation_maps=[np.zeros((384, 384) ).astype(np.floataa ), np.zeros((384, 384) ).astype(np.floataa )] , return_tensors="""pt""" , ) SCREAMING_SNAKE_CASE__ = inputs["""pixel_values"""].to(_A ) SCREAMING_SNAKE_CASE__ = [el.to(_A ) for el in inputs["""mask_labels"""]] SCREAMING_SNAKE_CASE__ = [el.to(_A ) for el in inputs["""class_labels"""]] with torch.no_grad(): SCREAMING_SNAKE_CASE__ = model(**_A ) self.assertTrue(outputs.loss is not None )
6
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case ( *__snake_case , __snake_case = None , __snake_case=True , __snake_case=2 ): from .. import __version__ _UpperCamelCase = take_from _UpperCamelCase = () if not isinstance(args[0] , __snake_case ): _UpperCamelCase = (args,) for attribute, version_name, message in args: if version.parse(version.parse(__snake_case ).base_version ) >= version.parse(__snake_case ): raise ValueError( f"""The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'""" f""" version {__version__} is >= {version_name}""" ) _UpperCamelCase = None if isinstance(__snake_case , __snake_case ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(__snake_case ),) _UpperCamelCase = f"""The `{attribute}` argument is deprecated and will be removed in version {version_name}.""" elif hasattr(__snake_case , __snake_case ): values += (getattr(__snake_case , __snake_case ),) _UpperCamelCase = f"""The `{attribute}` attribute is deprecated and will be removed in version {version_name}.""" elif deprecated_kwargs is None: _UpperCamelCase = f"""`{attribute}` is deprecated and will be removed in version {version_name}.""" if warning is not None: _UpperCamelCase = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , __snake_case , stacklevel=__snake_case ) if isinstance(__snake_case , __snake_case ) and len(__snake_case ) > 0: _UpperCamelCase = inspect.getouterframes(inspect.currentframe() )[1] _UpperCamelCase = call_frame.filename _UpperCamelCase = call_frame.lineno _UpperCamelCase = call_frame.function _UpperCamelCase , _UpperCamelCase = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f"""{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`""" ) if len(__snake_case ) == 0: return elif len(__snake_case ) == 1: return values[0] return values
10
0
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 lowercase_ ( __lowercase , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = CpmAntTokenizer UpperCAmelCase_ : Optional[int] = False def SCREAMING_SNAKE_CASE_ ( self ) ->int: super().setUp() lowerCAmelCase = [ '''<d>''', '''</d>''', '''<s>''', '''</s>''', '''</_>''', '''<unk>''', '''<pad>''', '''</n>''', '''我''', '''是''', '''C''', '''P''', '''M''', '''A''', '''n''', '''t''', ] lowerCAmelCase = 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 SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = CpmAntTokenizer.from_pretrained('''openbmb/cpm-ant-10b''' ) lowerCAmelCase = '''今天天气真好!''' lowerCAmelCase = ['''今天''', '''天气''', '''真''', '''好''', '''!'''] lowerCAmelCase = tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) lowerCAmelCase = '''今天天气真好!''' lowerCAmelCase = [tokenizer.bos_token] + tokens lowerCAmelCase = [6, 9802, 14962, 2082, 831, 244] self.assertListEqual(tokenizer.convert_tokens_to_ids(_A ) , _A ) lowerCAmelCase = tokenizer.decode(_A ) self.assertEqual(_A , _A )
312
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process _lowerCAmelCase = logging.getLogger(__name__) def _snake_case ( __snake_case , __snake_case ): return (preds == labels).mean() @dataclass class lowerCAmelCase_ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Pretrained config name or path if not the same as model_name"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) UpperCAmelCase = field(metadata={"help": "Should contain the data files for the task."} ) UpperCAmelCase = 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." ) }, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Overwrite the cached training and evaluation sets"} ) def _snake_case ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = 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.''' ) # 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''' , __snake_case ) # Set seed set_seed(training_args.seed ) try: _UpperCamelCase = processors[data_args.task_name]() _UpperCamelCase = processor.get_labels() _UpperCamelCase = len(__snake_case ) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _UpperCamelCase = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__snake_case , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) _UpperCamelCase = 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 , ) _UpperCamelCase = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__snake_case , cache_dir=model_args.cache_dir , ) # Get datasets _UpperCamelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__snake_case , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) _UpperCamelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__snake_case , task=data_args.task_name , 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 compute_metrics(__snake_case ) -> Dict: _UpperCamelCase = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(__snake_case , p.label_ids )} # Data collator _UpperCamelCase = DataCollatorWithPadding(__snake_case , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer _UpperCamelCase = Trainer( model=__snake_case , args=__snake_case , train_dataset=__snake_case , eval_dataset=__snake_case , compute_metrics=__snake_case , data_collator=__snake_case , ) # 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_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation _UpperCamelCase = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) _UpperCamelCase = trainer.evaluate() _UpperCamelCase = os.path.join(training_args.output_dir , '''eval_results.txt''' ) if trainer.is_world_master(): with open(__snake_case , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in result.items(): logger.info(''' %s = %s''' , __snake_case , __snake_case ) writer.write('''%s = %s\n''' % (key, value) ) results.update(__snake_case ) return results def _snake_case ( __snake_case ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
10
0
"""simple docstring""" from ...utils import logging from ..ta.modeling_tf_ta import TFTaEncoderModel, TFTaForConditionalGeneration, TFTaModel from .configuration_mta import MTaConfig _lowerCAmelCase : Tuple = logging.get_logger(__name__) _lowerCAmelCase : str = "T5Config" class UpperCAmelCase_ ( __lowercase ): __SCREAMING_SNAKE_CASE : Tuple = 'mt5' __SCREAMING_SNAKE_CASE : int = MTaConfig class UpperCAmelCase_ ( __lowercase ): __SCREAMING_SNAKE_CASE : List[Any] = 'mt5' __SCREAMING_SNAKE_CASE : Tuple = MTaConfig class UpperCAmelCase_ ( __lowercase ): __SCREAMING_SNAKE_CASE : Union[str, Any] = 'mt5' __SCREAMING_SNAKE_CASE : str = MTaConfig
289
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCAmelCase = logging.get_logger(__name__) _lowerCAmelCase = { "microsoft/trocr-base-handwritten": ( "https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json" ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class lowerCAmelCase_ ( __lowercase ): UpperCAmelCase = "trocr" UpperCAmelCase = ["past_key_values"] UpperCAmelCase = { "num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model", "num_hidden_layers": "decoder_layers", } def __init__( self : List[str] , _A : Optional[Any]=5_0265 , _A : Optional[Any]=1024 , _A : Optional[Any]=12 , _A : Any=16 , _A : Any=4096 , _A : Optional[Any]="gelu" , _A : Union[str, Any]=512 , _A : Dict=0.1 , _A : List[str]=0.0 , _A : Optional[Any]=0.0 , _A : Union[str, Any]=2 , _A : Any=0.02 , _A : List[str]=0.0 , _A : List[str]=True , _A : str=False , _A : List[str]=True , _A : Optional[Any]=True , _A : Optional[int]=1 , _A : int=0 , _A : Any=2 , **_A : Optional[int] , ): _UpperCamelCase = vocab_size _UpperCamelCase = d_model _UpperCamelCase = decoder_layers _UpperCamelCase = decoder_attention_heads _UpperCamelCase = decoder_ffn_dim _UpperCamelCase = activation_function _UpperCamelCase = max_position_embeddings _UpperCamelCase = dropout _UpperCamelCase = attention_dropout _UpperCamelCase = activation_dropout _UpperCamelCase = init_std _UpperCamelCase = decoder_layerdrop _UpperCamelCase = use_cache _UpperCamelCase = scale_embedding _UpperCamelCase = use_learned_position_embeddings _UpperCamelCase = layernorm_embedding super().__init__( pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , decoder_start_token_id=_A , **_A , )
10
0
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process _lowercase : Optional[int] =logging.getLogger(__name__) def lowerCAmelCase_ ( _lowercase : Optional[int] , _lowercase : Union[str, Any]) -> str: """simple docstring""" return (preds == labels).mean() @dataclass class snake_case__ : """simple docstring""" __lowerCAmelCase :Any = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) __lowerCAmelCase :Any = field( default=__lowercase , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) __lowerCAmelCase :int = field( default=__lowercase , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) __lowerCAmelCase :Any = field( default=__lowercase , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class snake_case__ : """simple docstring""" __lowerCAmelCase :str = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) __lowerCAmelCase :str = field(metadata={"help": "Should contain the data files for the task."} ) __lowerCAmelCase :List[Any] = 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." ) } , ) __lowerCAmelCase :List[Any] = field( default=__lowercase , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def lowerCAmelCase_ ( ) -> Optional[int]: """simple docstring""" # 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. a__ : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) a__ , a__ , a__ : Optional[int] = 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.""") # 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""" , __snake_case) # Set seed set_seed(training_args.seed) try: a__ : Any = processors[data_args.task_name]() a__ : Dict = processor.get_labels() a__ : Union[str, Any] = len(__snake_case) except KeyError: raise ValueError("""Task not found: %s""" % (data_args.task_name)) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. a__ : Any = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__snake_case , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) a__ : Optional[Any] = 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 , ) a__ : Union[str, Any] = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path) , config=__snake_case , cache_dir=model_args.cache_dir , ) # Get datasets a__ : int = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__snake_case , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) a__ : int = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__snake_case , task=data_args.task_name , 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 compute_metrics(_lowercase : List[Any]) -> Dict: a__ : Tuple = np.argmax(p.predictions , axis=1) return {"acc": simple_accuracy(__snake_case , p.label_ids)} # Data collator a__ : str = DataCollatorWithPadding(__snake_case , pad_to_multiple_of=8) if training_args.fpaa else None # Initialize our Trainer a__ : Optional[int] = Trainer( model=__snake_case , args=__snake_case , train_dataset=__snake_case , eval_dataset=__snake_case , compute_metrics=__snake_case , data_collator=__snake_case , ) # 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_master(): tokenizer.save_pretrained(training_args.output_dir) # Evaluation a__ : Union[str, Any] = {} if training_args.do_eval: logger.info("""*** Evaluate ***""") a__ : str = trainer.evaluate() a__ : List[Any] = os.path.join(training_args.output_dir , """eval_results.txt""") if trainer.is_world_master(): with open(__snake_case , """w""") as writer: logger.info("""***** Eval results *****""") for key, value in result.items(): logger.info(""" %s = %s""" , __snake_case , __snake_case) writer.write("""%s = %s\n""" % (key, value)) results.update(__snake_case) return results def lowerCAmelCase_ ( _lowercase : Optional[int]) -> Any: """simple docstring""" # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
136
import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, 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 ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase_ ( __lowercase ): def __init__( self : Union[str, Any] , _A : Optional[Any] , _A : Any=13 , _A : Union[str, Any]=7 , _A : List[str]=True , _A : List[str]=True , _A : List[str]=True , _A : List[str]=True , _A : List[Any]=True , _A : Optional[int]=False , _A : Any=False , _A : int=False , _A : Optional[Any]=2 , _A : Any=99 , _A : str=0 , _A : Union[str, Any]=32 , _A : List[Any]=5 , _A : Tuple=4 , _A : List[str]=0.1 , _A : Union[str, Any]=0.1 , _A : int=512 , _A : Union[str, Any]=12 , _A : List[str]=2 , _A : int=0.02 , _A : Optional[Any]=3 , _A : Any=4 , _A : Optional[int]="last" , _A : Any=None , _A : Dict=None , ): _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = seq_length _UpperCamelCase = is_training _UpperCamelCase = use_input_lengths _UpperCamelCase = use_token_type_ids _UpperCamelCase = use_labels _UpperCamelCase = gelu_activation _UpperCamelCase = sinusoidal_embeddings _UpperCamelCase = causal _UpperCamelCase = asm _UpperCamelCase = n_langs _UpperCamelCase = vocab_size _UpperCamelCase = n_special _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = max_position_embeddings _UpperCamelCase = type_vocab_size _UpperCamelCase = type_sequence_label_size _UpperCamelCase = initializer_range _UpperCamelCase = num_labels _UpperCamelCase = num_choices _UpperCamelCase = summary_type _UpperCamelCase = use_proj _UpperCamelCase = scope def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCamelCase = None if self.use_input_lengths: _UpperCamelCase = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length _UpperCamelCase = None if self.use_token_type_ids: _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None if self.use_labels: _UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCamelCase = ids_tensor([self.batch_size] , 2 ).float() _UpperCamelCase = ids_tensor([self.batch_size] , self.num_choices ) _UpperCamelCase = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def UpperCamelCase_ ( self : str ): return FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , ) def UpperCamelCase_ ( self : str , _A : Union[str, Any] , _A : Optional[Any] , _A : str , _A : Tuple , _A : List[str] , _A : List[Any] , _A : Any , _A : str , _A : Optional[int] , ): _UpperCamelCase = FlaubertModel(config=_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A , lengths=_A , langs=_A ) _UpperCamelCase = model(_A , langs=_A ) _UpperCamelCase = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase_ ( self : Tuple , _A : List[Any] , _A : str , _A : Optional[int] , _A : Optional[Any] , _A : List[str] , _A : int , _A : str , _A : List[Any] , _A : Any , ): _UpperCamelCase = FlaubertWithLMHeadModel(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase_ ( self : Tuple , _A : List[str] , _A : List[str] , _A : Optional[Any] , _A : Union[str, Any] , _A : str , _A : List[str] , _A : Tuple , _A : Optional[int] , _A : Dict , ): _UpperCamelCase = FlaubertForQuestionAnsweringSimple(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A ) _UpperCamelCase = model(_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 : Tuple , _A : str , _A : Tuple , _A : Tuple , _A : Union[str, Any] , _A : List[str] , _A : int , _A : str , _A : Dict , _A : List[Any] , ): _UpperCamelCase = FlaubertForQuestionAnswering(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A ) _UpperCamelCase = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , p_mask=_A , ) _UpperCamelCase = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , ) ((_UpperCamelCase) , ) = result_with_labels.to_tuple() _UpperCamelCase = model(_A , start_positions=_A , end_positions=_A ) ((_UpperCamelCase) , ) = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def UpperCamelCase_ ( self : List[Any] , _A : Union[str, Any] , _A : Tuple , _A : str , _A : int , _A : int , _A : Optional[int] , _A : Optional[int] , _A : int , _A : List[str] , ): _UpperCamelCase = FlaubertForSequenceClassification(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A ) _UpperCamelCase = model(_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCamelCase_ ( self : Optional[int] , _A : List[str] , _A : Optional[Any] , _A : str , _A : Union[str, Any] , _A : List[Any] , _A : int , _A : List[Any] , _A : str , _A : List[str] , ): _UpperCamelCase = self.num_labels _UpperCamelCase = FlaubertForTokenClassification(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A , attention_mask=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase_ ( self : Tuple , _A : Dict , _A : str , _A : Optional[Any] , _A : List[str] , _A : Any , _A : Optional[int] , _A : Optional[Any] , _A : List[Any] , _A : List[str] , ): _UpperCamelCase = self.num_choices _UpperCamelCase = FlaubertForMultipleChoice(config=_A ) model.to(_A ) model.eval() _UpperCamelCase = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCamelCase = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCamelCase = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCamelCase = model( _A , attention_mask=_A , token_type_ids=_A , labels=_A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase_ ( self : Tuple ): _UpperCamelCase = self.prepare_config_and_inputs() ( ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ) = config_and_inputs _UpperCamelCase = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class lowerCAmelCase_ ( __lowercase, __lowercase, unittest.TestCase ): UpperCAmelCase = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) UpperCAmelCase = ( { "feature-extraction": FlaubertModel, "fill-mask": FlaubertWithLMHeadModel, "question-answering": FlaubertForQuestionAnsweringSimple, "text-classification": FlaubertForSequenceClassification, "token-classification": FlaubertForTokenClassification, "zero-shot": FlaubertForSequenceClassification, } if is_torch_available() else {} ) def UpperCamelCase_ ( self : Union[str, Any] , _A : Dict , _A : Dict , _A : Tuple , _A : int , _A : Any ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def UpperCamelCase_ ( self : str , _A : Any , _A : List[str] , _A : Optional[int]=False ): _UpperCamelCase = super()._prepare_for_class(_A , _A , return_labels=_A ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": _UpperCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) _UpperCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) return inputs_dict def UpperCamelCase_ ( self : str ): _UpperCamelCase = FlaubertModelTester(self ) _UpperCamelCase = ConfigTester(self , config_class=_A , emb_dim=37 ) def UpperCamelCase_ ( self : Optional[Any] ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : str ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*_A ) def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*_A ) def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*_A ) def UpperCamelCase_ ( self : Union[str, Any] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*_A ) def UpperCamelCase_ ( self : Optional[int] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*_A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*_A ) def UpperCamelCase_ ( self : Optional[int] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*_A ) @slow def UpperCamelCase_ ( self : str ): for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCamelCase = FlaubertModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @slow @require_torch_gpu def UpperCamelCase_ ( self : List[Any] ): _UpperCamelCase , _UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return _UpperCamelCase = True _UpperCamelCase = model_class(config=_A ) _UpperCamelCase = self._prepare_for_class(_A , _A ) _UpperCamelCase = torch.jit.trace( _A , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_A , os.path.join(_A , '''traced_model.pt''' ) ) _UpperCamelCase = torch.jit.load(os.path.join(_A , '''traced_model.pt''' ) , map_location=_A ) loaded(inputs_dict['''input_ids'''].to(_A ) , inputs_dict['''attention_mask'''].to(_A ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): @slow def UpperCamelCase_ ( self : int ): _UpperCamelCase = FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased''' ) _UpperCamelCase = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) with torch.no_grad(): _UpperCamelCase = model(_A )[0] _UpperCamelCase = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , _A ) _UpperCamelCase = torch.tensor( [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _A , atol=1e-4 ) )
10
0
from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _lowercase = logging.get_logger(__name__) _lowercase = { """google/efficientnet-b7""": """https://huggingface.co/google/efficientnet-b7/resolve/main/config.json""", } class lowercase_ ( __lowercase ): __lowerCamelCase = "efficientnet" def __init__( self , __A = 3 , __A = 600 , __A = 2.0 , __A = 3.1 , __A = 8 , __A = [3, 3, 5, 3, 5, 5, 3] , __A = [32, 16, 24, 40, 80, 112, 192] , __A = [16, 24, 40, 80, 112, 192, 320] , __A = [] , __A = [1, 2, 2, 2, 1, 2, 1] , __A = [1, 2, 2, 3, 3, 4, 1] , __A = [1, 6, 6, 6, 6, 6, 6] , __A = 0.25 , __A = "swish" , __A = 2_560 , __A = "mean" , __A = 0.02 , __A = 0.001 , __A = 0.99 , __A = 0.5 , __A = 0.2 , **__A , ) -> int: super().__init__(**_A ) SCREAMING_SNAKE_CASE_ : Any =num_channels SCREAMING_SNAKE_CASE_ : Tuple =image_size SCREAMING_SNAKE_CASE_ : Dict =width_coefficient SCREAMING_SNAKE_CASE_ : int =depth_coefficient SCREAMING_SNAKE_CASE_ : List[str] =depth_divisor SCREAMING_SNAKE_CASE_ : Optional[int] =kernel_sizes SCREAMING_SNAKE_CASE_ : str =in_channels SCREAMING_SNAKE_CASE_ : Optional[int] =out_channels SCREAMING_SNAKE_CASE_ : str =depthwise_padding SCREAMING_SNAKE_CASE_ : str =strides SCREAMING_SNAKE_CASE_ : Any =num_block_repeats SCREAMING_SNAKE_CASE_ : Dict =expand_ratios SCREAMING_SNAKE_CASE_ : Optional[Any] =squeeze_expansion_ratio SCREAMING_SNAKE_CASE_ : Optional[int] =hidden_act SCREAMING_SNAKE_CASE_ : List[str] =hidden_dim SCREAMING_SNAKE_CASE_ : List[str] =pooling_type SCREAMING_SNAKE_CASE_ : Dict =initializer_range SCREAMING_SNAKE_CASE_ : Dict =batch_norm_eps SCREAMING_SNAKE_CASE_ : Optional[int] =batch_norm_momentum SCREAMING_SNAKE_CASE_ : List[Any] =dropout_rate SCREAMING_SNAKE_CASE_ : List[Any] =drop_connect_rate SCREAMING_SNAKE_CASE_ : Dict =sum(_A ) * 4 class lowercase_ ( __lowercase ): __lowerCamelCase = version.parse("1.11" ) @property def _snake_case ( self ) -> str: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _snake_case ( self ) -> List[str]: return 1e-5
443
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase_ : def __init__( self : Any , _A : int , _A : int=12 , _A : int=7 , _A : Tuple=True , _A : Optional[int]=True , _A : Union[str, Any]=True , _A : str=99 , _A : str=32 , _A : int=32 , _A : Optional[Any]=2 , _A : Dict=4 , _A : int=37 , _A : List[Any]=0.1 , _A : str=0.1 , _A : Any=512 , _A : int=0.02 , _A : Optional[Any]=0 , _A : Dict=None , ): _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = seq_length _UpperCamelCase = is_training _UpperCamelCase = use_input_mask _UpperCamelCase = use_labels _UpperCamelCase = vocab_size _UpperCamelCase = hidden_size _UpperCamelCase = projection_dim _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = intermediate_size _UpperCamelCase = dropout _UpperCamelCase = attention_dropout _UpperCamelCase = max_position_embeddings _UpperCamelCase = initializer_range _UpperCamelCase = scope _UpperCamelCase = bos_token_id def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCamelCase = None if self.use_input_mask: _UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: _UpperCamelCase = input_mask.numpy() _UpperCamelCase , _UpperCamelCase = input_mask.shape _UpperCamelCase = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_A ): _UpperCamelCase = 1 _UpperCamelCase = 0 _UpperCamelCase = self.get_config() return config, input_ids, tf.convert_to_tensor(_A ) def UpperCamelCase_ ( self : str ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCamelCase_ ( self : List[str] , _A : Tuple , _A : str , _A : Optional[Any] ): _UpperCamelCase = TFBlipTextModel(config=_A ) _UpperCamelCase = model(_A , attention_mask=_A , training=_A ) _UpperCamelCase = model(_A , training=_A ) 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 UpperCamelCase_ ( self : Tuple ): _UpperCamelCase = self.prepare_config_and_inputs() _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = config_and_inputs _UpperCamelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowerCAmelCase_ ( __lowercase, unittest.TestCase ): UpperCAmelCase = (TFBlipTextModel,) if is_tf_available() else () UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = BlipTextModelTester(self ) _UpperCamelCase = ConfigTester(self , config_class=_A , hidden_size=37 ) def UpperCamelCase_ ( self : Dict ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : int ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCamelCase_ ( self : List[Any] ): pass def UpperCamelCase_ ( self : Tuple ): pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCamelCase_ ( self : Dict ): pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCamelCase_ ( self : Dict ): pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCamelCase_ ( self : List[str] ): pass @slow def UpperCamelCase_ ( self : Optional[int] ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCamelCase = TFBlipTextModel.from_pretrained(_A ) self.assertIsNotNone(_A ) def UpperCamelCase_ ( self : int , _A : Optional[int]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_A )
10
0
'''simple docstring''' from abc import ABC, abstractmethod from typing import List, Optional class lowerCAmelCase__ ( __lowercase ): """simple docstring""" def __init__( self : Union[str, Any] ) -> Tuple: """simple docstring""" self.test() def UpperCAmelCase__ ( self : Union[str, Any] ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = False while not completed: if counter == 1: self.reset() __SCREAMING_SNAKE_CASE = self.advance() if not self.does_advance(_A ): raise Exception( """Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.""" ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.update(_A ) counter += 1 if counter > 10_000: raise Exception("""update() does not fulfill the constraint.""" ) if self.remaining() != 0: raise Exception("""Custom Constraint is not defined correctly.""" ) @abstractmethod def UpperCAmelCase__ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) @abstractmethod def UpperCAmelCase__ ( self : Any , __SCREAMING_SNAKE_CASE : int ) -> List[Any]: """simple docstring""" raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) @abstractmethod def UpperCAmelCase__ ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : int ) -> Dict: """simple docstring""" raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) @abstractmethod def UpperCAmelCase__ ( self : Optional[Any] ) -> Any: """simple docstring""" raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) @abstractmethod def UpperCAmelCase__ ( self : str ) -> Optional[Any]: """simple docstring""" raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) @abstractmethod def UpperCAmelCase__ ( self : Optional[int] , __SCREAMING_SNAKE_CASE : str=False ) -> str: """simple docstring""" raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) class lowerCAmelCase__ ( __lowercase ): """simple docstring""" def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : List[int] ) -> int: """simple docstring""" super(_A , self ).__init__() if not isinstance(_A , _A ) or len(_A ) == 0: raise ValueError(f'`token_ids` has to be a non-empty list, but is {token_ids}.' ) if any((not isinstance(_A , _A ) or token_id < 0) for token_id in token_ids ): raise ValueError(f'Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.' ) __SCREAMING_SNAKE_CASE = token_ids __SCREAMING_SNAKE_CASE = len(self.token_ids ) __SCREAMING_SNAKE_CASE = -1 # the index of the currently fulfilled step __SCREAMING_SNAKE_CASE = False def UpperCAmelCase__ ( self : Optional[int] ) -> List[Any]: """simple docstring""" if self.completed: return None return self.token_ids[self.fulfilled_idx + 1] def UpperCAmelCase__ ( self : int , __SCREAMING_SNAKE_CASE : int ) -> Optional[Any]: """simple docstring""" if not isinstance(_A , _A ): raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(_A )}' ) if self.completed: return False return token_id == self.token_ids[self.fulfilled_idx + 1] def UpperCAmelCase__ ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : int ) -> Optional[int]: """simple docstring""" if not isinstance(_A , _A ): raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(_A )}' ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False if self.does_advance(_A ): self.fulfilled_idx += 1 __SCREAMING_SNAKE_CASE = True if self.fulfilled_idx == (self.seqlen - 1): __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = completed else: # failed to make progress. __SCREAMING_SNAKE_CASE = True self.reset() return stepped, completed, reset def UpperCAmelCase__ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = 0 def UpperCAmelCase__ ( self : str ) -> Any: """simple docstring""" return self.seqlen - (self.fulfilled_idx + 1) def UpperCAmelCase__ ( self : Any , __SCREAMING_SNAKE_CASE : Optional[int]=False ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = PhrasalConstraint(self.token_ids ) if stateful: __SCREAMING_SNAKE_CASE = self.seqlen __SCREAMING_SNAKE_CASE = self.fulfilled_idx __SCREAMING_SNAKE_CASE = self.completed return new_constraint class lowerCAmelCase__ : """simple docstring""" def __init__( self : Any , __SCREAMING_SNAKE_CASE : List[List[int]] , __SCREAMING_SNAKE_CASE : Optional[int]=True ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = max([len(_A ) for one in nested_token_ids] ) __SCREAMING_SNAKE_CASE = {} for token_ids in nested_token_ids: __SCREAMING_SNAKE_CASE = root for tidx, token_id in enumerate(_A ): if token_id not in level: __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = level[token_id] if no_subsets and self.has_subsets(_A , _A ): raise ValueError( """Each list in `nested_token_ids` can\'t be a complete subset of another list, but is""" f' {nested_token_ids}.' ) __SCREAMING_SNAKE_CASE = root def UpperCAmelCase__ ( self : str , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE = self.trie for current_token in current_seq: __SCREAMING_SNAKE_CASE = start[current_token] __SCREAMING_SNAKE_CASE = list(start.keys() ) return next_tokens def UpperCAmelCase__ ( self : Any , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = self.next_tokens(_A ) return len(_A ) == 0 def UpperCAmelCase__ ( self : int , __SCREAMING_SNAKE_CASE : List[str] ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = list(root.values() ) if len(_A ) == 0: return 1 else: return sum([self.count_leaves(_A ) for nn in next_nodes] ) def UpperCAmelCase__ ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Any ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = self.count_leaves(_A ) return len(_A ) != leaf_count class lowerCAmelCase__ ( __lowercase ): """simple docstring""" def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : List[List[int]] ) -> Optional[Any]: """simple docstring""" super(_A , self ).__init__() if not isinstance(_A , _A ) or len(_A ) == 0: raise ValueError(f'`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.' ) if any(not isinstance(_A , _A ) for token_ids in nested_token_ids ): raise ValueError(f'`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.' ) if any( any((not isinstance(_A , _A ) or token_id < 0) for token_id in token_ids ) for token_ids in nested_token_ids ): raise ValueError( f'Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.' ) __SCREAMING_SNAKE_CASE = DisjunctiveTrie(_A ) __SCREAMING_SNAKE_CASE = nested_token_ids __SCREAMING_SNAKE_CASE = self.trie.max_height __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = False def UpperCAmelCase__ ( self : List[Any] ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = self.trie.next_tokens(self.current_seq ) if len(_A ) == 0: return None else: return token_list def UpperCAmelCase__ ( self : Optional[int] , __SCREAMING_SNAKE_CASE : int ) -> Optional[int]: """simple docstring""" if not isinstance(_A , _A ): raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(_A )}' ) __SCREAMING_SNAKE_CASE = self.trie.next_tokens(self.current_seq ) return token_id in next_tokens def UpperCAmelCase__ ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int ) -> Any: """simple docstring""" if not isinstance(_A , _A ): raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(_A )}' ) __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = False if self.does_advance(_A ): self.current_seq.append(_A ) __SCREAMING_SNAKE_CASE = True else: __SCREAMING_SNAKE_CASE = True self.reset() __SCREAMING_SNAKE_CASE = self.trie.reached_leaf(self.current_seq ) __SCREAMING_SNAKE_CASE = completed return stepped, completed, reset def UpperCAmelCase__ ( self : List[Any] ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = [] def UpperCAmelCase__ ( self : int ) -> Union[str, Any]: """simple docstring""" if self.completed: # since this can be completed without reaching max height return 0 else: return self.seqlen - len(self.current_seq ) def UpperCAmelCase__ ( self : List[str] , __SCREAMING_SNAKE_CASE : int=False ) -> Dict: """simple docstring""" __SCREAMING_SNAKE_CASE = DisjunctiveConstraint(self.token_ids ) if stateful: __SCREAMING_SNAKE_CASE = self.seqlen __SCREAMING_SNAKE_CASE = self.current_seq __SCREAMING_SNAKE_CASE = self.completed return new_constraint class lowerCAmelCase__ : """simple docstring""" def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : List[Constraint] ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = constraints # max # of steps required to fulfill a given constraint __SCREAMING_SNAKE_CASE = max([c.seqlen for c in constraints] ) __SCREAMING_SNAKE_CASE = len(_A ) __SCREAMING_SNAKE_CASE = False self.init_state() def UpperCAmelCase__ ( self : List[str] ) -> str: """simple docstring""" __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = [constraint.copy(stateful=_A ) for constraint in self.constraints] def UpperCAmelCase__ ( self : int ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = 0 if self.inprogress_constraint: # extra points for having a constraint mid-fulfilled add += self.max_seqlen - self.inprogress_constraint.remaining() return (len(self.complete_constraints ) * self.max_seqlen) + add def UpperCAmelCase__ ( self : List[Any] ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = [] if self.inprogress_constraint is None: for constraint in self.pending_constraints: # "pending" == "unfulfilled yet" __SCREAMING_SNAKE_CASE = constraint.advance() if isinstance(_A , _A ): token_list.append(_A ) elif isinstance(_A , _A ): token_list.extend(_A ) else: __SCREAMING_SNAKE_CASE = self.inprogress_constraint.advance() if isinstance(_A , _A ): token_list.append(_A ) elif isinstance(_A , _A ): token_list.extend(_A ) if len(_A ) == 0: return None else: return token_list def UpperCAmelCase__ ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] ) -> Union[str, Any]: """simple docstring""" self.init_state() if token_ids is not None: for token in token_ids: # completes or steps **one** constraint __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.add(_A ) # the entire list of constraints are fulfilled if self.completed: break def UpperCAmelCase__ ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int ) -> Optional[Any]: """simple docstring""" if not isinstance(_A , _A ): raise ValueError(f'`token_id` should be an `int`, but is `{token_id}`.' ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = False, False if self.completed: __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = False return complete, stepped if self.inprogress_constraint is not None: # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current # job, simply update the state __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.inprogress_constraint.update(_A ) if reset: # 1. If the next token breaks the progress, then we must restart. # e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books". # But that doesn't mean we self.init_state(), since we only reset the state for this particular # constraint, not the full list of constraints. self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_A ) ) __SCREAMING_SNAKE_CASE = None if complete: # 2. If the next token completes the constraint, move it to completed list, set # inprogress to None. If there are no pending constraints either, then this full list of constraints # is complete. self.complete_constraints.append(self.inprogress_constraint ) __SCREAMING_SNAKE_CASE = None if len(self.pending_constraints ) == 0: # we're done! __SCREAMING_SNAKE_CASE = True else: # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list # of constraints? for cidx, pending_constraint in enumerate(self.pending_constraints ): if pending_constraint.does_advance(_A ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = pending_constraint.update(_A ) if not stepped: raise Exception( """`constraint.update(token_id)` is not yielding incremental progress, """ """even though `constraint.does_advance(token_id)` is true.""" ) if complete: self.complete_constraints.append(_A ) __SCREAMING_SNAKE_CASE = None if not complete and stepped: __SCREAMING_SNAKE_CASE = pending_constraint if complete or stepped: # If we made any progress at all, then it's at least not a "pending constraint". __SCREAMING_SNAKE_CASE = ( self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :] ) if len(self.pending_constraints ) == 0 and self.inprogress_constraint is None: # If there's no longer any pending after this and no inprogress either, then we must be # complete. __SCREAMING_SNAKE_CASE = True break # prevent accidentally stepping through multiple constraints with just one token. return complete, stepped def UpperCAmelCase__ ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any]=True ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE = ConstraintListState(self.constraints ) # we actually never though self.constraints objects # throughout this process. So it's at initialization state. if stateful: __SCREAMING_SNAKE_CASE = [ constraint.copy(stateful=_A ) for constraint in self.complete_constraints ] if self.inprogress_constraint is not None: __SCREAMING_SNAKE_CASE = self.inprogress_constraint.copy(stateful=_A ) __SCREAMING_SNAKE_CASE = [constraint.copy() for constraint in self.pending_constraints] return new_state
627
from __future__ import annotations _lowerCAmelCase = [True] * 1_000_001 _lowerCAmelCase = 2 while i * i <= 1_000_000: if seive[i]: for j in range(i * i, 1_000_001, i): _lowerCAmelCase = False i += 1 def _snake_case ( __snake_case ): return seive[n] def _snake_case ( __snake_case ): return any(digit in '''02468''' for digit in str(__snake_case ) ) def _snake_case ( __snake_case = 1000000 ): _UpperCamelCase = [2] # result already includes the number 2. for num in range(3 , limit + 1 , 2 ): if is_prime(__snake_case ) and not contains_an_even_digit(__snake_case ): _UpperCamelCase = str(__snake_case ) _UpperCamelCase = [int(str_num[j:] + str_num[:j] ) for j in range(len(__snake_case ) )] if all(is_prime(__snake_case ) for i in list_nums ): result.append(__snake_case ) return result def _snake_case ( ): return len(find_circular_primes() ) if __name__ == "__main__": print(f'{len(find_circular_primes()) = }')
10
0
"""simple docstring""" from __future__ import annotations import math def lowerCAmelCase_ (_SCREAMING_SNAKE_CASE :Optional[Any] ) -> Dict: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(__snake_case ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True UpperCamelCase = [num for num in range(3, 10_00_01, 2) if not is_prime(num)] def lowerCAmelCase_ (_SCREAMING_SNAKE_CASE :str ) -> Any: if not isinstance(__snake_case , __snake_case ): raise ValueError("n must be an integer" ) if n <= 0: raise ValueError("n must be >= 0" ) a_ : Any = [] for num in range(len(__snake_case ) ): a_ : List[str] = 0 while 2 * i * i <= odd_composites[num]: a_ : Optional[int] = odd_composites[num] - 2 * i * i if is_prime(__snake_case ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(__snake_case ) == n: return list_nums return [] def lowerCAmelCase_ () -> Dict: return compute_nums(1 )[0] if __name__ == "__main__": print(F'{solution() = }')
473
import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _lowerCAmelCase = get_tests_dir("fixtures/spiece.model") @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( __lowercase, unittest.TestCase ): UpperCAmelCase = DebertaVaTokenizer UpperCAmelCase = DebertaVaTokenizerFast UpperCAmelCase = True UpperCAmelCase = True def UpperCamelCase_ ( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing _UpperCamelCase = DebertaVaTokenizer(_A , unk_token='''<unk>''' ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : Dict , _A : Union[str, Any] ): _UpperCamelCase = '''this is a test''' _UpperCamelCase = '''this is a test''' return input_text, output_text def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = '''<pad>''' _UpperCamelCase = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A ) , _A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A ) , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<pad>''' ) self.assertEqual(vocab_keys[1] , '''<unk>''' ) self.assertEqual(vocab_keys[-1] , '''[PAD]''' ) self.assertEqual(len(_A ) , 3_0001 ) def UpperCamelCase_ ( self : List[Any] ): self.assertEqual(self.get_tokenizer().vocab_size , 3_0000 ) def UpperCamelCase_ ( self : List[str] ): # fmt: off _UpperCamelCase = ''' \tHeLLo!how \n Are yoU? ''' _UpperCamelCase = ['''▁hello''', '''!''', '''how''', '''▁are''', '''▁you''', '''?'''] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) @unittest.skip('''There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.''' ) def UpperCamelCase_ ( self : Dict ): pass @unittest.skip('''There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.''' ) def UpperCamelCase_ ( self : Optional[Any] ): pass def UpperCamelCase_ ( self : Dict ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁''', '''<unk>''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', '''▁''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''▁''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : List[Any] ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁i''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', '''▁''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''▁''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Dict ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁i''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : int ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁''', '''<unk>''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', '''▁''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''▁''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Tuple ): # fmt: off _UpperCamelCase = ''' \tHeLLo!how \n Are yoU? ''' _UpperCamelCase = ['''▁''', '''<unk>''', '''e''', '''<unk>''', '''o''', '''!''', '''how''', '''▁''', '''<unk>''', '''re''', '''▁yo''', '''<unk>''', '''?'''] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = self.get_tokenizer() _UpperCamelCase = self.get_rust_tokenizer() _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.encode(_A , add_special_tokens=_A ) _UpperCamelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = self.get_rust_tokenizer() _UpperCamelCase = tokenizer.encode(_A ) _UpperCamelCase = rust_tokenizer.encode(_A ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = '''This is a test''' _UpperCamelCase = [13, 1, 4398, 25, 21, 1289] _UpperCamelCase = ['''▁''', '''T''', '''his''', '''▁is''', '''▁a''', '''▁test'''] _UpperCamelCase = ['''▁''', '''<unk>''', '''his''', '''▁is''', '''▁a''', '''▁test'''] _UpperCamelCase = DebertaVaTokenizer(_A , keep_accents=_A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , keep_accents=_A ) _UpperCamelCase = tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = [13, 1, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9] _UpperCamelCase = ['''▁''', '''I''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''é''', '''.''', ] _UpperCamelCase = ['''▁''', '''<unk>''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''.''', ] # fmt: on _UpperCamelCase = tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = DebertaVaTokenizer(_A ) _UpperCamelCase = tokenizer.encode('''sequence builders''' ) _UpperCamelCase = tokenizer.encode('''multi-sequence build''' ) _UpperCamelCase = tokenizer.build_inputs_with_special_tokens(_A ) _UpperCamelCase = tokenizer.build_inputs_with_special_tokens(_A , _A ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] , _A ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] , _A , ) @slow def UpperCamelCase_ ( self : Optional[Any] ): # fmt: off _UpperCamelCase = {'''input_ids''': [[1, 3_9867, 36, 1_9390, 486, 27, 3_5052, 8_1436, 18, 6_0685, 1225, 7, 3_5052, 8_1436, 18, 9367, 1_6899, 18, 1_5937, 53, 594, 773, 18, 1_6287, 3_0465, 36, 1_5937, 6, 4_1139, 38, 3_6979, 6_0763, 191, 6, 3_4132, 99, 6, 5_0538, 390, 4_3230, 6, 3_4132, 2779, 2_0850, 14, 699, 1072, 1194, 36, 382, 1_0901, 53, 7, 699, 1072, 2084, 36, 2_0422, 630, 53, 19, 105, 3049, 1896, 1053, 1_6899, 1506, 11, 3_7978, 4243, 7, 1237, 3_1869, 200, 1_6566, 654, 6, 3_5052, 8_1436, 7, 5_5630, 1_3593, 4, 2], [1, 26, 1_5011, 13, 667, 8, 1053, 18, 2_3611, 1237, 7_2356, 1_2820, 34, 10_4134, 1209, 35, 1_3313, 6627, 21, 202, 347, 7, 164, 2399, 11, 46, 4485, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 1232, 2864, 1_5785, 1_4951, 105, 5, 8581, 1250, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''token_type_ids''': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_A , model_name='''microsoft/deberta-v2-xlarge''' , revision='''ad6e42c1532ddf3a15c39246b63f5559d558b670''' , )
10
0
import glob import os import random from string import ascii_lowercase, digits import cva import numpy as np # Parrameters _UpperCAmelCase = (720, 1280) # Height, Width _UpperCAmelCase = (0.4, 0.6) # if height or width lower than this scale, drop it. _UpperCAmelCase = 1 / 100 _UpperCAmelCase = """""" _UpperCAmelCase = """""" _UpperCAmelCase = """""" _UpperCAmelCase = 250 def UpperCamelCase ( ): '''simple docstring''' A_ , A_ : List[str] = get_dataset(__snake_case ,__snake_case ) for index in range(__snake_case ): A_ : List[Any] = random.sample(range(len(__snake_case ) ) ,4 ) A_ , A_ , A_ : Dict = update_image_and_anno( __snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,filter_scale=__snake_case ,) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' A_ : str = random_chars(32 ) A_ : Tuple = path.split(os.sep )[-1].rsplit('.' ,1 )[0] A_ : Any = f'''{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}''' cva.imwrite(f'''{file_root}.jpg''' ,__snake_case ,[cva.IMWRITE_JPEG_QUALITY, 85] ) print(f'''Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}''' ) A_ : Tuple = [] for anno in new_annos: A_ : int = anno[3] - anno[1] A_ : List[Any] = anno[4] - anno[2] A_ : Tuple = anno[1] + width / 2 A_ : Optional[int] = anno[2] + height / 2 A_ : Optional[int] = f'''{anno[0]} {x_center} {y_center} {width} {height}''' annos_list.append(__snake_case ) with open(f'''{file_root}.txt''' ,'w' ) as outfile: outfile.write('\n'.join(line for line in annos_list ) ) def UpperCamelCase ( __lowercase : Optional[int] ,__lowercase : Tuple ): '''simple docstring''' A_ : Optional[int] = [] A_ : List[str] = [] for label_file in glob.glob(os.path.join(__snake_case ,'*.txt' ) ): A_ : Optional[Any] = label_file.split(os.sep )[-1].rsplit('.' ,1 )[0] with open(__snake_case ) as in_file: A_ : Optional[Any] = in_file.readlines() A_ : Dict = os.path.join(__snake_case ,f'''{label_name}.jpg''' ) A_ : List[Any] = [] for obj_list in obj_lists: A_ : Optional[Any] = obj_list.rstrip('\n' ).split(' ' ) A_ : List[Any] = float(obj[1] ) - float(obj[3] ) / 2 A_ : List[str] = float(obj[2] ) - float(obj[4] ) / 2 A_ : Tuple = float(obj[1] ) + float(obj[3] ) / 2 A_ : List[str] = float(obj[2] ) + float(obj[4] ) / 2 boxes.append([int(obj[0] ), xmin, ymin, xmax, ymax] ) if not boxes: continue img_paths.append(__snake_case ) labels.append(__snake_case ) return img_paths, labels def UpperCamelCase ( __lowercase : Optional[int] ,__lowercase : Union[str, Any] ,__lowercase : Any ,__lowercase : List[Any] ,__lowercase : int ,__lowercase : Union[str, Any] = 0.0 ,): '''simple docstring''' A_ : Union[str, Any] = np.zeros([output_size[0], output_size[1], 3] ,dtype=np.uinta ) A_ : List[str] = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) A_ : Optional[int] = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) A_ : Optional[int] = int(scale_x * output_size[1] ) A_ : Optional[Any] = int(scale_y * output_size[0] ) A_ : List[str] = [] A_ : Union[str, Any] = [] for i, index in enumerate(__snake_case ): A_ : Optional[Any] = all_img_list[index] path_list.append(__snake_case ) A_ : List[Any] = all_annos[index] A_ : Union[str, Any] = cva.imread(__snake_case ) if i == 0: # top-left A_ : Any = cva.resize(__snake_case ,(divid_point_x, divid_point_y) ) A_ : Dict = img for bbox in img_annos: A_ : Optional[Any] = bbox[1] * scale_x A_ : List[str] = bbox[2] * scale_y A_ : Any = bbox[3] * scale_x A_ : List[str] = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 1: # top-right A_ : List[Any] = cva.resize(__snake_case ,(output_size[1] - divid_point_x, divid_point_y) ) A_ : Any = img for bbox in img_annos: A_ : Optional[int] = scale_x + bbox[1] * (1 - scale_x) A_ : Union[str, Any] = bbox[2] * scale_y A_ : str = scale_x + bbox[3] * (1 - scale_x) A_ : List[Any] = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 2: # bottom-left A_ : Any = cva.resize(__snake_case ,(divid_point_x, output_size[0] - divid_point_y) ) A_ : int = img for bbox in img_annos: A_ : Union[str, Any] = bbox[1] * scale_x A_ : str = scale_y + bbox[2] * (1 - scale_y) A_ : Union[str, Any] = bbox[3] * scale_x A_ : Union[str, Any] = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) else: # bottom-right A_ : Any = cva.resize( __snake_case ,(output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) A_ : Any = img for bbox in img_annos: A_ : Optional[int] = scale_x + bbox[1] * (1 - scale_x) A_ : Optional[int] = scale_y + bbox[2] * (1 - scale_y) A_ : Optional[int] = scale_x + bbox[3] * (1 - scale_x) A_ : List[Any] = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) # Remove bounding box small than scale of filter if filter_scale > 0: A_ : List[str] = [ anno for anno in new_anno if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2]) ] return output_img, new_anno, path_list[0] def UpperCamelCase ( __lowercase : Any ): '''simple docstring''' assert number_char > 1, "The number of character should greater than 1" A_ : Any = ascii_lowercase + digits return "".join(random.choice(__snake_case ) for _ in range(__snake_case ) ) if __name__ == "__main__": main() print("""DONE ✅""")
558
import sys from collections import defaultdict class lowerCAmelCase_ : def __init__( self : Optional[int] ): _UpperCamelCase = [] def UpperCamelCase_ ( self : Any , _A : str ): return self.node_position[vertex] def UpperCamelCase_ ( self : Optional[Any] , _A : List[str] , _A : Union[str, Any] ): _UpperCamelCase = pos def UpperCamelCase_ ( self : Any , _A : List[str] , _A : int , _A : Optional[Any] , _A : Union[str, Any] ): if start > size // 2 - 1: return else: if 2 * start + 2 >= size: _UpperCamelCase = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: _UpperCamelCase = 2 * start + 1 else: _UpperCamelCase = 2 * start + 2 if heap[smallest_child] < heap[start]: _UpperCamelCase , _UpperCamelCase = heap[smallest_child], positions[smallest_child] _UpperCamelCase , _UpperCamelCase = ( heap[start], positions[start], ) _UpperCamelCase , _UpperCamelCase = temp, tempa _UpperCamelCase = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , _A ) self.top_to_bottom(_A , _A , _A , _A ) def UpperCamelCase_ ( self : List[str] , _A : Tuple , _A : Optional[Any] , _A : int , _A : Optional[int] ): _UpperCamelCase = position[index] while index != 0: _UpperCamelCase = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: _UpperCamelCase = heap[parent] _UpperCamelCase = position[parent] self.set_position(position[parent] , _A ) else: _UpperCamelCase = val _UpperCamelCase = temp self.set_position(_A , _A ) break _UpperCamelCase = parent else: _UpperCamelCase = val _UpperCamelCase = temp self.set_position(_A , 0 ) def UpperCamelCase_ ( self : int , _A : Tuple , _A : int ): _UpperCamelCase = len(_A ) // 2 - 1 for i in range(_A , -1 , -1 ): self.top_to_bottom(_A , _A , len(_A ) , _A ) def UpperCamelCase_ ( self : Any , _A : int , _A : List[str] ): _UpperCamelCase = positions[0] _UpperCamelCase = sys.maxsize self.top_to_bottom(_A , 0 , len(_A ) , _A ) return temp def _snake_case ( __snake_case ): _UpperCamelCase = Heap() _UpperCamelCase = [0] * len(__snake_case ) _UpperCamelCase = [-1] * len(__snake_case ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph _UpperCamelCase = [] # Heap of Distance of vertices from their neighboring vertex _UpperCamelCase = [] for vertex in range(len(__snake_case ) ): distance_tv.append(sys.maxsize ) positions.append(__snake_case ) heap.node_position.append(__snake_case ) _UpperCamelCase = [] _UpperCamelCase = 1 _UpperCamelCase = sys.maxsize for neighbor, distance in adjacency_list[0]: _UpperCamelCase = 0 _UpperCamelCase = distance heap.heapify(__snake_case , __snake_case ) for _ in range(1 , len(__snake_case ) ): _UpperCamelCase = heap.delete_minimum(__snake_case , __snake_case ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) _UpperCamelCase = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(__snake_case )] ): _UpperCamelCase = distance heap.bottom_to_top( __snake_case , heap.get_position(__snake_case ) , __snake_case , __snake_case ) _UpperCamelCase = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > _lowerCAmelCase = int(input("Enter number of edges: ").strip()) _lowerCAmelCase = defaultdict(list) for _ in range(edges_number): _lowerCAmelCase = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
10
0
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging a_ = logging.get_logger(__name__) # TODO: upload to AWS a_ = { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json' ), } class UpperCAmelCase_ ( __lowercase ): UpperCamelCase ="retribert" def __init__( self , UpperCamelCase_=3_05_22 , UpperCamelCase_=7_68 , UpperCamelCase_=8 , UpperCamelCase_=12 , UpperCamelCase_=30_72 , UpperCamelCase_="gelu" , UpperCamelCase_=0.1 , UpperCamelCase_=0.1 , UpperCamelCase_=5_12 , UpperCamelCase_=2 , UpperCamelCase_=0.0_2 , UpperCamelCase_=1E-12 , UpperCamelCase_=True , UpperCamelCase_=1_28 , UpperCamelCase_=0 , **UpperCamelCase_ , ) -> Tuple: super().__init__(pad_token_id=_A , **_A ) __lowercase : Optional[Any] = vocab_size __lowercase : Tuple = hidden_size __lowercase : List[str] = num_hidden_layers __lowercase : Any = num_attention_heads __lowercase : Dict = hidden_act __lowercase : Tuple = intermediate_size __lowercase : Optional[int] = hidden_dropout_prob __lowercase : Tuple = attention_probs_dropout_prob __lowercase : Tuple = max_position_embeddings __lowercase : Union[str, Any] = type_vocab_size __lowercase : Optional[int] = initializer_range __lowercase : Any = layer_norm_eps __lowercase : List[Any] = share_encoders __lowercase : Tuple = projection_dim
76
import logging import os from .state import PartialState class lowerCAmelCase_ ( logging.LoggerAdapter ): @staticmethod def UpperCamelCase_ ( _A : Any ): _UpperCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def UpperCamelCase_ ( self : Union[str, Any] , _A : Optional[Any] , _A : str , *_A : int , **_A : List[Any] ): if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) _UpperCamelCase = kwargs.pop('''main_process_only''' , _A ) _UpperCamelCase = kwargs.pop('''in_order''' , _A ) if self.isEnabledFor(_A ): if self._should_log(_A ): _UpperCamelCase , _UpperCamelCase = self.process(_A , _A ) self.logger.log(_A , _A , *_A , **_A ) elif in_order: _UpperCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: _UpperCamelCase , _UpperCamelCase = self.process(_A , _A ) self.logger.log(_A , _A , *_A , **_A ) state.wait_for_everyone() def _snake_case ( __snake_case , __snake_case = None ): if log_level is None: _UpperCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , __snake_case ) _UpperCamelCase = logging.getLogger(__snake_case ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(__snake_case , {} )
10
0
'''simple docstring''' from __future__ import annotations _lowercase = [] def lowerCamelCase__ ( a , a , a ): for i in range(len(__snake_case ) ): if board[row][i] == 1: return False for i in range(len(__snake_case ) ): if board[i][column] == 1: return False for i, j in zip(range(__snake_case , -1 , -1 ) , range(__snake_case , -1 , -1 ) ): if board[i][j] == 1: return False for i, j in zip(range(__snake_case , -1 , -1 ) , range(__snake_case , len(__snake_case ) ) ): if board[i][j] == 1: return False return True def lowerCamelCase__ ( a , a ): if row >= len(__snake_case ): solution.append(__snake_case ) printboard(__snake_case ) print() return True for i in range(len(__snake_case ) ): if is_safe(__snake_case , __snake_case , __snake_case ): __snake_case = 1 solve(__snake_case , row + 1 ) __snake_case = 0 return False def lowerCamelCase__ ( a ): for i in range(len(__snake_case ) ): for j in range(len(__snake_case ) ): if board[i][j] == 1: print('Q' , end=' ' ) else: print('.' , end=' ' ) print() # n=int(input("The no. of queens")) _lowercase = 8 _lowercase = [[0 for i in range(n)] for j in range(n)] solve(board, 0) print("""The total no. of solutions are :""", len(solution))
356
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin _lowerCAmelCase = "▁" _lowerCAmelCase = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowerCAmelCase_ ( __lowercase, unittest.TestCase ): UpperCAmelCase = BertGenerationTokenizer UpperCAmelCase = False UpperCAmelCase = True def UpperCamelCase_ ( self : List[str] ): super().setUp() _UpperCamelCase = BertGenerationTokenizer(_A , keep_accents=_A ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = '''<s>''' _UpperCamelCase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A ) , _A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A ) , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(_A ) , 1002 ) def UpperCamelCase_ ( self : Dict ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def UpperCamelCase_ ( self : int ): _UpperCamelCase = BertGenerationTokenizer(_A , keep_accents=_A ) _UpperCamelCase = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_A , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_A ) , [285, 46, 10, 170, 382] , ) _UpperCamelCase = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _A , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _UpperCamelCase = tokenizer.convert_tokens_to_ids(_A ) self.assertListEqual( _A , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual( _A , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def UpperCamelCase_ ( self : Union[str, Any] ): return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = '''Hello World!''' _UpperCamelCase = [1_8536, 2260, 101] self.assertListEqual(_A , self.big_tokenizer.encode(_A ) ) @slow def UpperCamelCase_ ( self : int ): _UpperCamelCase = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) _UpperCamelCase = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 3_4324, 497, 391, 408, 1_1342, 1244, 385, 100, 938, 985, 456, 574, 362, 1_2597, 3200, 3129, 1172, ] self.assertListEqual(_A , self.big_tokenizer.encode(_A ) ) @require_torch @slow def UpperCamelCase_ ( self : Dict ): import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence _UpperCamelCase = list(self.big_tokenizer.get_vocab().keys() )[:10] _UpperCamelCase = ''' '''.join(_A ) _UpperCamelCase = self.big_tokenizer.encode_plus(_A , return_tensors='''pt''' , return_token_type_ids=_A ) _UpperCamelCase = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_A ) _UpperCamelCase = BertGenerationConfig() _UpperCamelCase = BertGenerationEncoder(_A ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_A ) model(**_A ) @slow def UpperCamelCase_ ( self : Dict ): # fmt: off _UpperCamelCase = {'''input_ids''': [[3_9286, 458, 3_6335, 2001, 456, 1_3073, 1_3266, 455, 113, 7746, 1741, 1_1157, 391, 1_3073, 1_3266, 455, 113, 3967, 3_5412, 113, 4936, 109, 3870, 2377, 113, 3_0084, 4_5720, 458, 134, 1_7496, 112, 503, 1_1672, 113, 118, 112, 5665, 1_3347, 3_8687, 112, 1496, 3_1389, 112, 3268, 4_7264, 134, 962, 112, 1_6377, 8035, 2_3130, 430, 1_2169, 1_5518, 2_8592, 458, 146, 4_1697, 109, 391, 1_2169, 1_5518, 1_6689, 458, 146, 4_1358, 109, 452, 726, 4034, 111, 763, 3_5412, 5082, 388, 1903, 111, 9051, 391, 2870, 4_8918, 1900, 1123, 550, 998, 112, 9586, 1_5985, 455, 391, 410, 2_2955, 3_7636, 114], [448, 1_7496, 419, 3663, 385, 763, 113, 2_7533, 2870, 3283, 1_3043, 1639, 2_4713, 523, 656, 2_4013, 1_8550, 2521, 517, 2_7014, 2_1244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 1_1786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 2_1932, 1_8146, 726, 363, 1_7032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_A , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
10
0
'''simple docstring''' def snake_case_ ( __snake_case : Optional[int] , __snake_case : int , __snake_case : Optional[Any] , __snake_case : List[str]) -> Union[str, Any]: # Return True if there is node that has not iterated. lowerCAmelCase_ = [False] * len(__snake_case) lowerCAmelCase_ = [] queue.append(__snake_case) lowerCAmelCase_ = True while queue: lowerCAmelCase_ = queue.pop(0) for ind in range(len(graph[u])): if visited[ind] is False and graph[u][ind] > 0: queue.append(__snake_case) lowerCAmelCase_ = True lowerCAmelCase_ = u return visited[t] def snake_case_ ( __snake_case : str , __snake_case : Any , __snake_case : Union[str, Any]) -> str: # This array is filled by BFS and to store path lowerCAmelCase_ = [-1] * (len(__snake_case)) lowerCAmelCase_ = 0 while bfs(__snake_case , __snake_case , __snake_case , __snake_case): lowerCAmelCase_ = float('''Inf''') lowerCAmelCase_ = sink while s != source: # Find the minimum value in select path lowerCAmelCase_ = min(__snake_case , graph[parent[s]][s]) lowerCAmelCase_ = parent[s] max_flow += path_flow lowerCAmelCase_ = sink while v != source: lowerCAmelCase_ = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow lowerCAmelCase_ = parent[v] return max_flow A_ : Optional[int] =[ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] A_ , A_ : Tuple =0, 5 print(ford_fulkerson(graph, source, sink))
274
import gc import unittest import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DDPMScheduler, PriorTransformer, StableUnCLIPPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class lowerCAmelCase_ ( __lowercase, __lowercase, __lowercase, unittest.TestCase ): UpperCAmelCase = StableUnCLIPPipeline UpperCAmelCase = TEXT_TO_IMAGE_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_BATCH_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS # TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false UpperCAmelCase = False def UpperCamelCase_ ( self : Optional[int] ): _UpperCamelCase = 32 _UpperCamelCase = embedder_hidden_size # prior components torch.manual_seed(0 ) _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTextModelWithProjection( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=_A , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) _UpperCamelCase = PriorTransformer( num_attention_heads=2 , attention_head_dim=12 , embedding_dim=_A , num_layers=1 , ) torch.manual_seed(0 ) _UpperCamelCase = DDPMScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1000 , clip_sample=_A , clip_sample_range=5.0 , beta_schedule='''squaredcos_cap_v2''' , ) # regular denoising components torch.manual_seed(0 ) _UpperCamelCase = StableUnCLIPImageNormalizer(embedding_dim=_A ) _UpperCamelCase = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) _UpperCamelCase = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type='''projection''' , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=_A , layers_per_block=1 , upcast_attention=_A , use_linear_projection=_A , ) torch.manual_seed(0 ) _UpperCamelCase = DDIMScheduler( beta_schedule='''scaled_linear''' , beta_start=0.0_0085 , beta_end=0.012 , prediction_type='''v_prediction''' , set_alpha_to_one=_A , steps_offset=1 , ) torch.manual_seed(0 ) _UpperCamelCase = AutoencoderKL() _UpperCamelCase = { # prior components '''prior_tokenizer''': prior_tokenizer, '''prior_text_encoder''': prior_text_encoder, '''prior''': prior, '''prior_scheduler''': prior_scheduler, # image noising components '''image_normalizer''': image_normalizer, '''image_noising_scheduler''': image_noising_scheduler, # regular denoising components '''tokenizer''': tokenizer, '''text_encoder''': text_encoder, '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, } return components def UpperCamelCase_ ( self : Dict , _A : Tuple , _A : Dict=0 ): if str(_A ).startswith('''mps''' ): _UpperCamelCase = torch.manual_seed(_A ) else: _UpperCamelCase = torch.Generator(device=_A ).manual_seed(_A ) _UpperCamelCase = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''prior_num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = torch_device == '''cpu''' self._test_attention_slicing_forward_pass(test_max_difference=_A ) def UpperCamelCase_ ( self : List[Any] ): _UpperCamelCase = torch_device in ['''cpu''', '''mps'''] self._test_inference_batch_single_identical(test_max_difference=_A ) @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self : Optional[Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy''' ) _UpperCamelCase = StableUnCLIPPipeline.from_pretrained('''fusing/stable-unclip-2-1-l''' , torch_dtype=torch.floataa ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() _UpperCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) _UpperCamelCase = pipe('''anime turle''' , generator=_A , output_type='''np''' ) _UpperCamelCase = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_A , _A ) def UpperCamelCase_ ( self : Optional[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() _UpperCamelCase = StableUnCLIPPipeline.from_pretrained('''fusing/stable-unclip-2-1-l''' , torch_dtype=torch.floataa ) _UpperCamelCase = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() _UpperCamelCase = pipe( '''anime turtle''' , prior_num_inference_steps=2 , num_inference_steps=2 , output_type='''np''' , ) _UpperCamelCase = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
10
0
_lowerCamelCase = { 'a': 'AAAAA', 'b': 'AAAAB', 'c': 'AAABA', 'd': 'AAABB', 'e': 'AABAA', 'f': 'AABAB', 'g': 'AABBA', 'h': 'AABBB', 'i': 'ABAAA', 'j': 'BBBAA', 'k': 'ABAAB', 'l': 'ABABA', 'm': 'ABABB', 'n': 'ABBAA', 'o': 'ABBAB', 'p': 'ABBBA', 'q': 'ABBBB', 'r': 'BAAAA', 's': 'BAAAB', 't': 'BAABA', 'u': 'BAABB', 'v': 'BBBAB', 'w': 'BABAA', 'x': 'BABAB', 'y': 'BABBA', 'z': 'BABBB', ' ': ' ', } _lowerCamelCase = {value: key for key, value in encode_dict.items()} def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Dict ): SCREAMING_SNAKE_CASE__ = """""" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("""encode() accepts only letters of the alphabet and spaces""" ) return encoded def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Tuple ): if set(__snake_case ) - {"A", "B", " "} != set(): raise Exception("""decode() accepts only \'A\', \'B\' and spaces""" ) SCREAMING_SNAKE_CASE__ = """""" for word in coded.split(): while len(__snake_case ) != 0: decoded += decode_dict[word[:5]] SCREAMING_SNAKE_CASE__ = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
6
from __future__ import annotations import math import numpy as np from numpy.linalg import norm def _snake_case ( __snake_case , __snake_case ): return math.sqrt(sum(pow(a - b , 2 ) for a, b in zip(__snake_case , __snake_case ) ) ) def _snake_case ( __snake_case , __snake_case ): if dataset.ndim != value_array.ndim: _UpperCamelCase = ( '''Wrong input data\'s dimensions... ''' f"""dataset : {dataset.ndim}, value_array : {value_array.ndim}""" ) raise ValueError(__snake_case ) try: if dataset.shape[1] != value_array.shape[1]: _UpperCamelCase = ( '''Wrong input data\'s shape... ''' f"""dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}""" ) raise ValueError(__snake_case ) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError('''Wrong shape''' ) if dataset.dtype != value_array.dtype: _UpperCamelCase = ( '''Input data have different datatype... ''' f"""dataset : {dataset.dtype}, value_array : {value_array.dtype}""" ) raise TypeError(__snake_case ) _UpperCamelCase = [] for value in value_array: _UpperCamelCase = euclidean(__snake_case , dataset[0] ) _UpperCamelCase = dataset[0].tolist() for dataset_value in dataset[1:]: _UpperCamelCase = euclidean(__snake_case , __snake_case ) if dist > temp_dist: _UpperCamelCase = temp_dist _UpperCamelCase = dataset_value.tolist() answer.append([vector, dist] ) return answer def _snake_case ( __snake_case , __snake_case ): return np.dot(__snake_case , __snake_case ) / (norm(__snake_case ) * norm(__snake_case )) if __name__ == "__main__": import doctest doctest.testmod()
10
0
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> int: lowerCAmelCase , lowerCAmelCase = coefficient_matrix.shape lowerCAmelCase , lowerCAmelCase = constant_matrix.shape if rowsa != colsa: lowerCAmelCase = f"Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}" raise ValueError(__snake_case ) if colsa != 1: lowerCAmelCase = f"Constant matrix must be nx1 but received {rowsa}x{colsa}" raise ValueError(__snake_case ) if rowsa != rowsa: lowerCAmelCase = ( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' f"received {rowsa}x{colsa} and {rowsa}x{colsa}" ) raise ValueError(__snake_case ) if len(__snake_case ) != rowsa: lowerCAmelCase = ( '''Number of initial values must be equal to number of rows in coefficient ''' f"matrix but received {len(__snake_case )} and {rowsa}" ) raise ValueError(__snake_case ) if iterations <= 0: raise ValueError('''Iterations must be at least 1''' ) lowerCAmelCase = np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) lowerCAmelCase , lowerCAmelCase = table.shape strictly_diagonally_dominant(__snake_case ) # Iterates the whole matrix for given number of times for _ in range(__snake_case ): lowerCAmelCase = [] for row in range(__snake_case ): lowerCAmelCase = 0 for col in range(__snake_case ): if col == row: lowerCAmelCase = table[row][col] elif col == cols - 1: lowerCAmelCase = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] lowerCAmelCase = (temp + val) / denom new_val.append(__snake_case ) lowerCAmelCase = new_val return [float(__snake_case ) for i in new_val] def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: lowerCAmelCase , lowerCAmelCase = table.shape lowerCAmelCase = True for i in range(0 , __snake_case ): lowerCAmelCase = 0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('''Coefficient matrix is not strictly diagonally dominant''' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class lowerCAmelCase_ ( __lowercase, unittest.TestCase ): UpperCAmelCase = ShapEPipeline UpperCAmelCase = ["prompt"] UpperCAmelCase = ["prompt"] UpperCAmelCase = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] UpperCAmelCase = False @property def UpperCamelCase_ ( self : Union[str, Any] ): return 32 @property def UpperCamelCase_ ( self : int ): return 32 @property def UpperCamelCase_ ( self : List[str] ): return self.time_input_dim * 4 @property def UpperCamelCase_ ( self : Optional[Any] ): return 8 @property def UpperCamelCase_ ( self : int ): _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) return tokenizer @property def UpperCamelCase_ ( self : List[Any] ): torch.manual_seed(0 ) _UpperCamelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(_A ) @property def UpperCamelCase_ ( self : int ): torch.manual_seed(0 ) _UpperCamelCase = { '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } _UpperCamelCase = PriorTransformer(**_A ) return model @property def UpperCamelCase_ ( self : Union[str, Any] ): torch.manual_seed(0 ) _UpperCamelCase = { '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } _UpperCamelCase = ShapERenderer(**_A ) return model def UpperCamelCase_ ( self : str ): _UpperCamelCase = self.dummy_prior _UpperCamelCase = self.dummy_text_encoder _UpperCamelCase = self.dummy_tokenizer _UpperCamelCase = self.dummy_renderer _UpperCamelCase = HeunDiscreteScheduler( beta_schedule='''exp''' , num_train_timesteps=1024 , prediction_type='''sample''' , use_karras_sigmas=_A , clip_sample=_A , clip_sample_range=1.0 , ) _UpperCamelCase = { '''prior''': prior, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''renderer''': renderer, '''scheduler''': scheduler, } return components def UpperCamelCase_ ( self : Tuple , _A : Tuple , _A : Optional[int]=0 ): if str(_A ).startswith('''mps''' ): _UpperCamelCase = torch.manual_seed(_A ) else: _UpperCamelCase = torch.Generator(device=_A ).manual_seed(_A ) _UpperCamelCase = { '''prompt''': '''horse''', '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def UpperCamelCase_ ( self : Any ): _UpperCamelCase = '''cpu''' _UpperCamelCase = self.get_dummy_components() _UpperCamelCase = self.pipeline_class(**_A ) _UpperCamelCase = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) _UpperCamelCase = pipe(**self.get_dummy_inputs(_A ) ) _UpperCamelCase = output.images[0] _UpperCamelCase = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _UpperCamelCase = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def UpperCamelCase_ ( self : Any ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = torch_device == '''cpu''' _UpperCamelCase = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=_A , relax_max_difference=_A , ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = self.get_dummy_components() _UpperCamelCase = self.pipeline_class(**_A ) _UpperCamelCase = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) _UpperCamelCase = 1 _UpperCamelCase = 2 _UpperCamelCase = self.get_dummy_inputs(_A ) for key in inputs.keys(): if key in self.batch_params: _UpperCamelCase = batch_size * [inputs[key]] _UpperCamelCase = pipe(**_A , num_images_per_prompt=_A )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_np_out.npy''' ) _UpperCamelCase = ShapEPipeline.from_pretrained('''openai/shap-e''' ) _UpperCamelCase = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) _UpperCamelCase = torch.Generator(device=_A ).manual_seed(0 ) _UpperCamelCase = pipe( '''a shark''' , generator=_A , guidance_scale=15.0 , num_inference_steps=64 , frame_size=64 , output_type='''np''' , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(_A , _A )
10
0
"""simple docstring""" import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, 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 ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class UpperCAmelCase_ ( __lowercase ): def __init__( self : Union[str, Any] , A : Optional[Any] , A : Any=1_3 , A : Union[str, Any]=7 , A : List[str]=True , A : List[str]=True , A : List[str]=True , A : List[str]=True , A : List[Any]=True , A : Optional[int]=False , A : Any=False , A : int=False , A : Optional[Any]=2 , A : Any=9_9 , A : str=0 , A : Union[str, Any]=3_2 , A : List[Any]=5 , A : Tuple=4 , A : List[str]=0.1 , A : Union[str, Any]=0.1 , A : int=5_1_2 , A : Union[str, Any]=1_2 , A : List[str]=2 , A : int=0.02 , A : Optional[Any]=3 , A : Any=4 , A : Optional[int]="last" , A : Any=None , A : Dict=None , ): _UpperCAmelCase : Union[str, Any] = parent _UpperCAmelCase : Union[str, Any] = batch_size _UpperCAmelCase : str = seq_length _UpperCAmelCase : Union[str, Any] = is_training _UpperCAmelCase : Tuple = use_input_lengths _UpperCAmelCase : Optional[int] = use_token_type_ids _UpperCAmelCase : str = use_labels _UpperCAmelCase : Optional[Any] = gelu_activation _UpperCAmelCase : List[Any] = sinusoidal_embeddings _UpperCAmelCase : Dict = causal _UpperCAmelCase : int = asm _UpperCAmelCase : List[str] = n_langs _UpperCAmelCase : Optional[int] = vocab_size _UpperCAmelCase : str = n_special _UpperCAmelCase : List[Any] = hidden_size _UpperCAmelCase : List[Any] = num_hidden_layers _UpperCAmelCase : Optional[int] = num_attention_heads _UpperCAmelCase : Optional[int] = hidden_dropout_prob _UpperCAmelCase : str = attention_probs_dropout_prob _UpperCAmelCase : int = max_position_embeddings _UpperCAmelCase : List[str] = type_vocab_size _UpperCAmelCase : int = type_sequence_label_size _UpperCAmelCase : Optional[int] = initializer_range _UpperCAmelCase : Any = num_labels _UpperCAmelCase : List[str] = num_choices _UpperCAmelCase : Union[str, Any] = summary_type _UpperCAmelCase : Dict = use_proj _UpperCAmelCase : Tuple = scope def snake_case_ ( self : List[str] ): _UpperCAmelCase : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : List[str] = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase : str = None if self.use_input_lengths: _UpperCAmelCase : List[Any] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length _UpperCAmelCase : List[str] = None if self.use_token_type_ids: _UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) _UpperCAmelCase : List[str] = None _UpperCAmelCase : Optional[Any] = None _UpperCAmelCase : Any = None if self.use_labels: _UpperCAmelCase : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCAmelCase : List[str] = ids_tensor([self.batch_size] , 2 ).float() _UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) _UpperCAmelCase : str = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def snake_case_ ( self : str ): return FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , ) def snake_case_ ( self : str , A : Union[str, Any] , A : Optional[Any] , A : str , A : Tuple , A : List[str] , A : List[Any] , A : Any , A : str , A : Optional[int] , ): _UpperCAmelCase : List[Any] = FlaubertModel(config=_A ) model.to(_A ) model.eval() _UpperCAmelCase : Optional[Any] = model(_A , lengths=_A , langs=_A ) _UpperCAmelCase : Dict = model(_A , langs=_A ) _UpperCAmelCase : List[str] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self : Tuple , A : List[Any] , A : str , A : Optional[int] , A : Optional[Any] , A : List[str] , A : int , A : str , A : List[Any] , A : Any , ): _UpperCAmelCase : Any = FlaubertWithLMHeadModel(_A ) model.to(_A ) model.eval() _UpperCAmelCase : List[str] = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def snake_case_ ( self : Tuple , A : List[str] , A : List[str] , A : Optional[Any] , A : Union[str, Any] , A : str , A : List[str] , A : Tuple , A : Optional[int] , A : Dict , ): _UpperCAmelCase : Optional[Any] = FlaubertForQuestionAnsweringSimple(_A ) model.to(_A ) model.eval() _UpperCAmelCase : List[str] = model(_A ) _UpperCAmelCase : str = model(_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 snake_case_ ( self : Tuple , A : str , A : Tuple , A : Tuple , A : Union[str, Any] , A : List[str] , A : int , A : str , A : Dict , A : List[Any] , ): _UpperCAmelCase : Any = FlaubertForQuestionAnswering(_A ) model.to(_A ) model.eval() _UpperCAmelCase : List[str] = model(_A ) _UpperCAmelCase : Union[str, Any] = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , p_mask=_A , ) _UpperCAmelCase : Optional[int] = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , ) ((_UpperCAmelCase ) , ) : Any = result_with_labels.to_tuple() _UpperCAmelCase : Any = model(_A , start_positions=_A , end_positions=_A ) ((_UpperCAmelCase ) , ) : Union[str, Any] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def snake_case_ ( self : List[Any] , A : Union[str, Any] , A : Tuple , A : str , A : int , A : int , A : Optional[int] , A : Optional[int] , A : int , A : List[str] , ): _UpperCAmelCase : str = FlaubertForSequenceClassification(_A ) model.to(_A ) model.eval() _UpperCAmelCase : Tuple = model(_A ) _UpperCAmelCase : Dict = model(_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def snake_case_ ( self : Optional[int] , A : List[str] , A : Optional[Any] , A : str , A : Union[str, Any] , A : List[Any] , A : int , A : List[Any] , A : str , A : List[str] , ): _UpperCAmelCase : Optional[int] = self.num_labels _UpperCAmelCase : Union[str, Any] = FlaubertForTokenClassification(_A ) model.to(_A ) model.eval() _UpperCAmelCase : int = model(_A , attention_mask=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def snake_case_ ( self : Tuple , A : Dict , A : str , A : Optional[Any] , A : List[str] , A : Any , A : Optional[int] , A : Optional[Any] , A : List[Any] , A : List[str] , ): _UpperCAmelCase : str = self.num_choices _UpperCAmelCase : Optional[Any] = FlaubertForMultipleChoice(config=_A ) model.to(_A ) model.eval() _UpperCAmelCase : int = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCAmelCase : Optional[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCAmelCase : str = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCAmelCase : str = model( _A , attention_mask=_A , token_type_ids=_A , labels=_A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def snake_case_ ( self : Tuple ): _UpperCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) : int = config_and_inputs _UpperCAmelCase : List[Any] = { "input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths, "attention_mask": input_mask, } return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): __SCREAMING_SNAKE_CASE : List[str] = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) __SCREAMING_SNAKE_CASE : Tuple = ( { 'feature-extraction': FlaubertModel, 'fill-mask': FlaubertWithLMHeadModel, 'question-answering': FlaubertForQuestionAnsweringSimple, 'text-classification': FlaubertForSequenceClassification, 'token-classification': FlaubertForTokenClassification, 'zero-shot': FlaubertForSequenceClassification, } if is_torch_available() else {} ) def snake_case_ ( self : Union[str, Any] , A : Dict , A : Dict , A : Tuple , A : int , A : Any ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("Fast" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def snake_case_ ( self : str , A : Any , A : List[str] , A : Optional[int]=False ): _UpperCAmelCase : Optional[Any] = super()._prepare_for_class(_A , _A , return_labels=_A ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": _UpperCAmelCase : Tuple = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) _UpperCAmelCase : Optional[int] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) return inputs_dict def snake_case_ ( self : str ): _UpperCAmelCase : str = FlaubertModelTester(self ) _UpperCAmelCase : Optional[Any] = ConfigTester(self , config_class=_A , emb_dim=3_7 ) def snake_case_ ( self : Optional[Any] ): self.config_tester.run_common_tests() def snake_case_ ( self : str ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*_A ) def snake_case_ ( self : Optional[Any] ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*_A ) def snake_case_ ( self : Optional[Any] ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*_A ) def snake_case_ ( self : Union[str, Any] ): _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*_A ) def snake_case_ ( self : Optional[int] ): _UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*_A ) def snake_case_ ( self : Any ): _UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*_A ) def snake_case_ ( self : Optional[int] ): _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*_A ) @slow def snake_case_ ( self : str ): for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase : Tuple = FlaubertModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @slow @require_torch_gpu def snake_case_ ( self : List[Any] ): _UpperCAmelCase , _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return _UpperCAmelCase : List[str] = True _UpperCAmelCase : Dict = model_class(config=_A ) _UpperCAmelCase : List[Any] = self._prepare_for_class(_A , _A ) _UpperCAmelCase : Optional[int] = torch.jit.trace( _A , (inputs_dict["input_ids"].to("cpu" ), inputs_dict["attention_mask"].to("cpu" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_A , os.path.join(_A , "traced_model.pt" ) ) _UpperCAmelCase : Union[str, Any] = torch.jit.load(os.path.join(_A , "traced_model.pt" ) , map_location=_A ) loaded(inputs_dict["input_ids"].to(_A ) , inputs_dict["attention_mask"].to(_A ) ) @require_torch class UpperCAmelCase_ ( unittest.TestCase ): @slow def snake_case_ ( self : int ): _UpperCAmelCase : List[Any] = FlaubertModel.from_pretrained("flaubert/flaubert_base_cased" ) _UpperCAmelCase : Optional[int] = 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]] ) with torch.no_grad(): _UpperCAmelCase : Union[str, Any] = model(_A )[0] _UpperCAmelCase : List[str] = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , _A ) _UpperCAmelCase : List[Any] = torch.tensor( [[[-2.6_251, -1.4_298, -0.0_227], [-2.8_510, -1.6_387, 0.2_258], [-2.8_114, -1.1_832, -0.3_066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _A , atol=1e-4 ) )
289
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel _lowerCAmelCase = HfApi() _lowerCAmelCase = {} # fmt: off _lowerCAmelCase = torch.tensor([ -0.7515, -1.6883, 0.2420, 0.0300, 0.6347, 1.3433, -1.1743, -3.7467, 1.2342, -2.2485, 0.4636, 0.8076, -0.7991, 0.3969, 0.8498, 0.9189, -1.8887, -3.3522, 0.7639, 0.2040, 0.6271, -2.7148, -1.6316, 3.0839, 0.3186, 0.2721, -0.9759, -1.2461, 2.6257, 1.3557 ]) _lowerCAmelCase = torch.tensor([ -2.3639, -2.5344, 0.0054, -0.6674, 1.5990, 1.0158, 0.3124, -2.1436, 1.8795, -2.5429, -0.1566, -0.3973, 1.2490, 2.6447, 1.2283, -0.5208, -2.8154, -3.5119, 2.3838, 1.2033, 1.7201, -2.1256, -1.4576, 2.7948, 2.4204, -0.9752, -1.2546, 0.8027, 3.2758, 3.1365 ]) _lowerCAmelCase = torch.tensor([ -0.6531, -0.6891, -0.3172, -0.5375, -0.9140, -0.5367, -0.1175, -0.7869, -0.3808, -0.4513, -0.2098, -0.0083, 0.3183, 0.5140, 0.2247, -0.1304, -0.1302, -0.2802, -0.2084, -0.2025, -0.4967, -0.4873, -0.0861, 0.6925, 0.0250, 0.1290, -0.1543, 0.6316, 1.0460, 1.4943 ]) _lowerCAmelCase = torch.tensor([ 0.0911, 0.1107, 0.0182, 0.0435, -0.0805, -0.0608, 0.0381, 0.2172, -0.0280, 0.1327, -0.0299, -0.0255, -0.0050, -0.1170, -0.1046, 0.0309, 0.1367, 0.1728, -0.0533, -0.0748, -0.0534, 0.1624, 0.0384, -0.1805, -0.0707, 0.0642, 0.0220, -0.0134, -0.1333, -0.1505 ]) _lowerCAmelCase = torch.tensor([ 0.1321, 0.1337, 0.0440, 0.0622, -0.0591, -0.0370, 0.0503, 0.2133, -0.0177, 0.1415, -0.0116, -0.0112, 0.0044, -0.0980, -0.0789, 0.0395, 0.1502, 0.1785, -0.0488, -0.0514, -0.0404, 0.1539, 0.0454, -0.1559, -0.0665, 0.0659, 0.0383, -0.0005, -0.1266, -0.1386 ]) _lowerCAmelCase = torch.tensor([ 0.1154, 0.1218, 0.0307, 0.0526, -0.0711, -0.0541, 0.0366, 0.2078, -0.0267, 0.1317, -0.0226, -0.0193, -0.0014, -0.1055, -0.0902, 0.0330, 0.1391, 0.1709, -0.0562, -0.0693, -0.0560, 0.1482, 0.0381, -0.1683, -0.0681, 0.0661, 0.0331, -0.0046, -0.1268, -0.1431 ]) _lowerCAmelCase = torch.tensor([ 0.1192, 0.1240, 0.0414, 0.0606, -0.0557, -0.0412, 0.0430, 0.2042, -0.0200, 0.1385, -0.0115, -0.0132, 0.0017, -0.0965, -0.0802, 0.0398, 0.1433, 0.1747, -0.0458, -0.0533, -0.0407, 0.1545, 0.0419, -0.1574, -0.0645, 0.0626, 0.0341, -0.0010, -0.1199, -0.1390 ]) _lowerCAmelCase = torch.tensor([ 0.1075, 0.1074, 0.0205, 0.0431, -0.0774, -0.0607, 0.0298, 0.2042, -0.0320, 0.1267, -0.0281, -0.0250, -0.0064, -0.1091, -0.0946, 0.0290, 0.1328, 0.1650, -0.0580, -0.0738, -0.0586, 0.1440, 0.0337, -0.1746, -0.0712, 0.0605, 0.0250, -0.0099, -0.1316, -0.1473 ]) _lowerCAmelCase = torch.tensor([ -1.4572, -2.0481, -0.0414, -0.6005, 1.4136, 0.5848, 0.4028, -2.7330, 1.2212, -2.1228, 0.2155, 0.4039, 0.7662, 2.0535, 0.7477, -0.3243, -2.1758, -2.7648, 1.6947, 0.7026, 1.2338, -1.6078, -0.8682, 2.2810, 1.8574, -0.5718, -0.5586, -0.0186, 2.3415, 2.1251]) _lowerCAmelCase = torch.tensor([ -1.3690, -1.9720, -0.4090, -0.6966, 1.4660, 0.9938, -0.1385, -2.7324, 0.7736, -1.8917, 0.2923, 0.4293, 0.1693, 1.4112, 1.1887, -0.3181, -2.2160, -2.6381, 1.3170, 0.8163, 0.9240, -1.6544, -0.6099, 2.5259, 1.6430, -0.9090, -0.9392, -0.0126, 2.4268, 2.3266 ]) _lowerCAmelCase = torch.tensor([ -1.3525, -1.9628, -0.3956, -0.6860, 1.4664, 1.0014, -0.1259, -2.7212, 0.7772, -1.8811, 0.2996, 0.4388, 0.1704, 1.4029, 1.1701, -0.3027, -2.2053, -2.6287, 1.3350, 0.8131, 0.9274, -1.6292, -0.6098, 2.5131, 1.6505, -0.8958, -0.9298, -0.0151, 2.4257, 2.3355 ]) _lowerCAmelCase = torch.tensor([ -2.0585, -2.7897, -0.2850, -0.8940, 1.9052, 0.5702, 0.6345, -3.8959, 1.5932, -3.2319, 0.1974, 0.0287, 1.7566, 2.6543, 0.8387, -0.5351, -3.2736, -4.3375, 2.9029, 1.6390, 1.4640, -2.1701, -1.9013, 2.9341, 3.4981, -0.6255, -1.1644, -0.1591, 3.7097, 3.2066 ]) _lowerCAmelCase = torch.tensor([ -2.3139, -2.5594, -0.0197, -0.6785, 1.7001, 1.1606, 0.3075, -2.1740, 1.8071, -2.5630, -0.0926, -0.3811, 1.2116, 2.6246, 1.2731, -0.5398, -2.8153, -3.6140, 2.3893, 1.3262, 1.6258, -2.1856, -1.3267, 2.8395, 2.3779, -1.0623, -1.2468, 0.8959, 3.3367, 3.2243 ]) _lowerCAmelCase = torch.tensor([ -2.0628, -2.7667, -0.2089, -0.8263, 2.0539, 0.5992, 0.6495, -3.8336, 1.6025, -3.2817, 0.1721, -0.0633, 1.7516, 2.7039, 0.8100, -0.5908, -3.2113, -4.4343, 2.9257, 1.3632, 1.5562, -2.1489, -1.9894, 3.0560, 3.3396, -0.7328, -1.0417, 0.0383, 3.7093, 3.2343 ]) _lowerCAmelCase = torch.tensor([ -1.4574, -2.0569, -0.0473, -0.6117, 1.4018, 0.5769, 0.4129, -2.7344, 1.2241, -2.1397, 0.2000, 0.3937, 0.7616, 2.0453, 0.7324, -0.3391, -2.1746, -2.7744, 1.6963, 0.6921, 1.2187, -1.6172, -0.8877, 2.2439, 1.8471, -0.5839, -0.5605, -0.0464, 2.3250, 2.1219 ]) # fmt: on _lowerCAmelCase = api.list_models(filter="diffusers") for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": _lowerCAmelCase = "/home/patrick/google_checkpoints/" + mod.modelId.split("/")[-1] print(f'Started running {mod.modelId}!!!') if mod.modelId.startswith("CompVis"): _lowerCAmelCase = UNetaDModel.from_pretrained(local_checkpoint, subfolder="unet") else: _lowerCAmelCase = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) _lowerCAmelCase = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) _lowerCAmelCase = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): _lowerCAmelCase = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results["_".join("_".join(mod.modelId.split("/")).split("-"))], atol=1E-3 ) print(f'{mod.modelId} has passed successfully!!!')
10
0
from __future__ import annotations _lowercase : Tuple =8.988E9 # units = N * m^s * C^-2 def lowerCAmelCase_ ( _lowercase : str , _lowercase : Any , _lowercase : Tuple , _lowercase : Optional[Any]) -> Tuple: """simple docstring""" a__ : str = abs(chargea * chargea) if (force, chargea, chargea, distance).count(0) != 1: raise ValueError("""One and only one argument must be 0""") if distance < 0: raise ValueError("""Distance cannot be negative""") if force == 0: a__ : Union[str, Any] = COULOMBS_CONSTANT * charge_product / (distance**2) return {"force": force} elif chargea == 0: a__ : int = abs(__snake_case) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge1": chargea} elif chargea == 0: a__ : Tuple = abs(__snake_case) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge2": chargea} elif distance == 0: a__ : Dict = (COULOMBS_CONSTANT * charge_product / abs(__snake_case)) ** 0.5 return {"distance": distance} raise ValueError("""Exactly one argument must be 0""") if __name__ == "__main__": import doctest doctest.testmod()
136
from typing import List from .keymap import KEYMAP, get_character def _snake_case ( __snake_case ): def decorator(__snake_case ): _UpperCamelCase = getattr(__snake_case , '''handle_key''' , [] ) handle += [key] setattr(__snake_case , '''handle_key''' , __snake_case ) return func return decorator def _snake_case ( *__snake_case ): def decorator(__snake_case ): _UpperCamelCase = getattr(__snake_case , '''handle_key''' , [] ) handle += keys setattr(__snake_case , '''handle_key''' , __snake_case ) return func return decorator class lowerCAmelCase_ ( __lowercase ): def __new__( cls : Optional[Any] , _A : Optional[Any] , _A : Optional[int] , _A : Union[str, Any] ): _UpperCamelCase = super().__new__(cls , _A , _A , _A ) if not hasattr(_A , '''key_handler''' ): setattr(_A , '''key_handler''' , {} ) setattr(_A , '''handle_input''' , KeyHandler.handle_input ) for value in attrs.values(): _UpperCamelCase = getattr(_A , '''handle_key''' , [] ) for key in handled_keys: _UpperCamelCase = value return new_cls @staticmethod def UpperCamelCase_ ( cls : str ): _UpperCamelCase = get_character() if char != KEYMAP["undefined"]: _UpperCamelCase = ord(_A ) _UpperCamelCase = cls.key_handler.get(_A ) if handler: _UpperCamelCase = char return handler(cls ) else: return None def _snake_case ( cls ): return KeyHandler(cls.__name__ , cls.__bases__ , cls.__dict__.copy() )
10
0
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def SCREAMING_SNAKE_CASE_ ( ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ : Optional[int] =[randint(-1_0_0_0 , 1_0_0_0 ) for i in range(1_0 )] SCREAMING_SNAKE_CASE_ : str =randint(-5_0_0_0 , 5_0_0_0 ) return (arr, r) _lowercase = make_dataset() def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[str] ) -> Tuple: for triplet in permutations(__snake_case , 3 ): if sum(__snake_case ) == target: return tuple(sorted(__snake_case ) ) return (0, 0, 0) def SCREAMING_SNAKE_CASE_ ( UpperCAmelCase_ : int , UpperCAmelCase_ : Any ) -> List[str]: arr.sort() SCREAMING_SNAKE_CASE_ : Union[str, Any] =len(__snake_case ) for i in range(n - 1 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict =i + 1, n - 1 while left < right: if arr[i] + arr[left] + arr[right] == target: return (arr[i], arr[left], arr[right]) elif arr[i] + arr[left] + arr[right] < target: left += 1 elif arr[i] + arr[left] + arr[right] > target: right -= 1 return (0, 0, 0) def SCREAMING_SNAKE_CASE_ ( ) -> Dict: SCREAMING_SNAKE_CASE_ : Dict =''' from __main__ import dataset, triplet_sum1, triplet_sum2 ''' SCREAMING_SNAKE_CASE_ : Union[str, Any] =''' triplet_sum1(*dataset) ''' SCREAMING_SNAKE_CASE_ : Tuple =''' triplet_sum2(*dataset) ''' SCREAMING_SNAKE_CASE_ : List[str] =repeat(setup=__snake_case , stmt=__snake_case , repeat=5 , number=1_0_0_0_0 ) SCREAMING_SNAKE_CASE_ : int =repeat(setup=__snake_case , stmt=__snake_case , repeat=5 , number=1_0_0_0_0 ) return (min(__snake_case ), min(__snake_case )) if __name__ == "__main__": from doctest import testmod testmod() _lowercase = solution_times() print(F"The time for naive implementation is {times[0]}.") print(F"The time for optimized implementation is {times[1]}.")
443
import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class lowerCAmelCase_ ( unittest.TestCase ): UpperCAmelCase = MODEL_FOR_CAUSAL_LM_MAPPING UpperCAmelCase = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def UpperCamelCase_ ( self : str ): _UpperCamelCase = pipeline(task='''text-generation''' , model='''sshleifer/tiny-ctrl''' , framework='''pt''' ) # Using `do_sample=False` to force deterministic output _UpperCamelCase = text_generator('''This is a test''' , do_sample=_A ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.''' ''' oscope. FiliFili@@''' ) } ] , ) _UpperCamelCase = text_generator(['''This is a test''', '''This is a second test'''] ) self.assertEqual( _A , [ [ { '''generated_text''': ( '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.''' ''' oscope. FiliFili@@''' ) } ], [ { '''generated_text''': ( '''This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy''' ''' oscope. oscope. FiliFili@@''' ) } ], ] , ) _UpperCamelCase = text_generator('''This is a test''' , do_sample=_A , num_return_sequences=2 , return_tensors=_A ) self.assertEqual( _A , [ {'''generated_token_ids''': ANY(_A )}, {'''generated_token_ids''': ANY(_A )}, ] , ) _UpperCamelCase = text_generator.model.config.eos_token_id _UpperCamelCase = '''<pad>''' _UpperCamelCase = text_generator( ['''This is a test''', '''This is a second test'''] , do_sample=_A , num_return_sequences=2 , batch_size=2 , return_tensors=_A , ) self.assertEqual( _A , [ [ {'''generated_token_ids''': ANY(_A )}, {'''generated_token_ids''': ANY(_A )}, ], [ {'''generated_token_ids''': ANY(_A )}, {'''generated_token_ids''': ANY(_A )}, ], ] , ) @require_tf def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = pipeline(task='''text-generation''' , model='''sshleifer/tiny-ctrl''' , framework='''tf''' ) # Using `do_sample=False` to force deterministic output _UpperCamelCase = text_generator('''This is a test''' , do_sample=_A ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵''' ''' please,''' ) } ] , ) _UpperCamelCase = text_generator(['''This is a test''', '''This is a second test'''] , do_sample=_A ) self.assertEqual( _A , [ [ { '''generated_text''': ( '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵''' ''' please,''' ) } ], [ { '''generated_text''': ( '''This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes''' ''' Cannes 閲閲Cannes Cannes Cannes 攵 please,''' ) } ], ] , ) def UpperCamelCase_ ( self : int , _A : str , _A : Union[str, Any] , _A : Any ): _UpperCamelCase = TextGenerationPipeline(model=_A , tokenizer=_A ) return text_generator, ["This is a test", "Another test"] def UpperCamelCase_ ( self : Union[str, Any] ): _UpperCamelCase = '''Hello I believe in''' _UpperCamelCase = pipeline('''text-generation''' , model='''hf-internal-testing/tiny-random-gpt2''' ) _UpperCamelCase = text_generator(_A ) self.assertEqual( _A , [{'''generated_text''': '''Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe'''}] , ) _UpperCamelCase = text_generator(_A , stop_sequence=''' fe''' ) self.assertEqual(_A , [{'''generated_text''': '''Hello I believe in fe'''}] ) def UpperCamelCase_ ( self : Any , _A : List[Any] , _A : Union[str, Any] ): _UpperCamelCase = text_generator.model _UpperCamelCase = text_generator.tokenizer _UpperCamelCase = text_generator('''This is a test''' ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test''' ) ) _UpperCamelCase = text_generator('''This is a test''' , return_full_text=_A ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertNotIn('''This is a test''' , outputs[0]['''generated_text'''] ) _UpperCamelCase = pipeline(task='''text-generation''' , model=_A , tokenizer=_A , return_full_text=_A ) _UpperCamelCase = text_generator('''This is a test''' ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertNotIn('''This is a test''' , outputs[0]['''generated_text'''] ) _UpperCamelCase = text_generator('''This is a test''' , return_full_text=_A ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test''' ) ) _UpperCamelCase = text_generator(['''This is great !''', '''Something else'''] , num_return_sequences=2 , do_sample=_A ) self.assertEqual( _A , [ [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], ] , ) if text_generator.tokenizer.pad_token is not None: _UpperCamelCase = text_generator( ['''This is great !''', '''Something else'''] , num_return_sequences=2 , batch_size=2 , do_sample=_A ) self.assertEqual( _A , [ [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], [{'''generated_text''': ANY(_A )}, {'''generated_text''': ANY(_A )}], ] , ) with self.assertRaises(_A ): _UpperCamelCase = text_generator('''test''' , return_full_text=_A , return_text=_A ) with self.assertRaises(_A ): _UpperCamelCase = text_generator('''test''' , return_full_text=_A , return_tensors=_A ) with self.assertRaises(_A ): _UpperCamelCase = text_generator('''test''' , return_text=_A , return_tensors=_A ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): _UpperCamelCase = text_generator('''''' ) self.assertEqual(_A , [{'''generated_text''': ANY(_A )}] ) else: with self.assertRaises((ValueError, AssertionError) ): _UpperCamelCase = text_generator('''''' ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. _UpperCamelCase = ['''RwkvForCausalLM''', '''XGLMForCausalLM''', '''GPTNeoXForCausalLM'''] if ( tokenizer.model_max_length < 1_0000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator('''This is a test''' * 500 , max_new_tokens=20 ) _UpperCamelCase = text_generator('''This is a test''' * 500 , handle_long_generation='''hole''' , max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(_A ): text_generator( '''This is a test''' * 500 , handle_long_generation='''hole''' , max_new_tokens=tokenizer.model_max_length + 10 , ) @require_torch @require_accelerate @require_torch_gpu def UpperCamelCase_ ( self : Optional[int] ): import torch # Classic `model_kwargs` _UpperCamelCase = pipeline( model='''hf-internal-testing/tiny-random-bloom''' , model_kwargs={'''device_map''': '''auto''', '''torch_dtype''': torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) _UpperCamelCase = pipe('''This is a test''' ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test test test test test test test test test test test test test test test test''' ''' test''' ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device_map='''auto''' , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) _UpperCamelCase = pipe('''This is a test''' ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test test test test test test test test test test test test test test test test''' ''' test''' ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device_map='''auto''' ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) _UpperCamelCase = pipe('''This is a test''' ) self.assertEqual( _A , [ { '''generated_text''': ( '''This is a test test test test test test test test test test test test test test test test''' ''' test''' ) } ] , ) @require_torch @require_torch_gpu def UpperCamelCase_ ( self : Union[str, Any] ): import torch _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device=0 , torch_dtype=torch.floataa ) pipe('''This is a test''' ) @require_torch @require_accelerate @require_torch_gpu def UpperCamelCase_ ( self : Optional[int] ): import torch _UpperCamelCase = pipeline(model='''hf-internal-testing/tiny-random-bloom''' , device_map='''auto''' , torch_dtype=torch.floataa ) pipe('''This is a test''' , do_sample=_A , top_p=0.5 ) def UpperCamelCase_ ( self : Tuple ): _UpperCamelCase = '''Hello world''' _UpperCamelCase = pipeline('''text-generation''' , model='''hf-internal-testing/tiny-random-gpt2''' ) if text_generator.model.framework == "tf": _UpperCamelCase = logging.get_logger('''transformers.generation.tf_utils''' ) else: _UpperCamelCase = logging.get_logger('''transformers.generation.utils''' ) _UpperCamelCase = '''Both `max_new_tokens`''' # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(_A ) as cl: _UpperCamelCase = text_generator(_A , max_length=10 , max_new_tokens=1 ) self.assertIn(_A , cl.out ) # The user only sets one -> no warning with CaptureLogger(_A ) as cl: _UpperCamelCase = text_generator(_A , max_new_tokens=1 ) self.assertNotIn(_A , cl.out ) with CaptureLogger(_A ) as cl: _UpperCamelCase = text_generator(_A , max_length=10 ) self.assertNotIn(_A , cl.out )
10
0
'''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__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE = argparse.ArgumentParser() parser.add_argument("""--model_ckpt""" , type=__snake_case , default="""microsoft/unixcoder-base-nine""" ) parser.add_argument("""--num_epochs""" , type=__snake_case , default=5 ) parser.add_argument("""--batch_size""" , type=__snake_case , default=6 ) parser.add_argument("""--gradient_accumulation_steps""" , type=__snake_case , default=1 ) parser.add_argument("""--freeze""" , type=__snake_case , default=__snake_case ) parser.add_argument("""--learning_rate""" , type=__snake_case , default=5E-4 ) parser.add_argument("""--seed""" , type=__snake_case , default=0 ) parser.add_argument("""--lr_scheduler_type""" , type=__snake_case , default="""cosine""" ) parser.add_argument("""--num_warmup_steps""" , type=__snake_case , default=10 ) parser.add_argument("""--weight_decay""" , type=__snake_case , default=0.01 ) parser.add_argument("""--output_dir""" , type=__snake_case , default="""./results""" ) return parser.parse_args() UpperCAmelCase : int = load('accuracy') def a__ ( a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = eval_pred __SCREAMING_SNAKE_CASE = np.argmax(__snake_case , axis=1 ) return metric.compute(predictions=__snake_case , references=__snake_case ) class lowerCAmelCase__ ( __lowercase ): """simple docstring""" def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : Any ) -> Optional[int]: """simple docstring""" super().__init__() __SCREAMING_SNAKE_CASE = trainer def UpperCAmelCase__ ( self : int , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any] , **__SCREAMING_SNAKE_CASE : Tuple ) -> Dict: """simple docstring""" if control.should_evaluate: __SCREAMING_SNAKE_CASE = deepcopy(_A ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix="""train""" ) return control_copy def a__ ( ): """simple docstring""" __SCREAMING_SNAKE_CASE = get_args() set_seed(args.seed ) __SCREAMING_SNAKE_CASE = load_dataset("""codeparrot/codecomplex""" , split="""train""" ) __SCREAMING_SNAKE_CASE = dataset.train_test_split(test_size=0.2 ) __SCREAMING_SNAKE_CASE = train_test["""test"""].train_test_split(test_size=0.5 ) __SCREAMING_SNAKE_CASE = DatasetDict( { """train""": train_test["""train"""], """test""": test_validation["""train"""], """valid""": test_validation["""test"""], } ) print("""Loading tokenizer and model""" ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained(args.model_ckpt ) __SCREAMING_SNAKE_CASE = tokenizer.eos_token __SCREAMING_SNAKE_CASE = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) __SCREAMING_SNAKE_CASE = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = ClassLabel(num_classes=7 , names=list(set(train_test_validation["""train"""]["""complexity"""] ) ) ) def tokenize(a__ ): __SCREAMING_SNAKE_CASE = tokenizer(example["""src"""] , truncation=__snake_case , max_length=10_24 ) __SCREAMING_SNAKE_CASE = labels.straint(example["""complexity"""] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } __SCREAMING_SNAKE_CASE = train_test_validation.map( __snake_case , batched=__snake_case , remove_columns=train_test_validation["""train"""].column_names , ) __SCREAMING_SNAKE_CASE = DataCollatorWithPadding(tokenizer=__snake_case ) __SCREAMING_SNAKE_CASE = 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""" , ) __SCREAMING_SNAKE_CASE = Trainer( model=__snake_case , args=__snake_case , train_dataset=tokenized_datasets["""train"""] , eval_dataset=tokenized_datasets["""valid"""] , tokenizer=__snake_case , data_collator=__snake_case , compute_metrics=__snake_case , ) print("""Training...""" ) trainer.add_callback(CustomCallback(__snake_case ) ) trainer.train() if __name__ == "__main__": main()
627
def _snake_case ( __snake_case = 100 ): _UpperCamelCase = (n * (n + 1) // 2) ** 2 _UpperCamelCase = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(f'{solution() = }')
10
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCamelCase = { 'configuration_rag': ['RagConfig'], 'retrieval_rag': ['RagRetriever'], 'tokenization_rag': ['RagTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ 'RagModel', 'RagPreTrainedModel', 'RagSequenceForGeneration', 'RagTokenForGeneration', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ 'TFRagModel', 'TFRagPreTrainedModel', 'TFRagSequenceForGeneration', 'TFRagTokenForGeneration', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
473
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL _lowerCAmelCase = logging.get_logger(__name__) def _snake_case ( __snake_case , __snake_case , __snake_case , __snake_case ): def constraint_to_multiple_of(__snake_case , __snake_case , __snake_case=0 , __snake_case=None ): _UpperCamelCase = round(val / multiple ) * multiple if max_val is not None and x > max_val: _UpperCamelCase = math.floor(val / multiple ) * multiple if x < min_val: _UpperCamelCase = math.ceil(val / multiple ) * multiple return x _UpperCamelCase = (output_size, output_size) if isinstance(__snake_case , __snake_case ) else output_size _UpperCamelCase , _UpperCamelCase = get_image_size(__snake_case ) _UpperCamelCase , _UpperCamelCase = output_size # determine new height and width _UpperCamelCase = output_height / input_height _UpperCamelCase = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width _UpperCamelCase = scale_width else: # fit height _UpperCamelCase = scale_height _UpperCamelCase = constraint_to_multiple_of(scale_height * input_height , multiple=__snake_case ) _UpperCamelCase = constraint_to_multiple_of(scale_width * input_width , multiple=__snake_case ) return (new_height, new_width) class lowerCAmelCase_ ( __lowercase ): UpperCAmelCase = ["pixel_values"] def __init__( self : List[Any] , _A : bool = True , _A : Dict[str, int] = None , _A : PILImageResampling = PILImageResampling.BILINEAR , _A : bool = False , _A : int = 1 , _A : bool = True , _A : Union[int, float] = 1 / 255 , _A : bool = True , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , **_A : List[str] , ): super().__init__(**_A ) _UpperCamelCase = size if size is not None else {'''height''': 384, '''width''': 384} _UpperCamelCase = get_size_dict(_A ) _UpperCamelCase = do_resize _UpperCamelCase = size _UpperCamelCase = keep_aspect_ratio _UpperCamelCase = ensure_multiple_of _UpperCamelCase = resample _UpperCamelCase = do_rescale _UpperCamelCase = rescale_factor _UpperCamelCase = do_normalize _UpperCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _UpperCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCamelCase_ ( self : List[str] , _A : np.ndarray , _A : Dict[str, int] , _A : bool = False , _A : int = 1 , _A : PILImageResampling = PILImageResampling.BICUBIC , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Any , ): _UpperCamelCase = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(F"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) _UpperCamelCase = get_resize_output_image_size( _A , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=_A , multiple=_A , ) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def UpperCamelCase_ ( self : str , _A : np.ndarray , _A : Union[int, float] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Any , ): return rescale(_A , scale=_A , data_format=_A , **_A ) def UpperCamelCase_ ( self : int , _A : np.ndarray , _A : Union[float, List[float]] , _A : Union[float, List[float]] , _A : Optional[Union[str, ChannelDimension]] = None , **_A : Any , ): return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def UpperCamelCase_ ( self : Optional[int] , _A : ImageInput , _A : bool = None , _A : int = None , _A : bool = None , _A : int = None , _A : PILImageResampling = None , _A : bool = None , _A : float = None , _A : bool = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[float, List[float]]] = None , _A : Optional[Union[str, TensorType]] = None , _A : ChannelDimension = ChannelDimension.FIRST , **_A : str , ): _UpperCamelCase = do_resize if do_resize is not None else self.do_resize _UpperCamelCase = size if size is not None else self.size _UpperCamelCase = get_size_dict(_A ) _UpperCamelCase = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio _UpperCamelCase = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of _UpperCamelCase = resample if resample is not None else self.resample _UpperCamelCase = do_rescale if do_rescale is not None else self.do_rescale _UpperCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor _UpperCamelCase = do_normalize if do_normalize is not None else self.do_normalize _UpperCamelCase = image_mean if image_mean is not None else self.image_mean _UpperCamelCase = image_std if image_std is not None else self.image_std _UpperCamelCase = make_list_of_images(_A ) if not valid_images(_A ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. _UpperCamelCase = [to_numpy_array(_A ) for image in images] if do_resize: _UpperCamelCase = [self.resize(image=_A , size=_A , resample=_A ) for image in images] if do_rescale: _UpperCamelCase = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: _UpperCamelCase = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] _UpperCamelCase = [to_channel_dimension_format(_A , _A ) for image in images] _UpperCamelCase = {'''pixel_values''': images} return BatchFeature(data=_A , tensor_type=_A ) def UpperCamelCase_ ( self : Any , _A : Any , _A : List[Tuple] = None ): _UpperCamelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_A ) != len(_A ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(_A ): _UpperCamelCase = target_sizes.numpy() _UpperCamelCase = [] for idx in range(len(_A ) ): _UpperCamelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=_A ) _UpperCamelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_A ) else: _UpperCamelCase = logits.argmax(dim=1 ) _UpperCamelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
10
0
import os def UpperCamelCase ( __lowercase : Any = "input.txt" ): '''simple docstring''' with open(os.path.join(os.path.dirname(__snake_case ) ,__snake_case ) ) as input_file: A_ : List[str] = [ [int(__snake_case ) for element in line.split(',' )] for line in input_file.readlines() ] A_ : List[str] = len(__snake_case ) A_ : Optional[Any] = len(matrix[0] ) A_ : Dict = [[-1 for _ in range(__snake_case )] for _ in range(__snake_case )] for i in range(__snake_case ): A_ : int = matrix[i][0] for j in range(1 ,__snake_case ): for i in range(__snake_case ): A_ : Optional[Any] = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 ,__snake_case ): A_ : Optional[int] = min( minimal_path_sums[i][j] ,minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2 ,-1 ,-1 ): A_ : Tuple = min( minimal_path_sums[i][j] ,minimal_path_sums[i + 1][j] + matrix[i][j] ) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums ) if __name__ == "__main__": print(F"""{solution() = }""")
558
import os import re import shutil import sys import tempfile import unittest import black _lowerCAmelCase = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. _lowerCAmelCase = " \"\"\"\n Output class for the scheduler's step function output.\n\n Args:\n prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n denoising loop.\n pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n `pred_original_sample` can be used to preview progress or for guidance.\n \"\"\"\n\n prev_sample: torch.FloatTensor\n pred_original_sample: Optional[torch.FloatTensor] = None\n" class lowerCAmelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self : List[Any] ): _UpperCamelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , '''schedulers/''' ) ) _UpperCamelCase = self.diffusers_dir shutil.copy( os.path.join(_A , '''src/diffusers/schedulers/scheduling_ddpm.py''' ) , os.path.join(self.diffusers_dir , '''schedulers/scheduling_ddpm.py''' ) , ) def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = '''src/diffusers''' shutil.rmtree(self.diffusers_dir ) def UpperCamelCase_ ( self : str , _A : List[str] , _A : Optional[Any] , _A : List[str] , _A : Optional[int]=None ): _UpperCamelCase = comment + F"""\nclass {class_name}(nn.Module):\n""" + class_code if overwrite_result is not None: _UpperCamelCase = comment + F"""\nclass {class_name}(nn.Module):\n""" + overwrite_result _UpperCamelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _UpperCamelCase = black.format_str(_A , mode=_A ) _UpperCamelCase = os.path.join(self.diffusers_dir , '''new_code.py''' ) with open(_A , '''w''' , newline='''\n''' ) as f: f.write(_A ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_A ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_A ) with open(_A , '''r''' ) as f: self.assertTrue(f.read() , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = check_copies.find_code_in_diffusers('''schedulers.scheduling_ddpm.DDPMSchedulerOutput''' ) self.assertEqual(_A , _A ) def UpperCamelCase_ ( self : List[str] ): # Base copy consistency self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , _A , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , re.sub('''DDPM''' , '''Test''' , _A ) , ) # Copy consistency with a really long name _UpperCamelCase = '''TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( F"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" , F"""{long_class_name}SchedulerOutput""" , re.sub('''Bert''' , _A , _A ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , _A , overwrite_result=re.sub('''DDPM''' , '''Test''' , _A ) , )
10
0
"""simple docstring""" def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__snake_case ) ) def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): # Base Case if index == len(__snake_case ): return True # Recursive Step for i in range(__snake_case ): if valid_coloring(graph[index] , __snake_case , __snake_case ): # Color current vertex __lowercase : List[str] = i # Validate coloring if util_color(__snake_case , __snake_case , __snake_case , index + 1 ): return True # Backtrack __lowercase : Optional[Any] = -1 return False def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase ): __lowercase : Optional[Any] = [-1] * len(__snake_case ) if util_color(__snake_case , __snake_case , __snake_case , 0 ): return colored_vertices return []
76
import json import logging import os import re import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import datasets import numpy as np import torch import torchaudio from packaging import version from torch import nn import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaProcessor, is_apex_available, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse("1.6"): _lowerCAmelCase = True from torch.cuda.amp import autocast _lowerCAmelCase = logging.getLogger(__name__) def _snake_case ( __snake_case=None , __snake_case=None ): return field(default_factory=lambda: default , metadata=__snake_case ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) UpperCAmelCase = field( default=0.1, metadata={"help": "The dropout ratio for the attention probabilities."} ) UpperCAmelCase = field( default=0.1, metadata={"help": "The dropout ratio for activations inside the fully connected layer."} ) UpperCAmelCase = field( default=0.1, metadata={ "help": "The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler." }, ) UpperCAmelCase = field( default=0.1, metadata={"help": "The dropout probabilitiy for all 1D convolutional layers in feature extractor."}, ) UpperCAmelCase = field( default=0.0_5, metadata={ "help": ( "Propability of each feature vector along the time axis to be chosen as the start of the vector" "span to be masked. Approximately ``mask_time_prob * sequence_length // mask_time_length`` feature" "vectors will be masked along the time axis. This is only relevant if ``apply_spec_augment is True``." ) }, ) UpperCAmelCase = field(default=0.0, metadata={"help": "The LayerDrop probability."} ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = field( default=__lowercase, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) UpperCAmelCase = field( default="train+validation", metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" }, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Overwrite the cached preprocessed datasets or not."} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "The number of processes to use for the preprocessing."}, ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) }, ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of validation examples to this " "value if set." ) }, ) UpperCAmelCase = list_field( default=[",", "?", ".", "!", "-", ";", ":", "\"\"", "%", "'", "\"", "�"], metadata={"help": "A list of characters to remove from the transcripts."}, ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = 42 UpperCAmelCase = True UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None def __call__( self : Union[str, Any] , _A : List[Dict[str, Union[List[int], torch.Tensor]]] ): # split inputs and labels since they have to be of different lenghts and need # different padding methods _UpperCamelCase = [{'''input_values''': feature['''input_values''']} for feature in features] _UpperCamelCase = [{'''input_ids''': feature['''labels''']} for feature in features] _UpperCamelCase = self.processor.pad( _A , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) _UpperCamelCase = self.processor.pad( labels=_A , padding=self.padding , max_length=self.max_length_labels , pad_to_multiple_of=self.pad_to_multiple_of_labels , return_tensors='''pt''' , ) # replace padding with -100 to ignore loss correctly _UpperCamelCase = labels_batch['''input_ids'''].masked_fill(labels_batch.attention_mask.ne(1 ) , -100 ) _UpperCamelCase = labels return batch class lowerCAmelCase_ ( __lowercase ): def UpperCamelCase_ ( self : Dict , _A : nn.Module , _A : Dict[str, Union[torch.Tensor, Any]] ): model.train() _UpperCamelCase = self._prepare_inputs(_A ) if self.use_amp: with autocast(): _UpperCamelCase = self.compute_loss(_A , _A ) else: _UpperCamelCase = self.compute_loss(_A , _A ) if self.args.n_gpu > 1: if model.module.config.ctc_loss_reduction == "mean": _UpperCamelCase = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": _UpperCamelCase = loss.sum() / (inputs['''labels'''] >= 0).sum() else: raise ValueError(F"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']""" ) if self.args.gradient_accumulation_steps > 1: _UpperCamelCase = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(_A ).backward() elif self.use_apex: with amp.scale_loss(_A , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(_A ) else: loss.backward() return loss.detach() def _snake_case ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _UpperCamelCase = 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. _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = parser.parse_args_into_dataclasses() # Detecting last checkpoint. _UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _UpperCamelCase = 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.''' ) # 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 )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # 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}""" ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info('''Training/evaluation parameters %s''' , __snake_case ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: _UpperCamelCase = datasets.load_dataset( '''common_voice''' , data_args.dataset_config_name , split=data_args.train_split_name ) _UpperCamelCase = datasets.load_dataset('''common_voice''' , data_args.dataset_config_name , split='''test''' ) # Create and save tokenizer _UpperCamelCase = f"""[{"".join(data_args.chars_to_ignore )}]""" def remove_special_characters(__snake_case ): _UpperCamelCase = re.sub(__snake_case , '''''' , batch['''sentence'''] ).lower() + ''' ''' return batch _UpperCamelCase = train_dataset.map(__snake_case , remove_columns=['''sentence'''] ) _UpperCamelCase = eval_dataset.map(__snake_case , remove_columns=['''sentence'''] ) def extract_all_chars(__snake_case ): _UpperCamelCase = ''' '''.join(batch['''text'''] ) _UpperCamelCase = list(set(__snake_case ) ) return {"vocab": [vocab], "all_text": [all_text]} _UpperCamelCase = train_dataset.map( __snake_case , batched=__snake_case , batch_size=-1 , keep_in_memory=__snake_case , remove_columns=train_dataset.column_names , ) _UpperCamelCase = train_dataset.map( __snake_case , batched=__snake_case , batch_size=-1 , keep_in_memory=__snake_case , remove_columns=eval_dataset.column_names , ) _UpperCamelCase = list(set(vocab_train['''vocab'''][0] ) | set(vocab_test['''vocab'''][0] ) ) _UpperCamelCase = {v: k for k, v in enumerate(__snake_case )} _UpperCamelCase = vocab_dict[''' '''] del vocab_dict[" "] _UpperCamelCase = len(__snake_case ) _UpperCamelCase = len(__snake_case ) with open('''vocab.json''' , '''w''' ) as vocab_file: json.dump(__snake_case , __snake_case ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _UpperCamelCase = WavaVecaCTCTokenizer( '''vocab.json''' , unk_token='''[UNK]''' , pad_token='''[PAD]''' , word_delimiter_token='''|''' , ) _UpperCamelCase = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0.0 , do_normalize=__snake_case , return_attention_mask=__snake_case ) _UpperCamelCase = WavaVecaProcessor(feature_extractor=__snake_case , tokenizer=__snake_case ) _UpperCamelCase = WavaVecaForCTC.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , activation_dropout=model_args.activation_dropout , attention_dropout=model_args.attention_dropout , hidden_dropout=model_args.hidden_dropout , feat_proj_dropout=model_args.feat_proj_dropout , mask_time_prob=model_args.mask_time_prob , gradient_checkpointing=training_args.gradient_checkpointing , layerdrop=model_args.layerdrop , ctc_loss_reduction='''mean''' , pad_token_id=processor.tokenizer.pad_token_id , vocab_size=len(processor.tokenizer ) , ) if data_args.max_train_samples is not None: _UpperCamelCase = min(len(__snake_case ) , data_args.max_train_samples ) _UpperCamelCase = train_dataset.select(range(__snake_case ) ) if data_args.max_val_samples is not None: _UpperCamelCase = eval_dataset.select(range(data_args.max_val_samples ) ) _UpperCamelCase = torchaudio.transforms.Resample(48000 , 16000 ) # Preprocessing the datasets. # We need to read the aduio files as arrays and tokenize the targets. def speech_file_to_array_fn(__snake_case ): _UpperCamelCase , _UpperCamelCase = torchaudio.load(batch['''path'''] ) _UpperCamelCase = resampler(__snake_case ).squeeze().numpy() _UpperCamelCase = 16000 _UpperCamelCase = batch['''text'''] return batch _UpperCamelCase = train_dataset.map( __snake_case , remove_columns=train_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) _UpperCamelCase = eval_dataset.map( __snake_case , remove_columns=eval_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) def prepare_dataset(__snake_case ): # check that all files have the correct sampling rate assert ( len(set(batch['''sampling_rate'''] ) ) == 1 ), f"""Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}.""" _UpperCamelCase = processor( audio=batch['''speech'''] , text=batch['''target_text'''] , sampling_rate=batch['''sampling_rate'''][0] ) batch.update(__snake_case ) return batch _UpperCamelCase = train_dataset.map( __snake_case , remove_columns=train_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=__snake_case , num_proc=data_args.preprocessing_num_workers , ) _UpperCamelCase = eval_dataset.map( __snake_case , remove_columns=eval_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=__snake_case , num_proc=data_args.preprocessing_num_workers , ) # Metric _UpperCamelCase = datasets.load_metric('''wer''' ) def compute_metrics(__snake_case ): _UpperCamelCase = pred.predictions _UpperCamelCase = np.argmax(__snake_case , axis=-1 ) _UpperCamelCase = processor.tokenizer.pad_token_id _UpperCamelCase = processor.batch_decode(__snake_case ) # we do not want to group tokens when computing the metrics _UpperCamelCase = processor.batch_decode(pred.label_ids , group_tokens=__snake_case ) _UpperCamelCase = wer_metric.compute(predictions=__snake_case , references=__snake_case ) return {"wer": wer} if model_args.freeze_feature_extractor: model.freeze_feature_extractor() # Data collator _UpperCamelCase = DataCollatorCTCWithPadding(processor=__snake_case , padding=__snake_case ) # Initialize our Trainer _UpperCamelCase = CTCTrainer( model=__snake_case , data_collator=__snake_case , args=__snake_case , compute_metrics=__snake_case , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=processor.feature_extractor , ) # Training if training_args.do_train: if last_checkpoint is not None: _UpperCamelCase = last_checkpoint elif os.path.isdir(model_args.model_name_or_path ): _UpperCamelCase = model_args.model_name_or_path else: _UpperCamelCase = None # Save the feature_extractor and the tokenizer if is_main_process(training_args.local_rank ): processor.save_pretrained(training_args.output_dir ) _UpperCamelCase = trainer.train(resume_from_checkpoint=__snake_case ) trainer.save_model() _UpperCamelCase = train_result.metrics _UpperCamelCase = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__snake_case ) ) _UpperCamelCase = min(__snake_case , len(__snake_case ) ) trainer.log_metrics('''train''' , __snake_case ) trainer.save_metrics('''train''' , __snake_case ) trainer.save_state() # Evaluation _UpperCamelCase = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) _UpperCamelCase = trainer.evaluate() _UpperCamelCase = data_args.max_val_samples if data_args.max_val_samples is not None else len(__snake_case ) _UpperCamelCase = min(__snake_case , len(__snake_case ) ) trainer.log_metrics('''eval''' , __snake_case ) trainer.save_metrics('''eval''' , __snake_case ) return results if __name__ == "__main__": main()
10
0
'''simple docstring''' import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def lowerCamelCase__ ( a ): __snake_case = np.inf def set_batch_size(a ) -> None: nonlocal batch_size if isinstance(__snake_case , __snake_case ): __snake_case = min(__snake_case , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(__snake_case , __snake_case ): __snake_case = min(__snake_case , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(__snake_case , __snake_case ) and feature.dtype == "binary": __snake_case = min(__snake_case , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(__snake_case , __snake_case ) return None if batch_size is np.inf else batch_size class a_ ( __lowercase ): def __init__( self : List[str] , __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 : Dict , ): super().__init__( _A , split=_A , features=_A , cache_dir=_A , keep_in_memory=_A , streaming=_A , num_proc=_A , **_A , ) __snake_case = path_or_paths if isinstance(_A , _A ) else {self.split: path_or_paths} __snake_case = _PACKAGED_DATASETS_MODULES['parquet'][1] __snake_case = Parquet( cache_dir=_A , data_files=_A , features=_A , hash=_A , **_A , ) def lowercase__ ( self : List[str] ): # Build iterable dataset if self.streaming: __snake_case = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: __snake_case = None __snake_case = None __snake_case = None __snake_case = None self.builder.download_and_prepare( download_config=_A , download_mode=_A , verification_mode=_A , base_path=_A , num_proc=self.num_proc , ) __snake_case = self.builder.as_dataset( split=self.split , verification_mode=_A , in_memory=self.keep_in_memory ) return dataset class a_ : def __init__( self : Optional[Any] , __lowerCAmelCase : Dataset , __lowerCAmelCase : Union[PathLike, BinaryIO] , __lowerCAmelCase : Optional[int] = None , **__lowerCAmelCase : List[str] , ): __snake_case = dataset __snake_case = path_or_buf __snake_case = batch_size or get_writer_batch_size(dataset.features ) __snake_case = parquet_writer_kwargs def lowercase__ ( self : int ): __snake_case = 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: __snake_case = self._write(file_obj=_A , batch_size=_A , **self.parquet_writer_kwargs ) else: __snake_case = self._write(file_obj=self.path_or_buf , batch_size=_A , **self.parquet_writer_kwargs ) return written def lowercase__ ( self : Tuple , __lowerCAmelCase : BinaryIO , __lowerCAmelCase : int , **__lowerCAmelCase : Union[str, Any] ): __snake_case = 0 __snake_case = parquet_writer_kwargs.pop('path_or_buf' , _A ) __snake_case = self.dataset.features.arrow_schema __snake_case = pq.ParquetWriter(_A , schema=_A , **_A ) for offset in logging.tqdm( range(0 , len(self.dataset ) , _A ) , unit='ba' , disable=not logging.is_progress_bar_enabled() , desc='Creating parquet from Arrow format' , ): __snake_case = query_table( table=self.dataset._data , key=slice(_A , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(_A ) written += batch.nbytes writer.close() return written
356
import math class lowerCAmelCase_ : def __init__( self : Tuple , _A : int=0 ): # a graph with Node 0,1,...,N-1 _UpperCamelCase = n _UpperCamelCase = [ [math.inf for j in range(0 , _A )] for i in range(0 , _A ) ] # adjacency matrix for weight _UpperCamelCase = [ [math.inf for j in range(0 , _A )] for i in range(0 , _A ) ] # dp[i][j] stores minimum distance from i to j def UpperCamelCase_ ( self : Dict , _A : str , _A : List[str] , _A : Optional[Any] ): _UpperCamelCase = w def UpperCamelCase_ ( self : Optional[int] ): for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): _UpperCamelCase = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def UpperCamelCase_ ( self : List[str] , _A : Optional[int] , _A : Optional[int] ): return self.dp[u][v] if __name__ == "__main__": _lowerCAmelCase = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
10
0
'''simple docstring''' def snake_case_ ( __snake_case : Tuple = 100) -> Union[str, Any]: lowerCAmelCase_ = (n * (n + 1) // 2) ** 2 lowerCAmelCase_ = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(f'''{solution() = }''')
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging _lowerCAmelCase = logging.get_logger(__name__) def _snake_case ( __snake_case=None , __snake_case=None ): return field(default_factory=lambda: default , metadata=__snake_case ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = list_field( default=[], metadata={ "help": ( "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version" " of all available models" ) }, ) UpperCAmelCase = list_field( default=[8], metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"} ) UpperCAmelCase = list_field( default=[8, 32, 128, 512], metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."}, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Use FP16 to accelerate inference."} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Benchmark training of model"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Verbose memory tracing"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."}, ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory" }, ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Trace memory line by line"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Save result to a CSV file"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Save all print statements in a log file"} ) UpperCAmelCase = field(default=__lowercase, metadata={"help": "Whether to print environment information"} ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use" " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled" " for debugging / testing and on TPU." ) }, ) UpperCAmelCase = field( default=F"""inference_time_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving time results to csv."}, ) UpperCAmelCase = field( default=F"""inference_memory_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving memory results to csv."}, ) UpperCAmelCase = field( default=F"""train_time_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving time results to csv for training."}, ) UpperCAmelCase = field( default=F"""train_memory_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving memory results to csv for training."}, ) UpperCAmelCase = field( default=F"""env_info_{round(time() )}.csv""", metadata={"help": "CSV filename used if saving environment information."}, ) UpperCAmelCase = field( default=F"""log_{round(time() )}.csv""", metadata={"help": "Log filename used if print statements are saved in log."}, ) UpperCAmelCase = field(default=3, metadata={"help": "Times an experiment will be run."} ) UpperCAmelCase = field( default=__lowercase, metadata={ "help": ( "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain" " model weights." ) }, ) def UpperCamelCase_ ( self : Union[str, Any] ): warnings.warn( F"""The class {self.__class__} is deprecated. Hugging Face Benchmarking utils""" ''' are deprecated in general and it is advised to use external Benchmarking libraries ''' ''' to benchmark Transformer models.''' , _A , ) def UpperCamelCase_ ( self : str ): return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def UpperCamelCase_ ( self : List[Any] ): if len(self.models ) <= 0: raise ValueError( '''Please make sure you provide at least one model name / model identifier, *e.g.* `--models''' ''' bert-base-cased` or `args.models = [\'bert-base-cased\'].''' ) return self.models @property def UpperCamelCase_ ( self : Optional[int] ): if not self.multi_process: return False elif self.is_tpu: logger.info('''Multiprocessing is currently not possible on TPU.''' ) return False else: return True
10
0
from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class UpperCamelCase_ : lowerCamelCase_ = 42 lowerCamelCase_ = None lowerCamelCase_ = None def SCREAMING_SNAKE_CASE__ ( ): SCREAMING_SNAKE_CASE__ = Node(1 ) SCREAMING_SNAKE_CASE__ = Node(2 ) SCREAMING_SNAKE_CASE__ = Node(3 ) SCREAMING_SNAKE_CASE__ = Node(4 ) SCREAMING_SNAKE_CASE__ = Node(5 ) return tree def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: List[Any] ): return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: str ): return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: List[str] ): return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: List[str] ): return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Optional[int] ): SCREAMING_SNAKE_CASE__ = [] if root is None: return output SCREAMING_SNAKE_CASE__ = deque([root] ) while process_queue: SCREAMING_SNAKE_CASE__ = 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 SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Any , UpperCamelCase__: Optional[Any] ): SCREAMING_SNAKE_CASE__ = [] def populate_output(UpperCamelCase__: Optional[int] , UpperCamelCase__: Optional[Any] ) -> 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(__snake_case , __snake_case ) return output def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: Union[str, Any] , UpperCamelCase__: List[Any] ): SCREAMING_SNAKE_CASE__ = [] def populate_output(UpperCamelCase__: Optional[int] , UpperCamelCase__: Tuple ) -> 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(__snake_case , __snake_case ) return output def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: str ): if root is None: return [] SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = height(__snake_case ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(__snake_case , __snake_case ) ) SCREAMING_SNAKE_CASE__ = 1 else: output.append(get_nodes_from_right_to_left(__snake_case , __snake_case ) ) SCREAMING_SNAKE_CASE__ = 0 return output def SCREAMING_SNAKE_CASE__ ( ): # Main function for testing. SCREAMING_SNAKE_CASE__ = make_tree() print(f'''In-order Traversal: {inorder(__snake_case )}''' ) print(f'''Pre-order Traversal: {preorder(__snake_case )}''' ) print(f'''Post-order Traversal: {postorder(__snake_case )}''' , """\n""" ) print(f'''Height of Tree: {height(__snake_case )}''' , """\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(__snake_case ) , """\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 , height(__snake_case ) + 1 ): print(f'''Level {level}:''' , get_nodes_from_left_to_right(__snake_case , level=__snake_case ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(__snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
6
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _snake_case ( *__snake_case , __snake_case = None , __snake_case=True , __snake_case=2 ): from .. import __version__ _UpperCamelCase = take_from _UpperCamelCase = () if not isinstance(args[0] , __snake_case ): _UpperCamelCase = (args,) for attribute, version_name, message in args: if version.parse(version.parse(__snake_case ).base_version ) >= version.parse(__snake_case ): raise ValueError( f"""The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'""" f""" version {__version__} is >= {version_name}""" ) _UpperCamelCase = None if isinstance(__snake_case , __snake_case ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(__snake_case ),) _UpperCamelCase = f"""The `{attribute}` argument is deprecated and will be removed in version {version_name}.""" elif hasattr(__snake_case , __snake_case ): values += (getattr(__snake_case , __snake_case ),) _UpperCamelCase = f"""The `{attribute}` attribute is deprecated and will be removed in version {version_name}.""" elif deprecated_kwargs is None: _UpperCamelCase = f"""`{attribute}` is deprecated and will be removed in version {version_name}.""" if warning is not None: _UpperCamelCase = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , __snake_case , stacklevel=__snake_case ) if isinstance(__snake_case , __snake_case ) and len(__snake_case ) > 0: _UpperCamelCase = inspect.getouterframes(inspect.currentframe() )[1] _UpperCamelCase = call_frame.filename _UpperCamelCase = call_frame.lineno _UpperCamelCase = call_frame.function _UpperCamelCase , _UpperCamelCase = next(iter(deprecated_kwargs.items() ) ) raise TypeError(f"""{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`""" ) if len(__snake_case ) == 0: return elif len(__snake_case ) == 1: return values[0] return values
10
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase__ : List[str] = logging.get_logger(__name__) lowercase__ : Union[str, Any] = { '''kssteven/ibert-roberta-base''': '''https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json''', '''kssteven/ibert-roberta-large''': '''https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json''', '''kssteven/ibert-roberta-large-mnli''': ( '''https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json''' ), } class lowercase_ ( __lowercase ): """simple docstring""" UpperCAmelCase_ : Dict = """ibert""" def __init__( self , __SCREAMING_SNAKE_CASE=30522 , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.0_2 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE="absolute" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="none" , **__SCREAMING_SNAKE_CASE , ) ->Any: super().__init__(pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , **_A ) lowerCAmelCase = vocab_size lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = hidden_act lowerCAmelCase = intermediate_size 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 = position_embedding_type lowerCAmelCase = quant_mode lowerCAmelCase = force_dequant class lowercase_ ( __lowercase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self ) ->str: if self.task == "multiple-choice": lowerCAmelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowerCAmelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
312
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process _lowerCAmelCase = logging.getLogger(__name__) def _snake_case ( __snake_case , __snake_case ): return (preds == labels).mean() @dataclass class lowerCAmelCase_ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Pretrained config name or path if not the same as model_name"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) @dataclass class lowerCAmelCase_ : UpperCAmelCase = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) UpperCAmelCase = field(metadata={"help": "Should contain the data files for the task."} ) UpperCAmelCase = 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." ) }, ) UpperCAmelCase = field( default=__lowercase, metadata={"help": "Overwrite the cached training and evaluation sets"} ) def _snake_case ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = 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.''' ) # 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''' , __snake_case ) # Set seed set_seed(training_args.seed ) try: _UpperCamelCase = processors[data_args.task_name]() _UpperCamelCase = processor.get_labels() _UpperCamelCase = len(__snake_case ) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _UpperCamelCase = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=__snake_case , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) _UpperCamelCase = 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 , ) _UpperCamelCase = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=__snake_case , cache_dir=model_args.cache_dir , ) # Get datasets _UpperCamelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__snake_case , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) _UpperCamelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=__snake_case , task=data_args.task_name , 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 compute_metrics(__snake_case ) -> Dict: _UpperCamelCase = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(__snake_case , p.label_ids )} # Data collator _UpperCamelCase = DataCollatorWithPadding(__snake_case , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer _UpperCamelCase = Trainer( model=__snake_case , args=__snake_case , train_dataset=__snake_case , eval_dataset=__snake_case , compute_metrics=__snake_case , data_collator=__snake_case , ) # 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_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation _UpperCamelCase = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) _UpperCamelCase = trainer.evaluate() _UpperCamelCase = os.path.join(training_args.output_dir , '''eval_results.txt''' ) if trainer.is_world_master(): with open(__snake_case , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key, value in result.items(): logger.info(''' %s = %s''' , __snake_case , __snake_case ) writer.write('''%s = %s\n''' % (key, value) ) results.update(__snake_case ) return results def _snake_case ( __snake_case ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
10
0
"""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 snake_case_ ( self : str ): _UpperCAmelCase : Tuple = torch.nn.Linear(1_0 , 1_0 ) _UpperCAmelCase : List[str] = torch.optim.SGD(model.parameters() , 0.1 ) _UpperCAmelCase : Any = Accelerator() _UpperCAmelCase : Any = accelerator.prepare(_A ) try: pickle.loads(pickle.dumps(_A ) ) except Exception as e: self.fail(f'Accelerated optimizer pickling failed with {e}' ) AcceleratorState._reset_state()
289
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCAmelCase = logging.get_logger(__name__) _lowerCAmelCase = { "microsoft/trocr-base-handwritten": ( "https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json" ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class lowerCAmelCase_ ( __lowercase ): UpperCAmelCase = "trocr" UpperCAmelCase = ["past_key_values"] UpperCAmelCase = { "num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model", "num_hidden_layers": "decoder_layers", } def __init__( self : List[str] , _A : Optional[Any]=5_0265 , _A : Optional[Any]=1024 , _A : Optional[Any]=12 , _A : Any=16 , _A : Any=4096 , _A : Optional[Any]="gelu" , _A : Union[str, Any]=512 , _A : Dict=0.1 , _A : List[str]=0.0 , _A : Optional[Any]=0.0 , _A : Union[str, Any]=2 , _A : Any=0.02 , _A : List[str]=0.0 , _A : List[str]=True , _A : str=False , _A : List[str]=True , _A : Optional[Any]=True , _A : Optional[int]=1 , _A : int=0 , _A : Any=2 , **_A : Optional[int] , ): _UpperCamelCase = vocab_size _UpperCamelCase = d_model _UpperCamelCase = decoder_layers _UpperCamelCase = decoder_attention_heads _UpperCamelCase = decoder_ffn_dim _UpperCamelCase = activation_function _UpperCamelCase = max_position_embeddings _UpperCamelCase = dropout _UpperCamelCase = attention_dropout _UpperCamelCase = activation_dropout _UpperCamelCase = init_std _UpperCamelCase = decoder_layerdrop _UpperCamelCase = use_cache _UpperCamelCase = scale_embedding _UpperCamelCase = use_learned_position_embeddings _UpperCamelCase = layernorm_embedding super().__init__( pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , decoder_start_token_id=_A , **_A , )
10
0
import os from tempfile import TemporaryDirectory from unittest import TestCase import pytest from absl.testing import parameterized from datasets import config from datasets.arrow_reader import HF_GCP_BASE_URL from datasets.builder import DatasetBuilder from datasets.dataset_dict import IterableDatasetDict from datasets.iterable_dataset import IterableDataset from datasets.load import dataset_module_factory, import_main_class from datasets.utils.file_utils import cached_path _lowercase : Any =[ {"dataset": "wikipedia", "config_name": "20220301.de"}, {"dataset": "wikipedia", "config_name": "20220301.en"}, {"dataset": "wikipedia", "config_name": "20220301.fr"}, {"dataset": "wikipedia", "config_name": "20220301.frr"}, {"dataset": "wikipedia", "config_name": "20220301.it"}, {"dataset": "wikipedia", "config_name": "20220301.simple"}, {"dataset": "snli", "config_name": "plain_text"}, {"dataset": "eli5", "config_name": "LFQA_reddit"}, {"dataset": "wiki40b", "config_name": "en"}, {"dataset": "wiki_dpr", "config_name": "psgs_w100.nq.compressed"}, {"dataset": "wiki_dpr", "config_name": "psgs_w100.nq.no_index"}, {"dataset": "wiki_dpr", "config_name": "psgs_w100.multiset.no_index"}, {"dataset": "natural_questions", "config_name": "default"}, ] def lowerCAmelCase_ ( _lowercase : int=True) -> Optional[Any]: """simple docstring""" if with_config: return [ { "testcase_name": d["dataset"] + "/" + d["config_name"], "dataset": d["dataset"], "config_name": d["config_name"], } for d in DATASETS_ON_HF_GCP ] else: return [ {"testcase_name": dataset, "dataset": dataset} for dataset in {d["dataset"] for d in DATASETS_ON_HF_GCP} ] @parameterized.named_parameters(list_datasets_on_hf_gcp_parameters(with_config=__lowercase ) ) class snake_case__ (__lowercase ): """simple docstring""" __lowerCAmelCase :Dict = None __lowerCAmelCase :Dict = None def SCREAMING_SNAKE_CASE__( self , __lowercase , __lowercase ) -> Optional[Any]: """simple docstring""" with TemporaryDirectory() as tmp_dir: a__ : List[str] = dataset_module_factory(_A , cache_dir=_A ) a__ : int = import_main_class(dataset_module.module_path , dataset=_A ) a__ : List[str] = builder_cls( cache_dir=_A , config_name=_A , hash=dataset_module.hash , ) a__ : Any = """/""".join( [ HF_GCP_BASE_URL, builder_instance._relative_data_dir(with_hash=_A ).replace(os.sep , """/""" ), config.DATASET_INFO_FILENAME, ] ) a__ : Dict = cached_path(_A , cache_dir=_A ) self.assertTrue(os.path.exists(_A ) ) @pytest.mark.integration def lowerCAmelCase_ ( _lowercase : int) -> Union[str, Any]: """simple docstring""" a__ : List[Any] = tmp_path_factory.mktemp("""test_hf_gcp""") / """test_wikipedia_simple""" a__ : Any = dataset_module_factory("""wikipedia""" , cache_dir=__snake_case) a__ : int = import_main_class(dataset_module.module_path) a__ : List[Any] = builder_cls( cache_dir=__snake_case , config_name="""20220301.frr""" , hash=dataset_module.hash , ) # use the HF cloud storage, not the original download_and_prepare that uses apache-beam a__ : str = None builder_instance.download_and_prepare() a__ : int = builder_instance.as_dataset() assert ds @pytest.mark.integration def lowerCAmelCase_ ( _lowercase : Union[str, Any]) -> Optional[int]: """simple docstring""" a__ : Union[str, Any] = dataset_module_factory("""wikipedia""" , cache_dir=__snake_case) a__ : Tuple = import_main_class(dataset_module.module_path , dataset=__snake_case) a__ : int = builder_cls( cache_dir=__snake_case , config_name="""20220301.frr""" , hash=dataset_module.hash , ) a__ : List[Any] = builder_instance.as_streaming_dataset() assert ds assert isinstance(__snake_case , __snake_case) assert "train" in ds assert isinstance(ds["""train"""] , __snake_case) assert next(iter(ds["""train"""]))
136
import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, 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 ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase_ ( __lowercase ): def __init__( self : Union[str, Any] , _A : Optional[Any] , _A : Any=13 , _A : Union[str, Any]=7 , _A : List[str]=True , _A : List[str]=True , _A : List[str]=True , _A : List[str]=True , _A : List[Any]=True , _A : Optional[int]=False , _A : Any=False , _A : int=False , _A : Optional[Any]=2 , _A : Any=99 , _A : str=0 , _A : Union[str, Any]=32 , _A : List[Any]=5 , _A : Tuple=4 , _A : List[str]=0.1 , _A : Union[str, Any]=0.1 , _A : int=512 , _A : Union[str, Any]=12 , _A : List[str]=2 , _A : int=0.02 , _A : Optional[Any]=3 , _A : Any=4 , _A : Optional[int]="last" , _A : Any=None , _A : Dict=None , ): _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = seq_length _UpperCamelCase = is_training _UpperCamelCase = use_input_lengths _UpperCamelCase = use_token_type_ids _UpperCamelCase = use_labels _UpperCamelCase = gelu_activation _UpperCamelCase = sinusoidal_embeddings _UpperCamelCase = causal _UpperCamelCase = asm _UpperCamelCase = n_langs _UpperCamelCase = vocab_size _UpperCamelCase = n_special _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = max_position_embeddings _UpperCamelCase = type_vocab_size _UpperCamelCase = type_sequence_label_size _UpperCamelCase = initializer_range _UpperCamelCase = num_labels _UpperCamelCase = num_choices _UpperCamelCase = summary_type _UpperCamelCase = use_proj _UpperCamelCase = scope def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCamelCase = None if self.use_input_lengths: _UpperCamelCase = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length _UpperCamelCase = None if self.use_token_type_ids: _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) _UpperCamelCase = None _UpperCamelCase = None _UpperCamelCase = None if self.use_labels: _UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCamelCase = ids_tensor([self.batch_size] , 2 ).float() _UpperCamelCase = ids_tensor([self.batch_size] , self.num_choices ) _UpperCamelCase = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def UpperCamelCase_ ( self : str ): return FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , ) def UpperCamelCase_ ( self : str , _A : Union[str, Any] , _A : Optional[Any] , _A : str , _A : Tuple , _A : List[str] , _A : List[Any] , _A : Any , _A : str , _A : Optional[int] , ): _UpperCamelCase = FlaubertModel(config=_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A , lengths=_A , langs=_A ) _UpperCamelCase = model(_A , langs=_A ) _UpperCamelCase = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase_ ( self : Tuple , _A : List[Any] , _A : str , _A : Optional[int] , _A : Optional[Any] , _A : List[str] , _A : int , _A : str , _A : List[Any] , _A : Any , ): _UpperCamelCase = FlaubertWithLMHeadModel(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase_ ( self : Tuple , _A : List[str] , _A : List[str] , _A : Optional[Any] , _A : Union[str, Any] , _A : str , _A : List[str] , _A : Tuple , _A : Optional[int] , _A : Dict , ): _UpperCamelCase = FlaubertForQuestionAnsweringSimple(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A ) _UpperCamelCase = model(_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 : Tuple , _A : str , _A : Tuple , _A : Tuple , _A : Union[str, Any] , _A : List[str] , _A : int , _A : str , _A : Dict , _A : List[Any] , ): _UpperCamelCase = FlaubertForQuestionAnswering(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A ) _UpperCamelCase = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , p_mask=_A , ) _UpperCamelCase = model( _A , start_positions=_A , end_positions=_A , cls_index=_A , is_impossible=_A , ) ((_UpperCamelCase) , ) = result_with_labels.to_tuple() _UpperCamelCase = model(_A , start_positions=_A , end_positions=_A ) ((_UpperCamelCase) , ) = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape , () ) self.parent.assertEqual(result.start_top_log_probs.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape , (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape , (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape , (self.batch_size,) ) def UpperCamelCase_ ( self : List[Any] , _A : Union[str, Any] , _A : Tuple , _A : str , _A : int , _A : int , _A : Optional[int] , _A : Optional[int] , _A : int , _A : List[str] , ): _UpperCamelCase = FlaubertForSequenceClassification(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A ) _UpperCamelCase = model(_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCamelCase_ ( self : Optional[int] , _A : List[str] , _A : Optional[Any] , _A : str , _A : Union[str, Any] , _A : List[Any] , _A : int , _A : List[Any] , _A : str , _A : List[str] , ): _UpperCamelCase = self.num_labels _UpperCamelCase = FlaubertForTokenClassification(_A ) model.to(_A ) model.eval() _UpperCamelCase = model(_A , attention_mask=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase_ ( self : Tuple , _A : Dict , _A : str , _A : Optional[Any] , _A : List[str] , _A : Any , _A : Optional[int] , _A : Optional[Any] , _A : List[Any] , _A : List[str] , ): _UpperCamelCase = self.num_choices _UpperCamelCase = FlaubertForMultipleChoice(config=_A ) model.to(_A ) model.eval() _UpperCamelCase = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCamelCase = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCamelCase = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCamelCase = model( _A , attention_mask=_A , token_type_ids=_A , labels=_A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase_ ( self : Tuple ): _UpperCamelCase = self.prepare_config_and_inputs() ( ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ( _UpperCamelCase ) , ) = config_and_inputs _UpperCamelCase = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class lowerCAmelCase_ ( __lowercase, __lowercase, unittest.TestCase ): UpperCAmelCase = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) UpperCAmelCase = ( { "feature-extraction": FlaubertModel, "fill-mask": FlaubertWithLMHeadModel, "question-answering": FlaubertForQuestionAnsweringSimple, "text-classification": FlaubertForSequenceClassification, "token-classification": FlaubertForTokenClassification, "zero-shot": FlaubertForSequenceClassification, } if is_torch_available() else {} ) def UpperCamelCase_ ( self : Union[str, Any] , _A : Dict , _A : Dict , _A : Tuple , _A : int , _A : Any ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def UpperCamelCase_ ( self : str , _A : Any , _A : List[str] , _A : Optional[int]=False ): _UpperCamelCase = super()._prepare_for_class(_A , _A , return_labels=_A ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": _UpperCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) _UpperCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_A ) return inputs_dict def UpperCamelCase_ ( self : str ): _UpperCamelCase = FlaubertModelTester(self ) _UpperCamelCase = ConfigTester(self , config_class=_A , emb_dim=37 ) def UpperCamelCase_ ( self : Optional[Any] ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : str ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*_A ) def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*_A ) def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*_A ) def UpperCamelCase_ ( self : Union[str, Any] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*_A ) def UpperCamelCase_ ( self : Optional[int] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*_A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*_A ) def UpperCamelCase_ ( self : Optional[int] ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*_A ) @slow def UpperCamelCase_ ( self : str ): for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCamelCase = FlaubertModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @slow @require_torch_gpu def UpperCamelCase_ ( self : List[Any] ): _UpperCamelCase , _UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return _UpperCamelCase = True _UpperCamelCase = model_class(config=_A ) _UpperCamelCase = self._prepare_for_class(_A , _A ) _UpperCamelCase = torch.jit.trace( _A , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_A , os.path.join(_A , '''traced_model.pt''' ) ) _UpperCamelCase = torch.jit.load(os.path.join(_A , '''traced_model.pt''' ) , map_location=_A ) loaded(inputs_dict['''input_ids'''].to(_A ) , inputs_dict['''attention_mask'''].to(_A ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): @slow def UpperCamelCase_ ( self : int ): _UpperCamelCase = FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased''' ) _UpperCamelCase = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) with torch.no_grad(): _UpperCamelCase = model(_A )[0] _UpperCamelCase = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , _A ) _UpperCamelCase = torch.tensor( [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _A , atol=1e-4 ) )
10
0
def SCREAMING_SNAKE_CASE_ ( ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ : Dict =0 for i in range(1 , 1_0_0_1 ): total += i**i return str(__snake_case )[-1_0:] if __name__ == "__main__": print(solution())
443
from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase_ : def __init__( self : Any , _A : int , _A : int=12 , _A : int=7 , _A : Tuple=True , _A : Optional[int]=True , _A : Union[str, Any]=True , _A : str=99 , _A : str=32 , _A : int=32 , _A : Optional[Any]=2 , _A : Dict=4 , _A : int=37 , _A : List[Any]=0.1 , _A : str=0.1 , _A : Any=512 , _A : int=0.02 , _A : Optional[Any]=0 , _A : Dict=None , ): _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = seq_length _UpperCamelCase = is_training _UpperCamelCase = use_input_mask _UpperCamelCase = use_labels _UpperCamelCase = vocab_size _UpperCamelCase = hidden_size _UpperCamelCase = projection_dim _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = intermediate_size _UpperCamelCase = dropout _UpperCamelCase = attention_dropout _UpperCamelCase = max_position_embeddings _UpperCamelCase = initializer_range _UpperCamelCase = scope _UpperCamelCase = bos_token_id def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCamelCase = None if self.use_input_mask: _UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: _UpperCamelCase = input_mask.numpy() _UpperCamelCase , _UpperCamelCase = input_mask.shape _UpperCamelCase = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(_A ): _UpperCamelCase = 1 _UpperCamelCase = 0 _UpperCamelCase = self.get_config() return config, input_ids, tf.convert_to_tensor(_A ) def UpperCamelCase_ ( self : str ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def UpperCamelCase_ ( self : List[str] , _A : Tuple , _A : str , _A : Optional[Any] ): _UpperCamelCase = TFBlipTextModel(config=_A ) _UpperCamelCase = model(_A , attention_mask=_A , training=_A ) _UpperCamelCase = model(_A , training=_A ) 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 UpperCamelCase_ ( self : Tuple ): _UpperCamelCase = self.prepare_config_and_inputs() _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = config_and_inputs _UpperCamelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowerCAmelCase_ ( __lowercase, unittest.TestCase ): UpperCAmelCase = (TFBlipTextModel,) if is_tf_available() else () UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = BlipTextModelTester(self ) _UpperCamelCase = ConfigTester(self , config_class=_A , hidden_size=37 ) def UpperCamelCase_ ( self : Dict ): self.config_tester.run_common_tests() def UpperCamelCase_ ( self : int ): _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCamelCase_ ( self : List[Any] ): pass def UpperCamelCase_ ( self : Tuple ): pass @unittest.skip(reason='''Blip does not use inputs_embeds''' ) def UpperCamelCase_ ( self : Dict ): pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCamelCase_ ( self : Dict ): pass @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING''' ) def UpperCamelCase_ ( self : List[str] ): pass @slow def UpperCamelCase_ ( self : Optional[int] ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCamelCase = TFBlipTextModel.from_pretrained(_A ) self.assertIsNotNone(_A ) def UpperCamelCase_ ( self : int , _A : Optional[int]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=_A )
10
0
'''simple docstring''' def a__ ( a__ = 10**12 ): """simple docstring""" __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 0 __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = 1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f"""{solution() = }""")
627
from __future__ import annotations _lowerCAmelCase = [True] * 1_000_001 _lowerCAmelCase = 2 while i * i <= 1_000_000: if seive[i]: for j in range(i * i, 1_000_001, i): _lowerCAmelCase = False i += 1 def _snake_case ( __snake_case ): return seive[n] def _snake_case ( __snake_case ): return any(digit in '''02468''' for digit in str(__snake_case ) ) def _snake_case ( __snake_case = 1000000 ): _UpperCamelCase = [2] # result already includes the number 2. for num in range(3 , limit + 1 , 2 ): if is_prime(__snake_case ) and not contains_an_even_digit(__snake_case ): _UpperCamelCase = str(__snake_case ) _UpperCamelCase = [int(str_num[j:] + str_num[:j] ) for j in range(len(__snake_case ) )] if all(is_prime(__snake_case ) for i in list_nums ): result.append(__snake_case ) return result def _snake_case ( ): return len(find_circular_primes() ) if __name__ == "__main__": print(f'{len(find_circular_primes()) = }')
10
0
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" lowerCAmelCase__ : Tuple = MODEL_FOR_CAUSAL_LM_MAPPING lowerCAmelCase__ : Union[str, Any] = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def A ( self ) -> Union[str, Any]: a_ : Tuple = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="pt" ) # Using `do_sample=False` to force deterministic output a_ : Union[str, Any] = text_generator("This is a test" , do_sample=_A ) self.assertEqual( _A , [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ] , ) a_ : List[Any] = text_generator(["This is a test", "This is a second test"] ) self.assertEqual( _A , [ [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ], [ { "generated_text": ( "This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy" " oscope. oscope. FiliFili@@" ) } ], ] , ) a_ : str = text_generator("This is a test" , do_sample=_A , num_return_sequences=2 , return_tensors=_A ) self.assertEqual( _A , [ {"generated_token_ids": ANY(_A )}, {"generated_token_ids": ANY(_A )}, ] , ) a_ : Any = text_generator.model.config.eos_token_id a_ : str = "<pad>" a_ : str = text_generator( ["This is a test", "This is a second test"] , do_sample=_A , num_return_sequences=2 , batch_size=2 , return_tensors=_A , ) self.assertEqual( _A , [ [ {"generated_token_ids": ANY(_A )}, {"generated_token_ids": ANY(_A )}, ], [ {"generated_token_ids": ANY(_A )}, {"generated_token_ids": ANY(_A )}, ], ] , ) @require_tf def A ( self ) -> Union[str, Any]: a_ : str = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="tf" ) # Using `do_sample=False` to force deterministic output a_ : int = text_generator("This is a test" , do_sample=_A ) self.assertEqual( _A , [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ] , ) a_ : str = text_generator(["This is a test", "This is a second test"] , do_sample=_A ) self.assertEqual( _A , [ [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ], [ { "generated_text": ( "This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes" " Cannes 閲閲Cannes Cannes Cannes 攵 please," ) } ], ] , ) def A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: a_ : Any = TextGenerationPipeline(model=_A , tokenizer=_A ) return text_generator, ["This is a test", "Another test"] def A ( self ) -> List[str]: a_ : str = "Hello I believe in" a_ : List[str] = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) a_ : Optional[Any] = text_generator(_A ) self.assertEqual( _A , [{"generated_text": "Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"}] , ) a_ : Optional[Any] = text_generator(_A , stop_sequence=" fe" ) self.assertEqual(_A , [{"generated_text": "Hello I believe in fe"}] ) def A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: a_ : int = text_generator.model a_ : Optional[int] = text_generator.tokenizer a_ : str = text_generator("This is a test" ) self.assertEqual(_A , [{"generated_text": ANY(_A )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) a_ : Tuple = text_generator("This is a test" , return_full_text=_A ) self.assertEqual(_A , [{"generated_text": ANY(_A )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) a_ : int = pipeline(task="text-generation" , model=_A , tokenizer=_A , return_full_text=_A ) a_ : Union[str, Any] = text_generator("This is a test" ) self.assertEqual(_A , [{"generated_text": ANY(_A )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) a_ : Any = text_generator("This is a test" , return_full_text=_A ) self.assertEqual(_A , [{"generated_text": ANY(_A )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) a_ : int = text_generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=_A ) self.assertEqual( _A , [ [{"generated_text": ANY(_A )}, {"generated_text": ANY(_A )}], [{"generated_text": ANY(_A )}, {"generated_text": ANY(_A )}], ] , ) if text_generator.tokenizer.pad_token is not None: a_ : Optional[Any] = text_generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=_A ) self.assertEqual( _A , [ [{"generated_text": ANY(_A )}, {"generated_text": ANY(_A )}], [{"generated_text": ANY(_A )}, {"generated_text": ANY(_A )}], ] , ) with self.assertRaises(_A ): a_ : List[str] = text_generator("test" , return_full_text=_A , return_text=_A ) with self.assertRaises(_A ): a_ : Optional[int] = text_generator("test" , return_full_text=_A , return_tensors=_A ) with self.assertRaises(_A ): a_ : str = text_generator("test" , return_text=_A , return_tensors=_A ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): a_ : str = text_generator("" ) self.assertEqual(_A , [{"generated_text": ANY(_A )}] ) else: with self.assertRaises((ValueError, AssertionError) ): a_ : Any = text_generator("" ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. a_ : List[str] = ["RwkvForCausalLM", "XGLMForCausalLM", "GPTNeoXForCausalLM"] if ( tokenizer.model_max_length < 1_0_0_0_0 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator("This is a test" * 5_0_0 , max_new_tokens=2_0 ) a_ : Tuple = text_generator("This is a test" * 5_0_0 , handle_long_generation="hole" , max_new_tokens=2_0 ) # Hole strategy cannot work with self.assertRaises(_A ): text_generator( "This is a test" * 5_0_0 , handle_long_generation="hole" , max_new_tokens=tokenizer.model_max_length + 1_0 , ) @require_torch @require_accelerate @require_torch_gpu def A ( self ) -> int: import torch # Classic `model_kwargs` a_ : str = pipeline( model="hf-internal-testing/tiny-random-bloom" , model_kwargs={"device_map": "auto", "torch_dtype": torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) a_ : List[Any] = pipe("This is a test" ) self.assertEqual( _A , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) a_ : List[Any] = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) a_ : Optional[int] = pipe("This is a test" ) self.assertEqual( _A , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 a_ : int = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) a_ : Union[str, Any] = pipe("This is a test" ) self.assertEqual( _A , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) @require_torch @require_torch_gpu def A ( self ) -> Optional[int]: import torch a_ : List[str] = pipeline(model="hf-internal-testing/tiny-random-bloom" , device=0 , torch_dtype=torch.floataa ) pipe("This is a test" ) @require_torch @require_accelerate @require_torch_gpu def A ( self ) -> str: import torch a_ : Any = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.floataa ) pipe("This is a test" , do_sample=_A , top_p=0.5 ) def A ( self ) -> Dict: a_ : Optional[int] = "Hello world" a_ : Dict = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) if text_generator.model.framework == "tf": a_ : Any = logging.get_logger("transformers.generation.tf_utils" ) else: a_ : Optional[int] = logging.get_logger("transformers.generation.utils" ) a_ : Tuple = "Both `max_new_tokens`" # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(_A ) as cl: a_ : str = text_generator(_A , max_length=1_0 , max_new_tokens=1 ) self.assertIn(_A , cl.out ) # The user only sets one -> no warning with CaptureLogger(_A ) as cl: a_ : Optional[Any] = text_generator(_A , max_new_tokens=1 ) self.assertNotIn(_A , cl.out ) with CaptureLogger(_A ) as cl: a_ : List[Any] = text_generator(_A , max_length=1_0 ) self.assertNotIn(_A , cl.out )
473
import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _lowerCAmelCase = get_tests_dir("fixtures/spiece.model") @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( __lowercase, unittest.TestCase ): UpperCAmelCase = DebertaVaTokenizer UpperCAmelCase = DebertaVaTokenizerFast UpperCAmelCase = True UpperCAmelCase = True def UpperCamelCase_ ( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing _UpperCamelCase = DebertaVaTokenizer(_A , unk_token='''<unk>''' ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : Dict , _A : Union[str, Any] ): _UpperCamelCase = '''this is a test''' _UpperCamelCase = '''this is a test''' return input_text, output_text def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = '''<pad>''' _UpperCamelCase = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A ) , _A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A ) , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<pad>''' ) self.assertEqual(vocab_keys[1] , '''<unk>''' ) self.assertEqual(vocab_keys[-1] , '''[PAD]''' ) self.assertEqual(len(_A ) , 3_0001 ) def UpperCamelCase_ ( self : List[Any] ): self.assertEqual(self.get_tokenizer().vocab_size , 3_0000 ) def UpperCamelCase_ ( self : List[str] ): # fmt: off _UpperCamelCase = ''' \tHeLLo!how \n Are yoU? ''' _UpperCamelCase = ['''▁hello''', '''!''', '''how''', '''▁are''', '''▁you''', '''?'''] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) @unittest.skip('''There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.''' ) def UpperCamelCase_ ( self : Dict ): pass @unittest.skip('''There is an inconsistency between slow and fast tokenizer due to a bug in the fast one.''' ) def UpperCamelCase_ ( self : Optional[Any] ): pass def UpperCamelCase_ ( self : Dict ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁''', '''<unk>''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', '''▁''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''▁''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : List[Any] ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁i''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', '''▁''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''▁''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Dict ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁i''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : int ): # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = ['''▁''', '''<unk>''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', '''▁''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''▁''', '''.''', ] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Tuple ): # fmt: off _UpperCamelCase = ''' \tHeLLo!how \n Are yoU? ''' _UpperCamelCase = ['''▁''', '''<unk>''', '''e''', '''<unk>''', '''o''', '''!''', '''how''', '''▁''', '''<unk>''', '''re''', '''▁yo''', '''<unk>''', '''?'''] # fmt: on _UpperCamelCase = DebertaVaTokenizer(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , do_lower_case=_A , split_by_punct=_A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = self.get_tokenizer() _UpperCamelCase = self.get_rust_tokenizer() _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = tokenizer.convert_ids_to_tokens(tokenizer.encode(_A , add_special_tokens=_A ) ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(_A , add_special_tokens=_A ) ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.encode(_A , add_special_tokens=_A ) _UpperCamelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = self.get_rust_tokenizer() _UpperCamelCase = tokenizer.encode(_A ) _UpperCamelCase = rust_tokenizer.encode(_A ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = '''This is a test''' _UpperCamelCase = [13, 1, 4398, 25, 21, 1289] _UpperCamelCase = ['''▁''', '''T''', '''his''', '''▁is''', '''▁a''', '''▁test'''] _UpperCamelCase = ['''▁''', '''<unk>''', '''his''', '''▁is''', '''▁a''', '''▁test'''] _UpperCamelCase = DebertaVaTokenizer(_A , keep_accents=_A ) _UpperCamelCase = DebertaVaTokenizerFast(_A , keep_accents=_A ) _UpperCamelCase = tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) # fmt: off _UpperCamelCase = '''I was born in 92000, and this is falsé.''' _UpperCamelCase = [13, 1, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9] _UpperCamelCase = ['''▁''', '''I''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''é''', '''.''', ] _UpperCamelCase = ['''▁''', '''<unk>''', '''▁was''', '''▁born''', '''▁in''', '''▁9''', '''2000''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁fal''', '''s''', '''<unk>''', '''.''', ] # fmt: on _UpperCamelCase = tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) _UpperCamelCase = rust_tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual(_A , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = DebertaVaTokenizer(_A ) _UpperCamelCase = tokenizer.encode('''sequence builders''' ) _UpperCamelCase = tokenizer.encode('''multi-sequence build''' ) _UpperCamelCase = tokenizer.build_inputs_with_special_tokens(_A ) _UpperCamelCase = tokenizer.build_inputs_with_special_tokens(_A , _A ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] , _A ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] , _A , ) @slow def UpperCamelCase_ ( self : Optional[Any] ): # fmt: off _UpperCamelCase = {'''input_ids''': [[1, 3_9867, 36, 1_9390, 486, 27, 3_5052, 8_1436, 18, 6_0685, 1225, 7, 3_5052, 8_1436, 18, 9367, 1_6899, 18, 1_5937, 53, 594, 773, 18, 1_6287, 3_0465, 36, 1_5937, 6, 4_1139, 38, 3_6979, 6_0763, 191, 6, 3_4132, 99, 6, 5_0538, 390, 4_3230, 6, 3_4132, 2779, 2_0850, 14, 699, 1072, 1194, 36, 382, 1_0901, 53, 7, 699, 1072, 2084, 36, 2_0422, 630, 53, 19, 105, 3049, 1896, 1053, 1_6899, 1506, 11, 3_7978, 4243, 7, 1237, 3_1869, 200, 1_6566, 654, 6, 3_5052, 8_1436, 7, 5_5630, 1_3593, 4, 2], [1, 26, 1_5011, 13, 667, 8, 1053, 18, 2_3611, 1237, 7_2356, 1_2820, 34, 10_4134, 1209, 35, 1_3313, 6627, 21, 202, 347, 7, 164, 2399, 11, 46, 4485, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 1232, 2864, 1_5785, 1_4951, 105, 5, 8581, 1250, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''token_type_ids''': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_A , model_name='''microsoft/deberta-v2-xlarge''' , revision='''ad6e42c1532ddf3a15c39246b63f5559d558b670''' , )
10
0
from __future__ import annotations import math _UpperCAmelCase = """2020.9.26""" _UpperCAmelCase = """xcodz-dot, cclaus, dhruvmanila""" def UpperCamelCase ( __lowercase : Union[str, Any] ,__lowercase : Dict ,__lowercase : Union[str, Any] ,__lowercase : Union[str, Any] ,__lowercase : str ): '''simple docstring''' if not all(isinstance(__snake_case ,(float, int) ) for val in locals().values() ): A_ : Optional[int] = f'''Input values must either be float or int: {list(locals().values() )}''' raise TypeError(__snake_case ) A_ : Optional[int] = ((x * distance) / (z + distance)) * scale A_ : List[str] = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def UpperCamelCase ( __lowercase : Any ,__lowercase : str ,__lowercase : Optional[Any] ,__lowercase : Optional[int] ,__lowercase : List[Any] ): '''simple docstring''' if not isinstance(__snake_case ,__snake_case ): raise TypeError('Axis must be a str' ) A_ : List[str] = locals() del input_variables["axis"] if not all(isinstance(__snake_case ,(float, int) ) for val in input_variables.values() ): A_ : List[str] = ( 'Input values except axis must either be float or int: ' f'''{list(input_variables.values() )}''' ) raise TypeError(__snake_case ) A_ : Dict = (angle % 3_60) / 4_50 * 1_80 / math.pi if axis == "z": A_ : Optional[Any] = x * math.cos(__snake_case ) - y * math.sin(__snake_case ) A_ : Optional[int] = y * math.cos(__snake_case ) + x * math.sin(__snake_case ) A_ : List[str] = z elif axis == "x": A_ : Optional[int] = y * math.cos(__snake_case ) - z * math.sin(__snake_case ) A_ : Optional[int] = z * math.cos(__snake_case ) + y * math.sin(__snake_case ) A_ : int = x elif axis == "y": A_ : Optional[int] = x * math.cos(__snake_case ) - z * math.sin(__snake_case ) A_ : Optional[Any] = z * math.cos(__snake_case ) + x * math.sin(__snake_case ) A_ : List[str] = y else: raise ValueError('not a valid axis, choose one of \'x\', \'y\', \'z\'' ) return new_x, new_y, new_z if __name__ == "__main__": import doctest doctest.testmod() print(F"""{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }""") print(F"""{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }""")
558
import sys from collections import defaultdict class lowerCAmelCase_ : def __init__( self : Optional[int] ): _UpperCamelCase = [] def UpperCamelCase_ ( self : Any , _A : str ): return self.node_position[vertex] def UpperCamelCase_ ( self : Optional[Any] , _A : List[str] , _A : Union[str, Any] ): _UpperCamelCase = pos def UpperCamelCase_ ( self : Any , _A : List[str] , _A : int , _A : Optional[Any] , _A : Union[str, Any] ): if start > size // 2 - 1: return else: if 2 * start + 2 >= size: _UpperCamelCase = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: _UpperCamelCase = 2 * start + 1 else: _UpperCamelCase = 2 * start + 2 if heap[smallest_child] < heap[start]: _UpperCamelCase , _UpperCamelCase = heap[smallest_child], positions[smallest_child] _UpperCamelCase , _UpperCamelCase = ( heap[start], positions[start], ) _UpperCamelCase , _UpperCamelCase = temp, tempa _UpperCamelCase = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , _A ) self.top_to_bottom(_A , _A , _A , _A ) def UpperCamelCase_ ( self : List[str] , _A : Tuple , _A : Optional[Any] , _A : int , _A : Optional[int] ): _UpperCamelCase = position[index] while index != 0: _UpperCamelCase = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: _UpperCamelCase = heap[parent] _UpperCamelCase = position[parent] self.set_position(position[parent] , _A ) else: _UpperCamelCase = val _UpperCamelCase = temp self.set_position(_A , _A ) break _UpperCamelCase = parent else: _UpperCamelCase = val _UpperCamelCase = temp self.set_position(_A , 0 ) def UpperCamelCase_ ( self : int , _A : Tuple , _A : int ): _UpperCamelCase = len(_A ) // 2 - 1 for i in range(_A , -1 , -1 ): self.top_to_bottom(_A , _A , len(_A ) , _A ) def UpperCamelCase_ ( self : Any , _A : int , _A : List[str] ): _UpperCamelCase = positions[0] _UpperCamelCase = sys.maxsize self.top_to_bottom(_A , 0 , len(_A ) , _A ) return temp def _snake_case ( __snake_case ): _UpperCamelCase = Heap() _UpperCamelCase = [0] * len(__snake_case ) _UpperCamelCase = [-1] * len(__snake_case ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph _UpperCamelCase = [] # Heap of Distance of vertices from their neighboring vertex _UpperCamelCase = [] for vertex in range(len(__snake_case ) ): distance_tv.append(sys.maxsize ) positions.append(__snake_case ) heap.node_position.append(__snake_case ) _UpperCamelCase = [] _UpperCamelCase = 1 _UpperCamelCase = sys.maxsize for neighbor, distance in adjacency_list[0]: _UpperCamelCase = 0 _UpperCamelCase = distance heap.heapify(__snake_case , __snake_case ) for _ in range(1 , len(__snake_case ) ): _UpperCamelCase = heap.delete_minimum(__snake_case , __snake_case ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) _UpperCamelCase = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(__snake_case )] ): _UpperCamelCase = distance heap.bottom_to_top( __snake_case , heap.get_position(__snake_case ) , __snake_case , __snake_case ) _UpperCamelCase = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > _lowerCAmelCase = int(input("Enter number of edges: ").strip()) _lowerCAmelCase = defaultdict(list) for _ in range(edges_number): _lowerCAmelCase = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
10
0
"""simple docstring""" from typing import List, Optional, Union import numpy as np import tensorflow as tf from .utils import logging a_ = logging.get_logger(__name__) def __UpperCAmelCase ( __UpperCamelCase ): if isinstance(__snake_case , np.ndarray ): return list(tensor.shape ) __lowercase : Optional[Any] = tf.shape(__snake_case ) if tensor.shape == tf.TensorShape(__snake_case ): return dynamic __lowercase : Dict = tensor.shape.as_list() return [dynamic[i] if s is None else s for i, s in enumerate(__snake_case )] def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None ): return tf.nn.softmax(logits=logits + 1e-9 , axis=__snake_case , name=__snake_case ) def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=1e-5 , __UpperCamelCase=-1 ): # This is a very simplified functional layernorm, designed to duplicate # the functionality of PyTorch nn.functional.layer_norm when this is needed to port # models in Transformers. if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(__snake_case , __snake_case ): raise NotImplementedError('''Only 1D weight and bias tensors are supported for now, with only a single axis.''' ) # Get mean and variance on the axis to be normalized __lowercase ,__lowercase : Optional[int] = tf.nn.moments(__snake_case , axes=[axis] , keepdims=__snake_case ) if axis != -1: # Reshape scale and weight to have the same rank as inputs, but with 1 dimensions # on every dimension except axis __lowercase : Any = [1] * inputs.shape.rank __lowercase : List[str] = shape_list(__snake_case )[axis] __lowercase : Union[str, Any] = tf.reshape(__snake_case , __snake_case ) __lowercase : Optional[int] = tf.reshape(__snake_case , __snake_case ) # Compute layer normalization using the batch_normalization # function. __lowercase : List[Any] = tf.nn.batch_normalization( __snake_case , __snake_case , __snake_case , offset=__snake_case , scale=__snake_case , variance_epsilon=__snake_case , ) return outputs def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase=0 , __UpperCamelCase=-1 ): # Replicates the behavior of torch.flatten in TF # If end_dim or start_dim is negative, count them from the end if end_dim < 0: end_dim += input.shape.rank if start_dim < 0: start_dim += input.shape.rank if start_dim == end_dim: return input __lowercase : str = tf.shape(__snake_case ) __lowercase : Any = tf.math.reduce_prod(in_shape[start_dim : end_dim + 1] ) __lowercase : Optional[int] = tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]] , axis=0 ) return tf.reshape(__snake_case , __snake_case ) def __UpperCAmelCase ( __UpperCamelCase ): if not isinstance(__snake_case , tf.Tensor ): __lowercase : Optional[int] = tf.convert_to_tensor(__snake_case ) # Catches stray NumPy inputs if encoder_attention_mask.shape.rank == 3: __lowercase : Dict = encoder_attention_mask[:, None, :, :] if encoder_attention_mask.shape.rank == 2: __lowercase : Optional[int] = encoder_attention_mask[:, None, None, :] # T5 has a mask that can compare sequence ids, we can simulate this here with this transposition # Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow # /transformer/transformer_layers.py#L270 # encoder_extended_attention_mask = (encoder_extended_attention_mask == # encoder_extended_attention_mask.transpose(-1, -2)) __lowercase : str = ( tf.cast(1 , encoder_attention_mask.dtype ) - encoder_extended_attention_mask ) * encoder_extended_attention_mask.dtype.min return encoder_extended_attention_mask def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = "input_ids" ): tf.debugging.assert_less( __snake_case , tf.cast(__snake_case , dtype=tensor.dtype ) , message=( f"""The maximum value of {tensor_name} ({tf.math.reduce_max(__snake_case )}) must be smaller than the embedding """ f"""layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time.""" ) , ) def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): __lowercase : List[Any] = 6_45_12 # Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT` # because in that case even chunking the array would not make the saving # possible. __lowercase : Union[str, Any] = [x for x in data if len(__snake_case ) > HDF5_OBJECT_HEADER_LIMIT] # Expecting this to never be true. if bad_attributes: raise RuntimeError( '''The following attributes cannot be saved to HDF5 file because ''' f"""they are larger than {HDF5_OBJECT_HEADER_LIMIT} """ f"""bytes: {bad_attributes}""" ) __lowercase : Dict = np.asarray(__snake_case ) __lowercase : Optional[int] = 1 __lowercase : int = np.array_split(__snake_case , __snake_case ) # This will never loop forever thanks to the test above. while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data ): num_chunks += 1 __lowercase : Optional[int] = np.array_split(__snake_case , __snake_case ) if num_chunks > 1: for chunk_id, chunk_data in enumerate(__snake_case ): __lowercase : Tuple = chunk_data else: __lowercase : Optional[Any] = data def __UpperCAmelCase ( __UpperCamelCase , __UpperCamelCase ): if name in group.attrs: __lowercase : Any = [n.decode('''utf8''' ) if hasattr(__snake_case , '''decode''' ) else n for n in group.attrs[name]] else: __lowercase : Union[str, Any] = [] __lowercase : Dict = 0 while "%s%d" % (name, chunk_id) in group.attrs: data.extend( [n.decode('''utf8''' ) if hasattr(__snake_case , '''decode''' ) else n for n in group.attrs['''%s%d''' % (name, chunk_id)]] ) chunk_id += 1 return data def __UpperCAmelCase ( __UpperCamelCase ): def _expand_single_ad_tensor(__UpperCamelCase ): if isinstance(__snake_case , tf.Tensor ) and t.shape.rank == 1: return tf.expand_dims(__snake_case , axis=-1 ) return t return tf.nest.map_structure(_expand_single_ad_tensor , __snake_case )
76
import logging import os from .state import PartialState class lowerCAmelCase_ ( logging.LoggerAdapter ): @staticmethod def UpperCamelCase_ ( _A : Any ): _UpperCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def UpperCamelCase_ ( self : Union[str, Any] , _A : Optional[Any] , _A : str , *_A : int , **_A : List[Any] ): if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) _UpperCamelCase = kwargs.pop('''main_process_only''' , _A ) _UpperCamelCase = kwargs.pop('''in_order''' , _A ) if self.isEnabledFor(_A ): if self._should_log(_A ): _UpperCamelCase , _UpperCamelCase = self.process(_A , _A ) self.logger.log(_A , _A , *_A , **_A ) elif in_order: _UpperCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: _UpperCamelCase , _UpperCamelCase = self.process(_A , _A ) self.logger.log(_A , _A , *_A , **_A ) state.wait_for_everyone() def _snake_case ( __snake_case , __snake_case = None ): if log_level is None: _UpperCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , __snake_case ) _UpperCamelCase = logging.getLogger(__snake_case ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(__snake_case , {} )
10
0
'''simple docstring''' import torch from diffusers import DDIMParallelScheduler from .test_schedulers import SchedulerCommonTest class a_ ( __lowercase ): lowercase_ : int = (DDIMParallelScheduler,) lowercase_ : Union[str, Any] = (('''eta''', 0.0), ('''num_inference_steps''', 50)) def lowercase__ ( self : Dict , **__lowerCAmelCase : Any ): __snake_case = { 'num_train_timesteps': 1_0_0_0, 'beta_start': 0.0001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'clip_sample': True, } config.update(**_A ) return config def lowercase__ ( self : str , **__lowerCAmelCase : int ): __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config(**_A ) __snake_case = scheduler_class(**_A ) __snake_case , __snake_case = 1_0, 0.0 __snake_case = self.dummy_model() __snake_case = self.dummy_sample_deter scheduler.set_timesteps(_A ) for t in scheduler.timesteps: __snake_case = model(_A , _A ) __snake_case = scheduler.step(_A , _A , _A , _A ).prev_sample return sample def lowercase__ ( self : List[str] ): for timesteps in [1_0_0, 5_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=_A ) def lowercase__ ( self : Union[str, Any] ): for steps_offset in [0, 1]: self.check_over_configs(steps_offset=_A ) __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config(steps_offset=1 ) __snake_case = scheduler_class(**_A ) scheduler.set_timesteps(5 ) assert torch.equal(scheduler.timesteps , torch.LongTensor([8_0_1, 6_0_1, 4_0_1, 2_0_1, 1] ) ) def lowercase__ ( self : Union[str, Any] ): for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=_A , beta_end=_A ) def lowercase__ ( self : Optional[int] ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_A ) def lowercase__ ( self : Tuple ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_A ) def lowercase__ ( self : Union[str, Any] ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_A ) def lowercase__ ( self : Tuple ): for timestep_spacing in ["trailing", "leading"]: self.check_over_configs(timestep_spacing=_A ) def lowercase__ ( self : List[Any] ): for rescale_betas_zero_snr in [True, False]: self.check_over_configs(rescale_betas_zero_snr=_A ) def lowercase__ ( self : Union[str, Any] ): self.check_over_configs(thresholding=_A ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs( thresholding=_A , prediction_type=_A , sample_max_value=_A , ) def lowercase__ ( self : str ): for t in [1, 1_0, 4_9]: self.check_over_forward(time_step=_A ) def lowercase__ ( self : int ): for t, num_inference_steps in zip([1, 1_0, 5_0] , [1_0, 5_0, 5_0_0] ): self.check_over_forward(time_step=_A , num_inference_steps=_A ) def lowercase__ ( self : Dict ): for t, eta in zip([1, 1_0, 4_9] , [0.0, 0.5, 1.0] ): self.check_over_forward(time_step=_A , eta=_A ) def lowercase__ ( self : List[Any] ): __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config() __snake_case = scheduler_class(**_A ) assert torch.sum(torch.abs(scheduler._get_variance(0 , 0 ) - 0.0 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(4_2_0 , 4_0_0 ) - 0.14771 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(9_8_0 , 9_6_0 ) - 0.32460 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(0 , 0 ) - 0.0 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(4_8_7 , 4_8_6 ) - 0.00979 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(9_9_9 , 9_9_8 ) - 0.02 ) ) < 1E-5 def lowercase__ ( self : int ): __snake_case = self.scheduler_classes[0] __snake_case = self.get_scheduler_config() __snake_case = scheduler_class(**_A ) __snake_case , __snake_case = 1_0, 0.0 scheduler.set_timesteps(_A ) __snake_case = self.dummy_model() __snake_case = self.dummy_sample_deter __snake_case = self.dummy_sample_deter + 0.1 __snake_case = self.dummy_sample_deter - 0.1 __snake_case = samplea.shape[0] __snake_case = torch.stack([samplea, samplea, samplea] , dim=0 ) __snake_case = torch.arange(_A )[0:3, None].repeat(1 , _A ) __snake_case = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) __snake_case = scheduler.batch_step_no_noise(_A , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) , _A ) __snake_case = torch.sum(torch.abs(_A ) ) __snake_case = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 1147.7904 ) < 1E-2 assert abs(result_mean.item() - 0.4982 ) < 1E-3 def lowercase__ ( self : int ): __snake_case = self.full_loop() __snake_case = torch.sum(torch.abs(_A ) ) __snake_case = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 172.0067 ) < 1E-2 assert abs(result_mean.item() - 0.223967 ) < 1E-3 def lowercase__ ( self : str ): __snake_case = self.full_loop(prediction_type='v_prediction' ) __snake_case = torch.sum(torch.abs(_A ) ) __snake_case = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 52.5302 ) < 1E-2 assert abs(result_mean.item() - 0.0684 ) < 1E-3 def lowercase__ ( self : Any ): # We specify different beta, so that the first alpha is 0.99 __snake_case = self.full_loop(set_alpha_to_one=_A , beta_start=0.01 ) __snake_case = torch.sum(torch.abs(_A ) ) __snake_case = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 149.8295 ) < 1E-2 assert abs(result_mean.item() - 0.1951 ) < 1E-3 def lowercase__ ( self : List[Any] ): # We specify different beta, so that the first alpha is 0.99 __snake_case = self.full_loop(set_alpha_to_one=_A , beta_start=0.01 ) __snake_case = torch.sum(torch.abs(_A ) ) __snake_case = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 149.0784 ) < 1E-2 assert abs(result_mean.item() - 0.1941 ) < 1E-3
356
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin _lowerCAmelCase = "▁" _lowerCAmelCase = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class lowerCAmelCase_ ( __lowercase, unittest.TestCase ): UpperCAmelCase = BertGenerationTokenizer UpperCAmelCase = False UpperCAmelCase = True def UpperCamelCase_ ( self : List[str] ): super().setUp() _UpperCamelCase = BertGenerationTokenizer(_A , keep_accents=_A ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = '''<s>''' _UpperCamelCase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A ) , _A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A ) , _A ) def UpperCamelCase_ ( self : Any ): _UpperCamelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''<pad>''' ) self.assertEqual(len(_A ) , 1002 ) def UpperCamelCase_ ( self : Dict ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def UpperCamelCase_ ( self : int ): _UpperCamelCase = BertGenerationTokenizer(_A , keep_accents=_A ) _UpperCamelCase = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_A , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_A ) , [285, 46, 10, 170, 382] , ) _UpperCamelCase = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _A , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _UpperCamelCase = tokenizer.convert_tokens_to_ids(_A ) self.assertListEqual( _A , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) _UpperCamelCase = tokenizer.convert_ids_to_tokens(_A ) self.assertListEqual( _A , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) @cached_property def UpperCamelCase_ ( self : Union[str, Any] ): return BertGenerationTokenizer.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' ) @slow def UpperCamelCase_ ( self : Optional[Any] ): _UpperCamelCase = '''Hello World!''' _UpperCamelCase = [1_8536, 2260, 101] self.assertListEqual(_A , self.big_tokenizer.encode(_A ) ) @slow def UpperCamelCase_ ( self : int ): _UpperCamelCase = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) _UpperCamelCase = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 3_4324, 497, 391, 408, 1_1342, 1244, 385, 100, 938, 985, 456, 574, 362, 1_2597, 3200, 3129, 1172, ] self.assertListEqual(_A , self.big_tokenizer.encode(_A ) ) @require_torch @slow def UpperCamelCase_ ( self : Dict ): import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence _UpperCamelCase = list(self.big_tokenizer.get_vocab().keys() )[:10] _UpperCamelCase = ''' '''.join(_A ) _UpperCamelCase = self.big_tokenizer.encode_plus(_A , return_tensors='''pt''' , return_token_type_ids=_A ) _UpperCamelCase = self.big_tokenizer.batch_encode_plus( [sequence + ''' ''' + sequence] , return_tensors='''pt''' , return_token_type_ids=_A ) _UpperCamelCase = BertGenerationConfig() _UpperCamelCase = BertGenerationEncoder(_A ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**_A ) model(**_A ) @slow def UpperCamelCase_ ( self : Dict ): # fmt: off _UpperCamelCase = {'''input_ids''': [[3_9286, 458, 3_6335, 2001, 456, 1_3073, 1_3266, 455, 113, 7746, 1741, 1_1157, 391, 1_3073, 1_3266, 455, 113, 3967, 3_5412, 113, 4936, 109, 3870, 2377, 113, 3_0084, 4_5720, 458, 134, 1_7496, 112, 503, 1_1672, 113, 118, 112, 5665, 1_3347, 3_8687, 112, 1496, 3_1389, 112, 3268, 4_7264, 134, 962, 112, 1_6377, 8035, 2_3130, 430, 1_2169, 1_5518, 2_8592, 458, 146, 4_1697, 109, 391, 1_2169, 1_5518, 1_6689, 458, 146, 4_1358, 109, 452, 726, 4034, 111, 763, 3_5412, 5082, 388, 1903, 111, 9051, 391, 2870, 4_8918, 1900, 1123, 550, 998, 112, 9586, 1_5985, 455, 391, 410, 2_2955, 3_7636, 114], [448, 1_7496, 419, 3663, 385, 763, 113, 2_7533, 2870, 3283, 1_3043, 1639, 2_4713, 523, 656, 2_4013, 1_8550, 2521, 517, 2_7014, 2_1244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 1_1786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 2_1932, 1_8146, 726, 363, 1_7032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_A , model_name='''google/bert_for_seq_generation_L-24_bbc_encoder''' , revision='''c817d1fd1be2ffa69431227a1fe320544943d4db''' , )
10
0
'''simple docstring''' def snake_case_ ( __snake_case : Dict , __snake_case : Optional[int]) -> Tuple: assert x is not None assert y is not None lowerCAmelCase_ = len(__snake_case) lowerCAmelCase_ = len(__snake_case) # declaring the array for storing the dp values lowerCAmelCase_ = [[0] * (n + 1) for _ in range(m + 1)] # noqa: E741 for i in range(1 , m + 1): for j in range(1 , n + 1): lowerCAmelCase_ = 1 if x[i - 1] == y[j - 1] else 0 lowerCAmelCase_ = max(l[i - 1][j] , l[i][j - 1] , l[i - 1][j - 1] + match) lowerCAmelCase_ = '''''' lowerCAmelCase_ ,lowerCAmelCase_ = m, n while i > 0 and j > 0: lowerCAmelCase_ = 1 if x[i - 1] == y[j - 1] else 0 if l[i][j] == l[i - 1][j - 1] + match: if match == 1: lowerCAmelCase_ = x[i - 1] + seq i -= 1 j -= 1 elif l[i][j] == l[i - 1][j]: i -= 1 else: j -= 1 return l[m][n], seq if __name__ == "__main__": A_ : int ='''AGGTAB''' A_ : Optional[int] ='''GXTXAYB''' A_ : int =4 A_ : Union[str, Any] ='''GTAB''' A_ , A_ : Optional[Any] =longest_common_subsequence(a, b) print('''len =''', ln, ''', sub-sequence =''', subseq) import doctest doctest.testmod()
274
import gc import unittest import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DDPMScheduler, PriorTransformer, StableUnCLIPPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class lowerCAmelCase_ ( __lowercase, __lowercase, __lowercase, unittest.TestCase ): UpperCAmelCase = StableUnCLIPPipeline UpperCAmelCase = TEXT_TO_IMAGE_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_BATCH_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS # TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false UpperCAmelCase = False def UpperCamelCase_ ( self : Optional[int] ): _UpperCamelCase = 32 _UpperCamelCase = embedder_hidden_size # prior components torch.manual_seed(0 ) _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTextModelWithProjection( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=_A , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) _UpperCamelCase = PriorTransformer( num_attention_heads=2 , attention_head_dim=12 , embedding_dim=_A , num_layers=1 , ) torch.manual_seed(0 ) _UpperCamelCase = DDPMScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1000 , clip_sample=_A , clip_sample_range=5.0 , beta_schedule='''squaredcos_cap_v2''' , ) # regular denoising components torch.manual_seed(0 ) _UpperCamelCase = StableUnCLIPImageNormalizer(embedding_dim=_A ) _UpperCamelCase = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) _UpperCamelCase = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type='''projection''' , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=_A , layers_per_block=1 , upcast_attention=_A , use_linear_projection=_A , ) torch.manual_seed(0 ) _UpperCamelCase = DDIMScheduler( beta_schedule='''scaled_linear''' , beta_start=0.0_0085 , beta_end=0.012 , prediction_type='''v_prediction''' , set_alpha_to_one=_A , steps_offset=1 , ) torch.manual_seed(0 ) _UpperCamelCase = AutoencoderKL() _UpperCamelCase = { # prior components '''prior_tokenizer''': prior_tokenizer, '''prior_text_encoder''': prior_text_encoder, '''prior''': prior, '''prior_scheduler''': prior_scheduler, # image noising components '''image_normalizer''': image_normalizer, '''image_noising_scheduler''': image_noising_scheduler, # regular denoising components '''tokenizer''': tokenizer, '''text_encoder''': text_encoder, '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, } return components def UpperCamelCase_ ( self : Dict , _A : Tuple , _A : Dict=0 ): if str(_A ).startswith('''mps''' ): _UpperCamelCase = torch.manual_seed(_A ) else: _UpperCamelCase = torch.Generator(device=_A ).manual_seed(_A ) _UpperCamelCase = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''prior_num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = torch_device == '''cpu''' self._test_attention_slicing_forward_pass(test_max_difference=_A ) def UpperCamelCase_ ( self : List[Any] ): _UpperCamelCase = torch_device in ['''cpu''', '''mps'''] self._test_inference_batch_single_identical(test_max_difference=_A ) @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self : Optional[Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy''' ) _UpperCamelCase = StableUnCLIPPipeline.from_pretrained('''fusing/stable-unclip-2-1-l''' , torch_dtype=torch.floataa ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() _UpperCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) _UpperCamelCase = pipe('''anime turle''' , generator=_A , output_type='''np''' ) _UpperCamelCase = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_A , _A ) def UpperCamelCase_ ( self : Optional[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() _UpperCamelCase = StableUnCLIPPipeline.from_pretrained('''fusing/stable-unclip-2-1-l''' , torch_dtype=torch.floataa ) _UpperCamelCase = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() _UpperCamelCase = pipe( '''anime turtle''' , prior_num_inference_steps=2 , num_inference_steps=2 , output_type='''np''' , ) _UpperCamelCase = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
10
0
'''simple docstring''' import copy import re class __A : '''simple docstring''' __lowerCamelCase : List[Any] = 'hp' __lowerCamelCase : str = {} __lowerCamelCase : Tuple = None @classmethod def a__ (cls , A , A ) -> int: """simple docstring""" _a = prefix _a = defaults cls.build_naming_info() @staticmethod def a__ (A , A ) -> Union[str, Any]: """simple docstring""" if len(A ) == 0: return "" _a = None if any(char.isdigit() for char in word ): raise Exception(f'''Parameters should not contain numbers: \'{word}\' contains a number''' ) if word in info["short_word"]: return info["short_word"][word] for prefix_len in range(1 , len(A ) + 1 ): _a = word[:prefix_len] if prefix in info["reverse_short_word"]: continue else: _a = prefix break if short_word is None: # Paranoid fallback def int_to_alphabetic(A ): _a = '''''' while integer != 0: _a = chr(ord('''A''' ) + integer % 10 ) + s integer //= 10 return s _a = 0 while True: _a = word + '''#''' + int_to_alphabetic(A ) if sword in info["reverse_short_word"]: continue else: _a = sword break _a = short_word _a = word return short_word @staticmethod def a__ (A , A ) -> Optional[int]: """simple docstring""" _a = param_name.split('''_''' ) _a = [TrialShortNamer.shortname_for_word(A , A ) for word in words] # We try to create a separatorless short name, but if there is a collision we have to fallback # to a separated short name _a = ['''''', '''_'''] for separator in separators: _a = separator.join(A ) if shortname not in info["reverse_short_param"]: _a = shortname _a = param_name return shortname return param_name @staticmethod def a__ (A , A ) -> Optional[Any]: """simple docstring""" _a = TrialShortNamer.shortname_for_key(A , A ) _a = short_name _a = param_name @classmethod def a__ (cls ) -> Dict: """simple docstring""" if cls.NAMING_INFO is not None: return _a = { '''short_word''': {}, '''reverse_short_word''': {}, '''short_param''': {}, '''reverse_short_param''': {}, } _a = list(cls.DEFAULTS.keys() ) for k in field_keys: cls.add_new_param_name(A , A ) _a = info @classmethod def a__ (cls , A ) -> Tuple: """simple docstring""" cls.build_naming_info() assert cls.PREFIX is not None _a = [copy.copy(cls.PREFIX )] for k, v in params.items(): if k not in cls.DEFAULTS: raise Exception(f'''You should provide a default value for the param name {k} with value {v}''' ) if v == cls.DEFAULTS[k]: # The default value is not added to the name continue _a = cls.NAMING_INFO['''short_param'''][k] if isinstance(A , A ): _a = 1 if v else 0 _a = '''''' if isinstance(A , (int, float) ) else '''-''' _a = f'''{key}{sep}{v}''' name.append(A ) return "_".join(A ) @classmethod def a__ (cls , A ) -> List[Any]: """simple docstring""" _a = repr[len(cls.PREFIX ) + 1 :] if repr == "": _a = [] else: _a = repr.split('''_''' ) _a = {} for value in values: if "-" in value: _a , _a = value.split('''-''' ) else: _a = re.sub('''[0-9.]''' , '''''' , A ) _a = float(re.sub('''[^0-9.]''' , '''''' , A ) ) _a = cls.NAMING_INFO['''reverse_short_param'''][p_k] _a = p_v for k in cls.DEFAULTS: if k not in parameters: _a = cls.DEFAULTS[k] return parameters
11
'''simple docstring''' import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument lowercase_ = { "/attention/": "/0/SelfAttention/", "/self_attention/": "/0/SelfAttention/", "/encoder_decoder_attention/": "/1/EncDecAttention/", "value": "v", "query": "q", "key": "k", "out": "o", "pre_self_attention_layer_norm": "0/layer_norm", "pre_cross_attention_layer_norm": "1/layer_norm", "pre_attention_layer_norm": "0/layer_norm", # previously 1, but seems wrong "token_embedder": "shared", "encoder_norm": "final_layer_norm", "decoder_norm": "final_layer_norm", "relpos_bias/rel_embedding": "block/0/layer/0/SelfAttention/relative_attention_bias/weight", "router/router_weights/w/": "router/classifier/", "roer/roer_weights/w/": "router/classifier/", "logits_dense": "lm_head", } def lowerCAmelCase (__A): """simple docstring""" _a = list(s_dict.keys()) for key in keys: _a = r'''.*/layers_(\d+)''' _a = key if re.match(__A , __A): _a = re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __A) _a = r'''(encoder|decoder)\/''' if re.match(__A , __A): _a = re.match(__A , __A).groups() if groups[0] == "encoder": _a = re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __A) elif groups[0] == "decoder": _a = re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __A) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: _a = new_key.replace(__A , __A) print(F'''{key} -> {new_key}''') _a = s_dict.pop(__A) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = s_dict[ '''encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = s_dict[ '''decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys()): if "expert" in key: _a = s_dict[key].shape[0] _a = s_dict[key] for idx in range(__A): _a = expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring')}''') s_dict.pop(__A) return s_dict lowercase_ = { "NUM_ENCODER_LAYERS": "num_layers", "NUM_DECODER_LAYERS": "num_decoder_layers", "NUM_HEADS": "num_heads", "HEAD_DIM": "d_kv", "EMBED_DIM": "d_model", "MLP_DIM": "d_ff", "NUM_SELECTED_EXPERTS": "num_selected_experts", "NUM_ENCODER_SPARSE_LAYERS": "num_sparse_encoder_layers", "NUM_DECODER_SPARSE_LAYERS": "num_sparse_decoder_layers", "dense.MlpBlock.activations": "feed_forward_proj", } def lowerCAmelCase (__A , __A): """simple docstring""" import regex as re with open(__A , '''r''') as f: _a = f.read() _a = re.findall(r'''(.*) = ([0-9.]*)''' , __A) _a = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": _a = float(__A) if '''.''' in value else int(__A) _a = re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __A)[0] _a = str(activation[1]) _a = num_experts _a = SwitchTransformersConfig(**__A) return config def lowerCAmelCase (__A , __A , __A=None , __A="./" , __A=8): """simple docstring""" print(F'''Loading flax weights from : {flax_checkpoint_path}''') _a = checkpoints.load_tax_checkpoint(__A) if gin_file is not None: _a = convert_gin_to_config(__A , __A) else: _a = SwitchTransformersConfig.from_pretrained(__A) _a = SwitchTransformersForConditionalGeneration(__A) _a = flax_params['''target'''] _a = flatten_dict(__A , sep='''/''') _a = rename_keys(__A) _a = unflatten_dict(__A , sep='''/''') # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__A , __A) print(F'''Save PyTorch model to {pytorch_dump_path}''') pt_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the" " model architecture. If not provided, a `gin_file` has to be provided." ), ) parser.add_argument( "--gin_file", default=None, type=str, required=False, help="Path to the gin config file. If not provided, a `config_file` has to be passed ", ) parser.add_argument( "--config_name", default=None, type=str, required=False, help="Config name of SwitchTransformers model." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output pytorch model." ) parser.add_argument("--num_experts", default=8, type=int, required=False, help="Number of experts") lowercase_ = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
11
1
'''simple docstring''' 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 lowercase_ = { # 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 __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
1
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor lowercase_ = logging.get_logger(__name__) class __A ( A ): '''simple docstring''' def __init__(self , *A , **A ) -> None: """simple docstring""" warnings.warn( '''The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use DeiTImageProcessor instead.''' , A , ) super().__init__(*A , **A )
11
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
1
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = CTRLTokenizer __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = False def a__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _a = dict(zip(A , range(len(A ) ) ) ) _a = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _a = {'''unk_token''': '''<unk>'''} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(A ) ) def a__ (self , **A ) -> int: """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> Tuple: """simple docstring""" _a = '''adapt react readapt apt''' _a = '''adapt react readapt apt''' return input_text, output_text def a__ (self ) -> List[Any]: """simple docstring""" _a = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = '''adapt react readapt apt''' _a = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _a = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _a = tokens + [tokenizer.unk_token] _a = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A )
11
'''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 __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''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 a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class __A ( metaclass=A ): '''simple docstring''' __lowerCamelCase : Optional[int] = ['torch', 'transformers', 'onnx'] def __init__(self , *A , **A ) -> Any: """simple docstring""" requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> int: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> Any: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class __A ( metaclass=A ): '''simple docstring''' __lowerCamelCase : str = ['torch', 'transformers', 'onnx'] def __init__(self , *A , **A ) -> Any: """simple docstring""" requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> int: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> Any: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class __A ( metaclass=A ): '''simple docstring''' __lowerCamelCase : Optional[Any] = ['torch', 'transformers', 'onnx'] def __init__(self , *A , **A ) -> Union[str, Any]: """simple docstring""" requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> Dict: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> int: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class __A ( metaclass=A ): '''simple docstring''' __lowerCamelCase : str = ['torch', 'transformers', 'onnx'] def __init__(self , *A , **A ) -> str: """simple docstring""" requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> Optional[Any]: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> int: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class __A ( metaclass=A ): '''simple docstring''' __lowerCamelCase : List[Any] = ['torch', 'transformers', 'onnx'] def __init__(self , *A , **A ) -> Union[str, Any]: """simple docstring""" requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> str: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> Any: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) class __A ( metaclass=A ): '''simple docstring''' __lowerCamelCase : Dict = ['torch', 'transformers', 'onnx'] def __init__(self , *A , **A ) -> List[str]: """simple docstring""" requires_backends(self , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> str: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] ) @classmethod def a__ (cls , *A , **A ) -> str: """simple docstring""" requires_backends(cls , ['''torch''', '''transformers''', '''onnx'''] )
11
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
1
'''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 __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=16 , A=36 , A=6 , A=6 , A=6 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = embedding_size _a = hidden_size _a = num_hidden_layers _a = num_hidden_groups _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> Optional[int]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Tuple: """simple docstring""" return AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , num_hidden_groups=self.num_hidden_groups , ) def a__ (self , A , A , A , A , A , A , A ) -> int: """simple docstring""" _a = AlbertModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , token_type_ids=A ) _a = model(A , token_type_ids=A ) _a = model(A ) 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 a__ (self , A , A , A , A , A , A , A ) -> str: """simple docstring""" _a = AlbertForPreTraining(config=A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , token_type_ids=A , labels=A , sentence_order_label=A , ) 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 a__ (self , A , A , A , A , A , A , A ) -> Optional[int]: """simple docstring""" _a = AlbertForMaskedLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , token_type_ids=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A ) -> Dict: """simple docstring""" _a = AlbertForQuestionAnswering(config=A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , token_type_ids=A , start_positions=A , end_positions=A , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def a__ (self , A , A , A , A , A , A , A ) -> Optional[int]: """simple docstring""" _a = self.num_labels _a = AlbertForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , token_type_ids=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def a__ (self , A , A , A , A , A , A , A ) -> str: """simple docstring""" _a = self.num_labels _a = AlbertForTokenClassification(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , token_type_ids=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = self.num_choices _a = AlbertForMultipleChoice(config=A ) model.to(A ) model.eval() _a = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _a = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _a = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _a = model( A , attention_mask=A , token_type_ids=A , labels=A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def a__ (self ) -> str: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Any = ( ( AlbertModel, AlbertForPreTraining, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertForQuestionAnswering, ) if is_torch_available() else () ) __lowerCamelCase : str = ( { 'feature-extraction': AlbertModel, 'fill-mask': AlbertForMaskedLM, 'question-answering': AlbertForQuestionAnswering, 'text-classification': AlbertForSequenceClassification, 'token-classification': AlbertForTokenClassification, 'zero-shot': AlbertForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : str = True def a__ (self , A , A , A=False ) -> Optional[Any]: """simple docstring""" _a = super()._prepare_for_class(A , A , return_labels=A ) if return_labels: if model_class in get_values(A ): _a = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=A ) _a = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=A ) return inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a = AlbertModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Any: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> int: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A ) def a__ (self ) -> Dict: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A ) def a__ (self ) -> List[str]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Any: """simple docstring""" for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = AlbertModel.from_pretrained(A ) self.assertIsNotNone(A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AlbertModel.from_pretrained('''albert-base-v2''' ) _a = torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): _a = model(A , attention_mask=A )[0] _a = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig lowercase_ = logging.get_logger(__name__) # General docstring lowercase_ = "RegNetConfig" # Base docstring lowercase_ = "facebook/regnet-y-040" lowercase_ = [1, 1_088, 7, 7] # Image classification docstring lowercase_ = "facebook/regnet-y-040" lowercase_ = "tabby, tabby cat" lowercase_ = [ "facebook/regnet-y-040", # See all regnet models at https://huggingface.co/models?filter=regnet ] class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , A = 3 , A = 1 , A = 1 , A = "relu" , **A , ) -> List[Any]: """simple docstring""" super().__init__(**A ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb _a = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) _a = tf.keras.layers.ConvaD( filters=A , kernel_size=A , strides=A , padding='''VALID''' , groups=A , use_bias=A , name='''convolution''' , ) _a = tf.keras.layers.BatchNormalization(epsilon=1E-5 , momentum=0.9 , name='''normalization''' ) _a = ACTaFN[activation] if activation is not None else tf.identity def a__ (self , A ) -> List[str]: """simple docstring""" _a = self.convolution(self.padding(A ) ) _a = self.normalization(A ) _a = self.activation(A ) return hidden_state class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , **A ) -> Tuple: """simple docstring""" super().__init__(**A ) _a = config.num_channels _a = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='''embedder''' , ) def a__ (self , A ) -> Any: """simple docstring""" _a = shape_list(A )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.''' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) _a = tf.transpose(A , perm=(0, 2, 3, 1) ) _a = self.embedder(A ) return hidden_state class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , A = 2 , **A ) -> Optional[Any]: """simple docstring""" super().__init__(**A ) _a = tf.keras.layers.ConvaD( filters=A , kernel_size=1 , strides=A , use_bias=A , name='''convolution''' ) _a = tf.keras.layers.BatchNormalization(epsilon=1E-5 , momentum=0.9 , name='''normalization''' ) def a__ (self , A , A = False ) -> tf.Tensor: """simple docstring""" return self.normalization(self.convolution(A ) , training=A ) class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , A , **A ) -> List[str]: """simple docstring""" super().__init__(**A ) _a = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A , name='''pooler''' ) _a = [ tf.keras.layers.ConvaD(filters=A , kernel_size=1 , activation='''relu''' , name='''attention.0''' ), tf.keras.layers.ConvaD(filters=A , kernel_size=1 , activation='''sigmoid''' , name='''attention.2''' ), ] def a__ (self , A ) -> str: """simple docstring""" _a = self.pooler(A ) for layer_module in self.attention: _a = layer_module(A ) _a = hidden_state * pooled return hidden_state class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , A , A , A = 1 , **A ) -> Any: """simple docstring""" super().__init__(**A ) _a = in_channels != out_channels or stride != 1 _a = max(1 , out_channels // config.groups_width ) _a = ( TFRegNetShortCut(A , stride=A , name='''shortcut''' ) if should_apply_shortcut else tf.keras.layers.Activation('''linear''' , name='''shortcut''' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. _a = [ TFRegNetConvLayer(A , kernel_size=1 , activation=config.hidden_act , name='''layer.0''' ), TFRegNetConvLayer( A , stride=A , groups=A , activation=config.hidden_act , name='''layer.1''' ), TFRegNetConvLayer(A , kernel_size=1 , activation=A , name='''layer.2''' ), ] _a = ACTaFN[config.hidden_act] def a__ (self , A ) -> str: """simple docstring""" _a = hidden_state for layer_module in self.layers: _a = layer_module(A ) _a = self.shortcut(A ) hidden_state += residual _a = self.activation(A ) return hidden_state class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , A , A , A = 1 , **A ) -> Optional[int]: """simple docstring""" super().__init__(**A ) _a = in_channels != out_channels or stride != 1 _a = max(1 , out_channels // config.groups_width ) _a = ( TFRegNetShortCut(A , stride=A , name='''shortcut''' ) if should_apply_shortcut else tf.keras.layers.Activation('''linear''' , name='''shortcut''' ) ) _a = [ TFRegNetConvLayer(A , kernel_size=1 , activation=config.hidden_act , name='''layer.0''' ), TFRegNetConvLayer( A , stride=A , groups=A , activation=config.hidden_act , name='''layer.1''' ), TFRegNetSELayer(A , reduced_channels=int(round(in_channels / 4 ) ) , name='''layer.2''' ), TFRegNetConvLayer(A , kernel_size=1 , activation=A , name='''layer.3''' ), ] _a = ACTaFN[config.hidden_act] def a__ (self , A ) -> Any: """simple docstring""" _a = hidden_state for layer_module in self.layers: _a = layer_module(A ) _a = self.shortcut(A ) hidden_state += residual _a = self.activation(A ) return hidden_state class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , A , A , A = 2 , A = 2 , **A ) -> str: """simple docstring""" super().__init__(**A ) _a = TFRegNetXLayer if config.layer_type == '''x''' else TFRegNetYLayer _a = [ # downsampling is done in the first layer with stride of 2 layer(A , A , A , stride=A , name='''layers.0''' ), *[layer(A , A , A , name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def a__ (self , A ) -> str: """simple docstring""" for layer_module in self.layers: _a = layer_module(A ) return hidden_state class __A ( tf.keras.layers.Layer ): '''simple docstring''' def __init__(self , A , **A ) -> Optional[Any]: """simple docstring""" super().__init__(**A ) _a = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='''stages.0''' , ) ) _a = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A , config.depths[1:] ) ): self.stages.append(TFRegNetStage(A , A , A , depth=A , name=f'''stages.{i+1}''' ) ) def a__ (self , A , A = False , A = True ) -> TFBaseModelOutputWithNoAttention: """simple docstring""" _a = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: _a = hidden_states + (hidden_state,) _a = stage_module(A ) if output_hidden_states: _a = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A , hidden_states=A ) @keras_serializable class __A ( tf.keras.layers.Layer ): '''simple docstring''' __lowerCamelCase : Any = RegNetConfig def __init__(self , A , **A ) -> str: """simple docstring""" super().__init__(**A ) _a = config _a = TFRegNetEmbeddings(A , name='''embedder''' ) _a = TFRegNetEncoder(A , name='''encoder''' ) _a = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A , name='''pooler''' ) @unpack_inputs def a__ (self , A , A = None , A = None , A = False , ) -> TFBaseModelOutputWithPoolingAndNoAttention: """simple docstring""" _a = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _a = return_dict if return_dict is not None else self.config.use_return_dict _a = self.embedder(A , training=A ) _a = self.encoder( A , output_hidden_states=A , return_dict=A , training=A ) _a = encoder_outputs[0] _a = self.pooler(A ) # Change to NCHW output format have uniformity in the modules _a = tf.transpose(A , perm=(0, 3, 1, 2) ) _a = tf.transpose(A , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: _a = tuple([tf.transpose(A , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A , pooler_output=A , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class __A ( A ): '''simple docstring''' __lowerCamelCase : Dict = RegNetConfig __lowerCamelCase : List[str] = 'regnet' __lowerCamelCase : Optional[int] = 'pixel_values' @property def a__ (self ) -> List[str]: """simple docstring""" return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} lowercase_ = R"\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n" lowercase_ = R"\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , A , ) class __A ( A ): '''simple docstring''' def __init__(self , A , *A , **A ) -> List[str]: """simple docstring""" super().__init__(A , *A , **A ) _a = TFRegNetMainLayer(A , name='''regnet''' ) @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=A , config_class=_CONFIG_FOR_DOC , modality='''vision''' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def a__ (self , A , A = None , A = None , A=False , ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: """simple docstring""" _a = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _a = return_dict if return_dict is not None else self.config.use_return_dict _a = self.regnet( pixel_values=A , output_hidden_states=A , return_dict=A , training=A , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , A , ) class __A ( A , A ): '''simple docstring''' def __init__(self , A , *A , **A ) -> int: """simple docstring""" super().__init__(A , *A , **A ) _a = config.num_labels _a = TFRegNetMainLayer(A , name='''regnet''' ) # classification head _a = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='''classifier.1''' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=A , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def a__ (self , A = None , A = None , A = None , A = None , A=False , ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: """simple docstring""" _a = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _a = return_dict if return_dict is not None else self.config.use_return_dict _a = self.regnet( A , output_hidden_states=A , return_dict=A , training=A ) _a = outputs.pooler_output if return_dict else outputs[1] _a = self.classifier[0](A ) _a = self.classifier[1](A ) _a = None if labels is None else self.hf_compute_loss(labels=A , logits=A ) if not return_dict: _a = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A , logits=A , hidden_states=outputs.hidden_states )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
1
'''simple docstring''' from importlib import import_module from .logging import get_logger lowercase_ = get_logger(__name__) class __A : '''simple docstring''' def __init__(self , A , A=None ) -> List[str]: """simple docstring""" _a = attrs or [] if module is not None: for key in module.__dict__: if key in attrs or not key.startswith('''__''' ): setattr(self , A , getattr(A , A ) ) _a = module._original_module if isinstance(A , _PatchedModuleObj ) else module class __A : '''simple docstring''' __lowerCamelCase : Tuple = [] def __init__(self , A , A , A , A=None ) -> Optional[int]: """simple docstring""" _a = obj _a = target _a = new _a = target.split('''.''' )[0] _a = {} _a = attrs or [] def __enter__(self ) -> Any: """simple docstring""" *_a , _a = self.target.split('''.''' ) # Patch modules: # it's used to patch attributes of submodules like "os.path.join"; # in this case we need to patch "os" and "os.path" for i in range(len(A ) ): try: _a = import_module('''.'''.join(submodules[: i + 1] ) ) except ModuleNotFoundError: continue # We iterate over all the globals in self.obj in case we find "os" or "os.path" for attr in self.obj.__dir__(): _a = getattr(self.obj , A ) # We don't check for the name of the global, but rather if its value *is* "os" or "os.path". # This allows to patch renamed modules like "from os import path as ospath". if obj_attr is submodule or ( (isinstance(A , _PatchedModuleObj ) and obj_attr._original_module is submodule) ): _a = obj_attr # patch at top level setattr(self.obj , A , _PatchedModuleObj(A , attrs=self.attrs ) ) _a = getattr(self.obj , A ) # construct lower levels patches for key in submodules[i + 1 :]: setattr(A , A , _PatchedModuleObj(getattr(A , A , A ) , attrs=self.attrs ) ) _a = getattr(A , A ) # finally set the target attribute setattr(A , A , self.new ) # Patch attribute itself: # it's used for builtins like "open", # and also to patch "os.path.join" we may also need to patch "join" # itself if it was imported as "from os.path import join". if submodules: # if it's an attribute of a submodule like "os.path.join" try: _a = getattr(import_module('''.'''.join(A ) ) , A ) except (AttributeError, ModuleNotFoundError): return # We iterate over all the globals in self.obj in case we find "os.path.join" for attr in self.obj.__dir__(): # We don't check for the name of the global, but rather if its value *is* "os.path.join". # This allows to patch renamed attributes like "from os.path import join as pjoin". if getattr(self.obj , A ) is attr_value: _a = getattr(self.obj , A ) setattr(self.obj , A , self.new ) elif target_attr in globals()["__builtins__"]: # if it'a s builtin like "open" _a = globals()['''__builtins__'''][target_attr] setattr(self.obj , A , self.new ) else: raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.''' ) def __exit__(self , *A ) -> List[str]: """simple docstring""" for attr in list(self.original ): setattr(self.obj , A , self.original.pop(A ) ) def a__ (self ) -> str: """simple docstring""" self.__enter__() self._active_patches.append(self ) def a__ (self ) -> List[Any]: """simple docstring""" try: self._active_patches.remove(self ) except ValueError: # If the patch hasn't been started this will fail return None return self.__exit__()
11
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation lowercase_ = logging.get_logger(__name__) lowercase_ = { "vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_config_file": "tokenizer_config.json", } lowercase_ = { "vocab_file": {"facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"}, "merges_file": {"facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"}, "tokenizer_config_file": { "facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json" }, } lowercase_ = {"facebook/blenderbot-3B": 128} class __A ( A ): '''simple docstring''' __lowerCamelCase : List[str] = VOCAB_FILES_NAMES __lowerCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[Any] = ['input_ids', 'attention_mask'] __lowerCamelCase : Dict = BlenderbotTokenizer def __init__(self , A=None , A=None , A=None , A="replace" , A="<s>" , A="</s>" , A="</s>" , A="<s>" , A="<unk>" , A="<pad>" , A="<mask>" , A=False , A=True , **A , ) -> Optional[Any]: """simple docstring""" super().__init__( A , A , tokenizer_file=A , errors=A , bos_token=A , eos_token=A , sep_token=A , cls_token=A , unk_token=A , pad_token=A , mask_token=A , add_prefix_space=A , trim_offsets=A , **A , ) _a = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , A ) != add_prefix_space: _a = getattr(A , pre_tok_state.pop('''type''' ) ) _a = add_prefix_space _a = pre_tok_class(**A ) _a = add_prefix_space _a = '''post_processor''' _a = getattr(self.backend_tokenizer , A , A ) if tokenizer_component_instance: _a = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: _a = tuple(state['''sep'''] ) if "cls" in state: _a = tuple(state['''cls'''] ) _a = False if state.get('''add_prefix_space''' , A ) != add_prefix_space: _a = add_prefix_space _a = True if state.get('''trim_offsets''' , A ) != trim_offsets: _a = trim_offsets _a = True if changes_to_apply: _a = getattr(A , state.pop('''type''' ) ) _a = component_class(**A ) setattr(self.backend_tokenizer , A , A ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def a__ (self ) -> str: """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def a__ (self , A ) -> Optional[Any]: """simple docstring""" _a = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else value _a = value def a__ (self , *A , **A ) -> BatchEncoding: """simple docstring""" _a = kwargs.get('''is_split_into_words''' , A ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*A , **A ) def a__ (self , *A , **A ) -> BatchEncoding: """simple docstring""" _a = kwargs.get('''is_split_into_words''' , A ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*A , **A ) def a__ (self , A , A = None ) -> Tuple[str]: """simple docstring""" _a = self._tokenizer.model.save(A , name=A ) return tuple(A ) def a__ (self , A , A = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def a__ (self , A , A = None ) -> Tuple: """simple docstring""" return token_ids_a + [self.eos_token_id] def a__ (self , A ) -> List[int]: """simple docstring""" _a = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(A ) _a = ''' '''.join(A ) _a = self.encode(A ) if len(A ) > self.model_max_length: _a = input_ids[-self.model_max_length :] logger.warning(f'''Trimmed input from conversation as it was longer than {self.model_max_length} tokens.''' ) return input_ids
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from __future__ import annotations import collections import tempfile import unittest import numpy as np from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import is_tf_available, is_vision_available from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_tf_bert import TFBertModelTester from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester from ..deit.test_modeling_tf_deit import TFDeiTModelTester from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester from ..vit.test_modeling_tf_vit import TFViTModelTester if is_tf_available(): from transformers import ( TFBertModel, TFCLIPVisionModel, TFDeiTModel, TFRobertaModel, TFVisionTextDualEncoderModel, TFViTModel, VisionTextDualEncoderConfig, ) if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor def lowerCAmelCase (__A): """simple docstring""" if isinstance(__A , collections.abc.Iterable): return x return (x, x) @require_tf class __A : '''simple docstring''' def a__ (self , A , A ) -> Dict: """simple docstring""" pass def a__ (self ) -> List[str]: """simple docstring""" pass def a__ (self ) -> int: """simple docstring""" pass def a__ (self , A , A , A , A , A=None , **A ) -> Any: """simple docstring""" _a = VisionTextDualEncoderConfig.from_vision_text_configs(A , A ) _a = TFVisionTextDualEncoderModel(A ) _a = model(input_ids=A , pixel_values=A , attention_mask=A ) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], config.projection_dim) ) def a__ (self , A , A , A , A , A=None , **A ) -> List[Any]: """simple docstring""" _a , _a = self.get_vision_text_model(A , A ) _a = TFVisionTextDualEncoderModel(vision_model=A , text_model=A ) _a = model(input_ids=A , pixel_values=A , attention_mask=A ) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], model.config.projection_dim) ) def a__ (self , A , A , A , A , A=None , **A ) -> List[Any]: """simple docstring""" _a , _a = self.get_vision_text_model(A , A ) _a = {'''vision_model''': vision_model, '''text_model''': text_model} _a = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**A ) _a = model(input_ids=A , pixel_values=A , attention_mask=A ) self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], model.config.projection_dim) ) def a__ (self , A , A , A , A , A=None , **A ) -> Optional[Any]: """simple docstring""" _a , _a = self.get_vision_text_model(A , A ) _a = TFVisionTextDualEncoderModel(vision_model=A , text_model=A ) _a = model(input_ids=A , pixel_values=A , attention_mask=A ) _a = output[0].numpy() with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a = TFVisionTextDualEncoderModel.from_pretrained(A ) _a = model(input_ids=A , pixel_values=A , attention_mask=A ) _a = after_output[0].numpy() _a = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(A , 1E-5 ) def a__ (self , A , A , A , A , A=None , **A ) -> Tuple: """simple docstring""" _a , _a = self.get_vision_text_model(A , A ) _a = TFVisionTextDualEncoderModel(vision_model=A , text_model=A ) _a = model( input_ids=A , pixel_values=A , attention_mask=A , output_attentions=A ) _a = output.vision_model_output.attentions self.assertEqual(len(A ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) _a = to_atuple(vision_model.config.image_size ) _a = to_atuple(vision_model.config.patch_size ) _a = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) _a = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) _a = output.text_model_output.attentions self.assertEqual(len(A ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def a__ (self , A , A , A ) -> Union[str, Any]: """simple docstring""" _a = np.abs((a - b) ).max() self.assertLessEqual(A , A , f'''Difference between torch and flax is {diff} (>= {tol}).''' ) def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_model(**A ) def a__ (self ) -> List[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**A ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**A ) def a__ (self ) -> List[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() self.check_save_load(**A ) def a__ (self ) -> Any: """simple docstring""" _a = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**A ) @slow def a__ (self ) -> int: """simple docstring""" _a , _a = self.get_pretrained_model_and_inputs() _a = model_a(**A ) _a = outputs[0].numpy() with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(A ) _a = TFVisionTextDualEncoderModel.from_pretrained(A ) _a = model_a(**A ) _a = after_outputs[0].numpy() _a = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(A , 1E-5 ) @require_tf class __A ( A , unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''hf-internal-testing/tiny-random-vit''' , '''hf-internal-testing/tiny-random-bert''' ) _a = 13 _a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) _a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) _a = random_attention_mask([batch_size, 4] ) _a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def a__ (self , A , A ) -> str: """simple docstring""" _a = TFViTModel(A , name='''vision_model''' ) _a = TFBertModel(A , name='''text_model''' ) return vision_model, text_model def a__ (self ) -> Optional[Any]: """simple docstring""" _a = TFViTModelTester(self ) _a = TFBertModelTester(self ) _a = vit_model_tester.prepare_config_and_inputs() _a = bert_model_tester.prepare_config_and_inputs() _a , _a , _a = vision_config_and_inputs ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class __A ( A , unittest.TestCase ): '''simple docstring''' def a__ (self ) -> int: """simple docstring""" _a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''Rocketknight1/tiny-random-deit-tf''' , '''hf-internal-testing/tiny-random-roberta''' ) _a = 13 _a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) _a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) _a = random_attention_mask([batch_size, 4] ) _a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def a__ (self , A , A , A , A , A=None , **A ) -> List[str]: """simple docstring""" _a , _a = self.get_vision_text_model(A , A ) _a = TFVisionTextDualEncoderModel(vision_model=A , text_model=A ) _a = model( input_ids=A , pixel_values=A , attention_mask=A , output_attentions=A ) _a = output.vision_model_output.attentions self.assertEqual(len(A ) , vision_config.num_hidden_layers ) # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens) _a = to_atuple(vision_model.config.image_size ) _a = to_atuple(vision_model.config.patch_size ) _a = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) _a = num_patches + 2 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) _a = output.text_model_output.attentions self.assertEqual(len(A ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def a__ (self , A , A ) -> Dict: """simple docstring""" _a = TFDeiTModel(A , name='''vision_model''' ) _a = TFRobertaModel(A , name='''text_model''' ) return vision_model, text_model def a__ (self ) -> List[str]: """simple docstring""" _a = TFDeiTModelTester(self ) _a = TFRobertaModelTester(self ) _a = vit_model_tester.prepare_config_and_inputs() _a = bert_model_tester.prepare_config_and_inputs() _a , _a , _a = vision_config_and_inputs ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_tf class __A ( A , unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = TFVisionTextDualEncoderModel.from_vision_text_pretrained( '''Rocketknight1/tiny-random-clip-tf''' , '''hf-internal-testing/tiny-random-bert''' ) _a = 13 _a = floats_tensor( [ batch_size, model.vision_model.config.num_channels, model.vision_model.config.image_size, model.vision_model.config.image_size, ] ) _a = ids_tensor([batch_size, 4] , model.text_model.config.vocab_size ) _a = random_attention_mask([batch_size, 4] ) _a = {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask} return model, inputs def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = TFCLIPVisionModel(A , name='''vision_model''' ) _a = TFBertModel(A , name='''text_model''' ) return vision_model, text_model def a__ (self ) -> List[str]: """simple docstring""" _a = TFCLIPVisionModelTester(self ) _a = TFBertModelTester(self ) _a = clip_model_tester.prepare_config_and_inputs() _a = bert_model_tester.prepare_config_and_inputs() _a , _a = vision_config_and_inputs ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = text_config_and_inputs return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": input_mask, "input_ids": input_ids, "text_token_type_ids": token_type_ids, "text_sequence_labels": sequence_labels, "text_token_labels": token_labels, "text_choice_labels": choice_labels, } @require_vision @require_tf class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> List[Any]: """simple docstring""" _a = TFVisionTextDualEncoderModel.from_pretrained( '''clip-italian/clip-italian''' , logit_scale_init_value=1.0 , from_pt=A ) _a = VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian''' ) _a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) _a = processor( text=['''una foto di un gatto''', '''una foto di un cane'''] , images=A , padding=A , return_tensors='''np''' ) _a = model(**A ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) _a = np.array([[1.2284727, 0.3104122]] ) self.assertTrue(np.allclose(outputs.logits_per_image.numpy() , A , atol=1E-3 ) )
11
'''simple docstring''' 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 lowercase_ = { # 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 __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import heapq as hq import math from collections.abc import Iterator class __A : '''simple docstring''' def __init__(self , A ) -> Union[str, Any]: """simple docstring""" _a = str(id_ ) _a = None _a = None _a = [] _a = {} # {vertex:distance} def __lt__(self , A ) -> Dict: """simple docstring""" return self.key < other.key def __repr__(self ) -> Tuple: """simple docstring""" return self.id def a__ (self , A ) -> int: """simple docstring""" self.neighbors.append(A ) def a__ (self , A , A ) -> int: """simple docstring""" _a = weight def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1]) graph[b - 1].add_neighbor(graph[a - 1]) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __A) graph[b - 1].add_edge(graph[a - 1] , __A) def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for u in graph: _a = math.inf _a = None _a = 0 _a = graph[:] while q: _a = min(__A) q.remove(__A) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _a = u _a = u.edges[v.id] for i in range(1 , len(__A)): a.append((int(graph[i].id) + 1, int(graph[i].pi.id) + 1)) return a def lowerCAmelCase (__A , __A): """simple docstring""" for u in graph: _a = math.inf _a = None _a = 0 _a = list(__A) hq.heapify(__A) while h: _a = hq.heappop(__A) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _a = u _a = u.edges[v.id] hq.heapify(__A) for i in range(1 , len(__A)): yield (int(graph[i].id) + 1, int(graph[i].pi.id) + 1) def lowerCAmelCase (): """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig class __A ( A ): '''simple docstring''' __lowerCamelCase : Any = 'bert-generation' def __init__(self , A=50_358 , A=1_024 , A=24 , A=16 , A=4_096 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=0.02 , A=1E-12 , A=0 , A=2 , A=1 , A="absolute" , A=True , **A , ) -> int: """simple docstring""" super().__init__(pad_token_id=A , bos_token_id=A , eos_token_id=A , **A ) _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = hidden_act _a = intermediate_size _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = initializer_range _a = layer_norm_eps _a = position_embedding_type _a = use_cache
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" for i in range(len(__A) - 1 , 0 , -1): _a = False for j in range(__A , 0 , -1): if unsorted[j] < unsorted[j - 1]: _a , _a = unsorted[j - 1], unsorted[j] _a = True for j in range(__A): if unsorted[j] > unsorted[j + 1]: _a , _a = unsorted[j + 1], unsorted[j] _a = True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = input("Enter numbers separated by a comma:\n").strip() lowercase_ = [int(item) for item in user_input.split(",")] print(F"""{cocktail_shaker_sort(unsorted) = }""")
11
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """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=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> str: """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=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> Optional[int]: """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=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
11
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) class __A ( A ): '''simple docstring''' __lowerCamelCase : Optional[int] = 'timm_backbone' def __init__(self , A=None , A=3 , A=True , A=True , A=None , **A , ) -> Tuple: """simple docstring""" super().__init__(**A ) _a = backbone _a = num_channels _a = features_only _a = use_pretrained_backbone _a = True _a = out_indices if out_indices is not None else (-1,)
11
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _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 = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
1
'''simple docstring''' import requests def lowerCAmelCase (__A , __A): """simple docstring""" _a = {'''Content-Type''': '''application/json'''} _a = requests.post(__A , json={'''text''': message_body} , headers=__A) if response.status_code != 200: _a = ( '''Request to slack returned an error ''' F'''{response.status_code}, the response is:\n{response.text}''' ) raise ValueError(__A) if __name__ == "__main__": # Set the slack url to the one provided by Slack when you create the webhook at # https://my.slack.com/services/new/incoming-webhook/ send_slack_message("<YOUR MESSAGE BODY>", "<SLACK CHANNEL URL>")
11
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # 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) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
1
'''simple docstring''' from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import torch from ...utils import BaseOutput, OptionalDependencyNotAvailable, is_torch_available, is_transformers_available @dataclass class __A ( A ): '''simple docstring''' __lowerCamelCase : Union[List[np.ndarray], torch.FloatTensor] try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_text_to_video_synth import TextToVideoSDPipeline from .pipeline_text_to_video_synth_imgaimg import VideoToVideoSDPipeline # noqa: F401 from .pipeline_text_to_video_zero import TextToVideoZeroPipeline
11
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, 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.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = 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 , is_decoder=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available lowercase_ = {"configuration_speech_encoder_decoder": ["SpeechEncoderDecoderConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["SpeechEncoderDecoderModel"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["FlaxSpeechEncoderDecoderModel"] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
1
'''simple docstring''' import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class __A ( A ): '''simple docstring''' __lowerCamelCase : BigBirdConfig __lowerCamelCase : jnp.dtype = jnp.floataa __lowerCamelCase : bool = True def a__ (self ) -> Any: """simple docstring""" super().setup() _a = nn.Dense(5 , dtype=self.dtype ) def __call__(self , *A , **A ) -> Any: """simple docstring""" _a = super().__call__(*A , **A ) _a = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class __A ( A ): '''simple docstring''' __lowerCamelCase : Any = FlaxBigBirdForNaturalQuestionsModule def lowerCAmelCase (__A , __A , __A , __A , __A , __A): """simple docstring""" def cross_entropy(__A , __A , __A=None): _a = logits.shape[-1] _a = (labels[..., None] == jnp.arange(__A)[None]).astype('''f4''') _a = jax.nn.log_softmax(__A , axis=-1) _a = -jnp.sum(labels * logits , axis=-1) if reduction is not None: _a = reduction(__A) return loss _a = partial(__A , reduction=jnp.mean) _a = cross_entropy(__A , __A) _a = cross_entropy(__A , __A) _a = cross_entropy(__A , __A) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class __A : '''simple docstring''' __lowerCamelCase : str = "google/bigbird-roberta-base" __lowerCamelCase : int = 3_000 __lowerCamelCase : int = 10_500 __lowerCamelCase : int = 128 __lowerCamelCase : int = 3 __lowerCamelCase : int = 1 __lowerCamelCase : int = 5 # tx_args __lowerCamelCase : float = 3E-5 __lowerCamelCase : float = 0.0 __lowerCamelCase : int = 20_000 __lowerCamelCase : float = 0.00_95 __lowerCamelCase : str = "bigbird-roberta-natural-questions" __lowerCamelCase : str = "training-expt" __lowerCamelCase : str = "data/nq-training.jsonl" __lowerCamelCase : str = "data/nq-validation.jsonl" def a__ (self ) -> List[Any]: """simple docstring""" os.makedirs(self.base_dir , exist_ok=A ) _a = os.path.join(self.base_dir , self.save_dir ) _a = self.batch_size_per_device * jax.device_count() @dataclass class __A : '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : int = 4_096 # no dynamic padding on TPUs def __call__(self , A ) -> Tuple: """simple docstring""" _a = self.collate_fn(A ) _a = jax.tree_util.tree_map(A , A ) return batch def a__ (self , A ) -> List[str]: """simple docstring""" _a , _a = self.fetch_inputs(features['''input_ids'''] ) _a = { '''input_ids''': jnp.array(A , dtype=jnp.intaa ), '''attention_mask''': jnp.array(A , dtype=jnp.intaa ), '''start_labels''': jnp.array(features['''start_token'''] , dtype=jnp.intaa ), '''end_labels''': jnp.array(features['''end_token'''] , dtype=jnp.intaa ), '''pooled_labels''': jnp.array(features['''category'''] , dtype=jnp.intaa ), } return batch def a__ (self , A ) -> Tuple: """simple docstring""" _a = [self._fetch_inputs(A ) for ids in input_ids] return zip(*A ) def a__ (self , A ) -> Optional[Any]: """simple docstring""" _a = [1 for _ in range(len(A ) )] while len(A ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def lowerCAmelCase (__A , __A , __A=None): """simple docstring""" if seed is not None: _a = dataset.shuffle(seed=__A) for i in range(len(__A) // batch_size): _a = dataset[i * batch_size : (i + 1) * batch_size] yield dict(__A) @partial(jax.pmap , axis_name='''batch''') def lowerCAmelCase (__A , __A , **__A): """simple docstring""" def loss_fn(__A): _a = model_inputs.pop('''start_labels''') _a = model_inputs.pop('''end_labels''') _a = model_inputs.pop('''pooled_labels''') _a = state.apply_fn(**__A , params=__A , dropout_rng=__A , train=__A) _a , _a , _a = outputs return state.loss_fn( __A , __A , __A , __A , __A , __A , ) _a , _a = jax.random.split(__A) _a = jax.value_and_grad(__A) _a , _a = grad_fn(state.params) _a = jax.lax.pmean({'''loss''': loss} , axis_name='''batch''') _a = jax.lax.pmean(__A , '''batch''') _a = state.apply_gradients(grads=__A) return state, metrics, new_drp_rng @partial(jax.pmap , axis_name='''batch''') def lowerCAmelCase (__A , **__A): """simple docstring""" _a = model_inputs.pop('''start_labels''') _a = model_inputs.pop('''end_labels''') _a = model_inputs.pop('''pooled_labels''') _a = state.apply_fn(**__A , params=state.params , train=__A) _a , _a , _a = outputs _a = state.loss_fn(__A , __A , __A , __A , __A , __A) _a = jax.lax.pmean({'''loss''': loss} , axis_name='''batch''') return metrics class __A ( train_state.TrainState ): '''simple docstring''' __lowerCamelCase : Callable = struct.field(pytree_node=A ) @dataclass class __A : '''simple docstring''' __lowerCamelCase : Args __lowerCamelCase : Callable __lowerCamelCase : Callable __lowerCamelCase : Callable __lowerCamelCase : Callable __lowerCamelCase : wandb __lowerCamelCase : Callable = None def a__ (self , A , A , A , A=None ) -> Tuple: """simple docstring""" _a = model.params _a = TrainState.create( apply_fn=model.__call__ , params=A , tx=A , loss_fn=A , ) if ckpt_dir is not None: _a , _a , _a , _a , _a = restore_checkpoint(A , A ) _a = { '''lr''': args.lr, '''init_lr''': args.init_lr, '''warmup_steps''': args.warmup_steps, '''num_train_steps''': num_train_steps, '''weight_decay''': args.weight_decay, } _a , _a = build_tx(**A ) _a = train_state.TrainState( step=A , apply_fn=model.__call__ , params=A , tx=A , opt_state=A , ) _a = args _a = data_collator _a = lr _a = params _a = jax_utils.replicate(A ) return state def a__ (self , A , A , A ) -> Dict: """simple docstring""" _a = self.args _a = len(A ) // args.batch_size _a = jax.random.PRNGKey(0 ) _a = jax.random.split(A , jax.device_count() ) for epoch in range(args.max_epochs ): _a = jnp.array(0 , dtype=jnp.floataa ) _a = get_batched_dataset(A , args.batch_size , seed=A ) _a = 0 for batch in tqdm(A , total=A , desc=f'''Running EPOCH-{epoch}''' ): _a = self.data_collator(A ) _a , _a , _a = self.train_step_fn(A , A , **A ) running_loss += jax_utils.unreplicate(metrics['''loss'''] ) i += 1 if i % args.logging_steps == 0: _a = jax_utils.unreplicate(state.step ) _a = running_loss.item() / i _a = self.scheduler_fn(state_step - 1 ) _a = self.evaluate(A , A ) _a = { '''step''': state_step.item(), '''eval_loss''': eval_loss.item(), '''tr_loss''': tr_loss, '''lr''': lr.item(), } tqdm.write(str(A ) ) self.logger.log(A , commit=A ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + f'''-e{epoch}-s{i}''' , state=A ) def a__ (self , A , A ) -> List[Any]: """simple docstring""" _a = get_batched_dataset(A , self.args.batch_size ) _a = len(A ) // self.args.batch_size _a = jnp.array(0 , dtype=jnp.floataa ) _a = 0 for batch in tqdm(A , total=A , desc='''Evaluating ... ''' ): _a = self.data_collator(A ) _a = self.val_step_fn(A , **A ) running_loss += jax_utils.unreplicate(metrics['''loss'''] ) i += 1 return running_loss / i def a__ (self , A , A ) -> Dict: """simple docstring""" _a = jax_utils.unreplicate(A ) print(f'''SAVING CHECKPOINT IN {save_dir}''' , end=''' ... ''' ) self.model_save_fn(A , params=state.params ) with open(os.path.join(A , '''opt_state.msgpack''' ) , '''wb''' ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args , os.path.join(A , '''args.joblib''' ) ) joblib.dump(self.data_collator , os.path.join(A , '''data_collator.joblib''' ) ) with open(os.path.join(A , '''training_state.json''' ) , '''w''' ) as f: json.dump({'''step''': state.step.item()} , A ) print('''DONE''' ) def lowerCAmelCase (__A , __A): """simple docstring""" print(F'''RESTORING CHECKPOINT FROM {save_dir}''' , end=''' ... ''') with open(os.path.join(__A , '''flax_model.msgpack''') , '''rb''') as f: _a = from_bytes(state.params , f.read()) with open(os.path.join(__A , '''opt_state.msgpack''') , '''rb''') as f: _a = from_bytes(state.opt_state , f.read()) _a = joblib.load(os.path.join(__A , '''args.joblib''')) _a = joblib.load(os.path.join(__A , '''data_collator.joblib''')) with open(os.path.join(__A , '''training_state.json''') , '''r''') as f: _a = json.load(__A) _a = training_state['''step'''] print('''DONE''') return params, opt_state, step, args, data_collator def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = num_train_steps - warmup_steps _a = optax.linear_schedule(init_value=__A , end_value=__A , transition_steps=__A) _a = optax.linear_schedule(init_value=__A , end_value=1e-7 , transition_steps=__A) _a = optax.join_schedules(schedules=[warmup_fn, decay_fn] , boundaries=[warmup_steps]) return lr def lowerCAmelCase (__A , __A , __A , __A , __A): """simple docstring""" def weight_decay_mask(__A): _a = traverse_util.flatten_dict(__A) _a = {k: (v[-1] != '''bias''' and v[-2:] != ('''LayerNorm''', '''scale''')) for k, v in params.items()} return traverse_util.unflatten_dict(__A) _a = scheduler_fn(__A , __A , __A , __A) _a = optax.adamw(learning_rate=__A , weight_decay=__A , mask=__A) return tx, lr
11
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
1
'''simple docstring''' from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class __A : '''simple docstring''' __lowerCamelCase : List[str] = PegasusConfig __lowerCamelCase : Any = {} __lowerCamelCase : int = 'gelu' def __init__(self , A , A=13 , A=7 , A=True , A=False , A=99 , A=32 , A=2 , A=4 , A=37 , A=0.1 , A=0.1 , A=40 , A=2 , A=1 , A=0 , ) -> Union[str, Any]: """simple docstring""" _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 a__ (self ) -> List[Any]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) _a = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) _a = tf.concat([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 a__ (self , A , A ) -> str: """simple docstring""" _a = TFPegasusModel(config=A ).get_decoder() _a = inputs_dict['''input_ids'''] _a = input_ids[:1, :] _a = inputs_dict['''attention_mask'''][:1, :] _a = inputs_dict['''head_mask'''] _a = 1 # first forward pass _a = model(A , attention_mask=A , head_mask=A , use_cache=A ) _a , _a = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 3) , config.vocab_size ) _a = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and _a = tf.concat([input_ids, next_tokens] , axis=-1 ) _a = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) _a = model(A , attention_mask=A )[0] _a = model(A , attention_mask=A , past_key_values=A )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice _a = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) _a = output_from_no_past[:, -3:, random_slice_idx] _a = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(A , A , rtol=1E-3 ) def lowerCAmelCase (__A , __A , __A , __A=None , __A=None , __A=None , __A=None , __A=None , ): """simple docstring""" if attention_mask is None: _a = tf.cast(tf.math.not_equal(__A , config.pad_token_id) , tf.inta) if decoder_attention_mask is None: _a = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id) , tf.inta), ] , axis=-1 , ) if head_mask is None: _a = tf.ones((config.encoder_layers, config.encoder_attention_heads)) if decoder_head_mask is None: _a = tf.ones((config.decoder_layers, config.decoder_attention_heads)) if cross_attn_head_mask is None: _a = tf.ones((config.decoder_layers, config.decoder_attention_heads)) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () __lowerCamelCase : Union[str, Any] = (TFPegasusForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : List[Any] = ( { 'conversational': TFPegasusForConditionalGeneration, 'feature-extraction': TFPegasusModel, 'summarization': TFPegasusForConditionalGeneration, 'text2text-generation': TFPegasusForConditionalGeneration, 'translation': TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : str = True __lowerCamelCase : Any = False __lowerCamelCase : Tuple = False def a__ (self ) -> Dict: """simple docstring""" _a = TFPegasusModelTester(self ) _a = ConfigTester(self , config_class=A ) def a__ (self ) -> Optional[int]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*A ) @require_sentencepiece @require_tokenizers @require_tf class __A ( unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = [ ' 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!" ', ] __lowerCamelCase : Dict = [ 'California\'s largest electricity provider has cut power to hundreds of thousands of customers in an effort to' ' reduce the risk of wildfires.', 'N-Dubz have revealed they\'re "grateful" to have been nominated for four Mobo Awards.', ] # differs slightly from pytorch, likely due to numerical differences in linear layers __lowerCamelCase : str = 'google/pegasus-xsum' @cached_property def a__ (self ) -> Union[str, Any]: """simple docstring""" return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def a__ (self ) -> Tuple: """simple docstring""" _a = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def a__ (self , **A ) -> Optional[int]: """simple docstring""" _a = self.translate_src_text(**A ) assert self.expected_text == generated_words def a__ (self , **A ) -> Optional[int]: """simple docstring""" _a = self.tokenizer(self.src_text , **A , padding=A , return_tensors='''tf''' ) _a = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=A , ) _a = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=A ) return generated_words @slow def a__ (self ) -> int: """simple docstring""" self._assert_generated_batch_equal_expected()
11
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def lowerCAmelCase (__A = "laptop"): """simple docstring""" _a = F'''https://www.amazon.in/laptop/s?k={product}''' _a = { '''User-Agent''': '''Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36''', '''Accept-Language''': '''en-US, en;q=0.5''', } _a = BeautifulSoup(requests.get(__A , headers=__A).text) # Initialize a Pandas dataframe with the column titles _a = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ]) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' , attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} , ) , soup.find_all('''div''' , attrs={'''class''': '''a-row a-size-base a-color-base'''}) , ): try: _a = item.ha.text _a = '''https://www.amazon.in/''' + item.ha.a['''href'''] _a = item.find('''span''' , attrs={'''class''': '''a-offscreen'''}).text try: _a = item.find('''span''' , attrs={'''class''': '''a-icon-alt'''}).text except AttributeError: _a = '''Not available''' try: _a = ( '''₹''' + item.find( '''span''' , attrs={'''class''': '''a-price a-text-price'''}).text.split('''₹''')[1] ) except AttributeError: _a = '''''' try: _a = float( ( ( float(product_mrp.strip('''₹''').replace(''',''' , '''''')) - float(product_price.strip('''₹''').replace(''',''' , '''''')) ) / float(product_mrp.strip('''₹''').replace(''',''' , '''''')) ) * 100) except ValueError: _a = float('''nan''') except AttributeError: pass _a = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] _a = ''' ''' _a = ''' ''' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = "headphones" get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
11
1
'''simple docstring''' import os from itertools import chain from random import randrange, shuffle import pytest from .sola import PokerHand lowercase_ = ( "4S 3H 2C 7S 5H", "9D 8H 2C 6S 7H", "2D 6D 9D TH 7D", "TC 8C 2S JH 6C", "JH 8S TH AH QH", "TS KS 5S 9S AC", "KD 6S 9D TH AD", "KS 8D 4D 9S 4S", # pair "8C 4S KH JS 4D", # pair "QH 8H KD JH 8S", # pair "KC 4H KS 2H 8D", # pair "KD 4S KC 3H 8S", # pair "AH 8S AS KC JH", # pair "3H 4C 4H 3S 2H", # 2 pairs "5S 5D 2C KH KH", # 2 pairs "3C KH 5D 5S KH", # 2 pairs "AS 3C KH AD KH", # 2 pairs "7C 7S 3S 7H 5S", # 3 of a kind "7C 7S KH 2H 7H", # 3 of a kind "AC KH QH AH AS", # 3 of a kind "2H 4D 3C AS 5S", # straight (low ace) "3C 5C 4C 2C 6H", # straight "6S 8S 7S 5H 9H", # straight "JS QS 9H TS KH", # straight "QC KH TS JS AH", # straight (high ace) "8C 9C 5C 3C TC", # flush "3S 8S 9S 5S KS", # flush "4C 5C 9C 8C KC", # flush "JH 8H AH KH QH", # flush "3D 2H 3H 2C 2D", # full house "2H 2C 3S 3H 3D", # full house "KH KC 3S 3H 3D", # full house "JC 6H JS JD JH", # 4 of a kind "JC 7H JS JD JH", # 4 of a kind "JC KH JS JD JH", # 4 of a kind "2S AS 4S 5S 3S", # straight flush (low ace) "2D 6D 3D 4D 5D", # straight flush "5C 6C 3C 7C 4C", # straight flush "JH 9H TH KH QH", # straight flush "JH AH TH KH QH", # royal flush (high ace straight flush) ) lowercase_ = ( ("2H 3H 4H 5H 6H", "KS AS TS QS JS", "Loss"), ("2H 3H 4H 5H 6H", "AS AD AC AH JD", "Win"), ("AS AH 2H AD AC", "JS JD JC JH 3D", "Win"), ("2S AH 2H AS AC", "JS JD JC JH AD", "Loss"), ("2S AH 2H AS AC", "2H 3H 5H 6H 7H", "Win"), ("AS 3S 4S 8S 2S", "2H 3H 5H 6H 7H", "Win"), ("2H 3H 5H 6H 7H", "2S 3H 4H 5S 6C", "Win"), ("2S 3H 4H 5S 6C", "3D 4C 5H 6H 2S", "Tie"), ("2S 3H 4H 5S 6C", "AH AC 5H 6H AS", "Win"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H AS", "Loss"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H 7S", "Win"), ("6S AD 7H 4S AS", "AH AC 5H 6H 7S", "Loss"), ("2S AH 4H 5S KC", "AH AC 5H 6H 7S", "Loss"), ("2S 3H 6H 7S 9C", "7H 3C TH 6H 9S", "Loss"), ("4S 5H 6H TS AC", "3S 5H 6H TS AC", "Win"), ("2S AH 4H 5S 6C", "AD 4C 5H 6H 2C", "Tie"), ("AS AH 3H AD AC", "AS AH 2H AD AC", "Win"), ("AH AC 5H 5C QS", "AH AC 5H 5C KS", "Loss"), ("AH AC 5H 5C QS", "KH KC 5H 5C QS", "Win"), ("7C 7S KH 2H 7H", "3C 3S AH 2H 3H", "Win"), ("3C 3S AH 2H 3H", "7C 7S KH 2H 7H", "Loss"), ("6H 5H 4H 3H 2H", "5H 4H 3H 2H AH", "Win"), ("5H 4H 3H 2H AH", "5H 4H 3H 2H AH", "Tie"), ("5H 4H 3H 2H AH", "6H 5H 4H 3H 2H", "Loss"), ("AH AD KS KC AC", "AH KD KH AC KC", "Win"), ("2H 4D 3C AS 5S", "2H 4D 3C 6S 5S", "Loss"), ("2H 3S 3C 3H 2S", "3S 3C 2S 2H 2D", "Win"), ("4D 6D 5D 2D JH", "3S 8S 3H TC KH", "Loss"), ("4S 6C 8S 3S 7S", "AD KS 2D 7D 7C", "Loss"), ("6S 4C 7H 8C 3H", "5H JC AH 9D 9C", "Loss"), ("9D 9H JH TC QH", "3C 2S JS 5C 7H", "Win"), ("2H TC 8S AD 9S", "4H TS 7H 2C 5C", "Win"), ("9D 3S 2C 7S 7C", "JC TD 3C TC 9H", "Loss"), ) lowercase_ = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", True), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", False), ("AS 3S 4S 8S 2S", True), ) lowercase_ = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", False), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", True), ) lowercase_ = ( ("2H 4D 3C AS 5S", True, [5, 4, 3, 2, 14]), ("2H 5D 3C AS 5S", False, [14, 5, 5, 3, 2]), ("JH QD KC AS TS", False, [14, 13, 12, 11, 10]), ("9D 3S 2C 7S 7C", False, [9, 7, 7, 3, 2]), ) lowercase_ = ( ("JH AH TH KH QH", 0), ("JH 9H TH KH QH", 0), ("JC KH JS JD JH", 7), ("KH KC 3S 3H 3D", 6), ("8C 9C 5C 3C TC", 0), ("JS QS 9H TS KH", 0), ("7C 7S KH 2H 7H", 3), ("3C KH 5D 5S KH", 2), ("QH 8H KD JH 8S", 1), ("2D 6D 9D TH 7D", 0), ) lowercase_ = ( ("JH AH TH KH QH", 23), ("JH 9H TH KH QH", 22), ("JC KH JS JD JH", 21), ("KH KC 3S 3H 3D", 20), ("8C 9C 5C 3C TC", 19), ("JS QS 9H TS KH", 18), ("7C 7S KH 2H 7H", 17), ("3C KH 5D 5S KH", 16), ("QH 8H KD JH 8S", 15), ("2D 6D 9D TH 7D", 14), ) def lowerCAmelCase (): """simple docstring""" _a , _a = randrange(len(__A)), randrange(len(__A)) _a = ['''Loss''', '''Tie''', '''Win'''][(play >= oppo) + (play > oppo)] _a , _a = SORTED_HANDS[play], SORTED_HANDS[oppo] return hand, other, expected def lowerCAmelCase (__A = 100): """simple docstring""" return (generate_random_hand() for _ in range(__A)) @pytest.mark.parametrize('''hand, expected''' , __A) def lowerCAmelCase (__A , __A): """simple docstring""" assert PokerHand(__A)._is_flush() == expected @pytest.mark.parametrize('''hand, expected''' , __A) def lowerCAmelCase (__A , __A): """simple docstring""" assert PokerHand(__A)._is_straight() == expected @pytest.mark.parametrize('''hand, expected, card_values''' , __A) def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a = PokerHand(__A) assert player._is_five_high_straight() == expected assert player._card_values == card_values @pytest.mark.parametrize('''hand, expected''' , __A) def lowerCAmelCase (__A , __A): """simple docstring""" assert PokerHand(__A)._is_same_kind() == expected @pytest.mark.parametrize('''hand, expected''' , __A) def lowerCAmelCase (__A , __A): """simple docstring""" assert PokerHand(__A)._hand_type == expected @pytest.mark.parametrize('''hand, other, expected''' , __A) def lowerCAmelCase (__A , __A , __A): """simple docstring""" assert PokerHand(__A).compare_with(PokerHand(__A)) == expected @pytest.mark.parametrize('''hand, other, expected''' , generate_random_hands()) def lowerCAmelCase (__A , __A , __A): """simple docstring""" assert PokerHand(__A).compare_with(PokerHand(__A)) == expected def lowerCAmelCase (): """simple docstring""" _a = [PokerHand(__A) for hand in SORTED_HANDS] _a = poker_hands.copy() shuffle(__A) _a = chain(sorted(__A)) for index, hand in enumerate(__A): assert hand == poker_hands[index] def lowerCAmelCase (): """simple docstring""" _a = [PokerHand('''2D AC 3H 4H 5S'''), PokerHand('''2S 3H 4H 5S 6C''')] pokerhands.sort(reverse=__A) assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C" def lowerCAmelCase (): """simple docstring""" _a = PokerHand('''2C 4S AS 3D 5C''') _a = True _a = [5, 4, 3, 2, 14] for _ in range(10): assert pokerhand._is_five_high_straight() == expected assert pokerhand._card_values == expected_card_values def lowerCAmelCase (): """simple docstring""" _a = 0 _a = os.path.abspath(os.path.dirname(__A)) _a = os.path.join(__A , '''poker_hands.txt''') with open(__A) as file_hand: for line in file_hand: _a = line[:14].strip() _a = line[15:].strip() _a , _a = PokerHand(__A), PokerHand(__A) _a = player.compare_with(__A) if output == "Win": answer += 1 assert answer == 376
11
'''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 lowerCAmelCase (__A , __A , __A): """simple docstring""" if isinstance(__A , torch.Tensor): return image elif isinstance(__A , PIL.Image.Image): _a = [image] if isinstance(image[0] , PIL.Image.Image): _a = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos''']))[None, :] for i in image] _a = np.concatenate(__A , axis=0) _a = np.array(__A).astype(np.floataa) / 2_55.0 _a = image.transpose(0 , 3 , 1 , 2) _a = 2.0 * image - 1.0 _a = torch.from_numpy(__A) elif isinstance(image[0] , torch.Tensor): _a = torch.cat(__A , dim=0) return image def lowerCAmelCase (__A , __A , __A , __A=0.99_95): """simple docstring""" if not isinstance(__A , np.ndarray): _a = True _a = va.device _a = va.cpu().numpy() _a = va.cpu().numpy() _a = np.sum(va * va / (np.linalg.norm(__A) * np.linalg.norm(__A))) if np.abs(__A) > DOT_THRESHOLD: _a = (1 - t) * va + t * va else: _a = np.arccos(__A) _a = np.sin(__A) _a = theta_a * t _a = np.sin(__A) _a = np.sin(theta_a - theta_t) / sin_theta_a _a = sin_theta_t / sin_theta_a _a = sa * va + sa * va if inputs_are_torch: _a = torch.from_numpy(__A).to(__A) return va def lowerCAmelCase (__A , __A): """simple docstring""" _a = F.normalize(__A , dim=-1) _a = F.normalize(__A , dim=-1) return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2) def lowerCAmelCase (__A , __A): """simple docstring""" for param in model.parameters(): _a = value class __A ( A ): '''simple docstring''' def __init__(self , A , A , A , A , A , A , A , A=None , A=None , A=None , ) -> str: """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 , ) _a = ( feature_extractor.size if isinstance(feature_extractor.size , A ) else feature_extractor.size['''shortest_edge'''] ) _a = 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 a__ (self , A = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A ) def a__ (self ) -> Optional[Any]: """simple docstring""" self.enable_attention_slicing(A ) def a__ (self ) -> int: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Dict: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self ) -> str: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self , A , A , A ) -> Optional[Any]: """simple docstring""" _a = min(int(num_inference_steps * strength ) , A ) _a = max(num_inference_steps - init_timestep , 0 ) _a = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def a__ (self , A , A , A , A , A , A=None ) -> List[str]: """simple docstring""" if not isinstance(A , torch.Tensor ): raise ValueError(f'''`image` has to be of type `torch.Tensor` but is {type(A )}''' ) _a = image.to(device=A , dtype=A ) if isinstance(A , A ): _a = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(A ) ] _a = torch.cat(A , dim=0 ) else: _a = self.vae.encode(A ).latent_dist.sample(A ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 0.18215 * init_latents _a = init_latents.repeat_interleave(A , dim=0 ) _a = randn_tensor(init_latents.shape , generator=A , device=A , dtype=A ) # get latents _a = self.scheduler.add_noise(A , A , A ) _a = init_latents return latents def a__ (self , A ) -> Tuple: """simple docstring""" _a = self.coca_transform(A ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): _a = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) _a = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def a__ (self , A , A ) -> List[Any]: """simple docstring""" _a = self.feature_extractor.preprocess(A ) _a = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = image_embeddings_clip.repeat_interleave(A , dim=0 ) return image_embeddings_clip @torch.enable_grad() def a__ (self , A , A , A , A , A , A , A , ) -> Union[str, Any]: """simple docstring""" _a = latents.detach().requires_grad_() _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): _a = self.scheduler.alphas_cumprod[timestep] _a = 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 _a = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 _a = torch.sqrt(A ) _a = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , A ): _a = self.scheduler.sigmas[index] _a = 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 _a = 1 / 0.18215 * sample _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = transforms.Resize(self.feature_extractor_size )(A ) _a = self.normalize(A ).to(latents.dtype ) _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = spherical_dist_loss(A , A ).mean() * clip_guidance_scale _a = -torch.autograd.grad(A , A )[0] if isinstance(self.scheduler , A ): _a = latents.detach() + grads * (sigma**2) _a = noise_pred_original else: _a = noise_pred_original - torch.sqrt(A ) * grads return noise_pred, latents @torch.no_grad() def __call__(self , A , A , A = None , A = None , A = 512 , A = 512 , A = 0.6 , A = 50 , A = 7.5 , A = 1 , A = 0.0 , A = 100 , A = None , A = "pil" , A = True , A = 0.8 , A = 0.1 , A = 0.1 , ) -> str: """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: _a = [generator] + [None] * (batch_size - 1) _a = [ ('''model''', self.coca_model is None), ('''tokenizer''', self.coca_tokenizer is None), ('''transform''', self.coca_transform is None), ] _a = [x[0] for x in coca_is_none if x[1]] _a = ''', '''.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.''' ) _a = 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.''' ) _a = self.get_image_description(A ) # get prompt text embeddings for content and style _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] _a = slerp(A , A , A ) # duplicate text embeddings for each generation per prompt _a = text_embeddings.repeat_interleave(A , dim=0 ) # set timesteps _a = '''offset''' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) _a = {} if accepts_offset: _a = 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 ) _a , _a = self.get_timesteps(A , A , self.device ) _a = timesteps[:1].repeat(A ) # Preprocess image _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = slerp(A , A , A ) if clip_guidance_scale > 0: _a = self.get_clip_image_embeddings(A , A ) _a = self.get_clip_image_embeddings(A , A ) _a = 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. _a = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _a = content_text_input.input_ids.shape[-1] _a = self.tokenizer([''''''] , padding='''max_length''' , max_length=A , return_tensors='''pt''' ) _a = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt _a = 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 _a = 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`. _a = (batch_size, self.unet.config.in_channels, height // 8, width // 8) _a = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps _a = torch.randn(A , generator=A , device='''cpu''' , dtype=A ).to( self.device ) else: _a = 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}''' ) _a = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _a = 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] _a = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _a = {} if accepts_eta: _a = eta # check if the scheduler accepts generator _a = '''generator''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: _a = generator with self.progress_bar(total=A ): for i, t in enumerate(A ): # expand the latents if we are doing classifier free guidance _a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample # perform classifier free guidance if do_classifier_free_guidance: _a , _a = noise_pred.chunk(2 ) _a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: _a = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) _a , _a = self.cond_fn( A , A , A , A , A , A , A , ) # compute the previous noisy sample x_t -> x_t-1 _a = self.scheduler.step(A , A , A , **A ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * latents _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _a = self.numpy_to_pil(A ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=A , nsfw_content_detected=A )
11
1
'''simple docstring''' class __A : '''simple docstring''' def __init__(self ) -> None: """simple docstring""" _a = {} # Mapping from char to TrieNode _a = False def a__ (self , A ) -> None: """simple docstring""" for word in words: self.insert(A ) def a__ (self , A ) -> None: """simple docstring""" _a = self for char in word: if char not in curr.nodes: _a = TrieNode() _a = curr.nodes[char] _a = True def a__ (self , A ) -> bool: """simple docstring""" _a = self for char in word: if char not in curr.nodes: return False _a = curr.nodes[char] return curr.is_leaf def a__ (self , A ) -> None: """simple docstring""" def _delete(A , A , A ) -> bool: if index == len(A ): # If word does not exist if not curr.is_leaf: return False _a = False return len(curr.nodes ) == 0 _a = word[index] _a = curr.nodes.get(A ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted _a = _delete(A , A , index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self , A , 0 ) def lowerCAmelCase (__A , __A): """simple docstring""" if node.is_leaf: print(__A , end=''' ''') for key, value in node.nodes.items(): print_words(__A , word + key) def lowerCAmelCase (): """simple docstring""" _a = '''banana bananas bandana band apple all beast'''.split() _a = TrieNode() root.insert_many(__A) # print_words(root, "") assert all(root.find(__A) for word in words) assert root.find('''banana''') assert not root.find('''bandanas''') assert not root.find('''apps''') assert root.find('''apple''') assert root.find('''all''') root.delete('''all''') assert not root.find('''all''') root.delete('''banana''') assert not root.find('''banana''') assert root.find('''bananas''') return True def lowerCAmelCase (__A , __A): """simple docstring""" print(str(__A) , '''works!''' if passes else '''doesn\'t work :(''') def lowerCAmelCase (): """simple docstring""" assert test_trie() def lowerCAmelCase (): """simple docstring""" print_results('''Testing trie functionality''' , test_trie()) if __name__ == "__main__": main()
11
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = CTRLTokenizer __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = False def a__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _a = dict(zip(A , range(len(A ) ) ) ) _a = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _a = {'''unk_token''': '''<unk>'''} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(A ) ) def a__ (self , **A ) -> int: """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> Tuple: """simple docstring""" _a = '''adapt react readapt apt''' _a = '''adapt react readapt apt''' return input_text, output_text def a__ (self ) -> List[Any]: """simple docstring""" _a = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = '''adapt react readapt apt''' _a = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _a = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _a = tokens + [tokenizer.unk_token] _a = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A )
11
1
'''simple docstring''' import secrets from random import shuffle from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation def lowerCAmelCase (__A = 8): """simple docstring""" _a = ascii_letters + digits + punctuation return "".join(secrets.choice(__A) for _ in range(__A)) def lowerCAmelCase (__A , __A): """simple docstring""" i -= len(__A) _a = i // 3 _a = i % 3 # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) _a = ( chars_incl + random(__A , quotient + remainder) + random(__A , __A) + random(__A , __A) ) _a = list(__A) shuffle(__A) return "".join(__A) # random is a generalised function for letters, characters and numbers def lowerCAmelCase (__A , __A): """simple docstring""" return "".join(secrets.choice(__A) for _ in range(__A)) def lowerCAmelCase (__A , __A): """simple docstring""" pass # Put your code here... def lowerCAmelCase (__A , __A): """simple docstring""" pass # Put your code here... def lowerCAmelCase (__A , __A): """simple docstring""" pass # Put your code here... def lowerCAmelCase (__A , __A = 8): """simple docstring""" if len(__A) < min_length: # Your Password must be at least 8 characters long return False _a = any(char in ascii_uppercase for char in password) _a = any(char in ascii_lowercase for char in password) _a = any(char in digits for char in password) _a = any(char in punctuation for char in password) return upper and lower and num and spec_char # Passwords should contain UPPERCASE, lowerase # numbers, and special characters def lowerCAmelCase (): """simple docstring""" _a = int(input('''Please indicate the max length of your password: ''').strip()) _a = input( '''Please indicate the characters that must be in your password: ''').strip() print('''Password generated:''' , password_generator(__A)) print( '''Alternative Password generated:''' , alternative_password_generator(__A , __A) , ) print('''[If you are thinking of using this passsword, You better save it.]''') if __name__ == "__main__": main()
11
'''simple docstring''' import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument lowercase_ = { "/attention/": "/0/SelfAttention/", "/self_attention/": "/0/SelfAttention/", "/encoder_decoder_attention/": "/1/EncDecAttention/", "value": "v", "query": "q", "key": "k", "out": "o", "pre_self_attention_layer_norm": "0/layer_norm", "pre_cross_attention_layer_norm": "1/layer_norm", "pre_attention_layer_norm": "0/layer_norm", # previously 1, but seems wrong "token_embedder": "shared", "encoder_norm": "final_layer_norm", "decoder_norm": "final_layer_norm", "relpos_bias/rel_embedding": "block/0/layer/0/SelfAttention/relative_attention_bias/weight", "router/router_weights/w/": "router/classifier/", "roer/roer_weights/w/": "router/classifier/", "logits_dense": "lm_head", } def lowerCAmelCase (__A): """simple docstring""" _a = list(s_dict.keys()) for key in keys: _a = r'''.*/layers_(\d+)''' _a = key if re.match(__A , __A): _a = re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __A) _a = r'''(encoder|decoder)\/''' if re.match(__A , __A): _a = re.match(__A , __A).groups() if groups[0] == "encoder": _a = re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __A) elif groups[0] == "decoder": _a = re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __A) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: _a = new_key.replace(__A , __A) print(F'''{key} -> {new_key}''') _a = s_dict.pop(__A) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = s_dict[ '''encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = s_dict[ '''decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys()): if "expert" in key: _a = s_dict[key].shape[0] _a = s_dict[key] for idx in range(__A): _a = expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring')}''') s_dict.pop(__A) return s_dict lowercase_ = { "NUM_ENCODER_LAYERS": "num_layers", "NUM_DECODER_LAYERS": "num_decoder_layers", "NUM_HEADS": "num_heads", "HEAD_DIM": "d_kv", "EMBED_DIM": "d_model", "MLP_DIM": "d_ff", "NUM_SELECTED_EXPERTS": "num_selected_experts", "NUM_ENCODER_SPARSE_LAYERS": "num_sparse_encoder_layers", "NUM_DECODER_SPARSE_LAYERS": "num_sparse_decoder_layers", "dense.MlpBlock.activations": "feed_forward_proj", } def lowerCAmelCase (__A , __A): """simple docstring""" import regex as re with open(__A , '''r''') as f: _a = f.read() _a = re.findall(r'''(.*) = ([0-9.]*)''' , __A) _a = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": _a = float(__A) if '''.''' in value else int(__A) _a = re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __A)[0] _a = str(activation[1]) _a = num_experts _a = SwitchTransformersConfig(**__A) return config def lowerCAmelCase (__A , __A , __A=None , __A="./" , __A=8): """simple docstring""" print(F'''Loading flax weights from : {flax_checkpoint_path}''') _a = checkpoints.load_tax_checkpoint(__A) if gin_file is not None: _a = convert_gin_to_config(__A , __A) else: _a = SwitchTransformersConfig.from_pretrained(__A) _a = SwitchTransformersForConditionalGeneration(__A) _a = flax_params['''target'''] _a = flatten_dict(__A , sep='''/''') _a = rename_keys(__A) _a = unflatten_dict(__A , sep='''/''') # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__A , __A) print(F'''Save PyTorch model to {pytorch_dump_path}''') pt_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the" " model architecture. If not provided, a `gin_file` has to be provided." ), ) parser.add_argument( "--gin_file", default=None, type=str, required=False, help="Path to the gin config file. If not provided, a `config_file` has to be passed ", ) parser.add_argument( "--config_name", default=None, type=str, required=False, help="Config name of SwitchTransformers model." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output pytorch model." ) parser.add_argument("--num_experts", default=8, type=int, required=False, help="Number of experts") lowercase_ = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
11
1
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
1
'''simple docstring''' import argparse from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument( "--txt2img_unclip", default="kakaobrain/karlo-v1-alpha", type=str, required=False, help="The pretrained txt2img unclip.", ) lowercase_ = parser.parse_args() lowercase_ = UnCLIPPipeline.from_pretrained(args.txtaimg_unclip) lowercase_ = CLIPImageProcessor() lowercase_ = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14") lowercase_ = UnCLIPImageVariationPipeline( decoder=txtaimg.decoder, text_encoder=txtaimg.text_encoder, tokenizer=txtaimg.tokenizer, text_proj=txtaimg.text_proj, feature_extractor=feature_extractor, image_encoder=image_encoder, super_res_first=txtaimg.super_res_first, super_res_last=txtaimg.super_res_last, decoder_scheduler=txtaimg.decoder_scheduler, super_res_scheduler=txtaimg.super_res_scheduler, ) imgaimg.save_pretrained(args.dump_path)
11
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
1
'''simple docstring''' from copy import deepcopy from typing import Optional, Union import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, is_tf_available, is_torch_available if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf class __A ( A ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = ['image_processor'] __lowerCamelCase : Dict = 'SamImageProcessor' def __init__(self , A ) -> List[Any]: """simple docstring""" super().__init__(A ) _a = self.image_processor _a = -10 _a = self.image_processor.size['''longest_edge'''] def __call__(self , A=None , A=None , A=None , A=None , A = None , **A , ) -> BatchEncoding: """simple docstring""" _a = self.image_processor( A , return_tensors=A , **A , ) # pop arguments that are not used in the foward but used nevertheless _a = encoding_image_processor['''original_sizes'''] if hasattr(A , '''numpy''' ): # Checks if Torch or TF tensor _a = original_sizes.numpy() _a , _a , _a = self._check_and_preprocess_points( input_points=A , input_labels=A , input_boxes=A , ) _a = self._normalize_and_convert( A , A , input_points=A , input_labels=A , input_boxes=A , return_tensors=A , ) return encoding_image_processor def a__ (self , A , A , A=None , A=None , A=None , A="pt" , ) -> Dict: """simple docstring""" if input_points is not None: if len(A ) != len(A ): _a = [ self._normalize_coordinates(self.target_size , A , original_sizes[0] ) for point in input_points ] else: _a = [ self._normalize_coordinates(self.target_size , A , A ) for point, original_size in zip(A , A ) ] # check that all arrays have the same shape if not all(point.shape == input_points[0].shape for point in input_points ): if input_labels is not None: _a , _a = self._pad_points_and_labels(A , A ) _a = np.array(A ) if input_labels is not None: _a = np.array(A ) if input_boxes is not None: if len(A ) != len(A ): _a = [ self._normalize_coordinates(self.target_size , A , original_sizes[0] , is_bounding_box=A ) for box in input_boxes ] else: _a = [ self._normalize_coordinates(self.target_size , A , A , is_bounding_box=A ) for box, original_size in zip(A , A ) ] _a = np.array(A ) if input_boxes is not None: if return_tensors == "pt": _a = torch.from_numpy(A ) # boxes batch size of 1 by default _a = input_boxes.unsqueeze(1 ) if len(input_boxes.shape ) != 3 else input_boxes elif return_tensors == "tf": _a = tf.convert_to_tensor(A ) # boxes batch size of 1 by default _a = tf.expand_dims(A , 1 ) if len(input_boxes.shape ) != 3 else input_boxes encoding_image_processor.update({'''input_boxes''': input_boxes} ) if input_points is not None: if return_tensors == "pt": _a = torch.from_numpy(A ) # point batch size of 1 by default _a = input_points.unsqueeze(1 ) if len(input_points.shape ) != 4 else input_points elif return_tensors == "tf": _a = tf.convert_to_tensor(A ) # point batch size of 1 by default _a = tf.expand_dims(A , 1 ) if len(input_points.shape ) != 4 else input_points encoding_image_processor.update({'''input_points''': input_points} ) if input_labels is not None: if return_tensors == "pt": _a = torch.from_numpy(A ) # point batch size of 1 by default _a = input_labels.unsqueeze(1 ) if len(input_labels.shape ) != 3 else input_labels elif return_tensors == "tf": _a = tf.convert_to_tensor(A ) # point batch size of 1 by default _a = tf.expand_dims(A , 1 ) if len(input_labels.shape ) != 3 else input_labels encoding_image_processor.update({'''input_labels''': input_labels} ) return encoding_image_processor def a__ (self , A , A ) -> Dict: """simple docstring""" _a = max([point.shape[0] for point in input_points] ) _a = [] for i, point in enumerate(A ): if point.shape[0] != expected_nb_points: _a = np.concatenate( [point, np.zeros((expected_nb_points - point.shape[0], 2) ) + self.point_pad_value] , axis=0 ) _a = np.append(input_labels[i] , [self.point_pad_value] ) processed_input_points.append(A ) _a = processed_input_points return input_points, input_labels def a__ (self , A , A , A , A=False ) -> np.ndarray: """simple docstring""" _a , _a = original_size _a , _a = self.image_processor._get_preprocess_shape(A , longest_edge=A ) _a = deepcopy(A ).astype(A ) if is_bounding_box: _a = coords.reshape(-1 , 2 , 2 ) _a = coords[..., 0] * (new_w / old_w) _a = coords[..., 1] * (new_h / old_h) if is_bounding_box: _a = coords.reshape(-1 , 4 ) return coords def a__ (self , A=None , A=None , A=None , ) -> Dict: """simple docstring""" if input_points is not None: if hasattr(A , '''numpy''' ): # Checks for TF or Torch tensor _a = input_points.numpy().tolist() if not isinstance(A , A ) or not isinstance(input_points[0] , A ): raise ValueError('''Input points must be a list of list of floating points.''' ) _a = [np.array(A ) for input_point in input_points] else: _a = None if input_labels is not None: if hasattr(A , '''numpy''' ): _a = input_labels.numpy().tolist() if not isinstance(A , A ) or not isinstance(input_labels[0] , A ): raise ValueError('''Input labels must be a list of list integers.''' ) _a = [np.array(A ) for label in input_labels] else: _a = None if input_boxes is not None: if hasattr(A , '''numpy''' ): _a = input_boxes.numpy().tolist() if ( not isinstance(A , A ) or not isinstance(input_boxes[0] , A ) or not isinstance(input_boxes[0][0] , A ) ): raise ValueError('''Input boxes must be a list of list of list of floating points.''' ) _a = [np.array(A ).astype(np.floataa ) for box in input_boxes] else: _a = None return input_points, input_labels, input_boxes @property def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.image_processor.model_input_names return list(dict.fromkeys(A ) ) def a__ (self , *A , **A ) -> List[str]: """simple docstring""" return self.image_processor.post_process_masks(*A , **A )
11
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
1
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """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=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> str: """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=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> Optional[int]: """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=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
11
'''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 __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''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 a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
1
'''simple docstring''' def lowerCAmelCase (__A , __A = " "): """simple docstring""" _a = [] _a = 0 for index, char in enumerate(__A): if char == separator: split_words.append(string[last_index:index]) _a = index + 1 elif index + 1 == len(__A): split_words.append(string[last_index : index + 1]) return split_words if __name__ == "__main__": from doctest import testmod testmod()
11
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
1
'''simple docstring''' import copy from typing import Dict, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING from ..detr import DetrConfig from ..swin import SwinConfig lowercase_ = { "facebook/maskformer-swin-base-ade": ( "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json" ) # See all MaskFormer models at https://huggingface.co/models?filter=maskformer } lowercase_ = logging.get_logger(__name__) class __A ( A ): '''simple docstring''' __lowerCamelCase : Optional[int] = 'maskformer' __lowerCamelCase : int = {'hidden_size': 'mask_feature_size'} __lowerCamelCase : List[str] = ['resnet', 'swin'] __lowerCamelCase : Dict = ['detr'] def __init__(self , A = 256 , A = 256 , A = 0.1 , A = False , A = None , A = None , A = 0.02 , A = 1.0 , A = 1.0 , A = 1.0 , A = 20.0 , A = None , **A , ) -> List[Any]: """simple docstring""" if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k _a = SwinConfig( image_size=384 , in_channels=3 , patch_size=4 , embed_dim=128 , depths=[2, 2, 18, 2] , num_heads=[4, 8, 16, 32] , window_size=12 , drop_path_rate=0.3 , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] , ) if isinstance(A , A ): _a = backbone_config.pop('''model_type''' ) _a = CONFIG_MAPPING[backbone_model_type] _a = config_class.from_dict(A ) # verify that the backbone is supported if backbone_config.model_type not in self.backbones_supported: logger.warning_once( f'''Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. ''' f'''Supported model types: {','.join(self.backbones_supported )}''' ) if decoder_config is None: # fall back to https://huggingface.co/facebook/detr-resnet-50 _a = DetrConfig() else: # verify that the decoder is supported _a = ( decoder_config.pop('''model_type''' ) if isinstance(A , A ) else decoder_config.model_type ) if decoder_type not in self.decoders_supported: raise ValueError( f'''Transformer Decoder {decoder_type} not supported, please use one of''' f''' {','.join(self.decoders_supported )}''' ) if isinstance(A , A ): _a = CONFIG_MAPPING[decoder_type] _a = config_class.from_dict(A ) _a = backbone_config _a = decoder_config # main feature dimension for the model _a = fpn_feature_size _a = mask_feature_size # initializer _a = init_std _a = init_xavier_std # Hungarian matcher && loss _a = cross_entropy_weight _a = dice_weight _a = mask_weight _a = use_auxiliary_loss _a = no_object_weight _a = output_auxiliary_logits _a = self.decoder_config.encoder_attention_heads _a = self.decoder_config.num_hidden_layers super().__init__(**A ) @classmethod def a__ (cls , A , A , **A ) -> Optional[Any]: """simple docstring""" return cls( backbone_config=A , decoder_config=A , **A , ) def a__ (self ) -> Dict[str, any]: """simple docstring""" _a = copy.deepcopy(self.__dict__ ) _a = self.backbone_config.to_dict() _a = self.decoder_config.to_dict() _a = self.__class__.model_type return output
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import importlib.metadata import operator import re import sys from typing import Optional from packaging import version lowercase_ = { "<": operator.lt, "<=": operator.le, "==": operator.eq, "!=": operator.ne, ">=": operator.ge, ">": operator.gt, } def lowerCAmelCase (__A , __A , __A , __A , __A , __A): """simple docstring""" if got_ver is None or want_ver is None: raise ValueError( F'''Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider''' F''' reinstalling {pkg}.''') if not ops[op](version.parse(__A) , version.parse(__A)): raise ImportError( F'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}''') def lowerCAmelCase (__A , __A = None): """simple docstring""" _a = F'''\n{hint}''' if hint is not None else '''''' # non-versioned check if re.match(r'''^[\w_\-\d]+$''' , __A): _a , _a , _a = requirement, None, None else: _a = re.findall(r'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)''' , __A) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but''' F''' got {requirement}''') _a , _a = match[0] _a = want_full.split(''',''') # there could be multiple requirements _a = {} for w in want_range: _a = re.findall(r'''^([\s!=<>]{1,2})(.+)''' , __A) if not match: raise ValueError( '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,''' F''' but got {requirement}''') _a , _a = match[0] _a = want_ver if op not in ops: raise ValueError(F'''{requirement}: need one of {list(ops.keys())}, but got {op}''') # special case if pkg == "python": _a = '''.'''.join([str(__A) for x in sys.version_info[:3]]) for op, want_ver in wanted.items(): _compare_versions(__A , __A , __A , __A , __A , __A) return # check if any version is installed try: _a = importlib.metadata.version(__A) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( F'''The \'{requirement}\' distribution was not found and is required by this application. {hint}''') # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(__A , __A , __A , __A , __A , __A) def lowerCAmelCase (__A): """simple docstring""" _a = '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main''' return require_version(__A , __A)
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
1
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = str(__A) return len(__A) == 9 and set(__A) == set('''123456789''') def lowerCAmelCase (): """simple docstring""" for base_num in range(9_999 , 4_999 , -1): _a = 100_002 * base_num if is_9_pandigital(__A): return candidate for base_num in range(333 , 99 , -1): _a = 1_002_003 * base_num if is_9_pandigital(__A): return candidate return None if __name__ == "__main__": print(F"""{solution() = }""")
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from argparse import ArgumentParser, Namespace from typing import Any, List, Optional from ..pipelines import Pipeline, get_supported_tasks, pipeline from ..utils import logging from . import BaseTransformersCLICommand try: from fastapi import Body, FastAPI, HTTPException from fastapi.routing import APIRoute from pydantic import BaseModel from starlette.responses import JSONResponse from uvicorn import run lowercase_ = True except (ImportError, AttributeError): lowercase_ = object def lowerCAmelCase (*__A , **__A): """simple docstring""" pass lowercase_ = False lowercase_ = logging.get_logger("transformers-cli/serving") def lowerCAmelCase (__A): """simple docstring""" _a = pipeline( task=args.task , model=args.model if args.model else None , config=args.config , tokenizer=args.tokenizer , device=args.device , ) return ServeCommand(__A , args.host , args.port , args.workers) class __A ( A ): '''simple docstring''' __lowerCamelCase : dict class __A ( A ): '''simple docstring''' __lowerCamelCase : List[str] __lowerCamelCase : Optional[List[int]] class __A ( A ): '''simple docstring''' __lowerCamelCase : str class __A ( A ): '''simple docstring''' __lowerCamelCase : Any class __A ( A ): '''simple docstring''' @staticmethod def a__ (A ) -> Any: """simple docstring""" _a = parser.add_parser( '''serve''' , help='''CLI tool to run inference requests through REST and GraphQL endpoints.''' ) serve_parser.add_argument( '''--task''' , type=A , choices=get_supported_tasks() , help='''The task to run the pipeline on''' , ) serve_parser.add_argument('''--host''' , type=A , default='''localhost''' , help='''Interface the server will listen on.''' ) serve_parser.add_argument('''--port''' , type=A , default=8_888 , help='''Port the serving will listen to.''' ) serve_parser.add_argument('''--workers''' , type=A , default=1 , help='''Number of http workers''' ) serve_parser.add_argument('''--model''' , type=A , help='''Model\'s name or path to stored model.''' ) serve_parser.add_argument('''--config''' , type=A , help='''Model\'s config name or path to stored model.''' ) serve_parser.add_argument('''--tokenizer''' , type=A , help='''Tokenizer name to use.''' ) serve_parser.add_argument( '''--device''' , type=A , default=-1 , help='''Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)''' , ) serve_parser.set_defaults(func=A ) def __init__(self , A , A , A , A ) -> List[str]: """simple docstring""" _a = pipeline _a = host _a = port _a = workers if not _serve_dependencies_installed: raise RuntimeError( '''Using serve command requires FastAPI and uvicorn. ''' '''Please install transformers with [serving]: pip install "transformers[serving]".''' '''Or install FastAPI and uvicorn separately.''' ) else: logger.info(f'''Serving model over {host}:{port}''' ) _a = FastAPI( routes=[ APIRoute( '''/''' , self.model_info , response_model=A , response_class=A , methods=['''GET'''] , ), APIRoute( '''/tokenize''' , self.tokenize , response_model=A , response_class=A , methods=['''POST'''] , ), APIRoute( '''/detokenize''' , self.detokenize , response_model=A , response_class=A , methods=['''POST'''] , ), APIRoute( '''/forward''' , self.forward , response_model=A , response_class=A , methods=['''POST'''] , ), ] , timeout=600 , ) def a__ (self ) -> List[str]: """simple docstring""" run(self._app , host=self.host , port=self.port , workers=self.workers ) def a__ (self ) -> List[Any]: """simple docstring""" return ServeModelInfoResult(infos=vars(self._pipeline.model.config ) ) def a__ (self , A = Body(A , embed=A ) , A = Body(A , embed=A ) ) -> str: """simple docstring""" try: _a = self._pipeline.tokenizer.tokenize(A ) if return_ids: _a = self._pipeline.tokenizer.convert_tokens_to_ids(A ) return ServeTokenizeResult(tokens=A , tokens_ids=A ) else: return ServeTokenizeResult(tokens=A ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(A )} ) def a__ (self , A = Body(A , embed=A ) , A = Body(A , embed=A ) , A = Body(A , embed=A ) , ) -> List[str]: """simple docstring""" try: _a = self._pipeline.tokenizer.decode(A , A , A ) return ServeDeTokenizeResult(model='''''' , text=A ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(A )} ) async def a__ (self , A=Body(A , embed=A ) ) -> List[str]: """simple docstring""" if len(A ) == 0: return ServeForwardResult(output=[] , attention=[] ) try: # Forward through the model _a = self._pipeline(A ) return ServeForwardResult(output=A ) except Exception as e: raise HTTPException(500 , {'''error''': str(A )} )
11
'''simple docstring''' 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 lowercase_ = { # 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 __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' def lowerCAmelCase (__A = 4_000_000): """simple docstring""" _a = [] _a , _a = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__A) _a , _a = b, a + b return sum(__A) if __name__ == "__main__": print(F"""{solution() = }""")
11
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
1
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: lowercase_ = None lowercase_ = logging.get_logger(__name__) lowercase_ = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} lowercase_ = { "vocab_file": { "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/spiece.model", "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/spiece.model", "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model", "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model", "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/spiece.model", "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/spiece.model", "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model", "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model", }, "tokenizer_file": { "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json", "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json", "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json", "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json", "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json", "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json", "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json", "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json", }, } lowercase_ = { "albert-base-v1": 512, "albert-large-v1": 512, "albert-xlarge-v1": 512, "albert-xxlarge-v1": 512, "albert-base-v2": 512, "albert-large-v2": 512, "albert-xlarge-v2": 512, "albert-xxlarge-v2": 512, } lowercase_ = "▁" class __A ( A ): '''simple docstring''' __lowerCamelCase : Optional[int] = VOCAB_FILES_NAMES __lowerCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__(self , A=None , A=None , A=True , A=True , A=False , A="[CLS]" , A="[SEP]" , A="<unk>" , A="[SEP]" , A="<pad>" , A="[CLS]" , A="[MASK]" , **A , ) -> Tuple: """simple docstring""" _a = ( AddedToken(A , lstrip=A , rstrip=A , normalized=A ) if isinstance(A , A ) else mask_token ) super().__init__( A , tokenizer_file=A , do_lower_case=A , remove_space=A , keep_accents=A , bos_token=A , eos_token=A , unk_token=A , sep_token=A , pad_token=A , cls_token=A , mask_token=A , **A , ) _a = do_lower_case _a = remove_space _a = keep_accents _a = vocab_file _a = False if not self.vocab_file else True def a__ (self , A , A = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a__ (self , A , A = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a__ (self , A , A = None ) -> Tuple[str]: """simple docstring""" 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(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _a = 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 ): copyfile(self.vocab_file , A ) return (out_vocab_file,)
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import os import numpy import onnx def lowerCAmelCase (__A , __A): """simple docstring""" _a = a.name _a = b.name _a = '''''' _a = '''''' _a = a == b _a = name_a _a = name_b return res def lowerCAmelCase (__A , __A , __A): """simple docstring""" for i, input_name in enumerate(node_proto.input): if input_name == name: node_proto.input.insert(__A , __A) node_proto.input.pop(i + 1) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , __A , __A) _graph_replace_input_with(node_proto.attribute[1].g , __A , __A) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , __A , __A) def lowerCAmelCase (__A , __A , __A): """simple docstring""" for n in graph_proto.node: _node_replace_input_with(__A , __A , __A) def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a = list(model.graph.initializer) _a = list(model_without_ext.graph.initializer) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i _a = inits[i].name _a = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i]) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , __A , __A) def lowerCAmelCase (__A): """simple docstring""" _a = os.path.dirname(__A) _a = os.path.basename(__A) _a = onnx.load(os.path.join(__A , __A)) _a = list(model.graph.initializer) _a = set() _a = {} _a = [] _a = 0 for i in range(len(__A)): if i in dup_set: continue for j in range(i + 1 , len(__A)): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j]): dup_set.add(__A) dup_set.add(__A) _a = inits[j].data_type _a = numpy.prod(inits[j].dims) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print('''unexpected data type: ''' , __A) total_reduced_size += mem_size _a = inits[i].name _a = inits[j].name if name_i in dup_map: dup_map[name_i].append(__A) else: _a = [name_j] ind_to_replace.append((j, i)) print('''total reduced size: ''' , total_reduced_size / 1_024 / 1_024 / 1_024 , '''GB''') _a = sorted(__A) _remove_dup_initializers_from_model(__A , __A , __A) _a = '''optimized_''' + model_file_name _a = os.path.join(__A , __A) onnx.save(__A , __A) return new_model
11
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """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=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> str: """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=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> Optional[int]: """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=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
11
1
'''simple docstring''' from __future__ import annotations class __A : '''simple docstring''' def __init__(self , A , A ) -> Optional[Any]: """simple docstring""" _a , _a = text, pattern _a , _a = len(A ), len(A ) def a__ (self , A ) -> int: """simple docstring""" for i in range(self.patLen - 1 , -1 , -1 ): if char == self.pattern[i]: return i return -1 def a__ (self , A ) -> int: """simple docstring""" for i in range(self.patLen - 1 , -1 , -1 ): if self.pattern[i] != self.text[current_pos + i]: return current_pos + i return -1 def a__ (self ) -> list[int]: """simple docstring""" _a = [] for i in range(self.textLen - self.patLen + 1 ): _a = self.mismatch_in_text(A ) if mismatch_index == -1: positions.append(A ) else: _a = self.match_in_pattern(self.text[mismatch_index] ) _a = ( mismatch_index - match_index ) # shifting index lgtm [py/multiple-definition] return positions lowercase_ = "ABAABA" lowercase_ = "AB" lowercase_ = BoyerMooreSearch(text, pattern) lowercase_ = bms.bad_character_heuristic() if len(positions) == 0: print("No match found") else: print("Pattern found in following positions: ") print(positions)
11
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _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 = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
1
'''simple docstring''' import argparse import json import os import tensorstore as ts import torch from flax import serialization from flax.traverse_util import flatten_dict, unflatten_dict from tensorflow.io import gfile from transformers.modeling_utils import dtype_byte_size from transformers.models.switch_transformers.convert_switch_transformers_original_flax_checkpoint_to_pytorch import ( rename_keys, ) from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME from transformers.utils.hub import convert_file_size_to_int def lowerCAmelCase (__A , __A): """simple docstring""" if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 3: # expert layer _a = flax_key_tuple[:-1] + ('''weight''',) _a = torch.permute(__A , (0, 2, 1)) elif flax_key_tuple[-1] == "kernel" and ".".join(__A): # linear layer _a = flax_key_tuple[:-1] + ('''weight''',) _a = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: _a = flax_key_tuple[:-1] + ('''weight''',) return flax_key_tuple, flax_tensor def lowerCAmelCase (__A , __A , __A): """simple docstring""" if "metadata" in layer: _a = layer.split('''metadata''') _a = ''''''.join(split_layer[0])[:-1] _a = [tuple(('''metadata''' + split_layer[1]).split('''/'''))] elif "kvstore" in layer: _a = layer.split('''kvstore''') _a = ''''''.join(split_layer[0])[:-1] _a = [tuple(('''kvstore''' + split_layer[1]).split('''/'''))] else: _a = layer.split('''/''') _a = '''/'''.join(split_layer[:-1]) _a = (split_layer[-1],) if "kvstore/path" in layer: _a = F'''{switch_checkpoint_path}/{checkpoint_info[layer]}''' elif "kvstore/driver" in layer: _a = '''file''' else: _a = checkpoint_info[layer] return curr_real_layer_name, split_layer, content def lowerCAmelCase (__A , __A): """simple docstring""" _a = rename_keys(__A) _a = {} for k, v in current_block.items(): _a = v _a = new_current_block torch.save(__A , __A) def lowerCAmelCase (__A , __A , __A , __A , __A = WEIGHTS_NAME): """simple docstring""" _a = convert_file_size_to_int(__A) _a = [] _a = {} _a = 0 _a = 0 os.makedirs(__A , exist_ok=__A) with gfile.GFile(switch_checkpoint_path + '''/checkpoint''' , '''rb''') as fp: _a = serialization.msgpack_restore(fp.read())['''optimizer''']['''target'''] _a = flatten_dict(__A , sep='''/''') _a = {} for layer in checkpoint_info.keys(): _a , _a , _a = get_key_and_tensorstore_dict( __A , __A , __A) if curr_real_layer_name in all_layers: _a = content else: _a = {split_layer[-1]: content} for key in all_layers.keys(): # open tensorstore file _a = ts.open(unflatten_dict(all_layers[key])).result().read().result() _a = torch.tensor(__A) _a = raw_weights.numel() * dtype_byte_size(raw_weights.dtype) # use the renaming pattern from the small conversion scripts _a , _a = rename_base_flax_keys(tuple(key.split('''/''')) , __A) _a = '''/'''.join(__A) # If this weight is going to tip up over the maximal size, we split. if current_block_size + weight_size > max_shard_size: _a = os.path.join( __A , weights_name.replace('''.bin''' , F'''-{len(__A)+1:05d}-of-???.bin''')) rename_and_save_block(__A , __A) sharded_state_dicts.append(current_block.keys()) del current_block _a = {} _a = 0 _a = raw_weights.to(getattr(__A , __A)) current_block_size += weight_size total_size += weight_size # Add the last block _a = os.path.join(__A , weights_name.replace('''.bin''' , F'''-{len(__A)+1:05d}-of-???.bin''')) rename_and_save_block(__A , __A) sharded_state_dicts.append(current_block.keys()) # If we only have one shard, we return it if len(__A) == 1: return {weights_name: sharded_state_dicts[0]}, None # Otherwise, let's build the index _a = {} _a = {} for idx, shard in enumerate(__A): _a = weights_name.replace( '''.bin''' , F'''-{idx+1:05d}-of-{len(__A):05d}.bin''') # len(sharded_state_dicts):05d} _a = os.path.join(__A , weights_name.replace('''.bin''' , F'''-{idx+1:05d}-of-???.bin''')) os.rename(__A , os.path.join(__A , __A)) _a = shard for key in shard: _a = shard_file # Add the metadata _a = {'''total_size''': total_size} _a = {'''metadata''': metadata, '''weight_map''': weight_map} with open(os.path.join(__A , __A) , '''w''' , encoding='''utf-8''') as f: _a = json.dumps(__A , indent=2 , sort_keys=__A) + '''\n''' f.write(__A) return metadata, index if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default="/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128/checkpoint_634600", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--max_shard_size", default="10GB", required=False, help="Max shard size") parser.add_argument("--dtype", default="bfloat16", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128-converted", type=str, required=False, help="Path to the output pytorch model.", ) lowercase_ = parser.parse_args() shard_on_the_fly( args.switch_tax_checkpoint_path, args.pytorch_dump_folder_path, args.max_shard_size, args.dtype, ) def lowerCAmelCase (): """simple docstring""" from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration, TaTokenizer _a = SwitchTransformersConfig.from_pretrained('''google/switch-base-8''') config.save_pretrained('''/home/arthur_huggingface_co/transformers/switch_converted''') _a = SwitchTransformersForConditionalGeneration.from_pretrained( '''/home/arthur_huggingface_co/transformers/switch_converted''' , device_map='''auto''') _a = TaTokenizer.from_pretrained('''t5-small''') _a = '''A <extra_id_0> walks into a bar a orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''' _a = tokenizer(__A , return_tensors='''pt''').input_ids _a = model.generate(__A , decoder_start_token_id=0) print(tokenizer.decode(out[0]))
11
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # 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) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
1
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def lowerCAmelCase (__A = "laptop"): """simple docstring""" _a = F'''https://www.amazon.in/laptop/s?k={product}''' _a = { '''User-Agent''': '''Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36''', '''Accept-Language''': '''en-US, en;q=0.5''', } _a = BeautifulSoup(requests.get(__A , headers=__A).text) # Initialize a Pandas dataframe with the column titles _a = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ]) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' , attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} , ) , soup.find_all('''div''' , attrs={'''class''': '''a-row a-size-base a-color-base'''}) , ): try: _a = item.ha.text _a = '''https://www.amazon.in/''' + item.ha.a['''href'''] _a = item.find('''span''' , attrs={'''class''': '''a-offscreen'''}).text try: _a = item.find('''span''' , attrs={'''class''': '''a-icon-alt'''}).text except AttributeError: _a = '''Not available''' try: _a = ( '''₹''' + item.find( '''span''' , attrs={'''class''': '''a-price a-text-price'''}).text.split('''₹''')[1] ) except AttributeError: _a = '''''' try: _a = float( ( ( float(product_mrp.strip('''₹''').replace(''',''' , '''''')) - float(product_price.strip('''₹''').replace(''',''' , '''''')) ) / float(product_mrp.strip('''₹''').replace(''',''' , '''''')) ) * 100) except ValueError: _a = float('''nan''') except AttributeError: pass _a = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] _a = ''' ''' _a = ''' ''' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = "headphones" get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
11
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, 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.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = 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 , is_decoder=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class __A ( A ): '''simple docstring''' __lowerCamelCase : int = 'yolos' def __init__(self , A=768 , A=12 , A=12 , A=3_072 , A="gelu" , A=0.0 , A=0.0 , A=0.02 , A=1E-12 , A=[512, 864] , A=16 , A=3 , A=True , A=100 , A=True , A=False , A=1 , A=5 , A=2 , A=5 , A=2 , A=0.1 , **A , ) -> str: """simple docstring""" super().__init__(**A ) _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 = initializer_range _a = layer_norm_eps _a = image_size _a = patch_size _a = num_channels _a = qkv_bias _a = num_detection_tokens _a = use_mid_position_embeddings _a = auxiliary_loss # Hungarian matcher _a = class_cost _a = bbox_cost _a = giou_cost # Loss coefficients _a = bbox_loss_coefficient _a = giou_loss_coefficient _a = eos_coefficient class __A ( A ): '''simple docstring''' __lowerCamelCase : str = version.parse('1.11' ) @property def a__ (self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def a__ (self ) -> float: """simple docstring""" return 1E-4 @property def a__ (self ) -> int: """simple docstring""" return 12
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
1
'''simple docstring''' import math def lowerCAmelCase (__A): """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 lowerCAmelCase (__A = 0.1): """simple docstring""" _a = 3 _a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1): primes += is_prime(__A) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = {"configuration_deit": ["DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DeiTConfig", "DeiTOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["DeiTFeatureExtractor"] lowercase_ = ["DeiTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "DEIT_PRETRAINED_MODEL_ARCHIVE_LIST", "DeiTForImageClassification", "DeiTForImageClassificationWithTeacher", "DeiTForMaskedImageModeling", "DeiTModel", "DeiTPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDeiTForImageClassification", "TFDeiTForImageClassificationWithTeacher", "TFDeiTForMaskedImageModeling", "TFDeiTModel", "TFDeiTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def lowerCAmelCase (__A = "laptop"): """simple docstring""" _a = F'''https://www.amazon.in/laptop/s?k={product}''' _a = { '''User-Agent''': '''Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36''', '''Accept-Language''': '''en-US, en;q=0.5''', } _a = BeautifulSoup(requests.get(__A , headers=__A).text) # Initialize a Pandas dataframe with the column titles _a = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ]) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' , attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} , ) , soup.find_all('''div''' , attrs={'''class''': '''a-row a-size-base a-color-base'''}) , ): try: _a = item.ha.text _a = '''https://www.amazon.in/''' + item.ha.a['''href'''] _a = item.find('''span''' , attrs={'''class''': '''a-offscreen'''}).text try: _a = item.find('''span''' , attrs={'''class''': '''a-icon-alt'''}).text except AttributeError: _a = '''Not available''' try: _a = ( '''₹''' + item.find( '''span''' , attrs={'''class''': '''a-price a-text-price'''}).text.split('''₹''')[1] ) except AttributeError: _a = '''''' try: _a = float( ( ( float(product_mrp.strip('''₹''').replace(''',''' , '''''')) - float(product_price.strip('''₹''').replace(''',''' , '''''')) ) / float(product_mrp.strip('''₹''').replace(''',''' , '''''')) ) * 100) except ValueError: _a = float('''nan''') except AttributeError: pass _a = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] _a = ''' ''' _a = ''' ''' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = "headphones" get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
11
1
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" while a != 0: _a , _a = b % a, a return b def lowerCAmelCase (__A , __A): """simple docstring""" if gcd(__A , __A) != 1: _a = F'''mod inverse of {a!r} and {m!r} does not exist''' raise ValueError(__A) _a , _a , _a = 1, 0, a _a , _a , _a = 0, 1, m while va != 0: _a = ua // va _a , _a , _a , _a , _a , _a = (ua - q * va), (ua - q * va), (ua - q * va), va, va, va return ua % m
11
'''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 lowerCAmelCase (__A , __A , __A): """simple docstring""" if isinstance(__A , torch.Tensor): return image elif isinstance(__A , PIL.Image.Image): _a = [image] if isinstance(image[0] , PIL.Image.Image): _a = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos''']))[None, :] for i in image] _a = np.concatenate(__A , axis=0) _a = np.array(__A).astype(np.floataa) / 2_55.0 _a = image.transpose(0 , 3 , 1 , 2) _a = 2.0 * image - 1.0 _a = torch.from_numpy(__A) elif isinstance(image[0] , torch.Tensor): _a = torch.cat(__A , dim=0) return image def lowerCAmelCase (__A , __A , __A , __A=0.99_95): """simple docstring""" if not isinstance(__A , np.ndarray): _a = True _a = va.device _a = va.cpu().numpy() _a = va.cpu().numpy() _a = np.sum(va * va / (np.linalg.norm(__A) * np.linalg.norm(__A))) if np.abs(__A) > DOT_THRESHOLD: _a = (1 - t) * va + t * va else: _a = np.arccos(__A) _a = np.sin(__A) _a = theta_a * t _a = np.sin(__A) _a = np.sin(theta_a - theta_t) / sin_theta_a _a = sin_theta_t / sin_theta_a _a = sa * va + sa * va if inputs_are_torch: _a = torch.from_numpy(__A).to(__A) return va def lowerCAmelCase (__A , __A): """simple docstring""" _a = F.normalize(__A , dim=-1) _a = F.normalize(__A , dim=-1) return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2) def lowerCAmelCase (__A , __A): """simple docstring""" for param in model.parameters(): _a = value class __A ( A ): '''simple docstring''' def __init__(self , A , A , A , A , A , A , A , A=None , A=None , A=None , ) -> str: """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 , ) _a = ( feature_extractor.size if isinstance(feature_extractor.size , A ) else feature_extractor.size['''shortest_edge'''] ) _a = 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 a__ (self , A = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A ) def a__ (self ) -> Optional[Any]: """simple docstring""" self.enable_attention_slicing(A ) def a__ (self ) -> int: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Dict: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self ) -> str: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self , A , A , A ) -> Optional[Any]: """simple docstring""" _a = min(int(num_inference_steps * strength ) , A ) _a = max(num_inference_steps - init_timestep , 0 ) _a = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def a__ (self , A , A , A , A , A , A=None ) -> List[str]: """simple docstring""" if not isinstance(A , torch.Tensor ): raise ValueError(f'''`image` has to be of type `torch.Tensor` but is {type(A )}''' ) _a = image.to(device=A , dtype=A ) if isinstance(A , A ): _a = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(A ) ] _a = torch.cat(A , dim=0 ) else: _a = self.vae.encode(A ).latent_dist.sample(A ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 0.18215 * init_latents _a = init_latents.repeat_interleave(A , dim=0 ) _a = randn_tensor(init_latents.shape , generator=A , device=A , dtype=A ) # get latents _a = self.scheduler.add_noise(A , A , A ) _a = init_latents return latents def a__ (self , A ) -> Tuple: """simple docstring""" _a = self.coca_transform(A ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): _a = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) _a = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def a__ (self , A , A ) -> List[Any]: """simple docstring""" _a = self.feature_extractor.preprocess(A ) _a = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = image_embeddings_clip.repeat_interleave(A , dim=0 ) return image_embeddings_clip @torch.enable_grad() def a__ (self , A , A , A , A , A , A , A , ) -> Union[str, Any]: """simple docstring""" _a = latents.detach().requires_grad_() _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): _a = self.scheduler.alphas_cumprod[timestep] _a = 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 _a = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 _a = torch.sqrt(A ) _a = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , A ): _a = self.scheduler.sigmas[index] _a = 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 _a = 1 / 0.18215 * sample _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = transforms.Resize(self.feature_extractor_size )(A ) _a = self.normalize(A ).to(latents.dtype ) _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = spherical_dist_loss(A , A ).mean() * clip_guidance_scale _a = -torch.autograd.grad(A , A )[0] if isinstance(self.scheduler , A ): _a = latents.detach() + grads * (sigma**2) _a = noise_pred_original else: _a = noise_pred_original - torch.sqrt(A ) * grads return noise_pred, latents @torch.no_grad() def __call__(self , A , A , A = None , A = None , A = 512 , A = 512 , A = 0.6 , A = 50 , A = 7.5 , A = 1 , A = 0.0 , A = 100 , A = None , A = "pil" , A = True , A = 0.8 , A = 0.1 , A = 0.1 , ) -> str: """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: _a = [generator] + [None] * (batch_size - 1) _a = [ ('''model''', self.coca_model is None), ('''tokenizer''', self.coca_tokenizer is None), ('''transform''', self.coca_transform is None), ] _a = [x[0] for x in coca_is_none if x[1]] _a = ''', '''.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.''' ) _a = 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.''' ) _a = self.get_image_description(A ) # get prompt text embeddings for content and style _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] _a = slerp(A , A , A ) # duplicate text embeddings for each generation per prompt _a = text_embeddings.repeat_interleave(A , dim=0 ) # set timesteps _a = '''offset''' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) _a = {} if accepts_offset: _a = 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 ) _a , _a = self.get_timesteps(A , A , self.device ) _a = timesteps[:1].repeat(A ) # Preprocess image _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = slerp(A , A , A ) if clip_guidance_scale > 0: _a = self.get_clip_image_embeddings(A , A ) _a = self.get_clip_image_embeddings(A , A ) _a = 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. _a = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _a = content_text_input.input_ids.shape[-1] _a = self.tokenizer([''''''] , padding='''max_length''' , max_length=A , return_tensors='''pt''' ) _a = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt _a = 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 _a = 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`. _a = (batch_size, self.unet.config.in_channels, height // 8, width // 8) _a = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps _a = torch.randn(A , generator=A , device='''cpu''' , dtype=A ).to( self.device ) else: _a = 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}''' ) _a = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _a = 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] _a = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _a = {} if accepts_eta: _a = eta # check if the scheduler accepts generator _a = '''generator''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: _a = generator with self.progress_bar(total=A ): for i, t in enumerate(A ): # expand the latents if we are doing classifier free guidance _a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample # perform classifier free guidance if do_classifier_free_guidance: _a , _a = noise_pred.chunk(2 ) _a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: _a = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) _a , _a = self.cond_fn( A , A , A , A , A , A , A , ) # compute the previous noisy sample x_t -> x_t-1 _a = self.scheduler.step(A , A , A , **A ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * latents _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _a = self.numpy_to_pil(A ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=A , nsfw_content_detected=A )
11
1
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = CTRLTokenizer __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = False def a__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _a = dict(zip(A , range(len(A ) ) ) ) _a = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _a = {'''unk_token''': '''<unk>'''} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(A ) ) def a__ (self , **A ) -> int: """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> Tuple: """simple docstring""" _a = '''adapt react readapt apt''' _a = '''adapt react readapt apt''' return input_text, output_text def a__ (self ) -> List[Any]: """simple docstring""" _a = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = '''adapt react readapt apt''' _a = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _a = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _a = tokens + [tokenizer.unk_token] _a = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A )
11
1
'''simple docstring''' from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union import numpy as np import PIL from PIL import Image from ...utils import BaseOutput, is_torch_available, is_transformers_available @dataclass class __A ( A ): '''simple docstring''' __lowerCamelCase : Union[List[PIL.Image.Image], np.ndarray] __lowerCamelCase : Optional[List[bool]] if is_transformers_available() and is_torch_available(): from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline
11
'''simple docstring''' import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument lowercase_ = { "/attention/": "/0/SelfAttention/", "/self_attention/": "/0/SelfAttention/", "/encoder_decoder_attention/": "/1/EncDecAttention/", "value": "v", "query": "q", "key": "k", "out": "o", "pre_self_attention_layer_norm": "0/layer_norm", "pre_cross_attention_layer_norm": "1/layer_norm", "pre_attention_layer_norm": "0/layer_norm", # previously 1, but seems wrong "token_embedder": "shared", "encoder_norm": "final_layer_norm", "decoder_norm": "final_layer_norm", "relpos_bias/rel_embedding": "block/0/layer/0/SelfAttention/relative_attention_bias/weight", "router/router_weights/w/": "router/classifier/", "roer/roer_weights/w/": "router/classifier/", "logits_dense": "lm_head", } def lowerCAmelCase (__A): """simple docstring""" _a = list(s_dict.keys()) for key in keys: _a = r'''.*/layers_(\d+)''' _a = key if re.match(__A , __A): _a = re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __A) _a = r'''(encoder|decoder)\/''' if re.match(__A , __A): _a = re.match(__A , __A).groups() if groups[0] == "encoder": _a = re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __A) elif groups[0] == "decoder": _a = re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __A) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: _a = new_key.replace(__A , __A) print(F'''{key} -> {new_key}''') _a = s_dict.pop(__A) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = s_dict[ '''encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = s_dict[ '''decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys()): if "expert" in key: _a = s_dict[key].shape[0] _a = s_dict[key] for idx in range(__A): _a = expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring')}''') s_dict.pop(__A) return s_dict lowercase_ = { "NUM_ENCODER_LAYERS": "num_layers", "NUM_DECODER_LAYERS": "num_decoder_layers", "NUM_HEADS": "num_heads", "HEAD_DIM": "d_kv", "EMBED_DIM": "d_model", "MLP_DIM": "d_ff", "NUM_SELECTED_EXPERTS": "num_selected_experts", "NUM_ENCODER_SPARSE_LAYERS": "num_sparse_encoder_layers", "NUM_DECODER_SPARSE_LAYERS": "num_sparse_decoder_layers", "dense.MlpBlock.activations": "feed_forward_proj", } def lowerCAmelCase (__A , __A): """simple docstring""" import regex as re with open(__A , '''r''') as f: _a = f.read() _a = re.findall(r'''(.*) = ([0-9.]*)''' , __A) _a = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": _a = float(__A) if '''.''' in value else int(__A) _a = re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __A)[0] _a = str(activation[1]) _a = num_experts _a = SwitchTransformersConfig(**__A) return config def lowerCAmelCase (__A , __A , __A=None , __A="./" , __A=8): """simple docstring""" print(F'''Loading flax weights from : {flax_checkpoint_path}''') _a = checkpoints.load_tax_checkpoint(__A) if gin_file is not None: _a = convert_gin_to_config(__A , __A) else: _a = SwitchTransformersConfig.from_pretrained(__A) _a = SwitchTransformersForConditionalGeneration(__A) _a = flax_params['''target'''] _a = flatten_dict(__A , sep='''/''') _a = rename_keys(__A) _a = unflatten_dict(__A , sep='''/''') # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__A , __A) print(F'''Save PyTorch model to {pytorch_dump_path}''') pt_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the" " model architecture. If not provided, a `gin_file` has to be provided." ), ) parser.add_argument( "--gin_file", default=None, type=str, required=False, help="Path to the gin config file. If not provided, a `config_file` has to be passed ", ) parser.add_argument( "--config_name", default=None, type=str, required=False, help="Config name of SwitchTransformers model." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output pytorch model." ) parser.add_argument("--num_experts", default=8, type=int, required=False, help="Number of experts") lowercase_ = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
11
1
'''simple docstring''' import argparse import math import os import torch from neural_compressor.utils.pytorch import load from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() parser.add_argument( '''-m''' , '''--pretrained_model_name_or_path''' , type=__A , default=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , ) parser.add_argument( '''-c''' , '''--caption''' , type=__A , default='''robotic cat with wings''' , help='''Text used to generate images.''' , ) parser.add_argument( '''-n''' , '''--images_num''' , type=__A , default=4 , help='''How much images to generate.''' , ) parser.add_argument( '''-s''' , '''--seed''' , type=__A , default=42 , help='''Seed for random process.''' , ) parser.add_argument( '''-ci''' , '''--cuda_id''' , type=__A , default=0 , help='''cuda_id.''' , ) _a = parser.parse_args() return args def lowerCAmelCase (__A , __A , __A): """simple docstring""" if not len(__A) == rows * cols: raise ValueError('''The specified number of rows and columns are not correct.''') _a , _a = imgs[0].size _a = Image.new('''RGB''' , size=(cols * w, rows * h)) _a , _a = grid.size for i, img in enumerate(__A): grid.paste(__A , box=(i % cols * w, i // cols * h)) return grid def lowerCAmelCase (__A , __A="robotic cat with wings" , __A=7.5 , __A=50 , __A=1 , __A=42 , ): """simple docstring""" _a = torch.Generator(pipeline.device).manual_seed(__A) _a = pipeline( __A , guidance_scale=__A , num_inference_steps=__A , generator=__A , num_images_per_prompt=__A , ).images _a = int(math.sqrt(__A)) _a = image_grid(__A , rows=_rows , cols=num_images_per_prompt // _rows) return grid, images lowercase_ = parse_args() # Load models and create wrapper for stable diffusion lowercase_ = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="tokenizer") lowercase_ = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="text_encoder") lowercase_ = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder="vae") lowercase_ = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="unet") lowercase_ = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) lowercase_ = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, "best_model.pt")): lowercase_ = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, "unet", unet) else: lowercase_ = unet.to(torch.device("cuda", args.cuda_id)) lowercase_ = pipeline.to(unet.device) lowercase_ , lowercase_ = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed) grid.save(os.path.join(args.pretrained_model_name_or_path, "{}.png".format("_".join(args.caption.split())))) lowercase_ = os.path.join(args.pretrained_model_name_or_path, "_".join(args.caption.split())) os.makedirs(dirname, exist_ok=True) for idx, image in enumerate(images): image.save(os.path.join(dirname, "{}.png".format(idx + 1)))
11
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
1
'''simple docstring''' import json import os import tempfile import unittest import unittest.mock as mock from pathlib import Path from requests.exceptions import HTTPError from transformers.utils import ( CONFIG_NAME, FLAX_WEIGHTS_NAME, TF2_WEIGHTS_NAME, TRANSFORMERS_CACHE, WEIGHTS_NAME, cached_file, get_file_from_repo, has_file, ) lowercase_ = "hf-internal-testing/tiny-random-bert" lowercase_ = os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert") lowercase_ = "9b8c223d42b2188cb49d29af482996f9d0f3e5a6" class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> List[Any]: """simple docstring""" _a = cached_file(A , A ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(A ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(A , A ) ) ) with open(os.path.join(A , '''refs''' , '''main''' ) ) as f: _a = f.read() self.assertEqual(A , os.path.join(A , '''snapshots''' , A , A ) ) self.assertTrue(os.path.isfile(A ) ) # File is cached at the same place the second time. _a = cached_file(A , A ) self.assertEqual(A , A ) # Using a specific revision to test the full commit hash. _a = cached_file(A , A , revision='''9b8c223''' ) self.assertEqual(A , os.path.join(A , '''snapshots''' , A , A ) ) def a__ (self ) -> Dict: """simple docstring""" with self.assertRaisesRegex(A , '''is not a valid model identifier''' ): _a = cached_file('''tiny-random-bert''' , A ) with self.assertRaisesRegex(A , '''is not a valid git identifier''' ): _a = cached_file(A , A , revision='''aaaa''' ) with self.assertRaisesRegex(A , '''does not appear to have a file named''' ): _a = cached_file(A , '''conf''' ) def a__ (self ) -> int: """simple docstring""" with self.assertRaisesRegex(A , '''does not appear to have a file named''' ): _a = cached_file(A , '''conf''' ) with open(os.path.join(A , '''refs''' , '''main''' ) ) as f: _a = f.read() self.assertTrue(os.path.isfile(os.path.join(A , '''.no_exist''' , A , '''conf''' ) ) ) _a = cached_file(A , '''conf''' , _raise_exceptions_for_missing_entries=A ) self.assertIsNone(A ) _a = cached_file(A , '''conf''' , local_files_only=A , _raise_exceptions_for_missing_entries=A ) self.assertIsNone(A ) _a = mock.Mock() _a = 500 _a = {} _a = HTTPError _a = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('''requests.Session.request''' , return_value=A ) as mock_head: _a = cached_file(A , '''conf''' , _raise_exceptions_for_connection_errors=A ) self.assertIsNone(A ) # This check we did call the fake head request mock_head.assert_called() def a__ (self ) -> int: """simple docstring""" self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' , A ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , A ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , A ) ) def a__ (self ) -> str: """simple docstring""" self.assertIsNone(get_file_from_repo('''bert-base-cased''' , '''ahah.txt''' ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(A , '''is not a valid model identifier''' ): get_file_from_repo('''bert-base-case''' , A ) # The function raises if the revision does not exist. with self.assertRaisesRegex(A , '''is not a valid git identifier''' ): get_file_from_repo('''bert-base-cased''' , A , revision='''ahaha''' ) _a = get_file_from_repo('''bert-base-cased''' , A ) # The name is the cached name which is not very easy to test, so instead we load the content. _a = json.loads(open(A , '''r''' ).read() ) self.assertEqual(config['''hidden_size'''] , 768 ) def a__ (self ) -> List[str]: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: _a = Path(A ) / '''a.txt''' filename.touch() self.assertEqual(get_file_from_repo(A , '''a.txt''' ) , str(A ) ) self.assertIsNone(get_file_from_repo(A , '''b.txt''' ) )
11
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
1
'''simple docstring''' from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES lowercase_ = logging.get_logger(__name__) lowercase_ = OrderedDict( [ # Base model mapping ("albert", "FlaxAlbertModel"), ("bart", "FlaxBartModel"), ("beit", "FlaxBeitModel"), ("bert", "FlaxBertModel"), ("big_bird", "FlaxBigBirdModel"), ("blenderbot", "FlaxBlenderbotModel"), ("blenderbot-small", "FlaxBlenderbotSmallModel"), ("clip", "FlaxCLIPModel"), ("distilbert", "FlaxDistilBertModel"), ("electra", "FlaxElectraModel"), ("gpt-sw3", "FlaxGPT2Model"), ("gpt2", "FlaxGPT2Model"), ("gpt_neo", "FlaxGPTNeoModel"), ("gptj", "FlaxGPTJModel"), ("longt5", "FlaxLongT5Model"), ("marian", "FlaxMarianModel"), ("mbart", "FlaxMBartModel"), ("mt5", "FlaxMT5Model"), ("opt", "FlaxOPTModel"), ("pegasus", "FlaxPegasusModel"), ("regnet", "FlaxRegNetModel"), ("resnet", "FlaxResNetModel"), ("roberta", "FlaxRobertaModel"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormModel"), ("roformer", "FlaxRoFormerModel"), ("t5", "FlaxT5Model"), ("vision-text-dual-encoder", "FlaxVisionTextDualEncoderModel"), ("vit", "FlaxViTModel"), ("wav2vec2", "FlaxWav2Vec2Model"), ("whisper", "FlaxWhisperModel"), ("xglm", "FlaxXGLMModel"), ("xlm-roberta", "FlaxXLMRobertaModel"), ] ) lowercase_ = OrderedDict( [ # Model for pre-training mapping ("albert", "FlaxAlbertForPreTraining"), ("bart", "FlaxBartForConditionalGeneration"), ("bert", "FlaxBertForPreTraining"), ("big_bird", "FlaxBigBirdForPreTraining"), ("electra", "FlaxElectraForPreTraining"), ("longt5", "FlaxLongT5ForConditionalGeneration"), ("mbart", "FlaxMBartForConditionalGeneration"), ("mt5", "FlaxMT5ForConditionalGeneration"), ("roberta", "FlaxRobertaForMaskedLM"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForMaskedLM"), ("roformer", "FlaxRoFormerForMaskedLM"), ("t5", "FlaxT5ForConditionalGeneration"), ("wav2vec2", "FlaxWav2Vec2ForPreTraining"), ("whisper", "FlaxWhisperForConditionalGeneration"), ("xlm-roberta", "FlaxXLMRobertaForMaskedLM"), ] ) lowercase_ = OrderedDict( [ # Model for Masked LM mapping ("albert", "FlaxAlbertForMaskedLM"), ("bart", "FlaxBartForConditionalGeneration"), ("bert", "FlaxBertForMaskedLM"), ("big_bird", "FlaxBigBirdForMaskedLM"), ("distilbert", "FlaxDistilBertForMaskedLM"), ("electra", "FlaxElectraForMaskedLM"), ("mbart", "FlaxMBartForConditionalGeneration"), ("roberta", "FlaxRobertaForMaskedLM"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForMaskedLM"), ("roformer", "FlaxRoFormerForMaskedLM"), ("xlm-roberta", "FlaxXLMRobertaForMaskedLM"), ] ) lowercase_ = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ("bart", "FlaxBartForConditionalGeneration"), ("blenderbot", "FlaxBlenderbotForConditionalGeneration"), ("blenderbot-small", "FlaxBlenderbotSmallForConditionalGeneration"), ("encoder-decoder", "FlaxEncoderDecoderModel"), ("longt5", "FlaxLongT5ForConditionalGeneration"), ("marian", "FlaxMarianMTModel"), ("mbart", "FlaxMBartForConditionalGeneration"), ("mt5", "FlaxMT5ForConditionalGeneration"), ("pegasus", "FlaxPegasusForConditionalGeneration"), ("t5", "FlaxT5ForConditionalGeneration"), ] ) lowercase_ = OrderedDict( [ # Model for Image-classsification ("beit", "FlaxBeitForImageClassification"), ("regnet", "FlaxRegNetForImageClassification"), ("resnet", "FlaxResNetForImageClassification"), ("vit", "FlaxViTForImageClassification"), ] ) lowercase_ = OrderedDict( [ ("vision-encoder-decoder", "FlaxVisionEncoderDecoderModel"), ] ) lowercase_ = OrderedDict( [ # Model for Causal LM mapping ("bart", "FlaxBartForCausalLM"), ("bert", "FlaxBertForCausalLM"), ("big_bird", "FlaxBigBirdForCausalLM"), ("electra", "FlaxElectraForCausalLM"), ("gpt-sw3", "FlaxGPT2LMHeadModel"), ("gpt2", "FlaxGPT2LMHeadModel"), ("gpt_neo", "FlaxGPTNeoForCausalLM"), ("gptj", "FlaxGPTJForCausalLM"), ("opt", "FlaxOPTForCausalLM"), ("roberta", "FlaxRobertaForCausalLM"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForCausalLM"), ("xglm", "FlaxXGLMForCausalLM"), ("xlm-roberta", "FlaxXLMRobertaForCausalLM"), ] ) lowercase_ = OrderedDict( [ # Model for Sequence Classification mapping ("albert", "FlaxAlbertForSequenceClassification"), ("bart", "FlaxBartForSequenceClassification"), ("bert", "FlaxBertForSequenceClassification"), ("big_bird", "FlaxBigBirdForSequenceClassification"), ("distilbert", "FlaxDistilBertForSequenceClassification"), ("electra", "FlaxElectraForSequenceClassification"), ("mbart", "FlaxMBartForSequenceClassification"), ("roberta", "FlaxRobertaForSequenceClassification"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForSequenceClassification"), ("roformer", "FlaxRoFormerForSequenceClassification"), ("xlm-roberta", "FlaxXLMRobertaForSequenceClassification"), ] ) lowercase_ = OrderedDict( [ # Model for Question Answering mapping ("albert", "FlaxAlbertForQuestionAnswering"), ("bart", "FlaxBartForQuestionAnswering"), ("bert", "FlaxBertForQuestionAnswering"), ("big_bird", "FlaxBigBirdForQuestionAnswering"), ("distilbert", "FlaxDistilBertForQuestionAnswering"), ("electra", "FlaxElectraForQuestionAnswering"), ("mbart", "FlaxMBartForQuestionAnswering"), ("roberta", "FlaxRobertaForQuestionAnswering"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForQuestionAnswering"), ("roformer", "FlaxRoFormerForQuestionAnswering"), ("xlm-roberta", "FlaxXLMRobertaForQuestionAnswering"), ] ) lowercase_ = OrderedDict( [ # Model for Token Classification mapping ("albert", "FlaxAlbertForTokenClassification"), ("bert", "FlaxBertForTokenClassification"), ("big_bird", "FlaxBigBirdForTokenClassification"), ("distilbert", "FlaxDistilBertForTokenClassification"), ("electra", "FlaxElectraForTokenClassification"), ("roberta", "FlaxRobertaForTokenClassification"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForTokenClassification"), ("roformer", "FlaxRoFormerForTokenClassification"), ("xlm-roberta", "FlaxXLMRobertaForTokenClassification"), ] ) lowercase_ = OrderedDict( [ # Model for Multiple Choice mapping ("albert", "FlaxAlbertForMultipleChoice"), ("bert", "FlaxBertForMultipleChoice"), ("big_bird", "FlaxBigBirdForMultipleChoice"), ("distilbert", "FlaxDistilBertForMultipleChoice"), ("electra", "FlaxElectraForMultipleChoice"), ("roberta", "FlaxRobertaForMultipleChoice"), ("roberta-prelayernorm", "FlaxRobertaPreLayerNormForMultipleChoice"), ("roformer", "FlaxRoFormerForMultipleChoice"), ("xlm-roberta", "FlaxXLMRobertaForMultipleChoice"), ] ) lowercase_ = OrderedDict( [ ("bert", "FlaxBertForNextSentencePrediction"), ] ) lowercase_ = OrderedDict( [ ("speech-encoder-decoder", "FlaxSpeechEncoderDecoderModel"), ("whisper", "FlaxWhisperForConditionalGeneration"), ] ) lowercase_ = OrderedDict( [ ("whisper", "FlaxWhisperForAudioClassification"), ] ) lowercase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) lowercase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) lowercase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) lowercase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) lowercase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Dict = FLAX_MODEL_MAPPING lowercase_ = auto_class_update(FlaxAutoModel) class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : List[Any] = FLAX_MODEL_FOR_PRETRAINING_MAPPING lowercase_ = auto_class_update(FlaxAutoModelForPreTraining, head_doc="pretraining") class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : int = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING lowercase_ = auto_class_update(FlaxAutoModelForCausalLM, head_doc="causal language modeling") class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Optional[Any] = FLAX_MODEL_FOR_MASKED_LM_MAPPING lowercase_ = auto_class_update(FlaxAutoModelForMaskedLM, head_doc="masked language modeling") class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Optional[Any] = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING lowercase_ = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc="sequence-to-sequence language modeling", checkpoint_for_example="t5-base" ) class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Any = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING lowercase_ = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc="sequence classification" ) class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Optional[int] = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING lowercase_ = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc="question answering") class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Optional[int] = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING lowercase_ = auto_class_update( FlaxAutoModelForTokenClassification, head_doc="token classification" ) class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : str = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING lowercase_ = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc="multiple choice") class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Tuple = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING lowercase_ = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc="next sentence prediction" ) class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : str = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowercase_ = auto_class_update( FlaxAutoModelForImageClassification, head_doc="image classification" ) class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Optional[int] = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING lowercase_ = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc="vision-to-text modeling") class __A ( _BaseAutoModelClass ): '''simple docstring''' __lowerCamelCase : Any = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING lowercase_ = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc="sequence-to-sequence speech-to-text modeling" )
11
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
1
'''simple docstring''' # Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING, Dict, Optional import numpy as np import pyarrow as pa from .. import config from ..utils.logging import get_logger from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import jax import jaxlib lowercase_ = get_logger() lowercase_ = None class __A ( TensorFormatter[Mapping, 'jax.Array', Mapping] ): '''simple docstring''' def __init__(self , A=None , A=None , **A ) -> List[str]: """simple docstring""" super().__init__(features=A ) import jax from jaxlib.xla_client import Device if isinstance(A , A ): raise ValueError( f'''Expected {device} to be a `str` not {type(A )}, as `jaxlib.xla_extension.Device` ''' '''is not serializable neither with `pickle` nor with `dill`. Instead you can surround ''' '''the device with `str()` to get its string identifier that will be internally mapped ''' '''to the actual `jaxlib.xla_extension.Device`.''' ) _a = device if isinstance(A , A ) else str(jax.devices()[0] ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: _a = self._map_devices_to_str() if self.device not in list(DEVICE_MAPPING.keys() ): logger.warning( f'''Device with string identifier {self.device} not listed among the available ''' f'''devices: {list(DEVICE_MAPPING.keys() )}, so falling back to the default ''' f'''device: {str(jax.devices()[0] )}.''' ) _a = str(jax.devices()[0] ) _a = jnp_array_kwargs @staticmethod def a__ () -> Dict[str, "jaxlib.xla_extension.Device"]: """simple docstring""" import jax return {str(A ): device for device in jax.devices()} def a__ (self , A ) -> int: """simple docstring""" import jax import jax.numpy as jnp if isinstance(A , A ) and column: if all( isinstance(A , jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(A , axis=0 ) return column def a__ (self , A ) -> str: """simple docstring""" import jax import jax.numpy as jnp if isinstance(A , (str, bytes, type(A )) ): return value elif isinstance(A , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() _a = {} if isinstance(A , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: _a = {'''dtype''': jnp.intaa} else: _a = {'''dtype''': jnp.intaa} elif isinstance(A , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): _a = {'''dtype''': jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(A , PIL.Image.Image ): _a = np.asarray(A ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: _a = self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device] ): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(A , **{**default_dtype, **self.jnp_array_kwargs} ) def a__ (self , A ) -> Optional[int]: """simple docstring""" import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(A , torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(A , '''__array__''' ) and not isinstance(A , jax.Array ): _a = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(A , np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(A ) for substruct in data_struct] ) elif isinstance(A , (list, tuple) ): return self._consolidate([self.recursive_tensorize(A ) for substruct in data_struct] ) return self._tensorize(A ) def a__ (self , A ) -> Dict: """simple docstring""" return map_nested(self._recursive_tensorize , A , map_list=A ) def a__ (self , A ) -> Mapping: """simple docstring""" _a = self.numpy_arrow_extractor().extract_row(A ) _a = self.python_features_decoder.decode_row(A ) return self.recursive_tensorize(A ) def a__ (self , A ) -> "jax.Array": """simple docstring""" _a = self.numpy_arrow_extractor().extract_column(A ) _a = self.python_features_decoder.decode_column(A , pa_table.column_names[0] ) _a = self.recursive_tensorize(A ) _a = self._consolidate(A ) return column def a__ (self , A ) -> Mapping: """simple docstring""" _a = self.numpy_arrow_extractor().extract_batch(A ) _a = self.python_features_decoder.decode_batch(A ) _a = self.recursive_tensorize(A ) for column_name in batch: _a = self._consolidate(batch[column_name] ) return batch
11
'''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 __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''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 a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
1
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" if not nums: return 0 _a = nums[0] _a = 0 for num in nums[1:]: _a , _a = ( max_excluding + num, max(__A , __A), ) return max(__A , __A) if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
1
'''simple docstring''' from __future__ import annotations import unittest from transformers import 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 numpy import tensorflow as tf from transformers import ( TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, BertConfig, DPRConfig, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, ) class __A : '''simple docstring''' 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=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , A=0 , ) -> Union[str, Any]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope _a = projection_dim def a__ (self ) -> int: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: # follow test_modeling_tf_ctrl.py _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = BertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , ) _a = DPRConfig(projection_dim=self.projection_dim , **config.to_dict() ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self , A , A , A , A , A , A , A ) -> int: """simple docstring""" _a = TFDPRContextEncoder(config=A ) _a = model(A , attention_mask=A , token_type_ids=A ) _a = model(A , token_type_ids=A ) _a = model(A ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A ) -> Tuple: """simple docstring""" _a = TFDPRQuestionEncoder(config=A ) _a = model(A , attention_mask=A , token_type_ids=A ) _a = model(A , token_type_ids=A ) _a = model(A ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A ) -> List[str]: """simple docstring""" _a = TFDPRReader(config=A ) _a = model(A , attention_mask=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) ) self.parent.assertEqual(result.relevance_logits.shape , (self.batch_size,) ) def a__ (self ) -> str: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids} return config, inputs_dict @require_tf class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = ( ( TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, ) if is_tf_available() else () ) __lowerCamelCase : List[str] = {'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {} __lowerCamelCase : int = False __lowerCamelCase : Any = False __lowerCamelCase : Any = False __lowerCamelCase : List[Any] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> List[str]: """simple docstring""" _a = TFDPRModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Tuple: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_context_encoder(*A ) def a__ (self ) -> int: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_question_encoder(*A ) def a__ (self ) -> Dict: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_reader(*A ) @slow def a__ (self ) -> List[Any]: """simple docstring""" for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = TFDPRContextEncoder.from_pretrained(A ) self.assertIsNotNone(A ) for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = TFDPRContextEncoder.from_pretrained(A ) self.assertIsNotNone(A ) for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = TFDPRQuestionEncoder.from_pretrained(A ) self.assertIsNotNone(A ) for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = TFDPRReader.from_pretrained(A ) self.assertIsNotNone(A ) @require_tf class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Any: """simple docstring""" _a = TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base''' ) _a = tf.constant( [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]] ) # [CLS] hello, is my dog cute? [SEP] _a = model(A )[0] # embedding shape = (1, 768) # compare the actual values for a slice. _a = tf.constant( [ [ 0.03236253, 0.12753335, 0.16818509, 0.00279786, 0.3896933, 0.24264945, 0.2178971, -0.02335227, -0.08481959, -0.14324117, ] ] ) self.assertTrue(numpy.allclose(output[:, :10].numpy() , expected_slice.numpy() , atol=1E-4 ) )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _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 = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowercase_ = {"configuration_yolos": ["YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP", "YolosConfig", "YolosOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["YolosFeatureExtractor"] lowercase_ = ["YolosImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST", "YolosForObjectDetection", "YolosModel", "YolosPreTrainedModel", ] if TYPE_CHECKING: from .configuration_yolos import YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP, YolosConfig, YolosOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_yolos import YolosFeatureExtractor from .image_processing_yolos import YolosImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_yolos import ( YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST, YolosForObjectDetection, YolosModel, YolosPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "google/umt5-small": "https://huggingface.co/google/umt5-small/resolve/main/config.json", # See all umt5 models at https://huggingface.co/models?filter=umt5 } class __A ( A ): '''simple docstring''' __lowerCamelCase : Tuple = 'umt5' __lowerCamelCase : Optional[int] = ['past_key_values'] def __init__(self , A=250_112 , A=512 , A=64 , A=1_024 , A=8 , A=None , A=6 , A=32 , A=128 , A=0.1 , A=1E-6 , A=1.0 , A="gated-gelu" , A=True , A=True , A="T5Tokenizer" , A=True , A=0 , A=1 , A=0 , **A , ) -> Any: """simple docstring""" super().__init__( is_encoder_decoder=A , tokenizer_class=A , tie_word_embeddings=A , pad_token_id=A , eos_token_id=A , decoder_start_token_id=A , **A , ) _a = vocab_size _a = d_model _a = d_kv _a = d_ff _a = num_layers _a = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry _a = num_heads _a = relative_attention_num_buckets _a = relative_attention_max_distance _a = dropout_rate _a = layer_norm_epsilon _a = initializer_factor _a = feed_forward_proj _a = use_cache _a = self.feed_forward_proj.split('''-''' ) _a = act_info[-1] _a = act_info[0] == '''gated''' if len(A ) > 1 and act_info[0] != "gated" or len(A ) > 2: raise ValueError( f'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.''' '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ''' '''\'gated-gelu\' or \'relu\'''' ) if feed_forward_proj == "gated-gelu": _a = '''gelu_new''' @property def a__ (self ) -> Any: """simple docstring""" return self.d_model @property def a__ (self ) -> Tuple: """simple docstring""" return self.num_heads @property def a__ (self ) -> List[Any]: """simple docstring""" return self.num_layers class __A ( A ): '''simple docstring''' @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs def a__ (self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" _a = { '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''}, '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''}, } if self.use_past: _a = '''past_encoder_sequence + sequence''' _a = {0: '''batch'''} _a = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: _a = {0: '''batch''', 1: '''decoder_sequence'''} _a = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(A , direction='''inputs''' ) return common_inputs @property # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset def a__ (self ) -> int: """simple docstring""" return 13 @property def a__ (self ) -> float: """simple docstring""" return 5E-4
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' 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 lowercase_ = { # 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 __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self , A , A ) -> Optional[int]: """simple docstring""" _a = jnp.ones((batch_size, length) ) / length return scores def a__ (self ) -> Tuple: """simple docstring""" _a = None _a = 20 _a = self._get_uniform_logits(batch_size=2 , length=A ) # tweak scores to not be uniform anymore _a = scores.at[1, 5].set((1 / length) + 0.1 ) # peak, 1st batch _a = scores.at[1, 10].set((1 / length) - 0.4 ) # valley, 1st batch # compute softmax _a = jax.nn.softmax(A , axis=-1 ) _a = FlaxTemperatureLogitsWarper(temperature=0.5 ) _a = FlaxTemperatureLogitsWarper(temperature=1.3 ) _a = jax.nn.softmax(temp_dist_warper_sharper(A , scores.copy() , cur_len=A ) , axis=-1 ) _a = jax.nn.softmax(temp_dist_warper_smoother(A , scores.copy() , cur_len=A ) , axis=-1 ) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1E-3 ) ) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1E-3 ) ) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max() ) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min() ) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max() ) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min() ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = None _a = 10 _a = 2 # create ramp distribution _a = np.broadcast_to(np.arange(A )[None, :] , (batch_size, vocab_size) ).copy() _a = ramp_logits[1:, : vocab_size // 2] + vocab_size _a = FlaxTopKLogitsWarper(3 ) _a = top_k_warp(A , A , cur_len=A ) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0] ).tolist() , 7 * [True] + 3 * [False] ) self.assertListEqual(jnp.isinf(scores[1] ).tolist() , 2 * [True] + 3 * [False] + 5 * [True] ) # check special case _a = 5 _a = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3 ) _a = np.broadcast_to(np.arange(A )[None, :] , (batch_size, length) ).copy() _a = top_k_warp_safety_check(A , A , cur_len=A ) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist() , [2, 2] ) def a__ (self ) -> Tuple: """simple docstring""" _a = None _a = 10 _a = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) _a = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]] ) ) _a = FlaxTopPLogitsWarper(0.8 ) _a = np.exp(top_p_warp(A , A , cur_len=A ) ) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 _a = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]] ) self.assertTrue(np.allclose(A , A , atol=1E-3 ) ) # check edge cases with negative and extreme logits _a = np.broadcast_to(np.arange(A )[None, :] , (batch_size, vocab_size) ).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme _a = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept _a = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0 ) _a = top_p_warp(A , A , cur_len=A ) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1 ).tolist() , [3, 2] ) def a__ (self ) -> Tuple: """simple docstring""" _a = 20 _a = 4 _a = 0 _a = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=A ) # check that min length is applied at length 5 _a = ids_tensor((batch_size, 20) , vocab_size=20 ) _a = 5 _a = self._get_uniform_logits(A , A ) _a = min_dist_processor(A , A , cur_len=A ) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float('''inf''' )] ) # check that min length is not applied anymore at length 15 _a = self._get_uniform_logits(A , A ) _a = 15 _a = min_dist_processor(A , A , cur_len=A ) self.assertFalse(jnp.isinf(A ).any() ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = 20 _a = 4 _a = 0 _a = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=A ) # check that all scores are -inf except the bos_token_id score _a = ids_tensor((batch_size, 1) , vocab_size=20 ) _a = 1 _a = self._get_uniform_logits(A , A ) _a = logits_processor(A , A , cur_len=A ) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0] ) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 _a = 3 _a = self._get_uniform_logits(A , A ) _a = logits_processor(A , A , cur_len=A ) self.assertFalse(jnp.isinf(A ).any() ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = 20 _a = 4 _a = 0 _a = 5 _a = FlaxForcedEOSTokenLogitsProcessor(max_length=A , eos_token_id=A ) # check that all scores are -inf except the eos_token_id when max_length is reached _a = ids_tensor((batch_size, 4) , vocab_size=20 ) _a = 4 _a = self._get_uniform_logits(A , A ) _a = logits_processor(A , A , cur_len=A ) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0] ) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached _a = 3 _a = self._get_uniform_logits(A , A ) _a = logits_processor(A , A , cur_len=A ) self.assertFalse(jnp.isinf(A ).any() ) def a__ (self ) -> List[Any]: """simple docstring""" _a = 4 _a = 10 _a = 15 _a = 2 _a = 1 _a = 15 # dummy input_ids and scores _a = ids_tensor((batch_size, sequence_length) , A ) _a = input_ids.copy() _a = self._get_uniform_logits(A , A ) _a = scores.copy() # instantiate all dist processors _a = FlaxTemperatureLogitsWarper(temperature=0.5 ) _a = FlaxTopKLogitsWarper(3 ) _a = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors _a = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=A ) _a = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=A ) _a = FlaxForcedEOSTokenLogitsProcessor(max_length=A , eos_token_id=A ) _a = 10 # no processor list _a = temp_dist_warp(A , A , cur_len=A ) _a = top_k_warp(A , A , cur_len=A ) _a = top_p_warp(A , A , cur_len=A ) _a = min_dist_proc(A , A , cur_len=A ) _a = bos_dist_proc(A , A , cur_len=A ) _a = eos_dist_proc(A , A , cur_len=A ) # with processor list _a = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) _a = processor(A , A , cur_len=A ) # scores should be equal self.assertTrue(jnp.allclose(A , A , atol=1E-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = 4 _a = 10 _a = 15 _a = 2 _a = 1 _a = 15 # dummy input_ids and scores _a = ids_tensor((batch_size, sequence_length) , A ) _a = input_ids.copy() _a = self._get_uniform_logits(A , A ) _a = scores.copy() # instantiate all dist processors _a = FlaxTemperatureLogitsWarper(temperature=0.5 ) _a = FlaxTopKLogitsWarper(3 ) _a = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors _a = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=A ) _a = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=A ) _a = FlaxForcedEOSTokenLogitsProcessor(max_length=A , eos_token_id=A ) _a = 10 # no processor list def run_no_processor_list(A , A , A ): _a = temp_dist_warp(A , A , cur_len=A ) _a = top_k_warp(A , A , cur_len=A ) _a = top_p_warp(A , A , cur_len=A ) _a = min_dist_proc(A , A , cur_len=A ) _a = bos_dist_proc(A , A , cur_len=A ) _a = eos_dist_proc(A , A , cur_len=A ) return scores # with processor list def run_processor_list(A , A , A ): _a = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) _a = processor(A , A , cur_len=A ) return scores _a = jax.jit(A ) _a = jax.jit(A ) _a = jitted_run_no_processor_list(A , A , A ) _a = jitted_run_processor_list(A , A , A ) # scores should be equal self.assertTrue(jnp.allclose(A , A , atol=1E-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() )
11
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
1
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import ASTFeatureExtractor from transformers.testing_utils import require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin lowercase_ = random.Random() if is_torch_available(): import torch def lowerCAmelCase (__A , __A=1.0 , __A=None , __A=None): """simple docstring""" if rng is None: _a = global_rng _a = [] for batch_idx in range(shape[0]): values.append([]) for _ in range(shape[1]): values[-1].append(rng.random() * scale) return values class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=400 , A=2_000 , A=1 , A=0.0 , A=16_000 , A=True , A=True , ) -> Optional[Any]: """simple docstring""" _a = parent _a = batch_size _a = min_seq_length _a = max_seq_length _a = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _a = feature_size _a = padding_value _a = sampling_rate _a = return_attention_mask _a = do_normalize def a__ (self ) -> List[str]: """simple docstring""" return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def a__ (self , A=False , A=False ) -> Optional[Any]: """simple docstring""" def _flatten(A ): return list(itertools.chain(*A ) ) if equal_length: _a = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size _a = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: _a = [np.asarray(A ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : int = ASTFeatureExtractor def a__ (self ) -> List[str]: """simple docstring""" _a = ASTFeatureExtractionTester(self ) def a__ (self ) -> Dict: """simple docstring""" _a = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 _a = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] _a = [np.asarray(A ) for speech_input in speech_inputs] # Test not batched input _a = feat_extract(speech_inputs[0] , return_tensors='''np''' ).input_values _a = feat_extract(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(A , A , atol=1E-3 ) ) # Test batched _a = feat_extract(A , padding=A , return_tensors='''np''' ).input_values _a = feat_extract(A , padding=A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(A , A ): self.assertTrue(np.allclose(A , A , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. _a = [floats_list((1, x) )[0] for x in (800, 800, 800)] _a = np.asarray(A ) _a = feat_extract(A , return_tensors='''np''' ).input_values _a = feat_extract(A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(A , A ): self.assertTrue(np.allclose(A , A , atol=1E-3 ) ) @require_torch def a__ (self ) -> Union[str, Any]: """simple docstring""" import torch _a = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _a = np.random.rand(100 ).astype(np.floataa ) _a = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: _a = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) _a = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def a__ (self , A ) -> Optional[Any]: """simple docstring""" from datasets import load_dataset _a = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech _a = ds.sort('''id''' ).select(range(A ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] @require_torch def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = torch.tensor( [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776, -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133, -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936, -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869] ) # fmt: on _a = self._load_datasamples(1 ) _a = ASTFeatureExtractor() _a = feature_extractor(A , return_tensors='''pt''' ).input_values self.assertEquals(input_values.shape , (1, 1_024, 128) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , A , atol=1E-4 ) )
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): _a , _a = array[indexa], array[indexa] def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" if length > 1: _a = int(length / 2) for i in range(__A , low + middle): comp_and_swap(__A , __A , i + middle , __A) bitonic_merge(__A , __A , __A , __A) bitonic_merge(__A , low + middle , __A , __A) def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" if length > 1: _a = int(length / 2) bitonic_sort(__A , __A , __A , 1) bitonic_sort(__A , low + middle , __A , 0) bitonic_merge(__A , __A , __A , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by a comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] bitonic_sort(unsorted, 0, len(unsorted), 1) print("\nSorted array in ascending order is: ", end="") print(*unsorted, sep=", ") bitonic_merge(unsorted, 0, len(unsorted), 0) print("Sorted array in descending order is: ", end="") print(*unsorted, sep=", ")
11
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """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=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> str: """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=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def a__ (self ) -> Optional[int]: """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=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , )
11
1
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _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 = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
1
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A = None , __A = None): """simple docstring""" if start is None: _a = 0 if end is None: _a = len(__A) - 1 if start >= end: return _a = (start + end) // 2 slowsort(__A , __A , __A) slowsort(__A , mid + 1 , __A) if sequence[end] < sequence[mid]: _a , _a = sequence[mid], sequence[end] slowsort(__A , __A , end - 1) if __name__ == "__main__": from doctest import testmod testmod()
11
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # 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) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
1