code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging _lowercase : List[Any] = logging.get_logger(__name__) _lowercase : Tuple = { '''CarlCochet/trajectory-transformer-halfcheetah-medium-v2''': ( '''https://huggingface.co/CarlCochet/trajectory-transformer-halfcheetah-medium-v2/resolve/main/config.json''' ), # See all TrajectoryTransformer models at https://huggingface.co/models?filter=trajectory_transformer } class lowerCAmelCase__ ( lowerCamelCase_ ): lowerCAmelCase_ = '''trajectory_transformer''' lowerCAmelCase_ = ['''past_key_values'''] lowerCAmelCase_ = { '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self , __SCREAMING_SNAKE_CASE=1_00 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=2_49 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=17 , __SCREAMING_SNAKE_CASE=25 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=1_28 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.0_006 , __SCREAMING_SNAKE_CASE=5_12 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1E-1_2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=5_02_56 , __SCREAMING_SNAKE_CASE=5_02_56 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" lowercase_ : str = vocab_size lowercase_ : Any = action_weight lowercase_ : str = reward_weight lowercase_ : Union[str, Any] = value_weight lowercase_ : List[Any] = max_position_embeddings lowercase_ : Optional[int] = block_size lowercase_ : int = action_dim lowercase_ : List[str] = observation_dim lowercase_ : Union[str, Any] = transition_dim lowercase_ : Union[str, Any] = learning_rate lowercase_ : Optional[int] = n_layer lowercase_ : Optional[Any] = n_head lowercase_ : List[Any] = n_embd lowercase_ : List[str] = embd_pdrop lowercase_ : str = attn_pdrop lowercase_ : Optional[Any] = resid_pdrop lowercase_ : List[Any] = initializer_range lowercase_ : Dict = layer_norm_eps lowercase_ : Optional[int] = kaiming_initializer_range lowercase_ : str = use_cache super().__init__(pad_token_id=__snake_case , bos_token_id=__snake_case , eos_token_id=__snake_case , **__snake_case )
93
from abc import ABC, abstractmethod from argparse import ArgumentParser class snake_case_ (lowerCamelCase_ ): @staticmethod @abstractmethod def lowerCamelCase__( __snake_case :ArgumentParser ) -> Dict: raise NotImplementedError() @abstractmethod def lowerCamelCase__( self :Union[str, Any] ) -> Dict: raise NotImplementedError()
240
0
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, Pipeline, ZeroShotClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. __A = {"LayoutLMv2Config", "LayoutLMv3Config"} @is_pipeline_test class UpperCAmelCase (unittest.TestCase ): """simple docstring""" _UpperCAmelCase :Tuple = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING _UpperCAmelCase :Union[str, Any] = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: _UpperCAmelCase :Any = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: _UpperCAmelCase :Optional[Any] = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): lowercase__: Optional[int] = ZeroShotClassificationPipeline( model=_UpperCAmelCase , tokenizer=_UpperCAmelCase , candidate_labels=['''polics''', '''health'''] ) return classifier, ["Who are you voting for in 2020?", "My stomach hurts."] def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase ): lowercase__: Any = classifier('''Who are you voting for in 2020?''' , candidate_labels='''politics''' ) self.assertEqual(_UpperCAmelCase , {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase )]} ) # No kwarg lowercase__: int = classifier('''Who are you voting for in 2020?''' , ['''politics'''] ) self.assertEqual(_UpperCAmelCase , {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase )]} ) lowercase__: Dict = classifier('''Who are you voting for in 2020?''' , candidate_labels=['''politics'''] ) self.assertEqual(_UpperCAmelCase , {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase )]} ) lowercase__: List[Any] = classifier('''Who are you voting for in 2020?''' , candidate_labels='''politics, public health''' ) self.assertEqual( _UpperCAmelCase , {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs['''scores'''] ) ) , 1.0 ) lowercase__: Optional[Any] = classifier('''Who are you voting for in 2020?''' , candidate_labels=['''politics''', '''public health'''] ) self.assertEqual( _UpperCAmelCase , {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs['''scores'''] ) ) , 1.0 ) lowercase__: Tuple = classifier( '''Who are you voting for in 2020?''' , candidate_labels='''politics''' , hypothesis_template='''This text is about {}''' ) self.assertEqual(_UpperCAmelCase , {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase )]} ) # https://github.com/huggingface/transformers/issues/13846 lowercase__: Union[str, Any] = classifier(['''I am happy'''] , ['''positive''', '''negative'''] ) self.assertEqual( _UpperCAmelCase , [ {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} for i in range(1 ) ] , ) lowercase__: Any = classifier(['''I am happy''', '''I am sad'''] , ['''positive''', '''negative'''] ) self.assertEqual( _UpperCAmelCase , [ {'''sequence''': ANY(_UpperCAmelCase ), '''labels''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )], '''scores''': [ANY(_UpperCAmelCase ), ANY(_UpperCAmelCase )]} for i in range(2 ) ] , ) with self.assertRaises(_UpperCAmelCase ): classifier('''''' , candidate_labels='''politics''' ) with self.assertRaises(_UpperCAmelCase ): classifier(_UpperCAmelCase , candidate_labels='''politics''' ) with self.assertRaises(_UpperCAmelCase ): classifier('''Who are you voting for in 2020?''' , candidate_labels='''''' ) with self.assertRaises(_UpperCAmelCase ): classifier('''Who are you voting for in 2020?''' , candidate_labels=_UpperCAmelCase ) with self.assertRaises(_UpperCAmelCase ): classifier( '''Who are you voting for in 2020?''' , candidate_labels='''politics''' , hypothesis_template='''Not formatting template''' , ) with self.assertRaises(_UpperCAmelCase ): classifier( '''Who are you voting for in 2020?''' , candidate_labels='''politics''' , hypothesis_template=_UpperCAmelCase , ) self.run_entailment_id(_UpperCAmelCase ) def _snake_case ( self , _UpperCAmelCase ): lowercase__: List[Any] = zero_shot_classifier.model.config lowercase__: Optional[Any] = config.labelaid lowercase__: Dict = zero_shot_classifier.entailment_id lowercase__: Optional[int] = {'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2} self.assertEqual(zero_shot_classifier.entailment_id , -1 ) lowercase__: Union[str, Any] = {'''entailment''': 0, '''neutral''': 1, '''contradiction''': 2} self.assertEqual(zero_shot_classifier.entailment_id , 0 ) lowercase__: Union[str, Any] = {'''ENTAIL''': 0, '''NON-ENTAIL''': 1} self.assertEqual(zero_shot_classifier.entailment_id , 0 ) lowercase__: Optional[int] = {'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0} self.assertEqual(zero_shot_classifier.entailment_id , 2 ) lowercase__: List[str] = original_labelaid self.assertEqual(_UpperCAmelCase , zero_shot_classifier.entailment_id ) @require_torch def _snake_case ( self ): lowercase__: Tuple = pipeline( '''zero-shot-classification''' , model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''' , framework='''pt''' , ) # There was a regression in 4.10 for this # Adding a test so we don't make the mistake again. # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499 zero_shot_classifier( '''Who are you voting for in 2020?''' * 100 , candidate_labels=['''politics''', '''public health''', '''science'''] ) @require_torch def _snake_case ( self ): lowercase__: Tuple = pipeline( '''zero-shot-classification''' , model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''' , framework='''pt''' , ) lowercase__: List[Any] = zero_shot_classifier( '''Who are you voting for in 2020?''' , candidate_labels=['''politics''', '''public health''', '''science'''] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { '''sequence''': '''Who are you voting for in 2020?''', '''labels''': ['''science''', '''public health''', '''politics'''], '''scores''': [0.333, 0.333, 0.333], } , ) @require_tf def _snake_case ( self ): lowercase__: List[Any] = pipeline( '''zero-shot-classification''' , model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''' , framework='''tf''' , ) lowercase__: List[Any] = zero_shot_classifier( '''Who are you voting for in 2020?''' , candidate_labels=['''politics''', '''public health''', '''science'''] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { '''sequence''': '''Who are you voting for in 2020?''', '''labels''': ['''science''', '''public health''', '''politics'''], '''scores''': [0.333, 0.333, 0.333], } , ) @slow @require_torch def _snake_case ( self ): lowercase__: List[Any] = pipeline('''zero-shot-classification''' , model='''roberta-large-mnli''' , framework='''pt''' ) lowercase__: Optional[int] = zero_shot_classifier( '''Who are you voting for in 2020?''' , candidate_labels=['''politics''', '''public health''', '''science'''] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { '''sequence''': '''Who are you voting for in 2020?''', '''labels''': ['''politics''', '''public health''', '''science'''], '''scores''': [0.976, 0.015, 0.009], } , ) lowercase__: Optional[int] = zero_shot_classifier( '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks''' ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder''' ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based''' ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two''' ''' machine translation tasks show these models to be superior in quality while being more parallelizable''' ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014''' ''' English-to-German translation task, improving over the existing best results, including ensembles by''' ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new''' ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small''' ''' fraction of the training costs of the best models from the literature. We show that the Transformer''' ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with''' ''' large and limited training data.''' , candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision'''] , multi_label=_UpperCAmelCase , ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { '''sequence''': ( '''The dominant sequence transduction models are based on complex recurrent or convolutional neural''' ''' networks in an encoder-decoder configuration. The best performing models also connect the''' ''' encoder and decoder through an attention mechanism. We propose a new simple network''' ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence''' ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be''' ''' superior in quality while being more parallelizable and requiring significantly less time to''' ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,''' ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014''' ''' English-to-French translation task, our model establishes a new single-model state-of-the-art''' ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training''' ''' costs of the best models from the literature. We show that the Transformer generalizes well to''' ''' other tasks by applying it successfully to English constituency parsing both with large and''' ''' limited training data.''' ), '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''], '''scores''': [0.817, 0.713, 0.018, 0.018], } , ) @slow @require_tf def _snake_case ( self ): lowercase__: Union[str, Any] = pipeline('''zero-shot-classification''' , model='''roberta-large-mnli''' , framework='''tf''' ) lowercase__: List[Any] = zero_shot_classifier( '''Who are you voting for in 2020?''' , candidate_labels=['''politics''', '''public health''', '''science'''] ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { '''sequence''': '''Who are you voting for in 2020?''', '''labels''': ['''politics''', '''public health''', '''science'''], '''scores''': [0.976, 0.015, 0.009], } , ) lowercase__: Tuple = zero_shot_classifier( '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks''' ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder''' ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based''' ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two''' ''' machine translation tasks show these models to be superior in quality while being more parallelizable''' ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014''' ''' English-to-German translation task, improving over the existing best results, including ensembles by''' ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new''' ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small''' ''' fraction of the training costs of the best models from the literature. We show that the Transformer''' ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with''' ''' large and limited training data.''' , candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision'''] , multi_label=_UpperCAmelCase , ) self.assertEqual( nested_simplify(_UpperCAmelCase ) , { '''sequence''': ( '''The dominant sequence transduction models are based on complex recurrent or convolutional neural''' ''' networks in an encoder-decoder configuration. The best performing models also connect the''' ''' encoder and decoder through an attention mechanism. We propose a new simple network''' ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence''' ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be''' ''' superior in quality while being more parallelizable and requiring significantly less time to''' ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,''' ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014''' ''' English-to-French translation task, our model establishes a new single-model state-of-the-art''' ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training''' ''' costs of the best models from the literature. We show that the Transformer generalizes well to''' ''' other tasks by applying it successfully to English constituency parsing both with large and''' ''' limited training data.''' ), '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''], '''scores''': [0.817, 0.713, 0.018, 0.018], } , )
363
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() @skip_mps class UpperCAmelCase (_UpperCAmelCase ,_UpperCAmelCase ,unittest.TestCase ): """simple docstring""" _UpperCAmelCase :Optional[int] = StableDiffusionPanoramaPipeline _UpperCAmelCase :List[str] = TEXT_TO_IMAGE_PARAMS _UpperCAmelCase :str = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCAmelCase :Dict = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCAmelCase :List[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS def _snake_case ( self ): torch.manual_seed(0 ) lowercase__: Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) lowercase__: List[Any] = DDIMScheduler() torch.manual_seed(0 ) lowercase__: Tuple = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) lowercase__: Tuple = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) lowercase__: List[str] = CLIPTextModel(_UpperCAmelCase ) lowercase__: int = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) lowercase__: int = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def _snake_case ( self , _UpperCAmelCase , _UpperCAmelCase=0 ): lowercase__: int = torch.manual_seed(_UpperCAmelCase ) lowercase__: List[Any] = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, # Setting height and width to None to prevent OOMs on CPU. '''height''': None, '''width''': None, '''num_inference_steps''': 1, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def _snake_case ( self ): lowercase__: Optional[int] = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowercase__: List[str] = self.get_dummy_components() lowercase__: Union[str, Any] = StableDiffusionPanoramaPipeline(**_UpperCAmelCase ) lowercase__: int = sd_pipe.to(_UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: str = self.get_dummy_inputs(_UpperCAmelCase ) lowercase__: Any = sd_pipe(**_UpperCAmelCase ).images lowercase__: Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowercase__: List[str] = np.array([0.6_186, 0.5_374, 0.4_915, 0.4_135, 0.4_114, 0.4_563, 0.5_128, 0.4_977, 0.4_757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _snake_case ( self ): super().test_inference_batch_consistent(batch_sizes=[1, 2] ) def _snake_case ( self ): super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.2_5e-3 ) def _snake_case ( self ): lowercase__: Optional[Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowercase__: Union[str, Any] = self.get_dummy_components() lowercase__: str = StableDiffusionPanoramaPipeline(**_UpperCAmelCase ) lowercase__: str = sd_pipe.to(_UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: str = self.get_dummy_inputs(_UpperCAmelCase ) lowercase__: Union[str, Any] = '''french fries''' lowercase__: Union[str, Any] = sd_pipe(**_UpperCAmelCase , negative_prompt=_UpperCAmelCase ) lowercase__: Optional[Any] = output.images lowercase__: str = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowercase__: Optional[int] = np.array([0.6_187, 0.5_375, 0.4_915, 0.4_136, 0.4_114, 0.4_563, 0.5_128, 0.4_976, 0.4_757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _snake_case ( self ): lowercase__: Optional[Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowercase__: Union[str, Any] = self.get_dummy_components() lowercase__: Optional[Any] = StableDiffusionPanoramaPipeline(**_UpperCAmelCase ) lowercase__: str = sd_pipe.to(_UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: Optional[int] = self.get_dummy_inputs(_UpperCAmelCase ) lowercase__: Union[str, Any] = sd_pipe(**_UpperCAmelCase , view_batch_size=2 ) lowercase__: List[str] = output.images lowercase__: List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowercase__: List[Any] = np.array([0.6_187, 0.5_375, 0.4_915, 0.4_136, 0.4_114, 0.4_563, 0.5_128, 0.4_976, 0.4_757] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _snake_case ( self ): lowercase__: Optional[int] = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowercase__: int = self.get_dummy_components() lowercase__: List[str] = EulerAncestralDiscreteScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' ) lowercase__: Any = StableDiffusionPanoramaPipeline(**_UpperCAmelCase ) lowercase__: Any = sd_pipe.to(_UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: int = self.get_dummy_inputs(_UpperCAmelCase ) lowercase__: Dict = sd_pipe(**_UpperCAmelCase ).images lowercase__: Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowercase__: List[Any] = np.array([0.4_024, 0.6_510, 0.4_901, 0.5_378, 0.5_813, 0.5_622, 0.4_795, 0.4_467, 0.4_952] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _snake_case ( self ): lowercase__: int = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowercase__: List[Any] = self.get_dummy_components() lowercase__: Any = PNDMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , skip_prk_steps=_UpperCAmelCase ) lowercase__: Dict = StableDiffusionPanoramaPipeline(**_UpperCAmelCase ) lowercase__: int = sd_pipe.to(_UpperCAmelCase ) sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase ) lowercase__: Optional[int] = self.get_dummy_inputs(_UpperCAmelCase ) lowercase__: Dict = sd_pipe(**_UpperCAmelCase ).images lowercase__: str = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowercase__: List[Any] = np.array([0.6_391, 0.6_291, 0.4_861, 0.5_134, 0.5_552, 0.4_578, 0.5_032, 0.5_023, 0.4_539] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class UpperCAmelCase (unittest.TestCase ): """simple docstring""" def _snake_case ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def _snake_case ( self , _UpperCAmelCase=0 ): lowercase__: Union[str, Any] = torch.manual_seed(_UpperCAmelCase ) lowercase__: int = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def _snake_case ( self ): lowercase__: Any = '''stabilityai/stable-diffusion-2-base''' lowercase__: str = DDIMScheduler.from_pretrained(_UpperCAmelCase , subfolder='''scheduler''' ) lowercase__: Dict = StableDiffusionPanoramaPipeline.from_pretrained(_UpperCAmelCase , scheduler=_UpperCAmelCase , safety_checker=_UpperCAmelCase ) pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) pipe.enable_attention_slicing() lowercase__: Tuple = self.get_inputs() lowercase__: Optional[Any] = pipe(**_UpperCAmelCase ).images lowercase__: Optional[Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2048, 3) lowercase__: List[Any] = np.array( [ 0.36_968_392, 0.27_025_372, 0.32_446_766, 0.28_379_387, 0.36_363_274, 0.30_733_347, 0.27_100_027, 0.27_054_125, 0.25_536_096, ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-2 def _snake_case ( self ): lowercase__: int = StableDiffusionPanoramaPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-base''' , safety_checker=_UpperCAmelCase ) lowercase__: Tuple = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) pipe.enable_attention_slicing() lowercase__: List[str] = self.get_inputs() lowercase__: Dict = pipe(**_UpperCAmelCase ).images lowercase__: Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 2048, 3) lowercase__: List[Any] = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def _snake_case ( self ): lowercase__: int = 0 def callback_fn(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> None: lowercase__: List[str] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: lowercase__: Dict = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) lowercase__: Any = latents[0, -3:, -3:, -1] lowercase__: List[Any] = np.array( [ 0.18_681_869, 0.33_907_816, 0.5_361_276, 0.14_432_865, -0.02_856_611, -0.73_941_123, 0.23_397_987, 0.47_322_682, -0.37_823_164, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: lowercase__: Tuple = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 256) lowercase__: Optional[Any] = latents[0, -3:, -3:, -1] lowercase__: Any = np.array( [ 0.18_539_645, 0.33_987_248, 0.5_378_559, 0.14_437_142, -0.02_455_261, -0.7_338_317, 0.23_990_755, 0.47_356_272, -0.3_786_505, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 lowercase__: int = False lowercase__: str = '''stabilityai/stable-diffusion-2-base''' lowercase__: Union[str, Any] = DDIMScheduler.from_pretrained(_UpperCAmelCase , subfolder='''scheduler''' ) lowercase__: Tuple = StableDiffusionPanoramaPipeline.from_pretrained(_UpperCAmelCase , scheduler=_UpperCAmelCase , safety_checker=_UpperCAmelCase ) lowercase__: Optional[Any] = pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) pipe.enable_attention_slicing() lowercase__: Tuple = self.get_inputs() pipe(**_UpperCAmelCase , callback=_UpperCAmelCase , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def _snake_case ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() lowercase__: List[Any] = '''stabilityai/stable-diffusion-2-base''' lowercase__: Any = DDIMScheduler.from_pretrained(_UpperCAmelCase , subfolder='''scheduler''' ) lowercase__: int = StableDiffusionPanoramaPipeline.from_pretrained(_UpperCAmelCase , scheduler=_UpperCAmelCase , safety_checker=_UpperCAmelCase ) lowercase__: List[Any] = pipe.to(_UpperCAmelCase ) pipe.set_progress_bar_config(disable=_UpperCAmelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() lowercase__: Any = self.get_inputs() lowercase__: List[str] = pipe(**_UpperCAmelCase ) lowercase__: Optional[int] = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
2
0
"""simple docstring""" import logging import os import threading import time try: import warnings except ImportError: _SCREAMING_SNAKE_CASE : Tuple = None try: import msvcrt except ImportError: _SCREAMING_SNAKE_CASE : List[str] = None try: import fcntl except ImportError: _SCREAMING_SNAKE_CASE : Any = None # Backward compatibility # ------------------------------------------------ try: TimeoutError except NameError: _SCREAMING_SNAKE_CASE : Optional[int] = OSError # Data # ------------------------------------------------ _SCREAMING_SNAKE_CASE : int = [ '''Timeout''', '''BaseFileLock''', '''WindowsFileLock''', '''UnixFileLock''', '''SoftFileLock''', '''FileLock''', ] _SCREAMING_SNAKE_CASE : Optional[int] = '''3.0.12''' _SCREAMING_SNAKE_CASE : Dict = None def lowerCamelCase__ ( ) -> Optional[int]: global _logger lowerCamelCase_ = _logger or logging.getLogger(__name__ ) return _logger class a ( __snake_case ): def __init__( self : int , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: lowerCamelCase_ = lock_file return None def __str__( self : List[Any] ) -> Tuple: lowerCamelCase_ = F'''The file lock \'{self.lock_file}\' could not be acquired.''' return temp class a : def __init__( self : int , __SCREAMING_SNAKE_CASE : Tuple ) -> Any: lowerCamelCase_ = lock return None def __enter__( self : Any ) -> str: return self.lock def __exit__( self : List[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Any ) -> Union[str, Any]: self.lock.release() return None class a : def __init__( self : Dict , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Dict=-1 , __SCREAMING_SNAKE_CASE : Union[str, Any]=None ) -> int: lowerCamelCase_ = max_filename_length if max_filename_length is not None else 255 # Hash the filename if it's too long lowerCamelCase_ = self.hash_filename_if_too_long(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # The path to the lock file. lowerCamelCase_ = lock_file # The file descriptor for the *_lock_file* as it is returned by the # os.open() function. # This file lock is only NOT None, if the object currently holds the # lock. lowerCamelCase_ = None # The default timeout value. lowerCamelCase_ = timeout # We use this lock primarily for the lock counter. lowerCamelCase_ = threading.Lock() # The lock counter is used for implementing the nested locking # mechanism. Whenever the lock is acquired, the counter is increased and # the lock is only released, when this value is 0 again. lowerCamelCase_ = 0 return None @property def UpperCamelCase ( self : Union[str, Any] ) -> Dict: return self._lock_file @property def UpperCamelCase ( self : Any ) -> Dict: return self._timeout @timeout.setter def UpperCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : str ) -> List[str]: lowerCamelCase_ = float(__SCREAMING_SNAKE_CASE ) return None def UpperCamelCase ( self : List[Any] ) -> Optional[Any]: raise NotImplementedError() def UpperCamelCase ( self : Any ) -> str: raise NotImplementedError() @property def UpperCamelCase ( self : str ) -> Union[str, Any]: return self._lock_file_fd is not None def UpperCamelCase ( self : int , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , __SCREAMING_SNAKE_CASE : List[Any]=0.05 ) -> List[Any]: # Use the default timeout, if no timeout is provided. if timeout is None: lowerCamelCase_ = self.timeout # Increment the number right at the beginning. # We can still undo it, if something fails. with self._thread_lock: self._lock_counter += 1 lowerCamelCase_ = id(self ) lowerCamelCase_ = self._lock_file lowerCamelCase_ = time.time() try: while True: with self._thread_lock: if not self.is_locked: logger().debug(F'''Attempting to acquire lock {lock_id} on {lock_filename}''' ) self._acquire() if self.is_locked: logger().debug(F'''Lock {lock_id} acquired on {lock_filename}''' ) break elif timeout >= 0 and time.time() - start_time > timeout: logger().debug(F'''Timeout on acquiring lock {lock_id} on {lock_filename}''' ) raise Timeout(self._lock_file ) else: logger().debug( F'''Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ...''' ) time.sleep(__SCREAMING_SNAKE_CASE ) except: # noqa # Something did go wrong, so decrement the counter. with self._thread_lock: lowerCamelCase_ = max(0 , self._lock_counter - 1 ) raise return _Acquire_ReturnProxy(lock=self ) def UpperCamelCase ( self : str , __SCREAMING_SNAKE_CASE : Union[str, Any]=False ) -> Optional[Any]: with self._thread_lock: if self.is_locked: self._lock_counter -= 1 if self._lock_counter == 0 or force: lowerCamelCase_ = id(self ) lowerCamelCase_ = self._lock_file logger().debug(F'''Attempting to release lock {lock_id} on {lock_filename}''' ) self._release() lowerCamelCase_ = 0 logger().debug(F'''Lock {lock_id} released on {lock_filename}''' ) return None def __enter__( self : int ) -> Optional[Any]: self.acquire() return self def __exit__( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: self.release() return None def __del__( self : Tuple ) -> List[str]: self.release(force=__SCREAMING_SNAKE_CASE ) return None def UpperCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : int ) -> str: lowerCamelCase_ = os.path.basename(__SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ) > max_length and max_length > 0: lowerCamelCase_ = os.path.dirname(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = str(hash(__SCREAMING_SNAKE_CASE ) ) lowerCamelCase_ = filename[: max_length - len(__SCREAMING_SNAKE_CASE ) - 8] + '...' + hashed_filename + '.lock' return os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else: return path class a ( __snake_case ): def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[str]=-1 , __SCREAMING_SNAKE_CASE : Tuple=None ) -> List[str]: from .file_utils import relative_to_absolute_path super().__init__(__SCREAMING_SNAKE_CASE , timeout=__SCREAMING_SNAKE_CASE , max_filename_length=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = '\\\\?\\' + relative_to_absolute_path(self.lock_file ) def UpperCamelCase ( self : List[str] ) -> Any: lowerCamelCase_ = os.O_RDWR | os.O_CREAT | os.O_TRUNC try: lowerCamelCase_ = os.open(self._lock_file , __SCREAMING_SNAKE_CASE ) except OSError: pass else: try: msvcrt.locking(__SCREAMING_SNAKE_CASE , msvcrt.LK_NBLCK , 1 ) except OSError: os.close(__SCREAMING_SNAKE_CASE ) else: lowerCamelCase_ = fd return None def UpperCamelCase ( self : Optional[Any] ) -> List[str]: lowerCamelCase_ = self._lock_file_fd lowerCamelCase_ = None msvcrt.locking(__SCREAMING_SNAKE_CASE , msvcrt.LK_UNLCK , 1 ) os.close(__SCREAMING_SNAKE_CASE ) try: os.remove(self._lock_file ) # Probably another instance of the application # that acquired the file lock. except OSError: pass return None class a ( __snake_case ): def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Dict=-1 , __SCREAMING_SNAKE_CASE : Union[str, Any]=None ) -> List[Any]: lowerCamelCase_ = os.statvfs(os.path.dirname(__SCREAMING_SNAKE_CASE ) ).f_namemax super().__init__(__SCREAMING_SNAKE_CASE , timeout=__SCREAMING_SNAKE_CASE , max_filename_length=__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : Union[str, Any] ) -> List[str]: lowerCamelCase_ = os.O_RDWR | os.O_CREAT | os.O_TRUNC lowerCamelCase_ = os.open(self._lock_file , __SCREAMING_SNAKE_CASE ) try: fcntl.flock(__SCREAMING_SNAKE_CASE , fcntl.LOCK_EX | fcntl.LOCK_NB ) except OSError: os.close(__SCREAMING_SNAKE_CASE ) else: lowerCamelCase_ = fd return None def UpperCamelCase ( self : List[str] ) -> str: # Do not remove the lockfile: # # https://github.com/benediktschmitt/py-filelock/issues/31 # https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition lowerCamelCase_ = self._lock_file_fd lowerCamelCase_ = None fcntl.flock(__SCREAMING_SNAKE_CASE , fcntl.LOCK_UN ) os.close(__SCREAMING_SNAKE_CASE ) return None class a ( __snake_case ): def UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: lowerCamelCase_ = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC try: lowerCamelCase_ = os.open(self._lock_file , __SCREAMING_SNAKE_CASE ) except OSError: pass else: lowerCamelCase_ = fd return None def UpperCamelCase ( self : Dict ) -> Dict: os.close(self._lock_file_fd ) lowerCamelCase_ = None try: os.remove(self._lock_file ) # The file is already deleted and that's what we want. except OSError: pass return None _SCREAMING_SNAKE_CASE : int = None if msvcrt: _SCREAMING_SNAKE_CASE : Union[str, Any] = WindowsFileLock elif fcntl: _SCREAMING_SNAKE_CASE : Union[str, Any] = UnixFileLock else: _SCREAMING_SNAKE_CASE : Optional[int] = SoftFileLock if warnings is not None: warnings.warn('''only soft file lock is available''')
183
"""simple docstring""" import string def lowerCamelCase__ ( _lowerCamelCase : str ) -> None: for key in range(len(string.ascii_uppercase ) ): lowerCamelCase_ = '' for symbol in message: if symbol in string.ascii_uppercase: lowerCamelCase_ = string.ascii_uppercase.find(_lowerCamelCase ) lowerCamelCase_ = num - key if num < 0: lowerCamelCase_ = num + len(string.ascii_uppercase ) lowerCamelCase_ = translated + string.ascii_uppercase[num] else: lowerCamelCase_ = translated + symbol print(F'''Decryption using Key #{key}: {translated}''' ) def lowerCamelCase__ ( ) -> None: lowerCamelCase_ = input('Encrypted message: ' ) lowerCamelCase_ = message.upper() decrypt(_lowerCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() main()
183
1
'''simple docstring''' from timeit import timeit def _a( UpperCamelCase__ : int ): '''simple docstring''' if number < 0: raise ValueError('''the value of input must not be negative''' ) SCREAMING_SNAKE_CASE__ : Any =0 while number: number &= number - 1 result += 1 return result def _a( UpperCamelCase__ : int ): '''simple docstring''' if number < 0: raise ValueError('''the value of input must not be negative''' ) SCREAMING_SNAKE_CASE__ : str =0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def _a( ): '''simple docstring''' def do_benchmark(UpperCamelCase__ : int ) -> None: SCREAMING_SNAKE_CASE__ : int ='''import __main__ as z''' print(f"Benchmark when {number = }:" ) print(f"{get_set_bits_count_using_modulo_operator(__A ) = }" ) SCREAMING_SNAKE_CASE__ : Optional[Any] =timeit('''z.get_set_bits_count_using_modulo_operator(25)''', setup=__A ) print(f"timeit() runs in {timing} seconds" ) print(f"{get_set_bits_count_using_brian_kernighans_algorithm(__A ) = }" ) SCREAMING_SNAKE_CASE__ : Tuple =timeit( '''z.get_set_bits_count_using_brian_kernighans_algorithm(25)''', setup=__A, ) print(f"timeit() runs in {timing} seconds" ) for number in (2_5, 3_7, 5_8, 0): do_benchmark(__A ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
357
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __SCREAMING_SNAKE_CASE ( lowerCamelCase , lowerCamelCase , unittest.TestCase ): snake_case_ = StableDiffusionSAGPipeline snake_case_ = TEXT_TO_IMAGE_PARAMS snake_case_ = TEXT_TO_IMAGE_BATCH_PARAMS snake_case_ = TEXT_TO_IMAGE_IMAGE_PARAMS snake_case_ = TEXT_TO_IMAGE_IMAGE_PARAMS snake_case_ = False def __magic_name__ ( self : str ) -> Union[str, Any]: torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : Tuple =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) SCREAMING_SNAKE_CASE__ : Optional[Any] =DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__lowercase , set_alpha_to_one=__lowercase , ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : List[Any] =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : List[Any] =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) SCREAMING_SNAKE_CASE__ : Optional[Any] =CLIPTextModel(__lowercase ) SCREAMING_SNAKE_CASE__ : List[str] =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) SCREAMING_SNAKE_CASE__ : List[Any] ={ '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def __magic_name__ ( self : int , __lowercase : Union[str, Any] , __lowercase : Any=0 ) -> Optional[Any]: if str(__lowercase ).startswith('''mps''' ): SCREAMING_SNAKE_CASE__ : Optional[int] =torch.manual_seed(__lowercase ) else: SCREAMING_SNAKE_CASE__ : Optional[int] =torch.Generator(device=__lowercase ).manual_seed(__lowercase ) SCREAMING_SNAKE_CASE__ : Dict ={ '''prompt''': '''.''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 1.0, '''sag_scale''': 1.0, '''output_type''': '''numpy''', } return inputs def __magic_name__ ( self : int ) -> str: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __magic_name__ ( self : int ) -> Optional[int]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __magic_name__ ( self : int ) -> Optional[Any]: SCREAMING_SNAKE_CASE__ : Any =StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] =sag_pipe.to(__lowercase ) sag_pipe.set_progress_bar_config(disable=__lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] ='''.''' SCREAMING_SNAKE_CASE__ : Tuple =torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : List[Any] =sag_pipe( [prompt] , generator=__lowercase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) SCREAMING_SNAKE_CASE__ : int =output.images SCREAMING_SNAKE_CASE__ : int =image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) SCREAMING_SNAKE_CASE__ : str =np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2 def __magic_name__ ( self : List[Any] ) -> Any: SCREAMING_SNAKE_CASE__ : Tuple =StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) SCREAMING_SNAKE_CASE__ : List[Any] =sag_pipe.to(__lowercase ) sag_pipe.set_progress_bar_config(disable=__lowercase ) SCREAMING_SNAKE_CASE__ : List[str] ='''.''' SCREAMING_SNAKE_CASE__ : Optional[Any] =torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : Union[str, Any] =sag_pipe( [prompt] , generator=__lowercase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) SCREAMING_SNAKE_CASE__ : Tuple =output.images SCREAMING_SNAKE_CASE__ : Tuple =image[0, -3:, -3:, -1] assert image.shape == (1, 5_12, 5_12, 3) SCREAMING_SNAKE_CASE__ : List[Any] =np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2 def __magic_name__ ( self : str ) -> Any: SCREAMING_SNAKE_CASE__ : Union[str, Any] =StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) SCREAMING_SNAKE_CASE__ : Optional[int] =sag_pipe.to(__lowercase ) sag_pipe.set_progress_bar_config(disable=__lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] ='''.''' SCREAMING_SNAKE_CASE__ : Dict =torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : Tuple =sag_pipe( [prompt] , width=7_68 , height=5_12 , generator=__lowercase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' , ) SCREAMING_SNAKE_CASE__ : Any =output.images assert image.shape == (1, 5_12, 7_68, 3)
222
0
"""simple docstring""" from bisect import bisect from itertools import accumulate def __lowerCamelCase ( a_ : Tuple , a_ : List[Any] , a_ : int , a_ : Optional[int] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE :Dict = sorted(zip(a_ , a_ ) , key=lambda a_ : x[0] / x[1] , reverse=a_ ) __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :Optional[Any] = [i[0] for i in r], [i[1] for i in r] __SCREAMING_SNAKE_CASE :List[str] = list(accumulate(a_ ) ) __SCREAMING_SNAKE_CASE :int = bisect(a_ , a_ ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
191
"""simple docstring""" import argparse import os import torch from transformers import FlavaImageCodebook, FlavaImageCodebookConfig def __lowerCamelCase ( a_ : str , a_ : Dict , a_ : Any , a_ : str ) -> str: __SCREAMING_SNAKE_CASE :int = s.rsplit(a_ , a_ ) return new.join(a_ ) def __lowerCamelCase ( a_ : List[str] ) -> Dict: # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items() ) def __lowerCamelCase ( a_ : Optional[int] ) -> Any: __SCREAMING_SNAKE_CASE :Optional[int] = {} __SCREAMING_SNAKE_CASE :Union[str, Any] = ['''group_1''', '''group_2''', '''group_3''', '''group_4'''] for key, value in state_dict.items(): for group_key in group_keys: if group_key in key: __SCREAMING_SNAKE_CASE :Optional[Any] = key.replace(f'''{group_key}.''' , f'''{group_key}.group.''' ) if "res_path" in key: __SCREAMING_SNAKE_CASE :str = key.replace('''res_path.''' , '''res_path.path.''' ) if key.endswith('''.w''' ): __SCREAMING_SNAKE_CASE :List[Any] = rreplace(a_ , '''.w''' , '''.weight''' , 1 ) if key.endswith('''.b''' ): __SCREAMING_SNAKE_CASE :List[Any] = rreplace(a_ , '''.b''' , '''.bias''' , 1 ) __SCREAMING_SNAKE_CASE :Optional[Any] = value.float() return upgrade @torch.no_grad() def __lowerCamelCase ( a_ : List[Any] , a_ : Optional[int] , a_ : Optional[int]=None , a_ : Dict=True ) -> Union[str, Any]: from dall_e import Encoder __SCREAMING_SNAKE_CASE :int = Encoder() if os.path.exists(a_ ): __SCREAMING_SNAKE_CASE :Dict = torch.load(a_ ) else: __SCREAMING_SNAKE_CASE :List[str] = torch.hub.load_state_dict_from_url(a_ ) if isinstance(a_ , a_ ): __SCREAMING_SNAKE_CASE :List[str] = ckpt.state_dict() encoder.load_state_dict(a_ ) if config_path is not None: __SCREAMING_SNAKE_CASE :Any = FlavaImageCodebookConfig.from_pretrained(a_ ) else: __SCREAMING_SNAKE_CASE :Optional[int] = FlavaImageCodebookConfig() __SCREAMING_SNAKE_CASE :Tuple = FlavaImageCodebook(a_ ).eval() __SCREAMING_SNAKE_CASE :List[str] = encoder.state_dict() __SCREAMING_SNAKE_CASE :Union[str, Any] = upgrade_state_dict(a_ ) hf_model.load_state_dict(a_ ) __SCREAMING_SNAKE_CASE :Union[str, Any] = hf_model.state_dict() __SCREAMING_SNAKE_CASE :Union[str, Any] = count_parameters(a_ ) __SCREAMING_SNAKE_CASE :Any = count_parameters(a_ ) assert torch.allclose(a_ , a_ , atol=1e-3 ) if save_checkpoint: hf_model.save_pretrained(a_ ) else: return hf_state_dict if __name__ == "__main__": lowerCamelCase_ = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") lowerCamelCase_ = parser.parse_args() convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
191
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings from typing import List from unittest.mock import Mock import torch from torch.utils.data import DataLoader, IterableDataset, TensorDataset from accelerate.accelerator import Accelerator from accelerate.utils.dataclasses import DistributedType class __magic_name__ (__lowercase ): def __init__( self , _a ) -> Optional[Any]: lowerCAmelCase_ = data def __iter__( self ) -> int: for element in self.data: yield element def A(__a: Union[str, Any]=True ): lowerCAmelCase_ = Accelerator(even_batches=__a ) assert accelerator.num_processes == 2, "this script expects that two GPUs are available" return accelerator def A(__a: Accelerator , __a: int , __a: int , __a: bool = False ): if iterable: lowerCAmelCase_ = DummyIterableDataset(torch.as_tensor(range(__a ) ) ) else: lowerCAmelCase_ = TensorDataset(torch.as_tensor(range(__a ) ) ) lowerCAmelCase_ = DataLoader(__a , batch_size=__a ) lowerCAmelCase_ = accelerator.prepare(__a ) return dl def A(__a: Accelerator , __a: int , __a: int , __a: List[int] , __a: List[int] , ): lowerCAmelCase_ = create_dataloader(accelerator=__a , dataset_size=__a , batch_size=__a ) lowerCAmelCase_ = [len(batch[0] ) for batch in dl] if accelerator.process_index == 0: assert batch_sizes == process_0_expected_batch_sizes elif accelerator.process_index == 1: assert batch_sizes == process_1_expected_batch_sizes def A(): lowerCAmelCase_ = create_accelerator() # without padding, we would expect a different number of batches verify_dataloader_batch_sizes( __a , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1, 1] , ) # without padding, we would expect the same number of batches, but different sizes verify_dataloader_batch_sizes( __a , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 2] , ) def A(): lowerCAmelCase_ = create_accelerator(even_batches=__a ) verify_dataloader_batch_sizes( __a , dataset_size=3 , batch_size=1 , process_0_expected_batch_sizes=[1, 1] , process_1_expected_batch_sizes=[1] , ) verify_dataloader_batch_sizes( __a , dataset_size=7 , batch_size=2 , process_0_expected_batch_sizes=[2, 2] , process_1_expected_batch_sizes=[2, 1] , ) def A(): lowerCAmelCase_ = create_accelerator(even_batches=__a ) lowerCAmelCase_ = torch.nn.Linear(1 , 1 ) lowerCAmelCase_ = accelerator.prepare(__a ) lowerCAmelCase_ = create_dataloader(__a , dataset_size=3 , batch_size=1 ) lowerCAmelCase_ = [] with accelerator.join_uneven_inputs([ddp_model] ): for batch_idx, batch in enumerate(__a ): lowerCAmelCase_ = ddp_model(batch[0].float() ) lowerCAmelCase_ = output.sum() loss.backward() batch_idxs.append(__a ) accelerator.wait_for_everyone() if accelerator.process_index == 0: assert batch_idxs == [0, 1] elif accelerator.process_index == 1: assert batch_idxs == [0] def A(__a: List[str] ): with warnings.catch_warnings(record=__a ) as w: with accelerator.join_uneven_inputs([Mock()] ): pass assert issubclass(w[-1].category , __a ) assert "only supported for multi-GPU" in str(w[-1].message ) def A(): lowerCAmelCase_ = True lowerCAmelCase_ = False lowerCAmelCase_ = create_accelerator(even_batches=__a ) lowerCAmelCase_ = torch.nn.Linear(1 , 1 ) lowerCAmelCase_ = accelerator.prepare(__a ) lowerCAmelCase_ = create_dataloader(__a , dataset_size=3 , batch_size=1 ) lowerCAmelCase_ = create_dataloader(__a , dataset_size=3 , batch_size=1 ) with accelerator.join_uneven_inputs([ddp_model] , even_batches=__a ): lowerCAmelCase_ = train_dl.batch_sampler.even_batches lowerCAmelCase_ = valid_dl.batch_sampler.even_batches assert train_dl_overridden_value == overridden_even_batches assert valid_dl_overridden_value == overridden_even_batches assert train_dl.batch_sampler.even_batches == default_even_batches assert valid_dl.batch_sampler.even_batches == default_even_batches def A(): lowerCAmelCase_ = True lowerCAmelCase_ = False lowerCAmelCase_ = create_accelerator(even_batches=__a ) lowerCAmelCase_ = torch.nn.Linear(1 , 1 ) lowerCAmelCase_ = accelerator.prepare(__a ) create_dataloader(__a , dataset_size=3 , batch_size=1 , iterable=__a ) lowerCAmelCase_ = create_dataloader(__a , dataset_size=3 , batch_size=1 ) with warnings.catch_warnings(): warnings.filterwarnings("ignore" ) try: with accelerator.join_uneven_inputs([ddp_model] , even_batches=__a ): lowerCAmelCase_ = batch_dl.batch_sampler.even_batches except AttributeError: # ensure attribute error is not raised when processing iterable dl raise AssertionError assert batch_dl_overridden_value == overridden_even_batches assert batch_dl.batch_sampler.even_batches == default_even_batches def A(): lowerCAmelCase_ = create_accelerator() lowerCAmelCase_ = torch.nn.Linear(1 , 1 ) lowerCAmelCase_ = accelerator.prepare(__a ) create_dataloader(__a , dataset_size=3 , batch_size=1 , iterable=__a ) with warnings.catch_warnings(record=__a ) as w: with accelerator.join_uneven_inputs([ddp_model] , even_batches=__a ): pass assert issubclass(w[-1].category , __a ) assert "only supported for map-style datasets" in str(w[-1].message ) def A(): lowerCAmelCase_ = create_accelerator() accelerator.print("Test that even_batches variable ensures uniform batches across processes" ) test_default_ensures_even_batch_sizes() accelerator.print("Run tests with even_batches disabled" ) test_can_disable_even_batches() accelerator.print("Test joining uneven inputs" ) test_can_join_uneven_inputs() accelerator.print("Test overriding even_batches when joining uneven inputs" ) test_join_can_override_even_batches() accelerator.print("Test overriding even_batches for mixed dataloader types" ) test_join_can_override_for_mixed_type_dataloaders() accelerator.print("Test overriding even_batches raises a warning for iterable dataloaders" ) test_join_raises_warning_for_iterable_when_overriding_even_batches() accelerator.print("Test join with non DDP distributed raises warning" ) lowerCAmelCase_ = accelerator.state.distributed_type lowerCAmelCase_ = DistributedType.FSDP test_join_raises_warning_for_non_ddp_distributed(__a ) lowerCAmelCase_ = original_state if __name__ == "__main__": main()
22
import datasets lowerCamelCase__ = '''\ @InProceedings{conneau2018xnli, author = "Conneau, Alexis and Rinott, Ruty and Lample, Guillaume and Williams, Adina and Bowman, Samuel R. and Schwenk, Holger and Stoyanov, Veselin", title = "XNLI: Evaluating Cross-lingual Sentence Representations", booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing", year = "2018", publisher = "Association for Computational Linguistics", location = "Brussels, Belgium", } ''' lowerCamelCase__ = '''\ XNLI is a subset of a few thousand examples from MNLI which has been translated into a 14 different languages (some low-ish resource). As with MNLI, the goal is to predict textual entailment (does sentence A imply/contradict/neither sentence B) and is a classification task (given two sentences, predict one of three labels). ''' lowerCamelCase__ = ''' Computes XNLI score which is just simple accuracy. Args: predictions: Predicted labels. references: Ground truth labels. Returns: \'accuracy\': accuracy Examples: >>> predictions = [0, 1] >>> references = [0, 1] >>> xnli_metric = datasets.load_metric("xnli") >>> results = xnli_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} ''' def A(__a: Dict , __a: Union[str, Any] ): return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ (datasets.Metric ): def __a ( self ) -> Tuple: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("int64" if self.config_name != "sts-b" else "float32" ), "references": datasets.Value("int64" if self.config_name != "sts-b" else "float32" ), } ) , codebase_urls=[] , reference_urls=[] , format="numpy" , ) def __a ( self , _a , _a ) -> List[str]: return {"accuracy": simple_accuracy(_a , _a )}
22
1
from __future__ import annotations import copy import tempfile import unittest from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available from transformers.testing_utils import ( DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tensorflow_probability, require_tf, slow, ) from ..bert.test_modeling_bert import BertModelTester if is_tf_available(): from transformers import ( TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelForTableQuestionAnswering, TFAutoModelForTokenClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFFunnelBaseModel, TFFunnelModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, TFTapasForQuestionAnswering, ) from transformers.models.auto.modeling_tf_auto import ( TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_MAPPING, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST class __lowerCAmelCase ( UpperCamelCase__): _lowercase : str = """new-model""" if is_tf_available(): class __lowerCAmelCase ( UpperCamelCase__): _lowercase : Optional[int] = NewModelConfig @require_tf class __lowerCAmelCase ( unittest.TestCase): @slow def _lowercase ( self ) -> List[Any]: '''simple docstring''' a__ : str ="bert-base-cased" a__ : Any =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Optional[Any] =TFAutoModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowercase ( self ) -> int: '''simple docstring''' a__ : List[Any] ="bert-base-cased" a__ : Dict =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Tuple =TFAutoModelForPreTraining.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowercase ( self ) -> Dict: '''simple docstring''' for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Any =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Optional[int] =TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase__ ) a__ , a__ : Union[str, Any] =TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowercase ( self ) -> Dict: '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : str =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : List[str] =TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowercase ( self ) -> Any: '''simple docstring''' for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : Optional[int] =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Optional[int] =TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase__ ) a__ , a__ : Dict =TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowercase ( self ) -> Dict: '''simple docstring''' for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ : int =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Any =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase__ ) a__ , a__ : List[Any] =TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' for model_name in ["bert-base-uncased"]: a__ : Any =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : List[Any] =TFAutoModelForSequenceClassification.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowercase ( self ) -> Optional[int]: '''simple docstring''' for model_name in ["bert-base-uncased"]: a__ : Any =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Dict =TFAutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) @slow @require_tensorflow_probability def _lowercase ( self ) -> Union[str, Any]: '''simple docstring''' for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]: a__ : Any =AutoConfig.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Union[str, Any] =TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCAmelCase__ ) a__ , a__ : List[str] =TFAutoModelForTableQuestionAnswering.from_pretrained( lowerCAmelCase__ , output_loading_info=lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' a__ : Optional[int] =TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(model.num_parameters() , 1_4_4_1_0 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase__ ) , 1_4_4_1_0 ) def _lowercase ( self ) -> List[Any]: '''simple docstring''' a__ : int =TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(model.num_parameters() , 1_4_4_1_0 ) self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase__ ) , 1_4_4_1_0 ) def _lowercase ( self ) -> Optional[Any]: '''simple docstring''' a__ : str =TFAutoModel.from_pretrained("sgugger/funnel-random-tiny" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) a__ : Union[str, Any] =copy.deepcopy(model.config ) a__ : Union[str, Any] =["FunnelBaseModel"] a__ : Dict =TFAutoModel.from_config(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCAmelCase__ ) a__ : List[str] =TFAutoModel.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) def _lowercase ( self ) -> int: '''simple docstring''' try: AutoConfig.register("new-model" , lowerCAmelCase__ ) a__ : Tuple =[ TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSequenceClassification, TFAutoModelForTokenClassification, ] for auto_class in auto_classes: with self.subTest(auto_class.__name__ ): # Wrong config class will raise an error with self.assertRaises(lowerCAmelCase__ ): auto_class.register(lowerCAmelCase__ , lowerCAmelCase__ ) auto_class.register(lowerCAmelCase__ , lowerCAmelCase__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(lowerCAmelCase__ ): auto_class.register(lowerCAmelCase__ , lowerCAmelCase__ ) # Now that the config is registered, it can be used as any other config with the auto-API a__ : List[str] =BertModelTester(self ).get_config() a__ : Dict =NewModelConfig(**tiny_config.to_dict() ) a__ : Optional[int] =auto_class.from_config(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(lowerCAmelCase__ ) a__ : int =auto_class.from_pretrained(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"] for mapping in ( TF_MODEL_MAPPING, TF_MODEL_FOR_PRETRAINING_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, ): if NewModelConfig in mapping._extra_content: del mapping._extra_content[NewModelConfig] def _lowercase ( self ) -> List[Any]: '''simple docstring''' with self.assertRaisesRegex( lowerCAmelCase__ , "bert-base is not a local folder and is not a valid model identifier" ): a__ : Dict =TFAutoModel.from_pretrained("bert-base" ) def _lowercase ( self ) -> Any: '''simple docstring''' with self.assertRaisesRegex( lowerCAmelCase__ , r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): a__ : int =TFAutoModel.from_pretrained(lowerCAmelCase__ , revision="aaaaaa" ) def _lowercase ( self ) -> Tuple: '''simple docstring''' with self.assertRaisesRegex( lowerCAmelCase__ , "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin" , ): a__ : Optional[int] =TFAutoModel.from_pretrained("hf-internal-testing/config-no-model" ) def _lowercase ( self ) -> Tuple: '''simple docstring''' with self.assertRaisesRegex(lowerCAmelCase__ , "Use `from_pt=True` to load this model" ): a__ : Optional[Any] =TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only" ) def _lowercase ( self ) -> Dict: '''simple docstring''' a__ : Any =TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert" ) with RequestCounter() as counter: a__ : List[str] =TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert" ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 ) # With a sharded checkpoint a__ : Any =TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded" ) with RequestCounter() as counter: a__ : Dict =TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded" ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
95
import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DPMSolverMultistepScheduler, TextToVideoSDPipeline, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class _UpperCAmelCase ( A__ ,unittest.TestCase ): """simple docstring""" lowercase__ = TextToVideoSDPipeline lowercase__ = TEXT_TO_IMAGE_PARAMS lowercase__ = TEXT_TO_IMAGE_BATCH_PARAMS # No `output_type`. lowercase__ = frozenset( [ """num_inference_steps""", """generator""", """latents""", """return_dict""", """callback""", """callback_steps""", ] ) def lowercase__ ( self : str ): '''simple docstring''' torch.manual_seed(0 ) lowercase__ = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64), layers_per_block=2, sample_size=32, in_channels=4, out_channels=4, down_block_types=('''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''DownBlock3D'''), up_block_types=('''UpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D'''), cross_attention_dim=32, attention_head_dim=4, ) lowercase__ = DDIMScheduler( beta_start=0.00085, beta_end=0.012, beta_schedule='''scaled_linear''', clip_sample=lowerCamelCase, set_alpha_to_one=lowerCamelCase, ) torch.manual_seed(0 ) lowercase__ = AutoencoderKL( block_out_channels=[32, 64], in_channels=3, out_channels=3, down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], latent_channels=4, sample_size=128, ) torch.manual_seed(0 ) lowercase__ = CLIPTextConfig( bos_token_id=0, eos_token_id=2, hidden_size=32, intermediate_size=37, layer_norm_eps=1E-05, num_attention_heads=4, num_hidden_layers=5, pad_token_id=1, vocab_size=1_000, hidden_act='''gelu''', projection_dim=512, ) lowercase__ = CLIPTextModel(lowerCamelCase ) lowercase__ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) lowercase__ = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, } return components def lowercase__ ( self : int, lowerCamelCase : Union[str, Any], lowerCamelCase : int=0 ): '''simple docstring''' if str(lowerCamelCase ).startswith('''mps''' ): lowercase__ = torch.manual_seed(lowerCamelCase ) else: lowercase__ = torch.Generator(device=lowerCamelCase ).manual_seed(lowerCamelCase ) lowercase__ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''pt''', } return inputs def lowercase__ ( self : Union[str, Any] ): '''simple docstring''' lowercase__ = '''cpu''' # ensure determinism for the device-dependent torch.Generator lowercase__ = self.get_dummy_components() lowercase__ = TextToVideoSDPipeline(**lowerCamelCase ) lowercase__ = sd_pipe.to(lowerCamelCase ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase ) lowercase__ = self.get_dummy_inputs(lowerCamelCase ) lowercase__ = '''np''' lowercase__ = sd_pipe(**lowerCamelCase ).frames lowercase__ = frames[0][-3:, -3:, -1] assert frames[0].shape == (64, 64, 3) lowercase__ = np.array([158.0, 160.0, 153.0, 125.0, 100.0, 121.0, 111.0, 93.0, 113.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def lowercase__ ( self : str ): '''simple docstring''' self._test_attention_slicing_forward_pass(test_mean_pixel_difference=lowerCamelCase, expected_max_diff=3E-3 ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available(), reason='''XFormers attention is only available with CUDA and `xformers` installed''', ) def lowercase__ ( self : Optional[int] ): '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowerCamelCase, expected_max_diff=1E-2 ) @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''' ) def lowercase__ ( self : Optional[int] ): '''simple docstring''' pass @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''' ) def lowercase__ ( self : Optional[Any] ): '''simple docstring''' pass @unittest.skip(reason='''`num_images_per_prompt` argument is not supported for this pipeline.''' ) def lowercase__ ( self : int ): '''simple docstring''' pass def lowercase__ ( self : List[Any] ): '''simple docstring''' return super().test_progress_bar() @slow @skip_mps class _UpperCAmelCase ( unittest.TestCase ): """simple docstring""" def lowercase__ ( self : int ): '''simple docstring''' lowercase__ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy''' ) lowercase__ = TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''' ) lowercase__ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) lowercase__ = pipe.to('''cuda''' ) lowercase__ = '''Spiderman is surfing''' lowercase__ = torch.Generator(device='''cpu''' ).manual_seed(0 ) lowercase__ = pipe(lowerCamelCase, generator=lowerCamelCase, num_inference_steps=25, output_type='''pt''' ).frames lowercase__ = video_frames.cpu().numpy() assert np.abs(expected_video - video ).mean() < 5E-2 def lowercase__ ( self : int ): '''simple docstring''' lowercase__ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy''' ) lowercase__ = TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''' ) lowercase__ = pipe.to('''cuda''' ) lowercase__ = '''Spiderman is surfing''' lowercase__ = torch.Generator(device='''cpu''' ).manual_seed(0 ) lowercase__ = pipe(lowerCamelCase, generator=lowerCamelCase, num_inference_steps=2, output_type='''pt''' ).frames lowercase__ = video_frames.cpu().numpy() assert np.abs(expected_video - video ).mean() < 5E-2
207
0
"""simple docstring""" import io import math from typing import Dict, Optional, Union import numpy as np from huggingface_hub import hf_hub_download from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, get_image_size, infer_channel_dimension_format, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_vision_available, logging from ...utils.import_utils import requires_backends if is_vision_available(): import textwrap from PIL import Image, ImageDraw, ImageFont if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: _UpperCAmelCase = False _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = 'ybelkada/fonts' def lowerCAmelCase_ ( ) -> Dict: if is_torch_available() and not is_torch_greater_or_equal_than_1_11: raise ImportError( F'''You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use ''' "Pix2StructImageProcessor. Please upgrade torch." ) def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) -> List[Any]: requires_backends(UpperCamelCase_ , ["torch"] ) _check_torch_version() UpperCamelCase_ = image_tensor.unsqueeze(0 ) UpperCamelCase_ = torch.nn.functional.unfold(UpperCamelCase_ , (patch_height, patch_width) , stride=(patch_height, patch_width) ) UpperCamelCase_ = patches.reshape(image_tensor.size(0 ) , image_tensor.size(1 ) , UpperCamelCase_ , UpperCamelCase_ , -1 ) UpperCamelCase_ = patches.permute(0 , 4 , 2 , 3 , 1 ).reshape( image_tensor.size(2 ) // patch_height , image_tensor.size(3 ) // patch_width , image_tensor.size(1 ) * patch_height * patch_width , ) return patches.unsqueeze(0 ) def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ = 36 , UpperCamelCase_ = "black" , UpperCamelCase_ = "white" , UpperCamelCase_ = 5 , UpperCamelCase_ = 5 , UpperCamelCase_ = 5 , UpperCamelCase_ = 5 , UpperCamelCase_ = None , UpperCamelCase_ = None , ) -> Image.Image: requires_backends(UpperCamelCase_ , "vision" ) # Add new lines so that each line is no more than 80 characters. UpperCamelCase_ = textwrap.TextWrapper(width=80 ) UpperCamelCase_ = wrapper.wrap(text=UpperCamelCase_ ) UpperCamelCase_ = "\n".join(UpperCamelCase_ ) if font_bytes is not None and font_path is None: UpperCamelCase_ = io.BytesIO(UpperCamelCase_ ) elif font_path is not None: UpperCamelCase_ = font_path else: UpperCamelCase_ = hf_hub_download(UpperCamelCase_ , "Arial.TTF" ) UpperCamelCase_ = ImageFont.truetype(UpperCamelCase_ , encoding="UTF-8" , size=UpperCamelCase_ ) # Use a temporary canvas to determine the width and height in pixels when # rendering the text. UpperCamelCase_ = ImageDraw.Draw(Image.new("RGB" , (1, 1) , UpperCamelCase_ ) ) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = temp_draw.textbbox((0, 0) , UpperCamelCase_ , UpperCamelCase_ ) # Create the actual image with a bit of padding around the text. UpperCamelCase_ = text_width + left_padding + right_padding UpperCamelCase_ = text_height + top_padding + bottom_padding UpperCamelCase_ = Image.new("RGB" , (image_width, image_height) , UpperCamelCase_ ) UpperCamelCase_ = ImageDraw.Draw(UpperCamelCase_ ) draw.text(xy=(left_padding, top_padding) , text=UpperCamelCase_ , fill=UpperCamelCase_ , font=UpperCamelCase_ ) return image def lowerCAmelCase_ ( UpperCamelCase_ , UpperCamelCase_ , **UpperCamelCase_ ) -> Union[str, Any]: requires_backends(UpperCamelCase_ , "vision" ) # Convert to PIL image if necessary UpperCamelCase_ = to_pil_image(UpperCamelCase_ ) UpperCamelCase_ = render_text(UpperCamelCase_ , **UpperCamelCase_ ) UpperCamelCase_ = max(header_image.width , image.width ) UpperCamelCase_ = int(image.height * (new_width / image.width) ) UpperCamelCase_ = int(header_image.height * (new_width / header_image.width) ) UpperCamelCase_ = Image.new("RGB" , (new_width, new_height + new_header_height) , "white" ) new_image.paste(header_image.resize((new_width, new_header_height) ) , (0, 0) ) new_image.paste(image.resize((new_width, new_height) ) , (0, new_header_height) ) # Convert back to the original framework if necessary UpperCamelCase_ = to_numpy_array(UpperCamelCase_ ) if infer_channel_dimension_format(UpperCamelCase_ ) == ChannelDimension.LAST: UpperCamelCase_ = to_channel_dimension_format(UpperCamelCase_ , ChannelDimension.LAST ) return new_image class _UpperCamelCase ( lowerCAmelCase_ ): _UpperCamelCase : str = ['''flattened_patches'''] def __init__( self: List[Any] , _SCREAMING_SNAKE_CASE: bool = True , _SCREAMING_SNAKE_CASE: bool = True , _SCREAMING_SNAKE_CASE: Dict[str, int] = None , _SCREAMING_SNAKE_CASE: int = 2048 , _SCREAMING_SNAKE_CASE: bool = False , **_SCREAMING_SNAKE_CASE: Optional[Any] , ) -> None: """simple docstring""" super().__init__(**_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = patch_size if patch_size is not None else {"height": 16, "width": 16} UpperCamelCase_ = do_normalize UpperCamelCase_ = do_convert_rgb UpperCamelCase_ = max_patches UpperCamelCase_ = is_vqa def lowercase ( self: Dict , _SCREAMING_SNAKE_CASE: np.ndarray , _SCREAMING_SNAKE_CASE: int , _SCREAMING_SNAKE_CASE: dict , **_SCREAMING_SNAKE_CASE: Union[str, Any] ) -> np.ndarray: """simple docstring""" requires_backends(self.extract_flattened_patches , "torch" ) _check_torch_version() # convert to torch UpperCamelCase_ = to_channel_dimension_format(_SCREAMING_SNAKE_CASE , ChannelDimension.FIRST ) UpperCamelCase_ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ , UpperCamelCase_ = patch_size["height"], patch_size["width"] UpperCamelCase_ , UpperCamelCase_ = get_image_size(_SCREAMING_SNAKE_CASE ) # maximize scale s.t. UpperCamelCase_ = math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width) ) UpperCamelCase_ = max(min(math.floor(scale * image_height / patch_height ) , _SCREAMING_SNAKE_CASE ) , 1 ) UpperCamelCase_ = max(min(math.floor(scale * image_width / patch_width ) , _SCREAMING_SNAKE_CASE ) , 1 ) UpperCamelCase_ = max(num_feasible_rows * patch_height , 1 ) UpperCamelCase_ = max(num_feasible_cols * patch_width , 1 ) UpperCamelCase_ = torch.nn.functional.interpolate( image.unsqueeze(0 ) , size=(resized_height, resized_width) , mode="bilinear" , align_corners=_SCREAMING_SNAKE_CASE , antialias=_SCREAMING_SNAKE_CASE , ).squeeze(0 ) # [1, rows, columns, patch_height * patch_width * image_channels] UpperCamelCase_ = torch_extract_patches(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = patches.shape UpperCamelCase_ = patches_shape[1] UpperCamelCase_ = patches_shape[2] UpperCamelCase_ = patches_shape[3] # [rows * columns, patch_height * patch_width * image_channels] UpperCamelCase_ = patches.reshape([rows * columns, depth] ) # [rows * columns, 1] UpperCamelCase_ = torch.arange(_SCREAMING_SNAKE_CASE ).reshape([rows, 1] ).repeat(1 , _SCREAMING_SNAKE_CASE ).reshape([rows * columns, 1] ) UpperCamelCase_ = torch.arange(_SCREAMING_SNAKE_CASE ).reshape([1, columns] ).repeat(_SCREAMING_SNAKE_CASE , 1 ).reshape([rows * columns, 1] ) # Offset by 1 so the ids do not contain zeros, which represent padding. row_ids += 1 col_ids += 1 # Prepare additional patch features. # [rows * columns, 1] UpperCamelCase_ = row_ids.to(torch.floataa ) UpperCamelCase_ = col_ids.to(torch.floataa ) # [rows * columns, 2 + patch_height * patch_width * image_channels] UpperCamelCase_ = torch.cat([row_ids, col_ids, patches] , -1 ) # [max_patches, 2 + patch_height * patch_width * image_channels] UpperCamelCase_ = torch.nn.functional.pad(_SCREAMING_SNAKE_CASE , [0, 0, 0, max_patches - (rows * columns)] ).float() UpperCamelCase_ = to_numpy_array(_SCREAMING_SNAKE_CASE ) return result def lowercase ( self: Optional[Any] , _SCREAMING_SNAKE_CASE: np.ndarray , _SCREAMING_SNAKE_CASE: Optional[Union[str, ChannelDimension]] = None , **_SCREAMING_SNAKE_CASE: List[str] ) -> np.ndarray: """simple docstring""" if image.dtype == np.uinta: UpperCamelCase_ = image.astype(np.floataa ) # take mean across the whole `image` UpperCamelCase_ = np.mean(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = np.std(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = max(_SCREAMING_SNAKE_CASE , 1.0 / math.sqrt(np.prod(image.shape ) ) ) return normalize(_SCREAMING_SNAKE_CASE , mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def lowercase ( self: Optional[int] , _SCREAMING_SNAKE_CASE: ImageInput , _SCREAMING_SNAKE_CASE: Optional[str] = None , _SCREAMING_SNAKE_CASE: bool = None , _SCREAMING_SNAKE_CASE: Optional[bool] = None , _SCREAMING_SNAKE_CASE: Optional[int] = None , _SCREAMING_SNAKE_CASE: Optional[Dict[str, int]] = None , _SCREAMING_SNAKE_CASE: Optional[Union[str, TensorType]] = None , _SCREAMING_SNAKE_CASE: ChannelDimension = ChannelDimension.FIRST , **_SCREAMING_SNAKE_CASE: List[Any] , ) -> ImageInput: """simple docstring""" UpperCamelCase_ = do_normalize if do_normalize is not None else self.do_normalize UpperCamelCase_ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCamelCase_ = patch_size if patch_size is not None else self.patch_size UpperCamelCase_ = max_patches if max_patches is not None else self.max_patches UpperCamelCase_ = self.is_vqa if kwargs.get("data_format" , _SCREAMING_SNAKE_CASE ) is not None: raise ValueError("data_format is not an accepted input as the outputs are " ) UpperCamelCase_ = make_list_of_images(_SCREAMING_SNAKE_CASE ) if not valid_images(_SCREAMING_SNAKE_CASE ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCamelCase_ = [convert_to_rgb(_SCREAMING_SNAKE_CASE ) for image in images] # All transformations expect numpy arrays. UpperCamelCase_ = [to_numpy_array(_SCREAMING_SNAKE_CASE ) for image in images] if is_vqa: if header_text is None: raise ValueError("A header text must be provided for VQA models." ) UpperCamelCase_ = kwargs.pop("font_bytes" , _SCREAMING_SNAKE_CASE ) UpperCamelCase_ = kwargs.pop("font_path" , _SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): UpperCamelCase_ = [header_text] * len(_SCREAMING_SNAKE_CASE ) UpperCamelCase_ = [ render_header(_SCREAMING_SNAKE_CASE , header_text[i] , font_bytes=_SCREAMING_SNAKE_CASE , font_path=_SCREAMING_SNAKE_CASE ) for i, image in enumerate(_SCREAMING_SNAKE_CASE ) ] if do_normalize: UpperCamelCase_ = [self.normalize(image=_SCREAMING_SNAKE_CASE ) for image in images] # convert to torch tensor and permute UpperCamelCase_ = [ self.extract_flattened_patches(image=_SCREAMING_SNAKE_CASE , max_patches=_SCREAMING_SNAKE_CASE , patch_size=_SCREAMING_SNAKE_CASE ) for image in images ] # create attention mask in numpy UpperCamelCase_ = [(image.sum(axis=-1 ) != 0).astype(np.floataa ) for image in images] UpperCamelCase_ = BatchFeature( data={"flattened_patches": images, "attention_mask": attention_masks} , tensor_type=_SCREAMING_SNAKE_CASE ) return encoded_outputs
355
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent _UpperCAmelCase = {'UserAgent': UserAgent().random} def lowerCAmelCase_ ( UpperCamelCase_ ) -> dict: UpperCamelCase_ = script.contents[0] UpperCamelCase_ = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class _UpperCamelCase : def __init__( self: Optional[Any] , _SCREAMING_SNAKE_CASE: str ) -> str: """simple docstring""" UpperCamelCase_ = f'''https://www.instagram.com/{username}/''' UpperCamelCase_ = self.get_json() def lowercase ( self: Union[str, Any] ) -> dict: """simple docstring""" UpperCamelCase_ = requests.get(self.url , headers=_SCREAMING_SNAKE_CASE ).text UpperCamelCase_ = BeautifulSoup(_SCREAMING_SNAKE_CASE , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self: Tuple ) -> str: """simple docstring""" return f'''{self.__class__.__name__}(\'{self.username}\')''' def __str__( self: List[Any] ) -> str: """simple docstring""" return f'''{self.fullname} ({self.username}) is {self.biography}''' @property def lowercase ( self: List[str] ) -> str: """simple docstring""" return self.user_data["username"] @property def lowercase ( self: int ) -> str: """simple docstring""" return self.user_data["full_name"] @property def lowercase ( self: List[Any] ) -> str: """simple docstring""" return self.user_data["biography"] @property def lowercase ( self: List[Any] ) -> str: """simple docstring""" return self.user_data["business_email"] @property def lowercase ( self: List[Any] ) -> str: """simple docstring""" return self.user_data["external_url"] @property def lowercase ( self: List[Any] ) -> int: """simple docstring""" return self.user_data["edge_followed_by"]["count"] @property def lowercase ( self: List[str] ) -> int: """simple docstring""" return self.user_data["edge_follow"]["count"] @property def lowercase ( self: List[str] ) -> int: """simple docstring""" return self.user_data["edge_owner_to_timeline_media"]["count"] @property def lowercase ( self: List[str] ) -> str: """simple docstring""" return self.user_data["profile_pic_url_hd"] @property def lowercase ( self: Optional[int] ) -> bool: """simple docstring""" return self.user_data["is_verified"] @property def lowercase ( self: List[str] ) -> bool: """simple docstring""" return self.user_data["is_private"] def lowerCAmelCase_ ( UpperCamelCase_ = "github" ) -> None: import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCamelCase_ = InstagramUser(UpperCamelCase_ ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , UpperCamelCase_ ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() _UpperCAmelCase = InstagramUser('github') print(instagram_user) print(f'''{instagram_user.number_of_posts = }''') print(f'''{instagram_user.number_of_followers = }''') print(f'''{instagram_user.number_of_followings = }''') print(f'''{instagram_user.email = }''') print(f'''{instagram_user.website = }''') print(f'''{instagram_user.profile_picture_url = }''') print(f'''{instagram_user.is_verified = }''') print(f'''{instagram_user.is_private = }''')
328
0
import os import tempfile import unittest from transformers import DistilBertConfig, 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 ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, ) class lowercase_ ( __SCREAMING_SNAKE_CASE ): def __init__( self , __UpperCamelCase , __UpperCamelCase=1_3 , __UpperCamelCase=7 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=False , __UpperCamelCase=True , __UpperCamelCase=9_9 , __UpperCamelCase=3_2 , __UpperCamelCase=5 , __UpperCamelCase=4 , __UpperCamelCase=3_7 , __UpperCamelCase="gelu" , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=5_1_2 , __UpperCamelCase=1_6 , __UpperCamelCase=2 , __UpperCamelCase=0.02 , __UpperCamelCase=3 , __UpperCamelCase=4 , __UpperCamelCase=None , ): """simple docstring""" UpperCamelCase_ = parent UpperCamelCase_ = batch_size UpperCamelCase_ = seq_length UpperCamelCase_ = is_training UpperCamelCase_ = use_input_mask UpperCamelCase_ = use_token_type_ids UpperCamelCase_ = use_labels UpperCamelCase_ = vocab_size UpperCamelCase_ = hidden_size UpperCamelCase_ = num_hidden_layers UpperCamelCase_ = num_attention_heads UpperCamelCase_ = intermediate_size UpperCamelCase_ = hidden_act UpperCamelCase_ = hidden_dropout_prob UpperCamelCase_ = attention_probs_dropout_prob UpperCamelCase_ = max_position_embeddings UpperCamelCase_ = type_vocab_size UpperCamelCase_ = type_sequence_label_size UpperCamelCase_ = initializer_range UpperCamelCase_ = num_labels UpperCamelCase_ = num_choices UpperCamelCase_ = scope def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase_ = None if self.use_input_mask: UpperCamelCase_ = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase_ = None UpperCamelCase_ = None UpperCamelCase_ = None if self.use_labels: UpperCamelCase_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase_ = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase_ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCamelCase_ ( self ): """simple docstring""" return DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = DistilBertModel(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() UpperCamelCase_ = model(__UpperCamelCase , __UpperCamelCase ) UpperCamelCase_ = model(__UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = DistilBertForMaskedLM(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() UpperCamelCase_ = model(__UpperCamelCase , attention_mask=__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = DistilBertForQuestionAnswering(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() UpperCamelCase_ = model( __UpperCamelCase , attention_mask=__UpperCamelCase , start_positions=__UpperCamelCase , end_positions=__UpperCamelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = self.num_labels UpperCamelCase_ = DistilBertForSequenceClassification(__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() UpperCamelCase_ = model(__UpperCamelCase , attention_mask=__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = self.num_labels UpperCamelCase_ = DistilBertForTokenClassification(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() UpperCamelCase_ = model(__UpperCamelCase , attention_mask=__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = self.num_choices UpperCamelCase_ = DistilBertForMultipleChoice(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() UpperCamelCase_ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCamelCase_ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCamelCase_ = model( __UpperCamelCase , attention_mask=__UpperCamelCase , labels=__UpperCamelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = self.prepare_config_and_inputs() ((UpperCamelCase_) , (UpperCamelCase_) , (UpperCamelCase_) , (UpperCamelCase_) , (UpperCamelCase_) , (UpperCamelCase_)) = config_and_inputs UpperCamelCase_ = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class lowercase_ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): A__ : str = ( ( DistilBertModel, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, ) if is_torch_available() else None ) A__ : List[str] = ( { """feature-extraction""": DistilBertModel, """fill-mask""": DistilBertForMaskedLM, """question-answering""": DistilBertForQuestionAnswering, """text-classification""": DistilBertForSequenceClassification, """token-classification""": DistilBertForTokenClassification, """zero-shot""": DistilBertForSequenceClassification, } if is_torch_available() else {} ) A__ : Optional[int] = True A__ : Union[str, Any] = True A__ : Optional[int] = True A__ : Tuple = True def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = DistilBertModelTester(self ) UpperCamelCase_ = ConfigTester(self , config_class=__UpperCamelCase , dim=3_7 ) def lowerCamelCase_ ( self ): """simple docstring""" self.config_tester.run_common_tests() def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*__UpperCamelCase ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*__UpperCamelCase ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*__UpperCamelCase ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*__UpperCamelCase ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*__UpperCamelCase ) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*__UpperCamelCase ) @slow def lowerCamelCase_ ( self ): """simple docstring""" for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase_ = DistilBertModel.from_pretrained(__UpperCamelCase ) self.assertIsNotNone(__UpperCamelCase ) @slow @require_torch_gpu def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ , UpperCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # BertForMultipleChoice behaves incorrectly in JIT environments. if model_class == DistilBertForMultipleChoice: return UpperCamelCase_ = True UpperCamelCase_ = model_class(config=__UpperCamelCase ) UpperCamelCase_ = self._prepare_for_class(__UpperCamelCase , __UpperCamelCase ) UpperCamelCase_ = torch.jit.trace( __UpperCamelCase , (inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(__UpperCamelCase , os.path.join(__UpperCamelCase , """traced_model.pt""" ) ) UpperCamelCase_ = torch.jit.load(os.path.join(__UpperCamelCase , """traced_model.pt""" ) , map_location=__UpperCamelCase ) loaded(inputs_dict["""input_ids"""].to(__UpperCamelCase ) , inputs_dict["""attention_mask"""].to(__UpperCamelCase ) ) @require_torch class lowercase_ ( unittest.TestCase ): @slow def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = DistilBertModel.from_pretrained("""distilbert-base-uncased""" ) UpperCamelCase_ = 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]] ) UpperCamelCase_ = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): UpperCamelCase_ = model(__UpperCamelCase , attention_mask=__UpperCamelCase )[0] UpperCamelCase_ = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , __UpperCamelCase ) UpperCamelCase_ = torch.tensor( [[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , __UpperCamelCase , atol=1e-4 ) )
122
_A = { '''meter''': '''m''', '''kilometer''': '''km''', '''megametre''': '''Mm''', '''gigametre''': '''Gm''', '''terametre''': '''Tm''', '''petametre''': '''Pm''', '''exametre''': '''Em''', '''zettametre''': '''Zm''', '''yottametre''': '''Ym''', } # Exponent of the factor(meter) _A = { '''m''': 0, '''km''': 3, '''Mm''': 6, '''Gm''': 9, '''Tm''': 12, '''Pm''': 15, '''Em''': 18, '''Zm''': 21, '''Ym''': 24, } def lowerCamelCase__ ( a__ : float , a__ : str , a__ : str ) -> float: UpperCamelCase_ = from_type.lower().strip("""s""" ) UpperCamelCase_ = to_type.lower().strip("""s""" ) UpperCamelCase_ = UNIT_SYMBOL.get(a__ , a__ ) UpperCamelCase_ = UNIT_SYMBOL.get(a__ , a__ ) if from_sanitized not in METRIC_CONVERSION: UpperCamelCase_ = ( f'''Invalid \'from_type\' value: {from_type!r}.\n''' f'''Conversion abbreviations are: {", ".join(a__ )}''' ) raise ValueError(a__ ) if to_sanitized not in METRIC_CONVERSION: UpperCamelCase_ = ( f'''Invalid \'to_type\' value: {to_type!r}.\n''' f'''Conversion abbreviations are: {", ".join(a__ )}''' ) raise ValueError(a__ ) UpperCamelCase_ = METRIC_CONVERSION[from_sanitized] UpperCamelCase_ = METRIC_CONVERSION[to_sanitized] UpperCamelCase_ = 1 if from_exponent > to_exponent: UpperCamelCase_ = from_exponent - to_exponent else: UpperCamelCase_ = -(to_exponent - from_exponent) return value * pow(10 , a__ ) if __name__ == "__main__": from doctest import testmod testmod()
122
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __snake_case = { '''configuration_llama''': ['''LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LlamaConfig'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''LlamaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''LlamaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''LlamaForCausalLM''', '''LlamaModel''', '''LlamaPreTrainedModel''', '''LlamaForSequenceClassification''', ] if TYPE_CHECKING: from .configuration_llama import LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, LlamaConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama import LlamaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama_fast import LlamaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_llama import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
78
import colorsys from PIL import Image # type: ignore def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase )-> float: '''simple docstring''' UpperCAmelCase : Any =x UpperCAmelCase : List[str] =y for step in range(__lowerCAmelCase ): # noqa: B007 UpperCAmelCase : int =a * a - b * b + x UpperCAmelCase : Union[str, Any] =2 * a * b + y UpperCAmelCase : Optional[int] =a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def lowerCAmelCase_ ( __lowerCAmelCase )-> tuple: '''simple docstring''' if distance == 1: return (0, 0, 0) else: return (2_55, 2_55, 2_55) def lowerCAmelCase_ ( __lowerCAmelCase )-> tuple: '''simple docstring''' if distance == 1: return (0, 0, 0) else: return tuple(round(i * 2_55 ) for i in colorsys.hsv_to_rgb(__lowerCAmelCase , 1 , 1 ) ) def lowerCAmelCase_ ( __lowerCAmelCase = 8_00 , __lowerCAmelCase = 6_00 , __lowerCAmelCase = -0.6 , __lowerCAmelCase = 0 , __lowerCAmelCase = 3.2 , __lowerCAmelCase = 50 , __lowerCAmelCase = True , )-> Image.Image: '''simple docstring''' UpperCAmelCase : Dict =Image.new('''RGB''' , (image_width, image_height) ) UpperCAmelCase : str =img.load() # loop through the image-coordinates for image_x in range(__lowerCAmelCase ): for image_y in range(__lowerCAmelCase ): # determine the figure-coordinates based on the image-coordinates UpperCAmelCase : Union[str, Any] =figure_width / image_width * image_height UpperCAmelCase : Dict =figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCAmelCase : str =figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCAmelCase : int =get_distance(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCAmelCase : Any =get_color_coded_rgb(__lowerCAmelCase ) else: UpperCAmelCase : Optional[int] =get_black_and_white_rgb(__lowerCAmelCase ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure __snake_case = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
78
1
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer UpperCAmelCase_ : Union[str, Any] = logging.get_logger(__name__) UpperCAmelCase_ : Dict = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase_ : Dict = { '''vocab_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json''', }, '''merges_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''allenai/led-base-16384''': '''https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json''', }, } UpperCAmelCase_ : Any = { '''allenai/led-base-16384''': 1_63_84, } class _SCREAMING_SNAKE_CASE ( _a ): snake_case__ : int = VOCAB_FILES_NAMES snake_case__ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP snake_case__ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ : int = LEDTokenizer snake_case__ : str = ["""input_ids""", """attention_mask"""] def __init__( self : List[str] , __lowerCamelCase : Dict=None , __lowerCamelCase : Any=None , __lowerCamelCase : int=None , __lowerCamelCase : Any="replace" , __lowerCamelCase : Union[str, Any]="<s>" , __lowerCamelCase : str="</s>" , __lowerCamelCase : Tuple="</s>" , __lowerCamelCase : List[Any]="<s>" , __lowerCamelCase : Optional[int]="<unk>" , __lowerCamelCase : Any="<pad>" , __lowerCamelCase : Tuple="<mask>" , __lowerCamelCase : List[str]=False , __lowerCamelCase : List[Any]=True , **__lowerCamelCase : str , ): super().__init__( __lowerCamelCase , __lowerCamelCase , tokenizer_file=__lowerCamelCase , errors=__lowerCamelCase , bos_token=__lowerCamelCase , eos_token=__lowerCamelCase , sep_token=__lowerCamelCase , cls_token=__lowerCamelCase , unk_token=__lowerCamelCase , pad_token=__lowerCamelCase , mask_token=__lowerCamelCase , add_prefix_space=__lowerCamelCase , trim_offsets=__lowerCamelCase , **__lowerCamelCase , ) UpperCamelCase :Dict = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("""add_prefix_space""" , __lowerCamelCase ) != add_prefix_space: UpperCamelCase :Union[str, Any] = getattr(__lowerCamelCase , pre_tok_state.pop("""type""" ) ) UpperCamelCase :Optional[Any] = add_prefix_space UpperCamelCase :Tuple = pre_tok_class(**__lowerCamelCase ) UpperCamelCase :Optional[Any] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` UpperCamelCase :str = """post_processor""" UpperCamelCase :List[str] = getattr(self.backend_tokenizer , __lowerCamelCase , __lowerCamelCase ) if tokenizer_component_instance: UpperCamelCase :Tuple = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: UpperCamelCase :int = tuple(state["""sep"""] ) if "cls" in state: UpperCamelCase :Optional[int] = tuple(state["""cls"""] ) UpperCamelCase :Optional[Any] = False if state.get("""add_prefix_space""" , __lowerCamelCase ) != add_prefix_space: UpperCamelCase :Optional[Any] = add_prefix_space UpperCamelCase :Union[str, Any] = True if state.get("""trim_offsets""" , __lowerCamelCase ) != trim_offsets: UpperCamelCase :Tuple = trim_offsets UpperCamelCase :Tuple = True if changes_to_apply: UpperCamelCase :Tuple = getattr(__lowerCamelCase , state.pop("""type""" ) ) UpperCamelCase :int = component_class(**__lowerCamelCase ) setattr(self.backend_tokenizer , __lowerCamelCase , __lowerCamelCase ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def _A ( self : Dict ): 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 : Dict , __lowerCamelCase : List[str] ): UpperCamelCase :Dict = AddedToken(__lowerCamelCase , lstrip=__lowerCamelCase , rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ) else value UpperCamelCase :List[Any] = value def _A ( self : Union[str, Any] , *__lowerCamelCase : List[Any] , **__lowerCamelCase : Optional[int] ): UpperCamelCase :Optional[int] = kwargs.get("""is_split_into_words""" , __lowerCamelCase ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ """to use it with pretokenized inputs.""" ) return super()._batch_encode_plus(*__lowerCamelCase , **__lowerCamelCase ) def _A ( self : Tuple , *__lowerCamelCase : Union[str, Any] , **__lowerCamelCase : str ): UpperCamelCase :Tuple = kwargs.get("""is_split_into_words""" , __lowerCamelCase ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ """to use it with pretokenized inputs.""" ) return super()._encode_plus(*__lowerCamelCase , **__lowerCamelCase ) def _A ( self : Optional[int] , __lowerCamelCase : str , __lowerCamelCase : Optional[str] = None ): UpperCamelCase :List[str] = self._tokenizer.model.save(__lowerCamelCase , name=__lowerCamelCase ) return tuple(__lowerCamelCase ) def _A ( self : int , __lowerCamelCase : Dict , __lowerCamelCase : Optional[Any]=None ): UpperCamelCase :List[Any] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _A ( self : List[Any] , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): UpperCamelCase :Dict = [self.sep_token_id] UpperCamelCase :List[Any] = [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 : Tuple , __lowerCamelCase : Union[Dict[str, EncodedInput], BatchEncoding] , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , __lowerCamelCase : Optional[int] = None , __lowerCamelCase : Optional[bool] = None , ): UpperCamelCase :List[Any] = super()._pad( encoded_inputs=__lowerCamelCase , max_length=__lowerCamelCase , padding_strategy=__lowerCamelCase , pad_to_multiple_of=__lowerCamelCase , return_attention_mask=__lowerCamelCase , ) # Load from model defaults if return_attention_mask is None: UpperCamelCase :List[Any] = """attention_mask""" in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: UpperCamelCase :str = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. UpperCamelCase :Union[str, Any] = len(encoded_inputs["""global_attention_mask"""] ) != len(__lowerCamelCase ) if needs_to_be_padded: UpperCamelCase :Optional[int] = len(__lowerCamelCase ) - len(encoded_inputs["""global_attention_mask"""] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` UpperCamelCase :Optional[Any] = ( encoded_inputs["""global_attention_mask"""] + [-1] * difference ) elif self.padding_side == "left": UpperCamelCase :Union[str, Any] = [-1] * difference + encoded_inputs[ """global_attention_mask""" ] else: raise ValueError("""Invalid padding strategy:""" + str(self.padding_side ) ) return encoded_inputs
38
from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase = {"processing_wav2vec2_with_lm": ["Wav2Vec2ProcessorWithLM"]} if TYPE_CHECKING: from .processing_wavaveca_with_lm import WavaVecaProcessorWithLM else: import sys lowercase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
178
0
"""simple docstring""" import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) A_ : List[str] = logging.get_logger(__name__) A_ : Tuple = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) A_ : Union[str, Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowerCamelCase_ ( _lowerCamelCase ): for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: lowerCamelCase__ : List[Any] = model_type_to_module_name(_lowerCamelCase ) lowerCamelCase__ : Dict = importlib.import_module(f'''.{module_name}''' , 'transformers.models' ) try: return getattr(_lowerCamelCase , _lowerCamelCase ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(_lowerCamelCase , '__name__' , _lowerCamelCase ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. lowerCamelCase__ : Tuple = importlib.import_module('transformers' ) if hasattr(_lowerCamelCase , _lowerCamelCase ): return getattr(_lowerCamelCase , _lowerCamelCase ) return None def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = False , _lowerCamelCase = False , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = False , **_lowerCamelCase , ): lowerCamelCase__ : int = get_file_from_repo( _lowerCamelCase , _lowerCamelCase , cache_dir=_lowerCamelCase , force_download=_lowerCamelCase , resume_download=_lowerCamelCase , proxies=_lowerCamelCase , use_auth_token=_lowerCamelCase , revision=_lowerCamelCase , local_files_only=_lowerCamelCase , ) if resolved_config_file is None: logger.info( 'Could not locate the feature extractor configuration file, will try to use the model config instead.' ) return {} with open(_lowerCamelCase , encoding='utf-8' ) as reader: return json.load(_lowerCamelCase ) class a_ : '''simple docstring''' def __init__(self ): '''simple docstring''' raise EnvironmentError( 'AutoFeatureExtractor is designed to be instantiated ' 'using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.' ) @classmethod @replace_list_option_in_docstrings(lowerCamelCase_ ) def a__ (cls, lowerCamelCase_, **lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = kwargs.pop('config', lowerCamelCase_ ) lowerCamelCase__ : List[Any] = kwargs.pop('trust_remote_code', lowerCamelCase_ ) lowerCamelCase__ : Any = True lowerCamelCase__ , lowerCamelCase__ : str = FeatureExtractionMixin.get_feature_extractor_dict(lowerCamelCase_, **lowerCamelCase_ ) lowerCamelCase__ : str = config_dict.get('feature_extractor_type', lowerCamelCase_ ) lowerCamelCase__ : Union[str, Any] = None if "AutoFeatureExtractor" in config_dict.get('auto_map', {} ): lowerCamelCase__ : Tuple = config_dict['auto_map']['AutoFeatureExtractor'] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(lowerCamelCase_, lowerCamelCase_ ): lowerCamelCase__ : List[str] = AutoConfig.from_pretrained(lowerCamelCase_, **lowerCamelCase_ ) # It could be in `config.feature_extractor_type`` lowerCamelCase__ : List[Any] = getattr(lowerCamelCase_, 'feature_extractor_type', lowerCamelCase_ ) if hasattr(lowerCamelCase_, 'auto_map' ) and "AutoFeatureExtractor" in config.auto_map: lowerCamelCase__ : List[Any] = config.auto_map['AutoFeatureExtractor'] if feature_extractor_class is not None: lowerCamelCase__ : str = feature_extractor_class_from_name(lowerCamelCase_ ) lowerCamelCase__ : Optional[int] = feature_extractor_auto_map is not None lowerCamelCase__ : List[str] = feature_extractor_class is not None or type(lowerCamelCase_ ) in FEATURE_EXTRACTOR_MAPPING lowerCamelCase__ : Tuple = resolve_trust_remote_code( lowerCamelCase_, lowerCamelCase_, lowerCamelCase_, lowerCamelCase_ ) if has_remote_code and trust_remote_code: lowerCamelCase__ : str = get_class_from_dynamic_module( lowerCamelCase_, lowerCamelCase_, **lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] = kwargs.pop('code_revision', lowerCamelCase_ ) if os.path.isdir(lowerCamelCase_ ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(lowerCamelCase_, **lowerCamelCase_ ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(lowerCamelCase_, **lowerCamelCase_ ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(lowerCamelCase_ ) in FEATURE_EXTRACTOR_MAPPING: lowerCamelCase__ : int = FEATURE_EXTRACTOR_MAPPING[type(lowerCamelCase_ )] return feature_extractor_class.from_dict(lowerCamelCase_, **lowerCamelCase_ ) raise ValueError( f'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a ''' f'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following ''' f'''`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def a__ (lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' FEATURE_EXTRACTOR_MAPPING.register(lowerCamelCase_, lowerCamelCase_ )
316
"""simple docstring""" class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Tuple = len(lowerCamelCase_ ) lowerCamelCase__ : Any = [0] * len_array if len_array > 0: lowerCamelCase__ : Union[str, Any] = array[0] for i in range(1, lowerCamelCase_ ): lowerCamelCase__ : Optional[int] = self.prefix_sum[i - 1] + array[i] def a__ (self, lowerCamelCase_, lowerCamelCase_ ): '''simple docstring''' if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Dict = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(lowerCamelCase_ ) return False if __name__ == "__main__": import doctest doctest.testmod()
316
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) __lowerCamelCase = { "configuration_wav2vec2": ["WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Wav2Vec2Config"], "feature_extraction_wav2vec2": ["Wav2Vec2FeatureExtractor"], "processing_wav2vec2": ["Wav2Vec2Processor"], "tokenization_wav2vec2": ["Wav2Vec2CTCTokenizer", "Wav2Vec2Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ "WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST", "Wav2Vec2ForAudioFrameClassification", "Wav2Vec2ForCTC", "Wav2Vec2ForMaskedLM", "Wav2Vec2ForPreTraining", "Wav2Vec2ForSequenceClassification", "Wav2Vec2ForXVector", "Wav2Vec2Model", "Wav2Vec2PreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ "TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST", "TFWav2Vec2ForCTC", "TFWav2Vec2Model", "TFWav2Vec2PreTrainedModel", "TFWav2Vec2ForSequenceClassification", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ "FlaxWav2Vec2ForCTC", "FlaxWav2Vec2ForPreTraining", "FlaxWav2Vec2Model", "FlaxWav2Vec2PreTrainedModel", ] if TYPE_CHECKING: from .configuration_wavaveca import WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, WavaVecaConfig from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .processing_wavaveca import WavaVecaProcessor from .tokenization_wavaveca import WavaVecaCTCTokenizer, WavaVecaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavaveca import ( WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaForAudioFrameClassification, WavaVecaForCTC, WavaVecaForMaskedLM, WavaVecaForPreTraining, WavaVecaForSequenceClassification, WavaVecaForXVector, WavaVecaModel, WavaVecaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_wavaveca import ( TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, TFWavaVecaForCTC, TFWavaVecaForSequenceClassification, TFWavaVecaModel, TFWavaVecaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_wavaveca import ( FlaxWavaVecaForCTC, FlaxWavaVecaForPreTraining, FlaxWavaVecaModel, FlaxWavaVecaPreTrainedModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
221
"""simple docstring""" import contextlib import importlib import io import unittest import transformers # Try to import everything from transformers to ensure every object can be loaded. from transformers import * # noqa F406 from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available if is_torch_available(): from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification if is_tf_available(): from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification if is_flax_available(): from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification __lowerCamelCase = DUMMY_UNKNOWN_IDENTIFIER # An actual model hosted on huggingface.co __lowerCamelCase = "main" # Default branch name __lowerCamelCase = "f2c752cfc5c0ab6f4bdec59acea69eefbee381c2" # One particular commit (not the top of `main`) __lowerCamelCase = "aaaaaaa" # This commit does not exist, so we should 404. __lowerCamelCase = "d9e9f15bc825e4b2c9249e9578f884bbcb5e3684" # Sha-1 of config.json on the top of `main`, for checking purposes __lowerCamelCase = "4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3" @contextlib.contextmanager def UpperCAmelCase ( ): """simple docstring""" print('Welcome!' ) yield print('Bye!' ) @contextlib.contextmanager def UpperCAmelCase ( ): """simple docstring""" print('Bonjour!' ) yield print('Au revoir!' ) class UpperCamelCase__( unittest.TestCase ): def snake_case__ ( self ) -> List[str]: # If the spec is missing, importlib would not be able to import the module dynamically. assert transformers.__spec__ is not None assert importlib.util.find_spec('transformers' ) is not None class UpperCamelCase__( unittest.TestCase ): @unittest.mock.patch('sys.stdout' ,new_callable=io.StringIO ) def snake_case__ ( self ,__UpperCAmelCase ) -> Dict: with ContextManagers([] ): print('Transformers are awesome!' ) # The print statement adds a new line at the end of the output self.assertEqual(mock_stdout.getvalue() ,'Transformers are awesome!\n' ) @unittest.mock.patch('sys.stdout' ,new_callable=io.StringIO ) def snake_case__ ( self ,__UpperCAmelCase ) -> List[str]: with ContextManagers([context_en()] ): print('Transformers are awesome!' ) # The output should be wrapped with an English welcome and goodbye self.assertEqual(mock_stdout.getvalue() ,'Welcome!\nTransformers are awesome!\nBye!\n' ) @unittest.mock.patch('sys.stdout' ,new_callable=io.StringIO ) def snake_case__ ( self ,__UpperCAmelCase ) -> Any: with ContextManagers([context_fr(), context_en()] ): print('Transformers are awesome!' ) # The output should be wrapped with an English and French welcome and goodbye self.assertEqual(mock_stdout.getvalue() ,'Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n' ) @require_torch def snake_case__ ( self ) -> Union[str, Any]: self.assertEqual(find_labels(__UpperCAmelCase ) ,['labels'] ) self.assertEqual(find_labels(__UpperCAmelCase ) ,['labels', 'next_sentence_label'] ) self.assertEqual(find_labels(__UpperCAmelCase ) ,['start_positions', 'end_positions'] ) class UpperCamelCase__( __A ): pass self.assertEqual(find_labels(__UpperCAmelCase ) ,['labels'] ) @require_tf def snake_case__ ( self ) -> str: self.assertEqual(find_labels(__UpperCAmelCase ) ,['labels'] ) self.assertEqual(find_labels(__UpperCAmelCase ) ,['labels', 'next_sentence_label'] ) self.assertEqual(find_labels(__UpperCAmelCase ) ,['start_positions', 'end_positions'] ) class UpperCamelCase__( __A ): pass self.assertEqual(find_labels(__UpperCAmelCase ) ,['labels'] ) @require_flax def snake_case__ ( self ) -> List[Any]: # Flax models don't have labels self.assertEqual(find_labels(__UpperCAmelCase ) ,[] ) self.assertEqual(find_labels(__UpperCAmelCase ) ,[] ) self.assertEqual(find_labels(__UpperCAmelCase ) ,[] ) class UpperCamelCase__( __A ): pass self.assertEqual(find_labels(__UpperCAmelCase ) ,[] )
221
1
"""simple docstring""" import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params A_ : str =[ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ["""memory_attention""", """encoder_attn"""], ["""attention""", """attn"""], ["""/""", """."""], [""".LayerNorm.gamma""", """_layer_norm.weight"""], [""".LayerNorm.beta""", """_layer_norm.bias"""], ["""r.layer_""", """r.layers."""], ["""output_proj""", """out_proj"""], ["""ffn.dense_1.""", """fc2."""], ["""ffn.dense.""", """fc1."""], ["""ffn_layer_norm""", """final_layer_norm"""], ["""kernel""", """weight"""], ["""encoder_layer_norm.""", """encoder.layer_norm."""], ["""decoder_layer_norm.""", """decoder.layer_norm."""], ["""embeddings.weights""", """shared.weight"""], ] def SCREAMING_SNAKE_CASE_ ( snake_case : List[str] )-> int: for pegasus_name, hf_name in PATTERNS: _lowerCamelCase = k.replace(snake_case , snake_case ) return k def SCREAMING_SNAKE_CASE_ ( snake_case : dict , snake_case : dict )-> int: _lowerCamelCase = DEFAULTS.copy() cfg_kwargs.update(snake_case ) _lowerCamelCase = PegasusConfig(**snake_case ) _lowerCamelCase = PegasusForConditionalGeneration(snake_case ) _lowerCamelCase = torch_model.model.state_dict() _lowerCamelCase = {} for k, v in tf_weights.items(): _lowerCamelCase = rename_state_dict_key(snake_case ) if new_k not in sd: raise ValueError(f'could not find new key {new_k} in state dict. (converted from {k})' ) if "dense" in k or "proj" in new_k: _lowerCamelCase = v.T _lowerCamelCase = torch.tensor(snake_case , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, f'{new_k}, {k}, {v.shape}, {sd[new_k].shape}' # make sure embedding.padding_idx is respected _lowerCamelCase = torch.zeros_like(mapping['shared.weight'][cfg.pad_token_id + 1] ) _lowerCamelCase = mapping['shared.weight'] _lowerCamelCase = mapping['shared.weight'] _lowerCamelCase = {k: torch.zeros_like(snake_case ) for k, v in sd.items() if k.endswith('bias' ) and k not in mapping} mapping.update(**snake_case ) _lowerCamelCase , _lowerCamelCase = torch_model.model.load_state_dict(snake_case , strict=snake_case ) _lowerCamelCase = [ k for k in missing if k not in ['encoder.embed_positions.weight', 'decoder.embed_positions.weight'] ] assert unexpected_missing == [], f'no matches found for the following torch keys {unexpected_missing}' assert extra == [], f'no matches found for the following tf keys {extra}' return torch_model def SCREAMING_SNAKE_CASE_ ( snake_case : int="./ckpt/aeslc/model.ckpt-32000" )-> List[Any]: _lowerCamelCase = tf.train.list_variables(snake_case ) _lowerCamelCase = {} _lowerCamelCase = ['Adafactor', 'global_step'] for name, shape in tqdm(snake_case , desc='converting tf checkpoint to dict' ): _lowerCamelCase = any(pat in name for pat in ignore_name ) if skip_key: continue _lowerCamelCase = tf.train.load_variable(snake_case , snake_case ) _lowerCamelCase = array return tf_weights def SCREAMING_SNAKE_CASE_ ( snake_case : str , snake_case : str )-> List[Any]: # save tokenizer first _lowerCamelCase = Path(snake_case ).parent.name _lowerCamelCase = task_specific_params[f'summarization_{dataset}']['max_position_embeddings'] _lowerCamelCase = PegasusTokenizer.from_pretrained('sshleifer/pegasus' , model_max_length=snake_case ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(snake_case ) # convert model _lowerCamelCase = get_tf_weights_as_numpy(snake_case ) _lowerCamelCase = task_specific_params[f'summarization_{dataset}'] if dataset == "large": _lowerCamelCase = task_specific_params _lowerCamelCase = convert_pegasus(snake_case , snake_case ) torch_model.save_pretrained(snake_case ) _lowerCamelCase = torch_model.state_dict() sd.pop('model.decoder.embed_positions.weight' ) sd.pop('model.encoder.embed_positions.weight' ) torch.save(snake_case , Path(snake_case ) / 'pytorch_model.bin' ) if __name__ == "__main__": A_ : Optional[int] =argparse.ArgumentParser() # Required parameters parser.add_argument("""tf_ckpt_path""", type=str, help="""passed to tf.train.list_variables""") parser.add_argument("""save_dir""", default=None, type=str, help="""Path to the output PyTorch model.""") A_ : Optional[Any] =parser.parse_args() if args.save_dir is None: A_ : Optional[Any] =Path(args.tf_ckpt_path).parent.name A_ : Optional[int] =os.path.join("""pegasus""", dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
361
"""simple docstring""" from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def SCREAMING_SNAKE_CASE_ ( )-> Any: import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join _lowerCamelCase = '__test_patch_submodule_mock__' with patch_submodule(_test_patching , 'os.path.join' , snake_case ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os , _PatchedModuleObj ) assert isinstance(_test_patching.os.path , _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path , _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os , _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path , _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path , _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def SCREAMING_SNAKE_CASE_ ( )-> Optional[int]: assert _test_patching.open is open _lowerCamelCase = '__test_patch_submodule_builtin_mock__' # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching , 'open' , snake_case ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def SCREAMING_SNAKE_CASE_ ( )-> Tuple: # pandas.read_csv is not present in _test_patching _lowerCamelCase = '__test_patch_submodule_missing_mock__' with patch_submodule(_test_patching , 'pandas.read_csv' , snake_case ): pass def SCREAMING_SNAKE_CASE_ ( )-> Any: # builtin should always be mocked even if they're not in the globals # in case they're loaded at one point _lowerCamelCase = '__test_patch_submodule_missing_builtin_mock__' # _test_patching doesn't have "len" in its globals assert getattr(_test_patching , 'len' , snake_case ) is None with patch_submodule(_test_patching , 'len' , snake_case ): assert _test_patching.len is mock assert _test_patching.len is len def SCREAMING_SNAKE_CASE_ ( )-> Any: _lowerCamelCase = '__test_patch_submodule_start_and_stop_mock__' _lowerCamelCase = patch_submodule(_test_patching , 'open' , snake_case ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def SCREAMING_SNAKE_CASE_ ( )-> Tuple: from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join _lowerCamelCase = '__test_patch_submodule_successive_join__' _lowerCamelCase = '__test_patch_submodule_successive_dirname__' _lowerCamelCase = '__test_patch_submodule_successive_rename__' assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching , 'os.path.join' , snake_case ): with patch_submodule(_test_patching , 'os.rename' , snake_case ): with patch_submodule(_test_patching , 'os.path.dirname' , snake_case ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching , 'os.rename' , snake_case ): with patch_submodule(_test_patching , 'os.path.join' , snake_case ): with patch_submodule(_test_patching , 'os.path.dirname' , snake_case ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def SCREAMING_SNAKE_CASE_ ( )-> Optional[int]: _lowerCamelCase = '__test_patch_submodule_doesnt_exist_mock__' with patch_submodule(_test_patching , '__module_that_doesn_exist__.__attribute_that_doesn_exist__' , snake_case ): pass with patch_submodule(_test_patching , 'os.__attribute_that_doesn_exist__' , snake_case ): pass
80
0
import argparse import collections import json import os import re import string import sys import numpy as np SCREAMING_SNAKE_CASE__ : str = re.compile(r'\b(a|an|the)\b', re.UNICODE) SCREAMING_SNAKE_CASE__ : List[str] = None def A ( ) -> List[Any]: lowerCamelCase : Optional[int] = argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0." ) parser.add_argument("data_file" ,metavar="data.json" ,help="Input data JSON file." ) parser.add_argument("pred_file" ,metavar="pred.json" ,help="Model predictions." ) parser.add_argument( "--out-file" ,"-o" ,metavar="eval.json" ,help="Write accuracy metrics to file (default is stdout)." ) parser.add_argument( "--na-prob-file" ,"-n" ,metavar="na_prob.json" ,help="Model estimates of probability of no answer." ) parser.add_argument( "--na-prob-thresh" ,"-t" ,type=_SCREAMING_SNAKE_CASE ,default=1.0 ,help="Predict \"\" if no-answer probability exceeds this (default = 1.0)." ,) parser.add_argument( "--out-image-dir" ,"-p" ,metavar="out_images" ,default=_SCREAMING_SNAKE_CASE ,help="Save precision-recall curves to directory." ) parser.add_argument("--verbose" ,"-v" ,action="store_true" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def A ( _SCREAMING_SNAKE_CASE ) -> Optional[Any]: lowerCamelCase : Optional[int] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: lowerCamelCase : Tuple = bool(qa["answers"]["text"] ) return qid_to_has_ans def A ( _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: def remove_articles(_SCREAMING_SNAKE_CASE ): return ARTICLES_REGEX.sub(" " ,_SCREAMING_SNAKE_CASE ) def white_space_fix(_SCREAMING_SNAKE_CASE ): return " ".join(text.split() ) def remove_punc(_SCREAMING_SNAKE_CASE ): lowerCamelCase : Tuple = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(_SCREAMING_SNAKE_CASE ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(_SCREAMING_SNAKE_CASE ) ) ) ) def A ( _SCREAMING_SNAKE_CASE ) -> Optional[int]: if not s: return [] return normalize_answer(_SCREAMING_SNAKE_CASE ).split() def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Optional[int]: return int(normalize_answer(_SCREAMING_SNAKE_CASE ) == normalize_answer(_SCREAMING_SNAKE_CASE ) ) def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Tuple: lowerCamelCase : str = get_tokens(_SCREAMING_SNAKE_CASE ) lowerCamelCase : List[Any] = get_tokens(_SCREAMING_SNAKE_CASE ) lowerCamelCase : List[str] = collections.Counter(_SCREAMING_SNAKE_CASE ) & collections.Counter(_SCREAMING_SNAKE_CASE ) lowerCamelCase : Any = sum(common.values() ) if len(_SCREAMING_SNAKE_CASE ) == 0 or len(_SCREAMING_SNAKE_CASE ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 lowerCamelCase : List[str] = 1.0 * num_same / len(_SCREAMING_SNAKE_CASE ) lowerCamelCase : str = 1.0 * num_same / len(_SCREAMING_SNAKE_CASE ) lowerCamelCase : List[Any] = (2 * precision * recall) / (precision + recall) return fa def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> List[str]: lowerCamelCase : Any = {} lowerCamelCase : List[Any] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: lowerCamelCase : Dict = qa["id"] lowerCamelCase : List[str] = [t for t in qa["answers"]["text"] if normalize_answer(_SCREAMING_SNAKE_CASE )] if not gold_answers: # For unanswerable questions, only correct answer is empty string lowerCamelCase : List[Any] = [""] if qid not in preds: print(f'''Missing prediction for {qid}''' ) continue lowerCamelCase : Any = preds[qid] # Take max over all gold answers lowerCamelCase : Dict = max(compute_exact(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) for a in gold_answers ) lowerCamelCase : Optional[int] = max(compute_fa(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) for a in gold_answers ) return exact_scores, fa_scores def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: lowerCamelCase : int = {} for qid, s in scores.items(): lowerCamelCase : List[Any] = na_probs[qid] > na_prob_thresh if pred_na: lowerCamelCase : Union[str, Any] = float(not qid_to_has_ans[qid] ) else: lowerCamelCase : Tuple = s return new_scores def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=None ) -> str: if not qid_list: lowerCamelCase : List[str] = len(_SCREAMING_SNAKE_CASE ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: lowerCamelCase : Union[str, Any] = len(_SCREAMING_SNAKE_CASE ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("f1", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("total", total), ] ) def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Any: for k in new_eval: lowerCamelCase : Tuple = new_eval[k] def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> List[Any]: plt.step(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,color="b" ,alpha=0.2 ,where="post" ) plt.fill_between(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,step="post" ,alpha=0.2 ,color="b" ) plt.xlabel("Recall" ) plt.ylabel("Precision" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(_SCREAMING_SNAKE_CASE ) plt.savefig(_SCREAMING_SNAKE_CASE ) plt.clf() def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=None ,_SCREAMING_SNAKE_CASE=None ) -> List[str]: lowerCamelCase : Tuple = sorted(_SCREAMING_SNAKE_CASE ,key=lambda _SCREAMING_SNAKE_CASE : na_probs[k] ) lowerCamelCase : List[Any] = 0.0 lowerCamelCase : Union[str, Any] = 1.0 lowerCamelCase : int = 0.0 lowerCamelCase : Union[str, Any] = [1.0] lowerCamelCase : str = [0.0] lowerCamelCase : int = 0.0 for i, qid in enumerate(_SCREAMING_SNAKE_CASE ): if qid_to_has_ans[qid]: true_pos += scores[qid] lowerCamelCase : Optional[int] = true_pos / float(i + 1 ) lowerCamelCase : Optional[int] = true_pos / float(_SCREAMING_SNAKE_CASE ) if i == len(_SCREAMING_SNAKE_CASE ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(_SCREAMING_SNAKE_CASE ) recalls.append(_SCREAMING_SNAKE_CASE ) if out_image: plot_pr_curve(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) return {"ap": 100.0 * avg_prec} def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: if out_image_dir and not os.path.exists(_SCREAMING_SNAKE_CASE ): os.makedirs(_SCREAMING_SNAKE_CASE ) lowerCamelCase : Union[str, Any] = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return lowerCamelCase : int = make_precision_recall_eval( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,out_image=os.path.join(_SCREAMING_SNAKE_CASE ,"pr_exact.png" ) ,title="Precision-Recall curve for Exact Match score" ,) lowerCamelCase : Optional[int] = make_precision_recall_eval( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,out_image=os.path.join(_SCREAMING_SNAKE_CASE ,"pr_f1.png" ) ,title="Precision-Recall curve for F1 score" ,) lowerCamelCase : Union[str, Any] = {k: float(_SCREAMING_SNAKE_CASE ) for k, v in qid_to_has_ans.items()} lowerCamelCase : List[str] = make_precision_recall_eval( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,out_image=os.path.join(_SCREAMING_SNAKE_CASE ,"pr_oracle.png" ) ,title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)" ,) merge_eval(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,"pr_exact" ) merge_eval(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,"pr_f1" ) merge_eval(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,"pr_oracle" ) def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> int: if not qid_list: return lowerCamelCase : Any = [na_probs[k] for k in qid_list] lowerCamelCase : Optional[Any] = np.ones_like(_SCREAMING_SNAKE_CASE ) / float(len(_SCREAMING_SNAKE_CASE ) ) plt.hist(_SCREAMING_SNAKE_CASE ,weights=_SCREAMING_SNAKE_CASE ,bins=20 ,range=(0.0, 1.0) ) plt.xlabel("Model probability of no-answer" ) plt.ylabel("Proportion of dataset" ) plt.title(f'''Histogram of no-answer probability: {name}''' ) plt.savefig(os.path.join(_SCREAMING_SNAKE_CASE ,f'''na_prob_hist_{name}.png''' ) ) plt.clf() def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Dict: lowerCamelCase : List[str] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) lowerCamelCase : Dict = num_no_ans lowerCamelCase : List[Any] = cur_score lowerCamelCase : Optional[int] = 0.0 lowerCamelCase : Union[str, Any] = sorted(_SCREAMING_SNAKE_CASE ,key=lambda _SCREAMING_SNAKE_CASE : na_probs[k] ) for i, qid in enumerate(_SCREAMING_SNAKE_CASE ): if qid not in scores: continue if qid_to_has_ans[qid]: lowerCamelCase : Any = scores[qid] else: if preds[qid]: lowerCamelCase : Union[str, Any] = -1 else: lowerCamelCase : Optional[Any] = 0 cur_score += diff if cur_score > best_score: lowerCamelCase : Dict = cur_score lowerCamelCase : Optional[int] = na_probs[qid] return 100.0 * best_score / len(_SCREAMING_SNAKE_CASE ), best_thresh def A ( _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) -> Tuple: lowerCamelCase , lowerCamelCase : List[Any] = find_best_thresh(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) lowerCamelCase , lowerCamelCase : Dict = find_best_thresh(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) lowerCamelCase : Optional[int] = best_exact lowerCamelCase : str = exact_thresh lowerCamelCase : Union[str, Any] = best_fa lowerCamelCase : Union[str, Any] = fa_thresh def A ( ) -> Union[str, Any]: with open(OPTS.data_file ) as f: lowerCamelCase : Tuple = json.load(_SCREAMING_SNAKE_CASE ) lowerCamelCase : List[Any] = dataset_json["data"] with open(OPTS.pred_file ) as f: lowerCamelCase : Optional[Any] = json.load(_SCREAMING_SNAKE_CASE ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: lowerCamelCase : Optional[int] = json.load(_SCREAMING_SNAKE_CASE ) else: lowerCamelCase : List[Any] = {k: 0.0 for k in preds} lowerCamelCase : Optional[Any] = make_qid_to_has_ans(_SCREAMING_SNAKE_CASE ) # maps qid to True/False lowerCamelCase : Tuple = [k for k, v in qid_to_has_ans.items() if v] lowerCamelCase : Any = [k for k, v in qid_to_has_ans.items() if not v] lowerCamelCase , lowerCamelCase : Optional[Any] = get_raw_scores(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) lowerCamelCase : str = apply_no_ans_threshold(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,OPTS.na_prob_thresh ) lowerCamelCase : Optional[int] = apply_no_ans_threshold(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,OPTS.na_prob_thresh ) lowerCamelCase : Tuple = make_eval_dict(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) if has_ans_qids: lowerCamelCase : List[Any] = make_eval_dict(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,qid_list=_SCREAMING_SNAKE_CASE ) merge_eval(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,"HasAns" ) if no_ans_qids: lowerCamelCase : Union[str, Any] = make_eval_dict(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,qid_list=_SCREAMING_SNAKE_CASE ) merge_eval(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,"NoAns" ) if OPTS.na_prob_file: find_all_best_thresh(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,OPTS.out_image_dir ) histogram_na_prob(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,OPTS.out_image_dir ,"hasAns" ) histogram_na_prob(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,OPTS.out_image_dir ,"noAns" ) if OPTS.out_file: with open(OPTS.out_file ,"w" ) as f: json.dump(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ) else: print(json.dumps(_SCREAMING_SNAKE_CASE ,indent=2 ) ) if __name__ == "__main__": SCREAMING_SNAKE_CASE__ : List[str] = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt main()
48
"""simple docstring""" import sys from collections import defaultdict class lowerCamelCase__ : def __init__( self ): """simple docstring""" snake_case : Dict = [] def lowerCamelCase_ ( self , SCREAMING_SNAKE_CASE ): """simple docstring""" return self.node_position[vertex] def lowerCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" snake_case : Dict = pos def lowerCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" if start > size // 2 - 1: return else: if 2 * start + 2 >= size: snake_case : Any = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: snake_case : Any = 2 * start + 1 else: snake_case : Union[str, Any] = 2 * start + 2 if heap[smallest_child] < heap[start]: snake_case , snake_case : Dict = heap[smallest_child], positions[smallest_child] snake_case , snake_case : Any = ( heap[start], positions[start], ) snake_case , snake_case : str = temp, tempa snake_case : Dict = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , SCREAMING_SNAKE_CASE ) self.top_to_bottom(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" snake_case : Optional[Any] = position[index] while index != 0: snake_case : Dict = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: snake_case : Tuple = heap[parent] snake_case : str = position[parent] self.set_position(position[parent] , SCREAMING_SNAKE_CASE ) else: snake_case : Union[str, Any] = val snake_case : List[Any] = temp self.set_position(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) break snake_case : Optional[Any] = parent else: snake_case : Optional[int] = val snake_case : List[Any] = temp self.set_position(SCREAMING_SNAKE_CASE , 0 ) def lowerCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" snake_case : List[str] = len(SCREAMING_SNAKE_CASE ) // 2 - 1 for i in range(SCREAMING_SNAKE_CASE , -1 , -1 ): self.top_to_bottom(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , len(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) def lowerCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" snake_case : Union[str, Any] = positions[0] snake_case : List[str] = sys.maxsize self.top_to_bottom(SCREAMING_SNAKE_CASE , 0 , len(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) return temp def UpperCamelCase__ ( lowercase__ : Union[str, Any] ): snake_case : Tuple = Heap() snake_case : List[str] = [0] * len(lowercase__ ) snake_case : Optional[int] = [-1] * len(lowercase__ ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph snake_case : Optional[int] = [] # Heap of Distance of vertices from their neighboring vertex snake_case : List[Any] = [] for vertex in range(len(lowercase__ ) ): distance_tv.append(sys.maxsize ) positions.append(lowercase__ ) heap.node_position.append(lowercase__ ) snake_case : Optional[int] = [] snake_case : Union[str, Any] = 1 snake_case : Union[str, Any] = sys.maxsize for neighbor, distance in adjacency_list[0]: snake_case : List[Any] = 0 snake_case : Tuple = distance heap.heapify(lowercase__ , lowercase__ ) for _ in range(1 , len(lowercase__ ) ): snake_case : Optional[Any] = heap.delete_minimum(lowercase__ , lowercase__ ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) snake_case : Optional[int] = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(lowercase__ )] ): snake_case : str = distance heap.bottom_to_top( lowercase__ , heap.get_position(lowercase__ ) , lowercase__ , lowercase__ ) snake_case : Optional[int] = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > __A = int(input("Enter number of edges: ").strip()) __A = defaultdict(list) for _ in range(edges_number): __A = [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))
148
0
'''simple docstring''' from itertools import permutations def _lowerCAmelCase ( _UpperCamelCase : Dict ) -> bool: """simple docstring""" if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False _SCREAMING_SNAKE_CASE =[7, 11, 13, 17] for i, test in enumerate(_UpperCAmelCase ): if (num[i + 4] * 1_00 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def _lowerCAmelCase ( _UpperCamelCase : Dict = 10 ) -> int: """simple docstring""" return sum( int(''.join(map(_UpperCAmelCase , _UpperCAmelCase ) ) ) for num in permutations(range(_UpperCAmelCase ) ) if is_substring_divisible(_UpperCAmelCase ) ) if __name__ == "__main__": print(f'''{solution() = }''')
351
'''simple docstring''' import os import sys import unittest lowerCamelCase : Optional[Any] = 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_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path lowerCamelCase : Optional[int] = os.path.join(git_repo_path, "src", "transformers") lowerCamelCase : Union[str, Any] = "\n{0} = None\n" lowerCamelCase : Optional[Any] = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n" lowerCamelCase : List[Any] = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" class A__ ( unittest.TestCase ): def A ( self : Optional[int] ) -> Dict: '''simple docstring''' _SCREAMING_SNAKE_CASE =find_backend(' _import_structure["models.albert"].append("AlbertTokenizerFast")' ) self.assertIsNone(_a ) _SCREAMING_SNAKE_CASE =find_backend(' if not is_tokenizers_available():' ) self.assertEqual(_a , 'tokenizers' ) _SCREAMING_SNAKE_CASE =find_backend(' if not is_tensorflow_text_available():' ) self.assertEqual(_a , 'tensorflow_text' ) _SCREAMING_SNAKE_CASE =find_backend(' if not (is_sentencepiece_available() and is_tokenizers_available()):' ) self.assertEqual(_a , 'sentencepiece_and_tokenizers' ) _SCREAMING_SNAKE_CASE =find_backend( ' if not (is_sentencepiece_available() and is_tensorflow_text_available()):' ) self.assertEqual(_a , 'sentencepiece_and_tensorflow_text' ) _SCREAMING_SNAKE_CASE =find_backend( ' if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):' ) self.assertEqual(_a , 'sentencepiece_and_tokenizers_and_vision' ) def A ( self : Optional[int] ) -> Any: '''simple docstring''' _SCREAMING_SNAKE_CASE =read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn('torch' , _a ) self.assertIn('tensorflow_text' , _a ) self.assertIn('sentencepiece_and_tokenizers' , _a ) # Likewise, we can't assert on the exact content of a key self.assertIn('BertModel' , objects['torch'] ) self.assertIn('TFBertModel' , objects['tf'] ) self.assertIn('FlaxBertModel' , objects['flax'] ) self.assertIn('BertModel' , objects['torch'] ) self.assertIn('TFBertTokenizer' , objects['tensorflow_text'] ) self.assertIn('convert_slow_tokenizer' , objects['sentencepiece_and_tokenizers'] ) def A ( self : Optional[int] ) -> List[str]: '''simple docstring''' _SCREAMING_SNAKE_CASE =create_dummy_object('CONSTANT' , '\'torch\'' ) self.assertEqual(_a , '\nCONSTANT = None\n' ) _SCREAMING_SNAKE_CASE =create_dummy_object('function' , '\'torch\'' ) self.assertEqual( _a , '\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n' ) _SCREAMING_SNAKE_CASE ='\nclass FakeClass(metaclass=DummyObject):\n _backends = \'torch\'\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, \'torch\')\n' _SCREAMING_SNAKE_CASE =create_dummy_object('FakeClass' , '\'torch\'' ) self.assertEqual(_a , _a ) def A ( self : Optional[int] ) -> Optional[int]: '''simple docstring''' _SCREAMING_SNAKE_CASE ='# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n requires_backends(function, ["torch"])\n\n\nclass FakeClass(metaclass=DummyObject):\n _backends = ["torch"]\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, ["torch"])\n' _SCREAMING_SNAKE_CASE =create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']} ) self.assertEqual(dummy_files['torch'] , _a )
114
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A = { "configuration_electra": ["ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP", "ElectraConfig", "ElectraOnnxConfig"], "tokenization_electra": ["ElectraTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ["ElectraTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST", "ElectraForCausalLM", "ElectraForMaskedLM", "ElectraForMultipleChoice", "ElectraForPreTraining", "ElectraForQuestionAnswering", "ElectraForSequenceClassification", "ElectraForTokenClassification", "ElectraModel", "ElectraPreTrainedModel", "load_tf_weights_in_electra", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFElectraForMaskedLM", "TFElectraForMultipleChoice", "TFElectraForPreTraining", "TFElectraForQuestionAnswering", "TFElectraForSequenceClassification", "TFElectraForTokenClassification", "TFElectraModel", "TFElectraPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "FlaxElectraForCausalLM", "FlaxElectraForMaskedLM", "FlaxElectraForMultipleChoice", "FlaxElectraForPreTraining", "FlaxElectraForQuestionAnswering", "FlaxElectraForSequenceClassification", "FlaxElectraForTokenClassification", "FlaxElectraModel", "FlaxElectraPreTrainedModel", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys __A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
177
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING __A = logging.get_logger(__name__) __A = { "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json", } class UpperCAmelCase (_UpperCAmelCase ): """simple docstring""" _UpperCAmelCase :Dict = "deta" _UpperCAmelCase :Any = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , _UpperCAmelCase=None , _UpperCAmelCase=900 , _UpperCAmelCase=2048 , _UpperCAmelCase=6 , _UpperCAmelCase=2048 , _UpperCAmelCase=8 , _UpperCAmelCase=6 , _UpperCAmelCase=1024 , _UpperCAmelCase=8 , _UpperCAmelCase=0.0 , _UpperCAmelCase=True , _UpperCAmelCase="relu" , _UpperCAmelCase=256 , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.0 , _UpperCAmelCase=0.0 , _UpperCAmelCase=0.02 , _UpperCAmelCase=1.0 , _UpperCAmelCase=True , _UpperCAmelCase=False , _UpperCAmelCase="sine" , _UpperCAmelCase=5 , _UpperCAmelCase=4 , _UpperCAmelCase=4 , _UpperCAmelCase=True , _UpperCAmelCase=300 , _UpperCAmelCase=True , _UpperCAmelCase=True , _UpperCAmelCase=1 , _UpperCAmelCase=5 , _UpperCAmelCase=2 , _UpperCAmelCase=1 , _UpperCAmelCase=1 , _UpperCAmelCase=5 , _UpperCAmelCase=2 , _UpperCAmelCase=0.1 , _UpperCAmelCase=0.25 , **_UpperCAmelCase , ): if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) lowercase__: str = CONFIG_MAPPING['''resnet'''](out_features=['''stage2''', '''stage3''', '''stage4'''] ) else: if isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowercase__: Tuple = backbone_config.pop('''model_type''' ) lowercase__: str = CONFIG_MAPPING[backbone_model_type] lowercase__: Optional[int] = config_class.from_dict(_UpperCAmelCase ) lowercase__: int = backbone_config lowercase__: Any = num_queries lowercase__: List[str] = max_position_embeddings lowercase__: Optional[Any] = d_model lowercase__: List[Any] = encoder_ffn_dim lowercase__: Tuple = encoder_layers lowercase__: Dict = encoder_attention_heads lowercase__: Any = decoder_ffn_dim lowercase__: Union[str, Any] = decoder_layers lowercase__: List[Any] = decoder_attention_heads lowercase__: int = dropout lowercase__: List[str] = attention_dropout lowercase__: Tuple = activation_dropout lowercase__: Tuple = activation_function lowercase__: int = init_std lowercase__: Optional[Any] = init_xavier_std lowercase__: Optional[Any] = encoder_layerdrop lowercase__: Optional[int] = auxiliary_loss lowercase__: Union[str, Any] = position_embedding_type # deformable attributes lowercase__: List[str] = num_feature_levels lowercase__: Optional[Any] = encoder_n_points lowercase__: int = decoder_n_points lowercase__: str = two_stage lowercase__: Optional[int] = two_stage_num_proposals lowercase__: Tuple = with_box_refine lowercase__: str = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError('''If two_stage is True, with_box_refine must be True.''' ) # Hungarian matcher lowercase__: Union[str, Any] = class_cost lowercase__: Optional[int] = bbox_cost lowercase__: int = giou_cost # Loss coefficients lowercase__: Optional[int] = mask_loss_coefficient lowercase__: List[str] = dice_loss_coefficient lowercase__: str = bbox_loss_coefficient lowercase__: Union[str, Any] = giou_loss_coefficient lowercase__: Optional[int] = eos_coefficient lowercase__: str = focal_alpha super().__init__(is_encoder_decoder=_UpperCAmelCase , **_UpperCAmelCase ) @property def _snake_case ( self ): return self.encoder_attention_heads @property def _snake_case ( self ): return self.d_model def _snake_case ( self ): lowercase__: Union[str, Any] = copy.deepcopy(self.__dict__ ) lowercase__: Dict = self.backbone_config.to_dict() lowercase__: Union[str, Any] = self.__class__.model_type return output
177
1
A_ : Dict = frozenset( [ 'prompt', 'height', 'width', 'guidance_scale', 'negative_prompt', 'prompt_embeds', 'negative_prompt_embeds', 'cross_attention_kwargs', ] ) A_ : Union[str, Any] = frozenset(['prompt', 'negative_prompt']) A_ : Optional[int] = frozenset([]) A_ : Any = frozenset(['image']) A_ : int = frozenset( [ 'image', 'height', 'width', 'guidance_scale', ] ) A_ : List[Any] = frozenset(['image']) A_ : Optional[Any] = frozenset( [ 'prompt', 'image', 'height', 'width', 'guidance_scale', 'negative_prompt', 'prompt_embeds', 'negative_prompt_embeds', ] ) A_ : int = frozenset(['prompt', 'image', 'negative_prompt']) A_ : List[Any] = frozenset( [ # Text guided image variation with an image mask 'prompt', 'image', 'mask_image', 'height', 'width', 'guidance_scale', 'negative_prompt', 'prompt_embeds', 'negative_prompt_embeds', ] ) A_ : Tuple = frozenset(['prompt', 'image', 'mask_image', 'negative_prompt']) A_ : Union[str, Any] = frozenset( [ # image variation with an image mask 'image', 'mask_image', 'height', 'width', 'guidance_scale', ] ) A_ : Dict = frozenset(['image', 'mask_image']) A_ : List[Any] = frozenset( [ 'example_image', 'image', 'mask_image', 'height', 'width', 'guidance_scale', ] ) A_ : List[Any] = frozenset(['example_image', 'image', 'mask_image']) A_ : str = frozenset(['class_labels']) A_ : List[Any] = frozenset(['class_labels']) A_ : List[str] = frozenset(['batch_size']) A_ : Optional[Any] = frozenset([]) A_ : str = frozenset(['batch_size']) A_ : Optional[int] = frozenset([]) A_ : Optional[int] = frozenset( [ 'prompt', 'audio_length_in_s', 'guidance_scale', 'negative_prompt', 'prompt_embeds', 'negative_prompt_embeds', 'cross_attention_kwargs', ] ) A_ : List[str] = frozenset(['prompt', 'negative_prompt']) A_ : int = frozenset(['input_tokens']) A_ : Dict = frozenset(['input_tokens'])
362
from __future__ import annotations def UpperCamelCase (lowercase_: list[int] , lowercase_: list[int] , lowercase_: int ) -> tuple[float, list[float]]: A__ : Tuple = list(range(len(lowercase_ ) ) ) A__ : Union[str, Any] = [v / w for v, w in zip(lowercase_ , lowercase_ )] index.sort(key=lambda lowercase_ : ratio[i] , reverse=lowercase_ ) A__ : float = 0 A__ : list[float] = [0] * len(lowercase_ ) for i in index: if weight[i] <= capacity: A__ : Optional[int] = 1 max_value += value[i] capacity -= weight[i] else: A__ : Union[str, Any] = capacity / weight[i] max_value += value[i] * capacity / weight[i] break return max_value, fractions if __name__ == "__main__": import doctest doctest.testmod()
141
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import copy import importlib.metadata import json import os from dataclasses import dataclass from typing import Any, Dict, Union from packaging import version from ..utils import is_torch_available, logging if is_torch_available(): import torch __UpperCAmelCase = logging.get_logger(__name__) @dataclass class lowerCamelCase : '''simple docstring''' def __init__( self , _UpperCamelCase=False , _UpperCamelCase=False , _UpperCamelCase=6.0 , _UpperCamelCase=None , _UpperCamelCase=False , _UpperCamelCase=False , _UpperCamelCase=None , _UpperCamelCase="fp4" , _UpperCamelCase=False , **_UpperCamelCase , ) -> Optional[int]: UpperCAmelCase_ : Union[str, Any] = load_in_abit UpperCAmelCase_ : Any = load_in_abit UpperCAmelCase_ : List[Any] = llm_inta_threshold UpperCAmelCase_ : Tuple = llm_inta_skip_modules UpperCAmelCase_ : Tuple = llm_inta_enable_fpaa_cpu_offload UpperCAmelCase_ : Optional[Any] = llm_inta_has_fpaa_weight UpperCAmelCase_ : Union[str, Any] = bnb_abit_quant_type UpperCAmelCase_ : Dict = bnb_abit_use_double_quant if bnb_abit_compute_dtype is None: UpperCAmelCase_ : int = torch.floataa elif isinstance(_UpperCamelCase , _UpperCamelCase ): UpperCAmelCase_ : str = getattr(_UpperCamelCase , _UpperCamelCase ) elif isinstance(_UpperCamelCase , torch.dtype ): UpperCAmelCase_ : Optional[Any] = bnb_abit_compute_dtype else: raise ValueError('bnb_4bit_compute_dtype must be a string or a torch.dtype' ) self.post_init() def __UpperCAmelCase ( self ) -> int: if not isinstance(self.llm_inta_threshold , _UpperCamelCase ): raise ValueError('llm_int8_threshold must be a float' ) if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules , _UpperCamelCase ): raise ValueError('llm_int8_skip_modules must be a list of strings' ) if not isinstance(self.llm_inta_enable_fpaa_cpu_offload , _UpperCamelCase ): raise ValueError('llm_int8_enable_fp32_cpu_offload must be a boolean' ) if not isinstance(self.llm_inta_has_fpaa_weight , _UpperCamelCase ): raise ValueError('llm_int8_has_fp16_weight must be a boolean' ) if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype , torch.dtype ): raise ValueError('bnb_4bit_compute_dtype must be torch.dtype' ) if not isinstance(self.bnb_abit_quant_type , _UpperCamelCase ): raise ValueError('bnb_4bit_quant_type must be a string' ) if not isinstance(self.bnb_abit_use_double_quant , _UpperCamelCase ): raise ValueError('bnb_4bit_use_double_quant must be a boolean' ) if self.load_in_abit and not version.parse(importlib.metadata.version('bitsandbytes' ) ) >= version.parse( '0.39.0' ): raise ValueError( '4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version' ) def __UpperCAmelCase ( self ) -> str: return self.load_in_abit or self.load_in_abit def __UpperCAmelCase ( self ) -> List[str]: if self.load_in_abit: return "llm_int8" elif self.load_in_abit and self.bnb_abit_quant_type == "fp4": return "fp4" elif self.load_in_abit and self.bnb_abit_quant_type == "nf4": return "nf4" else: return None @classmethod def __UpperCAmelCase ( cls , _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) -> Tuple: UpperCAmelCase_ : str = cls(**_UpperCamelCase ) UpperCAmelCase_ : Dict = [] for key, value in kwargs.items(): if hasattr(_UpperCamelCase , _UpperCamelCase ): setattr(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) to_remove.append(_UpperCamelCase ) for key in to_remove: kwargs.pop(_UpperCamelCase , _UpperCamelCase ) if return_unused_kwargs: return config, kwargs else: return config def __UpperCAmelCase ( self , _UpperCamelCase ) -> int: with open(_UpperCamelCase , 'w' , encoding='utf-8' ) as writer: UpperCAmelCase_ : Union[str, Any] = self.to_dict() UpperCAmelCase_ : Optional[Any] = json.dumps(_UpperCamelCase , indent=2 , sort_keys=_UpperCamelCase ) + '\n' writer.write(_UpperCamelCase ) def __UpperCAmelCase ( self ) -> Dict[str, Any]: UpperCAmelCase_ : Optional[Any] = copy.deepcopy(self.__dict__ ) UpperCAmelCase_ : Any = str(output['bnb_4bit_compute_dtype'] ).split('.' )[1] return output def __repr__( self ) -> Optional[Any]: return f"{self.__class__.__name__} {self.to_json_string()}" def __UpperCAmelCase ( self , _UpperCamelCase = True ) -> str: if use_diff is True: UpperCAmelCase_ : Tuple = self.to_diff_dict() else: UpperCAmelCase_ : Dict = self.to_dict() return json.dumps(_UpperCamelCase , indent=2 , sort_keys=_UpperCamelCase ) + "\n" def __UpperCAmelCase ( self ) -> Dict[str, Any]: UpperCAmelCase_ : str = self.to_dict() # get the default config dict UpperCAmelCase_ : Optional[Any] = BitsAndBytesConfig().to_dict() UpperCAmelCase_ : Optional[int] = {} # only serialize values that differ from the default config for key, value in config_dict.items(): if value != default_config_dict[key]: UpperCAmelCase_ : List[str] = value return serializable_config_dict
29
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __a :List[str] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a :Union[str, Any] = { 'vocab_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt' ), 'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt', 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json' ), 'google/electra-base-generator': ( 'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json' ), 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json' ), }, } __a :Optional[int] = { 'google/electra-small-generator': 512, 'google/electra-base-generator': 512, 'google/electra-large-generator': 512, 'google/electra-small-discriminator': 512, 'google/electra-base-discriminator': 512, 'google/electra-large-discriminator': 512, } __a :str = { 'google/electra-small-generator': {'do_lower_case': True}, 'google/electra-base-generator': {'do_lower_case': True}, 'google/electra-large-generator': {'do_lower_case': True}, 'google/electra-small-discriminator': {'do_lower_case': True}, 'google/electra-base-discriminator': {'do_lower_case': True}, 'google/electra-large-discriminator': {'do_lower_case': True}, } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = VOCAB_FILES_NAMES _lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : int = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : int = ElectraTokenizer def __init__( self : Tuple , UpperCAmelCase : Dict=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Any=True , UpperCAmelCase : Any="[UNK]" , UpperCAmelCase : Union[str, Any]="[SEP]" , UpperCAmelCase : List[Any]="[PAD]" , UpperCAmelCase : Union[str, Any]="[CLS]" , UpperCAmelCase : List[Any]="[MASK]" , UpperCAmelCase : List[str]=True , UpperCAmelCase : Any=None , **UpperCAmelCase : Union[str, Any] , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) A_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , UpperCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , UpperCAmelCase ) != tokenize_chinese_chars ): A_ = getattr(UpperCAmelCase , normalizer_state.pop("type" ) ) A_ = do_lower_case A_ = strip_accents A_ = tokenize_chinese_chars A_ = normalizer_class(**UpperCAmelCase ) A_ = do_lower_case def __A ( self : int , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any]=None ): A_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A ( self : Union[str, Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): 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 : Tuple , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): A_ = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
312
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a : Union[str, Any] = logging.get_logger(__name__) a : Any = { 'sail/poolformer_s12': 'https://huggingface.co/sail/poolformer_s12/resolve/main/config.json', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class _a ( _lowerCAmelCase ): A = '''poolformer''' def __init__(self, SCREAMING_SNAKE_CASE_=3, SCREAMING_SNAKE_CASE_=16, SCREAMING_SNAKE_CASE_=16, SCREAMING_SNAKE_CASE_=3, SCREAMING_SNAKE_CASE_=4.0, SCREAMING_SNAKE_CASE_=[2, 2, 6, 2], SCREAMING_SNAKE_CASE_=[64, 128, 320, 512], SCREAMING_SNAKE_CASE_=[7, 3, 3, 3], SCREAMING_SNAKE_CASE_=[4, 2, 2, 2], SCREAMING_SNAKE_CASE_=[2, 1, 1, 1], SCREAMING_SNAKE_CASE_=4, SCREAMING_SNAKE_CASE_=0.0, SCREAMING_SNAKE_CASE_="gelu", SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=1E-5, SCREAMING_SNAKE_CASE_=0.0_2, **SCREAMING_SNAKE_CASE_, ) -> Any: UpperCAmelCase_: Any = num_channels UpperCAmelCase_: Optional[int] = patch_size UpperCAmelCase_: Any = stride UpperCAmelCase_: Any = padding UpperCAmelCase_: List[str] = pool_size UpperCAmelCase_: Optional[Any] = hidden_sizes UpperCAmelCase_: Tuple = mlp_ratio UpperCAmelCase_: Any = depths UpperCAmelCase_: Tuple = patch_sizes UpperCAmelCase_: Dict = strides UpperCAmelCase_: List[Any] = num_encoder_blocks UpperCAmelCase_: str = drop_path_rate UpperCAmelCase_: Optional[int] = hidden_act UpperCAmelCase_: Optional[Any] = use_layer_scale UpperCAmelCase_: Tuple = layer_scale_init_value UpperCAmelCase_: Optional[int] = initializer_range super().__init__(**SCREAMING_SNAKE_CASE_ ) class _a ( _lowerCAmelCase ): A = version.parse('''1.11''' ) @property def __snake_case (self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def __snake_case (self ) -> float: return 2E-3
352
def lowerCAmelCase_ (lowerCAmelCase__: int , lowerCAmelCase__: float , lowerCAmelCase__: float ): """simple docstring""" return round(float(moles / volume ) * nfactor ) def lowerCAmelCase_ (lowerCAmelCase__: float , lowerCAmelCase__: float , lowerCAmelCase__: float ): """simple docstring""" return round(float((moles * 0.0821 * temperature) / (volume) ) ) def lowerCAmelCase_ (lowerCAmelCase__: float , lowerCAmelCase__: float , lowerCAmelCase__: float ): """simple docstring""" return round(float((moles * 0.0821 * temperature) / (pressure) ) ) def lowerCAmelCase_ (lowerCAmelCase__: float , lowerCAmelCase__: float , lowerCAmelCase__: float ): """simple docstring""" return round(float((pressure * volume) / (0.0821 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
82
0
'''simple docstring''' import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class _a ( SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' A : Tuple = ProphetNetTokenizer A : Optional[Any] = False def UpperCamelCase_ ( self ): '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : List[str] = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] SCREAMING_SNAKE_CASE : Optional[Any] = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file, 'w', encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def UpperCamelCase_ ( self, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 'UNwant\u00E9d,running' SCREAMING_SNAKE_CASE : Union[str, Any] = 'unwanted, running' return input_text, output_text def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(A, ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A ), [9, 6, 7, 12, 10, 11] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ), ['ah', '\u535A', '\u63A8', 'zz'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = BasicTokenizer(do_lower_case=A ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ), ['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ), ['hello'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = BasicTokenizer(do_lower_case=A, strip_accents=A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ), ['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ), ['h\u00E9llo'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = BasicTokenizer(do_lower_case=A, strip_accents=A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ), ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ), ['hello'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = BasicTokenizer(do_lower_case=A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ), ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ), ['hello'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = BasicTokenizer(do_lower_case=A ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ), ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = BasicTokenizer(do_lower_case=A, strip_accents=A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ), ['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = BasicTokenizer(do_lower_case=A, strip_accents=A ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ), ['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = BasicTokenizer(do_lower_case=A, never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ), ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] SCREAMING_SNAKE_CASE : str = {} for i, token in enumerate(A ): SCREAMING_SNAKE_CASE : str = i SCREAMING_SNAKE_CASE : Optional[int] = WordpieceTokenizer(vocab=A, unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ), [] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ), ['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ), ['[UNK]', 'runn', '##ing'] ) @require_torch def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) SCREAMING_SNAKE_CASE : List[Any] = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] SCREAMING_SNAKE_CASE : List[str] = [1_037, 2_146, 20_423, 2_005, 7_680, 7_849, 3_989, 1_012, 102] SCREAMING_SNAKE_CASE : Dict = tokenizer(A, padding=A, return_tensors='pt' ) self.assertIsInstance(A, A ) SCREAMING_SNAKE_CASE : Union[str, Any] = list(batch.input_ids.numpy()[0] ) self.assertListEqual(A, A ) self.assertEqual((2, 9), batch.input_ids.shape ) self.assertEqual((2, 9), batch.attention_mask.shape ) def UpperCamelCase_ ( self ): '''simple docstring''' self.assertTrue(_is_whitespace(' ' ) ) self.assertTrue(_is_whitespace('\t' ) ) self.assertTrue(_is_whitespace('\r' ) ) self.assertTrue(_is_whitespace('\n' ) ) self.assertTrue(_is_whitespace('\u00A0' ) ) self.assertFalse(_is_whitespace('A' ) ) self.assertFalse(_is_whitespace('-' ) ) def UpperCamelCase_ ( self ): '''simple docstring''' self.assertTrue(_is_control('\u0005' ) ) self.assertFalse(_is_control('A' ) ) self.assertFalse(_is_control(' ' ) ) self.assertFalse(_is_control('\t' ) ) self.assertFalse(_is_control('\r' ) ) def UpperCamelCase_ ( self ): '''simple docstring''' self.assertTrue(_is_punctuation('-' ) ) self.assertTrue(_is_punctuation('$' ) ) self.assertTrue(_is_punctuation('`' ) ) self.assertTrue(_is_punctuation('.' ) ) self.assertFalse(_is_punctuation('A' ) ) self.assertFalse(_is_punctuation(' ' ) ) @slow def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.encode('sequence builders', add_special_tokens=A ) SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.encode('multi-sequence build', add_special_tokens=A ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.build_inputs_with_special_tokens(A ) SCREAMING_SNAKE_CASE : Dict = tokenizer.build_inputs_with_special_tokens(A, A ) assert encoded_sentence == text + [102] assert encoded_pair == text + [102] + text_a + [102]
251
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCamelCase_ = { "configuration_xlm": ["XLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMConfig", "XLMOnnxConfig"], "tokenization_xlm": ["XLMTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ "XLM_PRETRAINED_MODEL_ARCHIVE_LIST", "XLMForMultipleChoice", "XLMForQuestionAnswering", "XLMForQuestionAnsweringSimple", "XLMForSequenceClassification", "XLMForTokenClassification", "XLMModel", "XLMPreTrainedModel", "XLMWithLMHeadModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ "TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLMForMultipleChoice", "TFXLMForQuestionAnsweringSimple", "TFXLMForSequenceClassification", "TFXLMForTokenClassification", "TFXLMMainLayer", "TFXLMModel", "TFXLMPreTrainedModel", "TFXLMWithLMHeadModel", ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
251
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def _a ( a :str ) -> YolosConfig: a = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: a = 192 a = 768 a = 12 a = 3 a = [800, 1_333] a = False elif yolos_name == "yolos_s_dWr": a = 330 a = 14 a = 6 a = 1_320 elif "yolos_s" in yolos_name: a = 384 a = 1_536 a = 12 a = 6 elif "yolos_b" in yolos_name: a = [800, 1_344] a = 91 a = '''huggingface/label-files''' a = '''coco-detection-id2label.json''' a = json.load(open(hf_hub_download(a , a , repo_type='''dataset''' ) , '''r''' ) ) a = {int(a ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} return config def _a ( a :dict , a :YolosConfig , a :bool = False ) -> int: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) a = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) a = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict a = in_proj_weight[: config.hidden_size, :] a = in_proj_bias[: config.hidden_size] a = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] a = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] a = in_proj_weight[-config.hidden_size :, :] a = in_proj_bias[-config.hidden_size :] def _a ( a :str ) -> str: if "backbone" in name: a = name.replace('''backbone''' , '''vit''' ) if "cls_token" in name: a = name.replace('''cls_token''' , '''embeddings.cls_token''' ) if "det_token" in name: a = name.replace('''det_token''' , '''embeddings.detection_tokens''' ) if "mid_pos_embed" in name: a = name.replace('''mid_pos_embed''' , '''encoder.mid_position_embeddings''' ) if "pos_embed" in name: a = name.replace('''pos_embed''' , '''embeddings.position_embeddings''' ) if "patch_embed.proj" in name: a = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "blocks" in name: a = name.replace('''blocks''' , '''encoder.layer''' ) if "attn.proj" in name: a = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: a = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: a = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: a = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: a = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: a = name.replace('''mlp.fc2''' , '''output.dense''' ) if "class_embed" in name: a = name.replace('''class_embed''' , '''class_labels_classifier''' ) if "bbox_embed" in name: a = name.replace('''bbox_embed''' , '''bbox_predictor''' ) if "vit.norm" in name: a = name.replace('''vit.norm''' , '''vit.layernorm''' ) return name def _a ( a :dict , a :YolosForObjectDetection ) -> dict: for key in orig_state_dict.copy().keys(): a = orig_state_dict.pop(a ) if "qkv" in key: a = key.split('''.''' ) a = int(key_split[2] ) a = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: a = val[:dim, :] a = val[ dim : dim * 2, : ] a = val[-dim:, :] else: a = val[:dim] a = val[dim : dim * 2] a = val[-dim:] else: a = val return orig_state_dict def _a ( ) -> torch.Tensor: a = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a = Image.open(requests.get(a , stream=a ).raw ) return im @torch.no_grad() def _a ( a :str , a :str , a :str , a :bool = False ) -> List[str]: a = get_yolos_config(a ) # load original state_dict a = torch.load(a , map_location='''cpu''' )['''model'''] # load 🤗 model a = YolosForObjectDetection(a ) model.eval() a = convert_state_dict(a , a ) model.load_state_dict(a ) # Check outputs on an image, prepared by YolosImageProcessor a = 800 if yolos_name != '''yolos_ti''' else 512 a = YolosImageProcessor(format='''coco_detection''' , size=a ) a = image_processor(images=prepare_img() , return_tensors='''pt''' ) a = model(**a ) a , a = outputs.logits, outputs.pred_boxes a , a = None, None if yolos_name == "yolos_ti": a = torch.tensor( [[-39.5_022, -11.9_820, -17.6_888], [-29.9_574, -9.9_769, -17.7_691], [-42.3_281, -20.7_200, -30.6_294]] ) a = torch.tensor( [[0.4_021, 0.0_836, 0.7_979], [0.0_184, 0.2_609, 0.0_364], [0.1_781, 0.2_004, 0.2_095]] ) elif yolos_name == "yolos_s_200_pre": a = torch.tensor( [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]] ) a = torch.tensor( [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]] ) elif yolos_name == "yolos_s_300_pre": a = torch.tensor( [[-36.2_220, -14.4_385, -23.5_457], [-35.6_970, -14.7_583, -21.3_935], [-31.5_939, -13.6_042, -16.8_049]] ) a = torch.tensor( [[0.7_614, 0.2_316, 0.4_728], [0.7_168, 0.4_495, 0.3_855], [0.4_996, 0.1_466, 0.9_996]] ) elif yolos_name == "yolos_s_dWr": a = torch.tensor( [[-42.8_668, -24.1_049, -41.1_690], [-34.7_456, -14.1_274, -24.9_194], [-33.7_898, -12.1_946, -25.6_495]] ) a = torch.tensor( [[0.5_587, 0.2_773, 0.0_605], [0.5_004, 0.3_014, 0.9_994], [0.4_999, 0.1_548, 0.9_994]] ) elif yolos_name == "yolos_base": a = torch.tensor( [[-40.6_064, -24.3_084, -32.6_447], [-55.1_990, -30.7_719, -35.5_877], [-51.4_311, -33.3_507, -35.6_462]] ) a = torch.tensor( [[0.5_555, 0.2_794, 0.0_655], [0.9_049, 0.2_664, 0.1_894], [0.9_183, 0.1_984, 0.1_635]] ) else: raise ValueError(F"""Unknown yolos_name: {yolos_name}""" ) assert torch.allclose(logits[0, :3, :3] , a , atol=1e-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , a , atol=1e-4 ) Path(a ).mkdir(exist_ok=a ) print(F"""Saving model {yolos_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(a ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(a ) if push_to_hub: a = { '''yolos_ti''': '''yolos-tiny''', '''yolos_s_200_pre''': '''yolos-small''', '''yolos_s_300_pre''': '''yolos-small-300''', '''yolos_s_dWr''': '''yolos-small-dwr''', '''yolos_base''': '''yolos-base''', } print('''Pushing to the hub...''' ) a = model_mapping[yolos_name] image_processor.push_to_hub(a , organization='''hustvl''' ) model.push_to_hub(a , organization='''hustvl''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--yolos_name", default="yolos_s_200_pre", type=str, help=( "Name of the YOLOS model you'd like to convert. Should be one of 'yolos_ti', 'yolos_s_200_pre'," " 'yolos_s_300_pre', 'yolos_s_dWr', 'yolos_base'." ), ) parser.add_argument( "--checkpoint_path", default=None, type=str, help="Path to the original state dict (.pth file)." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub." ) UpperCAmelCase__ = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
26
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def _a ( a :Union[str, Any] , a :List[Any] ) -> List[Any]: a = checkpoint a = {} a = vae_state_dict['''encoder.conv_in.weight'''] a = vae_state_dict['''encoder.conv_in.bias'''] a = vae_state_dict['''encoder.conv_out.weight'''] a = vae_state_dict['''encoder.conv_out.bias'''] a = vae_state_dict['''encoder.norm_out.weight'''] a = vae_state_dict['''encoder.norm_out.bias'''] a = vae_state_dict['''decoder.conv_in.weight'''] a = vae_state_dict['''decoder.conv_in.bias'''] a = vae_state_dict['''decoder.conv_out.weight'''] a = vae_state_dict['''decoder.conv_out.bias'''] a = vae_state_dict['''decoder.norm_out.weight'''] a = vae_state_dict['''decoder.norm_out.bias'''] a = vae_state_dict['''quant_conv.weight'''] a = vae_state_dict['''quant_conv.bias'''] a = vae_state_dict['''post_quant_conv.weight'''] a = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""down.{layer_id}""" in key] for layer_id in range(a ) } # Retrieves the keys for the decoder up blocks only a = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) a = { layer_id: [key for key in vae_state_dict if F"""up.{layer_id}""" in key] for layer_id in range(a ) } for i in range(a ): a = [key for key in down_blocks[i] if F"""down.{i}""" in key and F"""down.{i}.downsample""" not in key] if F"""encoder.down.{i}.downsample.conv.weight""" in vae_state_dict: a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.weight""" ) a = vae_state_dict.pop( F"""encoder.down.{i}.downsample.conv.bias""" ) a = renew_vae_resnet_paths(a ) a = {'''old''': F"""down.{i}.block""", '''new''': F"""down_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""encoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) for i in range(a ): a = num_up_blocks - 1 - i a = [ key for key in up_blocks[block_id] if F"""up.{block_id}""" in key and F"""up.{block_id}.upsample""" not in key ] if F"""decoder.up.{block_id}.upsample.conv.weight""" in vae_state_dict: a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.weight""" ] a = vae_state_dict[ F"""decoder.up.{block_id}.upsample.conv.bias""" ] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""up.{block_id}.block""", '''new''': F"""up_blocks.{i}.resnets"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.block''' in key] a = 2 for i in range(1 , num_mid_res_blocks + 1 ): a = [key for key in mid_resnets if F"""decoder.mid.block_{i}""" in key] a = renew_vae_resnet_paths(a ) a = {'''old''': F"""mid.block_{i}""", '''new''': F"""mid_block.resnets.{i - 1}"""} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) a = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] a = renew_vae_attention_paths(a ) a = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a , a , a , additional_replacements=[meta_path] , config=a ) conv_attn_to_linear(a ) return new_checkpoint def _a ( a :str , a :str , ) -> List[str]: # Only support V1 a = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) a = io.BytesIO(r.content ) a = OmegaConf.load(a ) a = 512 a = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open a = {} with safe_open(a , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): a = f.get_tensor(a ) else: a = torch.load(a , map_location=a )['''state_dict'''] # Convert the VAE model. a = create_vae_diffusers_config(a , image_size=a ) a = custom_convert_ldm_vae_checkpoint(a , a ) a = AutoencoderKL(**a ) vae.load_state_dict(a ) vae.save_pretrained(a ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") UpperCAmelCase__ = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
26
1
'''simple docstring''' import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def _a( UpperCamelCase__ : bytes, UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Any =f"{sampling_rate}" SCREAMING_SNAKE_CASE__ : Dict ='''1''' SCREAMING_SNAKE_CASE__ : str ='''f32le''' SCREAMING_SNAKE_CASE__ : str =[ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(__a, stdin=subprocess.PIPE, stdout=subprocess.PIPE ) as ffmpeg_process: SCREAMING_SNAKE_CASE__ : List[str] =ffmpeg_process.communicate(__a ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error SCREAMING_SNAKE_CASE__ : Tuple =output_stream[0] SCREAMING_SNAKE_CASE__ : Union[str, Any] =np.frombuffer(__a, np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def _a( UpperCamelCase__ : int, UpperCamelCase__ : float, UpperCamelCase__ : str = "f32le", ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Union[str, Any] =f"{sampling_rate}" SCREAMING_SNAKE_CASE__ : Union[str, Any] ='''1''' if format_for_conversion == "s16le": SCREAMING_SNAKE_CASE__ : Optional[int] =2 elif format_for_conversion == "f32le": SCREAMING_SNAKE_CASE__ : int =4 else: raise ValueError(f"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`" ) SCREAMING_SNAKE_CASE__ : Tuple =platform.system() if system == "Linux": SCREAMING_SNAKE_CASE__ : List[str] ='''alsa''' SCREAMING_SNAKE_CASE__ : List[str] ='''default''' elif system == "Darwin": SCREAMING_SNAKE_CASE__ : List[str] ='''avfoundation''' SCREAMING_SNAKE_CASE__ : Optional[int] =''':0''' elif system == "Windows": SCREAMING_SNAKE_CASE__ : Any ='''dshow''' SCREAMING_SNAKE_CASE__ : Tuple ='''default''' SCREAMING_SNAKE_CASE__ : str =[ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] SCREAMING_SNAKE_CASE__ : List[Any] =int(round(sampling_rate * chunk_length_s ) ) * size_of_sample SCREAMING_SNAKE_CASE__ : List[str] =_ffmpeg_stream(__a, __a ) for item in iterator: yield item def _a( UpperCamelCase__ : int, UpperCamelCase__ : float, UpperCamelCase__ : Optional[int] = None, UpperCamelCase__ : Optional[Union[Tuple[float, float], float]] = None, UpperCamelCase__ : str = "f32le", ): '''simple docstring''' if stream_chunk_s is not None: SCREAMING_SNAKE_CASE__ : Union[str, Any] =stream_chunk_s else: SCREAMING_SNAKE_CASE__ : Any =chunk_length_s SCREAMING_SNAKE_CASE__ : Union[str, Any] =ffmpeg_microphone(__a, __a, format_for_conversion=__a ) if format_for_conversion == "s16le": SCREAMING_SNAKE_CASE__ : str =np.intaa SCREAMING_SNAKE_CASE__ : Tuple =2 elif format_for_conversion == "f32le": SCREAMING_SNAKE_CASE__ : List[str] =np.floataa SCREAMING_SNAKE_CASE__ : List[Any] =4 else: raise ValueError(f"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`" ) if stride_length_s is None: SCREAMING_SNAKE_CASE__ : List[str] =chunk_length_s / 6 SCREAMING_SNAKE_CASE__ : Any =int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__a, (int, float) ): SCREAMING_SNAKE_CASE__ : str =[stride_length_s, stride_length_s] SCREAMING_SNAKE_CASE__ : List[Any] =int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample SCREAMING_SNAKE_CASE__ : Optional[int] =int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample SCREAMING_SNAKE_CASE__ : Optional[int] =datetime.datetime.now() SCREAMING_SNAKE_CASE__ : Tuple =datetime.timedelta(seconds=__a ) for item in chunk_bytes_iter(__a, __a, stride=(stride_left, stride_right), stream=__a ): # Put everything back in numpy scale SCREAMING_SNAKE_CASE__ : List[str] =np.frombuffer(item['''raw'''], dtype=__a ) SCREAMING_SNAKE_CASE__ : Optional[Any] =( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) SCREAMING_SNAKE_CASE__ : List[Any] =sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 1_0 * delta: # We're late !! SKIP continue yield item def _a( UpperCamelCase__ : Any, UpperCamelCase__ : int, UpperCamelCase__ : Tuple[int, int], UpperCamelCase__ : bool = False ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str =B'''''' SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : int =stride if stride_left + stride_right >= chunk_len: raise ValueError( f"Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}" ) SCREAMING_SNAKE_CASE__ : Tuple =0 for raw in iterator: acc += raw if stream and len(__a ) < chunk_len: SCREAMING_SNAKE_CASE__ : Dict =(_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__a ) >= chunk_len: # We are flushing the accumulator SCREAMING_SNAKE_CASE__ : Optional[int] =(_stride_left, stride_right) SCREAMING_SNAKE_CASE__ : Optional[Any] ={'''raw''': acc[:chunk_len], '''stride''': stride} if stream: SCREAMING_SNAKE_CASE__ : Any =False yield item SCREAMING_SNAKE_CASE__ : List[str] =stride_left SCREAMING_SNAKE_CASE__ : int =acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__a ) > stride_left: SCREAMING_SNAKE_CASE__ : Any ={'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: SCREAMING_SNAKE_CASE__ : List[Any] =False yield item def _a( UpperCamelCase__ : Optional[Any], UpperCamelCase__ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[Any] =2**2_4 # 16Mo try: with subprocess.Popen(__a, stdout=subprocess.PIPE, bufsize=__a ) as ffmpeg_process: while True: SCREAMING_SNAKE_CASE__ : Tuple =ffmpeg_process.stdout.read(__a ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
152
import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def __magic_name__ ( __a : List[str] , __a : List[Any] , __a : int , __a : Optional[int]=None , __a : Union[str, Any]=None , __a : Union[str, Any]=None , __a : Union[str, Any]=None , __a : Tuple=None , ): '''simple docstring''' if attention_mask is None: UpperCamelCase__ = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: UpperCamelCase__ = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: UpperCamelCase__ = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=__a ) if decoder_head_mask is None: UpperCamelCase__ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__a ) if cross_attn_head_mask is None: UpperCamelCase__ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__a ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class __A: """simple docstring""" def __init__(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=13 , SCREAMING_SNAKE_CASE_=7 , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=99 , SCREAMING_SNAKE_CASE_=16 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_="relu" , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=20 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=1 , SCREAMING_SNAKE_CASE_=0 , ): UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = seq_length UpperCamelCase__ = is_training UpperCamelCase__ = use_labels UpperCamelCase__ = vocab_size UpperCamelCase__ = hidden_size UpperCamelCase__ = num_hidden_layers UpperCamelCase__ = num_attention_heads UpperCamelCase__ = intermediate_size UpperCamelCase__ = hidden_act UpperCamelCase__ = hidden_dropout_prob UpperCamelCase__ = attention_probs_dropout_prob UpperCamelCase__ = encoder_layerdrop UpperCamelCase__ = decoder_layerdrop UpperCamelCase__ = max_position_embeddings UpperCamelCase__ = eos_token_id UpperCamelCase__ = pad_token_id UpperCamelCase__ = bos_token_id def UpperCAmelCase_ (self ): UpperCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ = self.eos_token_id # Eos Token UpperCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input UpperCamelCase__ = input_ids.clamp(self.pad_token_id + 1 ) UpperCamelCase__ = decoder_input_ids.clamp(self.pad_token_id + 1 ) UpperCamelCase__ = self.get_config() UpperCamelCase__ = prepare_mam_aaa_inputs_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return config, inputs_dict def UpperCAmelCase_ (self ): return MaMaaaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , encoder_layerdrop=self.encoder_layerdrop , decoder_layerdrop=self.decoder_layerdrop , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.prepare_config_and_inputs() return config, inputs_dict def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = MaMaaaModel(config=SCREAMING_SNAKE_CASE_ ).get_decoder().to(SCREAMING_SNAKE_CASE_ ).eval() UpperCamelCase__ = inputs_dict["""input_ids"""] UpperCamelCase__ = inputs_dict["""attention_mask"""] UpperCamelCase__ = inputs_dict["""head_mask"""] # first forward pass UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , head_mask=SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ , UpperCamelCase__ = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase__ = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and UpperCamelCase__ = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase__ = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ )["""last_hidden_state"""] UpperCamelCase__ = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , past_key_values=SCREAMING_SNAKE_CASE_ )[ """last_hidden_state""" ] # select random slice UpperCamelCase__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase__ = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase__ = 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(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-2 ) ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = MaMaaaModel(config=SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ).eval() UpperCamelCase__ = model(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = outputs.encoder_last_hidden_state UpperCamelCase__ = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ = model.get_encoder() encoder.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaEncoder.from_pretrained(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = encoder(inputs_dict["""input_ids"""] , attention_mask=inputs_dict["""attention_mask"""] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ = model.get_decoder() decoder.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaDecoder.from_pretrained(SCREAMING_SNAKE_CASE_ ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = decoder( input_ids=inputs_dict["""decoder_input_ids"""] , attention_mask=inputs_dict["""decoder_attention_mask"""] , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , encoder_attention_mask=inputs_dict["""attention_mask"""] , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE__ = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ = (MaMaaaForConditionalGeneration,) if is_torch_available() else () SCREAMING_SNAKE_CASE__ = ( { """conversational""": MaMaaaForConditionalGeneration, """feature-extraction""": MaMaaaModel, """summarization""": MaMaaaForConditionalGeneration, """text2text-generation""": MaMaaaForConditionalGeneration, """translation""": MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaModelTester(self ) UpperCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): self.config_tester.run_common_tests() def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ , UpperCamelCase__ = model_class.from_pretrained(SCREAMING_SNAKE_CASE_ , output_loading_info=SCREAMING_SNAKE_CASE_ ) self.assertEqual(info["""missing_keys"""] , [] ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): UpperCamelCase__ = model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() UpperCamelCase__ = copy.deepcopy(self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) if not self.is_encoder_decoder: UpperCamelCase__ = inputs["""input_ids"""] del inputs["input_ids"] else: UpperCamelCase__ = inputs["""input_ids"""] UpperCamelCase__ = inputs.get("""decoder_input_ids""" , SCREAMING_SNAKE_CASE_ ) del inputs["input_ids"] inputs.pop("""decoder_input_ids""" , SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = model.get_input_embeddings() if not self.is_encoder_decoder: UpperCamelCase__ = wte(SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase__ = wte(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = wte(SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): model(**SCREAMING_SNAKE_CASE_ )[0] def UpperCAmelCase_ (self ): UpperCamelCase__ , UpperCamelCase__ = self.model_tester.prepare_config_and_inputs() UpperCamelCase__ = input_dict["""input_ids"""] UpperCamelCase__ = input_ids.ne(1 ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ).eval().to(SCREAMING_SNAKE_CASE_ ) if torch_device == "cuda": model.half() model.generate(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) model.generate(num_beams=4 , do_sample=SCREAMING_SNAKE_CASE_ , early_stopping=SCREAMING_SNAKE_CASE_ , num_return_sequences=3 ) def __magic_name__ ( __a : List[Any] ): '''simple docstring''' return torch.tensor(__a , dtype=torch.long , device=__a ) lowerCamelCase_ = 1E-4 @require_torch @require_sentencepiece @require_tokenizers @slow class __A( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase_ (self ): return MaMaaaTokenizer.from_pretrained("""facebook/m2m100_418M""" ) def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaModel.from_pretrained("""facebook/m2m100_418M""" ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = _long_tensor([[12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38, 2]] ) UpperCamelCase__ = _long_tensor([[2, 12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38]] ) UpperCamelCase__ = prepare_mam_aaa_inputs_dict(model.config , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): UpperCamelCase__ = model(**SCREAMING_SNAKE_CASE_ )[0] UpperCamelCase__ = torch.Size((1, 11, 10_24) ) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) # change to expected output here UpperCamelCase__ = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]] , device=SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(output[:, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaForConditionalGeneration.from_pretrained("""facebook/m2m100_418M""" ).to(SCREAMING_SNAKE_CASE_ ) # change to intended input UpperCamelCase__ = _long_tensor([[12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38, 2]] ) UpperCamelCase__ = _long_tensor([[2, 12_80_28, 98, 12, 3_05_27, 27_32, 1_59, 77_55, 6_19_04, 3_91_44, 38]] ) UpperCamelCase__ = prepare_mam_aaa_inputs_dict(model.config , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): UpperCamelCase__ = model(**SCREAMING_SNAKE_CASE_ )[0] UpperCamelCase__ = torch.Size((1, 11, model.config.vocab_size) ) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) # change to expected output here UpperCamelCase__ = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]] , device=SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(output[:, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=SCREAMING_SNAKE_CASE_ ) ) def UpperCAmelCase_ (self ): UpperCamelCase__ = MaMaaaForConditionalGeneration.from_pretrained("""facebook/m2m100_418M""" ).to(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = MaMaaaTokenizer.from_pretrained("""facebook/m2m100_418M""" , src_lang="""fr""" , tgt_lang="""en""" ) UpperCamelCase__ = [ """L'affaire NSA souligne l'absence totale de débat sur le renseignement""", """Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.""", """Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent""" """ Fabius convoque l'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de""" """ l'ampleur de la surveillance américaine sur l'ensemble des communications en France.""", ] # The below article tests that we don't add any hypotheses outside of the top n_beams UpperCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" ) UpperCamelCase__ = model.generate( input_ids=dct["""input_ids"""].to(SCREAMING_SNAKE_CASE_ ) , attention_mask=dct["""attention_mask"""].to(SCREAMING_SNAKE_CASE_ ) , num_beams=5 , forced_bos_token_id=tokenizer.get_lang_id("""en""" ) , ) UpperCamelCase__ = [ """The NSA case highlights the total absence of intelligence debate""", """I think there are two levels of response from the French government.""", """When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.""" """ Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all""" """ communications in France.""", ] UpperCamelCase__ = tokenizer.batch_decode( hypotheses_batch.tolist() , clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) assert generated == expected_en
244
0
"""simple docstring""" def __lowercase ( _a = 100 ): snake_case_ : Tuple = 0 snake_case_ : str = 0 for i in range(1 , n + 1 ): sum_of_squares += i**2 sum_of_ints += i return sum_of_ints**2 - sum_of_squares if __name__ == "__main__": print(f'{solution() = }')
361
"""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 from ..auto import CONFIG_MAPPING lowercase__ : List[str] = logging.get_logger(__name__) lowercase__ : List[Any] = { '''microsoft/table-transformer-detection''': ( '''https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json''' ), } class _UpperCAmelCase ( lowerCAmelCase__): _lowerCAmelCase : Optional[Any] = """table-transformer""" _lowerCAmelCase : Any = ["""past_key_values"""] _lowerCAmelCase : Union[str, Any] = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self : Any , lowercase_ : Any=True , lowercase_ : Dict=None , lowercase_ : Optional[int]=3 , lowercase_ : Any=100 , lowercase_ : List[str]=6 , lowercase_ : Any=2048 , lowercase_ : Any=8 , lowercase_ : Tuple=6 , lowercase_ : List[Any]=2048 , lowercase_ : List[str]=8 , lowercase_ : List[Any]=0.0 , lowercase_ : str=0.0 , lowercase_ : Dict=True , lowercase_ : Optional[int]="relu" , lowercase_ : Dict=256 , lowercase_ : Optional[int]=0.1 , lowercase_ : List[Any]=0.0 , lowercase_ : List[str]=0.0 , lowercase_ : Dict=0.02 , lowercase_ : int=1.0 , lowercase_ : Tuple=False , lowercase_ : Optional[Any]="sine" , lowercase_ : Union[str, Any]="resnet50" , lowercase_ : List[Any]=True , lowercase_ : List[Any]=False , lowercase_ : Optional[Any]=1 , lowercase_ : Dict=5 , lowercase_ : List[Any]=2 , lowercase_ : Tuple=1 , lowercase_ : List[Any]=1 , lowercase_ : Dict=5 , lowercase_ : Union[str, Any]=2 , lowercase_ : Union[str, Any]=0.1 , **lowercase_ : int , ): if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) snake_case_ : Dict = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(lowercase_ , lowercase_ ): snake_case_ : List[Any] = backbone_config.get('''model_type''' ) snake_case_ : int = CONFIG_MAPPING[backbone_model_type] snake_case_ : List[str] = config_class.from_dict(lowercase_ ) # set timm attributes to None snake_case_, snake_case_, snake_case_ : List[str] = None, None, None snake_case_ : Tuple = use_timm_backbone snake_case_ : int = backbone_config snake_case_ : str = num_channels snake_case_ : List[str] = num_queries snake_case_ : int = d_model snake_case_ : List[str] = encoder_ffn_dim snake_case_ : Any = encoder_layers snake_case_ : List[Any] = encoder_attention_heads snake_case_ : Optional[int] = decoder_ffn_dim snake_case_ : Tuple = decoder_layers snake_case_ : List[str] = decoder_attention_heads snake_case_ : Tuple = dropout snake_case_ : Union[str, Any] = attention_dropout snake_case_ : Dict = activation_dropout snake_case_ : Optional[Any] = activation_function snake_case_ : Optional[Any] = init_std snake_case_ : str = init_xavier_std snake_case_ : Any = encoder_layerdrop snake_case_ : Optional[Any] = decoder_layerdrop snake_case_ : List[str] = encoder_layers snake_case_ : Optional[int] = auxiliary_loss snake_case_ : List[Any] = position_embedding_type snake_case_ : List[Any] = backbone snake_case_ : Union[str, Any] = use_pretrained_backbone snake_case_ : Optional[Any] = dilation # Hungarian matcher snake_case_ : Tuple = class_cost snake_case_ : Any = bbox_cost snake_case_ : Dict = giou_cost # Loss coefficients snake_case_ : Optional[Any] = mask_loss_coefficient snake_case_ : str = dice_loss_coefficient snake_case_ : List[str] = bbox_loss_coefficient snake_case_ : int = giou_loss_coefficient snake_case_ : Optional[Any] = eos_coefficient super().__init__(is_encoder_decoder=lowercase_ , **lowercase_ ) @property def _snake_case ( self : Optional[int] ): return self.encoder_attention_heads @property def _snake_case ( self : Any ): return self.d_model class _UpperCAmelCase ( lowerCAmelCase__): _lowerCAmelCase : List[Any] = version.parse("""1.11""") @property def _snake_case ( self : List[Any] ): return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def _snake_case ( self : int ): return 1E-5 @property def _snake_case ( self : Optional[int] ): return 12
155
0
"""simple docstring""" # Author: OMKAR PATHAK, Nwachukwu Chidiebere # Use a Python dictionary to construct the graph. from __future__ import annotations from pprint import pformat from typing import Generic, TypeVar __magic_name__ = TypeVar("T") class SCREAMING_SNAKE_CASE_ ( Generic[T] ): """simple docstring""" def __init__( self , lowerCAmelCase__ = True): __SCREAMING_SNAKE_CASE = {} # dictionary of lists __SCREAMING_SNAKE_CASE = directed def snake_case_ ( self , lowerCAmelCase__ , lowerCAmelCase__): if not self.directed: # For undirected graphs # if both source vertex and destination vertex are both present in the # adjacency list, add destination vertex to source vertex list of adjacent # vertices and add source vertex to destination vertex list of adjacent # vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(lowerCAmelCase__) self.adj_list[destination_vertex].append(lowerCAmelCase__) # if only source vertex is present in adjacency list, add destination vertex # to source vertex list of adjacent vertices, then create a new vertex with # destination vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(lowerCAmelCase__) __SCREAMING_SNAKE_CASE = [source_vertex] # if only destination vertex is present in adjacency list, add source vertex # to destination vertex list of adjacent vertices, then create a new vertex # with source vertex as key and assign a list containing the source vertex # as it's first adjacent vertex. elif destination_vertex in self.adj_list: self.adj_list[destination_vertex].append(lowerCAmelCase__) __SCREAMING_SNAKE_CASE = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and assign a list # containing the destination vertex as it's first adjacent vertex also # create a new vertex with destination vertex as key and assign a list # containing the source vertex as it's first adjacent vertex. else: __SCREAMING_SNAKE_CASE = [destination_vertex] __SCREAMING_SNAKE_CASE = [source_vertex] else: # For directed graphs # if both source vertex and destination vertex are present in adjacency # list, add destination vertex to source vertex list of adjacent vertices. if source_vertex in self.adj_list and destination_vertex in self.adj_list: self.adj_list[source_vertex].append(lowerCAmelCase__) # if only source vertex is present in adjacency list, add destination # vertex to source vertex list of adjacent vertices and create a new vertex # with destination vertex as key, which has no adjacent vertex elif source_vertex in self.adj_list: self.adj_list[source_vertex].append(lowerCAmelCase__) __SCREAMING_SNAKE_CASE = [] # if only destination vertex is present in adjacency list, create a new # vertex with source vertex as key and assign a list containing destination # vertex as first adjacent vertex elif destination_vertex in self.adj_list: __SCREAMING_SNAKE_CASE = [destination_vertex] # if both source vertex and destination vertex are not present in adjacency # list, create a new vertex with source vertex as key and a list containing # destination vertex as it's first adjacent vertex. Then create a new vertex # with destination vertex as key, which has no adjacent vertex else: __SCREAMING_SNAKE_CASE = [destination_vertex] __SCREAMING_SNAKE_CASE = [] return self def __repr__( self): return pformat(self.adj_list)
100
"""simple docstring""" from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" __lowercase : List[Any] = ['''image_processor''', '''tokenizer'''] __lowercase : List[Any] = '''BridgeTowerImageProcessor''' __lowercase : Tuple = ('''RobertaTokenizer''', '''RobertaTokenizerFast''') def __init__( self , lowerCAmelCase__ , lowerCAmelCase__): super().__init__(lowerCAmelCase__ , lowerCAmelCase__) def __call__( self , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = True , lowerCAmelCase__ = False , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = 0 , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = None , lowerCAmelCase__ = False , lowerCAmelCase__ = False , lowerCAmelCase__ = False , lowerCAmelCase__ = False , lowerCAmelCase__ = True , lowerCAmelCase__ = None , **lowerCAmelCase__ , ): __SCREAMING_SNAKE_CASE = self.tokenizer( text=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ , stride=lowerCAmelCase__ , pad_to_multiple_of=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ , return_attention_mask=lowerCAmelCase__ , return_overflowing_tokens=lowerCAmelCase__ , return_special_tokens_mask=lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , return_length=lowerCAmelCase__ , verbose=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ , ) # add pixel_values + pixel_mask __SCREAMING_SNAKE_CASE = self.image_processor( lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , do_normalize=lowerCAmelCase__ , do_center_crop=lowerCAmelCase__ , **lowerCAmelCase__) encoding.update(lowerCAmelCase__) return encoding def snake_case_ ( self , *lowerCAmelCase__ , **lowerCAmelCase__): return self.tokenizer.batch_decode(*lowerCAmelCase__ , **lowerCAmelCase__) def snake_case_ ( self , *lowerCAmelCase__ , **lowerCAmelCase__): return self.tokenizer.decode(*lowerCAmelCase__ , **lowerCAmelCase__) @property def snake_case_ ( self): __SCREAMING_SNAKE_CASE = self.tokenizer.model_input_names __SCREAMING_SNAKE_CASE = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
100
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __A = { """configuration_bloom""": ["""BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BloomConfig""", """BloomOnnxConfig"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ["""BloomTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ """BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST""", """BloomForCausalLM""", """BloomModel""", """BloomPreTrainedModel""", """BloomForSequenceClassification""", """BloomForTokenClassification""", """BloomForQuestionAnswering""", ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys __A = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
363
"""simple docstring""" import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model __A = """0.12""" # assumed parallelism: 8 if is_torch_available(): import torch def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ) ->List[Any]: """simple docstring""" if rng is None: lowerCAmelCase__ :Dict = random.Random() lowerCAmelCase__ :Tuple = 1 for dim in shape: total_dims *= dim lowerCAmelCase__ :List[Any] = [] for _ in range(_SCREAMING_SNAKE_CASE ): values.append(rng.randint(0 , vocab_size - 1 ) ) lowerCAmelCase__ :int = np.array(_SCREAMING_SNAKE_CASE , dtype=jnp.intaa ).reshape(_SCREAMING_SNAKE_CASE ) return output def __A (_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ) ->List[Any]: """simple docstring""" lowerCAmelCase__ :Union[str, Any] = ids_tensor(_SCREAMING_SNAKE_CASE , vocab_size=2 , rng=_SCREAMING_SNAKE_CASE ) # make sure that at least one token is attended to for each batch lowerCAmelCase__ :Any = 1 return attn_mask @require_flax class _lowerCAmelCase : """simple docstring""" __magic_name__ :Optional[int] = None __magic_name__ :List[str] = () def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ :Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 lowerCAmelCase__ :Union[str, Any] = 2 lowerCAmelCase__ :List[Any] = inputs['input_ids'].shape[-1] // 2 lowerCAmelCase__ :Union[str, Any] = inputs['input_ids'][:max_batch_size, :sequence_length] lowerCAmelCase__ :str = jnp.ones_like(__UpperCAmelCase ) lowerCAmelCase__ :int = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens lowerCAmelCase__ :List[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` lowerCAmelCase__ :Optional[Any] = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Any = self._get_input_ids_and_config() lowerCAmelCase__ :int = False lowerCAmelCase__ :List[Any] = max_length lowerCAmelCase__ :List[Any] = 0 for model_class in self.all_generative_model_classes: lowerCAmelCase__ :int = model_class(__UpperCAmelCase ) lowerCAmelCase__ :Union[str, Any] = model_class.__name__[4:] # Skip the "Flax" at the beginning lowerCAmelCase__ :List[Any] = getattr(__UpperCAmelCase , __UpperCAmelCase ) lowerCAmelCase__ :Optional[int] = pt_model_class(__UpperCAmelCase ).eval() lowerCAmelCase__ :Dict = load_flax_weights_in_pytorch_model(__UpperCAmelCase , flax_model.params ) lowerCAmelCase__ :Union[str, Any] = flax_model.generate(__UpperCAmelCase ).sequences lowerCAmelCase__ :Union[str, Any] = pt_model.generate(torch.tensor(__UpperCAmelCase , dtype=torch.long ) ) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: lowerCAmelCase__ :Union[str, Any] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :str = self._get_input_ids_and_config() lowerCAmelCase__ :Any = False lowerCAmelCase__ :Any = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ :List[str] = model_class(__UpperCAmelCase ) lowerCAmelCase__ :Any = model.generate(__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :List[str] = jit(model.generate ) lowerCAmelCase__ :Optional[Any] = jit_generate(__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Dict = self._get_input_ids_and_config() lowerCAmelCase__ :int = True lowerCAmelCase__ :Optional[int] = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ :Dict = model_class(__UpperCAmelCase ) lowerCAmelCase__ :int = model.generate(__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :List[Any] = jit(model.generate ) lowerCAmelCase__ :Dict = jit_generate(__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Optional[Any] = self._get_input_ids_and_config() lowerCAmelCase__ :Optional[Any] = False lowerCAmelCase__ :Dict = max_length lowerCAmelCase__ :Dict = 2 for model_class in self.all_generative_model_classes: lowerCAmelCase__ :List[str] = model_class(__UpperCAmelCase ) lowerCAmelCase__ :Dict = model.generate(__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :Any = jit(model.generate ) lowerCAmelCase__ :Dict = jit_generate(__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Any = self._get_input_ids_and_config() lowerCAmelCase__ :int = False lowerCAmelCase__ :Optional[Any] = max_length lowerCAmelCase__ :Optional[Any] = 2 lowerCAmelCase__ :Optional[int] = 2 for model_class in self.all_generative_model_classes: lowerCAmelCase__ :Union[str, Any] = model_class(__UpperCAmelCase ) lowerCAmelCase__ :str = model.generate(__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :str = self._get_input_ids_and_config() lowerCAmelCase__ :Optional[int] = True lowerCAmelCase__ :Tuple = max_length lowerCAmelCase__ :Optional[int] = 0.8 lowerCAmelCase__ :Any = 1_0 lowerCAmelCase__ :List[Any] = 0.3 lowerCAmelCase__ :Tuple = 1 lowerCAmelCase__ :Union[str, Any] = 8 lowerCAmelCase__ :Optional[Any] = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase__ :List[Any] = model_class(__UpperCAmelCase ) lowerCAmelCase__ :Union[str, Any] = model.generate(__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :Optional[int] = jit(model.generate ) lowerCAmelCase__ :Any = jit_generate(__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Optional[Any] = self._get_input_ids_and_config() lowerCAmelCase__ :Union[str, Any] = max_length lowerCAmelCase__ :str = 1 lowerCAmelCase__ :Tuple = 8 lowerCAmelCase__ :Optional[Any] = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase__ :str = model_class(__UpperCAmelCase ) lowerCAmelCase__ :Union[str, Any] = model.generate(__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :List[str] = jit(model.generate ) lowerCAmelCase__ :Optional[int] = jit_generate(__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :List[str] = self._get_input_ids_and_config() lowerCAmelCase__ :str = max_length lowerCAmelCase__ :Dict = 2 lowerCAmelCase__ :Dict = 1 lowerCAmelCase__ :Optional[int] = 8 lowerCAmelCase__ :Optional[Any] = 9 for model_class in self.all_generative_model_classes: lowerCAmelCase__ :List[Any] = model_class(__UpperCAmelCase ) lowerCAmelCase__ :int = model.generate(__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :Dict = jit(model.generate ) lowerCAmelCase__ :Dict = jit_generate(__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :Optional[int] = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase__ :Tuple = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase__ :Union[str, Any] = False lowerCAmelCase__ :Optional[Any] = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ :Tuple = model_class(__UpperCAmelCase ) lowerCAmelCase__ :List[Any] = model.generate(__UpperCAmelCase , attention_mask=__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :int = jit(model.generate ) lowerCAmelCase__ :Optional[Any] = jit_generate(__UpperCAmelCase , attention_mask=__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :List[Any] = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase__ :Any = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase__ :Optional[Any] = True lowerCAmelCase__ :Dict = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ :str = model_class(__UpperCAmelCase ) lowerCAmelCase__ :Tuple = model.generate(__UpperCAmelCase , attention_mask=__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :Optional[int] = jit(model.generate ) lowerCAmelCase__ :Optional[int] = jit_generate(__UpperCAmelCase , attention_mask=__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ :int = self._get_input_ids_and_config() # pad attention mask on the left lowerCAmelCase__ :int = attention_mask.at[(0, 0)].set(0 ) lowerCAmelCase__ :Dict = 2 lowerCAmelCase__ :Optional[Any] = max_length for model_class in self.all_generative_model_classes: lowerCAmelCase__ :Any = model_class(__UpperCAmelCase ) lowerCAmelCase__ :List[Any] = model.generate(__UpperCAmelCase , attention_mask=__UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , __UpperCAmelCase ) lowerCAmelCase__ :Optional[Any] = jit(model.generate ) lowerCAmelCase__ :int = jit_generate(__UpperCAmelCase , attention_mask=__UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) @require_flax class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def snake_case ( self ): '''simple docstring''' lowerCAmelCase__ :Union[str, Any] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert' ) lowerCAmelCase__ :List[str] = FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only' ) lowerCAmelCase__ :Optional[int] = 'Hello world' lowerCAmelCase__ :Union[str, Any] = tokenizer(__UpperCAmelCase , return_tensors='np' ).input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(__UpperCAmelCase , 'do_samples' ): model.generate(__UpperCAmelCase , do_samples=__UpperCAmelCase ) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(__UpperCAmelCase , 'foo' ): lowerCAmelCase__ :Optional[int] = {'foo': 'bar'} model.generate(__UpperCAmelCase , **__UpperCAmelCase )
254
0
'''simple docstring''' import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCamelCase_ = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece @require_tokenizers class _a ( SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' A : int = XLMRobertaTokenizer A : Union[str, Any] = XLMRobertaTokenizerFast A : List[str] = True A : List[str] = True def UpperCamelCase_ ( self ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE : Dict = XLMRobertaTokenizer(A, keep_accents=A ) tokenizer.save_pretrained(self.tmpdirname ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = '<pad>' SCREAMING_SNAKE_CASE : Dict = 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 ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0], '<s>' ) self.assertEqual(vocab_keys[1], '<pad>' ) self.assertEqual(vocab_keys[-1], '<mask>' ) self.assertEqual(len(A ), 1_002 ) def UpperCamelCase_ ( self ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size, 1_002 ) def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = XLMRobertaTokenizer(A, keep_accents=A ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.tokenize('This is a test' ) self.assertListEqual(A, ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(A ), [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]], ) SCREAMING_SNAKE_CASE : str = 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', 'é', '.', ], ) SCREAMING_SNAKE_CASE : Dict = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual( A, [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ], ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer.convert_ids_to_tokens(A ) self.assertListEqual( A, [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ], ) def UpperCamelCase_ ( self ): '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return SCREAMING_SNAKE_CASE : Optional[Any] = (self.rust_tokenizer_class, 'hf-internal-testing/tiny-xlm-roberta', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): SCREAMING_SNAKE_CASE : Union[str, Any] = self.rust_tokenizer_class.from_pretrained(A, **A ) SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer_class.from_pretrained(A, **A ) SCREAMING_SNAKE_CASE : Optional[int] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.save_pretrained(A ) SCREAMING_SNAKE_CASE : Tuple = tokenizer_p.save_pretrained(A ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) SCREAMING_SNAKE_CASE : List[Any] = tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f ) self.assertSequenceEqual(A, A ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : List[Any] = tokenizer_r.from_pretrained(A ) SCREAMING_SNAKE_CASE : int = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A, A ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=True SCREAMING_SNAKE_CASE : Optional[int] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Optional[int] = tokenizer_r.save_pretrained(A, legacy_format=A ) SCREAMING_SNAKE_CASE : Tuple = tokenizer_p.save_pretrained(A ) # Checks it save with the same files self.assertSequenceEqual(A, A ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : Dict = tokenizer_r.from_pretrained(A ) SCREAMING_SNAKE_CASE : List[str] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A, A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=False SCREAMING_SNAKE_CASE : List[Any] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : Dict = tokenizer_r.save_pretrained(A, legacy_format=A ) SCREAMING_SNAKE_CASE : int = tokenizer_p.save_pretrained(A ) # Checks it saved the tokenizer.json file self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer_r.from_pretrained(A ) SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A, A ) ) shutil.rmtree(A ) @cached_property def UpperCamelCase_ ( self ): '''simple docstring''' return XLMRobertaTokenizer.from_pretrained('xlm-roberta-base' ) def UpperCamelCase_ ( self ): '''simple docstring''' with tempfile.NamedTemporaryFile() as f: shutil.copyfile(A, f.name ) SCREAMING_SNAKE_CASE : Any = XLMRobertaTokenizer(f.name, keep_accents=A ) SCREAMING_SNAKE_CASE : Tuple = pickle.dumps(A ) pickle.loads(A ) def UpperCamelCase_ ( self ): '''simple docstring''' if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_tokenizer() SCREAMING_SNAKE_CASE : Dict = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE : Any = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.tokenize(A ) SCREAMING_SNAKE_CASE : Optional[int] = rust_tokenizer.tokenize(A ) self.assertListEqual(A, A ) SCREAMING_SNAKE_CASE : str = tokenizer.encode(A, add_special_tokens=A ) SCREAMING_SNAKE_CASE : Optional[int] = rust_tokenizer.encode(A, add_special_tokens=A ) self.assertListEqual(A, A ) SCREAMING_SNAKE_CASE : Dict = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.encode(A ) SCREAMING_SNAKE_CASE : int = rust_tokenizer.encode(A ) self.assertListEqual(A, A ) @slow def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = 'Hello World!' SCREAMING_SNAKE_CASE : List[str] = [0, 35_378, 6_661, 38, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(A, self.big_tokenizer.encode(A ) ) @slow def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = ( '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' ) SCREAMING_SNAKE_CASE : Union[str, Any] = [ 0, 3_293, 83, 10, 4_552, 4_989, 7_986, 678, 10, 5_915, 111, 179_459, 124_850, 4, 6_044, 237, 12, 6, 5, 6, 4, 6_780, 705, 15, 1_388, 44, 378, 10_114, 711, 152, 20, 6, 5, 22_376, 642, 1_221, 15_190, 34_153, 450, 5_608, 959, 1_119, 57_702, 136, 186, 47, 1_098, 29_367, 47, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 6_044, 237, 6_284, 50_901, 528, 31, 90, 34, 927, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(A, self.big_tokenizer.encode(A ) ) @slow def UpperCamelCase_ ( self ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = {'input_ids': [[0, 11_062, 82_772, 7, 15, 82_772, 538, 51_529, 237, 17_198, 1_290, 206, 9, 215_175, 1_314, 136, 17_198, 1_290, 206, 9, 56_359, 42, 122_009, 9, 16_466, 16, 87_344, 4_537, 9, 4_717, 78_381, 6, 159_958, 7, 15, 24_480, 618, 4, 527, 22_693, 5_428, 4, 2_777, 24_480, 9_874, 4, 43_523, 594, 4, 803, 18_392, 33_189, 18, 4, 43_523, 24_447, 12_399, 100, 24_955, 83_658, 9_626, 144_057, 15, 839, 22_335, 16, 136, 24_955, 83_658, 83_479, 15, 39_102, 724, 16, 678, 645, 2_789, 1_328, 4_589, 42, 122_009, 115_774, 23, 805, 1_328, 46_876, 7, 136, 53_894, 1_940, 42_227, 41_159, 17_721, 823, 425, 4, 27_512, 98_722, 206, 136, 5_531, 4_970, 919, 17_336, 5, 2], [0, 20_080, 618, 83, 82_775, 47, 479, 9, 1_517, 73, 53_894, 333, 80_581, 110_117, 18_811, 5_256, 1_295, 51, 152_526, 297, 7_986, 390, 124_416, 538, 35_431, 214, 98, 15_044, 25_737, 136, 7_108, 43_701, 23, 756, 135_355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 581, 63_773, 119_455, 6, 147_797, 88_203, 7, 645, 70, 21, 3_285, 10_269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=A, model_name='xlm-roberta-base', revision='d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3', )
251
'''simple docstring''' import argparse import torch from transformers import BertForMaskedLM if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser( description=( "Extraction some layers of the full BertForMaskedLM or RObertaForMaskedLM for Transfer Learned" " Distillation" ) ) parser.add_argument("--model_type", default="bert", choices=["bert"]) parser.add_argument("--model_name", default="bert-base-uncased", type=str) parser.add_argument("--dump_checkpoint", default="serialization_dir/tf_bert-base-uncased_0247911.pth", type=str) parser.add_argument("--vocab_transform", action="store_true") UpperCamelCase_ = parser.parse_args() if args.model_type == "bert": UpperCamelCase_ = BertForMaskedLM.from_pretrained(args.model_name) UpperCamelCase_ = "bert" else: raise ValueError("args.model_type should be \"bert\".") UpperCamelCase_ = model.state_dict() UpperCamelCase_ = {} for w in ["word_embeddings", "position_embeddings"]: UpperCamelCase_ = state_dict[F"""{prefix}.embeddings.{w}.weight"""] for w in ["weight", "bias"]: UpperCamelCase_ = state_dict[F"""{prefix}.embeddings.LayerNorm.{w}"""] UpperCamelCase_ = 0 for teacher_idx in [0, 2, 4, 7, 9, 1_1]: for w in ["weight", "bias"]: UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.attention.self.query.{w}""" ] UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.attention.self.key.{w}""" ] UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.attention.self.value.{w}""" ] UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.attention.output.dense.{w}""" ] UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.attention.output.LayerNorm.{w}""" ] UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.intermediate.dense.{w}""" ] UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.output.dense.{w}""" ] UpperCamelCase_ = state_dict[ F"""{prefix}.encoder.layer.{teacher_idx}.output.LayerNorm.{w}""" ] std_idx += 1 UpperCamelCase_ = state_dict["cls.predictions.decoder.weight"] UpperCamelCase_ = state_dict["cls.predictions.bias"] if args.vocab_transform: for w in ["weight", "bias"]: UpperCamelCase_ = state_dict[F"""cls.predictions.transform.dense.{w}"""] UpperCamelCase_ = state_dict[F"""cls.predictions.transform.LayerNorm.{w}"""] print(F"""N layers selected for distillation: {std_idx}""") print(F"""Number of params transferred for distillation: {len(compressed_sd.keys())}""") print(F"""Save transferred checkpoint to {args.dump_checkpoint}.""") torch.save(compressed_sd, args.dump_checkpoint)
251
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) __UpperCamelCase : Tuple = { '''configuration_funnel''': ['''FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FunnelConfig'''], '''convert_funnel_original_tf_checkpoint_to_pytorch''': [], '''tokenization_funnel''': ['''FunnelTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : List[Any] = ['''FunnelTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Tuple = [ '''FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FunnelBaseModel''', '''FunnelForMaskedLM''', '''FunnelForMultipleChoice''', '''FunnelForPreTraining''', '''FunnelForQuestionAnswering''', '''FunnelForSequenceClassification''', '''FunnelForTokenClassification''', '''FunnelModel''', '''FunnelPreTrainedModel''', '''load_tf_weights_in_funnel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : str = [ '''TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFFunnelBaseModel''', '''TFFunnelForMaskedLM''', '''TFFunnelForMultipleChoice''', '''TFFunnelForPreTraining''', '''TFFunnelForQuestionAnswering''', '''TFFunnelForSequenceClassification''', '''TFFunnelForTokenClassification''', '''TFFunnelModel''', '''TFFunnelPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys __UpperCamelCase : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
369
"""simple docstring""" from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer __UpperCamelCase : Union[str, Any] = logging.get_logger(__name__) __UpperCamelCase : List[str] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } __UpperCamelCase : Tuple = { '''vocab_file''': { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json''' }, '''merges_file''': { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt''' }, '''tokenizer_config_file''': { '''facebook/blenderbot_small-90M''': ( '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json''' ) }, } __UpperCamelCase : List[Any] = { '''facebook/blenderbot_small-90M''': 5_1_2, } class SCREAMING_SNAKE_CASE ( a_ ): """simple docstring""" lowercase__ = VOCAB_FILES_NAMES lowercase__ = PRETRAINED_VOCAB_FILES_MAP lowercase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ = BlenderbotSmallTokenizer def __init__( self : Dict ,lowercase_ : Dict=None ,lowercase_ : Union[str, Any]=None ,lowercase_ : Any="<|endoftext|>" ,lowercase_ : Optional[Any]="<|endoftext|>" ,lowercase_ : Dict="<|endoftext|>" ,lowercase_ : Optional[int]=False ,lowercase_ : Union[str, Any]=True ,**lowercase_ : Union[str, Any] ,): super().__init__( ByteLevelBPETokenizer( vocab=lowercase_ ,merges=lowercase_ ,add_prefix_space=lowercase_ ,trim_offsets=lowercase_ ,) ,bos_token=lowercase_ ,eos_token=lowercase_ ,unk_token=lowercase_ ,**lowercase_ ,) lowerCAmelCase__ : Tuple = add_prefix_space def __lowerCAmelCase ( self : Optional[Any] ,lowercase_ : Optional[int] ,lowercase_ : int=None ): lowerCAmelCase__ : Dict = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def __lowerCAmelCase ( self : List[Any] ,lowercase_ : List[int] ,lowercase_ : Optional[List[int]] = None ): lowerCAmelCase__ : Union[str, Any] = [self.sep_token_id] lowerCAmelCase__ : Dict = [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]
74
0
import numpy as np import torch from torch.nn import CrossEntropyLoss from transformers import AutoModelForCausalLM, AutoTokenizer import datasets from datasets import logging _a = '\\n\n' _a = '\nPerplexity (PPL) is one of the most common metrics for evaluating language models.\nIt is defined as the exponentiated average negative log-likelihood of a sequence.\n\nFor more information, see https://huggingface.co/docs/transformers/perplexity\n' _a = '\nArgs:\n model_id (str): model used for calculating Perplexity\n NOTE: Perplexity can only be calculated for causal language models.\n This includes models such as gpt2, causal variations of bert,\n causal versions of t5, and more (the full list can be found\n in the AutoModelForCausalLM documentation here:\n https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM )\n\n input_texts (list of str): input text, each separate text snippet\n is one list entry.\n batch_size (int): the batch size to run texts through the model. Defaults to 16.\n add_start_token (bool): whether to add the start token to the texts,\n so the perplexity can include the probability of the first word. Defaults to True.\n device (str): device to run on, defaults to \'cuda\' when available\nReturns:\n perplexity: dictionary containing the perplexity scores for the texts\n in the input list, as well as the mean perplexity. If one of the input texts is\n longer than the max input length of the model, then it is truncated to the\n max length for the perplexity computation.\nExamples:\n Example 1:\n >>> perplexity = datasets.load_metric("perplexity")\n >>> input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]\n >>> results = perplexity.compute(model_id=\'gpt2\',\n ... add_start_token=False,\n ... input_texts=input_texts) # doctest:+ELLIPSIS\n >>> print(list(results.keys()))\n [\'perplexities\', \'mean_perplexity\']\n >>> print(round(results["mean_perplexity"], 2))\n 78.22\n >>> print(round(results["perplexities"][0], 2))\n 11.11\n\n Example 2:\n >>> perplexity = datasets.load_metric("perplexity")\n >>> input_texts = datasets.load_dataset("wikitext",\n ... "wikitext-2-raw-v1",\n ... split="test")["text"][:50] # doctest:+ELLIPSIS\n [...]\n >>> input_texts = [s for s in input_texts if s!=\'\']\n >>> results = perplexity.compute(model_id=\'gpt2\',\n ... input_texts=input_texts) # doctest:+ELLIPSIS\n >>> print(list(results.keys()))\n [\'perplexities\', \'mean_perplexity\']\n >>> print(round(results["mean_perplexity"], 2))\n 60.35\n >>> print(round(results["perplexities"][0], 2))\n 81.12\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): '''simple docstring''' def __lowerCamelCase ( self ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''input_texts''': datasets.Value('''string''' ), } ) , reference_urls=['''https://huggingface.co/docs/transformers/perplexity'''] , ) def __lowerCamelCase ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = 1_6 , __lowerCAmelCase = True , __lowerCAmelCase=None ): '''simple docstring''' if device is not None: assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu." if device == "gpu": lowerCamelCase__ = '''cuda''' else: lowerCamelCase__ = '''cuda''' if torch.cuda.is_available() else '''cpu''' lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(snake_case__ ) lowerCamelCase__ = model.to(snake_case__ ) lowerCamelCase__ = AutoTokenizer.from_pretrained(snake_case__ ) # if batch_size > 1 (which generally leads to padding being required), and # if there is not an already assigned pad_token, assign an existing # special token to also be the padding token if tokenizer.pad_token is None and batch_size > 1: lowerCamelCase__ = list(tokenizer.special_tokens_map_extended.values() ) # check that the model already has at least one special token defined assert ( len(snake_case__ ) > 0 ), "If batch_size > 1, model must have at least one special token to use for padding. Please use a different model or set batch_size=1." # assign one of the special tokens to also be the pad token tokenizer.add_special_tokens({'''pad_token''': existing_special_tokens[0]} ) if add_start_token: # leave room for <BOS> token to be added: assert ( tokenizer.bos_token is not None ), "Input model must already have a BOS token if using add_start_token=True. Please use a different model, or set add_start_token=False" lowerCamelCase__ = model.config.max_length - 1 else: lowerCamelCase__ = model.config.max_length lowerCamelCase__ = tokenizer( snake_case__ , add_special_tokens=snake_case__ , padding=snake_case__ , truncation=snake_case__ , max_length=snake_case__ , return_tensors='''pt''' , return_attention_mask=snake_case__ , ).to(snake_case__ ) lowerCamelCase__ = encodings['''input_ids'''] lowerCamelCase__ = encodings['''attention_mask'''] # check that each input is long enough: if add_start_token: assert torch.all(torch.ge(attn_masks.sum(1 ) , 1 ) ), "Each input text must be at least one token long." else: assert torch.all( torch.ge(attn_masks.sum(1 ) , 2 ) ), "When add_start_token=False, each input text must be at least two tokens long. Run with add_start_token=True if inputting strings of only one token, and remove all empty input strings." lowerCamelCase__ = [] lowerCamelCase__ = CrossEntropyLoss(reduction='''none''' ) for start_index in logging.tqdm(range(0 , len(snake_case__ ) , snake_case__ ) ): lowerCamelCase__ = min(start_index + batch_size , len(snake_case__ ) ) lowerCamelCase__ = encoded_texts[start_index:end_index] lowerCamelCase__ = attn_masks[start_index:end_index] if add_start_token: lowerCamelCase__ = torch.tensor([[tokenizer.bos_token_id]] * encoded_batch.size(dim=0 ) ).to(snake_case__ ) lowerCamelCase__ = torch.cat([bos_tokens_tensor, encoded_batch] , dim=1 ) lowerCamelCase__ = torch.cat( [torch.ones(bos_tokens_tensor.size() , dtype=torch.intaa ).to(snake_case__ ), attn_mask] , dim=1 ) lowerCamelCase__ = encoded_batch with torch.no_grad(): lowerCamelCase__ = model(snake_case__ , attention_mask=snake_case__ ).logits lowerCamelCase__ = out_logits[..., :-1, :].contiguous() lowerCamelCase__ = labels[..., 1:].contiguous() lowerCamelCase__ = attn_mask[..., 1:].contiguous() lowerCamelCase__ = torch.expa( (loss_fct(shift_logits.transpose(1 , 2 ) , snake_case__ ) * shift_attention_mask_batch).sum(1 ) / shift_attention_mask_batch.sum(1 ) ) ppls += perplexity_batch.tolist() return {"perplexities": ppls, "mean_perplexity": np.mean(snake_case__ )}
209
from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging lowercase_ : Optional[int] = logging.get_logger(__name__) lowercase_ : Dict = { 'Salesforce/codegen-350M-nl': 'https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json', 'Salesforce/codegen-350M-multi': 'https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json', 'Salesforce/codegen-350M-mono': 'https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json', 'Salesforce/codegen-2B-nl': 'https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json', 'Salesforce/codegen-2B-multi': 'https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json', 'Salesforce/codegen-2B-mono': 'https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json', 'Salesforce/codegen-6B-nl': 'https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json', 'Salesforce/codegen-6B-multi': 'https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json', 'Salesforce/codegen-6B-mono': 'https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json', 'Salesforce/codegen-16B-nl': 'https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json', 'Salesforce/codegen-16B-multi': 'https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json', 'Salesforce/codegen-16B-mono': 'https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json', } class __lowerCAmelCase ( UpperCAmelCase__ ): snake_case_ : Tuple = "codegen" snake_case_ : Optional[Any] = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Tuple , snake_case__ : Any=50_400 , snake_case__ : int=2_048 , snake_case__ : Optional[Any]=2_048 , snake_case__ : Tuple=4_096 , snake_case__ : List[str]=28 , snake_case__ : List[Any]=16 , snake_case__ : int=64 , snake_case__ : Tuple=None , snake_case__ : Union[str, Any]="gelu_new" , snake_case__ : List[Any]=0.0 , snake_case__ : List[str]=0.0 , snake_case__ : Optional[int]=0.0 , snake_case__ : Dict=1e-5 , snake_case__ : int=0.02 , snake_case__ : Union[str, Any]=True , snake_case__ : str=50_256 , snake_case__ : List[str]=50_256 , snake_case__ : Optional[int]=False , **snake_case__ : str , ): """simple docstring""" _UpperCAmelCase = vocab_size _UpperCAmelCase = n_ctx _UpperCAmelCase = n_positions _UpperCAmelCase = n_embd _UpperCAmelCase = n_layer _UpperCAmelCase = n_head _UpperCAmelCase = n_inner _UpperCAmelCase = rotary_dim _UpperCAmelCase = activation_function _UpperCAmelCase = resid_pdrop _UpperCAmelCase = embd_pdrop _UpperCAmelCase = attn_pdrop _UpperCAmelCase = layer_norm_epsilon _UpperCAmelCase = initializer_range _UpperCAmelCase = use_cache _UpperCAmelCase = bos_token_id _UpperCAmelCase = eos_token_id super().__init__( bos_token_id=snake_case__ , eos_token_id=snake_case__ , tie_word_embeddings=snake_case__ , **snake_case__ ) class __lowerCAmelCase ( UpperCAmelCase__ ): def __init__( self : List[str] , snake_case__ : PretrainedConfig , snake_case__ : str = "default" , snake_case__ : List[PatchingSpec] = None , snake_case__ : bool = False , ): """simple docstring""" super().__init__(snake_case__ , task=snake_case__ , patching_specs=snake_case__ , use_past=snake_case__ ) if not getattr(self._config , "pad_token_id" , snake_case__ ): # TODO: how to do that better? _UpperCAmelCase = 0 @property def UpperCamelCase ( self : Tuple ): """simple docstring""" _UpperCAmelCase = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: self.fill_with_past_key_values_(snake_case__ , direction="inputs" ) _UpperCAmelCase = {0: "batch", 1: "past_sequence + sequence"} else: _UpperCAmelCase = {0: "batch", 1: "sequence"} return common_inputs @property def UpperCamelCase ( self : int ): """simple docstring""" return self._config.n_layer @property def UpperCamelCase ( self : List[str] ): """simple docstring""" return self._config.n_head def UpperCamelCase ( self : List[Any] , snake_case__ : PreTrainedTokenizer , snake_case__ : int = -1 , snake_case__ : int = -1 , snake_case__ : bool = False , snake_case__ : Optional[TensorType] = None , ): """simple docstring""" _UpperCAmelCase = super(snake_case__ , self ).generate_dummy_inputs( snake_case__ , batch_size=snake_case__ , seq_length=snake_case__ , is_pair=snake_case__ , framework=snake_case__ ) # We need to order the input in the way they appears in the forward() _UpperCAmelCase = OrderedDict({"input_ids": common_inputs["input_ids"]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch _UpperCAmelCase , _UpperCAmelCase = common_inputs["input_ids"].shape # Not using the same length for past_key_values _UpperCAmelCase = seqlen + 2 _UpperCAmelCase = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) _UpperCAmelCase = [ (torch.zeros(snake_case__ ), torch.zeros(snake_case__ )) for _ in range(self.num_layers ) ] _UpperCAmelCase = common_inputs["attention_mask"] if self.use_past: _UpperCAmelCase = ordered_inputs["attention_mask"].dtype _UpperCAmelCase = torch.cat( [ordered_inputs["attention_mask"], torch.ones(snake_case__ , snake_case__ , dtype=snake_case__ )] , dim=1 ) return ordered_inputs @property def UpperCamelCase ( self : Any ): """simple docstring""" return 13
133
0
"""simple docstring""" from __future__ import annotations from collections.abc import Sequence from typing import Literal def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" A__ = list(UpperCamelCase__ ) A__ = list(UpperCamelCase__ ) A__ = 0 for i in range(len(UpperCamelCase__ ) ): if lista[i] != lista[i]: count += 1 A__ = '_' if count > 1: return False else: return "".join(UpperCamelCase__ ) def UpperCAmelCase ( UpperCamelCase__ ): """simple docstring""" A__ = [] while True: A__ = ['$'] * len(UpperCamelCase__ ) A__ = [] for i in range(len(UpperCamelCase__ ) ): for j in range(i + 1 , len(UpperCamelCase__ ) ): A__ = compare_string(binary[i] , binary[j] ) if k is False: A__ = '*' A__ = '*' temp.append('X' ) for i in range(len(UpperCamelCase__ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(UpperCamelCase__ ) == 0: return pi A__ = list(set(UpperCamelCase__ ) ) def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" A__ = [] for minterm in minterms: A__ = '' for _ in range(UpperCamelCase__ ): A__ = str(minterm % 2 ) + string minterm //= 2 temp.append(UpperCamelCase__ ) return temp def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" A__ = list(UpperCamelCase__ ) A__ = list(UpperCamelCase__ ) A__ = 0 for i in range(len(UpperCamelCase__ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" A__ = [] A__ = [0] * len(UpperCamelCase__ ) for i in range(len(chart[0] ) ): A__ = 0 A__ = -1 for j in range(len(UpperCamelCase__ ) ): if chart[j][i] == 1: count += 1 A__ = j if count == 1: A__ = 1 for i in range(len(UpperCamelCase__ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(UpperCamelCase__ ) ): A__ = 0 temp.append(prime_implicants[i] ) while True: A__ = 0 A__ = -1 A__ = 0 for i in range(len(UpperCamelCase__ ) ): A__ = chart[i].count(1 ) if count_n > max_n: A__ = count_n A__ = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(UpperCamelCase__ ) ): A__ = 0 def UpperCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ): """simple docstring""" A__ = [[0 for x in range(len(UpperCamelCase__ ) )] for x in range(len(UpperCamelCase__ ) )] for i in range(len(UpperCamelCase__ ) ): A__ = prime_implicants[i].count('_' ) for j in range(len(UpperCamelCase__ ) ): if is_for_table(prime_implicants[i] , binary[j] , UpperCamelCase__ ): A__ = 1 return chart def UpperCAmelCase ( ): """simple docstring""" A__ = int(input('Enter the no. of variables\n' ) ) A__ = [ float(UpperCamelCase__ ) for x in input( 'Enter the decimal representation of Minterms \'Spaces Separated\'\n' ).split() ] A__ = decimal_to_binary(UpperCamelCase__ , UpperCamelCase__ ) A__ = check(UpperCamelCase__ ) print('Prime Implicants are:' ) print(UpperCamelCase__ ) A__ = prime_implicant_chart(UpperCamelCase__ , UpperCamelCase__ ) A__ = selection(UpperCamelCase__ , UpperCamelCase__ ) print('Essential Prime Implicants are:' ) print(UpperCamelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() main()
154
"""simple docstring""" def UpperCAmelCase ( UpperCamelCase__ = 100 ): """simple docstring""" A__ = (n * (n + 1) // 2) ** 2 A__ = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(F'''{solution() = }''')
154
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) a : int = { 'configuration_layoutlmv3': [ 'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv3Config', 'LayoutLMv3OnnxConfig', ], 'processing_layoutlmv3': ['LayoutLMv3Processor'], 'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Any = ['LayoutLMv3TokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Dict = [ 'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST', 'LayoutLMv3ForQuestionAnswering', 'LayoutLMv3ForSequenceClassification', 'LayoutLMv3ForTokenClassification', 'LayoutLMv3Model', 'LayoutLMv3PreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Optional[int] = [ 'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFLayoutLMv3ForQuestionAnswering', 'TFLayoutLMv3ForSequenceClassification', 'TFLayoutLMv3ForTokenClassification', 'TFLayoutLMv3Model', 'TFLayoutLMv3PreTrainedModel', ] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Optional[Any] = ['LayoutLMv3FeatureExtractor'] a : Any = ['LayoutLMv3ImageProcessor'] if TYPE_CHECKING: from .configuration_layoutlmva import ( LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig, LayoutLMvaOnnxConfig, ) from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_layoutlmva import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, TFLayoutLMvaPreTrainedModel, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor from .image_processing_layoutlmva import LayoutLMvaImageProcessor else: import sys a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
56
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) a : Tuple = { 'configuration_llama': ['LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LlamaConfig'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Optional[Any] = ['LlamaTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : str = ['LlamaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Optional[Any] = [ 'LlamaForCausalLM', 'LlamaModel', 'LlamaPreTrainedModel', 'LlamaForSequenceClassification', ] if TYPE_CHECKING: from .configuration_llama import LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, LlamaConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama import LlamaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama_fast import LlamaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_llama import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaPreTrainedModel else: import sys a : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
56
1
"""simple docstring""" from collections.abc import Callable import numpy as np def _A ( lowercase , lowercase , lowercase , lowercase , lowercase ): """simple docstring""" a =int(np.ceil((x_end - xa) / step_size ) ) a =np.zeros((n + 1,) ) a =ya a =xa for k in range(lowercase ): a =y[k] + step_size * ode_func(lowercase , y[k] ) a =y[k] + ( (step_size / 2) * (ode_func(lowercase , y[k] ) + ode_func(x + step_size , lowercase )) ) x += step_size return y if __name__ == "__main__": import doctest doctest.testmod()
215
"""simple docstring""" import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def _A ( lowercase ): """simple docstring""" a ={} a =tokenizer(example['''content'''] , truncation=lowercase )['''input_ids'''] a =len(example['''content'''] ) / len(output['''input_ids'''] ) return output lowerCamelCase_ : Optional[int] = HfArgumentParser(PretokenizationArguments) lowerCamelCase_ : Optional[Any] = parser.parse_args() if args.num_workers is None: lowerCamelCase_ : Tuple = multiprocessing.cpu_count() lowerCamelCase_ : Any = AutoTokenizer.from_pretrained(args.tokenizer_dir) lowerCamelCase_ : Any = time.time() lowerCamelCase_ : int = load_dataset(args.dataset_name, split="""train""") print(F'Dataset loaded in {time.time()-t_start:.2f}s') lowerCamelCase_ : List[str] = time.time() lowerCamelCase_ : str = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ """repo_name""", """path""", """copies""", """size""", """content""", """license""", """hash""", """line_mean""", """line_max""", """alpha_frac""", """autogenerated""", ], ) print(F'Dataset tokenized in {time.time()-t_start:.2f}s') lowerCamelCase_ : Union[str, Any] = time.time() ds.push_to_hub(args.tokenized_data_repo) print(F'Data pushed to the hub in {time.time()-t_start:.2f}s')
215
1
from itertools import product def _UpperCAmelCase (UpperCamelCase__ : int , UpperCamelCase__ : int ): _A : Dict = sides_number _A : Any = max_face_number * dice_number _A : Optional[int] = [0] * (max_total + 1) _A : Any = 1 _A : str = range(UpperCamelCase__ , max_face_number + 1 ) for dice_numbers in product(UpperCamelCase__ , repeat=UpperCamelCase__ ): _A : Tuple = sum(UpperCamelCase__ ) totals_frequencies[total] += 1 return totals_frequencies def _UpperCAmelCase (): _A : Any = total_frequency_distribution( sides_number=4 , dice_number=9 ) _A : Tuple = total_frequency_distribution( sides_number=6 , dice_number=6 ) _A : Any = 0 _A : int = 9 _A : List[str] = 4 * 9 _A : Dict = 6 for peter_total in range(UpperCamelCase__ , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) _A : Dict = (4**9) * (6**6) _A : List[str] = peter_wins_count / total_games_number _A : Dict = round(UpperCamelCase__ , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(f"{solution() = }")
11
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer SCREAMING_SNAKE_CASE :Any = '''bart''' SCREAMING_SNAKE_CASE :Any = True @st.cache(allow_output_mutation=lowerCAmelCase_ ) def _lowerCAmelCase ( )->List[Any]: '''simple docstring''' if LOAD_DENSE_INDEX: snake_case_ = AutoTokenizer.from_pretrained("yjernite/retribert-base-uncased" ) snake_case_ = AutoModel.from_pretrained("yjernite/retribert-base-uncased" ).to("cuda:0" ) snake_case_ = qar_model.eval() else: snake_case_ , snake_case_ = (None, None) if MODEL_TYPE == "bart": snake_case_ = AutoTokenizer.from_pretrained("yjernite/bart_eli5" ) snake_case_ = AutoModelForSeqaSeqLM.from_pretrained("yjernite/bart_eli5" ).to("cuda:0" ) snake_case_ = torch.load("seq2seq_models/eli5_bart_model_blm_2.pth" ) sas_model.load_state_dict(save_dict["model"] ) snake_case_ = sas_model.eval() else: snake_case_ , snake_case_ = make_qa_sas_model( model_name="t5-small" , from_file="seq2seq_models/eli5_t5_model_1024_4.pth" , device="cuda:0" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=lowerCAmelCase_ ) def _lowerCAmelCase ( )->Tuple: '''simple docstring''' if LOAD_DENSE_INDEX: snake_case_ = faiss.StandardGpuResources() snake_case_ = datasets.load_dataset(path="wiki_snippets" , name="wiki40b_en_100_0" )["train"] snake_case_ = np.memmap( "wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat" , dtype="float32" , mode="r" , shape=(wikiaab_passages.num_rows, 128) , ) snake_case_ = faiss.IndexFlatIP(128 ) snake_case_ = faiss.index_cpu_to_gpu(lowerCAmelCase_ , 1 , lowerCAmelCase_ ) wikiaab_gpu_index_flat.add(lowerCAmelCase_ ) # TODO fix for larger GPU else: snake_case_ , snake_case_ = (None, None) snake_case_ = Elasticsearch([{"host": "localhost", "port": "9200"}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=lowerCAmelCase_ ) def _lowerCAmelCase ( )->Union[str, Any]: '''simple docstring''' snake_case_ = datasets.load_dataset("eli5" , name="LFQA_reddit" ) snake_case_ = elia["train_eli5"] snake_case_ = np.memmap( "eli5_questions_reps.dat" , dtype="float32" , mode="r" , shape=(elia_train.num_rows, 128) ) snake_case_ = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(lowerCAmelCase_ ) return (elia_train, eli5_train_q_index) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :Union[str, Any] = load_indexes() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :Optional[int] = load_models() SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :Union[str, Any] = load_train_data() def _lowerCAmelCase ( lowerCAmelCase_ :List[Any] , lowerCAmelCase_ :List[Any]=10 )->int: '''simple docstring''' snake_case_ = embed_questions_for_retrieval([question] , lowerCAmelCase_ , lowerCAmelCase_ ) snake_case_ , snake_case_ = eli5_train_q_index.search(lowerCAmelCase_ , lowerCAmelCase_ ) snake_case_ = [elia_train[int(lowerCAmelCase_ )] for i in I[0]] return nn_examples def _lowerCAmelCase ( lowerCAmelCase_ :Optional[int] , lowerCAmelCase_ :Optional[int]="wiki40b" , lowerCAmelCase_ :Optional[Any]="dense" , lowerCAmelCase_ :Any=10 )->Union[str, Any]: '''simple docstring''' if source == "none": snake_case_ , snake_case_ = (" <P> ".join(["" for _ in range(11 )] ).strip(), []) else: if method == "dense": snake_case_ , snake_case_ = query_qa_dense_index( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) else: snake_case_ , snake_case_ = query_es_index( lowerCAmelCase_ , lowerCAmelCase_ , index_name="english_wiki40b_snippets_100w" , n_results=lowerCAmelCase_ , ) snake_case_ = [ (res["article_title"], res["section_title"].strip(), res["score"], res["passage_text"]) for res in hit_lst ] snake_case_ = "question: {} context: {}".format(lowerCAmelCase_ , lowerCAmelCase_ ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda lowerCAmelCase_ : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda lowerCAmelCase_ : None), } ) def _lowerCAmelCase ( lowerCAmelCase_ :List[str] , lowerCAmelCase_ :Optional[int] , lowerCAmelCase_ :Union[str, Any] , lowerCAmelCase_ :int=64 , lowerCAmelCase_ :str=256 , lowerCAmelCase_ :int=False , lowerCAmelCase_ :Optional[int]=2 , lowerCAmelCase_ :Optional[int]=0.9_5 , lowerCAmelCase_ :str=0.8 )->Any: '''simple docstring''' with torch.no_grad(): snake_case_ = qa_sas_generate( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , num_answers=1 , num_beams=lowerCAmelCase_ , min_len=lowerCAmelCase_ , max_len=lowerCAmelCase_ , do_sample=lowerCAmelCase_ , temp=lowerCAmelCase_ , top_p=lowerCAmelCase_ , top_k=lowerCAmelCase_ , max_input_length=1_024 , device="cuda:0" , )[0] return (answer, support_list) st.title('''Long Form Question Answering with ELI5''') # Start sidebar SCREAMING_SNAKE_CASE :Dict = '''<img src=\'https://huggingface.co/front/assets/huggingface_logo.svg\'>''' SCREAMING_SNAKE_CASE :Optional[int] = ''' <html> <head> <style> .img-container { padding-left: 90px; padding-right: 90px; padding-top: 50px; padding-bottom: 50px; background-color: #f0f3f9; } </style> </head> <body> <span class="img-container"> <!-- Inline parent element --> %s </span> </body> </html> ''' % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia SCREAMING_SNAKE_CASE :Optional[Any] = ''' This demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html). First, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset, a pre-processed fixed snapshot of Wikipedia. ''' st.sidebar.markdown(description, unsafe_allow_html=True) SCREAMING_SNAKE_CASE :Tuple = [ '''Answer the question''', '''View the retrieved document only''', '''View the most similar ELI5 question and answer''', '''Show me everything, please!''', ] SCREAMING_SNAKE_CASE :Any = st.sidebar.checkbox('''Demo options''') if demo_options: SCREAMING_SNAKE_CASE :Tuple = st.sidebar.selectbox( '''''', action_list, index=3, ) SCREAMING_SNAKE_CASE :Optional[int] = action_list.index(action_st) SCREAMING_SNAKE_CASE :Any = st.sidebar.selectbox( '''''', ['''Show full text of passages''', '''Show passage section titles'''], index=0, ) SCREAMING_SNAKE_CASE :Optional[int] = show_type == '''Show full text of passages''' else: SCREAMING_SNAKE_CASE :List[str] = 3 SCREAMING_SNAKE_CASE :Dict = True SCREAMING_SNAKE_CASE :List[Any] = st.sidebar.checkbox('''Retrieval options''') if retrieval_options: SCREAMING_SNAKE_CASE :str = ''' ### Information retriever options The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs. The answer is then generated by sequence to sequence model which takes the question and retrieved document as input. ''' st.sidebar.markdown(retriever_info) SCREAMING_SNAKE_CASE :str = st.sidebar.selectbox('''Which Wikipedia format should the model use?''', ['''wiki40b''', '''none''']) SCREAMING_SNAKE_CASE :Union[str, Any] = st.sidebar.selectbox('''Which Wikipedia indexer should the model use?''', ['''dense''', '''sparse''', '''mixed''']) else: SCREAMING_SNAKE_CASE :Dict = '''wiki40b''' SCREAMING_SNAKE_CASE :Optional[int] = '''dense''' SCREAMING_SNAKE_CASE :str = '''beam''' SCREAMING_SNAKE_CASE :List[str] = 2 SCREAMING_SNAKE_CASE :int = 64 SCREAMING_SNAKE_CASE :List[str] = 2_56 SCREAMING_SNAKE_CASE :str = None SCREAMING_SNAKE_CASE :Optional[Any] = None SCREAMING_SNAKE_CASE :int = st.sidebar.checkbox('''Generation options''') if generate_options: SCREAMING_SNAKE_CASE :Optional[Any] = ''' ### Answer generation options The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large) weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with **beam** search, or **sample** from the decoder\'s output probabilities. ''' st.sidebar.markdown(generate_info) SCREAMING_SNAKE_CASE :str = st.sidebar.selectbox('''Would you like to use beam search or sample an answer?''', ['''beam''', '''sampled''']) SCREAMING_SNAKE_CASE :Optional[Any] = st.sidebar.slider( '''Minimum generation length''', min_value=8, max_value=2_56, value=64, step=8, format=None, key=None ) SCREAMING_SNAKE_CASE :Union[str, Any] = st.sidebar.slider( '''Maximum generation length''', min_value=64, max_value=5_12, value=2_56, step=16, format=None, key=None ) if sampled == "beam": SCREAMING_SNAKE_CASE :List[Any] = st.sidebar.slider('''Beam size''', min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: SCREAMING_SNAKE_CASE :Any = st.sidebar.slider( '''Nucleus sampling p''', min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) SCREAMING_SNAKE_CASE :Optional[Any] = st.sidebar.slider( '''Temperature''', min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) SCREAMING_SNAKE_CASE :Union[str, Any] = None # start main text SCREAMING_SNAKE_CASE :Any = [ '''<MY QUESTION>''', '''How do people make chocolate?''', '''Why do we get a fever when we are sick?''', '''How can different animals perceive different colors?''', '''What is natural language processing?''', '''What\'s the best way to treat a sunburn?''', '''What exactly are vitamins ?''', '''How does nuclear energy provide electricity?''', '''What\'s the difference between viruses and bacteria?''', '''Why are flutes classified as woodwinds when most of them are made out of metal ?''', '''Why do people like drinking coffee even though it tastes so bad?''', '''What happens when wine ages? How does it make the wine taste better?''', '''If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?''', '''How can we set a date to the beginning or end of an artistic period? Doesn\'t the change happen gradually?''', '''How does New Zealand have so many large bird predators?''', ] SCREAMING_SNAKE_CASE :Optional[Any] = st.selectbox( '''What would you like to ask? ---- select <MY QUESTION> to enter a new query''', questions_list, index=1, ) if question_s == "<MY QUESTION>": SCREAMING_SNAKE_CASE :List[Any] = st.text_input('''Enter your question here:''', '''''') else: SCREAMING_SNAKE_CASE :str = question_s if st.button('''Show me!'''): if action in [0, 1, 3]: if index_type == "mixed": SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :int = make_support(question, source=wiki_source, method='''dense''', n_results=10) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :Optional[int] = make_support(question, source=wiki_source, method='''sparse''', n_results=10) SCREAMING_SNAKE_CASE :Optional[Any] = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] SCREAMING_SNAKE_CASE :Union[str, Any] = support_list[:10] SCREAMING_SNAKE_CASE :int = '''<P> ''' + ''' <P> '''.join([res[-1] for res in support_list]) else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :str = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE :Any = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == '''sampled'''), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown('''### The model generated answer is:''') st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown('''--- \n ### The model is drawing information from the following Wikipedia passages:''') for i, res in enumerate(support_list): SCREAMING_SNAKE_CASE :Dict = '''https://en.wikipedia.org/wiki/{}'''.format(res[0].replace(''' ''', '''_''')) SCREAMING_SNAKE_CASE :Tuple = res[1].strip() if sec_titles == "": SCREAMING_SNAKE_CASE :Tuple = '''[{}]({})'''.format(res[0], wiki_url) else: SCREAMING_SNAKE_CASE :Union[str, Any] = sec_titles.split(''' & ''') SCREAMING_SNAKE_CASE :Optional[int] = ''' & '''.join( ['''[{}]({}#{})'''.format(sec.strip(), wiki_url, sec.strip().replace(''' ''', '''_''')) for sec in sec_list] ) st.markdown( '''{0:02d} - **Article**: {1:<18} <br> _Section_: {2}'''.format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( '''> <span style="font-family:arial; font-size:10pt;">''' + res[-1] + '''</span>''', unsafe_allow_html=True ) if action in [2, 3]: SCREAMING_SNAKE_CASE :List[Any] = find_nearest_training(question) SCREAMING_SNAKE_CASE :List[Any] = nn_train_list[0] st.markdown( '''--- \n ### The most similar question in the ELI5 training set was: \n\n {}'''.format(train_exple['''title''']) ) SCREAMING_SNAKE_CASE :Any = [ '''{}. {}'''.format(i + 1, ''' \n'''.join([line.strip() for line in ans.split('''\n''') if line.strip() != ''''''])) for i, (ans, sc) in enumerate(zip(train_exple['''answers''']['''text'''], train_exple['''answers''']['''score'''])) if i == 0 or sc > 2 ] st.markdown('''##### Its answers were: \n\n {}'''.format('''\n'''.join(answers_st))) SCREAMING_SNAKE_CASE :Optional[int] = ''' --- **Disclaimer** *The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system. Evaluating biases of such a model and ensuring factual generations are still very much open research problems. Therefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.* ''' st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
159
0
import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class __snake_case ( unittest.TestCase ): @slow def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = XLMRobertaModel.from_pretrained('''xlm-roberta-base''') UpperCAmelCase_ = torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]) # The dog is cute and lives in the garden house UpperCAmelCase_ = torch.Size((1, 12, 768)) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase_ = torch.tensor( [[-0.0_1_0_1, 0.1_2_1_8, -0.0_8_0_3, 0.0_8_0_1, 0.1_3_2_7, 0.0_7_7_6, -0.1_2_1_5, 0.2_3_8_3, 0.3_3_3_8, 0.3_1_0_6, 0.0_3_0_0, 0.0_2_5_2]]) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase_ = model(_snake_case)['''last_hidden_state'''].detach() self.assertEqual(output.shape , _snake_case) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _snake_case , atol=1e-3)) @slow def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = XLMRobertaModel.from_pretrained('''xlm-roberta-large''') UpperCAmelCase_ = torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]) # The dog is cute and lives in the garden house UpperCAmelCase_ = torch.Size((1, 12, 1024)) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase_ = torch.tensor( [[-0.0_6_9_9, -0.0_3_1_8, 0.0_7_0_5, -0.1_2_4_1, 0.0_9_9_9, -0.0_5_2_0, 0.1_0_0_4, -0.1_8_3_8, -0.4_7_0_4, 0.1_4_3_7, 0.0_8_2_1, 0.0_1_2_6]]) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase_ = model(_snake_case)['''last_hidden_state'''].detach() self.assertEqual(output.shape , _snake_case) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _snake_case , atol=1e-3))
7
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( ImageTextPipelineOutput, UniDiffuserPipeline, ) else: from .modeling_text_decoder import UniDiffuserTextDecoder from .modeling_uvit import UniDiffuserModel, UTransformeraDModel from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline
7
1
def __lowercase ( __lowerCAmelCase : str , __lowerCAmelCase : str ): a__ = len(__lowerCAmelCase ) a__ = len(__lowerCAmelCase ) a__ = ( first_str_length if first_str_length > second_str_length else second_str_length ) a__ = [] for char_count in range(__lowerCAmelCase ): if char_count < first_str_length: output_list.append(first_str[char_count] ) if char_count < second_str_length: output_list.append(second_str[char_count] ) return "".join(__lowerCAmelCase ) if __name__ == "__main__": print(alternative_string_arrange('''AB''', '''XYZ'''), end=''' ''')
240
import json import os import unittest from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class snake_case_ (lowerCamelCase_ , unittest.TestCase ): UpperCAmelCase__ : Tuple = XLMTokenizer UpperCAmelCase__ : List[str] = False def lowerCamelCase__( self :Any ) -> Optional[Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'w</w>', 'r</w>', 't</w>', 'lo', 'low', 'er</w>', 'low</w>', 'lowest</w>', 'newer</w>', 'wider</w>', '<unk>', ] a__ = dict(zip(__snake_case ,range(len(__snake_case ) ) ) ) a__ = ['l o 123', 'lo w 1456', 'e r</w> 1789', ''] 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' ) as fp: fp.write(json.dumps(__snake_case ) ) with open(self.merges_file ,'w' ) as fp: fp.write('\n'.join(__snake_case ) ) def lowerCamelCase__( self :Any ,__snake_case :int ) -> Optional[int]: a__ = 'lower newer' a__ = 'lower newer' return input_text, output_text def lowerCamelCase__( self :Tuple ) -> Tuple: a__ = XLMTokenizer(self.vocab_file ,self.merges_file ) a__ = 'lower' a__ = ['low', 'er</w>'] a__ = tokenizer.tokenize(__snake_case ) self.assertListEqual(__snake_case ,__snake_case ) a__ = tokens + ['<unk>'] a__ = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case ) ,__snake_case ) @slow def lowerCamelCase__( self :List[str] ) -> Optional[Any]: a__ = XLMTokenizer.from_pretrained('xlm-mlm-en-2048' ) a__ = tokenizer.encode('sequence builders' ,add_special_tokens=__snake_case ) a__ = tokenizer.encode('multi-sequence build' ,add_special_tokens=__snake_case ) a__ = tokenizer.build_inputs_with_special_tokens(__snake_case ) a__ = tokenizer.build_inputs_with_special_tokens(__snake_case ,__snake_case ) assert encoded_sentence == [0] + text + [1] assert encoded_pair == [0] + text + [1] + text_a + [1]
240
1
"""simple docstring""" from collections import defaultdict from math import ceil, sqrt def _a ( _snake_case = 100_0000 , _snake_case = 10 ): """simple docstring""" UpperCAmelCase = defaultdict(_snake_case ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: UpperCAmelCase = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: UpperCAmelCase = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(_snake_case , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 10 ) if __name__ == "__main__": print(F"""{solution() = }""")
234
"""simple docstring""" from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image _UpperCamelCase = ["""text""", """image""", """audio"""] def _a ( _snake_case ): """simple docstring""" UpperCAmelCase = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((512, 512) ) ) elif input_type == "audio": inputs.append(torch.ones(3000 ) ) elif isinstance(_snake_case , _snake_case ): inputs.append(create_inputs(_snake_case ) ) else: raise ValueError(F'''Invalid type requested: {input_type}''' ) return inputs def _a ( _snake_case ): """simple docstring""" UpperCAmelCase = [] for output in outputs: if isinstance(_snake_case , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(_snake_case , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(_snake_case , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(F'''Invalid output: {output}''' ) return output_types @is_tool_test class lowerCamelCase__ : def _UpperCamelCase ( self ): self.assertTrue(hasattr(self.tool ,"""inputs""" ) ) self.assertTrue(hasattr(self.tool ,"""outputs""" ) ) UpperCAmelCase = self.tool.inputs for _input in inputs: if isinstance(_input ,A ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) UpperCAmelCase = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def _UpperCamelCase ( self ): UpperCAmelCase = create_inputs(self.tool.inputs ) UpperCAmelCase = self.tool(*A ) # There is a single output if len(self.tool.outputs ) == 1: UpperCAmelCase = [outputs] self.assertListEqual(output_types(A ) ,self.tool.outputs ) def _UpperCamelCase ( self ): self.assertTrue(hasattr(self.tool ,"""description""" ) ) self.assertTrue(hasattr(self.tool ,"""default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def _UpperCamelCase ( self ): UpperCAmelCase = create_inputs(self.tool.inputs ) UpperCAmelCase = self.tool(*A ) if not isinstance(A ,A ): UpperCAmelCase = [outputs] self.assertEqual(len(A ) ,len(self.tool.outputs ) ) for output, output_type in zip(A ,self.tool.outputs ): UpperCAmelCase = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(A ,A ) ) def _UpperCamelCase ( self ): UpperCAmelCase = create_inputs(self.tool.inputs ) UpperCAmelCase = [] for _input, input_type in zip(A ,self.tool.inputs ): if isinstance(A ,A ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error UpperCAmelCase = self.tool(*A ) if not isinstance(A ,A ): UpperCAmelCase = [outputs] self.assertEqual(len(A ) ,len(self.tool.outputs ) )
234
1
'''simple docstring''' import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class _a : def __init__( self : List[str] , lowercase : Optional[Any] , lowercase : Union[str, Any]=13 , lowercase : Optional[int]=7 , lowercase : Optional[int]=True , lowercase : Tuple=True , lowercase : Dict=True , lowercase : str=True , lowercase : int=99 , lowercase : List[Any]=32 , lowercase : Optional[Any]=5 , lowercase : str=4 , lowercase : str=4 , lowercase : List[str]="gelu" , lowercase : Tuple=0.0 , lowercase : List[Any]=0.1 , lowercase : str=True , lowercase : List[Any]=512 , lowercase : Dict=16 , lowercase : Tuple=2 , lowercase : Tuple=0.02 , lowercase : List[str]=3 , lowercase : List[str]=4 , lowercase : Any=None , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_input_mask UpperCAmelCase = use_token_type_ids UpperCAmelCase = use_labels UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_multiple_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout UpperCAmelCase = attention_dropout UpperCAmelCase = weight_tying UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = type_sequence_label_size UpperCAmelCase = initializer_range UpperCAmelCase = num_labels UpperCAmelCase = num_choices UpperCAmelCase = scope def A ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = None if self.use_input_mask: UpperCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase = self.get_config() return config, input_ids, input_mask, token_labels def A ( self : List[str] ): '''simple docstring''' return GPTNeoXJapaneseConfig( 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_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowercase , initializer_range=self.initializer_range , ) def A ( self : Tuple ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = self.prepare_config_and_inputs() UpperCAmelCase = True return config, input_ids, input_mask, token_labels def A ( self : Optional[Any] , lowercase : Optional[int] , lowercase : Optional[int] , lowercase : str ): '''simple docstring''' UpperCAmelCase = GPTNeoXJapaneseModel(config=lowercase ) model.to(lowercase ) model.eval() UpperCAmelCase = model(lowercase , attention_mask=lowercase ) UpperCAmelCase = model(lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self : Tuple , lowercase : List[Any] , lowercase : str , lowercase : Optional[Any] ): '''simple docstring''' UpperCAmelCase = True UpperCAmelCase = GPTNeoXJapaneseModel(lowercase ) model.to(lowercase ) model.eval() UpperCAmelCase = model(lowercase , attention_mask=lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self : Any , lowercase : Optional[Any] , lowercase : Union[str, Any] , lowercase : str , lowercase : Any ): '''simple docstring''' UpperCAmelCase = GPTNeoXJapaneseForCausalLM(config=lowercase ) model.to(lowercase ) model.eval() UpperCAmelCase = model(lowercase , attention_mask=lowercase , labels=lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A ( self : Tuple , lowercase : Dict , lowercase : Tuple , lowercase : List[str] ): '''simple docstring''' UpperCAmelCase = True UpperCAmelCase = GPTNeoXJapaneseForCausalLM(config=lowercase ) model.to(lowercase ) model.eval() # first forward pass UpperCAmelCase = model(lowercase , attention_mask=lowercase , use_cache=lowercase ) UpperCAmelCase = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCAmelCase = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCAmelCase = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCAmelCase = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCAmelCase = model(lowercase , attention_mask=lowercase , output_hidden_states=lowercase ) UpperCAmelCase = output_from_no_past['''hidden_states'''][0] UpperCAmelCase = model( lowercase , attention_mask=lowercase , past_key_values=lowercase , output_hidden_states=lowercase , )['''hidden_states'''][0] # select random slice UpperCAmelCase = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCAmelCase = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCAmelCase = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowercase , lowercase , atol=1E-3 ) ) def A ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = config_and_inputs UpperCAmelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class _a ( __a , __a , unittest.TestCase ): __a : List[Any] = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () __a : Tuple = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () __a : Optional[Any] = ( {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) __a : Union[str, Any] = False __a : Optional[int] = False __a : List[Any] = False __a : Dict = False def A ( self : str ): '''simple docstring''' UpperCAmelCase = GPTNeoXJapaneseModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=lowercase , hidden_size=37 ) def A ( self : Optional[Any] ): '''simple docstring''' self.config_tester.run_common_tests() def A ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowercase , lowercase , lowercase ) def A ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(lowercase , lowercase , lowercase ) def A ( self : Dict ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_decoder() UpperCAmelCase = None self.model_tester.create_and_check_model_as_decoder(lowercase , lowercase , lowercase ) def A ( self : Any ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(lowercase , lowercase , lowercase ) def A ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*lowercase ) @slow def A ( self : List[str] ): '''simple docstring''' UpperCAmelCase = '''abeja/gpt-neox-japanese-2.7b''' UpperCAmelCase = ['''データサイエンティストとは、''', '''100年後に必要とされる会社は、''', '''フルリモートの環境で働くために必要なことは、''', '''国境の長いトンネルを抜けると''', '''美味しい日本食といえば、'''] UpperCAmelCase = [ '''データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。''', '''100年後に必要とされる会社は、「人」が中心の会社です。''', '''フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。''', '''国境の長いトンネルを抜けると、そこは雪国だった。''', '''美味しい日本食といえば、やっぱりお寿司ですよね。''', ] UpperCAmelCase = GPTNeoXJapaneseTokenizer.from_pretrained(lowercase ) UpperCAmelCase = GPTNeoXJapaneseForCausalLM.from_pretrained(lowercase ) UpperCAmelCase = [] for prompt in prompts: UpperCAmelCase = tokenizer(lowercase , return_tensors='''pt''' ).input_ids UpperCAmelCase = model.generate(lowercase , max_length=50 ) UpperCAmelCase = tokenizer.batch_decode(lowercase , skip_special_tokens=lowercase ) predicted_outputs += generated_string self.assertListEqual(lowercase , lowercase )
34
'''simple docstring''' import argparse import json from pathlib import Path import torch import torchaudio from datasets import load_dataset from huggingface_hub import hf_hub_download from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification from transformers.utils import logging logging.set_verbosity_info() A =logging.get_logger(__name__) def snake_case_ (_a : List[str] ): UpperCAmelCase = ASTConfig() if "10-10" in model_name: pass elif "speech-commands" in model_name: UpperCAmelCase = 1_2_8 elif "12-12" in model_name: UpperCAmelCase = 1_2 UpperCAmelCase = 1_2 elif "14-14" in model_name: UpperCAmelCase = 1_4 UpperCAmelCase = 1_4 elif "16-16" in model_name: UpperCAmelCase = 1_6 UpperCAmelCase = 1_6 else: raise ValueError('''Model not supported''' ) UpperCAmelCase = '''huggingface/label-files''' if "speech-commands" in model_name: UpperCAmelCase = 3_5 UpperCAmelCase = '''speech-commands-v2-id2label.json''' else: UpperCAmelCase = 5_2_7 UpperCAmelCase = '''audioset-id2label.json''' UpperCAmelCase = json.load(open(hf_hub_download(_a , _a , repo_type='''dataset''' ) , '''r''' ) ) UpperCAmelCase = {int(_a ): v for k, v in idalabel.items()} UpperCAmelCase = idalabel UpperCAmelCase = {v: k for k, v in idalabel.items()} return config def snake_case_ (_a : Tuple ): if "module.v" in name: UpperCAmelCase = name.replace('''module.v''' , '''audio_spectrogram_transformer''' ) if "cls_token" in name: UpperCAmelCase = name.replace('''cls_token''' , '''embeddings.cls_token''' ) if "dist_token" in name: UpperCAmelCase = name.replace('''dist_token''' , '''embeddings.distillation_token''' ) if "pos_embed" in name: UpperCAmelCase = name.replace('''pos_embed''' , '''embeddings.position_embeddings''' ) if "patch_embed.proj" in name: UpperCAmelCase = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) # transformer blocks if "blocks" in name: UpperCAmelCase = name.replace('''blocks''' , '''encoder.layer''' ) if "attn.proj" in name: UpperCAmelCase = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: UpperCAmelCase = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: UpperCAmelCase = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: UpperCAmelCase = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: UpperCAmelCase = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: UpperCAmelCase = name.replace('''mlp.fc2''' , '''output.dense''' ) # final layernorm if "audio_spectrogram_transformer.norm" in name: UpperCAmelCase = name.replace('''audio_spectrogram_transformer.norm''' , '''audio_spectrogram_transformer.layernorm''' ) # classifier head if "module.mlp_head.0" in name: UpperCAmelCase = name.replace('''module.mlp_head.0''' , '''classifier.layernorm''' ) if "module.mlp_head.1" in name: UpperCAmelCase = name.replace('''module.mlp_head.1''' , '''classifier.dense''' ) return name def snake_case_ (_a : Dict , _a : List[Any] ): for key in orig_state_dict.copy().keys(): UpperCAmelCase = orig_state_dict.pop(_a ) if "qkv" in key: UpperCAmelCase = key.split('''.''' ) UpperCAmelCase = int(key_split[3] ) UpperCAmelCase = config.hidden_size if "weight" in key: UpperCAmelCase = val[:dim, :] UpperCAmelCase = val[dim : dim * 2, :] UpperCAmelCase = val[-dim:, :] else: UpperCAmelCase = val[:dim] UpperCAmelCase = val[dim : dim * 2] UpperCAmelCase = val[-dim:] else: UpperCAmelCase = val return orig_state_dict def snake_case_ (_a : Tuple ): UpperCAmelCase = [ '''module.v.head.weight''', '''module.v.head.bias''', '''module.v.head_dist.weight''', '''module.v.head_dist.bias''', ] for k in ignore_keys: state_dict.pop(_a , _a ) @torch.no_grad() def snake_case_ (_a : int , _a : Union[str, Any] , _a : Dict=False ): UpperCAmelCase = get_audio_spectrogram_transformer_config(_a ) UpperCAmelCase = { '''ast-finetuned-audioset-10-10-0.4593''': ( '''https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.450''': ( '''https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.448''': ( '''https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1''' ), '''ast-finetuned-audioset-10-10-0.448-v2''': ( '''https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1''' ), '''ast-finetuned-audioset-12-12-0.447''': ( '''https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1''' ), '''ast-finetuned-audioset-14-14-0.443''': ( '''https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1''' ), '''ast-finetuned-audioset-16-16-0.442''': ( '''https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1''' ), '''ast-finetuned-speech-commands-v2''': ( '''https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1''' ), } # load original state_dict UpperCAmelCase = model_name_to_url[model_name] UpperCAmelCase = torch.hub.load_state_dict_from_url(_a , map_location='''cpu''' ) # remove some keys remove_keys(_a ) # rename some keys UpperCAmelCase = convert_state_dict(_a , _a ) # load 🤗 model UpperCAmelCase = ASTForAudioClassification(_a ) model.eval() model.load_state_dict(_a ) # verify outputs on dummy input # source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62 UpperCAmelCase = -4.267_7393 if '''speech-commands''' not in model_name else -6.84_5978 UpperCAmelCase = 4.568_9974 if '''speech-commands''' not in model_name else 5.565_4526 UpperCAmelCase = 1_0_2_4 if '''speech-commands''' not in model_name else 1_2_8 UpperCAmelCase = ASTFeatureExtractor(mean=_a , std=_a , max_length=_a ) if "speech-commands" in model_name: UpperCAmelCase = load_dataset('''speech_commands''' , '''v0.02''' , split='''validation''' ) UpperCAmelCase = dataset[0]['''audio''']['''array'''] else: UpperCAmelCase = hf_hub_download( repo_id='''nielsr/audio-spectogram-transformer-checkpoint''' , filename='''sample_audio.flac''' , repo_type='''dataset''' , ) UpperCAmelCase , UpperCAmelCase = torchaudio.load(_a ) UpperCAmelCase = waveform.squeeze().numpy() UpperCAmelCase = feature_extractor(_a , sampling_rate=1_6_0_0_0 , return_tensors='''pt''' ) # forward pass UpperCAmelCase = model(**_a ) UpperCAmelCase = outputs.logits if model_name == "ast-finetuned-audioset-10-10-0.4593": UpperCAmelCase = torch.tensor([-0.8760, -7.0042, -8.6602] ) elif model_name == "ast-finetuned-audioset-10-10-0.450": UpperCAmelCase = torch.tensor([-1.1986, -7.0903, -8.2718] ) elif model_name == "ast-finetuned-audioset-10-10-0.448": UpperCAmelCase = torch.tensor([-2.6128, -8.0080, -9.4344] ) elif model_name == "ast-finetuned-audioset-10-10-0.448-v2": UpperCAmelCase = torch.tensor([-1.5080, -7.4534, -8.8917] ) elif model_name == "ast-finetuned-audioset-12-12-0.447": UpperCAmelCase = torch.tensor([-0.5050, -6.5833, -8.0843] ) elif model_name == "ast-finetuned-audioset-14-14-0.443": UpperCAmelCase = torch.tensor([-0.3826, -7.0336, -8.2413] ) elif model_name == "ast-finetuned-audioset-16-16-0.442": UpperCAmelCase = torch.tensor([-1.2113, -6.9101, -8.3470] ) elif model_name == "ast-finetuned-speech-commands-v2": UpperCAmelCase = torch.tensor([6.1589, -8.0566, -8.7984] ) else: raise ValueError('''Unknown model name''' ) if not torch.allclose(logits[0, :3] , _a , atol=1E-4 ): raise ValueError('''Logits don\'t match''' ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(_a ).mkdir(exist_ok=_a ) print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_a ) print(F"Saving feature extractor to {pytorch_dump_folder_path}" ) feature_extractor.save_pretrained(_a ) if push_to_hub: print('''Pushing model and feature extractor to the hub...''' ) model.push_to_hub(F"MIT/{model_name}" ) feature_extractor.push_to_hub(F"MIT/{model_name}" ) if __name__ == "__main__": A =argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='ast-finetuned-audioset-10-10-0.4593', type=str, help='Name of the Audio Spectrogram Transformer model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) A =parser.parse_args() convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
34
1
'''simple docstring''' def _lowerCamelCase ( lowercase : int = 1000 ) -> int: _a = 1, 1 _a = 2 while True: _a = 0 _a = fa + fa _a = fa, f index += 1 for _ in str(a_ ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
367
'''simple docstring''' import argparse import logging import os import sys import numpy as np import onnxruntime import torch from bart_onnx.generation_onnx import BARTBeamSearchGenerator from bart_onnx.reduce_onnx_size import remove_dup_initializers import transformers from transformers import BartForConditionalGeneration, BartTokenizer logging.basicConfig( format='%(asctime)s | %(levelname)s | %(name)s | [%(filename)s:%(lineno)d] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=os.environ.get('LOGLEVEL', 'INFO').upper(), stream=sys.stdout, ) lowerCAmelCase_ : List[Any] = logging.getLogger(__name__) lowerCAmelCase_ : List[Any] = {'facebook/bart-base': BartForConditionalGeneration} lowerCAmelCase_ : int = {'facebook/bart-base': BartTokenizer} def _lowerCamelCase ( ) -> Union[str, Any]: _a = argparse.ArgumentParser(description="Export Bart model + Beam Search to ONNX graph." ) parser.add_argument( "--validation_file" , type=lowercase , default=lowercase , help="A csv or a json file containing the validation data." ) parser.add_argument( "--max_length" , type=lowercase , default=5 , help="The maximum total input sequence length after tokenization." , ) parser.add_argument( "--num_beams" , type=lowercase , default=lowercase , help=( "Number of beams to use for evaluation. This argument will be " "passed to ``model.generate``, which is used during ``evaluate`` and ``predict``." ) , ) parser.add_argument( "--model_name_or_path" , type=lowercase , help="Path to pretrained model or model identifier from huggingface.co/models." , required=lowercase , ) parser.add_argument( "--config_name" , type=lowercase , default=lowercase , help="Pretrained config name or path if not the same as model_name" , ) parser.add_argument( "--device" , type=lowercase , default="cpu" , help="Device where the model will be run" , ) parser.add_argument("--output_file_path" , type=lowercase , default=lowercase , help="Where to store the final ONNX file." ) _a = parser.parse_args() return args def _lowerCamelCase ( lowercase : Any , lowercase : Tuple="cpu" ) -> Optional[Any]: _a = model_dict[model_name].from_pretrained(lowercase ).to(lowercase ) _a = tokenizer_dict[model_name].from_pretrained(lowercase ) if model_name in ["facebook/bart-base"]: _a = 0 _a = None _a = 0 return huggingface_model, tokenizer def _lowerCamelCase ( lowercase : List[str] , lowercase : Tuple , lowercase : int , lowercase : Any , lowercase : Dict ) -> Any: model.eval() _a = None _a = torch.jit.script(BARTBeamSearchGenerator(lowercase ) ) with torch.no_grad(): _a = "My friends are cool but they eat too many carbs." _a = tokenizer([ARTICLE_TO_SUMMARIZE] , max_length=1024 , return_tensors="pt" ).to(model.device ) _a = model.generate( inputs["input_ids"] , attention_mask=inputs["attention_mask"] , num_beams=lowercase , max_length=lowercase , early_stopping=lowercase , decoder_start_token_id=model.config.decoder_start_token_id , ) torch.onnx.export( lowercase , ( inputs["input_ids"], inputs["attention_mask"], num_beams, max_length, model.config.decoder_start_token_id, ) , lowercase , opset_version=14 , input_names=["input_ids", "attention_mask", "num_beams", "max_length", "decoder_start_token_id"] , output_names=["output_ids"] , dynamic_axes={ "input_ids": {0: "batch", 1: "seq"}, "output_ids": {0: "batch", 1: "seq_out"}, } , example_outputs=lowercase , ) logger.info("Model exported to {}".format(lowercase ) ) _a = remove_dup_initializers(os.path.abspath(lowercase ) ) logger.info("Deduplicated and optimized model written to {}".format(lowercase ) ) _a = onnxruntime.InferenceSession(lowercase ) _a = ort_sess.run( lowercase , { "input_ids": inputs["input_ids"].cpu().numpy(), "attention_mask": inputs["attention_mask"].cpu().numpy(), "num_beams": np.array(lowercase ), "max_length": np.array(lowercase ), "decoder_start_token_id": np.array(model.config.decoder_start_token_id ), } , ) np.testing.assert_allclose(summary_ids.cpu().numpy() , ort_out[0] , rtol=1E-3 , atol=1E-3 ) logger.info("Model outputs from torch and ONNX Runtime are similar." ) logger.info("Success." ) def _lowerCamelCase ( ) -> Any: _a = parse_args() _a = 5 _a = 4 # Make one log on every process with the configuration for debugging. logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO , ) logger.setLevel(logging.INFO ) transformers.utils.logging.set_verbosity_error() _a = torch.device(args.device ) _a , _a = load_model_tokenizer(args.model_name_or_path , lowercase ) if model.config.decoder_start_token_id is None: raise ValueError("Make sure that `config.decoder_start_token_id` is correctly defined" ) model.to(lowercase ) if args.max_length: _a = args.max_length if args.num_beams: _a = args.num_beams if args.output_file_path: _a = args.output_file_path else: _a = "BART.onnx" logger.info("Exporting model to ONNX" ) export_and_validate_model(lowercase , lowercase , lowercase , lowercase , lowercase ) if __name__ == "__main__": main()
346
0
"""simple docstring""" import unittest from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryDirectory from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline from transformers.convert_graph_to_onnx import ( convert, ensure_valid_input, generate_identified_filename, infer_shapes, quantize, ) from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow class UpperCamelCase__: def snake_case__ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ) -> int: return None class UpperCamelCase__: def snake_case__ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ) -> Tuple: return None class UpperCamelCase__( unittest.TestCase ): lowerCAmelCase__ : Tuple = [ # (model_name, model_kwargs) ('bert-base-cased', {}), ('gpt2', {'use_cache': False}), # We don't support exporting GPT2 past keys anymore ] @require_tf @slow def snake_case__ ( self ) -> int: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__UpperCAmelCase ,'tf' ,12 ,**__UpperCAmelCase ) @require_torch @slow def snake_case__ ( self ) -> int: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: self._test_export(__UpperCAmelCase ,'pt' ,12 ,**__UpperCAmelCase ) @require_torch @slow def snake_case__ ( self ) -> Optional[Any]: from transformers import BertModel A__ = ['[UNK]', '[SEP]', '[CLS]', '[PAD]', '[MASK]', 'some', 'other', 'words'] with NamedTemporaryFile(mode='w+t' ) as vocab_file: vocab_file.write('\n'.join(__UpperCAmelCase ) ) vocab_file.flush() A__ = BertTokenizerFast(vocab_file.name ) with TemporaryDirectory() as bert_save_dir: A__ = BertModel(BertConfig(vocab_size=len(__UpperCAmelCase ) ) ) model.save_pretrained(__UpperCAmelCase ) self._test_export(__UpperCAmelCase ,'pt' ,12 ,__UpperCAmelCase ) @require_tf @slow def snake_case__ ( self ) -> Optional[Any]: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: A__ = self._test_export(__UpperCAmelCase ,'tf' ,12 ,**__UpperCAmelCase ) A__ = quantize(Path(__UpperCAmelCase ) ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__UpperCAmelCase ).stat().st_size: self.fail('Quantized model is bigger than initial ONNX model' ) @require_torch @slow def snake_case__ ( self ) -> str: for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: A__ = self._test_export(__UpperCAmelCase ,'pt' ,12 ,**__UpperCAmelCase ) A__ = quantize(__UpperCAmelCase ) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(__UpperCAmelCase ).stat().st_size: self.fail('Quantized model is bigger than initial ONNX model' ) def snake_case__ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase=None ,**__UpperCAmelCase ) -> Union[str, Any]: try: # Compute path with TemporaryDirectory() as tempdir: A__ = Path(__UpperCAmelCase ).joinpath('model.onnx' ) # Remove folder if exists if path.parent.exists(): path.parent.rmdir() # Export convert(__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,**__UpperCAmelCase ) return path except Exception as e: self.fail(__UpperCAmelCase ) @require_torch @require_tokenizers @slow def snake_case__ ( self ) -> Optional[Any]: from transformers import BertModel A__ = BertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random' ) ) A__ = BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random' ) self._test_infer_dynamic_axis(__UpperCAmelCase ,__UpperCAmelCase ,'pt' ) @require_tf @require_tokenizers @slow def snake_case__ ( self ) -> Optional[Any]: from transformers import TFBertModel A__ = TFBertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random' ) ) A__ = BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random' ) self._test_infer_dynamic_axis(__UpperCAmelCase ,__UpperCAmelCase ,'tf' ) def snake_case__ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ) -> str: A__ = FeatureExtractionPipeline(__UpperCAmelCase ,__UpperCAmelCase ) A__ = ['input_ids', 'token_type_ids', 'attention_mask', 'output_0', 'output_1'] A__ , A__ , A__ , A__ = infer_shapes(__UpperCAmelCase ,__UpperCAmelCase ) # Assert all variables are present self.assertEqual(len(__UpperCAmelCase ) ,len(__UpperCAmelCase ) ) self.assertTrue(all(var_name in shapes for var_name in variable_names ) ) self.assertSequenceEqual(variable_names[:3] ,__UpperCAmelCase ) self.assertSequenceEqual(variable_names[3:] ,__UpperCAmelCase ) # Assert inputs are {0: batch, 1: sequence} for var_name in ["input_ids", "token_type_ids", "attention_mask"]: self.assertDictEqual(shapes[var_name] ,{0: 'batch', 1: 'sequence'} ) # Assert outputs are {0: batch, 1: sequence} and {0: batch} self.assertDictEqual(shapes['output_0'] ,{0: 'batch', 1: 'sequence'} ) self.assertDictEqual(shapes['output_1'] ,{0: 'batch'} ) def snake_case__ ( self ) -> Union[str, Any]: A__ = ['input_ids', 'attention_mask', 'token_type_ids'] A__ = {'input_ids': [1, 2, 3, 4], 'attention_mask': [0, 0, 0, 0], 'token_type_ids': [1, 1, 1, 1]} A__ , A__ = ensure_valid_input(FuncContiguousArgs() ,__UpperCAmelCase ,__UpperCAmelCase ) # Should have exactly the same number of args (all are valid) self.assertEqual(len(__UpperCAmelCase ) ,3 ) # Should have exactly the same input names self.assertEqual(set(__UpperCAmelCase ) ,set(__UpperCAmelCase ) ) # Parameter should be reordered according to their respective place in the function: # (input_ids, token_type_ids, attention_mask) self.assertEqual(__UpperCAmelCase ,(tokens['input_ids'], tokens['token_type_ids'], tokens['attention_mask']) ) # Generated args are interleaved with another args (for instance parameter "past" in GPT2) A__ , A__ = ensure_valid_input(FuncNonContiguousArgs() ,__UpperCAmelCase ,__UpperCAmelCase ) # Should have exactly the one arg (all before the one not provided "some_other_args") self.assertEqual(len(__UpperCAmelCase ) ,1 ) self.assertEqual(len(__UpperCAmelCase ) ,1 ) # Should have only "input_ids" self.assertEqual(inputs_args[0] ,tokens['input_ids'] ) self.assertEqual(ordered_input_names[0] ,'input_ids' ) def snake_case__ ( self ) -> Union[str, Any]: A__ = generate_identified_filename(Path('/home/something/my_fake_model.onnx' ) ,'-test' ) self.assertEqual('/home/something/my_fake_model-test.onnx' ,generated.as_posix() )
221
"""simple docstring""" from io import BytesIO from typing import List, Union import requests from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_decord_available(): import numpy as np from decord import VideoReader if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING __lowerCamelCase = logging.get_logger(__name__) @add_end_docstrings(__A ) class UpperCamelCase__( __A ): def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Tuple: super().__init__(*__UpperCAmelCase ,**__UpperCAmelCase ) requires_backends(self ,'decord' ) self.check_model_type(__UpperCAmelCase ) def snake_case__ ( self ,__UpperCAmelCase=None ,__UpperCAmelCase=None ,__UpperCAmelCase=None ) -> int: A__ = {} if frame_sampling_rate is not None: A__ = frame_sampling_rate if num_frames is not None: A__ = num_frames A__ = {} if top_k is not None: A__ = top_k return preprocess_params, {}, postprocess_params def __call__( self ,__UpperCAmelCase ,**__UpperCAmelCase ) -> Dict: return super().__call__(__UpperCAmelCase ,**__UpperCAmelCase ) def snake_case__ ( self ,__UpperCAmelCase ,__UpperCAmelCase=None ,__UpperCAmelCase=1 ) -> Union[str, Any]: if num_frames is None: A__ = self.model.config.num_frames if video.startswith('http://' ) or video.startswith('https://' ): A__ = BytesIO(requests.get(__UpperCAmelCase ).content ) A__ = VideoReader(__UpperCAmelCase ) videoreader.seek(0 ) A__ = 0 A__ = num_frames * frame_sampling_rate - 1 A__ = np.linspace(__UpperCAmelCase ,__UpperCAmelCase ,num=__UpperCAmelCase ,dtype=np.intaa ) A__ = videoreader.get_batch(__UpperCAmelCase ).asnumpy() A__ = list(__UpperCAmelCase ) A__ = self.image_processor(__UpperCAmelCase ,return_tensors=self.framework ) return model_inputs def snake_case__ ( self ,__UpperCAmelCase ) -> Dict: A__ = self.model(**__UpperCAmelCase ) return model_outputs def snake_case__ ( self ,__UpperCAmelCase ,__UpperCAmelCase=5 ) -> Union[str, Any]: if top_k > self.model.config.num_labels: A__ = self.model.config.num_labels if self.framework == "pt": A__ = model_outputs.logits.softmax(-1 )[0] A__ , A__ = probs.topk(__UpperCAmelCase ) else: raise ValueError(f'''Unsupported framework: {self.framework}''' ) A__ = scores.tolist() A__ = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(__UpperCAmelCase ,__UpperCAmelCase )]
221
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ = logging.get_logger(__name__) UpperCamelCase__ = { '''facebook/vit-mae-base''': '''https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json''', # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae } class lowerCamelCase_ ( __a ): lowerCAmelCase__ = 'vit_mae' def __init__( self : str , _A : Dict=768 , _A : List[str]=12 , _A : Optional[int]=12 , _A : Optional[int]=3_072 , _A : Optional[Any]="gelu" , _A : Tuple=0.0 , _A : Tuple=0.0 , _A : Optional[Any]=0.0_2 , _A : Optional[Any]=1e-12 , _A : Union[str, Any]=224 , _A : str=16 , _A : Dict=3 , _A : List[Any]=True , _A : Optional[int]=16 , _A : Any=512 , _A : str=8 , _A : int=2_048 , _A : Optional[Any]=0.7_5 , _A : int=False , **_A : int , ): '''simple docstring''' super().__init__(**_A ) UpperCAmelCase__ : Tuple = hidden_size UpperCAmelCase__ : Tuple = num_hidden_layers UpperCAmelCase__ : List[str] = num_attention_heads UpperCAmelCase__ : Union[str, Any] = intermediate_size UpperCAmelCase__ : str = hidden_act UpperCAmelCase__ : Union[str, Any] = hidden_dropout_prob UpperCAmelCase__ : int = attention_probs_dropout_prob UpperCAmelCase__ : int = initializer_range UpperCAmelCase__ : int = layer_norm_eps UpperCAmelCase__ : str = image_size UpperCAmelCase__ : str = patch_size UpperCAmelCase__ : List[str] = num_channels UpperCAmelCase__ : Union[str, Any] = qkv_bias UpperCAmelCase__ : Union[str, Any] = decoder_num_attention_heads UpperCAmelCase__ : int = decoder_hidden_size UpperCAmelCase__ : int = decoder_num_hidden_layers UpperCAmelCase__ : Dict = decoder_intermediate_size UpperCAmelCase__ : int = mask_ratio UpperCAmelCase__ : Tuple = norm_pix_loss
360
'''simple docstring''' import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin UpperCamelCase__ = logging.get_logger(__name__) enable_full_determinism() class lowerCamelCase_ ( __a , __a , unittest.TestCase ): lowerCAmelCase__ = UNetaDModel lowerCAmelCase__ = 'sample' @property def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = 4 UpperCAmelCase__ : str = 3 UpperCAmelCase__ : str = (32, 32) UpperCAmelCase__ : List[Any] = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Tuple = torch.tensor([10] ).to(_A ) return {"sample": noise, "timestep": time_step} @property def lowercase_ ( self : int ): '''simple docstring''' return (3, 32, 32) @property def lowercase_ ( self : Dict ): '''simple docstring''' return (3, 32, 32) def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Tuple = { '''block_out_channels''': (32, 64), '''down_block_types''': ('''DownBlock2D''', '''AttnDownBlock2D'''), '''up_block_types''': ('''AttnUpBlock2D''', '''UpBlock2D'''), '''attention_head_dim''': 3, '''out_channels''': 3, '''in_channels''': 3, '''layers_per_block''': 2, '''sample_size''': 32, } UpperCAmelCase__ : Tuple = self.dummy_input return init_dict, inputs_dict class lowerCamelCase_ ( __a , __a , unittest.TestCase ): lowerCAmelCase__ = UNetaDModel lowerCAmelCase__ = 'sample' @property def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : List[str] = 4 UpperCAmelCase__ : Dict = 4 UpperCAmelCase__ : List[str] = (32, 32) UpperCAmelCase__ : List[str] = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : List[Any] = torch.tensor([10] ).to(_A ) return {"sample": noise, "timestep": time_step} @property def lowercase_ ( self : Tuple ): '''simple docstring''' return (4, 32, 32) @property def lowercase_ ( self : List[str] ): '''simple docstring''' return (4, 32, 32) def lowercase_ ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : List[str] = { '''sample_size''': 32, '''in_channels''': 4, '''out_channels''': 4, '''layers_per_block''': 2, '''block_out_channels''': (32, 64), '''attention_head_dim''': 32, '''down_block_types''': ('''DownBlock2D''', '''DownBlock2D'''), '''up_block_types''': ('''UpBlock2D''', '''UpBlock2D'''), } UpperCAmelCase__ : Optional[Any] = self.dummy_input return init_dict, inputs_dict def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : int = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_A ) self.assertIsNotNone(_A ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_A ) UpperCAmelCase__ : Dict = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != '''cuda''' , '''This test is supposed to run on GPU''' ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : Any = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_A ) model.to(_A ) UpperCAmelCase__ : Dict = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != '''cuda''' , '''This test is supposed to run on GPU''' ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : Optional[int] = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' , output_loading_info=_A ) model_accelerate.to(_A ) model_accelerate.eval() UpperCAmelCase__ : Tuple = torch.randn( 1 , model_accelerate.config.in_channels , model_accelerate.config.sample_size , model_accelerate.config.sample_size , generator=torch.manual_seed(0 ) , ) UpperCAmelCase__ : Union[str, Any] = noise.to(_A ) UpperCAmelCase__ : Optional[Any] = torch.tensor([10] * noise.shape[0] ).to(_A ) UpperCAmelCase__ : Any = model_accelerate(_A , _A )['''sample'''] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() UpperCAmelCase__ , UpperCAmelCase__ : Dict = UNetaDModel.from_pretrained( '''fusing/unet-ldm-dummy-update''' , output_loading_info=_A , low_cpu_mem_usage=_A ) model_normal_load.to(_A ) model_normal_load.eval() UpperCAmelCase__ : Optional[int] = model_normal_load(_A , _A )['''sample'''] assert torch_all_close(_A , _A , rtol=1e-3 ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = UNetaDModel.from_pretrained('''fusing/unet-ldm-dummy-update''' ) model.eval() model.to(_A ) UpperCAmelCase__ : Union[str, Any] = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) UpperCAmelCase__ : str = noise.to(_A ) UpperCAmelCase__ : str = torch.tensor([10] * noise.shape[0] ).to(_A ) with torch.no_grad(): UpperCAmelCase__ : Optional[int] = model(_A , _A ).sample UpperCAmelCase__ : List[Any] = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off UpperCAmelCase__ : Tuple = torch.tensor([-1_3.3_2_5_8, -2_0.1_1_0_0, -1_5.9_8_7_3, -1_7.6_6_1_7, -2_3.0_5_9_6, -1_7.9_4_1_9, -1_3.3_6_7_5, -1_6.1_8_8_9, -1_2.3_8_0_0] ) # fmt: on self.assertTrue(torch_all_close(_A , _A , rtol=1e-3 ) ) class lowerCamelCase_ ( __a , __a , unittest.TestCase ): lowerCAmelCase__ = UNetaDModel lowerCAmelCase__ = 'sample' @property def lowercase_ ( self : Any , _A : str=(32, 32) ): '''simple docstring''' UpperCAmelCase__ : Tuple = 4 UpperCAmelCase__ : List[str] = 3 UpperCAmelCase__ : str = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Dict = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa , device=_A ) return {"sample": noise, "timestep": time_step} @property def lowercase_ ( self : List[str] ): '''simple docstring''' return (3, 32, 32) @property def lowercase_ ( self : List[Any] ): '''simple docstring''' return (3, 32, 32) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : List[str] = { '''block_out_channels''': [32, 64, 64, 64], '''in_channels''': 3, '''layers_per_block''': 1, '''out_channels''': 3, '''time_embedding_type''': '''fourier''', '''norm_eps''': 1e-6, '''mid_block_scale_factor''': math.sqrt(2.0 ), '''norm_num_groups''': None, '''down_block_types''': [ '''SkipDownBlock2D''', '''AttnSkipDownBlock2D''', '''SkipDownBlock2D''', '''SkipDownBlock2D''', ], '''up_block_types''': [ '''SkipUpBlock2D''', '''SkipUpBlock2D''', '''AttnSkipUpBlock2D''', '''SkipUpBlock2D''', ], } UpperCAmelCase__ : Tuple = self.dummy_input return init_dict, inputs_dict @slow def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ , UpperCAmelCase__ : str = UNetaDModel.from_pretrained('''google/ncsnpp-celebahq-256''' , output_loading_info=_A ) self.assertIsNotNone(_A ) self.assertEqual(len(loading_info['''missing_keys'''] ) , 0 ) model.to(_A ) UpperCAmelCase__ : List[str] = self.dummy_input UpperCAmelCase__ : Dict = floats_tensor((4, 3) + (256, 256) ).to(_A ) UpperCAmelCase__ : Optional[Any] = noise UpperCAmelCase__ : Any = model(**_A ) assert image is not None, "Make sure output is not None" @slow def lowercase_ ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : int = UNetaDModel.from_pretrained('''google/ncsnpp-celebahq-256''' ) model.to(_A ) UpperCAmelCase__ : Optional[Any] = 4 UpperCAmelCase__ : List[str] = 3 UpperCAmelCase__ : Dict = (256, 256) UpperCAmelCase__ : Optional[int] = torch.ones((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Union[str, Any] = torch.tensor(batch_size * [1e-4] ).to(_A ) with torch.no_grad(): UpperCAmelCase__ : Optional[int] = model(_A , _A ).sample UpperCAmelCase__ : Any = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off UpperCAmelCase__ : Tuple = torch.tensor([-4_8_4_2.8_6_9_1, -6_4_9_9.6_6_3_1, -3_8_0_0.1_9_5_3, -7_9_7_8.2_6_8_6, -1_0_9_8_0.7_1_2_9, -2_0_0_2_8.8_5_3_5, 8_1_4_8.2_8_2_2, 2_3_4_2.2_9_0_5, 5_6_7.7_6_0_8] ) # fmt: on self.assertTrue(torch_all_close(_A , _A , rtol=1e-2 ) ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Dict = UNetaDModel.from_pretrained('''fusing/ncsnpp-ffhq-ve-dummy-update''' ) model.to(_A ) UpperCAmelCase__ : str = 4 UpperCAmelCase__ : Any = 3 UpperCAmelCase__ : int = (32, 32) UpperCAmelCase__ : Optional[Any] = torch.ones((batch_size, num_channels) + sizes ).to(_A ) UpperCAmelCase__ : Optional[Any] = torch.tensor(batch_size * [1e-4] ).to(_A ) with torch.no_grad(): UpperCAmelCase__ : int = model(_A , _A ).sample UpperCAmelCase__ : Dict = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off UpperCAmelCase__ : Any = torch.tensor([-0.0_3_2_5, -0.0_9_0_0, -0.0_8_6_9, -0.0_3_3_2, -0.0_7_2_5, -0.0_2_7_0, -0.0_1_0_1, 0.0_2_2_7, 0.0_2_5_6] ) # fmt: on self.assertTrue(torch_all_close(_A , _A , rtol=1e-2 ) ) def lowercase_ ( self : Tuple ): '''simple docstring''' pass
299
0
a =256 # Modulus to hash a string a =1000003 def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> List[str]: __lowerCamelCase : str = len(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Optional[Any] = len(SCREAMING_SNAKE_CASE__ ) if p_len > t_len: return False __lowerCamelCase : Union[str, Any] = 0 __lowerCamelCase : Union[str, Any] = 0 __lowerCamelCase : Union[str, Any] = 1 # Calculating the hash of pattern and substring of text for i in range(SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : str = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus __lowerCamelCase : str = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue __lowerCamelCase : Any = (modulus_power * alphabet_size) % modulus for i in range(0 , t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash __lowerCamelCase : int = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def SCREAMING_SNAKE_CASE__ ( ) -> List[str]: __lowerCamelCase : Union[str, Any] = 'abc1abc12' __lowerCamelCase : int = 'alskfjaldsabc1abc1abc12k23adsfabcabc' __lowerCamelCase : Tuple = 'alskfjaldsk23adsfabcabc' assert rabin_karp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and not rabin_karp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Test 2) __lowerCamelCase : Any = 'ABABX' __lowerCamelCase : List[str] = 'ABABZABABYABABX' assert rabin_karp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Test 3) __lowerCamelCase : Tuple = 'AAAB' __lowerCamelCase : Dict = 'ABAAAAAB' assert rabin_karp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Test 4) __lowerCamelCase : Optional[int] = 'abcdabcy' __lowerCamelCase : Union[str, Any] = 'abcxabcdabxabcdabcdabcy' assert rabin_karp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Test 5) __lowerCamelCase : str = 'Lü' __lowerCamelCase : Dict = 'Lüsai' assert rabin_karp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : List[Any] = 'Lue' assert not rabin_karp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) print('Success.' ) if __name__ == "__main__": test_rabin_karp()
73
from ..utils import DummyObject, requires_backends class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ["note_seq"] def __init__( self : Optional[int] , *_UpperCamelCase : str , **_UpperCamelCase : Optional[int] ) ->Any: requires_backends(self , ['''note_seq'''] ) @classmethod def snake_case__( cls : int , *_UpperCamelCase : Any , **_UpperCamelCase : List[Any] ) ->int: requires_backends(cls , ['''note_seq'''] ) @classmethod def snake_case__( cls : Dict , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Union[str, Any] ) ->List[str]: requires_backends(cls , ['''note_seq'''] )
8
0
'''simple docstring''' def _A (lowerCAmelCase__ :int ) -> None: '''simple docstring''' _a = generate_pascal_triangle(lowerCAmelCase__ ) for row_idx in range(lowerCAmelCase__ ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=' ' ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=' ' ) else: print(triangle[row_idx][col_idx] , end='' ) print() def _A (lowerCAmelCase__ :int ) -> list[list[int]]: '''simple docstring''' if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise TypeError('The input value of \'num_rows\' should be \'int\'' ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( 'The input value of \'num_rows\' should be greater than or equal to 0' ) _a = [] for current_row_idx in range(lowerCAmelCase__ ): _a = populate_current_row(lowerCAmelCase__ , lowerCAmelCase__ ) triangle.append(lowerCAmelCase__ ) return triangle def _A (lowerCAmelCase__ :list[list[int]] , lowerCAmelCase__ :int ) -> list[int]: '''simple docstring''' _a = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 _a , _a = 1, 1 for current_col_idx in range(1 , lowerCAmelCase__ ): calculate_current_element( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) return current_row def _A (lowerCAmelCase__ :list[list[int]] , lowerCAmelCase__ :list[int] , lowerCAmelCase__ :int , lowerCAmelCase__ :int , ) -> None: '''simple docstring''' _a = triangle[current_row_idx - 1][current_col_idx - 1] _a = triangle[current_row_idx - 1][current_col_idx] _a = above_to_left_elt + above_to_right_elt def _A (lowerCAmelCase__ :int ) -> list[list[int]]: '''simple docstring''' if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise TypeError('The input value of \'num_rows\' should be \'int\'' ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( 'The input value of \'num_rows\' should be greater than or equal to 0' ) _a = [[1]] for row_index in range(1 , lowerCAmelCase__ ): _a = [0] + result[-1] + [0] _a = row_index + 1 # Calculate the number of distinct elements in a row _a = sum(divmod(lowerCAmelCase__ , 2 ) ) _a = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] _a = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() _a = row_first_half + row_second_half result.append(lowerCAmelCase__ ) return result def _A () -> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(lowerCAmelCase__ :Callable , lowerCAmelCase__ :int ) -> None: _a = f'{func.__name__}({value})' _a = timeit(f'__main__.{call}' , setup='import __main__' ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(f'{call:38} -- {timing:.4f} seconds' ) for value in range(15 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(lowerCAmelCase__ , lowerCAmelCase__ ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
371
'''simple docstring''' from __future__ import annotations from sys import maxsize from typing import Generic, TypeVar a_ : Optional[Any] = TypeVar("T") def _A (lowerCAmelCase__ :int ) -> int: '''simple docstring''' return (position - 1) // 2 def _A (lowerCAmelCase__ :int ) -> int: '''simple docstring''' return (2 * position) + 1 def _A (lowerCAmelCase__ :int ) -> int: '''simple docstring''' return (2 * position) + 2 class a ( Generic[T] ): def __init__( self ) -> None: _a = [] _a = {} _a = 0 def __len__( self ) -> int: return self.elements def __repr__( self ) -> str: return str(self.heap ) def __UpperCAmelCase ( self ) -> bool: # Check if the priority queue is empty return self.elements == 0 def __UpperCAmelCase ( self , __magic_name__ , __magic_name__ ) -> None: # Add an element with given priority to the queue self.heap.append((elem, weight) ) _a = self.elements self.elements += 1 self._bubble_up(__magic_name__ ) def __UpperCAmelCase ( self ) -> T: # Remove and return the element with lowest weight (highest priority) if self.elements > 1: self._swap_nodes(0 , self.elements - 1 ) _a , _a = self.heap.pop() del self.position_map[elem] self.elements -= 1 if self.elements > 0: _a , _a = self.heap[0] self._bubble_down(__magic_name__ ) return elem def __UpperCAmelCase ( self , __magic_name__ , __magic_name__ ) -> None: # Update the weight of the given key _a = self.position_map[elem] _a = (elem, weight) if position > 0: _a = get_parent_position(__magic_name__ ) _a , _a = self.heap[parent_position] if parent_weight > weight: self._bubble_up(__magic_name__ ) else: self._bubble_down(__magic_name__ ) else: self._bubble_down(__magic_name__ ) def __UpperCAmelCase ( self , __magic_name__ ) -> None: # Place a node at the proper position (upward movement) [to be used internally # only] _a = self.position_map[elem] if curr_pos == 0: return None _a = get_parent_position(__magic_name__ ) _a , _a = self.heap[curr_pos] _a , _a = self.heap[parent_position] if parent_weight > weight: self._swap_nodes(__magic_name__ , __magic_name__ ) return self._bubble_up(__magic_name__ ) return None def __UpperCAmelCase ( self , __magic_name__ ) -> None: # Place a node at the proper position (downward movement) [to be used # internally only] _a = self.position_map[elem] _a , _a = self.heap[curr_pos] _a = get_child_left_position(__magic_name__ ) _a = get_child_right_position(__magic_name__ ) if child_left_position < self.elements and child_right_position < self.elements: _a , _a = self.heap[child_left_position] _a , _a = self.heap[child_right_position] if child_right_weight < child_left_weight and child_right_weight < weight: self._swap_nodes(__magic_name__ , __magic_name__ ) return self._bubble_down(__magic_name__ ) if child_left_position < self.elements: _a , _a = self.heap[child_left_position] if child_left_weight < weight: self._swap_nodes(__magic_name__ , __magic_name__ ) return self._bubble_down(__magic_name__ ) else: return None if child_right_position < self.elements: _a , _a = self.heap[child_right_position] if child_right_weight < weight: self._swap_nodes(__magic_name__ , __magic_name__ ) return self._bubble_down(__magic_name__ ) return None def __UpperCAmelCase ( self , __magic_name__ , __magic_name__ ) -> None: # Swap the nodes at the given positions _a = self.heap[nodea_pos][0] _a = self.heap[nodea_pos][0] _a , _a = ( self.heap[nodea_pos], self.heap[nodea_pos], ) _a = nodea_pos _a = nodea_pos class a ( Generic[T] ): def __init__( self ) -> None: _a = {} _a = 0 def __repr__( self ) -> str: return str(self.connections ) def __len__( self ) -> int: return self.nodes def __UpperCAmelCase ( self , __magic_name__ ) -> None: # Add a node in the graph if it is not in the graph if node not in self.connections: _a = {} self.nodes += 1 def __UpperCAmelCase ( self , __magic_name__ , __magic_name__ , __magic_name__ ) -> None: # Add an edge between 2 nodes in the graph self.add_node(__magic_name__ ) self.add_node(__magic_name__ ) _a = weight _a = weight def _A (lowerCAmelCase__ :GraphUndirectedWeighted[T] , ) -> tuple[dict[T, int], dict[T, T | None]]: '''simple docstring''' _a = {node: maxsize for node in graph.connections} _a = {node: None for node in graph.connections} _a = MinPriorityQueue() for node, weight in dist.items(): priority_queue.push(lowerCAmelCase__ , lowerCAmelCase__ ) if priority_queue.is_empty(): return dist, parent # initialization _a = priority_queue.extract_min() _a = 0 for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: _a = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(lowerCAmelCase__ , dist[neighbour] ) _a = node # running prim's algorithm while not priority_queue.is_empty(): _a = priority_queue.extract_min() for neighbour in graph.connections[node]: if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: _a = dist[node] + graph.connections[node][neighbour] priority_queue.update_key(lowerCAmelCase__ , dist[neighbour] ) _a = node return dist, parent
104
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __a (UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase): '''simple docstring''' _SCREAMING_SNAKE_CASE :Optional[int] = StableDiffusionInstructPixaPixPipeline _SCREAMING_SNAKE_CASE :int = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""} _SCREAMING_SNAKE_CASE :Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS _SCREAMING_SNAKE_CASE :int = IMAGE_TO_IMAGE_IMAGE_PARAMS _SCREAMING_SNAKE_CASE :List[Any] = IMAGE_TO_IMAGE_IMAGE_PARAMS def _a ( self ) -> Union[str, Any]: """simple docstring""" torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) SCREAMING_SNAKE_CASE__ : Optional[Any] = PNDMScheduler(skip_prk_steps=_a ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : Any = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) torch.manual_seed(0 ) SCREAMING_SNAKE_CASE__ : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) SCREAMING_SNAKE_CASE__ : str = CLIPTextModel(_a ) SCREAMING_SNAKE_CASE__ : List[str] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) SCREAMING_SNAKE_CASE__ : str = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def _a ( self , _a , _a=0 ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) SCREAMING_SNAKE_CASE__ : Tuple = image.cpu().permute(0 , 2 , 3 , 1 )[0] SCREAMING_SNAKE_CASE__ : Dict = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ) if str(_a ).startswith("""mps""" ): SCREAMING_SNAKE_CASE__ : List[Any] = torch.manual_seed(_a ) else: SCREAMING_SNAKE_CASE__ : str = torch.Generator(device=_a ).manual_seed(_a ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """image_guidance_scale""": 1, """output_type""": """numpy""", } return inputs def _a ( self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE__ : Optional[int] = self.get_dummy_components() SCREAMING_SNAKE_CASE__ : List[str] = StableDiffusionInstructPixaPixPipeline(**_a ) SCREAMING_SNAKE_CASE__ : Optional[int] = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) SCREAMING_SNAKE_CASE__ : List[Any] = self.get_dummy_inputs(_a ) SCREAMING_SNAKE_CASE__ : str = sd_pipe(**_a ).images SCREAMING_SNAKE_CASE__ : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE__ : Optional[int] = np.array([0.7_526, 0.3_750, 0.4_547, 0.6_117, 0.5_866, 0.5_016, 0.4_327, 0.5_642, 0.4_815] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _a ( self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE__ : str = self.get_dummy_components() SCREAMING_SNAKE_CASE__ : int = StableDiffusionInstructPixaPixPipeline(**_a ) SCREAMING_SNAKE_CASE__ : List[Any] = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) SCREAMING_SNAKE_CASE__ : Dict = self.get_dummy_inputs(_a ) SCREAMING_SNAKE_CASE__ : int = """french fries""" SCREAMING_SNAKE_CASE__ : List[str] = sd_pipe(**_a , negative_prompt=_a ) SCREAMING_SNAKE_CASE__ : Optional[int] = output.images SCREAMING_SNAKE_CASE__ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE__ : List[str] = np.array([0.7_511, 0.3_642, 0.4_553, 0.6_236, 0.5_797, 0.5_013, 0.4_343, 0.5_611, 0.4_831] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _a ( self ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = """cpu""" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE__ : Tuple = self.get_dummy_components() SCREAMING_SNAKE_CASE__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**_a ) SCREAMING_SNAKE_CASE__ : List[str] = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) SCREAMING_SNAKE_CASE__ : int = self.get_dummy_inputs(_a ) SCREAMING_SNAKE_CASE__ : List[str] = [inputs["""prompt"""]] * 2 SCREAMING_SNAKE_CASE__ : List[str] = np.array(inputs["""image"""] ).astype(np.floataa ) / 255.0 SCREAMING_SNAKE_CASE__ : Optional[Any] = torch.from_numpy(_a ).unsqueeze(0 ).to(_a ) SCREAMING_SNAKE_CASE__ : Any = image / 2 + 0.5 SCREAMING_SNAKE_CASE__ : Any = image.permute(0 , 3 , 1 , 2 ) SCREAMING_SNAKE_CASE__ : List[str] = image.repeat(2 , 1 , 1 , 1 ) SCREAMING_SNAKE_CASE__ : int = sd_pipe(**_a ).images SCREAMING_SNAKE_CASE__ : List[Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.array([0.5_812, 0.5_748, 0.5_222, 0.5_908, 0.5_695, 0.7_174, 0.6_804, 0.5_523, 0.5_579] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _a ( self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = """cpu""" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE__ : str = self.get_dummy_components() SCREAMING_SNAKE_CASE__ : int = EulerAncestralDiscreteScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule="""scaled_linear""" ) SCREAMING_SNAKE_CASE__ : int = StableDiffusionInstructPixaPixPipeline(**_a ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) SCREAMING_SNAKE_CASE__ : Tuple = self.get_dummy_inputs(_a ) SCREAMING_SNAKE_CASE__ : List[str] = sd_pipe(**_a ).images SCREAMING_SNAKE_CASE__ : List[Any] = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE__ : Optional[int] = [round(_a , 4 ) for x in image_slice.flatten().tolist()] print(""",""".join([str(_a ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE__ : Tuple = np.array([0.7_417, 0.3_842, 0.4_732, 0.5_776, 0.5_891, 0.5_139, 0.4_052, 0.5_673, 0.4_986] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def _a ( self ) -> Dict: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def _a ( self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[Any] = self.get_dummy_components() SCREAMING_SNAKE_CASE__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline(**_a ) SCREAMING_SNAKE_CASE__ : Optional[int] = VaeImageProcessor(do_resize=_a , do_normalize=_a ) SCREAMING_SNAKE_CASE__ : Tuple = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) SCREAMING_SNAKE_CASE__ : str = pipe(**self.get_dummy_inputs_by_type(_a , input_image_type="""pt""" ) )[0] SCREAMING_SNAKE_CASE__ : Tuple = components["""vae"""] SCREAMING_SNAKE_CASE__ : Dict = self.get_dummy_inputs_by_type(_a , input_image_type="""pt""" ) for image_param in self.image_latents_params: if image_param in inputs.keys(): SCREAMING_SNAKE_CASE__ : Any = vae.encode(inputs[image_param] ).latent_dist.mode() SCREAMING_SNAKE_CASE__ : Any = pipe(**_a )[0] SCREAMING_SNAKE_CASE__ : Optional[Any] = np.abs(out - out_latents_inputs ).max() self.assertLess(_a , 1E-4 , """passing latents as image input generate different result from passing image""" ) @slow @require_torch_gpu class __a (unittest.TestCase): '''simple docstring''' def _a ( self ) -> Union[str, Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _a ( self , _a=0 ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = torch.manual_seed(_a ) SCREAMING_SNAKE_CASE__ : str = load_image( """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg""" ) SCREAMING_SNAKE_CASE__ : Dict = { """prompt""": """turn him into a cyborg""", """image""": image, """generator""": generator, """num_inference_steps""": 3, """guidance_scale""": 7.5, """image_guidance_scale""": 1.0, """output_type""": """numpy""", } return inputs def _a ( self ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" , safety_checker=_a ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE__ : Optional[int] = self.get_inputs() SCREAMING_SNAKE_CASE__ : Tuple = pipe(**_a ).images SCREAMING_SNAKE_CASE__ : List[str] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.array([0.5_902, 0.6_015, 0.6_027, 0.5_983, 0.6_092, 0.6_061, 0.5_765, 0.5_785, 0.5_555] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def _a ( self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" , safety_checker=_a ) SCREAMING_SNAKE_CASE__ : str = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE__ : List[Any] = self.get_inputs() SCREAMING_SNAKE_CASE__ : str = pipe(**_a ).images SCREAMING_SNAKE_CASE__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE__ : str = np.array([0.6_578, 0.6_817, 0.6_972, 0.6_761, 0.6_856, 0.6_916, 0.6_428, 0.6_516, 0.6_301] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def _a ( self ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE__ : int = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" , safety_checker=_a ) SCREAMING_SNAKE_CASE__ : Tuple = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE__ : str = self.get_inputs() SCREAMING_SNAKE_CASE__ : Union[str, Any] = pipe(**_a ).images SCREAMING_SNAKE_CASE__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) SCREAMING_SNAKE_CASE__ : Any = np.array([0.3_828, 0.3_834, 0.3_818, 0.3_792, 0.3_865, 0.3_752, 0.3_792, 0.3_847, 0.3_753] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def _a ( self ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = 0 def callback_fn(_a , _a , _a ) -> None: SCREAMING_SNAKE_CASE__ : List[Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: SCREAMING_SNAKE_CASE__ : Union[str, Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) SCREAMING_SNAKE_CASE__ : str = latents[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.array([-0.2_463, -0.4_644, -0.9_756, 1.5_176, 1.4_414, 0.7_866, 0.9_897, 0.8_521, 0.7_983] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: SCREAMING_SNAKE_CASE__ : Any = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) SCREAMING_SNAKE_CASE__ : List[Any] = latents[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE__ : str = np.array([-0.2_644, -0.4_626, -0.9_653, 1.5_176, 1.4_551, 0.7_686, 0.9_805, 0.8_452, 0.8_115] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 SCREAMING_SNAKE_CASE__ : Optional[Any] = False SCREAMING_SNAKE_CASE__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" , safety_checker=_a , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE__ : Tuple = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE__ : Optional[int] = self.get_inputs() pipe(**_a , callback=_a , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def _a ( self ) -> List[str]: """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() SCREAMING_SNAKE_CASE__ : str = StableDiffusionInstructPixaPixPipeline.from_pretrained( """timbrooks/instruct-pix2pix""" , safety_checker=_a , torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE__ : str = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() SCREAMING_SNAKE_CASE__ : List[str] = self.get_inputs() SCREAMING_SNAKE_CASE__ : Any = pipe(**_a ) SCREAMING_SNAKE_CASE__ : Any = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def _a ( self ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE__ : Union[str, Any] = inputs["""image"""].resize((504, 504) ) SCREAMING_SNAKE_CASE__ : str = """timbrooks/instruct-pix2pix""" SCREAMING_SNAKE_CASE__ : Dict = StableDiffusionInstructPixaPixPipeline.from_pretrained( _a , safety_checker=_a , ) pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE__ : Union[str, Any] = pipe(**_a ) SCREAMING_SNAKE_CASE__ : Optional[Any] = output.images[0] SCREAMING_SNAKE_CASE__ : Any = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.array([0.2_726, 0.2_529, 0.2_664, 0.2_655, 0.2_641, 0.2_642, 0.2_591, 0.2_649, 0.2_590] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
132
"""simple docstring""" import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin a :Optional[int] = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.plbart.modeling_plbart import shift_tokens_right a :Tuple = 50_003 a :Optional[int] = 50_002 @require_sentencepiece @require_tokenizers class __a (UpperCamelCase_ , unittest.TestCase): '''simple docstring''' _SCREAMING_SNAKE_CASE :Dict = PLBartTokenizer _SCREAMING_SNAKE_CASE :List[str] = None _SCREAMING_SNAKE_CASE :Any = False def _a ( self ) -> Dict: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE__ : Any = PLBartTokenizer(_a , language_codes="""base""" , keep_accents=_a ) tokenizer.save_pretrained(self.tmpdirname ) def _a ( self ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : str = PLBartTokenizer(_a , language_codes="""base""" , keep_accents=_a ) SCREAMING_SNAKE_CASE__ : Dict = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(_a , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE__ : List[str] = 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""", """é""", """.""", ] , ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE__ : Any = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) SCREAMING_SNAKE_CASE__ : Tuple = tokenizer.vocab_size SCREAMING_SNAKE_CASE__ : Dict = [tokenizer.convert_ids_to_tokens(_a ) for x in range(end - 4 , _a )] self.assertListEqual(_a , ["""__java__""", """__python__""", """__en_XX__""", """<mask>"""] ) SCREAMING_SNAKE_CASE__ : List[str] = """java.lang.Exception, python.lang.Exception, javascript, php, ruby, go""" SCREAMING_SNAKE_CASE__ : Any = tokenizer(_a ).input_ids self.assertEqual( tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) , _a , ) def _a ( self ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = PLBartTokenizer(_a , language_codes="""multi""" , keep_accents=_a ) SCREAMING_SNAKE_CASE__ : List[Any] = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(_a , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE__ : str = 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""", """é""", """.""", ] , ) SCREAMING_SNAKE_CASE__ : List[Any] = tokenizer.convert_tokens_to_ids(_a ) self.assertListEqual( _a , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE__ : str = tokenizer.convert_ids_to_tokens(_a ) self.assertListEqual( _a , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) SCREAMING_SNAKE_CASE__ : List[Any] = tokenizer.vocab_size SCREAMING_SNAKE_CASE__ : Optional[int] = [tokenizer.convert_ids_to_tokens(_a ) for x in range(end - 7 , _a )] self.assertListEqual( _a , ["""__java__""", """__python__""", """__en_XX__""", """__javascript__""", """__php__""", """__ruby__""", """__go__"""] ) SCREAMING_SNAKE_CASE__ : Tuple = """java.lang.Exception, python.lang.Exception, javascript, php, ruby, go""" SCREAMING_SNAKE_CASE__ : Optional[int] = tokenizer(_a ).input_ids self.assertEqual( tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) , _a , ) @require_torch @require_sentencepiece @require_tokenizers class __a (unittest.TestCase): '''simple docstring''' _SCREAMING_SNAKE_CASE :Optional[int] = """uclanlp/plbart-python-en_XX""" _SCREAMING_SNAKE_CASE :List[Any] = [ """def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])""", """def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])""", ] _SCREAMING_SNAKE_CASE :Optional[Any] = [ """Returns the maximum value of a b c.""", """Sums the values of a b c.""", ] _SCREAMING_SNAKE_CASE :str = [ 1_34, 54_52, 3_34_60, 3_34_41, 3_34_63, 3_34_65, 3_34_63, 3_34_49, 9_88, 20, 3_34_56, 19, 3_34_56, 7_71, 39, 42_58, 8_89, 33_18, 3_34_41, 3_34_63, 3_34_65, 3_34_63, 3_34_49, 24_71, 2, PYTHON_CODE, ] @classmethod def _a ( cls ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : PLBartTokenizer = PLBartTokenizer.from_pretrained( cls.checkpoint_name , language_codes="""base""" , src_lang="""python""" , tgt_lang="""en_XX""" ) SCREAMING_SNAKE_CASE__ : Any = 1 return cls def _a ( self ) -> Union[str, Any]: """simple docstring""" self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""__java__"""] , 50_001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""__python__"""] , 50_002 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""__en_XX__"""] , 50_003 ) def _a ( self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , _a ) def _a ( self ) -> Dict: """simple docstring""" self.assertIn(_a , self.tokenizer.all_special_ids ) SCREAMING_SNAKE_CASE__ : Tuple = [EN_CODE, 9_037, 33_442, 57, 752, 153, 14, 56, 18, 9, 2] SCREAMING_SNAKE_CASE__ : Any = self.tokenizer.decode(_a , skip_special_tokens=_a ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_a ) self.assertEqual(_a , _a ) self.assertNotIn(self.tokenizer.eos_token , _a ) def _a ( self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = ["""def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])""" * 20] self.assertIsInstance(src_text[0] , _a ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = 10 SCREAMING_SNAKE_CASE__ : Optional[Any] = self.tokenizer(_a , max_length=_a , truncation=_a ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , _a ) self.assertEqual(len(_a ) , _a ) def _a ( self ) -> Optional[int]: """simple docstring""" self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["""<mask>""", """__java__"""] ) , [50_004, 50_001] ) def _a ( self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = tempfile.mkdtemp() SCREAMING_SNAKE_CASE__ : Tuple = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(_a ) SCREAMING_SNAKE_CASE__ : Dict = PLBartTokenizer.from_pretrained(_a ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , _a ) @require_torch def _a ( self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=_a , return_tensors="""pt""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 self.assertEqual(batch.input_ids[1][-2:].tolist() , [2, PYTHON_CODE] ) self.assertEqual(batch.decoder_input_ids[1][0] , _a ) self.assertEqual(batch.decoder_input_ids[1][-1] , 2 ) self.assertEqual(batch.labels[1][-2:].tolist() , [2, EN_CODE] ) @require_torch def _a ( self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ : int = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=_a , truncation=_a , max_length=len(self.expected_src_tokens ) , return_tensors="""pt""" , ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) self.assertIsInstance(_a , _a ) self.assertEqual((2, 26) , batch.input_ids.shape ) self.assertEqual((2, 26) , batch.attention_mask.shape ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , _a ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, PYTHON_CODE] ) def _a ( self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = self.tokenizer(self.src_text , padding=_a , truncation=_a , max_length=3 , return_tensors="""pt""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.tokenizer( text_target=self.tgt_text , padding=_a , truncation=_a , max_length=10 , return_tensors="""pt""" ) SCREAMING_SNAKE_CASE__ : List[Any] = targets["""input_ids"""] SCREAMING_SNAKE_CASE__ : Dict = shift_tokens_right(_a , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _a ( self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.tokenizer._build_translation_inputs( """A test""" , return_tensors="""pt""" , src_lang="""en_XX""" , tgt_lang="""java""" ) self.assertEqual( nested_simplify(_a ) , { # A, test, EOS, en_XX """input_ids""": [[150, 242, 2, 50_003]], """attention_mask""": [[1, 1, 1, 1]], # java """forced_bos_token_id""": 50_001, } , )
132
1
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __UpperCAmelCase = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE ( a_ , unittest.TestCase ): """simple docstring""" lowerCamelCase : Optional[Any] =XLMRobertaTokenizer lowerCamelCase : Union[str, Any] =XLMRobertaTokenizerFast lowerCamelCase : int =True lowerCamelCase : List[Any] =True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing __lowerCAmelCase : Optional[Any] = XLMRobertaTokenizer(lowerCAmelCase , keep_accents=lowerCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" __lowerCAmelCase : Optional[Any] = """<pad>""" __lowerCAmelCase : Optional[int] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase ) , lowerCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase ) , lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: """simple docstring""" __lowerCAmelCase : str = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<s>""" ) self.assertEqual(vocab_keys[1] , """<pad>""" ) self.assertEqual(vocab_keys[-1] , """<mask>""" ) self.assertEqual(len(lowerCAmelCase ) , 10_02 ) def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 10_02 ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Dict: """simple docstring""" __lowerCAmelCase : List[Any] = XLMRobertaTokenizer(lowerCAmelCase , keep_accents=lowerCAmelCase ) __lowerCAmelCase : List[str] = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(lowerCAmelCase , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) __lowerCAmelCase : List[str] = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) __lowerCAmelCase : int = tokenizer.convert_tokens_to_ids(lowerCAmelCase ) self.assertListEqual( lowerCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) __lowerCAmelCase : int = tokenizer.convert_ids_to_tokens(lowerCAmelCase ) self.assertListEqual( lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[int]: """simple docstring""" if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return __lowerCAmelCase : Dict = (self.rust_tokenizer_class, """hf-internal-testing/tiny-xlm-roberta""", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase , **lowerCAmelCase ) __lowerCAmelCase : str = self.tokenizer_class.from_pretrained(lowerCAmelCase , **lowerCAmelCase ) __lowerCAmelCase : int = tempfile.mkdtemp() __lowerCAmelCase : List[Any] = tokenizer_r.save_pretrained(lowerCAmelCase ) __lowerCAmelCase : int = tokenizer_p.save_pretrained(lowerCAmelCase ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) __lowerCAmelCase : Tuple = tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f ) self.assertSequenceEqual(lowerCAmelCase , lowerCAmelCase ) # Checks everything loads correctly in the same way __lowerCAmelCase : List[Any] = tokenizer_r.from_pretrained(lowerCAmelCase ) __lowerCAmelCase : str = tokenizer_p.from_pretrained(lowerCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCAmelCase , lowerCAmelCase ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(lowerCAmelCase ) # Save tokenizer rust, legacy_format=True __lowerCAmelCase : List[Any] = tempfile.mkdtemp() __lowerCAmelCase : str = tokenizer_r.save_pretrained(lowerCAmelCase , legacy_format=lowerCAmelCase ) __lowerCAmelCase : int = tokenizer_p.save_pretrained(lowerCAmelCase ) # Checks it save with the same files self.assertSequenceEqual(lowerCAmelCase , lowerCAmelCase ) # Checks everything loads correctly in the same way __lowerCAmelCase : int = tokenizer_r.from_pretrained(lowerCAmelCase ) __lowerCAmelCase : Optional[int] = tokenizer_p.from_pretrained(lowerCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCAmelCase , lowerCAmelCase ) ) shutil.rmtree(lowerCAmelCase ) # Save tokenizer rust, legacy_format=False __lowerCAmelCase : List[str] = tempfile.mkdtemp() __lowerCAmelCase : Any = tokenizer_r.save_pretrained(lowerCAmelCase , legacy_format=lowerCAmelCase ) __lowerCAmelCase : Optional[Any] = tokenizer_p.save_pretrained(lowerCAmelCase ) # Checks it saved the tokenizer.json file self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way __lowerCAmelCase : Tuple = tokenizer_r.from_pretrained(lowerCAmelCase ) __lowerCAmelCase : Any = tokenizer_p.from_pretrained(lowerCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(lowerCAmelCase , lowerCAmelCase ) ) shutil.rmtree(lowerCAmelCase ) @cached_property def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" return XLMRobertaTokenizer.from_pretrained("""xlm-roberta-base""" ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: """simple docstring""" with tempfile.NamedTemporaryFile() as f: shutil.copyfile(lowerCAmelCase , f.name ) __lowerCAmelCase : str = XLMRobertaTokenizer(f.name , keep_accents=lowerCAmelCase ) __lowerCAmelCase : Tuple = pickle.dumps(lowerCAmelCase ) pickle.loads(lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" if not self.test_rust_tokenizer: return __lowerCAmelCase : List[Any] = self.get_tokenizer() __lowerCAmelCase : List[str] = self.get_rust_tokenizer() __lowerCAmelCase : Optional[Any] = """I was born in 92000, and this is falsé.""" __lowerCAmelCase : Any = tokenizer.tokenize(lowerCAmelCase ) __lowerCAmelCase : List[Any] = rust_tokenizer.tokenize(lowerCAmelCase ) self.assertListEqual(lowerCAmelCase , lowerCAmelCase ) __lowerCAmelCase : Any = tokenizer.encode(lowerCAmelCase , add_special_tokens=lowerCAmelCase ) __lowerCAmelCase : Tuple = rust_tokenizer.encode(lowerCAmelCase , add_special_tokens=lowerCAmelCase ) self.assertListEqual(lowerCAmelCase , lowerCAmelCase ) __lowerCAmelCase : Optional[Any] = self.get_rust_tokenizer() __lowerCAmelCase : Any = tokenizer.encode(lowerCAmelCase ) __lowerCAmelCase : Optional[int] = rust_tokenizer.encode(lowerCAmelCase ) self.assertListEqual(lowerCAmelCase , lowerCAmelCase ) @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" __lowerCAmelCase : Optional[int] = """Hello World!""" __lowerCAmelCase : int = [0, 3_53_78, 66_61, 38, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(lowerCAmelCase , self.big_tokenizer.encode(lowerCAmelCase ) ) @slow def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : Dict = ( """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""" ) __lowerCAmelCase : int = [ 0, 32_93, 83, 10, 45_52, 49_89, 79_86, 6_78, 10, 59_15, 1_11, 17_94_59, 12_48_50, 4, 60_44, 2_37, 12, 6, 5, 6, 4, 67_80, 7_05, 15, 13_88, 44, 3_78, 1_01_14, 7_11, 1_52, 20, 6, 5, 2_23_76, 6_42, 12_21, 1_51_90, 3_41_53, 4_50, 56_08, 9_59, 11_19, 5_77_02, 1_36, 1_86, 47, 10_98, 2_93_67, 47, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 60_44, 2_37, 62_84, 5_09_01, 5_28, 31, 90, 34, 9_27, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(lowerCAmelCase , self.big_tokenizer.encode(lowerCAmelCase ) ) @slow def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Any: """simple docstring""" __lowerCAmelCase : Any = {"""input_ids""": [[0, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [0, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCAmelCase , model_name="""xlm-roberta-base""" , revision="""d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3""" , )
369
from pathlib import Path import fire def snake_case_ (__A : str , __A : str , __A : int ) -> Any: __lowerCAmelCase : Tuple = Path(__A ) __lowerCAmelCase : Tuple = Path(__A ) dest_dir.mkdir(exist_ok=__A ) for path in src_dir.iterdir(): __lowerCAmelCase : str = [x.rstrip() for x in list(path.open().readlines() )][:n] __lowerCAmelCase : Dict = dest_dir.joinpath(path.name ) print(__A ) dest_path.open("""w""" ).write("""\n""".join(__A ) ) if __name__ == "__main__": fire.Fire(minify)
139
0
'''simple docstring''' import argparse import json from collections import OrderedDict import torch from huggingface_hub import cached_download, hf_hub_url from transformers import AutoImageProcessor, CvtConfig, CvtForImageClassification def __lowerCamelCase ( A__ ) -> List[str]: """simple docstring""" UpperCamelCase = [] embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.weight""", F"""stage{idx}.patch_embed.proj.weight""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.projection.bias""", F"""stage{idx}.patch_embed.proj.bias""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.weight""", F"""stage{idx}.patch_embed.norm.weight""", ) ) embed.append( ( F"""cvt.encoder.stages.{idx}.embedding.convolution_embeddings.normalization.bias""", F"""stage{idx}.patch_embed.norm.bias""", ) ) return embed def __lowerCamelCase ( A__ , A__ ) -> Optional[int]: """simple docstring""" UpperCamelCase = [] attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_query.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_q.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_key.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_k.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.convolution.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.conv.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.weight""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.bias""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_mean""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_mean""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.running_var""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.running_var""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.convolution_projection_value.convolution_projection.normalization.num_batches_tracked""", F"""stage{idx}.blocks.{cnt}.attn.conv_proj_v.bn.num_batches_tracked""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_q.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_query.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_q.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_k.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_key.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_k.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj_v.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.attention.projection_value.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj_v.bias""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.weight""", F"""stage{idx}.blocks.{cnt}.attn.proj.weight""", ) ) attention_weights.append( ( F"""cvt.encoder.stages.{idx}.layers.{cnt}.attention.output.dense.bias""", F"""stage{idx}.blocks.{cnt}.attn.proj.bias""", ) ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.weight""", F"""stage{idx}.blocks.{cnt}.mlp.fc1.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.intermediate.dense.bias""", F"""stage{idx}.blocks.{cnt}.mlp.fc1.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.weight""", F"""stage{idx}.blocks.{cnt}.mlp.fc2.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.output.dense.bias""", F"""stage{idx}.blocks.{cnt}.mlp.fc2.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.weight""", F"""stage{idx}.blocks.{cnt}.norm1.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_before.bias""", F"""stage{idx}.blocks.{cnt}.norm1.bias""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.weight""", F"""stage{idx}.blocks.{cnt}.norm2.weight""") ) attention_weights.append( (F"""cvt.encoder.stages.{idx}.layers.{cnt}.layernorm_after.bias""", F"""stage{idx}.blocks.{cnt}.norm2.bias""") ) return attention_weights def __lowerCamelCase ( A__ ) -> Any: """simple docstring""" UpperCamelCase = [] token.append((F"""cvt.encoder.stages.{idx}.cls_token""", 'stage2.cls_token') ) return token def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" UpperCamelCase = [] head.append(('layernorm.weight', 'norm.weight') ) head.append(('layernorm.bias', 'norm.bias') ) head.append(('classifier.weight', 'head.weight') ) head.append(('classifier.bias', 'head.bias') ) return head def __lowerCamelCase ( A__ , A__ , A__ , A__ ) -> Any: """simple docstring""" UpperCamelCase = 'imagenet-1k-id2label.json' UpperCamelCase = 1_000 UpperCamelCase = 'huggingface/label-files' UpperCamelCase = num_labels UpperCamelCase = json.load(open(cached_download(hf_hub_url(A__ , A__ , repo_type='dataset' ) ) , 'r' ) ) UpperCamelCase = {int(A__ ): v for k, v in idalabel.items()} UpperCamelCase = idalabel UpperCamelCase = {v: k for k, v in idalabel.items()} UpperCamelCase = UpperCamelCase = CvtConfig(num_labels=A__ , idalabel=A__ , labelaid=A__ ) # For depth size 13 (13 = 1+2+10) if cvt_model.rsplit('/' , 1 )[-1][4:6] == "13": UpperCamelCase = [1, 2, 10] # For depth size 21 (21 = 1+4+16) elif cvt_model.rsplit('/' , 1 )[-1][4:6] == "21": UpperCamelCase = [1, 4, 16] # For wide cvt (similar to wide-resnet) depth size 24 (w24 = 2 + 2 20) else: UpperCamelCase = [2, 2, 20] UpperCamelCase = [3, 12, 16] UpperCamelCase = [192, 768, 1_024] UpperCamelCase = CvtForImageClassification(A__ ) UpperCamelCase = AutoImageProcessor.from_pretrained('facebook/convnext-base-224-22k-1k' ) UpperCamelCase = image_size UpperCamelCase = torch.load(A__ , map_location=torch.device('cpu' ) ) UpperCamelCase = OrderedDict() UpperCamelCase = [] for idx in range(len(config.depth ) ): if config.cls_token[idx]: UpperCamelCase = list_of_state_dict + cls_token(A__ ) UpperCamelCase = list_of_state_dict + embeddings(A__ ) for cnt in range(config.depth[idx] ): UpperCamelCase = list_of_state_dict + attention(A__ , A__ ) UpperCamelCase = list_of_state_dict + final() for gg in list_of_state_dict: print(A__ ) for i in range(len(A__ ) ): UpperCamelCase = original_weights[list_of_state_dict[i][1]] model.load_state_dict(A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) # Download the weights from zoo: https://1drv.ms/u/s!AhIXJn_J-blW9RzF3rMW7SsLHa8h?e=blQ0Al if __name__ == "__main__": _lowerCamelCase : Tuple = argparse.ArgumentParser() parser.add_argument( "--cvt_model", default="cvt-w24", type=str, help="Name of the cvt model you'd like to convert.", ) parser.add_argument( "--image_size", default=384, type=int, help="Input Image Size", ) parser.add_argument( "--cvt_file_name", default=R"cvtmodels\CvT-w24-384x384-IN-22k.pth", type=str, help="Input Image Size", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) _lowerCamelCase : Any = parser.parse_args() convert_cvt_checkpoint(args.cvt_model, args.image_size, args.cvt_file_name, args.pytorch_dump_folder_path)
28
'''simple docstring''' import unittest from transformers import MraConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : Any=2 , UpperCamelCase__ : Union[str, Any]=8 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Any=True , UpperCamelCase__ : str=True , UpperCamelCase__ : Dict=True , UpperCamelCase__ : List[Any]=9_9 , UpperCamelCase__ : List[Any]=1_6 , UpperCamelCase__ : List[str]=5 , UpperCamelCase__ : Dict=2 , UpperCamelCase__ : Optional[int]=3_6 , UpperCamelCase__ : str="gelu" , UpperCamelCase__ : Dict=0.0 , UpperCamelCase__ : Dict=0.0 , UpperCamelCase__ : Optional[int]=5_1_2 , UpperCamelCase__ : Dict=1_6 , UpperCamelCase__ : List[str]=2 , UpperCamelCase__ : Any=0.0_2 , UpperCamelCase__ : str=3 , UpperCamelCase__ : Tuple=4 , UpperCamelCase__ : Union[str, Any]=None , ): """simple docstring""" UpperCamelCase = parent UpperCamelCase = batch_size UpperCamelCase = seq_length UpperCamelCase = is_training UpperCamelCase = use_input_mask UpperCamelCase = use_token_type_ids UpperCamelCase = use_labels UpperCamelCase = vocab_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = max_position_embeddings UpperCamelCase = type_vocab_size UpperCamelCase = type_sequence_label_size UpperCamelCase = initializer_range UpperCamelCase = num_labels UpperCamelCase = num_choices UpperCamelCase = scope def A ( self : int ): """simple docstring""" UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase = None if self.use_input_mask: UpperCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase = None if self.use_token_type_ids: UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase = None UpperCamelCase = None UpperCamelCase = None if self.use_labels: UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase = 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 MraConfig( 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=UpperCamelCase__ , initializer_range=self.initializer_range , ) def A ( self : Any ): """simple docstring""" UpperCamelCase = self.get_config() UpperCamelCase = 3_0_0 return config def A ( self : Tuple ): """simple docstring""" ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) = self.prepare_config_and_inputs() UpperCamelCase = True UpperCamelCase = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCamelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def A ( self : Tuple , UpperCamelCase__ : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : int , UpperCamelCase__ : List[str] , UpperCamelCase__ : Dict ): """simple docstring""" UpperCamelCase = MraModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() UpperCamelCase = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ ) UpperCamelCase = model(UpperCamelCase__ , token_type_ids=UpperCamelCase__ ) UpperCamelCase = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Dict , UpperCamelCase__ : List[Any] , UpperCamelCase__ : str , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] , ): """simple docstring""" UpperCamelCase = True UpperCamelCase = MraModel(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() UpperCamelCase = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , encoder_hidden_states=UpperCamelCase__ , encoder_attention_mask=UpperCamelCase__ , ) UpperCamelCase = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , encoder_hidden_states=UpperCamelCase__ , ) UpperCamelCase = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self : int , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[Any] , UpperCamelCase__ : List[str] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[str] ): """simple docstring""" UpperCamelCase = MraForMaskedLM(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() UpperCamelCase = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A ( self : Any , UpperCamelCase__ : Any , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : List[str] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any] ): """simple docstring""" UpperCamelCase = MraForQuestionAnswering(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() UpperCamelCase = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , start_positions=UpperCamelCase__ , end_positions=UpperCamelCase__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def A ( self : Optional[int] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple ): """simple docstring""" UpperCamelCase = self.num_labels UpperCamelCase = MraForSequenceClassification(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() UpperCamelCase = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def A ( self : Any , UpperCamelCase__ : Any , UpperCamelCase__ : str , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : int , UpperCamelCase__ : int , UpperCamelCase__ : Optional[Any] ): """simple docstring""" UpperCamelCase = self.num_labels UpperCamelCase = MraForTokenClassification(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() UpperCamelCase = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def A ( self : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : str , UpperCamelCase__ : Dict ): """simple docstring""" UpperCamelCase = self.num_choices UpperCamelCase = MraForMultipleChoice(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) 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( UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , labels=UpperCamelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def A ( self : int ): """simple docstring""" UpperCamelCase = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) = config_and_inputs UpperCamelCase = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE ( _a , unittest.TestCase ): """simple docstring""" _SCREAMING_SNAKE_CASE = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = () def A ( self : str ): """simple docstring""" UpperCamelCase = MraModelTester(self ) UpperCamelCase = ConfigTester(self , config_class=UpperCamelCase__ , hidden_size=3_7 ) def A ( self : str ): """simple docstring""" self.config_tester.run_common_tests() def A ( self : Optional[Any] ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase__ ) def A ( self : str ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase = type self.model_tester.create_and_check_model(*UpperCamelCase__ ) def A ( self : Optional[int] ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__ ) def A ( self : List[Any] ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__ ) def A ( self : Tuple ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__ ) def A ( self : Any ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__ ) def A ( self : Any ): """simple docstring""" UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__ ) @slow def A ( self : List[Any] ): """simple docstring""" for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase = MraModel.from_pretrained(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ ) @unittest.skip(reason='MRA does not output attentions' ) def A ( self : List[str] ): """simple docstring""" return @require_torch class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" @slow def A ( self : Optional[int] ): """simple docstring""" UpperCamelCase = MraModel.from_pretrained('uw-madison/mra-base-512-4' ) UpperCamelCase = torch.arange(2_5_6 ).unsqueeze(0 ) with torch.no_grad(): UpperCamelCase = model(UpperCamelCase__ )[0] UpperCamelCase = torch.Size((1, 2_5_6, 7_6_8) ) self.assertEqual(output.shape , UpperCamelCase__ ) UpperCamelCase = torch.tensor( [[[-0.0_1_4_0, 0.0_8_3_0, -0.0_3_8_1], [0.1_5_4_6, 0.1_4_0_2, 0.0_2_2_0], [0.1_1_6_2, 0.0_8_5_1, 0.0_1_6_5]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , UpperCamelCase__ , atol=1E-4 ) ) @slow def A ( self : List[Any] ): """simple docstring""" UpperCamelCase = MraForMaskedLM.from_pretrained('uw-madison/mra-base-512-4' ) UpperCamelCase = torch.arange(2_5_6 ).unsqueeze(0 ) with torch.no_grad(): UpperCamelCase = model(UpperCamelCase__ )[0] UpperCamelCase = 5_0_2_6_5 UpperCamelCase = torch.Size((1, 2_5_6, vocab_size) ) self.assertEqual(output.shape , UpperCamelCase__ ) UpperCamelCase = torch.tensor( [[[9.2_5_9_5, -3.6_0_3_8, 1_1.8_8_1_9], [9.3_8_6_9, -3.2_6_9_3, 1_1.0_9_5_6], [1_1.8_5_2_4, -3.4_9_3_8, 1_3.1_2_1_0]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , UpperCamelCase__ , atol=1E-4 ) ) @slow def A ( self : List[Any] ): """simple docstring""" UpperCamelCase = MraForMaskedLM.from_pretrained('uw-madison/mra-base-4096-8-d3' ) UpperCamelCase = torch.arange(4_0_9_6 ).unsqueeze(0 ) with torch.no_grad(): UpperCamelCase = model(UpperCamelCase__ )[0] UpperCamelCase = 5_0_2_6_5 UpperCamelCase = torch.Size((1, 4_0_9_6, vocab_size) ) self.assertEqual(output.shape , UpperCamelCase__ ) UpperCamelCase = torch.tensor( [[[5.4_7_8_9, -2.3_5_6_4, 7.5_0_6_4], [7.9_0_6_7, -1.3_3_6_9, 9.9_6_6_8], [9.0_7_1_2, -1.8_1_0_6, 7.0_3_8_0]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , UpperCamelCase__ , atol=1E-4 ) )
28
1
"""simple docstring""" import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class lowerCAmelCase__ : def __init__( self : Any , snake_case__ : Union[str, Any] , snake_case__ : str=1_0_0 , snake_case__ : str=1_3 , snake_case__ : Optional[int]=3_0 , snake_case__ : List[Any]=2 , snake_case__ : Any=3 , snake_case__ : Union[str, Any]=True , snake_case__ : List[Any]=True , snake_case__ : Any=3_2 , snake_case__ : List[str]=4 , snake_case__ : Any=4 , snake_case__ : Dict=3_7 , snake_case__ : str="gelu" , snake_case__ : Union[str, Any]=0.1 , snake_case__ : int=0.1 , snake_case__ : List[Any]=1_0 , snake_case__ : Any=0.02 , snake_case__ : List[str]=3 , snake_case__ : Tuple=None , snake_case__ : Tuple=[0, 1, 2, 3] , ): '''simple docstring''' UpperCAmelCase__ : int = parent UpperCAmelCase__ : List[str] = 1_0_0 UpperCAmelCase__ : List[Any] = batch_size UpperCAmelCase__ : int = image_size UpperCAmelCase__ : List[Any] = patch_size UpperCAmelCase__ : List[Any] = num_channels UpperCAmelCase__ : Any = is_training UpperCAmelCase__ : str = use_labels UpperCAmelCase__ : Any = hidden_size UpperCAmelCase__ : Dict = num_hidden_layers UpperCAmelCase__ : int = num_attention_heads UpperCAmelCase__ : Tuple = intermediate_size UpperCAmelCase__ : Any = hidden_act UpperCAmelCase__ : Optional[int] = hidden_dropout_prob UpperCAmelCase__ : str = attention_probs_dropout_prob UpperCAmelCase__ : Optional[int] = type_sequence_label_size UpperCAmelCase__ : Any = initializer_range UpperCAmelCase__ : Any = scope UpperCAmelCase__ : Optional[Any] = out_indices UpperCAmelCase__ : int = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCAmelCase__ : List[Any] = (image_size // patch_size) ** 2 UpperCAmelCase__ : Optional[int] = num_patches + 1 def __a ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase__ : str = None UpperCAmelCase__ : Optional[int] = None if self.use_labels: UpperCAmelCase__ : Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase__ : Any = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) UpperCAmelCase__ : Tuple = self.get_config() return config, pixel_values, labels, pixel_labels def __a ( self : int ): '''simple docstring''' return BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=snake_case__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def __a ( self : int , snake_case__ : str , snake_case__ : str , snake_case__ : Dict , snake_case__ : List[str] ): '''simple docstring''' UpperCAmelCase__ : Dict = BeitModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : Dict = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self : Any , snake_case__ : List[str] , snake_case__ : Union[str, Any] , snake_case__ : Dict , snake_case__ : Any ): '''simple docstring''' UpperCAmelCase__ : int = BeitForMaskedImageModeling(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : List[Any] = model(snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def __a ( self : Optional[Any] , snake_case__ : Tuple , snake_case__ : Tuple , snake_case__ : str , snake_case__ : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : List[Any] = self.type_sequence_label_size UpperCAmelCase__ : Union[str, Any] = BeitForImageClassification(snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : Union[str, Any] = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCAmelCase__ : Any = 1 UpperCAmelCase__ : List[Any] = BeitForImageClassification(snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase__ : Optional[Any] = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __a ( self : Union[str, Any] , snake_case__ : int , snake_case__ : str , snake_case__ : Any , snake_case__ : Dict ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.num_labels UpperCAmelCase__ : int = BeitForSemanticSegmentation(snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : int = model(snake_case__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) UpperCAmelCase__ : Dict = model(snake_case__ , labels=snake_case__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def __a ( self : int ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.prepare_config_and_inputs() UpperCAmelCase__ : List[str] = config_and_inputs UpperCAmelCase__ : Any = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCAmelCase__ ( __magic_name__ , __magic_name__ , unittest.TestCase ): SCREAMING_SNAKE_CASE_ =( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) SCREAMING_SNAKE_CASE_ =( { '''feature-extraction''': BeitModel, '''image-classification''': BeitForImageClassification, '''image-segmentation''': BeitForSemanticSegmentation, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE_ =False SCREAMING_SNAKE_CASE_ =False SCREAMING_SNAKE_CASE_ =False def __a ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Dict = BeitModelTester(self ) UpperCAmelCase__ : List[str] = ConfigTester(self , config_class=snake_case__ , has_text_modality=snake_case__ , hidden_size=3_7 ) def __a ( self : List[str] ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def __a ( self : List[Any] ): '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __a ( self : List[str] ): '''simple docstring''' pass def __a ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase__ : Dict = model_class(snake_case__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCAmelCase__ : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case__ , nn.Linear ) ) def __a ( self : str ): '''simple docstring''' UpperCAmelCase__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase__ : int = model_class(snake_case__ ) UpperCAmelCase__ : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase__ : str = [*signature.parameters.keys()] UpperCAmelCase__ : int = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case__ ) def __a ( self : str ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def __a ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case__ ) def __a ( self : str ): '''simple docstring''' UpperCAmelCase__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) def __a ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*snake_case__ ) def __a ( self : List[Any] ): '''simple docstring''' if not self.model_tester.is_training: return UpperCAmelCase__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase__ : Optional[int] = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(snake_case__ ), BeitForMaskedImageModeling]: continue UpperCAmelCase__ : Optional[Any] = model_class(snake_case__ ) model.to(snake_case__ ) model.train() UpperCAmelCase__ : Optional[int] = self._prepare_for_class(snake_case__ , snake_case__ , return_labels=snake_case__ ) UpperCAmelCase__ : Tuple = model(**snake_case__ ).loss loss.backward() def __a ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return UpperCAmelCase__ : Optional[int] = False UpperCAmelCase__ : List[str] = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(snake_case__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue UpperCAmelCase__ : List[Any] = model_class(snake_case__ ) model.gradient_checkpointing_enable() model.to(snake_case__ ) model.train() UpperCAmelCase__ : Dict = self._prepare_for_class(snake_case__ , snake_case__ , return_labels=snake_case__ ) UpperCAmelCase__ : Optional[Any] = model(**snake_case__ ).loss loss.backward() def __a ( self : str ): '''simple docstring''' UpperCAmelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase__ : Union[str, Any] = _config_zero_init(snake_case__ ) for model_class in self.all_model_classes: UpperCAmelCase__ : int = model_class(config=snake_case__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=f'Parameter {name} of model {model_class} seems not properly initialized' , ) @slow def __a ( self : Any ): '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase__ : Optional[Any] = BeitModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def SCREAMING_SNAKE_CASE__ ( )-> Optional[Any]: '''simple docstring''' UpperCAmelCase__ : List[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowerCAmelCase__ ( unittest.TestCase ): @cached_property def __a ( self : Union[str, Any] ): '''simple docstring''' return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def __a ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(snake_case__ ) UpperCAmelCase__ : int = self.default_image_processor UpperCAmelCase__ : List[Any] = prepare_img() UpperCAmelCase__ : Dict = image_processor(images=snake_case__ , return_tensors="pt" ).pixel_values.to(snake_case__ ) # prepare bool_masked_pos UpperCAmelCase__ : Union[str, Any] = torch.ones((1, 1_9_6) , dtype=torch.bool ).to(snake_case__ ) # forward pass with torch.no_grad(): UpperCAmelCase__ : Optional[int] = model(pixel_values=snake_case__ , bool_masked_pos=snake_case__ ) UpperCAmelCase__ : str = outputs.logits # verify the logits UpperCAmelCase__ : int = torch.Size((1, 1_9_6, 8_1_9_2) ) self.assertEqual(logits.shape , snake_case__ ) UpperCAmelCase__ : Any = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(snake_case__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , snake_case__ , atol=1e-2 ) ) @slow def __a ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Tuple = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(snake_case__ ) UpperCAmelCase__ : Tuple = self.default_image_processor UpperCAmelCase__ : Dict = prepare_img() UpperCAmelCase__ : Tuple = image_processor(images=snake_case__ , return_tensors="pt" ).to(snake_case__ ) # forward pass with torch.no_grad(): UpperCAmelCase__ : Union[str, Any] = model(**snake_case__ ) UpperCAmelCase__ : Any = outputs.logits # verify the logits UpperCAmelCase__ : Optional[Any] = torch.Size((1, 1_0_0_0) ) self.assertEqual(logits.shape , snake_case__ ) UpperCAmelCase__ : Optional[Any] = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(snake_case__ ) self.assertTrue(torch.allclose(logits[0, :3] , snake_case__ , atol=1e-4 ) ) UpperCAmelCase__ : List[str] = 2_8_1 self.assertEqual(logits.argmax(-1 ).item() , snake_case__ ) @slow def __a ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : int = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( snake_case__ ) UpperCAmelCase__ : Tuple = self.default_image_processor UpperCAmelCase__ : Any = prepare_img() UpperCAmelCase__ : Union[str, Any] = image_processor(images=snake_case__ , return_tensors="pt" ).to(snake_case__ ) # forward pass with torch.no_grad(): UpperCAmelCase__ : List[Any] = model(**snake_case__ ) UpperCAmelCase__ : int = outputs.logits # verify the logits UpperCAmelCase__ : int = torch.Size((1, 2_1_8_4_1) ) self.assertEqual(logits.shape , snake_case__ ) UpperCAmelCase__ : int = torch.tensor([1.6881, -0.2787, 0.5901] ).to(snake_case__ ) self.assertTrue(torch.allclose(logits[0, :3] , snake_case__ , atol=1e-4 ) ) UpperCAmelCase__ : Any = 2_3_9_6 self.assertEqual(logits.argmax(-1 ).item() , snake_case__ ) @slow def __a ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Dict = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) UpperCAmelCase__ : List[Any] = model.to(snake_case__ ) UpperCAmelCase__ : int = BeitImageProcessor(do_resize=snake_case__ , size=6_4_0 , do_center_crop=snake_case__ ) UpperCAmelCase__ : Any = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) UpperCAmelCase__ : List[Any] = Image.open(ds[0]["file"] ) UpperCAmelCase__ : str = image_processor(images=snake_case__ , return_tensors="pt" ).to(snake_case__ ) # forward pass with torch.no_grad(): UpperCAmelCase__ : List[str] = model(**snake_case__ ) UpperCAmelCase__ : Dict = outputs.logits # verify the logits UpperCAmelCase__ : Any = torch.Size((1, 1_5_0, 1_6_0, 1_6_0) ) self.assertEqual(logits.shape , snake_case__ ) UpperCAmelCase__ : List[str] = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: UpperCAmelCase__ : Optional[Any] = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=snake_case__ , ) else: UpperCAmelCase__ : int = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=snake_case__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , snake_case__ , atol=1e-4 ) ) @slow def __a ( self : Any ): '''simple docstring''' UpperCAmelCase__ : str = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) UpperCAmelCase__ : Any = model.to(snake_case__ ) UpperCAmelCase__ : Dict = BeitImageProcessor(do_resize=snake_case__ , size=6_4_0 , do_center_crop=snake_case__ ) UpperCAmelCase__ : Tuple = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) UpperCAmelCase__ : Optional[int] = Image.open(ds[0]["file"] ) UpperCAmelCase__ : Optional[int] = image_processor(images=snake_case__ , return_tensors="pt" ).to(snake_case__ ) # forward pass with torch.no_grad(): UpperCAmelCase__ : Optional[int] = model(**snake_case__ ) UpperCAmelCase__ : int = outputs.logits.detach().cpu() UpperCAmelCase__ : str = image_processor.post_process_semantic_segmentation(outputs=snake_case__ , target_sizes=[(5_0_0, 3_0_0)] ) UpperCAmelCase__ : List[Any] = torch.Size((5_0_0, 3_0_0) ) self.assertEqual(segmentation[0].shape , snake_case__ ) UpperCAmelCase__ : Any = image_processor.post_process_semantic_segmentation(outputs=snake_case__ ) UpperCAmelCase__ : int = torch.Size((1_6_0, 1_6_0) ) self.assertEqual(segmentation[0].shape , snake_case__ )
350
"""simple docstring""" import argparse import re from typing import Dict import torch from datasets import Audio, Dataset, load_dataset, load_metric from transformers import AutoFeatureExtractor, pipeline def SCREAMING_SNAKE_CASE__ ( snake_case : Dataset , snake_case : Dict[str, str] )-> Any: '''simple docstring''' UpperCAmelCase__ : str = args.log_outputs UpperCAmelCase__ : str = "_".join(args.dataset.split("/" ) + [args.config, args.split] ) # load metric UpperCAmelCase__ : List[str] = load_metric("wer" ) UpperCAmelCase__ : Tuple = load_metric("cer" ) # compute metrics UpperCAmelCase__ : List[str] = wer.compute(references=result["target"] , predictions=result["prediction"] ) UpperCAmelCase__ : Tuple = cer.compute(references=result["target"] , predictions=result["prediction"] ) # print & log results UpperCAmelCase__ : Union[str, Any] = f'WER: {wer_result}\nCER: {cer_result}' print(snake_case ) with open(f'{dataset_id}_eval_results.txt' , "w" ) as f: f.write(snake_case ) # log all results in text file. Possibly interesting for analysis if log_outputs is not None: UpperCAmelCase__ : str = f'log_{dataset_id}_predictions.txt' UpperCAmelCase__ : List[str] = f'log_{dataset_id}_targets.txt' with open(snake_case , "w" ) as p, open(snake_case , "w" ) as t: # mapping function to write output def write_to_file(snake_case : List[Any] , snake_case : List[str] ): p.write(f'{i}' + "\n" ) p.write(batch["prediction"] + "\n" ) t.write(f'{i}' + "\n" ) t.write(batch["target"] + "\n" ) result.map(snake_case , with_indices=snake_case ) def SCREAMING_SNAKE_CASE__ ( snake_case : str )-> str: '''simple docstring''' UpperCAmelCase__ : str = "[,?.!\-\;\:\"“%‘”�—’…–]" # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training UpperCAmelCase__ : str = re.sub(snake_case , "" , text.lower() ) # In addition, we can normalize the target text, e.g. removing new lines characters etc... # note that order is important here! UpperCAmelCase__ : Tuple = ["\n\n", "\n", " ", " "] for t in token_sequences_to_ignore: UpperCAmelCase__ : List[Any] = " ".join(text.split(snake_case ) ) return text def SCREAMING_SNAKE_CASE__ ( snake_case : List[str] )-> str: '''simple docstring''' UpperCAmelCase__ : Optional[int] = load_dataset(args.dataset , args.config , split=args.split , use_auth_token=snake_case ) # for testing: only process the first two examples as a test # dataset = dataset.select(range(10)) # load processor UpperCAmelCase__ : List[Any] = AutoFeatureExtractor.from_pretrained(args.model_id ) UpperCAmelCase__ : str = feature_extractor.sampling_rate # resample audio UpperCAmelCase__ : Dict = dataset.cast_column("audio" , Audio(sampling_rate=snake_case ) ) # load eval pipeline if args.device is None: UpperCAmelCase__ : List[str] = 0 if torch.cuda.is_available() else -1 UpperCAmelCase__ : Optional[int] = pipeline("automatic-speech-recognition" , model=args.model_id , device=args.device ) # map function to decode audio def map_to_pred(snake_case : Any ): UpperCAmelCase__ : List[str] = asr( batch["audio"]["array"] , chunk_length_s=args.chunk_length_s , stride_length_s=args.stride_length_s ) UpperCAmelCase__ : List[Any] = prediction["text"] UpperCAmelCase__ : Optional[int] = normalize_text(batch["sentence"] ) return batch # run inference on all examples UpperCAmelCase__ : Dict = dataset.map(snake_case , remove_columns=dataset.column_names ) # compute and log_results # do not change function below log_results(snake_case , snake_case ) if __name__ == "__main__": _lowerCAmelCase : Any = argparse.ArgumentParser() parser.add_argument( """--model_id""", type=str, required=True, help="""Model identifier. Should be loadable with 🤗 Transformers""" ) parser.add_argument( """--dataset""", type=str, required=True, help="""Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets""", ) parser.add_argument( """--config""", type=str, required=True, help="""Config of the dataset. *E.g.* `'en'` for Common Voice""" ) parser.add_argument("""--split""", type=str, required=True, help="""Split of the dataset. *E.g.* `'test'`""") parser.add_argument( """--chunk_length_s""", type=float, default=None, help="""Chunk length in seconds. Defaults to 5 seconds.""" ) parser.add_argument( """--stride_length_s""", type=float, default=None, help="""Stride of the audio chunks. Defaults to 1 second.""" ) parser.add_argument( """--log_outputs""", action="""store_true""", help="""If defined, write outputs to log file for analysis.""" ) parser.add_argument( """--device""", type=int, default=None, help="""The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.""", ) _lowerCAmelCase : Tuple = parser.parse_args() main(args)
298
0
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) 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 .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
115
import argparse import collections import json import os import re import string import sys import numpy as np snake_case : Dict = re.compile(R"\b(a|an|the)\b", re.UNICODE) snake_case : Optional[int] = None def lowerCAmelCase_ ( ) -> Union[str, Any]: '''simple docstring''' __magic_name__ : Any = argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0." ) parser.add_argument("data_file" , metavar="data.json" , help="Input data JSON file." ) parser.add_argument("pred_file" , metavar="pred.json" , help="Model predictions." ) parser.add_argument( "--out-file" , "-o" , metavar="eval.json" , help="Write accuracy metrics to file (default is stdout)." ) parser.add_argument( "--na-prob-file" , "-n" , metavar="na_prob.json" , help="Model estimates of probability of no answer." ) parser.add_argument( "--na-prob-thresh" , "-t" , type=_snake_case , default=1.0 , help="Predict \"\" if no-answer probability exceeds this (default = 1.0)." , ) parser.add_argument( "--out-image-dir" , "-p" , metavar="out_images" , default=_snake_case , help="Save precision-recall curves to directory." ) parser.add_argument("--verbose" , "-v" , action="store_true" ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def lowerCAmelCase_ ( _snake_case : Optional[Any] ) -> Tuple: '''simple docstring''' __magic_name__ : Optional[int] = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: __magic_name__ : str = bool(qa["answers"]["text"] ) return qid_to_has_ans def lowerCAmelCase_ ( _snake_case : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' def remove_articles(_snake_case : List[str] ): return ARTICLES_REGEX.sub(" " , _snake_case ) def white_space_fix(_snake_case : Optional[int] ): return " ".join(text.split() ) def remove_punc(_snake_case : Optional[int] ): __magic_name__ : Dict = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(_snake_case : str ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(_snake_case ) ) ) ) def lowerCAmelCase_ ( _snake_case : Any ) -> Optional[Any]: '''simple docstring''' if not s: return [] return normalize_answer(_snake_case ).split() def lowerCAmelCase_ ( _snake_case : str , _snake_case : Dict ) -> Tuple: '''simple docstring''' return int(normalize_answer(_snake_case ) == normalize_answer(_snake_case ) ) def lowerCAmelCase_ ( _snake_case : List[str] , _snake_case : int ) -> str: '''simple docstring''' __magic_name__ : Any = get_tokens(_snake_case ) __magic_name__ : Optional[int] = get_tokens(_snake_case ) __magic_name__ : Tuple = collections.Counter(_snake_case ) & collections.Counter(_snake_case ) __magic_name__ : Tuple = sum(common.values() ) if len(_snake_case ) == 0 or len(_snake_case ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 __magic_name__ : Dict = 1.0 * num_same / len(_snake_case ) __magic_name__ : Optional[Any] = 1.0 * num_same / len(_snake_case ) __magic_name__ : List[Any] = (2 * precision * recall) / (precision + recall) return fa def lowerCAmelCase_ ( _snake_case : Optional[Any] , _snake_case : List[Any] ) -> List[Any]: '''simple docstring''' __magic_name__ : Union[str, Any] = {} __magic_name__ : int = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: __magic_name__ : Union[str, Any] = qa["id"] __magic_name__ : Any = [t for t in qa["answers"]["text"] if normalize_answer(_snake_case )] if not gold_answers: # For unanswerable questions, only correct answer is empty string __magic_name__ : Tuple = [""] if qid not in preds: print(F'''Missing prediction for {qid}''' ) continue __magic_name__ : Any = preds[qid] # Take max over all gold answers __magic_name__ : List[Any] = max(compute_exact(_snake_case , _snake_case ) for a in gold_answers ) __magic_name__ : int = max(compute_fa(_snake_case , _snake_case ) for a in gold_answers ) return exact_scores, fa_scores def lowerCAmelCase_ ( _snake_case : Optional[Any] , _snake_case : List[Any] , _snake_case : Optional[int] , _snake_case : Dict ) -> Union[str, Any]: '''simple docstring''' __magic_name__ : str = {} for qid, s in scores.items(): __magic_name__ : Dict = na_probs[qid] > na_prob_thresh if pred_na: __magic_name__ : str = float(not qid_to_has_ans[qid] ) else: __magic_name__ : Optional[int] = s return new_scores def lowerCAmelCase_ ( _snake_case : List[Any] , _snake_case : List[str] , _snake_case : Tuple=None ) -> Tuple: '''simple docstring''' if not qid_list: __magic_name__ : Any = len(_snake_case ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores.values() ) / total), ("f1", 100.0 * sum(fa_scores.values() ) / total), ("total", total), ] ) else: __magic_name__ : Tuple = len(_snake_case ) return collections.OrderedDict( [ ("exact", 100.0 * sum(exact_scores[k] for k in qid_list ) / total), ("f1", 100.0 * sum(fa_scores[k] for k in qid_list ) / total), ("total", total), ] ) def lowerCAmelCase_ ( _snake_case : Optional[int] , _snake_case : str , _snake_case : str ) -> Dict: '''simple docstring''' for k in new_eval: __magic_name__ : int = new_eval[k] def lowerCAmelCase_ ( _snake_case : Union[str, Any] , _snake_case : Dict , _snake_case : Optional[Any] , _snake_case : Union[str, Any] ) -> str: '''simple docstring''' plt.step(_snake_case , _snake_case , color="b" , alpha=0.2 , where="post" ) plt.fill_between(_snake_case , _snake_case , step="post" , alpha=0.2 , color="b" ) plt.xlabel("Recall" ) plt.ylabel("Precision" ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(_snake_case ) plt.savefig(_snake_case ) plt.clf() def lowerCAmelCase_ ( _snake_case : Dict , _snake_case : Any , _snake_case : Optional[int] , _snake_case : List[Any] , _snake_case : Optional[int]=None , _snake_case : int=None ) -> str: '''simple docstring''' __magic_name__ : Union[str, Any] = sorted(_snake_case , key=lambda _snake_case : na_probs[k] ) __magic_name__ : Optional[int] = 0.0 __magic_name__ : str = 1.0 __magic_name__ : str = 0.0 __magic_name__ : List[str] = [1.0] __magic_name__ : str = [0.0] __magic_name__ : Optional[Any] = 0.0 for i, qid in enumerate(_snake_case ): if qid_to_has_ans[qid]: true_pos += scores[qid] __magic_name__ : List[str] = true_pos / float(i + 1 ) __magic_name__ : Any = true_pos / float(_snake_case ) if i == len(_snake_case ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(_snake_case ) recalls.append(_snake_case ) if out_image: plot_pr_curve(_snake_case , _snake_case , _snake_case , _snake_case ) return {"ap": 100.0 * avg_prec} def lowerCAmelCase_ ( _snake_case : Tuple , _snake_case : Optional[Any] , _snake_case : Optional[int] , _snake_case : Optional[Any] , _snake_case : Any , _snake_case : List[Any] ) -> Union[str, Any]: '''simple docstring''' if out_image_dir and not os.path.exists(_snake_case ): os.makedirs(_snake_case ) __magic_name__ : Any = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return __magic_name__ : str = make_precision_recall_eval( _snake_case , _snake_case , _snake_case , _snake_case , out_image=os.path.join(_snake_case , "pr_exact.png" ) , title="Precision-Recall curve for Exact Match score" , ) __magic_name__ : Union[str, Any] = make_precision_recall_eval( _snake_case , _snake_case , _snake_case , _snake_case , out_image=os.path.join(_snake_case , "pr_f1.png" ) , title="Precision-Recall curve for F1 score" , ) __magic_name__ : str = {k: float(_snake_case ) for k, v in qid_to_has_ans.items()} __magic_name__ : str = make_precision_recall_eval( _snake_case , _snake_case , _snake_case , _snake_case , out_image=os.path.join(_snake_case , "pr_oracle.png" ) , title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)" , ) merge_eval(_snake_case , _snake_case , "pr_exact" ) merge_eval(_snake_case , _snake_case , "pr_f1" ) merge_eval(_snake_case , _snake_case , "pr_oracle" ) def lowerCAmelCase_ ( _snake_case : int , _snake_case : Optional[Any] , _snake_case : List[str] , _snake_case : Optional[Any] ) -> Dict: '''simple docstring''' if not qid_list: return __magic_name__ : Dict = [na_probs[k] for k in qid_list] __magic_name__ : str = np.ones_like(_snake_case ) / float(len(_snake_case ) ) plt.hist(_snake_case , weights=_snake_case , bins=20 , range=(0.0, 1.0) ) plt.xlabel("Model probability of no-answer" ) plt.ylabel("Proportion of dataset" ) plt.title(F'''Histogram of no-answer probability: {name}''' ) plt.savefig(os.path.join(_snake_case , F'''na_prob_hist_{name}.png''' ) ) plt.clf() def lowerCAmelCase_ ( _snake_case : Union[str, Any] , _snake_case : Tuple , _snake_case : List[str] , _snake_case : Dict ) -> List[Any]: '''simple docstring''' __magic_name__ : Union[str, Any] = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) __magic_name__ : List[str] = num_no_ans __magic_name__ : Dict = cur_score __magic_name__ : Dict = 0.0 __magic_name__ : Any = sorted(_snake_case , key=lambda _snake_case : na_probs[k] ) for i, qid in enumerate(_snake_case ): if qid not in scores: continue if qid_to_has_ans[qid]: __magic_name__ : Union[str, Any] = scores[qid] else: if preds[qid]: __magic_name__ : List[Any] = -1 else: __magic_name__ : Optional[int] = 0 cur_score += diff if cur_score > best_score: __magic_name__ : Optional[int] = cur_score __magic_name__ : List[Any] = na_probs[qid] return 100.0 * best_score / len(_snake_case ), best_thresh def lowerCAmelCase_ ( _snake_case : int , _snake_case : str , _snake_case : List[str] , _snake_case : Tuple , _snake_case : List[Any] , _snake_case : Dict ) -> Optional[Any]: '''simple docstring''' __magic_name__ , __magic_name__ : List[str] = find_best_thresh(_snake_case , _snake_case , _snake_case , _snake_case ) __magic_name__ , __magic_name__ : int = find_best_thresh(_snake_case , _snake_case , _snake_case , _snake_case ) __magic_name__ : Optional[int] = best_exact __magic_name__ : List[Any] = exact_thresh __magic_name__ : Dict = best_fa __magic_name__ : Any = fa_thresh def lowerCAmelCase_ ( ) -> int: '''simple docstring''' with open(OPTS.data_file ) as f: __magic_name__ : Optional[Any] = json.load(_snake_case ) __magic_name__ : List[Any] = dataset_json["data"] with open(OPTS.pred_file ) as f: __magic_name__ : Optional[Any] = json.load(_snake_case ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: __magic_name__ : Any = json.load(_snake_case ) else: __magic_name__ : Any = {k: 0.0 for k in preds} __magic_name__ : str = make_qid_to_has_ans(_snake_case ) # maps qid to True/False __magic_name__ : Tuple = [k for k, v in qid_to_has_ans.items() if v] __magic_name__ : Optional[Any] = [k for k, v in qid_to_has_ans.items() if not v] __magic_name__ , __magic_name__ : Union[str, Any] = get_raw_scores(_snake_case , _snake_case ) __magic_name__ : Optional[Any] = apply_no_ans_threshold(_snake_case , _snake_case , _snake_case , OPTS.na_prob_thresh ) __magic_name__ : Optional[Any] = apply_no_ans_threshold(_snake_case , _snake_case , _snake_case , OPTS.na_prob_thresh ) __magic_name__ : List[Any] = make_eval_dict(_snake_case , _snake_case ) if has_ans_qids: __magic_name__ : int = make_eval_dict(_snake_case , _snake_case , qid_list=_snake_case ) merge_eval(_snake_case , _snake_case , "HasAns" ) if no_ans_qids: __magic_name__ : List[Any] = make_eval_dict(_snake_case , _snake_case , qid_list=_snake_case ) merge_eval(_snake_case , _snake_case , "NoAns" ) if OPTS.na_prob_file: find_all_best_thresh(_snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(_snake_case , _snake_case , _snake_case , _snake_case , _snake_case , OPTS.out_image_dir ) histogram_na_prob(_snake_case , _snake_case , OPTS.out_image_dir , "hasAns" ) histogram_na_prob(_snake_case , _snake_case , OPTS.out_image_dir , "noAns" ) if OPTS.out_file: with open(OPTS.out_file , "w" ) as f: json.dump(_snake_case , _snake_case ) else: print(json.dumps(_snake_case , indent=2 ) ) if __name__ == "__main__": snake_case : int = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt main()
281
0
import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def UpperCamelCase (lowercase_: int , lowercase_: Dict=() , lowercase_: List[str]=None , lowercase_: Tuple="no" , lowercase_: str="29500" ) -> str: A__ : str = False A__ : Union[str, Any] = False if any(key.startswith("""KAGGLE""" ) for key in os.environ.keys() ): A__ : List[Any] = True elif "IPython" in sys.modules: A__ : str = """google.colab""" in str(sys.modules["""IPython"""].get_ipython() ) try: A__ : List[str] = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( f"""Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.""" ) if (in_colab or in_kaggle) and (os.environ.get("""TPU_NAME""" , lowercase_ ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( """To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside """ """your training function. Restart your notebook and make sure no cells initializes an """ """`Accelerator`.""" ) if num_processes is None: A__ : List[Any] = 8 A__ : Union[str, Any] = PrepareForLaunch(lowercase_ , distributed_type="""TPU""" ) print(f"""Launching a training on {num_processes} TPU cores.""" ) xmp.spawn(lowercase_ , args=lowercase_ , nprocs=lowercase_ , start_method="""fork""" ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print("""Launching training on one GPU.""" ) else: print("""Launching training on one CPU.""" ) function(*lowercase_ ) else: if num_processes is None: raise ValueError( """You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.""" ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( """To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized """ """inside your training function. Restart your notebook and make sure no cells initializes an """ """`Accelerator`.""" ) if torch.cuda.is_initialized(): raise ValueError( """To launch a multi-GPU training from your notebook, you need to avoid running any instruction """ """using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA """ """function.""" ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowercase_ , master_addr="""127.0.01""" , master_port=lowercase_ , mixed_precision=lowercase_ ): A__ : Optional[Any] = PrepareForLaunch(lowercase_ , distributed_type="""MULTI_GPU""" ) print(f"""Launching training on {num_processes} GPUs.""" ) try: start_processes(lowercase_ , args=lowercase_ , nprocs=lowercase_ , start_method="""fork""" ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( """CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. """ """This likely stems from an outside import causing issues once the `notebook_launcher()` is called. """ """Please review your imports and test them when running the `notebook_launcher()` to identify """ """which one is problematic.""" ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): A__ : List[Any] = """1""" print("""Launching training on MPS.""" ) elif torch.cuda.is_available(): print("""Launching training on one GPU.""" ) else: print("""Launching training on CPU.""" ) function(*lowercase_ ) def UpperCamelCase (lowercase_: Any , lowercase_: Dict=() , lowercase_: Optional[int]=2 ) -> Union[str, Any]: from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowercase_ , master_addr="""127.0.01""" , master_port="""29500""" , accelerate_mixed_precision="""no""" , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu="""yes""" , ): A__ : List[Any] = PrepareForLaunch(lowercase_ , debug=lowercase_ ) start_processes(lowercase_ , args=lowercase_ , nprocs=lowercase_ , start_method="""fork""" )
141
from __future__ import annotations def UpperCamelCase (lowercase_: list[int] , lowercase_: list[int] , lowercase_: int ) -> tuple[float, list[float]]: A__ : Tuple = list(range(len(lowercase_ ) ) ) A__ : Union[str, Any] = [v / w for v, w in zip(lowercase_ , lowercase_ )] index.sort(key=lambda lowercase_ : ratio[i] , reverse=lowercase_ ) A__ : float = 0 A__ : list[float] = [0] * len(lowercase_ ) for i in index: if weight[i] <= capacity: A__ : Optional[int] = 1 max_value += value[i] capacity -= weight[i] else: A__ : Union[str, Any] = capacity / weight[i] max_value += value[i] * capacity / weight[i] break return max_value, fractions if __name__ == "__main__": import doctest doctest.testmod()
141
1
"""simple docstring""" import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class UpperCamelCase ( _snake_case , unittest.TestCase ): lowercase = KandinskyVaaPriorPipeline lowercase = ['prompt'] lowercase = ['prompt', 'negative_prompt'] lowercase = [ 'num_images_per_prompt', 'generator', 'num_inference_steps', 'latents', 'negative_prompt', 'guidance_scale', 'output_type', 'return_dict', ] lowercase = False @property def _UpperCAmelCase ( self ) -> Any: '''simple docstring''' return 32 @property def _UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' return 32 @property def _UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' return self.time_input_dim @property def _UpperCAmelCase ( self ) -> List[str]: '''simple docstring''' return self.time_input_dim * 4 @property def _UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' return 100 @property def _UpperCAmelCase ( self ) -> Dict: '''simple docstring''' lowercase_ : List[str] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) return tokenizer @property def _UpperCAmelCase ( self ) -> Any: '''simple docstring''' torch.manual_seed(0 ) lowercase_ : Any = 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(_lowerCamelCase ) @property def _UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' torch.manual_seed(0 ) lowercase_ : int = { '''num_attention_heads''': 2, '''attention_head_dim''': 12, '''embedding_dim''': self.text_embedder_hidden_size, '''num_layers''': 1, } lowercase_ : str = PriorTransformer(**_lowerCamelCase ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 lowercase_ : str = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def _UpperCAmelCase ( self ) -> int: '''simple docstring''' torch.manual_seed(0 ) lowercase_ : Tuple = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size ,image_size=224 ,projection_dim=self.text_embedder_hidden_size ,intermediate_size=37 ,num_attention_heads=4 ,num_channels=3 ,num_hidden_layers=5 ,patch_size=14 ,) lowercase_ : Tuple = CLIPVisionModelWithProjection(_lowerCamelCase ) return model @property def _UpperCAmelCase ( self ) -> Dict: '''simple docstring''' lowercase_ : Any = CLIPImageProcessor( crop_size=224 ,do_center_crop=_lowerCamelCase ,do_normalize=_lowerCamelCase ,do_resize=_lowerCamelCase ,image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073] ,image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711] ,resample=3 ,size=224 ,) return image_processor def _UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' lowercase_ : Tuple = self.dummy_prior lowercase_ : int = self.dummy_image_encoder lowercase_ : Any = self.dummy_text_encoder lowercase_ : List[str] = self.dummy_tokenizer lowercase_ : Union[str, Any] = self.dummy_image_processor lowercase_ : List[Any] = UnCLIPScheduler( variance_type='fixed_small_log' ,prediction_type='sample' ,num_train_timesteps=1000 ,clip_sample=_lowerCamelCase ,clip_sample_range=10.0 ,) lowercase_ : Optional[Any] = { '''prior''': prior, '''image_encoder''': image_encoder, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''scheduler''': scheduler, '''image_processor''': image_processor, } return components def _UpperCAmelCase ( self ,__UpperCamelCase ,__UpperCamelCase=0 ) -> str: '''simple docstring''' if str(_lowerCamelCase ).startswith('mps' ): lowercase_ : str = torch.manual_seed(_lowerCamelCase ) else: lowercase_ : Any = torch.Generator(device=_lowerCamelCase ).manual_seed(_lowerCamelCase ) lowercase_ : List[str] = { '''prompt''': '''horse''', '''generator''': generator, '''guidance_scale''': 4.0, '''num_inference_steps''': 2, '''output_type''': '''np''', } return inputs def _UpperCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' lowercase_ : int = '''cpu''' lowercase_ : Tuple = self.get_dummy_components() lowercase_ : Optional[int] = self.pipeline_class(**_lowerCamelCase ) lowercase_ : Dict = pipe.to(_lowerCamelCase ) pipe.set_progress_bar_config(disable=_lowerCamelCase ) lowercase_ : Optional[Any] = pipe(**self.get_dummy_inputs(_lowerCamelCase ) ) lowercase_ : Optional[Any] = output.image_embeds lowercase_ : Union[str, Any] = pipe( **self.get_dummy_inputs(_lowerCamelCase ) ,return_dict=_lowerCamelCase ,)[0] lowercase_ : Tuple = image[0, -10:] lowercase_ : int = image_from_tuple[0, -10:] assert image.shape == (1, 32) lowercase_ : Optional[int] = np.array( [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def _UpperCAmelCase ( self ) -> Any: '''simple docstring''' lowercase_ : List[str] = torch_device == '''cpu''' lowercase_ : Union[str, Any] = True lowercase_ : int = False self._test_inference_batch_single_identical( test_max_difference=_lowerCamelCase ,relax_max_difference=_lowerCamelCase ,test_mean_pixel_difference=_lowerCamelCase ,) @skip_mps def _UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' lowercase_ : List[str] = torch_device == '''cpu''' lowercase_ : Union[str, Any] = False self._test_attention_slicing_forward_pass( test_max_difference=_lowerCamelCase ,test_mean_pixel_difference=_lowerCamelCase ,)
213
snake_case : str = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' snake_case : List[Any] = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] snake_case : int = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
94
0
import os from argparse import ArgumentParser, Namespace from ..data import SingleSentenceClassificationProcessor as Processor from ..pipelines import TextClassificationPipeline from ..utils import is_tf_available, is_torch_available, logging from . import BaseTransformersCLICommand if not is_tf_available() and not is_torch_available(): raise RuntimeError('''At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training''') # TF training parameters lowerCamelCase : List[Any] =False lowerCamelCase : Union[str, Any] =False def SCREAMING_SNAKE_CASE ( __lowerCAmelCase ) -> Union[str, Any]: return TrainCommand(__lowerCAmelCase ) class __a ( A__ ): @staticmethod def __lowercase ( SCREAMING_SNAKE_CASE : ArgumentParser ): '''simple docstring''' UpperCamelCase__ : str = parser.add_parser("train" , help="CLI tool to train a model on a task." ) train_parser.add_argument( "--train_data" , type=SCREAMING_SNAKE_CASE , required=SCREAMING_SNAKE_CASE , help="path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences." , ) train_parser.add_argument( "--column_label" , type=SCREAMING_SNAKE_CASE , default=0 , help="Column of the dataset csv file with example labels." ) train_parser.add_argument( "--column_text" , type=SCREAMING_SNAKE_CASE , default=1 , help="Column of the dataset csv file with example texts." ) train_parser.add_argument( "--column_id" , type=SCREAMING_SNAKE_CASE , default=2 , help="Column of the dataset csv file with example ids." ) train_parser.add_argument( "--skip_first_row" , action="store_true" , help="Skip the first row of the csv file (headers)." ) train_parser.add_argument("--validation_data" , type=SCREAMING_SNAKE_CASE , default="" , help="path to validation dataset." ) train_parser.add_argument( "--validation_split" , type=SCREAMING_SNAKE_CASE , default=0.1 , help="if validation dataset is not provided, fraction of train dataset to use as validation dataset." , ) train_parser.add_argument("--output" , type=SCREAMING_SNAKE_CASE , default="./" , help="path to saved the trained model." ) train_parser.add_argument( "--task" , type=SCREAMING_SNAKE_CASE , default="text_classification" , help="Task to train the model on." ) train_parser.add_argument( "--model" , type=SCREAMING_SNAKE_CASE , default="bert-base-uncased" , help="Model's name or path to stored model." ) train_parser.add_argument("--train_batch_size" , type=SCREAMING_SNAKE_CASE , default=32 , help="Batch size for training." ) train_parser.add_argument("--valid_batch_size" , type=SCREAMING_SNAKE_CASE , default=64 , help="Batch size for validation." ) train_parser.add_argument("--learning_rate" , type=SCREAMING_SNAKE_CASE , default=3e-5 , help="Learning rate." ) train_parser.add_argument("--adam_epsilon" , type=SCREAMING_SNAKE_CASE , default=1e-0_8 , help="Epsilon for Adam optimizer." ) train_parser.set_defaults(func=SCREAMING_SNAKE_CASE ) def __init__( self : str , SCREAMING_SNAKE_CASE : Namespace ): '''simple docstring''' UpperCamelCase__ : List[str] = logging.get_logger("transformers-cli/training" ) UpperCamelCase__ : List[Any] = "tf" if is_tf_available() else "torch" os.makedirs(args.output , exist_ok=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Tuple = args.output UpperCamelCase__ : Union[str, Any] = args.column_label UpperCamelCase__ : List[Any] = args.column_text UpperCamelCase__ : Tuple = args.column_id self.logger.info(F'Loading {args.task} pipeline for {args.model}' ) if args.task == "text_classification": UpperCamelCase__ : Any = TextClassificationPipeline.from_pretrained(args.model ) elif args.task == "token_classification": raise NotImplementedError elif args.task == "question_answering": raise NotImplementedError self.logger.info(F'Loading dataset from {args.train_data}' ) UpperCamelCase__ : Optional[int] = Processor.create_from_csv( args.train_data , column_label=args.column_label , column_text=args.column_text , column_id=args.column_id , skip_first_row=args.skip_first_row , ) UpperCamelCase__ : List[str] = None if args.validation_data: self.logger.info(F'Loading validation dataset from {args.validation_data}' ) UpperCamelCase__ : Optional[int] = Processor.create_from_csv( args.validation_data , column_label=args.column_label , column_text=args.column_text , column_id=args.column_id , skip_first_row=args.skip_first_row , ) UpperCamelCase__ : str = args.validation_split UpperCamelCase__ : List[str] = args.train_batch_size UpperCamelCase__ : List[str] = args.valid_batch_size UpperCamelCase__ : Any = args.learning_rate UpperCamelCase__ : Optional[int] = args.adam_epsilon def __lowercase ( self : Tuple ): '''simple docstring''' if self.framework == "tf": return self.run_tf() return self.run_torch() def __lowercase ( self : int ): '''simple docstring''' raise NotImplementedError def __lowercase ( self : Any ): '''simple docstring''' self.pipeline.fit( self.train_dataset , validation_data=self.valid_dataset , validation_split=self.validation_split , learning_rate=self.learning_rate , adam_epsilon=self.adam_epsilon , train_batch_size=self.train_batch_size , valid_batch_size=self.valid_batch_size , ) # Save trained pipeline self.pipeline.save_pretrained(self.output )
196
import argparse import os import re import packaging.version lowerCamelCase : Optional[Any] ='''examples/''' lowerCamelCase : List[Any] ={ '''examples''': (re.compile(R'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''), '''init''': (re.compile(R'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''), '''setup''': (re.compile(R'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), R'''\1version="VERSION",'''), '''doc''': (re.compile(R'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''), } lowerCamelCase : List[str] ={ '''init''': '''src/transformers/__init__.py''', '''setup''': '''setup.py''', } lowerCamelCase : int ='''README.md''' def SCREAMING_SNAKE_CASE ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Optional[Any]: with open(__lowerCAmelCase , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCamelCase__ : List[Any] = f.read() UpperCamelCase__ , UpperCamelCase__ : List[str] = REPLACE_PATTERNS[pattern] UpperCamelCase__ : Union[str, Any] = replace.replace("VERSION" , __lowerCAmelCase ) UpperCamelCase__ : Tuple = re_pattern.sub(__lowerCAmelCase , __lowerCAmelCase ) with open(__lowerCAmelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( __lowerCAmelCase ) -> Union[str, Any]: for folder, directories, fnames in os.walk(__lowerCAmelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("research_projects" ) if "legacy" in directories: directories.remove("legacy" ) for fname in fnames: if fname.endswith(".py" ): update_version_in_file(os.path.join(__lowerCAmelCase , __lowerCAmelCase ) , __lowerCAmelCase , pattern="examples" ) def SCREAMING_SNAKE_CASE ( __lowerCAmelCase , __lowerCAmelCase=False ) -> Optional[int]: for pattern, fname in REPLACE_FILES.items(): update_version_in_file(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if not patch: update_version_in_examples(__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: UpperCamelCase__ : Tuple = "🤗 Transformers currently provides the following architectures" UpperCamelCase__ : Tuple = "1. Want to contribute a new model?" with open(__lowerCAmelCase , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCamelCase__ : Optional[int] = f.readlines() # Find the start of the list. UpperCamelCase__ : List[Any] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 UpperCamelCase__ : Dict = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): UpperCamelCase__ : str = lines[index].replace( "https://huggingface.co/docs/transformers/main/model_doc" , "https://huggingface.co/docs/transformers/model_doc" , ) index += 1 with open(__lowerCAmelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( ) -> Tuple: with open(REPLACE_FILES["init"] , "r" ) as f: UpperCamelCase__ : str = f.read() UpperCamelCase__ : Dict = REPLACE_PATTERNS["init"][0].search(__lowerCAmelCase ).groups()[0] return packaging.version.parse(__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( __lowerCAmelCase=False ) -> Optional[int]: UpperCamelCase__ : Dict = get_version() if patch and default_version.is_devrelease: raise ValueError("Can't create a patch version from the dev branch, checkout a released version!" ) if default_version.is_devrelease: UpperCamelCase__ : List[str] = default_version.base_version elif patch: UpperCamelCase__ : int = f'{default_version.major}.{default_version.minor}.{default_version.micro + 1}' else: UpperCamelCase__ : Tuple = f'{default_version.major}.{default_version.minor + 1}.0' # Now let's ask nicely if that's the right one. UpperCamelCase__ : Tuple = input(f'Which version are you releasing? [{default_version}]' ) if len(__lowerCAmelCase ) == 0: UpperCamelCase__ : Any = default_version print(f'Updating version to {version}.' ) global_version_update(__lowerCAmelCase , patch=__lowerCAmelCase ) if not patch: print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() def SCREAMING_SNAKE_CASE ( ) -> int: UpperCamelCase__ : str = get_version() UpperCamelCase__ : Dict = f'{current_version.major}.{current_version.minor + 1}.0.dev0' UpperCamelCase__ : int = current_version.base_version # Check with the user we got that right. UpperCamelCase__ : List[str] = input(f'Which version are we developing now? [{dev_version}]' ) if len(__lowerCAmelCase ) == 0: UpperCamelCase__ : Optional[Any] = dev_version print(f'Updating version to {version}.' ) global_version_update(__lowerCAmelCase ) print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() if __name__ == "__main__": lowerCamelCase : List[Any] =argparse.ArgumentParser() parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''') parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''') lowerCamelCase : Optional[Any] =parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('''Nothing to do after a patch :-)''') else: post_release_work()
196
1
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor from ..utils import is_datasets_available from .base import PipelineTool if is_datasets_available(): from datasets import load_dataset class _lowercase ( snake_case_ ): lowercase = 'microsoft/speecht5_tts' lowercase = ( 'This is a tool that reads an English text out loud. It takes an input named `text` which should contain the ' 'text to read (in English) and returns a waveform object containing the sound.' ) lowercase = 'text_reader' lowercase = SpeechTaProcessor lowercase = SpeechTaForTextToSpeech lowercase = SpeechTaHifiGan lowercase = ['text'] lowercase = ['audio'] def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> int: """simple docstring""" if self.post_processor is None: UpperCamelCase_ : Dict = 'microsoft/speecht5_hifigan' super().setup() def SCREAMING_SNAKE_CASE__ ( self : Dict , snake_case : int , snake_case : int=None ) -> Dict: """simple docstring""" UpperCamelCase_ : Dict = self.pre_processor(text=snake_case , return_tensors='pt' , truncation=snake_case ) if speaker_embeddings is None: if not is_datasets_available(): raise ImportError('Datasets needs to be installed if not passing speaker embeddings.' ) UpperCamelCase_ : str = load_dataset('Matthijs/cmu-arctic-xvectors' , split='validation' ) UpperCamelCase_ : int = torch.tensor(embeddings_dataset[7_3_0_5]['xvector'] ).unsqueeze(0 ) return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings} def SCREAMING_SNAKE_CASE__ ( self : Tuple , snake_case : List[str] ) -> Any: """simple docstring""" with torch.no_grad(): return self.model.generate_speech(**snake_case ) def SCREAMING_SNAKE_CASE__ ( self : str , snake_case : Optional[int] ) -> Union[str, Any]: """simple docstring""" with torch.no_grad(): return self.post_processor(snake_case ).cpu().detach()
175
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_tf_available from transformers.testing_utils import require_tf if is_tf_available(): import tensorflow as tf from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments @require_tf class _lowercase ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Union[str, Any] ) -> Optional[int]: """simple docstring""" for model_result in results.values(): for batch_size, sequence_length in zip(model_result['bs'] , model_result['ss'] ): UpperCamelCase_ : Optional[Any] = model_result['result'][batch_size][sequence_length] self.assertIsNotNone(snake_case ) def SCREAMING_SNAKE_CASE__ ( self : int ) -> Tuple: """simple docstring""" UpperCamelCase_ : Optional[Any] = 'sshleifer/tiny-gpt2' UpperCamelCase_ : Dict = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=snake_case , multi_process=snake_case , ) UpperCamelCase_ : Tuple = TensorFlowBenchmark(snake_case ) UpperCamelCase_ : List[str] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[Any]: """simple docstring""" UpperCamelCase_ : Dict = 'sgugger/tiny-distilbert-classification' UpperCamelCase_ : int = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=snake_case , only_pretrain_model=snake_case , ) UpperCamelCase_ : int = TensorFlowBenchmark(snake_case ) UpperCamelCase_ : Tuple = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> List[str]: """simple docstring""" UpperCamelCase_ : str = 'sshleifer/tiny-gpt2' UpperCamelCase_ : Any = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=snake_case , ) UpperCamelCase_ : List[str] = TensorFlowBenchmark(snake_case ) UpperCamelCase_ : Optional[int] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> Dict: """simple docstring""" UpperCamelCase_ : List[str] = 'sshleifer/tiny-gpt2' UpperCamelCase_ : str = AutoConfig.from_pretrained(snake_case ) UpperCamelCase_ : List[str] = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , eager_mode=snake_case , multi_process=snake_case , ) UpperCamelCase_ : int = TensorFlowBenchmark(snake_case , [config] ) UpperCamelCase_ : Optional[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> List[str]: """simple docstring""" UpperCamelCase_ : List[str] = 'sshleifer/tiny-gpt2' UpperCamelCase_ : int = AutoConfig.from_pretrained(snake_case ) UpperCamelCase_ : Any = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=snake_case , ) UpperCamelCase_ : Tuple = TensorFlowBenchmark(snake_case , [config] ) UpperCamelCase_ : Tuple = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> int: """simple docstring""" UpperCamelCase_ : Optional[int] = 'sshleifer/tiny-gpt2' UpperCamelCase_ : Tuple = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=snake_case , ) UpperCamelCase_ : Optional[int] = TensorFlowBenchmark(snake_case ) UpperCamelCase_ : str = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def SCREAMING_SNAKE_CASE__ ( self : Any ) -> int: """simple docstring""" UpperCamelCase_ : Tuple = 'sshleifer/tiny-gpt2' UpperCamelCase_ : List[str] = AutoConfig.from_pretrained(snake_case ) UpperCamelCase_ : int = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=snake_case , ) UpperCamelCase_ : List[Any] = TensorFlowBenchmark(snake_case , [config] ) UpperCamelCase_ : int = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> Dict: """simple docstring""" UpperCamelCase_ : Tuple = 'patrickvonplaten/t5-tiny-random' UpperCamelCase_ : List[str] = AutoConfig.from_pretrained(snake_case ) UpperCamelCase_ : List[str] = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=snake_case , ) UpperCamelCase_ : int = TensorFlowBenchmark(snake_case , configs=[config] ) UpperCamelCase_ : Optional[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU' ) ) == 0 , 'Cannot do xla on CPU.' ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Dict: """simple docstring""" UpperCamelCase_ : int = 'sshleifer/tiny-gpt2' UpperCamelCase_ : Union[str, Any] = TensorFlowBenchmarkArguments( models=[MODEL_ID] , training=snake_case , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , use_xla=snake_case , multi_process=snake_case , ) UpperCamelCase_ : Union[str, Any] = TensorFlowBenchmark(snake_case ) UpperCamelCase_ : List[str] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ) -> Optional[int]: """simple docstring""" UpperCamelCase_ : List[Any] = 'sshleifer/tiny-gpt2' with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase_ : List[Any] = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=snake_case , save_to_csv=snake_case , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(snake_case , 'inf_time.csv' ) , inference_memory_csv_file=os.path.join(snake_case , 'inf_mem.csv' ) , env_info_csv_file=os.path.join(snake_case , 'env.csv' ) , multi_process=snake_case , ) UpperCamelCase_ : List[str] = TensorFlowBenchmark(snake_case ) benchmark.run() self.assertTrue(Path(os.path.join(snake_case , 'inf_time.csv' ) ).exists() ) self.assertTrue(Path(os.path.join(snake_case , 'inf_mem.csv' ) ).exists() ) self.assertTrue(Path(os.path.join(snake_case , 'env.csv' ) ).exists() ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ) -> str: """simple docstring""" UpperCamelCase_ : Optional[Any] = 'sshleifer/tiny-gpt2' def _check_summary_is_not_empty(snake_case : Union[str, Any] ): self.assertTrue(hasattr(snake_case , 'sequential' ) ) self.assertTrue(hasattr(snake_case , 'cumulative' ) ) self.assertTrue(hasattr(snake_case , 'current' ) ) self.assertTrue(hasattr(snake_case , 'total' ) ) with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase_ : str = TensorFlowBenchmarkArguments( models=[MODEL_ID] , inference=snake_case , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(snake_case , 'log.txt' ) , log_print=snake_case , trace_memory_line_by_line=snake_case , eager_mode=snake_case , multi_process=snake_case , ) UpperCamelCase_ : Tuple = TensorFlowBenchmark(snake_case ) UpperCamelCase_ : Union[str, Any] = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) self.assertTrue(Path(os.path.join(snake_case , 'log.txt' ) ).exists() )
175
1
'''simple docstring''' import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class lowerCAmelCase__ ( UpperCAmelCase__ ): lowerCAmelCase : Tuple = (KDPMaDiscreteScheduler,) lowerCAmelCase : Optional[int] = 10 def lowerCAmelCase__ ( self : Optional[int] , **lowerCamelCase__ : Optional[Any] ) ->Dict: '''simple docstring''' _UpperCAmelCase : Optional[Any] = { "num_train_timesteps": 11_00, "beta_start": 0.0_0_0_1, "beta_end": 0.0_2, "beta_schedule": "linear", } config.update(**lowerCamelCase__ ) return config def lowerCAmelCase__ ( self : Union[str, Any] ) ->Optional[int]: '''simple docstring''' for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=lowerCamelCase__ ) def lowerCAmelCase__ ( self : Dict ) ->str: '''simple docstring''' for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=lowerCamelCase__ , beta_end=lowerCamelCase__ ) def lowerCAmelCase__ ( self : Dict ) ->Tuple: '''simple docstring''' for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=lowerCamelCase__ ) def lowerCAmelCase__ ( self : List[str] ) ->List[str]: '''simple docstring''' for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCamelCase__ ) def lowerCAmelCase__ ( self : Union[str, Any] ) ->Union[str, Any]: '''simple docstring''' _UpperCAmelCase : Dict = self.scheduler_classes[0] _UpperCAmelCase : Optional[int] = self.get_scheduler_config(prediction_type="v_prediction" ) _UpperCAmelCase : Tuple = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) _UpperCAmelCase : Dict = self.dummy_model() _UpperCAmelCase : str = self.dummy_sample_deter * scheduler.init_noise_sigma _UpperCAmelCase : List[Any] = sample.to(lowerCamelCase__ ) for i, t in enumerate(scheduler.timesteps ): _UpperCAmelCase : Optional[int] = scheduler.scale_model_input(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : Dict = model(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : Tuple = scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : Any = output.prev_sample _UpperCAmelCase : Optional[Any] = torch.sum(torch.abs(lowerCamelCase__ ) ) _UpperCAmelCase : Tuple = torch.mean(torch.abs(lowerCamelCase__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6934E-07 ) < 1E-2 assert abs(result_mean.item() - 6.1112E-10 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 4.693428650170972E-07 ) < 1E-2 assert abs(result_mean.item() - 0.0_0_0_2 ) < 1E-3 def lowerCAmelCase__ ( self : List[Any] ) ->Optional[Any]: '''simple docstring''' if torch_device == "mps": return _UpperCAmelCase : Union[str, Any] = self.scheduler_classes[0] _UpperCAmelCase : Any = self.get_scheduler_config() _UpperCAmelCase : Any = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) _UpperCAmelCase : Optional[Any] = self.dummy_model() _UpperCAmelCase : str = self.dummy_sample_deter * scheduler.init_noise_sigma _UpperCAmelCase : List[Any] = sample.to(lowerCamelCase__ ) for i, t in enumerate(scheduler.timesteps ): _UpperCAmelCase : List[Any] = scheduler.scale_model_input(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : str = model(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : str = scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : List[Any] = output.prev_sample _UpperCAmelCase : Any = torch.sum(torch.abs(lowerCamelCase__ ) ) _UpperCAmelCase : List[str] = torch.mean(torch.abs(lowerCamelCase__ ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3 def lowerCAmelCase__ ( self : Any ) ->str: '''simple docstring''' if torch_device == "mps": return _UpperCAmelCase : Union[str, Any] = self.scheduler_classes[0] _UpperCAmelCase : Optional[int] = self.get_scheduler_config() _UpperCAmelCase : Any = scheduler_class(**lowerCamelCase__ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCamelCase__ ) _UpperCAmelCase : Tuple = self.dummy_model() _UpperCAmelCase : Dict = self.dummy_sample_deter.to(lowerCamelCase__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _UpperCAmelCase : Optional[int] = scheduler.scale_model_input(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : Any = model(lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : List[Any] = scheduler.step(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) _UpperCAmelCase : Tuple = output.prev_sample _UpperCAmelCase : List[Any] = torch.sum(torch.abs(lowerCamelCase__ ) ) _UpperCAmelCase : Optional[int] = torch.mean(torch.abs(lowerCamelCase__ ) ) if str(lowerCamelCase__ ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3 else: # CUDA assert abs(result_sum.item() - 20.41_25 ) < 1E-2 assert abs(result_mean.item() - 0.0_2_6_6 ) < 1E-3
355
'''simple docstring''' def __lowerCAmelCase (__lowerCAmelCase = 4_000_000 ): _UpperCAmelCase : List[Any] = [] _UpperCAmelCase , _UpperCAmelCase : Dict = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__lowerCAmelCase ) _UpperCAmelCase , _UpperCAmelCase : Any = b, a + b return sum(__lowerCAmelCase ) if __name__ == "__main__": print(F'''{solution() = }''')
322
0
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class __magic_name__ ( lowerCAmelCase_ , lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = 1 @register_to_config def __init__( self , __snake_case = 1000 , __snake_case = None ) -> List[str]: '''simple docstring''' # set `betas`, `alphas`, `timesteps` self.set_timesteps(__snake_case ) # standard deviation of the initial noise distribution __a =1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. __a =4 # running values __a =[] def __magic_name__ ( self , __snake_case , __snake_case = None ) -> str: '''simple docstring''' __a =num_inference_steps __a =torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] __a =torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: __a =torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: __a =torch.sin(steps * math.pi / 2 ) ** 2 __a =(1.0 - self.betas**2) ** 0.5 __a =(torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] __a =timesteps.to(__snake_case ) __a =[] def __magic_name__ ( self , __snake_case , __snake_case , __snake_case , __snake_case = True , ) -> Union[SchedulerOutput, Tuple]: '''simple docstring''' if self.num_inference_steps is None: raise ValueError( 'Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler' ) __a =(self.timesteps == timestep).nonzero().item() __a =timestep_index + 1 __a =sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(__snake_case ) if len(self.ets ) == 1: __a =self.ets[-1] elif len(self.ets ) == 2: __a =(3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: __a =(23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: __a =(1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) __a =self._get_prev_sample(__snake_case , __snake_case , __snake_case , __snake_case ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=__snake_case ) def __magic_name__ ( self , __snake_case , *__snake_case , **__snake_case ) -> torch.FloatTensor: '''simple docstring''' return sample def __magic_name__ ( self , __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: '''simple docstring''' __a =self.alphas[timestep_index] __a =self.betas[timestep_index] __a =self.alphas[prev_timestep_index] __a =self.betas[prev_timestep_index] __a =(sample - sigma * ets) / max(__snake_case , 1e-8 ) __a =next_alpha * pred + ets * next_sigma return prev_sample def __len__( self ) -> Dict: '''simple docstring''' return self.config.num_train_timesteps
218
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCAmelCase : Tuple = logging.get_logger(__name__) _lowerCAmelCase : Union[str, Any] = {"ctrl": "https://huggingface.co/ctrl/resolve/main/config.json"} class __magic_name__ ( lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = 'ctrl' SCREAMING_SNAKE_CASE = ['past_key_values'] SCREAMING_SNAKE_CASE = { 'max_position_embeddings': 'n_positions', 'hidden_size': 'n_embd', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , __snake_case=24_6534 , __snake_case=256 , __snake_case=1280 , __snake_case=8192 , __snake_case=48 , __snake_case=16 , __snake_case=0.1 , __snake_case=0.1 , __snake_case=1e-6 , __snake_case=0.02 , __snake_case=True , **__snake_case , ) -> Union[str, Any]: '''simple docstring''' __a =vocab_size __a =n_positions __a =n_embd __a =n_layer __a =n_head __a =dff __a =resid_pdrop __a =embd_pdrop __a =layer_norm_epsilon __a =initializer_range __a =use_cache super().__init__(**__snake_case )
218
1
def _A ( lowerCAmelCase_ : int = 400_0000 ): """simple docstring""" lowerCAmelCase__ = [] lowerCAmelCase__ , lowerCAmelCase__ = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(lowerCAmelCase_ ) lowerCAmelCase__ , lowerCAmelCase__ = b, a + b return sum(lowerCAmelCase_ ) if __name__ == "__main__": print(F"""{solution() = }""")
221
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConfig, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaForPreTraining, WavaVecaProcessor, logging, ) from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'adapter_layer': 'encoder.layers.*.adapter_layer', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', 'pooling_layer.linear': 'projector', 'pooling_layer.projection': 'classifier', } UpperCamelCase = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', 'projector', 'classifier', ] def _A ( lowerCAmelCase_ : Optional[int] ): """simple docstring""" lowerCAmelCase__ = {} with open(lowerCAmelCase_ , "r" ) as file: for line_number, line in enumerate(lowerCAmelCase_ ): lowerCAmelCase__ = line.strip() if line: lowerCAmelCase__ = line.split() lowerCAmelCase__ = line_number lowerCAmelCase__ = words[0] lowerCAmelCase__ = value return result def _A ( lowerCAmelCase_ : Any , lowerCAmelCase_ : int , lowerCAmelCase_ : int , lowerCAmelCase_ : Union[str, Any] , lowerCAmelCase_ : int ): """simple docstring""" for attribute in key.split("." ): lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(lowerCAmelCase_ ): lowerCAmelCase__ = PARAM_MAPPING[full_name.split("." )[-1]] lowerCAmelCase__ = "param" if weight_type is not None and weight_type != "param": lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ).shape elif weight_type is not None and weight_type == "param": lowerCAmelCase__ = hf_pointer for attribute in hf_param_name.split("." ): lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = shape_pointer.shape # let's reduce dimension lowerCAmelCase__ = value[0] else: lowerCAmelCase__ = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be' F' {value.shape} for {full_name}' ) if weight_type == "weight": lowerCAmelCase__ = value elif weight_type == "weight_g": lowerCAmelCase__ = value elif weight_type == "weight_v": lowerCAmelCase__ = value elif weight_type == "bias": lowerCAmelCase__ = value elif weight_type == "param": for attribute in hf_param_name.split("." ): lowerCAmelCase__ = getattr(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = value else: lowerCAmelCase__ = value logger.info(F'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' ) def _A ( lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : int , lowerCAmelCase_ : Optional[Any] ): """simple docstring""" lowerCAmelCase__ = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(lowerCAmelCase_ ): lowerCAmelCase__ = PARAM_MAPPING[full_name.split("." )[-1]] lowerCAmelCase__ = "param" if weight_type is not None and weight_type != "param": lowerCAmelCase__ = ".".join([key, weight_type] ) elif weight_type is not None and weight_type == "param": lowerCAmelCase__ = ".".join([key, hf_param_name] ) else: lowerCAmelCase__ = key lowerCAmelCase__ = value if "lm_head" in full_key else value[0] UpperCamelCase = { 'W_a': 'linear_1.weight', 'W_b': 'linear_2.weight', 'b_a': 'linear_1.bias', 'b_b': 'linear_2.bias', 'ln_W': 'norm.weight', 'ln_b': 'norm.bias', } def _A ( lowerCAmelCase_ : int , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : str=None , lowerCAmelCase_ : Any=None ): """simple docstring""" lowerCAmelCase__ = False for key, mapped_key in MAPPING.items(): lowerCAmelCase__ = "wav2vec2." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: lowerCAmelCase__ = True if "*" in mapped_key: lowerCAmelCase__ = name.split(lowerCAmelCase_ )[0].split("." )[-2] lowerCAmelCase__ = mapped_key.replace("*" , lowerCAmelCase_ ) if "weight_g" in name: lowerCAmelCase__ = "weight_g" elif "weight_v" in name: lowerCAmelCase__ = "weight_v" elif "bias" in name: lowerCAmelCase__ = "bias" elif "weight" in name: # TODO: don't match quantizer.weight_proj lowerCAmelCase__ = "weight" else: lowerCAmelCase__ = None if hf_dict is not None: rename_dict(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) else: set_recursively(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) return is_used return is_used def _A ( lowerCAmelCase_ : Optional[Any] , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Dict ): """simple docstring""" lowerCAmelCase__ = [] lowerCAmelCase__ = fairseq_model.state_dict() lowerCAmelCase__ = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): lowerCAmelCase__ = False if "conv_layers" in name: load_conv_layer( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , hf_model.config.feat_extract_norm == "group" , ) lowerCAmelCase__ = True else: lowerCAmelCase__ = load_wavaveca_layer(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) if not is_used: unused_weights.append(lowerCAmelCase_ ) logger.warning(F'Unused weights: {unused_weights}' ) def _A ( lowerCAmelCase_ : str , lowerCAmelCase_ : Dict , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Any , lowerCAmelCase_ : Optional[int] ): """simple docstring""" lowerCAmelCase__ = full_name.split("conv_layers." )[-1] lowerCAmelCase__ = name.split("." ) lowerCAmelCase__ = int(items[0] ) lowerCAmelCase__ = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F'{full_name} has size {value.shape}, but' F' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.' ) lowerCAmelCase__ = value logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(lowerCAmelCase_ ) @torch.no_grad() def _A ( lowerCAmelCase_ : str , lowerCAmelCase_ : Dict , lowerCAmelCase_ : List[Any]=None , lowerCAmelCase_ : Optional[Any]=None , lowerCAmelCase_ : Optional[int]=True , lowerCAmelCase_ : List[str]=False ): """simple docstring""" if config_path is not None: lowerCAmelCase__ = WavaVecaConfig.from_pretrained(lowerCAmelCase_ ) else: lowerCAmelCase__ = WavaVecaConfig() if is_seq_class: lowerCAmelCase__ = read_txt_into_dict(lowerCAmelCase_ ) lowerCAmelCase__ = idalabel lowerCAmelCase__ = WavaVecaForSequenceClassification(lowerCAmelCase_ ) lowerCAmelCase__ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , ) feature_extractor.save_pretrained(lowerCAmelCase_ ) elif is_finetuned: if dict_path: lowerCAmelCase__ = Dictionary.load(lowerCAmelCase_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq lowerCAmelCase__ = target_dict.pad_index lowerCAmelCase__ = target_dict.bos_index lowerCAmelCase__ = target_dict.eos_index lowerCAmelCase__ = len(target_dict.symbols ) lowerCAmelCase__ = os.path.join(lowerCAmelCase_ , "vocab.json" ) if not os.path.isdir(lowerCAmelCase_ ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(lowerCAmelCase_ ) ) return os.makedirs(lowerCAmelCase_ , exist_ok=lowerCAmelCase_ ) lowerCAmelCase__ = target_dict.indices # fairseq has the <pad> and <s> switched lowerCAmelCase__ = 0 lowerCAmelCase__ = 1 with open(lowerCAmelCase_ , "w" , encoding="utf-8" ) as vocab_handle: json.dump(lowerCAmelCase_ , lowerCAmelCase_ ) lowerCAmelCase__ = WavaVecaCTCTokenizer( lowerCAmelCase_ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=lowerCAmelCase_ , ) lowerCAmelCase__ = True if config.feat_extract_norm == "layer" else False lowerCAmelCase__ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=lowerCAmelCase_ , return_attention_mask=lowerCAmelCase_ , ) lowerCAmelCase__ = WavaVecaProcessor(feature_extractor=lowerCAmelCase_ , tokenizer=lowerCAmelCase_ ) processor.save_pretrained(lowerCAmelCase_ ) lowerCAmelCase__ = WavaVecaForCTC(lowerCAmelCase_ ) else: lowerCAmelCase__ = WavaVecaForPreTraining(lowerCAmelCase_ ) if is_finetuned or is_seq_class: lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: lowerCAmelCase__ = argparse.Namespace(task="audio_pretraining" ) lowerCAmelCase__ = fairseq.tasks.setup_task(lowerCAmelCase_ ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=lowerCAmelCase_ ) lowerCAmelCase__ = model[0].eval() recursively_load_weights(lowerCAmelCase_ , lowerCAmelCase_ , not is_finetuned ) hf_wavavec.save_pretrained(lowerCAmelCase_ ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) parser.add_argument( '--is_seq_class', action='store_true', help='Whether the model to convert is a fine-tuned sequence classification model or not', ) UpperCamelCase = parser.parse_args() UpperCamelCase = not args.not_finetuned and not args.is_seq_class convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, is_finetuned, args.is_seq_class, )
221
1
def __lowerCamelCase ( lowerCamelCase__ : Union[str, Any] ): '''simple docstring''' if len(_UpperCAmelCase ) < 2: return collection def circle_sort_util(lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : Optional[Any] ) -> bool: lowerCamelCase = False if low == high: return swapped lowerCamelCase = low lowerCamelCase = high while left < right: if collection[left] > collection[right]: lowerCamelCase = ( collection[right], collection[left], ) lowerCamelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCamelCase = ( collection[right + 1], collection[left], ) lowerCamelCase = True lowerCamelCase = low + int((high - low) / 2 ) lowerCamelCase = circle_sort_util(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) lowerCamelCase = circle_sort_util(_UpperCAmelCase , mid + 1 , _UpperCAmelCase ) return swapped or left_swap or right_swap lowerCamelCase = True while is_not_sorted is True: lowerCamelCase = circle_sort_util(_UpperCAmelCase , 0 , len(_UpperCAmelCase ) - 1 ) return collection if __name__ == "__main__": UpperCAmelCase : Optional[Any] = input("Enter numbers separated by a comma:\n").strip() UpperCAmelCase : List[str] = [int(item) for item in user_input.split(",")] print(circle_sort(unsorted))
252
def SCREAMING_SNAKE_CASE ( _UpperCAmelCase = 100_0000 ) -> int: lowerCamelCase__ : int = limit + 1 lowerCamelCase__ : Optional[Any] = [0] * limit for first_term in range(1 , _UpperCAmelCase ): for n in range(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): lowerCamelCase__ : Optional[Any] = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a lowerCamelCase__ : List[str] = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(F"""{solution() = }""")
50
0
'''simple docstring''' from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class lowerCamelCase_ ( __a ): lowerCAmelCase__ = '' lowerCAmelCase__ = 'hf-legacy' # "hf://"" is reserved for hffs def __init__( self : int , _A : Optional[DatasetInfo] = None , _A : Optional[str] = None , **_A : Optional[Any] , ): '''simple docstring''' super().__init__(self , **_A ) UpperCAmelCase__ : Any = repo_info UpperCAmelCase__ : Optional[Any] = token UpperCAmelCase__ : Optional[Any] = None def lowercase_ ( self : Optional[int] ): '''simple docstring''' if self.dir_cache is None: UpperCAmelCase__ : Dict = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes UpperCAmelCase__ : str = { '''name''': hf_file.rfilename, '''size''': None, '''type''': '''file''', } self.dir_cache.update( { str(_A ): {'''name''': str(_A ), '''size''': None, '''type''': '''directory'''} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def lowercase_ ( self : Any , _A : str , _A : str = "rb" , **_A : Optional[Any] , ): '''simple docstring''' if not isinstance(self.repo_info , _A ): raise NotImplementedError(f"""Open is only implemented for dataset repositories, but got {self.repo_info}""" ) UpperCAmelCase__ : Any = hf_hub_url(self.repo_info.id , _A , revision=self.repo_info.sha ) return fsspec.open( _A , mode=_A , headers=get_authentication_headers_for_url(_A , use_auth_token=self.token ) , client_kwargs={'''trust_env''': True} , ).open() def lowercase_ ( self : int , _A : str , **_A : int ): '''simple docstring''' self._get_dirs() UpperCAmelCase__ : Tuple = self._strip_protocol(_A ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(_A ) def lowercase_ ( self : Union[str, Any] , _A : List[Any] , _A : int=False , **_A : Optional[int] ): '''simple docstring''' self._get_dirs() UpperCAmelCase__ : Any = PurePosixPath(path.strip('''/''' ) ) UpperCAmelCase__ : List[str] = {} for p, f in self.dir_cache.items(): UpperCAmelCase__ : Any = PurePosixPath(p.strip('''/''' ) ) UpperCAmelCase__ : Optional[int] = p.parent if root == path: UpperCAmelCase__ : Tuple = f UpperCAmelCase__ : Any = list(paths.values() ) if detail: return out else: return sorted(f['''name'''] for f in out )
366
'''simple docstring''' import json import os import shutil import tempfile import unittest from multiprocessing import get_context from pathlib import Path import datasets import numpy as np from datasets import load_dataset from parameterized import parameterized from transformers import AutoProcessor from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.testing_utils import require_pyctcdecode, require_torch, require_torchaudio, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, is_pyctcdecode_available, is_torch_available from ..wavaveca.test_feature_extraction_wavaveca import floats_list if is_pyctcdecode_available(): from huggingface_hub import snapshot_download from pyctcdecode import BeamSearchDecoderCTC from transformers.models.wavaveca_with_lm import WavaVecaProcessorWithLM from transformers.models.wavaveca_with_lm.processing_wavaveca_with_lm import WavaVecaDecoderWithLMOutput if is_torch_available(): from transformers import WavaVecaForCTC @require_pyctcdecode class lowerCamelCase_ ( unittest.TestCase ): def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Any = '''| <pad> <unk> <s> </s> a b c d e f g h i j k'''.split() UpperCAmelCase__ : Optional[Any] = dict(zip(_A , range(len(_A ) ) ) ) UpperCAmelCase__ : Tuple = { '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>''', } UpperCAmelCase__ : Optional[int] = { '''feature_size''': 1, '''padding_value''': 0.0, '''sampling_rate''': 16_000, '''return_attention_mask''': False, '''do_normalize''': True, } UpperCAmelCase__ : Union[str, Any] = tempfile.mkdtemp() UpperCAmelCase__ : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCAmelCase__ : int = os.path.join(self.tmpdirname , _A ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_A ) + '''\n''' ) with open(self.feature_extraction_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_A ) + '''\n''' ) # load decoder from hub UpperCAmelCase__ : Any = '''hf-internal-testing/ngram-beam-search-decoder''' def lowercase_ ( self : int , **_A : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Dict = self.add_kwargs_tokens_map.copy() kwargs.update(_A ) return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname , **_A ) def lowercase_ ( self : str , **_A : Any ): '''simple docstring''' return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname , **_A ) def lowercase_ ( self : str , **_A : Any ): '''simple docstring''' return BeamSearchDecoderCTC.load_from_hf_hub(self.decoder_name , **_A ) def lowercase_ ( self : Any ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def lowercase_ ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase__ : Any = self.get_feature_extractor() UpperCAmelCase__ : Tuple = self.get_decoder() UpperCAmelCase__ : Tuple = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase__ : Union[str, Any] = WavaVecaProcessorWithLM.from_pretrained(self.tmpdirname ) # tokenizer self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , _A ) # feature extractor self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , _A ) # decoder self.assertEqual(processor.decoder._alphabet.labels , decoder._alphabet.labels ) self.assertEqual( processor.decoder.model_container[decoder._model_key]._unigram_set , decoder.model_container[decoder._model_key]._unigram_set , ) self.assertIsInstance(processor.decoder , _A ) def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = WavaVecaProcessorWithLM( tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) processor.save_pretrained(self.tmpdirname ) # make sure that error is thrown when decoder alphabet doesn't match UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM.from_pretrained( self.tmpdirname , alpha=5.0 , beta=3.0 , score_boundary=-7.0 , unk_score_offset=3 ) # decoder self.assertEqual(processor.language_model.alpha , 5.0 ) self.assertEqual(processor.language_model.beta , 3.0 ) self.assertEqual(processor.language_model.score_boundary , -7.0 ) self.assertEqual(processor.language_model.unk_score_offset , 3 ) def lowercase_ ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.get_tokenizer() # add token to trigger raise tokenizer.add_tokens(['''xx'''] ) with self.assertRaisesRegex(_A , '''include''' ): WavaVecaProcessorWithLM( tokenizer=_A , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Any = self.get_feature_extractor() UpperCAmelCase__ : Optional[int] = self.get_tokenizer() UpperCAmelCase__ : Any = self.get_decoder() UpperCAmelCase__ : Optional[Any] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : List[Any] = floats_list((3, 1_000) ) UpperCAmelCase__ : Dict = feature_extractor(_A , return_tensors='''np''' ) UpperCAmelCase__ : str = processor(_A , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def lowercase_ ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = self.get_feature_extractor() UpperCAmelCase__ : str = self.get_tokenizer() UpperCAmelCase__ : str = self.get_decoder() UpperCAmelCase__ : Union[str, Any] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : Union[str, Any] = '''This is a test string''' UpperCAmelCase__ : Optional[int] = processor(text=_A ) UpperCAmelCase__ : List[str] = tokenizer(_A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowercase_ ( self : Dict , _A : Optional[int]=(2, 10, 16) , _A : List[str]=77 ): '''simple docstring''' np.random.seed(_A ) return np.random.rand(*_A ) def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = self.get_feature_extractor() UpperCAmelCase__ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase__ : Optional[Any] = self.get_decoder() UpperCAmelCase__ : Tuple = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : int = self._get_dummy_logits(shape=(10, 16) , seed=13 ) UpperCAmelCase__ : List[Any] = processor.decode(_A ) UpperCAmelCase__ : List[Any] = decoder.decode_beams(_A )[0] self.assertEqual(decoded_decoder[0] , decoded_processor.text ) self.assertEqual('''</s> <s> </s>''' , decoded_processor.text ) self.assertEqual(decoded_decoder[-2] , decoded_processor.logit_score ) self.assertEqual(decoded_decoder[-1] , decoded_processor.lm_score ) @parameterized.expand([[None], ['''fork'''], ['''spawn''']] ) def lowercase_ ( self : Any , _A : str ): '''simple docstring''' UpperCAmelCase__ : Any = self.get_feature_extractor() UpperCAmelCase__ : Tuple = self.get_tokenizer() UpperCAmelCase__ : Tuple = self.get_decoder() UpperCAmelCase__ : Any = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : Optional[Any] = self._get_dummy_logits() # note: pool should be instantiated *after* Wav2Vec2ProcessorWithLM. # otherwise, the LM won't be available to the pool's sub-processes. # manual logic used to allow parameterized test for both pool=None and pool=Pool(...) if pool_context is None: UpperCAmelCase__ : Union[str, Any] = processor.batch_decode(_A ) else: with get_context(_A ).Pool() as pool: UpperCAmelCase__ : Union[str, Any] = processor.batch_decode(_A , _A ) UpperCAmelCase__ : str = list(_A ) with get_context('''fork''' ).Pool() as p: UpperCAmelCase__ : Dict = decoder.decode_beams_batch(_A , _A ) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ : Dict = [], [], [] for beams in decoded_beams: texts_decoder.append(beams[0][0] ) logit_scores_decoder.append(beams[0][-2] ) lm_scores_decoder.append(beams[0][-1] ) self.assertListEqual(_A , decoded_processor.text ) self.assertListEqual(['''<s> <s> </s>''', '''<s> <s> <s>'''] , decoded_processor.text ) self.assertListEqual(_A , decoded_processor.logit_score ) self.assertListEqual(_A , decoded_processor.lm_score ) def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : str = self.get_feature_extractor() UpperCAmelCase__ : List[Any] = self.get_tokenizer() UpperCAmelCase__ : int = self.get_decoder() UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : str = self._get_dummy_logits() UpperCAmelCase__ : Optional[int] = 15 UpperCAmelCase__ : Dict = -2_0.0 UpperCAmelCase__ : Optional[Any] = -4.0 UpperCAmelCase__ : Union[str, Any] = processor.batch_decode( _A , beam_width=_A , beam_prune_logp=_A , token_min_logp=_A , ) UpperCAmelCase__ : List[Any] = decoded_processor_out.text UpperCAmelCase__ : List[str] = list(_A ) with get_context('''fork''' ).Pool() as pool: UpperCAmelCase__ : Tuple = decoder.decode_beams_batch( _A , _A , beam_width=_A , beam_prune_logp=_A , token_min_logp=_A , ) UpperCAmelCase__ : Optional[int] = [d[0][0] for d in decoded_decoder_out] UpperCAmelCase__ : Optional[Any] = [d[0][2] for d in decoded_decoder_out] UpperCAmelCase__ : Optional[int] = [d[0][3] for d in decoded_decoder_out] self.assertListEqual(_A , _A ) self.assertListEqual(['''</s> <s> <s>''', '''<s> <s> <s>'''] , _A ) self.assertTrue(np.array_equal(_A , decoded_processor_out.logit_score ) ) self.assertTrue(np.allclose([-2_0.0_5_4, -1_8.4_4_7] , _A , atol=1e-3 ) ) self.assertTrue(np.array_equal(_A , decoded_processor_out.lm_score ) ) self.assertTrue(np.allclose([-1_5.5_5_4, -1_3.9_4_7_4] , _A , atol=1e-3 ) ) def lowercase_ ( self : str ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.get_feature_extractor() UpperCAmelCase__ : Optional[Any] = self.get_tokenizer() UpperCAmelCase__ : Dict = self.get_decoder() UpperCAmelCase__ : int = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : Optional[int] = self._get_dummy_logits() UpperCAmelCase__ : List[str] = 2.0 UpperCAmelCase__ : Union[str, Any] = 5.0 UpperCAmelCase__ : str = -2_0.0 UpperCAmelCase__ : Optional[int] = True UpperCAmelCase__ : Union[str, Any] = processor.batch_decode( _A , alpha=_A , beta=_A , unk_score_offset=_A , lm_score_boundary=_A , ) UpperCAmelCase__ : Union[str, Any] = decoded_processor_out.text UpperCAmelCase__ : Tuple = list(_A ) decoder.reset_params( alpha=_A , beta=_A , unk_score_offset=_A , lm_score_boundary=_A , ) with get_context('''fork''' ).Pool() as pool: UpperCAmelCase__ : Optional[Any] = decoder.decode_beams_batch( _A , _A , ) UpperCAmelCase__ : str = [d[0][0] for d in decoded_decoder_out] self.assertListEqual(_A , _A ) self.assertListEqual(['''<s> </s> <s> </s> </s>''', '''</s> </s> <s> </s> </s>'''] , _A ) UpperCAmelCase__ : Optional[Any] = processor.decoder.model_container[processor.decoder._model_key] self.assertEqual(lm_model.alpha , 2.0 ) self.assertEqual(lm_model.beta , 5.0 ) self.assertEqual(lm_model.unk_score_offset , -2_0.0 ) self.assertEqual(lm_model.score_boundary , _A ) def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Dict = processor.decoder.model_container[processor.decoder._model_key] UpperCAmelCase__ : Optional[int] = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() UpperCAmelCase__ : Dict = os.listdir(_A ) UpperCAmelCase__ : Optional[Any] = ['''alphabet.json''', '''language_model'''] downloaded_decoder_files.sort() expected_decoder_files.sort() # test that only decoder relevant files from # https://huggingface.co/hf-internal-testing/processor_with_lm/tree/main # are downloaded and none of the rest (e.g. README.md, ...) self.assertListEqual(_A , _A ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : str = snapshot_download('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Any = WavaVecaProcessorWithLM.from_pretrained(_A ) UpperCAmelCase__ : Optional[int] = processor.decoder.model_container[processor.decoder._model_key] UpperCAmelCase__ : str = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() UpperCAmelCase__ : List[str] = os.listdir(_A ) UpperCAmelCase__ : Any = os.listdir(_A ) local_decoder_files.sort() expected_decoder_files.sort() # test that both decoder form hub and local files in cache are the same self.assertListEqual(_A , _A ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Dict = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Dict = AutoProcessor.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Tuple = floats_list((3, 1_000) ) UpperCAmelCase__ : int = processor_wavaveca(_A , return_tensors='''np''' ) UpperCAmelCase__ : List[str] = processor_auto(_A , return_tensors='''np''' ) for key in input_wavaveca.keys(): self.assertAlmostEqual(input_wavaveca[key].sum() , input_auto[key].sum() , delta=1e-2 ) UpperCAmelCase__ : Tuple = self._get_dummy_logits() UpperCAmelCase__ : List[str] = processor_wavaveca.batch_decode(_A ) UpperCAmelCase__ : int = processor_auto.batch_decode(_A ) self.assertListEqual(decoded_wavaveca.text , decoded_auto.text ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : int = self.get_feature_extractor() UpperCAmelCase__ : int = self.get_tokenizer() UpperCAmelCase__ : Optional[Any] = self.get_decoder() UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) self.assertListEqual( processor.model_input_names , feature_extractor.model_input_names , msg='''`processor` and `feature_extractor` model input names do not match''' , ) @staticmethod def lowercase_ ( _A : Dict , _A : str ): '''simple docstring''' UpperCAmelCase__ : int = [d[key] for d in offsets] return retrieved_list def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : str = self._get_dummy_logits()[0] UpperCAmelCase__ : List[str] = processor.decode(_A , output_word_offsets=_A ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(_A , _A ) ) self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) ) , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''end_offset''' ) , [1, 3, 5] ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Any = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Dict = self._get_dummy_logits() UpperCAmelCase__ : Dict = processor.batch_decode(_A , output_word_offsets=_A ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(_A , _A ) ) self.assertListEqual( [''' '''.join(self.get_from_offsets(_A , '''word''' ) ) for o in outputs['''word_offsets''']] , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''end_offset''' ) , [1, 3, 5] ) @slow @require_torch @require_torchaudio def lowercase_ ( self : Optional[Any] ): '''simple docstring''' import torch UpperCAmelCase__ : Any = load_dataset('''common_voice''' , '''en''' , split='''train''' , streaming=_A ) UpperCAmelCase__ : Dict = ds.cast_column('''audio''' , datasets.Audio(sampling_rate=16_000 ) ) UpperCAmelCase__ : List[Any] = iter(_A ) UpperCAmelCase__ : Optional[Any] = next(_A ) UpperCAmelCase__ : Any = AutoProcessor.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) UpperCAmelCase__ : int = WavaVecaForCTC.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) # compare to filename `common_voice_en_100038.mp3` of dataset viewer on https://huggingface.co/datasets/common_voice/viewer/en/train UpperCAmelCase__ : int = processor(sample['''audio''']['''array'''] , return_tensors='''pt''' ).input_values with torch.no_grad(): UpperCAmelCase__ : Dict = model(_A ).logits.cpu().numpy() UpperCAmelCase__ : int = processor.decode(logits[0] , output_word_offsets=_A ) UpperCAmelCase__ : Any = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate UpperCAmelCase__ : Any = [ { '''start_time''': d['''start_offset'''] * time_offset, '''end_time''': d['''end_offset'''] * time_offset, '''word''': d['''word'''], } for d in output['''word_offsets'''] ] UpperCAmelCase__ : int = '''WHY DOES MILISANDRA LOOK LIKE SHE WANTS TO CONSUME JOHN SNOW ON THE RIVER AT THE WALL''' # output words self.assertEqual(''' '''.join(self.get_from_offsets(_A , '''word''' ) ) , _A ) self.assertEqual(''' '''.join(self.get_from_offsets(_A , '''word''' ) ) , output.text ) # output times UpperCAmelCase__ : List[Any] = torch.tensor(self.get_from_offsets(_A , '''start_time''' ) ) UpperCAmelCase__ : List[str] = torch.tensor(self.get_from_offsets(_A , '''end_time''' ) ) # fmt: off UpperCAmelCase__ : int = torch.tensor([1.4_1_9_9, 1.6_5_9_9, 2.2_5_9_9, 3.0, 3.2_4, 3.5_9_9_9, 3.7_9_9_9, 4.0_9_9_9, 4.2_6, 4.9_4, 5.2_8, 5.6_5_9_9, 5.7_8, 5.9_4, 6.3_2, 6.5_3_9_9, 6.6_5_9_9] ) UpperCAmelCase__ : List[str] = torch.tensor([1.5_3_9_9, 1.8_9_9_9, 2.9, 3.1_6, 3.5_3_9_9, 3.7_2, 4.0_1_9_9, 4.1_7_9_9, 4.7_6, 5.1_5_9_9, 5.5_5_9_9, 5.6_9_9_9, 5.8_6, 6.1_9_9_9, 6.3_8, 6.6_1_9_9, 6.9_4] ) # fmt: on self.assertTrue(torch.allclose(_A , _A , atol=0.0_1 ) ) self.assertTrue(torch.allclose(_A , _A , atol=0.0_1 ) )
299
0
from ...configuration_utils import PretrainedConfig from ...utils import logging A : str = logging.get_logger(__name__) A : Dict = { 'vinvino02/glpn-kitti': 'https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json', # See all GLPN models at https://huggingface.co/models?filter=glpn } class __A( a ): snake_case_ = '''glpn''' def __init__( self , _snake_case=3 , _snake_case=4 , _snake_case=[2, 2, 2, 2] , _snake_case=[8, 4, 2, 1] , _snake_case=[32, 64, 160, 256] , _snake_case=[7, 3, 3, 3] , _snake_case=[4, 2, 2, 2] , _snake_case=[1, 2, 5, 8] , _snake_case=[4, 4, 4, 4] , _snake_case="gelu" , _snake_case=0.0 , _snake_case=0.0 , _snake_case=0.02 , _snake_case=0.1 , _snake_case=1E-6 , _snake_case=64 , _snake_case=10 , _snake_case=-1 , **_snake_case , ) -> List[Any]: '''simple docstring''' super().__init__(**_snake_case ) __a = num_channels __a = num_encoder_blocks __a = depths __a = sr_ratios __a = hidden_sizes __a = patch_sizes __a = strides __a = mlp_ratios __a = num_attention_heads __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = initializer_range __a = drop_path_rate __a = layer_norm_eps __a = decoder_hidden_size __a = max_depth __a = head_in_index
6
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( 'stable diffusion controlnet', '0.22.0', 'Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.', standard_warn=False, stacklevel=3, )
6
1
import unittest import torch from torch import nn from diffusers.models.activations import get_activation class _SCREAMING_SNAKE_CASE( unittest.TestCase ): def _UpperCamelCase ( self ) -> Optional[int]: """simple docstring""" __SCREAMING_SNAKE_CASE :Tuple = get_activation('''swish''' ) self.assertIsInstance(lowerCAmelCase__ ,nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 ,dtype=torch.floataa ) ).item() ,0 ) self.assertNotEqual(act(torch.tensor(-1 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(0 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(20 ,dtype=torch.floataa ) ).item() ,20 ) def _UpperCamelCase ( self ) -> List[Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :Dict = get_activation('''silu''' ) self.assertIsInstance(lowerCAmelCase__ ,nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 ,dtype=torch.floataa ) ).item() ,0 ) self.assertNotEqual(act(torch.tensor(-1 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(0 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(20 ,dtype=torch.floataa ) ).item() ,20 ) def _UpperCamelCase ( self ) -> List[str]: """simple docstring""" __SCREAMING_SNAKE_CASE :Tuple = get_activation('''mish''' ) self.assertIsInstance(lowerCAmelCase__ ,nn.Mish ) self.assertEqual(act(torch.tensor(-2_00 ,dtype=torch.floataa ) ).item() ,0 ) self.assertNotEqual(act(torch.tensor(-1 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(0 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(20 ,dtype=torch.floataa ) ).item() ,20 ) def _UpperCamelCase ( self ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE :str = get_activation('''gelu''' ) self.assertIsInstance(lowerCAmelCase__ ,nn.GELU ) self.assertEqual(act(torch.tensor(-1_00 ,dtype=torch.floataa ) ).item() ,0 ) self.assertNotEqual(act(torch.tensor(-1 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(0 ,dtype=torch.floataa ) ).item() ,0 ) self.assertEqual(act(torch.tensor(20 ,dtype=torch.floataa ) ).item() ,20 )
367
"""simple docstring""" import math import unittest def __lowerCamelCase ( a_ : int ) -> bool: assert isinstance(a_ , a_ ) and ( number >= 0 ), "'number' must been an int and positive" 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 class _SCREAMING_SNAKE_CASE( unittest.TestCase ): def _UpperCamelCase ( self ) -> Union[str, Any]: """simple docstring""" self.assertTrue(is_prime(2 ) ) self.assertTrue(is_prime(3 ) ) self.assertTrue(is_prime(5 ) ) self.assertTrue(is_prime(7 ) ) self.assertTrue(is_prime(11 ) ) self.assertTrue(is_prime(13 ) ) self.assertTrue(is_prime(17 ) ) self.assertTrue(is_prime(19 ) ) self.assertTrue(is_prime(23 ) ) self.assertTrue(is_prime(29 ) ) def _UpperCamelCase ( self ) -> Tuple: """simple docstring""" with self.assertRaises(SCREAMING_SNAKE_CASE__ ): is_prime(-19 ) self.assertFalse( is_prime(0 ) ,'''Zero doesn\'t have any positive factors, primes must have exactly two.''' ,) self.assertFalse( is_prime(1 ) ,'''One only has 1 positive factor, primes must have exactly two.''' ,) self.assertFalse(is_prime(2 * 2 ) ) self.assertFalse(is_prime(2 * 3 ) ) self.assertFalse(is_prime(3 * 3 ) ) self.assertFalse(is_prime(3 * 5 ) ) self.assertFalse(is_prime(3 * 5 * 7 ) ) if __name__ == "__main__": unittest.main()
239
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL _lowerCAmelCase = logging.get_logger(__name__) def __lowerCAmelCase ( snake_case__ , snake_case__ ): __UpperCamelCase : Dict = b.T __UpperCamelCase : Optional[Any] = np.sum(np.square(snake_case__ ) , axis=1 ) __UpperCamelCase : Tuple = np.sum(np.square(snake_case__ ) , axis=0 ) __UpperCamelCase : Optional[int] = np.matmul(snake_case__ , snake_case__ ) __UpperCamelCase : Optional[Any] = aa[:, None] - 2 * ab + ba[None, :] return d def __lowerCAmelCase ( snake_case__ , snake_case__ ): __UpperCamelCase : List[str] = x.reshape(-1 , 3 ) __UpperCamelCase : Optional[Any] = squared_euclidean_distance(snake_case__ , snake_case__ ) return np.argmin(snake_case__ , axis=1 ) class A ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' A = ["pixel_values"] def __init__(self , _UpperCAmelCase = None , _UpperCAmelCase = True , _UpperCAmelCase = None , _UpperCAmelCase = PILImageResampling.BILINEAR , _UpperCAmelCase = True , _UpperCAmelCase = True , **_UpperCAmelCase , ) -> None: super().__init__(**_UpperCAmelCase ) __UpperCamelCase : Tuple = size if size is not None else {"height": 2_5_6, "width": 2_5_6} __UpperCamelCase : int = get_size_dict(_UpperCAmelCase ) __UpperCamelCase : Union[str, Any] = np.array(_UpperCAmelCase ) if clusters is not None else None __UpperCamelCase : str = do_resize __UpperCamelCase : List[Any] = size __UpperCamelCase : Union[str, Any] = resample __UpperCamelCase : Dict = do_normalize __UpperCamelCase : str = do_color_quantize def a_ (self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = PILImageResampling.BILINEAR , _UpperCAmelCase = None , **_UpperCAmelCase , ) -> np.ndarray: __UpperCamelCase : Any = get_size_dict(_UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f"Size dictionary must contain both height and width keys. Got {size.keys()}" ) return resize( _UpperCAmelCase , size=(size["height"], size["width"]) , resample=_UpperCAmelCase , data_format=_UpperCAmelCase , **_UpperCAmelCase ) def a_ (self , _UpperCAmelCase , _UpperCAmelCase = None , ) -> np.ndarray: __UpperCamelCase : Any = rescale(image=_UpperCAmelCase , scale=1 / 127.5 , data_format=_UpperCAmelCase ) __UpperCamelCase : str = image - 1 return image def a_ (self , _UpperCAmelCase , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = ChannelDimension.FIRST , **_UpperCAmelCase , ) -> PIL.Image.Image: __UpperCamelCase : List[Any] = do_resize if do_resize is not None else self.do_resize __UpperCamelCase : List[Any] = size if size is not None else self.size __UpperCamelCase : List[Any] = get_size_dict(_UpperCAmelCase ) __UpperCamelCase : str = resample if resample is not None else self.resample __UpperCamelCase : Dict = do_normalize if do_normalize is not None else self.do_normalize __UpperCamelCase : List[str] = do_color_quantize if do_color_quantize is not None else self.do_color_quantize __UpperCamelCase : Any = clusters if clusters is not None else self.clusters __UpperCamelCase : Optional[int] = np.array(_UpperCAmelCase ) __UpperCamelCase : Union[str, Any] = make_list_of_images(_UpperCAmelCase ) if not valid_images(_UpperCAmelCase ): 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_color_quantize and clusters is None: raise ValueError("Clusters must be specified if do_color_quantize is True." ) # All transformations expect numpy arrays. __UpperCamelCase : Tuple = [to_numpy_array(_UpperCAmelCase ) for image in images] if do_resize: __UpperCamelCase : List[Any] = [self.resize(image=_UpperCAmelCase , size=_UpperCAmelCase , resample=_UpperCAmelCase ) for image in images] if do_normalize: __UpperCamelCase : Dict = [self.normalize(image=_UpperCAmelCase ) for image in images] if do_color_quantize: __UpperCamelCase : Any = [to_channel_dimension_format(_UpperCAmelCase , ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) __UpperCamelCase : Optional[Any] = np.array(_UpperCAmelCase ) __UpperCamelCase : Optional[Any] = color_quantize(_UpperCAmelCase , _UpperCAmelCase ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) __UpperCamelCase : Any = images.shape[0] __UpperCamelCase : Union[str, Any] = images.reshape(_UpperCAmelCase , -1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. __UpperCamelCase : Any = list(_UpperCAmelCase ) else: __UpperCamelCase : Optional[int] = [to_channel_dimension_format(_UpperCAmelCase , _UpperCAmelCase ) for image in images] __UpperCamelCase : Union[str, Any] = {"input_ids": images} return BatchFeature(data=_UpperCAmelCase , tensor_type=_UpperCAmelCase )
298
'''simple docstring''' import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) _lowerCAmelCase = logging.getLogger() def __lowerCAmelCase ( ): __UpperCamelCase : List[str] = argparse.ArgumentParser() parser.add_argument("-f" ) __UpperCamelCase : Any = parser.parse_args() return args.f def __lowerCAmelCase ( snake_case__ ): __UpperCamelCase : Dict = {} __UpperCamelCase : Dict = os.path.join(snake_case__ , "all_results.json" ) if os.path.exists(snake_case__ ): with open(snake_case__ , "r" ) as f: __UpperCamelCase : Any = json.load(snake_case__ ) else: raise ValueError(F"can't find {path}" ) return results def __lowerCAmelCase ( ): __UpperCamelCase : Any = torch.cuda.is_available() and torch_device == "cuda" return is_using_cuda and is_apex_available() _lowerCAmelCase = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class A ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' @classmethod def a_ (cls ) -> Union[str, Any]: # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU __UpperCamelCase : Optional[Any] = tempfile.mkdtemp() __UpperCamelCase : List[str] = os.path.join(cls.tmpdir , "default_config.yml" ) write_basic_config(save_location=cls.configPath ) __UpperCamelCase : Optional[Any] = ["accelerate", "launch", "--config_file", cls.configPath] @classmethod def a_ (cls ) -> Union[str, Any]: shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Optional[int]: __UpperCamelCase : List[Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase : List[Any] = f"\n {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --seed=42\n --checkpointing_steps epoch\n --with_tracking\n ".split() if is_cuda_and_apex_available(): testargs.append("--fp16" ) run_command(self._launch_args + testargs ) __UpperCamelCase : Tuple = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.75 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "glue_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Dict: __UpperCamelCase : Optional[Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase : List[Any] = f"\n {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --block_size 128\n --per_device_train_batch_size 5\n --per_device_eval_batch_size 5\n --num_train_epochs 2\n --output_dir {tmp_dir}\n --checkpointing_steps epoch\n --with_tracking\n ".split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) __UpperCamelCase : int = get_results(_UpperCAmelCase ) self.assertLess(result["perplexity"] , 1_0_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "clm_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Any: __UpperCamelCase : List[Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase : Optional[Any] = f"\n {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py\n --model_name_or_path distilroberta-base\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --output_dir {tmp_dir}\n --num_train_epochs=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) __UpperCamelCase : Optional[Any] = get_results(_UpperCAmelCase ) self.assertLess(result["perplexity"] , 4_2 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "mlm_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> int: # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu __UpperCamelCase : int = 7 if get_gpu_count() > 1 else 2 __UpperCamelCase : int = self.get_auto_remove_tmp_dir() __UpperCamelCase : str = f"\n {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/conll/sample.json\n --validation_file tests/fixtures/tests_samples/conll/sample.json\n --output_dir {tmp_dir}\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) __UpperCamelCase : List[Any] = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.75 ) self.assertLess(result["train_loss"] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "ner_no_trainer" ) ) ) @unittest.skip(reason="Fix me @muellerzr" ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Any: __UpperCamelCase : Tuple = self.get_auto_remove_tmp_dir() __UpperCamelCase : str = f"\n {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py\n --model_name_or_path bert-base-uncased\n --version_2_with_negative\n --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n --output_dir {tmp_dir}\n --seed=42\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) __UpperCamelCase : Optional[int] = get_results(_UpperCAmelCase ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result["eval_f1"] , 2_8 ) self.assertGreaterEqual(result["eval_exact"] , 2_8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "qa_no_trainer" ) ) ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Dict: __UpperCamelCase : Tuple = self.get_auto_remove_tmp_dir() __UpperCamelCase : List[str] = f"\n {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/swag/sample.json\n --validation_file tests/fixtures/tests_samples/swag/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=20\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) __UpperCamelCase : Tuple = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "swag_no_trainer" ) ) ) @slow @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Union[str, Any]: __UpperCamelCase : str = self.get_auto_remove_tmp_dir() __UpperCamelCase : Dict = f"\n {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py\n --model_name_or_path t5-small\n --train_file tests/fixtures/tests_samples/xsum/sample.json\n --validation_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) __UpperCamelCase : Dict = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["eval_rouge1"] , 1_0 ) self.assertGreaterEqual(result["eval_rouge2"] , 2 ) self.assertGreaterEqual(result["eval_rougeL"] , 7 ) self.assertGreaterEqual(result["eval_rougeLsum"] , 7 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "summarization_no_trainer" ) ) ) @slow @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Tuple: __UpperCamelCase : Optional[int] = self.get_auto_remove_tmp_dir() __UpperCamelCase : List[Any] = f"\n {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py\n --model_name_or_path sshleifer/student_marian_en_ro_6_1\n --source_lang en\n --target_lang ro\n --train_file tests/fixtures/tests_samples/wmt16/sample.json\n --validation_file tests/fixtures/tests_samples/wmt16/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --num_beams=6\n --learning_rate=3e-3\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --source_lang en_XX\n --target_lang ro_RO\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) __UpperCamelCase : List[Any] = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["eval_bleu"] , 3_0 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "epoch_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "translation_no_trainer" ) ) ) @slow def a_ (self ) -> List[Any]: __UpperCamelCase : Tuple = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCAmelCase ) __UpperCamelCase : Dict = self.get_auto_remove_tmp_dir() __UpperCamelCase : List[Any] = f"\n {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py\n --dataset_name huggingface/semantic-segmentation-test-sample\n --output_dir {tmp_dir}\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n ".split() run_command(self._launch_args + testargs ) __UpperCamelCase : Optional[int] = get_results(_UpperCAmelCase ) self.assertGreaterEqual(result["eval_overall_accuracy"] , 0.10 ) @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def a_ (self ) -> Tuple: __UpperCamelCase : List[Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase : Optional[Any] = f"\n {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py\n --model_name_or_path google/vit-base-patch16-224-in21k\n --dataset_name hf-internal-testing/cats_vs_dogs_sample\n --learning_rate 1e-4\n --per_device_train_batch_size 2\n --per_device_eval_batch_size 1\n --max_train_steps 2\n --train_val_split 0.1\n --seed 42\n --output_dir {tmp_dir}\n --with_tracking\n --checkpointing_steps 1\n ".split() if is_cuda_and_apex_available(): testargs.append("--fp16" ) run_command(self._launch_args + testargs ) __UpperCamelCase : str = get_results(_UpperCAmelCase ) # The base model scores a 25% self.assertGreaterEqual(result["eval_accuracy"] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "step_1" ) ) ) self.assertTrue(os.path.exists(os.path.join(_UpperCAmelCase , "image_classification_no_trainer" ) ) )
298
1
"""simple docstring""" import argparse import math import traceback import dateutil.parser as date_parser import requests def lowerCamelCase ( _UpperCamelCase : Tuple ) -> Union[str, Any]: '''simple docstring''' __UpperCAmelCase : Union[str, Any] = {} __UpperCAmelCase : str = job["""started_at"""] __UpperCAmelCase : str = job["""completed_at"""] __UpperCAmelCase : Tuple = date_parser.parse(snake_case__ ) __UpperCAmelCase : int = date_parser.parse(snake_case__ ) __UpperCAmelCase : Any = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __UpperCAmelCase : Any = start __UpperCAmelCase : Dict = end __UpperCAmelCase : str = duration_in_min return job_info def lowerCamelCase ( _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any]=None ) -> List[Any]: '''simple docstring''' __UpperCAmelCase : List[Any] = None if token is not None: __UpperCAmelCase : Any = {"""Accept""": """application/vnd.github+json""", """Authorization""": f'''Bearer {token}'''} __UpperCAmelCase : int = f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __UpperCAmelCase : Dict = requests.get(snake_case__ , headers=snake_case__ ).json() __UpperCAmelCase : Dict = {} try: job_time.update({job["""name"""]: extract_time_from_single_job(snake_case__ ) for job in result["""jobs"""]} ) __UpperCAmelCase : Union[str, Any] = math.ceil((result["""total_count"""] - 1_0_0) / 1_0_0 ) for i in range(snake_case__ ): __UpperCAmelCase : List[str] = requests.get(url + f'''&page={i + 2}''' , headers=snake_case__ ).json() job_time.update({job["""name"""]: extract_time_from_single_job(snake_case__ ) for job in result["""jobs"""]} ) return job_time except Exception: print(f'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": UpperCAmelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') UpperCAmelCase : Any = parser.parse_args() UpperCAmelCase : Optional[int] = get_job_time(args.workflow_run_id) UpperCAmelCase : List[str] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(F"{k}: {v['duration']}")
365
"""simple docstring""" import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase : List[str] = logging.get_logger(__name__) UpperCAmelCase : List[str] = { 'b0': efficientnet.EfficientNetBa, 'b1': efficientnet.EfficientNetBa, 'b2': efficientnet.EfficientNetBa, 'b3': efficientnet.EfficientNetBa, 'b4': efficientnet.EfficientNetBa, 'b5': efficientnet.EfficientNetBa, 'b6': efficientnet.EfficientNetBa, 'b7': efficientnet.EfficientNetBa, } UpperCAmelCase : List[str] = { 'b0': { 'hidden_dim': 1280, 'width_coef': 1.0, 'depth_coef': 1.0, 'image_size': 224, 'dropout_rate': 0.2, 'dw_padding': [], }, 'b1': { 'hidden_dim': 1280, 'width_coef': 1.0, 'depth_coef': 1.1, 'image_size': 240, 'dropout_rate': 0.2, 'dw_padding': [16], }, 'b2': { 'hidden_dim': 1408, 'width_coef': 1.1, 'depth_coef': 1.2, 'image_size': 260, 'dropout_rate': 0.3, 'dw_padding': [5, 8, 16], }, 'b3': { 'hidden_dim': 1536, 'width_coef': 1.2, 'depth_coef': 1.4, 'image_size': 300, 'dropout_rate': 0.3, 'dw_padding': [5, 18], }, 'b4': { 'hidden_dim': 1792, 'width_coef': 1.4, 'depth_coef': 1.8, 'image_size': 380, 'dropout_rate': 0.4, 'dw_padding': [6], }, 'b5': { 'hidden_dim': 2048, 'width_coef': 1.6, 'depth_coef': 2.2, 'image_size': 456, 'dropout_rate': 0.4, 'dw_padding': [13, 27], }, 'b6': { 'hidden_dim': 2304, 'width_coef': 1.8, 'depth_coef': 2.6, 'image_size': 528, 'dropout_rate': 0.5, 'dw_padding': [31], }, 'b7': { 'hidden_dim': 2560, 'width_coef': 2.0, 'depth_coef': 3.1, 'image_size': 600, 'dropout_rate': 0.5, 'dw_padding': [18], }, } def lowerCamelCase ( _UpperCamelCase : List[Any] ) -> Optional[int]: '''simple docstring''' __UpperCAmelCase : List[Any] = EfficientNetConfig() __UpperCAmelCase : Dict = CONFIG_MAP[model_name]["""hidden_dim"""] __UpperCAmelCase : Dict = CONFIG_MAP[model_name]["""width_coef"""] __UpperCAmelCase : str = CONFIG_MAP[model_name]["""depth_coef"""] __UpperCAmelCase : Dict = CONFIG_MAP[model_name]["""image_size"""] __UpperCAmelCase : Dict = CONFIG_MAP[model_name]["""dropout_rate"""] __UpperCAmelCase : Union[str, Any] = CONFIG_MAP[model_name]["""dw_padding"""] __UpperCAmelCase : int = """huggingface/label-files""" __UpperCAmelCase : Optional[int] = """imagenet-1k-id2label.json""" __UpperCAmelCase : str = 1_0_0_0 __UpperCAmelCase : Dict = json.load(open(hf_hub_download(_UpperCamelCase , _UpperCamelCase , repo_type="""dataset""" ) , """r""" ) ) __UpperCAmelCase : int = {int(_UpperCamelCase ): v for k, v in idalabel.items()} __UpperCAmelCase : Dict = idalabel __UpperCAmelCase : Tuple = {v: k for k, v in idalabel.items()} return config def lowerCamelCase ( ) -> Union[str, Any]: '''simple docstring''' __UpperCAmelCase : Dict = """http://images.cocodataset.org/val2017/000000039769.jpg""" __UpperCAmelCase : Optional[Any] = Image.open(requests.get(_UpperCamelCase , stream=_UpperCamelCase ).raw ) return im def lowerCamelCase ( _UpperCamelCase : Any ) -> str: '''simple docstring''' __UpperCAmelCase : Tuple = CONFIG_MAP[model_name]["""image_size"""] __UpperCAmelCase : List[str] = EfficientNetImageProcessor( size={"""height""": size, """width""": size} , image_mean=[0.485, 0.456, 0.406] , image_std=[0.47_853_944, 0.4_732_864, 0.47_434_163] , do_center_crop=_UpperCamelCase , ) return preprocessor def lowerCamelCase ( _UpperCamelCase : Dict ) -> Optional[int]: '''simple docstring''' __UpperCAmelCase : Union[str, Any] = [v.split("""_""" )[0].split("""block""" )[1] for v in original_param_names if v.startswith("""block""" )] __UpperCAmelCase : str = sorted(set(_UpperCamelCase ) ) __UpperCAmelCase : Optional[int] = len(_UpperCamelCase ) __UpperCAmelCase : Any = {b: str(_UpperCamelCase ) for b, i in zip(_UpperCamelCase , range(_UpperCamelCase ) )} __UpperCAmelCase : Any = [] rename_keys.append(("""stem_conv/kernel:0""", """embeddings.convolution.weight""") ) rename_keys.append(("""stem_bn/gamma:0""", """embeddings.batchnorm.weight""") ) rename_keys.append(("""stem_bn/beta:0""", """embeddings.batchnorm.bias""") ) rename_keys.append(("""stem_bn/moving_mean:0""", """embeddings.batchnorm.running_mean""") ) rename_keys.append(("""stem_bn/moving_variance:0""", """embeddings.batchnorm.running_var""") ) for b in block_names: __UpperCAmelCase : List[str] = block_name_mapping[b] rename_keys.append((f'''block{b}_expand_conv/kernel:0''', f'''encoder.blocks.{hf_b}.expansion.expand_conv.weight''') ) rename_keys.append((f'''block{b}_expand_bn/gamma:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.weight''') ) rename_keys.append((f'''block{b}_expand_bn/beta:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.bias''') ) rename_keys.append( (f'''block{b}_expand_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_mean''') ) rename_keys.append( (f'''block{b}_expand_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_var''') ) rename_keys.append( (f'''block{b}_dwconv/depthwise_kernel:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight''') ) rename_keys.append((f'''block{b}_bn/gamma:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight''') ) rename_keys.append((f'''block{b}_bn/beta:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias''') ) rename_keys.append( (f'''block{b}_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean''') ) rename_keys.append( (f'''block{b}_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var''') ) rename_keys.append((f'''block{b}_se_reduce/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.weight''') ) rename_keys.append((f'''block{b}_se_reduce/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.bias''') ) rename_keys.append((f'''block{b}_se_expand/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.weight''') ) rename_keys.append((f'''block{b}_se_expand/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.bias''') ) rename_keys.append( (f'''block{b}_project_conv/kernel:0''', f'''encoder.blocks.{hf_b}.projection.project_conv.weight''') ) rename_keys.append((f'''block{b}_project_bn/gamma:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.weight''') ) rename_keys.append((f'''block{b}_project_bn/beta:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.bias''') ) rename_keys.append( (f'''block{b}_project_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_mean''') ) rename_keys.append( (f'''block{b}_project_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_var''') ) rename_keys.append(("""top_conv/kernel:0""", """encoder.top_conv.weight""") ) rename_keys.append(("""top_bn/gamma:0""", """encoder.top_bn.weight""") ) rename_keys.append(("""top_bn/beta:0""", """encoder.top_bn.bias""") ) rename_keys.append(("""top_bn/moving_mean:0""", """encoder.top_bn.running_mean""") ) rename_keys.append(("""top_bn/moving_variance:0""", """encoder.top_bn.running_var""") ) __UpperCAmelCase : Optional[int] = {} for item in rename_keys: if item[0] in original_param_names: __UpperCAmelCase : Optional[Any] = """efficientnet.""" + item[1] __UpperCAmelCase : Tuple = """classifier.weight""" __UpperCAmelCase : Optional[int] = """classifier.bias""" return key_mapping def lowerCamelCase ( _UpperCamelCase : Any , _UpperCamelCase : Dict , _UpperCamelCase : int ) -> Tuple: '''simple docstring''' for key, value in tf_params.items(): if "normalization" in key: continue __UpperCAmelCase : List[Any] = key_mapping[key] if "_conv" in key and "kernel" in key: __UpperCAmelCase : int = torch.from_numpy(_UpperCamelCase ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: __UpperCAmelCase : Optional[Any] = torch.from_numpy(_UpperCamelCase ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: __UpperCAmelCase : List[str] = torch.from_numpy(np.transpose(_UpperCamelCase ) ) else: __UpperCAmelCase : Tuple = torch.from_numpy(_UpperCamelCase ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_UpperCamelCase ) @torch.no_grad() def lowerCamelCase ( _UpperCamelCase : Optional[int] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[str] ) -> Tuple: '''simple docstring''' __UpperCAmelCase : int = model_classes[model_name]( include_top=_UpperCamelCase , weights="""imagenet""" , input_tensor=_UpperCamelCase , input_shape=_UpperCamelCase , pooling=_UpperCamelCase , classes=1_0_0_0 , classifier_activation="""softmax""" , ) __UpperCAmelCase : List[str] = original_model.trainable_variables __UpperCAmelCase : List[Any] = original_model.non_trainable_variables __UpperCAmelCase : Union[str, Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: __UpperCAmelCase : int = param.numpy() __UpperCAmelCase : Dict = list(tf_params.keys() ) # Load HuggingFace model __UpperCAmelCase : Optional[Any] = get_efficientnet_config(_UpperCamelCase ) __UpperCAmelCase : Optional[Any] = EfficientNetForImageClassification(_UpperCamelCase ).eval() __UpperCAmelCase : Any = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print("""Converting parameters...""" ) __UpperCAmelCase : Tuple = rename_keys(_UpperCamelCase ) replace_params(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # Initialize preprocessor and preprocess input image __UpperCAmelCase : List[Any] = convert_image_processor(_UpperCamelCase ) __UpperCAmelCase : Union[str, Any] = preprocessor(images=prepare_img() , return_tensors="""pt""" ) # HF model inference hf_model.eval() with torch.no_grad(): __UpperCAmelCase : Optional[int] = hf_model(**_UpperCamelCase ) __UpperCAmelCase : Any = outputs.logits.detach().numpy() # Original model inference __UpperCAmelCase : Union[str, Any] = False __UpperCAmelCase : Dict = CONFIG_MAP[model_name]["""image_size"""] __UpperCAmelCase : str = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) __UpperCAmelCase : Optional[Any] = image.img_to_array(_UpperCamelCase ) __UpperCAmelCase : Tuple = np.expand_dims(_UpperCamelCase , axis=0 ) __UpperCAmelCase : str = original_model.predict(_UpperCamelCase ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_UpperCamelCase , _UpperCamelCase , atol=1E-3 ), "The predicted logits are not the same." print("""Model outputs match!""" ) if save_model: # Create folder to save model if not os.path.isdir(_UpperCamelCase ): os.mkdir(_UpperCamelCase ) # Save converted model and image processor hf_model.save_pretrained(_UpperCamelCase ) preprocessor.save_pretrained(_UpperCamelCase ) if push_to_hub: # Push model and image processor to hub print(f'''Pushing converted {model_name} to the hub...''' ) __UpperCAmelCase : List[str] = f'''efficientnet-{model_name}''' preprocessor.push_to_hub(_UpperCamelCase ) hf_model.push_to_hub(_UpperCamelCase ) if __name__ == "__main__": UpperCAmelCase : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='b0', type=str, help='Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].', ) parser.add_argument( '--pytorch_dump_folder_path', default='hf_model', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--save_model', action='store_true', help='Save model to local') parser.add_argument('--push_to_hub', action='store_true', help='Push model and image processor to the hub') UpperCAmelCase : Any = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
320
0
from math import ceil def lowerCamelCase_ ( UpperCamelCase__ : int = 1001 ) -> int: """simple docstring""" __lowerCamelCase = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): __lowerCamelCase = 2 * i + 1 __lowerCamelCase = 2 * i __lowerCamelCase = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: __A = int(sys.argv[1]) print(solution(n)) except ValueError: print("Invalid entry - please enter a number")
90
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
326
0
"""simple docstring""" import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class lowerCamelCase__ ( ctypes.Structure ): '''simple docstring''' _lowerCamelCase = [('''size''', ctypes.c_int), ('''visible''', ctypes.c_byte)] def _A ( ): """simple docstring""" if os.name == "nt": A = CursorInfo() A = ctypes.windll.kernelaa.GetStdHandle(-1_1 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(_a , ctypes.byref(_a ) ) A = False ctypes.windll.kernelaa.SetConsoleCursorInfo(_a , ctypes.byref(_a ) ) elif os.name == "posix": sys.stdout.write("""\033[?25l""" ) sys.stdout.flush() def _A ( ): """simple docstring""" if os.name == "nt": A = CursorInfo() A = ctypes.windll.kernelaa.GetStdHandle(-1_1 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(_a , ctypes.byref(_a ) ) A = True ctypes.windll.kernelaa.SetConsoleCursorInfo(_a , ctypes.byref(_a ) ) elif os.name == "posix": sys.stdout.write("""\033[?25h""" ) sys.stdout.flush() @contextmanager def _A ( ): """simple docstring""" try: hide_cursor() yield finally: show_cursor()
354
"""simple docstring""" from math import factorial def _A ( _a : int = 1_0_0 ): """simple docstring""" return sum(map(_a , str(factorial(_a ) ) ) ) if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
77
0
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 __a = True except (ImportError, AttributeError): __a = object def __lowercase ( *_UpperCamelCase, **_UpperCamelCase ) ->str: """simple docstring""" pass __a = False __a = logging.get_logger('''transformers-cli/serving''') def __lowercase ( _UpperCamelCase ) ->Optional[int]: """simple docstring""" lowercase : List[str] = pipeline( task=args.task, model=args.model if args.model else None, config=args.config, tokenizer=args.tokenizer, device=args.device, ) return ServeCommand(_UpperCamelCase, args.host, args.port, args.workers ) class __SCREAMING_SNAKE_CASE ( A__ ): A : dict class __SCREAMING_SNAKE_CASE ( A__ ): A : List[str] A : Optional[List[int]] class __SCREAMING_SNAKE_CASE ( A__ ): A : str class __SCREAMING_SNAKE_CASE ( A__ ): A : Any class __SCREAMING_SNAKE_CASE ( A__ ): @staticmethod def __lowerCamelCase ( SCREAMING_SNAKE_CASE__ ): lowercase : Tuple = parser.add_parser( '''serve''' , help='''CLI tool to run inference requests through REST and GraphQL endpoints.''' ) serve_parser.add_argument( '''--task''' , type=SCREAMING_SNAKE_CASE__ , choices=get_supported_tasks() , help='''The task to run the pipeline on''' , ) serve_parser.add_argument('''--host''' , type=SCREAMING_SNAKE_CASE__ , default='''localhost''' , help='''Interface the server will listen on.''' ) serve_parser.add_argument('''--port''' , type=SCREAMING_SNAKE_CASE__ , default=8888 , help='''Port the serving will listen to.''' ) serve_parser.add_argument('''--workers''' , type=SCREAMING_SNAKE_CASE__ , default=1 , help='''Number of http workers''' ) serve_parser.add_argument('''--model''' , type=SCREAMING_SNAKE_CASE__ , help='''Model\'s name or path to stored model.''' ) serve_parser.add_argument('''--config''' , type=SCREAMING_SNAKE_CASE__ , help='''Model\'s config name or path to stored model.''' ) serve_parser.add_argument('''--tokenizer''' , type=SCREAMING_SNAKE_CASE__ , help='''Tokenizer name to use.''' ) serve_parser.add_argument( '''--device''' , type=SCREAMING_SNAKE_CASE__ , default=-1 , help='''Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)''' , ) serve_parser.set_defaults(func=SCREAMING_SNAKE_CASE__ ) def __init__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : Any = pipeline lowercase : List[Any] = host lowercase : str = port lowercase : List[str] = 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}""" ) lowercase : List[str] = FastAPI( routes=[ APIRoute( '''/''' , self.model_info , response_model=SCREAMING_SNAKE_CASE__ , response_class=SCREAMING_SNAKE_CASE__ , methods=['''GET'''] , ), APIRoute( '''/tokenize''' , self.tokenize , response_model=SCREAMING_SNAKE_CASE__ , response_class=SCREAMING_SNAKE_CASE__ , methods=['''POST'''] , ), APIRoute( '''/detokenize''' , self.detokenize , response_model=SCREAMING_SNAKE_CASE__ , response_class=SCREAMING_SNAKE_CASE__ , methods=['''POST'''] , ), APIRoute( '''/forward''' , self.forward , response_model=SCREAMING_SNAKE_CASE__ , response_class=SCREAMING_SNAKE_CASE__ , methods=['''POST'''] , ), ] , timeout=600 , ) def __lowerCamelCase ( self ): run(self._app , host=self.host , port=self.port , workers=self.workers ) def __lowerCamelCase ( self ): return ServeModelInfoResult(infos=vars(self._pipeline.model.config ) ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ = Body(SCREAMING_SNAKE_CASE__ , embed=SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ = Body(SCREAMING_SNAKE_CASE__ , embed=SCREAMING_SNAKE_CASE__ ) ): try: lowercase : Optional[int] = self._pipeline.tokenizer.tokenize(SCREAMING_SNAKE_CASE__ ) if return_ids: lowercase : Tuple = self._pipeline.tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) return ServeTokenizeResult(tokens=SCREAMING_SNAKE_CASE__ , tokens_ids=SCREAMING_SNAKE_CASE__ ) else: return ServeTokenizeResult(tokens=SCREAMING_SNAKE_CASE__ ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(SCREAMING_SNAKE_CASE__ )} ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ = Body(SCREAMING_SNAKE_CASE__ , embed=SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ = Body(SCREAMING_SNAKE_CASE__ , embed=SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ = Body(SCREAMING_SNAKE_CASE__ , embed=SCREAMING_SNAKE_CASE__ ) , ): try: lowercase : Optional[Any] = self._pipeline.tokenizer.decode(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return ServeDeTokenizeResult(model='''''' , text=SCREAMING_SNAKE_CASE__ ) except Exception as e: raise HTTPException(status_code=500 , detail={'''model''': '''''', '''error''': str(SCREAMING_SNAKE_CASE__ )} ) async def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__=Body(SCREAMING_SNAKE_CASE__ , embed=SCREAMING_SNAKE_CASE__ ) ): # Check we don't have empty string if len(SCREAMING_SNAKE_CASE__ ) == 0: return ServeForwardResult(output=[] , attention=[] ) try: # Forward through the model lowercase : Optional[Any] = self._pipeline(SCREAMING_SNAKE_CASE__ ) return ServeForwardResult(output=SCREAMING_SNAKE_CASE__ ) except Exception as e: raise HTTPException(500 , {'''error''': str(SCREAMING_SNAKE_CASE__ )} )
337
from collections.abc import Callable class __SCREAMING_SNAKE_CASE : def __init__( self , SCREAMING_SNAKE_CASE__ = None ): # Stores actual heap items. lowercase : list = [] # Stores indexes of each item for supporting updates and deletion. lowercase : dict = {} # Stores current size of heap. lowercase : str = 0 # Stores function used to evaluate the score of an item on which basis ordering # will be done. lowercase : Tuple = key or (lambda SCREAMING_SNAKE_CASE__ : x) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ ): return int((i - 1) / 2 ) if i > 0 else None def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ ): lowercase : Any = int(2 * i + 1 ) return left if 0 < left < self.size else None def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ ): lowercase : Any = int(2 * i + 2 ) return right if 0 < right < self.size else None def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase , lowercase : Dict = ( self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. lowercase , lowercase : int = self.arr[j], self.arr[i] def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return self.arr[i][1] < self.arr[j][1] def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ ): lowercase : int = self._left(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = self._right(SCREAMING_SNAKE_CASE__ ) lowercase : List[Any] = i if left is not None and not self._cmp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = left if right is not None and not self._cmp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : List[str] = right return valid_parent def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ ): lowercase : Optional[int] = self._parent(SCREAMING_SNAKE_CASE__ ) while parent is not None and not self._cmp(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): self._swap(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : Optional[int] = parent, self._parent(SCREAMING_SNAKE_CASE__ ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ ): lowercase : Dict = self._get_valid_parent(SCREAMING_SNAKE_CASE__ ) while valid_parent != index: self._swap(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowercase , lowercase : str = valid_parent, self._get_valid_parent(SCREAMING_SNAKE_CASE__ ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if item not in self.pos_map: return lowercase : str = self.pos_map[item] lowercase : Optional[int] = [item, self.key(SCREAMING_SNAKE_CASE__ )] # Make sure heap is right in both up and down direction. # Ideally only one of them will make any change. self._heapify_up(SCREAMING_SNAKE_CASE__ ) self._heapify_down(SCREAMING_SNAKE_CASE__ ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ ): if item not in self.pos_map: return lowercase : List[str] = self.pos_map[item] del self.pos_map[item] lowercase : Optional[int] = self.arr[self.size - 1] lowercase : int = index self.size -= 1 # Make sure heap is right in both up and down direction. Ideally only one # of them will make any change- so no performance loss in calling both. if self.size > index: self._heapify_up(SCREAMING_SNAKE_CASE__ ) self._heapify_down(SCREAMING_SNAKE_CASE__ ) def __lowerCamelCase ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase : str = len(self.arr ) if arr_len == self.size: self.arr.append([item, self.key(SCREAMING_SNAKE_CASE__ )] ) else: lowercase : int = [item, self.key(SCREAMING_SNAKE_CASE__ )] lowercase : str = self.size self.size += 1 self._heapify_up(self.size - 1 ) def __lowerCamelCase ( self ): return self.arr[0] if self.size else None def __lowerCamelCase ( self ): lowercase : str = self.get_top() if top_item_tuple: self.delete_item(top_item_tuple[0] ) return top_item_tuple def __lowercase ( ) ->None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
337
1
import inspect import os import unittest from pathlib import Path import torch import accelerate from accelerate.test_utils import execute_subprocess_async from accelerate.test_utils.testing import run_command class snake_case_ (unittest.TestCase ): UpperCAmelCase__ : Optional[Any] = inspect.getfile(accelerate.test_utils ) UpperCAmelCase__ : Tuple = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_cli.py'''] ) UpperCAmelCase__ : Optional[int] = ['''accelerate''', '''launch'''] UpperCAmelCase__ : List[str] = Path.home() / '''.cache/huggingface/accelerate''' UpperCAmelCase__ : Union[str, Any] = '''default_config.yaml''' UpperCAmelCase__ : List[Any] = config_folder / config_file UpperCAmelCase__ : Optional[int] = config_folder / '''_default_config.yaml''' UpperCAmelCase__ : int = Path('''tests/test_configs''' ) @classmethod def lowerCamelCase__( cls :Any ) -> int: if cls.config_path.is_file(): cls.config_path.rename(cls.changed_path ) @classmethod def lowerCamelCase__( cls :Dict ) -> Union[str, Any]: if cls.changed_path.is_file(): cls.changed_path.rename(cls.config_path ) def lowerCamelCase__( self :Any ) -> Optional[int]: a__ = self.base_cmd if torch.cuda.is_available() and (torch.cuda.device_count() > 1): cmd += ["--multi_gpu"] execute_subprocess_async(cmd + [self.test_file_path] ,env=os.environ.copy() ) def lowerCamelCase__( self :str ) -> List[Any]: for config in sorted(self.test_config_path.glob('**/*.yaml' ) ): with self.subTest(config_file=__snake_case ): execute_subprocess_async( self.base_cmd + ['--config_file', str(__snake_case ), self.test_file_path] ,env=os.environ.copy() ) def lowerCamelCase__( self :int ) -> List[str]: execute_subprocess_async(['accelerate', 'test'] ,env=os.environ.copy() ) class snake_case_ (unittest.TestCase ): UpperCAmelCase__ : List[Any] = '''test-tpu''' UpperCAmelCase__ : str = '''us-central1-a''' UpperCAmelCase__ : Optional[Any] = '''ls''' UpperCAmelCase__ : Optional[int] = ['''accelerate''', '''tpu-config'''] UpperCAmelCase__ : Any = '''cd /usr/share''' UpperCAmelCase__ : Tuple = '''tests/test_samples/test_command_file.sh''' UpperCAmelCase__ : List[Any] = '''Running gcloud compute tpus tpu-vm ssh''' def lowerCamelCase__( self :Optional[Any] ) -> Tuple: a__ = run_command( self.cmd + ['--command', self.command, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug'] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all' ,__snake_case ,) def lowerCamelCase__( self :int ) -> str: a__ = run_command( self.cmd + [ '--config_file', 'tests/test_configs/0_12_0.yaml', '--command', self.command, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug', ] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all' ,__snake_case ,) def lowerCamelCase__( self :Any ) -> Dict: a__ = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--debug'] ,return_stdout=__snake_case ) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all' ,__snake_case ,) def lowerCamelCase__( self :Tuple ) -> str: a__ = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--command', self.command, '--debug'] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all' ,__snake_case ,) def lowerCamelCase__( self :Dict ) -> int: a__ = run_command( self.cmd + [ '--config_file', 'tests/test_configs/latest.yaml', '--command', self.command, '--command', 'echo "Hello World"', '--debug', ] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo "Hello World" --worker all' ,__snake_case ,) def lowerCamelCase__( self :Union[str, Any] ) -> List[Any]: a__ = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--command_file', self.command_file, '--debug'] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all' ,__snake_case ,) def lowerCamelCase__( self :int ) -> int: a__ = run_command( self.cmd + [ '--config_file', 'tests/test_configs/0_12_0.yaml', '--command_file', self.command_file, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug', ] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all' ,__snake_case ,) def lowerCamelCase__( self :Union[str, Any] ) -> List[Any]: a__ = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--install_accelerate', '--debug'] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo "hello world"; echo "this is a second command" --worker all' ,__snake_case ,) def lowerCamelCase__( self :Any ) -> Optional[int]: a__ = run_command( self.cmd + [ '--config_file', 'tests/test_configs/latest.yaml', '--install_accelerate', '--accelerate_version', '12.0.0', '--debug', ] ,return_stdout=__snake_case ,) self.assertIn( F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo "hello world"; echo "this is a second command" --worker all' ,__snake_case ,)
109
import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() snake_case : int = logging.get_logger(__name__) def __lowercase ( __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str ): a__ = [] for i in range(encoder_config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'encoder.deit.blocks.{i}.norm1.weight', F'encoder.encoder.layer.{i}.layernorm_before.weight') ) rename_keys.append((F'encoder.deit.blocks.{i}.norm1.bias', F'encoder.encoder.layer.{i}.layernorm_before.bias') ) rename_keys.append( (F'encoder.deit.blocks.{i}.attn.proj.weight', F'encoder.encoder.layer.{i}.attention.output.dense.weight') ) rename_keys.append( (F'encoder.deit.blocks.{i}.attn.proj.bias', F'encoder.encoder.layer.{i}.attention.output.dense.bias') ) rename_keys.append( (F'encoder.deit.blocks.{i}.norm2.weight', F'encoder.encoder.layer.{i}.layernorm_after.weight') ) rename_keys.append((F'encoder.deit.blocks.{i}.norm2.bias', F'encoder.encoder.layer.{i}.layernorm_after.bias') ) rename_keys.append( (F'encoder.deit.blocks.{i}.mlp.fc1.weight', F'encoder.encoder.layer.{i}.intermediate.dense.weight') ) rename_keys.append( (F'encoder.deit.blocks.{i}.mlp.fc1.bias', F'encoder.encoder.layer.{i}.intermediate.dense.bias') ) rename_keys.append( (F'encoder.deit.blocks.{i}.mlp.fc2.weight', F'encoder.encoder.layer.{i}.output.dense.weight') ) rename_keys.append((F'encoder.deit.blocks.{i}.mlp.fc2.bias', F'encoder.encoder.layer.{i}.output.dense.bias') ) # cls token, position embeddings and patch embeddings of encoder rename_keys.extend( [ ('encoder.deit.cls_token', 'encoder.embeddings.cls_token'), ('encoder.deit.pos_embed', 'encoder.embeddings.position_embeddings'), ('encoder.deit.patch_embed.proj.weight', 'encoder.embeddings.patch_embeddings.projection.weight'), ('encoder.deit.patch_embed.proj.bias', 'encoder.embeddings.patch_embeddings.projection.bias'), ('encoder.deit.norm.weight', 'encoder.layernorm.weight'), ('encoder.deit.norm.bias', 'encoder.layernorm.bias'), ] ) return rename_keys def __lowercase ( __lowerCAmelCase : Dict , __lowerCAmelCase : Dict ): for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) a__ = state_dict.pop(F'encoder.deit.blocks.{i}.attn.qkv.weight' ) a__ = in_proj_weight[ : encoder_config.hidden_size, : ] a__ = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] a__ = in_proj_weight[ -encoder_config.hidden_size :, : ] def __lowercase ( __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[Any] ): a__ = dct.pop(__lowerCAmelCase ) a__ = val def __lowercase ( __lowerCAmelCase : Optional[Any] ): if "handwritten" in checkpoint_url: a__ = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg' # industry # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg" # # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg" elif "printed" in checkpoint_url or "stage1" in checkpoint_url: a__ = 'https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg' a__ = Image.open(requests.get(__lowerCAmelCase , stream=__lowerCAmelCase ).raw ).convert('RGB' ) return im @torch.no_grad() def __lowercase ( __lowerCAmelCase : Optional[int] , __lowerCAmelCase : int ): a__ = ViTConfig(image_size=3_8_4 , qkv_bias=__lowerCAmelCase ) a__ = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: a__ = 7_6_8 elif "large" in checkpoint_url: # use ViT-large encoder a__ = 1_0_2_4 a__ = 4_0_9_6 a__ = 2_4 a__ = 1_6 a__ = 1_0_2_4 else: raise ValueError('Should either find \'base\' or \'large\' in checkpoint URL' ) # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards if "large-printed" in checkpoint_url or "stage1" in checkpoint_url: a__ = False a__ = 'relu' a__ = 1_0_2_4 a__ = True a__ = False a__ = False # load HuggingFace model a__ = ViTModel(__lowerCAmelCase , add_pooling_layer=__lowerCAmelCase ) a__ = TrOCRForCausalLM(__lowerCAmelCase ) a__ = VisionEncoderDecoderModel(encoder=__lowerCAmelCase , decoder=__lowerCAmelCase ) model.eval() # load state_dict of original model, rename some keys a__ = torch.hub.load_state_dict_from_url(__lowerCAmelCase , map_location='cpu' , check_hash=__lowerCAmelCase )['model'] a__ = create_rename_keys(__lowerCAmelCase , __lowerCAmelCase ) for src, dest in rename_keys: rename_key(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) read_in_q_k_v(__lowerCAmelCase , __lowerCAmelCase ) # remove parameters we don't need del state_dict["encoder.deit.head.weight"] del state_dict["encoder.deit.head.bias"] del state_dict["decoder.version"] # add prefix to decoder keys for key, val in state_dict.copy().items(): a__ = state_dict.pop(__lowerCAmelCase ) if key.startswith('decoder' ) and "output_projection" not in key: a__ = val else: a__ = val # load state dict model.load_state_dict(__lowerCAmelCase ) # Check outputs on an image a__ = ViTImageProcessor(size=encoder_config.image_size ) a__ = RobertaTokenizer.from_pretrained('roberta-large' ) a__ = TrOCRProcessor(__lowerCAmelCase , __lowerCAmelCase ) a__ = processor(images=prepare_img(__lowerCAmelCase ) , return_tensors='pt' ).pixel_values # verify logits a__ = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) a__ = model(pixel_values=__lowerCAmelCase , decoder_input_ids=__lowerCAmelCase ) a__ = outputs.logits a__ = torch.Size([1, 1, 5_0_2_6_5] ) if "trocr-base-handwritten" in checkpoint_url: a__ = torch.tensor( [-1.4_502, -4.6_683, -0.5_347, -2.9_291, 9.1_435, -3.0_571, 8.9_764, 1.7_560, 8.7_358, -1.5_311] ) elif "trocr-large-handwritten" in checkpoint_url: a__ = torch.tensor( [-2.6_437, -1.3_129, -2.2_596, -5.3_455, 6.3_539, 1.7_604, 5.4_991, 1.4_702, 5.6_113, 2.0_170] ) elif "trocr-base-printed" in checkpoint_url: a__ = torch.tensor( [-5.6_816, -5.8_388, 1.1_398, -6.9_034, 6.8_505, -2.4_393, 1.2_284, -1.0_232, -1.9_661, -3.9_210] ) elif "trocr-large-printed" in checkpoint_url: a__ = torch.tensor( [-6.0_162, -7.0_959, 4.4_155, -5.1_063, 7.0_468, -3.1_631, 2.6_466, -0.3_081, -0.8_106, -1.7_535] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :1_0] , __lowerCAmelCase , atol=1E-3 ), "First elements of logits not as expected" Path(__lowerCAmelCase ).mkdir(exist_ok=__lowerCAmelCase ) print(F'Saving model to {pytorch_dump_folder_path}' ) model.save_pretrained(__lowerCAmelCase ) print(F'Saving processor to {pytorch_dump_folder_path}' ) processor.save_pretrained(__lowerCAmelCase ) if __name__ == "__main__": snake_case : Optional[int] = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) snake_case : int = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
109
1
from typing import List import jiwer import jiwer.transforms as tr from packaging import version import datasets from datasets.config import PY_VERSION if PY_VERSION < version.parse("""3.8"""): import importlib_metadata else: import importlib.metadata as importlib_metadata _UpperCAmelCase : str = """""" if version.parse(importlib_metadata.version("""jiwer""")) < version.parse("""2.3.0"""): class lowerCAmelCase ( tr.AbstractTransform ): def __init__( self : Tuple , UpperCAmelCase : str = " " ) -> Tuple: lowerCamelCase__ : Dict = sentence_delimiter def A_ ( self : Dict , UpperCAmelCase : str ) -> Any: return list(UpperCAmelCase ) def A_ ( self : Union[str, Any] , UpperCAmelCase : List[str] ) -> Tuple: lowerCamelCase__ : Tuple = [] for sent_idx, sentence in enumerate(UpperCAmelCase ): chars.extend(self.process_string(UpperCAmelCase ) ) if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(UpperCAmelCase ) - 1: chars.append(self.sentence_delimiter ) return chars _UpperCAmelCase : str = tr.Compose( [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)] ) else: _UpperCAmelCase : Optional[Any] = tr.Compose( [ tr.RemoveMultipleSpaces(), tr.Strip(), tr.ReduceToSingleSentence(SENTENCE_DELIMITER), tr.ReduceToListOfListOfChars(), ] ) _UpperCAmelCase : str = """\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } """ _UpperCAmelCase : List[Any] = """\ Character error rate (CER) is a common metric of the performance of an automatic speech recognition system. CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information. Character error rate can be computed as: CER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct characters, N is the number of characters in the reference (N=S+D+C). CER's output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the performance of the ASR system with a CER of 0 being a perfect score. """ _UpperCAmelCase : str = """ Computes CER score of transcribed segments against references. Args: references: list of references for each speech input. predictions: list of transcribtions to score. concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result. Returns: (float): the character error rate Examples: >>> predictions = [\"this is the prediction\", \"there is an other sample\"] >>> references = [\"this is the reference\", \"there is another one\"] >>> cer = datasets.load_metric(\"cer\") >>> cer_score = cer.compute(predictions=predictions, references=references) >>> print(cer_score) 0.34146341463414637 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class lowerCAmelCase ( datasets.Metric ): def A_ ( self : int ) -> Tuple: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' , id='sequence' ), 'references': datasets.Value('string' , id='sequence' ), } ) , codebase_urls=['https://github.com/jitsi/jiwer/'] , reference_urls=[ 'https://en.wikipedia.org/wiki/Word_error_rate', 'https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates', ] , ) def A_ ( self : Tuple , UpperCAmelCase : Optional[Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : int=False ) -> Any: if concatenate_texts: return jiwer.compute_measures( UpperCAmelCase , UpperCAmelCase , truth_transform=UpperCAmelCase , hypothesis_transform=UpperCAmelCase , )["wer"] lowerCamelCase__ : List[str] = 0 lowerCamelCase__ : List[Any] = 0 for prediction, reference in zip(UpperCAmelCase , UpperCAmelCase ): lowerCamelCase__ : Optional[int] = jiwer.compute_measures( UpperCAmelCase , UpperCAmelCase , truth_transform=UpperCAmelCase , hypothesis_transform=UpperCAmelCase , ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
50
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __lowerCAmelCase : Dict =logging.get_logger(__name__) def _UpperCamelCase ( lowercase__ , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = nn.functional.normalize(lowercase__ ) __SCREAMING_SNAKE_CASE : Tuple = nn.functional.normalize(lowercase__ ) return torch.mm(lowercase__ , normalized_text_embeds.t() ) class _lowercase ( A__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = CLIPConfig SCREAMING_SNAKE_CASE__ : List[str] = ['''CLIPEncoderLayer'''] def __init__( self :str , lowerCAmelCase__ :CLIPConfig ) -> Tuple: super().__init__(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = CLIPVisionModel(config.vision_config ) __SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = nn.Parameter(torch.ones(17 ) , requires_grad=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : str = nn.Parameter(torch.ones(3 ) , requires_grad=lowerCAmelCase__ ) @torch.no_grad() def __magic_name__( self :Optional[int] , lowerCAmelCase__ :int , lowerCAmelCase__ :Optional[int] ) -> Dict: __SCREAMING_SNAKE_CASE : int = self.vision_model(lowerCAmelCase__ )[1] # pooled_output __SCREAMING_SNAKE_CASE : Optional[Any] = self.visual_projection(lowerCAmelCase__ ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __SCREAMING_SNAKE_CASE : Optional[Any] = cosine_distance(lowerCAmelCase__ , self.special_care_embeds ).cpu().float().numpy() __SCREAMING_SNAKE_CASE : Optional[Any] = cosine_distance(lowerCAmelCase__ , self.concept_embeds ).cpu().float().numpy() __SCREAMING_SNAKE_CASE : List[Any] = [] __SCREAMING_SNAKE_CASE : List[Any] = image_embeds.shape[0] for i in range(lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : Optional[int] = {'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images __SCREAMING_SNAKE_CASE : List[Any] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): __SCREAMING_SNAKE_CASE : List[str] = special_cos_dist[i][concept_idx] __SCREAMING_SNAKE_CASE : Any = self.special_care_embeds_weights[concept_idx].item() __SCREAMING_SNAKE_CASE : Union[str, Any] = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} ) __SCREAMING_SNAKE_CASE : Union[str, Any] = 0.01 for concept_idx in range(len(cos_dist[0] ) ): __SCREAMING_SNAKE_CASE : int = cos_dist[i][concept_idx] __SCREAMING_SNAKE_CASE : Union[str, Any] = self.concept_embeds_weights[concept_idx].item() __SCREAMING_SNAKE_CASE : Tuple = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(lowerCAmelCase__ ) result.append(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = [len(res['''bad_concepts'''] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def __magic_name__( self :Optional[int] , lowerCAmelCase__ :torch.FloatTensor , lowerCAmelCase__ :torch.FloatTensor ) -> Any: __SCREAMING_SNAKE_CASE : Optional[Any] = self.vision_model(lowerCAmelCase__ )[1] # pooled_output __SCREAMING_SNAKE_CASE : List[str] = self.visual_projection(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Dict = cosine_distance(lowerCAmelCase__ , self.special_care_embeds ) __SCREAMING_SNAKE_CASE : Optional[int] = cosine_distance(lowerCAmelCase__ , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images __SCREAMING_SNAKE_CASE : List[Any] = 0.0 __SCREAMING_SNAKE_CASE : Union[str, Any] = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) __SCREAMING_SNAKE_CASE : List[str] = torch.any(special_scores > 0 , dim=1 ) __SCREAMING_SNAKE_CASE : List[str] = special_care * 0.01 __SCREAMING_SNAKE_CASE : int = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) __SCREAMING_SNAKE_CASE : Optional[int] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) __SCREAMING_SNAKE_CASE : Any = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
9
0
'''simple docstring''' def _SCREAMING_SNAKE_CASE ( UpperCamelCase = 1000 ): """simple docstring""" lowerCAmelCase__ : Union[str, Any] = -1 lowerCAmelCase__ : Optional[Any] = 0 for a in range(1 , n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c lowerCAmelCase__ : Optional[Any] = (n * n - 2 * a * n) // (2 * n - 2 * a) lowerCAmelCase__ : Tuple = n - a - b if c * c == (a * a + b * b): lowerCAmelCase__ : int = a * b * c if candidate >= product: lowerCAmelCase__ : Any = candidate return product if __name__ == "__main__": print(F"""{solution() = }""")
184
'''simple docstring''' _lowerCAmelCase = '''Input must be a string of 8 numbers plus letter''' _lowerCAmelCase = '''TRWAGMYFPDXBNJZSQVHLCKE''' def _SCREAMING_SNAKE_CASE ( UpperCamelCase ): """simple docstring""" if not isinstance(UpperCamelCase , UpperCamelCase ): lowerCAmelCase__ : List[Any] = f"""Expected string as input, found {type(UpperCamelCase ).__name__}""" raise TypeError(UpperCamelCase ) lowerCAmelCase__ : Union[str, Any] = spanish_id.replace("""-""" , """""" ).upper() if len(UpperCamelCase ) != 9: raise ValueError(UpperCamelCase ) try: lowerCAmelCase__ : Optional[int] = int(spanish_id_clean[0:8] ) lowerCAmelCase__ : int = spanish_id_clean[8] except ValueError as ex: raise ValueError(UpperCamelCase ) from ex if letter.isdigit(): raise ValueError(UpperCamelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
184
1
"""simple docstring""" import argparse from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird from transformers.utils import logging logging.set_verbosity_info() def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) ->int: """simple docstring""" a_ = BigBirdConfig.from_json_file(UpperCAmelCase ) print(F'''Building PyTorch model from configuration: {config}''' ) if is_trivia_qa: a_ = BigBirdForQuestionAnswering(UpperCAmelCase ) else: a_ = BigBirdForPreTraining(UpperCAmelCase ) # Load weights from tf checkpoint load_tf_weights_in_big_bird(UpperCAmelCase , UpperCAmelCase , is_trivia_qa=UpperCAmelCase ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) model.save_pretrained(UpperCAmelCase ) if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--big_bird_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained BERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) parser.add_argument( '--is_trivia_qa', action='store_true', help='Whether to convert a model with a trivia_qa head.' ) UpperCamelCase_ = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa )
243
"""simple docstring""" from __future__ import annotations class snake_case : def __init__( self , __UpperCAmelCase) ->Any: a_ = TypeError( "Matrices must be formed from a list of zero or more lists containing at " "least one and the same number of values, each of which must be of type " "int or float.") if len(__UpperCAmelCase) != 0: a_ = len(rows[0]) if cols == 0: raise error for row in rows: if len(__UpperCAmelCase) != cols: raise error for value in row: if not isinstance(__UpperCAmelCase , (int, float)): raise error a_ = rows else: a_ = [] def UpperCAmelCase__ ( self) ->list[list[int]]: return [[row[i] for row in self.rows] for i in range(len(self.rows[0]))] @property def UpperCAmelCase__ ( self) ->int: return len(self.rows) @property def UpperCAmelCase__ ( self) ->int: return len(self.rows[0]) @property def UpperCAmelCase__ ( self) ->tuple[int, int]: return (self.num_rows, self.num_columns) @property def UpperCAmelCase__ ( self) ->bool: return self.order[0] == self.order[1] def UpperCAmelCase__ ( self) ->Matrix: a_ = [ [0 if column_num != row_num else 1 for column_num in range(self.num_rows)] for row_num in range(self.num_rows) ] return Matrix(__UpperCAmelCase) def UpperCAmelCase__ ( self) ->int: if not self.is_square: return 0 if self.order == (0, 0): return 1 if self.order == (1, 1): return int(self.rows[0][0]) if self.order == (2, 2): return int( (self.rows[0][0] * self.rows[1][1]) - (self.rows[0][1] * self.rows[1][0])) else: return sum( self.rows[0][column] * self.cofactors().rows[0][column] for column in range(self.num_columns)) def UpperCAmelCase__ ( self) ->bool: return bool(self.determinant()) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase) ->int: a_ = [ [ self.rows[other_row][other_column] for other_column in range(self.num_columns) if other_column != column ] for other_row in range(self.num_rows) if other_row != row ] return Matrix(__UpperCAmelCase).determinant() def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase) ->int: if (row + column) % 2 == 0: return self.get_minor(__UpperCAmelCase , __UpperCAmelCase) return -1 * self.get_minor(__UpperCAmelCase , __UpperCAmelCase) def UpperCAmelCase__ ( self) ->Matrix: return Matrix( [ [self.get_minor(__UpperCAmelCase , __UpperCAmelCase) for column in range(self.num_columns)] for row in range(self.num_rows) ]) def UpperCAmelCase__ ( self) ->Matrix: return Matrix( [ [ self.minors().rows[row][column] if (row + column) % 2 == 0 else self.minors().rows[row][column] * -1 for column in range(self.minors().num_columns) ] for row in range(self.minors().num_rows) ]) def UpperCAmelCase__ ( self) ->Matrix: a_ = [ [self.cofactors().rows[column][row] for column in range(self.num_columns)] for row in range(self.num_rows) ] return Matrix(__UpperCAmelCase) def UpperCAmelCase__ ( self) ->Matrix: a_ = self.determinant() if not determinant: raise TypeError("Only matrices with a non-zero determinant have an inverse") return self.adjugate() * (1 / determinant) def __repr__( self) ->str: return str(self.rows) def __str__( self) ->str: if self.num_rows == 0: return "[]" if self.num_rows == 1: return "[[" + ". ".join(str(self.rows[0])) + "]]" return ( "[" + "\n ".join( [ "[" + ". ".join([str(__UpperCAmelCase) for value in row]) + ".]" for row in self.rows ]) + "]" ) def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->None: a_ = TypeError("Row must be a list containing all ints and/or floats") if not isinstance(__UpperCAmelCase , __UpperCAmelCase): raise type_error for value in row: if not isinstance(__UpperCAmelCase , (int, float)): raise type_error if len(__UpperCAmelCase) != self.num_columns: raise ValueError( "Row must be equal in length to the other rows in the matrix") if position is None: self.rows.append(__UpperCAmelCase) else: a_ = self.rows[0:position] + [row] + self.rows[position:] def UpperCAmelCase__ ( self , __UpperCAmelCase , __UpperCAmelCase = None) ->None: a_ = TypeError( "Column must be a list containing all ints and/or floats") if not isinstance(__UpperCAmelCase , __UpperCAmelCase): raise type_error for value in column: if not isinstance(__UpperCAmelCase , (int, float)): raise type_error if len(__UpperCAmelCase) != self.num_rows: raise ValueError( "Column must be equal in length to the other columns in the matrix") if position is None: a_ = [self.rows[i] + [column[i]] for i in range(self.num_rows)] else: a_ = [ self.rows[i][0:position] + [column[i]] + self.rows[i][position:] for i in range(self.num_rows) ] def __eq__( self , __UpperCAmelCase) ->bool: if not isinstance(__UpperCAmelCase , __UpperCAmelCase): return NotImplemented return self.rows == other.rows def __ne__( self , __UpperCAmelCase) ->bool: return not self == other def __neg__( self) ->Matrix: return self * -1 def __add__( self , __UpperCAmelCase) ->Matrix: if self.order != other.order: raise ValueError("Addition requires matrices of the same order") return Matrix( [ [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns)] for i in range(self.num_rows) ]) def __sub__( self , __UpperCAmelCase) ->Matrix: if self.order != other.order: raise ValueError("Subtraction requires matrices of the same order") return Matrix( [ [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns)] for i in range(self.num_rows) ]) def __mul__( self , __UpperCAmelCase) ->Matrix: if isinstance(__UpperCAmelCase , (int, float)): return Matrix( [[int(element * other) for element in row] for row in self.rows]) elif isinstance(__UpperCAmelCase , __UpperCAmelCase): if self.num_columns != other.num_rows: raise ValueError( "The number of columns in the first matrix must " "be equal to the number of rows in the second") return Matrix( [ [Matrix.dot_product(__UpperCAmelCase , __UpperCAmelCase) for column in other.columns()] for row in self.rows ]) else: raise TypeError( "A Matrix can only be multiplied by an int, float, or another matrix") def __pow__( self , __UpperCAmelCase) ->Matrix: if not isinstance(__UpperCAmelCase , __UpperCAmelCase): raise TypeError("A Matrix can only be raised to the power of an int") if not self.is_square: raise ValueError("Only square matrices can be raised to a power") if other == 0: return self.identity() if other < 0: if self.is_invertable(): return self.inverse() ** (-other) raise ValueError( "Only invertable matrices can be raised to a negative power") a_ = self for _ in range(other - 1): result *= self return result @classmethod def UpperCAmelCase__ ( cls , __UpperCAmelCase , __UpperCAmelCase) ->int: return sum(row[i] * column[i] for i in range(len(__UpperCAmelCase))) if __name__ == "__main__": import doctest doctest.testmod()
243
1
import numpy as np from scipy.spatial.distance import cdist from sklearn.metrics import fa_score import datasets __A ='''\ @inproceedings{kakwani2020indicnlpsuite, title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}}, author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar}, year={2020}, booktitle={Findings of EMNLP}, } ''' __A ='''\ IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te. ''' __A =''' Compute IndicGLUE evaluation metric associated to each IndicGLUE dataset. Args: predictions: list of predictions to score (as int64), except for \'cvit-mkb-clsr\' where each prediction is a vector (of float32). references: list of ground truth labels corresponding to the predictions (as int64), except for \'cvit-mkb-clsr\' where each reference is a vector (of float32). Returns: depending on the IndicGLUE subset, one or several of: "accuracy": Accuracy "f1": F1 score "precision": Precision@10 Examples: >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wnli\') # \'wnli\' or any of ["copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = indic_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wiki-ner\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = indic_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'cvit-mkb-clsr\') >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]] >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]] >>> results = indic_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'precision@10\': 1.0} ''' def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ ): return float((preds == labels).mean() ) def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ ): lowerCamelCase_ = simple_accuracy(lowerCamelCase__ , lowerCamelCase__ ) lowerCamelCase_ = float(fa_score(y_true=lowerCamelCase__ , y_pred=lowerCamelCase__ ) ) return { "accuracy": acc, "f1": fa, } def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ ): lowerCamelCase_ = np.array(lowerCamelCase__ ) lowerCamelCase_ = np.array(lowerCamelCase__ ) lowerCamelCase_ = en_sentvecs.shape[0] # mean centering lowerCamelCase_ = en_sentvecs - np.mean(lowerCamelCase__ , axis=0 ) lowerCamelCase_ = in_sentvecs - np.mean(lowerCamelCase__ , axis=0 ) lowerCamelCase_ = cdist(lowerCamelCase__ , lowerCamelCase__ , "cosine" ) lowerCamelCase_ = np.array(range(lowerCamelCase__ ) ) lowerCamelCase_ = sim.argsort(axis=1 )[:, :1_0] lowerCamelCase_ = np.any(preds == actual[:, None] , axis=1 ) return float(matches.mean() ) @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE_( self ) -> Optional[int]: if self.config_name not in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", "wiki-ner", ]: raise KeyError( "You should supply a configuration name selected in " "[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", " "\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", " "\"wiki-ner\"]" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("int64" ) if self.config_name != "cvit-mkb-clsr" else datasets.Sequence(datasets.Value("float32" ) ), "references": datasets.Value("int64" ) if self.config_name != "cvit-mkb-clsr" else datasets.Sequence(datasets.Value("float32" ) ), } ) , codebase_urls=[] , reference_urls=[] , format="numpy" if self.config_name != "cvit-mkb-clsr" else None , ) def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase ) -> str: if self.config_name == "cvit-mkb-clsr": return {"precision@10": precision_at_aa(lowercase , lowercase )} elif self.config_name in ["wiki-ner"]: return acc_and_fa(lowercase , lowercase ) elif self.config_name in [ "wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md", ]: return {"accuracy": simple_accuracy(lowercase , lowercase )} else: raise KeyError( "You should supply a configuration name selected in " "[\"wnli\", \"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", " "\"cvit-mkb-clsr\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\", " "\"wiki-ner\"]" )
47
import numpy as np from cva import destroyAllWindows, imread, imshow, waitKey class _SCREAMING_SNAKE_CASE : def __init__( self , lowercase , lowercase , lowercase ) -> List[Any]: if dst_width < 0 or dst_height < 0: raise ValueError("Destination width/height should be > 0" ) lowerCamelCase_ = img lowerCamelCase_ = img.shape[1] lowerCamelCase_ = img.shape[0] lowerCamelCase_ = dst_width lowerCamelCase_ = dst_height lowerCamelCase_ = self.src_w / self.dst_w lowerCamelCase_ = self.src_h / self.dst_h lowerCamelCase_ = lowerCamelCase_ = ( np.ones((self.dst_h, self.dst_w, 3) , np.uinta ) * 255 ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[int]: for i in range(self.dst_h ): for j in range(self.dst_w ): lowerCamelCase_ = self.img[self.get_y(lowercase )][self.get_x(lowercase )] def SCREAMING_SNAKE_CASE_( self , lowercase ) -> int: return int(self.ratio_x * x ) def SCREAMING_SNAKE_CASE_( self , lowercase ) -> int: return int(self.ratio_y * y ) if __name__ == "__main__": __A, __A =8_0_0, 6_0_0 __A =imread('''image_data/lena.jpg''', 1) __A =NearestNeighbour(im, dst_w, dst_h) n.process() imshow( F"""Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}""", n.output ) waitKey(0) destroyAllWindows()
47
1
"""simple docstring""" # Logistic Regression from scratch # In[62]: # In[63]: # importing all the required libraries import numpy as np from matplotlib import pyplot as plt from sklearn import datasets def _snake_case ( _snake_case : Optional[int] ): return 1 / (1 + np.exp(-z )) def _snake_case ( _snake_case : Tuple , _snake_case : str ): return (-y * np.log(_snake_case ) - (1 - y) * np.log(1 - h )).mean() def _snake_case ( _snake_case : Optional[Any] , _snake_case : List[Any] , _snake_case : str ): lowerCAmelCase : Any = np.dot(_snake_case , _snake_case ) return np.sum(y * scores - np.log(1 + np.exp(_snake_case ) ) ) def _snake_case ( _snake_case : Tuple , _snake_case : Dict , _snake_case : Any , _snake_case : int=70000 ): lowerCAmelCase : int = np.zeros(x.shape[1] ) for iterations in range(_snake_case ): lowerCAmelCase : Dict = np.dot(_snake_case , _snake_case ) lowerCAmelCase : int = sigmoid_function(_snake_case ) lowerCAmelCase : Dict = np.dot(x.T , h - y ) / y.size lowerCAmelCase : str = theta - alpha * gradient # updating the weights lowerCAmelCase : str = np.dot(_snake_case , _snake_case ) lowerCAmelCase : Dict = sigmoid_function(_snake_case ) lowerCAmelCase : str = cost_function(_snake_case , _snake_case ) if iterations % 100 == 0: print(f'''loss: {j} \t''' ) # printing the loss after every 100 iterations return theta # In[68]: if __name__ == "__main__": snake_case__ : int = datasets.load_iris() snake_case__ : Optional[Any] = iris.data[:, :2] snake_case__ : Union[str, Any] = (iris.target != 0) * 1 snake_case__ : str = 0.1 snake_case__ : str = logistic_reg(alpha, x, y, max_iterations=70_000) print('''theta: ''', theta) # printing the theta i.e our weights vector def _snake_case ( _snake_case : Any ): return sigmoid_function( np.dot(_snake_case , _snake_case ) ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) plt.scatter(x[y == 0][:, 0], x[y == 0][:, 1], color='''b''', label='''0''') plt.scatter(x[y == 1][:, 0], x[y == 1][:, 1], color='''r''', label='''1''') ((snake_case__) , (snake_case__)) : Optional[int] = (x[:, 0].min(), x[:, 0].max()) ((snake_case__) , (snake_case__)) : Any = (x[:, 1].min(), x[:, 1].max()) ((snake_case__) , (snake_case__)) : str = np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max)) snake_case__ : int = np.c_[xxa.ravel(), xxa.ravel()] snake_case__ : Tuple = predict_prob(grid).reshape(xxa.shape) plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='''black''') plt.legend() plt.show()
60
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets a_ : str = """\ @inproceedings{lin-2004-rouge, title = \"{ROUGE}: A Package for Automatic Evaluation of Summaries\", author = \"Lin, Chin-Yew\", booktitle = \"Text Summarization Branches Out\", month = jul, year = \"2004\", address = \"Barcelona, Spain\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/W04-1013\", pages = \"74--81\", } """ a_ : int = """\ ROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for evaluating automatic summarization and machine translation software in natural language processing. The metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation. Note that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters. This metrics is a wrapper around Google Research reimplementation of ROUGE: https://github.com/google-research/google-research/tree/master/rouge """ a_ : Tuple = """ Calculates average rouge scores for a list of hypotheses and references Args: predictions: list of predictions to score. Each prediction should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. rouge_types: A list of rouge types to calculate. Valid names: `\"rouge{n}\"` (e.g. `\"rouge1\"`, `\"rouge2\"`) where: {n} is the n-gram based scoring, `\"rougeL\"`: Longest common subsequence based scoring. `\"rougeLSum\"`: rougeLsum splits text using `\"\n\"`. See details in https://github.com/huggingface/datasets/issues/617 use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes. use_aggregator: Return aggregates if this is set to True Returns: rouge1: rouge_1 (precision, recall, f1), rouge2: rouge_2 (precision, recall, f1), rougeL: rouge_l (precision, recall, f1), rougeLsum: rouge_lsum (precision, recall, f1) Examples: >>> rouge = datasets.load_metric('rouge') >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> results = rouge.compute(predictions=predictions, references=references) >>> print(list(results.keys())) ['rouge1', 'rouge2', 'rougeL', 'rougeLsum'] >>> print(results[\"rouge1\"]) AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0)) >>> print(results[\"rouge1\"].mid.fmeasure) 1.0 """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case ( datasets.Metric ): """simple docstring""" def snake_case ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/google-research/google-research/tree/master/rouge"] , reference_urls=[ "https://en.wikipedia.org/wiki/ROUGE_(metric)", "https://github.com/google-research/google-research/tree/master/rouge", ] , ) def snake_case ( self , UpperCamelCase , UpperCamelCase , UpperCamelCase=None , UpperCamelCase=True , UpperCamelCase=False ): """simple docstring""" if rouge_types is None: lowerCamelCase_ = ["rouge1", "rouge2", "rougeL", "rougeLsum"] lowerCamelCase_ = rouge_scorer.RougeScorer(rouge_types=UpperCamelCase , use_stemmer=UpperCamelCase ) if use_aggregator: lowerCamelCase_ = scoring.BootstrapAggregator() else: lowerCamelCase_ = [] for ref, pred in zip(UpperCamelCase , UpperCamelCase ): lowerCamelCase_ = scorer.score(UpperCamelCase , UpperCamelCase ) if use_aggregator: aggregator.add_scores(UpperCamelCase ) else: scores.append(UpperCamelCase ) if use_aggregator: lowerCamelCase_ = aggregator.aggregate() else: lowerCamelCase_ = {} for key in scores[0]: lowerCamelCase_ = [score[key] for score in scores] return result
55
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging A : List[Any] = logging.get_logger(__name__) if is_vision_available(): import PIL class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = ['''pixel_values'''] def __init__( self : Union[str, Any] , __lowerCAmelCase : bool = True , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , __lowerCAmelCase : bool = True , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : bool = True , __lowerCAmelCase : Union[int, float] = 1 / 2_55 , __lowerCAmelCase : bool = True , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : bool = True , **__lowerCAmelCase : str , ) -> None: """simple docstring""" super().__init__(**__lowerCAmelCase ) A__ = size if size is not None else {"""shortest_edge""": 2_24} A__ = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) A__ = crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} A__ = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase , param_name="""crop_size""" ) A__ = do_resize A__ = size A__ = resample A__ = do_center_crop A__ = crop_size A__ = do_rescale A__ = rescale_factor A__ = do_normalize A__ = image_mean if image_mean is not None else OPENAI_CLIP_MEAN A__ = image_std if image_std is not None else OPENAI_CLIP_STD A__ = do_convert_rgb def a_ ( self : Optional[int] , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Dict[str, int] , __lowerCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : Union[str, Any] , ) -> np.ndarray: """simple docstring""" A__ = get_size_dict(__lowerCAmelCase , default_to_square=__lowerCAmelCase ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) A__ = get_resize_output_image_size(__lowerCAmelCase , size=size["""shortest_edge"""] , default_to_square=__lowerCAmelCase ) return resize(__lowerCAmelCase , size=__lowerCAmelCase , resample=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : str , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Dict[str, int] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : List[Any] , ) -> np.ndarray: """simple docstring""" A__ = get_size_dict(__lowerCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys (height, width). Got {size.keys()}' ) return center_crop(__lowerCAmelCase , size=(size["""height"""], size["""width"""]) , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Tuple , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Union[int, float] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : Union[str, Any] , ) -> Union[str, Any]: """simple docstring""" return rescale(__lowerCAmelCase , scale=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : int , __lowerCAmelCase : np.ndarray , __lowerCAmelCase : Union[float, List[float]] , __lowerCAmelCase : Union[float, List[float]] , __lowerCAmelCase : Optional[Union[str, ChannelDimension]] = None , **__lowerCAmelCase : List[Any] , ) -> np.ndarray: """simple docstring""" return normalize(__lowerCAmelCase , mean=__lowerCAmelCase , std=__lowerCAmelCase , data_format=__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Tuple , __lowerCAmelCase : ImageInput , __lowerCAmelCase : bool = None , __lowerCAmelCase : Dict[str, int] = None , __lowerCAmelCase : PILImageResampling = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : int = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : float = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : Optional[Union[float, List[float]]] = None , __lowerCAmelCase : bool = None , __lowerCAmelCase : Optional[Union[str, TensorType]] = None , __lowerCAmelCase : Optional[ChannelDimension] = ChannelDimension.FIRST , **__lowerCAmelCase : Optional[Any] , ) -> PIL.Image.Image: """simple docstring""" A__ = do_resize if do_resize is not None else self.do_resize A__ = size if size is not None else self.size A__ = get_size_dict(__lowerCAmelCase , param_name="""size""" , default_to_square=__lowerCAmelCase ) A__ = resample if resample is not None else self.resample A__ = do_center_crop if do_center_crop is not None else self.do_center_crop A__ = crop_size if crop_size is not None else self.crop_size A__ = get_size_dict(__lowerCAmelCase , param_name="""crop_size""" , default_to_square=__lowerCAmelCase ) A__ = do_rescale if do_rescale is not None else self.do_rescale A__ = rescale_factor if rescale_factor is not None else self.rescale_factor A__ = do_normalize if do_normalize is not None else self.do_normalize A__ = image_mean if image_mean is not None else self.image_mean A__ = image_std if image_std is not None else self.image_std A__ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb A__ = make_list_of_images(__lowerCAmelCase ) if not valid_images(__lowerCAmelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # PIL RGBA images are converted to RGB if do_convert_rgb: A__ = [convert_to_rgb(__lowerCAmelCase ) for image in images] # All transformations expect numpy arrays. A__ = [to_numpy_array(__lowerCAmelCase ) for image in images] if do_resize: A__ = [self.resize(image=__lowerCAmelCase , size=__lowerCAmelCase , resample=__lowerCAmelCase ) for image in images] if do_center_crop: A__ = [self.center_crop(image=__lowerCAmelCase , size=__lowerCAmelCase ) for image in images] if do_rescale: A__ = [self.rescale(image=__lowerCAmelCase , scale=__lowerCAmelCase ) for image in images] if do_normalize: A__ = [self.normalize(image=__lowerCAmelCase , mean=__lowerCAmelCase , std=__lowerCAmelCase ) for image in images] A__ = [to_channel_dimension_format(__lowerCAmelCase , __lowerCAmelCase ) for image in images] A__ = {"""pixel_values""": images} return BatchFeature(data=__lowerCAmelCase , tensor_type=__lowerCAmelCase )
353
from string import ascii_uppercase A : List[str] = {str(ord(c) - 5_5): c for c in ascii_uppercase} def __lowerCamelCase ( __a :int , __a :int ) -> str: """simple docstring""" if isinstance(__a , __a ): raise TypeError("""int() can't convert non-string with explicit base""" ) if num < 0: raise ValueError("""parameter must be positive int""" ) if isinstance(__a , __a ): raise TypeError("""'str' object cannot be interpreted as an integer""" ) if isinstance(__a , __a ): raise TypeError("""'float' object cannot be interpreted as an integer""" ) if base in (0, 1): raise ValueError("""base must be >= 2""" ) if base > 3_6: raise ValueError("""base must be <= 36""" ) A__ = """""" A__ = 0 A__ = 0 while div != 1: A__ , A__ = divmod(__a , __a ) if base >= 1_1 and 9 < mod < 3_6: A__ = ALPHABET_VALUES[str(__a )] else: A__ = str(__a ) new_value += actual_value A__ = num // base A__ = div if div == 0: return str(new_value[::-1] ) elif div == 1: new_value += str(__a ) return str(new_value[::-1] ) return new_value[::-1] if __name__ == "__main__": import doctest doctest.testmod() for base in range(2, 3_7): for num in range(1_0_0_0): assert int(decimal_to_any(num, base), base) == num, ( num, base, decimal_to_any(num, base), int(decimal_to_any(num, base), base), )
276
0
'''simple docstring''' from collections.abc import Callable import numpy as np def __UpperCAmelCase ( A : Callable , A : float , A : float , A : float , A : float ) -> np.ndarray: UpperCAmelCase_ : Any = int(np.ceil((x_end - xa) / step_size ) ) UpperCAmelCase_ : int = np.zeros((n + 1,) ) UpperCAmelCase_ : Dict = ya UpperCAmelCase_ : Optional[int] = xa for k in range(A ): UpperCAmelCase_ : str = y[k] + step_size * ode_func(A , y[k] ) x += step_size return y if __name__ == "__main__": import doctest doctest.testmod()
304
'''simple docstring''' import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process _UpperCamelCase : Union[str, Any] = logging.getLogger(__name__) _UpperCamelCase : Optional[int] = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) _UpperCamelCase : str = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class snake_case__ : a_ = field( default=UpperCamelCase , metadata={ "help": ( "The model checkpoint for weights initialization. Leave None if you want to train a model from" " scratch." ) } , ) a_ = field( default=UpperCamelCase , metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(UpperCamelCase)} , ) a_ = field( default=UpperCamelCase , metadata={"help": "Pretrained config name or path if not the same as model_name"}) a_ = field( default=UpperCamelCase , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) a_ = field( default=UpperCamelCase , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class snake_case__ : a_ = field( default=UpperCamelCase , metadata={"help": "The input training data file (a text file)."}) a_ = field( default=UpperCamelCase , metadata={ "help": ( "The input training data files (multiple files in glob format). " "Very often splitting large files to smaller files can prevent tokenizer going out of memory" ) } , ) a_ = field( default=UpperCamelCase , metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."} , ) a_ = field( default=UpperCamelCase , metadata={"help": "An optional input train ref data file for whole word mask in Chinese."} , ) a_ = field( default=UpperCamelCase , metadata={"help": "An optional input eval ref data file for whole word mask in Chinese."} , ) a_ = field( default=UpperCamelCase , metadata={"help": "Whether distinct lines of text in the dataset are to be handled as distinct sequences."} , ) a_ = field( default=UpperCamelCase , metadata={"help": "Train with masked-language modeling loss instead of language modeling."}) a_ = field(default=UpperCamelCase , metadata={"help": "Whether ot not to use whole word mask."}) a_ = field( default=0.15 , metadata={"help": "Ratio of tokens to mask for masked language modeling loss"}) a_ = field( default=1 / 6 , metadata={ "help": ( "Ratio of length of a span of masked tokens to surrounding context length for permutation language" " modeling." ) } , ) a_ = field( default=5 , metadata={"help": "Maximum length of a span of masked tokens for permutation language modeling."}) a_ = field( default=-1 , metadata={ "help": ( "Optional input sequence length after tokenization." "The training dataset will be truncated in block of this size for training." "Default to the model max input length for single sentence inputs (take into account special tokens)." ) } , ) a_ = field( default=UpperCamelCase , metadata={"help": "Overwrite the cached training and evaluation sets"}) def __UpperCAmelCase ( A : DataTrainingArguments , A : PreTrainedTokenizer , A : bool = False , A : Optional[str] = None , ) -> List[Any]: def _dataset(A : Dict , A : str=None ): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError('''You need to set world whole masking and mlm to True for Chinese Whole Word Mask''' ) return LineByLineWithRefDataset( tokenizer=A , file_path=A , block_size=args.block_size , ref_path=A , ) return LineByLineTextDataset(tokenizer=A , file_path=A , block_size=args.block_size ) else: return TextDataset( tokenizer=A , file_path=A , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=A , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file ) elif args.train_data_files: return ConcatDataset([_dataset(A ) for f in glob(args.train_data_files )] ) else: return _dataset(args.train_data_file , args.train_ref_file ) def __UpperCAmelCase ( ) -> Optional[Any]: # 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_ : str = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : str = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( '''Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file ''' '''or remove the --do_eval argument.''' ) if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"Output directory ({training_args.output_dir}) already exists and is not empty. Use" ''' --overwrite_output_dir to overcome.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , A ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: UpperCAmelCase_ : List[Any] = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.tokenizer_name: UpperCAmelCase_ : str = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: UpperCAmelCase_ : List[str] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: raise ValueError( '''You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another''' ''' script, save it,and load it from here, using --tokenizer_name''' ) if model_args.model_name_or_path: UpperCAmelCase_ : str = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=A , cache_dir=model_args.cache_dir , ) else: logger.info('''Training new model from scratch''' ) UpperCAmelCase_ : int = AutoModelWithLMHead.from_config(A ) model.resize_token_embeddings(len(A ) ) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( '''BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the''' '''--mlm flag (masked language modeling).''' ) if data_args.block_size <= 0: UpperCAmelCase_ : List[str] = tokenizer.max_len # Our input block size will be the max possible for the model else: UpperCAmelCase_ : Dict = min(data_args.block_size , tokenizer.max_len ) # Get datasets UpperCAmelCase_ : str = ( get_dataset(A , tokenizer=A , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) UpperCAmelCase_ : Any = ( get_dataset(A , tokenizer=A , evaluate=A , cache_dir=model_args.cache_dir ) if training_args.do_eval else None ) if config.model_type == "xlnet": UpperCAmelCase_ : Optional[int] = DataCollatorForPermutationLanguageModeling( tokenizer=A , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: UpperCAmelCase_ : Tuple = DataCollatorForWholeWordMask( tokenizer=A , mlm_probability=data_args.mlm_probability ) else: UpperCAmelCase_ : List[str] = DataCollatorForLanguageModeling( tokenizer=A , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer UpperCAmelCase_ : Any = Trainer( model=A , args=A , data_collator=A , train_dataset=A , eval_dataset=A , prediction_loss_only=A , ) # Training if training_args.do_train: UpperCAmelCase_ : List[str] = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ) else None ) trainer.train(model_path=A ) 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_ : Tuple = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) UpperCAmelCase_ : Dict = trainer.evaluate() UpperCAmelCase_ : Union[str, Any] = math.exp(eval_output['''eval_loss'''] ) UpperCAmelCase_ : Optional[int] = {'''perplexity''': perplexity} UpperCAmelCase_ : int = os.path.join(training_args.output_dir , '''eval_results_lm.txt''' ) if trainer.is_world_master(): with open(A , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key in sorted(result.keys() ): logger.info(''' %s = %s''' , A , str(result[key] ) ) writer.write('''%s = %s\n''' % (key, str(result[key] )) ) results.update(A ) return results def __UpperCAmelCase ( A : Tuple ) -> Tuple: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
304
1
'''simple docstring''' import argparse import json import os import torch from transformers.file_utils import has_file from diffusers import UNetaDConditionModel, UNetaDModel _lowerCAmelCase = False _lowerCAmelCase = True _lowerCAmelCase = False if __name__ == "__main__": _lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '''--repo_path''', default=None, type=str, required=True, help='''The config json file corresponding to the architecture.''', ) parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') _lowerCAmelCase = parser.parse_args() _lowerCAmelCase = { "image_size": "sample_size", "num_res_blocks": "layers_per_block", "block_channels": "block_out_channels", "down_blocks": "down_block_types", "up_blocks": "up_block_types", "downscale_freq_shift": "freq_shift", "resnet_num_groups": "norm_num_groups", "resnet_act_fn": "act_fn", "resnet_eps": "norm_eps", "num_head_channels": "attention_head_dim", } _lowerCAmelCase = { "time_steps": "time_proj", "mid": "mid_block", "downsample_blocks": "down_blocks", "upsample_blocks": "up_blocks", } _lowerCAmelCase = "" if has_file(args.repo_path, '''config.json''') else "unet" with open(os.path.join(args.repo_path, subfolder, '''config.json'''), '''r''', encoding='''utf-8''') as reader: _lowerCAmelCase = reader.read() _lowerCAmelCase = json.loads(text) if do_only_config: for key in config_parameters_to_change.keys(): config.pop(key, None) if has_file(args.repo_path, '''config.json'''): _lowerCAmelCase = UNetaDModel(**config) else: _lowerCAmelCase = UNetaDConditionModel if "ldm-text2im-large-256" in args.repo_path else UNetaDModel _lowerCAmelCase = class_name(**config) if do_only_config: model.save_config(os.path.join(args.repo_path, subfolder)) _lowerCAmelCase = dict(model.config) if do_only_renaming: for key, value in config_parameters_to_change.items(): if key in config: _lowerCAmelCase = config[key] del config[key] _lowerCAmelCase = [k.replace('''UNetRes''', '''''') for k in config["down_block_types"]] _lowerCAmelCase = [k.replace('''UNetRes''', '''''') for k in config["up_block_types"]] if do_only_weights: _lowerCAmelCase = torch.load(os.path.join(args.repo_path, subfolder, '''diffusion_pytorch_model.bin''')) _lowerCAmelCase = {} for param_key, param_value in state_dict.items(): if param_key.endswith('''.op.bias''') or param_key.endswith('''.op.weight'''): continue _lowerCAmelCase = False for key, new_key in key_parameters_to_change.items(): if not has_changed and param_key.split('''.''')[0] == key: _lowerCAmelCase = param_value _lowerCAmelCase = True if not has_changed: _lowerCAmelCase = param_value model.load_state_dict(new_state_dict) model.save_pretrained(os.path.join(args.repo_path, subfolder))
351
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _lowerCAmelCase = logging.get_logger(__name__) _lowerCAmelCase = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class lowerCAmelCase_( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Tuple = '''convnextv2''' def __init__( self ,__UpperCAmelCase=3 ,__UpperCAmelCase=4 ,__UpperCAmelCase=4 ,__UpperCAmelCase=None ,__UpperCAmelCase=None ,__UpperCAmelCase="gelu" ,__UpperCAmelCase=0.0_2 ,__UpperCAmelCase=1E-12 ,__UpperCAmelCase=0.0 ,__UpperCAmelCase=224 ,__UpperCAmelCase=None ,__UpperCAmelCase=None ,**__UpperCAmelCase ,) -> Union[str, Any]: super().__init__(**__UpperCAmelCase ) lowerCAmelCase__ : int = num_channels lowerCAmelCase__ : List[Any] = patch_size lowerCAmelCase__ : Union[str, Any] = num_stages lowerCAmelCase__ : Tuple = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes lowerCAmelCase__ : str = [3, 3, 9, 3] if depths is None else depths lowerCAmelCase__ : Optional[Any] = hidden_act lowerCAmelCase__ : str = initializer_range lowerCAmelCase__ : List[str] = layer_norm_eps lowerCAmelCase__ : Dict = drop_path_rate lowerCAmelCase__ : int = image_size lowerCAmelCase__ : int = ["""stem"""] + [F"""stage{idx}""" for idx in range(1 ,len(self.depths ) + 1 )] lowerCAmelCase__ , lowerCAmelCase__ : Tuple = get_aligned_output_features_output_indices( out_features=__UpperCAmelCase ,out_indices=__UpperCAmelCase ,stage_names=self.stage_names )
184
0
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 _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE ): A_ : Any = np.inf def set_batch_size(SCREAMING_SNAKE_CASE ) -> None: nonlocal batch_size if isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A_ : Tuple = min(SCREAMING_SNAKE_CASE , config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): A_ : str = min(SCREAMING_SNAKE_CASE , config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and feature.dtype == "binary": A_ : Union[str, Any] = min(SCREAMING_SNAKE_CASE , config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return None if batch_size is np.inf else batch_size class _lowerCamelCase ( UpperCamelCase ): """simple docstring""" def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , )->str: '''simple docstring''' super().__init__( _SCREAMING_SNAKE_CASE , split=_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE , keep_in_memory=_SCREAMING_SNAKE_CASE , streaming=_SCREAMING_SNAKE_CASE , num_proc=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) A_ : str = path_or_paths if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else {self.split: path_or_paths} A_ : Optional[int] = _PACKAGED_DATASETS_MODULES['''parquet'''][1] A_ : Union[str, Any] = Parquet( cache_dir=_SCREAMING_SNAKE_CASE , data_files=_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE , hash=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def _snake_case ( self )->Optional[int]: '''simple docstring''' if self.streaming: A_ : List[str] = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: A_ : List[str] = None A_ : List[str] = None A_ : List[Any] = None A_ : Dict = None self.builder.download_and_prepare( download_config=_SCREAMING_SNAKE_CASE , download_mode=_SCREAMING_SNAKE_CASE , verification_mode=_SCREAMING_SNAKE_CASE , base_path=_SCREAMING_SNAKE_CASE , num_proc=self.num_proc , ) A_ : Union[str, Any] = self.builder.as_dataset( split=self.split , verification_mode=_SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory ) return dataset class _lowerCamelCase : """simple docstring""" def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , )->List[Any]: '''simple docstring''' A_ : Union[str, Any] = dataset A_ : Union[str, Any] = path_or_buf A_ : Any = batch_size or get_writer_batch_size(dataset.features ) A_ : Optional[int] = parquet_writer_kwargs def _snake_case ( self )->int: '''simple docstring''' A_ : Union[str, Any] = 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: A_ : str = self._write(file_obj=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) else: A_ : Tuple = self._write(file_obj=self.path_or_buf , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) return written def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE )->int: '''simple docstring''' A_ : List[Any] = 0 A_ : int = parquet_writer_kwargs.pop('''path_or_buf''' , _SCREAMING_SNAKE_CASE ) A_ : List[Any] = self.dataset.features.arrow_schema A_ : List[str] = pq.ParquetWriter(_SCREAMING_SNAKE_CASE , schema=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) for offset in logging.tqdm( range(0 , len(self.dataset ) , _SCREAMING_SNAKE_CASE ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating parquet from Arrow format''' , ): A_ : List[Any] = query_table( table=self.dataset._data , key=slice(_SCREAMING_SNAKE_CASE , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(_SCREAMING_SNAKE_CASE ) written += batch.nbytes writer.close() return written
186
import sys from typing import Tuple import numpy as np import torch from PIL import Image from torch import nn from transformers.image_utils import PILImageResampling from utils import img_tensorize class _lowerCamelCase : """simple docstring""" def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=sys.maxsize )->Any: '''simple docstring''' A_ : Dict = '''bilinear''' A_ : Optional[Any] = max_size A_ : Optional[Any] = short_edge_length def __call__( self , _SCREAMING_SNAKE_CASE )->List[Any]: '''simple docstring''' A_ : str = [] for img in imgs: A_ , A_ : List[str] = img.shape[:2] # later: provide list and randomly choose index for resize A_ : List[Any] = np.random.randint(self.short_edge_length[0] , self.short_edge_length[1] + 1 ) if size == 0: return img A_ : int = size * 1.0 / min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if h < w: A_ , A_ : Tuple = size, scale * w else: A_ , A_ : List[str] = scale * h, size if max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) > self.max_size: A_ : List[Any] = self.max_size * 1.0 / max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ : Any = newh * scale A_ : List[str] = neww * scale A_ : List[Any] = int(neww + 0.5 ) A_ : Tuple = int(newh + 0.5 ) if img.dtype == np.uinta: A_ : List[str] = Image.fromarray(_SCREAMING_SNAKE_CASE ) A_ : Optional[Any] = pil_image.resize((neww, newh) , PILImageResampling.BILINEAR ) A_ : Dict = np.asarray(_SCREAMING_SNAKE_CASE ) else: A_ : Any = img.permute(2 , 0 , 1 ).unsqueeze(0 ) # 3, 0, 1) # hw(c) -> nchw A_ : List[str] = nn.functional.interpolate( _SCREAMING_SNAKE_CASE , (newh, neww) , mode=self.interp_method , align_corners=_SCREAMING_SNAKE_CASE ).squeeze(0 ) img_augs.append(_SCREAMING_SNAKE_CASE ) return img_augs class _lowerCamelCase : """simple docstring""" def __init__( self , _SCREAMING_SNAKE_CASE )->Tuple: '''simple docstring''' A_ : Tuple = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] , cfg.INPUT.MAX_SIZE_TEST ) A_ : Union[str, Any] = cfg.INPUT.FORMAT A_ : int = cfg.SIZE_DIVISIBILITY A_ : Tuple = cfg.PAD_VALUE A_ : List[Any] = cfg.INPUT.MAX_SIZE_TEST A_ : List[str] = cfg.MODEL.DEVICE A_ : Dict = torch.tensor(cfg.MODEL.PIXEL_STD ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) A_ : List[Any] = torch.tensor(cfg.MODEL.PIXEL_MEAN ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ) , 1 , 1 ) A_ : List[Any] = lambda _SCREAMING_SNAKE_CASE : (x - self.pixel_mean) / self.pixel_std def _snake_case ( self , _SCREAMING_SNAKE_CASE )->Optional[int]: '''simple docstring''' A_ : Any = tuple(max(_SCREAMING_SNAKE_CASE ) for s in zip(*[img.shape for img in images] ) ) A_ : List[Any] = [im.shape[-2:] for im in images] A_ : Any = [ nn.functional.pad( _SCREAMING_SNAKE_CASE , [0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] , value=self.pad_value , ) for size, im in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ] return torch.stack(_SCREAMING_SNAKE_CASE ), torch.tensor(_SCREAMING_SNAKE_CASE ) def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False )->Dict: '''simple docstring''' with torch.no_grad(): if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ : Dict = [images] if single_image: assert len(_SCREAMING_SNAKE_CASE ) == 1 for i in range(len(_SCREAMING_SNAKE_CASE ) ): if isinstance(images[i] , torch.Tensor ): images.insert(_SCREAMING_SNAKE_CASE , images.pop(_SCREAMING_SNAKE_CASE ).to(self.device ).float() ) elif not isinstance(images[i] , torch.Tensor ): images.insert( _SCREAMING_SNAKE_CASE , torch.as_tensor(img_tensorize(images.pop(_SCREAMING_SNAKE_CASE ) , input_format=self.input_format ) ) .to(self.device ) .float() , ) # resize smallest edge A_ : List[str] = torch.tensor([im.shape[:2] for im in images] ) A_ : Union[str, Any] = self.aug(_SCREAMING_SNAKE_CASE ) # transpose images and convert to torch tensors # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images] # now normalize before pad to avoid useless arithmetic A_ : List[str] = [self.normalizer(_SCREAMING_SNAKE_CASE ) for x in images] # now pad them to do the following operations A_ , A_ : Any = self.pad(_SCREAMING_SNAKE_CASE ) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad A_ : str = torch.true_divide(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): assert torch.isfinite(SCREAMING_SNAKE_CASE ).all(), "Box tensor contains infinite or NaN!" A_ , A_ : int = box_size tensor[:, 0].clamp_(min=0 , max=SCREAMING_SNAKE_CASE ) tensor[:, 1].clamp_(min=0 , max=SCREAMING_SNAKE_CASE ) tensor[:, 2].clamp_(min=0 , max=SCREAMING_SNAKE_CASE ) tensor[:, 3].clamp_(min=0 , max=SCREAMING_SNAKE_CASE )
186
1
"""simple docstring""" import argparse import requests import torch from PIL import Image from transformers import CLIPProcessor, GroupViTConfig, GroupViTModel def A ( snake_case :str ) -> List[Any]: if "img_encoder.pos_embed" in name: __UpperCamelCase = name.replace('img_encoder.pos_embed' , 'vision_model.embeddings.position_embeddings' ) if "img_encoder.patch_embed.proj" in name: __UpperCamelCase = name.replace('img_encoder.patch_embed.proj' , 'vision_model.embeddings.patch_embeddings.projection' ) if "img_encoder.patch_embed.norm" in name: __UpperCamelCase = name.replace('img_encoder.patch_embed.norm' , 'vision_model.embeddings.layernorm' ) if "img_encoder.layers" in name: __UpperCamelCase = name.replace('img_encoder.layers' , 'vision_model.encoder.stages' ) if "blocks" in name and "res" not in name: __UpperCamelCase = name.replace('blocks' , 'layers' ) if "attn" in name and "pre_assign" not in name: __UpperCamelCase = name.replace('attn' , 'self_attn' ) if "proj" in name and "self_attn" in name and "text" not in name: __UpperCamelCase = name.replace('proj' , 'out_proj' ) if "pre_assign_attn.attn.proj" in name: __UpperCamelCase = name.replace('pre_assign_attn.attn.proj' , 'pre_assign_attn.attn.out_proj' ) if "norm1" in name: __UpperCamelCase = name.replace('norm1' , 'layer_norm1' ) if "norm2" in name and "pre_assign" not in name: __UpperCamelCase = name.replace('norm2' , 'layer_norm2' ) if "img_encoder.norm" in name: __UpperCamelCase = name.replace('img_encoder.norm' , 'vision_model.layernorm' ) # text encoder if "text_encoder.token_embedding" in name: __UpperCamelCase = name.replace('text_encoder.token_embedding' , 'text_model.embeddings.token_embedding' ) if "text_encoder.positional_embedding" in name: __UpperCamelCase = name.replace('text_encoder.positional_embedding' , 'text_model.embeddings.position_embedding.weight' ) if "text_encoder.transformer.resblocks." in name: __UpperCamelCase = name.replace('text_encoder.transformer.resblocks.' , 'text_model.encoder.layers.' ) if "ln_1" in name: __UpperCamelCase = name.replace('ln_1' , 'layer_norm1' ) if "ln_2" in name: __UpperCamelCase = name.replace('ln_2' , 'layer_norm2' ) if "c_fc" in name: __UpperCamelCase = name.replace('c_fc' , 'fc1' ) if "c_proj" in name: __UpperCamelCase = name.replace('c_proj' , 'fc2' ) if "text_encoder" in name: __UpperCamelCase = name.replace('text_encoder' , 'text_model' ) if "ln_final" in name: __UpperCamelCase = name.replace('ln_final' , 'final_layer_norm' ) # projection layers if "img_projector.linear_hidden." in name: __UpperCamelCase = name.replace('img_projector.linear_hidden.' , 'visual_projection.' ) if "img_projector.linear_out." in name: __UpperCamelCase = name.replace('img_projector.linear_out.' , 'visual_projection.3.' ) if "text_projector.linear_hidden" in name: __UpperCamelCase = name.replace('text_projector.linear_hidden' , 'text_projection' ) if "text_projector.linear_out" in name: __UpperCamelCase = name.replace('text_projector.linear_out' , 'text_projection.3' ) return name def A ( snake_case :Any , snake_case :List[str] ) -> Any: for key in orig_state_dict.copy().keys(): __UpperCamelCase = orig_state_dict.pop(__A ) if "qkv" in key: # weights and biases of the key, value and query projections of vision encoder's attention layers require special treatment: # we need to split them up into separate matrices/vectors __UpperCamelCase = key.split('.' ) __UpperCamelCase , __UpperCamelCase = int(key_split[2] ), int(key_split[4] ) __UpperCamelCase = config.vision_config.hidden_size if "weight" in key: __UpperCamelCase = val[:dim, :] __UpperCamelCase = val[dim : dim * 2, :] __UpperCamelCase = val[-dim:, :] else: __UpperCamelCase = val[:dim] __UpperCamelCase = val[dim : dim * 2] __UpperCamelCase = val[-dim:] elif "in_proj" in key: # weights and biases of the key, value and query projections of text encoder's attention layers require special treatment: # we need to split them up into separate matrices/vectors __UpperCamelCase = key.split('.' ) __UpperCamelCase = int(key_split[3] ) __UpperCamelCase = config.text_config.hidden_size if "weight" in key: __UpperCamelCase = val[:dim, :] __UpperCamelCase = val[ dim : dim * 2, : ] __UpperCamelCase = val[-dim:, :] else: __UpperCamelCase = val[:dim] __UpperCamelCase = val[dim : dim * 2] __UpperCamelCase = val[-dim:] else: __UpperCamelCase = rename_key(__A ) # squeeze if necessary if ( "text_projection.0" in new_name or "text_projection.3" in new_name or "visual_projection.0" in new_name or "visual_projection.3" in new_name ): __UpperCamelCase = val.squeeze_() else: __UpperCamelCase = val return orig_state_dict def A ( ) -> Any: __UpperCamelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' __UpperCamelCase = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def A ( snake_case :Union[str, Any] , snake_case :Optional[Any] , snake_case :List[str]="groupvit-gcc-yfcc" , snake_case :Tuple=False ) -> Optional[Any]: __UpperCamelCase = GroupViTConfig() __UpperCamelCase = GroupViTModel(__A ).eval() __UpperCamelCase = torch.load(__A , map_location='cpu' )['model'] __UpperCamelCase = convert_state_dict(__A , __A ) __UpperCamelCase , __UpperCamelCase = model.load_state_dict(__A , strict=__A ) assert missing_keys == ["text_model.embeddings.position_ids"] assert (unexpected_keys == ["multi_label_logit_scale"]) or (len(__A ) == 0) # verify result __UpperCamelCase = CLIPProcessor.from_pretrained('openai/clip-vit-base-patch32' ) __UpperCamelCase = prepare_img() __UpperCamelCase = processor(text=['a photo of a cat', 'a photo of a dog'] , images=__A , padding=__A , return_tensors='pt' ) with torch.no_grad(): __UpperCamelCase = model(**__A ) if model_name == "groupvit-gcc-yfcc": __UpperCamelCase = torch.tensor([[13.3_523, 6.3_629]] ) elif model_name == "groupvit-gcc-redcaps": __UpperCamelCase = torch.tensor([[16.1_873, 8.6_230]] ) else: raise ValueError(f'Model name {model_name} not supported.' ) assert torch.allclose(outputs.logits_per_image , __A , atol=1e-3 ) processor.save_pretrained(__A ) model.save_pretrained(__A ) print('Successfully saved processor and model to' , __A ) if push_to_hub: print('Pushing to the hub...' ) processor.push_to_hub(__A , organization='nielsr' ) model.push_to_hub(__A , organization='nielsr' ) if __name__ == "__main__": UpperCamelCase : Any = argparse.ArgumentParser() parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to dump the processor and PyTorch model." ) parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to GroupViT checkpoint") parser.add_argument( "--model_name", default="groupvit-gccy-fcc", type=str, help="Name of the model. Expecting either 'groupvit-gcc-yfcc' or 'groupvit-gcc-redcaps'", ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model and processor to the 🤗 hub using the provided `model_name`.", ) UpperCamelCase : Optional[int] = parser.parse_args() convert_groupvit_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
357
"""simple docstring""" from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def A ( ) -> Union[str, Any]: __UpperCamelCase = ArgumentParser('Transformers CLI tool' , usage='transformers-cli <command> [<args>]' ) __UpperCamelCase = parser.add_subparsers(help='transformers-cli command helpers' ) # Register commands ConvertCommand.register_subcommand(snake_case ) DownloadCommand.register_subcommand(snake_case ) EnvironmentCommand.register_subcommand(snake_case ) RunCommand.register_subcommand(snake_case ) ServeCommand.register_subcommand(snake_case ) UserCommands.register_subcommand(snake_case ) AddNewModelCommand.register_subcommand(snake_case ) AddNewModelLikeCommand.register_subcommand(snake_case ) LfsCommands.register_subcommand(snake_case ) PTtoTFCommand.register_subcommand(snake_case ) # Let's go __UpperCamelCase = parser.parse_args() if not hasattr(snake_case , 'func' ): parser.print_help() exit(1 ) # Run __UpperCamelCase = args.func(snake_case ) service.run() if __name__ == "__main__": main()
263
0
'''simple docstring''' def UpperCamelCase_ ( ) -> Dict: '''simple docstring''' __lowerCAmelCase = 0 for i in range(1 , 10_01 ): total += i**i return str(snake_case_ )[-10:] if __name__ == "__main__": print(solution())
229
'''simple docstring''' import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class _lowercase ( unittest.TestCase ): '''simple docstring''' @property def a ( self : Any ) -> int: torch.manual_seed(0 ) __lowerCAmelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=("""DownBlock2D""", """AttnDownBlock2D""") , up_block_types=("""AttnUpBlock2D""", """UpBlock2D""") , ) return model def a ( self : Dict ) -> int: __lowerCAmelCase = self.dummy_uncond_unet __lowerCAmelCase = PNDMScheduler() __lowerCAmelCase = PNDMPipeline(unet=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ ) pndm.to(SCREAMING_SNAKE_CASE__ ) pndm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = pndm(generator=SCREAMING_SNAKE_CASE__ , num_inference_steps=20 , output_type="""numpy""" ).images __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = pndm(generator=SCREAMING_SNAKE_CASE__ , num_inference_steps=20 , output_type="""numpy""" , return_dict=SCREAMING_SNAKE_CASE__ )[0] __lowerCAmelCase = image[0, -3:, -3:, -1] __lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __lowerCAmelCase = np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class _lowercase ( unittest.TestCase ): '''simple docstring''' def a ( self : Any ) -> Any: __lowerCAmelCase = """google/ddpm-cifar10-32""" __lowerCAmelCase = UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = PNDMScheduler() __lowerCAmelCase = PNDMPipeline(unet=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ ) pndm.to(SCREAMING_SNAKE_CASE__ ) pndm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) __lowerCAmelCase = torch.manual_seed(0 ) __lowerCAmelCase = pndm(generator=SCREAMING_SNAKE_CASE__ , output_type="""numpy""" ).images __lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __lowerCAmelCase = np.array([0.1_5_6_4, 0.1_4_6_4_5, 0.1_4_0_6, 0.1_4_7_1_5, 0.1_2_4_2_5, 0.1_4_0_4_5, 0.1_3_1_1_5, 0.1_2_1_7_5, 0.1_2_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
229
1
from __future__ import annotations import requests def UpperCamelCase_( _snake_case : str ): """simple docstring""" __a =F'https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty' return requests.get(_snake_case ).json() def UpperCamelCase_( _snake_case : int = 10 ): """simple docstring""" __a ='https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty' __a =requests.get(_snake_case ).json()[:max_stories] return [get_hackernews_story(_snake_case ) for story_id in story_ids] def UpperCamelCase_( _snake_case : int = 10 ): """simple docstring""" __a =hackernews_top_stories(_snake_case ) return "\n".join('* [{title}]({url})'.format(**_snake_case ) for story in stories ) if __name__ == "__main__": print(hackernews_top_stories_as_markdown())
308
import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class __magic_name__ : @staticmethod def __magic_name__ ( *__snake_case , **__snake_case ) -> List[str]: '''simple docstring''' pass def UpperCamelCase_( _snake_case : Image ): """simple docstring""" __a =hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class __magic_name__ ( unittest.TestCase ): SCREAMING_SNAKE_CASE = MODEL_FOR_DEPTH_ESTIMATION_MAPPING def __magic_name__ ( self , __snake_case , __snake_case , __snake_case ) -> Dict: '''simple docstring''' __a =DepthEstimationPipeline(model=__snake_case , image_processor=__snake_case ) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def __magic_name__ ( self , __snake_case , __snake_case ) -> Tuple: '''simple docstring''' __a =depth_estimator('./tests/fixtures/tests_samples/COCO/000000039769.png' ) self.assertEqual({'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )} , __snake_case ) import datasets __a =datasets.load_dataset('hf-internal-testing/fixtures_image_utils' , 'image' , split='test' ) __a =depth_estimator( [ Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ), 'http://images.cocodataset.org/val2017/000000039769.jpg', # RGBA dataset[0]['file'], # LA dataset[1]['file'], # L dataset[2]['file'], ] ) self.assertEqual( [ {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, {'predicted_depth': ANY(torch.Tensor ), 'depth': ANY(Image.Image )}, ] , __snake_case , ) @require_tf @unittest.skip('Depth estimation is not implemented in TF' ) def __magic_name__ ( self ) -> Optional[Any]: '''simple docstring''' pass @slow @require_torch def __magic_name__ ( self ) -> int: '''simple docstring''' __a ='Intel/dpt-large' __a =pipeline('depth-estimation' , model=__snake_case ) __a =depth_estimator('http://images.cocodataset.org/val2017/000000039769.jpg' ) __a =hashimage(outputs['depth'] ) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs['predicted_depth'].max().item() ) , 29.304 ) self.assertEqual(nested_simplify(outputs['predicted_depth'].min().item() ) , 2.662 ) @require_torch def __magic_name__ ( self ) -> Any: '''simple docstring''' # This is highly irregular to have no small tests. self.skipTest('There is not hf-internal-testing tiny model for either GLPN nor DPT' )
308
1
import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class A_ ( unittest.TestCase ): def __init__( self : Union[str, Any] , UpperCAmelCase : Tuple , UpperCAmelCase : List[str]=1_3 , UpperCAmelCase : List[str]=3_0 , UpperCAmelCase : Dict=2 , UpperCAmelCase : str=3 , UpperCAmelCase : int=True , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : int=3_2 , UpperCAmelCase : Union[str, Any]=5 , UpperCAmelCase : str=4 , UpperCAmelCase : List[Any]=3_7 , UpperCAmelCase : Any="gelu" , UpperCAmelCase : Tuple=0.1 , UpperCAmelCase : str=0.1 , UpperCAmelCase : List[Any]=1_0 , UpperCAmelCase : Tuple=0.02 , ) -> int: __lowerCAmelCase: Optional[Any] = parent __lowerCAmelCase: Any = batch_size __lowerCAmelCase: int = image_size __lowerCAmelCase: Optional[Any] = patch_size __lowerCAmelCase: List[str] = num_channels __lowerCAmelCase: Optional[int] = is_training __lowerCAmelCase: List[Any] = use_labels __lowerCAmelCase: Union[str, Any] = hidden_size __lowerCAmelCase: str = num_hidden_layers __lowerCAmelCase: int = num_attention_heads __lowerCAmelCase: List[Any] = intermediate_size __lowerCAmelCase: Any = hidden_act __lowerCAmelCase: Tuple = hidden_dropout_prob __lowerCAmelCase: Union[str, Any] = attention_probs_dropout_prob __lowerCAmelCase: List[Any] = type_sequence_label_size __lowerCAmelCase: Optional[Any] = initializer_range # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) __lowerCAmelCase: Tuple = (image_size // patch_size) ** 2 __lowerCAmelCase: Union[str, Any] = num_patches + 1 def UpperCAmelCase ( self : Tuple ) -> List[Any]: __lowerCAmelCase: int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __lowerCAmelCase: Tuple = ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , ) return config, pixel_values def UpperCAmelCase ( self : Any , UpperCAmelCase : Dict , UpperCAmelCase : Union[str, Any] ) -> Optional[Any]: __lowerCAmelCase: List[Any] = FlaxViTModel(config=UpperCAmelCase ) __lowerCAmelCase: Any = model(UpperCAmelCase ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) __lowerCAmelCase: List[str] = (self.image_size, self.image_size) __lowerCAmelCase: Optional[int] = (self.patch_size, self.patch_size) __lowerCAmelCase: List[Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, num_patches + 1, self.hidden_size) ) def UpperCAmelCase ( self : int , UpperCAmelCase : Any , UpperCAmelCase : Optional[int] ) -> Any: __lowerCAmelCase: Any = self.type_sequence_label_size __lowerCAmelCase: Tuple = FlaxViTForImageClassification(config=UpperCAmelCase ) __lowerCAmelCase: str = model(UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images __lowerCAmelCase: Tuple = 1 __lowerCAmelCase: Union[str, Any] = FlaxViTForImageClassification(UpperCAmelCase ) __lowerCAmelCase: Tuple = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __lowerCAmelCase: str = model(UpperCAmelCase ) def UpperCAmelCase ( self : List[str] ) -> Optional[Any]: __lowerCAmelCase: List[str] = self.prepare_config_and_inputs() ( ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ): List[str] = config_and_inputs __lowerCAmelCase: Optional[Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_flax class A_ ( snake_case__ , unittest.TestCase ): _lowercase : Dict = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def UpperCAmelCase ( self : Optional[int] ) -> None: __lowerCAmelCase: List[Any] = FlaxViTModelTester(self ) __lowerCAmelCase: Any = ConfigTester(self , config_class=UpperCAmelCase , has_text_modality=UpperCAmelCase , hidden_size=3_7 ) def UpperCAmelCase ( self : Optional[Any] ) -> Any: self.config_tester.run_common_tests() def UpperCAmelCase ( self : Dict ) -> Any: __lowerCAmelCase: Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase ) def UpperCAmelCase ( self : int ) -> Dict: __lowerCAmelCase: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase ) def UpperCAmelCase ( self : str ) -> Tuple: __lowerCAmelCase , __lowerCAmelCase: Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCAmelCase: Any = model_class(UpperCAmelCase ) __lowerCAmelCase: List[str] = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowerCAmelCase: Any = [*signature.parameters.keys()] __lowerCAmelCase: Union[str, Any] = ['pixel_values'] self.assertListEqual(arg_names[:1] , UpperCAmelCase ) def UpperCAmelCase ( self : Dict ) -> List[str]: __lowerCAmelCase , __lowerCAmelCase: Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __lowerCAmelCase: Optional[int] = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) __lowerCAmelCase: Union[str, Any] = model_class(UpperCAmelCase ) @jax.jit def model_jitted(UpperCAmelCase : str , **UpperCAmelCase : Union[str, Any] ): return model(pixel_values=UpperCAmelCase , **UpperCAmelCase ) with self.subTest('JIT Enabled' ): __lowerCAmelCase: Optional[Any] = model_jitted(**UpperCAmelCase ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): __lowerCAmelCase: Optional[Any] = model_jitted(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for jitted_output, output in zip(UpperCAmelCase , UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) @slow def UpperCAmelCase ( self : Optional[int] ) -> List[Any]: for model_class_name in self.all_model_classes: __lowerCAmelCase: Optional[Any] = model_class_name.from_pretrained('google/vit-base-patch16-224' ) __lowerCAmelCase: List[str] = model(np.ones((1, 3, 2_2_4, 2_2_4) ) ) self.assertIsNotNone(UpperCAmelCase )
322
import importlib import math import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Tuple, Union import flax import jax.numpy as jnp from ..utils import BaseOutput _a = '''scheduler_config.json''' class A_ ( snake_case__ ): _lowercase : Optional[Any] = 1 _lowercase : Tuple = 2 _lowercase : Dict = 3 _lowercase : int = 4 _lowercase : Optional[Any] = 5 @dataclass class A_ ( snake_case__ ): _lowercase : jnp.ndarray class A_ : _lowercase : Optional[int] = SCHEDULER_CONFIG_NAME _lowercase : Dict = ['dtype'] _lowercase : int = [] _lowercase : Union[str, Any] = True @classmethod def UpperCAmelCase ( cls : Union[str, Any] , UpperCAmelCase : Dict[str, Any] = None , UpperCAmelCase : Optional[str] = None , UpperCAmelCase : List[str]=False , **UpperCAmelCase : Optional[int] , ) -> Tuple: __lowerCAmelCase , __lowerCAmelCase: Optional[Any] = cls.load_config( pretrained_model_name_or_path=UpperCAmelCase , subfolder=UpperCAmelCase , return_unused_kwargs=UpperCAmelCase , **UpperCAmelCase , ) __lowerCAmelCase , __lowerCAmelCase: Optional[Any] = cls.from_config(UpperCAmelCase , return_unused_kwargs=UpperCAmelCase , **UpperCAmelCase ) if hasattr(UpperCAmelCase , 'create_state' ) and getattr(UpperCAmelCase , 'has_state' , UpperCAmelCase ): __lowerCAmelCase: Dict = scheduler.create_state() if return_unused_kwargs: return scheduler, state, unused_kwargs return scheduler, state def UpperCAmelCase ( self : Tuple , UpperCAmelCase : Union[str, os.PathLike] , UpperCAmelCase : bool = False , **UpperCAmelCase : Any ) -> List[str]: self.save_config(save_directory=UpperCAmelCase , push_to_hub=UpperCAmelCase , **UpperCAmelCase ) @property def UpperCAmelCase ( self : str ) -> Dict: return self._get_compatibles() @classmethod def UpperCAmelCase ( cls : Optional[int] ) -> Any: __lowerCAmelCase: Optional[int] = list(set([cls.__name__] + cls._compatibles ) ) __lowerCAmelCase: Dict = importlib.import_module(__name__.split('.' )[0] ) __lowerCAmelCase: Dict = [ getattr(UpperCAmelCase , UpperCAmelCase ) for c in compatible_classes_str if hasattr(UpperCAmelCase , UpperCAmelCase ) ] return compatible_classes def _a ( SCREAMING_SNAKE_CASE : jnp.ndarray , SCREAMING_SNAKE_CASE : Tuple[int] ) -> jnp.ndarray: """simple docstring""" assert len(SCREAMING_SNAKE_CASE ) >= x.ndim return jnp.broadcast_to(x.reshape(x.shape + (1,) * (len(SCREAMING_SNAKE_CASE ) - x.ndim) ) , SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Any=0.9_9_9 , SCREAMING_SNAKE_CASE : List[Any]=jnp.floataa ) -> jnp.ndarray: """simple docstring""" def alpha_bar(SCREAMING_SNAKE_CASE : str ): return math.cos((time_step + 0.0_0_8) / 1.0_0_8 * math.pi / 2 ) ** 2 __lowerCAmelCase: str = [] for i in range(SCREAMING_SNAKE_CASE ): __lowerCAmelCase: Union[str, Any] = i / num_diffusion_timesteps __lowerCAmelCase: List[str] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar(SCREAMING_SNAKE_CASE ) / alpha_bar(SCREAMING_SNAKE_CASE ) , SCREAMING_SNAKE_CASE ) ) return jnp.array(SCREAMING_SNAKE_CASE , dtype=SCREAMING_SNAKE_CASE ) @flax.struct.dataclass class A_ : _lowercase : jnp.ndarray _lowercase : jnp.ndarray _lowercase : jnp.ndarray @classmethod def UpperCAmelCase ( cls : str , UpperCAmelCase : Optional[int] ) -> Any: __lowerCAmelCase: str = scheduler.config if config.trained_betas is not None: __lowerCAmelCase: Tuple = jnp.asarray(config.trained_betas , dtype=scheduler.dtype ) elif config.beta_schedule == "linear": __lowerCAmelCase: Any = jnp.linspace(config.beta_start , config.beta_end , config.num_train_timesteps , dtype=scheduler.dtype ) elif config.beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. __lowerCAmelCase: List[Any] = ( jnp.linspace( config.beta_start**0.5 , config.beta_end**0.5 , config.num_train_timesteps , dtype=scheduler.dtype ) ** 2 ) elif config.beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule __lowerCAmelCase: str = betas_for_alpha_bar(config.num_train_timesteps , dtype=scheduler.dtype ) else: raise NotImplementedError( F'''beta_schedule {config.beta_schedule} is not implemented for scheduler {scheduler.__class__.__name__}''' ) __lowerCAmelCase: Optional[Any] = 1.0 - betas __lowerCAmelCase: Optional[Any] = jnp.cumprod(UpperCAmelCase , axis=0 ) return cls( alphas=UpperCAmelCase , betas=UpperCAmelCase , alphas_cumprod=UpperCAmelCase , ) def _a ( SCREAMING_SNAKE_CASE : CommonSchedulerState , SCREAMING_SNAKE_CASE : jnp.ndarray , SCREAMING_SNAKE_CASE : jnp.ndarray , SCREAMING_SNAKE_CASE : jnp.ndarray ) -> int: """simple docstring""" __lowerCAmelCase: Optional[int] = state.alphas_cumprod __lowerCAmelCase: str = alphas_cumprod[timesteps] ** 0.5 __lowerCAmelCase: Any = sqrt_alpha_prod.flatten() __lowerCAmelCase: Any = broadcast_to_shape_from_left(SCREAMING_SNAKE_CASE , original_samples.shape ) __lowerCAmelCase: Any = (1 - alphas_cumprod[timesteps]) ** 0.5 __lowerCAmelCase: str = sqrt_one_minus_alpha_prod.flatten() __lowerCAmelCase: str = broadcast_to_shape_from_left(SCREAMING_SNAKE_CASE , original_samples.shape ) return sqrt_alpha_prod, sqrt_one_minus_alpha_prod def _a ( SCREAMING_SNAKE_CASE : CommonSchedulerState , SCREAMING_SNAKE_CASE : jnp.ndarray , SCREAMING_SNAKE_CASE : jnp.ndarray , SCREAMING_SNAKE_CASE : jnp.ndarray ) -> str: """simple docstring""" __lowerCAmelCase , __lowerCAmelCase: Union[str, Any] = get_sqrt_alpha_prod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Optional[int] = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples def _a ( SCREAMING_SNAKE_CASE : CommonSchedulerState , SCREAMING_SNAKE_CASE : jnp.ndarray , SCREAMING_SNAKE_CASE : jnp.ndarray , SCREAMING_SNAKE_CASE : jnp.ndarray ) -> Any: """simple docstring""" __lowerCAmelCase , __lowerCAmelCase: Tuple = get_sqrt_alpha_prod(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: int = sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample return velocity
322
1
'''simple docstring''' class UpperCamelCase__ : """simple docstring""" def __init__( self , snake_case__ = "" , snake_case__ = False ): '''simple docstring''' _lowerCAmelCase : dict[str, RadixNode] = {} # A node will be a leaf if the tree contains its word _lowerCAmelCase : Optional[int] = is_leaf _lowerCAmelCase : str = prefix def a ( self , snake_case__ ): '''simple docstring''' _lowerCAmelCase : Optional[int] = 0 for q, w in zip(self.prefix , snake_case__ ): if q != w: break x += 1 return self.prefix[:x], self.prefix[x:], word[x:] def a ( self , snake_case__ ): '''simple docstring''' for word in words: self.insert(snake_case__ ) def a ( self , snake_case__ ): '''simple docstring''' if self.prefix == word: _lowerCAmelCase : List[Any] = True # Case 2: The node has no edges that have a prefix to the word # Solution: We create an edge from the current node to a new one # containing the word elif word[0] not in self.nodes: _lowerCAmelCase : Tuple = RadixNode(prefix=snake_case__ , is_leaf=snake_case__ ) else: _lowerCAmelCase : str = self.nodes[word[0]] _lowerCAmelCase : Optional[Any] = incoming_node.match( snake_case__ ) # Case 3: The node prefix is equal to the matching # Solution: We insert remaining word on the next node if remaining_prefix == "": self.nodes[matching_string[0]].insert(snake_case__ ) # Case 4: The word is greater equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: _lowerCAmelCase : Optional[Any] = remaining_prefix _lowerCAmelCase : Optional[Any] = self.nodes[matching_string[0]] _lowerCAmelCase : str = RadixNode(snake_case__ , snake_case__ ) _lowerCAmelCase : List[str] = aux_node if remaining_word == "": _lowerCAmelCase : int = True else: self.nodes[matching_string[0]].insert(snake_case__ ) def a ( self , snake_case__ ): '''simple docstring''' _lowerCAmelCase : Optional[int] = self.nodes.get(word[0] , snake_case__ ) if not incoming_node: return False else: _lowerCAmelCase : Dict = incoming_node.match( snake_case__ ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # This applies when the word and the prefix are equal elif remaining_word == "": return incoming_node.is_leaf # We have word remaining so we check the next node else: return incoming_node.find(snake_case__ ) def a ( self , snake_case__ ): '''simple docstring''' _lowerCAmelCase : Tuple = self.nodes.get(word[0] , snake_case__ ) if not incoming_node: return False else: _lowerCAmelCase : str = incoming_node.match( snake_case__ ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # We have word remaining so we check the next node elif remaining_word != "": return incoming_node.delete(snake_case__ ) else: # If it is not a leaf, we don't have to delete if not incoming_node.is_leaf: return False else: # We delete the nodes if no edges go from it if len(incoming_node.nodes ) == 0: del self.nodes[word[0]] # We merge the current node with its only child if len(self.nodes ) == 1 and not self.is_leaf: _lowerCAmelCase : List[str] = list(self.nodes.values() )[0] _lowerCAmelCase : int = merging_node.is_leaf self.prefix += merging_node.prefix _lowerCAmelCase : Tuple = merging_node.nodes # If there is more than 1 edge, we just mark it as non-leaf elif len(incoming_node.nodes ) > 1: _lowerCAmelCase : Tuple = False # If there is 1 edge, we merge it with its child else: _lowerCAmelCase : List[str] = list(incoming_node.nodes.values() )[0] _lowerCAmelCase : Any = merging_node.is_leaf incoming_node.prefix += merging_node.prefix _lowerCAmelCase : Any = merging_node.nodes return True def a ( self , snake_case__ = 0 ): '''simple docstring''' if self.prefix != "": print('-' * height , self.prefix , ' (leaf)' if self.is_leaf else '' ) for value in self.nodes.values(): value.print_tree(height + 1 ) def lowercase (): """simple docstring""" _lowerCAmelCase : Tuple = "banana bananas bandana band apple all beast".split() _lowerCAmelCase : Union[str, Any] = RadixNode() root.insert_many(__lowerCamelCase ) assert all(root.find(__lowerCamelCase ) for word in words ) assert not root.find('bandanas' ) assert not root.find('apps' ) root.delete('all' ) assert not root.find('all' ) root.delete('banana' ) assert not root.find('banana' ) assert root.find('bananas' ) return True def lowercase (): """simple docstring""" assert test_trie() def lowercase (): """simple docstring""" _lowerCAmelCase : List[str] = RadixNode() _lowerCAmelCase : List[Any] = "banana bananas bandanas bandana band apple all beast".split() root.insert_many(__lowerCamelCase ) print('Words:' , __lowerCamelCase ) print('Tree:' ) root.print_tree() if __name__ == "__main__": main()
360
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase : List[Any] = logging.get_logger(__name__) lowerCAmelCase : Tuple = { """shi-labs/nat-mini-in1k-224""": """https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json""", # See all Nat models at https://huggingface.co/models?filter=nat } class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): """simple docstring""" __magic_name__ = "nat" __magic_name__ = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self , snake_case__=4 , snake_case__=3 , snake_case__=64 , snake_case__=[3, 4, 6, 5] , snake_case__=[2, 4, 8, 16] , snake_case__=7 , snake_case__=3.0 , snake_case__=True , snake_case__=0.0 , snake_case__=0.0 , snake_case__=0.1 , snake_case__="gelu" , snake_case__=0.02 , snake_case__=1E-5 , snake_case__=0.0 , snake_case__=None , snake_case__=None , **snake_case__ , ): '''simple docstring''' super().__init__(**snake_case__ ) _lowerCAmelCase : Union[str, Any] = patch_size _lowerCAmelCase : List[str] = num_channels _lowerCAmelCase : Tuple = embed_dim _lowerCAmelCase : Any = depths _lowerCAmelCase : Dict = len(snake_case__ ) _lowerCAmelCase : str = num_heads _lowerCAmelCase : Dict = kernel_size _lowerCAmelCase : Union[str, Any] = mlp_ratio _lowerCAmelCase : int = qkv_bias _lowerCAmelCase : Optional[Any] = hidden_dropout_prob _lowerCAmelCase : Union[str, Any] = attention_probs_dropout_prob _lowerCAmelCase : List[str] = drop_path_rate _lowerCAmelCase : Union[str, Any] = hidden_act _lowerCAmelCase : Tuple = layer_norm_eps _lowerCAmelCase : Dict = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model _lowerCAmelCase : str = int(embed_dim * 2 ** (len(snake_case__ ) - 1) ) _lowerCAmelCase : Any = layer_scale_init_value _lowerCAmelCase : Any = ['stem'] + [F'stage{idx}' for idx in range(1 , len(snake_case__ ) + 1 )] _lowerCAmelCase , _lowerCAmelCase : str = get_aligned_output_features_output_indices( out_features=snake_case__ , out_indices=snake_case__ , stage_names=self.stage_names )
25
0
import os from shutil import copyfile from typing import List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _snake_case : List[Any] = logging.get_logger(__name__) _snake_case : str = {'vocab_file': 'sentencepiece.model'} _snake_case : Optional[Any] = { 'vocab_file': { 'google/rembert': 'https://huggingface.co/google/rembert/resolve/main/sentencepiece.model', }, } _snake_case : Optional[Any] = { 'google/rembert': 256, } class _UpperCAmelCase ( _UpperCamelCase ): """simple docstring""" a_ = VOCAB_FILES_NAMES a_ = PRETRAINED_VOCAB_FILES_MAP a_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : Optional[int] , lowerCAmelCase_ : Tuple , lowerCAmelCase_ : Optional[Any]=False , lowerCAmelCase_ : Any=True , lowerCAmelCase_ : List[Any]=True , lowerCAmelCase_ : Union[str, Any]="[CLS]" , lowerCAmelCase_ : Tuple="[SEP]" , lowerCAmelCase_ : Optional[int]="[UNK]" , lowerCAmelCase_ : List[Any]="[SEP]" , lowerCAmelCase_ : Any="[PAD]" , lowerCAmelCase_ : Union[str, Any]="[CLS]" , lowerCAmelCase_ : Dict="[MASK]" , **lowerCAmelCase_ : Optional[int] , ) -> Optional[int]: super().__init__( do_lower_case=lowerCAmelCase_ , remove_space=lowerCAmelCase_ , keep_accents=lowerCAmelCase_ , bos_token=lowerCAmelCase_ , eos_token=lowerCAmelCase_ , unk_token=lowerCAmelCase_ , sep_token=lowerCAmelCase_ , pad_token=lowerCAmelCase_ , cls_token=lowerCAmelCase_ , mask_token=lowerCAmelCase_ , **lowerCAmelCase_ , ) __lowerCAmelCase = do_lower_case __lowerCAmelCase = remove_space __lowerCAmelCase = keep_accents __lowerCAmelCase = vocab_file __lowerCAmelCase = spm.SentencePieceProcessor() self.sp_model.Load(lowerCAmelCase_ ) @property def lowercase ( self : Dict ) -> List[str]: return len(self.sp_model ) def lowercase ( self : Dict ) -> Dict: __lowerCAmelCase = {self.convert_ids_to_tokens(lowerCAmelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : int ) -> Any: __lowerCAmelCase = self.__dict__.copy() __lowerCAmelCase = None return state def __setstate__( self : int , lowerCAmelCase_ : str ) -> Union[str, Any]: __lowerCAmelCase = d __lowerCAmelCase = spm.SentencePieceProcessor() self.sp_model.Load(self.vocab_file ) def lowercase ( self : List[Any] , lowerCAmelCase_ : Optional[int] , lowerCAmelCase_ : int=False ) -> Optional[Any]: __lowerCAmelCase = self.sp_model.EncodeAsPieces(lowerCAmelCase_ ) return pieces def lowercase ( self : int , lowerCAmelCase_ : Union[str, Any] ) -> Optional[int]: return self.sp_model.PieceToId(lowerCAmelCase_ ) def lowercase ( self : Optional[Any] , lowerCAmelCase_ : str ) -> Optional[Any]: return self.sp_model.IdToPiece(lowerCAmelCase_ ) def lowercase ( self : str , lowerCAmelCase_ : Dict ) -> List[str]: __lowerCAmelCase = self.sp_model.decode_pieces(lowerCAmelCase_ ) return out_string def lowercase ( self : str , lowerCAmelCase_ : List[int] , lowerCAmelCase_ : Optional[List[int]] = None ) -> List[int]: __lowerCAmelCase = [self.sep_token_id] __lowerCAmelCase = [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 lowercase ( self : Tuple , lowerCAmelCase_ : List[int] , lowerCAmelCase_ : Optional[List[int]] = None , lowerCAmelCase_ : bool = False ) -> List[int]: if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(lowerCAmelCase_ )) + [1] + ([0] * len(lowerCAmelCase_ )) + [1] return [1] + ([0] * len(lowerCAmelCase_ )) + [1] def lowercase ( self : Optional[int] , lowerCAmelCase_ : List[int] , lowerCAmelCase_ : Optional[List[int]] = None ) -> List[int]: __lowerCAmelCase = [self.sep_token_id] __lowerCAmelCase = [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 lowercase ( self : Optional[int] , lowerCAmelCase_ : str , lowerCAmelCase_ : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(lowerCAmelCase_ ): logger.error('Vocabulary path ({}) should be a directory'.format(lowerCAmelCase_ ) ) return __lowerCAmelCase = os.path.join( lowerCAmelCase_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCAmelCase_ ): copyfile(self.vocab_file , lowerCAmelCase_ ) return (out_vocab_file,)
284
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case : List[Any] = { 'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'], 'tokenization_biogpt': ['BioGptTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Optional[int] = [ 'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BioGptForCausalLM', 'BioGptForTokenClassification', 'BioGptForSequenceClassification', 'BioGptModel', 'BioGptPreTrainedModel', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys _snake_case : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
284
1
'''simple docstring''' from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration lowerCAmelCase : int ='''facebook/wmt19-en-de''' lowerCAmelCase : int =FSMTTokenizer.from_pretrained(mname) # get the correct vocab sizes, etc. from the master model lowerCAmelCase : List[Any] =FSMTConfig.from_pretrained(mname) config.update( dict( d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) ) lowerCAmelCase : List[str] =FSMTForConditionalGeneration(config) print(F'''num of params {tiny_model.num_parameters()}''') # Test lowerCAmelCase : Tuple =tokenizer(['''Making tiny model'''], return_tensors='''pt''') lowerCAmelCase : Union[str, Any] =tiny_model(**batch) print('''test output:''', len(outputs.logits[0])) # Save lowerCAmelCase : Optional[int] ='''tiny-wmt19-en-de''' tiny_model.half() # makes it smaller tiny_model.save_pretrained(mname_tiny) tokenizer.save_pretrained(mname_tiny) print(F'''Generated {mname_tiny}''') # Upload # transformers-cli upload tiny-wmt19-en-de
371
'''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 lowerCAmelCase : Any ={ '''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 } lowerCAmelCase : int =logging.get_logger(__name__) class a_ ( _lowerCAmelCase ): __A = "maskformer" __A = {"hidden_size": "mask_feature_size"} __A = ["resnet", "swin"] __A = ["detr"] def __init__( self : List[Any] , lowercase : int = 256 , lowercase : int = 256 , lowercase : float = 0.1 , lowercase : bool = False , lowercase : Optional[Dict] = None , lowercase : Optional[Dict] = None , lowercase : float = 0.02 , lowercase : float = 1.0 , lowercase : float = 1.0 , lowercase : float = 1.0 , lowercase : float = 20.0 , lowercase : Optional[bool] = None , **lowercase : Any , ): """simple docstring""" if backbone_config is None: # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k lowercase_ :Any = 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(lowercase , lowercase ): lowercase_ :Optional[int] = backbone_config.pop("model_type" ) lowercase_ :Optional[int] = CONFIG_MAPPING[backbone_model_type] lowercase_ :int = config_class.from_dict(lowercase ) # 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 lowercase_ :Optional[Any] = DetrConfig() else: # verify that the decoder is supported lowercase_ :Tuple = ( decoder_config.pop("model_type" ) if isinstance(lowercase , lowercase ) 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(lowercase , lowercase ): lowercase_ :str = CONFIG_MAPPING[decoder_type] lowercase_ :List[str] = config_class.from_dict(lowercase ) lowercase_ :str = backbone_config lowercase_ :Union[str, Any] = decoder_config # main feature dimension for the model lowercase_ :Any = fpn_feature_size lowercase_ :Optional[int] = mask_feature_size # initializer lowercase_ :List[Any] = init_std lowercase_ :Union[str, Any] = init_xavier_std # Hungarian matcher && loss lowercase_ :List[str] = cross_entropy_weight lowercase_ :int = dice_weight lowercase_ :List[str] = mask_weight lowercase_ :Optional[Any] = use_auxiliary_loss lowercase_ :str = no_object_weight lowercase_ :int = output_auxiliary_logits lowercase_ :Optional[Any] = self.decoder_config.encoder_attention_heads lowercase_ :int = self.decoder_config.num_hidden_layers super().__init__(**lowercase ) @classmethod def lowercase__ ( cls : Tuple , lowercase : PretrainedConfig , lowercase : PretrainedConfig , **lowercase : Union[str, Any] ): """simple docstring""" return cls( backbone_config=lowercase , decoder_config=lowercase , **lowercase , ) def lowercase__ ( self : Optional[Any] ): """simple docstring""" lowercase_ :str = copy.deepcopy(self.__dict__ ) lowercase_ :int = self.backbone_config.to_dict() lowercase_ :List[Any] = self.decoder_config.to_dict() lowercase_ :Optional[Any] = self.__class__.model_type return output
147
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowerCAmelCase : Optional[int] = logging.get_logger(__name__) __lowerCAmelCase : Tuple = { 'distilbert-base-uncased': 'https://huggingface.co/distilbert-base-uncased/resolve/main/config.json', 'distilbert-base-uncased-distilled-squad': ( 'https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json' ), 'distilbert-base-cased': 'https://huggingface.co/distilbert-base-cased/resolve/main/config.json', 'distilbert-base-cased-distilled-squad': ( 'https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json' ), 'distilbert-base-german-cased': 'https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json', 'distilbert-base-multilingual-cased': ( 'https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json' ), 'distilbert-base-uncased-finetuned-sst-2-english': ( 'https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json' ), } class snake_case__ (_UpperCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = """distilbert""" SCREAMING_SNAKE_CASE_ : Optional[Any] = { """hidden_size""": """dim""", """num_attention_heads""": """n_heads""", """num_hidden_layers""": """n_layers""", } def __init__( self : int , __lowerCamelCase : Tuple=3_05_22 , __lowerCamelCase : Any=5_12 , __lowerCamelCase : List[str]=False , __lowerCamelCase : List[Any]=6 , __lowerCamelCase : List[str]=12 , __lowerCamelCase : Optional[Any]=7_68 , __lowerCamelCase : Any=4 * 7_68 , __lowerCamelCase : Tuple=0.1 , __lowerCamelCase : str=0.1 , __lowerCamelCase : str="gelu" , __lowerCamelCase : str=0.02 , __lowerCamelCase : str=0.1 , __lowerCamelCase : Any=0.2 , __lowerCamelCase : Any=0 , **__lowerCamelCase : Dict , ) -> int: a = vocab_size a = max_position_embeddings a = sinusoidal_pos_embds a = n_layers a = n_heads a = dim a = hidden_dim a = dropout a = attention_dropout a = activation a = initializer_range a = qa_dropout a = seq_classif_dropout super().__init__(**__lowerCamelCase , pad_token_id=__lowerCamelCase ) class snake_case__ (_UpperCamelCase ): """simple docstring""" @property def __UpperCAmelCase ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": a = {0: "batch", 1: "choice", 2: "sequence"} else: a = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
107
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __lowerCAmelCase : str = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece @require_tokenizers class snake_case__ (_UpperCamelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = XLMRobertaTokenizer SCREAMING_SNAKE_CASE_ : int = XLMRobertaTokenizerFast SCREAMING_SNAKE_CASE_ : str = True SCREAMING_SNAKE_CASE_ : Optional[Any] = True def __UpperCAmelCase ( self : int ) -> List[str]: super().setUp() # We have a SentencePiece fixture for testing a = XLMRobertaTokenizer(__lowerCamelCase , keep_accents=__lowerCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __UpperCAmelCase ( self : List[str] ) -> Any: a = "<pad>" a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCamelCase ) , __lowerCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCamelCase ) , __lowerCamelCase ) def __UpperCAmelCase ( self : Dict ) -> Optional[Any]: a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(vocab_keys[-1] , "<mask>" ) self.assertEqual(len(__lowerCamelCase ) , 10_02 ) def __UpperCAmelCase ( self : List[Any] ) -> Any: self.assertEqual(self.get_tokenizer().vocab_size , 10_02 ) def __UpperCAmelCase ( self : Dict ) -> List[str]: a = XLMRobertaTokenizer(__lowerCamelCase , keep_accents=__lowerCamelCase ) a = tokenizer.tokenize("This is a test" ) self.assertListEqual(__lowerCamelCase , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__lowerCamelCase ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) a = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( __lowerCamelCase , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) a = tokenizer.convert_tokens_to_ids(__lowerCamelCase ) self.assertListEqual( __lowerCamelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) a = tokenizer.convert_ids_to_tokens(__lowerCamelCase ) self.assertListEqual( __lowerCamelCase , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) def __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return a = (self.rust_tokenizer_class, "hf-internal-testing/tiny-xlm-roberta", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): a = self.rust_tokenizer_class.from_pretrained(__lowerCamelCase , **__lowerCamelCase ) a = self.tokenizer_class.from_pretrained(__lowerCamelCase , **__lowerCamelCase ) a = tempfile.mkdtemp() a = tokenizer_r.save_pretrained(__lowerCamelCase ) a = tokenizer_p.save_pretrained(__lowerCamelCase ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) a = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(__lowerCamelCase , __lowerCamelCase ) # Checks everything loads correctly in the same way a = tokenizer_r.from_pretrained(__lowerCamelCase ) a = tokenizer_p.from_pretrained(__lowerCamelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCamelCase , __lowerCamelCase ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(__lowerCamelCase ) # Save tokenizer rust, legacy_format=True a = tempfile.mkdtemp() a = tokenizer_r.save_pretrained(__lowerCamelCase , legacy_format=__lowerCamelCase ) a = tokenizer_p.save_pretrained(__lowerCamelCase ) # Checks it save with the same files self.assertSequenceEqual(__lowerCamelCase , __lowerCamelCase ) # Checks everything loads correctly in the same way a = tokenizer_r.from_pretrained(__lowerCamelCase ) a = tokenizer_p.from_pretrained(__lowerCamelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCamelCase , __lowerCamelCase ) ) shutil.rmtree(__lowerCamelCase ) # Save tokenizer rust, legacy_format=False a = tempfile.mkdtemp() a = tokenizer_r.save_pretrained(__lowerCamelCase , legacy_format=__lowerCamelCase ) a = tokenizer_p.save_pretrained(__lowerCamelCase ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way a = tokenizer_r.from_pretrained(__lowerCamelCase ) a = tokenizer_p.from_pretrained(__lowerCamelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCamelCase , __lowerCamelCase ) ) shutil.rmtree(__lowerCamelCase ) @cached_property def __UpperCAmelCase ( self : List[str] ) -> Union[str, Any]: return XLMRobertaTokenizer.from_pretrained("xlm-roberta-base" ) def __UpperCAmelCase ( self : List[Any] ) -> List[Any]: with tempfile.NamedTemporaryFile() as f: shutil.copyfile(__lowerCamelCase , f.name ) a = XLMRobertaTokenizer(f.name , keep_accents=__lowerCamelCase ) a = pickle.dumps(__lowerCamelCase ) pickle.loads(__lowerCamelCase ) def __UpperCAmelCase ( self : int ) -> str: if not self.test_rust_tokenizer: return a = self.get_tokenizer() a = self.get_rust_tokenizer() a = "I was born in 92000, and this is falsé." a = tokenizer.tokenize(__lowerCamelCase ) a = rust_tokenizer.tokenize(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase ) a = tokenizer.encode(__lowerCamelCase , add_special_tokens=__lowerCamelCase ) a = rust_tokenizer.encode(__lowerCamelCase , add_special_tokens=__lowerCamelCase ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase ) a = self.get_rust_tokenizer() a = tokenizer.encode(__lowerCamelCase ) a = rust_tokenizer.encode(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase ) @slow def __UpperCAmelCase ( self : Dict ) -> Any: a = "Hello World!" a = [0, 3_53_78, 66_61, 38, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(__lowerCamelCase , self.big_tokenizer.encode(__lowerCamelCase ) ) @slow def __UpperCAmelCase ( self : Tuple ) -> int: a = ( "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will" " add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth" ) a = [ 0, 32_93, 83, 10, 45_52, 49_89, 79_86, 6_78, 10, 59_15, 1_11, 17_94_59, 12_48_50, 4, 60_44, 2_37, 12, 6, 5, 6, 4, 67_80, 7_05, 15, 13_88, 44, 3_78, 1_01_14, 7_11, 1_52, 20, 6, 5, 2_23_76, 6_42, 12_21, 1_51_90, 3_41_53, 4_50, 56_08, 9_59, 11_19, 5_77_02, 1_36, 1_86, 47, 10_98, 2_93_67, 47, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 60_44, 2_37, 62_84, 5_09_01, 5_28, 31, 90, 34, 9_27, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(__lowerCamelCase , self.big_tokenizer.encode(__lowerCamelCase ) ) @slow def __UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: # fmt: off a = {"input_ids": [[0, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [0, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__lowerCamelCase , model_name="xlm-roberta-base" , revision="d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3" , )
107
1
"""simple docstring""" 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 , lowerCAmelCase__ , lowerCAmelCase__=2 , lowerCAmelCase__=True , lowerCAmelCase__=False , lowerCAmelCase__=10 , lowerCAmelCase__=3 , lowerCAmelCase__=32 * 8 , lowerCAmelCase__=32 * 8 , lowerCAmelCase__=4 , lowerCAmelCase__=64 , ) -> Optional[Any]: a : str = parent a : List[Any] = batch_size a : Dict = is_training a : int = use_auxiliary_loss a : int = num_queries a : Tuple = num_channels a : List[str] = min_size a : List[Any] = max_size a : Optional[int] = num_labels a : Dict = hidden_dim a : Optional[Any] = hidden_dim def __a ( self ) -> Any: a : Dict = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( lowerCAmelCase__ ) a : int = torch.ones([self.batch_size, self.min_size, self.max_size] , device=lowerCAmelCase__ ) a : Dict = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=lowerCAmelCase__ ) > 0.5 ).float() a : Tuple = (torch.rand((self.batch_size, self.num_labels) , device=lowerCAmelCase__ ) > 0.5).long() a : Tuple = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def __a ( self ) -> Tuple: a : List[Any] = MaskaFormerConfig( hidden_size=self.hidden_dim , ) a : Dict = self.num_queries a : List[str] = self.num_labels a : Any = [1, 1, 1, 1] a : int = self.num_channels a : Optional[Any] = 64 a : Tuple = 128 a : Tuple = self.hidden_dim a : str = self.hidden_dim a : Dict = self.hidden_dim return config def __a ( self ) -> Union[str, Any]: a, a, a, a, a : str = self.prepare_config_and_inputs() a : List[str] = {"pixel_values": pixel_values, "pixel_mask": pixel_mask} return config, inputs_dict def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> List[Any]: a : List[Any] = output.encoder_hidden_states a : Dict = output.pixel_decoder_hidden_states a : Optional[int] = output.transformer_decoder_hidden_states self.parent.assertTrue(len(lowerCAmelCase__ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(lowerCAmelCase__ ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(lowerCAmelCase__ ) , config.decoder_layers ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=False ) -> Optional[int]: with torch.no_grad(): a : Any = MaskaFormerModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() a : Dict = model(pixel_values=lowerCAmelCase__ , pixel_mask=lowerCAmelCase__ ) a : Any = model(lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ ) 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(lowerCAmelCase__ , lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Tuple: a : int = MaskaFormerForUniversalSegmentation(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() def comm_check_on_output(lowerCAmelCase__ ): # 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(): a : Optional[Any] = model(pixel_values=lowerCAmelCase__ , pixel_mask=lowerCAmelCase__ ) a : List[str] = model(lowerCAmelCase__ ) comm_check_on_output(lowerCAmelCase__ ) a : Union[str, Any] = model( pixel_values=lowerCAmelCase__ , pixel_mask=lowerCAmelCase__ , mask_labels=lowerCAmelCase__ , class_labels=lowerCAmelCase__ ) comm_check_on_output(lowerCAmelCase__ ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class __UpperCamelCase ( a__ , a__ , unittest.TestCase ): lowerCamelCase : int =(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () lowerCamelCase : int ={"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {} lowerCamelCase : List[str] =False lowerCamelCase : str =False lowerCamelCase : Tuple =False lowerCamelCase : List[str] =False def __a ( self ) -> Tuple: a : Union[str, Any] = MaskaFormerModelTester(self ) a : Dict = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ ) def __a ( self ) -> List[str]: self.config_tester.run_common_tests() def __a ( self ) -> Dict: a, a : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(lowerCAmelCase__ , **lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ ) def __a ( self ) -> int: a : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*lowerCAmelCase__ ) @unittest.skip(reason="Mask2Former does not use inputs_embeds" ) def __a ( self ) -> Optional[Any]: pass @unittest.skip(reason="Mask2Former does not have a get_input_embeddings method" ) def __a ( self ) -> List[str]: pass @unittest.skip(reason="Mask2Former is not a generative model" ) def __a ( self ) -> Optional[Any]: pass @unittest.skip(reason="Mask2Former does not use token embeddings" ) def __a ( self ) -> Tuple: pass @require_torch_multi_gpu @unittest.skip( reason="Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __a ( self ) -> List[str]: pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def __a ( self ) -> List[Any]: pass def __a ( self ) -> Optional[Any]: a, a : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Any = model_class(lowerCAmelCase__ ) a : Union[str, Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : int = [*signature.parameters.keys()] a : List[str] = ["pixel_values"] self.assertListEqual(arg_names[:1] , lowerCAmelCase__ ) @slow def __a ( self ) -> str: for model_name in ["facebook/mask2former-swin-small-coco-instance"]: a : Optional[Any] = MaskaFormerModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) def __a ( self ) -> Union[str, Any]: a : Dict = (self.model_tester.min_size,) * 2 a : List[str] = { "pixel_values": torch.randn((2, 3, *size) , device=lowerCAmelCase__ ), "mask_labels": torch.randn((2, 10, *size) , device=lowerCAmelCase__ ), "class_labels": torch.zeros(2 , 10 , device=lowerCAmelCase__ ).long(), } a : Dict = self.model_tester.get_config() a : List[Any] = MaskaFormerForUniversalSegmentation(lowerCAmelCase__ ).to(lowerCAmelCase__ ) a : Union[str, Any] = model(**lowerCAmelCase__ ) self.assertTrue(outputs.loss is not None ) def __a ( self ) -> Dict: a, a : int = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(lowerCAmelCase__ , **lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ ) def __a ( self ) -> List[str]: a, a : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Optional[int] = model_class(lowerCAmelCase__ ).to(lowerCAmelCase__ ) a : Tuple = model(**lowerCAmelCase__ , output_attentions=lowerCAmelCase__ ) self.assertTrue(outputs.attentions is not None ) def __a ( self ) -> Tuple: if not self.model_tester.is_training: return a : List[Any] = self.all_model_classes[1] a, a, a, a, a : Tuple = self.model_tester.prepare_config_and_inputs() a : Tuple = model_class(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.train() a : Any = model(lowerCAmelCase__ , mask_labels=lowerCAmelCase__ , class_labels=lowerCAmelCase__ ).loss loss.backward() def __a ( self ) -> int: a : str = self.all_model_classes[1] a, a, a, a, a : List[Any] = self.model_tester.prepare_config_and_inputs() a : List[Any] = True a : Tuple = True a : str = model_class(lowerCAmelCase__ ).to(lowerCAmelCase__ ) model.train() a : Optional[Any] = model(lowerCAmelCase__ , mask_labels=lowerCAmelCase__ , class_labels=lowerCAmelCase__ ) a : int = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() a : Union[str, Any] = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() a : int = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() a : Dict = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=lowerCAmelCase__ ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) a : List[str] = 1E-4 def _SCREAMING_SNAKE_CASE ( ) ->List[str]: '''simple docstring''' a : Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_vision @slow class __UpperCamelCase ( unittest.TestCase ): @cached_property def __a ( self ) -> Tuple: return "facebook/mask2former-swin-small-coco-instance" @cached_property def __a ( self ) -> Dict: return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def __a ( self ) -> Tuple: a : int = MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(lowerCAmelCase__ ) a : Dict = self.default_image_processor a : List[Any] = prepare_img() a : List[str] = image_processor(lowerCAmelCase__ , return_tensors="pt" ).to(lowerCAmelCase__ ) a : Dict = 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(lowerCAmelCase__ , (1, 3, 384, 384) ) with torch.no_grad(): a : Tuple = model(**lowerCAmelCase__ ) a : List[str] = torch.tensor( [[-0.2_790, -1.0_717, -1.1_668], [-0.5_128, -0.3_128, -0.4_987], [-0.5_832, 0.1_971, -0.0_197]] ).to(lowerCAmelCase__ ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , lowerCAmelCase__ , atol=lowerCAmelCase__ ) ) a : int = torch.tensor( [[0.8_973, 1.1_847, 1.1_776], [1.1_934, 1.5_040, 1.5_128], [1.1_153, 1.4_486, 1.4_951]] ).to(lowerCAmelCase__ ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , lowerCAmelCase__ , atol=lowerCAmelCase__ ) ) a : List[Any] = torch.tensor( [[2.1_152, 1.7_000, -0.8_603], [1.5_808, 1.8_004, -0.9_353], [1.6_043, 1.7_495, -0.5_999]] ).to(lowerCAmelCase__ ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , lowerCAmelCase__ , atol=lowerCAmelCase__ ) ) def __a ( self ) -> List[Any]: a : Any = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(lowerCAmelCase__ ).eval() a : Tuple = self.default_image_processor a : Optional[Any] = prepare_img() a : Optional[Any] = image_processor(lowerCAmelCase__ , return_tensors="pt" ).to(lowerCAmelCase__ ) a : Tuple = 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(lowerCAmelCase__ , (1, 3, 384, 384) ) with torch.no_grad(): a : Tuple = model(**lowerCAmelCase__ ) # masks_queries_logits a : Tuple = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) a : str = [ [-8.7_839, -9.0_056, -8.8_121], [-7.4_104, -7.0_313, -6.5_401], [-6.6_105, -6.3_427, -6.4_675], ] a : List[str] = torch.tensor(lowerCAmelCase__ ).to(lowerCAmelCase__ ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , lowerCAmelCase__ , atol=lowerCAmelCase__ ) ) # class_queries_logits a : Optional[int] = outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1) ) a : Any = torch.tensor( [ [1.8_324, -8.0_835, -4.1_922], [0.8_450, -9.0_050, -3.6_053], [0.3_045, -7.7_293, -3.0_275], ] ).to(lowerCAmelCase__ ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , lowerCAmelCase__ , atol=lowerCAmelCase__ ) ) def __a ( self ) -> str: a : Optional[int] = MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(lowerCAmelCase__ ).eval() a : Optional[Any] = self.default_image_processor a : Dict = 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" , ) a : Union[str, Any] = inputs["pixel_values"].to(lowerCAmelCase__ ) a : str = [el.to(lowerCAmelCase__ ) for el in inputs["mask_labels"]] a : List[str] = [el.to(lowerCAmelCase__ ) for el in inputs["class_labels"]] with torch.no_grad(): a : Optional[int] = model(**lowerCAmelCase__ ) self.assertTrue(outputs.loss is not None )
79
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging a : Dict = logging.get_logger(__name__) a : List[Any] = { '''s-JoL/Open-Llama-V1''': '''https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json''', } class __UpperCamelCase ( a__ ): lowerCamelCase : Tuple ="""open-llama""" def __init__( self , lowerCAmelCase__=10_0000 , lowerCAmelCase__=4096 , lowerCAmelCase__=1_1008 , lowerCAmelCase__=32 , lowerCAmelCase__=32 , lowerCAmelCase__="silu" , lowerCAmelCase__=2048 , lowerCAmelCase__=0.02 , lowerCAmelCase__=1E-6 , lowerCAmelCase__=True , lowerCAmelCase__=0 , lowerCAmelCase__=1 , lowerCAmelCase__=2 , lowerCAmelCase__=False , lowerCAmelCase__=True , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.1 , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=None , **lowerCAmelCase__ , ) -> Tuple: a : Any = vocab_size a : List[str] = max_position_embeddings a : int = hidden_size a : str = intermediate_size a : List[str] = num_hidden_layers a : int = num_attention_heads a : Dict = hidden_act a : Union[str, Any] = initializer_range a : Tuple = rms_norm_eps a : Union[str, Any] = use_cache a : Union[str, Any] = kwargs.pop( "use_memorry_efficient_attention" , lowerCAmelCase__ ) a : int = hidden_dropout_prob a : Tuple = attention_dropout_prob a : Optional[Any] = use_stable_embedding a : str = shared_input_output_embedding a : str = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , tie_word_embeddings=lowerCAmelCase__ , **lowerCAmelCase__ , ) def __a ( self ) -> Union[str, Any]: if self.rope_scaling is None: return if not isinstance(self.rope_scaling , lowerCAmelCase__ ) or len(self.rope_scaling ) != 2: raise ValueError( "`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, " f"""got {self.rope_scaling}""" ) a : Any = self.rope_scaling.get("type" , lowerCAmelCase__ ) a : List[str] = self.rope_scaling.get("factor" , lowerCAmelCase__ ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}""" ) if rope_scaling_factor is None or not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) or rope_scaling_factor <= 1.0: raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}""" )
79
1
import unittest from parameterized import parameterized from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed 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, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXModel, ) class A : """simple docstring""" def __init__( self : str,lowercase_ : Any,lowercase_ : Tuple=1_3,lowercase_ : str=7,lowercase_ : Tuple=True,lowercase_ : int=True,lowercase_ : List[Any]=True,lowercase_ : List[str]=True,lowercase_ : List[str]=9_9,lowercase_ : List[Any]=6_4,lowercase_ : List[str]=5,lowercase_ : Optional[Any]=4,lowercase_ : Optional[Any]=3_7,lowercase_ : Optional[Any]="gelu",lowercase_ : int=0.1,lowercase_ : str=0.1,lowercase_ : Optional[Any]=5_1_2,lowercase_ : int=1_6,lowercase_ : List[Any]=2,lowercase_ : Union[str, Any]=0.02,lowercase_ : Tuple=3,lowercase_ : List[Any]=4,lowercase_ : str=None,)-> 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__ = vocab_size - 1 def snake_case__ ( self : str )-> Optional[Any]: '''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_labels: A__ = ids_tensor([self.batch_size, self.seq_length],self.num_labels ) A__ = self.get_config() return config, input_ids, input_mask, token_labels def snake_case__ ( self : List[Any] )-> Tuple: '''simple docstring''' return GPTNeoXConfig( vocab_size=self.vocab_size,hidden_size=self.hidden_size,num_hidden_layers=self.num_hidden_layers,num_attention_heads=self.num_attention_heads,intermediate_size=self.intermediate_size,hidden_act=self.hidden_act,hidden_dropout_prob=self.hidden_dropout_prob,attention_probs_dropout_prob=self.attention_probs_dropout_prob,max_position_embeddings=self.max_position_embeddings,type_vocab_size=self.type_vocab_size,is_decoder=lowercase_,initializer_range=self.initializer_range,pad_token_id=self.pad_token_id,) def snake_case__ ( self : Optional[int] )-> Union[str, Any]: '''simple docstring''' A__ , A__ , A__ , A__ = self.prepare_config_and_inputs() A__ = True return config, input_ids, input_mask, token_labels def snake_case__ ( self : Any,lowercase_ : List[Any],lowercase_ : List[Any],lowercase_ : str )-> Any: '''simple docstring''' A__ = GPTNeoXModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_,attention_mask=lowercase_ ) A__ = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case__ ( self : Union[str, Any],lowercase_ : List[str],lowercase_ : Dict,lowercase_ : Optional[Any] )-> Tuple: '''simple docstring''' A__ = True A__ = GPTNeoXModel(lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_,attention_mask=lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case__ ( self : Union[str, Any],lowercase_ : str,lowercase_ : Union[str, Any],lowercase_ : Union[str, Any],lowercase_ : List[str] )-> List[str]: '''simple docstring''' A__ = GPTNeoXForCausalLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_,attention_mask=lowercase_,labels=lowercase_ ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case__ ( self : Optional[int],lowercase_ : Optional[int],lowercase_ : Optional[int],lowercase_ : Dict,lowercase_ : Any )-> int: '''simple docstring''' A__ = self.num_labels A__ = GPTNeoXForQuestionAnswering(lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_,attention_mask=lowercase_ ) self.parent.assertEqual(result.start_logits.shape,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape,(self.batch_size, self.seq_length) ) def snake_case__ ( self : List[str],lowercase_ : List[str],lowercase_ : int,lowercase_ : Union[str, Any],lowercase_ : Optional[int] )-> str: '''simple docstring''' A__ = self.num_labels A__ = GPTNeoXForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() A__ = ids_tensor([self.batch_size],self.type_sequence_label_size ) A__ = model(lowercase_,attention_mask=lowercase_,labels=lowercase_ ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels) ) def snake_case__ ( self : Any,lowercase_ : Union[str, Any],lowercase_ : List[Any],lowercase_ : Optional[Any],lowercase_ : int )-> Union[str, Any]: '''simple docstring''' A__ = self.num_labels A__ = GPTNeoXForTokenClassification(lowercase_ ) model.to(lowercase_ ) model.eval() A__ = model(lowercase_,attention_mask=lowercase_,labels=lowercase_ ) self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.num_labels) ) def snake_case__ ( self : int,lowercase_ : str,lowercase_ : int,lowercase_ : Union[str, Any] )-> List[Any]: '''simple docstring''' A__ = True A__ = GPTNeoXForCausalLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() # first forward pass A__ = model(lowercase_,attention_mask=lowercase_,use_cache=lowercase_ ) 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(lowercase_,attention_mask=lowercase_,output_hidden_states=lowercase_ ) A__ = output_from_no_past['hidden_states'][0] A__ = model( lowercase_,attention_mask=lowercase_,past_key_values=lowercase_,output_hidden_states=lowercase_,)['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(lowercase_,lowercase_,atol=1E-3 ) ) def snake_case__ ( self : str )-> Union[str, Any]: '''simple docstring''' A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class A ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase = ( ( GPTNeoXModel, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, ) if is_torch_available() else () ) lowerCamelCase = (GPTNeoXForCausalLM,) if is_torch_available() else () lowerCamelCase = ( { 'feature-extraction': GPTNeoXModel, 'question-answering': GPTNeoXForQuestionAnswering, 'text-classification': GPTNeoXForSequenceClassification, 'text-generation': GPTNeoXForCausalLM, 'token-classification': GPTNeoXForTokenClassification, 'zero-shot': GPTNeoXForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False lowerCamelCase = False def snake_case__ ( self : str )-> Tuple: '''simple docstring''' A__ = GPTNeoXModelTester(self ) A__ = ConfigTester(self,config_class=lowercase_,hidden_size=6_4,num_attention_heads=8 ) def snake_case__ ( self : Optional[Any] )-> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def snake_case__ ( self : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' A__ , A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(lowercase_,lowercase_,lowercase_ ) def snake_case__ ( self : Dict )-> List[Any]: '''simple docstring''' A__ , A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(lowercase_,lowercase_,lowercase_ ) def snake_case__ ( self : List[str] )-> Any: '''simple docstring''' A__ , A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs_for_decoder() A__ = None self.model_tester.create_and_check_model_as_decoder(lowercase_,lowercase_,lowercase_ ) def snake_case__ ( self : Optional[Any] )-> str: '''simple docstring''' A__ , A__ , A__ , A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(lowercase_,lowercase_,lowercase_ ) def snake_case__ ( self : Dict )-> Dict: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*lowercase_ ) def snake_case__ ( self : Tuple )-> List[Any]: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowercase_ ) def snake_case__ ( self : Any )-> List[str]: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowercase_ ) def snake_case__ ( self : str )-> Tuple: '''simple docstring''' A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowercase_ ) @unittest.skip(reason='Feed forward chunking is not implemented' ) def snake_case__ ( self : Union[str, Any] )-> Optional[Any]: '''simple docstring''' pass @parameterized.expand([('linear',), ('dynamic',)] ) def snake_case__ ( self : List[str],lowercase_ : Any )-> List[str]: '''simple docstring''' A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = ids_tensor([1, 1_0],config.vocab_size ) A__ = ids_tensor([1, int(config.max_position_embeddings * 1.5 )],config.vocab_size ) set_seed(4_2 ) # Fixed seed at init time so the two models get the same random weights A__ = GPTNeoXModel(lowercase_ ) original_model.to(lowercase_ ) original_model.eval() A__ = original_model(lowercase_ ).last_hidden_state A__ = original_model(lowercase_ ).last_hidden_state set_seed(4_2 ) # Fixed seed at init time so the two models get the same random weights A__ = {'type': scaling_type, 'factor': 10.0} A__ = GPTNeoXModel(lowercase_ ) scaled_model.to(lowercase_ ) scaled_model.eval() A__ = scaled_model(lowercase_ ).last_hidden_state A__ = scaled_model(lowercase_ ).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(lowercase_,lowercase_,atol=1E-5 ) ) else: self.assertFalse(torch.allclose(lowercase_,lowercase_,atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(lowercase_,lowercase_,atol=1E-5 ) ) @require_torch class A ( unittest.TestCase ): """simple docstring""" @slow def snake_case__ ( self : Tuple )-> Union[str, Any]: '''simple docstring''' A__ = AutoTokenizer.from_pretrained('EleutherAI/pythia-410m-deduped' ) for checkpointing in [True, False]: A__ = GPTNeoXForCausalLM.from_pretrained('EleutherAI/pythia-410m-deduped' ) if checkpointing: model.gradient_checkpointing_enable() else: model.gradient_checkpointing_disable() model.to(lowercase_ ) A__ = tokenizer('My favorite food is',return_tensors='pt' ).to(lowercase_ ) # The hub repo. is updated on 2023-04-04, resulting in poor outputs. # See: https://github.com/huggingface/transformers/pull/24193 A__ = 'My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI\'m not sure' A__ = model.generate(**lowercase_,do_sample=lowercase_,max_new_tokens=2_0 ) A__ = tokenizer.batch_decode(lowercase_ )[0] self.assertEqual(lowercase_,lowercase_ )
7
'''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_rembert import RemBertTokenizer else: __lowerCamelCase = None __lowerCamelCase = logging.get_logger(__name__) __lowerCamelCase = {'''vocab_file''': '''sentencepiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __lowerCamelCase = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, '''tokenizer_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/tokenizer.json''', }, } __lowerCamelCase = { '''google/rembert''': 256, } __lowerCamelCase = '''▁''' class A__ ( _snake_case ): lowercase = VOCAB_FILES_NAMES lowercase = PRETRAINED_VOCAB_FILES_MAP lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase = RemBertTokenizer def __init__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=True , UpperCamelCase__=True , UpperCamelCase__=False , UpperCamelCase__="[CLS]" , UpperCamelCase__="[SEP]" , UpperCamelCase__="<unk>" , UpperCamelCase__="[SEP]" , UpperCamelCase__="<pad>" , UpperCamelCase__="[CLS]" , UpperCamelCase__="[MASK]" , **UpperCamelCase__ , ) -> List[Any]: '''simple docstring''' # Mask token behave like a normal word, i.e. include the space before it A_ = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else mask_token super().__init__( UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , do_lower_case=UpperCamelCase__ , remove_space=UpperCamelCase__ , keep_accents=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , **UpperCamelCase__ , ) A_ = do_lower_case A_ = remove_space A_ = keep_accents A_ = vocab_file A_ = False if not self.vocab_file else True def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ = 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 snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( """You should not supply a second sequence if the provided sequence of """ """ids is already formatted with special tokens for the model.""" ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(UpperCamelCase__ )) + [1] + ([0] * len(UpperCamelCase__ )) + [1] return [1] + ([0] * len(UpperCamelCase__ )) + [1] def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ = 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 snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(UpperCamelCase__ ): logger.error("""Vocabulary path ({}) should be a directory""".format(UpperCamelCase__ ) ) return A_ = os.path.join( UpperCamelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCamelCase__ ): copyfile(self.vocab_file , UpperCamelCase__ ) return (out_vocab_file,)
162
0
"""simple docstring""" import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_gpta import GPTaTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation __A = logging.get_logger(__name__) __A = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} __A = { '''vocab_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/vocab.json''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/vocab.json''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/vocab.json''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/vocab.json''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/vocab.json''', }, '''merges_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/merges.txt''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/merges.txt''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/merges.txt''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/merges.txt''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/merges.txt''', }, '''tokenizer_file''': { '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/tokenizer.json''', '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json''', '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/tokenizer.json''', '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json''', '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/tokenizer.json''', }, } __A = { '''gpt2''': 1024, '''gpt2-medium''': 1024, '''gpt2-large''': 1024, '''gpt2-xl''': 1024, '''distilgpt2''': 1024, } class _snake_case ( a__ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] snake_case__ = GPTaTokenizer def __init__( self : Any , UpperCAmelCase : Optional[Any]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : List[Any]=None , UpperCAmelCase : List[Any]="<|endoftext|>" , UpperCAmelCase : Any="<|endoftext|>" , UpperCAmelCase : Tuple="<|endoftext|>" , UpperCAmelCase : Optional[int]=False , **UpperCAmelCase : Optional[int] , ): super().__init__( UpperCAmelCase , UpperCAmelCase , tokenizer_file=UpperCAmelCase , unk_token=UpperCAmelCase , bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , add_prefix_space=UpperCAmelCase , **UpperCAmelCase , ) __lowerCamelCase : Tuple = kwargs.pop("add_bos_token" , UpperCAmelCase ) __lowerCamelCase : Optional[int] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , UpperCAmelCase ) != add_prefix_space: __lowerCamelCase : str = getattr(UpperCAmelCase , pre_tok_state.pop("type" ) ) __lowerCamelCase : Any = add_prefix_space __lowerCamelCase : Any = pre_tok_class(**UpperCAmelCase ) __lowerCamelCase : str = add_prefix_space def lowerCamelCase__ ( self : List[str] , *UpperCAmelCase : Tuple , **UpperCAmelCase : Union[str, Any] ): __lowerCamelCase : str = kwargs.get("is_split_into_words" , UpperCAmelCase ) 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(*UpperCAmelCase , **UpperCAmelCase ) def lowerCamelCase__ ( self : Any , *UpperCAmelCase : Tuple , **UpperCAmelCase : Any ): __lowerCamelCase : Dict = kwargs.get("is_split_into_words" , UpperCAmelCase ) 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(*UpperCAmelCase , **UpperCAmelCase ) def lowerCamelCase__ ( self : Optional[int] , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): __lowerCamelCase : str = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase ) def lowerCamelCase__ ( self : Optional[int] , UpperCAmelCase : "Conversation" ): __lowerCamelCase : Tuple = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) + [self.eos_token_id] ) if len(UpperCAmelCase ) > self.model_max_length: __lowerCamelCase : List[Any] = input_ids[-self.model_max_length :] return input_ids
64
"""simple docstring""" 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 __A = logging.get_logger(__name__) __A = { '''google/efficientnet-b7''': '''https://huggingface.co/google/efficientnet-b7/resolve/main/config.json''', } class _snake_case ( a__ ): snake_case__ = "efficientnet" def __init__( self : Dict , UpperCAmelCase : int = 3 , UpperCAmelCase : int = 600 , UpperCAmelCase : float = 2.0 , UpperCAmelCase : float = 3.1 , UpperCAmelCase : int = 8 , UpperCAmelCase : List[int] = [3, 3, 5, 3, 5, 5, 3] , UpperCAmelCase : List[int] = [32, 16, 24, 40, 80, 112, 192] , UpperCAmelCase : List[int] = [16, 24, 40, 80, 112, 192, 320] , UpperCAmelCase : List[int] = [] , UpperCAmelCase : List[int] = [1, 2, 2, 2, 1, 2, 1] , UpperCAmelCase : List[int] = [1, 2, 2, 3, 3, 4, 1] , UpperCAmelCase : List[int] = [1, 6, 6, 6, 6, 6, 6] , UpperCAmelCase : float = 0.2_5 , UpperCAmelCase : str = "swish" , UpperCAmelCase : int = 2560 , UpperCAmelCase : str = "mean" , UpperCAmelCase : float = 0.0_2 , UpperCAmelCase : float = 0.0_0_1 , UpperCAmelCase : float = 0.9_9 , UpperCAmelCase : float = 0.5 , UpperCAmelCase : float = 0.2 , **UpperCAmelCase : Union[str, Any] , ): super().__init__(**UpperCAmelCase ) __lowerCamelCase : Dict = num_channels __lowerCamelCase : str = image_size __lowerCamelCase : Any = width_coefficient __lowerCamelCase : Any = depth_coefficient __lowerCamelCase : Any = depth_divisor __lowerCamelCase : Optional[Any] = kernel_sizes __lowerCamelCase : Union[str, Any] = in_channels __lowerCamelCase : List[Any] = out_channels __lowerCamelCase : Optional[Any] = depthwise_padding __lowerCamelCase : int = strides __lowerCamelCase : int = num_block_repeats __lowerCamelCase : Optional[Any] = expand_ratios __lowerCamelCase : int = squeeze_expansion_ratio __lowerCamelCase : Any = hidden_act __lowerCamelCase : Optional[Any] = hidden_dim __lowerCamelCase : Union[str, Any] = pooling_type __lowerCamelCase : Optional[Any] = initializer_range __lowerCamelCase : Tuple = batch_norm_eps __lowerCamelCase : Optional[int] = batch_norm_momentum __lowerCamelCase : Any = dropout_rate __lowerCamelCase : List[Any] = drop_connect_rate __lowerCamelCase : int = sum(UpperCAmelCase ) * 4 class _snake_case ( a__ ): snake_case__ = version.parse("1.11" ) @property def lowerCamelCase__ ( self : Union[str, Any] ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowerCamelCase__ ( self : List[Any] ): return 1E-5
64
1
from __future__ import annotations from collections import namedtuple def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> tuple: """simple docstring""" _lowercase =namedtuple('''result''' , '''name value''' ) if (voltage, current, power).count(0 ) != 1: raise ValueError('''Only one argument must be 0''' ) elif power < 0: raise ValueError( '''Power cannot be negative in any electrical/electronics system''' ) elif voltage == 0: return result('''voltage''' , power / current ) elif current == 0: return result('''current''' , power / voltage ) elif power == 0: return result('''power''' , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
5
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 _lowerCAmelCase ( __a ): _lowercase ='''sew''' def __init__( self , _UpperCamelCase=32 , _UpperCamelCase=768 , _UpperCamelCase=12 , _UpperCamelCase=12 , _UpperCamelCase=3_072 , _UpperCamelCase=2 , _UpperCamelCase="gelu" , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=0.0 , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=0.02 , _UpperCamelCase=1e-5 , _UpperCamelCase="group" , _UpperCamelCase="gelu" , _UpperCamelCase=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _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=128 , _UpperCamelCase=16 , _UpperCamelCase=True , _UpperCamelCase=0.05 , _UpperCamelCase=10 , _UpperCamelCase=2 , _UpperCamelCase=0.0 , _UpperCamelCase=10 , _UpperCamelCase=0 , _UpperCamelCase="mean" , _UpperCamelCase=False , _UpperCamelCase=False , _UpperCamelCase=256 , _UpperCamelCase=0 , _UpperCamelCase=1 , _UpperCamelCase=2 , **_UpperCamelCase , ) -> Union[str, Any]: super().__init__(**_UpperCamelCase , pad_token_id=_UpperCamelCase , bos_token_id=_UpperCamelCase , eos_token_id=_UpperCamelCase ) lowerCAmelCase_ = hidden_size lowerCAmelCase_ = feat_extract_norm lowerCAmelCase_ = feat_extract_activation lowerCAmelCase_ = list(_UpperCamelCase ) lowerCAmelCase_ = list(_UpperCamelCase ) lowerCAmelCase_ = list(_UpperCamelCase ) lowerCAmelCase_ = conv_bias lowerCAmelCase_ = num_conv_pos_embeddings lowerCAmelCase_ = num_conv_pos_embedding_groups lowerCAmelCase_ = len(self.conv_dim ) lowerCAmelCase_ = num_hidden_layers lowerCAmelCase_ = intermediate_size lowerCAmelCase_ = squeeze_factor lowerCAmelCase_ = hidden_act lowerCAmelCase_ = num_attention_heads lowerCAmelCase_ = hidden_dropout lowerCAmelCase_ = attention_dropout lowerCAmelCase_ = activation_dropout lowerCAmelCase_ = feat_proj_dropout lowerCAmelCase_ = final_dropout lowerCAmelCase_ = layerdrop lowerCAmelCase_ = layer_norm_eps lowerCAmelCase_ = initializer_range lowerCAmelCase_ = 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 lowerCAmelCase_ = apply_spec_augment lowerCAmelCase_ = mask_time_prob lowerCAmelCase_ = mask_time_length lowerCAmelCase_ = mask_time_min_masks lowerCAmelCase_ = mask_feature_prob lowerCAmelCase_ = mask_feature_length lowerCAmelCase_ = mask_feature_min_masks # ctc loss lowerCAmelCase_ = ctc_loss_reduction lowerCAmelCase_ = ctc_zero_infinity # sequence classification lowerCAmelCase_ = use_weighted_layer_sum lowerCAmelCase_ = classifier_proj_size @property def __a ( self ) -> Optional[Any]: return functools.reduce(operator.mul , self.conv_stride , 1 )
231
0
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case : List[Any] = logging.get_logger(__name__) _snake_case : Dict = { """microsoft/unispeech-sat-base-100h-libri-ft""": ( """https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json""" ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class a (__A ): """simple docstring""" __UpperCAmelCase : int = 'unispeech-sat' def __init__( self : Dict , lowerCamelCase : Tuple=32 , lowerCamelCase : int=768 , lowerCamelCase : Optional[int]=12 , lowerCamelCase : Tuple=12 , lowerCamelCase : Optional[int]=3072 , lowerCamelCase : int="gelu" , lowerCamelCase : Dict=0.1 , lowerCamelCase : str=0.1 , lowerCamelCase : List[Any]=0.1 , lowerCamelCase : Optional[int]=0.0 , lowerCamelCase : str=0.0 , lowerCamelCase : Union[str, Any]=0.1 , lowerCamelCase : List[str]=0.1 , lowerCamelCase : int=0.02 , lowerCamelCase : List[str]=1E-5 , lowerCamelCase : Union[str, Any]="group" , lowerCamelCase : Optional[Any]="gelu" , lowerCamelCase : Optional[int]=(512, 512, 512, 512, 512, 512, 512) , lowerCamelCase : Union[str, Any]=(5, 2, 2, 2, 2, 2, 2) , lowerCamelCase : Dict=(10, 3, 3, 3, 3, 2, 2) , lowerCamelCase : Any=False , lowerCamelCase : Tuple=128 , lowerCamelCase : Dict=16 , lowerCamelCase : List[Any]=False , lowerCamelCase : Dict=True , lowerCamelCase : List[Any]=0.05 , lowerCamelCase : Optional[int]=10 , lowerCamelCase : Optional[int]=2 , lowerCamelCase : Union[str, Any]=0.0 , lowerCamelCase : List[str]=10 , lowerCamelCase : List[Any]=0 , lowerCamelCase : int=320 , lowerCamelCase : Optional[int]=2 , lowerCamelCase : Union[str, Any]=0.1 , lowerCamelCase : str=100 , lowerCamelCase : Union[str, Any]=256 , lowerCamelCase : Tuple=256 , lowerCamelCase : int=0.1 , lowerCamelCase : int="mean" , lowerCamelCase : List[str]=False , lowerCamelCase : List[Any]=False , lowerCamelCase : Optional[Any]=256 , lowerCamelCase : Optional[Any]=(512, 512, 512, 512, 1500) , lowerCamelCase : List[Any]=(5, 3, 3, 1, 1) , lowerCamelCase : Tuple=(1, 2, 3, 1, 1) , lowerCamelCase : Dict=512 , lowerCamelCase : int=0 , lowerCamelCase : List[Any]=1 , lowerCamelCase : List[Any]=2 , lowerCamelCase : int=504 , **lowerCamelCase : Optional[int] , ) -> str: super().__init__(**__lowercase , pad_token_id=__lowercase , bos_token_id=__lowercase , eos_token_id=__lowercase ) __snake_case : Union[str, Any] = hidden_size __snake_case : List[Any] = feat_extract_norm __snake_case : Optional[Any] = feat_extract_activation __snake_case : Dict = list(__lowercase ) __snake_case : Optional[Any] = list(__lowercase ) __snake_case : List[str] = list(__lowercase ) __snake_case : Union[str, Any] = conv_bias __snake_case : Optional[Any] = num_conv_pos_embeddings __snake_case : List[str] = num_conv_pos_embedding_groups __snake_case : Union[str, Any] = len(self.conv_dim ) __snake_case : Tuple = num_hidden_layers __snake_case : List[str] = intermediate_size __snake_case : Union[str, Any] = hidden_act __snake_case : Optional[Any] = num_attention_heads __snake_case : int = hidden_dropout __snake_case : Tuple = attention_dropout __snake_case : str = activation_dropout __snake_case : Dict = feat_proj_dropout __snake_case : List[str] = final_dropout __snake_case : str = layerdrop __snake_case : str = layer_norm_eps __snake_case : Optional[int] = initializer_range __snake_case : Union[str, Any] = vocab_size __snake_case : List[str] = num_clusters __snake_case : List[Any] = do_stable_layer_norm __snake_case : str = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==" " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =" F' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,' F' `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __snake_case : Tuple = apply_spec_augment __snake_case : Optional[Any] = mask_time_prob __snake_case : str = mask_time_length __snake_case : List[Any] = mask_time_min_masks __snake_case : List[Any] = mask_feature_prob __snake_case : str = mask_feature_length __snake_case : Union[str, Any] = mask_feature_min_masks # parameters for pretraining with codevector quantized representations __snake_case : List[Any] = num_codevectors_per_group __snake_case : Tuple = num_codevector_groups __snake_case : Optional[Any] = contrastive_logits_temperature __snake_case : Dict = feat_quantizer_dropout __snake_case : Optional[int] = num_negatives __snake_case : Optional[int] = codevector_dim __snake_case : Optional[int] = proj_codevector_dim __snake_case : List[Any] = diversity_loss_weight # ctc loss __snake_case : Union[str, Any] = ctc_loss_reduction __snake_case : List[str] = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. __snake_case : Optional[Any] = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. __snake_case : List[str] = list(__lowercase ) __snake_case : int = list(__lowercase ) __snake_case : List[str] = list(__lowercase ) __snake_case : Optional[int] = xvector_output_dim @property def __snake_case ( self : Tuple ) -> Dict: return functools.reduce(operator.mul , self.conv_stride , 1 )
359
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split _snake_case : Union[str, Any] = datasets.load_iris() _snake_case : Tuple = np.array(data["data"]) _snake_case : int = np.array(data["target"]) _snake_case : int = data["target_names"] _snake_case , _snake_case , _snake_case , _snake_case : Any = train_test_split(X, y) def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase ): return np.linalg.norm(np.array(__lowerCamelCase ) - np.array(__lowerCamelCase ) ) def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=5 ): __snake_case : Optional[Any] = zip(__lowerCamelCase , __lowerCamelCase ) # List of distances of all points from the point to be classified __snake_case : Optional[int] = [] for data_point in data: __snake_case : Union[str, Any] = euclidean_distance(data_point[0] , __lowerCamelCase ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. __snake_case : Dict = [i[1] for i in sorted(__lowerCamelCase )[:k]] # Most commonly occurring class among them # is the class into which the point is classified __snake_case : Any = Counter(__lowerCamelCase ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
134
0
from collections import defaultdict from graphs.minimum_spanning_tree_prims import prisms_algorithm as mst def UpperCamelCase ( ): snake_case , snake_case : Union[str, Any] = 9, 14 # noqa: F841 snake_case : int = [ [0, 1, 4], [0, 7, 8], [1, 2, 8], [7, 8, 7], [7, 6, 1], [2, 8, 2], [8, 6, 6], [2, 3, 7], [2, 5, 4], [6, 5, 2], [3, 5, 14], [3, 4, 9], [5, 4, 10], [1, 7, 11], ] snake_case : int = defaultdict(__lowerCamelCase ) for nodea, nodea, cost in edges: adjancency[nodea].append([nodea, cost] ) adjancency[nodea].append([nodea, cost] ) snake_case : Any = mst(__lowerCamelCase ) snake_case : Optional[Any] = [ [7, 6, 1], [2, 8, 2], [6, 5, 2], [0, 1, 4], [2, 5, 4], [2, 3, 7], [0, 7, 8], [3, 4, 9], ] for answer in expected: snake_case : Union[str, Any] = tuple(answer[:2] ) snake_case : Tuple = tuple(edge[::-1] ) assert edge in result or reverse in result
59
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter UpperCAmelCase_ : Any = '''Create a default config file for Accelerate with only a few flags set.''' def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Optional[int]="no" , __magic_name__ : str = default_json_config_file , __magic_name__ : bool = False ) -> str: """simple docstring""" UpperCamelCase :Any = Path(__magic_name__ ) path.parent.mkdir(parents=__magic_name__ , exist_ok=__magic_name__ ) if path.exists(): print( f"""Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.""" ) return False UpperCamelCase :Dict = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( f"""`mixed_precision` should be one of 'no', 'fp16', 'bf16', or 'fp8'. Received {mixed_precision}""" ) UpperCamelCase :Optional[Any] = { """compute_environment""": """LOCAL_MACHINE""", """mixed_precision""": mixed_precision, } if torch.cuda.is_available(): UpperCamelCase :Union[str, Any] = torch.cuda.device_count() UpperCamelCase :List[Any] = num_gpus UpperCamelCase :Dict = False if num_gpus > 1: UpperCamelCase :Any = """MULTI_GPU""" else: UpperCamelCase :Any = """NO""" elif is_xpu_available() and use_xpu: UpperCamelCase :Optional[Any] = torch.xpu.device_count() UpperCamelCase :Optional[int] = num_xpus UpperCamelCase :int = False if num_xpus > 1: UpperCamelCase :Union[str, Any] = """MULTI_XPU""" else: UpperCamelCase :Union[str, Any] = """NO""" elif is_npu_available(): UpperCamelCase :List[Any] = torch.npu.device_count() UpperCamelCase :Optional[Any] = num_npus UpperCamelCase :Tuple = False if num_npus > 1: UpperCamelCase :Optional[Any] = """MULTI_NPU""" else: UpperCamelCase :List[Any] = """NO""" else: UpperCamelCase :Any = 0 UpperCamelCase :Optional[Any] = True UpperCamelCase :Optional[Any] = 1 UpperCamelCase :List[str] = """NO""" UpperCamelCase :int = ClusterConfig(**__magic_name__ ) config.to_json_file(__magic_name__ ) return path def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Dict , __magic_name__ : Tuple ) -> List[str]: """simple docstring""" UpperCamelCase :Dict = parser.add_parser("""default""" , parents=__magic_name__ , help=__magic_name__ , formatter_class=__magic_name__ ) parser.add_argument( """--config_file""" , default=__magic_name__ , help=( """The path to use to store the config file. Will default to a file named default_config.yaml in the cache """ """location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have """ """such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed """ """with 'huggingface'.""" ) , dest="""save_location""" , ) parser.add_argument( """--mixed_precision""" , choices=["""no""", """fp16""", """bf16"""] , type=__magic_name__ , help="""Whether or not to use mixed precision training. """ """Choose between FP16 and BF16 (bfloat16) training. """ """BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.""" , default="""no""" , ) parser.set_defaults(func=__magic_name__ ) return parser def SCREAMING_SNAKE_CASE_ ( __magic_name__ : List[Any] ) -> List[str]: """simple docstring""" UpperCamelCase :Optional[Any] = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(f"""accelerate configuration saved at {config_file}""" )
38
0
import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets lowercase__ = "\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n booktitle = {NeurIPS},\n year = {2021}\n}\n\n" lowercase__ = "\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n" lowercase__ = "\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n predictions: list of generated text to score. Each predictions\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\nOptional Args:\n num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer\n pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n kmeans_max_iter: maximum number of k-means iterations. Default 500\n featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'].\n device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU\n max_text_length: maximum number of tokens to consider. Default 1024\n divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n mauve_scaling_factor: \"c\" from the paper. Default 5.\n verbose: If True (default), print running time updates\n seed: random seed to initialize k-means cluster assignments.\nReturns:\n mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n q_hist: same as above, but with q_text.\nExamples:\n\n >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n >>> import datasets\n >>> mauve = datasets.load_metric('mauve')\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n >>> print(out.mauve) # doctest: +SKIP\n 1.0\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case__ ( datasets.Metric ): """simple docstring""" def lowerCAmelCase ( self : Dict ) -> Optional[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://github.com/krishnap25/mauve''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/krishnap25/mauve'''] , reference_urls=[ '''https://arxiv.org/abs/2102.01454''', '''https://github.com/krishnap25/mauve''', ] , ) def lowerCAmelCase ( self : Tuple , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : int=None , UpperCamelCase__ : Dict="auto" , UpperCamelCase__ : Optional[Any]=-1 , UpperCamelCase__ : str=0.9 , UpperCamelCase__ : List[Any]=5 , UpperCamelCase__ : str=500 , UpperCamelCase__ : Dict="gpt2-large" , UpperCamelCase__ : Optional[Any]=-1 , UpperCamelCase__ : Any=1024 , UpperCamelCase__ : Any=25 , UpperCamelCase__ : str=5 , UpperCamelCase__ : Optional[Any]=True , UpperCamelCase__ : Any=25 , ) -> Optional[int]: """simple docstring""" snake_case : str = compute_mauve( p_text=UpperCamelCase__ , q_text=UpperCamelCase__ , p_features=UpperCamelCase__ , q_features=UpperCamelCase__ , p_tokens=UpperCamelCase__ , q_tokens=UpperCamelCase__ , num_buckets=UpperCamelCase__ , pca_max_data=UpperCamelCase__ , kmeans_explained_var=UpperCamelCase__ , kmeans_num_redo=UpperCamelCase__ , kmeans_max_iter=UpperCamelCase__ , featurize_model_name=UpperCamelCase__ , device_id=UpperCamelCase__ , max_text_length=UpperCamelCase__ , divergence_curve_discretization_size=UpperCamelCase__ , mauve_scaling_factor=UpperCamelCase__ , verbose=UpperCamelCase__ , seed=UpperCamelCase__ , ) return out
364
'''simple docstring''' from collections.abc import Generator def _UpperCamelCase ( ) -> Generator[int, None, None]: '''simple docstring''' snake_case ,snake_case : Tuple = 0, 1 while True: snake_case ,snake_case : List[Any] = b, a + b yield b def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ = 1000 ) -> int: '''simple docstring''' snake_case : Optional[int] = 1 snake_case : List[Any] = fibonacci_generator() while len(str(next(SCREAMING_SNAKE_CASE__ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
83
0
from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __lowerCAmelCase = { 'vocab_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json' }, 'merges_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt' }, } __lowerCAmelCase = {'allegro/herbert-base-cased': 5_14} __lowerCAmelCase = {} class __a ( lowerCamelCase__ ): __lowercase : Optional[Any] = VOCAB_FILES_NAMES __lowercase : Dict = PRETRAINED_VOCAB_FILES_MAP __lowercase : Any = PRETRAINED_INIT_CONFIGURATION __lowercase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Tuple = HerbertTokenizer def __init__( self , lowerCAmelCase__=None , lowerCAmelCase__=None , lowerCAmelCase__=None , lowerCAmelCase__="<s>" , lowerCAmelCase__="<unk>" , lowerCAmelCase__="<pad>" , lowerCAmelCase__="<mask>" , lowerCAmelCase__="</s>" , **lowerCAmelCase__ , ) -> Optional[int]: '''simple docstring''' super().__init__( __lowerCamelCase , __lowerCamelCase , tokenizer_file=__lowerCamelCase , cls_token=__lowerCamelCase , unk_token=__lowerCamelCase , pad_token=__lowerCamelCase , mask_token=__lowerCamelCase , sep_token=__lowerCamelCase , **__lowerCamelCase , ) def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> List[int]: '''simple docstring''' lowercase__: Tuple = [self.cls_token_id] lowercase__: str = [self.sep_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 SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCamelCase , token_ids_a=__lowerCamelCase , already_has_special_tokens=__lowerCamelCase ) if token_ids_a is None: return [1] + ([0] * len(__lowerCamelCase )) + [1] return [1] + ([0] * len(__lowerCamelCase )) + [1] + ([0] * len(__lowerCamelCase )) + [1] def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> List[int]: '''simple docstring''' lowercase__: Dict = [self.sep_token_id] lowercase__: Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> Tuple[str]: '''simple docstring''' lowercase__: str = self._tokenizer.model.save(__lowerCamelCase , name=__lowerCamelCase ) return tuple(__lowerCamelCase )
196
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized, parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" , """False""")) is not True , reason="""Skipping test because should only be run when releasing minor transformers version""" , ) @pytest.mark.usefixtures("""sm_env""") @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """pytorch""", """script""": """run_ddp.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.7, """eval_loss""": 0.6}, }, { """framework""": """tensorflow""", """script""": """run_tf_dist.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.p3.16xlarge""", """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.6, """eval_loss""": 0.7}, }, ]) class lowerCamelCase__ ( unittest.TestCase): '''simple docstring''' def lowerCAmelCase__ (self ) -> List[str]: """simple docstring""" if self.framework == "pytorch": subprocess.run( f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split() ,encoding='''utf-8''' ,check=__lowerCamelCase ,) assert hasattr(self ,'''env''' ) def lowerCAmelCase__ (self ,__lowerCamelCase ) -> str: """simple docstring""" lowerCAmelCase__ : Optional[int] = f"""{self.env.base_job_name}-{instance_count}-{'ddp' if 'ddp' in self.script else 'smd'}""" # distributed data settings lowerCAmelCase__ : Optional[Any] = {'''smdistributed''': {'''dataparallel''': {'''enabled''': True}}} if self.script != '''run_ddp.py''' else None # creates estimator return HuggingFace( entry_point=self.script ,source_dir=self.env.test_path ,role=self.env.role ,image_uri=self.env.image_uri ,base_job_name=__lowerCamelCase ,instance_count=__lowerCamelCase ,instance_type=self.instance_type ,debugger_hook_config=__lowerCamelCase ,hyperparameters={**self.env.distributed_hyperparameters, '''model_name_or_path''': self.model_name_or_path} ,metric_definitions=self.env.metric_definitions ,distribution=__lowerCamelCase ,py_version='''py36''' ,) def lowerCAmelCase__ (self ,__lowerCamelCase ) -> str: """simple docstring""" TrainingJobAnalytics(__lowerCamelCase ).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv""" ) @parameterized.expand([(2,)] ) def lowerCAmelCase__ (self ,__lowerCamelCase ) -> Optional[Any]: """simple docstring""" lowerCAmelCase__ : Optional[Any] = self.create_estimator(__lowerCamelCase ) # run training estimator.fit() # result dataframe lowerCAmelCase__ : Dict = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis lowerCAmelCase__ : List[Any] = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) lowerCAmelCase__ : Union[str, Any] = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping lowerCAmelCase__ : Optional[Any] = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' ,99_99_99 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"""{estimator.latest_training_job.name}.json""" ,'''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} ,__lowerCamelCase )
129
0
"""simple docstring""" import argparse import logging import os import re import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, DataCollatorForLanguageModeling, PushToHubCallback, TFAutoModelForMaskedLM, create_optimizer, ) __UpperCAmelCase = logging.getLogger(__name__) __UpperCAmelCase = tf.data.AUTOTUNE def __lowerCamelCase ( ): a__: Optional[Any] =argparse.ArgumentParser(description="Train a masked language model on TPU." ) parser.add_argument( "--pretrained_model_config" , type=__magic_name__ , default="roberta-base" , help="The model config to use. Note that we don't copy the model's weights, only the config!" , ) parser.add_argument( "--tokenizer" , type=__magic_name__ , default="unigram-tokenizer-wikitext" , help="The name of the tokenizer to load. We use the pretrained tokenizer to initialize the model's vocab size." , ) parser.add_argument( "--per_replica_batch_size" , type=__magic_name__ , default=8 , help="Batch size per TPU core." , ) parser.add_argument( "--no_tpu" , action="store_true" , help="If set, run on CPU and don't try to initialize a TPU. Useful for debugging on non-TPU instances." , ) parser.add_argument( "--tpu_name" , type=__magic_name__ , help="Name of TPU resource to initialize. Should be blank on Colab, and 'local' on TPU VMs." , default="local" , ) parser.add_argument( "--tpu_zone" , type=__magic_name__ , help="Google cloud zone that TPU resource is located in. Only used for non-Colab TPU nodes." , ) parser.add_argument( "--gcp_project" , type=__magic_name__ , help="Google cloud project name. Only used for non-Colab TPU nodes." ) parser.add_argument( "--bfloat16" , action="store_true" , help="Use mixed-precision bfloat16 for training. This is the recommended lower-precision format for TPU." , ) parser.add_argument( "--train_dataset" , type=__magic_name__ , help="Path to training dataset to load. If the path begins with `gs://`" " then the dataset will be loaded from a Google Cloud Storage bucket." , ) parser.add_argument( "--shuffle_buffer_size" , type=__magic_name__ , default=2**18 , help="Size of the shuffle buffer (in samples)" , ) parser.add_argument( "--eval_dataset" , type=__magic_name__ , help="Path to evaluation dataset to load. If the path begins with `gs://`" " then the dataset will be loaded from a Google Cloud Storage bucket." , ) parser.add_argument( "--num_epochs" , type=__magic_name__ , default=1 , help="Number of epochs to train for." , ) parser.add_argument( "--learning_rate" , type=__magic_name__ , default=1e-4 , help="Learning rate to use for training." , ) parser.add_argument( "--weight_decay_rate" , type=__magic_name__ , default=1e-3 , help="Weight decay rate to use for training." , ) parser.add_argument( "--max_length" , type=__magic_name__ , default=512 , help="Maximum length of tokenized sequences. Should match the setting used in prepare_tfrecord_shards.py" , ) parser.add_argument( "--mlm_probability" , type=__magic_name__ , default=0.15 , help="Fraction of tokens to mask during training." , ) parser.add_argument("--output_dir" , type=__magic_name__ , required=__magic_name__ , help="Path to save model checkpoints to." ) parser.add_argument("--hub_model_id" , type=__magic_name__ , help="Model ID to upload to on the Hugging Face Hub." ) a__: str =parser.parse_args() return args def __lowerCamelCase ( __magic_name__ : str ): try: if args.tpu_name: a__: Dict =tf.distribute.cluster_resolver.TPUClusterResolver( args.tpu_name , zone=args.tpu_zone , project=args.gcp_project ) else: a__: Any =tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: raise RuntimeError( "Couldn't connect to TPU! Most likely you need to specify --tpu_name, --tpu_zone, or " "--gcp_project. When running on a TPU VM, use --tpu_name local." ) tf.config.experimental_connect_to_cluster(__magic_name__ ) tf.tpu.experimental.initialize_tpu_system(__magic_name__ ) return tpu def __lowerCamelCase ( __magic_name__ : Optional[Any] ): a__: Union[str, Any] =0 for file in file_list: a__: str =file.split("/" )[-1] a__: List[Any] =re.search(R"-\d+-(\d+)\.tfrecord" , __magic_name__ ).group(1 ) a__: Dict =int(__magic_name__ ) num_samples += sample_count return num_samples def __lowerCamelCase ( __magic_name__ : Optional[int] , __magic_name__ : int , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Tuple=None ): a__: Any =count_samples(__magic_name__ ) a__: List[Any] =tf.data.Dataset.from_tensor_slices(__magic_name__ ) if shuffle: a__: int =dataset.shuffle(len(__magic_name__ ) ) a__: Optional[int] =tf.data.TFRecordDataset(__magic_name__ , num_parallel_reads=__magic_name__ ) # TF can't infer the total sample count because it doesn't read all the records yet, so we assert it here a__: Dict =dataset.apply(tf.data.experimental.assert_cardinality(__magic_name__ ) ) a__: Any =dataset.map(__magic_name__ , num_parallel_calls=__magic_name__ ) if shuffle: assert shuffle_buffer_size is not None a__: Dict =dataset.shuffle(args.shuffle_buffer_size ) a__: Tuple =dataset.batch(__magic_name__ , drop_remainder=__magic_name__ ) a__: List[Any] =dataset.map(__magic_name__ , num_parallel_calls=__magic_name__ ) a__: int =dataset.prefetch(__magic_name__ ) return dataset def __lowerCamelCase ( __magic_name__ : List[str] ): if not args.no_tpu: a__: Any =initialize_tpu(__magic_name__ ) a__: Any =tf.distribute.TPUStrategy(__magic_name__ ) else: a__: str =tf.distribute.OneDeviceStrategy(device="/gpu:0" ) if args.bfloataa: tf.keras.mixed_precision.set_global_policy("mixed_bfloat16" ) a__: List[Any] =AutoTokenizer.from_pretrained(args.tokenizer ) a__: List[Any] =AutoConfig.from_pretrained(args.pretrained_model_config ) a__: int =tokenizer.vocab_size a__: Union[str, Any] =tf.io.gfile.glob(os.path.join(args.train_dataset , "*.tfrecord" ) ) if not training_records: raise ValueError(F"No .tfrecord files found in {args.train_dataset}." ) a__: List[str] =tf.io.gfile.glob(os.path.join(args.eval_dataset , "*.tfrecord" ) ) if not eval_records: raise ValueError(F"No .tfrecord files found in {args.eval_dataset}." ) a__: int =count_samples(__magic_name__ ) a__: Union[str, Any] =num_train_samples // (args.per_replica_batch_size * strategy.num_replicas_in_sync) a__: int =steps_per_epoch * args.num_epochs with strategy.scope(): a__: Tuple =TFAutoModelForMaskedLM.from_config(__magic_name__ ) model(model.dummy_inputs ) # Pass some dummy inputs through the model to ensure all the weights are built a__: int =create_optimizer( num_train_steps=__magic_name__ , num_warmup_steps=total_train_steps // 20 , init_lr=args.learning_rate , weight_decay_rate=args.weight_decay_rate , ) # Transformers models compute the right loss for their task by default when labels are passed, and will # use this for training unless you specify your own loss function in compile(). model.compile(optimizer=__magic_name__ , metrics=["accuracy"] ) def decode_fn(__magic_name__ : Optional[int] ): a__: List[Any] ={ "input_ids": tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), "attention_mask": tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), } return tf.io.parse_single_example(__magic_name__ , __magic_name__ ) # Many of the data collators in Transformers are TF-compilable when return_tensors == "tf", so we can # use their methods in our data pipeline. a__: List[Any] =DataCollatorForLanguageModeling( tokenizer=__magic_name__ , mlm_probability=args.mlm_probability , mlm=__magic_name__ , return_tensors="tf" ) def mask_with_collator(__magic_name__ : Tuple ): # TF really needs an isin() function a__: str =( ~tf.cast(batch["attention_mask"] , tf.bool ) | (batch["input_ids"] == tokenizer.cls_token_id) | (batch["input_ids"] == tokenizer.sep_token_id) ) a__: str =data_collator.tf_mask_tokens( batch["input_ids"] , vocab_size=len(__magic_name__ ) , mask_token_id=tokenizer.mask_token_id , special_tokens_mask=__magic_name__ , ) return batch a__: List[Any] =args.per_replica_batch_size * strategy.num_replicas_in_sync a__: Tuple =prepare_dataset( __magic_name__ , decode_fn=__magic_name__ , mask_fn=__magic_name__ , batch_size=__magic_name__ , shuffle=__magic_name__ , shuffle_buffer_size=args.shuffle_buffer_size , ) a__: Union[str, Any] =prepare_dataset( __magic_name__ , decode_fn=__magic_name__ , mask_fn=__magic_name__ , batch_size=__magic_name__ , shuffle=__magic_name__ , ) a__: Any =[] if args.hub_model_id: callbacks.append( PushToHubCallback(output_dir=args.output_dir , hub_model_id=args.hub_model_id , tokenizer=__magic_name__ ) ) model.fit( __magic_name__ , validation_data=__magic_name__ , epochs=args.num_epochs , callbacks=__magic_name__ , ) model.save_pretrained(args.output_dir ) if __name__ == "__main__": __UpperCAmelCase = parse_args() main(args)
371
import gc import unittest import numpy as np import torch from diffusers import StableDiffusionKDiffusionPipeline from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() @slow @require_torch_gpu class lowerCamelCase__ ( unittest.TestCase ): def _lowerCamelCase ( self : Optional[int] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCamelCase ( self : List[Any] ): a__: Any =StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" ) a__: List[str] =sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) sd_pipe.set_scheduler("sample_euler" ) a__: Dict ="A painting of a squirrel eating a burger" a__: List[Any] =torch.manual_seed(0 ) a__: int =sd_pipe([prompt] , generator=_a , guidance_scale=9.0 , num_inference_steps=2_0 , output_type="np" ) a__: int =output.images a__: Optional[Any] =image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) a__: Any =np.array([0.0_4_4_7, 0.0_4_9_2, 0.0_4_6_8, 0.0_4_0_8, 0.0_3_8_3, 0.0_4_0_8, 0.0_3_5_4, 0.0_3_8_0, 0.0_3_3_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def _lowerCamelCase ( self : Optional[Any] ): a__: List[Any] =StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) a__: List[str] =sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) sd_pipe.set_scheduler("sample_euler" ) a__: int ="A painting of a squirrel eating a burger" a__: List[Any] =torch.manual_seed(0 ) a__: Optional[int] =sd_pipe([prompt] , generator=_a , guidance_scale=9.0 , num_inference_steps=2_0 , output_type="np" ) a__: List[str] =output.images a__: List[str] =image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) a__: Any =np.array([0.1_2_3_7, 0.1_3_2_0, 0.1_4_3_8, 0.1_3_5_9, 0.1_3_9_0, 0.1_1_3_2, 0.1_2_7_7, 0.1_1_7_5, 0.1_1_1_2] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-1 def _lowerCamelCase ( self : List[str] ): a__: Optional[Any] =StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base" ) a__: Optional[Any] =sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) sd_pipe.set_scheduler("sample_dpmpp_2m" ) a__: Tuple ="A painting of a squirrel eating a burger" a__: Tuple =torch.manual_seed(0 ) a__: Optional[int] =sd_pipe( [prompt] , generator=_a , guidance_scale=7.5 , num_inference_steps=1_5 , output_type="np" , use_karras_sigmas=_a , ) a__: str =output.images a__: str =image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) a__: List[Any] =np.array( [0.1_1_3_8_1_6_8_9, 0.1_2_1_1_2_9_2_1, 0.1_3_8_9_4_5_7, 0.1_2_5_4_9_6_0_6, 0.1_2_4_4_9_6_4, 0.1_0_8_3_1_5_1_7, 0.1_1_5_6_2_8_6_6, 0.1_0_8_6_7_8_1_6, 0.1_0_4_9_9_0_4_8] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
42
0
import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase_ ( self : Optional[int] ): debug_launcher(test_script.main ) def UpperCamelCase_ ( self : Optional[Any] ): debug_launcher(test_ops.main )
15
import math def UpperCAmelCase ( a_ , a_ = 0 , a_ = 0 ) -> list: """simple docstring""" __A = end or len(a_ ) for i in range(a_ , a_ ): __A = i __A = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: __A = array[temp_index - 1] temp_index -= 1 __A = temp_index_value return array def UpperCAmelCase ( a_ , a_ , a_ ) -> None: # Max Heap """simple docstring""" __A = index __A = 2 * index + 1 # Left Node __A = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: __A = left_index if right_index < heap_size and array[largest] < array[right_index]: __A = right_index if largest != index: __A , __A = array[largest], array[index] heapify(a_ , a_ , a_ ) def UpperCAmelCase ( a_ ) -> list: """simple docstring""" __A = len(a_ ) for i in range(n // 2 , -1 , -1 ): heapify(a_ , a_ , a_ ) for i in range(n - 1 , 0 , -1 ): __A , __A = array[0], array[i] heapify(a_ , 0 , a_ ) return array def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def UpperCAmelCase ( a_ , a_ , a_ , a_ ) -> int: """simple docstring""" __A = low __A = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i __A , __A = array[j], array[i] i += 1 def UpperCAmelCase ( a_ ) -> list: """simple docstring""" if len(a_ ) == 0: return array __A = 2 * math.ceil(math.loga(len(a_ ) ) ) __A = 1_6 return intro_sort(a_ , 0 , len(a_ ) , a_ , a_ ) def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ ) -> list: """simple docstring""" while end - start > size_threshold: if max_depth == 0: return heap_sort(a_ ) max_depth -= 1 __A = median_of_a(a_ , a_ , start + ((end - start) // 2) + 1 , end - 1 ) __A = partition(a_ , a_ , a_ , a_ ) intro_sort(a_ , a_ , a_ , a_ , a_ ) __A = p return insertion_sort(a_ , a_ , a_ ) if __name__ == "__main__": import doctest doctest.testmod() SCREAMING_SNAKE_CASE :List[Any] = input('Enter numbers separated by a comma : ').strip() SCREAMING_SNAKE_CASE :str = [float(item) for item in user_input.split(',')] print(sort(unsorted))
15
1
import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class a_ ( _snake_case ): UpperCamelCase__ : Any ="Wav2Vec2FeatureExtractor" UpperCamelCase__ : Optional[Any] ="AutoTokenizer" def __init__( self :Dict , _lowercase :Any , _lowercase :Optional[Any]) -> Any: super().__init__(_lowercase , _lowercase) UpperCAmelCase_ = self.feature_extractor UpperCAmelCase_ = False @classmethod def __a ( cls :List[str] , _lowercase :str , **_lowercase :List[str]) -> List[Any]: try: return super().from_pretrained(_lowercase , **_lowercase) except OSError: warnings.warn( f"Loading a tokenizer inside {cls.__name__} from a config that does not" ''' include a `tokenizer_class` attribute is deprecated and will be ''' '''removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`''' ''' attribute to either your `config.json` or `tokenizer_config.json` ''' '''file to suppress this warning: ''' , _lowercase , ) UpperCAmelCase_ = WavaVecaFeatureExtractor.from_pretrained(_lowercase , **_lowercase) UpperCAmelCase_ = WavaVecaCTCTokenizer.from_pretrained(_lowercase , **_lowercase) return cls(feature_extractor=_lowercase , tokenizer=_lowercase) def __call__( self :Tuple , *_lowercase :Optional[Any] , **_lowercase :Optional[int]) -> Tuple: # For backward compatibility if self._in_target_context_manager: return self.current_processor(*_lowercase , **_lowercase) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''') UpperCAmelCase_ = kwargs.pop('''raw_speech''') else: UpperCAmelCase_ = kwargs.pop('''audio''' , _lowercase) UpperCAmelCase_ = kwargs.pop('''sampling_rate''' , _lowercase) UpperCAmelCase_ = kwargs.pop('''text''' , _lowercase) if len(_lowercase) > 0: UpperCAmelCase_ = args[0] UpperCAmelCase_ = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''') if audio is not None: UpperCAmelCase_ = self.feature_extractor(_lowercase , *_lowercase , sampling_rate=_lowercase , **_lowercase) if text is not None: UpperCAmelCase_ = self.tokenizer(_lowercase , **_lowercase) if text is None: return inputs elif audio is None: return encodings else: UpperCAmelCase_ = encodings['''input_ids'''] return inputs def __a ( self :int , *_lowercase :Tuple , **_lowercase :int) -> Tuple: # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*_lowercase , **_lowercase) UpperCAmelCase_ = kwargs.pop('''input_features''' , _lowercase) UpperCAmelCase_ = kwargs.pop('''labels''' , _lowercase) if len(_lowercase) > 0: UpperCAmelCase_ = args[0] UpperCAmelCase_ = args[1:] if input_features is not None: UpperCAmelCase_ = self.feature_extractor.pad(_lowercase , *_lowercase , **_lowercase) if labels is not None: UpperCAmelCase_ = self.tokenizer.pad(_lowercase , **_lowercase) if labels is None: return input_features elif input_features is None: return labels else: UpperCAmelCase_ = labels['''input_ids'''] return input_features def __a ( self :Union[str, Any] , *_lowercase :Optional[int] , **_lowercase :List[Any]) -> Tuple: return self.tokenizer.batch_decode(*_lowercase , **_lowercase) def __a ( self :Dict , *_lowercase :int , **_lowercase :Optional[Any]) -> Any: return self.tokenizer.decode(*_lowercase , **_lowercase) @contextmanager def __a ( self :Any) -> Tuple: warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your audio inputs, or in a separate call.''') UpperCAmelCase_ = True UpperCAmelCase_ = self.tokenizer yield UpperCAmelCase_ = self.feature_extractor UpperCAmelCase_ = False
344
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class a_ ( _snake_case ): UpperCamelCase__ : Optional[Any] =(DPMSolverSinglestepScheduler,) UpperCamelCase__ : Tuple =(("num_inference_steps", 25),) def __a ( self :List[Any] , **_lowercase :Optional[Any]) -> int: UpperCAmelCase_ = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''solver_order''': 2, '''prediction_type''': '''epsilon''', '''thresholding''': False, '''sample_max_value''': 1.0, '''algorithm_type''': '''dpmsolver++''', '''solver_type''': '''midpoint''', '''lambda_min_clipped''': -float('''inf'''), '''variance_type''': None, } config.update(**_lowercase) return config def __a ( self :Union[str, Any] , _lowercase :List[Any]=0 , **_lowercase :Optional[int]) -> List[Any]: UpperCAmelCase_ = dict(self.forward_default_kwargs) UpperCAmelCase_ = kwargs.pop('''num_inference_steps''' , _lowercase) UpperCAmelCase_ = self.dummy_sample UpperCAmelCase_ = 0.1 * sample UpperCAmelCase_ = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: UpperCAmelCase_ = self.get_scheduler_config(**_lowercase) UpperCAmelCase_ = scheduler_class(**_lowercase) scheduler.set_timesteps(_lowercase) # copy over dummy past residuals UpperCAmelCase_ = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_lowercase) UpperCAmelCase_ = scheduler_class.from_pretrained(_lowercase) new_scheduler.set_timesteps(_lowercase) # copy over dummy past residuals UpperCAmelCase_ = dummy_past_residuals[: new_scheduler.config.solver_order] UpperCAmelCase_ , UpperCAmelCase_ = sample, sample for t in range(_lowercase , time_step + scheduler.config.solver_order + 1): UpperCAmelCase_ = scheduler.step(_lowercase , _lowercase , _lowercase , **_lowercase).prev_sample UpperCAmelCase_ = new_scheduler.step(_lowercase , _lowercase , _lowercase , **_lowercase).prev_sample assert torch.sum(torch.abs(output - new_output)) < 1E-5, "Scheduler outputs are not identical" def __a ( self :Union[str, Any]) -> List[Any]: pass def __a ( self :Optional[Any] , _lowercase :str=0 , **_lowercase :Union[str, Any]) -> Dict: UpperCAmelCase_ = dict(self.forward_default_kwargs) UpperCAmelCase_ = kwargs.pop('''num_inference_steps''' , _lowercase) UpperCAmelCase_ = self.dummy_sample UpperCAmelCase_ = 0.1 * sample UpperCAmelCase_ = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: UpperCAmelCase_ = self.get_scheduler_config() UpperCAmelCase_ = scheduler_class(**_lowercase) scheduler.set_timesteps(_lowercase) # copy over dummy past residuals (must be after setting timesteps) UpperCAmelCase_ = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_lowercase) UpperCAmelCase_ = scheduler_class.from_pretrained(_lowercase) # copy over dummy past residuals new_scheduler.set_timesteps(_lowercase) # copy over dummy past residual (must be after setting timesteps) UpperCAmelCase_ = dummy_past_residuals[: new_scheduler.config.solver_order] UpperCAmelCase_ = scheduler.step(_lowercase , _lowercase , _lowercase , **_lowercase).prev_sample UpperCAmelCase_ = new_scheduler.step(_lowercase , _lowercase , _lowercase , **_lowercase).prev_sample assert torch.sum(torch.abs(output - new_output)) < 1E-5, "Scheduler outputs are not identical" def __a ( self :Dict , _lowercase :Union[str, Any]=None , **_lowercase :List[Any]) -> int: if scheduler is None: UpperCAmelCase_ = self.scheduler_classes[0] UpperCAmelCase_ = self.get_scheduler_config(**_lowercase) UpperCAmelCase_ = scheduler_class(**_lowercase) UpperCAmelCase_ = self.scheduler_classes[0] UpperCAmelCase_ = self.get_scheduler_config(**_lowercase) UpperCAmelCase_ = scheduler_class(**_lowercase) UpperCAmelCase_ = 10 UpperCAmelCase_ = self.dummy_model() UpperCAmelCase_ = self.dummy_sample_deter scheduler.set_timesteps(_lowercase) for i, t in enumerate(scheduler.timesteps): UpperCAmelCase_ = model(_lowercase , _lowercase) UpperCAmelCase_ = scheduler.step(_lowercase , _lowercase , _lowercase).prev_sample return sample def __a ( self :int) -> Tuple: UpperCAmelCase_ = DPMSolverSinglestepScheduler(**self.get_scheduler_config()) UpperCAmelCase_ = 50 UpperCAmelCase_ = self.dummy_model() UpperCAmelCase_ = self.dummy_sample_deter scheduler.set_timesteps(_lowercase) # make sure that the first t is uneven for i, t in enumerate(scheduler.timesteps[3:]): UpperCAmelCase_ = model(_lowercase , _lowercase) UpperCAmelCase_ = scheduler.step(_lowercase , _lowercase , _lowercase).prev_sample UpperCAmelCase_ = torch.mean(torch.abs(_lowercase)) assert abs(result_mean.item() - 0.2_574) < 1E-3 def __a ( self :List[Any]) -> List[Any]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=_lowercase) def __a ( self :int) -> Optional[Any]: # make sure that iterating over schedulers with same config names gives same results # for defaults UpperCAmelCase_ = DPMSolverSinglestepScheduler(**self.get_scheduler_config()) UpperCAmelCase_ = self.full_loop(scheduler=_lowercase) UpperCAmelCase_ = torch.mean(torch.abs(_lowercase)) assert abs(result_mean.item() - 0.2_791) < 1E-3 UpperCAmelCase_ = DEISMultistepScheduler.from_config(scheduler.config) UpperCAmelCase_ = DPMSolverMultistepScheduler.from_config(scheduler.config) UpperCAmelCase_ = UniPCMultistepScheduler.from_config(scheduler.config) UpperCAmelCase_ = DPMSolverSinglestepScheduler.from_config(scheduler.config) UpperCAmelCase_ = self.full_loop(scheduler=_lowercase) UpperCAmelCase_ = torch.mean(torch.abs(_lowercase)) assert abs(result_mean.item() - 0.2_791) < 1E-3 def __a ( self :Tuple) -> int: self.check_over_configs(thresholding=_lowercase) for order in [1, 2, 3]: for solver_type in ["midpoint", "heun"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=_lowercase , prediction_type=_lowercase , sample_max_value=_lowercase , algorithm_type='''dpmsolver++''' , solver_order=_lowercase , solver_type=_lowercase , ) def __a ( self :List[Any]) -> Any: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_lowercase) def __a ( self :Any) -> Optional[int]: for algorithm_type in ["dpmsolver", "dpmsolver++"]: for solver_type in ["midpoint", "heun"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=_lowercase , solver_type=_lowercase , prediction_type=_lowercase , algorithm_type=_lowercase , ) UpperCAmelCase_ = self.full_loop( solver_order=_lowercase , solver_type=_lowercase , prediction_type=_lowercase , algorithm_type=_lowercase , ) assert not torch.isnan(_lowercase).any(), "Samples have nan numbers" def __a ( self :Tuple) -> int: self.check_over_configs(lower_order_final=_lowercase) self.check_over_configs(lower_order_final=_lowercase) def __a ( self :Tuple) -> Optional[Any]: self.check_over_configs(lambda_min_clipped=-float('''inf''')) self.check_over_configs(lambda_min_clipped=-5.1) def __a ( self :Any) -> List[str]: self.check_over_configs(variance_type=_lowercase) self.check_over_configs(variance_type='''learned_range''') def __a ( self :Any) -> Dict: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=_lowercase , time_step=0) def __a ( self :Dict) -> Union[str, Any]: UpperCAmelCase_ = self.full_loop() UpperCAmelCase_ = torch.mean(torch.abs(_lowercase)) assert abs(result_mean.item() - 0.2_791) < 1E-3 def __a ( self :Any) -> Union[str, Any]: UpperCAmelCase_ = self.full_loop(use_karras_sigmas=_lowercase) UpperCAmelCase_ = torch.mean(torch.abs(_lowercase)) assert abs(result_mean.item() - 0.2_248) < 1E-3 def __a ( self :str) -> Optional[int]: UpperCAmelCase_ = self.full_loop(prediction_type='''v_prediction''') UpperCAmelCase_ = torch.mean(torch.abs(_lowercase)) assert abs(result_mean.item() - 0.1_453) < 1E-3 def __a ( self :List[Any]) -> Dict: UpperCAmelCase_ = self.full_loop(prediction_type='''v_prediction''' , use_karras_sigmas=_lowercase) UpperCAmelCase_ = torch.mean(torch.abs(_lowercase)) assert abs(result_mean.item() - 0.0_649) < 1E-3 def __a ( self :Any) -> Optional[Any]: UpperCAmelCase_ = self.scheduler_classes[0] UpperCAmelCase_ = self.get_scheduler_config(thresholding=_lowercase , dynamic_thresholding_ratio=0) UpperCAmelCase_ = scheduler_class(**_lowercase) UpperCAmelCase_ = 10 UpperCAmelCase_ = self.dummy_model() UpperCAmelCase_ = self.dummy_sample_deter.half() scheduler.set_timesteps(_lowercase) for i, t in enumerate(scheduler.timesteps): UpperCAmelCase_ = model(_lowercase , _lowercase) UpperCAmelCase_ = scheduler.step(_lowercase , _lowercase , _lowercase).prev_sample assert sample.dtype == torch.floataa
344
1
import os import tempfile import unittest from transformers import DistilBertConfig, 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 ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, ) class A__ ( __SCREAMING_SNAKE_CASE): def __init__( self , __magic_name__ , __magic_name__=1_3 , __magic_name__=7 , __magic_name__=True , __magic_name__=True , __magic_name__=False , __magic_name__=True , __magic_name__=9_9 , __magic_name__=3_2 , __magic_name__=5 , __magic_name__=4 , __magic_name__=3_7 , __magic_name__="gelu" , __magic_name__=0.1 , __magic_name__=0.1 , __magic_name__=5_1_2 , __magic_name__=1_6 , __magic_name__=2 , __magic_name__=0.02 , __magic_name__=3 , __magic_name__=4 , __magic_name__=None , ): lowerCamelCase : Optional[int] = parent lowerCamelCase : Optional[Any] = batch_size lowerCamelCase : Optional[Any] = seq_length lowerCamelCase : Optional[Any] = is_training lowerCamelCase : Optional[int] = use_input_mask lowerCamelCase : str = use_token_type_ids lowerCamelCase : Any = use_labels lowerCamelCase : Optional[Any] = vocab_size lowerCamelCase : List[Any] = hidden_size lowerCamelCase : Optional[int] = num_hidden_layers lowerCamelCase : Union[str, Any] = num_attention_heads lowerCamelCase : Optional[Any] = intermediate_size lowerCamelCase : List[Any] = hidden_act lowerCamelCase : Union[str, Any] = hidden_dropout_prob lowerCamelCase : Any = attention_probs_dropout_prob lowerCamelCase : Tuple = max_position_embeddings lowerCamelCase : Union[str, Any] = type_vocab_size lowerCamelCase : List[str] = type_sequence_label_size lowerCamelCase : Optional[int] = initializer_range lowerCamelCase : str = num_labels lowerCamelCase : Union[str, Any] = num_choices lowerCamelCase : List[Any] = scope def UpperCamelCase__ ( self ): lowerCamelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase : Dict = None if self.use_input_mask: lowerCamelCase : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase : Any = None lowerCamelCase : Optional[int] = None lowerCamelCase : Union[str, Any] = None if self.use_labels: lowerCamelCase : Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCamelCase : Tuple = ids_tensor([self.batch_size] , self.num_choices ) lowerCamelCase : Optional[int] = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase__ ( self ): return DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): lowerCamelCase : List[str] = DistilBertModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCamelCase : str = model(__magic_name__ , __magic_name__ ) lowerCamelCase : Optional[int] = model(__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): lowerCamelCase : List[Any] = DistilBertForMaskedLM(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCamelCase : Tuple = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): lowerCamelCase : Union[str, Any] = DistilBertForQuestionAnswering(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCamelCase : Any = model( __magic_name__ , attention_mask=__magic_name__ , start_positions=__magic_name__ , end_positions=__magic_name__ ) 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 , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): lowerCamelCase : List[Any] = self.num_labels lowerCamelCase : List[Any] = DistilBertForSequenceClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCamelCase : int = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): lowerCamelCase : Any = self.num_labels lowerCamelCase : Optional[Any] = DistilBertForTokenClassification(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCamelCase : str = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): lowerCamelCase : str = self.num_choices lowerCamelCase : List[str] = DistilBertForMultipleChoice(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCamelCase : List[str] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase : Any = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase : Optional[Any] = model( __magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase__ ( self ): lowerCamelCase : List[Any] = self.prepare_config_and_inputs() ((lowerCamelCase) , (lowerCamelCase) , (lowerCamelCase) , (lowerCamelCase) , (lowerCamelCase) , (lowerCamelCase)) : Any = config_and_inputs lowerCamelCase : Union[str, Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class A__ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase): _UpperCAmelCase : Any = ( ( DistilBertModel, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, ) if is_torch_available() else None ) _UpperCAmelCase : List[str] = ( { """feature-extraction""": DistilBertModel, """fill-mask""": DistilBertForMaskedLM, """question-answering""": DistilBertForQuestionAnswering, """text-classification""": DistilBertForSequenceClassification, """token-classification""": DistilBertForTokenClassification, """zero-shot""": DistilBertForSequenceClassification, } if is_torch_available() else {} ) _UpperCAmelCase : Dict = True _UpperCAmelCase : str = True _UpperCAmelCase : str = True _UpperCAmelCase : Union[str, Any] = True def UpperCamelCase__ ( self ): lowerCamelCase : List[Any] = DistilBertModelTester(self ) lowerCamelCase : Optional[Any] = ConfigTester(self , config_class=__magic_name__ , dim=3_7 ) def UpperCamelCase__ ( self ): self.config_tester.run_common_tests() def UpperCamelCase__ ( self ): lowerCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*__magic_name__ ) def UpperCamelCase__ ( self ): lowerCamelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*__magic_name__ ) def UpperCamelCase__ ( self ): lowerCamelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*__magic_name__ ) def UpperCamelCase__ ( self ): lowerCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*__magic_name__ ) def UpperCamelCase__ ( self ): lowerCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*__magic_name__ ) def UpperCamelCase__ ( self ): lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*__magic_name__ ) @slow def UpperCamelCase__ ( self ): for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase : List[str] = DistilBertModel.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) @slow @require_torch_gpu def UpperCamelCase__ ( self ): lowerCamelCase , lowerCamelCase : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # BertForMultipleChoice behaves incorrectly in JIT environments. if model_class == DistilBertForMultipleChoice: return lowerCamelCase : Any = True lowerCamelCase : List[Any] = model_class(config=__magic_name__ ) lowerCamelCase : Any = self._prepare_for_class(__magic_name__ , __magic_name__ ) lowerCamelCase : Optional[Any] = torch.jit.trace( __magic_name__ , (inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(__magic_name__ , os.path.join(__magic_name__ , """traced_model.pt""" ) ) lowerCamelCase : List[str] = torch.jit.load(os.path.join(__magic_name__ , """traced_model.pt""" ) , map_location=__magic_name__ ) loaded(inputs_dict["""input_ids"""].to(__magic_name__ ) , inputs_dict["""attention_mask"""].to(__magic_name__ ) ) @require_torch class A__ ( unittest.TestCase): @slow def UpperCamelCase__ ( self ): lowerCamelCase : Optional[int] = DistilBertModel.from_pretrained("""distilbert-base-uncased""" ) lowerCamelCase : Any = torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) lowerCamelCase : Union[str, Any] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): lowerCamelCase : List[str] = model(__magic_name__ , attention_mask=__magic_name__ )[0] lowerCamelCase : Any = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , __magic_name__ ) lowerCamelCase : Tuple = torch.tensor( [[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , __magic_name__ , atol=1e-4 ) )
287
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFCamembertModel @require_tf @require_sentencepiece @require_tokenizers class A__ ( unittest.TestCase): @slow def UpperCamelCase__ ( self ): lowerCamelCase : Union[str, Any] = TFCamembertModel.from_pretrained("""jplu/tf-camembert-base""" ) lowerCamelCase : Any = tf.convert_to_tensor( [[5, 1_2_1, 1_1, 6_6_0, 1_6, 7_3_0, 2_5_5_4_3, 1_1_0, 8_3, 6]] , dtype=tf.intaa , ) # J'aime le camembert !" lowerCamelCase : str = model(__magic_name__ )["""last_hidden_state"""] lowerCamelCase : Union[str, Any] = tf.TensorShape((1, 1_0, 7_6_8) ) self.assertEqual(output.shape , __magic_name__ ) # compare the actual values for a slice. lowerCamelCase : Dict = tf.convert_to_tensor( [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]] , dtype=tf.floataa , ) # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0') # camembert.eval() # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach() self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
287
1
def _a ( SCREAMING_SNAKE_CASE_ : list[list[int]] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : set ): __lowerCAmelCase , __lowerCAmelCase = len(SCREAMING_SNAKE_CASE_ ), len(grid[0] ) if ( min(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) __lowerCAmelCase = 0 count += depth_first_search(SCREAMING_SNAKE_CASE_ , row + 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) count += depth_first_search(SCREAMING_SNAKE_CASE_ , row - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) count += depth_first_search(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , col + 1 , SCREAMING_SNAKE_CASE_ ) count += depth_first_search(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , col - 1 , SCREAMING_SNAKE_CASE_ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
102
import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def _a ( SCREAMING_SNAKE_CASE_ : Any ): # encoder.embeddings are double copied in original FLAVA return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items() ) def _a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict ): __lowerCAmelCase = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue __lowerCAmelCase = key.replace("heads.cmd.mim_head.cls.predictions" , "mmm_image_head" ) __lowerCAmelCase = key.replace("heads.cmd.mlm_head.cls.predictions" , "mmm_text_head" ) __lowerCAmelCase = key.replace("heads.cmd.itm_head.cls" , "itm_head" ) __lowerCAmelCase = key.replace("heads.cmd.itm_head.pooler" , "itm_head.pooler" ) __lowerCAmelCase = key.replace("heads.cmd.clip_head.logit_scale" , "flava.logit_scale" ) __lowerCAmelCase = key.replace("heads.fairseq_mlm.cls.predictions" , "mlm_head" ) __lowerCAmelCase = key.replace("heads.imagenet.mim_head.cls.predictions" , "mim_head" ) __lowerCAmelCase = key.replace("mm_text_projection" , "flava.text_to_mm_projection" ) __lowerCAmelCase = key.replace("mm_image_projection" , "flava.image_to_mm_projection" ) __lowerCAmelCase = key.replace("image_encoder.module" , "flava.image_model" ) __lowerCAmelCase = key.replace("text_encoder.module" , "flava.text_model" ) __lowerCAmelCase = key.replace("mm_encoder.module.encoder.cls_token" , "flava.multimodal_model.cls_token" ) __lowerCAmelCase = key.replace("mm_encoder.module" , "flava.multimodal_model" ) __lowerCAmelCase = key.replace("text_projection" , "flava.text_projection" ) __lowerCAmelCase = key.replace("image_projection" , "flava.image_projection" ) __lowerCAmelCase = value.float() for key, value in codebook_state_dict.items(): __lowerCAmelCase = value return upgrade @torch.no_grad() def _a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : int=None ): if config_path is not None: __lowerCAmelCase = FlavaConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) else: __lowerCAmelCase = FlavaConfig() __lowerCAmelCase = FlavaForPreTraining(SCREAMING_SNAKE_CASE_ ).eval() __lowerCAmelCase = convert_dalle_checkpoint(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , save_checkpoint=SCREAMING_SNAKE_CASE_ ) if os.path.exists(SCREAMING_SNAKE_CASE_ ): __lowerCAmelCase = torch.load(SCREAMING_SNAKE_CASE_ , map_location="cpu" ) else: __lowerCAmelCase = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ , map_location="cpu" ) __lowerCAmelCase = upgrade_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) hf_model.load_state_dict(SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = hf_model.state_dict() __lowerCAmelCase = count_parameters(SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = count_parameters(SCREAMING_SNAKE_CASE_ ) + count_parameters(SCREAMING_SNAKE_CASE_ ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-3 ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to flava checkpoint""") parser.add_argument("""--codebook_path""", default=None, type=str, help="""Path to flava codebook checkpoint""") parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""") UpperCamelCase__ = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
102
1
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCamelCase : str = {"configuration_mmbt": ["MMBTConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase : Dict = ["MMBTForClassification", "MMBTModel", "ModalEmbeddings"] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys __lowerCamelCase : Optional[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
52
import cmath import math def _a ( SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float ): """simple docstring""" UpperCamelCase__ : Union[str, Any] = math.radians(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = math.radians(SCREAMING_SNAKE_CASE ) # Convert voltage and current to rectangular form UpperCamelCase__ : str = cmath.rect(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Any = cmath.rect(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
146
0
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) 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 .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
289
"""simple docstring""" from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def UpperCamelCase_ ( lowerCAmelCase__ : int ) -> bool: """simple docstring""" lowerCAmelCase_ : int = int(number**0.5 ) return number == sq * sq def UpperCamelCase_ ( lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> tuple[int, int]: """simple docstring""" lowerCAmelCase_ : int = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den lowerCAmelCase_ : int = x_den * y_den * z_den lowerCAmelCase_ : int = gcd(lowerCAmelCase__ , lowerCAmelCase__ ) top //= hcf bottom //= hcf return top, bottom def UpperCamelCase_ ( lowerCAmelCase__ : int = 35 ) -> int: """simple docstring""" lowerCAmelCase_ : set = set() lowerCAmelCase_ : int lowerCAmelCase_ : Fraction = Fraction(0 ) lowerCAmelCase_ : tuple[int, int] for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 lowerCAmelCase_ : str = x_num * y_den + x_den * y_num lowerCAmelCase_ : int = x_den * y_den lowerCAmelCase_ : int = gcd(lowerCAmelCase__ , lowerCAmelCase__ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowerCAmelCase_ : List[str] = add_three( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) unique_s.add(lowerCAmelCase__ ) # n=2 lowerCAmelCase_ : Optional[int] = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) lowerCAmelCase_ : Dict = x_den * x_den * y_den * y_den if is_sq(lowerCAmelCase__ ) and is_sq(lowerCAmelCase__ ): lowerCAmelCase_ : Optional[int] = int(sqrt(lowerCAmelCase__ ) ) lowerCAmelCase_ : List[str] = int(sqrt(lowerCAmelCase__ ) ) lowerCAmelCase_ : Union[str, Any] = gcd(lowerCAmelCase__ , lowerCAmelCase__ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowerCAmelCase_ : Union[str, Any] = add_three( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) unique_s.add(lowerCAmelCase__ ) # n=-1 lowerCAmelCase_ : Dict = x_num * y_num lowerCAmelCase_ : Optional[int] = x_den * y_num + x_num * y_den lowerCAmelCase_ : Any = gcd(lowerCAmelCase__ , lowerCAmelCase__ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowerCAmelCase_ : Tuple = add_three( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) unique_s.add(lowerCAmelCase__ ) # n=2 lowerCAmelCase_ : List[str] = x_num * x_num * y_num * y_num lowerCAmelCase_ : Optional[Any] = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(lowerCAmelCase__ ) and is_sq(lowerCAmelCase__ ): lowerCAmelCase_ : Tuple = int(sqrt(lowerCAmelCase__ ) ) lowerCAmelCase_ : Optional[Any] = int(sqrt(lowerCAmelCase__ ) ) lowerCAmelCase_ : Optional[int] = gcd(lowerCAmelCase__ , lowerCAmelCase__ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: lowerCAmelCase_ : Any = add_three( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) unique_s.add(lowerCAmelCase__ ) for num, den in unique_s: total += Fraction(lowerCAmelCase__ , lowerCAmelCase__ ) return total.denominator + total.numerator if __name__ == "__main__": print(f'{solution() = }')
289
1
'''simple docstring''' def _A ( snake_case = "The quick brown fox jumps over the lazy dog" , ) -> bool: _lowercase : int = set() # Replace all the whitespace in our sentence _lowercase : Dict = input_str.replace(" " , "" ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(snake_case ) == 26 def _A ( snake_case = "The quick brown fox jumps over the lazy dog" , ) -> bool: _lowercase : List[Any] = [False] * 26 for char in input_str: if char.islower(): _lowercase : Dict = True elif char.isupper(): _lowercase : Union[str, Any] = True return all(snake_case ) def _A ( snake_case = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def _A ( ) -> None: from timeit import timeit _lowercase : Optional[Any] = "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest" print(timeit("is_pangram()" , setup=snake_case ) ) print(timeit("is_pangram_faster()" , setup=snake_case ) ) print(timeit("is_pangram_fastest()" , setup=snake_case ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
250
'''simple docstring''' import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class a__ : def __init__( self , _UpperCamelCase , _UpperCamelCase=13 , _UpperCamelCase=10 , _UpperCamelCase=3 , _UpperCamelCase=2 , _UpperCamelCase=2 , _UpperCamelCase=2 , _UpperCamelCase=True , _UpperCamelCase=True , _UpperCamelCase=32 , _UpperCamelCase=5 , _UpperCamelCase=4 , _UpperCamelCase=37 , _UpperCamelCase="gelu" , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=10 , _UpperCamelCase=0.0_2 , _UpperCamelCase=0.9 , _UpperCamelCase=None , ): """simple docstring""" _lowercase : List[str] = parent _lowercase : Tuple = batch_size _lowercase : Tuple = image_size _lowercase : Any = num_channels _lowercase : Tuple = patch_size _lowercase : Union[str, Any] = tubelet_size _lowercase : str = num_frames _lowercase : Any = is_training _lowercase : Tuple = use_labels _lowercase : List[Any] = hidden_size _lowercase : int = num_hidden_layers _lowercase : Tuple = num_attention_heads _lowercase : int = intermediate_size _lowercase : Optional[int] = hidden_act _lowercase : Union[str, Any] = hidden_dropout_prob _lowercase : Optional[Any] = attention_probs_dropout_prob _lowercase : Optional[Any] = type_sequence_label_size _lowercase : Optional[Any] = initializer_range _lowercase : int = mask_ratio _lowercase : Union[str, Any] = scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame _lowercase : List[str] = (image_size // patch_size) ** 2 _lowercase : Optional[Any] = (num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos _lowercase : str = int(mask_ratio * self.seq_length ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Union[str, Any] = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) _lowercase : Tuple = None if self.use_labels: _lowercase : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowercase : Optional[Any] = self.get_config() return config, pixel_values, labels def _lowerCamelCase ( self ): """simple docstring""" return VideoMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_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 , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , ) def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : Dict = VideoMAEModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() _lowercase : Dict = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" _lowercase : Dict = VideoMAEForPreTraining(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch _lowercase : Optional[int] = torch.ones((self.num_masks,) ) _lowercase : List[Any] = torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) _lowercase : Tuple = mask.expand(self.batch_size , -1 ).bool() _lowercase : Tuple = model(_UpperCamelCase , _UpperCamelCase ) # model only returns predictions for masked patches _lowercase : Tuple = mask.sum().item() _lowercase : Optional[Any] = 3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels) ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : List[Any] = self.prepare_config_and_inputs() _lowercase , _lowercase , _lowercase : List[Any] = config_and_inputs _lowercase : str = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class a__ ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): _SCREAMING_SNAKE_CASE : List[str] = ( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) _SCREAMING_SNAKE_CASE : List[Any] = ( {'feature-extraction': VideoMAEModel, 'video-classification': VideoMAEForVideoClassification} if is_torch_available() else {} ) _SCREAMING_SNAKE_CASE : Tuple = False _SCREAMING_SNAKE_CASE : Dict = False _SCREAMING_SNAKE_CASE : Optional[int] = False _SCREAMING_SNAKE_CASE : Optional[Any] = False def _lowerCamelCase ( self ): """simple docstring""" _lowercase : int = VideoMAEModelTester(self ) _lowercase : Union[str, Any] = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase , hidden_size=37 ) def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase=False ): """simple docstring""" _lowercase : Any = copy.deepcopy(_UpperCamelCase ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch _lowercase : Union[str, Any] = torch.ones((self.model_tester.num_masks,) ) _lowercase : Dict = torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) _lowercase : List[str] = mask.expand(self.model_tester.batch_size , -1 ).bool() _lowercase : Any = bool_masked_pos.to(_UpperCamelCase ) if return_labels: if model_class in [ *get_values(_UpperCamelCase ), ]: _lowercase : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_UpperCamelCase ) return inputs_dict def _lowerCamelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="VideoMAE does not use inputs_embeds" ) def _lowerCamelCase ( self ): """simple docstring""" pass def _lowerCamelCase ( self ): """simple docstring""" _lowercase , _lowercase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowercase : List[str] = model_class(_UpperCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _lowercase : Any = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_UpperCamelCase , nn.Linear ) ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase , _lowercase : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowercase : List[Any] = model_class(_UpperCamelCase ) _lowercase : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowercase : str = [*signature.parameters.keys()] _lowercase : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def _lowerCamelCase ( self ): """simple docstring""" _lowercase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_UpperCamelCase ) @slow def _lowerCamelCase ( self ): """simple docstring""" for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowercase : int = VideoMAEModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def _lowerCamelCase ( self ): """simple docstring""" if not self.has_attentions: pass else: _lowercase , _lowercase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() _lowercase : Optional[int] = True for model_class in self.all_model_classes: _lowercase : List[str] = self.model_tester.seq_length - self.model_tester.num_masks _lowercase : List[Any] = ( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) _lowercase : int = True _lowercase : str = False _lowercase : Any = True _lowercase : Optional[Any] = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): _lowercase : Union[str, Any] = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) _lowercase : List[str] = outputs.attentions self.assertEqual(len(_UpperCamelCase ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _lowercase : Tuple = True _lowercase : Optional[Any] = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): _lowercase : Dict = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) _lowercase : Tuple = outputs.attentions self.assertEqual(len(_UpperCamelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) _lowercase : str = len(_UpperCamelCase ) # Check attention is always last and order is fine _lowercase : List[Any] = True _lowercase : List[str] = True _lowercase : Any = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): _lowercase : Optional[int] = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) self.assertEqual(out_len + 1 , len(_UpperCamelCase ) ) _lowercase : Optional[int] = outputs.attentions self.assertEqual(len(_UpperCamelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def _lowerCamelCase ( self ): """simple docstring""" def check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): _lowercase : Optional[int] = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): _lowercase : Tuple = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) _lowercase : List[str] = outputs.hidden_states _lowercase : List[Any] = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(_UpperCamelCase ) , _UpperCamelCase ) _lowercase : List[str] = self.model_tester.seq_length - self.model_tester.num_masks _lowercase : Optional[Any] = num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) _lowercase , _lowercase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowercase : Dict = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _lowercase : List[str] = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def _lowerCamelCase ( self ): """simple docstring""" pass def _A ( ) -> Any: _lowercase : Tuple = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) _lowercase : int = np.load(snake_case ) return list(snake_case ) @require_torch @require_vision class a__ ( unittest.TestCase ): @cached_property def _lowerCamelCase ( self ): """simple docstring""" return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Tuple = VideoMAEForVideoClassification.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics" ).to( _UpperCamelCase ) _lowercase : Dict = self.default_image_processor _lowercase : Optional[Any] = prepare_video() _lowercase : Union[str, Any] = image_processor(_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): _lowercase : str = model(**_UpperCamelCase ) # verify the logits _lowercase : List[Any] = torch.Size((1, 400) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) _lowercase : int = torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1E-4 ) ) @slow def _lowerCamelCase ( self ): """simple docstring""" _lowercase : Union[str, Any] = VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short" ).to(_UpperCamelCase ) _lowercase : Dict = self.default_image_processor _lowercase : Optional[Any] = prepare_video() _lowercase : List[Any] = image_processor(_UpperCamelCase , return_tensors="pt" ).to(_UpperCamelCase ) # add boolean mask, indicating which patches to mask _lowercase : int = hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" , filename="bool_masked_pos.pt" ) _lowercase : Any = torch.load(_UpperCamelCase ) # forward pass with torch.no_grad(): _lowercase : List[Any] = model(**_UpperCamelCase ) # verify the logits _lowercase : Dict = torch.Size([1, 1408, 1536] ) _lowercase : Tuple = torch.tensor( [[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] , device=_UpperCamelCase ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , _UpperCamelCase , atol=1E-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) _lowercase : Tuple = torch.tensor([0.5_1_4_2] , device=_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.loss , _UpperCamelCase , atol=1E-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) _lowercase : Dict = VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short" , norm_pix_loss=_UpperCamelCase ).to( _UpperCamelCase ) with torch.no_grad(): _lowercase : Optional[int] = model(**_UpperCamelCase ) _lowercase : List[str] = torch.tensor(torch.tensor([0.6_4_6_9] ) , device=_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.loss , _UpperCamelCase , atol=1E-4 ) )
250
1
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(snake_case_ ) , "Tatoeba directory does not exist." ) class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): @cached_property def UpperCAmelCase_ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" snake_case_ : Dict = tempfile.mkdtemp() return TatoebaConverter(save_dir=_A ) @slow def UpperCAmelCase_ ( self : Optional[Any] ) -> Any: """simple docstring""" self.resolver.convert_models(['heb-eng'] ) @slow def UpperCAmelCase_ ( self : str ) -> List[str]: """simple docstring""" snake_case_ ,snake_case_ : List[Any] = self.resolver.write_model_card('opus-mt-he-en' , dry_run=_A ) assert mmeta["long_pair"] == "heb-eng"
88
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _SCREAMING_SNAKE_CASE = { """configuration_mctct""": ["""MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MCTCTConfig"""], """feature_extraction_mctct""": ["""MCTCTFeatureExtractor"""], """processing_mctct""": ["""MCTCTProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ """MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST""", """MCTCTForCTC""", """MCTCTModel""", """MCTCTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
88
1
'''simple docstring''' import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class UpperCamelCase_ ( unittest.TestCase ): def _lowercase( self ) -> List[Any]: debug_launcher(test_script.main ) def _lowercase( self ) -> List[Any]: debug_launcher(test_ops.main )
265
'''simple docstring''' import unittest from parameterized import parameterized from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed 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, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXModel, ) class UpperCamelCase_ : def __init__( self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=64 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.0_2 , A=3 , A=4 , A=None , ) -> Optional[int]: UpperCAmelCase : List[Any] = parent UpperCAmelCase : Optional[int] = batch_size UpperCAmelCase : Union[str, Any] = seq_length UpperCAmelCase : Optional[Any] = is_training UpperCAmelCase : Dict = use_input_mask UpperCAmelCase : str = use_token_type_ids UpperCAmelCase : List[Any] = use_labels UpperCAmelCase : List[Any] = vocab_size UpperCAmelCase : Dict = hidden_size UpperCAmelCase : Dict = num_hidden_layers UpperCAmelCase : Optional[int] = num_attention_heads UpperCAmelCase : int = intermediate_size UpperCAmelCase : List[str] = hidden_act UpperCAmelCase : List[str] = hidden_dropout_prob UpperCAmelCase : int = attention_probs_dropout_prob UpperCAmelCase : str = max_position_embeddings UpperCAmelCase : Optional[Any] = type_vocab_size UpperCAmelCase : List[str] = type_sequence_label_size UpperCAmelCase : int = initializer_range UpperCAmelCase : str = num_labels UpperCAmelCase : Optional[int] = num_choices UpperCAmelCase : Dict = scope UpperCAmelCase : Union[str, Any] = vocab_size - 1 def _lowercase( self ) -> Union[str, Any]: UpperCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Any = None if self.use_input_mask: UpperCAmelCase : int = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase : List[str] = None if self.use_labels: UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase : Optional[int] = self.get_config() return config, input_ids, input_mask, token_labels def _lowercase( self ) -> Optional[Any]: return GPTNeoXConfig( 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 , pad_token_id=self.pad_token_id , ) def _lowercase( self ) -> Optional[Any]: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[str] = self.prepare_config_and_inputs() UpperCAmelCase : Any = True return config, input_ids, input_mask, token_labels def _lowercase( self , A , A , A ) -> int: UpperCAmelCase : str = GPTNeoXModel(config=A ) model.to(A ) model.eval() UpperCAmelCase : List[str] = model(A , attention_mask=A ) UpperCAmelCase : List[str] = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase( self , A , A , A ) -> Optional[int]: UpperCAmelCase : str = True UpperCAmelCase : Optional[Any] = GPTNeoXModel(A ) model.to(A ) model.eval() UpperCAmelCase : List[Any] = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase( self , A , A , A , A ) -> List[str]: UpperCAmelCase : Tuple = GPTNeoXForCausalLM(config=A ) model.to(A ) model.eval() UpperCAmelCase : str = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase( self , A , A , A , A ) -> Tuple: UpperCAmelCase : List[str] = self.num_labels UpperCAmelCase : Any = GPTNeoXForQuestionAnswering(A ) model.to(A ) model.eval() UpperCAmelCase : str = 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) ) def _lowercase( self , A , A , A , A ) -> int: UpperCAmelCase : Tuple = self.num_labels UpperCAmelCase : List[str] = GPTNeoXForSequenceClassification(A ) model.to(A ) model.eval() UpperCAmelCase : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase : Tuple = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowercase( self , A , A , A , A ) -> str: UpperCAmelCase : List[Any] = self.num_labels UpperCAmelCase : Tuple = GPTNeoXForTokenClassification(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 _lowercase( self , A , A , A ) -> Union[str, Any]: UpperCAmelCase : Optional[int] = True UpperCAmelCase : str = GPTNeoXForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass UpperCAmelCase : List[str] = model(A , attention_mask=A , use_cache=A ) UpperCAmelCase : List[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCAmelCase : Optional[int] = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : Any = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCAmelCase : str = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCAmelCase : Any = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCAmelCase : Dict = model(A , attention_mask=A , output_hidden_states=A ) UpperCAmelCase : Any = output_from_no_past["""hidden_states"""][0] UpperCAmelCase : List[str] = model( A , attention_mask=A , past_key_values=A , output_hidden_states=A , )["""hidden_states"""][0] # select random slice UpperCAmelCase : Tuple = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCAmelCase : List[Any] = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCAmelCase : List[str] = 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 _lowercase( self ) -> int: UpperCAmelCase : Tuple = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Optional[Any] = config_and_inputs UpperCAmelCase : Union[str, Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class UpperCamelCase_ ( __magic_name__ , __magic_name__ , __magic_name__ , unittest.TestCase ): lowercase = ( ( GPTNeoXModel, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, ) if is_torch_available() else () ) lowercase = (GPTNeoXForCausalLM,) if is_torch_available() else () lowercase = ( { 'feature-extraction': GPTNeoXModel, 'question-answering': GPTNeoXForQuestionAnswering, 'text-classification': GPTNeoXForSequenceClassification, 'text-generation': GPTNeoXForCausalLM, 'token-classification': GPTNeoXForTokenClassification, 'zero-shot': GPTNeoXForSequenceClassification, } if is_torch_available() else {} ) lowercase = False lowercase = False lowercase = False lowercase = False def _lowercase( self ) -> Union[str, Any]: UpperCAmelCase : str = GPTNeoXModelTester(self ) UpperCAmelCase : Optional[Any] = ConfigTester(self , config_class=A , hidden_size=64 , num_attention_heads=8 ) def _lowercase( self ) -> Optional[Any]: self.config_tester.run_common_tests() def _lowercase( self ) -> str: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(A , A , A ) def _lowercase( self ) -> str: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(A , A , A ) def _lowercase( self ) -> Optional[Any]: # This regression test was failing with PyTorch < 1.3 UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_decoder() UpperCAmelCase : Optional[Any] = None self.model_tester.create_and_check_model_as_decoder(A , A , A ) def _lowercase( self ) -> str: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(A , A , A ) def _lowercase( self ) -> int: UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*A ) def _lowercase( self ) -> Optional[int]: UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A ) def _lowercase( self ) -> Any: UpperCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A ) def _lowercase( self ) -> Optional[Any]: UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A ) @unittest.skip(reason="""Feed forward chunking is not implemented""" ) def _lowercase( self ) -> Optional[int]: pass @parameterized.expand([("""linear""",), ("""dynamic""",)] ) def _lowercase( self , A ) -> str: UpperCAmelCase , UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase : int = ids_tensor([1, 10] , config.vocab_size ) UpperCAmelCase : Optional[Any] = 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 UpperCAmelCase : Dict = GPTNeoXModel(A ) original_model.to(A ) original_model.eval() UpperCAmelCase : List[str] = original_model(A ).last_hidden_state UpperCAmelCase : Any = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights UpperCAmelCase : Any = {"""type""": scaling_type, """factor""": 1_0.0} UpperCAmelCase : str = GPTNeoXModel(A ) scaled_model.to(A ) scaled_model.eval() UpperCAmelCase : Optional[Any] = scaled_model(A ).last_hidden_state UpperCAmelCase : Optional[Any] = 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 ) ) @require_torch class UpperCamelCase_ ( unittest.TestCase ): @slow def _lowercase( self ) -> List[Any]: UpperCAmelCase : str = AutoTokenizer.from_pretrained("""EleutherAI/pythia-410m-deduped""" ) for checkpointing in [True, False]: UpperCAmelCase : int = GPTNeoXForCausalLM.from_pretrained("""EleutherAI/pythia-410m-deduped""" ) if checkpointing: model.gradient_checkpointing_enable() else: model.gradient_checkpointing_disable() model.to(A ) UpperCAmelCase : List[Any] = tokenizer("""My favorite food is""" , return_tensors="""pt""" ).to(A ) # The hub repo. is updated on 2023-04-04, resulting in poor outputs. # See: https://github.com/huggingface/transformers/pull/24193 UpperCAmelCase : List[str] = """My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI'm not sure""" UpperCAmelCase : Union[str, Any] = model.generate(**A , do_sample=A , max_new_tokens=20 ) UpperCAmelCase : Tuple = tokenizer.batch_decode(A )[0] self.assertEqual(A , A )
265
1
"""simple docstring""" import argparse import os import torch from transformers import FlavaConfig, FlavaForPreTraining from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint def _A ( UpperCamelCase_ : Dict) -> Optional[int]: '''simple docstring''' return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items()) def _A ( UpperCamelCase_ : Optional[int], UpperCamelCase_ : List[str]) -> Dict: '''simple docstring''' __lowercase = {} for key, value in state_dict.items(): if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key: continue __lowercase = key.replace("heads.cmd.mim_head.cls.predictions", "mmm_image_head") __lowercase = key.replace("heads.cmd.mlm_head.cls.predictions", "mmm_text_head") __lowercase = key.replace("heads.cmd.itm_head.cls", "itm_head") __lowercase = key.replace("heads.cmd.itm_head.pooler", "itm_head.pooler") __lowercase = key.replace("heads.cmd.clip_head.logit_scale", "flava.logit_scale") __lowercase = key.replace("heads.fairseq_mlm.cls.predictions", "mlm_head") __lowercase = key.replace("heads.imagenet.mim_head.cls.predictions", "mim_head") __lowercase = key.replace("mm_text_projection", "flava.text_to_mm_projection") __lowercase = key.replace("mm_image_projection", "flava.image_to_mm_projection") __lowercase = key.replace("image_encoder.module", "flava.image_model") __lowercase = key.replace("text_encoder.module", "flava.text_model") __lowercase = key.replace("mm_encoder.module.encoder.cls_token", "flava.multimodal_model.cls_token") __lowercase = key.replace("mm_encoder.module", "flava.multimodal_model") __lowercase = key.replace("text_projection", "flava.text_projection") __lowercase = key.replace("image_projection", "flava.image_projection") __lowercase = value.float() for key, value in codebook_state_dict.items(): __lowercase = value return upgrade @torch.no_grad() def _A ( UpperCamelCase_ : str, UpperCamelCase_ : List[str], UpperCamelCase_ : List[Any], UpperCamelCase_ : Any=None) -> Dict: '''simple docstring''' if config_path is not None: __lowercase = FlavaConfig.from_pretrained(UpperCamelCase_) else: __lowercase = FlavaConfig() __lowercase = FlavaForPreTraining(UpperCamelCase_).eval() __lowercase = convert_dalle_checkpoint(UpperCamelCase_, UpperCamelCase_, save_checkpoint=UpperCamelCase_) if os.path.exists(UpperCamelCase_): __lowercase = torch.load(UpperCamelCase_, map_location="cpu") else: __lowercase = torch.hub.load_state_dict_from_url(UpperCamelCase_, map_location="cpu") __lowercase = upgrade_state_dict(UpperCamelCase_, UpperCamelCase_) hf_model.load_state_dict(UpperCamelCase_) __lowercase = hf_model.state_dict() __lowercase = count_parameters(UpperCamelCase_) __lowercase = count_parameters(UpperCamelCase_) + count_parameters(UpperCamelCase_) assert torch.allclose(UpperCamelCase_, UpperCamelCase_, atol=1E-3) hf_model.save_pretrained(UpperCamelCase_) if __name__ == "__main__": _a = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint') parser.add_argument('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') _a = parser.parse_args() convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)
144
"""simple docstring""" from ...utils import is_note_seq_available, is_transformers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable 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 .notes_encoder import SpectrogramNotesEncoder from .continous_encoder import SpectrogramContEncoder from .pipeline_spectrogram_diffusion import ( SpectrogramContEncoder, SpectrogramDiffusionPipeline, TaFilmDecoder, ) try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .midi_utils import MidiProcessor
144
1